castle-web-cli 0.4.156 → 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.
Files changed (243) hide show
  1. package/dist/agent-failures.d.ts +1 -0
  2. package/dist/agent-failures.js +6 -4
  3. package/dist/agent-prompts.js +4 -0
  4. package/dist/agent.d.ts +11 -0
  5. package/dist/agent.js +175 -15
  6. package/dist/bundle.js +8 -2
  7. package/dist/castle-host/host.js +114 -8
  8. package/dist/castleJson.js +6 -1
  9. package/dist/check.d.ts +1 -0
  10. package/dist/check.js +25 -0
  11. package/dist/devSessionServer.js +31 -7
  12. package/dist/imports.js +1 -19
  13. package/dist/index.js +15 -1
  14. package/dist/init.d.ts +2 -2
  15. package/dist/init.js +78 -42
  16. package/dist/mcpPlaytest.d.ts +1 -0
  17. package/dist/mcpPlaytest.js +182 -0
  18. package/dist/metering.d.ts +24 -0
  19. package/dist/metering.js +87 -0
  20. package/dist/sdkResolve.d.ts +3 -0
  21. package/dist/sdkResolve.js +55 -0
  22. package/dist/serve.js +50 -4
  23. package/dist/serverBundle.js +8 -3
  24. package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
  25. package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
  26. package/dist/shell/index.html +2 -2
  27. package/dist/vitePlugins.d.ts +1 -0
  28. package/dist/vitePlugins.js +20 -0
  29. package/kits/base/CLAUDE.md +196 -0
  30. package/kits/{blank → base}/castle.json +5 -2
  31. package/kits/base/editorHost.js +172 -0
  32. package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
  33. package/kits/{blank → base}/package-lock.json +7 -4
  34. package/kits/{blank → base}/package.json +4 -2
  35. package/kits/base/sdk/README.md +569 -0
  36. package/kits/base/sdk/castle.d.ts +27 -0
  37. package/kits/base/sdk/castle.js +16 -0
  38. package/kits/base/sdk/commands.d.ts +181 -0
  39. package/kits/base/sdk/commands.js +16 -0
  40. package/kits/base/sdk/context.d.ts +17 -0
  41. package/kits/base/sdk/context.js +15 -0
  42. package/kits/base/sdk/errors.d.ts +22 -0
  43. package/kits/base/sdk/errors.js +16 -0
  44. package/kits/base/sdk/files.d.ts +15 -0
  45. package/kits/base/sdk/files.js +52 -0
  46. package/kits/base/sdk/haptics.d.ts +6 -0
  47. package/kits/base/sdk/haptics.js +36 -0
  48. package/kits/base/sdk/leaderboard.d.ts +26 -0
  49. package/kits/base/sdk/leaderboard.js +296 -0
  50. package/kits/base/sdk/lifecycle.d.ts +4 -0
  51. package/kits/base/sdk/lifecycle.js +13 -0
  52. package/kits/base/sdk/multiplayer.d.ts +43 -0
  53. package/kits/base/sdk/multiplayer.js +464 -0
  54. package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
  55. package/kits/base/sdk/multiplayerProtocol.js +5 -0
  56. package/kits/base/sdk/passes.d.ts +7 -0
  57. package/kits/base/sdk/passes.js +84 -0
  58. package/kits/base/sdk/portal.d.ts +7 -0
  59. package/kits/base/sdk/portal.js +35 -0
  60. package/kits/base/sdk/runtime.d.ts +62 -0
  61. package/kits/base/sdk/runtime.js +970 -0
  62. package/kits/base/sdk/saveQueue.d.ts +27 -0
  63. package/kits/base/sdk/saveQueue.js +205 -0
  64. package/kits/base/sdk/server/platformHandle.d.ts +25 -0
  65. package/kits/base/sdk/server/platformHandle.js +1 -0
  66. package/kits/base/sdk/server/wrapper.d.ts +28 -0
  67. package/kits/base/sdk/server/wrapper.js +109 -0
  68. package/kits/base/sdk/storage.d.ts +16 -0
  69. package/kits/base/sdk/storage.js +323 -0
  70. package/kits/base/sdk/time.d.ts +17 -0
  71. package/kits/base/sdk/time.js +120 -0
  72. package/kits/base/sdk/transport.d.ts +17 -0
  73. package/kits/base/sdk/transport.js +141 -0
  74. package/kits/base/sdk/types.d.ts +3 -0
  75. package/kits/base/sdk/types.js +1 -0
  76. package/kits/base/sdk/user.d.ts +10 -0
  77. package/kits/base/sdk/user.js +45 -0
  78. package/kits/multiplayer-2d/.prettierrc +5 -0
  79. package/kits/multiplayer-2d/CLAUDE.md +129 -0
  80. package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
  81. package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
  82. package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
  83. package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
  84. package/kits/multiplayer-2d/blueprints/player.scene +15 -0
  85. package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
  86. package/kits/multiplayer-2d/castle.json +39 -0
  87. package/kits/multiplayer-2d/code/client/avatars.js +79 -0
  88. package/kits/multiplayer-2d/code/client/poses.js +168 -0
  89. package/kits/multiplayer-2d/code/server/index.js +26 -0
  90. package/kits/multiplayer-2d/code/server/limits.js +99 -0
  91. package/kits/multiplayer-2d/code/server/ownership.js +177 -0
  92. package/kits/multiplayer-2d/code/server/players.js +127 -0
  93. package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
  94. package/kits/multiplayer-2d/code/server/world.js +337 -0
  95. package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
  96. package/kits/multiplayer-2d/eslint.config.js +21 -0
  97. package/kits/multiplayer-2d/fonts.generated.js +9 -0
  98. package/kits/multiplayer-2d/index.html +25 -0
  99. package/kits/multiplayer-2d/main.jsx +10 -0
  100. package/kits/multiplayer-2d/package-lock.json +351 -0
  101. package/kits/multiplayer-2d/package.json +30 -0
  102. package/kits/multiplayer-2d/scenes/main.scene +20 -0
  103. package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
  104. package/kits/multiplayer-2d/theme.style +4 -0
  105. package/kits/multiplayer-3d/.prettierrc +5 -0
  106. package/kits/multiplayer-3d/CLAUDE.md +215 -0
  107. package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
  108. package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
  109. package/kits/multiplayer-3d/castle.json +43 -0
  110. package/kits/multiplayer-3d/code/client/avatars.js +136 -0
  111. package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
  112. package/kits/multiplayer-3d/code/client/poses.js +220 -0
  113. package/kits/multiplayer-3d/code/server/index.js +23 -0
  114. package/kits/multiplayer-3d/code/server/limits.js +107 -0
  115. package/kits/multiplayer-3d/code/server/ownership.js +157 -0
  116. package/kits/multiplayer-3d/code/server/players.js +114 -0
  117. package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
  118. package/kits/multiplayer-3d/code/server/world.js +306 -0
  119. package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
  120. package/kits/multiplayer-3d/eslint.config.js +20 -0
  121. package/kits/multiplayer-3d/fonts.generated.js +9 -0
  122. package/kits/multiplayer-3d/index.html +22 -0
  123. package/kits/multiplayer-3d/main.jsx +9 -0
  124. package/kits/multiplayer-3d/package-lock.json +368 -0
  125. package/kits/multiplayer-3d/package.json +32 -0
  126. package/kits/multiplayer-3d/scenes/main.scene +409 -0
  127. package/kits/multiplayer-3d/theme.style +3 -0
  128. package/kits/physics-2d/CLAUDE.md +6 -0
  129. package/kits/physics-2d/castle.json +7 -1
  130. package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
  131. package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
  132. package/kits/physics-2d/engine/files.js +9 -5
  133. package/kits/physics-2d/engine/systemRegistry.js +2 -0
  134. package/kits/physics-2d/engine/ui.module.css +11 -0
  135. package/kits/physics-2d/package-lock.json +8 -2
  136. package/kits/physics-2d/package.json +2 -2
  137. package/kits/physics-3d/CLAUDE.md +4 -4
  138. package/kits/physics-3d/behaviors/Shape.jsx +1 -1
  139. package/kits/physics-3d/blueprints/crate.scene +1 -1
  140. package/kits/physics-3d/blueprints/door.scene +4 -4
  141. package/kits/physics-3d/blueprints/pillar.scene +1 -1
  142. package/kits/physics-3d/blueprints/platform.scene +1 -1
  143. package/kits/physics-3d/blueprints/statue.scene +1 -1
  144. package/kits/physics-3d/blueprints/wall.scene +1 -1
  145. package/kits/physics-3d/castle.json +8 -11
  146. package/kits/physics-3d/docs/pxmodel-format.md +1 -1
  147. package/kits/physics-3d/engine3d/materials.js +3 -3
  148. package/kits/physics-3d/engine3d/modelFiles.js +4 -2
  149. package/kits/physics-3d/engine3d/world3d.js +1 -1
  150. package/kits/physics-3d/fonts.generated.js +9 -0
  151. package/kits/physics-3d/main.jsx +1 -1
  152. package/kits/physics-3d/package-lock.json +85 -2
  153. package/kits/physics-3d/package.json +6 -2
  154. package/kits/physics-3d/scenes/main.scene +1 -1
  155. package/kits/physics-3d/scenes/model-lab.scene +1 -1
  156. package/kits/real-time/.prettierrc +5 -0
  157. package/kits/real-time/CLAUDE.md +60 -0
  158. package/kits/real-time/castle.json +19 -0
  159. package/kits/real-time/code/client/clock.js +68 -0
  160. package/kits/real-time/code/client/connection.js +134 -0
  161. package/kits/real-time/code/client/gameHooks.js +50 -0
  162. package/kits/real-time/code/client/interpolate.js +82 -0
  163. package/kits/real-time/code/client/messages.js +55 -0
  164. package/kits/real-time/code/client/ownership.js +94 -0
  165. package/kits/real-time/code/client/smooth.js +54 -0
  166. package/kits/real-time/code/server/blockDelta.js +48 -0
  167. package/kits/real-time/code/server/gameHooks.js +47 -0
  168. package/kits/real-time/code/server/limits.js +35 -0
  169. package/kits/real-time/code/server/session.js +251 -0
  170. package/kits/real-time/eslint.config.js +22 -0
  171. package/kits/real-time/index.html +45 -0
  172. package/kits/real-time/package.json +14 -0
  173. package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
  174. package/kits/turn-based/blueprints/board.scene +15 -0
  175. package/kits/turn-based/blueprints/square.scene +25 -0
  176. package/kits/turn-based/castle.json +37 -0
  177. package/kits/turn-based/code/behaviors/Board.jsx +169 -0
  178. package/kits/turn-based/code/behaviors/Square.jsx +26 -0
  179. package/kits/turn-based/code/server/index.js +152 -0
  180. package/kits/turn-based/drawings/square.sprite +25 -0
  181. package/kits/turn-based/eslint.config.js +12 -0
  182. package/kits/turn-based/fonts.generated.js +9 -0
  183. package/kits/{tabletop → turn-based}/index.html +5 -3
  184. package/kits/{tabletop → turn-based}/main.jsx +2 -2
  185. package/kits/{tabletop → turn-based}/package-lock.json +17 -4
  186. package/kits/{tabletop → turn-based}/package.json +5 -2
  187. package/kits/turn-based/room.js +289 -0
  188. package/kits/turn-based/scenes/main.scene +227 -0
  189. package/kits/{tabletop → turn-based}/testing.js +131 -99
  190. package/kits/turn-based/theme.style +3 -0
  191. package/package.json +1 -1
  192. package/kits/blank/CLAUDE.md +0 -198
  193. package/kits/tabletop/castle.json +0 -34
  194. package/kits/tabletop/room.js +0 -269
  195. /package/kits/{blank → base}/index.html +0 -0
  196. /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
  197. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
  198. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
  199. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
  200. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
  201. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
  202. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
  203. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
  204. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
  205. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
  206. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
  207. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
  208. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
  209. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
  210. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
  211. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
  212. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
  213. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
  214. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
  215. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
  216. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
  217. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
  218. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
  219. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
  220. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
  221. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
  222. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
  223. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
  224. /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
  225. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
  226. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
  227. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
  228. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
  229. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
  230. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
  231. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
  232. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
  233. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
  234. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
  235. /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
  236. /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
  237. /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
  238. /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
  239. /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
  240. /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
  241. /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
  242. /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
  243. /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
@@ -0,0 +1,427 @@
1
+ // Engine system that connects a deck runtime to the shared multiplayer session.
2
+ // It receives timestamped state, draws other players' actors and objects, reconciles the
3
+ // local player, and reports locally simulated poses after engine behaviors run.
4
+
5
+ import * as Avatars from '../client/avatars.js';
6
+ import * as Clock from '@imports/castle.real-time/code/client/clock.js';
7
+ import * as Interpolate from '@imports/castle.real-time/code/client/interpolate.js';
8
+ import * as Ownership from '@imports/castle.real-time/code/client/ownership.js';
9
+ import * as Smoothing from '@imports/castle.real-time/code/client/smooth.js';
10
+ import * as Poses from '../client/poses.js';
11
+ import { connectSession } from '@imports/castle.real-time/code/client/connection.js';
12
+ import { STATE, WORLD } from '@imports/castle.real-time/code/client/messages.js';
13
+ import { game } from '@imports/castle.real-time/code/client/gameHooks.js';
14
+
15
+ // Apply this fraction of a server player correction per frame. Corrections over
16
+ // the snap distance are applied immediately because their intermediate positions
17
+ // were rejected by the server.
18
+ const CLAMP_EASE = 0.25;
19
+ const CLAMP_SNAP = 4;
20
+
21
+ // A deck may provide its own multiplayer system. The engine registry permits
22
+ // duplicate systems, which would create duplicate session connections, so these
23
+ // lazy globs detect deck-owned files without evaluating them. Vite excludes the
24
+ // current module from its own glob when this kit is served directly.
25
+ const deckSystem = {
26
+ ...import.meta.glob('/systems/multiplayer.js'),
27
+ ...import.meta.glob('/code/systems/multiplayer.js'),
28
+ };
29
+
30
+ // The engine system owns connection-wide state and runtime actor lifecycles. The
31
+ // system registry calls `installSystem` once for each created engine runtime.
32
+ // Install one multiplayer system into an engine runtime. Return null when a
33
+ // deck-owned copy exists; otherwise return the registered system for callers
34
+ // that need its state.
35
+ export function installSystem(runtime) {
36
+ if (Object.keys(deckSystem).length > 0) {
37
+ return null;
38
+ }
39
+ const system = new MultiplayerSystem();
40
+ runtime.registerSystem(system);
41
+ return system;
42
+ }
43
+
44
+ class MultiplayerSystem {
45
+ constructor() {
46
+ // The Castle transport is created on the first stepped frame.
47
+ this.net = null;
48
+
49
+ // `starting` prevents concurrent join attempts while the promise is pending.
50
+ this.starting = false;
51
+
52
+ // Network histories are indexed separately for players and scene actors.
53
+ this.players = new Map();
54
+ this.objects = new Map();
55
+
56
+ // The clock maps page time to delayed server simulation time.
57
+ this.clock = Clock.makeClock();
58
+
59
+ // Ownership records objects currently simulated by this client.
60
+ this.own = Ownership.makeOwnership();
61
+
62
+ // Smoothing retains the pose displayed for each network-drawn id.
63
+ this.shown = Smoothing.makeSmoothing();
64
+
65
+ // Avatar state maps other players to spawned actors and name tags.
66
+ this.avatars = Avatars.makeAvatars();
67
+
68
+ // A clamp stores the last server position that rejected a local move.
69
+ this.clampedTo = null;
70
+ }
71
+
72
+ // Run the network frame after engine behaviors and local physics. Connection,
73
+ // receive, draw, clamp, report, and deck hooks execute in that order.
74
+ afterBehaviors(scene, dt) {
75
+ // Connecting on the first stepped frame keeps editor-only runtimes out of
76
+ // multiplayer sessions because the scene editor constructs but never steps them.
77
+ if (!this.net) {
78
+ return void this.start();
79
+ }
80
+ const local = scene.actorWith('Player');
81
+
82
+ // Drain all queued messages before choosing the render time for this frame.
83
+ this.receive(scene);
84
+
85
+ // Draw at a fixed delay behind the server clock so interpolation normally has
86
+ // samples on both sides. The local engine keeps stepping before the first
87
+ // timestamp establishes a clock offset.
88
+ const at = Clock.ready(this.clock) ? Clock.renderTime(this.clock, performance.now()) : null;
89
+ if (at !== null) {
90
+ this.applyObjectPoses(scene, at, dt);
91
+ this.applyPlayerPoses(scene, at, dt);
92
+ }
93
+
94
+ // Apply a server clamp before collecting this frame's outgoing local pose.
95
+ this.acceptClamp(scene, local);
96
+
97
+ // Deck code can make a final local adjustment after engine Player behavior
98
+ // and physics, then optionally replace the kit's standard report.
99
+ game.beforeReport?.(scene, local, this);
100
+ if (game.report) {
101
+ game.report(scene, local, this);
102
+ } else {
103
+ this.report(scene, local);
104
+ }
105
+ if (game.afterFrame) {
106
+ // A deck-owned final frame hook also owns its HUD update for this frame.
107
+ game.afterFrame(scene, dt, this);
108
+ } else {
109
+ // Preserve the engine's look text while adding the standard presence line.
110
+ const line = game.presence?.(this.net.status()) ?? this.presenceLine();
111
+ const look = scene.hud?.look;
112
+ scene.hud = {
113
+ ...scene.hud,
114
+ look: look ? `${line} · ${look}` : line,
115
+ };
116
+ }
117
+ this.publish(scene, local);
118
+ }
119
+
120
+ // Join the Castle public session once and retain the transport across scene loads.
121
+ async start() {
122
+ if (this.starting) {
123
+ return;
124
+ }
125
+ this.starting = true;
126
+ try {
127
+ this.net = await connectSession({ onLog: (text) => console.log(`[multiplayer] ${text}`) });
128
+ } catch (err) {
129
+ console.log(`[multiplayer] could not join: ${err?.message ?? err}`);
130
+ this.starting = false;
131
+ }
132
+ }
133
+
134
+ // Drain queued transport messages. State messages update clock, ownership, pose
135
+ // histories, and deck state in that order; other message kinds go to the deck.
136
+ receive(scene) {
137
+ const selfId = this.net.selfId();
138
+ for (const msg of this.net.drain()) {
139
+ if (msg.k !== STATE && msg.k !== WORLD) {
140
+ game.message?.(scene, msg, this);
141
+ continue;
142
+ }
143
+
144
+ // Pair simulation time with the page arrival time before filing samples.
145
+ Clock.noteArrival(this.clock, msg.t, msg.localMs);
146
+
147
+ // Apply ownership first so an object granted this tick stops receiving
148
+ // network poses on the same frame local simulation begins.
149
+ Ownership.applyHandovers(this.own, msg.own, selfId);
150
+ for (const [id, x, y, z, facing, clamped] of msg.ps ?? []) {
151
+ if (id !== selfId) {
152
+ sample(this.players, msg, id, [x, y, z, facing]);
153
+ } else if (clamped) {
154
+ // The local player's returned pose is used only when the server marks
155
+ // it clamped; ordinary local movement remains client-simulated.
156
+ this.clampedTo = { x, y, z };
157
+ }
158
+ }
159
+ for (const [id, ...pose] of msg.os ?? []) {
160
+ sample(this.objects, msg, id, pose);
161
+ }
162
+
163
+ // Let the deck read its added fields after kit poses and handovers are
164
+ // available through the system state.
165
+ game.state?.(scene, msg, this);
166
+ }
167
+ }
168
+
169
+ //
170
+ // Applying the room state
171
+ //
172
+
173
+ // Apply server-simulated scene object poses at one delayed simulation timestamp.
174
+ applyObjectPoses(scene, at, dt) {
175
+ const phys = scene.physics3d;
176
+ for (const [id, track] of this.objects) {
177
+ // Skip locally owned objects. Applying the older server pose would
178
+ // overwrite local Rapier motion and prevent a local push from taking effect.
179
+ if (this.own.mine.has(id)) {
180
+ Smoothing.forget(this.shown, id);
181
+ continue;
182
+ }
183
+ const actor = scene.getActor(id);
184
+
185
+ // Sample between the two history entries bracketing the render time.
186
+ const sampled = actor?.components.Transform && Interpolate.sampleAt(track, at, Poses.blend);
187
+ if (!sampled) {
188
+ continue;
189
+ }
190
+
191
+ // Smooth the sampled target to absorb small steps from uneven delivery.
192
+ const pose = Smoothing.smooth(this.shown, id, sampled, dt, Poses);
193
+ Poses.applyPose(actor.components.Transform, pose);
194
+
195
+ // Write both engine representations. Rapier copies a dynamic body pose back
196
+ // over its Transform each frame, so changing only the Transform is temporary.
197
+ writeBody(phys?.bodyOf(actor), pose);
198
+ }
199
+ }
200
+
201
+ // Apply each other player's pose, spawning its avatar and name tag when needed.
202
+ applyPlayerPoses(scene, at, dt) {
203
+ const others = this.net.others();
204
+ for (const [playerId, username] of others) {
205
+ const track = this.players.get(playerId);
206
+ const sampled = track && Interpolate.sampleAt(track, at, Poses.blend);
207
+ if (!sampled) {
208
+ continue;
209
+ }
210
+
211
+ // The avatar hook may select a deck blueprint and displayed username. With
212
+ // no hook, `showPlayer` uses `blueprints/other-player.scene` and the SDK name.
213
+ const pose = Smoothing.smooth(this.shown, playerId, sampled, dt, Poses);
214
+ const look = game.avatar ? game.avatar(playerId, username, this) : { username };
215
+ if (!look) {
216
+ continue;
217
+ }
218
+ Avatars.showPlayer(this.avatars, scene, playerId, look.username ?? null, pose, {
219
+ blueprint: look.blueprint,
220
+ });
221
+ }
222
+ Avatars.dropMissing(this.avatars, scene, others);
223
+ for (const playerId of [...this.players.keys()]) {
224
+ if (!others.has(playerId)) {
225
+ this.dropPlayer(playerId);
226
+ }
227
+ }
228
+ }
229
+
230
+ // Remove history and smoothing state after a player leaves the SDK roster.
231
+ dropPlayer(playerId) {
232
+ this.players.delete(playerId);
233
+ Smoothing.forget(this.shown, playerId);
234
+ }
235
+
236
+ //
237
+ // This player
238
+ //
239
+
240
+ // Apply a server position after it clamps an excessive local player move.
241
+ // Ordinary server snapshots do not move the locally simulated player.
242
+ acceptClamp(scene, local) {
243
+ if (!this.clampedTo) {
244
+ return;
245
+ }
246
+ const body = local && scene.physics3d?.bodyOf(local);
247
+ const to = this.clampedTo;
248
+ this.clampedTo = null;
249
+ if (!body) {
250
+ return;
251
+ }
252
+ const at = body.translation();
253
+ const off = { x: to.x - at.x, y: to.y - at.y, z: to.z - at.z };
254
+
255
+ // Apply large corrections immediately because easing would display rejected
256
+ // intermediate positions as valid movement.
257
+ if (Math.hypot(off.x, off.y, off.z) > CLAMP_SNAP) {
258
+ body.setTranslation(to, true);
259
+ body.setLinvel({ x: 0, y: 0, z: 0 }, true);
260
+ return;
261
+ }
262
+
263
+ // Ease smaller corrections to avoid an abrupt backward change after an
264
+ // ordinary dropped frame or client stall.
265
+ body.setTranslation(
266
+ { x: at.x + off.x * CLAMP_EASE, y: at.y + off.y * CLAMP_EASE, z: at.z + off.z * CLAMP_EASE },
267
+ true,
268
+ );
269
+ }
270
+
271
+ // Report the post-physics local Player pose, poses of locally owned objects,
272
+ // and proximity-based ownership requests. Engine Player behavior and local
273
+ // Rapier simulation have already completed for this frame.
274
+ report(scene, local) {
275
+ if (!local) {
276
+ return;
277
+ }
278
+ const now = performance.now();
279
+ const me = local.components.Transform;
280
+ const objects = game.claimable?.(scene, this) ?? this.claimable(scene);
281
+ const claims = Ownership.toClaim(
282
+ this.own,
283
+ objects,
284
+ me,
285
+ this.otherPlaces(now),
286
+ now,
287
+ Poses.REACH,
288
+ );
289
+ const releases = Ownership.toRelease(this.own, objects, me, Poses.REACH);
290
+
291
+ // Include only granted objects because the server rejects poses for other ids.
292
+ const owned = [];
293
+ for (const [id, transform] of objects) {
294
+ if (this.own.mine.has(id)) {
295
+ owned.push([id, ...Poses.poseOf(transform)]);
296
+ }
297
+ }
298
+
299
+ // `sendMove` only goes out at MOVE_HZ; claims are held until it does. The
300
+ // transport takes a packed pose, so the Transform is encoded here.
301
+ this.net.sendMove(Poses.packMove(me), owned, claims, releases);
302
+ Avatars.showSelf(this.avatars, scene, this.net.status().you, me);
303
+ }
304
+
305
+ // Return `[id, Transform]` for every scene object known from server snapshots.
306
+ // The default report uses this same set for claims, releases, and owned poses.
307
+ claimable(scene) {
308
+ const out = [];
309
+ for (const id of this.objects.keys()) {
310
+ const transform = scene.getActor(id)?.components.Transform;
311
+ if (transform) {
312
+ out.push([id, transform]);
313
+ }
314
+ }
315
+ return out;
316
+ }
317
+
318
+ // Sample other player positions at the current delayed render time. Ownership
319
+ // claims use these points to require the local player to be closest.
320
+ otherPlaces(now) {
321
+ const at = Clock.ready(this.clock) ? Clock.renderTime(this.clock, now) : null;
322
+ if (at === null) {
323
+ return [];
324
+ }
325
+ const out = [];
326
+ for (const track of this.players.values()) {
327
+ const pose = Interpolate.sampleAt(track, at, Poses.blend);
328
+ if (pose) {
329
+ out.push({ x: pose[0], y: pose[1], z: pose[2] });
330
+ }
331
+ }
332
+ return out;
333
+ }
334
+
335
+ // What this client currently believes about the room, on
336
+ // `globalThis.__multiplayer`, for a two-client test to read out of the page.
337
+ publish(scene, local) {
338
+ const objects = {};
339
+ for (const id of this.objects.keys()) {
340
+ const transform = scene.getActor(id)?.components.Transform;
341
+ if (transform) {
342
+ objects[id] = [transform.x, transform.y, transform.z];
343
+ }
344
+ }
345
+ const players = {};
346
+ for (const [playerId, actorId] of this.avatars.actors) {
347
+ const transform = scene.getActor(actorId)?.components.Transform;
348
+ if (transform) {
349
+ players[playerId] = [transform.x, transform.y, transform.z];
350
+ }
351
+ }
352
+ const me = local?.components.Transform;
353
+ const status = this.net.status();
354
+ globalThis.__multiplayer = {
355
+ selfId: this.net.selfId(),
356
+ state: status.state,
357
+ others: status.others,
358
+ delay: Math.round(this.clock.delay),
359
+ ready: Clock.ready(this.clock),
360
+ mine: [...this.own.mine],
361
+ me: me ? [me.x, me.y, me.z] : null,
362
+ players,
363
+ objects,
364
+ };
365
+ }
366
+
367
+ // Format the default multiplayer presence text used by the engine HUD.
368
+ presenceLine() {
369
+ const { state, solo, others } = this.net.status();
370
+ if (solo) {
371
+ return 'playing solo';
372
+ }
373
+ if (state !== 'connected') {
374
+ return state;
375
+ }
376
+ return others === 0 ? 'nobody else here' : `${others} other player${others === 1 ? '' : 's'}`;
377
+ }
378
+
379
+ // Reset scene-bound actors, histories, ownership, and smoothing after an engine
380
+ // scene load. The Castle connection remains active for the replacement scene.
381
+ reset(scene) {
382
+ game.reset?.(scene, this);
383
+ Avatars.clearAvatars(this.avatars, scene);
384
+ this.players.clear();
385
+ this.objects.clear();
386
+ this.own = Ownership.makeOwnership();
387
+ this.shown = Smoothing.makeSmoothing();
388
+ this.clampedTo = null;
389
+ }
390
+
391
+ // Release scene resources and leave the session when the engine runtime ends.
392
+ dispose(scene) {
393
+ game.dispose?.(scene, this);
394
+ this.reset(scene);
395
+ this.net?.close();
396
+ this.net = null;
397
+ }
398
+ }
399
+
400
+ // File one player or object pose under the message's simulation timestamp.
401
+ function sample(tracks, msg, id, pose) {
402
+ // A WORLD message represents complete current state. Replace an existing track
403
+ // so samples from an earlier scene or connection do not precede this snapshot.
404
+ if (msg.k === WORLD) {
405
+ tracks.delete(id);
406
+ }
407
+ let track = tracks.get(id);
408
+ if (!track) {
409
+ tracks.set(id, (track = Interpolate.makeTrack()));
410
+ }
411
+
412
+ // Use `msg.t` rather than arrival time so interpolation intervals match server
413
+ // physics time and exclude delivery jitter.
414
+ Interpolate.pushSample(track, msg.t, pose, Poses.same);
415
+ }
416
+
417
+ // Apply a full object pose directly to Rapier and clear residual velocities. A
418
+ // later network sample supplies the next position and orientation.
419
+ function writeBody(body, pose) {
420
+ if (!body || pose.length !== 7) {
421
+ return;
422
+ }
423
+ body.setTranslation({ x: pose[0], y: pose[1], z: pose[2] }, true);
424
+ body.setRotation(Poses.quatOf(pose), true);
425
+ body.setLinvel({ x: 0, y: 0, z: 0 }, true);
426
+ body.setAngvel({ x: 0, y: 0, z: 0 }, true);
427
+ }
@@ -0,0 +1,20 @@
1
+ // ESLint scope and kit-specific rules. The first block excludes dependency and
2
+ // build directories; the second applies JavaScript parsing and required braces
3
+ // to the hand-written code modules.
4
+ export default [
5
+ // Keep imported, installed, generated, and runtime files outside lint checks.
6
+ {
7
+ ignores: ['node_modules/**', 'dist/**', '.castle/**', 'imports/**'],
8
+ },
9
+ // Prettier does not add braces, so `eslint --fix` applies the `curly` rule.
10
+ {
11
+ files: ['code/**/*.{js,jsx}'],
12
+ languageOptions: {
13
+ ecmaVersion: 'latest',
14
+ sourceType: 'module',
15
+ },
16
+ rules: {
17
+ curly: ['error', 'all'],
18
+ },
19
+ },
20
+ ];
@@ -0,0 +1,9 @@
1
+ // Generated by the Style editor. Do not edit -- picking a font rewrites it.
2
+ //
3
+ // `theme.style` records that this deck's font is DMSans; this module is what
4
+ // puts DMSans's bytes in the published bundle. It has to name the face in a
5
+ // static import: reaching it through the name in `theme.style` instead would
6
+ // inline all nine Castle faces into the deck, since no bundler can tell which
7
+ // one a runtime lookup reads.
8
+ export { installDMSans as installDeckFont } from 'castle-web-fonts';
9
+ export const deckFont = 'DMSans';
@@ -0,0 +1,22 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
5
+ <meta name="color-scheme" content="dark" />
6
+ <title>multiplayer-3d</title>
7
+ <!-- Declare the dark scheme before the JS-imported CSS loads, so the frame
8
+ never paints light first (Safari won't repaint a composited iframe on a
9
+ color-scheme change). -->
10
+ <style>
11
+ html {
12
+ color-scheme: dark;
13
+ background: #0a0a0a;
14
+ }
15
+ </style>
16
+ </head>
17
+ <body>
18
+ <div id="root"></div>
19
+ <!-- The kit's own entry, so playing this kit plays its starter game. -->
20
+ <script type="module" src="./main.jsx"></script>
21
+ </body>
22
+ </html>
@@ -0,0 +1,9 @@
1
+ // Entry point for decks that use the multiplayer-3d kit. It loads the
2
+ // physics-3d entry point that provides the engine and editors.
3
+ // The multiplayer-3d kit provides the netcode, client system, and starter room.
4
+ // Playing and editing use the physics-3d facilities as provided.
5
+ //
6
+ // A deck's `index.html` can load `/imports/castle.multiplayer-3d/main.jsx` as
7
+ // its single kit entry point. The same path supplies the `main` module named in
8
+ // `castle.json`, while this import loads physics-3d for the deck.
9
+ import '@imports/castle.physics-3d/main.jsx';