castle-web-cli 0.4.228 → 0.4.230

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 (236) hide show
  1. package/dist/agent-prompts.d.ts +11 -3
  2. package/dist/agent-prompts.js +153 -53
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +43 -33
  5. package/dist/api.d.ts +47 -2
  6. package/dist/api.js +85 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +779 -3
  9. package/dist/castleJson.d.ts +59 -0
  10. package/dist/castleJson.js +267 -3
  11. package/dist/check.js +27 -1
  12. package/dist/cloudGit.d.ts +20 -1
  13. package/dist/cloudGit.js +267 -10
  14. package/dist/contentPack.d.ts +49 -0
  15. package/dist/contentPack.js +273 -0
  16. package/dist/devExperiments.d.ts +21 -0
  17. package/dist/devExperiments.js +139 -0
  18. package/dist/devHub.d.ts +55 -0
  19. package/dist/devHub.js +187 -0
  20. package/dist/devSessionChild.js +3 -1
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +229 -44
  23. package/dist/experimentAssignment.d.ts +15 -0
  24. package/dist/experimentAssignment.js +41 -0
  25. package/dist/importCheckHook.d.ts +40 -0
  26. package/dist/importCheckHook.js +171 -0
  27. package/dist/importRegression.d.ts +15 -0
  28. package/dist/importRegression.js +100 -0
  29. package/dist/importTrust.d.ts +2 -0
  30. package/dist/importTrust.js +27 -0
  31. package/dist/importUpdateHook.d.ts +18 -0
  32. package/dist/importUpdateHook.js +31 -8
  33. package/dist/importUpdateHookRunner.js +19 -7
  34. package/dist/imports.d.ts +16 -0
  35. package/dist/imports.js +145 -33
  36. package/dist/index.js +8 -1
  37. package/dist/init.d.ts +3 -0
  38. package/dist/init.js +125 -8
  39. package/dist/install.js +4 -1
  40. package/dist/localPaths.js +4 -0
  41. package/dist/login.js +2 -2
  42. package/dist/platformDoc.js +3 -8
  43. package/dist/playPagePolicy.js +3 -1
  44. package/dist/remix.js +5 -0
  45. package/dist/routerTaskReport.d.ts +28 -0
  46. package/dist/routerTaskReport.js +56 -0
  47. package/dist/save-deck.d.ts +18 -0
  48. package/dist/save-deck.js +145 -6
  49. package/dist/serve.d.ts +8 -0
  50. package/dist/serve.js +94 -13
  51. package/dist/serverBundle.d.ts +1 -1
  52. package/dist/serverBundle.js +5 -1
  53. package/dist/sourceLineage.d.ts +42 -0
  54. package/dist/sourceLineage.js +380 -0
  55. package/dist/webSourceSync.d.ts +11 -0
  56. package/dist/webSourceSync.js +26 -9
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +301 -52
  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 +6 -1
  64. package/kits/base/sdk/castle.js +3 -0
  65. package/kits/base/sdk/commands.d.ts +208 -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 +86 -6
  73. package/kits/base/sdk/multiplayer.js +157 -4
  74. package/kits/base/sdk/server/hub.d.ts +169 -0
  75. package/kits/base/sdk/server/hub.js +125 -0
  76. package/kits/base/sdk/server/platformHandle.d.ts +49 -2
  77. package/kits/base/sdk/server/storage.d.ts +55 -2
  78. package/kits/base/sdk/server/wrapper.d.ts +85 -6
  79. package/kits/base/sdk/server/wrapper.js +393 -30
  80. package/kits/base/sdk/transport.d.ts +2 -1
  81. package/kits/base/sdk/transport.js +13 -1
  82. package/kits/hub-game/.prettierrc +5 -0
  83. package/kits/hub-game/CLAUDE.md +146 -0
  84. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  85. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  86. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  87. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  88. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  89. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  90. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  91. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  92. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  93. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  94. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  95. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  96. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  97. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  98. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  99. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  100. package/kits/hub-game/castle.json +96 -0
  101. package/kits/hub-game/code/client/attention.js +133 -0
  102. package/kits/hub-game/code/client/avatar.js +288 -0
  103. package/kits/hub-game/code/client/cabinets.js +90 -0
  104. package/kits/hub-game/code/client/chat.js +135 -0
  105. package/kits/hub-game/code/client/chrome.js +1001 -0
  106. package/kits/hub-game/code/client/doorFrame.js +15 -0
  107. package/kits/hub-game/code/client/firstJoin.js +14 -0
  108. package/kits/hub-game/code/client/grab.js +241 -0
  109. package/kits/hub-game/code/client/hookLayer.js +230 -0
  110. package/kits/hub-game/code/client/idle.js +326 -0
  111. package/kits/hub-game/code/client/keys.js +48 -0
  112. package/kits/hub-game/code/client/pack.js +232 -0
  113. package/kits/hub-game/code/client/packFormat.js +397 -0
  114. package/kits/hub-game/code/client/say.js +127 -0
  115. package/kits/hub-game/code/client/sha256.js +80 -0
  116. package/kits/hub-game/code/client/sound.js +147 -0
  117. package/kits/hub-game/code/client/state.js +131 -0
  118. package/kits/hub-game/code/client/talk.js +259 -0
  119. package/kits/hub-game/code/client/together.js +365 -0
  120. package/kits/hub-game/code/client/wear.js +149 -0
  121. package/kits/hub-game/code/server/actors.js +627 -0
  122. package/kits/hub-game/code/server/blocks.js +103 -0
  123. package/kits/hub-game/code/server/cabinets.js +309 -0
  124. package/kits/hub-game/code/server/game.js +137 -0
  125. package/kits/hub-game/code/server/grab.js +247 -0
  126. package/kits/hub-game/code/server/history.js +96 -0
  127. package/kits/hub-game/code/server/hub.js +1904 -0
  128. package/kits/hub-game/code/server/idle.js +412 -0
  129. package/kits/hub-game/code/server/index.js +64 -0
  130. package/kits/hub-game/code/server/labels.js +59 -0
  131. package/kits/hub-game/code/server/manifest.js +527 -0
  132. package/kits/hub-game/code/server/npcs.js +745 -0
  133. package/kits/hub-game/code/server/party.js +381 -0
  134. package/kits/hub-game/code/server/place.js +43 -0
  135. package/kits/hub-game/code/server/say.js +83 -0
  136. package/kits/hub-game/code/server/wear.js +142 -0
  137. package/kits/hub-game/code/systems/hub.js +2230 -0
  138. package/kits/hub-game/docs/content-pack.md +151 -0
  139. package/kits/hub-game/docs/contract.md +1139 -0
  140. package/kits/hub-game/docs/style.md +137 -0
  141. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  142. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  143. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  144. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  145. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  146. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  147. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  148. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  149. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  150. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  151. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  152. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  153. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  154. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  155. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  156. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  157. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  158. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  159. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  160. package/kits/hub-game/eslint.config.js +20 -0
  161. package/kits/hub-game/fonts.generated.js +9 -0
  162. package/kits/hub-game/index.html +22 -0
  163. package/kits/hub-game/main.jsx +6 -0
  164. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  165. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  167. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  168. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  169. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  170. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  171. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  172. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  174. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  175. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  176. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  177. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  178. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  179. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  180. package/kits/hub-game/package-lock.json +365 -0
  181. package/kits/hub-game/package.json +32 -0
  182. package/kits/hub-game/scenes/main.scene +318 -0
  183. package/kits/hub-game/scripts/check.mjs +339 -0
  184. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  185. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  186. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  187. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  188. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  189. package/kits/hub-game/scripts/pack.mjs +192 -0
  190. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  191. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  192. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  193. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  194. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  195. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  196. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  197. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  198. package/kits/hub-game/scripts/test-door.mjs +56 -0
  199. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  200. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  201. package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
  202. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  203. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  204. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  205. package/kits/hub-game/scripts/test-manifest.mjs +224 -0
  206. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  207. package/kits/hub-game/scripts/test-npcs.mjs +714 -0
  208. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  209. package/kits/hub-game/scripts/test-party.mjs +286 -0
  210. package/kits/hub-game/scripts/test-say.mjs +149 -0
  211. package/kits/hub-game/scripts/test-together.mjs +183 -0
  212. package/kits/hub-game/theme.style +3 -0
  213. package/kits/multiplayer-3d/castle.json +5 -5
  214. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  215. package/kits/multiplayer-3d/code/server/players.js +4 -1
  216. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  217. package/kits/multiplayer-3d/code/server/world.js +20 -7
  218. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  219. package/kits/physics-2d/castle.json +2 -2
  220. package/kits/physics-2d/engine/art.js +13 -0
  221. package/kits/physics-2d/engine/assets.js +4 -2
  222. package/kits/physics-2d/engine/scene.js +25 -5
  223. package/kits/physics-3d/CLAUDE.md +6 -1
  224. package/kits/physics-3d/castle.json +3 -3
  225. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
  226. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  227. package/kits/physics-3d/engine3d/materials.js +29 -1
  228. package/kits/physics-3d/engine3d/world3d.js +65 -1
  229. package/kits/physics-3d/systems/physics3d.js +5 -2
  230. package/kits/real-time/castle.json +2 -2
  231. package/kits/real-time/code/client/connection.js +77 -7
  232. package/kits/real-time/code/client/gameHooks.js +63 -3
  233. package/kits/real-time/code/client/messages.js +7 -0
  234. package/kits/real-time/code/server/gameHooks.js +24 -0
  235. package/kits/real-time/code/server/session.js +390 -22
  236. package/package.json +2 -2
@@ -0,0 +1,748 @@
1
+ // Writes the kit's four `bean` avatars, `models/bean/avatar-0.pxmodel` to
2
+ // `avatar-3.pxmodel`: the Castle mascot, Miso the cat, Sprout and Pip the
3
+ // chick. Run from the kit: `node scripts/gen-avatars-bean.mjs` (or with a
4
+ // directory to write into instead of `models/bean/`). The model files are this
5
+ // script's output; change a character here and run it again.
6
+ //
7
+ // A bean is one rounded body spun round its own axis, with no separate head:
8
+ // the `head` part is the face, hung from a pivot on that axis, so the idle
9
+ // look-around slides the face (and whatever sits on top) round the body. The
10
+ // body has to stay round for that, so it is never stretched in x or z.
11
+ //
12
+ // What every avatar keeps, because the kit and the phone budget depend on it:
13
+ // - part ids `torso`, `head`, `legL`, `legR`, `armL`, `armR` (the walk moves
14
+ // them), head and arms under `torso`, feet on y = 0, facing +z;
15
+ // - at most 1,500 triangles and 8 draw calls: six batches (torso, head, two
16
+ // legs, two arms) and a seventh for the eyes, which blink. There is no
17
+ // outline, which would draw each outlined batch a second time;
18
+ // - flat colours: no textures and no glow.
19
+
20
+ import fs from 'node:fs';
21
+ import path from 'node:path';
22
+ import { fileURLToPath } from 'node:url';
23
+
24
+ const KIT_DIR = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
25
+ const OUT_DIR = process.argv[2]
26
+ ? path.resolve(process.argv[2])
27
+ : path.join(KIT_DIR, 'models', 'bean');
28
+
29
+ const INK = '#241a2e';
30
+ const WHITE = '#ffffff';
31
+ const LOOK = { outline: 0, rim: 0.3, ao: 0.15 };
32
+ // Colours come out darker than written under the engine's lights; this
33
+ // multiplies every colour but the ink.
34
+ const BRIGHT = 2;
35
+ // The mascot is near-black, lifted less so it stays black.
36
+ const MASCOT_BRIGHT = 1.6;
37
+ // The parts the walk and the idle move; each is its own batch with everything
38
+ // under it.
39
+ const ANCHORS = ['torso', 'head', 'legL', 'legR', 'armL', 'armR', 'tail'];
40
+
41
+ // The body's underside, and the torso's pivot, above the feet.
42
+ const FLOOR = 0.2;
43
+ // The leg pivot above the feet, inside the body.
44
+ const HIP = 0.3;
45
+ const ARM = 0.3;
46
+ // A part under 3.5 cm in radius is drawn apart from larger ones, so a glint is
47
+ // no smaller.
48
+ const GLINT = 0.036;
49
+
50
+ const round = (n) => Math.round(n * 1000) / 1000;
51
+ const rad = (d) => (d * Math.PI) / 180;
52
+ const deg = (r) => (r * 180) / Math.PI;
53
+
54
+ function mat(part, color, bright = BRIGHT) {
55
+ part.color = color;
56
+ part.roughness = 1;
57
+ if (color !== INK && bright !== 1) {
58
+ part.brightness = bright;
59
+ }
60
+ return part;
61
+ }
62
+
63
+ // A body's outline from the bottom of its axis to the top, as [radius, y]
64
+ // above the torso pivot: a flat sole `sole` wide, a corner `corner` tall, a
65
+ // side that narrows to `top` times the radius, and a dome `dome` tall.
66
+ function beanProfile({ height, radius, sole = 0, corner, top = 1, dome }) {
67
+ const profile = [[0, 0]];
68
+ for (const turn of [0, 30, 60, 90]) {
69
+ const at = rad(turn);
70
+ profile.push([sole + (radius - sole) * Math.sin(at), corner * (1 - Math.cos(at))]);
71
+ }
72
+ const shoulder = height - dome;
73
+ for (const elevation of [0, 20, 40, 58, 74]) {
74
+ const at = rad(elevation);
75
+ profile.push([radius * top * Math.cos(at), shoulder + dome * Math.sin(at)]);
76
+ }
77
+ profile.push([0, height]);
78
+ return profile.filter(([r], k) => k === 0 || r > 0 || k === profile.length - 1);
79
+ }
80
+
81
+ function body(profile, color, { bright = BRIGHT, segments = 16 } = {}) {
82
+ return mat(
83
+ {
84
+ id: 'torso',
85
+ kind: 'lathe',
86
+ position: [0, FLOOR, 0],
87
+ profile: profile.map(([r, y]) => [round(r), round(y)]),
88
+ segments,
89
+ },
90
+ color,
91
+ bright,
92
+ );
93
+ }
94
+
95
+ // Places things on a body. `at(angle, y, lift)` is the point on its surface
96
+ // `angle` degrees round from the front and `y` above the torso pivot, pushed
97
+ // `lift` along the normal, with the turn that lays a +z-facing shape flat on
98
+ // it. Positions are measured from the head pivot, `headY` up the axis.
99
+ function surface(profile, headY) {
100
+ return (angle, y, lift = 0) => {
101
+ const k = Math.max(
102
+ 1,
103
+ profile.findIndex(([, top], i) => i > 0 && top >= y),
104
+ );
105
+ const [r0, y0] = profile[k - 1];
106
+ const [r1, y1] = profile[k];
107
+ const r = r0 + ((r1 - r0) * (y - y0)) / (y1 - y0 || 1);
108
+ const length = Math.hypot(y1 - y0, r1 - r0);
109
+ const out = (y1 - y0) / length;
110
+ const up = -(r1 - r0) / length;
111
+ const a = rad(angle);
112
+ const reach = r + lift * out;
113
+ const normal = [out * Math.sin(a), up, out * Math.cos(a)];
114
+ return {
115
+ position: [
116
+ round(reach * Math.sin(a)),
117
+ round(y - headY + lift * up),
118
+ round(reach * Math.cos(a)),
119
+ ],
120
+ rotation: [
121
+ round(deg(Math.atan2(-normal[1], normal[2]))),
122
+ round(deg(Math.asin(normal[0]))),
123
+ 0,
124
+ ],
125
+ };
126
+ };
127
+ }
128
+
129
+ // Stub legs and mitten arms, the same on all four. `radius` is the body's,
130
+ // where the arms hang; `shoulder` their height above the torso pivot.
131
+ function limbs({ color, feet = color, radius, shoulder, legX = 0.165, bright = BRIGHT }) {
132
+ const leg = [
133
+ [0, -HIP],
134
+ [0.085, -HIP + 0.01],
135
+ [0.118, -HIP + 0.06],
136
+ [0.112, -0.08],
137
+ [0.07, 0],
138
+ ];
139
+ const arm = [
140
+ [0, -ARM],
141
+ [0.058, -ARM + 0.01],
142
+ [0.092, -ARM + 0.042],
143
+ [0.102, -ARM + 0.1],
144
+ [0.088, -0.06],
145
+ [0.05, 0],
146
+ ];
147
+ const parts = [];
148
+ for (const [side, sx] of [
149
+ ['L', 1],
150
+ ['R', -1],
151
+ ]) {
152
+ parts.push(
153
+ mat(
154
+ {
155
+ id: `leg${side}`,
156
+ kind: 'lathe',
157
+ position: [sx * legX, HIP, 0],
158
+ profile: leg,
159
+ segments: 10,
160
+ },
161
+ feet,
162
+ bright,
163
+ ),
164
+ // The toe, so a foot points the way the bean faces.
165
+ mat(
166
+ {
167
+ id: `toe${side}`,
168
+ parent: `leg${side}`,
169
+ kind: 'sphere',
170
+ radius: 0.1,
171
+ position: [0, -HIP + 0.068, 0.06],
172
+ scale: [1.05, 0.68, 1.25],
173
+ segments: 6,
174
+ },
175
+ feet,
176
+ bright,
177
+ ),
178
+ );
179
+ }
180
+ for (const [side, sx] of [
181
+ ['L', 1],
182
+ ['R', -1],
183
+ ]) {
184
+ parts.push(
185
+ mat(
186
+ {
187
+ id: `arm${side}`,
188
+ parent: 'torso',
189
+ kind: 'lathe',
190
+ position: [sx * round(radius + 0.005), shoulder, 0.02],
191
+ rotation: [0, 0, sx * 20],
192
+ profile: arm,
193
+ segments: 8,
194
+ },
195
+ color,
196
+ bright,
197
+ ),
198
+ );
199
+ }
200
+ return parts;
201
+ }
202
+
203
+ // A round patch lying on the body, +z before `at` turns it: a cheek, a nose.
204
+ // A squashed sphere, so its edge sinks into the curve of the body.
205
+ function patch(id, parent, at, { r, wide = 1, tall = 1, segments = 8, mirror = true }, color) {
206
+ const part = {
207
+ id,
208
+ parent,
209
+ kind: 'sphere',
210
+ radius: r,
211
+ position: at.position,
212
+ rotation: at.rotation,
213
+ scale: [wide, tall, 0.3],
214
+ segments,
215
+ };
216
+ if (mirror) {
217
+ part.mirror = 'x';
218
+ }
219
+ return mat(part, color);
220
+ }
221
+
222
+ // Two eyes that blink together, `angle` degrees either side of the front and
223
+ // `y` above the torso pivot. With `white` each is a white oval carrying an ink
224
+ // pupil; without, an ink oval. `look` shifts the pupil inside the white. They
225
+ // hang from an empty part at their own height, which is what blinks: a mirror
226
+ // copy of a part that moves is drawn apart, a copy under one is not.
227
+ function eyes(at, headY, { angle, y, r, wide = 1, tall = 1, white = 0, look = [0, 0], every = 4 }) {
228
+ const outer = white || r;
229
+ const placed = at(angle, y, -0.004);
230
+ placed.position[1] = 0;
231
+ const parts = [
232
+ {
233
+ id: 'eyes',
234
+ parent: 'head',
235
+ kind: 'model',
236
+ position: [0, round(y - headY), 0],
237
+ motion: { blink: every },
238
+ },
239
+ patch('eye', 'eyes', placed, { r: outer, wide, tall }, white ? WHITE : INK),
240
+ ];
241
+ let lift = outer * 0.3;
242
+ if (white) {
243
+ parts.push(
244
+ mat(
245
+ {
246
+ id: 'pupil',
247
+ parent: 'eye',
248
+ kind: 'sphere',
249
+ radius: r,
250
+ position: [look[0], look[1], round(lift * 0.55)],
251
+ scale: [wide, tall, 0.3],
252
+ segments: 6,
253
+ },
254
+ INK,
255
+ ),
256
+ );
257
+ lift = lift * 0.55 + r * 0.3;
258
+ }
259
+ parts.push(
260
+ mat(
261
+ {
262
+ id: 'glint',
263
+ parent: 'eye',
264
+ kind: 'sphere',
265
+ radius: GLINT,
266
+ position: [
267
+ round(look[0] + r * wide * 0.28),
268
+ round(look[1] + r * tall * 0.36),
269
+ round(lift * 0.8),
270
+ ],
271
+ scale: [1, 1, 0.4],
272
+ segments: 6,
273
+ },
274
+ WHITE,
275
+ ),
276
+ );
277
+ return parts;
278
+ }
279
+
280
+ //
281
+ // 0: the Castle mascot. A flat-bottomed black body with a domed top, one
282
+ // almond eye with a big pupil, and the logo's white sheen down its side.
283
+ //
284
+
285
+ const MASCOT = { height: 1.17, radius: 0.4, sole: 0.33, corner: 0.07, dome: 0.4 };
286
+ // Where the eye looks out, above the torso pivot.
287
+ const MASCOT_EYE_Y = 0.65;
288
+
289
+ // The logo's sheen is the `head`: a sliver on the body's surface down its
290
+ // right side, round at the foot and pointed where it follows the dome. The
291
+ // eye hangs from it, so the idle look-around slides both round the body as
292
+ // one face.
293
+ function mascotSheen() {
294
+ const { height, radius, dome } = MASCOT;
295
+ const shoulder = height - dome;
296
+ const lift = 0.012;
297
+ const edge = -76;
298
+ // Each row is a height above the torso pivot, or an elevation on the dome,
299
+ // and where the sliver starts and how wide it is there, in degrees round
300
+ // the body.
301
+ const rows = [
302
+ { y: 0.12, from: 9, width: 0 },
303
+ { y: 0.14, from: 3, width: 14 },
304
+ { y: 0.19, from: 0, width: 22 },
305
+ { y: 0.3, from: 0, width: 25 },
306
+ { y: 0.5, from: 0, width: 25 },
307
+ { y: shoulder, from: 0, width: 24 },
308
+ { elevation: 20, from: 1, width: 22 },
309
+ { elevation: 38, from: 4, width: 18 },
310
+ { elevation: 54, from: 9, width: 11 },
311
+ { elevation: 66, from: 15, width: 0 },
312
+ ];
313
+ const vertices = [];
314
+ const faces = [];
315
+ for (const row of rows) {
316
+ const elevation = rad(row.elevation ?? 0);
317
+ const r = (radius + lift) * Math.cos(elevation);
318
+ const y = (row.y ?? shoulder + (dome + lift) * Math.sin(elevation)) - MASCOT_EYE_Y;
319
+ // The dome narrows, so a width there is widened to hold its size.
320
+ const widen = 1 / Math.max(0.4, Math.cos(elevation));
321
+ for (const turn of [row.from, row.from + row.width * widen]) {
322
+ const angle = rad(edge + turn);
323
+ vertices.push([round(r * Math.sin(angle)), round(y), round(r * Math.cos(angle))]);
324
+ }
325
+ }
326
+ for (let j = 0; j < rows.length - 1; j++) {
327
+ const a = j * 2;
328
+ faces.push([a, a + 1, a + 3, a + 2]);
329
+ }
330
+ return mat(
331
+ {
332
+ id: 'head',
333
+ parent: 'torso',
334
+ kind: 'mesh',
335
+ position: [0, MASCOT_EYE_Y, 0],
336
+ vertices,
337
+ faces,
338
+ },
339
+ WHITE,
340
+ );
341
+ }
342
+
343
+ // An almond wrapped round the front of the body. Its pivot is the head's, on
344
+ // the body's axis at the eye's height, so a blink closes it top to bottom.
345
+ function mascotEye() {
346
+ const columns = 10;
347
+ const halfWidth = 0.285;
348
+ const halfHeight = 0.15;
349
+ const outer = MASCOT.radius + 0.022;
350
+ const inner = MASCOT.radius - 0.05;
351
+ const vertices = [];
352
+ const faces = [];
353
+ // Per column: top and bottom on the outer shell, then the same on the inner.
354
+ for (let i = 0; i <= columns; i++) {
355
+ const u = -halfWidth + (2 * halfWidth * i) / columns;
356
+ const angle = u / outer;
357
+ const h = halfHeight * (1 - Math.abs(u / halfWidth) ** 2.2);
358
+ for (const r of [outer, inner]) {
359
+ vertices.push([round(r * Math.sin(angle)), round(h), round(r * Math.cos(angle))]);
360
+ vertices.push([round(r * Math.sin(angle)), round(-h), round(r * Math.cos(angle))]);
361
+ }
362
+ }
363
+ for (let i = 0; i < columns; i++) {
364
+ const a = i * 4;
365
+ const b = a + 4;
366
+ faces.push([a + 1, b + 1, b, a], [a, b, b + 2, a + 2], [b + 1, a + 1, a + 3, b + 3]);
367
+ }
368
+ const eye = mat({ id: 'eye', parent: 'head', kind: 'mesh', vertices, faces }, WHITE);
369
+ eye.motion = { blink: 5 };
370
+ return eye;
371
+ }
372
+
373
+ function mascot() {
374
+ const black = '#3a3644';
375
+ const front = MASCOT.radius + 0.022;
376
+ const parts = limbs({
377
+ color: black,
378
+ radius: MASCOT.radius,
379
+ shoulder: 0.52,
380
+ legX: 0.17,
381
+ bright: MASCOT_BRIGHT,
382
+ });
383
+ parts.push(
384
+ body(beanProfile(MASCOT), black, { bright: MASCOT_BRIGHT, segments: 20 }),
385
+ mascotSheen(),
386
+ mascotEye(),
387
+ mat(
388
+ {
389
+ id: 'pupil',
390
+ parent: 'eye',
391
+ kind: 'sphere',
392
+ radius: 0.125,
393
+ position: [0, 0, round(front - 0.012)],
394
+ scale: [1, 1, 0.3],
395
+ segments: 12,
396
+ },
397
+ INK,
398
+ ),
399
+ mat(
400
+ {
401
+ id: 'glint',
402
+ parent: 'eye',
403
+ kind: 'sphere',
404
+ radius: 0.042,
405
+ position: [0.048, 0.052, round(front + 0.02)],
406
+ scale: [1, 1, 0.4],
407
+ segments: 6,
408
+ },
409
+ WHITE,
410
+ ),
411
+ );
412
+ return { name: 'Castle', parts };
413
+ }
414
+
415
+ //
416
+ // 1: Miso, a cat: two ears, two tall eyes, and a tail with a cream tip.
417
+ //
418
+
419
+ function miso() {
420
+ const pink = '#ff4f86';
421
+ const deep = '#d93a70';
422
+ const cream = '#fff1cf';
423
+ const shape = { height: 1.1, radius: 0.385, corner: 0.26, top: 0.9, dome: 0.4 };
424
+ const profile = beanProfile(shape);
425
+ const headY = 0.67;
426
+ const at = surface(profile, headY);
427
+ const parts = limbs({ color: pink, feet: deep, radius: 0.375, shoulder: 0.53 });
428
+ const ear = [
429
+ [0.15, 0],
430
+ [0.13, 0.09],
431
+ [0.085, 0.19],
432
+ [0.035, 0.255],
433
+ [0, 0.27],
434
+ ];
435
+ // The tail leaves the back, curls up and leans to one side, so it shows as
436
+ // a tail from straight behind. Its id makes it sway, as its own batch. It is
437
+ // bent into an arc, and the tip sits on the end of that.
438
+ const tail = { length: 0.52, curl: 95 };
439
+ const tailProfile = [[0.055, 0]];
440
+ for (let k = 1; k <= 6; k++) {
441
+ tailProfile.push([0.07, round((tail.length * k) / 6)]);
442
+ }
443
+ const curl = rad(tail.curl);
444
+ const tailEnd = [
445
+ 0,
446
+ round((tail.length * Math.sin(curl)) / curl),
447
+ round((tail.length * (1 - Math.cos(curl))) / curl),
448
+ ];
449
+ parts.push(
450
+ body(profile, pink),
451
+ // The nose is the `head`: its pivot is on the axis and the nose sits out
452
+ // on the surface.
453
+ mat(
454
+ {
455
+ id: 'head',
456
+ parent: 'torso',
457
+ kind: 'sphere',
458
+ radius: 0.042,
459
+ position: [0, headY, 0],
460
+ offset: [0, -0.085, 0.372],
461
+ scale: [1.25, 0.8, 0.6],
462
+ segments: 6,
463
+ },
464
+ deep,
465
+ ),
466
+ ...eyes(at, headY, {
467
+ angle: 22,
468
+ y: headY + 0.02,
469
+ white: 0.1,
470
+ r: 0.068,
471
+ wide: 0.9,
472
+ tall: 1.3,
473
+ look: [-0.012, -0.012],
474
+ }),
475
+ mat(
476
+ {
477
+ id: 'ear',
478
+ parent: 'head',
479
+ kind: 'lathe',
480
+ position: [0.205, 0.29, 0],
481
+ rotation: [0, 0, -22],
482
+ scale: [1, 1, 0.6],
483
+ profile: ear,
484
+ segments: 8,
485
+ mirror: 'x',
486
+ },
487
+ pink,
488
+ ),
489
+ mat(
490
+ {
491
+ id: 'earIn',
492
+ parent: 'ear',
493
+ kind: 'lathe',
494
+ position: [0, 0.03, 0.045],
495
+ scale: [1, 1, 0.45],
496
+ profile: ear.map(([r, y]) => [round(r * 0.6), round(y * 0.62)]),
497
+ segments: 6,
498
+ },
499
+ cream,
500
+ ),
501
+ mat(
502
+ {
503
+ id: 'tail',
504
+ parent: 'torso',
505
+ kind: 'lathe',
506
+ position: [0, 0.2, -0.3],
507
+ rotation: [-75, 0, 28],
508
+ bend: tail.curl,
509
+ profile: tailProfile,
510
+ segments: 8,
511
+ },
512
+ pink,
513
+ ),
514
+ mat(
515
+ {
516
+ id: 'tailTip',
517
+ parent: 'tail',
518
+ kind: 'sphere',
519
+ radius: 0.1,
520
+ position: tailEnd,
521
+ segments: 10,
522
+ },
523
+ cream,
524
+ ),
525
+ );
526
+ return { name: 'Miso', parts };
527
+ }
528
+
529
+ //
530
+ // 2: Sprout, a tall bulb with two leaves growing from its top.
531
+ //
532
+
533
+ function sprout() {
534
+ const blue = '#2f7dff';
535
+ const deep = '#2460d6';
536
+ const leaf = '#5fdc4a';
537
+ const shape = { height: 1.15, radius: 0.35, corner: 0.3, top: 0.74, dome: 0.34 };
538
+ const profile = beanProfile(shape);
539
+ const headY = 0.68;
540
+ const at = surface(profile, headY);
541
+ const top = shape.height - headY;
542
+ const blade = [
543
+ [0, 0],
544
+ [0.075, 0.05],
545
+ [0.12, 0.15],
546
+ [0.105, 0.27],
547
+ [0.045, 0.36],
548
+ [0, 0.385],
549
+ ];
550
+ const parts = limbs({ color: blue, feet: deep, radius: 0.34, shoulder: 0.54, legX: 0.15 });
551
+ parts.push(
552
+ body(profile, blue, { segments: 20 }),
553
+ // The stem is the `head`, so the leaves turn with the face.
554
+ mat(
555
+ {
556
+ id: 'head',
557
+ parent: 'torso',
558
+ kind: 'cylinder',
559
+ radius: 0.034,
560
+ height: 0.16,
561
+ position: [0, headY, 0],
562
+ offset: [0, round(top + 0.04), 0],
563
+ segments: 6,
564
+ },
565
+ leaf,
566
+ ),
567
+ mat(
568
+ {
569
+ id: 'smile',
570
+ parent: 'head',
571
+ kind: 'torus',
572
+ radius: 0.05,
573
+ tube: 0.017,
574
+ arc: 180,
575
+ ...at(0, headY - 0.03, 0.002),
576
+ segments: 8,
577
+ },
578
+ INK,
579
+ ),
580
+ ...eyes(at, headY, { angle: 25, y: headY + 0.03, r: 0.07, tall: 1.2 }),
581
+ mat(
582
+ {
583
+ id: 'leafBig',
584
+ parent: 'head',
585
+ kind: 'lathe',
586
+ position: [0.01, round(top + 0.09), 0],
587
+ rotation: [0, 0, -52],
588
+ scale: [1, 1, 0.3],
589
+ profile: blade,
590
+ segments: 8,
591
+ },
592
+ leaf,
593
+ ),
594
+ mat(
595
+ {
596
+ id: 'leafSmall',
597
+ parent: 'head',
598
+ kind: 'lathe',
599
+ position: [-0.01, round(top + 0.09), 0],
600
+ rotation: [0, 0, 58],
601
+ scale: [1, 1, 0.3],
602
+ profile: blade.map(([r, y]) => [round(r * 0.78), round(y * 0.74)]),
603
+ segments: 8,
604
+ },
605
+ leaf,
606
+ ),
607
+ );
608
+ parts.find((part) => part.id === 'smile').rotation[2] = 180;
609
+ return { name: 'Sprout', parts };
610
+ }
611
+
612
+ //
613
+ // 3: Pip, a chick in a backwards cap.
614
+ //
615
+
616
+ function pip() {
617
+ const yellow = '#ffc61a';
618
+ const orange = '#ff7a1f';
619
+ const purple = '#8a4dff';
620
+ const pinkish = '#ff7d8e';
621
+ const shape = { height: 1.03, radius: 0.43, corner: 0.3, top: 0.94, dome: 0.42 };
622
+ const profile = beanProfile(shape);
623
+ const headY = 0.52;
624
+ const at = surface(profile, headY);
625
+ const rim = 0.71;
626
+ // The cap is the body's own dome, a little larger, from the rim up.
627
+ const cap = [[round(at(0, rim).position[2] - 0.01), rim - 0.03]];
628
+ for (const [r, y] of profile) {
629
+ if (y > rim) {
630
+ cap.push([round(r === 0 ? 0 : r + 0.03), round(y + 0.03)]);
631
+ }
632
+ }
633
+ cap.splice(1, 0, [round(at(0, rim).position[2] + 0.032), rim]);
634
+ const parts = limbs({ color: yellow, feet: orange, radius: 0.42, shoulder: 0.47, legX: 0.18 });
635
+ parts.push(
636
+ body(profile, yellow),
637
+ mat({ id: 'cap', parent: 'torso', kind: 'lathe', profile: cap, segments: 16 }, purple),
638
+ mat(
639
+ {
640
+ id: 'button',
641
+ parent: 'torso',
642
+ kind: 'sphere',
643
+ radius: 0.06,
644
+ position: [0, shape.height + 0.04, 0],
645
+ scale: [1, 0.7, 1],
646
+ segments: 6,
647
+ },
648
+ yellow,
649
+ ),
650
+ mat(
651
+ {
652
+ id: 'tuft',
653
+ parent: 'torso',
654
+ kind: 'sphere',
655
+ radius: 0.1,
656
+ position: [0, 0.22, -0.42],
657
+ rotation: [-35, 0, 0],
658
+ scale: [0.9, 0.75, 1.5],
659
+ segments: 8,
660
+ },
661
+ yellow,
662
+ ),
663
+ // The beak is the `head`.
664
+ mat(
665
+ {
666
+ id: 'head',
667
+ parent: 'torso',
668
+ kind: 'sphere',
669
+ radius: 0.085,
670
+ position: [0, headY, 0],
671
+ offset: [0, -0.04, 0.43],
672
+ scale: [1.15, 0.62, 0.9],
673
+ segments: 8,
674
+ },
675
+ orange,
676
+ ),
677
+ ...eyes(at, headY, { angle: 27, y: headY + 0.02, r: 0.064, tall: 1.1 }),
678
+ patch(
679
+ 'cheek',
680
+ 'head',
681
+ at(40, headY - 0.07, -0.006),
682
+ { r: 0.06, tall: 0.7, segments: 6 },
683
+ pinkish,
684
+ ),
685
+ // The cap is on backwards: the brim shows from behind, where a player
686
+ // sees their own bean, and leaves the face clear from above.
687
+ mat(
688
+ {
689
+ id: 'brim',
690
+ parent: 'torso',
691
+ kind: 'sphere',
692
+ radius: 0.24,
693
+ position: [0, rim - 0.005, -0.34],
694
+ rotation: [-10, 0, 0],
695
+ scale: [1.2, 0.16, 1],
696
+ segments: 10,
697
+ },
698
+ purple,
699
+ ),
700
+ );
701
+ return { name: 'Pip', parts };
702
+ }
703
+
704
+ //
705
+ // Writing
706
+ //
707
+
708
+ // A lathe draws both sides, and a batch splits where its parts differ in
709
+ // that, so every part sharing a batch with one is drawn two-sided too. A
710
+ // batch is a walk part, or a part with a motion of its own, and everything
711
+ // under it.
712
+ function evenBatches(parts) {
713
+ const byId = new Map(parts.map((part) => [part.id, part]));
714
+ const anchorOf = (part) => {
715
+ let at = part;
716
+ while (!ANCHORS.includes(at.id) && !at.motion) {
717
+ at = byId.get(at.parent);
718
+ }
719
+ return at.id;
720
+ };
721
+ const twoSided = (part) => part.kind === 'lathe';
722
+ const both = new Set(parts.filter(twoSided).map(anchorOf));
723
+ for (const part of parts) {
724
+ if (both.has(anchorOf(part)) && !twoSided(part)) {
725
+ part.doubleSided = true;
726
+ }
727
+ }
728
+ }
729
+
730
+ function write(index, { name, parts }) {
731
+ evenBatches(parts);
732
+ const lines = [
733
+ '{',
734
+ ' "format": "pxmodel",',
735
+ ` "name": ${JSON.stringify(name)},`,
736
+ ' "triangles": 1500,',
737
+ ` "look": ${JSON.stringify(LOOK)},`,
738
+ ' "parts": [',
739
+ parts.map((part) => ` ${JSON.stringify(part)}`).join(',\n'),
740
+ ' ]',
741
+ '}',
742
+ '',
743
+ ];
744
+ fs.writeFileSync(path.join(OUT_DIR, `avatar-${index}.pxmodel`), lines.join('\n'));
745
+ }
746
+
747
+ fs.mkdirSync(OUT_DIR, { recursive: true });
748
+ [mascot, miso, sprout, pip].forEach((make, index) => write(index, make()));