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,607 @@
1
+ // Writes the kit's `blocky` avatar set, `models/blocky/avatar-0.pxmodel` to
2
+ // `avatar-3.pxmodel`, and the faces printed on them, `drawings/blocky/*.sprite`:
3
+ // the Castle mascot, Tabby, Pom and Dash as plastic block figures. Run from the
4
+ // kit: `node scripts/gen-avatars-blocky.mjs` (or with a directory to write the
5
+ // models into and a second for the drawings). The model and sprite files are
6
+ // this script's output; change a character here and run it again.
7
+ //
8
+ // What every avatar keeps, because the kit and the phone budget depend on it:
9
+ // - part ids `torso`, `head`, `legL`, `legR`, `armL`, `armR` (the walk moves
10
+ // them), head and arms under `torso`, feet on y = 0, facing +z;
11
+ // - at most 1,500 triangles and 8 draw calls: six batches (torso, head, two
12
+ // legs, two arms) and the printed face, the one textured part. There is no
13
+ // outline: each would draw its batch again, and the edges are shown by the
14
+ // bevels instead;
15
+ // - no glow.
16
+
17
+ import fs from 'node:fs';
18
+ import path from 'node:path';
19
+ import { fileURLToPath } from 'node:url';
20
+
21
+ const KIT_DIR = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
22
+ const MODEL_DIR = process.argv[2]
23
+ ? path.resolve(process.argv[2])
24
+ : path.join(KIT_DIR, 'models', 'blocky');
25
+ const ART_DIR = process.argv[3]
26
+ ? path.resolve(process.argv[3])
27
+ : path.join(KIT_DIR, 'drawings', 'blocky');
28
+ // A model names its art from the root of the deck that imports the kit.
29
+ const ART_REF = 'imports/castle.hub-game/drawings/blocky';
30
+
31
+ const INK = '#1b1730';
32
+ const WHITE = '#ffffff';
33
+ const LOOK = { outline: 0, rim: 0.3, ao: 0.3 };
34
+ // Glossy plastic, the same on every part.
35
+ const PLASTIC = { roughness: 0.3, clearcoat: 0.6 };
36
+ // Colours come out darker than written under the engine's lights; this
37
+ // multiplies every colour.
38
+ const BRIGHT = 2.2;
39
+ // The mascot's black is lifted less, so it stays black.
40
+ const MASCOT_BRIGHT = 1.7;
41
+
42
+ // The leg pivot above the feet; the torso's own pivot sits there too.
43
+ const HIP = 0.44;
44
+ // The bevel on a body block, in metres.
45
+ const BEVEL = 0.035;
46
+
47
+ const round = (n) => Math.round(n * 1000) / 1000;
48
+ const rad = (d) => (d * Math.PI) / 180;
49
+
50
+ // A colour with each channel multiplied by `by`.
51
+ function shade(color, by) {
52
+ const channels = [1, 3, 5].map((at) => parseInt(color.slice(at, at + 2), 16));
53
+ return `#${channels
54
+ .map((c) =>
55
+ Math.round(Math.min(255, c * by))
56
+ .toString(16)
57
+ .padStart(2, '0'),
58
+ )
59
+ .join('')}`;
60
+ }
61
+
62
+ function mat(part, color, bright = BRIGHT) {
63
+ part.color = color;
64
+ Object.assign(part, PLASTIC);
65
+ if (bright !== 1) {
66
+ part.brightness = bright;
67
+ }
68
+ return part;
69
+ }
70
+
71
+ //
72
+ // Blocks
73
+ //
74
+
75
+ // A convex outline (counter-clockwise, [x, y]) pushed `depth` along z and
76
+ // centred on z = 0, with the rim of both ends bevelled by `bevel`: a moulded
77
+ // plastic piece, flat shaded. `up: true` stands it on end instead, the
78
+ // outline seen from above ([x, -z]) and `depth` along y.
79
+ function slab(outline, depth, bevel, { up = false } = {}) {
80
+ const n = outline.length;
81
+ const inner = outline.map(([x, y], i) => {
82
+ const [px, py] = outline[(i + n - 1) % n];
83
+ const [nx, ny] = outline[(i + 1) % n];
84
+ // Inward normals of the two edges that meet here.
85
+ const a = unit([-(y - py), x - px]);
86
+ const b = unit([-(ny - y), nx - x]);
87
+ const k = bevel / (1 + a[0] * b[0] + a[1] * b[1]);
88
+ return [x + (a[0] + b[0]) * k, y + (a[1] + b[1]) * k];
89
+ });
90
+ const half = depth / 2;
91
+ // Four rings: front cap, front rim, back rim, back cap.
92
+ const rings = [
93
+ [inner, half],
94
+ [outline, half - bevel],
95
+ [outline, bevel - half],
96
+ [inner, -half],
97
+ ];
98
+ const vertices = [];
99
+ for (const [ring, z] of rings) {
100
+ for (const [x, y] of ring) {
101
+ vertices.push(up ? [round(x), round(z), round(-y)] : [round(x), round(y), round(z)]);
102
+ }
103
+ }
104
+ const faces = [];
105
+ faces.push(outline.map((_, i) => i));
106
+ for (let r = 0; r < 3; r++) {
107
+ for (let i = 0; i < n; i++) {
108
+ const j = (i + 1) % n;
109
+ faces.push([r * n + i, (r + 1) * n + i, (r + 1) * n + j, r * n + j]);
110
+ }
111
+ }
112
+ faces.push(outline.map((_, i) => 3 * n + (n - 1 - i)));
113
+ return { kind: 'mesh', vertices, faces };
114
+ }
115
+
116
+ function unit([x, y]) {
117
+ const length = Math.hypot(x, y);
118
+ return [x / length, y / length];
119
+ }
120
+
121
+ // A rectangle with its four corners cut by `cut`.
122
+ function cutRect(w, h, cut) {
123
+ const x = w / 2;
124
+ const y = h / 2;
125
+ return [
126
+ [x, -y + cut],
127
+ [x, y - cut],
128
+ [x - cut, y],
129
+ [-x + cut, y],
130
+ [-x, y - cut],
131
+ [-x, -y + cut],
132
+ [-x + cut, -y],
133
+ [x - cut, -y],
134
+ ];
135
+ }
136
+
137
+ // A block of `size` with every edge bevelled.
138
+ function block(size, bevel = BEVEL) {
139
+ const [w, h, d] = size;
140
+ const cut = Math.min(bevel, Math.min(w, h, d) * 0.35);
141
+ return slab(cutRect(w, h, cut), d, cut);
142
+ }
143
+
144
+ // A block with only its four upright edges bevelled: a limb.
145
+ function post(size, bevel = BEVEL) {
146
+ const [w, h, d] = size;
147
+ return slab(cutRect(w, d, Math.min(bevel, Math.min(w, d) * 0.35)), h, 0, { up: true });
148
+ }
149
+
150
+ // A plain box: small trim, twelve triangles.
151
+ function box(size) {
152
+ return { size };
153
+ }
154
+
155
+ function part(id, parent, shape, position, color, extra = {}) {
156
+ const { bright, ...rest } = extra;
157
+ return mat({ id, ...(parent ? { parent } : {}), ...shape, position, ...rest }, color, bright);
158
+ }
159
+
160
+ //
161
+ // The body every avatar shares: block legs with a big shoe, block arms with a
162
+ // mitten block. `shoulder` is [x, y] from the torso pivot.
163
+ //
164
+
165
+ const ARM = [0.15, 0.3, 0.19];
166
+ const HAND = [0.17, 0.14, 0.21];
167
+
168
+ function limbs({ leg, shoe, sleeve, hand, shoulder, legX = 0.12, bright = BRIGHT }) {
169
+ const parts = [];
170
+ for (const [side, sx] of [
171
+ ['L', 1],
172
+ ['R', -1],
173
+ ]) {
174
+ parts.push(
175
+ part(`leg${side}`, null, post([0.21, 0.34, 0.24]), [sx * legX, HIP, 0], leg, {
176
+ offset: [0, -0.15, 0],
177
+ bright,
178
+ }),
179
+ part(
180
+ `shoe${side}`,
181
+ `leg${side}`,
182
+ block([0.235, 0.14, 0.35], 0.04),
183
+ [sx * 0.005, 0.07 - HIP, 0.045],
184
+ shoe,
185
+ { bright },
186
+ ),
187
+ );
188
+ }
189
+ for (const [side, sx] of [
190
+ ['L', 1],
191
+ ['R', -1],
192
+ ]) {
193
+ parts.push(
194
+ part(`arm${side}`, 'torso', post(ARM), [sx * shoulder[0], shoulder[1], 0], sleeve, {
195
+ rotation: [0, 0, sx * 7],
196
+ offset: [0, 0.04 - ARM[1] / 2, 0],
197
+ bright,
198
+ }),
199
+ part(
200
+ `hand${side}`,
201
+ `arm${side}`,
202
+ block(HAND, 0.04),
203
+ [0, 0.04 - ARM[1] - HAND[1] / 2 + 0.01, 0],
204
+ hand,
205
+ { bright },
206
+ ),
207
+ );
208
+ }
209
+ return parts;
210
+ }
211
+
212
+ // The printed face: a thin plate on the front of the head, clear wherever
213
+ // nothing is drawn. `at` is the plate's centre from the head pivot.
214
+ function facePlate(file, size, at, color, bright = BRIGHT) {
215
+ return part('face', 'head', box([size[0], size[1], 0.004]), at, color, {
216
+ art: { front: `${ART_REF}/${file}` },
217
+ bright,
218
+ });
219
+ }
220
+
221
+ //
222
+ // A person: block torso and hips, a big block head with a printed face.
223
+ //
224
+
225
+ const TORSO = [0.5, 0.4, 0.29];
226
+ const HEAD = [0.58, 0.48, 0.5];
227
+ const HEAD_BEVEL = 0.07;
228
+ // The head's pivot (its underside) above the torso pivot.
229
+ const NECK = 0.5;
230
+
231
+ function person({ skin, shirt, pants, shoe, leg = pants, hand = skin, belt = pants, face }) {
232
+ const parts = limbs({ leg, shoe, sleeve: shirt, hand, shoulder: [0.33, 0.44] });
233
+ parts.push(
234
+ part('torso', null, block(TORSO), [0, HIP, 0], shirt, { offset: [0, 0.29, 0] }),
235
+ part('hips', 'torso', box([0.5, 0.12, 0.29]), [0, 0.04, 0], belt),
236
+ part('head', 'torso', block(HEAD, HEAD_BEVEL), [0, NECK, 0], skin, {
237
+ offset: [0, HEAD[1] / 2, 0],
238
+ }),
239
+ facePlate(
240
+ face,
241
+ [HEAD[0] - 2 * HEAD_BEVEL, HEAD[1] - 2 * HEAD_BEVEL],
242
+ [0, HEAD[1] / 2, HEAD[2] / 2],
243
+ skin,
244
+ ),
245
+ );
246
+ return parts;
247
+ }
248
+
249
+ //
250
+ // Faces: a small canvas of palette letters, drawn with shapes in a 32 x 24
251
+ // space (y down) and written at `FACE_SCALE` times that.
252
+ //
253
+
254
+ const FACE_SCALE = 2;
255
+ const FACE_W = 32;
256
+ const FACE_H = 24;
257
+
258
+ function canvas() {
259
+ const w = FACE_W * FACE_SCALE;
260
+ const h = FACE_H * FACE_SCALE;
261
+ const grid = Array.from({ length: h }, () => Array(w).fill('s'));
262
+ const paint = (inside, key) => {
263
+ for (let j = 0; j < h; j++) {
264
+ for (let i = 0; i < w; i++) {
265
+ if (inside((i + 0.5) / FACE_SCALE, (j + 0.5) / FACE_SCALE)) {
266
+ grid[j][i] = key;
267
+ }
268
+ }
269
+ }
270
+ };
271
+ const cv = {
272
+ grid,
273
+ ellipse(cx, cy, rx, ry, key) {
274
+ paint((x, y) => ((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 <= 1, key);
275
+ return cv;
276
+ },
277
+ rect(x0, y0, x1, y1, key) {
278
+ paint((x, y) => x >= x0 && x <= x1 && y >= y0 && y <= y1, key);
279
+ return cv;
280
+ },
281
+ // The lower part of a ring: a smile. `from` is the y it starts at.
282
+ smile(cx, cy, r, thick, from, key) {
283
+ paint((x, y) => {
284
+ const d = Math.hypot(x - cx, y - cy);
285
+ return y >= from && d <= r && d >= r - thick;
286
+ }, key);
287
+ return cv;
288
+ },
289
+ // The lower part of a disc: an open mouth.
290
+ bowl(cx, cy, rx, ry, key) {
291
+ paint((x, y) => y >= cy && ((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 <= 1, key);
292
+ return cv;
293
+ },
294
+ // Where two discs overlap: an almond.
295
+ almond(cx, cy, halfWidth, halfHeight, key) {
296
+ const r = (halfWidth ** 2 + halfHeight ** 2) / (2 * halfHeight);
297
+ const lift = r - halfHeight;
298
+ paint(
299
+ (x, y) =>
300
+ Math.hypot(x - cx, y - (cy + lift)) <= r && Math.hypot(x - cx, y - (cy - lift)) <= r,
301
+ key,
302
+ );
303
+ return cv;
304
+ },
305
+ };
306
+ return cv;
307
+ }
308
+
309
+ const MID = FACE_W / 2;
310
+
311
+ // Two tall ink eyes with a white glint each.
312
+ function eyes(cv, { y, dx = 7.5, rx = 3.5, ry = 5 }) {
313
+ for (const s of [-1, 1]) {
314
+ cv.ellipse(MID + s * dx, y, rx, ry, 'k');
315
+ cv.ellipse(MID + s * dx - rx * 0.3, y - ry * 0.45, rx * 0.42, rx * 0.42, 'w');
316
+ }
317
+ return cv;
318
+ }
319
+
320
+ function writeFace(file, cv, palette) {
321
+ const sprite = {
322
+ format: 'compact',
323
+ palette: { k: INK, w: WHITE, ...palette },
324
+ grid: cv.grid.map((row) => row.join('')),
325
+ };
326
+ fs.writeFileSync(path.join(ART_DIR, file), `${JSON.stringify(sprite, null, 1)}\n`);
327
+ }
328
+
329
+ //
330
+ // 0: the Castle mascot as a block figure. The logo's shape cut from black
331
+ // plastic: a slab with an arched top, one almond eye printed on it, and the
332
+ // white sheen down its side. The arched part is the head and turns on a low
333
+ // block that carries the arms.
334
+ //
335
+
336
+ const MASCOT_R = 0.35;
337
+ const MASCOT_DEPTH = 0.44;
338
+ const MASCOT_BEVEL = 0.05;
339
+ // The lower block, from its underside to the seam, and the head's straight
340
+ // side above the seam.
341
+ const MASCOT_BASE = 0.28;
342
+ const MASCOT_SIDE = 0.46;
343
+ const MASCOT_ARCH = 6;
344
+
345
+ function mascotArch() {
346
+ const outline = [
347
+ [-MASCOT_R, 0],
348
+ [MASCOT_R, 0],
349
+ ];
350
+ for (let k = 0; k <= MASCOT_ARCH; k++) {
351
+ const t = (Math.PI * k) / MASCOT_ARCH;
352
+ outline.push([MASCOT_R * Math.cos(t), MASCOT_SIDE + MASCOT_R * Math.sin(t)]);
353
+ }
354
+ return outline;
355
+ }
356
+
357
+ // The logo's sheen: a white strip on the flat of the head down one side,
358
+ // from just above the seam and following the arch to a point. `z` is the face
359
+ // it lies on, front (+1) or back (-1).
360
+ function mascotSheen(z) {
361
+ const lift = 0.006;
362
+ const edge = MASCOT_R - MASCOT_BEVEL - 0.035;
363
+ const face = (MASCOT_DEPTH / 2 + lift) * z;
364
+ // [height above the seam, outer x, width, the way the width runs]
365
+ const rows = [
366
+ [0.03, edge - 0.03, 0, [1, 0]],
367
+ [0.1, edge, 0.09, [1, 0]],
368
+ ];
369
+ for (const [degrees, width] of [
370
+ [0, 0.1],
371
+ [20, 0.09],
372
+ [40, 0.085],
373
+ [58, 0.055],
374
+ [74, 0],
375
+ ]) {
376
+ const t = rad(degrees);
377
+ rows.push([
378
+ MASCOT_SIDE + edge * Math.sin(t),
379
+ edge * Math.cos(t),
380
+ width,
381
+ [Math.cos(t), -Math.sin(t)],
382
+ ]);
383
+ }
384
+ const vertices = [];
385
+ const faces = [];
386
+ for (const [y, outer, width, inward] of rows) {
387
+ vertices.push(
388
+ [round(-outer), round(y), round(face)],
389
+ [round(-outer + inward[0] * width), round(y + inward[1] * width), round(face)],
390
+ );
391
+ }
392
+ for (let j = 0; j < rows.length - 1; j++) {
393
+ const k = j * 2;
394
+ faces.push(z > 0 ? [k, k + 1, k + 3, k + 2] : [k, k + 2, k + 3, k + 1]);
395
+ }
396
+ const id = z > 0 ? 'sheenFront' : 'sheenBack';
397
+ return mat({ id, parent: 'head', kind: 'mesh', vertices, faces }, WHITE);
398
+ }
399
+
400
+ function mascot() {
401
+ const body = '#2e2b38';
402
+ // The plate is lit like the white of the eye, so its black is written darker
403
+ // to match the body's.
404
+ const plate = shade(body, MASCOT_BRIGHT / BRIGHT);
405
+ // The torso pivot is the hip; the lower block starts a little under it.
406
+ const under = -0.08;
407
+ const seam = under + MASCOT_BASE;
408
+ const parts = limbs({
409
+ leg: body,
410
+ shoe: body,
411
+ sleeve: body,
412
+ hand: WHITE,
413
+ shoulder: [MASCOT_R + 0.085, seam - 0.06],
414
+ legX: 0.14,
415
+ bright: MASCOT_BRIGHT,
416
+ });
417
+ parts.find((each) => each.id === 'handL').brightness = BRIGHT;
418
+ parts.find((each) => each.id === 'handR').brightness = BRIGHT;
419
+ parts.push(
420
+ part(
421
+ 'torso',
422
+ null,
423
+ block([MASCOT_R * 2, MASCOT_BASE, MASCOT_DEPTH], MASCOT_BEVEL),
424
+ [0, HIP, 0],
425
+ body,
426
+ { offset: [0, under + MASCOT_BASE / 2, 0], bright: MASCOT_BRIGHT },
427
+ ),
428
+ part('head', 'torso', slab(mascotArch(), MASCOT_DEPTH, MASCOT_BEVEL), [0, seam, 0], body, {
429
+ bright: MASCOT_BRIGHT,
430
+ }),
431
+ facePlate('castle-eye.sprite', [0.56, 0.42], [0.05, 0.4, MASCOT_DEPTH / 2], plate),
432
+ mascotSheen(1),
433
+ mascotSheen(-1),
434
+ );
435
+
436
+ const cv = canvas();
437
+ cv.almond(MID, 12, 12.6, 7, 'w')
438
+ .ellipse(MID, 12, 5.6, 5.6, 'k')
439
+ .ellipse(18.2, 9.8, 1.7, 1.7, 'w');
440
+ writeFace('castle-eye.sprite', cv, { s: plate });
441
+ return { name: 'Castle', parts };
442
+ }
443
+
444
+ //
445
+ // 1: Tabby, in a cat hoodie: ears, a cream belly, a tail.
446
+ //
447
+
448
+ function tabby() {
449
+ const fur = '#ff8a14';
450
+ const stripe = '#d9560a';
451
+ const cream = '#ffe9b0';
452
+ const skin = '#ffcf9e';
453
+ const pink = '#ff6f91';
454
+ const parts = person({
455
+ skin,
456
+ shirt: fur,
457
+ pants: '#34418c',
458
+ shoe: cream,
459
+ hand: cream,
460
+ face: 'tabby.sprite',
461
+ });
462
+ const [w, h, d] = HEAD;
463
+ const ear = [
464
+ [-0.11, 0],
465
+ [0.11, 0],
466
+ [0, 0.2],
467
+ ];
468
+ parts.push(
469
+ part('hoodTop', 'head', block([w + 0.08, 0.17, d + 0.08], 0.055), [0, h - 0.03, -0.02], fur),
470
+ part('hoodBack', 'head', box([w + 0.08, h - 0.1, 0.12]), [0, h / 2 - 0.04, -d / 2 - 0.01], fur),
471
+ part('hoodSide', 'head', box([0.07, h - 0.1, d - 0.08]), [w / 2, h / 2 - 0.04, -0.05], fur, {
472
+ mirror: 'x',
473
+ }),
474
+ part('ear', 'head', slab(ear, 0.16, 0.03), [0.2, h + 0.04, -0.02], fur, { mirror: 'x' }),
475
+ part('earIn', 'head', slab(ear, 0.03, 0.005), [0.2, h + 0.05, 0.062], pink, {
476
+ scale: [0.55, 0.55, 1],
477
+ mirror: 'x',
478
+ }),
479
+ part('belly', 'torso', box([0.3, 0.26, 0.03]), [0, 0.27, TORSO[2] / 2], cream),
480
+ part('tailA', 'torso', box([0.11, 0.11, 0.26]), [0, 0.1, -0.25], fur),
481
+ part('stripe', 'head', box([0.3, 0.06, 0.02]), [0, h / 2 + 0.08, -d / 2 - 0.07], stripe, {
482
+ repeat: { count: 3, offset: [0, -0.11, 0] },
483
+ }),
484
+ part('tailB', 'torso', block([0.13, 0.24, 0.13], 0.03), [0, 0.165, -0.36], fur),
485
+ part('tailTip', 'torso', block([0.15, 0.13, 0.15], 0.03), [0, 0.33, -0.36], cream),
486
+ );
487
+
488
+ const cv = eyes(canvas(), { y: 10.5 });
489
+ for (const s of [-1, 1]) {
490
+ cv.smile(MID + s * 2.8, 17.4, 3.4, 1.6, 18, 'k');
491
+ cv.rect(MID + s * 13.6 - 2, 16.2, MID + s * 13.6 + 2, 17.2, 'k');
492
+ cv.rect(MID + s * 13.6 - 2, 19.2, MID + s * 13.6 + 2, 20.2, 'k');
493
+ }
494
+ cv.rect(MID - 1.8, 15, MID + 1.8, 16.4, 'p').rect(MID - 0.9, 16.4, MID + 0.9, 17.4, 'p');
495
+ writeFace('tabby.sprite', cv, { s: skin, p: pink });
496
+ return { name: 'Tabby', parts };
497
+ }
498
+
499
+ //
500
+ // 2: Pom, with two big pigtails and a bow.
501
+ //
502
+
503
+ function pom() {
504
+ const hair = '#ff4f9a';
505
+ const skin = '#a5643c';
506
+ const teal = '#12d1c0';
507
+ const gold = '#ffc61a';
508
+ const parts = person({
509
+ skin,
510
+ leg: skin,
511
+ shirt: '#ff6fb0',
512
+ pants: teal,
513
+ shoe: gold,
514
+ face: 'pom.sprite',
515
+ });
516
+ const [w, h, d] = HEAD;
517
+ parts.push(
518
+ part('skirt', 'torso', block([0.58, 0.17, 0.4], 0.04), [0, 0.03, 0], teal),
519
+ part('hairTop', 'head', block([w + 0.06, 0.17, d + 0.06], 0.055), [0, h - 0.035, -0.01], hair),
520
+ part('hairBack', 'head', box([w + 0.06, h + 0.04, 0.12]), [0, h / 2 - 0.08, -d / 2], hair),
521
+ part('fringe', 'head', box([w - 0.1, 0.07, 0.04]), [0, h - 0.12, d / 2], hair),
522
+ part('pigtail', 'head', block([0.19, 0.34, 0.23], 0.05), [w / 2 + 0.105, 0.18, -0.06], hair, {
523
+ mirror: 'x',
524
+ }),
525
+ part('tie', 'head', box([0.195, 0.05, 0.235]), [w / 2 + 0.105, 0.27, -0.06], gold, {
526
+ mirror: 'x',
527
+ }),
528
+ part('bow', 'head', block([0.2, 0.15, 0.09], 0.03), [0.115, h + 0.09, -0.08], gold, {
529
+ rotation: [0, 0, 20],
530
+ mirror: 'x',
531
+ }),
532
+ part('knot', 'head', box([0.08, 0.1, 0.11]), [0, h + 0.075, -0.08], gold),
533
+ );
534
+
535
+ const cv = eyes(canvas(), { y: 10.5 });
536
+ for (const s of [-1, 1]) {
537
+ cv.ellipse(MID + s * 12.2, 17.2, 2.6, 1.5, 'b');
538
+ }
539
+ cv.bowl(MID, 16.6, 4.4, 4.6, 'k').ellipse(MID, 20.2, 2.4, 1.2, 'b');
540
+ writeFace('pom.sprite', cv, { s: skin, b: '#ff7d9a' });
541
+ return { name: 'Pom', parts };
542
+ }
543
+
544
+ //
545
+ // 3: Dash, in a cap, with a backpack.
546
+ //
547
+
548
+ function dash() {
549
+ const blue = '#1f7bff';
550
+ const navy = '#1746b8';
551
+ const skin = '#ffb877';
552
+ const hair = '#6b3a1f';
553
+ const gold = '#ffc61a';
554
+ const parts = person({
555
+ skin,
556
+ shirt: blue,
557
+ pants: '#2b3566',
558
+ shoe: '#fbfbf6',
559
+ face: 'dash.sprite',
560
+ });
561
+ const [w, h, d] = HEAD;
562
+ parts.push(
563
+ part('capDome', 'head', block([w + 0.06, 0.19, d + 0.06], 0.06), [0, h - 0.035, -0.005], blue),
564
+ part('capBrim', 'head', block([0.48, 0.05, 0.22], 0.02), [0, h - 0.07, d / 2 + 0.1], navy, {
565
+ rotation: [10, 0, 0],
566
+ }),
567
+ part('capButton', 'head', box([0.09, 0.04, 0.09]), [0, h + 0.07, 0], navy),
568
+ part('hairBack', 'head', box([w + 0.03, 0.2, 0.08]), [0, h / 2 + 0.03, -d / 2 + 0.01], hair),
569
+ part('pack', 'torso', block([0.4, 0.4, 0.2], 0.045), [0, 0.3, -0.24], gold),
570
+ part('packPocket', 'torso', block([0.26, 0.17, 0.07], 0.02), [0, 0.22, -0.36], '#ff8a1c'),
571
+ part('strap', 'torso', box([0.07, 0.3, TORSO[2] + 0.02]), [0.15, 0.35, 0], gold, {
572
+ mirror: 'x',
573
+ }),
574
+ );
575
+
576
+ const cv = eyes(canvas(), { y: 11 });
577
+ cv.smile(MID, 13.5, 7.5, 1.9, 18, 'k');
578
+ for (const s of [-1, 1]) {
579
+ cv.rect(MID + s * 7.5 - 3, 3.8, MID + s * 7.5 + 3, 5.2, 'h');
580
+ }
581
+ writeFace('dash.sprite', cv, { s: skin, h: hair });
582
+ return { name: 'Dash', parts };
583
+ }
584
+
585
+ //
586
+ // Writing
587
+ //
588
+
589
+ function write(index, { name, parts }) {
590
+ const lines = [
591
+ '{',
592
+ ' "format": "pxmodel",',
593
+ ` "name": ${JSON.stringify(name)},`,
594
+ ' "triangles": 1500,',
595
+ ` "look": ${JSON.stringify(LOOK)},`,
596
+ ' "parts": [',
597
+ parts.map((each) => ` ${JSON.stringify(each)}`).join(',\n'),
598
+ ' ]',
599
+ '}',
600
+ '',
601
+ ];
602
+ fs.writeFileSync(path.join(MODEL_DIR, `avatar-${index}.pxmodel`), lines.join('\n'));
603
+ }
604
+
605
+ fs.mkdirSync(MODEL_DIR, { recursive: true });
606
+ fs.mkdirSync(ART_DIR, { recursive: true });
607
+ [mascot, tabby, pom, dash].forEach((make, index) => write(index, make()));