castle-web-cli 0.4.204 → 0.4.206

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 (134) hide show
  1. package/dist/agent-failures.d.ts +3 -1
  2. package/dist/agent-failures.js +19 -0
  3. package/dist/agent-prompts.d.ts +1 -0
  4. package/dist/agent-prompts.js +7 -2
  5. package/dist/agent.d.ts +3 -2
  6. package/dist/agent.js +29 -18
  7. package/dist/castle-host/host.d.ts +17 -0
  8. package/dist/castle-host/host.js +551 -4
  9. package/dist/devSessionStorage.js +65 -4
  10. package/dist/effortSupport.d.ts +7 -0
  11. package/dist/effortSupport.js +31 -0
  12. package/dist/imports.d.ts +1 -1
  13. package/dist/imports.js +59 -19
  14. package/dist/index.js +4 -3
  15. package/dist/init.d.ts +9 -1
  16. package/dist/init.js +64 -25
  17. package/dist/kitCatalog.d.ts +13 -0
  18. package/dist/kitCatalog.js +111 -0
  19. package/dist/platformDoc.js +7 -2
  20. package/dist/serve.js +35 -2
  21. package/dist/shell/assets/{index-DHzA0-Sj.js → index-hWQTC8q6.js} +5 -5
  22. package/dist/shell/index.html +1 -1
  23. package/dist/vitePlugins.js +19 -1
  24. package/kits/base/CLAUDE.md +2 -0
  25. package/kits/base/castle.json +1 -1
  26. package/kits/base/sdk/README.md +131 -0
  27. package/kits/base/sdk/agent.d.ts +6 -0
  28. package/kits/base/sdk/agent.js +70 -0
  29. package/kits/base/sdk/castle.d.ts +4 -0
  30. package/kits/base/sdk/castle.js +2 -0
  31. package/kits/base/sdk/commands.d.ts +106 -0
  32. package/kits/base/sdk/commands.js +21 -0
  33. package/kits/base/sdk/decks.d.ts +7 -0
  34. package/kits/base/sdk/decks.js +31 -0
  35. package/kits/base/sdk/runtime.d.ts +4 -2
  36. package/kits/base/sdk/runtime.js +51 -40
  37. package/kits/base/sdk/server/storage.d.ts +44 -0
  38. package/kits/base/sdk/server/storage.js +54 -0
  39. package/kits/base/sdk/transport.d.ts +5 -1
  40. package/kits/base/sdk/transport.js +106 -23
  41. package/kits/multiplayer-2d/castle.json +5 -4
  42. package/kits/multiplayer-2d/code/client/poses.js +7 -0
  43. package/kits/multiplayer-2d/code/server/limits.js +3 -0
  44. package/kits/multiplayer-2d/code/server/ownership.js +36 -8
  45. package/kits/multiplayer-2d/code/systems/multiplayer.js +23 -2
  46. package/kits/multiplayer-3d/CLAUDE.md +12 -1
  47. package/kits/multiplayer-3d/castle.json +5 -4
  48. package/kits/multiplayer-3d/code/client/avatars.js +7 -1
  49. package/kits/multiplayer-3d/code/client/poses.js +61 -1
  50. package/kits/multiplayer-3d/code/client/steer.js +383 -0
  51. package/kits/multiplayer-3d/code/server/limits.js +3 -0
  52. package/kits/multiplayer-3d/code/server/ownership.js +114 -18
  53. package/kits/multiplayer-3d/code/server/world.js +12 -4
  54. package/kits/multiplayer-3d/code/systems/multiplayer.js +315 -34
  55. package/kits/physics-2d/castle.json +1 -0
  56. package/kits/physics-3d/CLAUDE.md +65 -7
  57. package/kits/physics-3d/castle.json +4 -3
  58. package/kits/physics-3d/docs/pxmodel-format.md +916 -15
  59. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  60. package/kits/physics-3d/engine3d/artobjects.js +218 -0
  61. package/kits/physics-3d/engine3d/blob.js +1058 -0
  62. package/kits/physics-3d/engine3d/branches.js +300 -0
  63. package/kits/physics-3d/engine3d/cage.js +496 -0
  64. package/kits/physics-3d/engine3d/cageops.js +516 -0
  65. package/kits/physics-3d/engine3d/frame.js +669 -0
  66. package/kits/physics-3d/engine3d/look.js +675 -0
  67. package/kits/physics-3d/engine3d/materialpresets.js +486 -0
  68. package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
  69. package/kits/physics-3d/engine3d/modelrefs.js +120 -0
  70. package/kits/physics-3d/engine3d/motion.js +560 -0
  71. package/kits/physics-3d/engine3d/objtext.js +37 -0
  72. package/kits/physics-3d/engine3d/optimize.js +1083 -0
  73. package/kits/physics-3d/engine3d/paint.js +674 -0
  74. package/kits/physics-3d/engine3d/partgeometry.js +467 -0
  75. package/kits/physics-3d/engine3d/paths.js +324 -0
  76. package/kits/physics-3d/engine3d/pxmodel.js +429 -26
  77. package/kits/physics-3d/engine3d/scatter.js +102 -0
  78. package/kits/physics-3d/engine3d/sculpt.js +1148 -0
  79. package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
  80. package/kits/physics-3d/engine3d/sweep.js +480 -0
  81. package/kits/physics-3d/engine3d/thumbnails.js +6 -4
  82. package/kits/physics-3d/engine3d/world3d.js +119 -17
  83. package/kits/physics-3d/eslint.config.js +13 -0
  84. package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
  85. package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
  86. package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
  87. package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
  88. package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
  89. package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
  90. package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
  91. package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
  92. package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
  93. package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
  94. package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
  95. package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
  96. package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
  97. package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
  98. package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
  99. package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
  100. package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
  101. package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
  102. package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
  103. package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
  104. package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
  105. package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
  106. package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
  107. package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
  108. package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
  109. package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
  110. package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
  111. package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
  112. package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
  113. package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
  114. package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
  115. package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
  116. package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
  117. package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
  118. package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
  119. package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
  120. package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
  121. package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
  122. package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
  123. package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
  124. package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
  125. package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
  126. package/kits/physics-3d/scripts/render/index.html +20 -0
  127. package/kits/physics-3d/scripts/render/sheet.js +411 -0
  128. package/kits/physics-3d/scripts/render.mjs +646 -0
  129. package/kits/real-time/castle.json +2 -2
  130. package/kits/real-time/code/client/ownership.js +14 -4
  131. package/kits/real-time/code/client/smooth.js +2 -2
  132. package/kits/real-time/code/server/session.js +20 -4
  133. package/kits/turn-based/castle.json +1 -0
  134. package/package.json +2 -2
@@ -0,0 +1,300 @@
1
+ // The `branches` part kind: a trunk that splits again and again into tapered
2
+ // branches, built as one geometry, and the `tips` option that puts copies of
3
+ // another part at the ends of the last level. See docs/pxmodel-format.md for
4
+ // the fields.
5
+ //
6
+ // Every random choice comes from a generator seeded by `seed`, so the same
7
+ // fields always give the same shape.
8
+
9
+ import * as THREE from 'three';
10
+
11
+ const DEG = Math.PI / 180;
12
+ const UP = new THREE.Vector3(0, 1, 0);
13
+ const MAX_BRANCHES = 800;
14
+
15
+ // Each branch is a curve through a few control points, drawn from here.
16
+ const CONTROL_SEGMENTS = 4;
17
+
18
+ // A branch is `points` (its control points), `radiusStart` and `radiusEnd`,
19
+ // its `level` (0 for the trunk) and `fork`, true when it splits at its end.
20
+
21
+ // A small seeded random number generator (mulberry32) returning [0, 1).
22
+ function seededRandom(seed) {
23
+ let state = Math.round(seed * 9973 + 17) | 0;
24
+ return () => {
25
+ state = (state + 0x6d2b79f5) | 0;
26
+ let t = Math.imul(state ^ (state >>> 15), 1 | state);
27
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
28
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
29
+ };
30
+ }
31
+
32
+ function number(value, fallback, min, max) {
33
+ if (!Number.isFinite(value)) {
34
+ return fallback;
35
+ }
36
+ return Math.max(min, Math.min(max, value));
37
+ }
38
+
39
+ // The fields with their defaults, clamped to what makes sense.
40
+ function readSettings(part) {
41
+ const direction = Array.isArray(part.direction)
42
+ ? new THREE.Vector3(
43
+ ...[0, 1, 2].map((i) => (Number.isFinite(part.direction[i]) ? part.direction[i] : 0))
44
+ )
45
+ : UP.clone();
46
+ if (direction.lengthSq() < 1e-8) {
47
+ direction.copy(UP);
48
+ }
49
+ return {
50
+ length: number(part.length, 2, 0.01, 100),
51
+ radius: number(part.radius, 0.15, 0.001, 10),
52
+ depth: Math.round(number(part.depth, 4, 0, 7)),
53
+ split: Math.round(number(part.split, 2, 1, 6)),
54
+ spread: number(part.spread, 35, 0, 180) * DEG,
55
+ shrink: number(part.shrink, 0.72, 0.05, 1.5),
56
+ thin: number(part.thin, 0.65, 0.05, 1),
57
+ curve: number(part.curve, 15, 0, 90) * DEG,
58
+ lift: number(part.lift, 0.2, -2, 2),
59
+ seed: Number.isFinite(part.seed) ? part.seed : 1,
60
+ direction: direction.normalize(),
61
+ };
62
+ }
63
+
64
+ // Two unit vectors square to `direction` and to each other.
65
+ function sideAxes(direction) {
66
+ const helper = Math.abs(direction.y) < 0.9 ? UP : new THREE.Vector3(1, 0, 0);
67
+ const side = new THREE.Vector3().crossVectors(helper, direction).normalize();
68
+ const other = new THREE.Vector3().crossVectors(direction, side);
69
+ return [side, other];
70
+ }
71
+
72
+ // `direction` turned by `angle` toward the side at `azimuth` around it.
73
+ function turnAway(direction, angle, azimuth) {
74
+ const [side, other] = sideAxes(direction);
75
+ const toward = side.multiplyScalar(Math.cos(azimuth)).addScaledVector(other, Math.sin(azimuth));
76
+ return direction
77
+ .clone()
78
+ .multiplyScalar(Math.cos(angle))
79
+ .addScaledVector(toward, Math.sin(angle))
80
+ .normalize();
81
+ }
82
+
83
+ //
84
+ // Growing
85
+ //
86
+
87
+ // The whole tree as a flat list of branches, trunk first. Each branch walks
88
+ // its length in a few steps, turning a little each step toward one side
89
+ // (`curve`) and toward +y or -y (`lift`). At its end, unless it is on the
90
+ // last level, it forks into `split` children spread evenly around it at about
91
+ // `spread` from its direction.
92
+ export function growBranches(settings) {
93
+ const random = seededRandom(settings.seed);
94
+ const branches = [];
95
+ const queue = [
96
+ {
97
+ start: new THREE.Vector3(),
98
+ direction: settings.direction.clone(),
99
+ length: settings.length,
100
+ radius: settings.radius,
101
+ level: 0,
102
+ },
103
+ ];
104
+ while (queue.length > 0) {
105
+ const seedling = queue.shift();
106
+ const branch = growOne(seedling, settings, random);
107
+ branches.push(branch);
108
+ if (seedling.level >= settings.depth || branches.length + queue.length >= MAX_BRANCHES) {
109
+ continue;
110
+ }
111
+ branch.fork = true;
112
+ const end = branch.points[branch.points.length - 1];
113
+ const direction = end
114
+ .clone()
115
+ .sub(branch.points[branch.points.length - 2])
116
+ .normalize();
117
+ const phase = random() * Math.PI * 2;
118
+ for (let k = 0; k < settings.split; k++) {
119
+ const azimuth = phase + (k / settings.split) * Math.PI * 2 + (random() - 0.5) * 0.8;
120
+ const angle = settings.spread * (0.75 + random() * 0.5);
121
+ queue.push({
122
+ start: end.clone(),
123
+ direction: turnAway(direction, angle, azimuth),
124
+ length: seedling.length * settings.shrink * (0.85 + random() * 0.3),
125
+ radius: branch.radiusEnd * 0.95,
126
+ level: seedling.level + 1,
127
+ });
128
+ }
129
+ }
130
+ return branches;
131
+ }
132
+
133
+ // One branch: control points from the start along a gently turning direction.
134
+ // The last level narrows to a finer point than the others.
135
+ function growOne(seedling, settings, random) {
136
+ const direction = seedling.direction.clone();
137
+ const step = seedling.length / CONTROL_SEGMENTS;
138
+ const bendAxis = sideAxes(direction)[0].applyAxisAngle(direction, random() * Math.PI * 2);
139
+ const points = [seedling.start.clone()];
140
+ for (let s = 0; s < CONTROL_SEGMENTS; s++) {
141
+ if (s > 0) {
142
+ const turn = settings.curve * (0.4 + random() * 0.6) * (random() < 0.2 ? -1 : 1);
143
+ direction.applyAxisAngle(bendAxis, turn / 2);
144
+ direction.addScaledVector(UP, settings.lift * 0.3).normalize();
145
+ }
146
+ points.push(points[s].clone().addScaledVector(direction, step));
147
+ }
148
+ const last = seedling.level >= settings.depth;
149
+ return {
150
+ points,
151
+ radiusStart: seedling.radius,
152
+ radiusEnd: seedling.radius * settings.thin * (last ? 0.45 : 1),
153
+ level: seedling.level,
154
+ fork: false,
155
+ };
156
+ }
157
+
158
+ //
159
+ // Meshing
160
+ //
161
+
162
+ // Rings of vertices along each branch's smooth curve, joined into a tube.
163
+ // The frame at each ring is carried from the one before, so the tube does not
164
+ // twist. The trunk's base and every last-level tip are capped; a fork is
165
+ // covered by a ball as wide as the branch there, which also hides the
166
+ // children's open starts.
167
+ function addTube(branch, mesh) {
168
+ const curve = new THREE.CatmullRomCurve3(branch.points, false, 'centripetal');
169
+ const rings = branch.level === 0 ? 14 : 8;
170
+ const sides = Math.max(5, 12 - branch.level * 2);
171
+ const first = mesh.positions.length / 3;
172
+ let normal = null;
173
+ for (let ring = 0; ring <= rings; ring++) {
174
+ const t = ring / rings;
175
+ const center = curve.getPointAt(t);
176
+ const tangent = curve.getTangentAt(t);
177
+ normal = normal
178
+ ? normal.addScaledVector(tangent, -normal.dot(tangent)).normalize()
179
+ : sideAxes(tangent)[0];
180
+ const binormal = new THREE.Vector3().crossVectors(tangent, normal);
181
+ const radius = branch.radiusStart + (branch.radiusEnd - branch.radiusStart) * t;
182
+ for (let side = 0; side < sides; side++) {
183
+ const angle = (side / sides) * Math.PI * 2;
184
+ const cos = Math.cos(angle) * radius;
185
+ const sin = Math.sin(angle) * radius;
186
+ mesh.positions.push(
187
+ center.x + cos * normal.x + sin * binormal.x,
188
+ center.y + cos * normal.y + sin * binormal.y,
189
+ center.z + cos * normal.z + sin * binormal.z
190
+ );
191
+ }
192
+ }
193
+ for (let ring = 0; ring < rings; ring++) {
194
+ for (let side = 0; side < sides; side++) {
195
+ const a = first + ring * sides + side;
196
+ const b = first + ring * sides + ((side + 1) % sides);
197
+ mesh.indices.push(a, b, a + sides, b, b + sides, a + sides);
198
+ }
199
+ }
200
+ if (branch.level === 0) {
201
+ addCap(mesh, curve.getPointAt(0), first, sides, true);
202
+ }
203
+ if (branch.fork) {
204
+ addBall(mesh, curve.getPointAt(1), branch.radiusEnd, sides);
205
+ } else {
206
+ addCap(mesh, curve.getPointAt(1), first + rings * sides, sides, false);
207
+ }
208
+ return { position: curve.getPointAt(1), direction: curve.getTangentAt(1) };
209
+ }
210
+
211
+ function addCap(mesh, center, ringStart, sides, flip) {
212
+ const centerIndex = mesh.positions.length / 3;
213
+ mesh.positions.push(center.x, center.y, center.z);
214
+ for (let side = 0; side < sides; side++) {
215
+ const a = ringStart + side;
216
+ const b = ringStart + ((side + 1) % sides);
217
+ mesh.indices.push(...(flip ? [centerIndex, b, a] : [centerIndex, a, b]));
218
+ }
219
+ }
220
+
221
+ function addBall(mesh, center, radius, sides) {
222
+ const ball = new THREE.SphereGeometry(radius, sides, Math.max(3, Math.round(sides / 2)));
223
+ const first = mesh.positions.length / 3;
224
+ const position = ball.attributes.position;
225
+ for (let i = 0; i < position.count; i++) {
226
+ mesh.positions.push(
227
+ position.getX(i) + center.x,
228
+ position.getY(i) + center.y,
229
+ position.getZ(i) + center.z
230
+ );
231
+ }
232
+ for (const index of ball.index.array) {
233
+ mesh.indices.push(first + index);
234
+ }
235
+ ball.dispose();
236
+ }
237
+
238
+ // The geometry of a `branches` part. `userData.branchTips` lists the end of
239
+ // every last-level branch as { position, direction }, for `tips`.
240
+ export function branchesGeometry(part) {
241
+ const settings = readSettings(part);
242
+ const mesh = { positions: [], indices: [] };
243
+ const tips = [];
244
+ for (const branch of growBranches(settings)) {
245
+ const end = addTube(branch, mesh);
246
+ if (!branch.fork) {
247
+ tips.push(end);
248
+ }
249
+ }
250
+ const geometry = new THREE.BufferGeometry();
251
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(mesh.positions, 3));
252
+ geometry.setIndex(mesh.indices);
253
+ geometry.computeVertexNormals();
254
+ geometry.userData.branchTips = tips;
255
+ return geometry;
256
+ }
257
+
258
+ //
259
+ // Tips
260
+ //
261
+
262
+ // Copies of the `tips` part at every branch end, added under the branches
263
+ // part's own group. Each copy sits in a frame at the end whose +y runs along
264
+ // the branch, turned a random amount about it; the tip part's own position
265
+ // and rotation apply inside that frame. The tip part itself is hidden and
266
+ // serves as the pattern. `copyOf` makes a copy of a group that has no part
267
+ // ids.
268
+ export function placeTips(part, group, groups, copyOf) {
269
+ const pattern = groups.get(part.tips);
270
+ const mesh = group.children.find((child) => child.userData.partMesh === part.id);
271
+ const tips = mesh?.userData.branchTips;
272
+ if (!pattern || part.tips === part.id || !tips || isAncestor(pattern, group)) {
273
+ return;
274
+ }
275
+ mesh.updateMatrix();
276
+ const random = seededRandom((Number.isFinite(part.seed) ? part.seed : 1) + 101);
277
+ const spin = new THREE.Quaternion();
278
+ for (const tip of tips) {
279
+ const holder = new THREE.Group();
280
+ holder.userData.copyOf = part.tips;
281
+ holder.position.copy(tip.position).applyMatrix4(mesh.matrix);
282
+ const direction = tip.direction.clone().transformDirection(mesh.matrix);
283
+ holder.quaternion.setFromUnitVectors(UP, direction);
284
+ holder.quaternion.multiply(spin.setFromAxisAngle(UP, random() * Math.PI * 2));
285
+ const copy = copyOf(pattern);
286
+ copy.visible = true;
287
+ holder.add(copy);
288
+ group.add(holder);
289
+ }
290
+ pattern.visible = false;
291
+ }
292
+
293
+ function isAncestor(node, of) {
294
+ for (let current = of; current; current = current.parent) {
295
+ if (current === node) {
296
+ return true;
297
+ }
298
+ }
299
+ return false;
300
+ }