castle-web-cli 0.4.203 → 0.4.205

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 (133) 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 +20 -8
  5. package/dist/agent.d.ts +5 -2
  6. package/dist/agent.js +59 -125
  7. package/dist/castle-host/host.d.ts +17 -0
  8. package/dist/castle-host/host.js +551 -4
  9. package/dist/castleJson.d.ts +1 -0
  10. package/dist/devSessionStorage.js +65 -4
  11. package/dist/effortSupport.d.ts +7 -0
  12. package/dist/effortSupport.js +31 -0
  13. package/dist/init.js +13 -3
  14. package/dist/platformDoc.js +6 -1
  15. package/dist/routerFences.d.ts +22 -0
  16. package/dist/routerFences.js +139 -0
  17. package/dist/serve.js +35 -2
  18. package/dist/shell/assets/index-b_bebkRb.css +1 -0
  19. package/dist/shell/assets/{index-CxrtbzHf.js → index-hWQTC8q6.js} +5 -5
  20. package/dist/shell/index.html +2 -2
  21. package/dist/taskAccount.d.ts +13 -0
  22. package/dist/taskAccount.js +38 -0
  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 +4 -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 +4 -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-3d/CLAUDE.md +65 -7
  56. package/kits/physics-3d/castle.json +3 -3
  57. package/kits/physics-3d/docs/pxmodel-format.md +916 -15
  58. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  59. package/kits/physics-3d/engine3d/artobjects.js +218 -0
  60. package/kits/physics-3d/engine3d/blob.js +1058 -0
  61. package/kits/physics-3d/engine3d/branches.js +300 -0
  62. package/kits/physics-3d/engine3d/cage.js +496 -0
  63. package/kits/physics-3d/engine3d/cageops.js +516 -0
  64. package/kits/physics-3d/engine3d/frame.js +669 -0
  65. package/kits/physics-3d/engine3d/look.js +675 -0
  66. package/kits/physics-3d/engine3d/materialpresets.js +486 -0
  67. package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
  68. package/kits/physics-3d/engine3d/modelrefs.js +120 -0
  69. package/kits/physics-3d/engine3d/motion.js +560 -0
  70. package/kits/physics-3d/engine3d/objtext.js +37 -0
  71. package/kits/physics-3d/engine3d/optimize.js +1083 -0
  72. package/kits/physics-3d/engine3d/paint.js +674 -0
  73. package/kits/physics-3d/engine3d/partgeometry.js +467 -0
  74. package/kits/physics-3d/engine3d/paths.js +324 -0
  75. package/kits/physics-3d/engine3d/pxmodel.js +429 -26
  76. package/kits/physics-3d/engine3d/scatter.js +102 -0
  77. package/kits/physics-3d/engine3d/sculpt.js +1148 -0
  78. package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
  79. package/kits/physics-3d/engine3d/sweep.js +480 -0
  80. package/kits/physics-3d/engine3d/thumbnails.js +6 -4
  81. package/kits/physics-3d/engine3d/world3d.js +119 -17
  82. package/kits/physics-3d/eslint.config.js +13 -0
  83. package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
  84. package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
  85. package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
  86. package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
  87. package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
  88. package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
  89. package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
  90. package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
  91. package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
  92. package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
  93. package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
  94. package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
  95. package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
  96. package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
  97. package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
  98. package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
  99. package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
  100. package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
  101. package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
  102. package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
  103. package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
  104. package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
  105. package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
  106. package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
  107. package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
  108. package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
  109. package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
  110. package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
  111. package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
  112. package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
  113. package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
  114. package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
  115. package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
  116. package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
  117. package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
  118. package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
  119. package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
  120. package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
  121. package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
  122. package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
  123. package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
  124. package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
  125. package/kits/physics-3d/scripts/render/index.html +20 -0
  126. package/kits/physics-3d/scripts/render/sheet.js +411 -0
  127. package/kits/physics-3d/scripts/render.mjs +646 -0
  128. package/kits/real-time/castle.json +2 -2
  129. package/kits/real-time/code/client/ownership.js +14 -4
  130. package/kits/real-time/code/client/smooth.js +2 -2
  131. package/kits/real-time/code/server/session.js +20 -4
  132. package/package.json +2 -2
  133. package/dist/shell/assets/index-CHMcsJrW.css +0 -1
@@ -0,0 +1,267 @@
1
+ // Surface detail applied to a part's finished mesh: `displace` roughens the
2
+ // surface with noise. See docs/pxmodel-format.md for the fields.
3
+ //
4
+ // Noise needs enough vertices to show, so a coarse geometry is first refined
5
+ // by splitting its long edges. An edge is split by the same rule in both
6
+ // triangles that share it, so no cracks open.
7
+
8
+ import * as THREE from 'three';
9
+ import { valueNoise } from './partgeometry';
10
+
11
+ // Refinement stops at this many triangles.
12
+ const MAX_TRIANGLES = 60000;
13
+
14
+ export function hasSurfaceDetail(part) {
15
+ return part.displace > 0;
16
+ }
17
+
18
+ // Replaces the mesh's geometry with a displaced one, as the part asks. Edges shorter than `detail.edge` meters (optimize.js) are
19
+ // not split.
20
+ export function applySurfaceDetail(mesh, part, detail = null) {
21
+ let geometry = mesh.geometry;
22
+
23
+ // Edges are measured in the part's own units, which the part's scale
24
+ // stretches by about the mean of its three scales.
25
+ const [x, y, z] = part.scale.map((value) => Math.abs(value) || 1);
26
+ const shortest = (detail?.edge ?? 0) / Math.cbrt(x * y * z);
27
+ if (part.displace > 0) {
28
+ const scale = noiseScale(part);
29
+ geometry = refine(geometry, Math.max(shortest, 1 / (Math.max(...scale) * 2.5 * 2.5)));
30
+ displace(geometry, part, scale);
31
+ }
32
+ if (geometry !== mesh.geometry) {
33
+ mesh.geometry.dispose();
34
+ mesh.geometry = geometry;
35
+ }
36
+ }
37
+
38
+ //
39
+ // Refinement
40
+ //
41
+
42
+ // A copy of the geometry, unindexed, with every edge longer than `target`
43
+ // split at its middle, repeated until none is or the triangle limit is
44
+ // reached. A triangle with one split edge becomes two, with two becomes
45
+ // three, with three becomes four. Every attribute is carried to the new
46
+ // vertices, and material groups are kept.
47
+ function refine(geometry, target) {
48
+ let soup = toSoup(geometry);
49
+ for (let pass = 0; pass < 10 && soup.triangles < MAX_TRIANGLES; pass++) {
50
+ const next = splitLongEdges(soup, target);
51
+ if (next.triangles === soup.triangles) {
52
+ break;
53
+ }
54
+ soup = next;
55
+ }
56
+ return fromSoup(soup, geometry);
57
+ }
58
+
59
+ // A triangle soup: per attribute, a flat array with three vertices per
60
+ // triangle, plus each triangle's material group.
61
+ function toSoup(geometry) {
62
+ const flat = geometry.index ? geometry.toNonIndexed() : geometry;
63
+ const attributes = {};
64
+ for (const [name, attribute] of Object.entries(flat.attributes)) {
65
+ const values = [];
66
+ for (let i = 0; i < attribute.count; i++) {
67
+ for (let k = 0; k < attribute.itemSize; k++) {
68
+ values.push(attribute.getComponent(i, k));
69
+ }
70
+ }
71
+ attributes[name] = { itemSize: attribute.itemSize, values };
72
+ }
73
+ const triangles = flat.attributes.position.count / 3;
74
+ const groups = new Array(triangles).fill(0);
75
+ for (const group of flat.groups) {
76
+ for (let t = group.start / 3; t < (group.start + group.count) / 3 && t < triangles; t++) {
77
+ groups[t] = group.materialIndex ?? 0;
78
+ }
79
+ }
80
+ return { attributes, groups, triangles };
81
+ }
82
+
83
+ function fromSoup(soup, original) {
84
+ const geometry = new THREE.BufferGeometry();
85
+ for (const [name, { itemSize, values }] of Object.entries(soup.attributes)) {
86
+ geometry.setAttribute(name, new THREE.Float32BufferAttribute(values, itemSize));
87
+ }
88
+ if (original.groups.length > 0) {
89
+ let start = 0;
90
+ for (let t = 1; t <= soup.triangles; t++) {
91
+ if (t === soup.triangles || soup.groups[t] !== soup.groups[start]) {
92
+ geometry.addGroup(start * 3, (t - start) * 3, soup.groups[start]);
93
+ start = t;
94
+ }
95
+ }
96
+ }
97
+ return geometry;
98
+ }
99
+
100
+ function splitLongEdges(soup, target) {
101
+ const names = Object.keys(soup.attributes);
102
+ const out = { attributes: {}, groups: [], triangles: 0 };
103
+ for (const name of names) {
104
+ out.attributes[name] = { itemSize: soup.attributes[name].itemSize, values: [] };
105
+ }
106
+ const position = soup.attributes.position.values;
107
+
108
+ // A vertex is one entry per attribute; a midpoint averages two of them.
109
+ const vertex = (index) =>
110
+ names.map((name) => {
111
+ const { itemSize, values } = soup.attributes[name];
112
+ return values.slice(index * itemSize, index * itemSize + itemSize);
113
+ });
114
+ const middle = (a, b) => a.map((item, n) => item.map((value, k) => (value + b[n][k]) / 2));
115
+ const emit = (group, ...corners) => {
116
+ for (const corner of corners) {
117
+ names.forEach((name, n) => out.attributes[name].values.push(...corner[n]));
118
+ }
119
+ out.groups.push(group);
120
+ out.triangles++;
121
+ };
122
+ const length = (i, j) =>
123
+ Math.hypot(
124
+ position[i * 3] - position[j * 3],
125
+ position[i * 3 + 1] - position[j * 3 + 1],
126
+ position[i * 3 + 2] - position[j * 3 + 2]
127
+ );
128
+
129
+ for (let t = 0; t < soup.triangles; t++) {
130
+ const group = soup.groups[t];
131
+ const index = [t * 3, t * 3 + 1, t * 3 + 2];
132
+ const split = [0, 1, 2].map((e) => length(index[e], index[(e + 1) % 3]) > target);
133
+ const count = split.filter(Boolean).length;
134
+ const corners = index.map(vertex);
135
+ if (count === 0) {
136
+ emit(group, ...corners);
137
+ continue;
138
+ }
139
+
140
+ // Turn the corners so the pattern starts at corner 0: for one split,
141
+ // edge 0-1 is split; for two, edge 2-0 is the one left whole.
142
+ let turn = 0;
143
+ if (count === 1) {
144
+ turn = split.indexOf(true);
145
+ } else if (count === 2) {
146
+ turn = (split.indexOf(false) + 1) % 3;
147
+ }
148
+ const [a, b, c] = [0, 1, 2].map((k) => corners[(k + turn) % 3]);
149
+ const ab = middle(a, b);
150
+ const bc = middle(b, c);
151
+ const ca = middle(c, a);
152
+ if (count === 1) {
153
+ emit(group, a, ab, c);
154
+ emit(group, ab, b, c);
155
+ } else if (count === 2) {
156
+ emit(group, ab, b, bc);
157
+ emit(group, a, ab, bc);
158
+ emit(group, a, bc, c);
159
+ } else {
160
+ emit(group, a, ab, ca);
161
+ emit(group, ab, b, bc);
162
+ emit(group, ca, bc, c);
163
+ emit(group, ab, bc, ca);
164
+ }
165
+ }
166
+ return out;
167
+ }
168
+
169
+ //
170
+ // Displacement
171
+ //
172
+
173
+ // `noiseScale` is how many bumps per unit, a number or [x, y, z]. A small
174
+ // value along one axis stretches the bumps along it, as in bark.
175
+ function noiseScale(part) {
176
+ const value = part.noiseScale;
177
+ if (Array.isArray(value)) {
178
+ return [0, 1, 2].map((i) => (value[i] > 0 ? value[i] : 5));
179
+ }
180
+ return [0, 1, 2].map(() => (value > 0 ? value : 5));
181
+ }
182
+
183
+ // Vertices at the same place are one point of the surface, even where the
184
+ // geometry splits them for a sharp edge or a seam.
185
+ function samePlaceGroups(position) {
186
+ const groups = new Map();
187
+ for (let i = 0; i < position.count; i++) {
188
+ const key = [position.getX(i), position.getY(i), position.getZ(i)]
189
+ .map((value) => Math.round(value * 1e4))
190
+ .join(',');
191
+ if (!groups.has(key)) {
192
+ groups.set(key, []);
193
+ }
194
+ groups.get(key).push(i);
195
+ }
196
+ return [...groups.values()];
197
+ }
198
+
199
+ // Each point moves along the average of its normals by `displace` times the
200
+ // noise at that point. Two octaves, as in the lumpy sphere. All vertices at
201
+ // one place move together, so sharp edges stay closed.
202
+ function displace(geometry, part, scale) {
203
+ if (!geometry.attributes.normal) {
204
+ geometry.computeVertexNormals();
205
+ }
206
+ const position = geometry.attributes.position;
207
+ const normal = geometry.attributes.normal;
208
+ const originalNormals = normal.clone();
209
+ const seed = Number.isFinite(part.seed) ? part.seed : 1;
210
+ const places = samePlaceGroups(position);
211
+ const direction = new THREE.Vector3();
212
+ const point = new THREE.Vector3();
213
+ for (const place of places) {
214
+ direction.set(0, 0, 0);
215
+ for (const i of place) {
216
+ direction.x += normal.getX(i);
217
+ direction.y += normal.getY(i);
218
+ direction.z += normal.getZ(i);
219
+ }
220
+ direction.normalize();
221
+ point.fromBufferAttribute(position, place[0]);
222
+ const [x, y, z] = [point.x * scale[0], point.y * scale[1], point.z * scale[2]];
223
+ const n =
224
+ valueNoise(x, y, z, seed) * 0.7 + valueNoise(x * 2.5, y * 2.5, z * 2.5, seed + 7) * 0.3;
225
+ point.addScaledVector(direction, part.displace * n);
226
+ for (const i of place) {
227
+ position.setXYZ(i, point.x, point.y, point.z);
228
+ }
229
+ }
230
+ smoothNormals(geometry, places, originalNormals);
231
+ }
232
+
233
+ // Normals of the displaced surface: each vertex averages the faces around its
234
+ // point that faced within 60 degrees of its own original normal, so round
235
+ // parts shade smoothly and the edges of a box stay sharp.
236
+ function smoothNormals(geometry, places, originalNormals) {
237
+ const position = geometry.attributes.position;
238
+ const faceNormals = [];
239
+ const a = new THREE.Vector3();
240
+ const b = new THREE.Vector3();
241
+ const c = new THREE.Vector3();
242
+ for (let t = 0; t < position.count / 3; t++) {
243
+ a.fromBufferAttribute(position, t * 3);
244
+ b.fromBufferAttribute(position, t * 3 + 1);
245
+ c.fromBufferAttribute(position, t * 3 + 2);
246
+ faceNormals.push(new THREE.Triangle(a, b, c).getNormal(new THREE.Vector3()));
247
+ }
248
+ const normal = geometry.attributes.normal;
249
+ const mine = new THREE.Vector3();
250
+ const other = new THREE.Vector3();
251
+ const sum = new THREE.Vector3();
252
+ for (const place of places) {
253
+ for (const i of place) {
254
+ mine.fromBufferAttribute(originalNormals, i);
255
+ sum.set(0, 0, 0);
256
+ for (const j of place) {
257
+ other.fromBufferAttribute(originalNormals, j);
258
+ if (mine.dot(other) > 0.5) {
259
+ sum.add(faceNormals[Math.floor(j / 3)]);
260
+ }
261
+ }
262
+ sum.normalize();
263
+ normal.setXYZ(i, sum.x, sum.y, sum.z);
264
+ }
265
+ }
266
+ normal.needsUpdate = true;
267
+ }