roamwright 0.61.20 → 0.61.22

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 (43) hide show
  1. package/README.md +13 -12
  2. package/examples/scenes.html +54 -0
  3. package/examples/scenes.js +594 -0
  4. package/geometry/projectile-shapes.js +85 -0
  5. package/index.js +50 -0
  6. package/motion/fallen-mount.js +63 -0
  7. package/motion/melee-combat.js +328 -0
  8. package/motion/melee-cuts.js +395 -0
  9. package/motion/melee-duels.js +142 -0
  10. package/motion/melee-exchange-creatures.js +86 -0
  11. package/motion/melee-exchange.js +498 -0
  12. package/motion/melee-reactions.js +147 -0
  13. package/motion/prop-contact.js +87 -0
  14. package/package.json +1 -1
  15. package/rendering/blade-trails.js +128 -0
  16. package/rendering/scene-beams.js +486 -0
  17. package/rendering/scene-coverage.js +500 -0
  18. package/rendering/scene-markers.js +421 -0
  19. package/rendering/scene-projectiles.js +395 -0
  20. package/rendering/scene-props.js +383 -0
  21. package/rendering/scene-shots.js +199 -0
  22. package/runtime/foley-surface.js +79 -0
  23. package/runtime/scene-script.js +137 -0
  24. package/runtime/scene-zones.js +117 -0
  25. package/tools/README.md +34 -25
  26. package/tools/cpu-profile.cjs +117 -0
  27. package/tools/cpu-profile.test.cjs +31 -1
  28. package/tools/figure-inspect.cjs +475 -0
  29. package/tools/figure-inspect.test.cjs +153 -0
  30. package/tools/geometry-hash.cjs +361 -0
  31. package/tools/geometry-hash.test.cjs +146 -0
  32. package/tools/load-audit.cjs +199 -2
  33. package/tools/load-audit.test.cjs +68 -0
  34. package/tools/quiet-bench.cjs +202 -0
  35. package/tools/quiet-bench.test.cjs +91 -0
  36. package/tools/scene-detail.cjs +355 -0
  37. package/tools/scene-detail.test.cjs +162 -0
  38. package/tools/scrub-clear.cjs +191 -0
  39. package/tools/scrub-clear.test.cjs +100 -0
  40. package/tools/tree-inspect.cjs +271 -0
  41. package/tools/tree-inspect.test.cjs +123 -0
  42. package/tools/world-tour.cjs +223 -0
  43. package/tools/world-tour.test.cjs +77 -0
@@ -0,0 +1,498 @@
1
+ /**
2
+ * Melee exchange choreography: single blows (human cuts, mounted flank strikes, beast leaps, and
3
+ * winged stoops), steel-on-steel hit-stop compression, defender recoil and pushback, blade trails,
4
+ * and post-pose contact IK locking weapons and shields at the shared clash point.
5
+ * @module
6
+ */
7
+
8
+ import * as THREE from 'three';
9
+ import { clamp, sstep } from '../math/scalar.js';
10
+ import { compressHitStopPhase, meleeContactWeight, sampleMeleeRecoil, solveMeleeContactPoint } from './melee-contact.js';
11
+ import { turnBetween } from './figure-locomotion.js';
12
+ import {
13
+ DEFAULT_CUTS,
14
+ DEFAULT_CUT_STYLES,
15
+ DEFAULT_DODGE_STEP,
16
+ MELEE_MEET,
17
+ MELEE_THRU,
18
+ deathOf,
19
+ dodgeBody,
20
+ dodgeOf,
21
+ parryBody,
22
+ pickCut,
23
+ swingBody,
24
+ woundBody,
25
+ woundDrive,
26
+ } from './melee-cuts.js';
27
+ import { BODY_GAP, DEFAULT_WEAPON_LENGTHS, fightFx as defaultFightFx, horseGap, strikeReach } from './melee-combat.js';
28
+ import { STOOP_HIT, stepBeastLeap, stepWingedStoop } from './melee-exchange-creatures.js';
29
+
30
+ const GIVE_GROUND = 0.42, // m/s defender yields while bracing before impact
31
+ STOOP_MIN = 2.4, // minimum seconds for a winged stoop
32
+ LUNGE = 4.6, // m/s maximum lunge closing speed
33
+ CHARGE = 4.2, // m/s closing run speed before strike begins
34
+ LUNGE_REACH = 1.1, // metres short of strike reach where the blow begins
35
+ RIDE_ON = 1.5, // m/s above which a mounted rider continues past rather than turning the mount
36
+ ACROSS = 1.2, // radians off the horse's nose a stationary rider turns to strike at their flank
37
+ BEHIND = 2.5, // radians past which a target is behind a moving mount
38
+ FLANK_TWIST = 1.1, // max radians trunk turns in the saddle to strike to the side
39
+ FOE_NEAR = 5; // metres within which another armed fighter keeps the victor on guard
40
+
41
+ /**
42
+ * Returns which side (`1` or `-1`) of a rider's heading a target `other` lies on.
43
+ * @param {{ x: number, z: number, yaw: number }} rider Mounted actor.
44
+ * @param {{ x: number, z: number }} other Target actor.
45
+ * @returns {1 | -1} Side sign (`-1` if left/negative turn, `1` otherwise).
46
+ */
47
+ export function flankOf(rider, other) {
48
+ return turnBetween(rider.yaw, Math.atan2(other.x - rider.x, other.z - rider.z)) < 0 ? -1 : 1;
49
+ }
50
+
51
+ /**
52
+ * Turns a standing (or passed) mount side-on to a target vector `(bx, bz)` and returns the bearing
53
+ * offset in radians that the rider's trunk twists to meet.
54
+ * @param {{ yaw: number, turn: number, speed: number }} rider Mounted actor.
55
+ * @param {number} bx Bearing X component toward target.
56
+ * @param {number} bz Bearing Z component toward target.
57
+ * @param {number} flank Preferred flank sign (`1` or `-1`).
58
+ * @returns {number} Signed bearing offset in radians.
59
+ */
60
+ export function sideOn(rider, bx, bz, flank) {
61
+ const bearing = Math.atan2(bx, bz),
62
+ off = turnBetween(rider.yaw, bearing);
63
+ if (!(rider.speed > RIDE_ON && Math.abs(off) < BEHIND)) rider.turn = bearing - flank * ACROSS;
64
+ return off;
65
+ }
66
+
67
+ /**
68
+ * Creates a zero-allocation melee exchange runner and post-pose weapon contact IK solver.
69
+ * @param {object} options
70
+ * @param {Record<string, object>} options.actors Actor map keyed by id (scanned for nearby foes after a kill).
71
+ * @param {(x: number, z: number, radius: number) => boolean} [options.collide] Obstacle test.
72
+ * @param {(x: number, z: number) => number} [options.groundAt] Ground height sampler for beast leaps.
73
+ * @param {(x: number, y: number, salt: number) => number} options.hash Deterministic `[0, 1)` hash function.
74
+ * @param {Function} [options.reachArm] Two-bone arm IK solver for weapon-on-weapon parry lock.
75
+ * @param {Record<string, number>} [options.weaponLengths] Effective blade/shaft length per weapon key.
76
+ * @param {Record<string, readonly string[]>} [options.cutStyles] Cut preferences per weapon key.
77
+ * @param {Record<string, object>} [options.cuts] Cut keyframe table.
78
+ * @param {object} [options.trails] Blade trail pool (`acquire`, `push`).
79
+ * @param {(actor: object) => object} [options.fightFx] Fight-state getter.
80
+ * @param {(actor: object, dust?: number, sweat?: number, blood?: number, scorch?: number) => void} [options.addWear] Progressive battle-wear updater.
81
+ * @param {(event: object) => void} [options.onParry] Called at impact on a human parry.
82
+ * @param {(event: object) => void} [options.onHit] Called at impact on a wound, kill, or non-human parry.
83
+ * @param {(event: object) => void} [options.onDodge] Called at impact on a dodged blow.
84
+ * @param {(x: number, y: number, z: number, intensity: number) => void} [options.onScuff] Foot scuff callback.
85
+ * @param {(x: number, z: number) => void} [options.onAlarm] Crowd alarm callback.
86
+ * @param {(x: number, z: number, attacker: object, defender: object) => void} [options.onFlinch] Bystander flinch callback.
87
+ * @param {(fightState: object, mount: object, side: number) => void} [options.onFellMount] Called when a beast brings down a rider's mount.
88
+ * @param {string} [options.wingedKind] Actor `kind` that performs diving aerial stoops (default `'dragon'`).
89
+ * @returns {{ list: object[], startAttack: Function, updateExchanges: (dt: number) => void, applyMeleeContactIK: () => void, clear: () => void }}
90
+ */
91
+ export function createMeleeExchanges({
92
+ actors = {},
93
+ collide = () => false,
94
+ groundAt = () => 0,
95
+ hash,
96
+ reachArm = null,
97
+ weaponLengths = DEFAULT_WEAPON_LENGTHS,
98
+ cutStyles = DEFAULT_CUT_STYLES,
99
+ cuts = DEFAULT_CUTS,
100
+ trails = null,
101
+ fightFx = defaultFightFx,
102
+ addWear = () => {},
103
+ onParry = null,
104
+ onHit = null,
105
+ onDodge = null,
106
+ onScuff = null,
107
+ onAlarm = null,
108
+ onFlinch = null,
109
+ onFellMount = null,
110
+ wingedKind = 'dragon',
111
+ }) {
112
+ const list = [];
113
+ const _pa = new THREE.Vector3(),
114
+ _pb = new THREE.Vector3(),
115
+ _pc = new THREE.Vector3(),
116
+ _wristLocal = new THREE.Vector3(),
117
+ _zeroGrip = new THREE.Vector3(),
118
+ _atkInfo = { x: 0, y: 0, z: 0, scale: 1, weaponLength: 0.9 },
119
+ _defInfo = { x: 0, y: 0, z: 0, scale: 1, weaponLength: 0.9, hasShield: false },
120
+ _contactOpts = { cutStyle: 'diagonal', side: 1, attackerWeaponPoint: null, defenderGuardPoint: null },
121
+ _recoil = { jolt: 0, torsoLean: 0, torsoTwist: 0, shieldCompression: 0, pushbackSpeed: 0 },
122
+ _recoilOpts = { heavy: false, hasShield: false };
123
+ let cutCounter = 0;
124
+
125
+ function isHeavyStrike(a) {
126
+ const item = a.def.look?.item;
127
+ return (weaponLengths[item] || 0.6) >= 1.15 || item === 'axe';
128
+ }
129
+
130
+ function clashPoint(E, a, d, out) {
131
+ if (E.hasClash) return out.set(E.cx, E.cy, E.cz);
132
+ const blade = (E.left ? a.itemL : a.itemR) || a.handR,
133
+ bladeLen = weaponLengths[a.def.look?.item] || 0.6,
134
+ guard = d.shield || d.itemR || d.handR;
135
+
136
+ _atkInfo.x = a.x;
137
+ _atkInfo.y = a.y;
138
+ _atkInfo.z = a.z;
139
+ _atkInfo.scale = a.group.scale.y || 1;
140
+ _atkInfo.weaponLength = bladeLen;
141
+
142
+ _defInfo.x = d.x;
143
+ _defInfo.y = d.y;
144
+ _defInfo.z = d.z;
145
+ _defInfo.scale = d.group.scale.y || 1;
146
+ _defInfo.weaponLength = weaponLengths[d.def.look?.item] || 0.55;
147
+ _defInfo.hasShield = !!d.shield && d.def.look?.item !== 'spear';
148
+
149
+ _contactOpts.cutStyle = E.cut || 'diagonal';
150
+ _contactOpts.side = E.side;
151
+ if (blade && guard) {
152
+ _pa.set(0, 0, blade === a.handR ? 0.1 : 0.72 * bladeLen);
153
+ blade.localToWorld(_pa);
154
+ if (d.shield) {
155
+ _pb.set(0, 0.04, 0);
156
+ d.shield.localToWorld(_pb);
157
+ } else {
158
+ _pb.set(0, 0, guard === d.handR ? 0.1 : 0.52 * _defInfo.weaponLength);
159
+ guard.localToWorld(_pb);
160
+ }
161
+ _contactOpts.attackerWeaponPoint = _pa;
162
+ _contactOpts.defenderGuardPoint = _pb;
163
+ } else {
164
+ _contactOpts.attackerWeaponPoint = null;
165
+ _contactOpts.defenderGuardPoint = null;
166
+ }
167
+ return solveMeleeContactPoint(out, _atkInfo, _defInfo, _contactOpts);
168
+ }
169
+
170
+ function guideWeaponContact(actor, left, contactNode, localX, localY, localZ, target, weight) {
171
+ if (!reachArm || !actor || weight <= 0 || !contactNode) return;
172
+ const arm = left ? actor.armL : actor.armR,
173
+ fore = left ? actor.foreL : actor.foreR,
174
+ wrist = left ? actor.handL : actor.handR;
175
+ if (!arm?.parent || !fore || !wrist) return;
176
+ arm.parent.updateWorldMatrix(true, false);
177
+ arm.updateWorldMatrix(false, true);
178
+ _wristLocal.set(localX, localY, localZ);
179
+ contactNode.localToWorld(_wristLocal);
180
+ wrist.worldToLocal(_wristLocal);
181
+ reachArm(arm, fore, wrist, _wristLocal, _zeroGrip, target, left ? -1 : 1, weight);
182
+ }
183
+
184
+ function applyMeleeContactIK() {
185
+ for (let i = 0; i < list.length; i++) {
186
+ const E = list[i];
187
+ if (E.kind !== 'parry' || E.beast || E.wing) continue;
188
+ const a = E.a,
189
+ d = E.d;
190
+ if (!a.built || !d.built || a.kind !== 'human' || d.kind !== 'human' || d.pose === 'dead') continue;
191
+ const rawU = E.t / E.dur,
192
+ weight = meleeContactWeight(rawU, MELEE_MEET, 0.13, 0.17);
193
+ if (weight <= 0.002) {
194
+ E.hasClash = false;
195
+ continue;
196
+ }
197
+
198
+ a.chest?.updateWorldMatrix(true, false);
199
+ d.chest?.updateWorldMatrix(true, false);
200
+ a.armR?.updateWorldMatrix(false, true);
201
+ a.armL?.updateWorldMatrix(false, true);
202
+ d.armR?.updateWorldMatrix(false, true);
203
+ d.armL?.updateWorldMatrix(false, true);
204
+
205
+ E.hasClash = false;
206
+ clashPoint(E, a, d, _pc);
207
+ E.cx = _pc.x;
208
+ E.cy = _pc.y;
209
+ E.cz = _pc.z;
210
+ E.hasClash = true;
211
+
212
+ const atkBladeLen = weaponLengths[a.def.look?.item] || 0.65,
213
+ atkNode = (E.left ? a.itemL : a.itemR) || (E.left ? a.handL : a.handR);
214
+ guideWeaponContact(
215
+ a,
216
+ E.left,
217
+ atkNode,
218
+ 0,
219
+ 0,
220
+ atkNode === a.handR || atkNode === a.handL ? 0.08 : 0.66 * atkBladeLen,
221
+ _pc,
222
+ weight * 0.86,
223
+ );
224
+
225
+ const hasShield = !!d.shield && d.def.look?.item !== 'spear';
226
+ if (hasShield) {
227
+ guideWeaponContact(d, true, d.shield, 0, 0.05, 0, _pc, weight * 0.9);
228
+ } else {
229
+ const defBladeLen = weaponLengths[d.def.look?.item] || 0.6,
230
+ defNode = d.itemR || d.handR;
231
+ guideWeaponContact(d, false, defNode, 0, 0, defNode === d.handR ? 0.08 : 0.48 * defBladeLen, _pc, weight * 0.88);
232
+ }
233
+ }
234
+ }
235
+
236
+ function foeNear(a) {
237
+ for (const id in actors) {
238
+ const o = actors[id];
239
+ if (o === a || !o.active || !o.inScene || o.space !== a.space || o.pose === 'dead') continue;
240
+ if ((o.pose === 'guard' || o.pose === 'strike' || o.kind !== 'human') && Math.hypot(o.x - a.x, o.z - a.z) < FOE_NEAR) return true;
241
+ }
242
+ return false;
243
+ }
244
+
245
+ function startAttack(a, d, kind = 'parry', dur = 1.15) {
246
+ if (!a || !d || a.pose === 'dead') return null;
247
+ const E = {
248
+ a,
249
+ d,
250
+ kind,
251
+ t: 0,
252
+ dur,
253
+ hit: false,
254
+ hitT: 0,
255
+ hasClash: false,
256
+ cx: 0,
257
+ cy: 0,
258
+ cz: 0,
259
+ beast: a.kind !== 'human' && a.kind !== wingedKind,
260
+ wing: a.kind === wingedKind,
261
+ left: !!(a.hold && a.hold.both) && hash(list.length, (a.x * 7) | 0, 71) > 0.5,
262
+ side: hash((a.z * 3) | 0, list.length, 72) > 0.5 ? 1 : -1,
263
+ trail: null,
264
+ cut: a.kind === 'human' ? pickCut(a, cutCounter++, hash, cutStyles) : null,
265
+ flank: a.mount && a.kind === 'human' ? flankOf(a, d) : 0,
266
+ guard: d.mount && d.kind === 'human' ? flankOf(d, a) : 0,
267
+ };
268
+ if (a.kind === 'human') a.pose = 'guard';
269
+ if (a.kind === 'human' && !a.mount) {
270
+ const far = Math.hypot(d.x - a.x, d.z - a.z) - strikeReach(a, d, kind, weaponLengths) - LUNGE_REACH;
271
+ if (far > 0) E.t = -far / CHARGE;
272
+ }
273
+ if (d.kind === 'human' && (d.pose === 'idle' || d.pose === 'talk' || d.pose === 'point') && kind !== 'kill' && kind !== 'hit')
274
+ d.pose = 'guard';
275
+ if (E.beast) {
276
+ E.fy = a.fixY;
277
+ E.x0 = a.x;
278
+ E.z0 = a.z;
279
+ E.dur = Math.max(dur, 0.95);
280
+ }
281
+ if (E.wing) {
282
+ a.move = null;
283
+ E.x0 = a.x;
284
+ E.y0 = a.y;
285
+ E.z0 = a.z;
286
+ E.dur = Math.max(dur, STOOP_MIN);
287
+ }
288
+ list.push(E);
289
+ return E;
290
+ }
291
+
292
+ function updateExchanges(dt) {
293
+ for (let i = list.length - 1; i >= 0; i--) {
294
+ const E = list[i],
295
+ a = E.a,
296
+ d = E.d;
297
+ E.t += dt;
298
+ if (E.t < 0) {
299
+ const gap = Math.hypot(d.x - a.x, d.z - a.z) || 1,
300
+ step = Math.min(dt * CHARGE, Math.max(0, gap - strikeReach(a, d, E.kind, weaponLengths) - LUNGE_REACH * 0.5));
301
+ a.x += ((d.x - a.x) / gap) * step;
302
+ a.z += ((d.z - a.z) / gap) * step;
303
+ a.turn = Math.atan2(d.x - a.x, d.z - a.z);
304
+ if (step <= 0) E.t = 0;
305
+ continue;
306
+ }
307
+ const rawU = E.t / E.dur,
308
+ heavy = isHeavyStrike(a),
309
+ isHumanParry = E.kind === 'parry' && !E.beast && !E.wing && d.kind === 'human',
310
+ u = isHumanParry ? compressHitStopPhase(rawU, E.dur, MELEE_MEET, heavy ? 0.062 : 0.052, 0.17) : rawU,
311
+ fa = fightFx(a),
312
+ fd = fightFx(d),
313
+ ha = a.group.scale.y,
314
+ hd = d.group.scale.y;
315
+ fa.busy = fd.busy = true;
316
+ const ddx = d.x - a.x,
317
+ ddz = d.z - a.z,
318
+ dist = Math.hypot(ddx, ddz) || 1,
319
+ nx = ddx / dist,
320
+ nz = ddz / dist,
321
+ kill = E.kind === 'kill',
322
+ wound = kill || E.kind === 'hit',
323
+ hasShield = !!d.shield && d.def.look?.item !== 'spear';
324
+ let off = 0;
325
+ if (E.flank) off = sideOn(a, nx, nz, E.flank);
326
+ else if (!E.wing) {
327
+ a.yaw = Math.atan2(nx, nz);
328
+ a.turn = NaN;
329
+ }
330
+ if (E.wing) stepWingedStoop(E, rawU, wingedKind, fightFx);
331
+ else if (E.beast) stepBeastLeap(E, fa, rawU, dt, dist, nx, nz, ha, groundAt);
332
+ else {
333
+ const reach = strikeReach(a, d, E.kind, weaponLengths);
334
+ if (rawU < MELEE_MEET && !(E.flank && a.speed > RIDE_ON)) {
335
+ const want = dist - reach;
336
+ if (want > 0.02) {
337
+ const pace = Math.min(LUNGE, Math.max(3.2 * (0.4 + rawU * 1.7), want / Math.max(0.04, (MELEE_MEET - rawU) * E.dur))),
338
+ st = Math.min(want, dt * pace);
339
+ a.x += nx * st;
340
+ a.z += nz * st;
341
+ } else if (want < -0.28) {
342
+ a.x += nx * want * dt * 4.5;
343
+ a.z += nz * want * dt * 4.5;
344
+ }
345
+ } else if (u < MELEE_THRU && E.kind === 'dodge') {
346
+ a.x += nx * dt * 0.95;
347
+ a.z += nz * dt * 0.95;
348
+ } else if (u > MELEE_THRU) {
349
+ a.x -= nx * dt * 0.28;
350
+ a.z -= nz * dt * 0.28;
351
+ }
352
+ const into = Math.max(BODY_GAP * Math.max(ha, hd), horseGap(a, d)) - dist;
353
+ if (into > 0 && d.kind === 'human') {
354
+ a.x -= nx * into;
355
+ a.z -= nz * into;
356
+ }
357
+ swingBody(fa, E.cut, u, isHumanParry, E.kind === 'dodge', E.left, cuts);
358
+ if (E.flank) fa.tw = clamp(fa.tw + off, -FLANK_TWIST, FLANK_TWIST);
359
+ if (rawU > 0.28 && rawU < 0.64 && trails) {
360
+ if (!E.trail) {
361
+ E.trail = trails.acquire();
362
+ if (E.trail) {
363
+ E.trail.n = 0;
364
+ E.trail.life = 0.22;
365
+ E.trail.mesh.visible = true;
366
+ }
367
+ }
368
+ const blade = (E.left ? a.itemL : a.itemR) || a.handR;
369
+ if (E.trail && blade) {
370
+ const L = weaponLengths[a.def.look.item] || 0.5;
371
+ _pa.set(0, 0, 0.58 * L);
372
+ _pb.set(0, 0, L);
373
+ blade.localToWorld(_pa);
374
+ blade.localToWorld(_pb);
375
+ trails.push(E.trail, _pa, _pb);
376
+ E.trail.life = 0.22;
377
+ }
378
+ }
379
+ }
380
+ let guardOff = 0;
381
+ if (E.guard && d.pose !== 'dead') guardOff = sideOn(d, -nx, -nz, E.guard);
382
+ else if (d.pose !== 'dead' && !(wound && E.hit) && d.kind !== wingedKind) {
383
+ d.yaw = Math.atan2(-nx, -nz);
384
+ d.turn = NaN;
385
+ }
386
+ const cut = E.cut || 'overhead';
387
+ if (E.kind === 'parry' && d.kind === 'human') {
388
+ const sinceHit = E.hit ? E.t - E.hitT : -1;
389
+ _recoilOpts.heavy = heavy;
390
+ _recoilOpts.hasShield = hasShield;
391
+ sampleMeleeRecoil(_recoil, sinceHit, _recoilOpts);
392
+ parryBody(fd, cut, u, _recoil.jolt, d.def.look?.item === 'spear', hasShield, cuts);
393
+ fd.lean += _recoil.torsoLean * 0.25;
394
+ fd.tw += _recoil.torsoTwist * 0.2;
395
+ if (E.guard) fd.tw = clamp(fd.tw + guardOff, -FLANK_TWIST, FLANK_TWIST);
396
+ if (rawU > 0.24 && rawU < MELEE_MEET && !collide(d.x + nx * 0.55, d.z + nz * 0.55, 0.35)) {
397
+ d.x += nx * dt * GIVE_GROUND;
398
+ d.z += nz * dt * GIVE_GROUND;
399
+ }
400
+ if (E.hit && _recoil.pushbackSpeed > 0 && !d.mount) {
401
+ if (!collide(d.x + nx * 0.5, d.z + nz * 0.5, 0.35)) {
402
+ d.x += nx * dt * _recoil.pushbackSpeed;
403
+ d.z += nz * dt * _recoil.pushbackSpeed;
404
+ }
405
+ }
406
+ } else if (E.kind === 'dodge') {
407
+ const [across, back] = DEFAULT_DODGE_STEP[dodgeOf(cut, cuts)];
408
+ if (rawU > 0.26 && rawU < 0.58) {
409
+ const stepX = -nz * E.side * dt * across + nx * dt * back,
410
+ stepZ = nx * E.side * dt * across + nz * dt * back;
411
+ if (!collide(d.x + stepX * 3, d.z + stepZ * 3, 0.35)) {
412
+ d.x += stepX;
413
+ d.z += stepZ;
414
+ }
415
+ }
416
+ if (d.kind === 'human') dodgeBody(fd, cut, rawU, E.side, d.def.look?.item === 'spear', cuts);
417
+ } else if (wound && E.hit && d.pose !== 'dead' && d.kind === 'human') {
418
+ woundBody(fd, cut, rawU, E.side, cuts);
419
+ const drive = woundDrive(cut, cuts),
420
+ k = rawU < 0.75 ? (E.wing ? 4 : 1) : 0;
421
+ d.x += (nx * drive[9] - nz * E.side * drive[10]) * dt * k;
422
+ d.z += (nz * drive[9] + nx * E.side * drive[10]) * dt * k;
423
+ } else if (wound && E.hit && d.pose !== 'dead') {
424
+ fd.lean = -0.4;
425
+ fd.w = 1 - sstep(MELEE_MEET, 1, rawU);
426
+ if (rawU < 0.75) {
427
+ const thrown = E.wing ? 6 : 1.5;
428
+ d.x += nx * dt * thrown;
429
+ d.z += nz * dt * thrown;
430
+ }
431
+ }
432
+ if (!E.hit && rawU >= (E.wing ? STOOP_HIT : E.beast ? 0.6 : MELEE_MEET)) {
433
+ E.hit = true;
434
+ E.hitT = E.t;
435
+ const aloft = d.kind === wingedKind,
436
+ y = aloft ? d.y : d.y + 1.22 * hd;
437
+ clashPoint(E, a, d, _pc);
438
+ const mx = _pc.x,
439
+ my = _pc.y,
440
+ mz = _pc.z;
441
+ if (aloft) d.jolt = 1;
442
+ addWear(a, 0.03, 0.045, 0, 0);
443
+ addWear(d, 0.035, 0.045, 0, 0);
444
+
445
+ if (E.kind === 'parry' && d.kind === 'human') {
446
+ onParry?.(mx, my, mz, d, nx, nz, heavy, hasShield);
447
+ } else if (wound || E.kind === 'parry') {
448
+ addWear(d, 0.04, 0.03, kill ? 0.62 : 0.38, 0);
449
+ if (!E.wing && dist < 2.6) addWear(a, 0, 0, kill ? 0.2 : 0.12, 0);
450
+ onHit?.(d, y, nx, nz, ha, E.wing, E.beast, heavy, kill, aloft);
451
+ if (kill) {
452
+ const [forward, holds] = deathOf(cut, cuts);
453
+ if (!aloft) {
454
+ d.forward = E.cut ? forward : hash(list.length + i, (d.x * 5) | 0, 81) > 0.55;
455
+ d.turn = Math.atan2(-nx, -nz) + (d.forward ? 1.15 : 0.5) * (E.side > 0 ? 1 : -1);
456
+ }
457
+ fd.down = true;
458
+ d.move = null;
459
+ if (d.mount) {
460
+ d.pose = 'dead';
461
+ fd.landing = true;
462
+ if (E.beast) onFellMount?.(fd, d.mount, E.side);
463
+ } else if (d.kind === 'human' && d.pose !== 'kneel') {
464
+ d.pose = 'kneel';
465
+ fd.die = holds;
466
+ } else {
467
+ fd.w = 0;
468
+ d.pose = 'dead';
469
+ fd.landing = true;
470
+ }
471
+ }
472
+ } else {
473
+ onDodge?.(mx, my, mz, d);
474
+ }
475
+ if (!E.wing) onScuff?.(a.x, a.y, a.z, 0.85);
476
+ if (E.kind !== 'dodge') {
477
+ onFlinch?.(mx, mz, a, d);
478
+ onAlarm?.(mx, mz);
479
+ }
480
+ }
481
+ if (rawU >= 1) {
482
+ if (kill && a.kind === 'human' && a.pose === 'guard' && !foeNear(a)) a.pose = 'idle';
483
+ fa.w = 0;
484
+ fa.lean = 0;
485
+ if (!wound || d.pose === 'dead') fd.w = 0;
486
+ if (E.beast) a.fixY = E.fy;
487
+ list.splice(i, 1);
488
+ }
489
+ }
490
+ }
491
+
492
+ function clear() {
493
+ for (let i = 0; i < list.length; i++) if (list[i].fy !== undefined) list[i].a.fixY = list[i].fy;
494
+ list.length = 0;
495
+ }
496
+
497
+ return { list, startAttack, updateExchanges, applyMeleeContactIK, clear };
498
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Combat reactions for actors not directly in a melee exchange: bystander flinches away from nearby
3
+ * blows, asymmetric knee-buckling death collapses, and sustained bracing under beam impacts.
4
+ * @module
5
+ */
6
+
7
+ import { solveCollapseReaction } from './action-dynamics.js';
8
+ import { clamp } from '../math/scalar.js';
9
+
10
+ const LEVEL = 2.5, // metres vertical separation within which a blow startles bystanders
11
+ THROWN = 0.55, // seconds from lethal impact until knees give way
12
+ SETTLE_DURATION = 0.48;
13
+
14
+ /**
15
+ * Creates bystander flinch, collapse, and brace controllers for a scene's actor list.
16
+ * @param {object} options
17
+ * @param {object[]} options.actorList Live array of actors to scan each frame.
18
+ * @param {() => string} options.getSpaceId Returns the active space identifier.
19
+ * @param {(x: number, z: number, radius: number) => boolean} [options.collide] Obstacle check at `(x, z)`.
20
+ * @param {() => number} options.getElapsed Returns elapsed simulation time in seconds.
21
+ * @param {(actor: object) => object} options.fightFx Ensures and returns an actor's fight state object.
22
+ * @returns {{ combatFlinch: (x: number, z: number, attacker: object, defender: object) => void, throwDown: (actor: object) => void, updateReactions: (dt: number) => void }}
23
+ */
24
+ export function createMeleeReactions({ actorList, getSpaceId, collide = () => false, getElapsed, fightFx }) {
25
+ const settleTimers = new WeakMap();
26
+
27
+ function combatFlinch(x, z, attacker, defender) {
28
+ const spaceId = getSpaceId();
29
+ for (let i = 0; i < actorList.length; i++) {
30
+ const o = actorList[i];
31
+ if (
32
+ o === attacker ||
33
+ o === defender ||
34
+ !o.active ||
35
+ o.kind !== 'human' ||
36
+ o.space !== spaceId ||
37
+ (o.pose !== 'idle' && o.pose !== 'talk' && o.pose !== 'point')
38
+ )
39
+ continue;
40
+ const dx = o.x - x,
41
+ dz = o.z - z,
42
+ l = Math.hypot(dx, dz);
43
+ if (l > 7 || l < 0.01 || Math.abs(o.y - attacker.y) > LEVEL) continue;
44
+ const X = fightFx(o);
45
+ if (X.fl <= 0) {
46
+ X.fl = 0.75;
47
+ X.bx = dx / l;
48
+ X.bz = dz / l;
49
+ }
50
+ }
51
+ }
52
+
53
+ function throwDown(actor) {
54
+ const X = fightFx(actor);
55
+ X.down = true;
56
+ X.die = X.thrown = THROWN;
57
+ actor.pose = 'kneel';
58
+ settleTimers.set(actor, SETTLE_DURATION);
59
+ }
60
+
61
+ function updateReactions(dt) {
62
+ const elapsed = getElapsed();
63
+ for (let i = 0; i < actorList.length; i++) {
64
+ const a = actorList[i],
65
+ X = a.fx_;
66
+ if (!X || X.busy) continue;
67
+ if (X.cw > 0 && (X.cw -= dt) <= 0 && a.pose === 'cower') a.pose = 'idle';
68
+ if (X.thrown > 0) {
69
+ const since = THROWN - X.thrown;
70
+ X.thrown -= dt;
71
+ const r = solveCollapseReaction(since, THROWN, 0, a.seed);
72
+ X.ra = r.armRight;
73
+ X.la = r.armLeft;
74
+ X.lw = 1;
75
+ X.foreR = r.foreRight;
76
+ X.foreL = r.foreLeft;
77
+ X.outR = r.outRight;
78
+ X.outL = r.outLeft;
79
+ X.nod = r.headNod;
80
+ X.turn = r.headTurn;
81
+ X.lean = r.trunkLean;
82
+ X.tw = r.trunkTwist;
83
+ X.sink = r.hipSink;
84
+ X.w = r.weight;
85
+ const stepBack = r.staggerSpeed * dt,
86
+ nx = a.x - Math.sin(a.yaw) * stepBack,
87
+ nz = a.z - Math.cos(a.yaw) * stepBack;
88
+ if (!collide(nx, nz, 0.35)) {
89
+ a.x = nx;
90
+ a.z = nz;
91
+ }
92
+ } else if (a.pose === 'dead' && (settleTimers.get(a) || 0) > 0) {
93
+ const rem = Math.max(0, settleTimers.get(a) - dt);
94
+ settleTimers.set(a, rem);
95
+ const r = solveCollapseReaction(THROWN + 0.01, THROWN, SETTLE_DURATION - rem, a.seed);
96
+ X.ra = r.armRight;
97
+ X.la = r.armLeft;
98
+ X.lw = 1;
99
+ X.foreR = r.foreRight;
100
+ X.foreL = r.foreLeft;
101
+ X.outR = r.outRight;
102
+ X.outL = r.outLeft;
103
+ X.nod = r.headNod;
104
+ X.turn = r.headTurn;
105
+ X.lean = r.trunkLean;
106
+ X.tw = r.trunkTwist;
107
+ X.w = r.weight;
108
+ } else if (X.fl > 0) {
109
+ X.fl -= dt;
110
+ const p = Math.sin(Math.PI * clamp(1 - X.fl / 0.75, 0, 1));
111
+ X.ra = -1.15;
112
+ X.la = -1.35;
113
+ X.lw = 1;
114
+ X.foreR = X.foreL = -1.3;
115
+ X.outR = -0.2;
116
+ X.outL = 0.2;
117
+ X.nod = 0.25 * p;
118
+ X.lean = -0.2;
119
+ X.tw = 0;
120
+ X.w = p;
121
+ const nx = a.x + X.bx * dt * 1.1 * p,
122
+ nz = a.z + X.bz * dt * 1.1 * p;
123
+ if (!collide(nx, nz, 0.35)) {
124
+ a.x = nx;
125
+ a.z = nz;
126
+ }
127
+ } else if (X.br > 0) {
128
+ X.br -= dt;
129
+ X.ra = -1.0;
130
+ X.la = -1.0;
131
+ X.lw = 0.8;
132
+ X.foreR = X.foreL = -1.15;
133
+ X.lean = -0.17 + Math.sin(elapsed * 31 + i) * 0.035;
134
+ X.tw = Math.sin(elapsed * 23 + i * 2) * 0.05;
135
+ X.w += (0.85 - X.w) * Math.min(1, dt * 9);
136
+ const nx = a.x + X.bx * dt * 0.22,
137
+ nz = a.z + X.bz * dt * 0.22;
138
+ if (!collide(nx, nz, 0.35)) {
139
+ a.x = nx;
140
+ a.z = nz;
141
+ }
142
+ } else if (X.w > 0) X.w = Math.max(0, X.w - dt * 4);
143
+ }
144
+ }
145
+
146
+ return { combatFlinch, throwDown, updateReactions };
147
+ }