@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,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link GridWFCTileSet} expanded for the solver: every turned copy of every prototype is a tile
|
|
3
|
+
* of its own, and the sockets have become a {@link WFCRuleTable}.
|
|
4
|
+
*
|
|
5
|
+
* Read-only, and shared by every solve over the set.
|
|
6
|
+
*/
|
|
7
|
+
export class GridWFCCompiledTileSet {
|
|
8
|
+
/**
|
|
9
|
+
* @param {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}[]} prototypes
|
|
10
|
+
*/
|
|
11
|
+
constructor(prototypes: {
|
|
12
|
+
name: string;
|
|
13
|
+
edges: string[];
|
|
14
|
+
weight: number;
|
|
15
|
+
rotations: number;
|
|
16
|
+
tags: string[];
|
|
17
|
+
action: (GridCellAction | null);
|
|
18
|
+
}[]);
|
|
19
|
+
/**
|
|
20
|
+
* @type {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}[]}
|
|
21
|
+
*/
|
|
22
|
+
prototypes: {
|
|
23
|
+
name: string;
|
|
24
|
+
edges: string[];
|
|
25
|
+
weight: number;
|
|
26
|
+
rotations: number;
|
|
27
|
+
tags: string[];
|
|
28
|
+
action: (GridCellAction | null);
|
|
29
|
+
}[];
|
|
30
|
+
/** @type {number} */
|
|
31
|
+
tile_count: number;
|
|
32
|
+
/** @type {Uint32Array} */
|
|
33
|
+
tile_prototype: Uint32Array;
|
|
34
|
+
/** quarter turns, 0..3 @type {Uint8Array} */
|
|
35
|
+
tile_turns: Uint8Array;
|
|
36
|
+
/** @type {string[]} */
|
|
37
|
+
tile_edges: string[];
|
|
38
|
+
/** @type {WFCRuleTable} */
|
|
39
|
+
rules: WFCRuleTable;
|
|
40
|
+
/** @type {WFCTileWeights} */
|
|
41
|
+
weights: WFCTileWeights;
|
|
42
|
+
/**
|
|
43
|
+
* @type {Map<string, Uint32Array>}
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
private __tag_masks;
|
|
47
|
+
/**
|
|
48
|
+
* @type {Map<string, number[][]>}
|
|
49
|
+
* @private
|
|
50
|
+
*/
|
|
51
|
+
private __tag_twins;
|
|
52
|
+
/**
|
|
53
|
+
* The solver direction a tile edge faces.
|
|
54
|
+
*
|
|
55
|
+
* @param {GridWFCEdge} edge
|
|
56
|
+
* @returns {number}
|
|
57
|
+
*/
|
|
58
|
+
direction_of(edge: GridWFCEdge): number;
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} name
|
|
61
|
+
* @returns {number} prototype index, or -1
|
|
62
|
+
*/
|
|
63
|
+
prototype_named(name: string): number;
|
|
64
|
+
/**
|
|
65
|
+
* @param {number} tile
|
|
66
|
+
* @param {string} tag
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
tile_has_tag(tile: number, tag: string): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* The tiles carrying `tag`, as the domain mask `wfc_state_restrict` takes.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} tag
|
|
74
|
+
* @returns {Uint32Array} `words_per_cell` words; shared, do not write to it
|
|
75
|
+
*/
|
|
76
|
+
mask_of_tag(tag: string): Uint32Array;
|
|
77
|
+
/**
|
|
78
|
+
* The tiles carrying `tag` that show the same socket as `tile` on every edge.
|
|
79
|
+
*
|
|
80
|
+
* Such a tile can stand in for `tile` anywhere, whatever is around it: every neighbour sees
|
|
81
|
+
* the sockets it saw before. It is how a result is changed after the solve without solving
|
|
82
|
+
* again — a stretch of wall for the gap that has its sockets.
|
|
83
|
+
*
|
|
84
|
+
* @param {number} tile
|
|
85
|
+
* @param {string} tag
|
|
86
|
+
* @returns {number[]} shared, do not write to it; never includes `tile` itself
|
|
87
|
+
*/
|
|
88
|
+
twins_of(tile: number, tag: string): number[];
|
|
89
|
+
/**
|
|
90
|
+
* What may stand against each edge of the grid, as the masks `wfc_state_apply_boundary`
|
|
91
|
+
* takes.
|
|
92
|
+
*
|
|
93
|
+
* The solver leaves a border open: a cell there has no neighbour on its outward side, so
|
|
94
|
+
* nothing constrains that edge of its tile. For a multi-cell piece that would let the grid's
|
|
95
|
+
* edge cut it in two — one cell placed, the rest wanted where there are no cells. So a tile is
|
|
96
|
+
* never allowed to show the inside of a piece to the outside, whatever else is asked.
|
|
97
|
+
*
|
|
98
|
+
* @param {string|null} [tag] also require this tag of every tile on the border
|
|
99
|
+
* @returns {Uint32Array} `4 × words_per_cell` words, by solver direction
|
|
100
|
+
*/
|
|
101
|
+
boundary_masks(tag?: string | null): Uint32Array;
|
|
102
|
+
/**
|
|
103
|
+
* What looks wrong with the set, in words. Empty when nothing does.
|
|
104
|
+
*
|
|
105
|
+
* Two things are checked, and neither can prove a set solvable — nothing can, short of
|
|
106
|
+
* solving it:
|
|
107
|
+
* - a tile showing a socket no tile answers can only ever stand against the edge of the grid;
|
|
108
|
+
* - tiles that fall into separate groups with no adjacency between them never appear in the
|
|
109
|
+
* same result, so all but one group is dead weight.
|
|
110
|
+
*
|
|
111
|
+
* @returns {string[]}
|
|
112
|
+
*/
|
|
113
|
+
lint(): string[];
|
|
114
|
+
/**
|
|
115
|
+
* @param {number} tile
|
|
116
|
+
* @returns {string} the prototype's name and how far it is turned
|
|
117
|
+
*/
|
|
118
|
+
describe(tile: number): string;
|
|
119
|
+
}
|
|
120
|
+
import { WFCRuleTable } from "../../core/wfc/WFCRuleTable.js";
|
|
121
|
+
import { WFCTileWeights } from "../../core/wfc/WFCTileWeights.js";
|
|
122
|
+
import { GridWFCEdge } from "./GridWFCEdge.js";
|
|
123
|
+
//# sourceMappingURL=GridWFCCompiledTileSet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GridWFCCompiledTileSet.d.ts","sourceRoot":"","sources":["../../../../src/generation/wfc/GridWFCCompiledTileSet.js"],"names":[],"mappings":"AA2CA;;;;;GAKG;AACH;IACI;;OAEG;IACH,wBAFW;QAAC,IAAI,EAAC,MAAM,CAAC;QAAC,KAAK,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,MAAM,CAAC;QAAC,SAAS,EAAC,MAAM,CAAC;QAAC,IAAI,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,CAAC,iBAAe,IAAI,CAAC,CAAA;KAAC,EAAE,EAiHvH;IA1GG;;OAEG;IACH,YAFU;QAAC,IAAI,EAAC,MAAM,CAAC;QAAC,KAAK,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,MAAM,CAAC;QAAC,SAAS,EAAC,MAAM,CAAC;QAAC,IAAI,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,CAAC,iBAAe,IAAI,CAAC,CAAA;KAAC,EAAE,CAEnF;IAuCpC,qBAAqB;IACrB,YADW,MAAM,CACW;IAC5B,0BAA0B;IAC1B,gBADW,WAAW,CACgC;IACtD,6CAA6C;IAC7C,YAD+B,UAAU,CACI;IAC7C,uBAAuB;IACvB,YADW,MAAM,EAAE,CACS;IAyC5B,2BAA2B;IAC3B,OADW,YAAY,CACL;IAClB,6BAA6B;IAC7B,SADW,cAAc,CACiB;IAE1C;;;OAGG;IACH,oBAA4B;IAE5B;;;OAGG;IACH,oBAA4B;IAGhC;;;;;OAKG;IACH,mBAHW,WAAW,GACT,MAAM,CAIlB;IAED;;;OAGG;IACH,sBAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;OAIG;IACH,mBAJW,MAAM,OACN,MAAM,GACJ,OAAO,CAInB;IAED;;;;;OAKG;IACH,iBAHW,MAAM,GACJ,WAAW,CAoBvB;IAED;;;;;;;;;;OAUG;IACH,eAJW,MAAM,OACN,MAAM,GACJ,MAAM,EAAE,CAoCpB;IAED;;;;;;;;;;;OAWG;IACH,qBAHW,MAAM,GAAC,IAAI,GACT,WAAW,CAuBvB;IAED;;;;;;;;;;OAUG;IACH,QAFa,MAAM,EAAE,CAiCpB;IAED;;;OAGG;IACH,eAHW,MAAM,GACJ,MAAM,CAMlB;CACJ;6BAjW4B,gCAAgC;+BAC9B,kCAAkC;4BAGrC,kBAAkB"}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { assert } from "../../core/assert.js";
|
|
2
|
+
import { WFCRuleTable } from "../../core/wfc/WFCRuleTable.js";
|
|
3
|
+
import { WFCTileWeights } from "../../core/wfc/WFCTileWeights.js";
|
|
4
|
+
import { wfc_rule_table_tile_components } from "../../core/wfc/wfc_rule_table_tile_components.js";
|
|
5
|
+
import { GRID_WFC_PIECE_SOCKET_PREFIX } from "./GRID_WFC_PIECE_SOCKET_PREFIX.js";
|
|
6
|
+
import { GridWFCEdge } from "./GridWFCEdge.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Edge `e` of a tile faces edge `e ^ 1` of its neighbour on that side.
|
|
10
|
+
*
|
|
11
|
+
* @param {number} edge
|
|
12
|
+
* @returns {number}
|
|
13
|
+
*/
|
|
14
|
+
function opposite_edge(edge) {
|
|
15
|
+
return edge ^ 1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where edge `e` ends up after one quarter turn, in the convention of `v2_rotate` — which is the
|
|
20
|
+
* one every {@link GridCellAction} already applies to its `rotation`: +x turns onto +y, +y onto
|
|
21
|
+
* -x, -x onto -y and -y onto +x.
|
|
22
|
+
*
|
|
23
|
+
* @type {number[]}
|
|
24
|
+
*/
|
|
25
|
+
const QUARTER_TURN = [];
|
|
26
|
+
QUARTER_TURN[GridWFCEdge.PositiveX] = GridWFCEdge.PositiveY;
|
|
27
|
+
QUARTER_TURN[GridWFCEdge.PositiveY] = GridWFCEdge.NegativeX;
|
|
28
|
+
QUARTER_TURN[GridWFCEdge.NegativeX] = GridWFCEdge.NegativeY;
|
|
29
|
+
QUARTER_TURN[GridWFCEdge.NegativeY] = GridWFCEdge.PositiveX;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The lattice the grid task solves on is `[height, width]`, so that a cell's index there is the
|
|
33
|
+
* grid's own `y * width + x`. That puts y on axis 0 and x on axis 1, and a solver direction is
|
|
34
|
+
* `axis << 1 | side`.
|
|
35
|
+
*
|
|
36
|
+
* @type {number[]}
|
|
37
|
+
*/
|
|
38
|
+
const EDGE_TO_DIRECTION = [];
|
|
39
|
+
EDGE_TO_DIRECTION[GridWFCEdge.NegativeX] = 2;
|
|
40
|
+
EDGE_TO_DIRECTION[GridWFCEdge.PositiveX] = 3;
|
|
41
|
+
EDGE_TO_DIRECTION[GridWFCEdge.NegativeY] = 0;
|
|
42
|
+
EDGE_TO_DIRECTION[GridWFCEdge.PositiveY] = 1;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A {@link GridWFCTileSet} expanded for the solver: every turned copy of every prototype is a tile
|
|
46
|
+
* of its own, and the sockets have become a {@link WFCRuleTable}.
|
|
47
|
+
*
|
|
48
|
+
* Read-only, and shared by every solve over the set.
|
|
49
|
+
*/
|
|
50
|
+
export class GridWFCCompiledTileSet {
|
|
51
|
+
/**
|
|
52
|
+
* @param {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}[]} prototypes
|
|
53
|
+
*/
|
|
54
|
+
constructor(prototypes) {
|
|
55
|
+
if (prototypes.length === 0) {
|
|
56
|
+
throw new Error('a tile set needs at least one tile');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @type {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}[]}
|
|
61
|
+
*/
|
|
62
|
+
this.prototypes = prototypes.slice();
|
|
63
|
+
|
|
64
|
+
/** prototype of each tile @type {number[]} */
|
|
65
|
+
const tile_prototype = [];
|
|
66
|
+
/** quarter turns of each tile @type {number[]} */
|
|
67
|
+
const tile_turns = [];
|
|
68
|
+
/** socket on each edge of each tile, four per tile @type {string[]} */
|
|
69
|
+
const tile_edges = [];
|
|
70
|
+
const weights = [];
|
|
71
|
+
|
|
72
|
+
for (let p = 0; p < prototypes.length; p++) {
|
|
73
|
+
const prototype = prototypes[p];
|
|
74
|
+
|
|
75
|
+
for (let turns = 0; turns < prototype.rotations; turns++) {
|
|
76
|
+
const edges = new Array(4);
|
|
77
|
+
|
|
78
|
+
for (let e = 0; e < 4; e++) {
|
|
79
|
+
let to = e;
|
|
80
|
+
|
|
81
|
+
for (let k = 0; k < turns; k++) {
|
|
82
|
+
to = QUARTER_TURN[to];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const socket = prototype.edges[e];
|
|
86
|
+
|
|
87
|
+
// the inside of a piece turned one way must not fit the inside of it turned another
|
|
88
|
+
edges[to] = socket.startsWith(GRID_WFC_PIECE_SOCKET_PREFIX) ? `${socket}@${turns}` : socket;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
tile_prototype.push(p);
|
|
92
|
+
tile_turns.push(turns);
|
|
93
|
+
tile_edges.push(edges[0], edges[1], edges[2], edges[3]);
|
|
94
|
+
// a prototype's weight is its own, however many ways it can be turned
|
|
95
|
+
weights.push(prototype.weight / prototype.rotations);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const tile_count = tile_prototype.length;
|
|
100
|
+
|
|
101
|
+
/** @type {number} */
|
|
102
|
+
this.tile_count = tile_count;
|
|
103
|
+
/** @type {Uint32Array} */
|
|
104
|
+
this.tile_prototype = Uint32Array.from(tile_prototype);
|
|
105
|
+
/** quarter turns, 0..3 @type {Uint8Array} */
|
|
106
|
+
this.tile_turns = Uint8Array.from(tile_turns);
|
|
107
|
+
/** @type {string[]} */
|
|
108
|
+
this.tile_edges = tile_edges;
|
|
109
|
+
|
|
110
|
+
const rules = new WFCRuleTable(tile_count, 4);
|
|
111
|
+
|
|
112
|
+
rules.fill_deny();
|
|
113
|
+
|
|
114
|
+
// tiles by the socket they show on each edge, so matching is by lookup rather than all pairs
|
|
115
|
+
const by_socket = [new Map(), new Map(), new Map(), new Map()];
|
|
116
|
+
|
|
117
|
+
for (let t = 0; t < tile_count; t++) {
|
|
118
|
+
for (let e = 0; e < 4; e++) {
|
|
119
|
+
const socket = tile_edges[t * 4 + e];
|
|
120
|
+
const list = by_socket[e].get(socket);
|
|
121
|
+
|
|
122
|
+
if (list === undefined) {
|
|
123
|
+
by_socket[e].set(socket, [t]);
|
|
124
|
+
} else {
|
|
125
|
+
list.push(t);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// the positive side of each axis is enough: allow_symmetric writes the other
|
|
131
|
+
for (const e of [GridWFCEdge.PositiveX, GridWFCEdge.PositiveY]) {
|
|
132
|
+
const direction = EDGE_TO_DIRECTION[e];
|
|
133
|
+
|
|
134
|
+
for (let a = 0; a < tile_count; a++) {
|
|
135
|
+
const facing = by_socket[opposite_edge(e)].get(tile_edges[a * 4 + e]);
|
|
136
|
+
|
|
137
|
+
if (facing === undefined) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (let i = 0; i < facing.length; i++) {
|
|
142
|
+
rules.allow_symmetric(direction, a, facing[i]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
rules.finalize();
|
|
148
|
+
|
|
149
|
+
/** @type {WFCRuleTable} */
|
|
150
|
+
this.rules = rules;
|
|
151
|
+
/** @type {WFCTileWeights} */
|
|
152
|
+
this.weights = new WFCTileWeights(weights);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @type {Map<string, Uint32Array>}
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
this.__tag_masks = new Map();
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @type {Map<string, number[][]>}
|
|
162
|
+
* @private
|
|
163
|
+
*/
|
|
164
|
+
this.__tag_twins = new Map();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The solver direction a tile edge faces.
|
|
169
|
+
*
|
|
170
|
+
* @param {GridWFCEdge} edge
|
|
171
|
+
* @returns {number}
|
|
172
|
+
*/
|
|
173
|
+
direction_of(edge) {
|
|
174
|
+
return EDGE_TO_DIRECTION[edge];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @param {string} name
|
|
179
|
+
* @returns {number} prototype index, or -1
|
|
180
|
+
*/
|
|
181
|
+
prototype_named(name) {
|
|
182
|
+
return this.prototypes.findIndex(p => p.name === name);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {number} tile
|
|
187
|
+
* @param {string} tag
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
*/
|
|
190
|
+
tile_has_tag(tile, tag) {
|
|
191
|
+
return this.prototypes[this.tile_prototype[tile]].tags.includes(tag);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The tiles carrying `tag`, as the domain mask `wfc_state_restrict` takes.
|
|
196
|
+
*
|
|
197
|
+
* @param {string} tag
|
|
198
|
+
* @returns {Uint32Array} `words_per_cell` words; shared, do not write to it
|
|
199
|
+
*/
|
|
200
|
+
mask_of_tag(tag) {
|
|
201
|
+
assert.isString(tag, 'tag');
|
|
202
|
+
|
|
203
|
+
let mask = this.__tag_masks.get(tag);
|
|
204
|
+
|
|
205
|
+
if (mask === undefined) {
|
|
206
|
+
mask = new Uint32Array(this.rules.words_per_cell);
|
|
207
|
+
|
|
208
|
+
for (let t = 0; t < this.tile_count; t++) {
|
|
209
|
+
if (this.tile_has_tag(t, tag)) {
|
|
210
|
+
mask[t >> 5] |= (1 << (t & 31)) >>> 0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.__tag_masks.set(tag, mask);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return mask;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The tiles carrying `tag` that show the same socket as `tile` on every edge.
|
|
222
|
+
*
|
|
223
|
+
* Such a tile can stand in for `tile` anywhere, whatever is around it: every neighbour sees
|
|
224
|
+
* the sockets it saw before. It is how a result is changed after the solve without solving
|
|
225
|
+
* again — a stretch of wall for the gap that has its sockets.
|
|
226
|
+
*
|
|
227
|
+
* @param {number} tile
|
|
228
|
+
* @param {string} tag
|
|
229
|
+
* @returns {number[]} shared, do not write to it; never includes `tile` itself
|
|
230
|
+
*/
|
|
231
|
+
twins_of(tile, tag) {
|
|
232
|
+
let twins = this.__tag_twins.get(tag);
|
|
233
|
+
|
|
234
|
+
if (twins === undefined) {
|
|
235
|
+
const edges = this.tile_edges;
|
|
236
|
+
const by_sockets = new Map();
|
|
237
|
+
|
|
238
|
+
for (let t = 0; t < this.tile_count; t++) {
|
|
239
|
+
if (!this.tile_has_tag(t, tag)) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const key = JSON.stringify(edges.slice(t * 4, t * 4 + 4));
|
|
244
|
+
const list = by_sockets.get(key);
|
|
245
|
+
|
|
246
|
+
if (list === undefined) {
|
|
247
|
+
by_sockets.set(key, [t]);
|
|
248
|
+
} else {
|
|
249
|
+
list.push(t);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
twins = [];
|
|
254
|
+
|
|
255
|
+
for (let t = 0; t < this.tile_count; t++) {
|
|
256
|
+
const same = by_sockets.get(JSON.stringify(edges.slice(t * 4, t * 4 + 4))) ?? [];
|
|
257
|
+
|
|
258
|
+
twins.push(same.filter(other => other !== t));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
this.__tag_twins.set(tag, twins);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return twins[tile];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* What may stand against each edge of the grid, as the masks `wfc_state_apply_boundary`
|
|
269
|
+
* takes.
|
|
270
|
+
*
|
|
271
|
+
* The solver leaves a border open: a cell there has no neighbour on its outward side, so
|
|
272
|
+
* nothing constrains that edge of its tile. For a multi-cell piece that would let the grid's
|
|
273
|
+
* edge cut it in two — one cell placed, the rest wanted where there are no cells. So a tile is
|
|
274
|
+
* never allowed to show the inside of a piece to the outside, whatever else is asked.
|
|
275
|
+
*
|
|
276
|
+
* @param {string|null} [tag] also require this tag of every tile on the border
|
|
277
|
+
* @returns {Uint32Array} `4 × words_per_cell` words, by solver direction
|
|
278
|
+
*/
|
|
279
|
+
boundary_masks(tag = null) {
|
|
280
|
+
const words = this.rules.words_per_cell;
|
|
281
|
+
const masks = new Uint32Array(4 * words);
|
|
282
|
+
|
|
283
|
+
for (let e = 0; e < 4; e++) {
|
|
284
|
+
const base = EDGE_TO_DIRECTION[e] * words;
|
|
285
|
+
|
|
286
|
+
for (let t = 0; t < this.tile_count; t++) {
|
|
287
|
+
if (this.tile_edges[t * 4 + e].startsWith(GRID_WFC_PIECE_SOCKET_PREFIX)) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (tag !== null && !this.tile_has_tag(t, tag)) {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
masks[base + (t >> 5)] |= (1 << (t & 31)) >>> 0;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return masks;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* What looks wrong with the set, in words. Empty when nothing does.
|
|
304
|
+
*
|
|
305
|
+
* Two things are checked, and neither can prove a set solvable — nothing can, short of
|
|
306
|
+
* solving it:
|
|
307
|
+
* - a tile showing a socket no tile answers can only ever stand against the edge of the grid;
|
|
308
|
+
* - tiles that fall into separate groups with no adjacency between them never appear in the
|
|
309
|
+
* same result, so all but one group is dead weight.
|
|
310
|
+
*
|
|
311
|
+
* @returns {string[]}
|
|
312
|
+
*/
|
|
313
|
+
lint() {
|
|
314
|
+
const result = [];
|
|
315
|
+
const edge_name = [];
|
|
316
|
+
|
|
317
|
+
edge_name[EDGE_TO_DIRECTION[GridWFCEdge.NegativeX]] = '-x';
|
|
318
|
+
edge_name[EDGE_TO_DIRECTION[GridWFCEdge.PositiveX]] = '+x';
|
|
319
|
+
edge_name[EDGE_TO_DIRECTION[GridWFCEdge.NegativeY]] = '-y';
|
|
320
|
+
edge_name[EDGE_TO_DIRECTION[GridWFCEdge.PositiveY]] = '+y';
|
|
321
|
+
|
|
322
|
+
for (const { direction, tile } of this.rules.rows_without_support()) {
|
|
323
|
+
result.push(`${this.describe(tile)}: nothing fits its ${edge_name[direction]} edge`);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const component = new Uint32Array(this.tile_count);
|
|
327
|
+
const count = wfc_rule_table_tile_components(component, this.rules);
|
|
328
|
+
|
|
329
|
+
if (count > 1) {
|
|
330
|
+
for (let c = 1; c < count; c++) {
|
|
331
|
+
const members = [];
|
|
332
|
+
|
|
333
|
+
for (let t = 0; t < this.tile_count; t++) {
|
|
334
|
+
if (component[t] === c) {
|
|
335
|
+
members.push(this.describe(t));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
result.push(`can never appear beside ${this.describe(0)}: ${members.join(', ')}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* @param {number} tile
|
|
348
|
+
* @returns {string} the prototype's name and how far it is turned
|
|
349
|
+
*/
|
|
350
|
+
describe(tile) {
|
|
351
|
+
const prototype = this.prototypes[this.tile_prototype[tile]];
|
|
352
|
+
|
|
353
|
+
return prototype.rotations === 1 ? prototype.name : `${prototype.name} (${this.tile_turns[tile] * 90}°)`;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four edges of a grid tile, in the order a {@link GridWFCTileSet } tile lists its sockets.
|
|
3
|
+
*
|
|
4
|
+
* An edge and the one facing it across a shared border differ in the lowest bit.
|
|
5
|
+
*/
|
|
6
|
+
export type GridWFCEdge = number;
|
|
7
|
+
export namespace GridWFCEdge {
|
|
8
|
+
let NegativeX: number;
|
|
9
|
+
let PositiveX: number;
|
|
10
|
+
let NegativeY: number;
|
|
11
|
+
let PositiveY: number;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=GridWFCEdge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GridWFCEdge.d.ts","sourceRoot":"","sources":["../../../../src/generation/wfc/GridWFCEdge.js"],"names":[],"mappings":";;;;;0BAMU,MAAM"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four edges of a grid tile, in the order a {@link GridWFCTileSet} tile lists its sockets.
|
|
3
|
+
*
|
|
4
|
+
* An edge and the one facing it across a shared border differ in the lowest bit.
|
|
5
|
+
*
|
|
6
|
+
* @readonly
|
|
7
|
+
* @enum {number}
|
|
8
|
+
*/
|
|
9
|
+
export const GridWFCEdge = {
|
|
10
|
+
NegativeX: 0,
|
|
11
|
+
PositiveX: 1,
|
|
12
|
+
NegativeY: 2,
|
|
13
|
+
PositiveY: 3
|
|
14
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A set of tiles for a 2D grid, described the way they are drawn: each tile names the **socket**
|
|
3
|
+
* on each of its four edges, and two tiles may sit side by side when the sockets on their shared
|
|
4
|
+
* edge are the same.
|
|
5
|
+
*
|
|
6
|
+
* This is the authoring half of wave function collapse. The solver in `core/wfc/` knows a tile
|
|
7
|
+
* only as an integer and a rule only as a bit; {@link compile} is what turns turned copies and
|
|
8
|
+
* multi-cell pieces into distinct integers and their sockets into that table.
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const tiles = new GridWFCTileSet();
|
|
12
|
+
*
|
|
13
|
+
* // -x +x -y +y
|
|
14
|
+
* tiles.add({ name: 'open', edges: ['open', 'open', 'open', 'open'], weight: 20, tags: ['walkable'] });
|
|
15
|
+
* tiles.add({ name: 'wall', edges: ['wall', 'wall', 'open', 'open'], rotations: 2, action: place_wall });
|
|
16
|
+
* tiles.add({ name: 'bend', edges: ['open', 'wall', 'open', 'wall'], rotations: 4, action: place_bend });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* A socket has no handedness: it matches itself, read from either side. That is enough for
|
|
20
|
+
* anything centred on its edge (a wall, a path, a region's border) and is not enough for a feature
|
|
21
|
+
* that crosses an edge off-centre.
|
|
22
|
+
*/
|
|
23
|
+
export class GridWFCTileSet {
|
|
24
|
+
/**
|
|
25
|
+
* Tiles as they were added — one per {@link add} call, one per cell of an {@link add_piece}.
|
|
26
|
+
*
|
|
27
|
+
* @type {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}[]}
|
|
28
|
+
*/
|
|
29
|
+
prototypes: {
|
|
30
|
+
name: string;
|
|
31
|
+
edges: string[];
|
|
32
|
+
weight: number;
|
|
33
|
+
rotations: number;
|
|
34
|
+
tags: string[];
|
|
35
|
+
action: (GridCellAction | null);
|
|
36
|
+
}[];
|
|
37
|
+
/**
|
|
38
|
+
* @type {number}
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
private __piece_count;
|
|
42
|
+
/**
|
|
43
|
+
* @type {GridWFCCompiledTileSet|null}
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
private __compiled;
|
|
47
|
+
/**
|
|
48
|
+
* Add a tile that occupies one cell.
|
|
49
|
+
*
|
|
50
|
+
* @param {Object} options
|
|
51
|
+
* @param {string} options.name for diagnostics, and how {@link GridWFCCompiledTileSet#prototype_named} finds it
|
|
52
|
+
* @param {string[]} options.edges socket per edge, in {@link GridWFCEdge} order: -x, +x, -y, +y
|
|
53
|
+
* @param {number} [options.weight=1] relative likelihood of the tile, shared between its turned copies
|
|
54
|
+
* @param {number} [options.rotations=1] how many distinct quarter turns it has: 1 for a tile
|
|
55
|
+
* that looks the same turned any way, 2 for one with a half-turn symmetry (a straight
|
|
56
|
+
* wall), 4 for one with none (a bend)
|
|
57
|
+
* @param {string[]} [options.tags] what the tile is, for restricting cells to a kind of tile
|
|
58
|
+
* @param {GridCellAction|null} [options.action] run on the cell the tile lands in, with the
|
|
59
|
+
* tile's rotation in radians
|
|
60
|
+
* @returns {number} index of the prototype
|
|
61
|
+
*/
|
|
62
|
+
add({ name, edges, weight, rotations, tags, action }: {
|
|
63
|
+
name: string;
|
|
64
|
+
edges: string[];
|
|
65
|
+
weight?: number;
|
|
66
|
+
rotations?: number;
|
|
67
|
+
tags?: string[];
|
|
68
|
+
action?: GridCellAction | null;
|
|
69
|
+
}): number;
|
|
70
|
+
/**
|
|
71
|
+
* Add a piece that occupies several cells and only ever appears whole: a building two cells by
|
|
72
|
+
* three, a gatehouse with its two towers.
|
|
73
|
+
*
|
|
74
|
+
* Each cell becomes a tile of its own, and the edges between the piece's cells get sockets
|
|
75
|
+
* nothing else carries — so wherever one cell of it lands, propagation forces the rest.
|
|
76
|
+
*
|
|
77
|
+
* @param {Object} options
|
|
78
|
+
* @param {string} options.name
|
|
79
|
+
* @param {{x:number, y:number, edges?:string[], tags?:string[], action?:(GridCellAction|null)}[]} options.cells
|
|
80
|
+
* offsets of the piece's cells at rotation 0, every cell sharing an edge with another —
|
|
81
|
+
* cells that only touch at a corner, or not at all, would be placed apart, so they are an
|
|
82
|
+
* error. `edges` gives a cell's OUTWARD-facing
|
|
83
|
+
* sockets (entries for edges that face another cell of the piece are ignored) and
|
|
84
|
+
* defaults to `edge` all round. `action` usually sits on one cell only — the one the
|
|
85
|
+
* piece's model stands on.
|
|
86
|
+
* @param {string} [options.edge] socket of every outward-facing edge a cell does not name
|
|
87
|
+
* @param {number} [options.weight=1]
|
|
88
|
+
* @param {number} [options.rotations=1]
|
|
89
|
+
* @param {string[]} [options.tags] carried by every cell of the piece, with the cell's own
|
|
90
|
+
* @returns {number[]} index of each cell's prototype, in the order of `cells`
|
|
91
|
+
*/
|
|
92
|
+
add_piece({ name, cells, edge, weight, rotations, tags }: {
|
|
93
|
+
name: string;
|
|
94
|
+
cells: {
|
|
95
|
+
x: number;
|
|
96
|
+
y: number;
|
|
97
|
+
edges?: string[];
|
|
98
|
+
tags?: string[];
|
|
99
|
+
action?: (GridCellAction | null);
|
|
100
|
+
}[];
|
|
101
|
+
edge?: string;
|
|
102
|
+
weight?: number;
|
|
103
|
+
rotations?: number;
|
|
104
|
+
tags?: string[];
|
|
105
|
+
}): number[];
|
|
106
|
+
/**
|
|
107
|
+
* @param {{name:string, edges:string[], weight:number, rotations:number, tags:string[], action:(GridCellAction|null)}} prototype
|
|
108
|
+
* @returns {number}
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
111
|
+
private __push;
|
|
112
|
+
/**
|
|
113
|
+
* The set as the solver takes it. Built once and kept until a tile is added.
|
|
114
|
+
*
|
|
115
|
+
* @returns {GridWFCCompiledTileSet}
|
|
116
|
+
*/
|
|
117
|
+
compile(): GridWFCCompiledTileSet;
|
|
118
|
+
}
|
|
119
|
+
import { GridWFCCompiledTileSet } from "./GridWFCCompiledTileSet.js";
|
|
120
|
+
//# sourceMappingURL=GridWFCTileSet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GridWFCTileSet.d.ts","sourceRoot":"","sources":["../../../../src/generation/wfc/GridWFCTileSet.js"],"names":[],"mappings":"AA+DA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH;IAEI;;;;OAIG;IACH,YAFU;QAAC,IAAI,EAAC,MAAM,CAAC;QAAC,KAAK,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,MAAM,CAAC;QAAC,SAAS,EAAC,MAAM,CAAC;QAAC,IAAI,EAAC,MAAM,EAAE,CAAC;QAAC,MAAM,EAAC,CAAC,iBAAe,IAAI,CAAC,CAAA;KAAC,EAAE,CAEvG;IAEhB;;;OAGG;IACH,sBAAkB;IAElB;;;OAGG;IACH,mBAAkB;IAElB;;;;;;;;;;;;;;OAcG;IACH;QAX2B,IAAI,EAApB,MAAM;QACY,KAAK,EAAvB,MAAM,EAAE;QACS,MAAM,GAAvB,MAAM;QACW,SAAS,GAA1B,MAAM;QAGa,IAAI,GAAvB,MAAM,EAAE;QACsB,MAAM,GAApC,iBAAe,IAAI;QAEjB,MAAM,CASlB;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH;QAd2B,IAAI,EAApB,MAAM;QAC0F,KAAK,EAArG;YAAC,CAAC,EAAC,MAAM,CAAC;YAAC,CAAC,EAAC,MAAM,CAAC;YAAC,KAAK,CAAC,EAAC,MAAM,EAAE,CAAC;YAAC,IAAI,CAAC,EAAC,MAAM,EAAE,CAAC;YAAC,MAAM,CAAC,EAAC,CAAC,iBAAe,IAAI,CAAC,CAAA;SAAC,EAAE;QAOrE,IAAI,GAArB,MAAM;QACW,MAAM,GAAvB,MAAM;QACW,SAAS,GAA1B,MAAM;QACa,IAAI,GAAvB,MAAM,EAAE;QACN,MAAM,EAAE,CAkGpB;IAED;;;;OAIG;IACH,eAKC;IAED;;;;OAIG;IACH,WAFa,sBAAsB,CAQlC;CACJ;uCAhRsC,6BAA6B"}
|