@holtjs/stage 0.4.1 → 0.5.1
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 +19 -0
- package/README.md +140 -119
- package/dist/{game-Cu1G9xgW.js → game-C5za2LGl.js} +28 -691
- package/dist/{game-GdPaQUqH.d.ts → game-CfUZiazr.d.ts} +9 -2
- package/dist/headless.d.ts +1 -1
- package/dist/headless.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/joystick.d.ts +1 -1
- package/dist/kernel.d.ts +1 -1
- package/dist/lighting.d.ts +129 -0
- package/dist/lighting.js +527 -0
- package/dist/projection.d.ts +131 -0
- package/dist/projection.js +262 -0
- package/dist/{renderer-webgpu-BorAD6Kk.js → renderer-webgpu-BE6fTBzo.js} +718 -1
- package/dist/renderer-webgpu.d.ts +1 -1
- package/dist/renderer-webgpu.js +1 -1
- package/dist/rig2d.d.ts +317 -0
- package/dist/rig2d.js +871 -0
- package/dist/tilemap.d.ts +158 -0
- package/dist/tilemap.js +583 -0
- package/dist/trace.d.ts +1 -1
- package/dist/world.d.ts +183 -0
- package/dist/world.js +400 -0
- package/llms.txt +26 -17
- package/package.json +21 -1
- package/skills/stage/SKILL.md +140 -157
- package/skills/stage/references/subpaths.md +62 -54
package/dist/tilemap.js
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
import { o as FilterMode } from "./types-DRFB0Klc.js";
|
|
2
|
+
// src/tilemap.ts
|
|
3
|
+
/** Pack signed 16-bit chunk coordinates `[-32768..32767]` into a single 32-bit integer Map key (zero string allocation). */
|
|
4
|
+
function packChunkKey(cx, cy) {
|
|
5
|
+
return cx & 65535 | (cy & 65535) << 16 | 0;
|
|
6
|
+
}
|
|
7
|
+
function unpackChunkX(key) {
|
|
8
|
+
return key << 16 >> 16;
|
|
9
|
+
}
|
|
10
|
+
function unpackChunkY(key) {
|
|
11
|
+
return key >> 16;
|
|
12
|
+
}
|
|
13
|
+
function floorDiv(a, b) {
|
|
14
|
+
return Math.floor(a / b);
|
|
15
|
+
}
|
|
16
|
+
function positiveMod(a, b) {
|
|
17
|
+
const r = a % b;
|
|
18
|
+
return r < 0 ? r + b : r;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Compute Oskar Stålberg's 4-bit **Dual-Grid** mask (`0..15`) for visual vertex `(vx, vy)`,
|
|
22
|
+
* which sits at world `(vx * tileSize - tileSize * 0.5, vy * tileSize - tileSize * 0.5)` and samples
|
|
23
|
+
* the 4 surrounding logical cells `(vx-1, vy-1)`, `(vx, vy-1)`, `(vx-1, vy)`, `(vx, vy)`.
|
|
24
|
+
*
|
|
25
|
+
* Bit layout: `TL (8) | TR (4) | BL (2) | BR (1)`.
|
|
26
|
+
* - `0` = completely empty (do not draw this overlay layer)
|
|
27
|
+
* - `15` = solid interior fill
|
|
28
|
+
* - `1..14` = 14 organic rounded corners, edges, and diagonal saddles (`6` and `9`)
|
|
29
|
+
*/
|
|
30
|
+
function computeDualGridMask(tl, tr, bl, br) {
|
|
31
|
+
return (tl ? 8 : 0) | (tr ? 4 : 0) | (bl ? 2 : 0) | (br ? 1 : 0);
|
|
32
|
+
}
|
|
33
|
+
const BLOB47_CANONICAL_MASKS = [
|
|
34
|
+
0,
|
|
35
|
+
1,
|
|
36
|
+
2,
|
|
37
|
+
4,
|
|
38
|
+
8,
|
|
39
|
+
3,
|
|
40
|
+
6,
|
|
41
|
+
12,
|
|
42
|
+
9,
|
|
43
|
+
5,
|
|
44
|
+
10,
|
|
45
|
+
7,
|
|
46
|
+
14,
|
|
47
|
+
13,
|
|
48
|
+
11,
|
|
49
|
+
15,
|
|
50
|
+
19,
|
|
51
|
+
38,
|
|
52
|
+
76,
|
|
53
|
+
137,
|
|
54
|
+
23,
|
|
55
|
+
39,
|
|
56
|
+
54,
|
|
57
|
+
114,
|
|
58
|
+
108,
|
|
59
|
+
204,
|
|
60
|
+
139,
|
|
61
|
+
153,
|
|
62
|
+
55,
|
|
63
|
+
118,
|
|
64
|
+
205,
|
|
65
|
+
155,
|
|
66
|
+
31,
|
|
67
|
+
47,
|
|
68
|
+
79,
|
|
69
|
+
143,
|
|
70
|
+
62,
|
|
71
|
+
115,
|
|
72
|
+
158,
|
|
73
|
+
206,
|
|
74
|
+
124,
|
|
75
|
+
209,
|
|
76
|
+
219,
|
|
77
|
+
63,
|
|
78
|
+
127,
|
|
79
|
+
223,
|
|
80
|
+
255
|
|
81
|
+
];
|
|
82
|
+
const BLOB47_LOOKUP = (() => {
|
|
83
|
+
const lut = /* @__PURE__ */ new Uint8Array(256);
|
|
84
|
+
for (let i = 0; i < BLOB47_CANONICAL_MASKS.length; i++) lut[BLOB47_CANONICAL_MASKS[i]] = i;
|
|
85
|
+
return lut;
|
|
86
|
+
})();
|
|
87
|
+
/**
|
|
88
|
+
* Compute the canonical **47-tile Blob** index (`0..46`) from 8 neighbors (`n, ne, e, se, s, sw, w, nw`).
|
|
89
|
+
* Diagonal bits are automatically gated by their two adjacent cardinal neighbors so invalid corner combinations collapse cleanly.
|
|
90
|
+
*/
|
|
91
|
+
function computeBlob47Index(n, ne, e, se, s, sw, w, nw) {
|
|
92
|
+
let mask = (n ? 1 : 0) | (e ? 2 : 0) | (s ? 4 : 0) | (w ? 8 : 0);
|
|
93
|
+
if (n && e && ne) mask |= 16;
|
|
94
|
+
if (s && e && se) mask |= 32;
|
|
95
|
+
if (s && w && sw) mask |= 64;
|
|
96
|
+
if (n && w && nw) mask |= 128;
|
|
97
|
+
return BLOB47_LOOKUP[mask] ?? 0;
|
|
98
|
+
}
|
|
99
|
+
const TileCellFlags = {
|
|
100
|
+
NONE: 0,
|
|
101
|
+
SOLID: 1,
|
|
102
|
+
WATER: 2,
|
|
103
|
+
MODIFIED: 4
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Sparse chunked 2D world grid supporting massive/infinite survival worlds (`+/-1,000,000` cells)
|
|
107
|
+
* with lazy procedural chunk creation, cross-border autotile dirty propagation, and O(1) nav collision queries.
|
|
108
|
+
*/
|
|
109
|
+
var ChunkedTilemap = class {
|
|
110
|
+
chunkSize;
|
|
111
|
+
tileSize;
|
|
112
|
+
defaultGround;
|
|
113
|
+
chunks = /* @__PURE__ */ new Map();
|
|
114
|
+
generator;
|
|
115
|
+
globalRevision = 1;
|
|
116
|
+
constructor(opts = {}) {
|
|
117
|
+
const cs = Math.max(4, Math.round(opts.chunkSize ?? 16));
|
|
118
|
+
this.chunkSize = cs;
|
|
119
|
+
this.tileSize = Math.max(1, Math.round(opts.tileSize ?? 32));
|
|
120
|
+
this.defaultGround = opts.defaultGround ?? 0;
|
|
121
|
+
this.generator = opts.generator;
|
|
122
|
+
}
|
|
123
|
+
get loadedChunkCount() {
|
|
124
|
+
return this.chunks.size;
|
|
125
|
+
}
|
|
126
|
+
get revision() {
|
|
127
|
+
return this.globalRevision;
|
|
128
|
+
}
|
|
129
|
+
setGenerator(gen) {
|
|
130
|
+
this.generator = gen;
|
|
131
|
+
}
|
|
132
|
+
hasChunk(cx, cy) {
|
|
133
|
+
return this.chunks.has(packChunkKey(cx, cy));
|
|
134
|
+
}
|
|
135
|
+
peekChunk(cx, cy) {
|
|
136
|
+
return this.chunks.get(packChunkKey(cx, cy));
|
|
137
|
+
}
|
|
138
|
+
getOrCreateChunk(cx, cy) {
|
|
139
|
+
const key = packChunkKey(cx, cy);
|
|
140
|
+
let ch = this.chunks.get(key);
|
|
141
|
+
if (ch) return ch;
|
|
142
|
+
const len = this.chunkSize * this.chunkSize;
|
|
143
|
+
const ground = new Uint16Array(len);
|
|
144
|
+
if (this.defaultGround !== 0) ground.fill(this.defaultGround);
|
|
145
|
+
ch = {
|
|
146
|
+
cx,
|
|
147
|
+
cy,
|
|
148
|
+
size: this.chunkSize,
|
|
149
|
+
ground,
|
|
150
|
+
overlay: new Uint16Array(len),
|
|
151
|
+
flags: new Uint8Array(len),
|
|
152
|
+
version: 1,
|
|
153
|
+
modified: false
|
|
154
|
+
};
|
|
155
|
+
this.chunks.set(key, ch);
|
|
156
|
+
if (this.generator) {
|
|
157
|
+
this.generator(cx, cy, ch);
|
|
158
|
+
ch.modified = false;
|
|
159
|
+
}
|
|
160
|
+
return ch;
|
|
161
|
+
}
|
|
162
|
+
/** Iterate over all currently materialized chunks without copying. */
|
|
163
|
+
forEachChunk(fn) {
|
|
164
|
+
for (const ch of this.chunks.values()) fn(ch);
|
|
165
|
+
}
|
|
166
|
+
unloadChunk(cx, cy) {
|
|
167
|
+
return this.chunks.delete(packChunkKey(cx, cy));
|
|
168
|
+
}
|
|
169
|
+
worldToTileX(wx) {
|
|
170
|
+
return Math.floor(wx / this.tileSize);
|
|
171
|
+
}
|
|
172
|
+
worldToTileY(wy) {
|
|
173
|
+
return Math.floor(wy / this.tileSize);
|
|
174
|
+
}
|
|
175
|
+
tileToWorldCenter(tx, ty, out = {
|
|
176
|
+
x: 0,
|
|
177
|
+
y: 0
|
|
178
|
+
}) {
|
|
179
|
+
out.x = (tx + .5) * this.tileSize;
|
|
180
|
+
out.y = (ty + .5) * this.tileSize;
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
getGround(tx, ty, createIfMissing = true) {
|
|
184
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
185
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
186
|
+
const ch = createIfMissing ? this.getOrCreateChunk(cx, cy) : this.peekChunk(cx, cy);
|
|
187
|
+
if (!ch) return this.defaultGround;
|
|
188
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
189
|
+
const ly = positiveMod(ty, this.chunkSize);
|
|
190
|
+
return ch.ground[ly * this.chunkSize + lx];
|
|
191
|
+
}
|
|
192
|
+
setGround(tx, ty, turfId, markModified = true) {
|
|
193
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
194
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
195
|
+
const ch = this.getOrCreateChunk(cx, cy);
|
|
196
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
197
|
+
const ly = positiveMod(ty, this.chunkSize);
|
|
198
|
+
const idx = ly * this.chunkSize + lx;
|
|
199
|
+
if (ch.ground[idx] === turfId && (!markModified || (ch.flags[idx] & TileCellFlags.MODIFIED) !== 0)) return;
|
|
200
|
+
ch.ground[idx] = turfId;
|
|
201
|
+
if (markModified) {
|
|
202
|
+
ch.flags[idx] |= TileCellFlags.MODIFIED;
|
|
203
|
+
ch.modified = true;
|
|
204
|
+
}
|
|
205
|
+
this.bumpChunkAndBorderNeighbors(ch, lx, ly);
|
|
206
|
+
}
|
|
207
|
+
getOverlay(tx, ty, createIfMissing = false) {
|
|
208
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
209
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
210
|
+
const ch = createIfMissing ? this.getOrCreateChunk(cx, cy) : this.peekChunk(cx, cy);
|
|
211
|
+
if (!ch) return 0;
|
|
212
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
213
|
+
const ly = positiveMod(ty, this.chunkSize);
|
|
214
|
+
return ch.overlay[ly * this.chunkSize + lx];
|
|
215
|
+
}
|
|
216
|
+
setOverlay(tx, ty, overlayId, solid, markModified = true) {
|
|
217
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
218
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
219
|
+
const ch = this.getOrCreateChunk(cx, cy);
|
|
220
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
221
|
+
const ly = positiveMod(ty, this.chunkSize);
|
|
222
|
+
const idx = ly * this.chunkSize + lx;
|
|
223
|
+
ch.overlay[idx] = overlayId;
|
|
224
|
+
if (solid !== void 0) {
|
|
225
|
+
if (solid) ch.flags[idx] |= TileCellFlags.SOLID;
|
|
226
|
+
else ch.flags[idx] &= ~TileCellFlags.SOLID;
|
|
227
|
+
}
|
|
228
|
+
if (markModified) {
|
|
229
|
+
ch.flags[idx] |= TileCellFlags.MODIFIED;
|
|
230
|
+
ch.modified = true;
|
|
231
|
+
}
|
|
232
|
+
this.bumpChunkAndBorderNeighbors(ch, lx, ly);
|
|
233
|
+
}
|
|
234
|
+
getFlags(tx, ty, createIfMissing = false) {
|
|
235
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
236
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
237
|
+
const ch = createIfMissing ? this.getOrCreateChunk(cx, cy) : this.peekChunk(cx, cy);
|
|
238
|
+
if (!ch) return 0;
|
|
239
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
240
|
+
const ly = positiveMod(ty, this.chunkSize);
|
|
241
|
+
return ch.flags[ly * this.chunkSize + lx];
|
|
242
|
+
}
|
|
243
|
+
setSolid(tx, ty, solid, markModified = true) {
|
|
244
|
+
const cx = floorDiv(tx, this.chunkSize);
|
|
245
|
+
const cy = floorDiv(ty, this.chunkSize);
|
|
246
|
+
const ch = this.getOrCreateChunk(cx, cy);
|
|
247
|
+
const lx = positiveMod(tx, this.chunkSize);
|
|
248
|
+
const idx = positiveMod(ty, this.chunkSize) * this.chunkSize + lx;
|
|
249
|
+
if (solid) ch.flags[idx] |= TileCellFlags.SOLID;
|
|
250
|
+
else ch.flags[idx] &= ~TileCellFlags.SOLID;
|
|
251
|
+
if (markModified) {
|
|
252
|
+
ch.flags[idx] |= TileCellFlags.MODIFIED;
|
|
253
|
+
ch.modified = true;
|
|
254
|
+
}
|
|
255
|
+
ch.version = ++this.globalRevision;
|
|
256
|
+
}
|
|
257
|
+
isWalkable(tx, ty, createIfMissing = true) {
|
|
258
|
+
return (this.getFlags(tx, ty, createIfMissing) & (TileCellFlags.SOLID | TileCellFlags.WATER)) === 0;
|
|
259
|
+
}
|
|
260
|
+
/** Sample the 4-bit Dual-Grid mask (`0..15`) for visual vertex `(vx, vy)` matching `predicate(turfId)`. */
|
|
261
|
+
sampleDualGridMask(vx, vy, predicate) {
|
|
262
|
+
return computeDualGridMask(predicate(this.getGround(vx - 1, vy - 1, true)), predicate(this.getGround(vx, vy - 1, true)), predicate(this.getGround(vx - 1, vy, true)), predicate(this.getGround(vx, vy, true)));
|
|
263
|
+
}
|
|
264
|
+
/** Sample the canonical 47-tile Blob index (`0..46`) at `(tx, ty)` for wall/cliff `overlayId`. */
|
|
265
|
+
sampleOverlayBlob47(tx, ty, predicate) {
|
|
266
|
+
return computeBlob47Index(predicate(this.getOverlay(tx, ty - 1, false)), predicate(this.getOverlay(tx + 1, ty - 1, false)), predicate(this.getOverlay(tx + 1, ty, false)), predicate(this.getOverlay(tx + 1, ty + 1, false)), predicate(this.getOverlay(tx, ty + 1, false)), predicate(this.getOverlay(tx - 1, ty + 1, false)), predicate(this.getOverlay(tx - 1, ty, false)), predicate(this.getOverlay(tx - 1, ty - 1, false)));
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Extract a dense `Uint8Array` obstacle grid (`1 = blocked, 0 = walkable`) over `[originTx..originTx+cols-1, originTy..originTy+rows-1]`
|
|
270
|
+
* ready to feed directly into `FlowFieldGrid` or `findGridPath` (`@holtjs/stage/nav`).
|
|
271
|
+
*/
|
|
272
|
+
extractObstacleGrid(originTx, originTy, cols, rows, out) {
|
|
273
|
+
const buf = out && out.length >= cols * rows ? out : new Uint8Array(cols * rows);
|
|
274
|
+
let k = 0;
|
|
275
|
+
for (let r = 0; r < rows; r++) {
|
|
276
|
+
const ty = originTy + r;
|
|
277
|
+
for (let c = 0; c < cols; c++, k++) buf[k] = this.isWalkable(originTx + c, ty, true) ? 0 : 1;
|
|
278
|
+
}
|
|
279
|
+
return buf;
|
|
280
|
+
}
|
|
281
|
+
bumpChunkAndBorderNeighbors(ch, lx, ly) {
|
|
282
|
+
const rev = ++this.globalRevision;
|
|
283
|
+
ch.version = rev;
|
|
284
|
+
const last = this.chunkSize - 1;
|
|
285
|
+
const dx = lx === 0 ? -1 : lx === last ? 1 : 0;
|
|
286
|
+
const dy = ly === 0 ? -1 : ly === last ? 1 : 0;
|
|
287
|
+
if (dx !== 0) {
|
|
288
|
+
const n = this.peekChunk(ch.cx + dx, ch.cy);
|
|
289
|
+
if (n) n.version = rev;
|
|
290
|
+
}
|
|
291
|
+
if (dy !== 0) {
|
|
292
|
+
const n = this.peekChunk(ch.cx, ch.cy + dy);
|
|
293
|
+
if (n) n.version = rev;
|
|
294
|
+
}
|
|
295
|
+
if (dx !== 0 && dy !== 0) {
|
|
296
|
+
const n = this.peekChunk(ch.cx + dx, ch.cy + dy);
|
|
297
|
+
if (n) n.version = rev;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
function hash2D(ix, iy, seed) {
|
|
302
|
+
let h = Math.imul(ix, 374761393) + Math.imul(iy, 668265263) + Math.imul(seed | 0, 0x55fdc781) | 0;
|
|
303
|
+
h = Math.imul(h ^ h >>> 13, 0x4bf19f61);
|
|
304
|
+
return ((h ^ h >>> 16) >>> 0) / 4294967296;
|
|
305
|
+
}
|
|
306
|
+
function valueNoise2D(x, y, seed) {
|
|
307
|
+
const ix = Math.floor(x);
|
|
308
|
+
const iy = Math.floor(y);
|
|
309
|
+
const fx = x - ix;
|
|
310
|
+
const fy = y - iy;
|
|
311
|
+
const ux = fx * fx * (3 - 2 * fx);
|
|
312
|
+
const uy = fy * fy * (3 - 2 * fy);
|
|
313
|
+
const a = hash2D(ix, iy, seed);
|
|
314
|
+
const b = hash2D(ix + 1, iy, seed);
|
|
315
|
+
const c = hash2D(ix, iy + 1, seed);
|
|
316
|
+
const d = hash2D(ix + 1, iy + 1, seed);
|
|
317
|
+
return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
|
|
318
|
+
}
|
|
319
|
+
function fbm2D(x, y, seed, octaves = 3) {
|
|
320
|
+
let sum = 0;
|
|
321
|
+
let amp = .5;
|
|
322
|
+
let freq = 1;
|
|
323
|
+
let norm = 0;
|
|
324
|
+
for (let i = 0; i < octaves; i++) {
|
|
325
|
+
sum += valueNoise2D(x * freq, y * freq, seed + i * 1013) * amp;
|
|
326
|
+
norm += amp;
|
|
327
|
+
amp *= .5;
|
|
328
|
+
freq *= 2.03;
|
|
329
|
+
}
|
|
330
|
+
return sum / (norm || 1);
|
|
331
|
+
}
|
|
332
|
+
const SurvivalBiomes = {
|
|
333
|
+
OCEAN: 0,
|
|
334
|
+
BEACH: 1,
|
|
335
|
+
MEADOW: 2,
|
|
336
|
+
FOREST: 3,
|
|
337
|
+
GRAVEYARD_STONE: 4,
|
|
338
|
+
COBBLESTONE_ROAD: 5
|
|
339
|
+
};
|
|
340
|
+
/** Build a deterministic *Don't Starve* / *Graveyard Keeper* island chunk generator from a single integer seed. */
|
|
341
|
+
function createIslandBiomeGenerator(cfg) {
|
|
342
|
+
const seed = cfg.seed | 0;
|
|
343
|
+
const radius = Math.max(8, cfg.radiusTiles ?? 40);
|
|
344
|
+
const scale = cfg.featureScale ?? .065;
|
|
345
|
+
return (cx, cy, chunk) => {
|
|
346
|
+
const cs = chunk.size;
|
|
347
|
+
const baseTx = cx * cs;
|
|
348
|
+
const baseTy = cy * cs;
|
|
349
|
+
for (let ly = 0; ly < cs; ly++) {
|
|
350
|
+
const ty = baseTy + ly;
|
|
351
|
+
for (let lx = 0; lx < cs; lx++) {
|
|
352
|
+
const tx = baseTx + lx;
|
|
353
|
+
const idx = ly * cs + lx;
|
|
354
|
+
const dist = Math.hypot(tx, ty) / radius;
|
|
355
|
+
const elevation = fbm2D(tx * scale, ty * scale, seed, 3) - dist * dist * .62;
|
|
356
|
+
const moisture = fbm2D((tx + 400) * scale * 1.3, (ty - 300) * scale * 1.3, seed ^ 0x5bd1e995, 2);
|
|
357
|
+
const roadCenterY = Math.sin(tx * .11 + seed * .1) * 3.2;
|
|
358
|
+
const onRoad = dist < .78 && elevation > .08 && Math.abs(ty - roadCenterY) <= 1.05;
|
|
359
|
+
if (elevation < 0) {
|
|
360
|
+
chunk.ground[idx] = SurvivalBiomes.OCEAN;
|
|
361
|
+
chunk.flags[idx] = TileCellFlags.WATER;
|
|
362
|
+
} else if (onRoad) {
|
|
363
|
+
chunk.ground[idx] = SurvivalBiomes.COBBLESTONE_ROAD;
|
|
364
|
+
chunk.flags[idx] = TileCellFlags.NONE;
|
|
365
|
+
} else if (elevation < .065) {
|
|
366
|
+
chunk.ground[idx] = SurvivalBiomes.BEACH;
|
|
367
|
+
chunk.flags[idx] = TileCellFlags.NONE;
|
|
368
|
+
} else if (moisture > .62 && elevation > .18) {
|
|
369
|
+
chunk.ground[idx] = SurvivalBiomes.GRAVEYARD_STONE;
|
|
370
|
+
chunk.flags[idx] = TileCellFlags.NONE;
|
|
371
|
+
} else if (moisture > .42) {
|
|
372
|
+
chunk.ground[idx] = SurvivalBiomes.FOREST;
|
|
373
|
+
chunk.flags[idx] = TileCellFlags.NONE;
|
|
374
|
+
} else {
|
|
375
|
+
chunk.ground[idx] = SurvivalBiomes.MEADOW;
|
|
376
|
+
chunk.flags[idx] = TileCellFlags.NONE;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Bake one or more 16-tile (`0..15`) organic Dual-Grid terrain layers onto a single crisp texture atlas!
|
|
384
|
+
* Uses smooth analytical quarter-circle / saddle SDF distance fields per pixel so beaches, grass, dark forest,
|
|
385
|
+
* and cobblestone roads interlock with hand-crafted *Don't Starve* turf borders.
|
|
386
|
+
*/
|
|
387
|
+
function bakeDualGridTileset(textureKey, tileSize, layers, createCanvasFn) {
|
|
388
|
+
const ts = Math.max(8, Math.round(tileSize));
|
|
389
|
+
const cols = 16;
|
|
390
|
+
const rows = Math.max(1, layers.length);
|
|
391
|
+
const w = cols * ts;
|
|
392
|
+
const h = rows * ts;
|
|
393
|
+
const canvas = (createCanvasFn ?? ((cw, ch) => {
|
|
394
|
+
if (typeof document === "undefined") throw new Error("bakeDualGridTileset: pass createCanvasFn in headless Node environments");
|
|
395
|
+
const c = document.createElement("canvas");
|
|
396
|
+
c.width = cw;
|
|
397
|
+
c.height = ch;
|
|
398
|
+
return c;
|
|
399
|
+
}))(w, h);
|
|
400
|
+
const ctx = canvas.getContext("2d");
|
|
401
|
+
if (!ctx) throw new Error("bakeDualGridTileset: 2D context unavailable");
|
|
402
|
+
ctx.clearRect(0, 0, w, h);
|
|
403
|
+
const img = ctx.createImageData(w, h);
|
|
404
|
+
const data = img.data;
|
|
405
|
+
for (let li = 0; li < layers.length; li++) {
|
|
406
|
+
const layer = layers[li];
|
|
407
|
+
const fr = layer.fillColor >> 16 & 255;
|
|
408
|
+
const fg = layer.fillColor >> 8 & 255;
|
|
409
|
+
const fb = layer.fillColor & 255;
|
|
410
|
+
const er = layer.edgeColor >> 16 & 255;
|
|
411
|
+
const eg = layer.edgeColor >> 8 & 255;
|
|
412
|
+
const eb = layer.edgeColor & 255;
|
|
413
|
+
const sp = layer.speckleColor ?? layer.edgeColor;
|
|
414
|
+
const sr = sp >> 16 & 255;
|
|
415
|
+
const sg = sp >> 8 & 255;
|
|
416
|
+
const sb = sp & 255;
|
|
417
|
+
for (let mask = 1; mask < 16; mask++) {
|
|
418
|
+
const tl = mask & 8 ? 1 : 0;
|
|
419
|
+
const tr = mask & 4 ? 1 : 0;
|
|
420
|
+
const bl = mask & 2 ? 1 : 0;
|
|
421
|
+
const br = mask & 1 ? 1 : 0;
|
|
422
|
+
const ox = mask * ts;
|
|
423
|
+
const oy = li * ts;
|
|
424
|
+
for (let py = 0; py < ts; py++) {
|
|
425
|
+
const v = (py + .5) / ts;
|
|
426
|
+
for (let px = 0; px < ts; px++) {
|
|
427
|
+
const u = (px + .5) / ts;
|
|
428
|
+
const top = tl * (1 - u) + tr * u;
|
|
429
|
+
const bot = bl * (1 - u) + br * u;
|
|
430
|
+
let field = top * (1 - v) + bot * v;
|
|
431
|
+
const borderSafeWobble = (hash2D(px + li * 31, py + mask * 17, 777) - .5) * .14 * (4 * u * (1 - u));
|
|
432
|
+
field += borderSafeWobble * (4 * v * (1 - v));
|
|
433
|
+
if (field < .46) continue;
|
|
434
|
+
const pIdx = ((oy + py) * w + (ox + px)) * 4;
|
|
435
|
+
const isEdge = mask !== 15 && field < .56;
|
|
436
|
+
const grain = hash2D(px, py, li * 97 + mask * 13);
|
|
437
|
+
if (isEdge) {
|
|
438
|
+
data[pIdx] = er;
|
|
439
|
+
data[pIdx + 1] = eg;
|
|
440
|
+
data[pIdx + 2] = eb;
|
|
441
|
+
data[pIdx + 3] = field < .5 ? 200 : 255;
|
|
442
|
+
} else if (grain > .89) {
|
|
443
|
+
data[pIdx] = sr;
|
|
444
|
+
data[pIdx + 1] = sg;
|
|
445
|
+
data[pIdx + 2] = sb;
|
|
446
|
+
data[pIdx + 3] = 255;
|
|
447
|
+
} else {
|
|
448
|
+
data[pIdx] = fr;
|
|
449
|
+
data[pIdx + 1] = fg;
|
|
450
|
+
data[pIdx + 2] = fb;
|
|
451
|
+
data[pIdx + 3] = 255;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
ctx.putImageData(img, 0, 0);
|
|
458
|
+
return {
|
|
459
|
+
textureKey,
|
|
460
|
+
tileSize: ts,
|
|
461
|
+
canvas,
|
|
462
|
+
layers,
|
|
463
|
+
frameName: (layerId, mask) => `${layerId}:${mask & 15}`,
|
|
464
|
+
registerOnScene(scene) {
|
|
465
|
+
const tex = scene.textures.addImage(textureKey, canvas, { filterMode: FilterMode.NEAREST });
|
|
466
|
+
if (!tex) return;
|
|
467
|
+
for (let li = 0; li < layers.length; li++) {
|
|
468
|
+
const lid = layers[li].id;
|
|
469
|
+
for (let mask = 0; mask < 16; mask++) tex.add(`${lid}:${mask}`, 0, mask * ts, li * ts, ts, ts);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Mount a `ChunkedTilemap` + `BakedDualGridAtlas` onto a Stage `Scene` using one `BlitterObject` per Dual-Grid layer.
|
|
476
|
+
* Streams chunks in/out of the active viewport and only rebuilds `Bob`s for chunks whose `version` changed!
|
|
477
|
+
*/
|
|
478
|
+
function mountChunkedTilemap(scene, map, atlas, opts = {}) {
|
|
479
|
+
const root = scene.add.container(0, 0);
|
|
480
|
+
if (opts.depth !== void 0) root.setDepth(opts.depth);
|
|
481
|
+
if (opts.trackId) root.trackId = opts.trackId;
|
|
482
|
+
if (!scene.textures.exists(atlas.textureKey)) atlas.registerOnScene(scene);
|
|
483
|
+
const resolvedTex = scene.textures.get(atlas.textureKey);
|
|
484
|
+
if (!resolvedTex) throw new Error(`mountChunkedTilemap: missing atlas texture "${atlas.textureKey}"`);
|
|
485
|
+
const layers = atlas.layers;
|
|
486
|
+
const frameTable = layers.map((l) => {
|
|
487
|
+
const arr = new Array(16);
|
|
488
|
+
for (let m = 0; m < 16; m++) arr[m] = resolvedTex.get(`${l.id}:${m}`);
|
|
489
|
+
return arr;
|
|
490
|
+
});
|
|
491
|
+
const blitters = layers.map((_, i) => {
|
|
492
|
+
const b = scene.make.blitter(0, 0, atlas.textureKey);
|
|
493
|
+
b.zIndex = i;
|
|
494
|
+
root.add(b);
|
|
495
|
+
return b;
|
|
496
|
+
});
|
|
497
|
+
const mountedChunks = /* @__PURE__ */ new Map();
|
|
498
|
+
const margin = opts.marginChunks ?? 1;
|
|
499
|
+
const ts = map.tileSize;
|
|
500
|
+
const cs = map.chunkSize;
|
|
501
|
+
const halfTs = ts * .5;
|
|
502
|
+
let totalBobs = 0;
|
|
503
|
+
const buildChunkBobs = (cx, cy, version) => {
|
|
504
|
+
const bobsByLayer = layers.map(() => []);
|
|
505
|
+
const startVx = cx * cs;
|
|
506
|
+
const startVy = cy * cs;
|
|
507
|
+
for (let ly = 0; ly < cs; ly++) {
|
|
508
|
+
const vy = startVy + ly;
|
|
509
|
+
const wy = vy * ts - halfTs;
|
|
510
|
+
for (let lx = 0; lx < cs; lx++) {
|
|
511
|
+
const vx = startVx + lx;
|
|
512
|
+
const wx = vx * ts - halfTs;
|
|
513
|
+
for (let li = 0; li < layers.length; li++) {
|
|
514
|
+
const mask = map.sampleDualGridMask(vx, vy, layers[li].matches);
|
|
515
|
+
if (mask === 0) continue;
|
|
516
|
+
bobsByLayer[li].push({
|
|
517
|
+
x: wx,
|
|
518
|
+
y: wy,
|
|
519
|
+
visible: true,
|
|
520
|
+
alpha: 1,
|
|
521
|
+
frame: frameTable[li][mask]
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
version,
|
|
528
|
+
bobsByLayer
|
|
529
|
+
};
|
|
530
|
+
};
|
|
531
|
+
return {
|
|
532
|
+
container: root,
|
|
533
|
+
blitters,
|
|
534
|
+
get activeChunkCount() {
|
|
535
|
+
return mountedChunks.size;
|
|
536
|
+
},
|
|
537
|
+
get activeBobCount() {
|
|
538
|
+
return totalBobs;
|
|
539
|
+
},
|
|
540
|
+
syncViewport(viewLeft, viewTop, viewWidth, viewHeight) {
|
|
541
|
+
const chunkPx = cs * ts;
|
|
542
|
+
const minCx = Math.floor(viewLeft / chunkPx) - margin;
|
|
543
|
+
const maxCx = Math.floor((viewLeft + viewWidth) / chunkPx) + margin;
|
|
544
|
+
const minCy = Math.floor(viewTop / chunkPx) - margin;
|
|
545
|
+
const maxCy = Math.floor((viewTop + viewHeight) / chunkPx) + margin;
|
|
546
|
+
let changed = false;
|
|
547
|
+
for (const key of mountedChunks.keys()) {
|
|
548
|
+
const cx = unpackChunkX(key);
|
|
549
|
+
const cy = unpackChunkY(key);
|
|
550
|
+
if (cx < minCx || cx > maxCx || cy < minCy || cy > maxCy) {
|
|
551
|
+
mountedChunks.delete(key);
|
|
552
|
+
changed = true;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
for (let cy = minCy; cy <= maxCy; cy++) for (let cx = minCx; cx <= maxCx; cx++) {
|
|
556
|
+
const ch = map.getOrCreateChunk(cx, cy);
|
|
557
|
+
const key = packChunkKey(cx, cy);
|
|
558
|
+
const prev = mountedChunks.get(key);
|
|
559
|
+
if (prev && prev.version === ch.version) continue;
|
|
560
|
+
mountedChunks.set(key, buildChunkBobs(cx, cy, ch.version));
|
|
561
|
+
changed = true;
|
|
562
|
+
}
|
|
563
|
+
if (changed) {
|
|
564
|
+
totalBobs = 0;
|
|
565
|
+
for (let li = 0; li < layers.length; li++) {
|
|
566
|
+
const dst = blitters[li].children.list;
|
|
567
|
+
dst.length = 0;
|
|
568
|
+
for (const entry of mountedChunks.values()) {
|
|
569
|
+
const src = entry.bobsByLayer[li];
|
|
570
|
+
for (let k = 0; k < src.length; k++) dst.push(src[k]);
|
|
571
|
+
totalBobs += src.length;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
},
|
|
576
|
+
destroy() {
|
|
577
|
+
mountedChunks.clear();
|
|
578
|
+
totalBobs = 0;
|
|
579
|
+
root.destroy();
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
export { ChunkedTilemap, SurvivalBiomes, TileCellFlags, bakeDualGridTileset, computeBlob47Index, computeDualGridMask, createIslandBiomeGenerator, fbm2D, floorDiv, hash2D, mountChunkedTilemap, packChunkKey, positiveMod, unpackChunkX, unpackChunkY, valueNoise2D };
|
package/dist/trace.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { B as SceneDigestSurfaceSource, F as SceneDigestNode, H as buildSceneDigest, I as SceneDigestOptions, L as SceneDigestOverlayStats, M as SceneDigest, N as SceneDigestActiveFocus, P as SceneDigestEngineSandbox, R as SceneDigestSandbox, U as sanitizeTrackToken, V as SceneDigestSurfaces, z as SceneDigestSurfaceEntry } from "./game-
|
|
1
|
+
import { B as SceneDigestSurfaceSource, F as SceneDigestNode, H as buildSceneDigest, I as SceneDigestOptions, L as SceneDigestOverlayStats, M as SceneDigest, N as SceneDigestActiveFocus, P as SceneDigestEngineSandbox, R as SceneDigestSandbox, U as sanitizeTrackToken, V as SceneDigestSurfaces, z as SceneDigestSurfaceEntry } from "./game-CfUZiazr.js";
|
|
2
2
|
export { SceneDigest, SceneDigestActiveFocus, SceneDigestEngineSandbox, SceneDigestNode, SceneDigestOptions, SceneDigestOverlayStats, SceneDigestSandbox, SceneDigestSurfaceEntry, SceneDigestSurfaceSource, SceneDigestSurfaces, buildSceneDigest, sanitizeTrackToken };
|