@margoengine/wfc 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +298 -2
- package/dist/assemble.d.ts +38 -0
- package/dist/assemble.js +112 -0
- package/dist/chunks.d.ts +26 -0
- package/dist/chunks.js +309 -0
- package/dist/constraints.d.ts +51 -0
- package/dist/constraints.js +193 -0
- package/dist/graph-BI3KRQ6B.js +113 -0
- package/dist/graph.d.ts +35 -0
- package/dist/grid-BGBmzG3D.js +33 -0
- package/dist/grid.d.ts +13 -0
- package/dist/heap.d.ts +41 -0
- package/dist/hex.d.ts +65 -0
- package/dist/hex.js +130 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +145 -0
- package/dist/overlapping.d.ts +59 -0
- package/dist/overlapping.js +147 -0
- package/dist/overlapping3d.d.ts +43 -0
- package/dist/overlapping3d.js +186 -0
- package/dist/sockets.d.ts +21 -0
- package/dist/sockets.js +92 -0
- package/dist/tileset.d.ts +31 -0
- package/dist/tileset.js +169 -0
- package/dist/wfc.wasm +0 -0
- package/dist/wfc.worker.d.ts +1 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +82 -0
- package/dist/workers/wfc.js +1 -0
- package/package.json +84 -4
package/dist/assemble.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const e=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { engineRuntime as __margoRuntime, meshRuntime as t } from "@margoengine/core/extension";
|
|
5
|
+
import { Quaternion as n } from "@margoengine/math";
|
|
6
|
+
//#region src/assemble.ts
|
|
7
|
+
var r = class {
|
|
8
|
+
constructor(e, t, n) {
|
|
9
|
+
this.root = e, this.cells = t, this.meshes = n;
|
|
10
|
+
}
|
|
11
|
+
dispose() {
|
|
12
|
+
for (let e = this.meshes.length - 1; e >= 0; e--) this.meshes[e].dispose();
|
|
13
|
+
for (let e = this.cells.length - 1; e >= 0; e--) this.cells[e].dispose();
|
|
14
|
+
this.root.dispose();
|
|
15
|
+
}
|
|
16
|
+
[Symbol.dispose]() {
|
|
17
|
+
this.dispose();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function i(i, a, o, s, c, { spacing: l = [
|
|
21
|
+
1,
|
|
22
|
+
1,
|
|
23
|
+
1
|
|
24
|
+
], origin: u = [
|
|
25
|
+
0,
|
|
26
|
+
0,
|
|
27
|
+
0
|
|
28
|
+
], parent: d, signal: f } = {}) {
|
|
29
|
+
f?.throwIfAborted();
|
|
30
|
+
let p = (e) => e.length === 3 && Array.from(e).every((e) => Number.isFinite(e) && Number.isFinite(Math.fround(e)));
|
|
31
|
+
if (!p(a) || a.some((e) => !Number.isSafeInteger(e) || e < 1) || a.reduce((e, t) => e * t, 1) > 1e6 || o.length !== a[0] * a[1] * a[2] || !p(l) || l.some((e) => e <= 0) || !p(u)) throw RangeError("Invalid WFC assembly grid or placement; provide three positive dimensions and finite spacing/origin");
|
|
32
|
+
let m = e(i);
|
|
33
|
+
if (d?.check(), d && t(d).engine !== m) throw RangeError("WFC assembly parent belongs to another engine; use this engine parent");
|
|
34
|
+
let h = c.map((e) => ({
|
|
35
|
+
root: t(e.root),
|
|
36
|
+
meshes: e.meshes.map(t)
|
|
37
|
+
})), g = h.map((e) => {
|
|
38
|
+
if (e.root.check(), e.root.engine !== m) throw RangeError("WFC tile model belongs to another engine; load it with this engine");
|
|
39
|
+
let t = /* @__PURE__ */ new Set([e.root]);
|
|
40
|
+
for (let n of e.meshes) {
|
|
41
|
+
if (n.check(), n.engine !== m) throw RangeError("WFC primitive belongs to another engine; load it with this engine");
|
|
42
|
+
let r = n;
|
|
43
|
+
for (; r !== e.root;) {
|
|
44
|
+
if (!r) throw RangeError("WFC primitive is outside its model root; provide a complete model");
|
|
45
|
+
t.add(r), r = m.meshes.get(m.store.parents[r.id]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return t;
|
|
49
|
+
}), _ = Array.from(s, (e) => {
|
|
50
|
+
if (!e || !Number.isInteger(e.source) || !g[e.source]) throw RangeError("Invalid WFC assembly source; provide an existing model index");
|
|
51
|
+
let t = Array.from(e.rotation);
|
|
52
|
+
if (t.length !== 9 || t.some((e) => !Number.isFinite(e))) throw RangeError("Invalid WFC orientation; provide nine finite matrix values");
|
|
53
|
+
for (let e = 0; e < 3; e++) for (let n = 0; n < 3; n++) if (Math.abs(t[e * 3] * t[n * 3] + t[e * 3 + 1] * t[n * 3 + 1] + t[e * 3 + 2] * t[n * 3 + 2] - +(e === n)) > 1e-8) throw RangeError("WFC orientation must be orthogonal; remove scale and shear");
|
|
54
|
+
let r = t[0] * (t[4] * t[8] - t[5] * t[7]) - t[3] * (t[1] * t[8] - t[2] * t[7]) + t[6] * (t[1] * t[5] - t[2] * t[4]) < 0 ? -1 : 1;
|
|
55
|
+
for (let e = 0; e < 3; e++) t[e] *= r;
|
|
56
|
+
let i = new n().setFromRotationMatrix({ elements: t });
|
|
57
|
+
return {
|
|
58
|
+
source: e.source,
|
|
59
|
+
sign: r,
|
|
60
|
+
q: [
|
|
61
|
+
i.x,
|
|
62
|
+
i.y,
|
|
63
|
+
i.z,
|
|
64
|
+
i.w
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
for (let e = 0; e < o.length; e++) if (!Number.isInteger(o[e]) || !_[o[e]]) throw RangeError("Invalid WFC assembly tile ID; use an existing variant index");
|
|
69
|
+
let v = m.createMesh(d ? t(d) : null), y = [], b = [], x = new r(v, y, b);
|
|
70
|
+
try {
|
|
71
|
+
for (let e = 0; e < o.length; e++) {
|
|
72
|
+
f?.throwIfAborted();
|
|
73
|
+
let t = _[o[e]], n = m.createMesh(v);
|
|
74
|
+
y.push(n), n.setPosition([
|
|
75
|
+
u[0] + e % a[0] * l[0],
|
|
76
|
+
u[1] + Math.floor(e / a[0]) % a[1] * l[1],
|
|
77
|
+
u[2] + Math.floor(e / (a[0] * a[1])) * l[2]
|
|
78
|
+
]), n.setQuaternion(t.q).setScale([
|
|
79
|
+
t.sign,
|
|
80
|
+
1,
|
|
81
|
+
1
|
|
82
|
+
]);
|
|
83
|
+
let r = /* @__PURE__ */ new Map(), i = h[t.source], s = (e) => {
|
|
84
|
+
let t = r.get(e);
|
|
85
|
+
if (t) return t;
|
|
86
|
+
let a = e === i.root ? n : s(m.meshes.get(m.store.parents[e.id]));
|
|
87
|
+
t = m.createMesh(a), b.push(t), r.set(e, t), t.name = e.name, t.setPosition([
|
|
88
|
+
e.position.x,
|
|
89
|
+
e.position.y,
|
|
90
|
+
e.position.z
|
|
91
|
+
]);
|
|
92
|
+
let o = m.store.transforms;
|
|
93
|
+
return t.setQuaternion([
|
|
94
|
+
o.qx[e.id],
|
|
95
|
+
o.qy[e.id],
|
|
96
|
+
o.qz[e.id],
|
|
97
|
+
o.qw[e.id]
|
|
98
|
+
]), t.setScale([
|
|
99
|
+
e.scale.x,
|
|
100
|
+
e.scale.y,
|
|
101
|
+
e.scale.z
|
|
102
|
+
]), t.setGeometry(e.geometry).setMaterial(e.material), t.visible = e === i.root || e.visible, t.receiveShadow = e.receiveShadow, t;
|
|
103
|
+
};
|
|
104
|
+
for (let e of g[t.source]) s(e);
|
|
105
|
+
}
|
|
106
|
+
return x;
|
|
107
|
+
} catch (e) {
|
|
108
|
+
throw x.dispose(), e;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { r as TileAssembly, i as assembleWFC };
|
package/dist/chunks.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HeapWFC } from './heap.js';
|
|
2
|
+
import type { HeapBackendOptions, HeapSolveOptions } from './heap.js';
|
|
3
|
+
import type { Tile } from './index.js';
|
|
4
|
+
import type { CellPosition } from './constraints.js';
|
|
5
|
+
export { HeapWFC };
|
|
6
|
+
export type { HeapBackendOptions, HeapSolveOptions };
|
|
7
|
+
/** Chunked local WFC. Previously committed neighbour cells constrain a one-cell halo. */
|
|
8
|
+
export declare class ChunkedWFC {
|
|
9
|
+
private readonly seed;
|
|
10
|
+
private readonly dimensions;
|
|
11
|
+
private readonly tiles;
|
|
12
|
+
private readonly chunks;
|
|
13
|
+
private busy;
|
|
14
|
+
/** Construct a world with fixed chunk dimensions and rules. Seed defaults to 1. */
|
|
15
|
+
constructor(dimensions: CellPosition, tiles: readonly Tile[], seed?: number);
|
|
16
|
+
/** Number of retained chunks. Memory grows with visited coordinates until clear/dispose. */
|
|
17
|
+
get size(): number;
|
|
18
|
+
/** Generate or return a copy of a chunk. Serial calls commit atomically; concurrent calls are rejected. */
|
|
19
|
+
solveChunk(coordinate: CellPosition, options?: HeapBackendOptions & Pick<HeapSolveOptions, 'attempts'>): Promise<Uint16Array>;
|
|
20
|
+
/** Release saved chunks. Solve order affects new boundaries after clearing. */
|
|
21
|
+
clear(): void;
|
|
22
|
+
/** Release retained chunk outputs. */
|
|
23
|
+
dispose(): void;
|
|
24
|
+
/** Explicit resource management. */
|
|
25
|
+
[Symbol.dispose](): void;
|
|
26
|
+
}
|
package/dist/chunks.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { t as e } from "./grid-BGBmzG3D.js";
|
|
2
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/heap.ts
|
|
4
|
+
var n = "Help: https://margoengine.com/llms/packages/wfc.md", r = [
|
|
5
|
+
[
|
|
6
|
+
1,
|
|
7
|
+
0,
|
|
8
|
+
0
|
|
9
|
+
],
|
|
10
|
+
[
|
|
11
|
+
-1,
|
|
12
|
+
0,
|
|
13
|
+
0
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
0,
|
|
17
|
+
1,
|
|
18
|
+
0
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
0,
|
|
22
|
+
-1,
|
|
23
|
+
0
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
0,
|
|
27
|
+
0,
|
|
28
|
+
1
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
-1
|
|
34
|
+
]
|
|
35
|
+
], i = (e) => (e = Math.imul(e ^ e >>> 16, 2146121005), e = Math.imul(e ^ e >>> 15, 2221713035), (e ^ e >>> 16) >>> 0), a = class {
|
|
36
|
+
report(e, t, n, r) {}
|
|
37
|
+
static wasmURL() {
|
|
38
|
+
return new URL("./wfc.wasm?no-inline",
|
|
39
|
+
import.meta.url
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
constructor(t, n) {
|
|
43
|
+
this.stats = {
|
|
44
|
+
decisions: 0,
|
|
45
|
+
updates: 0,
|
|
46
|
+
intersections: 0,
|
|
47
|
+
attempts: 0
|
|
48
|
+
}, this.backend = "typescript";
|
|
49
|
+
let r = e(t, n);
|
|
50
|
+
this.dims = r.dimensions, this.tiles = r.tiles, this.weights = this.tiles.map((e) => e.weight), this.equal = this.weights.every((e) => e === this.weights[0]);
|
|
51
|
+
}
|
|
52
|
+
static async create(e, r, i = {}) {
|
|
53
|
+
let a = new this(e, r), { backend: o = "auto", signal: s } = i;
|
|
54
|
+
if (s?.throwIfAborted(), ![
|
|
55
|
+
"auto",
|
|
56
|
+
"typescript",
|
|
57
|
+
"wasm"
|
|
58
|
+
].includes(o)) throw RangeError("Invalid WFC backend; use auto, typescript or wasm");
|
|
59
|
+
let c = a.weights.every((e) => e === 1) && r.length <= 32;
|
|
60
|
+
if (o === "wasm" && !c) throw new t("unsupported", `WFC SIMD needs at most 32 unit-weight tiles; use typescript or auto. ${n}`);
|
|
61
|
+
if (o === "typescript" || !c) return a;
|
|
62
|
+
let l;
|
|
63
|
+
try {
|
|
64
|
+
let e = this.wasmURL();
|
|
65
|
+
if (e.protocol === "file:") {
|
|
66
|
+
let { readFile: t } = await import(
|
|
67
|
+
/* @vite-ignore */
|
|
68
|
+
"node:fs/promises"
|
|
69
|
+
);
|
|
70
|
+
l = await t(e, { signal: s });
|
|
71
|
+
} else {
|
|
72
|
+
let r = await fetch(e, { signal: s });
|
|
73
|
+
if (!r.ok) throw new t("failed", `WFC SIMD download failed (${r.status}); check the asset URL. ${n}`);
|
|
74
|
+
l = await r.arrayBuffer();
|
|
75
|
+
}
|
|
76
|
+
if (s?.throwIfAborted(), !WebAssembly.validate(l)) {
|
|
77
|
+
if (o === "auto") return a;
|
|
78
|
+
throw new t("unsupported", `WFC SIMD unavailable; use the typescript backend. ${n}`);
|
|
79
|
+
}
|
|
80
|
+
let r = await WebAssembly.instantiate(l, { progress: { report: (e, t, n, r) => a.report(e >>> 0, t >>> 0, n >>> 0, r >>> 0) } });
|
|
81
|
+
s?.throwIfAborted(), a.wasm = r.instance.exports;
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw s?.throwIfAborted(), e instanceof t ? e : Object.assign(new t("failed", `WFC SIMD loading failed; check asset hosting and availability. ${n}`), { cause: e });
|
|
84
|
+
}
|
|
85
|
+
return a.backend = "wasm", a;
|
|
86
|
+
}
|
|
87
|
+
solve({ seed: e = 1, pins: a = [], attempts: o = 4, signal: s } = {}) {
|
|
88
|
+
let [c, l, u] = this.dims, d = c * l * u, f = this.tiles.length, p = Math.ceil(f / 32);
|
|
89
|
+
if (!Number.isSafeInteger(e) || !Number.isInteger(o) || o < 1 || o > 64) throw RangeError("Invalid heap WFC budget; use an integer seed and 1..64 attempts");
|
|
90
|
+
let m = Array.from(a, (e) => {
|
|
91
|
+
if (e.position.length !== 3 || Array.from(e.position).some((e, t) => !Number.isInteger(e) || e < 0 || e >= this.dims[t]) || !Number.isInteger(e.tile) || e.tile < 0 || e.tile >= f) throw RangeError("Invalid heap WFC pin; use integer coordinates inside the grid and an existing tile");
|
|
92
|
+
return {
|
|
93
|
+
cell: e.position[0] + c * (e.position[1] + l * e.position[2]),
|
|
94
|
+
tile: e.tile
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
if (s?.throwIfAborted(), Object.assign(this.stats, {
|
|
98
|
+
decisions: 0,
|
|
99
|
+
updates: 0,
|
|
100
|
+
intersections: 0,
|
|
101
|
+
attempts: 0
|
|
102
|
+
}), this.wasm) {
|
|
103
|
+
let r = 12 + 8 * f + 2 * m.length + d, i = this.wasm.alloc(r);
|
|
104
|
+
try {
|
|
105
|
+
let t = new Uint32Array(this.wasm.memory.buffer, i, r);
|
|
106
|
+
t.set([
|
|
107
|
+
c,
|
|
108
|
+
l,
|
|
109
|
+
u,
|
|
110
|
+
f,
|
|
111
|
+
e >>> 0 || 1,
|
|
112
|
+
o,
|
|
113
|
+
m.length,
|
|
114
|
+
0,
|
|
115
|
+
0,
|
|
116
|
+
0,
|
|
117
|
+
0,
|
|
118
|
+
e >>> 0
|
|
119
|
+
]);
|
|
120
|
+
for (let e = 0; e < f; e++) for (let n = 0; n < 6; n++) for (let r of this.tiles[e].neighbors[n]) t[12 + e * 8 + n] |= 1 << r;
|
|
121
|
+
m.forEach((e, n) => t.set([e.cell, e.tile], 12 + 8 * f + 2 * n));
|
|
122
|
+
let n = this.wasm.solve(i), a = new Uint32Array(this.wasm.memory.buffer, i, r);
|
|
123
|
+
if (Object.assign(this.stats, {
|
|
124
|
+
decisions: a[7],
|
|
125
|
+
updates: a[8],
|
|
126
|
+
intersections: a[9],
|
|
127
|
+
attempts: a[10]
|
|
128
|
+
}), s?.throwIfAborted(), n) return Uint16Array.from(a.subarray(r - d));
|
|
129
|
+
} finally {
|
|
130
|
+
this.wasm.release(i, r);
|
|
131
|
+
}
|
|
132
|
+
throw new t("failed", `Heap WFC exhausted attempts; check pins/rules or raise attempts. ${n}`);
|
|
133
|
+
}
|
|
134
|
+
let h = new Uint32Array(f * 6 * p), g = new Uint32Array(d * p), _ = new Uint16Array(d), v = new Float64Array(d), y = Uint32Array.from({ length: d }, (t, n) => i(n ^ e >>> 0)), b = new Int32Array(d), x = [], S = [], C = new Uint8Array(d), w = new Uint32Array(6 * p), T = 0, E = () => this.report(this.stats.decisions, this.stats.updates, this.stats.intersections, this.stats.attempts);
|
|
135
|
+
for (let e = 0; e < f; e++) for (let t = 0; t < 6; t++) for (let n of this.tiles[e].neighbors[t]) h[(e * 6 + t) * p + (n >>> 5)] |= 1 << (n & 31);
|
|
136
|
+
let D = new Uint32Array(6 * p);
|
|
137
|
+
for (let e = 0; e < f; e++) for (let t = 0; t < 6 * p; t++) D[t] |= h[e * 6 * p + t];
|
|
138
|
+
let O = (e, t) => v[e] < v[t] || v[e] === v[t] && (y[e] < y[t] || y[e] === y[t] && e < t), k = (e, t) => {
|
|
139
|
+
let n = x[e];
|
|
140
|
+
x[e] = x[t], x[t] = n, b[x[e]] = e, b[x[t]] = t;
|
|
141
|
+
}, A = (e) => {
|
|
142
|
+
this.stats.updates++;
|
|
143
|
+
let t = _[e] > 1 ? _[e] : Infinity;
|
|
144
|
+
if (!this.equal && _[e] > 1) {
|
|
145
|
+
let n = 0, r = 0;
|
|
146
|
+
for (let t = 0; t < f; t++) g[e * p + (t >>> 5)] & 1 << (t & 31) && (n += this.weights[t], r += this.weights[t] * Math.log(this.weights[t]));
|
|
147
|
+
t = Math.log(n) - r / n;
|
|
148
|
+
}
|
|
149
|
+
v[e] = t;
|
|
150
|
+
let n = b[e];
|
|
151
|
+
for (; n > 0;) {
|
|
152
|
+
let e = n - 1 >>> 1;
|
|
153
|
+
if (!O(x[n], x[e])) break;
|
|
154
|
+
k(n, e), n = e;
|
|
155
|
+
}
|
|
156
|
+
for (;;) {
|
|
157
|
+
let e = n * 2 + 1;
|
|
158
|
+
if (e >= d || (e + 1 < d && O(x[e + 1], x[e]) && e++, !O(x[e], x[n]))) break;
|
|
159
|
+
k(n, e), n = e;
|
|
160
|
+
}
|
|
161
|
+
}, j = (e, t) => {
|
|
162
|
+
for (let n = 0; n < p; n++) {
|
|
163
|
+
let r = g[e * p + n];
|
|
164
|
+
for (; r;) {
|
|
165
|
+
let e = r & -r;
|
|
166
|
+
t(n * 32 + 31 - Math.clz32(e)), r = (r ^ e) >>> 0;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}, M = (e) => {
|
|
170
|
+
C[e] || (C[e] = 1, S.push(e));
|
|
171
|
+
}, N = e >>> 0 || 1, P = () => (N ^= N << 13, N ^= N >>> 17, N ^= N << 5, (N >>> 0) / 4294967296);
|
|
172
|
+
for (let e = 1; e <= o; e++) {
|
|
173
|
+
this.stats.attempts = e, S.length = 0, C.fill(0), _.fill(f), x.length = 0;
|
|
174
|
+
for (let e = 0; e < d; e++) {
|
|
175
|
+
x.push(e), b[e] = e, v[e] = Infinity;
|
|
176
|
+
for (let t = 0; t < p; t++) g[e * p + t] = t === p - 1 && f % 32 ? 2 ** (f % 32) - 1 : 4294967295;
|
|
177
|
+
}
|
|
178
|
+
for (let e = 0; e < d; e++) A(e), M(e);
|
|
179
|
+
let t = !1;
|
|
180
|
+
for (let e of m) {
|
|
181
|
+
if (!(g[e.cell * p + (e.tile >>> 5)] & 1 << (e.tile & 31))) {
|
|
182
|
+
t = !0;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
g.fill(0, e.cell * p, (e.cell + 1) * p), g[e.cell * p + (e.tile >>> 5)] = 1 << (e.tile & 31), _[e.cell] = 1, A(e.cell);
|
|
186
|
+
}
|
|
187
|
+
for (;;) {
|
|
188
|
+
for (; S.length && !t;) {
|
|
189
|
+
s?.throwIfAborted(), ++T % 1024 == 0 && E();
|
|
190
|
+
let e = S.pop();
|
|
191
|
+
C[e] = 0, _[e] === f ? w.set(D) : (w.fill(0), j(e, (e) => {
|
|
192
|
+
for (let t = 0; t < 6 * p; t++) w[t] |= h[e * 6 * p + t];
|
|
193
|
+
}));
|
|
194
|
+
for (let n = 0; n < 6; n++) {
|
|
195
|
+
let i = e % c + r[n][0], a = Math.floor(e / c) % l + r[n][1], o = Math.floor(e / (c * l)) + r[n][2];
|
|
196
|
+
if (i < 0 || i >= c || a < 0 || a >= l || o < 0 || o >= u) continue;
|
|
197
|
+
let s = i + c * (a + l * o), d = !1, f = 0;
|
|
198
|
+
for (let e = 0; e < p; e++) {
|
|
199
|
+
let t = s * p + e, r = g[t], i = (r & w[n * p + e]) >>> 0;
|
|
200
|
+
this.stats.intersections++, i !== r && (g[t] = i, d = !0);
|
|
201
|
+
let a = i;
|
|
202
|
+
for (; a;) f++, a = (a & a - 1) >>> 0;
|
|
203
|
+
}
|
|
204
|
+
if (d && (_[s] = f, A(s), M(s), !f)) {
|
|
205
|
+
t = !0;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (t) break;
|
|
211
|
+
let e = x[0];
|
|
212
|
+
if (_[e] <= 1) {
|
|
213
|
+
let e = new Uint16Array(d);
|
|
214
|
+
for (let t = 0; t < d; t++) j(t, (n) => {
|
|
215
|
+
e[t] = n;
|
|
216
|
+
});
|
|
217
|
+
return e;
|
|
218
|
+
}
|
|
219
|
+
let n = 0;
|
|
220
|
+
j(e, (e) => {
|
|
221
|
+
n += this.weights[e];
|
|
222
|
+
});
|
|
223
|
+
let i = P() * n, a = -1;
|
|
224
|
+
j(e, (e) => {
|
|
225
|
+
a < 0 && (i -= this.weights[e], i <= 0 && (a = e));
|
|
226
|
+
}), a < 0 && j(e, (e) => {
|
|
227
|
+
a = e;
|
|
228
|
+
}), g.fill(0, e * p, (e + 1) * p), g[e * p + (a >>> 5)] = 1 << (a & 31), _[e] = 1, A(e), M(e), this.stats.decisions++, this.stats.decisions % 256 == 0 && E();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
throw new t("failed", `Heap WFC exhausted attempts; check pins/rules or raise attempts. ${n}`);
|
|
232
|
+
}
|
|
233
|
+
}, o = class {
|
|
234
|
+
constructor(e, t, n = 1) {
|
|
235
|
+
if (this.seed = n, this.chunks = /* @__PURE__ */ new Map(), this.busy = !1, !Number.isSafeInteger(n)) throw RangeError("Invalid WFC world seed; use a safe integer");
|
|
236
|
+
if (e.length !== 3 || Array.from(e).some((e) => !Number.isInteger(e) || e < 1)) throw RangeError("Invalid WFC chunk; use positive integer dimensions");
|
|
237
|
+
new a(e.map((e) => e + 2), t), this.dimensions = [...e], this.tiles = t.map((e) => ({
|
|
238
|
+
weight: e.weight,
|
|
239
|
+
neighbors: e.neighbors.map((e) => [...e])
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
get size() {
|
|
243
|
+
return this.chunks.size;
|
|
244
|
+
}
|
|
245
|
+
async solveChunk(e, n = {}) {
|
|
246
|
+
let r = [...e];
|
|
247
|
+
if (r.length !== 3 || r.some((e, t) => !Number.isSafeInteger(e) || !Number.isSafeInteger(e * this.dimensions[t]) || !Number.isSafeInteger(e * this.dimensions[t] - 1) || !Number.isSafeInteger((e + 1) * this.dimensions[t]))) throw RangeError("Invalid WFC chunk coordinate; coordinates and world cell bounds must be safe integers");
|
|
248
|
+
if (n.signal?.throwIfAborted(), this.busy) throw new t("failed", "WFC chunk solve already active; await it before solving or clearing. Help: https://margoengine.com/llms/packages/wfc.md");
|
|
249
|
+
let i = r.join(","), o = this.chunks.get(i);
|
|
250
|
+
if (o) return o.slice();
|
|
251
|
+
this.busy = !0;
|
|
252
|
+
try {
|
|
253
|
+
let [e, t, o] = this.dimensions, [s, c, l] = [
|
|
254
|
+
e + 2,
|
|
255
|
+
t + 2,
|
|
256
|
+
o + 2
|
|
257
|
+
], u = [];
|
|
258
|
+
for (let n = -1; n <= o; n++) for (let i = -1; i <= t; i++) for (let a = -1; a <= e; a++) {
|
|
259
|
+
if (a >= 0 && a < e && i >= 0 && i < t && n >= 0 && n < o) continue;
|
|
260
|
+
let s = [
|
|
261
|
+
a,
|
|
262
|
+
i,
|
|
263
|
+
n
|
|
264
|
+
], c = s.map((e, t) => r[t] + Math.floor(e / this.dimensions[t])), l = this.chunks.get(c.join(","));
|
|
265
|
+
if (l) {
|
|
266
|
+
let r = s.map((e, t) => (e % this.dimensions[t] + this.dimensions[t]) % this.dimensions[t]);
|
|
267
|
+
u.push({
|
|
268
|
+
position: [
|
|
269
|
+
a + 1,
|
|
270
|
+
i + 1,
|
|
271
|
+
n + 1
|
|
272
|
+
],
|
|
273
|
+
tile: l[r[0] + e * (r[1] + t * r[2])]
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
let d = await a.create([
|
|
278
|
+
s,
|
|
279
|
+
c,
|
|
280
|
+
l
|
|
281
|
+
], this.tiles, n);
|
|
282
|
+
n.signal?.throwIfAborted();
|
|
283
|
+
let f = this.seed >>> 0;
|
|
284
|
+
for (let e of r) f = Math.imul(f ^ e >>> 0, 16777619), f = Math.imul(f ^ Math.floor(e / 4294967296) >>> 0, 16777619);
|
|
285
|
+
let p = d.solve({
|
|
286
|
+
seed: f,
|
|
287
|
+
pins: u,
|
|
288
|
+
attempts: n.attempts,
|
|
289
|
+
signal: n.signal
|
|
290
|
+
}), m = new Uint16Array(e * t * o);
|
|
291
|
+
for (let n = 0; n < o; n++) for (let r = 0; r < t; r++) for (let i = 0; i < e; i++) m[i + e * (r + t * n)] = p[i + 1 + s * (r + 1 + c * (n + 1))];
|
|
292
|
+
return n.signal?.throwIfAborted(), this.chunks.set(i, m), m.slice();
|
|
293
|
+
} finally {
|
|
294
|
+
this.busy = !1;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
clear() {
|
|
298
|
+
if (this.busy) throw new t("failed", "WFC chunk solve already active; await it before clearing. Help: https://margoengine.com/llms/packages/wfc.md");
|
|
299
|
+
this.chunks.clear();
|
|
300
|
+
}
|
|
301
|
+
dispose() {
|
|
302
|
+
this.clear();
|
|
303
|
+
}
|
|
304
|
+
[Symbol.dispose]() {
|
|
305
|
+
this.dispose();
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
//#endregion
|
|
309
|
+
export { o as ChunkedWFC, a as HeapWFC };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Pin, Tile } from './index.js';
|
|
2
|
+
/** Grid coordinates in X, Y, Z order. */
|
|
3
|
+
export type CellPosition = readonly [number, number, number];
|
|
4
|
+
/** A bound on the total number of cells using any tile in a group. */
|
|
5
|
+
export interface TileCount {
|
|
6
|
+
/** Tile IDs counted as one group, without duplicates. */ readonly tiles: readonly number[];
|
|
7
|
+
/** Inclusive minimum, default 0. */ readonly min?: number;
|
|
8
|
+
/** Inclusive maximum, default cell count. */ readonly max?: number;
|
|
9
|
+
}
|
|
10
|
+
/** A six-neighbour path whose cells all use tiles from the given group. */
|
|
11
|
+
export interface TilePath {
|
|
12
|
+
/** First cell; forced to the path's tile group. */ readonly from: CellPosition;
|
|
13
|
+
/** Last cell; forced to the path's tile group. */ readonly to: CellPosition;
|
|
14
|
+
/** Walkable tile IDs. */ readonly tiles: readonly number[];
|
|
15
|
+
}
|
|
16
|
+
/** Global constraints, combined with reciprocal local adjacency. */
|
|
17
|
+
export interface GlobalConstraints {
|
|
18
|
+
/** All cells using these tiles must form one nonempty six-neighbour component. Default absent. */
|
|
19
|
+
readonly connected?: readonly number[];
|
|
20
|
+
/** Required paths. Default none. */ readonly paths?: readonly TilePath[];
|
|
21
|
+
/** Tile-group count bounds. Default none. */ readonly counts?: readonly TileCount[];
|
|
22
|
+
}
|
|
23
|
+
/** Bounded backtracking search options. */
|
|
24
|
+
export interface ConstrainedSolveOptions {
|
|
25
|
+
/** Integer seed, default 1. */ seed?: number;
|
|
26
|
+
/** Fixed tile cells, default none. */ pins?: readonly Pin[];
|
|
27
|
+
/** Backtrack budget per attempt, 0..100,000, default 10,000. */ backtracks?: number;
|
|
28
|
+
/** Attempts, 1..64, default 4. */ attempts?: number;
|
|
29
|
+
/** Synchronous cancellation checkpoints, default absent. */ signal?: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
/** CPU WFC with domain propagation, count bounds, reachability pruning and bounded chronological backtracking. */
|
|
32
|
+
export declare class ConstrainedWFC {
|
|
33
|
+
/** Copied grid dimensions. */ readonly dimensions: CellPosition;
|
|
34
|
+
/** Counts from the latest solve. */ readonly stats: {
|
|
35
|
+
attempts: number;
|
|
36
|
+
backtracks: number;
|
|
37
|
+
decisions: number;
|
|
38
|
+
};
|
|
39
|
+
private readonly tiles;
|
|
40
|
+
private readonly weights;
|
|
41
|
+
private readonly logs;
|
|
42
|
+
private readonly size;
|
|
43
|
+
private readonly connected?;
|
|
44
|
+
private readonly paths;
|
|
45
|
+
private readonly limits;
|
|
46
|
+
/** Dimensions: positive integers, at most 1,000,000 cells and 16,777,216 cell/tile pairs. Tiles: 1..4096. */
|
|
47
|
+
constructor(dimensions: CellPosition, tiles: readonly Tile[], constraints?: GlobalConstraints);
|
|
48
|
+
private index;
|
|
49
|
+
/** Return Uint16 tile IDs, X fastest. A returned solution satisfies every local and global constraint. */
|
|
50
|
+
solve({ seed, pins, backtracks, attempts, signal }?: ConstrainedSolveOptions): Uint16Array;
|
|
51
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { t as e } from "./grid-BGBmzG3D.js";
|
|
2
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/constraints.ts
|
|
4
|
+
var n = [
|
|
5
|
+
[
|
|
6
|
+
1,
|
|
7
|
+
0,
|
|
8
|
+
0
|
|
9
|
+
],
|
|
10
|
+
[
|
|
11
|
+
-1,
|
|
12
|
+
0,
|
|
13
|
+
0
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
0,
|
|
17
|
+
1,
|
|
18
|
+
0
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
0,
|
|
22
|
+
-1,
|
|
23
|
+
0
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
0,
|
|
27
|
+
0,
|
|
28
|
+
1
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
-1
|
|
34
|
+
]
|
|
35
|
+
], r = "Help: https://margoengine.com/llms/packages/wfc.md", i = class {
|
|
36
|
+
constructor(t, n, r = {}) {
|
|
37
|
+
this.stats = {
|
|
38
|
+
attempts: 0,
|
|
39
|
+
backtracks: 0,
|
|
40
|
+
decisions: 0
|
|
41
|
+
};
|
|
42
|
+
let i = e(t, n), a = i.size;
|
|
43
|
+
this.dimensions = i.dimensions, this.size = i.size, this.weights = i.weights, this.logs = i.logs, this.tiles = i.tiles;
|
|
44
|
+
let o = (e) => {
|
|
45
|
+
if (!e.length || Array.from(e).some((e) => !Number.isInteger(e) || e < 0 || e >= n.length) || new Set(e).size !== e.length) throw RangeError("Invalid WFC tile group; use a nonempty list of distinct existing tile IDs");
|
|
46
|
+
return [...e];
|
|
47
|
+
};
|
|
48
|
+
this.connected = r.connected && o(r.connected), this.paths = Array.from(r.paths ?? [], (e) => ({
|
|
49
|
+
from: this.index(e.from),
|
|
50
|
+
to: this.index(e.to),
|
|
51
|
+
tiles: o(e.tiles)
|
|
52
|
+
})), this.limits = Array.from(r.counts ?? [], (e) => {
|
|
53
|
+
let t = e.min ?? 0, n = e.max ?? a;
|
|
54
|
+
if (!Number.isInteger(t) || !Number.isInteger(n) || t < 0 || n < t || n > a) throw RangeError("Invalid WFC count bound; use integers with 0 <= min <= max <= cell count");
|
|
55
|
+
return {
|
|
56
|
+
tiles: o(e.tiles),
|
|
57
|
+
min: t,
|
|
58
|
+
max: n
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
index(e) {
|
|
63
|
+
if (e.length !== 3 || Array.from(e).some((e, t) => !Number.isInteger(e) || e < 0 || e >= this.dimensions[t])) throw RangeError("Invalid WFC position; use three integer coordinates inside the grid");
|
|
64
|
+
return e[0] + this.dimensions[0] * (e[1] + this.dimensions[1] * e[2]);
|
|
65
|
+
}
|
|
66
|
+
solve({ seed: e = 1, pins: i = [], backtracks: a = 1e4, attempts: o = 4, signal: s } = {}) {
|
|
67
|
+
if (!Number.isSafeInteger(e) || !Number.isInteger(a) || a < 0 || a > 1e5 || !Number.isInteger(o) || o < 1 || o > 64) throw RangeError("Invalid WFC search budget; use an integer seed, 0..100,000 backtracks and 1..64 attempts");
|
|
68
|
+
let c = this.tiles.length, l = this.size, [u, d, f] = this.dimensions, p = Array.from(i, (e) => {
|
|
69
|
+
if (!Number.isInteger(e.tile) || e.tile < 0 || e.tile >= c) throw RangeError("Invalid WFC pin tile; use an existing tile ID");
|
|
70
|
+
return {
|
|
71
|
+
cell: this.index(e.position),
|
|
72
|
+
tile: e.tile
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
s?.throwIfAborted();
|
|
76
|
+
let m = new Uint8Array(l * c), h = new Int32Array(l), g = [], _ = [], v = new Uint8Array(l), y = new Uint8Array(c), b = new Uint8Array(l), x = new Uint8Array(l), S = new Uint8Array(l), C = new Int32Array(l), w = [], T = e >>> 0 || 1, E = !1, D = () => (T ^= T << 13, T ^= T >>> 17, T ^= T << 5, (T >>> 0) / 4294967296), O = (e, t) => {
|
|
77
|
+
let r = e % u + n[t][0], i = Math.floor(e / u) % d + n[t][1], a = Math.floor(e / (u * d)) + n[t][2];
|
|
78
|
+
return r < 0 || r >= u || i < 0 || i >= d || a < 0 || a >= f ? -1 : r + u * (i + d * a);
|
|
79
|
+
}, k = (e) => {
|
|
80
|
+
v[e] || (v[e] = 1, _.push(e));
|
|
81
|
+
}, A = (e, t) => {
|
|
82
|
+
let n = e * c + t;
|
|
83
|
+
m[n] && (m[n] = 0, g.push(n), --h[e] === 0 && (E = !0), k(e));
|
|
84
|
+
}, j = (e, t, n) => {
|
|
85
|
+
for (let r = 0; r < c; r++) t.includes(r) !== n && A(e, r);
|
|
86
|
+
}, M = (e, t, n) => {
|
|
87
|
+
let r = t ?? -1, i = !1;
|
|
88
|
+
for (let n = 0; n < l; n++) {
|
|
89
|
+
let a = 0;
|
|
90
|
+
for (let t of e) a += m[n * c + t];
|
|
91
|
+
b[n] = +(a > 0), x[n] = +(a === h[n]), t === void 0 && (r < 0 || !i && x[n]) && (r = b[n] ? n : r), x[n] && (i = !0);
|
|
92
|
+
}
|
|
93
|
+
if (r < 0 || !b[r]) {
|
|
94
|
+
E = !0;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
S.fill(0), S[r] = 1, C[0] = r;
|
|
98
|
+
let a = 0, o = 1;
|
|
99
|
+
for (; a < o;) {
|
|
100
|
+
let e = C[a++];
|
|
101
|
+
for (let t = 0; t < 6; t++) {
|
|
102
|
+
let n = O(e, t);
|
|
103
|
+
n >= 0 && b[n] && !S[n] && (S[n] = 1, C[o++] = n);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (n !== void 0) S[n] || (E = !0);
|
|
107
|
+
else if (i) {
|
|
108
|
+
for (let t = 0; t < l; t++) if (!S[t]) {
|
|
109
|
+
if (x[t]) {
|
|
110
|
+
E = !0;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
for (let n of e) A(t, n);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}, N = () => {
|
|
117
|
+
for (;;) {
|
|
118
|
+
for (s?.throwIfAborted(); _.length && !E;) {
|
|
119
|
+
let e = _.pop();
|
|
120
|
+
v[e] = 0;
|
|
121
|
+
for (let t = 0; t < 6; t++) {
|
|
122
|
+
let n = O(e, t);
|
|
123
|
+
if (!(n < 0)) {
|
|
124
|
+
y.fill(0);
|
|
125
|
+
for (let n = 0; n < c; n++) if (m[e * c + n]) for (let e of this.tiles[n].neighbors[t]) y[e] = 1;
|
|
126
|
+
for (let e = 0; e < c; e++) y[e] || A(n, e);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (E) return !1;
|
|
131
|
+
for (let e of this.limits) {
|
|
132
|
+
let t = 0, n = 0;
|
|
133
|
+
for (let r = 0; r < l; r++) {
|
|
134
|
+
let i = 0;
|
|
135
|
+
for (let t of e.tiles) i += m[r * c + t];
|
|
136
|
+
b[r] = +(i > 0), x[r] = +(i === h[r]), t += x[r], n += b[r];
|
|
137
|
+
}
|
|
138
|
+
if (t > e.max || n < e.min) return E = !0, !1;
|
|
139
|
+
if (t === e.max || n === e.min) for (let t = 0; t < l; t++) b[t] && !x[t] && j(t, e.tiles, n === e.min);
|
|
140
|
+
}
|
|
141
|
+
this.connected && M(this.connected);
|
|
142
|
+
for (let e of this.paths) M(e.tiles, e.from, e.to);
|
|
143
|
+
if (E) return !1;
|
|
144
|
+
if (!_.length) return !0;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
this.stats.attempts = this.stats.backtracks = this.stats.decisions = 0;
|
|
148
|
+
for (let e = 1; e <= o; e++) {
|
|
149
|
+
this.stats.attempts = e, E = !1, m.fill(1), h.fill(c), v.fill(0), g.length = _.length = w.length = 0;
|
|
150
|
+
for (let e = 0; e < l; e++) k(e);
|
|
151
|
+
for (let e of p) j(e.cell, [e.tile], !0);
|
|
152
|
+
for (let e of this.paths) j(e.from, e.tiles, !0), j(e.to, e.tiles, !0);
|
|
153
|
+
let t = N(), n = 0;
|
|
154
|
+
for (;;) {
|
|
155
|
+
if (!t) {
|
|
156
|
+
let e = w.pop();
|
|
157
|
+
if (!e || n++ >= a) break;
|
|
158
|
+
for (this.stats.backtracks++; g.length > e.mark;) {
|
|
159
|
+
let e = g.pop();
|
|
160
|
+
m[e] = 1, h[Math.floor(e / c)]++;
|
|
161
|
+
}
|
|
162
|
+
_.length = 0, v.fill(0), E = !1, A(e.cell, e.tile), t = N();
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
let e = -1, r = Infinity, i = 0;
|
|
166
|
+
for (let t = 0; t < l; t++) if (h[t] > 1) {
|
|
167
|
+
let n = 0, a = 0;
|
|
168
|
+
for (let e = 0; e < c; e++) m[t * c + e] && (n += this.weights[e], a += this.logs[e]);
|
|
169
|
+
let o = Math.log(n) - a / n + D() * 1e-8;
|
|
170
|
+
o < r && (r = o, e = t, i = n);
|
|
171
|
+
}
|
|
172
|
+
if (e < 0) {
|
|
173
|
+
let e = new Uint16Array(l);
|
|
174
|
+
for (let t = 0; t < l; t++) for (let n = 0; n < c; n++) if (m[t * c + n]) {
|
|
175
|
+
e[t] = n;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
return e;
|
|
179
|
+
}
|
|
180
|
+
let o = D() * i, s = -1;
|
|
181
|
+
for (let t = 0; t < c && !(m[e * c + t] && (s = t, o -= this.weights[t], o <= 0)); t++);
|
|
182
|
+
this.stats.decisions++, w.push({
|
|
183
|
+
mark: g.length,
|
|
184
|
+
cell: e,
|
|
185
|
+
tile: s
|
|
186
|
+
}), j(e, [s], !0), t = N();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
throw new t("failed", `WFC exhausted its search budget; check global constraints or raise backtracks/attempts. ${r}`);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
//#endregion
|
|
193
|
+
export { i as ConstrainedWFC };
|