@woosh/meep-engine 3.78.1 → 3.78.3
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/CHANGELOG.md +13 -0
- package/package.json +1 -1
- package/src/engine/asset/loaders/GroomAssetLoader.d.ts.map +1 -1
- package/src/engine/asset/loaders/GroomAssetLoader.js +2 -1
- package/src/engine/graphics/hair/Groom.d.ts +30 -0
- package/src/engine/graphics/hair/Groom.d.ts.map +1 -1
- package/src/engine/graphics/hair/Groom.js +35 -0
- package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts +18 -2
- package/src/engine/graphics/hair/GroomSerializationAdapter.d.ts.map +1 -1
- package/src/engine/graphics/hair/GroomSerializationAdapter.js +36 -6
- package/src/engine/graphics/hair/GroomSerializationUpgrader_1_2.d.ts +8 -0
- package/src/engine/graphics/hair/GroomSerializationUpgrader_1_2.d.ts.map +1 -0
- package/src/engine/graphics/hair/GroomSerializationUpgrader_1_2.js +44 -0
- package/src/engine/graphics/hair/groom_bake_shelter.d.ts +33 -0
- package/src/engine/graphics/hair/groom_bake_shelter.d.ts.map +1 -0
- package/src/engine/graphics/hair/groom_bake_shelter.js +288 -0
- package/src/engine/graphics/hair/groom_build_group.d.ts +10 -2
- package/src/engine/graphics/hair/groom_build_group.d.ts.map +1 -1
- package/src/engine/graphics/hair/groom_build_group.js +8 -2
- package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts +119 -0
- package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts.map +1 -0
- package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.js +563 -0
- package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts +11 -0
- package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts.map +1 -0
- package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.js +10 -0
- package/src/generation/wfc/GridWFCCompiledTileSet.d.ts +123 -0
- package/src/generation/wfc/GridWFCCompiledTileSet.d.ts.map +1 -0
- package/src/generation/wfc/GridWFCCompiledTileSet.js +355 -0
- package/src/generation/wfc/GridWFCEdge.d.ts +13 -0
- package/src/generation/wfc/GridWFCEdge.d.ts.map +1 -0
- package/src/generation/wfc/GridWFCEdge.js +14 -0
- package/src/generation/wfc/GridWFCTileSet.d.ts +120 -0
- package/src/generation/wfc/GridWFCTileSet.d.ts.map +1 -0
- package/src/generation/wfc/GridWFCTileSet.js +275 -0
- package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -1
- package/src/shade/renderer/hair/GPUHairSystem.js +55 -11
- package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.d.ts.map +1 -1
- package/src/shade/renderer/hair/HAIR_DATABASE_SPEC.js +4 -0
- package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts +77 -15
- package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts.map +1 -1
- package/src/shade/renderer/hair/HAIR_SIMULATION.js +104 -17
- package/src/shade/renderer/hair/chunk_hair_guide.d.ts +9 -2
- package/src/shade/renderer/hair/chunk_hair_guide.d.ts.map +1 -1
- package/src/shade/renderer/hair/chunk_hair_guide.js +30 -2
- package/src/shade/renderer/hair/shader_hair_simulate.d.ts +27 -18
- package/src/shade/renderer/hair/shader_hair_simulate.d.ts.map +1 -1
- package/src/shade/renderer/hair/shader_hair_simulate.js +174 -83
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
import { assert } from "../../../../core/assert.js";
|
|
2
|
+
import { array_shuffle } from "../../../../core/collection/array/array_shuffle.js";
|
|
3
|
+
import { uf_find, uf_init, uf_union } from "../../../../core/collection/union-find/union_find.js";
|
|
4
|
+
import { PI_HALF } from "../../../../core/math/PI_HALF.js";
|
|
5
|
+
import { seededRandom } from "../../../../core/math/random/seededRandom.js";
|
|
6
|
+
import Task from "../../../../core/process/task/Task.js";
|
|
7
|
+
import TaskGroup from "../../../../core/process/task/TaskGroup.js";
|
|
8
|
+
import { TaskSignal } from "../../../../core/process/task/TaskSignal.js";
|
|
9
|
+
import { actionTask } from "../../../../core/process/task/util/actionTask.js";
|
|
10
|
+
import { countTask } from "../../../../core/process/task/util/countTask.js";
|
|
11
|
+
import { WFCCellChoice } from "../../../../core/wfc/WFCCellChoice.js";
|
|
12
|
+
import { WFCLattice } from "../../../../core/wfc/WFCLattice.js";
|
|
13
|
+
import { WFCState } from "../../../../core/wfc/WFCState.js";
|
|
14
|
+
import { WFCStatus } from "../../../../core/wfc/WFCStatus.js";
|
|
15
|
+
import { wfc_solve } from "../../../../core/wfc/wfc_solve.js";
|
|
16
|
+
import { wfc_state_apply_boundary } from "../../../../core/wfc/wfc_state_apply_boundary.js";
|
|
17
|
+
import { wfc_state_read } from "../../../../core/wfc/wfc_state_read.js";
|
|
18
|
+
import { wfc_state_restrict } from "../../../../core/wfc/wfc_state_restrict.js";
|
|
19
|
+
import { compute_writes_union } from "../../../compute_writes_union.js";
|
|
20
|
+
import { GridTaskGenerator } from "../../GridTaskGenerator.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Solver operations per task cycle. A cycle is the unit the executor time-slices on, so this is
|
|
24
|
+
* how long the solve can hold a slice: a few thousand edge relaxations is well under a
|
|
25
|
+
* millisecond.
|
|
26
|
+
*
|
|
27
|
+
* @type {number}
|
|
28
|
+
*/
|
|
29
|
+
const OPERATIONS_PER_CYCLE = 4096;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Lays tiles over the whole grid by wave function collapse: every cell gets one tile of a
|
|
33
|
+
* {@link GridWFCTileSet}, and every pair of neighbours shows the same socket on the edge they
|
|
34
|
+
* share. Each tile's {@link GridCellAction} is then run on its cell, turned the way the tile is.
|
|
35
|
+
*
|
|
36
|
+
* What adjacency alone cannot say is said around the solve:
|
|
37
|
+
* - {@link restrictions} narrow what a cell may hold before it starts — keep these cells open,
|
|
38
|
+
* let paving stand only where that field is high;
|
|
39
|
+
* - {@link boundary} says what may stand against the edge of the grid;
|
|
40
|
+
* - {@link connected} makes the tiles of one kind hang together — by opening what keeps them
|
|
41
|
+
* apart where the set has a tile for it, and by drawing again, up to {@link attempts} times,
|
|
42
|
+
* where it has not;
|
|
43
|
+
* - {@link fallback} covers the grid when every attempt failed, so a generator that must always
|
|
44
|
+
* produce a map does.
|
|
45
|
+
*
|
|
46
|
+
* The same grid, tile set and seed always give the same result.
|
|
47
|
+
*/
|
|
48
|
+
export class GridTaskWaveFunctionCollapse extends GridTaskGenerator {
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @type {GridWFCTileSet}
|
|
52
|
+
*/
|
|
53
|
+
tiles = null;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A cell that `matcher` matches may only hold tiles carrying `tag`. A cell matched by several
|
|
57
|
+
* may only hold tiles carrying all their tags.
|
|
58
|
+
*
|
|
59
|
+
* @type {{matcher: CellMatcher, tag: string}[]}
|
|
60
|
+
*/
|
|
61
|
+
restrictions = [];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Tag of the tiles allowed in the cells along the edge of the grid; `null` allows any.
|
|
65
|
+
*
|
|
66
|
+
* A tile set whose walls must close on themselves wants its open tile here: a wall could
|
|
67
|
+
* otherwise run off the edge and never have to turn back.
|
|
68
|
+
*
|
|
69
|
+
* @type {string|null}
|
|
70
|
+
*/
|
|
71
|
+
boundary = null;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Tag of the tiles that must form one region, joined edge to edge; `null` skips the check.
|
|
75
|
+
*
|
|
76
|
+
* Adjacency rules cannot express this — a ring of wall with no gate in it is locally perfect —
|
|
77
|
+
* so it is seen to on the finished result. Where a tile keeping two regions apart has a
|
|
78
|
+
* **twin** carrying the tag (a tile with the same four sockets: the gap in a wall, for a
|
|
79
|
+
* stretch of that wall), the twin is swapped in and nothing around it has to change. Give the
|
|
80
|
+
* set such twins and one draw nearly always does; a result no swap can join (a pocket walled
|
|
81
|
+
* by tiles with no twin, or by two walls back to back) is thrown away and drawn again — which
|
|
82
|
+
* is all a set without twins can do, and a large grid can rarely afford.
|
|
83
|
+
*
|
|
84
|
+
* @type {string|null}
|
|
85
|
+
*/
|
|
86
|
+
connected = null;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @type {WFCCellChoice}
|
|
90
|
+
*/
|
|
91
|
+
cell_choice = WFCCellChoice.SCANLINE;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* How many draws are tried before giving up. A draw is abandoned when the search runs out of
|
|
95
|
+
* backtracks, or when its result cannot be made {@link connected}.
|
|
96
|
+
*
|
|
97
|
+
* @type {number}
|
|
98
|
+
*/
|
|
99
|
+
attempts = 8;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Name of the tile laid over the whole grid when every attempt failed; `null` fails the task
|
|
103
|
+
* instead. It has to be a tile that may neighbour itself, which {@link build} checks, and that
|
|
104
|
+
* may stand in every restricted cell and against the edge of the grid, which nothing checks:
|
|
105
|
+
* the fallback is laid over restrictions and boundary alike — the set's open tile, usually.
|
|
106
|
+
*
|
|
107
|
+
* @type {string|null}
|
|
108
|
+
*/
|
|
109
|
+
fallback = null;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* How the most recent build went, for tuning a tile set. Not an input. `backtracks` and
|
|
113
|
+
* `openings` are summed over every draw of the build, the ones thrown away included.
|
|
114
|
+
*
|
|
115
|
+
* @type {{attempts: number, backtracks: number, openings: number, solved: boolean, fallback: boolean}}
|
|
116
|
+
*/
|
|
117
|
+
statistics = { attempts: 0, backtracks: 0, openings: 0, solved: false, fallback: false };
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {Object} options
|
|
121
|
+
* @param {string} [options.name]
|
|
122
|
+
* @param {GridWFCTileSet} options.tiles
|
|
123
|
+
* @param {{matcher: CellMatcher, tag: string}[]} [options.restrictions]
|
|
124
|
+
* @param {string|null} [options.boundary]
|
|
125
|
+
* @param {string|null} [options.connected]
|
|
126
|
+
* @param {WFCCellChoice} [options.cell_choice]
|
|
127
|
+
* @param {number} [options.attempts]
|
|
128
|
+
* @param {string|null} [options.fallback]
|
|
129
|
+
* @returns {GridTaskWaveFunctionCollapse}
|
|
130
|
+
*/
|
|
131
|
+
static from({
|
|
132
|
+
name = 'wave function collapse',
|
|
133
|
+
tiles,
|
|
134
|
+
restrictions = [],
|
|
135
|
+
boundary = null,
|
|
136
|
+
connected = null,
|
|
137
|
+
cell_choice = WFCCellChoice.SCANLINE,
|
|
138
|
+
attempts = 8,
|
|
139
|
+
fallback = null
|
|
140
|
+
}) {
|
|
141
|
+
assert.defined(tiles, 'tiles');
|
|
142
|
+
assert.isArray(restrictions, 'restrictions');
|
|
143
|
+
assert.isPositiveInteger(attempts, 'attempts');
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < restrictions.length; i++) {
|
|
146
|
+
assert.equal(restrictions[i].matcher.isCellMatcher, true, `restrictions[${i}].matcher.isCellMatcher !== true`);
|
|
147
|
+
assert.isString(restrictions[i].tag, `restrictions[${i}].tag`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const r = new GridTaskWaveFunctionCollapse();
|
|
151
|
+
|
|
152
|
+
r.name = name;
|
|
153
|
+
r.tiles = tiles;
|
|
154
|
+
r.restrictions = restrictions;
|
|
155
|
+
r.boundary = boundary;
|
|
156
|
+
r.connected = connected;
|
|
157
|
+
r.cell_choice = cell_choice;
|
|
158
|
+
r.attempts = attempts;
|
|
159
|
+
r.fallback = fallback;
|
|
160
|
+
|
|
161
|
+
return r;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
computeWrites() {
|
|
165
|
+
return compute_writes_union(this.tiles.prototypes.map(p => p.action).filter(a => a !== null));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
build(grid, ecd, seed) {
|
|
169
|
+
const compiled = this.tiles.compile();
|
|
170
|
+
const prototypes = compiled.prototypes;
|
|
171
|
+
|
|
172
|
+
const width = grid.width;
|
|
173
|
+
const height = grid.height;
|
|
174
|
+
const cell_count = width * height;
|
|
175
|
+
|
|
176
|
+
// y on axis 0, so a cell's index in the lattice is the grid's own `y * width + x`
|
|
177
|
+
const lattice = cell_count > 0 ? WFCLattice.from_shape([height, width]) : null;
|
|
178
|
+
|
|
179
|
+
const restrictions = this.restrictions;
|
|
180
|
+
const boundary = this.boundary;
|
|
181
|
+
const connected = this.connected;
|
|
182
|
+
const cell_choice = this.cell_choice;
|
|
183
|
+
const attempts = this.attempts;
|
|
184
|
+
|
|
185
|
+
let fallback_tile = -1;
|
|
186
|
+
|
|
187
|
+
if (this.fallback !== null) {
|
|
188
|
+
const prototype = compiled.prototype_named(this.fallback);
|
|
189
|
+
|
|
190
|
+
// a real throw: a misspelt fallback would otherwise only be found the day it is needed
|
|
191
|
+
if (prototype === -1) {
|
|
192
|
+
throw new Error(`fallback tile '${this.fallback}' is not in the tile set`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fallback_tile = compiled.tile_prototype.indexOf(prototype);
|
|
196
|
+
|
|
197
|
+
// the same reasoning: a fallback that cannot stand beside itself lays a grid of broken
|
|
198
|
+
// seams, and only on the day every draw failed
|
|
199
|
+
for (let direction = 0; direction < 4; direction++) {
|
|
200
|
+
if (!compiled.rules.allows(direction, fallback_tile, fallback_tile)) {
|
|
201
|
+
throw new Error(`fallback tile '${this.fallback}' cannot stand beside itself, so it cannot cover the grid`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const statistics = { attempts: 0, backtracks: 0, openings: 0, solved: false, fallback: false };
|
|
207
|
+
this.statistics = statistics;
|
|
208
|
+
|
|
209
|
+
// what may stand against the grid's edge: the same for every attempt, and asked again
|
|
210
|
+
// of any tile swapped in after a solve
|
|
211
|
+
const boundary_masks = compiled.boundary_masks(boundary);
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The tile each cell ended up with
|
|
215
|
+
* @type {Uint32Array}
|
|
216
|
+
*/
|
|
217
|
+
const result = new Uint32Array(cell_count);
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* One draw from this for the whole build: an attempt continues the stream the last one
|
|
221
|
+
* left, so the build as a whole is a function of the seed
|
|
222
|
+
*/
|
|
223
|
+
const random = seededRandom(seed);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Which restrictions hold in which cell is a fact about the grid, the same for every
|
|
227
|
+
* attempt: `cell_restriction[cell]` indexes `restriction_masks`, 0 meaning unrestricted
|
|
228
|
+
* @type {Uint32Array}
|
|
229
|
+
*/
|
|
230
|
+
const cell_restriction = new Uint32Array(cell_count);
|
|
231
|
+
/** @type {(Uint32Array|null)[]} */
|
|
232
|
+
const restriction_masks = [null];
|
|
233
|
+
|
|
234
|
+
const tInitialize = actionTask(() => {
|
|
235
|
+
for (let i = 0; i < restrictions.length; i++) {
|
|
236
|
+
restrictions[i].matcher.initialize(grid, seed);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
for (let i = 0; i < prototypes.length; i++) {
|
|
240
|
+
const action = prototypes[i].action;
|
|
241
|
+
|
|
242
|
+
if (action !== null) {
|
|
243
|
+
action.initialize(grid, seed);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (restrictions.length === 0) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const words = compiled.rules.words_per_cell;
|
|
252
|
+
// cells matched by the same restrictions share a mask
|
|
253
|
+
const mask_of_set = new Map();
|
|
254
|
+
|
|
255
|
+
for (let cell = 0; cell < cell_count; cell++) {
|
|
256
|
+
const x = cell % width;
|
|
257
|
+
const y = (cell / width) | 0;
|
|
258
|
+
|
|
259
|
+
let key = '';
|
|
260
|
+
|
|
261
|
+
for (let i = 0; i < restrictions.length; i++) {
|
|
262
|
+
if (restrictions[i].matcher.match(grid, x, y, 0)) {
|
|
263
|
+
key += i + ',';
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (key === '') {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let index = mask_of_set.get(key);
|
|
272
|
+
|
|
273
|
+
if (index === undefined) {
|
|
274
|
+
const mask = new Uint32Array(words).fill(0xFFFFFFFF);
|
|
275
|
+
|
|
276
|
+
for (const i of key.split(',')) {
|
|
277
|
+
if (i === '') {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const tagged = compiled.mask_of_tag(restrictions[Number(i)].tag);
|
|
282
|
+
|
|
283
|
+
for (let w = 0; w < words; w++) {
|
|
284
|
+
mask[w] &= tagged[w];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
index = restriction_masks.length;
|
|
289
|
+
restriction_masks.push(mask);
|
|
290
|
+
mask_of_set.set(key, index);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
cell_restriction[cell] = index;
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* A fresh solver state with the boundary and the restrictions applied, or `null` when they
|
|
299
|
+
* leave some cell with nothing to hold — which no other draw would fix.
|
|
300
|
+
*
|
|
301
|
+
* @returns {WFCState|null}
|
|
302
|
+
*/
|
|
303
|
+
const start = () => {
|
|
304
|
+
const state = new WFCState(compiled.rules, lattice, compiled.weights, random, cell_choice);
|
|
305
|
+
|
|
306
|
+
// always: even with no tag asked for, the edge of the grid must not cut a piece in two
|
|
307
|
+
if (!wfc_state_apply_boundary(state, boundary_masks)) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
for (let cell = 0; cell < cell_count; cell++) {
|
|
312
|
+
const index = cell_restriction[cell];
|
|
313
|
+
|
|
314
|
+
if (index !== 0 && !wfc_state_restrict(state, cell, restriction_masks[index])) {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return state;
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* May `tile` stand in `cell`, by the restrictions and the boundary? The solve has already
|
|
324
|
+
* answered this for the tile it chose; it is asked again for a tile swapped in afterwards.
|
|
325
|
+
*
|
|
326
|
+
* @param {number} cell
|
|
327
|
+
* @param {number} tile
|
|
328
|
+
* @returns {boolean}
|
|
329
|
+
*/
|
|
330
|
+
const may_stand = (cell, tile) => {
|
|
331
|
+
const word = tile >> 5, bit = (1 << (tile & 31)) >>> 0;
|
|
332
|
+
const index = cell_restriction[cell];
|
|
333
|
+
|
|
334
|
+
if (index !== 0 && (restriction_masks[index][word] & bit) === 0) {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const x = cell % width, y = (cell / width) | 0;
|
|
339
|
+
const words = compiled.rules.words_per_cell;
|
|
340
|
+
|
|
341
|
+
// solver directions: axis 0 is y, axis 1 is x, the low side first
|
|
342
|
+
return (y !== 0 || (boundary_masks[word] & bit) !== 0)
|
|
343
|
+
&& (y !== height - 1 || (boundary_masks[words + word] & bit) !== 0)
|
|
344
|
+
&& (x !== 0 || (boundary_masks[2 * words + word] & bit) !== 0)
|
|
345
|
+
&& (x !== width - 1 || (boundary_masks[3 * words + word] & bit) !== 0);
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Make the cells of `result` whose tile carries the `connected` tag one region, if a few
|
|
350
|
+
* swaps can.
|
|
351
|
+
*
|
|
352
|
+
* Where two regions are kept apart by a tile that has a twin carrying the tag — the same
|
|
353
|
+
* sockets, so nothing around it has to change — the twin takes its place and the regions
|
|
354
|
+
* are one. One such opening per pair of regions, at a cell chosen by the build's draw.
|
|
355
|
+
*
|
|
356
|
+
* @returns {boolean} whether it ended as one region
|
|
357
|
+
*/
|
|
358
|
+
const connect = () => {
|
|
359
|
+
const tagged = compiled.mask_of_tag(connected);
|
|
360
|
+
const member = (cell) => {
|
|
361
|
+
const tile = result[cell];
|
|
362
|
+
|
|
363
|
+
return (tagged[tile >> 5] >>> (tile & 31)) & 1;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const parent = new Uint32Array(cell_count);
|
|
367
|
+
|
|
368
|
+
uf_init(parent, cell_count);
|
|
369
|
+
|
|
370
|
+
for (let cell = 0; cell < cell_count; cell++) {
|
|
371
|
+
if (member(cell) === 0) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// the cell on the +x side, unless this one ends its row, and the cell on the +y side
|
|
376
|
+
if ((cell + 1) % width !== 0 && member(cell + 1) === 1) {
|
|
377
|
+
uf_union(parent, cell, cell + 1);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (cell + width < cell_count && member(cell + width) === 1) {
|
|
381
|
+
uf_union(parent, cell, cell + width);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const is_one_region = () => {
|
|
386
|
+
let root = -1;
|
|
387
|
+
|
|
388
|
+
for (let cell = 0; cell < cell_count; cell++) {
|
|
389
|
+
if (member(cell) === 0) {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const r = uf_find(parent, cell);
|
|
394
|
+
|
|
395
|
+
if (root === -1) {
|
|
396
|
+
root = r;
|
|
397
|
+
} else if (r !== root) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return true;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
if (is_one_region()) {
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// every cell that could be opened, in an order the draw decides: taken in cell order,
|
|
410
|
+
// every pocket would be opened at its top-left
|
|
411
|
+
const candidates = [];
|
|
412
|
+
|
|
413
|
+
for (let cell = 0; cell < cell_count; cell++) {
|
|
414
|
+
if (member(cell) === 0 && compiled.twins_of(result[cell], connected).length > 0) {
|
|
415
|
+
candidates.push(cell);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
array_shuffle(random, candidates);
|
|
420
|
+
|
|
421
|
+
const neighbours = [0, 0, 0, 0];
|
|
422
|
+
|
|
423
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
424
|
+
const cell = candidates[i];
|
|
425
|
+
const x = cell % width;
|
|
426
|
+
|
|
427
|
+
let count = 0;
|
|
428
|
+
|
|
429
|
+
if (x > 0 && member(cell - 1) === 1) {
|
|
430
|
+
neighbours[count++] = cell - 1;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (x < width - 1 && member(cell + 1) === 1) {
|
|
434
|
+
neighbours[count++] = cell + 1;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (cell >= width && member(cell - width) === 1) {
|
|
438
|
+
neighbours[count++] = cell - width;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (cell + width < cell_count && member(cell + width) === 1) {
|
|
442
|
+
neighbours[count++] = cell + width;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// worth opening only if it stands between two regions that are still apart
|
|
446
|
+
let apart = false;
|
|
447
|
+
|
|
448
|
+
for (let n = 1; n < count && !apart; n++) {
|
|
449
|
+
apart = uf_find(parent, neighbours[n]) !== uf_find(parent, neighbours[0]);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (!apart) {
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const twins = compiled.twins_of(result[cell], connected);
|
|
457
|
+
const first = Math.floor(random() * twins.length);
|
|
458
|
+
|
|
459
|
+
for (let t = 0; t < twins.length; t++) {
|
|
460
|
+
const twin = twins[(first + t) % twins.length];
|
|
461
|
+
|
|
462
|
+
if (!may_stand(cell, twin)) {
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
result[cell] = twin;
|
|
467
|
+
statistics.openings++;
|
|
468
|
+
|
|
469
|
+
for (let n = 0; n < count; n++) {
|
|
470
|
+
uf_union(parent, cell, neighbours[n]);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return is_one_region();
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
/** @type {WFCState|null} */
|
|
481
|
+
let state = null;
|
|
482
|
+
let attempt = 0;
|
|
483
|
+
|
|
484
|
+
const give_up = () => {
|
|
485
|
+
if (fallback_tile === -1) {
|
|
486
|
+
return TaskSignal.EndFailure;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
result.fill(fallback_tile);
|
|
490
|
+
statistics.fallback = true;
|
|
491
|
+
|
|
492
|
+
return TaskSignal.EndSuccess;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const tSolve = new Task({
|
|
496
|
+
name: `${this.name}: solve`,
|
|
497
|
+
cycleFunction() {
|
|
498
|
+
if (cell_count === 0) {
|
|
499
|
+
return TaskSignal.EndSuccess;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (state === null) {
|
|
503
|
+
if (attempt >= attempts) {
|
|
504
|
+
return give_up();
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
attempt++;
|
|
508
|
+
statistics.attempts = attempt;
|
|
509
|
+
|
|
510
|
+
state = start();
|
|
511
|
+
|
|
512
|
+
if (state === null) {
|
|
513
|
+
// the restrictions contradict each other before a single tile is drawn
|
|
514
|
+
return give_up();
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const status = wfc_solve(state, OPERATIONS_PER_CYCLE);
|
|
519
|
+
|
|
520
|
+
if (status === WFCStatus.RUNNING) {
|
|
521
|
+
return TaskSignal.Continue;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
statistics.backtracks += state.backtrack_count;
|
|
525
|
+
|
|
526
|
+
if (status === WFCStatus.UNSATISFIABLE) {
|
|
527
|
+
// the search was exhaustive: the instance has no solution, whatever the draw
|
|
528
|
+
return give_up();
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (status === WFCStatus.SOLVED) {
|
|
532
|
+
wfc_state_read(result, state);
|
|
533
|
+
|
|
534
|
+
if (connected === null || connect()) {
|
|
535
|
+
statistics.solved = true;
|
|
536
|
+
|
|
537
|
+
return TaskSignal.EndSuccess;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// out of backtracks, or solved into pieces no swap could join: draw again
|
|
542
|
+
state = null;
|
|
543
|
+
|
|
544
|
+
return TaskSignal.Continue;
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
tSolve.addDependency(tInitialize);
|
|
549
|
+
|
|
550
|
+
const tApply = countTask(0, cell_count, cell => {
|
|
551
|
+
const tile = result[cell];
|
|
552
|
+
const action = prototypes[compiled.tile_prototype[tile]].action;
|
|
553
|
+
|
|
554
|
+
if (action !== null) {
|
|
555
|
+
action.execute(grid, cell % width, (cell / width) | 0, compiled.tile_turns[tile] * PI_HALF);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
tApply.addDependency(tSolve);
|
|
560
|
+
|
|
561
|
+
return new TaskGroup([tInitialize, tSolve, tApply], this.name);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a socket as belonging to the inside of a multi-cell piece. Such a socket is suffixed with
|
|
3
|
+
* the variant's rotation when the set is compiled, so the cells of one turned copy of a piece can
|
|
4
|
+
* only ever meet each other — never the cells of the same piece turned another way.
|
|
5
|
+
*
|
|
6
|
+
* Not a character a caller's socket name can start with by accident.
|
|
7
|
+
*
|
|
8
|
+
* @type {string}
|
|
9
|
+
*/
|
|
10
|
+
export const GRID_WFC_PIECE_SOCKET_PREFIX: string;
|
|
11
|
+
//# sourceMappingURL=GRID_WFC_PIECE_SOCKET_PREFIX.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GRID_WFC_PIECE_SOCKET_PREFIX.d.ts","sourceRoot":"","sources":["../../../../src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,2CAFU,MAAM,CAEqC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a socket as belonging to the inside of a multi-cell piece. Such a socket is suffixed with
|
|
3
|
+
* the variant's rotation when the set is compiled, so the cells of one turned copy of a piece can
|
|
4
|
+
* only ever meet each other — never the cells of the same piece turned another way.
|
|
5
|
+
*
|
|
6
|
+
* Not a character a caller's socket name can start with by accident.
|
|
7
|
+
*
|
|
8
|
+
* @type {string}
|
|
9
|
+
*/
|
|
10
|
+
export const GRID_WFC_PIECE_SOCKET_PREFIX = '\u0000';
|