roamwright 0.61.19 → 0.61.21

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.
@@ -0,0 +1,395 @@
1
+ /**
2
+ * Procedural melee cut keyframes: wind-up, contact, follow-through, parry, dodge, wound recoil,
3
+ * and death-fall direction profiles for blades and polearms.
4
+ * Writes directly into a figure's fight-state object with zero per-frame allocation.
5
+ * @module
6
+ */
7
+
8
+ import { lerp, sstep } from '../math/scalar.js';
9
+ import { SPEAR_GUARD } from './figure-arms.js';
10
+
11
+ // Row indices: right upper arm, right out, right elbow, wrist (or spear shaft pitch), trunk twist,
12
+ // trunk lean, hip sink, hip forward shift, plus optional left-arm indices for two-handed polearms.
13
+ const ARM = 0,
14
+ OUT = 1,
15
+ ELBOW = 2,
16
+ WRIST = 3,
17
+ TWIST = 4,
18
+ LEAN = 5,
19
+ SINK = 6,
20
+ SHIFT = 7,
21
+ ARM_L = 8,
22
+ OUT_L = 9,
23
+ ELBOW_L = 10;
24
+
25
+ // On-guard stance where one-handed cuts begin (matches the guard pose in figure-pose-table.js).
26
+ const READY = [-0.42, -0.12, -1.25, 0.3, 0, 0.02, -0.03, 0];
27
+
28
+ /**
29
+ * Default melee cut definitions keyed by cut name. Each entry holds `wind`, `meet`, `thru`, and `parry`
30
+ * pose rows, optional `ready` row for two-handed polearms, `dodge` evasion kind, and struck body `zone`.
31
+ */
32
+ export const DEFAULT_CUTS = Object.freeze({
33
+ overhead: {
34
+ wind: [-2.85, -0.15, -1.65, 0.65, -0.5, -0.15, -0.05, -0.1],
35
+ meet: [-1.32, 0.05, -0.14, 0, 0.4, 0.25, -0.12, 0.13],
36
+ thru: [-0.35, 0.15, -0.3, -0.2, 0.55, 0.27, -0.16, 0.15],
37
+ parry: [-2.65, 0.3, -1.25, 0.5, -0.18, -0.12, -0.1, -0.04],
38
+ dodge: 'aside',
39
+ zone: 'head',
40
+ },
41
+ diagonal: {
42
+ wind: [-2.45, -0.9, -1.5, 0.5, -0.75, -0.1, -0.05, -0.09],
43
+ meet: [-1.45, 0.25, -0.18, 0, 0.46, 0.18, -0.11, 0.12],
44
+ thru: [-0.65, 0.8, -0.35, -0.1, 0.78, 0.28, -0.15, 0.14],
45
+ parry: [-2.05, -0.5, -1.4, 0.35, -0.34, -0.09, -0.09, -0.05],
46
+ dodge: 'aside',
47
+ zone: 'shoulder',
48
+ },
49
+ flat: {
50
+ wind: [-1.35, -1.25, -1.2, 0.2, -0.88, -0.05, -0.07, -0.08],
51
+ meet: [-1.45, 0, -0.1, 0, 0.38, 0.12, -0.12, 0.11],
52
+ thru: [-1.2, 0.95, -0.3, 0, 0.88, 0.15, -0.15, 0.12],
53
+ parry: [-1.15, -0.7, -1.55, 0.15, -0.38, -0.06, -0.12, -0.05],
54
+ dodge: 'duck',
55
+ zone: 'flank',
56
+ },
57
+ thrust: {
58
+ wind: [-0.75, -0.25, -2.0, 0.2, -0.44, -0.12, -0.07, -0.12],
59
+ meet: [-1.52, 0.05, -0.04, -0.15, 0.36, 0.33, -0.19, 0.23],
60
+ thru: [-1.55, 0.1, 0, -0.15, 0.4, 0.38, -0.22, 0.25],
61
+ parry: [-1.35, 0.35, -0.9, 0.2, 0.28, -0.13, -0.08, -0.06],
62
+ dodge: 'turn',
63
+ zone: 'belly',
64
+ },
65
+ lunge: {
66
+ ready: SPEAR_GUARD,
67
+ wind: [0.35, -0.15, -0.8, -0.05, -0.55, -0.02, -0.07, -0.1, -0.3, -0.2, -0.5],
68
+ meet: [-0.5, 0, -0.3, -0.15, 0.25, 0.3, -0.16, 0.2, -0.9, -0.3, -0.2],
69
+ thru: [-0.6, 0.05, -0.2, -0.12, 0.32, 0.34, -0.19, 0.22, -1.0, -0.3, -0.15],
70
+ parry: [-1.35, 0.35, -0.9, 0.2, 0.25, -0.12, -0.06, -0.05],
71
+ dodge: 'turn',
72
+ zone: 'shoulder',
73
+ },
74
+ low: {
75
+ ready: SPEAR_GUARD,
76
+ wind: [0.35, -0.15, -0.7, 0.2, -0.5, 0.05, -0.1, -0.1, -0.3, -0.2, -0.55],
77
+ meet: [-0.35, 0, -0.35, 0.3, 0.25, 0.4, -0.22, 0.2, -0.7, -0.3, -0.3],
78
+ thru: [-0.45, 0.05, -0.25, 0.28, 0.3, 0.44, -0.24, 0.22, -0.8, -0.3, -0.25],
79
+ parry: [-1.15, -0.7, -1.55, 0.15, -0.35, -0.05, -0.11, -0.04],
80
+ dodge: 'aside',
81
+ zone: 'belly',
82
+ },
83
+ butt: {
84
+ ready: SPEAR_GUARD,
85
+ wind: [-0.2, -0.1, -0.7, 0.3, -0.45, -0.02, -0.06, -0.05, -0.7, -0.15, -0.6],
86
+ meet: [0.55, 0.05, -0.05, 1.7, 0.45, 0.18, -0.12, 0.14, 0.2, 0.1, -0.5],
87
+ thru: [0.7, 0.1, 0, 1.9, 0.5, 0.2, -0.13, 0.15, 0.25, 0.1, -0.45],
88
+ parry: [-2.65, 0.3, -1.25, 0.5, -0.15, -0.1, -0.09, -0.03],
89
+ dodge: 'duck',
90
+ zone: 'head',
91
+ },
92
+ });
93
+
94
+ /**
95
+ * Two-handed polearm shaft-block pose row (right hand low, left hand high across the body).
96
+ */
97
+ export const SPEAR_PARRY = Object.freeze([-0.52, -0.2, -0.88, -0.45, -0.14, -0.08, -0.09, -0.04, -1.18, -0.22, -0.92]);
98
+
99
+ /**
100
+ * Radians a blocking spear shaft turns across the body from pointing straight ahead.
101
+ */
102
+ export const SPEAR_ACROSS = -1.2;
103
+
104
+ /**
105
+ * Default weighted cut preferences per generic weapon kind. Worlds may merge custom weapon keys.
106
+ */
107
+ export const DEFAULT_CUT_STYLES = Object.freeze({
108
+ rapier: ['thrust', 'thrust', 'diagonal', 'thrust', 'flat'],
109
+ daggers: ['thrust', 'diagonal', 'flat', 'thrust'],
110
+ scimitars: ['diagonal', 'flat', 'diagonal', 'thrust', 'flat'],
111
+ spear: ['lunge', 'low', 'lunge', 'butt'],
112
+ axe: ['overhead', 'diagonal', 'diagonal', 'flat'],
113
+ any: ['diagonal', 'overhead', 'flat', 'thrust', 'diagonal', 'flat'],
114
+ });
115
+
116
+ const WIND = 0.3;
117
+
118
+ /**
119
+ * Normalized strike phase `[0, 1]` at which weapon contact occurs.
120
+ */
121
+ export const MELEE_MEET = 0.45;
122
+
123
+ /**
124
+ * Normalized strike phase `[0, 1]` at which follow-through completes.
125
+ */
126
+ export const MELEE_THRU = 0.6;
127
+
128
+ const BACK = 0.64,
129
+ REBOUND = 0.3,
130
+ OVERREACH = 1.18,
131
+ OFF_HAND = [-0.4, 0.1, -0.1],
132
+ // Fraction of cut lean, hip sink, and forward shift transferred to the body so strikes stay balanced.
133
+ INTO_BLOW = [0.7, 0.5, 0.6];
134
+
135
+ const _row = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
136
+
137
+ function defaultCutHash(x, y, salt) {
138
+ const s = Math.sin(x * 127.1 + y * 311.7 + salt * 74.7) * 43758.5453123;
139
+ return s - Math.floor(s);
140
+ }
141
+
142
+ /**
143
+ * Picks which cut keyframe `actor` executes on its `strikeIndex`-th blow using a deterministic `hash(a, b, salt)` function.
144
+ * @param {object} actor Actor record with optional `seed` and `def.look.item`.
145
+ * @param {number} strikeIndex Monotonic strike counter.
146
+ * @param {(x: number, y: number, salt: number) => number} [hash] Deterministic `[0, 1)` hash function.
147
+ * @param {Record<string, readonly string[]>} [styles] Weapon-to-cut preference table.
148
+ * @returns {string} Cut key in {@link DEFAULT_CUTS}.
149
+ */
150
+ export function pickCut(actor, strikeIndex, hash = defaultCutHash, styles = DEFAULT_CUT_STYLES) {
151
+ const style = styles[actor?.def?.look?.item] || styles.any || DEFAULT_CUT_STYLES.any;
152
+ return style[Math.floor(hash(strikeIndex, (actor?.seed || 0) * 97, 83) * style.length) % style.length];
153
+ }
154
+
155
+ function blend(from, to, k) {
156
+ for (let j = 0; j < from.length; j++) _row[j] = from[j] + (to[j] - from[j]) * k;
157
+ return _row;
158
+ }
159
+
160
+ /**
161
+ * Evaluates interpolated strike joint targets into a shared scratch array at normalized phase `u` in `[0, 1]`.
162
+ * @param {string} cut Cut name in `cuts`.
163
+ * @param {number} u Normalized strike phase in `[0, 1]`.
164
+ * @param {boolean} stopped True when stopped by a parry (springs back toward wind-up).
165
+ * @param {boolean} missed True when cutting through air (overreaches slightly).
166
+ * @param {Record<string, object>} [cuts] Cut definition table.
167
+ * @returns {number[]} Shared scratch row array (do not retain across calls).
168
+ */
169
+ export function swingRow(cut, u, stopped, missed, cuts = DEFAULT_CUTS) {
170
+ const C = cuts[cut];
171
+ if (u < WIND) return blend(C.ready || READY, C.wind, sstep(0, WIND, u));
172
+ if (u < MELEE_MEET) {
173
+ const k = (u - WIND) / (MELEE_MEET - WIND);
174
+ return blend(C.wind, C.meet, k * k);
175
+ }
176
+ const k = Math.min(1, (u - MELEE_MEET) / (MELEE_THRU - MELEE_MEET)),
177
+ out = 1 - (1 - k) * (1 - k);
178
+ if (stopped) return blend(C.meet, C.wind, REBOUND * Math.sin(Math.PI * Math.min(1, k * 0.8)));
179
+ if (!missed) return blend(C.meet, C.thru, out);
180
+ blend(C.meet, C.thru, out * OVERREACH);
181
+ return _row;
182
+ }
183
+
184
+ /**
185
+ * Writes the attacker's pose channels into fight state `X` at normalized strike phase `u`.
186
+ * @param {object} X Actor fight state object.
187
+ * @param {string} cut Cut name in `cuts`.
188
+ * @param {number} u Normalized strike phase in `[0, 1]`.
189
+ * @param {boolean} stopped True when met by a parry.
190
+ * @param {boolean} missed True when dodged.
191
+ * @param {boolean} left True when striking with the left arm.
192
+ * @param {Record<string, object>} [cuts] Cut definition table.
193
+ */
194
+ export function swingBody(X, cut, u, stopped, missed, left, cuts = DEFAULT_CUTS) {
195
+ const r = swingRow(cut, u, stopped, missed, cuts),
196
+ off =
197
+ u < WIND
198
+ ? lerp(0, OFF_HAND[0], sstep(0, WIND, u))
199
+ : u < MELEE_MEET
200
+ ? lerp(OFF_HAND[0], OFF_HAND[1], sstep(WIND, MELEE_MEET, u))
201
+ : OFF_HAND[2],
202
+ turn = left ? -1 : 1,
203
+ spear = !!cuts[cut].ready;
204
+ X.ra = left ? off : r[ARM];
205
+ X.la = spear ? r[ARM_L] : left ? r[ARM] : off;
206
+ X.outR = left ? -0.15 : r[OUT];
207
+ X.outL = spear ? r[OUT_L] : left ? -r[OUT] : 0.15;
208
+ X.foreR = left ? -0.7 : r[ELBOW];
209
+ X.foreL = spear ? r[ELBOW_L] : left ? r[ELBOW] : -0.7;
210
+ X.lw = left || spear ? 1 : 0.7;
211
+ X.across = 0;
212
+ X.cover = 0;
213
+ X.wrist = r[WRIST];
214
+ X.tw = r[TWIST] * turn;
215
+ X.lean = r[LEAN] * INTO_BLOW[0];
216
+ X.sink = r[SINK] * INTO_BLOW[1];
217
+ X.shift = r[SHIFT] * INTO_BLOW[2];
218
+ X.nod = -0.1 * r[LEAN];
219
+ X.w = Math.min(1, u * 9) * (1 - sstep(BACK, 1, u));
220
+ }
221
+
222
+ /**
223
+ * Writes the defender's parry or shield-block pose channels into fight state `X`.
224
+ * @param {object} X Actor fight state object.
225
+ * @param {string} cut Incoming cut name in `cuts`.
226
+ * @param {number} u Normalized exchange phase in `[0, 1]`.
227
+ * @param {number} jolt Impact recoil jolt magnitude (`0..1.45`).
228
+ * @param {boolean} spear True when blocking with a two-handed polearm shaft.
229
+ * @param {boolean} [hasShield] True when absorbing impact on a shield.
230
+ * @param {Record<string, object>} [cuts] Cut definition table.
231
+ */
232
+ export function parryBody(X, cut, u, jolt, spear, hasShield = false, cuts = DEFAULT_CUTS) {
233
+ const p = spear ? SPEAR_PARRY : cuts[cut].parry;
234
+ if (hasShield) {
235
+ // Shield bearer turns their left shoulder into the blow, cocking the weapon arm clear to counter while absorbing shock.
236
+ X.ra = -0.82 - 0.18 * jolt;
237
+ X.outR = -0.35;
238
+ X.foreR = -1.18 - 0.12 * jolt;
239
+ X.la = -0.75 + 0.25 * jolt;
240
+ X.outL = -0.18;
241
+ X.foreL = -0.95 + 0.18 * jolt;
242
+ X.lw = 0.9;
243
+ X.across = 0;
244
+ X.cover = 1;
245
+ X.wrist = 0.25;
246
+ X.tw = -0.42 - 0.22 * jolt;
247
+ X.lean = -0.06 - 0.18 * jolt;
248
+ X.sink = -0.14 - 0.06 * jolt;
249
+ X.shift = -0.06 - 0.07 * jolt;
250
+ X.nod = 0.15 * jolt;
251
+ } else {
252
+ X.ra = p[ARM] + 0.44 * jolt;
253
+ X.outR = p[OUT] + 0.08 * jolt;
254
+ X.foreR = p[ELBOW] + 0.28 * jolt;
255
+ X.la = spear ? p[ARM_L] + 0.16 * jolt : -0.48 - 0.22 * jolt;
256
+ X.outL = spear ? p[OUT_L] : 0.22;
257
+ X.foreL = spear ? p[ELBOW_L] + 0.24 * jolt : -0.84;
258
+ X.lw = spear ? 1 : 0.55;
259
+ X.across = spear ? SPEAR_ACROSS : 0;
260
+ X.cover = 1;
261
+ X.wrist = p[WRIST] - 0.12 * jolt;
262
+ X.tw = p[TWIST] - 0.2 * jolt;
263
+ X.lean = p[LEAN] - 0.16 * jolt;
264
+ X.sink = p[SINK] - 0.06 * jolt;
265
+ X.shift = p[SHIFT] - 0.07 * jolt;
266
+ X.nod = 0.14 * jolt;
267
+ }
268
+ X.w = sstep(0.16, 0.41, u) * (1 - sstep(0.68, 0.98, u));
269
+ }
270
+
271
+ const DODGE_ARMS = [-1.1, 0, -0.9, 0.2, 0, 0, 0, 0, -0.7, 0, -0.8];
272
+
273
+ /**
274
+ * Writes the defender's dodge pose (sidestep, duck, or edge-on turn) into fight state `X`.
275
+ * @param {object} X Actor fight state object.
276
+ * @param {string} cut Incoming cut name in `cuts`.
277
+ * @param {number} u Normalized exchange phase in `[0, 1]`.
278
+ * @param {number} side Lateral dodge direction (`1` or `-1`).
279
+ * @param {boolean} spear True when keeping a polearm in its two-handed guard.
280
+ * @param {Record<string, object>} [cuts] Cut definition table.
281
+ */
282
+ export function dodgeBody(X, cut, u, side, spear, cuts = DEFAULT_CUTS) {
283
+ const how = cuts[cut].dodge,
284
+ G = spear ? SPEAR_GUARD : DODGE_ARMS;
285
+ X.ra = G[ARM];
286
+ X.outR = G[OUT];
287
+ X.foreR = G[ELBOW];
288
+ X.wrist = G[WRIST];
289
+ X.la = G[ARM_L];
290
+ X.outL = G[OUT_L];
291
+ X.foreL = G[ELBOW_L];
292
+ X.lw = spear ? 1 : 0.7;
293
+ X.across = 0;
294
+ X.cover = 0;
295
+ X.nod = 0;
296
+ if (how === 'duck') {
297
+ X.lean = 0.45;
298
+ X.sink = -0.3;
299
+ X.tw = 0.2 * side;
300
+ X.shift = -0.04;
301
+ X.nod = 0.25;
302
+ } else if (how === 'turn') {
303
+ X.lean = -0.1;
304
+ X.sink = -0.08;
305
+ X.tw = 0.85 * side;
306
+ X.shift = -0.06;
307
+ } else {
308
+ X.lean = 0.16;
309
+ X.sink = -0.14;
310
+ X.tw = 0.45 * side;
311
+ X.shift = 0;
312
+ }
313
+ X.w = sstep(0.22, 0.42, u) * (1 - sstep(0.7, 1, u));
314
+ }
315
+
316
+ /**
317
+ * Dodge displacement speeds in metres per second: `[acrossLine, backAlongLine]` per dodge kind.
318
+ */
319
+ export const DEFAULT_DODGE_STEP = Object.freeze({
320
+ aside: [2.7, 0.9],
321
+ duck: [0.4, 1.2],
322
+ turn: [1.4, 0.5],
323
+ });
324
+
325
+ // Struck reaction rows by zone: [armR, armL, elbowR, elbowL, twist, lean, sink, headNod, headTurn, backMps, asideMps].
326
+ const WOUNDS = {
327
+ head: [-1.55, -1.8, -1.0, -1.1, 0.15, -0.5, -0.17, -0.55, 0.4, 0.85, 0.1],
328
+ shoulder: [0.2, -1.0, -0.2, -2.0, -0.7, -0.3, -0.12, 0.1, 0.35, 0.6, 0.3],
329
+ flank: [-0.75, -0.8, -1.85, -1.9, 0.55, 0.42, -0.15, 0.3, 0.1, 0.4, 0.5],
330
+ belly: [-0.6, -0.7, -2.0, -2.05, 0.1, 0.58, -0.2, 0.4, 0, 0.4, 0],
331
+ };
332
+
333
+ const woundWeight = u => sstep(MELEE_MEET - 0.02, MELEE_MEET + 0.1, u) * (1 - sstep(MELEE_MEET + 0.1, 1, u));
334
+
335
+ /**
336
+ * Writes a struck body's wound reaction pose channels into fight state `X`.
337
+ * @param {object} X Actor fight state object.
338
+ * @param {string} cut Struck cut name in `cuts`.
339
+ * @param {number} u Normalized exchange phase in `[0, 1]`.
340
+ * @param {number} side Side the blow arrived from (`1` or `-1`).
341
+ * @param {Record<string, object>} [cuts] Cut definition table.
342
+ */
343
+ export function woundBody(X, cut, u, side, cuts = DEFAULT_CUTS) {
344
+ const W = WOUNDS[cuts[cut].zone];
345
+ X.ra = W[0];
346
+ X.la = W[1];
347
+ X.foreR = W[2];
348
+ X.foreL = W[3];
349
+ X.outR = -0.25;
350
+ X.outL = 0.25;
351
+ X.lw = 1;
352
+ X.wrist = 0;
353
+ X.across = 0;
354
+ X.cover = -1;
355
+ X.tw = W[4] * side;
356
+ X.lean = W[5] * 0.65;
357
+ X.sink = W[6] * 0.7;
358
+ X.shift = -0.05;
359
+ X.nod = W[7];
360
+ X.turn = W[8] * side;
361
+ X.w = woundWeight(u);
362
+ }
363
+
364
+ /**
365
+ * Returns the wound reaction row whose indices 9 and 10 hold back and lateral stagger speeds (m/s).
366
+ * @param {string} cut Cut name in `cuts`.
367
+ * @param {Record<string, object>} [cuts] Cut definition table.
368
+ * @returns {readonly number[]} Wound table row.
369
+ */
370
+ export function woundDrive(cut, cuts = DEFAULT_CUTS) {
371
+ return WOUNDS[cuts[cut].zone];
372
+ }
373
+
374
+ // [fallsForward, kneeHoldSeconds] by struck zone.
375
+ const DEATHS = { head: [false, 0.18], shoulder: [false, 0.38], flank: [true, 0.55], belly: [true, 0.85] };
376
+
377
+ /**
378
+ * Returns `[fallsForward, kneeHoldSeconds]` for a lethal blow of `cut`.
379
+ * @param {string} cut Cut name in `cuts`.
380
+ * @param {Record<string, object>} [cuts] Cut definition table.
381
+ * @returns {[boolean, number]} Whether the body folds forward and seconds before collapsing.
382
+ */
383
+ export function deathOf(cut, cuts = DEFAULT_CUTS) {
384
+ return DEATHS[cuts[cut].zone];
385
+ }
386
+
387
+ /**
388
+ * Returns the evasion kind (`'aside'`, `'duck'`, or `'turn'`) associated with `cut`.
389
+ * @param {string} cut Cut name in `cuts`.
390
+ * @param {Record<string, object>} [cuts] Cut definition table.
391
+ * @returns {string} Key into {@link DEFAULT_DODGE_STEP}.
392
+ */
393
+ export function dodgeOf(cut, cuts = DEFAULT_CUTS) {
394
+ return cuts[cut].dodge;
395
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Running melee duels: schedules brisk flurries of exchanges between two fighters while keeping
3
+ * them circling, measuring weapon distance, and pressing/yielding ground without stepping into obstacles.
4
+ * @module
5
+ */
6
+
7
+ import { lerp } from '../math/scalar.js';
8
+
9
+ const PRESS = 1.25, // metres/s max ground pressure shift during a flurry
10
+ DUEL_ROOM = 2.4, // metres maximum drift from where the duel opened
11
+ REGAIN = 0.95, // metres/s rate at which duelists recover toward their held ground
12
+ CIRCLE = 0.85, // radians/s circling angular speed between flurries
13
+ OUT_OF_MEASURE = 0.26, // metres beyond weapon contact distance kept between flurries
14
+ BREAK_OFF = 0.36, // probability a strike ends a flurry
15
+ CLOSING = 0.32; // seconds before an exchange when the pair close into measure
16
+
17
+ /**
18
+ * Creates a stateful runner for multi-exchange duels.
19
+ * @param {object} options
20
+ * @param {(attacker: object, defender: object, kind?: string, dur?: number) => object | null} options.startAttack Starts one melee exchange.
21
+ * @param {(x: number, z: number, radius: number) => boolean} [options.collide] Obstacle test at `(x, z)` with `radius`.
22
+ * @param {(x: number, y: number, salt: number) => number} options.hash Deterministic `[0, 1)` hash function.
23
+ * @param {(a: object, b: object) => number} options.duelMeasure Preferred sword-measure distance between `a` and `b`.
24
+ * @param {(actor: object, dust?: number, sweat?: number, blood?: number, scorch?: number) => void} [options.addWear] Progressive battle-wear accumulator.
25
+ * @returns {{ duels: object[], startDuel: Function, updateDuels: (dt: number) => void, clear: () => void }}
26
+ */
27
+ export function createMeleeDuels({ startAttack, collide = () => false, hash, duelMeasure, addWear = () => {} }) {
28
+ const duels = [];
29
+
30
+ function startDuel(a, b, dur, end, endBy, seed) {
31
+ duels.push({
32
+ a,
33
+ b,
34
+ t: 0,
35
+ dur: dur || 6,
36
+ end: end || null,
37
+ endBy: endBy || 'a',
38
+ n: 0,
39
+ gap: 0.24,
40
+ seed,
41
+ cur: null,
42
+ circ: 0,
43
+ circTo: CIRCLE,
44
+ spell: 0.55,
45
+ spells: 0,
46
+ measure: 0,
47
+ hx: 0,
48
+ hz: 0,
49
+ press: 0,
50
+ });
51
+ }
52
+
53
+ function updateDuels(dt) {
54
+ for (let i = duels.length - 1; i >= 0; i--) {
55
+ const D = duels[i];
56
+ D.t += dt;
57
+ const left = D.dur - D.t,
58
+ a = D.a,
59
+ b = D.b;
60
+ if (a.pose === 'dead' || b.pose === 'dead' || left <= 0) {
61
+ duels.splice(i, 1);
62
+ continue;
63
+ }
64
+ addWear(a, dt * 0.018, dt * 0.045, 0, 0);
65
+ addWear(b, dt * 0.018, dt * 0.045, 0, 0);
66
+ if (D.cur && D.cur.t >= D.cur.dur) D.cur = null;
67
+ if (!D.cur) {
68
+ const dx = b.x - a.x,
69
+ dz = b.z - a.z,
70
+ dist = Math.hypot(dx, dz) || 1,
71
+ mx = (a.x + b.x) / 2,
72
+ mz = (a.z + b.z) / 2,
73
+ reach = duelMeasure(a, b);
74
+ if ((D.spell -= dt) <= 0) {
75
+ const h = hash(D.spells++, D.seed, 84);
76
+ D.circTo = h < 0.32 ? 0 : (h < 0.66 ? 1 : -1) * CIRCLE * (0.65 + 0.4 * hash(D.spells, D.seed, 86));
77
+ D.spell = 0.42 + 0.65 * hash(D.spells, D.seed, 85);
78
+ }
79
+ D.circ += (D.circTo - D.circ) * Math.min(1, dt * 6);
80
+ const closing = D.gap < CLOSING;
81
+ D.measure += ((closing ? -0.05 : OUT_OF_MEASURE) - D.measure) * Math.min(1, dt * (closing ? 7 : 3.5));
82
+ const ang = Math.atan2(dx, dz) + dt * D.circ,
83
+ r = lerp(dist, reach + D.measure, Math.min(1, dt * 4.8)) / 2;
84
+ D.ox ??= mx;
85
+ D.oz ??= mz;
86
+ const hx = D.hx + Math.sin(ang) * D.press * dt,
87
+ hz = D.hz + Math.cos(ang) * D.press * dt;
88
+ if (
89
+ Math.hypot(hx, hz) > DUEL_ROOM ||
90
+ collide(D.ox + hx + Math.sin(ang) * r * Math.sign(D.press), D.oz + hz + Math.cos(ang) * r * Math.sign(D.press), 0.4)
91
+ )
92
+ D.press = -D.press;
93
+ else {
94
+ D.hx = hx;
95
+ D.hz = hz;
96
+ }
97
+ const gx = D.ox + D.hx - mx,
98
+ gz = D.oz + D.hz - mz,
99
+ off = Math.hypot(gx, gz),
100
+ k = off > 1e-4 ? Math.min(off, REGAIN * dt) / off : 0,
101
+ cx = mx + gx * k,
102
+ cz = mz + gz * k;
103
+ const ax = cx - Math.sin(ang) * r,
104
+ az = cz - Math.cos(ang) * r,
105
+ bx = cx + Math.sin(ang) * r,
106
+ bz = cz + Math.cos(ang) * r;
107
+ if (!collide(ax, az, 0.35)) {
108
+ a.x = ax;
109
+ a.z = az;
110
+ }
111
+ if (!collide(bx, bz, 0.35)) {
112
+ b.x = bx;
113
+ b.z = bz;
114
+ }
115
+ a.yaw = ang;
116
+ b.yaw = ang + Math.PI;
117
+ a.turn = b.turn = NaN;
118
+ if ((D.gap -= dt) <= 0) {
119
+ const h1 = hash(D.n, D.seed, 77),
120
+ fin = D.end && left < 1.45;
121
+ if (fin) {
122
+ D.cur = startAttack(D.endBy === 'b' ? b : a, D.endBy === 'b' ? a : b, D.end);
123
+ D.end = null;
124
+ D.dur = D.t + 1.3;
125
+ } else if (left > 1.5 + (D.end ? 1.3 : 0)) {
126
+ const atk = (D.n + (h1 > 0.82 ? 1 : 0)) % 2 === 0 ? a : b;
127
+ D.cur = startAttack(atk, atk === a ? b : a, h1 < 0.74 ? 'parry' : 'dodge', 0.96 + h1 * 0.24);
128
+ D.n++;
129
+ D.gap = hash(D.n, D.seed, 87) < BREAK_OFF ? 0.78 + 0.58 * hash(D.n, D.seed, 88) : 0.1 + hash(D.n, D.seed, 78) * 0.28;
130
+ D.press = (atk === a ? 1 : -1) * hash(D.n, D.seed, 80) * PRESS * 0.5;
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+
137
+ function clear() {
138
+ duels.length = 0;
139
+ }
140
+
141
+ return { duels, startDuel, updateDuels, clear };
142
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Winged stoop and quadruped leap kinematics used by {@link createMeleeExchanges}.
3
+ * @module
4
+ */
5
+
6
+ import { lerp, sstep } from '../math/scalar.js';
7
+ import { horseGap } from './melee-combat.js';
8
+
9
+ /** Normalized phase at which an aerial stoop makes contact with its target. */
10
+ export const STOOP_HIT = 0.5;
11
+
12
+ /**
13
+ * Advances a winged attacker diving toward its target and pulling out after contact.
14
+ * @param {object} exchange Active exchange record.
15
+ * @param {number} u Normalized exchange phase in `[0, 1]`.
16
+ * @param {string} wingedKind Actor kind string for winged fighters.
17
+ * @param {(actor: object) => object} fightFx Returns the attacker's combat pose scratch record.
18
+ */
19
+ export function stepWingedStoop(exchange, u, wingedKind, fightFx) {
20
+ const a = exchange.a,
21
+ d = exchange.d,
22
+ ha = a.group.scale.y;
23
+ if (u < STOOP_HIT) {
24
+ const k = u / STOOP_HIT,
25
+ dx = d.x - exchange.x0,
26
+ dz = d.z - exchange.z0,
27
+ l = Math.hypot(dx, dz) || 1,
28
+ over = d.kind === wingedKind ? 3.2 * ha : d.height * 0.8 + 3.1 * ha;
29
+ exchange.nx = dx / l;
30
+ exchange.nz = dz / l;
31
+ exchange.hx = d.x - exchange.nx * 3 * ha;
32
+ exchange.hy = d.y + over;
33
+ exchange.hz = d.z - exchange.nz * 3 * ha;
34
+ a.x = lerp(exchange.x0, exchange.hx, k * k);
35
+ a.fixY = lerp(exchange.y0, exchange.hy, k * k);
36
+ a.z = lerp(exchange.z0, exchange.hz, k * k);
37
+ a.yaw = Math.atan2(exchange.nx, exchange.nz);
38
+ } else {
39
+ const k = (u - STOOP_HIT) / (1 - STOOP_HIT),
40
+ carry = Math.min(Math.hypot(exchange.hx - exchange.x0, exchange.hz - exchange.z0), 45 * ha) * k * (2 - k);
41
+ a.x = exchange.hx + exchange.nx * carry;
42
+ a.fixY = exchange.hy + 12 * ha * k * k;
43
+ a.z = exchange.hz + exchange.nz * carry;
44
+ }
45
+ const fa = fightFx(a);
46
+ fa.tuck = sstep(0, 0.1, u) * (1 - sstep(0.3, 0.44, u));
47
+ fa.reach = sstep(0.3, 0.45, u) * (1 - sstep(0.56, 0.8, u));
48
+ }
49
+
50
+ /**
51
+ * Advances a quadruped beast crouch, leap arc, bite, and worry pose during an exchange.
52
+ * @param {object} exchange Active exchange record.
53
+ * @param {object} fa Attacker combat pose record.
54
+ * @param {number} rawU Normalized exchange phase in `[0, 1]`.
55
+ * @param {number} dt Frame step in seconds.
56
+ * @param {number} dist Current horizontal distance to target.
57
+ * @param {number} nx Normalized horizontal X vector toward target.
58
+ * @param {number} nz Normalized horizontal Z vector toward target.
59
+ * @param {number} ha Attacker vertical scale.
60
+ * @param {(x: number, z: number) => number} groundAt Ground height sampler.
61
+ */
62
+ export function stepBeastLeap(exchange, fa, rawU, dt, dist, nx, nz, ha, groundAt) {
63
+ const a = exchange.a,
64
+ d = exchange.d,
65
+ cr = sstep(0, 0.22, rawU) * (1 - sstep(0.22, 0.32, rawU)),
66
+ lp = sstep(0.24, 0.6, rawU),
67
+ reachB = Math.max(0.9 * ha, horseGap(a, d));
68
+ if (rawU > 0.24 && rawU < 0.6 && dist > reachB) {
69
+ const st = Math.min(dist - reachB, (dt * (dist - reachB)) / Math.max(0.02, (0.6 - rawU) * exchange.dur));
70
+ a.x += nx * st;
71
+ a.z += nz * st;
72
+ }
73
+ const arc = Math.sin(lp * Math.PI) * (0.5 + 0.35 * ha),
74
+ gy = exchange.fy ?? groundAt(a.x, a.z);
75
+ a.fixY = gy + arc - cr * 0.22 * ha;
76
+ fa.lean =
77
+ cr * 0.28 -
78
+ Math.sin(lp * Math.PI) * 0.45 * (1 - lp) +
79
+ (rawU > 0.6 ? Math.sin(exchange.t * 26) * 0.09 * (1 - sstep(0.75, 1, rawU)) : 0) +
80
+ lp * (1 - sstep(0.6, 0.75, rawU)) * 0.5;
81
+ fa.w = 1;
82
+ fa.cr = cr;
83
+ fa.reach = lp * (1 - sstep(0.6, 0.72, rawU));
84
+ fa.jaw = sstep(0.3, 0.56, rawU) * (1 - sstep(0.6, 0.64, rawU));
85
+ fa.worry = rawU > 0.6 ? 1 - sstep(0.82, 1, rawU) : 0;
86
+ }