castle-web-cli 0.4.229 → 0.4.231

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 (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,440 @@
1
+ // Finding the people you came with, in a town (a place with no `result`): who
2
+ // of the players here is a friend, where each one is (asked of the place's
3
+ // server, `hub-where`), the rows the chrome's People list draws, and the
4
+ // pointers to the friends, and to anyone the player pressed Find on, who are
5
+ // too far away to see. A game draws none of it.
6
+
7
+ import { hub } from './state.js';
8
+ import { NAME_NEAR_M } from './attention.js';
9
+
10
+ // How often the place is asked where the tracked players are.
11
+ const WHERE_EVERY_MS = 1000;
12
+ // As many as the place answers about at once (`WHERE_MAX` on the server).
13
+ const TRACK_MAX = 12;
14
+ // Pointers on screen at once, nearest first.
15
+ const POINTERS_MAX = 3;
16
+ // A party's size (`PARTY_MAX` on the server), and how near someone must stand
17
+ // to be offered an invitation without opening the People list.
18
+ const PARTY_MAX = 8;
19
+ const NEAR_INVITE_M = 2.2;
20
+ // Once offered, the invitation stays until they are this far off: two players
21
+ // standing at the edge of the near distance would otherwise blink it.
22
+ const NEAR_LEAVE_M = 3.4;
23
+ // The invitation is offered after standing beside someone this long, for this
24
+ // long (a friend's stays as long as they are beside the player).
25
+ const NEAR_DWELL_MS = 1500;
26
+ const NEAR_OFFER_MS = 8000;
27
+ // After a press the button says the invitation went, this long, and then is
28
+ // not offered for that player again for a while.
29
+ const NEAR_SENT_MS = 4000;
30
+ const NEAR_AGAIN_MS = 30000;
31
+ // How long an "Over here!" keeps the pointer on its speaker.
32
+ const PING_MS = 10000;
33
+ // A friend first seen this long after the session began has arrived (the
34
+ // ones here at the start were here already).
35
+ const ARRIVAL_AFTER_MS = 8000;
36
+ // How long a friend who left the roster can still be named.
37
+ const RECENT_MS = 20000;
38
+ // The part of the screen an edge pointer keeps to: clear of the HUD above and
39
+ // the buttons below.
40
+ const EDGE = { side: 46, top: 84, bottom: 150 };
41
+
42
+ // `?hubFriends=all` on a page served from localhost: everyone here counts as
43
+ // a friend, since the dev serve has no host to say who is.
44
+ function devEveryone() {
45
+ try {
46
+ const { hostname, search } = globalThis.location ?? {};
47
+ return (hostname === 'localhost' || hostname === '127.0.0.1') && new URLSearchParams(search ?? '').get('hubFriends') === 'all';
48
+ } catch {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ export function makeTogether() {
54
+ const everyone = devEveryone();
55
+ let askedAt = -Infinity;
56
+ // playerId -> { userId, username, friend, party }: the friends and party
57
+ // members in this session and anyone being found, this frame
58
+ let tracked = new Map();
59
+ // Players the pointer is on because the player pressed Find on them.
60
+ const found = new Set();
61
+ // playerId -> until (page ms): a party member or friend who said "Over
62
+ // here!" gets the pointer for a little while, however near they are.
63
+ const pinged = new Map();
64
+ // The player stood beside, and since when (`beside`).
65
+ let near = { playerId: null, since: 0 };
66
+ // playerId -> when (page ms) the player invited them with that button.
67
+ const asked = new Map();
68
+ // Players whose offer ran out, until they walk off.
69
+ const passed = new Set();
70
+ // Every player seen in this session's roster (whether they came in after
71
+ // its first moments, and whether they have been announced), and the
72
+ // friends and party members who came in late, not yet told to the player.
73
+ const seen = new Map();
74
+ // playerId -> { username, until }: friends and party members lately here.
75
+ const recent = new Map();
76
+ let seenSince = null;
77
+ const arrived = [];
78
+ // Where the last ask left off, when there are more players to ask about
79
+ // than one ask covers: they are worked through in turn.
80
+ let turn = 0;
81
+
82
+ return {
83
+ // `friends` is the host's answer (`Hub.friends`: [{ userId, username,
84
+ // here, door? }]) or null. Marks who here is one, and asks the place
85
+ // where they, anyone being found and, while the People list is open, the
86
+ // rest of its rows are.
87
+ frame(mp, friends, active, now, listOpen = false) {
88
+ tracked = new Map();
89
+ const net = mp?.net;
90
+ if (!active || !net || !hub.place || hub.place.result) {
91
+ found.clear();
92
+ pinged.clear();
93
+ return;
94
+ }
95
+ const ids = new Set((friends ?? []).map((f) => String(f.userId)));
96
+ const others = net.others?.() ?? new Map();
97
+ const rest = [];
98
+ for (const [playerId, username] of others) {
99
+ const userId = net.userId?.(playerId) ?? null;
100
+ const friend = everyone || Boolean(userId && ids.has(String(userId)));
101
+ const party = hub.party.members?.includes(playerId) === true;
102
+ if (friend || party || found.has(playerId)) {
103
+ tracked.set(playerId, { userId, username: username || null, friend, party });
104
+ } else {
105
+ rest.push(playerId);
106
+ }
107
+ }
108
+ // A friend or party member who turns up after the first moments here
109
+ // has arrived: the player is told (those already here were the start).
110
+ // A friend or party member is remembered a little after they leave the
111
+ // roster, so word that they went through a door can still name them.
112
+ for (const [playerId, t] of tracked) {
113
+ if (t.friend || t.party) {
114
+ recent.set(playerId, { username: t.username, until: now + RECENT_MS });
115
+ }
116
+ }
117
+ for (const [playerId, r] of recent) {
118
+ if (r.until <= now) {
119
+ recent.delete(playerId);
120
+ }
121
+ }
122
+ // When each player was first in the roster is kept apart from whether
123
+ // they are a friend: a friends answer that comes late must not announce
124
+ // someone who was here all along.
125
+ seenSince ??= now;
126
+ for (const playerId of others.keys()) {
127
+ if (!seen.has(playerId)) {
128
+ seen.set(playerId, { late: now - seenSince > ARRIVAL_AFTER_MS, told: false });
129
+ }
130
+ }
131
+ for (const [playerId, t] of tracked) {
132
+ const first = seen.get(playerId);
133
+ if ((t.friend || t.party) && first && !first.told) {
134
+ first.told = true;
135
+ if (first.late) {
136
+ arrived.push({ playerId, username: t.username });
137
+ }
138
+ }
139
+ }
140
+ for (const playerId of seen.keys()) {
141
+ if (!others.has(playerId)) {
142
+ seen.delete(playerId);
143
+ }
144
+ }
145
+ for (const playerId of found) {
146
+ if (!others.has(playerId)) {
147
+ found.delete(playerId);
148
+ }
149
+ }
150
+ for (const [playerId, until] of pinged) {
151
+ if (!others.has(playerId) || until <= now) {
152
+ pinged.delete(playerId);
153
+ }
154
+ }
155
+ for (const playerId of Object.keys(hub.whereAt)) {
156
+ if (!others.has(playerId)) {
157
+ delete hub.whereAt[playerId];
158
+ }
159
+ }
160
+ if (now - askedAt < WHERE_EVERY_MS || (!tracked.size && !(listOpen && rest.length))) {
161
+ return;
162
+ }
163
+ askedAt = now;
164
+ // More than one ask covers: each ask takes the next twelve in turn.
165
+ const all = listOpen ? [...tracked.keys(), ...rest] : [...tracked.keys()];
166
+ turn = turn >= all.length ? 0 : turn;
167
+ net.send({ k: 'hub-where', ids: all.slice(turn, turn + TRACK_MAX) });
168
+ turn += all.length > TRACK_MAX ? TRACK_MAX : 0;
169
+ },
170
+
171
+ // A new place: nobody is being found or has called.
172
+ clear() {
173
+ found.clear();
174
+ pinged.clear();
175
+ tracked = new Map();
176
+ near = { playerId: null, since: 0 };
177
+ asked.clear();
178
+ passed.clear();
179
+ seen.clear();
180
+ recent.clear();
181
+ seenSince = null;
182
+ arrived.length = 0;
183
+ },
184
+
185
+ // The friends and party members who arrived since this was last asked.
186
+ arrivals: () => arrived.splice(0),
187
+
188
+ // The name of a friend or party member who was here a moment ago, or null.
189
+ lately: (playerId) => recent.get(playerId)?.username ?? null,
190
+
191
+ // Whom a player who just came in should start beside: a party member
192
+ // here first, else the first friend here in the host's order (ghost lists
193
+ // the most recently active first); `{ playerId, username }` or null.
194
+ startBeside() {
195
+ let friend = null;
196
+ for (const [playerId, t] of tracked) {
197
+ if (t.party) {
198
+ return { playerId, username: t.username };
199
+ }
200
+ if (t.friend && !friend) {
201
+ friend = { playerId, username: t.username };
202
+ }
203
+ }
204
+ return friend;
205
+ },
206
+
207
+ // "Over here!" from a party member or a friend: the pointer goes to them.
208
+ ping(playerId, now, always = false) {
209
+ if (always || tracked.get(playerId)?.party || tracked.get(playerId)?.friend) {
210
+ pinged.set(playerId, now + PING_MS);
211
+ }
212
+ },
213
+
214
+ // Find, from the People list: the pointer goes on that player, or off.
215
+ find(playerId) {
216
+ if (!found.delete(playerId)) {
217
+ found.add(playerId);
218
+ }
219
+ },
220
+
221
+ // The People list's rows: the player's party first, then friends (here,
222
+ // then elsewhere), then everyone else here. Each has `actions`, the first
223
+ // the one drawn beside the name: Join goes to a party member or a friend
224
+ // (`goto` here, `jump` to another layer, `door` to the game they are
225
+ // in), Invite asks someone into the party, Find points the way to them.
226
+ // `canReport` is whether the host takes reports.
227
+ rows(mp, friends, canReport) {
228
+ const net = mp?.net;
229
+ const town = Boolean(hub.place && !hub.place.result);
230
+ const members = hub.party.members;
231
+ const room = !members || members.length + hub.party.away + 1 < PARTY_MAX;
232
+ const here = [];
233
+ const seen = new Set();
234
+ for (const [playerId, username] of net?.others?.() ?? []) {
235
+ const userId = net.userId?.(playerId) ?? null;
236
+ if (userId) {
237
+ seen.add(String(userId));
238
+ }
239
+ const t = town ? tracked.get(playerId) : null;
240
+ const actions = [];
241
+ if (t?.party || t?.friend) {
242
+ actions.push({ id: 'goto', text: 'Join' });
243
+ }
244
+ // Anyone standing on or by a door can be gone to the way Doors > Go
245
+ // goes: to that door's pad, which is no more than the Doors list offers.
246
+ const spot = town ? hub.whereAt[playerId] : null;
247
+ if (!t?.party && !t?.friend && spot?.door && hub.doors.some((d) => d.id === spot.door)) {
248
+ actions.push({ id: 'door', text: 'Go there' });
249
+ }
250
+ if (town && !t?.party) {
251
+ if (room) {
252
+ actions.push({ id: 'invite', text: 'Invite' });
253
+ }
254
+ if (!t?.friend) {
255
+ actions.push({ id: 'find', text: found.has(playerId) ? 'Stop' : 'Find' });
256
+ }
257
+ }
258
+ if (canReport && userId) {
259
+ actions.push({ id: 'report', text: 'Report' });
260
+ }
261
+ here.push({
262
+ key: `p:${playerId}`,
263
+ playerId,
264
+ userId,
265
+ username: username || null,
266
+ rank: t?.party ? 0 : t?.friend ? 1 : 3,
267
+ mark: t?.party ? '● ' : t?.friend ? '★ ' : '',
268
+ where: town ? (hub.whereAt[playerId]?.where ?? 'in town') : null,
269
+ actions,
270
+ door: spot?.door ?? null,
271
+ });
272
+ }
273
+ for (const f of town ? (friends ?? []) : []) {
274
+ if (!f?.userId || seen.has(String(f.userId))) {
275
+ continue;
276
+ }
277
+ // In a game behind one of this town's doors (a host that says so), or
278
+ // in another layer of the town.
279
+ const door = typeof f.door === 'string' ? hub.doors.find((d) => d.id === f.door) : null;
280
+ if (typeof f.door === 'string' && !door) {
281
+ continue;
282
+ }
283
+ here.push({
284
+ key: `u:${f.userId}`,
285
+ playerId: null,
286
+ userId: String(f.userId),
287
+ username: f.username ?? null,
288
+ rank: 2,
289
+ mark: '★ ',
290
+ where: door ? `inside ${door.title ?? 'a game'}` : 'in another part of town',
291
+ actions: [{ id: door ? 'door' : 'jump', text: 'Join' }],
292
+ door: door?.id ?? null,
293
+ });
294
+ }
295
+ // A stable sort: each group keeps the roster's order.
296
+ return here.sort((a, b) => a.rank - b.rank);
297
+ },
298
+
299
+ // The line over the People list for a player in a party, or null.
300
+ partyLine() {
301
+ const members = hub.party.members;
302
+ if (!members) {
303
+ return null;
304
+ }
305
+ const away = hub.party.away;
306
+ return `Your party · ${members.length + 1} here${away ? ` · ${away} in a game` : ''}`;
307
+ },
308
+
309
+ // The player stood beside (within `NEAR_INVITE_M`, their avatar drawn)
310
+ // who could be invited into the party: `{ playerId, username }`, or null.
311
+ // Offered only after standing by them a moment, and not for long: a
312
+ // button for everyone walked past, or one that never leaves, is clutter.
313
+ beside(scene, local, now = performance.now()) {
314
+ const lt = local?.components.Transform;
315
+ const mp = scene.multiplayer;
316
+ const members = hub.party.members;
317
+ if (!lt || !hub.place || hub.place.result || (members && members.length + hub.party.away + 1 >= PARTY_MAX)) {
318
+ return null;
319
+ }
320
+ let best = null;
321
+ let held = null;
322
+ const here = new Set();
323
+ for (const [playerId, username] of mp?.net?.others?.() ?? []) {
324
+ const actorId = mp.avatars?.actors?.get(playerId);
325
+ const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
326
+ const d = at ? Math.hypot(at.x - lt.x, at.z - lt.z) : Infinity;
327
+ here.add(playerId);
328
+ if (d > NEAR_LEAVE_M) {
329
+ passed.delete(playerId);
330
+ }
331
+ const sent = asked.get(playerId);
332
+ if (sent !== undefined && now - sent >= NEAR_AGAIN_MS) {
333
+ asked.delete(playerId);
334
+ }
335
+ // One whose offer ran out, or who was just invited, is not in the way
336
+ // of anyone else stood here.
337
+ const quiet = passed.has(playerId) || (asked.has(playerId) && now - sent >= NEAR_SENT_MS);
338
+ if (members?.includes(playerId) || quiet) {
339
+ continue;
340
+ }
341
+ if (d <= NEAR_INVITE_M && (!best || d < best.d)) {
342
+ best = { d, playerId, username: username || null };
343
+ }
344
+ if (playerId === near.playerId && d <= NEAR_LEAVE_M) {
345
+ held = { d, playerId, username: username || null };
346
+ }
347
+ }
348
+ for (const playerId of passed) {
349
+ if (!here.has(playerId)) {
350
+ passed.delete(playerId);
351
+ }
352
+ }
353
+ // The one already stood beside is kept while they are still about.
354
+ best = held ?? best;
355
+ if (best?.playerId !== near.playerId) {
356
+ near = { playerId: best?.playerId ?? null, since: now };
357
+ }
358
+ if (!best) {
359
+ return null;
360
+ }
361
+ if (asked.has(best.playerId)) {
362
+ return { ...best, sent: true };
363
+ }
364
+ const stood = now - near.since;
365
+ if (!tracked.get(best.playerId)?.friend && stood >= NEAR_DWELL_MS + NEAR_OFFER_MS) {
366
+ // Not offered again until they have walked off and come back.
367
+ passed.add(best.playerId);
368
+ return null;
369
+ }
370
+ return stood >= NEAR_DWELL_MS ? best : null;
371
+ },
372
+ // The player pressed the invite button for this one.
373
+ invited(playerId, now = performance.now()) {
374
+ asked.set(playerId, now);
375
+ },
376
+
377
+ // Where each friend here is on screen, for the ones too far to read a
378
+ // name over: `{ id, x, y, text, angle }` in CSS pixels, `angle` (radians,
379
+ // screen space) set when the friend is off screen and the chip sits at
380
+ // the edge pointing their way.
381
+ pointers(scene, local, cam, view) {
382
+ const lt = local?.components.Transform;
383
+ if (!lt || !cam || !tracked.size) {
384
+ return [];
385
+ }
386
+ const mp = scene.multiplayer;
387
+ const far = [];
388
+ for (const [playerId, { username }] of tracked) {
389
+ const actorId = mp?.avatars?.actors?.get(playerId);
390
+ const drawn = actorId ? scene.getActor(actorId)?.components.Transform : null;
391
+ const at = drawn ?? hub.whereAt[playerId];
392
+ if (!at) {
393
+ continue;
394
+ }
395
+ const d = Math.hypot(at.x - lt.x, at.z - lt.z);
396
+ if (d > NAME_NEAR_M || pinged.has(playerId)) {
397
+ far.push({ playerId, username, at, d });
398
+ }
399
+ }
400
+ far.sort((a, b) => a.d - b.d);
401
+ const out = [];
402
+ for (const { playerId, username, at, d } of far.slice(0, POINTERS_MAX)) {
403
+ out.push(pointerAt(cam, view, { x: at.x, y: (at.y ?? lt.y) + 1.9, z: at.z }, `to:${playerId}`, `${username ?? 'friend'} · ${Math.round(d)} m`));
404
+ }
405
+ return out;
406
+ },
407
+ };
408
+ }
409
+
410
+ // One pointer at a point in the world: over it while it is on the clear part
411
+ // of the screen (`angle` null), else at the screen's edge along the way to it
412
+ // as the camera sees it. Friends (above) and a place's goal labels
413
+ // (`hub.label` with `style: "goal"`, contract v1.13) are drawn by it.
414
+ export function pointerAt(cam, view, at, id, text) {
415
+ const v = cam.position.clone().set(at.x, at.y, at.z);
416
+ const inView = v.clone().applyMatrix4(cam.matrixWorldInverse);
417
+ const ndc = v.project(cam);
418
+ const x = ((ndc.x + 1) / 2) * view.w;
419
+ const y = ((1 - ndc.y) / 2) * view.h;
420
+ const onScreen = inView.z < 0 && x >= EDGE.side && x <= view.w - EDGE.side && y >= EDGE.top && y <= view.h - EDGE.bottom;
421
+ if (onScreen) {
422
+ return { id, x, y, text, angle: null };
423
+ }
424
+ let dx = inView.x;
425
+ let dy = -inView.y;
426
+ const len = Math.hypot(dx, dy);
427
+ if (len < 1e-6) {
428
+ dx = 0;
429
+ dy = 1;
430
+ } else {
431
+ dx /= len;
432
+ dy /= len;
433
+ }
434
+ const cx = view.w / 2;
435
+ const cy = (EDGE.top + view.h - EDGE.bottom) / 2;
436
+ const reachX = dx === 0 ? Infinity : (view.w / 2 - EDGE.side) / Math.abs(dx);
437
+ const reachY = dy === 0 ? Infinity : (dy < 0 ? cy - EDGE.top : view.h - EDGE.bottom - cy) / Math.abs(dy);
438
+ const reach = Math.min(reachX, reachY);
439
+ return { id, x: cx + dx * reach, y: cy + dy * reach, text, angle: Math.atan2(dy, dx) };
440
+ }
@@ -0,0 +1,149 @@
1
+ // What players wear on their avatars (contract §11, v1.4): the place sends
2
+ // playerId -> { kind, color, rarity } (`hubWear`), and this draws each one as a
3
+ // few glowing, non-solid Shape actors that follow the avatar's Transform every
4
+ // frame -- this player's own body included. Kinds this kit does not draw are
5
+ // not drawn. A piece is `wear:<playerId>:<n>`; it is despawned when the
6
+ // wearable changes or goes, or the avatar does, and retired with the
7
+ // multiplayer kit so it is never reported to the session as gone.
8
+
9
+ // Heights from the avatar's origin, the capsule's centre. A halo sits just
10
+ // over the avatar's own top and an orbit circles it (`top`, metres above its
11
+ // model's feet), so a short avatar wears them as close as a tall one, under
12
+ // its name tag.
13
+ const FEET_Y = -0.85;
14
+ const MODEL_FEET_Y = -0.7;
15
+ const HALO_GAP = 0.15;
16
+ export const DEFAULT_TOP = 1.65;
17
+ const ORBIT_RADIUS = 0.5;
18
+ const COLOR = /^#[0-9a-f]{6}$/i;
19
+ const MAX_RARITY = 9;
20
+
21
+ function shape(kind, color, rarity, size) {
22
+ return {
23
+ kind,
24
+ ...size,
25
+ color,
26
+ emissive: color,
27
+ emissiveIntensity: 0.6 + rarity * 0.15,
28
+ roughness: 0.4,
29
+ metalness: 0.1,
30
+ solid: false,
31
+ };
32
+ }
33
+
34
+ // The pieces of a wearable, each a Shape and where it sits relative to the
35
+ // avatar at time `t` (seconds). Null for a kind this kit does not draw.
36
+ export function pieces(wearable, top = DEFAULT_TOP) {
37
+ const color = typeof wearable?.color === 'string' && COLOR.test(wearable.color) ? wearable.color.toLowerCase() : null;
38
+ const rarity = Number.isInteger(wearable?.rarity) ? Math.min(MAX_RARITY, Math.max(0, wearable.rarity)) : 0;
39
+ if (!color) {
40
+ return null;
41
+ }
42
+ const grow = 1 + rarity * 0.05;
43
+ switch (wearable.kind) {
44
+ case 'halo':
45
+ return [
46
+ {
47
+ shape: shape('cylinder', color, rarity, { radius: 0.35 * grow, height: 0.04 }),
48
+ at: (t) => ({ x: 0, y: MODEL_FEET_Y + top + HALO_GAP + Math.sin(t * 2) * 0.03, z: 0 }),
49
+ },
50
+ ];
51
+ case 'aura':
52
+ return [
53
+ {
54
+ shape: shape('cylinder', color, rarity, { radius: 0.6 * grow, height: 0.02 }),
55
+ at: () => ({ x: 0, y: FEET_Y + 0.02, z: 0 }),
56
+ },
57
+ ];
58
+ case 'orbit': {
59
+ const n = 1 + Math.floor(rarity / 3);
60
+ const speed = 1.6 + rarity * 0.1;
61
+ return Array.from({ length: n }, (_, i) => ({
62
+ shape: shape('sphere', color, rarity, { radius: 0.07 * grow }),
63
+ at: (t) => {
64
+ const a = t * speed + (i * 2 * Math.PI) / n;
65
+ return { x: Math.cos(a) * ORBIT_RADIUS, y: MODEL_FEET_Y + top + Math.sin(a * 2) * 0.05, z: Math.sin(a) * ORBIT_RADIUS };
66
+ },
67
+ }));
68
+ }
69
+ default:
70
+ return null;
71
+ }
72
+ }
73
+
74
+ export function makeWear() {
75
+ // playerId -> { json, ids, pieces }
76
+ const drawn = new Map();
77
+
78
+ function remove(scene, playerId) {
79
+ const entry = drawn.get(playerId);
80
+ drawn.delete(playerId);
81
+ for (const id of entry?.ids ?? []) {
82
+ if (scene.getActor(id)) {
83
+ scene.multiplayer?.avatars?.retired?.add(id);
84
+ scene.despawnActor(id);
85
+ }
86
+ }
87
+ }
88
+
89
+ return {
90
+ // Spawn, move and remove pieces to match `wearing` (playerId -> wearable).
91
+ // `topOf(playerId)` is how tall that player's avatar stands.
92
+ frame(scene, local, wearing, topOf = () => DEFAULT_TOP, now = performance.now()) {
93
+ const mp = scene.multiplayer;
94
+ const self = mp?.net?.selfId?.() ?? null;
95
+ const t = now / 1000;
96
+ for (const playerId of [...drawn.keys()]) {
97
+ if (!wearing.has(playerId)) {
98
+ remove(scene, playerId);
99
+ }
100
+ }
101
+ for (const [playerId, wearable] of wearing) {
102
+ const actorId = playerId === self ? local?.id : mp?.avatars?.actors?.get(playerId);
103
+ const at = actorId ? scene.getActor(actorId)?.components.Transform : null;
104
+ const top = topOf(playerId);
105
+ const json = `${top}:${JSON.stringify(wearable)}`;
106
+ if (!at || drawn.get(playerId)?.json !== json) {
107
+ remove(scene, playerId);
108
+ }
109
+ if (!at) {
110
+ continue;
111
+ }
112
+ let entry = drawn.get(playerId);
113
+ if (!entry) {
114
+ const parts = pieces(wearable, top) ?? [];
115
+ entry = { json, pieces: parts, ids: parts.map((_, n) => `wear:${playerId}:${n}`) };
116
+ drawn.set(playerId, entry);
117
+ entry.pieces.forEach((piece, n) => {
118
+ const p = piece.at(t);
119
+ scene.spawnActor({
120
+ id: entry.ids[n],
121
+ components: { Transform: { x: at.x + p.x, y: at.y + p.y, z: at.z + p.z }, Shape: { ...piece.shape } },
122
+ });
123
+ });
124
+ }
125
+ entry.pieces.forEach((piece, n) => {
126
+ const pt = scene.getActor(entry.ids[n])?.components.Transform;
127
+ if (pt) {
128
+ const p = piece.at(t);
129
+ pt.x = at.x + p.x;
130
+ pt.y = at.y + p.y;
131
+ pt.z = at.z + p.z;
132
+ }
133
+ });
134
+ }
135
+ },
136
+
137
+ // A scene load took every piece with it: forget them.
138
+ forget() {
139
+ drawn.clear();
140
+ },
141
+
142
+ // A session switch that kept the scene: take every piece down.
143
+ clear(scene) {
144
+ for (const playerId of [...drawn.keys()]) {
145
+ remove(scene, playerId);
146
+ }
147
+ },
148
+ };
149
+ }