castle-web-cli 0.4.230 → 0.4.232
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/castle-host/host.js +81 -0
- package/dist/devSaveStorage.d.ts +36 -0
- package/dist/devSaveStorage.js +469 -0
- package/dist/devSessionChild.js +22 -12
- package/dist/devSessionServer.js +8 -2
- package/dist/devSessionStorage.d.ts +9 -0
- package/dist/devSessionStorage.js +66 -1
- package/dist/init.js +1 -1
- package/dist/serve.js +5 -1
- package/dist/serverBundle.js +5 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +148 -1
- package/kits/base/sdk/castle.d.ts +4 -0
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +15 -0
- package/kits/base/sdk/multiplayer.js +52 -4
- package/kits/base/sdk/save.d.ts +197 -0
- package/kits/base/sdk/save.js +525 -0
- package/kits/base/sdk/saveCoordinator.d.ts +39 -0
- package/kits/base/sdk/saveCoordinator.js +252 -0
- package/kits/base/sdk/server/storage.d.ts +3 -2
- package/kits/base/sdk/server/storage.js +25 -5
- package/kits/base/sdk/server/wrapper.d.ts +4 -0
- package/kits/base/sdk/server/wrapper.js +2 -0
- package/kits/base/sdk/store.d.ts +3 -2
- package/kits/base/sdk/store.js +5 -0
- package/kits/hub-game/CLAUDE.md +29 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +237 -13
- package/kits/hub-game/code/client/grab.js +166 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +36 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +35 -1
- package/kits/hub-game/code/client/talk.js +10 -36
- package/kits/hub-game/code/client/tiers.js +505 -0
- package/kits/hub-game/code/client/together.js +144 -4
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +299 -13
- package/kits/hub-game/code/server/hub.js +162 -7
- package/kits/hub-game/code/server/manifest.js +80 -8
- package/kits/hub-game/code/server/npcs.js +21 -33
- package/kits/hub-game/code/server/party.js +75 -6
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +386 -21
- package/kits/hub-game/docs/contract.md +285 -19
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +122 -1
- package/kits/hub-game/scripts/test-npcs.mjs +34 -24
- package/kits/hub-game/scripts/test-party.mjs +108 -4
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +854 -0
- package/kits/hub-game/scripts/test-together.mjs +33 -1
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/CLAUDE.md +2 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +127 -0
- package/kits/physics-2d/package.json +1 -1
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
- package/kits/physics-3d/CLAUDE.md +531 -0
- package/kits/physics-3d/behaviors/Player.jsx +437 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
- package/kits/physics-3d/engine3d/actornode.js +253 -0
- package/kits/physics-3d/engine3d/nearshadows.js +600 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/unchanged.js +153 -0
- package/kits/physics-3d/engine3d/world3d.js +525 -11
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
- package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
- package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +273 -9
- package/package.json +1 -1
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// `syncActors` (engine3d/world3d.js) for a scene that set
|
|
2
|
+
// `"engine": { "skipUnchangedActors": true }`, when a pass is not alone: code
|
|
3
|
+
// outside the engine runs in the middle of it and changes the scene, calls
|
|
4
|
+
// `syncActors` again, or loads another scene. The rule (world3d.js): such a
|
|
5
|
+
// pass finishes exactly as it does without the key and leaves nothing written
|
|
6
|
+
// down. Every scenario runs twice, without the key and with it, on the real
|
|
7
|
+
// `syncActors` and real models, and must end with the same nodes, built from
|
|
8
|
+
// the same things; and with the key nothing may be left on the world.
|
|
9
|
+
//
|
|
10
|
+
// The callbacks are the ones a deck has: `scene.userData.groundHeight`, which
|
|
11
|
+
// the engine calls while it animates a walking model, and a three.js listener
|
|
12
|
+
// on the scene. Components are plain stored data throughout.
|
|
13
|
+
//
|
|
14
|
+
// test-static-actors.mjs runs it (`npm run check`).
|
|
15
|
+
import assert from 'node:assert/strict';
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import { register } from 'node:module';
|
|
18
|
+
import * as THREE from 'three';
|
|
19
|
+
|
|
20
|
+
// The kit's modules are written for the bundler: imports without extensions,
|
|
21
|
+
// and the 2d kit by its alias.
|
|
22
|
+
const hooks = `
|
|
23
|
+
import { existsSync } from 'node:fs';
|
|
24
|
+
import { fileURLToPath } from 'node:url';
|
|
25
|
+
export async function resolve(specifier, context, next) {
|
|
26
|
+
const parent = context.parentURL ?? '';
|
|
27
|
+
if (specifier.startsWith('@imports/castle.physics-2d/')) {
|
|
28
|
+
specifier = parent.replace(/\\/kits\\/.*/, '/kits/physics-2d/') + specifier.slice(27);
|
|
29
|
+
}
|
|
30
|
+
if ((specifier.startsWith('.') || specifier.startsWith('file:')) && parent.includes('/kits/') && !/\\.(m?js|jsx|json)$/.test(specifier)) {
|
|
31
|
+
const base = new URL(specifier, parent).href;
|
|
32
|
+
for (const ext of ['.js', '.jsx']) if (existsSync(fileURLToPath(base + ext))) return next(base + ext, context);
|
|
33
|
+
}
|
|
34
|
+
return next(specifier, context);
|
|
35
|
+
}`;
|
|
36
|
+
register(`data:text/javascript,${encodeURIComponent(hooks)}`);
|
|
37
|
+
const { syncActors, disposeWorld } = await import('../engine3d/world3d.js');
|
|
38
|
+
|
|
39
|
+
const WALKER = 'models/player.pxmodel';
|
|
40
|
+
const walkerText = fs.readFileSync(new URL('../models/player.pxmodel', import.meta.url), 'utf8');
|
|
41
|
+
const DT = 1 / 60;
|
|
42
|
+
|
|
43
|
+
// A world as `makeWorld3D` makes it, without the renderer.
|
|
44
|
+
export function makeWorld() {
|
|
45
|
+
const scene = new THREE.Scene();
|
|
46
|
+
const ground = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshStandardMaterial());
|
|
47
|
+
const grid = new THREE.Object3D();
|
|
48
|
+
scene.add(ground, grid);
|
|
49
|
+
return {
|
|
50
|
+
scene,
|
|
51
|
+
camera: new THREE.PerspectiveCamera(),
|
|
52
|
+
renderer: { dispose() {} },
|
|
53
|
+
nodes: new Map(),
|
|
54
|
+
scenePath: undefined,
|
|
55
|
+
parked: new Map(),
|
|
56
|
+
models: new Map(),
|
|
57
|
+
skipping: { on: false },
|
|
58
|
+
modelSigs: new Map(),
|
|
59
|
+
sigEpoch: 0,
|
|
60
|
+
syncCount: 0,
|
|
61
|
+
syncDepth: 0,
|
|
62
|
+
generation: 0,
|
|
63
|
+
snapshots: new WeakMap(),
|
|
64
|
+
instances: new Map(),
|
|
65
|
+
instanceModels: false,
|
|
66
|
+
ground,
|
|
67
|
+
grid,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// What a SceneRuntime is to `syncActors`.
|
|
72
|
+
function makeRuntime(key, path, actors, files) {
|
|
73
|
+
return {
|
|
74
|
+
skipsUnchangedActors: key,
|
|
75
|
+
scenePath: path,
|
|
76
|
+
data: { ground: { color: '#445566' } },
|
|
77
|
+
files,
|
|
78
|
+
actors,
|
|
79
|
+
getActors() {
|
|
80
|
+
return [...this.actors];
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const walker = (id, x) => ({
|
|
86
|
+
id,
|
|
87
|
+
runtime: {},
|
|
88
|
+
components: {
|
|
89
|
+
Transform: { x, y: 0, z: 0, rotationX: 0, rotationY: 0, rotationZ: 0 },
|
|
90
|
+
Shape: { kind: 'box', width: 1, height: 1, depth: 1, color: '#8899aa' },
|
|
91
|
+
Model: { file: WALKER, scale: 1, yOffset: 0 },
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// What the world ends up drawing: each node's id, signature, and the colours
|
|
96
|
+
// of its meshes.
|
|
97
|
+
function drawn(world) {
|
|
98
|
+
const out = {};
|
|
99
|
+
for (const [id, entry] of world.nodes) {
|
|
100
|
+
const colors = new Set();
|
|
101
|
+
entry.node.traverse((object) => {
|
|
102
|
+
for (const material of [object.material].flat()) if (material?.color) colors.add(material.color.getHexString());
|
|
103
|
+
});
|
|
104
|
+
out[id] = { sigLength: entry.sig.length, inScene: entry.node.parent === world.scene, colors: [...colors].sort().join(',') };
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Whether anything is written down for an entry.
|
|
110
|
+
const vouchedFor = (world, entry) => Boolean(world.snapshots?.has(entry) || entry.builtFrom || entry.turnedTo);
|
|
111
|
+
|
|
112
|
+
function nothingWrittenDown(world, what) {
|
|
113
|
+
const entries = [...world.nodes.values(), ...[...world.parked.values()].flatMap((parked) => [...parked.values()])];
|
|
114
|
+
assert.equal(entries.filter((entry) => vouchedFor(world, entry)).length, 0, `${what}: an entry still holds what it was built from`);
|
|
115
|
+
assert.equal(world.modelSigs.size, 0, `${what}: model signatures are still held`);
|
|
116
|
+
assert.equal(world.syncDepth, 0, `${what}: the depth is back to none`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A scenario is given the world and whether the scene sets the key; it returns
|
|
120
|
+
// what to compare and, with the key, what must be true of the world.
|
|
121
|
+
const scenarios = {
|
|
122
|
+
// First case: during A's animation the callback loads and syncs a scene
|
|
123
|
+
// without the key that has actors of the same ids and looks. Nothing of the
|
|
124
|
+
// scene that was left may stay reachable from the world.
|
|
125
|
+
'a callback loads a scene without the key'(world, key) {
|
|
126
|
+
const files = { [WALKER]: walkerText };
|
|
127
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
128
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
129
|
+
for (let i = 0; i < 4; i++) {
|
|
130
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
131
|
+
syncActors(world, runtime, {}, DT);
|
|
132
|
+
}
|
|
133
|
+
if (key) assert.ok(vouchedFor(world, world.nodes.get('b')), 'warm: the nodes are vouched for');
|
|
134
|
+
const next = makeRuntime(false, 'scenes/two.scene', [walker('a', 0), walker('b', 3)], files);
|
|
135
|
+
let called = 0;
|
|
136
|
+
world.scene.userData.groundHeight = () => {
|
|
137
|
+
called += 1;
|
|
138
|
+
if (called === 1) syncActors(world, next, {}, DT);
|
|
139
|
+
return 0;
|
|
140
|
+
};
|
|
141
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
142
|
+
const leftB = actors[1];
|
|
143
|
+
syncActors(world, runtime, {}, DT);
|
|
144
|
+
assert.ok(called > 0, 'the callback ran');
|
|
145
|
+
delete world.scene.userData.groundHeight;
|
|
146
|
+
for (let i = 0; i < 3; i++) syncActors(world, next, {}, DT);
|
|
147
|
+
if (key) {
|
|
148
|
+
nothingWrittenDown(world, 'after the nested load');
|
|
149
|
+
assert.equal(world.skipping.on, false);
|
|
150
|
+
}
|
|
151
|
+
const held = JSON.stringify([...world.nodes.values()].map((entry) => Object.keys(entry).filter((name) => entry[name] !== undefined && typeof entry[name] === 'object').sort()));
|
|
152
|
+
return { result: drawn(world), held, leftB };
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
// Second case: the callback spawns C and syncs again. The pass underneath
|
|
156
|
+
// must end with the nodes it ends with without the key.
|
|
157
|
+
'a callback spawns an actor and syncs again'(world, key) {
|
|
158
|
+
const files = { [WALKER]: walkerText };
|
|
159
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
160
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
161
|
+
for (let i = 0; i < 4; i++) {
|
|
162
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
163
|
+
syncActors(world, runtime, {}, DT);
|
|
164
|
+
}
|
|
165
|
+
let called = 0;
|
|
166
|
+
world.scene.userData.groundHeight = () => {
|
|
167
|
+
called += 1;
|
|
168
|
+
if (called === 1) {
|
|
169
|
+
actors.push(walker('c', 6));
|
|
170
|
+
syncActors(world, runtime, {}, DT);
|
|
171
|
+
}
|
|
172
|
+
return 0;
|
|
173
|
+
};
|
|
174
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
175
|
+
syncActors(world, runtime, {}, DT);
|
|
176
|
+
const after = Object.keys(drawn(world)).sort().join(',');
|
|
177
|
+
if (key) nothingWrittenDown(world, 'after the nested sync');
|
|
178
|
+
delete world.scene.userData.groundHeight;
|
|
179
|
+
syncActors(world, runtime, {}, DT);
|
|
180
|
+
return { result: { after, later: drawn(world) } };
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
// Third case: the callback rewrites the model's file. B, placed from the
|
|
184
|
+
// same file and reached later in the same pass, is rebuilt that frame.
|
|
185
|
+
'a callback rewrites a model file'(world, key) {
|
|
186
|
+
const files = { [WALKER]: walkerText };
|
|
187
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
188
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
189
|
+
for (let i = 0; i < 4; i++) {
|
|
190
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
191
|
+
syncActors(world, runtime, {}, DT);
|
|
192
|
+
}
|
|
193
|
+
const before = world.nodes.get('b').node;
|
|
194
|
+
let called = 0;
|
|
195
|
+
world.scene.userData.groundHeight = () => {
|
|
196
|
+
called += 1;
|
|
197
|
+
if (called === 1) files[WALKER] = walkerText.replace(/"color": "#[0-9a-fA-F]{6}"/g, '"color": "#ff0000"');
|
|
198
|
+
return 0;
|
|
199
|
+
};
|
|
200
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
201
|
+
syncActors(world, runtime, {}, DT);
|
|
202
|
+
const rebuiltThatFrame = world.nodes.get('b').node !== before;
|
|
203
|
+
assert.ok(called > 0 && files[WALKER] !== walkerText, 'the callback rewrote the file');
|
|
204
|
+
return { result: { rebuiltThatFrame, drawn: drawn(world) } };
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
// The same re-entry by the other way out of the engine: a listener on the
|
|
208
|
+
// scene, told when a node is added, syncs again.
|
|
209
|
+
'a scene listener syncs again when a node is added'(world, key) {
|
|
210
|
+
const files = { [WALKER]: walkerText };
|
|
211
|
+
const actors = [walker('a', 0), walker('b', 3), walker('c', 6)];
|
|
212
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
213
|
+
for (let i = 0; i < 3; i++) syncActors(world, runtime, {}, DT);
|
|
214
|
+
let told = 0;
|
|
215
|
+
const listener = () => {
|
|
216
|
+
told += 1;
|
|
217
|
+
if (told !== 1) return;
|
|
218
|
+
actors.splice(0, 1);
|
|
219
|
+
actors.push(walker('d', 9));
|
|
220
|
+
syncActors(world, runtime, {}, DT);
|
|
221
|
+
};
|
|
222
|
+
world.scene.addEventListener('childadded', listener);
|
|
223
|
+
actors[1].components.Shape.color = '#00ff00';
|
|
224
|
+
actors[1].components.Model.scale = 2;
|
|
225
|
+
syncActors(world, runtime, {}, DT);
|
|
226
|
+
world.scene.removeEventListener('childadded', listener);
|
|
227
|
+
assert.ok(told > 0, 'the listener was told');
|
|
228
|
+
const after = Object.keys(drawn(world)).sort().join(',');
|
|
229
|
+
if (key) nothingWrittenDown(world, 'after the listener');
|
|
230
|
+
for (let i = 0; i < 2; i++) syncActors(world, runtime, {}, DT);
|
|
231
|
+
return { result: { after, later: drawn(world) } };
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
// A pass that throws part way leaves nothing behind either.
|
|
235
|
+
'a callback throws'(world, key) {
|
|
236
|
+
const files = { [WALKER]: walkerText };
|
|
237
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
238
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
239
|
+
for (let i = 0; i < 4; i++) {
|
|
240
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
241
|
+
syncActors(world, runtime, {}, DT);
|
|
242
|
+
}
|
|
243
|
+
world.scene.userData.groundHeight = () => {
|
|
244
|
+
throw new Error('no ground');
|
|
245
|
+
};
|
|
246
|
+
for (const actor of actors) actor.components.Transform.x += 0.05;
|
|
247
|
+
assert.throws(() => syncActors(world, runtime, {}, DT), /no ground/);
|
|
248
|
+
if (key) nothingWrittenDown(world, 'after the throw');
|
|
249
|
+
delete world.scene.userData.groundHeight;
|
|
250
|
+
for (let i = 0; i < 2; i++) syncActors(world, runtime, {}, DT);
|
|
251
|
+
return { result: drawn(world) };
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
// A fifth review, first case: A's node is moved under B's and given a child
|
|
255
|
+
// C; the frame is drawn; C is removed and A despawned (its node stays under
|
|
256
|
+
// B, as it always has); then a scene without the key keeps B. What A's node
|
|
257
|
+
// remembers of its matrices must hold no object, C least of all.
|
|
258
|
+
'a despawned node kept under another keeps nothing alive'(world, key) {
|
|
259
|
+
const files = { [WALKER]: walkerText };
|
|
260
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
261
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
262
|
+
const draw = () => world.scene.updateMatrixWorld();
|
|
263
|
+
for (let i = 0; i < 3; i++) {
|
|
264
|
+
syncActors(world, runtime, {}, DT);
|
|
265
|
+
draw();
|
|
266
|
+
}
|
|
267
|
+
const nodeA = world.nodes.get('a').node;
|
|
268
|
+
const child = new THREE.Mesh();
|
|
269
|
+
world.nodes.get('b').node.add(nodeA);
|
|
270
|
+
nodeA.add(child);
|
|
271
|
+
for (let i = 0; i < 3; i++) {
|
|
272
|
+
syncActors(world, runtime, {}, DT);
|
|
273
|
+
draw();
|
|
274
|
+
}
|
|
275
|
+
nodeA.remove(child);
|
|
276
|
+
actors.splice(0, 1);
|
|
277
|
+
syncActors(world, runtime, {}, DT);
|
|
278
|
+
const next = makeRuntime(false, 'scenes/one.scene', [walker('b', 3)], files);
|
|
279
|
+
for (let i = 0; i < 2; i++) {
|
|
280
|
+
syncActors(world, next, {}, DT);
|
|
281
|
+
draw();
|
|
282
|
+
}
|
|
283
|
+
const record = nodeA.computedFrom ?? null;
|
|
284
|
+
const objectsHeld = record === null ? 0 : Object.values(record).filter((value) => typeof value === 'object' && value !== null && !ArrayBuffer.isView(value)).length;
|
|
285
|
+
if (key) {
|
|
286
|
+
assert.equal(objectsHeld, 0, 'the node\'s record refers to an object');
|
|
287
|
+
assert.ok(!reaches(nodeA, child), 'the removed child is still reachable from the despawned node');
|
|
288
|
+
assert.equal(record, null, 'the record is dropped once the switch is off');
|
|
289
|
+
}
|
|
290
|
+
return { result: { nodes: drawn(world), stillUnderB: nodeA.parent === world.nodes.get('b').node } };
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
// A fifth review, second case: an instanced mesh is replaced by a bigger
|
|
294
|
+
// one (one removed, one added: the scene has as many children as before),
|
|
295
|
+
// which tells a listener on the scene. Nothing may be left written down.
|
|
296
|
+
'an instanced mesh is replaced'(world, key) {
|
|
297
|
+
const ROCK = 'models/rock.pxmodel';
|
|
298
|
+
const files = { [ROCK]: fs.readFileSync(new URL('../models/rock.pxmodel', import.meta.url), 'utf8') };
|
|
299
|
+
world.instanceModels = true;
|
|
300
|
+
world.camera.position.set(0, 10, 40);
|
|
301
|
+
world.camera.lookAt(0, 0, 0);
|
|
302
|
+
world.camera.updateMatrixWorld();
|
|
303
|
+
// One in view and nineteen far out of it: the instanced mesh is made for
|
|
304
|
+
// a few. Then all twenty in view: it has to be made again, bigger.
|
|
305
|
+
const actors = Array.from({ length: 20 }, (_, i) => {
|
|
306
|
+
const actor = walker(`r${i}`, i === 0 ? 0 : 5000 + i);
|
|
307
|
+
actor.components.Model = { file: ROCK, scale: 1, yOffset: 0 };
|
|
308
|
+
return actor;
|
|
309
|
+
});
|
|
310
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
311
|
+
const draw = () => world.scene.updateMatrixWorld();
|
|
312
|
+
for (let i = 0; i < 3; i++) {
|
|
313
|
+
syncActors(world, runtime, {}, DT);
|
|
314
|
+
draw();
|
|
315
|
+
}
|
|
316
|
+
const children = world.scene.children.length;
|
|
317
|
+
let told = 0;
|
|
318
|
+
const listener = () => {
|
|
319
|
+
told += 1;
|
|
320
|
+
};
|
|
321
|
+
world.scene.addEventListener('childadded', listener);
|
|
322
|
+
actors.forEach((actor, i) => {
|
|
323
|
+
actor.components.Transform.x = i - 10;
|
|
324
|
+
});
|
|
325
|
+
syncActors(world, runtime, {}, DT);
|
|
326
|
+
draw();
|
|
327
|
+
syncActors(world, runtime, {}, DT);
|
|
328
|
+
world.scene.removeEventListener('childadded', listener);
|
|
329
|
+
assert.equal(world.scene.children.length, children, 'the scene has as many children as before');
|
|
330
|
+
assert.ok(world.instances.size > 0 && told > 0, `an instanced mesh was replaced and the listener told (instances ${world.instances.size}, told ${told}, capacity ${[...world.instances.values()].map((i) => i.parts.map((p) => p.capacity + ':' + (p.mesh?.count ?? '-')).join('/')).join(' ')})`);
|
|
331
|
+
if (key) nothingWrittenDown(world, 'after the listener was told');
|
|
332
|
+
return { result: { nodes: drawn(world), told, instanced: [...world.instances.values()].map((i) => i.parts.map((part) => part.mesh?.count ?? 0).join('/')).join(' ') } };
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
// A fifth review, third case: the ground is dressed anew and a listener on
|
|
336
|
+
// the old material's `dispose` throws. The pass ends there, and leaves
|
|
337
|
+
// nothing written down.
|
|
338
|
+
'a ground material listener throws'(world, key) {
|
|
339
|
+
const files = { [WALKER]: walkerText };
|
|
340
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
341
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, files);
|
|
342
|
+
for (let i = 0; i < 4; i++) syncActors(world, runtime, {}, DT);
|
|
343
|
+
if (key) assert.ok(vouchedFor(world, world.nodes.get('a')), 'warm: the nodes are vouched for');
|
|
344
|
+
world.ground.material.addEventListener('dispose', () => {
|
|
345
|
+
throw new Error('ground listener');
|
|
346
|
+
});
|
|
347
|
+
runtime.data = { ground: { color: '#102030' } };
|
|
348
|
+
assert.throws(() => syncActors(world, runtime, {}, DT), /ground listener/);
|
|
349
|
+
if (key) nothingWrittenDown(world, 'after the listener threw');
|
|
350
|
+
for (let i = 0; i < 2; i++) syncActors(world, runtime, {}, DT);
|
|
351
|
+
return { result: drawn(world) };
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
// Fourth case: a Shape holding a plain value 3,000 levels deep syncs with
|
|
355
|
+
// the key as it does without.
|
|
356
|
+
'a component deeper than the stack'(world, key) {
|
|
357
|
+
const actors = [walker('a', 0)];
|
|
358
|
+
delete actors[0].components.Model;
|
|
359
|
+
let at = actors[0].components.Shape;
|
|
360
|
+
for (let i = 0; i < 3000; i++) {
|
|
361
|
+
at.inner = {};
|
|
362
|
+
at = at.inner;
|
|
363
|
+
}
|
|
364
|
+
const runtime = makeRuntime(key, 'scenes/one.scene', actors, {});
|
|
365
|
+
for (let i = 0; i < 3; i++) syncActors(world, runtime, {}, DT);
|
|
366
|
+
actors[0].components.Shape.color = '#010203';
|
|
367
|
+
syncActors(world, runtime, {}, DT);
|
|
368
|
+
return { result: drawn(world) };
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// And the plain course of things, so the scenarios above are not the only
|
|
373
|
+
// passes compared: a pass that is alone keeps what it wrote down.
|
|
374
|
+
function alonePassesKeepTheirState() {
|
|
375
|
+
const world = makeWorld();
|
|
376
|
+
const files = { [WALKER]: walkerText };
|
|
377
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
378
|
+
const runtime = makeRuntime(true, 'scenes/one.scene', actors, files);
|
|
379
|
+
for (let i = 0; i < 4; i++) syncActors(world, runtime, {}, DT);
|
|
380
|
+
assert.ok(vouchedFor(world, world.nodes.get('a')) && world.modelSigs.size === 1, 'still actors are vouched for');
|
|
381
|
+
const generation = world.generation;
|
|
382
|
+
for (let i = 0; i < 4; i++) syncActors(world, runtime, {}, DT);
|
|
383
|
+
assert.equal(world.generation, generation, 'and a still pass never leaves the engine');
|
|
384
|
+
disposeWorld(world);
|
|
385
|
+
nothingWrittenDown(world, 'after dispose');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Whether `target` can be reached from `root` through own properties, Map
|
|
389
|
+
// entries and Set members: what a collector follows, looked at directly.
|
|
390
|
+
function reaches(root, target) {
|
|
391
|
+
const visited = new Set();
|
|
392
|
+
const stack = [root];
|
|
393
|
+
while (stack.length) {
|
|
394
|
+
const value = stack.pop();
|
|
395
|
+
if (value === target) return true;
|
|
396
|
+
if (value === null || (typeof value !== 'object' && typeof value !== 'function') || visited.has(value)) continue;
|
|
397
|
+
visited.add(value);
|
|
398
|
+
if (value instanceof Map) for (const [key, held] of value) stack.push(key, held);
|
|
399
|
+
else if (value instanceof Set) stack.push(...value);
|
|
400
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
401
|
+
const own = Object.getOwnPropertyDescriptor(value, key);
|
|
402
|
+
if (own && 'value' in own) stack.push(own.value);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Disposing the world releases every node, which runs `dispose` listeners.
|
|
409
|
+
// One that syncs the world again must not leave anything written down: a
|
|
410
|
+
// world being disposed never skips.
|
|
411
|
+
function disposeLeavesNothing() {
|
|
412
|
+
const world = makeWorld();
|
|
413
|
+
const actors = [walker('a', 0), walker('b', 3)];
|
|
414
|
+
for (const actor of actors) delete actor.components.Model;
|
|
415
|
+
const runtime = makeRuntime(true, 'scenes/one.scene', actors, {});
|
|
416
|
+
for (let i = 0; i < 4; i++) syncActors(world, runtime, {}, DT);
|
|
417
|
+
const held = world.nodes.get('a');
|
|
418
|
+
assert.ok(vouchedFor(world, held), 'warm: the node is vouched for');
|
|
419
|
+
let called = 0;
|
|
420
|
+
for (const entry of world.nodes.values()) {
|
|
421
|
+
entry.node.traverse((object) => {
|
|
422
|
+
for (const thing of [object.geometry, object.material].flat()) {
|
|
423
|
+
thing?.addEventListener('dispose', () => {
|
|
424
|
+
called += 1;
|
|
425
|
+
syncActors(world, runtime, {}, DT);
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
disposeWorld(world);
|
|
431
|
+
assert.ok(called > 0, 'a dispose listener ran and synced the world again');
|
|
432
|
+
assert.ok(!vouchedFor(world, held), 'after dispose: the entry someone holds is still vouched for');
|
|
433
|
+
nothingWrittenDown(world, 'after a dispose that was re-entered');
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export async function run() {
|
|
437
|
+
let count = 0;
|
|
438
|
+
for (const [name, scenario] of Object.entries(scenarios)) {
|
|
439
|
+
if (process.env.ONLY && !name.includes(process.env.ONLY)) continue;
|
|
440
|
+
const plainWorld = makeWorld();
|
|
441
|
+
const plain = { ...scenario(plainWorld, false), world: plainWorld };
|
|
442
|
+
const keyedWorld = makeWorld();
|
|
443
|
+
const keyed = scenario(keyedWorld, true);
|
|
444
|
+
assert.deepEqual(keyed.result, plain.result, `${name}: the pass ends differently with the key`);
|
|
445
|
+
if (keyed.held !== undefined) assert.equal(keyed.held, plain.held, `${name}: entries hold more with the key`);
|
|
446
|
+
if (keyed.leftB) {
|
|
447
|
+
// Nothing of the scene that was left is reachable from the world. (The
|
|
448
|
+
// collector itself is asked in the browser: qa/engine-follows.mjs.)
|
|
449
|
+
const shape = keyed.leftB.components.Shape;
|
|
450
|
+
assert.ok(reaches({ hold: [shape] }, shape) && !reaches(plain.world, plain.leftB.components.Shape), 'the walk finds what is held, and nothing without the key');
|
|
451
|
+
assert.ok(!reaches(keyedWorld, shape), `${name}: the left scene's Shape is still reachable from the world`);
|
|
452
|
+
assert.ok(!reaches(keyedWorld, keyed.leftB), `${name}: the left scene's actor is still reachable from the world`);
|
|
453
|
+
}
|
|
454
|
+
count += 1;
|
|
455
|
+
}
|
|
456
|
+
if (!process.env.ONLY) alonePassesKeepTheirState();
|
|
457
|
+
if (!process.env.ONLY) disposeLeavesNothing();
|
|
458
|
+
return count;
|
|
459
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// The physics system's half of `"engine": { "skipUnchangedActors": true }`:
|
|
2
|
+
// what a skipping frame writes down (`remembered`, systems/physics3d.js) goes
|
|
3
|
+
// when its world goes. After `reset` (every scene load), after a frame without
|
|
4
|
+
// the key, and for one body when that body is removed, nothing written down is
|
|
5
|
+
// left, so a body entry someone still holds cannot keep the old scene's
|
|
6
|
+
// components alive. rapier is stubbed: this is about what the system keeps,
|
|
7
|
+
// not about the simulation.
|
|
8
|
+
//
|
|
9
|
+
// test-static-actors.mjs runs it (`npm run check`).
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { register } from 'node:module';
|
|
12
|
+
|
|
13
|
+
const stub = `
|
|
14
|
+
const any = () => new Proxy(function () {}, {
|
|
15
|
+
get: (t, k) => (k === 'then' ? undefined : k === Symbol.toPrimitive ? () => 0 : any()),
|
|
16
|
+
apply: () => any(),
|
|
17
|
+
construct: () => any(),
|
|
18
|
+
});
|
|
19
|
+
export const init = async () => {};
|
|
20
|
+
export const World = any();
|
|
21
|
+
export const EventQueue = any();
|
|
22
|
+
export const RigidBodyDesc = any();
|
|
23
|
+
export const ColliderDesc = any();
|
|
24
|
+
export const RigidBodyType = { Dynamic: 0, Fixed: 1, KinematicPositionBased: 2, KinematicVelocityBased: 3 };
|
|
25
|
+
export const ActiveEvents = any();
|
|
26
|
+
export const ActiveCollisionTypes = any();
|
|
27
|
+
export const Ray = any();
|
|
28
|
+
export const Quaternion = any();
|
|
29
|
+
export const Vector3 = any();
|
|
30
|
+
`;
|
|
31
|
+
const hooks = `
|
|
32
|
+
import { existsSync } from 'node:fs';
|
|
33
|
+
import { fileURLToPath } from 'node:url';
|
|
34
|
+
export async function resolve(specifier, context, next) {
|
|
35
|
+
const parent = context.parentURL ?? '';
|
|
36
|
+
if (specifier === '@dimforge/rapier3d-compat') {
|
|
37
|
+
return { url: 'data:text/javascript,' + encodeURIComponent(${JSON.stringify(stub)}), shortCircuit: true };
|
|
38
|
+
}
|
|
39
|
+
if (specifier.startsWith('@imports/castle.physics-2d/')) {
|
|
40
|
+
specifier = parent.replace(/\\/kits\\/.*/, '/kits/physics-2d/') + specifier.slice(27);
|
|
41
|
+
}
|
|
42
|
+
if ((specifier.startsWith('.') || specifier.startsWith('file:')) && parent.includes('/kits/') && !/\\.(m?js|jsx|json)$/.test(specifier)) {
|
|
43
|
+
const base = new URL(specifier, parent).href;
|
|
44
|
+
for (const ext of ['.js', '.jsx']) if (existsSync(fileURLToPath(base + ext))) return next(base + ext, context);
|
|
45
|
+
}
|
|
46
|
+
return next(specifier, context);
|
|
47
|
+
}`;
|
|
48
|
+
register(`data:text/javascript,${encodeURIComponent(hooks)}`);
|
|
49
|
+
const { Physics3DSystem } = await import('../systems/physics3d.js');
|
|
50
|
+
|
|
51
|
+
const DT = 1 / 60;
|
|
52
|
+
const actor = (id, x) => ({
|
|
53
|
+
id,
|
|
54
|
+
components: {
|
|
55
|
+
Transform: { x, y: 0.5, z: 0, rotX: 0, rotY: 0, rotZ: 0, scaleX: 1, scaleY: 1, scaleZ: 1 },
|
|
56
|
+
Shape: { kind: 'box', width: 1, height: 1, depth: 1, solid: true },
|
|
57
|
+
},
|
|
58
|
+
behaviors: [],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
function makeScene(keyed, actors) {
|
|
62
|
+
return { skipsUnchangedActors: keyed, data: {}, actors, getActors: () => actors, getActor: (id) => actors.find((a) => a.id === id) };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function makeSystem() {
|
|
66
|
+
const system = new Physics3DSystem();
|
|
67
|
+
system.attach({});
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
69
|
+
return system;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function frames(system, scene, n) {
|
|
73
|
+
for (let i = 0; i < n; i += 1) system.afterBehaviors(scene, DT);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function run() {
|
|
77
|
+
let scenarios = 0;
|
|
78
|
+
|
|
79
|
+
// A scene load: reset drops everything, so a held entry reaches nothing.
|
|
80
|
+
{
|
|
81
|
+
const system = await makeSystem();
|
|
82
|
+
const scene = makeScene(true, [actor('a', 0), actor('b', 2)]);
|
|
83
|
+
frames(system, scene, 3);
|
|
84
|
+
const held = system.bodies.get('a');
|
|
85
|
+
assert.ok(held, 'the keyed frames made a body');
|
|
86
|
+
assert.ok(system.remembered.has(held), 'and wrote down what it was made from');
|
|
87
|
+
system.reset();
|
|
88
|
+
assert.equal(system.remembered, null, 'reset keeps nothing written down');
|
|
89
|
+
assert.equal(system.rememberedIn, null, 'nor the world it was written for');
|
|
90
|
+
frames(system, makeScene(false, [actor('a', 0)]), 4);
|
|
91
|
+
assert.equal(system.remembered, null, 'frames without the key write nothing down');
|
|
92
|
+
scenarios += 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The key goes without a reset in between: the next frame drops it.
|
|
96
|
+
{
|
|
97
|
+
const system = await makeSystem();
|
|
98
|
+
const actors = [actor('a', 0), actor('b', 2)];
|
|
99
|
+
frames(system, makeScene(true, actors), 3);
|
|
100
|
+
assert.ok(system.remembered);
|
|
101
|
+
frames(system, makeScene(false, actors), 1);
|
|
102
|
+
assert.equal(system.remembered, null, 'a frame without the key drops what was written down');
|
|
103
|
+
assert.equal(system.rememberedIn, null);
|
|
104
|
+
scenarios += 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// One body removed while skipping: its entry holds nothing afterwards.
|
|
108
|
+
{
|
|
109
|
+
const system = await makeSystem();
|
|
110
|
+
const actors = [actor('a', 0), actor('b', 2)];
|
|
111
|
+
const scene = makeScene(true, actors);
|
|
112
|
+
frames(system, scene, 3);
|
|
113
|
+
const held = system.bodies.get('b');
|
|
114
|
+
assert.ok(system.remembered.has(held));
|
|
115
|
+
actors.pop();
|
|
116
|
+
frames(system, scene, 1);
|
|
117
|
+
assert.equal(system.bodies.has('b'), false, 'the body went with its actor');
|
|
118
|
+
assert.equal(system.remembered.has(held), false, 'and what was written down for it went too');
|
|
119
|
+
assert.ok(system.remembered.has(system.bodies.get('a')), 'the other body keeps its own');
|
|
120
|
+
scenarios += 1;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return scenarios;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
127
|
+
console.log(`static physics: ${await run()} scenarios keep nothing past their world`);
|
|
128
|
+
}
|