castle-web-cli 0.4.157 → 0.4.159
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-DMSoTyXh.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 +93 -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,26 @@
|
|
|
1
|
+
// This is the entry point for one Castle multiplayer session process. It binds
|
|
2
|
+
// the shared real-time session loop to the 2D pose, Matter world, player, and
|
|
3
|
+
// ownership modules declared below. multiplayer-3d has the same entry shape
|
|
4
|
+
// with Rapier modules in place of the Matter ones.
|
|
5
|
+
|
|
6
|
+
import { makeSession } from '@imports/castle.real-time/code/server/session.js';
|
|
7
|
+
import { game } from '@imports/castle.real-time/code/server/gameHooks.js';
|
|
8
|
+
import * as Poses from '../client/poses.js';
|
|
9
|
+
import * as Ownership from './ownership.js';
|
|
10
|
+
import * as Players from './players.js';
|
|
11
|
+
import * as World from './world.js';
|
|
12
|
+
|
|
13
|
+
// Castle loads this object from `castle.json.server.main`. makeSession supplies
|
|
14
|
+
// message routing, fixed ticks, delta broadcasts, and the connection handlers.
|
|
15
|
+
//
|
|
16
|
+
// Deck-specific server behavior belongs in `code/server/game.js`; gameHooks
|
|
17
|
+
// discovers that root-anchored module and exposes its optional hooks here.
|
|
18
|
+
export default makeSession({
|
|
19
|
+
// The shared core calls these adapters without inspecting their 2D values.
|
|
20
|
+
label: 'multiplayer',
|
|
21
|
+
poses: Poses,
|
|
22
|
+
world: World,
|
|
23
|
+
players: Players,
|
|
24
|
+
ownership: Ownership,
|
|
25
|
+
game,
|
|
26
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// This module defines the 2D session's validation and physics limits in card
|
|
2
|
+
// pixels. Constants are grouped by player reports, ownership, and server-side
|
|
3
|
+
// corrections; reusable point tests follow them.
|
|
4
|
+
|
|
5
|
+
import { number } from '@imports/castle.real-time/code/server/limits.js';
|
|
6
|
+
|
|
7
|
+
//
|
|
8
|
+
// Player reports
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
// Step rate, catch-up, and elapsed-time accounting remain in castle.real-time
|
|
12
|
+
// because those values do not depend on the 2D card coordinate system.
|
|
13
|
+
|
|
14
|
+
// A character walks at 4 pixels per step, which is 240 a second. The limit is
|
|
15
|
+
// well above that so ordinary play never touches it; a report past this rate is
|
|
16
|
+
// clamped by `players.setPose`.
|
|
17
|
+
export const MAX_SPEED = 420;
|
|
18
|
+
|
|
19
|
+
// A fixed allowance on top, for reports that arrive together after a stall.
|
|
20
|
+
export const MOVE_SLACK = 20;
|
|
21
|
+
|
|
22
|
+
// Reports outside this rectangle are refused outright. It is the card with a
|
|
23
|
+
// wide margin, so a body pushed slightly out of the room is still reportable.
|
|
24
|
+
export const BOUNDS = { minX: -200, maxX: 700, minY: -200, maxY: 900 };
|
|
25
|
+
|
|
26
|
+
//
|
|
27
|
+
// Ownership
|
|
28
|
+
//
|
|
29
|
+
|
|
30
|
+
// How close a character has to be to claim an object. It is wider than the body
|
|
31
|
+
// itself so the request completes before contact.
|
|
32
|
+
export const CLAIM_RADIUS = 90;
|
|
33
|
+
|
|
34
|
+
// How long a grant is held before a nearer player can take it. The delay stops
|
|
35
|
+
// two players either side of a crate from trading it every tick.
|
|
36
|
+
export const HOLD_MS = 500;
|
|
37
|
+
|
|
38
|
+
// An object is released after this long with no report from its owner. The value
|
|
39
|
+
// covers the round trip a new owner needs before its first report arrives.
|
|
40
|
+
export const OWNER_TIMEOUT_MS = 1500;
|
|
41
|
+
|
|
42
|
+
// Reports of an owned object moving faster than this are refused. A crate shoved
|
|
43
|
+
// by a character travels a few hundred pixels a second.
|
|
44
|
+
export const MAX_OBJECT_SPEED = 1500;
|
|
45
|
+
|
|
46
|
+
//
|
|
47
|
+
// Server-side corrections
|
|
48
|
+
//
|
|
49
|
+
|
|
50
|
+
// The speed a server-side character body may impart to a crate, in pixels per
|
|
51
|
+
// step. Matter treats a static body as infinite mass, so a character walked into
|
|
52
|
+
// a crate can otherwise send it across the room. Keeping this near walking speed
|
|
53
|
+
// also keeps the crate in claim range while the ownership request completes.
|
|
54
|
+
// The cap applies to server-simulated crates; client reports place owned crates.
|
|
55
|
+
export const MAX_PUSHED_SPEED = 5;
|
|
56
|
+
|
|
57
|
+
// An object outside this rectangle is put back where the scene placed it. Every
|
|
58
|
+
// wall is inside it, so reaching this means the object left through a seam.
|
|
59
|
+
export const ROOM = { minX: -60, maxX: 560, minY: -60, maxY: 760 };
|
|
60
|
+
|
|
61
|
+
// How far apart joining players are placed around the authored spawn.
|
|
62
|
+
export const SPAWN_RING = 34;
|
|
63
|
+
|
|
64
|
+
//
|
|
65
|
+
// Validation helpers
|
|
66
|
+
//
|
|
67
|
+
|
|
68
|
+
// A finite client point inside the world bounds, or null. The player and
|
|
69
|
+
// owned-object report handlers both run a value through this before writing it
|
|
70
|
+
// into the world.
|
|
71
|
+
export function readPoint(point) {
|
|
72
|
+
// Two values or more, so a character report's trailing facing value passes.
|
|
73
|
+
if (!Array.isArray(point) || point.length < 2) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const x = number(point[0]);
|
|
77
|
+
const y = number(point[1]);
|
|
78
|
+
if (x < BOUNDS.minX || x > BOUNDS.maxX || y < BOUNDS.minY || y > BOUNDS.maxY) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
return { x, y };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Return the Euclidean distance between two card-space points.
|
|
85
|
+
export function distanceBetween(from, to) {
|
|
86
|
+
return Math.hypot(from.x - to.x, from.y - to.y);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Return whether a claim target is within the server's ownership radius.
|
|
90
|
+
export function withinReach(from, to) {
|
|
91
|
+
return distanceBetween(from, to) <= CLAIM_RADIUS;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Return whether a server-simulated object's centre remains in the reset area.
|
|
95
|
+
export function inRoom(point) {
|
|
96
|
+
return (
|
|
97
|
+
point.x >= ROOM.minX && point.x <= ROOM.maxX && point.y >= ROOM.minY && point.y <= ROOM.maxY
|
|
98
|
+
);
|
|
99
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// This module manages which client simulates each claimable scene object. It
|
|
2
|
+
// validates claims and pose reports, changes Matter body modes during handover,
|
|
3
|
+
// expires inactive owners, and serializes ownership for snapshots.
|
|
4
|
+
//
|
|
5
|
+
// The client half, real-time's `client/ownership.js`, decides what to ask for;
|
|
6
|
+
// this module decides what is granted and remains the authority. Every change
|
|
7
|
+
// it makes lands in `handovers`, which the session broadcasts the same tick.
|
|
8
|
+
|
|
9
|
+
import Matter from 'matter-js';
|
|
10
|
+
import { number } from '@imports/castle.real-time/code/server/limits.js';
|
|
11
|
+
import * as Limits from './limits.js';
|
|
12
|
+
|
|
13
|
+
// Grant requested objects that are claimable, in reach, and available to this
|
|
14
|
+
// player. Server-side grants provide one authoritative owner for each object.
|
|
15
|
+
export function claim(sim, playerId, ids, handovers) {
|
|
16
|
+
const player = sim.players.get(playerId);
|
|
17
|
+
|
|
18
|
+
// Claims require an active server-side character and a bounded iterable list.
|
|
19
|
+
if (!player || !Array.isArray(ids)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Eight checks per message bound the validation work requested by one client.
|
|
24
|
+
for (const id of ids.slice(0, 8)) {
|
|
25
|
+
const entry = sim.objects.get(id);
|
|
26
|
+
if (!entry?.claimable || entry.owner === playerId) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Reach and takeover comparisons use the object's current Matter centre.
|
|
31
|
+
const at = entry.body.position;
|
|
32
|
+
if (!Limits.withinReach(centreOf(player), at)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (entry.owner && !mayTakeOver(sim, entry, player, at)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The handover list is included in the next state broadcast.
|
|
40
|
+
handOver(sim, entry, id, playerId, handovers);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Return the listed objects owned by this player to server simulation. Matching
|
|
45
|
+
// the current owner prevents one client from releasing another client's object.
|
|
46
|
+
export function release(sim, playerId, ids, handovers) {
|
|
47
|
+
if (!Array.isArray(ids)) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
for (const id of ids) {
|
|
51
|
+
const entry = sim.objects.get(id);
|
|
52
|
+
if (entry?.owner === playerId) {
|
|
53
|
+
handOver(sim, entry, id, null, handovers);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Whether a claimant may take an object from the player holding it.
|
|
59
|
+
function mayTakeOver(sim, entry, claimant, at) {
|
|
60
|
+
// An expired report allows an immediate takeover before the periodic sweep.
|
|
61
|
+
if (sim.timeMs - entry.reportedAt >= Limits.OWNER_TIMEOUT_MS) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// A recent grant remains with its current owner for the minimum hold period.
|
|
66
|
+
if (sim.timeMs - entry.claimedAt < Limits.HOLD_MS) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const owner = sim.players.get(entry.owner);
|
|
70
|
+
if (!owner) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// After the hold period, a strictly nearer claimant may take ownership.
|
|
75
|
+
return (
|
|
76
|
+
Limits.distanceBetween(centreOf(claimant), at) < Limits.distanceBetween(centreOf(owner), at)
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Assign one object to `playerId`, or to server simulation when it is null, and
|
|
81
|
+
// append the change to the current broadcast when a list was supplied.
|
|
82
|
+
function handOver(sim, entry, id, playerId, handovers) {
|
|
83
|
+
// Both timestamps start at the handover tick. reportedAt drives expiration;
|
|
84
|
+
// claimedAt enforces the minimum hold before a proximity-based takeover.
|
|
85
|
+
entry.owner = playerId;
|
|
86
|
+
entry.reportedAt = sim.timeMs;
|
|
87
|
+
entry.claimedAt = sim.timeMs;
|
|
88
|
+
if (playerId) {
|
|
89
|
+
// An owned object is placed from client reports. Static mode prevents server
|
|
90
|
+
// physics from moving it while retaining collision response for other bodies.
|
|
91
|
+
Matter.Body.setStatic(entry.body, true);
|
|
92
|
+
} else {
|
|
93
|
+
// Server simulation resumes in dynamic mode at rest. Client-only velocity
|
|
94
|
+
// is not a valid initial velocity for the server's Matter world.
|
|
95
|
+
Matter.Body.setStatic(entry.body, false);
|
|
96
|
+
Matter.Body.setVelocity(entry.body, { x: 0, y: 0 });
|
|
97
|
+
Matter.Body.setAngularVelocity(entry.body, 0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// On the wire, zero identifies server ownership and a player id identifies a grant.
|
|
101
|
+
// The list is optional so a deck hook can reuse this outside a broadcast.
|
|
102
|
+
handovers?.push([id, playerId ?? 0]);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Apply the poses reported for the objects granted to `playerId`. A report for
|
|
106
|
+
// an object owned by somebody else, or by nobody, is ignored.
|
|
107
|
+
export function setObjectPoses(sim, playerId, poses, handovers) {
|
|
108
|
+
if (!Array.isArray(poses)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Sixteen checks per message bound the validation work requested by one client.
|
|
113
|
+
for (const pose of poses.slice(0, 16)) {
|
|
114
|
+
const [id, x, y, angle] = pose;
|
|
115
|
+
const entry = sim.objects.get(id);
|
|
116
|
+
if (entry?.owner !== playerId) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const asked = Limits.readPoint([x, y]);
|
|
120
|
+
if (!asked) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const to = { x: asked.x + entry.offset.dx, y: asked.y + entry.offset.dy };
|
|
124
|
+
|
|
125
|
+
// reportedAt measures the interval used by this report's speed allowance.
|
|
126
|
+
const elapsed = Math.max(0, sim.timeMs - entry.reportedAt) / 1000;
|
|
127
|
+
entry.reportedAt = sim.timeMs;
|
|
128
|
+
|
|
129
|
+
// A report past the object speed allowance returns the object to server
|
|
130
|
+
// simulation. Character reports use a clamp because the reporting client
|
|
131
|
+
// continues to control that character.
|
|
132
|
+
if (
|
|
133
|
+
Limits.distanceBetween(entry.body.position, to) >
|
|
134
|
+
Limits.MAX_OBJECT_SPEED * elapsed + Limits.MOVE_SLACK
|
|
135
|
+
) {
|
|
136
|
+
handOver(sim, entry, id, null, handovers);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Matter derives velocity from this position change. The derived velocity
|
|
141
|
+
// lets an owned crate transfer an impulse to server-simulated bodies.
|
|
142
|
+
Matter.Body.setPosition(entry.body, to, true);
|
|
143
|
+
Matter.Body.setAngle(entry.body, number(angle));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Release objects whose owner left or stopped reporting. The shared session
|
|
148
|
+
// calls this every tick before it processes new grants.
|
|
149
|
+
export function sweepOwners(sim, handovers) {
|
|
150
|
+
for (const [id, entry] of sim.objects) {
|
|
151
|
+
if (!entry.owner) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const gone = !sim.players.has(entry.owner);
|
|
155
|
+
if (gone || sim.timeMs - entry.reportedAt > Limits.OWNER_TIMEOUT_MS) {
|
|
156
|
+
handOver(sim, entry, id, null, handovers);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Return `[objectId, playerId]` pairs for a joining client's WORLD snapshot.
|
|
162
|
+
export function ownerships(sim) {
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const [id, entry] of sim.objects) {
|
|
165
|
+
if (entry.owner) {
|
|
166
|
+
// Server-owned entries are represented by their absence from this snapshot.
|
|
167
|
+
out.push([id, entry.owner]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Return a player's body centre for the same centre-to-centre reach test used by
|
|
174
|
+
// the client ownership helper.
|
|
175
|
+
function centreOf(player) {
|
|
176
|
+
return player.body.position;
|
|
177
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// This module owns the connected players' character bodies in the shared Matter
|
|
2
|
+
// world. It creates and removes bodies, validates reported positions, and packs
|
|
3
|
+
// authoritative character poses for snapshots.
|
|
4
|
+
|
|
5
|
+
import Matter from 'matter-js';
|
|
6
|
+
import { packPlayer } from '../client/poses.js';
|
|
7
|
+
import { earned, number } from '@imports/castle.real-time/code/server/limits.js';
|
|
8
|
+
import * as Limits from './limits.js';
|
|
9
|
+
import { addBody } from './world.js';
|
|
10
|
+
|
|
11
|
+
// An entry in `sim.players`:
|
|
12
|
+
//
|
|
13
|
+
// body the character's body in the matter world
|
|
14
|
+
// offset top-left of the Layout box to the body's centre
|
|
15
|
+
// at the last top-left position accepted, which the speed check is
|
|
16
|
+
// against
|
|
17
|
+
// facing radians, as the client sent them
|
|
18
|
+
// clamped true only on the tick a move was refused
|
|
19
|
+
// movedAt the simulation time of that acceptance
|
|
20
|
+
function newPlayer(entry, at) {
|
|
21
|
+
// Spread the body's offset data beside the report-validation state.
|
|
22
|
+
return { ...entry, at, facing: 0, clamped: false, movedAt: 0 };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Add a character body for a session player. world.step writes `player.at`
|
|
26
|
+
// before each physics step. The engine represents a kinematic body as a static
|
|
27
|
+
// Matter body moved through explicit position writes.
|
|
28
|
+
export function addPlayer(sim, playerId) {
|
|
29
|
+
const at = spawnPoint(sim);
|
|
30
|
+
const template = sim.template?.components;
|
|
31
|
+
|
|
32
|
+
// Reuse the authored Player shape and material while forcing the server body
|
|
33
|
+
// to the kinematic type used for connected characters.
|
|
34
|
+
const actor = {
|
|
35
|
+
id: `player:${playerId}`,
|
|
36
|
+
components: {
|
|
37
|
+
Layout: { ...(template?.Layout ?? { width: 34, height: 34 }), ...at, rotation: 0 },
|
|
38
|
+
Collider: { ...(template?.Collider ?? {}), friction: 0 },
|
|
39
|
+
RigidBody: { bodyType: 'kinematic' },
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// addBody returns the centre offset needed to translate Layout reports.
|
|
44
|
+
sim.players.set(playerId, newPlayer(addBody(sim, actor), at));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Remove a departing player's body and its entry.
|
|
48
|
+
export function removePlayer(sim, playerId) {
|
|
49
|
+
const player = sim.players.get(playerId);
|
|
50
|
+
if (!player) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
Matter.Composite.remove(sim.engine.world, player.body);
|
|
54
|
+
sim.players.delete(playerId);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Validate and keep the pose one authenticated connection reported. The session
|
|
58
|
+
// supplies `playerId` independently of the untrusted message contents.
|
|
59
|
+
export function setPose(sim, playerId, message) {
|
|
60
|
+
const player = sim.players.get(playerId);
|
|
61
|
+
if (!player) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Reject malformed or out-of-bounds positions before changing player state.
|
|
66
|
+
const asked = Limits.readPoint(message?.p);
|
|
67
|
+
if (!asked) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Facing is the direction the character is drawn as moving. It has no effect
|
|
72
|
+
// on the body.
|
|
73
|
+
player.facing = number(message?.p?.[2]);
|
|
74
|
+
|
|
75
|
+
// earned returns elapsed simulation time since the last accepted report. The
|
|
76
|
+
// fixed allowance covers multiple reports delivered together after a stall.
|
|
77
|
+
const reach = Limits.MAX_SPEED * earned(sim, player.movedAt) + Limits.MOVE_SLACK;
|
|
78
|
+
player.movedAt = sim.timeMs;
|
|
79
|
+
const step = { x: asked.x - player.at.x, y: asked.y - player.at.y };
|
|
80
|
+
const distance = Math.hypot(step.x, step.y);
|
|
81
|
+
if (distance <= reach) {
|
|
82
|
+
// An accepted move becomes the target written before the next Matter step.
|
|
83
|
+
player.at = asked;
|
|
84
|
+
player.clamped = false;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A move that is too long is cut down to the distance allowed, and the
|
|
89
|
+
// resulting snapshot is marked clamped. That flag tells the reporting client
|
|
90
|
+
// to take this position instead of continuing from the one it was refused.
|
|
91
|
+
const scale = reach / distance;
|
|
92
|
+
player.at = { x: player.at.x + step.x * scale, y: player.at.y + step.y * scale };
|
|
93
|
+
player.clamped = true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The current pose of every character, for a WORLD or STATE message. The
|
|
97
|
+
// session calls this every tick and drops the unchanged poses before sending.
|
|
98
|
+
// Contacts can put the body somewhere other than the last accepted target, so
|
|
99
|
+
// the body is what is read.
|
|
100
|
+
export function playerPoses(sim) {
|
|
101
|
+
const out = [];
|
|
102
|
+
for (const [id, player] of sim.players) {
|
|
103
|
+
// Convert Matter's body centre back to the Layout top-left used on the wire.
|
|
104
|
+
out.push([
|
|
105
|
+
id,
|
|
106
|
+
...packPlayer(
|
|
107
|
+
player.body.position.x - player.offset.dx,
|
|
108
|
+
player.body.position.y - player.offset.dy,
|
|
109
|
+
player.facing,
|
|
110
|
+
player.clamped,
|
|
111
|
+
),
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// A different point around the authored spawn for each player, so two joining at
|
|
118
|
+
// once do not start inside each other.
|
|
119
|
+
function spawnPoint(sim) {
|
|
120
|
+
// A 1.7-radian increment distributes successive players around the ring
|
|
121
|
+
// without repeating a small set of cardinal positions.
|
|
122
|
+
const angle = sim.players.size * 1.7;
|
|
123
|
+
return {
|
|
124
|
+
x: sim.spawn.x + Math.cos(angle) * Limits.SPAWN_RING,
|
|
125
|
+
y: sim.spawn.y + Math.sin(angle) * Limits.SPAWN_RING,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// This module reads the deck's authored scene and blueprints for the session
|
|
2
|
+
// process. It imports source text, resolves component defaults in engine order,
|
|
3
|
+
// and returns the reduced actor data used to build Matter bodies.
|
|
4
|
+
|
|
5
|
+
import { componentDefaults } from '@imports/castle.real-time/code/server/gameHooks.js';
|
|
6
|
+
|
|
7
|
+
// Root-anchored globs select files from the deck. Eager raw imports put the text
|
|
8
|
+
// into the server bundle, whose runtime has no deck filesystem to read.
|
|
9
|
+
const sceneTexts = import.meta.glob('/scenes/main.scene', {
|
|
10
|
+
eager: true,
|
|
11
|
+
query: '?raw',
|
|
12
|
+
import: 'default',
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Deck blueprints and the first level of imported ones, because a scene instance
|
|
16
|
+
// can name either.
|
|
17
|
+
const blueprintTexts = import.meta.glob(['/blueprints/*.scene', '/imports/*/blueprints/*.scene'], {
|
|
18
|
+
eager: true,
|
|
19
|
+
query: '?raw',
|
|
20
|
+
import: 'default',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// The defaults the server's physics representation needs. `Layout`, `Collider`
|
|
24
|
+
// and `RigidBody` are the engine's behaviors; this process reads their authored
|
|
25
|
+
// data without loading the renderer or any behavior code. `Player` marks the
|
|
26
|
+
// authored character, which is a spawn point and a body shape rather than a
|
|
27
|
+
// scene object.
|
|
28
|
+
//
|
|
29
|
+
// A deck's own server-side components go in `code/server/deckComponents.js` and
|
|
30
|
+
// are merged over these. A component in neither table keeps its authored
|
|
31
|
+
// instance values and gets no behavior defaults.
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
Layout: { x: 0, y: 0, z: 0, rotation: 0, width: 50, height: 50 },
|
|
34
|
+
Collider: { friction: 0.1, frictionStatic: 0.5, bounciness: 0, density: 0.001, isTrigger: false },
|
|
35
|
+
RigidBody: {
|
|
36
|
+
bodyType: 'dynamic',
|
|
37
|
+
gravityScale: 1,
|
|
38
|
+
drag: 0.01,
|
|
39
|
+
angularDrag: 0,
|
|
40
|
+
freezeRotation: false,
|
|
41
|
+
},
|
|
42
|
+
Player: {},
|
|
43
|
+
...componentDefaults,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Parse the deck's main scene. `world.createSimulation` builds the matter world
|
|
47
|
+
// from what this returns.
|
|
48
|
+
export function readScene() {
|
|
49
|
+
const text = sceneTexts['/scenes/main.scene'];
|
|
50
|
+
if (!text) {
|
|
51
|
+
// A deck without the conventional main scene produces an empty, zero-gravity world.
|
|
52
|
+
return { actors: [], gravity: { x: 0, y: 0 } };
|
|
53
|
+
}
|
|
54
|
+
const scene = JSON.parse(text);
|
|
55
|
+
|
|
56
|
+
// Authored actor ids are kept, because ownership and object-pose messages
|
|
57
|
+
// address the matching actors on each client by the same id.
|
|
58
|
+
const actors = (scene.actors ?? []).map((actor) => ({
|
|
59
|
+
id: actor.id,
|
|
60
|
+
components: resolveComponents(actor),
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
// The scene's own gravity setting, in the engine's units. `gravity` is the
|
|
64
|
+
// engine's key for the Y component, beside `gravityX`. This room is top-down
|
|
65
|
+
// and sets it to zero.
|
|
66
|
+
return {
|
|
67
|
+
actors,
|
|
68
|
+
gravity: { x: scene.physics?.gravityX ?? 0, y: scene.physics?.gravity ?? 1 },
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Shallow-merge each component in engine precedence order: defaults, blueprint,
|
|
73
|
+
// then the scene instance's values.
|
|
74
|
+
function resolveComponents(actor) {
|
|
75
|
+
const template = blueprintOf(actor.blueprint);
|
|
76
|
+
const merged = {};
|
|
77
|
+
|
|
78
|
+
// The union of both, so a component authored only on the instance and one
|
|
79
|
+
// supplied only by the blueprint both reach the server.
|
|
80
|
+
for (const name of new Set([...Object.keys(template), ...Object.keys(actor.components ?? {})])) {
|
|
81
|
+
// Object spread implements the engine order without mutating imported data.
|
|
82
|
+
merged[name] = { ...DEFAULTS[name], ...template[name], ...actor.components?.[name] };
|
|
83
|
+
}
|
|
84
|
+
return merged;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// The first actor of a referenced blueprint. Scene refs use deck-visible paths
|
|
88
|
+
// such as `blueprints/crate.scene`, so matching the end of a glob key handles
|
|
89
|
+
// both deck and imported locations.
|
|
90
|
+
function blueprintOf(ref) {
|
|
91
|
+
if (!ref) {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
// Imported blueprint keys include their alias prefix, while scene references
|
|
95
|
+
// use the deck-visible suffix such as `blueprints/crate.scene`.
|
|
96
|
+
const key = Object.keys(blueprintTexts).find((path) => path.endsWith(`/${ref}`));
|
|
97
|
+
return key ? (JSON.parse(blueprintTexts[key])?.actors?.[0]?.components ?? {}) : {};
|
|
98
|
+
}
|