dredless 1.4.1 → 1.5.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/dist/browser/dredless.global.js +856 -309
- package/dist/browser/dredless.global.js.map +4 -4
- package/docs/EVENTS.md +1 -1
- package/docs/MACHINES.md +43 -0
- package/docs/MANAGEMENT.md +2 -2
- package/docs/WORLD_STATE.md +70 -1
- package/package.json +1 -1
- package/src/browser.d.ts +1 -0
- package/src/browser.js +1 -1
- package/src/client/core.js +19 -1
- package/src/client/domains/index.js +50 -0
- package/src/crypto/chacha.js +27 -8
- package/src/data/item-schema.js +18 -2
- package/src/index.d.ts +137 -6
- package/src/index.js +2 -1
- package/src/protocol/codec/binary.js +3 -0
- package/src/protocol/codec/msgpack.js +32 -1
- package/src/protocol/codec/ui-integer.js +7 -0
- package/src/protocol/outbound/advanced-expando.js +65 -0
- package/src/protocol/outbound/machine-config.js +1 -6
- package/src/protocol/outbound/ui-config.d.ts +6 -0
- package/src/protocol/outbound/ui-config.js +7 -9
- package/src/state/index.js +1 -1
- package/src/state/model/decoder.js +47 -11
- package/src/state/model/state.js +75 -16
- package/src/state/world/index.d.ts +4 -0
- package/src/state/world/index.js +9 -3
- package/src/state/world/state.js +382 -45
|
@@ -430,10 +430,41 @@ function readArrayFast(bytes, count) {
|
|
|
430
430
|
return items;
|
|
431
431
|
}
|
|
432
432
|
|
|
433
|
+
// Packet maps repeat the same few dozen short keys. Resolve those through a
|
|
434
|
+
// small byte-keyed cache instead of a TextDecoder call per key.
|
|
435
|
+
const KEY_CACHE = new Map();
|
|
436
|
+
const KEY_CACHE_LIMIT = 512;
|
|
437
|
+
|
|
438
|
+
function readKeyFast(bytes) {
|
|
439
|
+
const head = bytes[cursor];
|
|
440
|
+
if ((head & 0xe0) !== 0xa0) return readFast(bytes);
|
|
441
|
+
const len = head & 0x1f;
|
|
442
|
+
const start = cursor + 1;
|
|
443
|
+
const end = start + len;
|
|
444
|
+
if (end > bytes.length) return readFast(bytes);
|
|
445
|
+
let hash = len;
|
|
446
|
+
for (let i = start; i < end; i++) hash = (Math.imul(hash, 31) + bytes[i]) | 0;
|
|
447
|
+
const entry = KEY_CACHE.get(hash);
|
|
448
|
+
if (entry !== undefined) {
|
|
449
|
+
const cached = entry.bytes;
|
|
450
|
+
let same = cached.length === len;
|
|
451
|
+
for (let i = 0; same && i < len; i++) same = cached[i] === bytes[start + i];
|
|
452
|
+
if (same) {
|
|
453
|
+
cursor = end;
|
|
454
|
+
return entry.text;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
cursor = start;
|
|
458
|
+
const text = readStrFast(bytes, len);
|
|
459
|
+
// A colliding key is simply decoded each time; the first one keeps the slot.
|
|
460
|
+
if (entry === undefined && KEY_CACHE.size < KEY_CACHE_LIMIT) KEY_CACHE.set(hash, {bytes: bytes.slice(start, end), text});
|
|
461
|
+
return text;
|
|
462
|
+
}
|
|
463
|
+
|
|
433
464
|
function readMapFast(bytes, count) {
|
|
434
465
|
const obj = {};
|
|
435
466
|
for (let i = 0; i < count; i++) {
|
|
436
|
-
const key =
|
|
467
|
+
const key = readKeyFast(bytes);
|
|
437
468
|
obj[typeof key === "string" ? key : String(key)] = readFast(bytes);
|
|
438
469
|
}
|
|
439
470
|
return obj;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Compact unsigned integers used by the UI command format (little endian).
|
|
2
|
+
export function encodeUnsigned(value) {
|
|
3
|
+
if (value < 64) return [value];
|
|
4
|
+
if (value < 256) return [0x80, value];
|
|
5
|
+
if (value < 65536) return [0x81, value & 255, value >>> 8];
|
|
6
|
+
return [0x82, value & 255, value >>> 8 & 255, value >>> 16 & 255, value >>> 24];
|
|
7
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {encoder} from "../../constants.js";
|
|
2
|
+
import {encodeUnsigned} from "../codec/ui-integer.js";
|
|
3
|
+
|
|
4
|
+
function number(value, name, min, max) {
|
|
5
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < min || value > max) {
|
|
6
|
+
throw new RangeError(`${name} must be a finite number between ${min} and ${max}`);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function float(value, name, min, max) {
|
|
12
|
+
const bytes = new Uint8Array(5);
|
|
13
|
+
bytes[0] = 0x88;
|
|
14
|
+
new DataView(bytes.buffer).setFloat32(1, number(value, name, min, max), true);
|
|
15
|
+
return [...bytes];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function color(value, name) {
|
|
19
|
+
number(value, name, 0, 0xffffff);
|
|
20
|
+
if (!Number.isInteger(value)) throw new RangeError(`${name} must be an integer RGB color`);
|
|
21
|
+
return encodeUnsigned(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function bool(value, name) {
|
|
25
|
+
if (typeof value !== "boolean") throw new TypeError(`${name} must be a boolean`);
|
|
26
|
+
return value ? 0x8d : 0x8e;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function section(name, values) {
|
|
30
|
+
const text = encoder.encode(name);
|
|
31
|
+
return [0x8a, text.length, ...text, 0, 0x90, ...values, 0x91];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function message(entity, action, config) {
|
|
35
|
+
number(entity, "entity", 0, 0xffffffff);
|
|
36
|
+
if (!Number.isInteger(entity)) throw new RangeError("entity must be an unsigned 32-bit integer");
|
|
37
|
+
const {
|
|
38
|
+
minWidth = 2, maxWidth = 10, minHeight = 2, maxHeight = 10,
|
|
39
|
+
minEnabled = false, maxEnabled = false, fixedSize = false,
|
|
40
|
+
backgroundColor = 0xdddddd, borderColor = 0x404040, opacity = 1
|
|
41
|
+
} = config;
|
|
42
|
+
// Native ConfigExpandoSize order: min X, max X, min Y, max Y, then flags.
|
|
43
|
+
return Uint8Array.from([0x90, action, ...encodeUnsigned(entity),
|
|
44
|
+
...section("config_expando_size", [
|
|
45
|
+
...float(minWidth, "minWidth", 2, 100), ...float(maxWidth, "maxWidth", 2, 100),
|
|
46
|
+
...float(minHeight, "minHeight", 2, 100), ...float(maxHeight, "maxHeight", 2, 100),
|
|
47
|
+
bool(minEnabled, "minEnabled"), bool(maxEnabled, "maxEnabled"), bool(fixedSize, "fixedSize")
|
|
48
|
+
]),
|
|
49
|
+
...section("config_expando_colors", [
|
|
50
|
+
...color(backgroundColor, "backgroundColor"), ...color(borderColor, "borderColor"),
|
|
51
|
+
...float(opacity, "opacity", 0, 1)
|
|
52
|
+
]), 0x91]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function buildAdvancedExpandoBoxConfigData(entity, config = {}) {
|
|
56
|
+
return message(entity, 0, config);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function buildAdvancedExpandoBoxCopyConfigData(config = {}) {
|
|
60
|
+
return message(11, 1, config);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function buildAdvancedExpandoBoxPasteConfigData(entity, config = {}) {
|
|
64
|
+
return message(entity, 2, config);
|
|
65
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {encodeUnsigned} from "../codec/ui-integer.js";
|
|
1
2
|
import {encoder} from "../../constants.js";
|
|
2
3
|
|
|
3
4
|
function uint32(value, name) {
|
|
@@ -7,12 +8,6 @@ function uint32(value, name) {
|
|
|
7
8
|
return value;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
function encodeUnsigned(value) {
|
|
11
|
-
if (value < 64) return [value];
|
|
12
|
-
if (value < 256) return [0x80, value];
|
|
13
|
-
if (value < 65536) return [0x81, value & 255, value >>> 8];
|
|
14
|
-
return [0x82, value & 255, value >>> 8 & 255, value >>> 16 & 255, value >>> 24];
|
|
15
|
-
}
|
|
16
11
|
|
|
17
12
|
function encodeFloat(value, name, max) {
|
|
18
13
|
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > max) {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export {buildAdvancedExpandoBoxConfigData, buildAdvancedExpandoBoxCopyConfigData, buildAdvancedExpandoBoxPasteConfigData} from "./advanced-expando.js";
|
|
2
|
+
import {encodeUnsigned} from "../codec/ui-integer.js";
|
|
1
3
|
import {encoder} from "../../constants.js";
|
|
2
4
|
export * from "./machine-config.js";
|
|
3
5
|
|
|
@@ -221,16 +223,12 @@ function encodeLoaderCycle(cycle) {
|
|
|
221
223
|
}
|
|
222
224
|
|
|
223
225
|
function encodeUiCommandTarget(entity, action = 0) {
|
|
224
|
-
const entityId =
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
throw new RangeError(`copy/paste UI config currently requires an entity id below 256`);
|
|
226
|
+
const entityId = Number(entity);
|
|
227
|
+
if (!Number.isInteger(entityId) || entityId < 0 || entityId > 0xffffffff) {
|
|
228
|
+
throw new RangeError("entity must be an unsigned 32-bit integer");
|
|
228
229
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
actionId ? actionId : (entityId >> 8) & 0xff,
|
|
232
|
-
entityId & 0xff
|
|
233
|
-
];
|
|
230
|
+
const actionId = requireByteInteger(action, "action");
|
|
231
|
+
return [NAV_UNIT_HEADER_TAG, actionId, ...encodeUnsigned(entityId)];
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
function encodeUiCommandSection(commandName) {
|
package/src/state/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {WorldStore, WorldState} from "./world/index.js";
|
|
1
|
+
export {WorldStore, WorldState, TileFlags} from "./world/index.js";
|
|
2
2
|
export {FabricatorType, ModelState, decodeModelData} from "./model/index.js";
|
|
3
3
|
export {generateGeneratorMaze, solveGeneratorMazeSeed, maybeSolveGeneratorMazeSeed} from "./generator-maze.js";
|
|
@@ -21,12 +21,15 @@ export function defineLazyProperty(target, key, compute) {
|
|
|
21
21
|
if (!resolved) {
|
|
22
22
|
cached = compute();
|
|
23
23
|
resolved = true;
|
|
24
|
+
// Release whatever the closure captured once it has run.
|
|
25
|
+
compute = null;
|
|
24
26
|
}
|
|
25
27
|
return cached;
|
|
26
28
|
},
|
|
27
29
|
set(value) {
|
|
28
30
|
cached = value;
|
|
29
31
|
resolved = true;
|
|
32
|
+
compute = null;
|
|
30
33
|
}
|
|
31
34
|
});
|
|
32
35
|
return target;
|
|
@@ -356,7 +359,17 @@ export const MODEL_TABLE_SPECS = new Map([
|
|
|
356
359
|
// Append new wire components without shifting stable Dredless table IDs.
|
|
357
360
|
[79, {name: "launcher_config", defaults: {q20: 0, q24: 300}, fields: numericFields({1: 20, 2: 24})}],
|
|
358
361
|
[80, {name: "rank_filter", defaults: {q20: 4}, fields: numericFields({1: 20})}],
|
|
359
|
-
[81, {name: "spawn_point", fields: []}]
|
|
362
|
+
[81, {name: "spawn_point", fields: []}],
|
|
363
|
+
// Official production WASM e0ed7795 (2026-11-14), dispatcher tags 173/174.
|
|
364
|
+
// Keep the wire field names until their public domain semantics are exposed.
|
|
365
|
+
[82, {
|
|
366
|
+
name: "advanced_expando_size_config",
|
|
367
|
+
defaults: {q20: 20, q24: 100, q28: 20, q32: 100, q36: 0, q37: 0, q38: 0, q39: 0},
|
|
368
|
+
packedBeforeValues: true,
|
|
369
|
+
packedBits: [{bit: 16, offset: 37}, {bit: 32, offset: 38}, {bit: 64, offset: 39}],
|
|
370
|
+
fields: numericFields({1: 20, 2: 24, 4: 28, 8: 32})
|
|
371
|
+
}],
|
|
372
|
+
[83, {name: "advanced_expando_color_config", defaults: {q20: 0xdddddd, q24: 0x404040, q28: 100, q32: 0}, fields: numericFields({1: 20, 2: 24, 4: 28})}]
|
|
360
373
|
]);
|
|
361
374
|
|
|
362
375
|
// Precompute everything about a spec that is fixed at module load. Decoding a
|
|
@@ -367,6 +380,7 @@ for (const spec of MODEL_TABLE_SPECS.values()) {
|
|
|
367
380
|
for (const blob of spec.blobs || []) blob.key = blobKey(blob.offset);
|
|
368
381
|
for (const item of spec.packedBits || []) item.key = qKey(item.offset);
|
|
369
382
|
if (spec.packedOffsets) spec.packedOffsetKeys = spec.packedOffsets.map(qKey);
|
|
383
|
+
spec.packedAnyMask = (spec.packedBits || []).reduce((mask, item) => mask | item.bit, spec.packedMask || 0);
|
|
370
384
|
if (spec.orderedValues) {
|
|
371
385
|
spec.orderedItems = [
|
|
372
386
|
...(spec.blobs || []).map((item) => ({bit: item.bit, offset: item.offset, key: item.key, kind: "blob"})),
|
|
@@ -462,7 +476,9 @@ export const WIRE_TAG_TABLES = new Map([
|
|
|
462
476
|
[169, 77],
|
|
463
477
|
[170, 79],
|
|
464
478
|
[171, 80],
|
|
465
|
-
[172, 81]
|
|
479
|
+
[172, 81],
|
|
480
|
+
[173, 82],
|
|
481
|
+
[174, 83]
|
|
466
482
|
]);
|
|
467
483
|
|
|
468
484
|
export const MASK_ONLY_TABLES = new Set([13, 22, 23, 27, 28, 30, 36, 40, 46, 48, 52, 58, 64, 65, 66, 68, 73, 74]);
|
|
@@ -480,7 +496,7 @@ export const ENTITY_FOOTPRINTS = new Map([
|
|
|
480
496
|
export const PLACED_ENTITY_TYPE_IDS = new Set([
|
|
481
497
|
215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 226, 227, 228, 229,
|
|
482
498
|
230, 231, 240, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252,
|
|
483
|
-
253, 255, 256, 257, 258, 259, 260, 261, 263, 264, 265
|
|
499
|
+
253, 255, 256, 257, 258, 259, 260, 261, 263, 264, 265, 266
|
|
484
500
|
]);
|
|
485
501
|
|
|
486
502
|
export const FabricatorType = Object.freeze({
|
|
@@ -1212,14 +1228,17 @@ export function summarizeShipWorldMetadata(lockdownRecord, shipRecord) {
|
|
|
1212
1228
|
|
|
1213
1229
|
export function summarizeItemCrate(entity, sizeRecord, itemRecord = null, healthRecord = null) {
|
|
1214
1230
|
if (!sizeRecord) return null;
|
|
1231
|
+
const size = scaledSizeSummary(sizeRecord);
|
|
1215
1232
|
const itemId = itemRecord?.q20 ?? null;
|
|
1216
1233
|
const count = itemRecord?.q24 ?? null;
|
|
1217
1234
|
return {
|
|
1218
1235
|
entity,
|
|
1219
1236
|
...itemSummary(itemId, count),
|
|
1220
1237
|
boxSprite: Boolean(itemRecord?.q29),
|
|
1221
|
-
width:
|
|
1222
|
-
height:
|
|
1238
|
+
width: size.width,
|
|
1239
|
+
height: size.height,
|
|
1240
|
+
rawWidth: size.rawWidth,
|
|
1241
|
+
rawHeight: size.rawHeight,
|
|
1223
1242
|
itemState: cloneRecord(itemRecord || healthRecord || {}),
|
|
1224
1243
|
sizeState: cloneRecord(sizeRecord)
|
|
1225
1244
|
};
|
|
@@ -1238,11 +1257,26 @@ export function scaledSizeSummary(record) {
|
|
|
1238
1257
|
};
|
|
1239
1258
|
}
|
|
1240
1259
|
|
|
1241
|
-
export function summarizeExpandoBox(entity, itemHolderRecord, sizeRecord) {
|
|
1242
|
-
if (!itemHolderRecord && !sizeRecord) return null;
|
|
1260
|
+
export function summarizeExpandoBox(entity, itemHolderRecord, sizeRecord, sizeConfig = null, colorConfig = null, advanced = false) {
|
|
1261
|
+
if (!itemHolderRecord && !sizeRecord && !advanced) return null;
|
|
1243
1262
|
const size = scaledSizeSummary(sizeRecord);
|
|
1244
1263
|
return {
|
|
1245
1264
|
entity,
|
|
1265
|
+
advanced,
|
|
1266
|
+
config: advanced ? {
|
|
1267
|
+
minWidth: (sizeConfig?.q20 ?? 20) / 10,
|
|
1268
|
+
maxWidth: (sizeConfig?.q24 ?? 100) / 10,
|
|
1269
|
+
minHeight: (sizeConfig?.q28 ?? 20) / 10,
|
|
1270
|
+
maxHeight: (sizeConfig?.q32 ?? 100) / 10,
|
|
1271
|
+
minEnabled: Boolean(sizeConfig?.q37),
|
|
1272
|
+
maxEnabled: Boolean(sizeConfig?.q38),
|
|
1273
|
+
fixedSize: Boolean(sizeConfig?.q39),
|
|
1274
|
+
backgroundColor: (colorConfig?.q20 ?? 0xdddddd) >>> 0,
|
|
1275
|
+
borderColor: (colorConfig?.q24 ?? 0x404040) >>> 0,
|
|
1276
|
+
opacity: (colorConfig?.q28 ?? 100) / 100
|
|
1277
|
+
} : null,
|
|
1278
|
+
sizeConfigState: cloneRecord(sizeConfig || {}),
|
|
1279
|
+
colorConfigState: cloneRecord(colorConfig || {}),
|
|
1246
1280
|
...itemSummary(itemHolderRecord?.q20 ?? null, itemHolderRecord?.q24 ?? null),
|
|
1247
1281
|
width: size?.width ?? null,
|
|
1248
1282
|
height: size?.height ?? null,
|
|
@@ -1284,13 +1318,15 @@ export function summarizeMapMarker(entity, labelRecord, zoneRecord = null, sizeR
|
|
|
1284
1318
|
};
|
|
1285
1319
|
}
|
|
1286
1320
|
|
|
1287
|
-
export function summarizeDockingSpring(entity, springRecord,
|
|
1288
|
-
|
|
1321
|
+
export function summarizeDockingSpring(entity, springRecord, sizeRecord = null) {
|
|
1322
|
+
// Wire 85 is the marker iterated by the official dock_springs renderer.
|
|
1323
|
+
// Generic wire 89 plus size/body heuristics also matched decorative walls.
|
|
1324
|
+
if (!springRecord) return null;
|
|
1289
1325
|
return {
|
|
1290
1326
|
entity,
|
|
1291
1327
|
id: springRecord.q20 ?? null,
|
|
1292
|
-
width: sizeRecord.q20,
|
|
1293
|
-
height: sizeRecord.q24,
|
|
1328
|
+
width: sizeRecord?.q20 == null ? null : sizeRecord.q20 / 10,
|
|
1329
|
+
height: sizeRecord?.q24 == null ? null : sizeRecord.q24 / 10,
|
|
1294
1330
|
state: cloneRecord(springRecord)
|
|
1295
1331
|
};
|
|
1296
1332
|
}
|
package/src/state/model/state.js
CHANGED
|
@@ -128,6 +128,10 @@ const {
|
|
|
128
128
|
} = modelDecoder;
|
|
129
129
|
|
|
130
130
|
export class ModelState {
|
|
131
|
+
#entityRevisions = new Map();
|
|
132
|
+
#entityIds = null;
|
|
133
|
+
#recordRevisions = new WeakMap();
|
|
134
|
+
#revision = 0;
|
|
131
135
|
#localPlayerEntity = null;
|
|
132
136
|
#blueprintItems = null;
|
|
133
137
|
#helmOccupied = new Map();
|
|
@@ -176,10 +180,32 @@ export class ModelState {
|
|
|
176
180
|
return this.table(tableId).get(Number(entityId)) || null;
|
|
177
181
|
}
|
|
178
182
|
|
|
183
|
+
/** Monotonic local revision, independent of the server's generation counter. */
|
|
184
|
+
get revision() { return this.#revision; }
|
|
185
|
+
|
|
186
|
+
/** Live component IDs without creating entity/debug summaries. */
|
|
187
|
+
componentEntityIds() {
|
|
188
|
+
this.#entityIds ??= [...this.#entityRevisions.keys()].sort((a, b) => a - b);
|
|
189
|
+
return this.#entityIds.values();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Latest record mutation for a live entity, or zero after removal. */
|
|
193
|
+
entityRevision(entityId) { return this.#entityRevisions.get(Number(entityId)) ?? 0; }
|
|
194
|
+
|
|
195
|
+
/** Revision of a decoded record, without creating entity/debug summaries. */
|
|
196
|
+
recordRevision(record) { return record ? this.#recordRevisions.get(record) ?? 0 : 0; }
|
|
197
|
+
|
|
198
|
+
/** Stateful navigation destination without constructing derived entity lists. */
|
|
199
|
+
navigationDestination(entityId) {
|
|
200
|
+
const record = this.record(78, entityId);
|
|
201
|
+
return this.#navigationUnitAutoWarp.get(Number(entityId))?.destination
|
|
202
|
+
?? navigationDestinationFromRecord(record);
|
|
203
|
+
}
|
|
204
|
+
|
|
179
205
|
entity(entityId) {
|
|
180
206
|
const id = Number(entityId);
|
|
181
207
|
if (!Number.isFinite(id)) return null;
|
|
182
|
-
return this.#
|
|
208
|
+
return this.#derivedEntityIndex().entitiesById.get(id) || null;
|
|
183
209
|
}
|
|
184
210
|
|
|
185
211
|
entities() {
|
|
@@ -194,11 +220,12 @@ export class ModelState {
|
|
|
194
220
|
// O(1) point lookup. Callers previously had to materialise and scan the whole
|
|
195
221
|
// block list to answer this.
|
|
196
222
|
blockAt(x, y) {
|
|
197
|
-
this.#
|
|
223
|
+
this.#derivedEntityIndex();
|
|
198
224
|
return this.#blocksIndex().get(blockKey(x, y)) || null;
|
|
199
225
|
}
|
|
200
226
|
|
|
201
227
|
apply(bytes, {full = false} = {}) {
|
|
228
|
+
this.#revision++;
|
|
202
229
|
this.#blueprintItems = null;
|
|
203
230
|
const reader = new ModelReader(bytes);
|
|
204
231
|
const update = {
|
|
@@ -400,20 +427,48 @@ export class ModelState {
|
|
|
400
427
|
}
|
|
401
428
|
|
|
402
429
|
#updateDerived(update) {
|
|
403
|
-
|
|
430
|
+
const derived = this._derived;
|
|
431
|
+
if (!derived) return;
|
|
404
432
|
if (!update.removals.length && !update.changedEntities.size) return;
|
|
405
433
|
|
|
406
434
|
for (const section of update.sections) {
|
|
407
435
|
for (const record of section.records || []) this.#addDerivedEntityTable(record.entity, section.table);
|
|
408
436
|
}
|
|
409
437
|
|
|
438
|
+
// Only record what changed. Re-summarising happens on the next read, so
|
|
439
|
+
// an entity that moves every tick is summarised once per read rather
|
|
440
|
+
// than once per packet, and ingestion costs the same whether or not
|
|
441
|
+
// anything has read derived state.
|
|
442
|
+
const dirty = derived.dirtyEntities ??= new Set();
|
|
410
443
|
if (update.sections.some((section) => section.table === 12)) {
|
|
444
|
+
// Player summaries embed blueprint previews, so they change too.
|
|
411
445
|
for (const entityId of this.table(55).keys()) update.changedEntities.add(entityId);
|
|
412
446
|
}
|
|
413
|
-
for (const entityId of update.removals)
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
447
|
+
for (const entityId of update.removals) {
|
|
448
|
+
derived.entityTableIds.delete(entityId);
|
|
449
|
+
dirty.add(entityId);
|
|
450
|
+
}
|
|
451
|
+
for (const entityId of update.changedEntities) dirty.add(entityId);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
#flushDerivedEntities() {
|
|
455
|
+
const derived = this._derived;
|
|
456
|
+
const dirty = derived.dirtyEntities;
|
|
457
|
+
if (!dirty || dirty.size === 0) return;
|
|
458
|
+
derived.dirtyEntities = null;
|
|
459
|
+
// An entity with no remaining rows is dropped by the refresh.
|
|
460
|
+
for (const entityId of dirty) this.#refreshDerivedEntity(entityId);
|
|
461
|
+
derived.summariesDirty = true;
|
|
462
|
+
derived.tableSummariesDirty = true;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Current entity summaries without the per-category lists. Point lookups go
|
|
466
|
+
// through here: rebuilding those lists is O(total entities) and was the whole
|
|
467
|
+
// cost of entity(id) on a large world.
|
|
468
|
+
#derivedEntityIndex() {
|
|
469
|
+
if (!this._derived) return this.#derivedState();
|
|
470
|
+
this.#flushDerivedEntities();
|
|
471
|
+
return this._derived;
|
|
417
472
|
}
|
|
418
473
|
|
|
419
474
|
#updateNavigationUnitAutoWarp(update) {
|
|
@@ -558,6 +613,7 @@ export class ModelState {
|
|
|
558
613
|
|
|
559
614
|
#derivedState() {
|
|
560
615
|
if (this._derived) {
|
|
616
|
+
this.#flushDerivedEntities();
|
|
561
617
|
this.#ensureDerivedSummaries();
|
|
562
618
|
return this._derived;
|
|
563
619
|
}
|
|
@@ -645,7 +701,7 @@ export class ModelState {
|
|
|
645
701
|
|
|
646
702
|
const next = this.#summarizeEntity(entityId, rows);
|
|
647
703
|
derived.entitiesById.set(entityId, next);
|
|
648
|
-
if (!
|
|
704
|
+
if (!old) insertSorted(derived.entityIds, entityId);
|
|
649
705
|
this.#addEntityToDerivedBlocks(derived, next);
|
|
650
706
|
derived.entityCount = derived.entityIds.length;
|
|
651
707
|
}
|
|
@@ -863,7 +919,7 @@ export class ModelState {
|
|
|
863
919
|
const thrusterRecord = rec(50);
|
|
864
920
|
const labelRecord = rec(9);
|
|
865
921
|
const zoneLabelRecord = rec(25);
|
|
866
|
-
const dockingSpringRecord = rec(
|
|
922
|
+
const dockingSpringRecord = rec(22);
|
|
867
923
|
const hugeThrusterRecord = rec(23);
|
|
868
924
|
const bodyStateRecord = rec(1);
|
|
869
925
|
const typeRecord = rec(7);
|
|
@@ -878,7 +934,8 @@ export class ModelState {
|
|
|
878
934
|
const dynamicBody = bodyStateRecord?.q20 === -4;
|
|
879
935
|
const typeId = entityTypeIdFromRecord(typeRecord);
|
|
880
936
|
const itemHolder = summarizeItemHolder(entityId, itemHolderRecord);
|
|
881
|
-
const
|
|
937
|
+
const isAdvancedExpandoBox = typeId === 266 || Boolean(rec(82) || rec(83));
|
|
938
|
+
const isExpandoBox = isAdvancedExpandoBox || typeId === EXPANDO_BOX_TYPE_ID || markerTypeId === EXPANDO_BOX_TYPE_ID;
|
|
882
939
|
const health = summarizeHealth(entityId, healthRecord);
|
|
883
940
|
const projectile = summarizeProjectile(entityId, rec(24));
|
|
884
941
|
const combatBeams = summarizeCombatBeams(entityId, rec(15));
|
|
@@ -915,10 +972,10 @@ export class ModelState {
|
|
|
915
972
|
const door = typeId === 220 ? summarizeDoor(entityId, spawnPointRecord, doorRecord, rec(80)) : null;
|
|
916
973
|
const shipSize = shipControl && this.isOverworld ? summarizeShipSize(entityId, rec(3)) : null;
|
|
917
974
|
const hoverOutline = summarizeHoverOutline(entityId, crateSizeRecord);
|
|
918
|
-
const expandoBox = isExpandoBox ? summarizeExpandoBox(entityId, itemHolderRecord, expandoSizeRecord) : null;
|
|
975
|
+
const expandoBox = isExpandoBox ? summarizeExpandoBox(entityId, itemHolderRecord, expandoSizeRecord, rec(82), rec(83), isAdvancedExpandoBox) : null;
|
|
919
976
|
const mapMarker = this.isOverworld ? summarizeMapMarker(entityId, labelRecord, zoneLabelRecord, crateSizeRecord) : null;
|
|
920
977
|
const dockingSpring = this.isOverworld
|
|
921
|
-
? summarizeDockingSpring(entityId, dockingSpringRecord,
|
|
978
|
+
? summarizeDockingSpring(entityId, dockingSpringRecord, crateSizeRecord)
|
|
922
979
|
: null;
|
|
923
980
|
const hugeThruster = this.isOverworld
|
|
924
981
|
? summarizeHugeThruster(entityId, hugeThrusterRecord, crateSizeRecord)
|
|
@@ -1135,6 +1192,8 @@ export class ModelState {
|
|
|
1135
1192
|
if (delta === 0) break;
|
|
1136
1193
|
entity += delta;
|
|
1137
1194
|
removals.push(entity);
|
|
1195
|
+
this.#entityRevisions.delete(entity);
|
|
1196
|
+
this.#entityIds = null;
|
|
1138
1197
|
// The derived membership index describes the previous apply(), so it
|
|
1139
1198
|
// can miss component tables added earlier in this same packet (or
|
|
1140
1199
|
// in a partially decoded section). A removal must clear every table.
|
|
@@ -1177,6 +1236,9 @@ export class ModelState {
|
|
|
1177
1236
|
if (!records) this.tables.set(tableId, records = new Map());
|
|
1178
1237
|
let record = records.get(entity);
|
|
1179
1238
|
if (!record) records.set(entity, record = {...spec?.defaults});
|
|
1239
|
+
if (!this.#entityRevisions.has(entity)) this.#entityIds = null;
|
|
1240
|
+
this.#entityRevisions.set(entity, this.#revision);
|
|
1241
|
+
this.#recordRevisions.set(record, this.#revision);
|
|
1180
1242
|
record.lastMask = mask;
|
|
1181
1243
|
|
|
1182
1244
|
try {
|
|
@@ -1199,10 +1261,7 @@ export class ModelState {
|
|
|
1199
1261
|
return;
|
|
1200
1262
|
}
|
|
1201
1263
|
|
|
1202
|
-
const hasPacked =
|
|
1203
|
-
(spec.packedMask && (mask & spec.packedMask)) ||
|
|
1204
|
-
spec.packedBits?.some((item) => mask & item.bit)
|
|
1205
|
-
);
|
|
1264
|
+
const hasPacked = (mask & spec.packedAnyMask) !== 0;
|
|
1206
1265
|
let packed = null;
|
|
1207
1266
|
if (hasPacked && spec.packedBeforeValues) packed = reader.readUnsigned();
|
|
1208
1267
|
|
package/src/state/world/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {WorldState} from "./state.js";
|
|
1
|
+
import {TileFlags, WorldState} from "./state.js";
|
|
2
2
|
import {ModelState} from "../model/index.js";
|
|
3
|
+
import {defineLazyProperty} from "../model/decoder.js";
|
|
3
4
|
import {decodeModelPayload} from "../../protocol/inbound/world-payload.js";
|
|
4
5
|
|
|
5
|
-
export {WorldState};
|
|
6
|
+
export {WorldState, TileFlags};
|
|
6
7
|
|
|
7
8
|
// Retention limits for per-world history. These arrays previously grew without
|
|
8
9
|
// bound for the lifetime of a connection. Pass Infinity to restore that.
|
|
@@ -143,7 +144,12 @@ export class WorldStore {
|
|
|
143
144
|
if (decoded && typeof decoded === "object") {
|
|
144
145
|
for (const [kind, value] of Object.entries(decoded)) {
|
|
145
146
|
try {
|
|
146
|
-
if (kind === "0")
|
|
147
|
+
if (kind === "0") {
|
|
148
|
+
// `tiles` is built on first read: most consumers never
|
|
149
|
+
// look at it, and a full chunk is 4096 objects.
|
|
150
|
+
const patch = world.applyChunkPatch(value);
|
|
151
|
+
updates.push(defineLazyProperty({kind}, "tiles", () => (patch ? patch.tiles : null)));
|
|
152
|
+
}
|
|
147
153
|
else if (kind === "1") updates.push({kind, tile: world.applyTile(value)});
|
|
148
154
|
else updates.push({kind, value});
|
|
149
155
|
} catch (error) {
|