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.
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 +4 -2
  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,136 @@
1
+ // Other-player actors and their floating name tags. This module creates actors
2
+ // from deck blueprints, updates their network poses, and removes their scene
3
+ // resources when players leave.
4
+
5
+ import * as NameTags from './nameTags.js';
6
+ import { applyPose } from './poses.js';
7
+
8
+ // The blueprint spawned when a caller names none. A deck's own file wins; a deck
9
+ // scaffolded from this kit has no blueprints yet, so the kit's copy stands in
10
+ // until it makes one.
11
+ const BLUEPRINT = 'blueprints/other-player.scene';
12
+
13
+ function defaultBlueprint(scene) {
14
+ const files = scene.files ?? {};
15
+ if (files[BLUEPRINT]) {
16
+ return BLUEPRINT;
17
+ }
18
+ return Object.keys(files).find((path) => path.endsWith(`/${BLUEPRINT}`)) ?? BLUEPRINT;
19
+ }
20
+
21
+ // Create the actor and tag indexes used by one multiplayer system. Callers may
22
+ // read both maps; a deck can restyle a label through `tags.get(id).sprite` after
23
+ // `showPlayer` creates it.
24
+ export function makeAvatars() {
25
+ return {
26
+ actors: new Map(), // playerId -> actor id
27
+ tags: new Map(), // playerId, or 'self', -> name tag
28
+ };
29
+ }
30
+
31
+ // Create or update one other player and return its engine actor. A deck can
32
+ // modify the returned actor after the kit applies the network pose.
33
+ //
34
+ // A `username` of null draws no label. Options are `blueprint`, the scene to
35
+ // spawn from (default `blueprints/other-player.scene`, the deck's own or the kit's),
36
+ // and `tagHeight`, how far over the body the label floats.
37
+ export function showPlayer(avatars, scene, playerId, username, pose, options = {}) {
38
+ // The actor keeps the blueprint used at creation; `ensureActor` only spawns
39
+ // when this player has no live actor in the current scene.
40
+ const blueprint = options.blueprint ?? defaultBlueprint(scene);
41
+ const actor = ensureActor(avatars, scene, playerId, pose, blueprint);
42
+
43
+ // The engine Transform is the client-visible pose for the other player's actor.
44
+ applyPose(actor.components.Transform, pose);
45
+ const transform = actor.components.Transform;
46
+ if (username === null) {
47
+ dropTag(avatars, scene, playerId);
48
+ } else {
49
+ showTag(avatars, scene, playerId, username, transform, options.tagHeight);
50
+ }
51
+ return actor;
52
+ }
53
+
54
+ // Update the local player's label over the engine's local Player actor. The SDK
55
+ // roster can arrive several frames after connection, so an empty username
56
+ // leaves the label absent until the roster supplies one.
57
+ export function showSelf(avatars, scene, username, transform, tagHeight) {
58
+ if (!username) {
59
+ return;
60
+ }
61
+ showTag(avatars, scene, 'self', username, transform, tagHeight);
62
+ }
63
+
64
+ // Despawn the actor and dispose the tag of every player absent from `present`.
65
+ // The multiplayer system calls this after drawing the current SDK roster.
66
+ export function dropMissing(avatars, scene, present) {
67
+ for (const playerId of [...avatars.actors.keys()]) {
68
+ if (present.has(playerId)) {
69
+ continue;
70
+ }
71
+ scene.despawnActor(avatars.actors.get(playerId));
72
+ avatars.actors.delete(playerId);
73
+ dropTag(avatars, scene, playerId);
74
+ }
75
+ }
76
+
77
+ // Clear bookkeeping after an engine scene load. Scene loading has already
78
+ // despawned the actors, while the directly attached three.js tags still require
79
+ // explicit disposal.
80
+ export function clearAvatars(avatars, scene) {
81
+ avatars.actors.clear();
82
+ for (const key of [...avatars.tags.keys()]) {
83
+ dropTag(avatars, scene, key);
84
+ }
85
+ }
86
+
87
+ // Return the current actor or spawn the deck blueprint for a newly seen player.
88
+ // Player counts are unknown when the scene is authored, so every other player
89
+ // is created from a blueprint at runtime.
90
+ //
91
+ // The actor keeps its original blueprint. To change appearance, call
92
+ // `dropMissing` with an empty map and let the next `showPlayer` create it again.
93
+ function ensureActor(avatars, scene, playerId, pose, blueprint) {
94
+ const known = avatars.actors.get(playerId);
95
+ const existing = known && scene.getActor(known);
96
+ if (existing) {
97
+ return existing;
98
+ }
99
+
100
+ // The other-player blueprint should use a kinematic body. Network poses then
101
+ // place it while its collider can still block and push in the local world.
102
+ const spawned = scene.spawnFromBlueprint(blueprint, {
103
+ components: { Transform: { x: pose[0], y: pose[1], z: pose[2] } },
104
+ });
105
+ avatars.actors.set(playerId, spawned.id);
106
+ return spawned;
107
+ }
108
+
109
+ // Create a tag when the displayed text changes, then place it over the actor.
110
+ // Each redraw allocates a canvas, texture, and sprite, so unchanged text reuses
111
+ // the existing resources.
112
+ function showTag(avatars, scene, key, username, transform, tagHeight) {
113
+ const label = NameTags.shortName(username);
114
+ let tag = avatars.tags.get(key);
115
+
116
+ // Compare against the shortened text stored on the tag. An
117
+ // `anonymous-user-<uuid>` differs from the `anon-xxxxxx` string on the sprite.
118
+ if (!tag || tag.text !== label) {
119
+ if (tag) {
120
+ NameTags.disposeNameTag(scene.three, tag);
121
+ }
122
+ tag = NameTags.makeNameTag(scene.three, label);
123
+ avatars.tags.set(key, tag);
124
+ }
125
+
126
+ // A label's drawn size follows its distance to the camera (`nameTags.js`).
127
+ NameTags.moveNameTag(tag, transform, scene.three?.camera, tagHeight);
128
+ }
129
+
130
+ function dropTag(avatars, scene, key) {
131
+ const tag = avatars.tags.get(key);
132
+ if (tag) {
133
+ NameTags.disposeNameTag(scene.three, tag);
134
+ }
135
+ avatars.tags.delete(key);
136
+ }
@@ -0,0 +1,101 @@
1
+ // Floating username-label sprites for other players and the local player. This module
2
+ // draws text to a canvas, attaches the resulting sprite directly to the engine's
3
+ // three.js scene, scales it by camera distance, and disposes its GPU resources.
4
+
5
+ import * as THREE from 'three';
6
+
7
+ // A capsule origin is at its middle; this offset places the label above its head.
8
+ const HEIGHT_ABOVE = 1.25;
9
+ const PIXELS_PER_UNIT = 200;
10
+
11
+ // The distance at which the label is the size it was drawn to be: 68 canvas
12
+ // pixels over 200 per unit is 0.34m, which on a 500x700 card at fov 55 is 29px.
13
+ // This reference distance matches the common camera placement in kit decks.
14
+ const SAME_SIZE_M = 8;
15
+
16
+ // The minimum scale keeps a label readable when the camera is near the player.
17
+ const MIN_SCALE = 0.35;
18
+
19
+ // Create `{ sprite, text, width, height }` and add the sprite to the engine's
20
+ // three.js scene. The engine actor-to-mesh sync does not update these directly
21
+ // attached sprites, so callers move them each frame with `moveNameTag`.
22
+ export function makeNameTag(world, text) {
23
+ const canvas = drawLabel(text);
24
+ const texture = new THREE.CanvasTexture(canvas);
25
+ texture.colorSpace = THREE.SRGBColorSpace;
26
+ const sprite = new THREE.Sprite(
27
+ new THREE.SpriteMaterial({
28
+ map: texture,
29
+ depthTest: false,
30
+ transparent: true,
31
+ }),
32
+ );
33
+
34
+ // Disabling depth tests keeps names visible through scene geometry. A deck can
35
+ // change `depthTest`, `renderOrder`, or `material.opacity` on the returned
36
+ // sprite material.
37
+ sprite.renderOrder = 10;
38
+ const width = canvas.width / PIXELS_PER_UNIT;
39
+ const height = canvas.height / PIXELS_PER_UNIT;
40
+ sprite.scale.set(width, height, 1);
41
+ world.scene.add(sprite);
42
+
43
+ // `text` is the string actually drawn, not the username it came from. The
44
+ // caller compares against it to decide whether a name changed.
45
+ return { sprite, text, width, height };
46
+ }
47
+
48
+ // Move and scale a tag after its player pose is drawn for the current frame.
49
+ // `heightAbove` lets a deck account for body shapes other than capsules.
50
+ export function moveNameTag(tag, transform, camera, heightAbove = HEIGHT_ABOVE) {
51
+ tag.sprite.position.set(transform.x, transform.y + heightAbove, transform.z);
52
+
53
+ // Apparent sprite size is world scale divided by camera distance. A fixed
54
+ // world scale made labels cover much of the card when common deck cameras
55
+ // moved within one metre of a player. Without a camera, use the reference
56
+ // distance as the available scale input.
57
+ const away = camera ? tag.sprite.position.distanceTo(camera.position) : SAME_SIZE_M;
58
+
59
+ // Hold the apparent size inside SAME_SIZE_M and use normal perspective shrink
60
+ // beyond it. Measured at fov 55, fixed scale versus adjusted scale:
61
+ // 0.8m 286px -> 100, 2m 114px -> 40, 4m 57px -> 29, 8m 29px -> 29, 20m 11 -> 11.
62
+ const scale = Math.max(MIN_SCALE, Math.min(1, away / SAME_SIZE_M));
63
+ tag.sprite.scale.set(tag.width * scale, tag.height * scale, 1);
64
+ }
65
+
66
+ // Remove a tag from the three.js scene and release its texture and material.
67
+ export function disposeNameTag(world, tag) {
68
+ world.scene.remove(tag.sprite);
69
+ tag.sprite.material.map.dispose();
70
+ tag.sprite.material.dispose();
71
+ }
72
+
73
+ // A logged-out player's name is `anonymous-user-<uuid>`. Drawn in full it is
74
+ // wider than the avatar it floats over. The last six characters of the uuid are
75
+ // enough to tell two of them apart.
76
+ export function shortName(username) {
77
+ const anon = /^anonymous-user-(.+)$/.exec(username ?? '');
78
+ return anon ? `anon-${anon[1].replace(/-/g, '').slice(-6)}` : (username ?? 'player');
79
+ }
80
+
81
+ // Rasterize one display name into the canvas used as the sprite texture.
82
+ function drawLabel(text) {
83
+ const canvas = document.createElement('canvas');
84
+ const ctx = canvas.getContext('2d');
85
+ const font = '600 44px Inter, system-ui, sans-serif';
86
+ ctx.font = font;
87
+ const width = Math.ceil(ctx.measureText(text).width) + 40;
88
+
89
+ // Canvas resizing clears the context state, so measure before resizing and
90
+ // restore the font before drawing.
91
+ canvas.width = width;
92
+ canvas.height = 68;
93
+ ctx.font = font;
94
+ ctx.fillStyle = 'rgba(12,12,16,0.72)';
95
+ ctx.roundRect(0, 0, width, 68, 16);
96
+ ctx.fill();
97
+ ctx.fillStyle = '#f2f2f5';
98
+ ctx.textBaseline = 'middle';
99
+ ctx.fillText(text, 20, 36);
100
+ return canvas;
101
+ }
@@ -0,0 +1,220 @@
1
+ // This module defines the multiplayer-3d pose format. The `real-time` core
2
+ // treats a pose as an opaque array of numbers and takes these functions as
3
+ // arguments; every operation that reads inside a pose is here.
4
+ //
5
+ // Two forms. A player pose is `[x, y, z, facing]`: the body stays upright, so
6
+ // one yaw value is its whole orientation. Every other pose is
7
+ // `[x, y, z, qx, qy, qz, qw]`, the same shape the server sends back for what
8
+ // it simulates. Units are metres and radians.
9
+ //
10
+ // Wire packing comes first, then writes onto an engine Transform, then the
11
+ // comparisons, blends and measurements the core takes as callbacks.
12
+ // `multiplayer-2d/code/client/poses.js` is this file's 2D counterpart.
13
+
14
+ import * as THREE from 'three';
15
+ import { rounded } from '@imports/castle.real-time/code/client/messages.js';
16
+
17
+ const DEG = Math.PI / 180;
18
+
19
+ // Millimetre-scale precision on the wire keeps engine float noise out of the
20
+ // payload and out of the change comparisons.
21
+ const PLACES = 3;
22
+
23
+ // A correction over this many metres takes the target at once. Smoothing a
24
+ // multi-metre jump would draw motion through positions nobody reported.
25
+ const SNAP = 3;
26
+
27
+ // How close a player has to be to claim an object. It matches the server reach
28
+ // test in `server/limits.js`, so a claim can be sent before contact, and it
29
+ // exceeds the engine `Door` trigger range of 3 m.
30
+ const CLAIM_RADIUS = 6;
31
+
32
+ // How many of a player pose's values are geometry, before the `clamped` flag at
33
+ // the end. The session's delta filter compares only these, so the flag beside
34
+ // them is not itself a reason to send.
35
+ export const PLAYER_GEOMETRY = 4;
36
+
37
+ // How the shared ownership rule measures reach. Every value in a pose is metres,
38
+ // so there is no `epsilonAt` here and the core's single threshold stands.
39
+ export const REACH = { radius: CLAIM_RADIUS, distanceBetween: flatDistance };
40
+
41
+ // Scratch objects reused by the functions below. No function keeps a reference
42
+ // to one past its return, so no call can see another call's values.
43
+ const qa = new THREE.Quaternion();
44
+ const qb = new THREE.Quaternion();
45
+ const euler = new THREE.Euler();
46
+
47
+ //
48
+ // Packing for the wire
49
+ //
50
+
51
+ // Pack a server-simulated object's position and quaternion.
52
+ export function packPose(p, q) {
53
+ return [round(p.x), round(p.y), round(p.z), round(q.x), round(q.y), round(q.z), round(q.w)];
54
+ }
55
+
56
+ // Pack an upright player as `[x, y, z, facing, clamped]`.
57
+ //
58
+ // `clamped` is 1 only on the tick a player's own move was refused for being
59
+ // faster than the limit. The reporting client uses that flag to take the server
60
+ // position for the body it is simulating itself.
61
+ export function packPlayer(p, facing, clamped) {
62
+ return [round(p.x), round(p.y), round(p.z), round(facing), clamped ? 1 : 0];
63
+ }
64
+
65
+ // Pack the local player's Transform as the pose a move report carries. The
66
+ // engine keeps rotation in degrees; the wire carries radians.
67
+ export function packMove(transform) {
68
+ return [
69
+ round(transform.x),
70
+ round(transform.y),
71
+ round(transform.z),
72
+ round((transform.rotationY ?? 0) * DEG),
73
+ ];
74
+ }
75
+
76
+ // The wire form of a locally simulated Transform: the full object pose.
77
+ export function poseOf(transform) {
78
+ euler.set(
79
+ (transform.rotationX ?? 0) * DEG,
80
+ (transform.rotationY ?? 0) * DEG,
81
+ (transform.rotationZ ?? 0) * DEG,
82
+ );
83
+ qa.setFromEuler(euler);
84
+ return [
85
+ round(transform.x),
86
+ round(transform.y),
87
+ round(transform.z),
88
+ round(qa.x),
89
+ round(qa.y),
90
+ round(qa.z),
91
+ round(qa.w),
92
+ ];
93
+ }
94
+
95
+ //
96
+ // Writing onto the engine
97
+ //
98
+
99
+ // Write a wire pose onto an engine Transform. The engine stores Euler rotations
100
+ // in degrees, while a full pose carries quaternion components.
101
+ export function applyPose(transform, pose) {
102
+ transform.x = pose[0];
103
+ transform.y = pose[1];
104
+ transform.z = pose[2];
105
+
106
+ // The four-value player pose carries only upright yaw. Preserve any authored
107
+ // values on the Transform's other rotation axes.
108
+ if (pose.length === 4) {
109
+ transform.rotationY = pose[3] / DEG;
110
+ return;
111
+ }
112
+ qa.set(pose[3], pose[4], pose[5], pose[6]);
113
+ euler.setFromQuaternion(qa);
114
+ transform.rotationX = euler.x / DEG;
115
+ transform.rotationY = euler.y / DEG;
116
+ transform.rotationZ = euler.z / DEG;
117
+ }
118
+
119
+ // The quaternion part of a full pose, in rapier's `setRotation` shape.
120
+ export function quatOf(pose) {
121
+ return { x: pose[3], y: pose[4], z: pose[5], w: pose[6] };
122
+ }
123
+
124
+ //
125
+ // Callbacks for the shared core
126
+ //
127
+
128
+ // Whether two poses are the same on the wire. `interpolate.pushSample` uses
129
+ // this to drop a repeated broadcast of an unchanged pose. The comparison is
130
+ // tighter than the wire's own rounding, so two poses that differ only by
131
+ // rounding count as equal.
132
+ export function same(a, b) {
133
+ if (a.length !== b.length) {
134
+ return false;
135
+ }
136
+ for (let i = 0; i < a.length; i += 1) {
137
+ if (Math.abs(a[i] - b[i]) > 0.0005) {
138
+ return false;
139
+ }
140
+ }
141
+ return true;
142
+ }
143
+
144
+ // Blend two poses at fraction `f`, where zero selects `a` and one selects `b`.
145
+ // `interpolate.sampleAt` calls this with the samples either side of the render
146
+ // time.
147
+ export function blend(a, b, f) {
148
+ // An upright player pose interpolates its single yaw over the shorter turn.
149
+ if (a.length === 4) {
150
+ return [
151
+ a[0] + (b[0] - a[0]) * f,
152
+ a[1] + (b[1] - a[1]) * f,
153
+ a[2] + (b[2] - a[2]) * f,
154
+ a[3] + shortestTurn(a[3], b[3]) * f,
155
+ ];
156
+ }
157
+
158
+ // Position interpolates linearly and orientation spherically. Independent
159
+ // Euler interpolation can cross each axis wrap at a different time and make a
160
+ // tumbling body change orientation abruptly.
161
+ qa.set(a[3], a[4], a[5], a[6]);
162
+ qb.set(b[3], b[4], b[5], b[6]);
163
+ qa.slerp(qb, f);
164
+ return [
165
+ a[0] + (b[0] - a[0]) * f,
166
+ a[1] + (b[1] - a[1]) * f,
167
+ a[2] + (b[2] - a[2]) * f,
168
+ qa.x,
169
+ qa.y,
170
+ qa.z,
171
+ qa.w,
172
+ ];
173
+ }
174
+
175
+ // Whether the positional correction is past the smoothing range. `smooth`
176
+ // starts over at the target when this reports true.
177
+ export function far(a, b) {
178
+ return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]) > SNAP;
179
+ }
180
+
181
+ // Move the displayed pose a fraction of the way toward the target, in place.
182
+ // `smooth` calls this each frame with the fraction its rate closes over `dt`.
183
+ export function advance(shown, target, f) {
184
+ shown[0] += (target[0] - shown[0]) * f;
185
+ shown[1] += (target[1] - shown[1]) * f;
186
+ shown[2] += (target[2] - shown[2]) * f;
187
+ if (target.length === 4) {
188
+ shown[3] += shortestTurn(shown[3], target[3]) * f;
189
+ return;
190
+ }
191
+ qa.set(shown[3], shown[4], shown[5], shown[6]);
192
+ qb.set(target[3], target[4], target[5], target[6]);
193
+ qa.slerp(qb, f);
194
+ shown[3] = qa.x;
195
+ shown[4] = qa.y;
196
+ shown[5] = qa.z;
197
+ shown[6] = qa.w;
198
+ }
199
+
200
+ // Ground-plane distance. An object on a shelf belongs to the player below it,
201
+ // and a vertically sliding object stays the same distance away.
202
+ export function flatDistance(a, b) {
203
+ return Math.hypot(a.x - b.x, a.z - b.z);
204
+ }
205
+
206
+ // The signed angular difference in the interval [-pi, pi].
207
+ function shortestTurn(from, to) {
208
+ let turn = (to - from) % (2 * Math.PI);
209
+ if (turn > Math.PI) {
210
+ turn -= 2 * Math.PI;
211
+ }
212
+ if (turn < -Math.PI) {
213
+ turn += 2 * Math.PI;
214
+ }
215
+ return turn;
216
+ }
217
+
218
+ function round(value) {
219
+ return rounded(value, PLACES);
220
+ }
@@ -0,0 +1,23 @@
1
+ // The entry point for one Castle multiplayer session process. The tick loop, the
2
+ // message routing and the delta broadcast are `castle.real-time`'s; this file
3
+ // hands it the rapier world, the player capsules and the ownership rule that
4
+ // make the simulation three-dimensional.
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`. Deck-specific
14
+ // behavior belongs in `code/server/game.js`, which `gameHooks.js` loads into the
15
+ // session's seams.
16
+ export default makeSession({
17
+ label: 'multiplayer',
18
+ poses: Poses,
19
+ world: World,
20
+ players: Players,
21
+ ownership: Ownership,
22
+ game,
23
+ });
@@ -0,0 +1,107 @@
1
+ // Simulation and validation limits for the session server. Distances are metres
2
+ // and speeds are metres per second. Constants come first, grouped by player
3
+ // reports, ownership and what the world does on its own, and the tests that
4
+ // apply them come last.
5
+ //
6
+ // The step rate, the catch-up cap and the earned-movement window carry no unit
7
+ // and live in `castle.real-time`; a caller that wants those imports them there.
8
+
9
+ import { number } from '@imports/castle.real-time/code/server/limits.js';
10
+
11
+ //
12
+ // What a player may claim about itself
13
+ //
14
+
15
+ // Deck server code may set these two movement values before the first tick. The
16
+ // remaining limits define the shared simulation and ownership protocol.
17
+
18
+ // The default exceeds the engine controller's 6 m/s walking speed and expected
19
+ // falling speed. Reports beyond this rate are clamped by `players.setPose`.
20
+ // The exported `let` is a live ES-module binding read through this namespace.
21
+ export let MAX_SPEED = 12;
22
+
23
+ // Set the player movement limit for a deck before its first server tick.
24
+ export function setMaxSpeed(metresPerSecond) {
25
+ MAX_SPEED = metresPerSecond;
26
+ }
27
+
28
+ // Add fixed movement allowance for reports delivered together after a connection
29
+ // stall.
30
+ export let MOVE_SLACK = 0.6;
31
+
32
+ // Set the deck-specific fixed allowance added to each player or object move.
33
+ export function setMoveSlack(metres) {
34
+ MOVE_SLACK = metres;
35
+ }
36
+
37
+ // Reject client positions outside the shared ground area and useful vertical range.
38
+ // The horizontal bounds match the ground dimensions created in `world.js`.
39
+ export const BOUNDS = { xz: 200, low: -30, high: 80 };
40
+
41
+ //
42
+ // Ownership
43
+ //
44
+
45
+ // Maximum player-to-object claim distance. It exceeds the engine `Door` trigger
46
+ // range of 3 m so the request can complete before contact.
47
+ export const CLAIM_RADIUS = 6;
48
+
49
+ // Minimum ownership duration before a nearer player may take an object. The
50
+ // delay prevents players on opposite sides from alternating ownership each tick.
51
+ export const HOLD_MS = 500;
52
+
53
+ // Release objects after this interval without an owner report. The value covers
54
+ // the round trip required to receive a grant before the new owner can report its
55
+ // pose. A 500 ms timeout immediately released new grants on a 150 ms connection.
56
+ export const OWNER_TIMEOUT_MS = 1500;
57
+
58
+ // Reject client-owned object reports above this generous physical speed.
59
+ export const MAX_OBJECT_SPEED = 40;
60
+
61
+ //
62
+ // What the world does on its own
63
+ //
64
+
65
+ // Cap velocity imparted by server-side kinematic player capsules. Rapier treats
66
+ // kinematic bodies as infinite mass, which can accelerate a dynamic body across
67
+ // the room. Keeping this below walking speed also keeps the object within claim
68
+ // range until the ownership request completes. Owned objects use client reports
69
+ // and bypass this cap.
70
+ export const MAX_PUSHED_SPEED = 4;
71
+
72
+ // Return a body to its authored position after it falls below the 200 m ground.
73
+ export const FELL_OUT_Y = -12;
74
+
75
+ // Radius used to separate players around the fallback scene origin spawn.
76
+ export const SPAWN_RING = 1.1;
77
+
78
+ //
79
+ // The tests these rules are made of
80
+ //
81
+
82
+ // Parse a finite client point inside the configured world bounds, or return null.
83
+ // Player and owned-object report handlers use this before applying Rapier writes.
84
+ export function readPoint(p) {
85
+ if (!Array.isArray(p) || p.length < 3) {
86
+ return null;
87
+ }
88
+ const [x, y, z] = p.map(number);
89
+ if (Math.abs(x) > BOUNDS.xz || Math.abs(z) > BOUNDS.xz) {
90
+ return null;
91
+ }
92
+ if (y < BOUNDS.low || y > BOUNDS.high) {
93
+ return null;
94
+ }
95
+ return { x, y, z };
96
+ }
97
+
98
+ // Return whether two 3D points are within the server claim radius.
99
+ export function withinReach(from, to) {
100
+ return Math.hypot(from.x - to.x, from.y - to.y, from.z - to.z) <= CLAIM_RADIUS;
101
+ }
102
+
103
+ // Return ground-plane distance, matching the client ownership comparison. An
104
+ // object on a shelf therefore belongs to the player below it.
105
+ export function flatTo(from, to) {
106
+ return Math.hypot(from.x - to.x, from.z - to.z);
107
+ }