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
@@ -1,11 +1,23 @@
1
1
  // Server-side ownership of loose scene objects. Claims and releases change body
2
2
  // types, owner reports place granted objects, and timeout checks return abandoned
3
3
  // objects to server simulation.
4
+ //
5
+ // Every handover adds one to the object's epoch. The epoch goes out with the
6
+ // handover and with each broadcast pose, and an owner's reports carry the epoch
7
+ // of the grant they were made under, so a report or a pose from before the
8
+ // latest handover is recognised and dropped.
4
9
 
5
10
  import RAPIER from '@dimforge/rapier3d-compat';
6
11
  import { number } from '@imports/castle.real-time/code/server/limits.js';
12
+ import { packVelocity } from '../client/poses.js';
7
13
  import * as Limits from './limits.js';
8
14
 
15
+ // A velocity of zero, for an owner that never reported one.
16
+ const AT_REST = [0, 0, 0, 0, 0, 0];
17
+
18
+ // Dropped stale-epoch reports are logged at most this often.
19
+ const STALE_LOG_MS = 5000;
20
+
9
21
  // Grant requested objects that are claimable, within reach, and available for
10
22
  // this player to take. The shared server grant prevents clients from retaining
11
23
  // contradictory owners for the same object.
@@ -69,28 +81,58 @@ function mayTakeOver(sim, entry, claimant, at) {
69
81
  // Transfer one object to `playerId`, or to server simulation when it is null.
70
82
  // Append the transfer to `handovers` for the next state broadcast.
71
83
  function handOver(sim, entry, id, playerId, handovers) {
84
+ const velocity = velocityOf(entry);
85
+ entry.epoch = (entry.epoch ?? 0) + 1;
86
+ entry.ack = null;
72
87
  entry.owner = playerId;
73
88
  entry.reportedAt = sim.timeMs;
74
89
  entry.claimedAt = sim.timeMs;
90
+ entry.velocity = velocity;
91
+ const pose = entry.pose;
92
+ entry.pose = null;
75
93
  if (playerId) {
76
94
  // An owned object is kinematic-position-based, so owner reports place it and
77
95
  // its collider remains active in the shared Rapier world.
78
96
  entry.body.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased, true);
79
97
  } else {
80
- // A returned object becomes dynamic and starts from rest. This avoids
81
- // retaining velocity that existed only in the previous owner's local world.
98
+ // A returned object becomes dynamic and keeps the velocity it had, so a
99
+ // rolling object is still rolling when the server takes it back. Its last
100
+ // report is put on the body first: a kinematic target set for the next
101
+ // step does not reach a body that is dynamic by then.
102
+ const [vx, vy, vz, wx, wy, wz] = velocity;
103
+ if (pose) {
104
+ entry.body.setTranslation(pose.at, true);
105
+ entry.body.setRotation(pose.rotation, true);
106
+ }
82
107
  entry.body.setBodyType(RAPIER.RigidBodyType.Dynamic, true);
83
- entry.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
84
- entry.body.setAngvel({ x: 0, y: 0, z: 0 }, true);
108
+ entry.body.setLinvel({ x: vx, y: vy, z: vz }, true);
109
+ entry.body.setAngvel({ x: wx, y: wy, z: wz }, true);
85
110
  }
86
111
 
87
112
  // Wire handovers encode zero as server ownership and a player id as a grant.
113
+ // The velocity follows, and a new owner starts its local body at it. The new
114
+ // epoch comes last.
88
115
  // An optional list permits deck hooks to reuse this helper outside a broadcast.
89
- handovers?.push([id, playerId ?? 0]);
116
+ handovers?.push([id, playerId ?? 0, ...velocity, entry.epoch]);
117
+ }
118
+
119
+ // The object's velocity as `[vx, vy, vz, wx, wy, wz]`. An owned object moves at
120
+ // the velocity its owner last reported. A kinematic body's own velocity follows
121
+ // the report rate, not the object.
122
+ function velocityOf(entry) {
123
+ if (entry.owner) {
124
+ return entry.velocity ?? AT_REST;
125
+ }
126
+ return packVelocity(entry.body.linvel(), entry.body.angvel());
90
127
  }
91
128
 
92
129
  // Apply reported poses to objects granted to `playerId`. Reports for unowned or
93
- // differently owned ids are ignored.
130
+ // differently owned ids are ignored. A report is
131
+ // `[id, x, y, z, qx, qy, qz, qw, vx, vy, vz, wx, wy, wz, epoch, seq, base]`:
132
+ // the pose, then the object's velocity, which the object keeps when it is
133
+ // handed over, then the epoch of the grant, the report's sequence number, and
134
+ // the last sequence number the owner had seen acknowledged. A report from an
135
+ // older epoch is dropped. `seq` is acknowledged; `base` is not read yet.
94
136
  export function setObjectPoses(sim, playerId, poses, handovers) {
95
137
  if (!Array.isArray(poses)) {
96
138
  return;
@@ -98,9 +140,20 @@ export function setObjectPoses(sim, playerId, poses, handovers) {
98
140
 
99
141
  // Limit one message to sixteen pose checks to bound per-message server work.
100
142
  for (const pose of poses.slice(0, 16)) {
101
- const [id, x, y, z, qx, qy, qz, qw] = pose;
143
+ const [id, x, y, z, qx, qy, qz, qw, ...rest] = pose;
102
144
  const entry = sim.objects.get(id);
103
- if (entry?.owner !== playerId) {
145
+ if (!entry) {
146
+ continue;
147
+ }
148
+
149
+ // A report without an epoch comes from a client older than epochs, and is
150
+ // judged by owner alone.
151
+ const epoch = rest[6];
152
+ if (typeof epoch === 'number' && epoch < entry.epoch) {
153
+ dropStale(sim);
154
+ continue;
155
+ }
156
+ if (entry.owner !== playerId) {
104
157
  continue;
105
158
  }
106
159
  const asked = Limits.readPoint([x, y, z]);
@@ -121,16 +174,57 @@ export function setObjectPoses(sim, playerId, poses, handovers) {
121
174
  handOver(sim, entry, id, null, handovers);
122
175
  continue;
123
176
  }
177
+ const rotation = { x: number(qx), y: number(qy), z: number(qz), w: number(qw) };
124
178
  entry.body.setNextKinematicTranslation(asked);
125
- entry.body.setNextKinematicRotation({
126
- x: number(qx),
127
- y: number(qy),
128
- z: number(qz),
129
- w: number(qw),
130
- });
179
+ entry.body.setNextKinematicRotation(rotation);
180
+ entry.pose = { at: asked, rotation };
181
+ entry.velocity = readVelocity(rest.slice(0, 6));
182
+
183
+ // The acknowledgement is `[epoch, lastSeq, tAck, ageApplied, vetoedSeq]`.
184
+ // The report is steered toward as it is and nothing is vetoed yet, so the
185
+ // last two are zero.
186
+ entry.ack = [entry.epoch, number(rest[7]), sim.timeMs, 0, 0];
131
187
  }
132
188
  }
133
189
 
190
+ // Count one report dropped for an old epoch, and log the running count.
191
+ function dropStale(sim) {
192
+ sim.staleReports = (sim.staleReports ?? 0) + 1;
193
+ if (sim.timeMs - (sim.staleLoggedAt ?? -Infinity) >= STALE_LOG_MS) {
194
+ sim.staleLoggedAt = sim.timeMs;
195
+ console.log(`[multiplayer] ${sim.staleReports} stale-epoch reports dropped`);
196
+ }
197
+ }
198
+
199
+ // Return `[objectId, ...ack]` for each object whose owner's report was applied
200
+ // since the last broadcast, and clear them. The session sends these with the
201
+ // next STATE delta.
202
+ export function acks(sim) {
203
+ const out = [];
204
+ for (const [id, entry] of sim.objects) {
205
+ if (entry.ack) {
206
+ out.push([id, ...entry.ack]);
207
+ entry.ack = null;
208
+ }
209
+ }
210
+ return out;
211
+ }
212
+
213
+ // Read a reported velocity. Missing values read as zero, and each part is cut
214
+ // to the object limits so a report cannot fling the object on return.
215
+ function readVelocity(values) {
216
+ const linear = capped(values.slice(0, 3).map(number), Limits.MAX_OBJECT_SPEED);
217
+ const angular = capped(values.slice(3, 6).map(number), Limits.MAX_OBJECT_SPIN);
218
+ return [...linear, ...angular];
219
+ }
220
+
221
+ // Scale a three-part vector down to `most` in length, keeping its direction.
222
+ function capped([x = 0, y = 0, z = 0], most) {
223
+ const length = Math.hypot(x, y, z);
224
+ const scale = length > most ? most / length : 1;
225
+ return [x * scale, y * scale, z * scale];
226
+ }
227
+
134
228
  // Release objects whose owner disconnected or exceeded the report timeout. The
135
229
  // session server calls this every tick before processing new grants.
136
230
  export function sweepOwners(sim, handovers) {
@@ -145,13 +239,15 @@ export function sweepOwners(sim, handovers) {
145
239
  }
146
240
  }
147
241
 
148
- // Return `[objectId, playerId]` entries for a joiner's complete WORLD snapshot.
242
+ // Return one row per object for a joiner's complete WORLD snapshot. A row is
243
+ // the handover that would describe the object now, `[objectId, playerId|0,
244
+ // vx, vy, vz, wx, wy, wz, epoch]`, followed by 1 when the body is asleep and
245
+ // 0 when it is not.
149
246
  export function ownerships(sim) {
150
247
  const out = [];
151
248
  for (const [id, entry] of sim.objects) {
152
- if (entry.owner) {
153
- out.push([id, entry.owner]);
154
- }
249
+ const asleep = entry.body.isSleeping() ? 1 : 0;
250
+ out.push([id, entry.owner ?? 0, ...velocityOf(entry), entry.epoch ?? 0, asleep]);
155
251
  }
156
252
  return out;
157
253
  }
@@ -20,7 +20,10 @@ const DEG = Math.PI / 180;
20
20
  // world the rapier world
21
21
  // bodies actor id -> rapier body, for every scene actor with a shape
22
22
  // objects actor id -> { body, home, claimable, fast, owner, reportedAt,
23
- // claimedAt }
23
+ // claimedAt, epoch, ack }. `epoch` counts the object's
24
+ // handovers; `ack` is the acknowledgement of its owner's last
25
+ // report, waiting for the next broadcast, or null
26
+ // staleReports how many owner reports were dropped for an old epoch
24
27
  // doors [{ body, props, baseY, openT }]
25
28
  // players playerId -> { body, at, facing, clamped, movedAt }
26
29
  // spawn where players come in
@@ -40,6 +43,7 @@ function emptySim() {
40
43
  template: null,
41
44
  acc: 0,
42
45
  timeMs: 0,
46
+ staleReports: 0,
43
47
  };
44
48
  }
45
49
 
@@ -105,6 +109,8 @@ function buildScene(sim) {
105
109
  owner: null,
106
110
  reportedAt: 0,
107
111
  claimedAt: 0,
112
+ epoch: 0,
113
+ ack: null,
108
114
  });
109
115
  }
110
116
  }
@@ -300,12 +306,14 @@ function driveDoor(sim, door, dt) {
300
306
  // Reading it out
301
307
  //
302
308
 
303
- // Pack every indexed dynamic or kinematic object's current Rapier pose.
304
- // `server/index.js` filters unchanged entries before broadcasting a STATE delta.
309
+ // Pack every indexed dynamic or kinematic object's current Rapier pose, followed
310
+ // by its epoch. A client drops a pose from an epoch older than the newest
311
+ // handover it has seen for the object. The session filters unchanged entries
312
+ // before broadcasting a STATE delta, and a new epoch counts as a change.
305
313
  export function objectPoses(sim) {
306
314
  const out = [];
307
315
  for (const [id, entry] of sim.objects) {
308
- out.push([id, ...packPose(entry.body.translation(), entry.body.rotation())]);
316
+ out.push([id, ...packPose(entry.body.translation(), entry.body.rotation()), entry.epoch]);
309
317
  }
310
318
  return out;
311
319
  }