castle-web-cli 0.4.157 → 0.4.158
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/agent-failures.d.ts +1 -0
- package/dist/agent-failures.js +6 -4
- package/dist/agent-prompts.js +4 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +175 -15
- package/dist/bundle.js +8 -2
- package/dist/castle-host/host.js +4 -2
- package/dist/castleJson.js +6 -1
- package/dist/check.d.ts +1 -0
- package/dist/check.js +25 -0
- package/dist/devSessionServer.js +31 -7
- package/dist/imports.js +1 -19
- package/dist/index.js +15 -1
- package/dist/init.d.ts +2 -2
- package/dist/init.js +78 -42
- package/dist/mcpPlaytest.d.ts +1 -0
- package/dist/mcpPlaytest.js +182 -0
- package/dist/metering.d.ts +24 -0
- package/dist/metering.js +87 -0
- package/dist/sdkResolve.d.ts +3 -0
- package/dist/sdkResolve.js +55 -0
- package/dist/serve.js +50 -4
- package/dist/serverBundle.js +8 -3
- package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
- package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +20 -0
- package/kits/base/CLAUDE.md +196 -0
- package/kits/{blank → base}/castle.json +5 -2
- package/kits/base/editorHost.js +172 -0
- package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
- package/kits/{blank → base}/package-lock.json +7 -4
- package/kits/{blank → base}/package.json +4 -2
- package/kits/base/sdk/README.md +569 -0
- package/kits/base/sdk/castle.d.ts +27 -0
- package/kits/base/sdk/castle.js +16 -0
- package/kits/base/sdk/commands.d.ts +181 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +17 -0
- package/kits/base/sdk/context.js +15 -0
- package/kits/base/sdk/errors.d.ts +22 -0
- package/kits/base/sdk/errors.js +16 -0
- package/kits/base/sdk/files.d.ts +15 -0
- package/kits/base/sdk/files.js +52 -0
- package/kits/base/sdk/haptics.d.ts +6 -0
- package/kits/base/sdk/haptics.js +36 -0
- package/kits/base/sdk/leaderboard.d.ts +26 -0
- package/kits/base/sdk/leaderboard.js +296 -0
- package/kits/base/sdk/lifecycle.d.ts +4 -0
- package/kits/base/sdk/lifecycle.js +13 -0
- package/kits/base/sdk/multiplayer.d.ts +43 -0
- package/kits/base/sdk/multiplayer.js +464 -0
- package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
- package/kits/base/sdk/multiplayerProtocol.js +5 -0
- package/kits/base/sdk/passes.d.ts +7 -0
- package/kits/base/sdk/passes.js +84 -0
- package/kits/base/sdk/portal.d.ts +7 -0
- package/kits/base/sdk/portal.js +35 -0
- package/kits/base/sdk/runtime.d.ts +62 -0
- package/kits/base/sdk/runtime.js +970 -0
- package/kits/base/sdk/saveQueue.d.ts +27 -0
- package/kits/base/sdk/saveQueue.js +205 -0
- package/kits/base/sdk/server/platformHandle.d.ts +25 -0
- package/kits/base/sdk/server/platformHandle.js +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +28 -0
- package/kits/base/sdk/server/wrapper.js +109 -0
- package/kits/base/sdk/storage.d.ts +16 -0
- package/kits/base/sdk/storage.js +323 -0
- package/kits/base/sdk/time.d.ts +17 -0
- package/kits/base/sdk/time.js +120 -0
- package/kits/base/sdk/transport.d.ts +17 -0
- package/kits/base/sdk/transport.js +141 -0
- package/kits/base/sdk/types.d.ts +3 -0
- package/kits/base/sdk/types.js +1 -0
- package/kits/base/sdk/user.d.ts +10 -0
- package/kits/base/sdk/user.js +45 -0
- package/kits/multiplayer-2d/.prettierrc +5 -0
- package/kits/multiplayer-2d/CLAUDE.md +129 -0
- package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
- package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
- package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
- package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
- package/kits/multiplayer-2d/blueprints/player.scene +15 -0
- package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
- package/kits/multiplayer-2d/castle.json +39 -0
- package/kits/multiplayer-2d/code/client/avatars.js +79 -0
- package/kits/multiplayer-2d/code/client/poses.js +168 -0
- package/kits/multiplayer-2d/code/server/index.js +26 -0
- package/kits/multiplayer-2d/code/server/limits.js +99 -0
- package/kits/multiplayer-2d/code/server/ownership.js +177 -0
- package/kits/multiplayer-2d/code/server/players.js +127 -0
- package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
- package/kits/multiplayer-2d/code/server/world.js +337 -0
- package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
- package/kits/multiplayer-2d/eslint.config.js +21 -0
- package/kits/multiplayer-2d/fonts.generated.js +9 -0
- package/kits/multiplayer-2d/index.html +25 -0
- package/kits/multiplayer-2d/main.jsx +10 -0
- package/kits/multiplayer-2d/package-lock.json +351 -0
- package/kits/multiplayer-2d/package.json +30 -0
- package/kits/multiplayer-2d/scenes/main.scene +20 -0
- package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
- package/kits/multiplayer-2d/theme.style +4 -0
- package/kits/multiplayer-3d/.prettierrc +5 -0
- package/kits/multiplayer-3d/CLAUDE.md +215 -0
- package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
- package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
- package/kits/multiplayer-3d/castle.json +43 -0
- package/kits/multiplayer-3d/code/client/avatars.js +136 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
- package/kits/multiplayer-3d/code/client/poses.js +220 -0
- package/kits/multiplayer-3d/code/server/index.js +23 -0
- package/kits/multiplayer-3d/code/server/limits.js +107 -0
- package/kits/multiplayer-3d/code/server/ownership.js +157 -0
- package/kits/multiplayer-3d/code/server/players.js +114 -0
- package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
- package/kits/multiplayer-3d/code/server/world.js +306 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
- package/kits/multiplayer-3d/eslint.config.js +20 -0
- package/kits/multiplayer-3d/fonts.generated.js +9 -0
- package/kits/multiplayer-3d/index.html +22 -0
- package/kits/multiplayer-3d/main.jsx +9 -0
- package/kits/multiplayer-3d/package-lock.json +368 -0
- package/kits/multiplayer-3d/package.json +32 -0
- package/kits/multiplayer-3d/scenes/main.scene +409 -0
- package/kits/multiplayer-3d/theme.style +3 -0
- package/kits/physics-2d/CLAUDE.md +6 -0
- package/kits/physics-2d/castle.json +7 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
- package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
- package/kits/physics-2d/engine/files.js +9 -5
- package/kits/physics-2d/engine/systemRegistry.js +2 -0
- package/kits/physics-2d/engine/ui.module.css +11 -0
- package/kits/physics-2d/package-lock.json +8 -2
- package/kits/physics-2d/package.json +2 -2
- package/kits/physics-3d/CLAUDE.md +4 -4
- package/kits/physics-3d/behaviors/Shape.jsx +1 -1
- package/kits/physics-3d/blueprints/crate.scene +1 -1
- package/kits/physics-3d/blueprints/door.scene +4 -4
- package/kits/physics-3d/blueprints/pillar.scene +1 -1
- package/kits/physics-3d/blueprints/platform.scene +1 -1
- package/kits/physics-3d/blueprints/statue.scene +1 -1
- package/kits/physics-3d/blueprints/wall.scene +1 -1
- package/kits/physics-3d/castle.json +8 -11
- package/kits/physics-3d/docs/pxmodel-format.md +1 -1
- package/kits/physics-3d/engine3d/materials.js +3 -3
- package/kits/physics-3d/engine3d/modelFiles.js +4 -2
- package/kits/physics-3d/engine3d/world3d.js +1 -1
- package/kits/physics-3d/fonts.generated.js +9 -0
- package/kits/physics-3d/main.jsx +1 -1
- package/kits/physics-3d/package-lock.json +85 -2
- package/kits/physics-3d/package.json +6 -2
- package/kits/physics-3d/scenes/main.scene +1 -1
- package/kits/physics-3d/scenes/model-lab.scene +1 -1
- package/kits/real-time/.prettierrc +5 -0
- package/kits/real-time/CLAUDE.md +60 -0
- package/kits/real-time/castle.json +19 -0
- package/kits/real-time/code/client/clock.js +68 -0
- package/kits/real-time/code/client/connection.js +134 -0
- package/kits/real-time/code/client/gameHooks.js +50 -0
- package/kits/real-time/code/client/interpolate.js +82 -0
- package/kits/real-time/code/client/messages.js +55 -0
- package/kits/real-time/code/client/ownership.js +94 -0
- package/kits/real-time/code/client/smooth.js +54 -0
- package/kits/real-time/code/server/blockDelta.js +48 -0
- package/kits/real-time/code/server/gameHooks.js +47 -0
- package/kits/real-time/code/server/limits.js +35 -0
- package/kits/real-time/code/server/session.js +251 -0
- package/kits/real-time/eslint.config.js +22 -0
- package/kits/real-time/index.html +45 -0
- package/kits/real-time/package.json +14 -0
- package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
- package/kits/turn-based/blueprints/board.scene +15 -0
- package/kits/turn-based/blueprints/square.scene +25 -0
- package/kits/turn-based/castle.json +37 -0
- package/kits/turn-based/code/behaviors/Board.jsx +169 -0
- package/kits/turn-based/code/behaviors/Square.jsx +26 -0
- package/kits/turn-based/code/server/index.js +152 -0
- package/kits/turn-based/drawings/square.sprite +25 -0
- package/kits/turn-based/eslint.config.js +12 -0
- package/kits/turn-based/fonts.generated.js +9 -0
- package/kits/{tabletop → turn-based}/index.html +5 -3
- package/kits/{tabletop → turn-based}/main.jsx +2 -2
- package/kits/{tabletop → turn-based}/package-lock.json +17 -4
- package/kits/{tabletop → turn-based}/package.json +5 -2
- package/kits/turn-based/room.js +289 -0
- package/kits/turn-based/scenes/main.scene +227 -0
- package/kits/{tabletop → turn-based}/testing.js +131 -99
- package/kits/turn-based/theme.style +3 -0
- package/package.json +1 -1
- package/kits/blank/CLAUDE.md +0 -198
- package/kits/tabletop/castle.json +0 -34
- package/kits/tabletop/room.js +0 -269
- /package/kits/{blank → base}/index.html +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
- /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
- /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
- /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
- /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
- /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
- /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
- /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Server-side ownership of loose scene objects. Claims and releases change body
|
|
2
|
+
// types, owner reports place granted objects, and timeout checks return abandoned
|
|
3
|
+
// objects to server simulation.
|
|
4
|
+
|
|
5
|
+
import RAPIER from '@dimforge/rapier3d-compat';
|
|
6
|
+
import { number } from '@imports/castle.real-time/code/server/limits.js';
|
|
7
|
+
import * as Limits from './limits.js';
|
|
8
|
+
|
|
9
|
+
// Grant requested objects that are claimable, within reach, and available for
|
|
10
|
+
// this player to take. The shared server grant prevents clients from retaining
|
|
11
|
+
// contradictory owners for the same object.
|
|
12
|
+
export function claim(sim, playerId, ids, handovers) {
|
|
13
|
+
const player = sim.players.get(playerId);
|
|
14
|
+
if (!player || !Array.isArray(ids)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Limit one message to eight claim checks to bound per-message server work.
|
|
19
|
+
for (const id of ids.slice(0, 8)) {
|
|
20
|
+
const entry = sim.objects.get(id);
|
|
21
|
+
if (!entry?.claimable || entry.owner === playerId) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const at = entry.body.translation();
|
|
25
|
+
if (!Limits.withinReach(player.at, at)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (entry.owner && !mayTakeOver(sim, entry, player, at)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
handOver(sim, entry, id, playerId, handovers);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Return listed objects currently owned by `playerId` to server simulation.
|
|
36
|
+
// Owner matching prevents one client from releasing another client's object.
|
|
37
|
+
export function release(sim, playerId, ids, handovers) {
|
|
38
|
+
if (!Array.isArray(ids)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (const id of ids) {
|
|
42
|
+
const entry = sim.objects.get(id);
|
|
43
|
+
if (entry?.owner === playerId) {
|
|
44
|
+
handOver(sim, entry, id, null, handovers);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Report whether a claimant may take an object from its current owner.
|
|
50
|
+
function mayTakeOver(sim, entry, claimant, at) {
|
|
51
|
+
// A timed-out owner no longer retains priority.
|
|
52
|
+
if (sim.timeMs - entry.reportedAt >= Limits.OWNER_TIMEOUT_MS) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Preserve every new grant for at least the minimum hold interval.
|
|
57
|
+
if (sim.timeMs - entry.claimedAt < Limits.HOLD_MS) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const owner = sim.players.get(entry.owner);
|
|
61
|
+
if (!owner) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// After the hold interval, grant priority to the closer player's ground pose.
|
|
66
|
+
return Limits.flatTo(claimant.at, at) < Limits.flatTo(owner.at, at);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Transfer one object to `playerId`, or to server simulation when it is null.
|
|
70
|
+
// Append the transfer to `handovers` for the next state broadcast.
|
|
71
|
+
function handOver(sim, entry, id, playerId, handovers) {
|
|
72
|
+
entry.owner = playerId;
|
|
73
|
+
entry.reportedAt = sim.timeMs;
|
|
74
|
+
entry.claimedAt = sim.timeMs;
|
|
75
|
+
if (playerId) {
|
|
76
|
+
// An owned object is kinematic-position-based, so owner reports place it and
|
|
77
|
+
// its collider remains active in the shared Rapier world.
|
|
78
|
+
entry.body.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased, true);
|
|
79
|
+
} else {
|
|
80
|
+
// A returned object becomes dynamic and starts from rest. This avoids
|
|
81
|
+
// retaining velocity that existed only in the previous owner's local world.
|
|
82
|
+
entry.body.setBodyType(RAPIER.RigidBodyType.Dynamic, true);
|
|
83
|
+
entry.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
|
|
84
|
+
entry.body.setAngvel({ x: 0, y: 0, z: 0 }, true);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Wire handovers encode zero as server ownership and a player id as a grant.
|
|
88
|
+
// An optional list permits deck hooks to reuse this helper outside a broadcast.
|
|
89
|
+
handovers?.push([id, playerId ?? 0]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Apply reported poses to objects granted to `playerId`. Reports for unowned or
|
|
93
|
+
// differently owned ids are ignored.
|
|
94
|
+
export function setObjectPoses(sim, playerId, poses, handovers) {
|
|
95
|
+
if (!Array.isArray(poses)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Limit one message to sixteen pose checks to bound per-message server work.
|
|
100
|
+
for (const pose of poses.slice(0, 16)) {
|
|
101
|
+
const [id, x, y, z, qx, qy, qz, qw] = pose;
|
|
102
|
+
const entry = sim.objects.get(id);
|
|
103
|
+
if (entry?.owner !== playerId) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const asked = Limits.readPoint([x, y, z]);
|
|
107
|
+
if (!asked) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const at = entry.body.translation();
|
|
111
|
+
const elapsed = Math.max(0, sim.timeMs - entry.reportedAt) / 1000;
|
|
112
|
+
entry.reportedAt = sim.timeMs;
|
|
113
|
+
|
|
114
|
+
// A report beyond the object speed allowance releases the object to server
|
|
115
|
+
// simulation. Player reports use clamping instead because each client must
|
|
116
|
+
// continue controlling its own player (`players.js`).
|
|
117
|
+
if (
|
|
118
|
+
Math.hypot(asked.x - at.x, asked.y - at.y, asked.z - at.z) >
|
|
119
|
+
Limits.MAX_OBJECT_SPEED * elapsed + Limits.MOVE_SLACK
|
|
120
|
+
) {
|
|
121
|
+
handOver(sim, entry, id, null, handovers);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
entry.body.setNextKinematicTranslation(asked);
|
|
125
|
+
entry.body.setNextKinematicRotation({
|
|
126
|
+
x: number(qx),
|
|
127
|
+
y: number(qy),
|
|
128
|
+
z: number(qz),
|
|
129
|
+
w: number(qw),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Release objects whose owner disconnected or exceeded the report timeout. The
|
|
135
|
+
// session server calls this every tick before processing new grants.
|
|
136
|
+
export function sweepOwners(sim, handovers) {
|
|
137
|
+
for (const [id, entry] of sim.objects) {
|
|
138
|
+
if (!entry.owner) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const gone = !sim.players.has(entry.owner);
|
|
142
|
+
if (gone || sim.timeMs - entry.reportedAt > Limits.OWNER_TIMEOUT_MS) {
|
|
143
|
+
handOver(sim, entry, id, null, handovers);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Return `[objectId, playerId]` entries for a joiner's complete WORLD snapshot.
|
|
149
|
+
export function ownerships(sim) {
|
|
150
|
+
const out = [];
|
|
151
|
+
for (const [id, entry] of sim.objects) {
|
|
152
|
+
if (entry.owner) {
|
|
153
|
+
out.push([id, entry.owner]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Server-side Rapier capsules for connected players. Client reports update each
|
|
2
|
+
// target position, validation limits excessive movement, and snapshots expose
|
|
3
|
+
// the resulting shared-world pose.
|
|
4
|
+
|
|
5
|
+
import { packPlayer } from '../client/poses.js';
|
|
6
|
+
import { earned, number } from '@imports/castle.real-time/code/server/limits.js';
|
|
7
|
+
import * as Limits from './limits.js';
|
|
8
|
+
import { createBody } from './world.js';
|
|
9
|
+
|
|
10
|
+
// An entry in `sim.players`:
|
|
11
|
+
//
|
|
12
|
+
// body the kinematic capsule in the rapier world
|
|
13
|
+
// at the last position accepted, which the speed check is against
|
|
14
|
+
// facing radians, as the client sent them
|
|
15
|
+
// clamped true only on the tick a move was refused
|
|
16
|
+
// movedAt simulation time of that acceptance
|
|
17
|
+
function newPlayer(body, at) {
|
|
18
|
+
return { body, at, facing: 0, clamped: false, movedAt: 0 };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Add a kinematic capsule for a session player. `world.step` places it at
|
|
22
|
+
// `player.at` before physics; zero friction prevents a capsule leaning against a
|
|
23
|
+
// wall from remaining attached to it.
|
|
24
|
+
export function addPlayer(sim, playerId) {
|
|
25
|
+
// The shape comes off the scene's own player actor, so the server collides
|
|
26
|
+
// with the same body the browser draws. The default is a person-sized capsule.
|
|
27
|
+
const shape = sim.template?.shape ?? { kind: 'capsule', radius: 0.35, height: 1.4 };
|
|
28
|
+
const at = spawnPoint(sim);
|
|
29
|
+
const body = createBody(
|
|
30
|
+
sim,
|
|
31
|
+
{ components: { Solid: {} } },
|
|
32
|
+
{ ...at, rotationX: 0, rotationY: 0, rotationZ: 0 },
|
|
33
|
+
shape,
|
|
34
|
+
{ type: 'kinematic', friction: 0 },
|
|
35
|
+
);
|
|
36
|
+
sim.players.set(playerId, newPlayer(body, at));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Remove a departing player's Rapier body and simulation entry.
|
|
40
|
+
export function removePlayer(sim, playerId) {
|
|
41
|
+
const player = sim.players.get(playerId);
|
|
42
|
+
if (!player) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
sim.world.removeRigidBody(player.body);
|
|
46
|
+
sim.players.delete(playerId);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Validate and retain the pose reported by one authenticated player connection.
|
|
50
|
+
// The session supplies `playerId`; the message does not select its own identity.
|
|
51
|
+
export function setPose(sim, playerId, message) {
|
|
52
|
+
const player = sim.players.get(playerId);
|
|
53
|
+
if (!player) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Accept only finite positions within the shared world bounds.
|
|
58
|
+
const asked = Limits.readPoint(message?.p);
|
|
59
|
+
if (!asked) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Facing rides in the report's pose, after the three position values. It
|
|
64
|
+
// changes the rendered model orientation and has no effect on the upright
|
|
65
|
+
// capsule's movement or collision shape.
|
|
66
|
+
player.facing = number(message?.p?.[3]);
|
|
67
|
+
|
|
68
|
+
// Combine elapsed-time movement allowance with fixed delivery slack.
|
|
69
|
+
const reach = Limits.MAX_SPEED * earned(sim, player.movedAt) + Limits.MOVE_SLACK;
|
|
70
|
+
player.movedAt = sim.timeMs;
|
|
71
|
+
const step = {
|
|
72
|
+
x: asked.x - player.at.x,
|
|
73
|
+
y: asked.y - player.at.y,
|
|
74
|
+
z: asked.z - player.at.z,
|
|
75
|
+
};
|
|
76
|
+
const distance = Math.hypot(step.x, step.y, step.z);
|
|
77
|
+
if (distance <= reach) {
|
|
78
|
+
player.at = asked;
|
|
79
|
+
player.clamped = false;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Reduce an excessive move to its allowed distance and mark the resulting
|
|
84
|
+
// snapshot as clamped. The flag directs the reporting client to apply this
|
|
85
|
+
// server position instead of continuing from its rejected local pose.
|
|
86
|
+
const scale = reach / distance;
|
|
87
|
+
player.at = {
|
|
88
|
+
x: player.at.x + step.x * scale,
|
|
89
|
+
y: player.at.y + step.y * scale,
|
|
90
|
+
z: player.at.z + step.z * scale,
|
|
91
|
+
};
|
|
92
|
+
player.clamped = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Pack current Rapier body poses for a WORLD or STATE message. Physics contacts
|
|
96
|
+
// can make the body position differ from the last accepted target in `player.at`.
|
|
97
|
+
export function playerPoses(sim) {
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const [id, player] of sim.players) {
|
|
100
|
+
out.push([id, ...packPlayer(player.body.translation(), player.facing, player.clamped)]);
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Return a distinct point around the authored spawn for each current player.
|
|
106
|
+
// The angular spacing prevents simultaneous joins from overlapping capsules.
|
|
107
|
+
function spawnPoint(sim) {
|
|
108
|
+
const angle = sim.players.size * 1.7;
|
|
109
|
+
return {
|
|
110
|
+
x: sim.spawn.x + Math.cos(angle) * Limits.SPAWN_RING,
|
|
111
|
+
y: sim.spawn.y + 0.2,
|
|
112
|
+
z: sim.spawn.z + Math.sin(angle) * Limits.SPAWN_RING,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Server-side reading of the deck's authored scene and blueprints. It extracts
|
|
2
|
+
// component data needed to construct Rapier bodies and resolves blueprint
|
|
3
|
+
// defaults in the same order as the engine.
|
|
4
|
+
|
|
5
|
+
import { componentDefaults } from '@imports/castle.real-time/code/server/gameHooks.js';
|
|
6
|
+
|
|
7
|
+
// Root-anchored globs select files from the importing deck. Eager raw imports
|
|
8
|
+
// embed the text in the server bundle, whose runtime has no deck filesystem.
|
|
9
|
+
// Globbing also permits this kit's own package to omit a main scene.
|
|
10
|
+
const sceneTexts = import.meta.glob('/scenes/main.scene', {
|
|
11
|
+
eager: true,
|
|
12
|
+
query: '?raw',
|
|
13
|
+
import: 'default',
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Include direct deck blueprints and first-level imported blueprints because
|
|
17
|
+
// authored scene instances can refer to either location.
|
|
18
|
+
const blueprintTexts = import.meta.glob(['/blueprints/*.scene', '/imports/*/blueprints/*.scene'], {
|
|
19
|
+
eager: true,
|
|
20
|
+
query: '?raw',
|
|
21
|
+
import: 'default',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Define the properties needed by the server's physics representation.
|
|
25
|
+
// `Transform`, `Shape`, `Body`, and `Door` are engine behaviors; the server
|
|
26
|
+
// process reads their authored data without loading the renderer or behaviors.
|
|
27
|
+
//
|
|
28
|
+
// A deck's own server-side components go in `code/server/deckComponents.js` and
|
|
29
|
+
// are merged over these. Components absent from this table or the deck table
|
|
30
|
+
// retain authored instance values but receive no authored behavior defaults.
|
|
31
|
+
const DEFAULTS = {
|
|
32
|
+
Transform: { x: 0, y: 0, z: 0, rotationX: 0, rotationY: 0, rotationZ: 0 },
|
|
33
|
+
Shape: { kind: 'box', width: 1, height: 1, depth: 1, radius: 0.5, solid: false },
|
|
34
|
+
Body: { type: 'fixed', friction: 0.6, restitution: 0, gravityScale: 1, lockRotations: false },
|
|
35
|
+
Door: { range: 3, slide: 2.1, speed: 2.5 },
|
|
36
|
+
|
|
37
|
+
// `Ball` marks a fast-moving actor. `world.js` enables continuous collision
|
|
38
|
+
// detection and excludes it from the server-side shove-speed cap.
|
|
39
|
+
Ball: {},
|
|
40
|
+
...componentDefaults,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Parse the deck's main scene and return actor ids with resolved components.
|
|
44
|
+
// `world.createSimulation` consumes this reduced representation at startup.
|
|
45
|
+
export function readScene() {
|
|
46
|
+
const text = sceneTexts['/scenes/main.scene'];
|
|
47
|
+
|
|
48
|
+
// An empty actor list lets this kit bundle independently without a deck scene.
|
|
49
|
+
if (!text) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
const scene = JSON.parse(text);
|
|
53
|
+
|
|
54
|
+
// Preserve authored actor ids because ownership and object-pose messages use
|
|
55
|
+
// the same ids to address the corresponding client scene actors.
|
|
56
|
+
return (scene.actors ?? []).map((actor) => ({
|
|
57
|
+
id: actor.id,
|
|
58
|
+
components: resolveComponents(actor),
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Shallow-merge each component in engine precedence order: defaults, blueprint,
|
|
63
|
+
// then scene instance values.
|
|
64
|
+
function resolveComponents(actor) {
|
|
65
|
+
const template = blueprintOf(actor.blueprint);
|
|
66
|
+
const merged = {};
|
|
67
|
+
|
|
68
|
+
// Iterate the union so components authored only on the instance and components
|
|
69
|
+
// supplied only by the blueprint both reach the server representation.
|
|
70
|
+
for (const name of new Set([...Object.keys(template), ...Object.keys(actor.components ?? {})])) {
|
|
71
|
+
merged[name] = { ...DEFAULTS[name], ...template[name], ...actor.components?.[name] };
|
|
72
|
+
}
|
|
73
|
+
return merged;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Resolve the first actor of a referenced blueprint. Scene refs use deck-visible
|
|
77
|
+
// paths such as `imports/castle.physics-3d/blueprints/crate.scene`; matching the
|
|
78
|
+
// glob key suffix handles both deck and imported blueprint locations.
|
|
79
|
+
function blueprintOf(ref) {
|
|
80
|
+
// Actors without a blueprint use only component defaults and instance values.
|
|
81
|
+
if (!ref) {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
const key = Object.keys(blueprintTexts).find((path) => path.endsWith(`/${ref}`));
|
|
85
|
+
|
|
86
|
+
// Engine blueprints use their first actor as the component template.
|
|
87
|
+
return key ? (JSON.parse(blueprintTexts[key])?.actors?.[0]?.components ?? {}) : {};
|
|
88
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// Shared Rapier world built from the deck's scene. This module creates bodies,
|
|
2
|
+
// advances fixed physics steps, applies server-driven world rules, and packs
|
|
3
|
+
// current object poses for network snapshots.
|
|
4
|
+
|
|
5
|
+
import RAPIER from '@dimforge/rapier3d-compat';
|
|
6
|
+
import { packPose } from '../client/poses.js';
|
|
7
|
+
import { MAX_STEPS_PER_TICK, STEP, clamp } from '@imports/castle.real-time/code/server/limits.js';
|
|
8
|
+
import { readScene } from './sceneBodies.js';
|
|
9
|
+
import * as Limits from './limits.js';
|
|
10
|
+
|
|
11
|
+
const DEG = Math.PI / 180;
|
|
12
|
+
|
|
13
|
+
// The simulation every hook is handed as `sim`:
|
|
14
|
+
//
|
|
15
|
+
// world the rapier world
|
|
16
|
+
// objects actor id -> { body, home, claimable, fast, owner, reportedAt,
|
|
17
|
+
// claimedAt }
|
|
18
|
+
// doors [{ body, props, baseY, openT }]
|
|
19
|
+
// players playerId -> { body, at, facing, clamped, movedAt }
|
|
20
|
+
// spawn where players come in
|
|
21
|
+
// template the scene's own player actor, for its shape
|
|
22
|
+
// acc seconds not yet turned into whole physics steps
|
|
23
|
+
// timeMs milliseconds of simulation, not of wall clock. It advances by
|
|
24
|
+
// whole physics steps, so the client can space samples by it
|
|
25
|
+
// exactly.
|
|
26
|
+
function emptySim() {
|
|
27
|
+
return {
|
|
28
|
+
world: new RAPIER.World({ x: 0, y: -9.81, z: 0 }),
|
|
29
|
+
objects: new Map(),
|
|
30
|
+
doors: [],
|
|
31
|
+
players: new Map(),
|
|
32
|
+
spawn: { x: 0, y: 1, z: 0 },
|
|
33
|
+
template: null,
|
|
34
|
+
acc: 0,
|
|
35
|
+
timeMs: 0,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Initialize Rapier and return the complete simulation consumed by server hooks.
|
|
40
|
+
// Scene bodies and the server-added ground exist before the promise resolves.
|
|
41
|
+
export async function createSimulation() {
|
|
42
|
+
await RAPIER.init();
|
|
43
|
+
const sim = emptySim();
|
|
44
|
+
buildGround(sim);
|
|
45
|
+
buildScene(sim);
|
|
46
|
+
return sim;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Add the server's 200 m square ground slab below the authored scene.
|
|
50
|
+
// `Limits.FELL_OUT_Y` handles dynamic bodies that fall beyond its edge.
|
|
51
|
+
function buildGround(sim) {
|
|
52
|
+
const ground = sim.world.createRigidBody(RAPIER.RigidBodyDesc.fixed().setTranslation(0, -0.5, 0));
|
|
53
|
+
sim.world.createCollider(RAPIER.ColliderDesc.cuboid(100, 0.5, 100), ground);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Create Rapier bodies and indexes from the reduced scene representation.
|
|
57
|
+
function buildScene(sim) {
|
|
58
|
+
for (const actor of readScene()) {
|
|
59
|
+
const {
|
|
60
|
+
Transform: t,
|
|
61
|
+
Shape: shape,
|
|
62
|
+
Body: body,
|
|
63
|
+
Player: player,
|
|
64
|
+
Door: door,
|
|
65
|
+
Ball: ball,
|
|
66
|
+
} = actor.components;
|
|
67
|
+
if (!t || !shape) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Use the authored Player actor as the spawn and body-shape template.
|
|
72
|
+
// Connected-player capsules replace this single authored instance.
|
|
73
|
+
if (player) {
|
|
74
|
+
sim.spawn = { x: t.x, y: t.y, z: t.z };
|
|
75
|
+
sim.template = { shape, body };
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const rb = createBody(sim, actor, t, shape, body);
|
|
79
|
+
if (door) {
|
|
80
|
+
sim.doors.push({ body: rb, props: door, baseY: t.y, openT: 0 });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Index dynamic and kinematic actors because their poses can change and
|
|
84
|
+
// therefore require network snapshots. Fixed bodies remain only in Rapier.
|
|
85
|
+
if (body?.type === 'dynamic' || body?.type === 'kinematic') {
|
|
86
|
+
sim.objects.set(actor.id, {
|
|
87
|
+
body: rb,
|
|
88
|
+
home: { x: t.x, y: t.y, z: t.z },
|
|
89
|
+
|
|
90
|
+
// Server-driven kinematic doors remain server-owned. Other dynamic
|
|
91
|
+
// bodies, including Ball actors, can transfer to client simulation.
|
|
92
|
+
claimable: body.type === 'dynamic',
|
|
93
|
+
|
|
94
|
+
// `Ball` marks a fast-moving body. Post-step speed limiting excludes it,
|
|
95
|
+
// and `createBody` enables continuous collision detection.
|
|
96
|
+
fast: Boolean(ball),
|
|
97
|
+
owner: null,
|
|
98
|
+
reportedAt: 0,
|
|
99
|
+
claimedAt: 0,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Create one Rapier body and collider from engine component data. These mappings
|
|
106
|
+
// mirror the engine's `bodyDescFor` and `colliderDescFor`, so server and browser
|
|
107
|
+
// worlds use the same body type, material values, shape, and sensor state.
|
|
108
|
+
export function createBody(sim, actor, t, shape, body) {
|
|
109
|
+
const desc = descFor(body?.type).setTranslation(t.x, t.y, t.z).setRotation(quatFromEuler(t));
|
|
110
|
+
if (body?.gravityScale !== undefined) {
|
|
111
|
+
desc.setGravityScale(body.gravityScale);
|
|
112
|
+
}
|
|
113
|
+
if (body?.lockRotations) {
|
|
114
|
+
desc.lockRotations();
|
|
115
|
+
}
|
|
116
|
+
const rb = sim.world.createRigidBody(desc);
|
|
117
|
+
const collider = colliderFor(shape);
|
|
118
|
+
collider.setFriction(body?.friction ?? 0.6);
|
|
119
|
+
collider.setRestitution(body?.restitution ?? 0);
|
|
120
|
+
if (!isSolid(actor)) {
|
|
121
|
+
collider.setSensor(true);
|
|
122
|
+
}
|
|
123
|
+
sim.world.createCollider(collider, rb);
|
|
124
|
+
|
|
125
|
+
// Continuous collision detection prevents a Ball moving metres per step from
|
|
126
|
+
// crossing a wall between discrete physics positions.
|
|
127
|
+
if (actor.components.Ball) {
|
|
128
|
+
rb.enableCcd(true);
|
|
129
|
+
}
|
|
130
|
+
return rb;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Map the engine Body type string to its Rapier rigid-body descriptor.
|
|
134
|
+
function descFor(type) {
|
|
135
|
+
if (type === 'dynamic') {
|
|
136
|
+
return RAPIER.RigidBodyDesc.dynamic();
|
|
137
|
+
}
|
|
138
|
+
if (type === 'kinematic') {
|
|
139
|
+
return RAPIER.RigidBodyDesc.kinematicPositionBased();
|
|
140
|
+
}
|
|
141
|
+
return RAPIER.RigidBodyDesc.fixed();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Map engine Shape properties to the matching Rapier collider descriptor.
|
|
145
|
+
function colliderFor(shape) {
|
|
146
|
+
if (shape.kind === 'sphere') {
|
|
147
|
+
return RAPIER.ColliderDesc.ball(shape.radius);
|
|
148
|
+
}
|
|
149
|
+
if (shape.kind === 'capsule') {
|
|
150
|
+
return RAPIER.ColliderDesc.capsule(
|
|
151
|
+
Math.max(0.01, shape.height / 2 - shape.radius),
|
|
152
|
+
shape.radius,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (shape.kind === 'cylinder') {
|
|
156
|
+
return RAPIER.ColliderDesc.cylinder(shape.height / 2, shape.radius);
|
|
157
|
+
}
|
|
158
|
+
return RAPIER.ColliderDesc.cuboid(shape.width / 2, shape.height / 2, shape.depth / 2);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Engine actors are solid through either a Solid component or `Shape.solid`.
|
|
162
|
+
function isSolid(actor) {
|
|
163
|
+
return Boolean(actor.components.Solid) || Boolean(actor.components.Shape?.solid);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
//
|
|
167
|
+
// Stepping
|
|
168
|
+
//
|
|
169
|
+
|
|
170
|
+
// Advance as many whole fixed steps as the tick's elapsed time permits, up to
|
|
171
|
+
// the catch-up limit. `timeMs` advances in exact step units used by client pose
|
|
172
|
+
// interpolation.
|
|
173
|
+
export function step(sim, elapsedSeconds) {
|
|
174
|
+
sim.acc += elapsedSeconds;
|
|
175
|
+
let steps = 0;
|
|
176
|
+
while (sim.acc >= STEP && steps < MAX_STEPS_PER_TICK) {
|
|
177
|
+
// Apply each accepted client target through Rapier's deferred kinematic
|
|
178
|
+
// position before the physics step reads contacts.
|
|
179
|
+
for (const player of sim.players.values()) {
|
|
180
|
+
player.body.setNextKinematicTranslation(player.at);
|
|
181
|
+
}
|
|
182
|
+
for (const door of sim.doors) {
|
|
183
|
+
driveDoor(sim, door, STEP);
|
|
184
|
+
}
|
|
185
|
+
sim.world.timestep = STEP;
|
|
186
|
+
sim.world.step();
|
|
187
|
+
keepObjectsSane(sim);
|
|
188
|
+
sim.timeMs += STEP * 1000;
|
|
189
|
+
sim.acc -= STEP;
|
|
190
|
+
steps += 1;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Skip physics beyond the catch-up limit while advancing the simulation clock
|
|
194
|
+
// over those steps. A backlog dropped without advancing the clock leaves
|
|
195
|
+
// `timeMs` behind after a host stall, and clients then play network motion slowly.
|
|
196
|
+
if (steps === MAX_STEPS_PER_TICK) {
|
|
197
|
+
// Floating-point division can represent 56 steps as 55.99999. The epsilon
|
|
198
|
+
// preserves the complete skipped-step count and keeps the clock current.
|
|
199
|
+
const skipped = Math.floor(sim.acc / STEP + 1e-9);
|
|
200
|
+
sim.timeMs += skipped * STEP * 1000;
|
|
201
|
+
sim.acc -= skipped * STEP;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Apply post-step corrections to server-simulated dynamic objects. Each object
|
|
206
|
+
// may need waking near a player, restoration after falling, or velocity limiting
|
|
207
|
+
// after contact with a kinematic capsule.
|
|
208
|
+
function keepObjectsSane(sim) {
|
|
209
|
+
for (const entry of sim.objects.values()) {
|
|
210
|
+
if (entry.owner || entry.body.bodyType() !== RAPIER.RigidBodyType.Dynamic) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const at = entry.body.translation();
|
|
214
|
+
|
|
215
|
+
// Rapier can leave a sleeping dynamic body asleep when a positioned
|
|
216
|
+
// kinematic capsule contacts it. Wake nearby bodies so player contact can
|
|
217
|
+
// produce a push.
|
|
218
|
+
if (nearAnyPlayer(sim, at)) {
|
|
219
|
+
entry.body.wakeUp();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Restore a body below the ground limit to its authored position at rest.
|
|
223
|
+
if (at.y < Limits.FELL_OUT_Y) {
|
|
224
|
+
entry.body.setTranslation(entry.home, true);
|
|
225
|
+
entry.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
|
|
226
|
+
entry.body.setAngvel({ x: 0, y: 0, z: 0 }, true);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (entry.fast) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Limit velocities imparted by server-side player capsules; the constant's
|
|
234
|
+
// declaration documents why the cap remains below walking speed.
|
|
235
|
+
const v = entry.body.linvel();
|
|
236
|
+
const speed = Math.hypot(v.x, v.y, v.z);
|
|
237
|
+
if (speed > Limits.MAX_PUSHED_SPEED) {
|
|
238
|
+
const scale = Limits.MAX_PUSHED_SPEED / speed;
|
|
239
|
+
entry.body.setLinvel({ x: v.x * scale, y: v.y * scale, z: v.z * scale }, true);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Return whether any accepted player position lies within claim distance.
|
|
245
|
+
function nearAnyPlayer(sim, at) {
|
|
246
|
+
for (const player of sim.players.values()) {
|
|
247
|
+
if (Limits.flatTo(player.at, at) < Limits.CLAIM_RADIUS) {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Move a server-owned Door toward its open or closed position based on nearby
|
|
255
|
+
// player capsules. The resulting shared pose is broadcast to every client.
|
|
256
|
+
function driveDoor(sim, door, dt) {
|
|
257
|
+
const at = door.body.translation();
|
|
258
|
+
let near = false;
|
|
259
|
+
for (const player of sim.players.values()) {
|
|
260
|
+
const p = player.body.translation();
|
|
261
|
+
if (Math.hypot(p.x - at.x, p.z - at.z) < door.props.range) {
|
|
262
|
+
near = true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Advance the normalized door state at a fixed rate, then apply smoothstep to
|
|
267
|
+
// the final vertical translation.
|
|
268
|
+
const delta = Math.sign((near ? 1 : 0) - door.openT) * door.props.speed * dt;
|
|
269
|
+
door.openT = clamp(door.openT + delta, 0, 1);
|
|
270
|
+
const eased = door.openT * door.openT * (3 - 2 * door.openT);
|
|
271
|
+
door.body.setNextKinematicTranslation({
|
|
272
|
+
x: at.x,
|
|
273
|
+
y: door.baseY + eased * door.props.slide,
|
|
274
|
+
z: at.z,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//
|
|
279
|
+
// Reading it out
|
|
280
|
+
//
|
|
281
|
+
|
|
282
|
+
// Pack every indexed dynamic or kinematic object's current Rapier pose.
|
|
283
|
+
// `server/index.js` filters unchanged entries before broadcasting a STATE delta.
|
|
284
|
+
export function objectPoses(sim) {
|
|
285
|
+
const out = [];
|
|
286
|
+
for (const [id, entry] of sim.objects) {
|
|
287
|
+
out.push([id, ...packPose(entry.body.translation(), entry.body.rotation())]);
|
|
288
|
+
}
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Convert the engine Transform's degree Euler angles to a Rapier quaternion.
|
|
293
|
+
function quatFromEuler(t) {
|
|
294
|
+
const cx = Math.cos((t.rotationX ?? 0) * DEG * 0.5);
|
|
295
|
+
const sx = Math.sin((t.rotationX ?? 0) * DEG * 0.5);
|
|
296
|
+
const cy = Math.cos((t.rotationY ?? 0) * DEG * 0.5);
|
|
297
|
+
const sy = Math.sin((t.rotationY ?? 0) * DEG * 0.5);
|
|
298
|
+
const cz = Math.cos((t.rotationZ ?? 0) * DEG * 0.5);
|
|
299
|
+
const sz = Math.sin((t.rotationZ ?? 0) * DEG * 0.5);
|
|
300
|
+
return {
|
|
301
|
+
x: sx * cy * cz + cx * sy * sz,
|
|
302
|
+
y: cx * sy * cz - sx * cy * sz,
|
|
303
|
+
z: cx * cy * sz + sx * sy * cz,
|
|
304
|
+
w: cx * cy * cz - sx * sy * sz,
|
|
305
|
+
};
|
|
306
|
+
}
|