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,511 @@
|
|
|
1
|
+
// Grabbing and throwing (contract §6), the client's offer (code/client/grab.js)
|
|
2
|
+
// on a fake scene, multiplayer kit and chrome: a place without `grab.reach`
|
|
3
|
+
// or `grab.steady` is offered exactly what it was (the 1.6 m reach, the 60
|
|
4
|
+
// degree cone, an object the moment it is this client's and not a frame
|
|
5
|
+
// longer); v1.22's `reach` lengthens the reach; `steady` keeps the same object
|
|
6
|
+
// on offer through a frame out of the cone or out of this client's hands, holds
|
|
7
|
+
// the button up for its grace and no longer, lets a press in the grace wait
|
|
8
|
+
// for the object to be this client's again, and never sends a grab for an
|
|
9
|
+
// object this client does not own. v1.27: an object the place keeps for
|
|
10
|
+
// another player (`hub.keepFor`, told in `hubKept`) is never offered, the next
|
|
11
|
+
// nearest is, and it does not hold the steady offer. `npm run check` runs it;
|
|
12
|
+
// it needs nothing installed.
|
|
13
|
+
|
|
14
|
+
import assert from 'node:assert/strict';
|
|
15
|
+
import {
|
|
16
|
+
KEEP_MARGIN_M,
|
|
17
|
+
KEEP_TELL_MS,
|
|
18
|
+
OFFER_CENTRE_M,
|
|
19
|
+
offerCap,
|
|
20
|
+
PRESS_WAIT_MS,
|
|
21
|
+
STEADY_GRACE_MS,
|
|
22
|
+
makeGrab,
|
|
23
|
+
} from '../code/client/grab.js';
|
|
24
|
+
import { hub } from '../code/client/state.js';
|
|
25
|
+
import { GRAB_REACH, KEEP_LEASE_MS } from '../code/server/grab.js';
|
|
26
|
+
import { GRAB_DEFAULTS } from '../code/server/manifest.js';
|
|
27
|
+
|
|
28
|
+
let clock = 1000;
|
|
29
|
+
Object.defineProperty(globalThis, 'performance', {
|
|
30
|
+
value: { now: () => clock },
|
|
31
|
+
configurable: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const DEG = Math.PI / 180;
|
|
35
|
+
// A pillow: a sphere 0.3 m from its centre to its edge.
|
|
36
|
+
const PILLOW = { kind: 'sphere', radius: 0.3 };
|
|
37
|
+
|
|
38
|
+
// One client in a place: the player at the origin facing +z, and loose
|
|
39
|
+
// objects `{ id: { x, z, y?, shape? } }`, all this client's to begin with.
|
|
40
|
+
function rig(grab, objects) {
|
|
41
|
+
hub.place = grab === null ? {} : { grab };
|
|
42
|
+
hub.hidden.clear();
|
|
43
|
+
hub.held.clear();
|
|
44
|
+
hub.grabEvents.length = 0;
|
|
45
|
+
hub.keptFor = new Map();
|
|
46
|
+
const prompts = [];
|
|
47
|
+
const sent = [];
|
|
48
|
+
let press = null;
|
|
49
|
+
const chrome = {
|
|
50
|
+
onGrab: (fn) => (press = fn),
|
|
51
|
+
grabPrompt: (text) => prompts.push(text),
|
|
52
|
+
};
|
|
53
|
+
const g = makeGrab(chrome);
|
|
54
|
+
const actors = new Map();
|
|
55
|
+
const bodies = new Map();
|
|
56
|
+
for (const [id, o] of Object.entries(objects)) {
|
|
57
|
+
const transform = { x: o.x, y: o.y ?? 0.85, z: o.z };
|
|
58
|
+
actors.set(id, {
|
|
59
|
+
components: { Transform: transform, Shape: o.shape ?? PILLOW, Body: { type: 'dynamic' } },
|
|
60
|
+
});
|
|
61
|
+
bodies.set(id, {
|
|
62
|
+
translation: () => ({ x: transform.x, y: transform.y, z: transform.z }),
|
|
63
|
+
isDynamic: () => true,
|
|
64
|
+
setTranslation(p) {
|
|
65
|
+
Object.assign(transform, p);
|
|
66
|
+
},
|
|
67
|
+
setLinvel() {},
|
|
68
|
+
setAngvel() {},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const scene = {
|
|
72
|
+
getActor: (id) => actors.get(id) ?? null,
|
|
73
|
+
physics3d: { ready: () => true, bodyOf: (id) => bodies.get(id) ?? null },
|
|
74
|
+
};
|
|
75
|
+
const mine = new Set(actors.keys());
|
|
76
|
+
const mp = {
|
|
77
|
+
net: { selfId: () => 'me', send: (d) => sent.push(d) },
|
|
78
|
+
own: { mine },
|
|
79
|
+
claimable: () => [...actors].map(([id, a]) => [id, a.components.Transform]),
|
|
80
|
+
};
|
|
81
|
+
const me = { x: 0, y: 0.85, z: 0, rotationY: 0 };
|
|
82
|
+
const local = { components: { Transform: me } };
|
|
83
|
+
// One frame `ms` after the last, with a press in it or not; the button's word.
|
|
84
|
+
const step = (ms = 16, pressed = false, idle = true) => {
|
|
85
|
+
clock += ms;
|
|
86
|
+
if (pressed) {
|
|
87
|
+
press();
|
|
88
|
+
}
|
|
89
|
+
g.frame(scene, mp, local, idle);
|
|
90
|
+
return prompts.at(-1);
|
|
91
|
+
};
|
|
92
|
+
const place = (id, x, z) => {
|
|
93
|
+
const t = actors.get(id).components.Transform;
|
|
94
|
+
t.x = x;
|
|
95
|
+
t.z = z;
|
|
96
|
+
};
|
|
97
|
+
const grabs = () => sent.filter((d) => d.k === 'hub-grab' && d.op === 'grab').map((d) => d.actor);
|
|
98
|
+
return { g, step, me, mine, place, grabs, sent, prompts, actors };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Where an object at `distance` (centre, flat) and `deg` off facing stands.
|
|
102
|
+
function at(distance, deg) {
|
|
103
|
+
return { x: Math.sin(deg * DEG) * distance, z: Math.cos(deg * DEG) * distance };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let n = 0;
|
|
107
|
+
function test(name, fn) {
|
|
108
|
+
try {
|
|
109
|
+
fn();
|
|
110
|
+
} catch (err) {
|
|
111
|
+
console.error(`test-grab-client: ${name}`);
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
n += 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
test('without reach or steady: the 1.6 m reach, the 60 degree cone, and ownership each frame, as before', () => {
|
|
118
|
+
const { step, place, mine, me } = rig({ ...GRAB_DEFAULTS }, { a: at(1.85, 0) });
|
|
119
|
+
// Near side 1.55 m: offered; 1.65 m: not.
|
|
120
|
+
assert.equal(step(), 'Grab');
|
|
121
|
+
place('a', 0, 1.95);
|
|
122
|
+
assert.equal(step(), null);
|
|
123
|
+
// 59 degrees off facing: offered; 61: not.
|
|
124
|
+
place('a', at(1.5, 59).x, at(1.5, 59).z);
|
|
125
|
+
assert.equal(step(), 'Grab');
|
|
126
|
+
place('a', at(1.5, 61).x, at(1.5, 61).z);
|
|
127
|
+
assert.equal(step(), null);
|
|
128
|
+
// A frame out of this client's hands takes the button down at once.
|
|
129
|
+
place('a', 0, 1.5);
|
|
130
|
+
assert.equal(step(), 'Grab');
|
|
131
|
+
mine.delete('a');
|
|
132
|
+
assert.equal(step(), null);
|
|
133
|
+
mine.add('a');
|
|
134
|
+
assert.equal(step(), 'Grab');
|
|
135
|
+
// So does a turn out of the cone.
|
|
136
|
+
me.rotationY = 70;
|
|
137
|
+
assert.equal(step(), null);
|
|
138
|
+
// And nothing farther than 1.6 m is offered, however steady the player.
|
|
139
|
+
me.rotationY = 0;
|
|
140
|
+
place('a', 0, 2.4);
|
|
141
|
+
for (let i = 0; i < 5; i += 1) {
|
|
142
|
+
assert.equal(step(), null);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('without reach or steady: a big object is offered past 3.5 m centre to centre, as before', () => {
|
|
147
|
+
// A wide box (1.84 m from centre to edge) whose centre is 3.4 m out and
|
|
148
|
+
// 1.8 m up: the v1.22 centre bound applies only to a place that asks.
|
|
149
|
+
const box = { kind: 'box', width: 2.6, height: 1, depth: 2.6 };
|
|
150
|
+
const before = rig({ ...GRAB_DEFAULTS }, { a: { x: 0, z: 3.4, y: 2.65, shape: box } });
|
|
151
|
+
assert.ok(Math.hypot(3.4, 1.8) > OFFER_CENTRE_M);
|
|
152
|
+
assert.equal(before.step(), 'Grab');
|
|
153
|
+
const asked = rig({ ...GRAB_DEFAULTS, reach: 1.6 }, { a: { x: 0, z: 3.4, y: 2.65, shape: box } });
|
|
154
|
+
assert.equal(asked.step(), null);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('a place without grab draws no button and sends nothing', () => {
|
|
158
|
+
const { step, grabs } = rig(null, { a: at(1, 0) });
|
|
159
|
+
assert.equal(step(0, true), null);
|
|
160
|
+
assert.deepEqual(grabs(), []);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('a press grabs what is offered, the same with or without steady', () => {
|
|
164
|
+
for (const grab of [{ ...GRAB_DEFAULTS }, { ...GRAB_DEFAULTS, steady: true }]) {
|
|
165
|
+
const { step, grabs, g } = rig(grab, { a: at(1.2, 0), b: at(1.5, 10) });
|
|
166
|
+
assert.equal(step(), 'Grab');
|
|
167
|
+
assert.equal(step(16, true), 'Throw');
|
|
168
|
+
assert.deepEqual(grabs(), ['a']);
|
|
169
|
+
assert.equal(g.holding(), 'a');
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('reach: objects are offered farther away, and never past 3 m centre to centre', () => {
|
|
174
|
+
const { step, place } = rig({ ...GRAB_DEFAULTS, reach: 2.2 }, { a: at(2.45, 0) });
|
|
175
|
+
assert.equal(step(), 'Grab');
|
|
176
|
+
place('a', 0, 2.55);
|
|
177
|
+
assert.equal(step(), null);
|
|
178
|
+
// Without steady, the reach has no hysteresis.
|
|
179
|
+
place('a', 0, 2.45);
|
|
180
|
+
assert.equal(step(), 'Grab');
|
|
181
|
+
place('a', 0, 2.55);
|
|
182
|
+
assert.equal(step(), null);
|
|
183
|
+
// At the top of the reach, a wide object whose near side is in reach but
|
|
184
|
+
// whose centre is past the cap (3 m at most, 2.55 m for a walking player) is not offered.
|
|
185
|
+
const box = { kind: 'box', width: 2, height: 1, depth: 2 };
|
|
186
|
+
const far = rig({ ...GRAB_DEFAULTS, reach: 3 }, { a: { x: 0, z: 3.9, shape: box } });
|
|
187
|
+
assert.equal(far.step(), null);
|
|
188
|
+
far.place('a', 0, 2.5);
|
|
189
|
+
assert.equal(far.step(), 'Grab');
|
|
190
|
+
assert.ok(OFFER_CENTRE_M < GRAB_REACH);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('reach: a faster-moving player is offered less far, so a running grab stays inside the server reach', () => {
|
|
194
|
+
// The server judges a grab from a pose that may trail the player's own;
|
|
195
|
+
// the offer keeps inside 4 m by what they travel in 0.2 s at the speed
|
|
196
|
+
// they move (their body's velocity, jumps included) or are tuned to.
|
|
197
|
+
const lag = 0.2;
|
|
198
|
+
for (const [velocity, tune, base] of [
|
|
199
|
+
[null, null, undefined],
|
|
200
|
+
[{ x: 6, y: 0, z: 0 }, null, undefined],
|
|
201
|
+
[{ x: 18, y: 0, z: 0 }, null, undefined],
|
|
202
|
+
[{ x: 0, y: 20, z: 0 }, { speed: 1, jump: 2.5 }, undefined],
|
|
203
|
+
[null, { speed: 3 }, 36], // an authored 12 m/s preset, tuned three times (Player.speed is already tuned)
|
|
204
|
+
[{ x: 18, y: 0, z: 0 }, null, 6], // a tune just lowered: the body still runs
|
|
205
|
+
]) {
|
|
206
|
+
const cap = offerCap(velocity, tune, base);
|
|
207
|
+
const speed = Math.max(velocity ? Math.hypot(velocity.x, velocity.y, velocity.z) : 0, base ?? 6 * Math.max(1, tune?.speed ?? 1));
|
|
208
|
+
assert.ok(cap >= 0 && cap <= OFFER_CENTRE_M);
|
|
209
|
+
assert.ok(cap === 0 || cap + speed * lag < GRAB_REACH, `${JSON.stringify([velocity, tune, base])}: ${cap}`);
|
|
210
|
+
}
|
|
211
|
+
assert.ok(offerCap({ x: 18, y: 0, z: 0 }) < offerCap(null));
|
|
212
|
+
// The tune is counted once: a walk preset tuned twice as fast (Player.speed 12) still offers.
|
|
213
|
+
assert.ok(offerCap(null, { speed: 2 }, 12) > 0);
|
|
214
|
+
assert.equal(offerCap(null, { speed: 2 }, 12), offerCap(null, { speed: 2 }));
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('steady: the same object stays offered through a frame out of the cone or out of this client’s hands', () => {
|
|
218
|
+
const { step, me, mine, place, grabs } = rig(
|
|
219
|
+
{ ...GRAB_DEFAULTS, steady: true, reach: 2.2 },
|
|
220
|
+
{
|
|
221
|
+
a: at(2.2, 0),
|
|
222
|
+
b: at(4, 0),
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
assert.equal(step(), 'Grab');
|
|
226
|
+
// Another object comes nearer: the offer stays on the first.
|
|
227
|
+
place('b', 0, 1);
|
|
228
|
+
assert.equal(step(), 'Grab');
|
|
229
|
+
// Out of the 60 degree cone, inside 90: still offered, no grace needed.
|
|
230
|
+
me.rotationY = 75;
|
|
231
|
+
assert.equal(step(), 'Grab');
|
|
232
|
+
// (Through a lost frame another object in reach would take the offer over:
|
|
233
|
+
// the next test. Here there is none.)
|
|
234
|
+
place('b', 0, 4);
|
|
235
|
+
// A step back past the reach, inside its margin: still offered.
|
|
236
|
+
me.rotationY = 0;
|
|
237
|
+
place('a', 0, 2.2 + 0.3 + 0.03); // past the reach, inside its margin and the walking cap
|
|
238
|
+
assert.equal(step(), 'Grab');
|
|
239
|
+
// A frame where the multiplayer kit hands it to someone else.
|
|
240
|
+
mine.delete('a');
|
|
241
|
+
assert.equal(step(), 'Grab');
|
|
242
|
+
mine.add('a');
|
|
243
|
+
assert.equal(step(), 'Grab');
|
|
244
|
+
// A frame turned right away from it.
|
|
245
|
+
me.rotationY = 180;
|
|
246
|
+
assert.equal(step(), 'Grab');
|
|
247
|
+
me.rotationY = 0;
|
|
248
|
+
// The press grabs the kept object, not the nearer one.
|
|
249
|
+
assert.equal(step(16, true), 'Throw');
|
|
250
|
+
assert.deepEqual(grabs(), ['a']);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('steady: the grace ends after 600 ms and the button goes; another object takes the offer over meanwhile', () => {
|
|
254
|
+
const { step, mine, place } = rig(
|
|
255
|
+
{ ...GRAB_DEFAULTS, steady: true },
|
|
256
|
+
{ a: at(1.5, 0), b: at(5, 0) },
|
|
257
|
+
);
|
|
258
|
+
assert.equal(step(), 'Grab');
|
|
259
|
+
mine.delete('a');
|
|
260
|
+
let t = 0;
|
|
261
|
+
while (t + 100 <= STEADY_GRACE_MS) {
|
|
262
|
+
assert.equal(step(100), 'Grab');
|
|
263
|
+
t += 100;
|
|
264
|
+
}
|
|
265
|
+
assert.equal(step(STEADY_GRACE_MS - t + 1), null);
|
|
266
|
+
assert.equal(step(), null);
|
|
267
|
+
// Back in this client's hands: offered again.
|
|
268
|
+
mine.add('a');
|
|
269
|
+
assert.equal(step(), 'Grab');
|
|
270
|
+
// Out of hands again, and another object comes into reach: it is offered.
|
|
271
|
+
mine.delete('a');
|
|
272
|
+
place('b', 0, 1);
|
|
273
|
+
assert.equal(step(), 'Grab');
|
|
274
|
+
assert.equal(step(STEADY_GRACE_MS + 100), 'Grab');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test('steady: a press in the grace waits for the object to be this client’s again, then grabs it', () => {
|
|
278
|
+
const { step, mine, grabs, sent } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
|
|
279
|
+
assert.equal(step(), 'Grab');
|
|
280
|
+
mine.delete('a');
|
|
281
|
+
assert.equal(step(16, true), 'Grab');
|
|
282
|
+
assert.equal(step(100), 'Grab');
|
|
283
|
+
assert.deepEqual(sent, []);
|
|
284
|
+
mine.add('a');
|
|
285
|
+
assert.equal(step(100), 'Throw');
|
|
286
|
+
assert.deepEqual(grabs(), ['a']);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test('steady: a press is dropped when the object never comes back, and no grab is ever sent for one this client does not own', () => {
|
|
290
|
+
const { step, mine, sent } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
|
|
291
|
+
assert.equal(step(), 'Grab');
|
|
292
|
+
mine.delete('a');
|
|
293
|
+
assert.equal(step(16, true), 'Grab');
|
|
294
|
+
for (let i = 0; i < 30; i += 1) {
|
|
295
|
+
step(50, i % 3 === 0);
|
|
296
|
+
}
|
|
297
|
+
assert.deepEqual(sent, []);
|
|
298
|
+
assert.equal(step(), null);
|
|
299
|
+
// Back after the press's wait: offered, but the old press does not grab it.
|
|
300
|
+
assert.ok(30 * 50 > PRESS_WAIT_MS + STEADY_GRACE_MS);
|
|
301
|
+
mine.add('a');
|
|
302
|
+
assert.equal(step(), 'Grab');
|
|
303
|
+
assert.equal(step(), 'Grab');
|
|
304
|
+
assert.deepEqual(sent, []);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test('steady: a hidden or removed object, or a player who cannot act, ends the offer at once', () => {
|
|
308
|
+
const { step, actors } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
|
|
309
|
+
assert.equal(step(), 'Grab');
|
|
310
|
+
hub.hidden.add('a');
|
|
311
|
+
assert.equal(step(), null);
|
|
312
|
+
hub.hidden.delete('a');
|
|
313
|
+
assert.equal(step(), 'Grab');
|
|
314
|
+
assert.equal(step(16, false, false), null);
|
|
315
|
+
assert.equal(step(), 'Grab');
|
|
316
|
+
actors.delete('a');
|
|
317
|
+
assert.equal(step(), null);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test('steady: nothing past the reach and its margin is kept, and nothing is first offered past the reach', () => {
|
|
321
|
+
const { step, place } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(2.05, 0) });
|
|
322
|
+
// Near side 1.75 m: past the 1.6 m reach, never offered.
|
|
323
|
+
assert.equal(step(), null);
|
|
324
|
+
place('a', 0, 1.8);
|
|
325
|
+
assert.equal(step(), 'Grab');
|
|
326
|
+
// Past the reach and its margin: the grace runs out.
|
|
327
|
+
place('a', 0, 1.6 + 0.3 + KEEP_MARGIN_M + 0.1);
|
|
328
|
+
assert.equal(step(), 'Grab');
|
|
329
|
+
assert.equal(step(STEADY_GRACE_MS + 1), null);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('keepHeld (v1.25): a holder tells the server the hold goes on, about once a second; no other place sends it', () => {
|
|
333
|
+
const holds = (sent) => sent.filter((d) => d.op === 'hold');
|
|
334
|
+
for (const grab of [{ ...GRAB_DEFAULTS }, { ...GRAB_DEFAULTS, steady: true, reach: 2.2 }]) {
|
|
335
|
+
const { step, sent } = rig(grab, { a: at(1.2, 0) });
|
|
336
|
+
step();
|
|
337
|
+
assert.equal(step(16, true), 'Throw');
|
|
338
|
+
for (let i = 0; i < 300; i += 1) {
|
|
339
|
+
step(33);
|
|
340
|
+
}
|
|
341
|
+
assert.deepEqual(sent, [{ k: 'hub-grab', actor: 'a', op: 'grab' }]);
|
|
342
|
+
}
|
|
343
|
+
for (const grab of [{ ...GRAB_DEFAULTS, keepHeld: true }, { ...GRAB_DEFAULTS, keepHeld: true, steady: true }]) {
|
|
344
|
+
const { step, sent, mine } = rig(grab, { a: at(1.2, 0) });
|
|
345
|
+
step();
|
|
346
|
+
assert.equal(step(16, true), 'Throw');
|
|
347
|
+
for (let i = 0; i < 300; i += 1) {
|
|
348
|
+
step(33);
|
|
349
|
+
}
|
|
350
|
+
// 9.9 s of holding: nine or ten words, each about the held object.
|
|
351
|
+
assert.ok(holds(sent).length >= 9 && holds(sent).length <= 10, String(holds(sent).length));
|
|
352
|
+
assert.ok(holds(sent).every((d) => d.k === 'hub-grab' && d.actor === 'a'));
|
|
353
|
+
// Often enough that the server's pin never lapses between two.
|
|
354
|
+
assert.ok(KEEP_TELL_MS * 2 < KEEP_LEASE_MS);
|
|
355
|
+
// Nothing after the hold ends, whichever way: a throw, or the object gone to another.
|
|
356
|
+
const n = sent.length;
|
|
357
|
+
assert.equal(step(16, true), null);
|
|
358
|
+
for (let i = 0; i < 100; i += 1) {
|
|
359
|
+
step(33);
|
|
360
|
+
}
|
|
361
|
+
assert.equal(holds(sent).length, holds(sent.slice(0, n)).length);
|
|
362
|
+
assert.equal(step(16, true), 'Throw');
|
|
363
|
+
mine.delete('a');
|
|
364
|
+
for (let i = 0; i < 100; i += 1) {
|
|
365
|
+
step(33);
|
|
366
|
+
}
|
|
367
|
+
assert.equal(holds(sent).length, holds(sent.slice(0, n)).length);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// --- v1.27 `hub.keepFor`: an object kept for one player ----------------------
|
|
372
|
+
|
|
373
|
+
// What the server's `hubKept` becomes on this client (`hookLayer.js`).
|
|
374
|
+
const keep = (kept) => {
|
|
375
|
+
hub.keptFor = new Map(Object.entries(kept));
|
|
376
|
+
};
|
|
377
|
+
const PLAIN = { ...GRAB_DEFAULTS };
|
|
378
|
+
const STEADY = { ...GRAB_DEFAULTS, steady: true, reach: 2.2 };
|
|
379
|
+
|
|
380
|
+
test('keepFor: another player’s object is passed over for the next nearest, which is the one grabbed', () => {
|
|
381
|
+
for (const grab of [PLAIN, { ...PLAIN, reach: 2.2 }, STEADY, { ...STEADY, keepHeld: true }]) {
|
|
382
|
+
// `a` is nearer and is someone else's; `b`, a little farther, is this player's.
|
|
383
|
+
const { step, grabs, g } = rig(grab, { a: at(1, 0), b: at(1.5, 10) });
|
|
384
|
+
keep({ a: 'other', b: 'me' });
|
|
385
|
+
assert.equal(step(), 'Grab');
|
|
386
|
+
assert.equal(step(16, true), 'Throw');
|
|
387
|
+
assert.deepEqual(grabs(), ['b']);
|
|
388
|
+
assert.equal(g.holding(), 'b');
|
|
389
|
+
// An object kept for nobody is offered as ever, behind nothing.
|
|
390
|
+
const free = rig(grab, { a: at(1, 0), b: at(1.5, 10) });
|
|
391
|
+
keep({ a: 'other' });
|
|
392
|
+
free.step();
|
|
393
|
+
free.step(16, true);
|
|
394
|
+
assert.deepEqual(free.grabs(), ['b']);
|
|
395
|
+
// The nearest still wins when it is this player's own, or anyone's.
|
|
396
|
+
for (const kept of [{ a: 'me', b: 'other' }, { b: 'other' }, { a: 'me' }]) {
|
|
397
|
+
const own = rig(grab, { a: at(1, 0), b: at(1.5, 10) });
|
|
398
|
+
keep(kept);
|
|
399
|
+
own.step();
|
|
400
|
+
own.step(16, true);
|
|
401
|
+
assert.deepEqual(own.grabs(), ['a']);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test('keepFor: with only another player’s object in reach nothing is offered, and a press sends nothing', () => {
|
|
407
|
+
for (const grab of [PLAIN, STEADY]) {
|
|
408
|
+
const { step, grabs, sent } = rig(grab, { a: at(1, 0) });
|
|
409
|
+
keep({ a: 'other' });
|
|
410
|
+
for (let i = 0; i < 80; i += 1) {
|
|
411
|
+
assert.equal(step(16, i % 7 === 0), null);
|
|
412
|
+
}
|
|
413
|
+
assert.deepEqual(grabs(), []);
|
|
414
|
+
assert.deepEqual(sent, []);
|
|
415
|
+
// Anyone's again (`hub.keepFor(actor, null)`): offered at once.
|
|
416
|
+
keep({});
|
|
417
|
+
assert.equal(step(), 'Grab');
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('keepFor, steady: an object kept for someone else gives up the steady offer at once, grace and waiting press included', () => {
|
|
422
|
+
// `a` holds the steady offer although `b` is nearer (v1.22)...
|
|
423
|
+
const { step, place, grabs } = rig(STEADY, { a: at(1.6, 0), b: at(2.9, 40) });
|
|
424
|
+
assert.equal(step(), 'Grab');
|
|
425
|
+
place('b', at(1.2, 20).x, at(1.2, 20).z);
|
|
426
|
+
step();
|
|
427
|
+
// ...until the place keeps it for another player: `b` takes the offer.
|
|
428
|
+
keep({ a: 'other' });
|
|
429
|
+
assert.equal(step(), 'Grab');
|
|
430
|
+
assert.equal(step(16, true), 'Throw');
|
|
431
|
+
assert.deepEqual(grabs(), ['b']);
|
|
432
|
+
|
|
433
|
+
// Alone, its button does not stay up for the grace.
|
|
434
|
+
const lone = rig(STEADY, { a: at(1.6, 0) });
|
|
435
|
+
assert.equal(lone.step(), 'Grab');
|
|
436
|
+
keep({ a: 'other' });
|
|
437
|
+
assert.equal(lone.step(), null);
|
|
438
|
+
assert.equal(lone.step(16, true), null);
|
|
439
|
+
for (let ms = 0; ms < STEADY_GRACE_MS + PRESS_WAIT_MS + 200; ms += 16) {
|
|
440
|
+
assert.equal(lone.step(), null);
|
|
441
|
+
}
|
|
442
|
+
assert.deepEqual(lone.grabs(), []);
|
|
443
|
+
|
|
444
|
+
// A press already waiting for it (its owner briefly someone else) is dropped,
|
|
445
|
+
// and is not answered when the object is this client's again.
|
|
446
|
+
const waiting = rig(STEADY, { a: at(1.6, 0) });
|
|
447
|
+
assert.equal(waiting.step(), 'Grab');
|
|
448
|
+
waiting.mine.delete('a');
|
|
449
|
+
assert.equal(waiting.step(16, true), 'Grab');
|
|
450
|
+
keep({ a: 'other' });
|
|
451
|
+
assert.equal(waiting.step(), null);
|
|
452
|
+
waiting.mine.add('a');
|
|
453
|
+
assert.equal(waiting.step(), null);
|
|
454
|
+
assert.deepEqual(waiting.grabs(), []);
|
|
455
|
+
// Kept for this player instead, the same press is answered, as before v1.27.
|
|
456
|
+
const mine = rig(STEADY, { a: at(1.6, 0) });
|
|
457
|
+
mine.step();
|
|
458
|
+
mine.mine.delete('a');
|
|
459
|
+
mine.step(16, true);
|
|
460
|
+
keep({ a: 'me' });
|
|
461
|
+
mine.step();
|
|
462
|
+
mine.mine.add('a');
|
|
463
|
+
assert.equal(mine.step(), 'Throw');
|
|
464
|
+
assert.deepEqual(mine.grabs(), ['a']);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
test('keepFor never used, or used only for this player and for objects not here: every prompt and message is what it was', () => {
|
|
468
|
+
const run = (grab, kept) => {
|
|
469
|
+
const r = rig(grab, { a: at(1.2, 0), b: at(1.5, 10), c: at(2.3, -30) });
|
|
470
|
+
if (kept) {
|
|
471
|
+
keep(kept);
|
|
472
|
+
}
|
|
473
|
+
const words = [];
|
|
474
|
+
const go = (...args) => words.push(r.step(...args));
|
|
475
|
+
go();
|
|
476
|
+
r.mine.delete('a');
|
|
477
|
+
go();
|
|
478
|
+
go(16, true);
|
|
479
|
+
r.mine.add('a');
|
|
480
|
+
go();
|
|
481
|
+
go(16, true);
|
|
482
|
+
for (let i = 0; i < 70; i += 1) {
|
|
483
|
+
go(33);
|
|
484
|
+
}
|
|
485
|
+
go(16, true);
|
|
486
|
+
go();
|
|
487
|
+
r.me.rotationY = 75;
|
|
488
|
+
for (let i = 0; i < 50; i += 1) {
|
|
489
|
+
go(16, i === 3);
|
|
490
|
+
}
|
|
491
|
+
r.me.rotationY = -20;
|
|
492
|
+
for (let i = 0; i < 40; i += 1) {
|
|
493
|
+
go(16, i === 30);
|
|
494
|
+
}
|
|
495
|
+
r.place('c', 0, 1.1);
|
|
496
|
+
hub.hidden.add('b');
|
|
497
|
+
for (let i = 0; i < 40; i += 1) {
|
|
498
|
+
go(16, i % 13 === 5);
|
|
499
|
+
}
|
|
500
|
+
return JSON.stringify([words, r.sent]);
|
|
501
|
+
};
|
|
502
|
+
for (const grab of [PLAIN, { ...PLAIN, reach: 2.2 }, STEADY, { ...STEADY, keepHeld: true }]) {
|
|
503
|
+
const plain = run(grab, null);
|
|
504
|
+
assert.ok(plain.includes('"op":"grab"') && plain.includes('"op":"throw"'));
|
|
505
|
+
assert.equal(run(grab, {}), plain);
|
|
506
|
+
assert.equal(run(grab, { a: 'me', b: 'me', c: 'me' }), plain);
|
|
507
|
+
assert.equal(run(grab, { elsewhere: 'other' }), plain);
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
console.log(`test-grab-client: ${n} passed`);
|