roamwright 0.61.26 → 0.61.27
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.
- package/README.md +2 -1
- package/examples/troops.html +56 -0
- package/examples/troops.js +309 -0
- package/geometry/index.js +171 -0
- package/index.js +6 -469
- package/math/index.js +8 -0
- package/motion/index.js +100 -0
- package/motion/troop-director.js +482 -0
- package/motion/troop-volleys.js +197 -0
- package/package.json +1 -1
- package/rendering/index.js +178 -0
- package/rendering/troop-director.js +374 -0
- package/rendering/troop-mounts.js +326 -0
- package/rendering/troop-standards.js +259 -0
- package/rendering/troop-volleys.js +143 -0
- package/runtime/index.js +95 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-safe troop formation director state and timing: mustering organic staggered ranks,
|
|
3
|
+
* smooth march easing, line-on-line melee pockets, casualties and recovery, opponent engagement
|
|
4
|
+
* and volley targeting, and scene cue dispatch (`go`, `fight`, `fall`, `rise`, `role`, `show`,
|
|
5
|
+
* `hide`). Zero Three.js dependencies and zero per-frame allocations.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { clamp, sstep } from '../math/scalar.js';
|
|
10
|
+
import { formationSlot, marchTiming, meleeOffset } from './formation.js';
|
|
11
|
+
import { TROOP_VOLLEY_DEFAULTS } from './troop-volleys.js';
|
|
12
|
+
|
|
13
|
+
/** Default height in metres from ground to a mounted rider's seat over the horse's stride rise. */
|
|
14
|
+
export const TROOP_SADDLE = 1.2;
|
|
15
|
+
|
|
16
|
+
/** Default offsets (metres) of a wagon's paired draft oxen ahead of its bed and either side of the pole. */
|
|
17
|
+
export const TROOP_TEAM = { ahead: 3.5, apart: 0.62 };
|
|
18
|
+
|
|
19
|
+
/** Default camera distance in metres within which a raised host updates and renders. */
|
|
20
|
+
export const TROOP_SEEN_FROM = 900;
|
|
21
|
+
|
|
22
|
+
/** Crowd figure `home` sentinel value that hides a figure immediately. */
|
|
23
|
+
export const TROOP_UNSEEN = -1;
|
|
24
|
+
|
|
25
|
+
/** Share of a march spent accelerating from rest and decelerating to a halt. */
|
|
26
|
+
export const TROOP_EASE = 0.14;
|
|
27
|
+
|
|
28
|
+
/** Duration in seconds for a struck figure to collapse to the ground. */
|
|
29
|
+
export const TROOP_FALL_TIME = 0.55;
|
|
30
|
+
|
|
31
|
+
/** Maximum front-to-front gap in metres within which opposing fighting formations interlock. */
|
|
32
|
+
export const TROOP_ENGAGE_MAX_GAP = 14;
|
|
33
|
+
|
|
34
|
+
/** Target front-to-front overlap gap in metres so front ranks fight at blade reach. */
|
|
35
|
+
export const TROOP_ENGAGE_TARGET_GAP = 0.5;
|
|
36
|
+
|
|
37
|
+
/** Default dust view cutoffs and clash foley cadence for an active host. */
|
|
38
|
+
export const TROOP_DUST_DEFAULTS = {
|
|
39
|
+
viewDist: 210,
|
|
40
|
+
fallDist: 95,
|
|
41
|
+
clashFoleyInterval: 0.36,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Default set of armed crowd kinds whose resting stance is `'ready'` rather than `'wait'`. */
|
|
45
|
+
export const TROOP_ARMED_KINDS = new Set(['trooper', 'raider', 'guard']);
|
|
46
|
+
|
|
47
|
+
/** Default name pattern marking troops that loose ranged volleys across open ground. */
|
|
48
|
+
export const TROOP_RANGED_NAME = /archer|bow|raft|wood|skirmish/i;
|
|
49
|
+
|
|
50
|
+
const _slot = { x: 0, z: 0 };
|
|
51
|
+
const _pace = { at: 0, rate: 0 };
|
|
52
|
+
const _formSlot = { slotX: 0, slotZ: 0, rank: 0, file: 0, rankNorm: 0, edgeNorm: 0 };
|
|
53
|
+
const _march = { delay: 0, duration: 0 };
|
|
54
|
+
const _melee = { along: 0, across: 0, sway: 0, speed: 0 };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Musters a troop's members into organic, staggered ranks (`formationSlot`).
|
|
58
|
+
* Rank and file metadata are returned in parallel typed arrays so crowd figure objects keep
|
|
59
|
+
* a monomorphic hidden class.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} spec Troop specification (`{ n, wide?, gap?, loose?, size?, beast?, body? }`).
|
|
62
|
+
* @param {() => number} rng Deterministic PRNG returning values in `[0, 1)`.
|
|
63
|
+
* @param {object} [options]
|
|
64
|
+
* @param {boolean} [options.hasStandard=false] True when the troop carries a front-centre standard.
|
|
65
|
+
* @returns {{ list: object[], wide: number, loose: number, halfWidth: number, ranks: Uint16Array, files: Uint16Array, rankNorms: Float32Array, edgeNorms: Float32Array }}
|
|
66
|
+
*/
|
|
67
|
+
export function musterTroop(spec, rng, { hasStandard = false } = {}) {
|
|
68
|
+
const wide = spec.wide || Math.ceil(Math.sqrt(spec.n * 2)),
|
|
69
|
+
[across, deep] = spec.gap || [1.1, 1.3],
|
|
70
|
+
loose = spec.loose ?? 0.22,
|
|
71
|
+
sizes = spec.size || [0.95, 1.05],
|
|
72
|
+
bearerIndex = hasStandard && !spec.beast ? Math.min(spec.n - 1, (wide - 1) >> 1) : -1,
|
|
73
|
+
bow = spec.beast ? 0.35 : 1,
|
|
74
|
+
list = [],
|
|
75
|
+
ranks = new Uint16Array(spec.n),
|
|
76
|
+
files = new Uint16Array(spec.n),
|
|
77
|
+
rankNorms = new Float32Array(spec.n),
|
|
78
|
+
edgeNorms = new Float32Array(spec.n);
|
|
79
|
+
let halfWidth = 0;
|
|
80
|
+
for (let i = 0; i < spec.n; i++) {
|
|
81
|
+
formationSlot(i, spec.n, wide, across, deep, loose, rng, _formSlot, { bearerIndex, bow });
|
|
82
|
+
ranks[i] = _formSlot.rank;
|
|
83
|
+
files[i] = _formSlot.file;
|
|
84
|
+
rankNorms[i] = _formSlot.rankNorm;
|
|
85
|
+
edgeNorms[i] = _formSlot.edgeNorm;
|
|
86
|
+
halfWidth = Math.max(halfWidth, Math.abs(_formSlot.slotX));
|
|
87
|
+
list.push({
|
|
88
|
+
slotX: _formSlot.slotX,
|
|
89
|
+
slotZ: _formSlot.slotZ,
|
|
90
|
+
ax: 0,
|
|
91
|
+
az: 0,
|
|
92
|
+
bx: 0,
|
|
93
|
+
bz: 0,
|
|
94
|
+
sp: 1e-6, // dynamic movers look up ground as they move rather than caching once as standers
|
|
95
|
+
yaw: 0,
|
|
96
|
+
ph: rng() * 100,
|
|
97
|
+
vary: rng(),
|
|
98
|
+
s: sizes[0] + rng() * (sizes[1] - sizes[0]),
|
|
99
|
+
walkSpeed: 1.5,
|
|
100
|
+
steady: true, // troops hold formation and do not scatter at stray blows
|
|
101
|
+
travel: 0,
|
|
102
|
+
speed: 0,
|
|
103
|
+
coat: (rng() * 97) | 0,
|
|
104
|
+
hgx: NaN,
|
|
105
|
+
hgz: NaN,
|
|
106
|
+
hy: 0,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { list, wide, loose, halfWidth: Math.max(halfWidth, across), ranks, files, rankNorms, edgeNorms };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Projects a troop member's local formation slot `(slotX, slotZ)` into world `(x, z)` around a
|
|
114
|
+
* troop centre `(cx, cz)` facing `yaw` with spacing scale `spread`.
|
|
115
|
+
*
|
|
116
|
+
* @param {{ slotX: number, slotZ: number }} member Troop member record.
|
|
117
|
+
* @param {number} cx Troop centre X coordinate in metres.
|
|
118
|
+
* @param {number} cz Troop centre Z coordinate in metres.
|
|
119
|
+
* @param {number} yaw Troop facing angle in radians.
|
|
120
|
+
* @param {number} spread Lateral and depth spread multiplier (1 = nominal spacing).
|
|
121
|
+
* @param {{ x: number, z: number }} out Reusable output coordinates record.
|
|
122
|
+
* @returns {{ x: number, z: number }} The populated `out` record.
|
|
123
|
+
*/
|
|
124
|
+
export function troopSlot(member, cx, cz, yaw, spread, out) {
|
|
125
|
+
const c = Math.cos(yaw),
|
|
126
|
+
s = Math.sin(yaw);
|
|
127
|
+
out.x = cx + (member.slotX * c + member.slotZ * s) * spread;
|
|
128
|
+
out.z = cz + (-member.slotX * s + member.slotZ * c) * spread;
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Computes trapezoidal march progress (`at` in `[0, 1]`) and speed ratio (`rate`) at normalized
|
|
134
|
+
* transit time `u`: smooth acceleration over `ease`, steady cruise in the middle, smooth halt.
|
|
135
|
+
*
|
|
136
|
+
* @param {number} u Normalized progress in `[0, 1]`.
|
|
137
|
+
* @param {{ at: number, rate: number }} out Reusable output record.
|
|
138
|
+
* @param {number} [ease=0.14] Fraction of duration spent accelerating and decelerating.
|
|
139
|
+
* @returns {{ at: number, rate: number }} The populated `out` record.
|
|
140
|
+
*/
|
|
141
|
+
export function marchPace(u, out, ease = TROOP_EASE) {
|
|
142
|
+
const k = ease,
|
|
143
|
+
top = 1 / (1 - k);
|
|
144
|
+
if (u < k) {
|
|
145
|
+
out.at = (u * u * top) / (2 * k);
|
|
146
|
+
out.rate = (u / k) * top;
|
|
147
|
+
} else if (u > 1 - k) {
|
|
148
|
+
const v = 1 - u;
|
|
149
|
+
out.at = 1 - (v * v * top) / (2 * k);
|
|
150
|
+
out.rate = (v / k) * top;
|
|
151
|
+
} else {
|
|
152
|
+
out.at = (u - k / 2) * top;
|
|
153
|
+
out.rate = top;
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Schedules a single troop member to move from its current home `(hx, hz)` to `(x, z)` starting at
|
|
160
|
+
* `t0` over `dur` seconds, finishing at heading `yawEnd` (`dur <= 0` teleports immediately).
|
|
161
|
+
*
|
|
162
|
+
* @param {object} member Troop member record mutated in place.
|
|
163
|
+
* @param {number} x Destination X coordinate in metres.
|
|
164
|
+
* @param {number} z Destination Z coordinate in metres.
|
|
165
|
+
* @param {number} t0 Start time in seconds.
|
|
166
|
+
* @param {number} dur Transit duration in seconds.
|
|
167
|
+
* @param {number} yawEnd Final heading angle in radians.
|
|
168
|
+
*/
|
|
169
|
+
export function sendTroopMember(member, x, z, t0, dur, yawEnd) {
|
|
170
|
+
const dx = x - member.hx,
|
|
171
|
+
dz = z - member.hz,
|
|
172
|
+
len = Math.hypot(dx, dz);
|
|
173
|
+
if (dur <= 0) {
|
|
174
|
+
member.hx = member.ax = x;
|
|
175
|
+
member.hz = member.az = z;
|
|
176
|
+
member.yaw = yawEnd;
|
|
177
|
+
member.speed = 0;
|
|
178
|
+
member.move = null;
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
member.move = { x0: member.hx, z0: member.hz, dx, dz, len, dir: Math.atan2(dx, dz), t0, dur, yawEnd };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Resolves a troop's resting stance from its specification: riders hold `'ride'`, explicit roles
|
|
186
|
+
* win, armed infantry hold `'ready'`, and civilians or beasts hold `'wait'`.
|
|
187
|
+
*
|
|
188
|
+
* @param {object} spec Troop specification.
|
|
189
|
+
* @param {Set<string>} [armedKinds] Set of crowd kinds defaulting to `'ready'`.
|
|
190
|
+
* @returns {string}
|
|
191
|
+
*/
|
|
192
|
+
export function defaultTroopRole(spec, armedKinds = TROOP_ARMED_KINDS) {
|
|
193
|
+
if (spec.mounted) return 'ride';
|
|
194
|
+
if (spec.role) return spec.role;
|
|
195
|
+
return armedKinds.has(spec.kind) ? 'ready' : 'wait';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Resets a troop to its initial formation, position, facing, visibility, stance, and casualty state.
|
|
200
|
+
*
|
|
201
|
+
* @param {object} troop Troop record (`{ spec, at, yaw, list, ... }`).
|
|
202
|
+
* @param {object} [options]
|
|
203
|
+
* @param {number} [options.saddle=1.2] Mounted rider seat lift in metres.
|
|
204
|
+
* @param {Set<string>} [options.armedKinds] Armed kinds defaulting to `'ready'`.
|
|
205
|
+
* @param {number} [options.unseen=-1] Hidden `home` sentinel.
|
|
206
|
+
*/
|
|
207
|
+
export function standTroop(troop, { saddle = TROOP_SADDLE, armedKinds = TROOP_ARMED_KINDS, unseen = TROOP_UNSEEN } = {}) {
|
|
208
|
+
const { spec, at, yaw } = troop;
|
|
209
|
+
troop.busy = false;
|
|
210
|
+
troop.fighting = false;
|
|
211
|
+
troop.visible = !spec.hide;
|
|
212
|
+
troop.cx = at.x;
|
|
213
|
+
troop.cz = at.z;
|
|
214
|
+
troop.facing = yaw;
|
|
215
|
+
troop.engageDepth = 0;
|
|
216
|
+
troop.volleyTarget = null;
|
|
217
|
+
troop.nextVolleyAt = 0;
|
|
218
|
+
troop.nextDustAt = 0;
|
|
219
|
+
troop.dustCursor = 0;
|
|
220
|
+
const role = defaultTroopRole(spec, armedKinds);
|
|
221
|
+
for (let i = 0; i < troop.list.length; i++) {
|
|
222
|
+
const g = troop.list[i];
|
|
223
|
+
troopSlot(g, at.x, at.z, yaw, 1, _slot);
|
|
224
|
+
g.hx = _slot.x;
|
|
225
|
+
g.hz = _slot.z;
|
|
226
|
+
sendTroopMember(g, _slot.x, _slot.z, 0, 0, yaw + (spec.turn ? (g.vary - 0.5) * spec.turn : 0));
|
|
227
|
+
g.bx = g.ax;
|
|
228
|
+
g.bz = g.az;
|
|
229
|
+
g.mounted = !!spec.mounted;
|
|
230
|
+
g.role = role;
|
|
231
|
+
g.lift = spec.mounted ? saddle : 0;
|
|
232
|
+
g.fallTo = g.vary < 0.5 ? -1 : 1;
|
|
233
|
+
g.fall = spec.down ? g.fallTo : 0;
|
|
234
|
+
g.fallAt = g.riseAt = undefined;
|
|
235
|
+
g.reach = 0;
|
|
236
|
+
g.home = spec.hide ? unseen : undefined;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Advances one troop member for the current frame: march interpolation, localized melee pocket
|
|
242
|
+
* motion, rising from prone, and collapsing when struck. Returns true while the member still has
|
|
243
|
+
* active motion or state transitions.
|
|
244
|
+
*
|
|
245
|
+
* @param {object} member Troop member record updated in place.
|
|
246
|
+
* @param {number} t Elapsed simulation time in seconds.
|
|
247
|
+
* @param {number} dt Frame step in seconds.
|
|
248
|
+
* @param {number} engageDepth Forward scrimmage press depth in metres.
|
|
249
|
+
* @param {((x: number, z: number) => void)|null} [onFallLand=null] Optional callback fired once when a falling member hits the ground.
|
|
250
|
+
* @param {object} [options]
|
|
251
|
+
* @param {number} [options.ease=0.14] March acceleration/deceleration share.
|
|
252
|
+
* @param {number} [options.fallTime=0.55] Collapse duration in seconds.
|
|
253
|
+
* @returns {boolean} Whether the member remains busy.
|
|
254
|
+
*/
|
|
255
|
+
export function stepTroopMember(member, t, dt, engageDepth, onFallLand = null, { ease = TROOP_EASE, fallTime = TROOP_FALL_TIME } = {}) {
|
|
256
|
+
let busy = false,
|
|
257
|
+
speed = 0;
|
|
258
|
+
const m = member.move;
|
|
259
|
+
if (m) {
|
|
260
|
+
const u = clamp((t - m.t0) / m.dur, 0, 1);
|
|
261
|
+
marchPace(u, _pace, ease);
|
|
262
|
+
member.hx = m.x0 + m.dx * _pace.at;
|
|
263
|
+
member.hz = m.z0 + m.dz * _pace.at;
|
|
264
|
+
if (u < 1) {
|
|
265
|
+
speed = t < m.t0 ? 0 : (m.len / m.dur) * _pace.rate;
|
|
266
|
+
// Fighting soldiers giving ground keep facing their opponent rather than turning their backs.
|
|
267
|
+
if (m.len > 0.4 && t >= m.t0 && !member.reach) member.yaw = m.dir;
|
|
268
|
+
busy = true;
|
|
269
|
+
} else {
|
|
270
|
+
member.yaw = m.yawEnd;
|
|
271
|
+
member.move = null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
let x = member.hx,
|
|
275
|
+
z = member.hz;
|
|
276
|
+
member.sway = 0;
|
|
277
|
+
if (member.reach) {
|
|
278
|
+
meleeOffset(
|
|
279
|
+
t,
|
|
280
|
+
member.ph,
|
|
281
|
+
member.vary,
|
|
282
|
+
member.troopPhase,
|
|
283
|
+
member.slotX,
|
|
284
|
+
member.slotZ,
|
|
285
|
+
member.reach,
|
|
286
|
+
Math.max(0, t - member.fightFrom),
|
|
287
|
+
engageDepth,
|
|
288
|
+
_melee,
|
|
289
|
+
);
|
|
290
|
+
const fx = Math.sin(member.yaw),
|
|
291
|
+
fz = Math.cos(member.yaw);
|
|
292
|
+
x += fx * _melee.along + fz * _melee.across;
|
|
293
|
+
z += fz * _melee.along - fx * _melee.across;
|
|
294
|
+
member.sway = _melee.sway;
|
|
295
|
+
speed = Math.max(speed, _melee.speed);
|
|
296
|
+
busy = true;
|
|
297
|
+
}
|
|
298
|
+
if (member.riseAt !== undefined) {
|
|
299
|
+
const since = t - member.riseAt;
|
|
300
|
+
if (since >= 0) member.fall = member.fallTo * (1 - sstep(0, fallTime * 3, since));
|
|
301
|
+
if (since > fallTime * 3) {
|
|
302
|
+
member.riseAt = undefined;
|
|
303
|
+
if (member.mounted) member.role = 'ride';
|
|
304
|
+
}
|
|
305
|
+
busy = true;
|
|
306
|
+
}
|
|
307
|
+
if (member.fallAt !== undefined) {
|
|
308
|
+
const since = t - member.fallAt;
|
|
309
|
+
if (since >= 0) {
|
|
310
|
+
const wasFalling = Math.abs(member.fall) < 0.85;
|
|
311
|
+
member.fall = member.fallTo * sstep(0, fallTime, since);
|
|
312
|
+
member.move = null;
|
|
313
|
+
member.reach = 0;
|
|
314
|
+
member.role = 'wait';
|
|
315
|
+
speed = 0;
|
|
316
|
+
if (wasFalling && Math.abs(member.fall) >= 0.85 && onFallLand) onFallLand(x, z);
|
|
317
|
+
if (since > fallTime) member.fallAt = undefined;
|
|
318
|
+
}
|
|
319
|
+
busy = true;
|
|
320
|
+
}
|
|
321
|
+
member.ax = x;
|
|
322
|
+
member.az = z;
|
|
323
|
+
member.speed = speed;
|
|
324
|
+
member.travel += speed * dt;
|
|
325
|
+
return busy;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Matches each visible non-beast troop against opposing bodies for front-line scrimmage interlocking
|
|
330
|
+
* (`engageDepth`) and ranged or rear-rank volley targeting (`volleyTarget`). Zero allocations.
|
|
331
|
+
*
|
|
332
|
+
* @param {object[]} troops Array of troop records belonging to one host.
|
|
333
|
+
* @param {object} [options]
|
|
334
|
+
* @param {number} [options.engageMaxGap=14] Maximum front gap (metres) for melee interlocking.
|
|
335
|
+
* @param {number} [options.engageTargetGap=0.5] Target centre-to-centre scrimmage overlap gap (metres).
|
|
336
|
+
* @param {number} [options.volleyMinDist=12] Minimum range (metres) for skirmisher volleys.
|
|
337
|
+
* @param {number} [options.volleyMaxDist=125] Maximum volley distance (metres).
|
|
338
|
+
*/
|
|
339
|
+
export function refreshTroopOpponents(
|
|
340
|
+
troops,
|
|
341
|
+
{
|
|
342
|
+
engageMaxGap = TROOP_ENGAGE_MAX_GAP,
|
|
343
|
+
engageTargetGap = TROOP_ENGAGE_TARGET_GAP,
|
|
344
|
+
volleyMinDist = TROOP_VOLLEY_DEFAULTS.minDist,
|
|
345
|
+
volleyMaxDist = TROOP_VOLLEY_DEFAULTS.maxDist,
|
|
346
|
+
} = {},
|
|
347
|
+
) {
|
|
348
|
+
for (let i = 0; i < troops.length; i++) {
|
|
349
|
+
const a = troops[i];
|
|
350
|
+
a.engageDepth = 0;
|
|
351
|
+
a.volleyTarget = null;
|
|
352
|
+
if (!a.visible || a.spec.beast) continue;
|
|
353
|
+
const fax = Math.sin(a.facing),
|
|
354
|
+
faz = Math.cos(a.facing);
|
|
355
|
+
let bestClashGap = engageMaxGap,
|
|
356
|
+
bestVolleyDist = volleyMaxDist,
|
|
357
|
+
volleyEnemy = null;
|
|
358
|
+
for (let j = 0; j < troops.length; j++) {
|
|
359
|
+
if (i === j) continue;
|
|
360
|
+
const b = troops[j];
|
|
361
|
+
if (!b.visible || b.spec.beast || b.spec.body === a.spec.body) continue;
|
|
362
|
+
const dx = b.cx - a.cx,
|
|
363
|
+
dz = b.cz - a.cz,
|
|
364
|
+
along = dx * fax + dz * faz;
|
|
365
|
+
if (along < -2) continue;
|
|
366
|
+
const lateral = Math.abs(dx * faz - dz * fax);
|
|
367
|
+
const maxSpan = a.halfWidth + b.halfWidth + 8;
|
|
368
|
+
if (lateral > maxSpan) continue;
|
|
369
|
+
const fbx = Math.sin(b.facing),
|
|
370
|
+
fbz = Math.cos(b.facing),
|
|
371
|
+
opposing = fax * fbx + faz * fbz < -0.3;
|
|
372
|
+
if (a.fighting && opposing && along < bestClashGap) bestClashGap = along;
|
|
373
|
+
const dist = Math.hypot(dx, dz);
|
|
374
|
+
// Deep opposing infantry blocks (n >= 24) also loose rear-rank volleys over their own front ranks.
|
|
375
|
+
const deepRearVolley = opposing && (a.fighting || a.busy) && a.list.length >= 24 && dist >= 1.5;
|
|
376
|
+
const canShoot = (a.ranged && dist >= volleyMinDist) || deepRearVolley;
|
|
377
|
+
if (canShoot && dist < bestVolleyDist) {
|
|
378
|
+
bestVolleyDist = dist;
|
|
379
|
+
volleyEnemy = b;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (a.fighting) {
|
|
383
|
+
if (bestClashGap < engageMaxGap) {
|
|
384
|
+
a.engageDepth = clamp((bestClashGap - engageTargetGap) * 0.56, 0.3, 1.85);
|
|
385
|
+
} else {
|
|
386
|
+
a.engageDepth = 0.45;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
a.volleyTarget = volleyEnemy;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Applies one timeline cue step (`[seconds, 'cue', setId, verb, targetOrTargets, ...]`) to a host's
|
|
395
|
+
* troops relative to scene `anchor` at simulation clock `t`.
|
|
396
|
+
* Supported verbs: `'go'`, `'fight'`, `'fall'`, `'rise'`, `'role'`, `'show'`, `'hide'`.
|
|
397
|
+
*
|
|
398
|
+
* @param {{ troops: Record<string, object>, opponentsDirty: boolean }} host Host record.
|
|
399
|
+
* @param {any[]} step Scene cue tuple.
|
|
400
|
+
* @param {{ x: number, z: number }} anchor Scene anchor offset in world space (metres).
|
|
401
|
+
* @param {number} t Current simulation time in seconds.
|
|
402
|
+
* @param {object} [options]
|
|
403
|
+
* @param {Set<string>} [options.armedKinds] Armed kinds mapped from `'wait'` to `'ready'`.
|
|
404
|
+
* @param {number} [options.unseen=-1] Hidden `home` sentinel.
|
|
405
|
+
*/
|
|
406
|
+
export function applyTroopCue(host, step, anchor, t, { armedKinds = TROOP_ARMED_KINDS, unseen = TROOP_UNSEEN } = {}) {
|
|
407
|
+
const verb = step[3],
|
|
408
|
+
names = Array.isArray(step[4]) ? step[4] : [step[4]];
|
|
409
|
+
for (let ni = 0; ni < names.length; ni++) {
|
|
410
|
+
const troop = host.troops[names[ni]];
|
|
411
|
+
if (!troop) continue;
|
|
412
|
+
const { list, ranks, files, rankNorms, edgeNorms } = troop;
|
|
413
|
+
if (verb === 'go') {
|
|
414
|
+
const x = anchor.x + step[5],
|
|
415
|
+
z = anchor.z + step[6],
|
|
416
|
+
dur = step[7] || 0,
|
|
417
|
+
yaw = step[8] === undefined ? Math.atan2(x - troop.cx, z - troop.cz) : Math.atan2(anchor.x + step[8] - x, anchor.z + step[9] - z),
|
|
418
|
+
spread = step[10] || 1;
|
|
419
|
+
troop.cx = x;
|
|
420
|
+
troop.cz = z;
|
|
421
|
+
troop.facing = yaw;
|
|
422
|
+
for (let i = 0; i < list.length; i++) {
|
|
423
|
+
const g = list[i];
|
|
424
|
+
if (g.fall) continue;
|
|
425
|
+
troopSlot(g, x, z, yaw, spread, _slot);
|
|
426
|
+
marchTiming(ranks[i], files[i], troop.wide, g.vary, dur, troop.loose, _march);
|
|
427
|
+
sendTroopMember(g, _slot.x, _slot.z, t + _march.delay, _march.duration, yaw);
|
|
428
|
+
}
|
|
429
|
+
troop.busy = true;
|
|
430
|
+
} else if (verb === 'fight') {
|
|
431
|
+
troop.fighting = true;
|
|
432
|
+
for (let i = 0; i < list.length; i++) {
|
|
433
|
+
const g = list[i];
|
|
434
|
+
if (g.fall) continue;
|
|
435
|
+
if (!g.reach) g.fightFrom = t;
|
|
436
|
+
g.reach = (step[5] ?? 1.2) * (0.55 + 0.85 * g.vary);
|
|
437
|
+
g.troopPhase = troop.phase;
|
|
438
|
+
if (!troop.spec.mounted) g.role = 'fight';
|
|
439
|
+
}
|
|
440
|
+
troop.busy = true;
|
|
441
|
+
} else if (verb === 'fall') {
|
|
442
|
+
const share = step[5] ?? 1,
|
|
443
|
+
over = step[6] || 0;
|
|
444
|
+
for (let i = 0; i < list.length; i++) {
|
|
445
|
+
const g = list[i];
|
|
446
|
+
if (g.fall || g.fallAt !== undefined) continue;
|
|
447
|
+
// Weight casualties toward leading ranks, exposed flanks, and local scrimmage pockets while honouring full wipes.
|
|
448
|
+
const pocket = 0.5 + 0.5 * Math.sin(g.slotX * 0.42 + troop.phase * 1.3);
|
|
449
|
+
const exposure = 0.55 * (1 - rankNorms[i]) + 0.25 * edgeNorms[i] + 0.2 * pocket;
|
|
450
|
+
const roll = (g.ph * 0.37) % 1;
|
|
451
|
+
const threshold = share >= 0.99 ? 1 : clamp(share * (0.62 + 0.76 * exposure), 0, 1);
|
|
452
|
+
if (roll > threshold) continue;
|
|
453
|
+
const waveBias = clamp(0.15 + 0.55 * rankNorms[i] + 0.3 * ((g.ph * 0.61) % 1) - 0.12 * pocket, 0, 1);
|
|
454
|
+
g.fallAt = t + over * waveBias;
|
|
455
|
+
}
|
|
456
|
+
troop.busy = true;
|
|
457
|
+
} else if (verb === 'rise') {
|
|
458
|
+
for (let i = 0; i < list.length; i++) {
|
|
459
|
+
const g = list[i];
|
|
460
|
+
if (g.fall) g.riseAt = t + (step[5] || 0) * ((g.ph * 0.61) % 1);
|
|
461
|
+
}
|
|
462
|
+
troop.busy = true;
|
|
463
|
+
} else if (verb === 'role') {
|
|
464
|
+
troop.fighting = step[5] === 'fight';
|
|
465
|
+
for (let i = 0; i < list.length; i++) {
|
|
466
|
+
const g = list[i];
|
|
467
|
+
if (!g.fall) {
|
|
468
|
+
g.role = g.mounted ? 'ride' : step[5] === 'wait' && armedKinds.has(troop.spec.kind) ? 'ready' : step[5];
|
|
469
|
+
g.reach = 0;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
} else if (verb === 'show' || verb === 'hide') {
|
|
473
|
+
troop.visible = verb === 'show';
|
|
474
|
+
// Mark `out = -1` so crowd visibility applies the cue immediately even while the troop is on screen.
|
|
475
|
+
for (let i = 0; i < list.length; i++) {
|
|
476
|
+
list[i].home = verb === 'hide' ? unseen : undefined;
|
|
477
|
+
list[i].out = -1;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
host.opponentsDirty = true;
|
|
482
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-safe ballistic volley state pool in flat typed arrays. Tracks arrow and javelin
|
|
3
|
+
* launches between opposing troops, parabolic arc flight (`sampleBallisticArc`), ground-stick
|
|
4
|
+
* duration, and recycling with zero Three.js dependencies and zero per-frame allocations.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { clamp } from '../math/scalar.js';
|
|
9
|
+
import { sampleBallisticArc } from './ballistic-arc.js';
|
|
10
|
+
|
|
11
|
+
/** Default ballistic volley pool size, range, flight speed, gravity, and ground stick duration. */
|
|
12
|
+
export const TROOP_VOLLEY_DEFAULTS = {
|
|
13
|
+
pool: 96,
|
|
14
|
+
minDist: 12,
|
|
15
|
+
maxDist: 125,
|
|
16
|
+
viewDist: 260,
|
|
17
|
+
gravity: 16.5,
|
|
18
|
+
speed: 24,
|
|
19
|
+
stick: 1.6,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const DEFAULT_SADDLE = 1.2;
|
|
23
|
+
const DEFAULT_UNSEEN = -1;
|
|
24
|
+
|
|
25
|
+
function makeArcVec() {
|
|
26
|
+
return {
|
|
27
|
+
x: 0,
|
|
28
|
+
y: 0,
|
|
29
|
+
z: 0,
|
|
30
|
+
set(x, y, z) {
|
|
31
|
+
this.x = x;
|
|
32
|
+
this.y = y;
|
|
33
|
+
this.z = z;
|
|
34
|
+
return this;
|
|
35
|
+
},
|
|
36
|
+
lerpVectors(a, b, alpha) {
|
|
37
|
+
this.x = a.x + (b.x - a.x) * alpha;
|
|
38
|
+
this.y = a.y + (b.y - a.y) * alpha;
|
|
39
|
+
this.z = a.z + (b.z - a.z) * alpha;
|
|
40
|
+
return this;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const _arcFrom = makeArcVec();
|
|
46
|
+
const _arcTo = makeArcVec();
|
|
47
|
+
const _arcPos = makeArcVec();
|
|
48
|
+
const _arcNext = makeArcVec();
|
|
49
|
+
const _launchResult = { fired: 0, x: 0, y: 0, z: 0 };
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a worker-safe ballistic volley state pool in flat typed arrays.
|
|
53
|
+
*
|
|
54
|
+
* @param {object} [options]
|
|
55
|
+
* @param {number} [options.pool=96] Maximum simultaneous projectiles in the pool.
|
|
56
|
+
* @param {number} [options.gravity=16.5] Arc gravity acceleration (m/s^2).
|
|
57
|
+
* @param {number} [options.speed=24] Nominal horizontal flight speed (m/s).
|
|
58
|
+
* @param {number} [options.stick=1.6] Seconds a landed shaft remains visible before recycling.
|
|
59
|
+
* @param {number} [options.saddle=1.2] Mounted shooter height offset above ground (metres).
|
|
60
|
+
* @param {number} [options.unseen=-1] Hidden `home` sentinel skipped when choosing shooters or targets.
|
|
61
|
+
* @returns {{ reset: () => void, launch: Function, sample: Function, getLiveCount: () => number }}
|
|
62
|
+
*/
|
|
63
|
+
export function createVolleyState({
|
|
64
|
+
pool = TROOP_VOLLEY_DEFAULTS.pool,
|
|
65
|
+
gravity = TROOP_VOLLEY_DEFAULTS.gravity,
|
|
66
|
+
speed = TROOP_VOLLEY_DEFAULTS.speed,
|
|
67
|
+
stick = TROOP_VOLLEY_DEFAULTS.stick,
|
|
68
|
+
saddle = DEFAULT_SADDLE,
|
|
69
|
+
unseen = DEFAULT_UNSEEN,
|
|
70
|
+
} = {}) {
|
|
71
|
+
const fx = new Float32Array(pool),
|
|
72
|
+
fy = new Float32Array(pool),
|
|
73
|
+
fz = new Float32Array(pool),
|
|
74
|
+
tx = new Float32Array(pool),
|
|
75
|
+
ty = new Float32Array(pool),
|
|
76
|
+
tz = new Float32Array(pool),
|
|
77
|
+
t0 = new Float32Array(pool),
|
|
78
|
+
dur = new Float32Array(pool),
|
|
79
|
+
landYaw = new Float32Array(pool),
|
|
80
|
+
landPitch = new Float32Array(pool),
|
|
81
|
+
stuck = new Uint8Array(pool),
|
|
82
|
+
active = new Uint8Array(pool);
|
|
83
|
+
let cursor = 0,
|
|
84
|
+
liveCount = 0;
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
getLiveCount: () => liveCount,
|
|
88
|
+
reset() {
|
|
89
|
+
active.fill(0);
|
|
90
|
+
stuck.fill(0);
|
|
91
|
+
liveCount = 0;
|
|
92
|
+
},
|
|
93
|
+
launch(fromTroop, toTroop, groundAt, t, rng) {
|
|
94
|
+
_launchResult.fired = 0;
|
|
95
|
+
const shooters = fromTroop.list,
|
|
96
|
+
targets = toTroop.list;
|
|
97
|
+
if (!shooters.length || !targets.length) return _launchResult;
|
|
98
|
+
const count = fromTroop.ranged ? 10 + ((rng() * 6) | 0) : 6 + ((rng() * 5) | 0);
|
|
99
|
+
let fired = 0,
|
|
100
|
+
sumX = 0,
|
|
101
|
+
sumY = 0,
|
|
102
|
+
sumZ = 0;
|
|
103
|
+
for (let n = 0; n < count; n++) {
|
|
104
|
+
const sIdx = fromTroop.ranged
|
|
105
|
+
? (rng() * shooters.length) | 0
|
|
106
|
+
: Math.min(shooters.length - 1, ((0.35 + rng() * 0.65) * shooters.length) | 0);
|
|
107
|
+
const shooter = shooters[sIdx];
|
|
108
|
+
if (shooter.fall || shooter.home === unseen) continue;
|
|
109
|
+
const target = targets[(rng() * targets.length) | 0];
|
|
110
|
+
if (target.home === unseen) continue;
|
|
111
|
+
const sx = shooter.ax,
|
|
112
|
+
sz = shooter.az,
|
|
113
|
+
sy = groundAt(sx, sz) + (shooter.mounted ? saddle + 1.45 : 1.55);
|
|
114
|
+
const ex = target.ax + (rng() - 0.5) * 4.5,
|
|
115
|
+
ez = target.az + (rng() - 0.5) * 4.5,
|
|
116
|
+
ey = groundAt(ex, ez) + 0.15;
|
|
117
|
+
const horiz = Math.hypot(ex - sx, ez - sz);
|
|
118
|
+
if (horiz < 8) continue;
|
|
119
|
+
const flight = clamp(horiz / (speed * (0.88 + rng() * 0.24)), 0.45, 2.8);
|
|
120
|
+
const idx = cursor;
|
|
121
|
+
cursor = (cursor + 1) % pool;
|
|
122
|
+
if (!active[idx]) liveCount++;
|
|
123
|
+
active[idx] = 1;
|
|
124
|
+
stuck[idx] = 0;
|
|
125
|
+
fx[idx] = sx;
|
|
126
|
+
fy[idx] = sy;
|
|
127
|
+
fz[idx] = sz;
|
|
128
|
+
tx[idx] = ex;
|
|
129
|
+
ty[idx] = ey;
|
|
130
|
+
tz[idx] = ez;
|
|
131
|
+
t0[idx] = t + rng() * 0.28;
|
|
132
|
+
dur[idx] = flight;
|
|
133
|
+
landYaw[idx] = Math.atan2(ex - sx, ez - sz);
|
|
134
|
+
const vyEnd = (ey - sy) / flight - 0.5 * gravity * flight;
|
|
135
|
+
landPitch[idx] = -Math.atan2(vyEnd, horiz / flight);
|
|
136
|
+
sumX += (sx + ex) * 0.5;
|
|
137
|
+
sumY += (sy + ey) * 0.5 + 3;
|
|
138
|
+
sumZ += (sz + ez) * 0.5;
|
|
139
|
+
fired++;
|
|
140
|
+
}
|
|
141
|
+
_launchResult.fired = fired;
|
|
142
|
+
if (fired > 0) {
|
|
143
|
+
_launchResult.x = sumX / fired;
|
|
144
|
+
_launchResult.y = sumY / fired;
|
|
145
|
+
_launchResult.z = sumZ / fired;
|
|
146
|
+
}
|
|
147
|
+
return _launchResult;
|
|
148
|
+
},
|
|
149
|
+
sample(t, onImpact, onDraw) {
|
|
150
|
+
if (!liveCount) return 0;
|
|
151
|
+
let stillLive = 0;
|
|
152
|
+
for (let i = 0; i < pool; i++) {
|
|
153
|
+
if (!active[i]) continue;
|
|
154
|
+
const age = t - t0[i];
|
|
155
|
+
if (age < 0) {
|
|
156
|
+
stillLive++;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const flight = dur[i];
|
|
160
|
+
if (age > flight + stick) {
|
|
161
|
+
active[i] = 0;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
stillLive++;
|
|
165
|
+
let px, py, pz, yaw, pitch;
|
|
166
|
+
if (age < flight) {
|
|
167
|
+
const phase = age / flight;
|
|
168
|
+
_arcFrom.set(fx[i], fy[i], fz[i]);
|
|
169
|
+
_arcTo.set(tx[i], ty[i], tz[i]);
|
|
170
|
+
sampleBallisticArc(_arcPos, _arcFrom, _arcTo, phase, flight, gravity);
|
|
171
|
+
sampleBallisticArc(_arcNext, _arcFrom, _arcTo, Math.min(1, phase + 0.03), flight, gravity);
|
|
172
|
+
px = _arcPos.x;
|
|
173
|
+
py = _arcPos.y;
|
|
174
|
+
pz = _arcPos.z;
|
|
175
|
+
const dx = _arcNext.x - px,
|
|
176
|
+
dy = _arcNext.y - py,
|
|
177
|
+
dz = _arcNext.z - pz;
|
|
178
|
+
yaw = Math.atan2(dx, dz);
|
|
179
|
+
pitch = -Math.atan2(dy, Math.hypot(dx, dz) || 1e-4);
|
|
180
|
+
} else {
|
|
181
|
+
if (!stuck[i]) {
|
|
182
|
+
stuck[i] = 1;
|
|
183
|
+
if (onImpact) onImpact(i, tx[i], ty[i], tz[i]);
|
|
184
|
+
}
|
|
185
|
+
px = tx[i];
|
|
186
|
+
py = ty[i];
|
|
187
|
+
pz = tz[i];
|
|
188
|
+
yaw = landYaw[i];
|
|
189
|
+
pitch = landPitch[i];
|
|
190
|
+
}
|
|
191
|
+
if (onDraw) onDraw(i, px, py, pz, yaw, pitch);
|
|
192
|
+
}
|
|
193
|
+
liveCount = stillLive;
|
|
194
|
+
return liveCount;
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "roamwright",
|
|
3
|
-
"version": "0.61.
|
|
3
|
+
"version": "0.61.27",
|
|
4
4
|
"description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"threejs",
|