roamwright 0.61.20 → 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,486 @@
1
+ /**
2
+ * Energy beam controller: manages cast-point gathering, arcing/writhing energy ribbons, rival
3
+ * counter-beams meeting mid-air between casters, impact splashes, defensive wards, bloom boost,
4
+ * and optional short-shot delegation to physical projectiles.
5
+ * @module
6
+ */
7
+
8
+ import * as THREE from 'three';
9
+ import { clamp, lerp } from '../math/scalar.js';
10
+ import { createEnergyStreamPool } from './energy-stream.js';
11
+
12
+ const GATHER = 0.35, // seconds energy draws in before leaving the caster (capped at 25% of duration)
13
+ NEAR_GROUND = 3, // impact height below which ground dust is raised (metres)
14
+ WINGED_BREATH = 1.7, // beam girth multiplier for winged/dragon casters
15
+ SHADOW_RATE = 0.6; // particle emission share for dark-smoke streams
16
+
17
+ /**
18
+ * Default beam kinematics by kind: head speed (m/s), arc height share, lateral writhe (m), and flame flag.
19
+ */
20
+ export const DEFAULT_BEAM_STYLES = Object.freeze({
21
+ fire: { speed: 75, arc: 0.03, writhe: 0.45, flame: true },
22
+ ice: { speed: 130, arc: 0, writhe: 0.1 },
23
+ dark: { speed: 65, arc: -0.02, writhe: 0.8 },
24
+ violet: { speed: 80, arc: 0.02, writhe: 0.6 },
25
+ gold: { speed: 110, arc: 0.015, writhe: 0.25 },
26
+ shadow: { speed: 30, arc: 0.04, writhe: 0.5 },
27
+ earth: { speed: 22, arc: 0, writhe: 0.3 },
28
+ });
29
+
30
+ /**
31
+ * Fallback kinematics when a beam kind is not listed in `styles`.
32
+ */
33
+ export const DEFAULT_BEAM_STYLE = Object.freeze({ speed: 95, arc: 0.02, writhe: 0.35 });
34
+
35
+ /**
36
+ * Generic linear RGB beam colour palette used when a world supplies no custom colour table.
37
+ */
38
+ export const DEFAULT_BEAM_COLORS = Object.freeze({
39
+ white: [1.0, 0.96, 0.9],
40
+ fire: [1.0, 0.55, 0.16],
41
+ ice: [0.65, 0.88, 1.0],
42
+ dark: [0.35, 0.16, 0.55],
43
+ violet: [0.72, 0.34, 0.95],
44
+ gold: [1.0, 0.82, 0.32],
45
+ shadow: [0.055, 0.062, 0.09],
46
+ earth: [0.65, 0.52, 0.36],
47
+ });
48
+
49
+ /**
50
+ * Creates a pooled energy-beam manager bound to `scene`.
51
+ * @param {object} options
52
+ * @param {THREE.Object3D} options.scene Scene receiving the pre-allocated energy ribbon group.
53
+ * @param {Record<string, object>} [options.actors] Actor table keyed by id.
54
+ * @param {(spec: any, out: THREE.Vector3) => THREE.Vector3} [options.resolvePos] Resolves non-actor target specs into `out`.
55
+ * @param {(x: number, z: number) => number} [options.groundAt] Ground height sampler.
56
+ * @param {() => number} [options.getQualityFx] Returns current particle density multiplier (`0..1`).
57
+ * @param {Record<string, object>} [options.styles] Kinematics table per beam kind.
58
+ * @param {object} [options.defaultStyle] Fallback beam kinematics.
59
+ * @param {Record<string, [number, number, number]>} [options.colors] RGB colour table per beam kind.
60
+ * @param {[number, number, number]} [options.darkTint] Ribbon tint for `darkKind` beams.
61
+ * @param {string} [options.darkKind] Beam kind rendered as dark smoke rather than additive light (default `'shadow'`).
62
+ * @param {string} [options.wingedKind] Actor `kind` that breathes from its neck bone and widens the stream (default `'dragon'`).
63
+ * @param {number} [options.capacity] Ribbon pool capacity (default `8`).
64
+ * @param {number} [options.segments] Ribbon segment count (default `32`).
65
+ * @param {(r: number, g: number, b: number) => { move: Function, release: Function } | null} [options.acquireLight] Optional travelling light-spill allocator.
66
+ * @param {(from: any, to: any, dur: number) => boolean} [options.routeShortBeam] Optional delegate for short `'white'` projectile shots.
67
+ * @param {(dt: number) => void} [options.onBeforeUpdate] Hook invoked at the start of `update(dt)`.
68
+ * @param {Function} [options.onGatherDark] Emits one dark-smoke gather puff.
69
+ * @param {Function} [options.onGatherParticle] Emits one additive gather mote.
70
+ * @param {Function} [options.onSplashDark] Emits one dark-smoke splash puff.
71
+ * @param {Function} [options.onSplashParticle] Emits one additive splash spark.
72
+ * @param {Function} [options.onStreamDark] Emits one dark-smoke stream puff.
73
+ * @param {Function} [options.onStreamParticle] Emits one additive stream mote.
74
+ * @param {Function} [options.onWardParticle] Emits one additive defensive-ward spark.
75
+ * @param {Function} [options.onHeadParticle] Emits one additive beam-head spark.
76
+ * @param {Function} [options.onLand] Called once when the beam head arrives on target or meets a rival beam.
77
+ * @param {Function} [options.onScar] Called when a non-dark beam expires on its target.
78
+ * @param {(x: number, z: number, radius: number) => void} [options.onAlarm] Crowd alarm callback.
79
+ * @param {(actor: object, dust: number, sweat: number, blood: number, scorch: number) => void} [options.onWear] Progressive battle-wear callback.
80
+ * @param {(actor: object, dirX: number, dirZ: number) => void} [options.onBrace] Applies directional brace state to a warding or straining actor.
81
+ * @param {(boost: number) => void} [options.onBloomBoost] Receives the eased bloom boost level each frame.
82
+ * @param {() => number} [options.onClear] Optional hook invoked on `clear()`.
83
+ * @param {() => number} [options.random] Random source in `[0, 1)` (defaults to live `Math.random`).
84
+ * @returns {{ start: Function, update: (dt: number) => void, clear: () => void, dispose: () => void, beams: object[], ribbons: object }}
85
+ */
86
+ export function createEnergyBeams({
87
+ scene,
88
+ actors = {},
89
+ resolvePos = (spec, out) => (Array.isArray(spec) ? out.set(spec[0], spec[1], spec[2]) : out.set(0, 0, 0)),
90
+ groundAt = () => 0,
91
+ getQualityFx = () => 1,
92
+ styles = DEFAULT_BEAM_STYLES,
93
+ defaultStyle = DEFAULT_BEAM_STYLE,
94
+ colors = DEFAULT_BEAM_COLORS,
95
+ darkTint = DEFAULT_BEAM_COLORS.shadow,
96
+ darkKind = 'shadow',
97
+ wingedKind = 'dragon',
98
+ capacity = 8,
99
+ segments = 32,
100
+ acquireLight = null,
101
+ routeShortBeam = null,
102
+ onBeforeUpdate = null,
103
+ onGatherDark = null,
104
+ onGatherParticle = null,
105
+ onSplashDark = null,
106
+ onSplashParticle = null,
107
+ onStreamDark = null,
108
+ onStreamParticle = null,
109
+ onWardParticle = null,
110
+ onHeadParticle = null,
111
+ onLand = null,
112
+ onScar = null,
113
+ onAlarm = null,
114
+ onWear = null,
115
+ onBrace = null,
116
+ onBloomBoost = null,
117
+ onClear = null,
118
+ random = () => Math.random(),
119
+ } = {}) {
120
+ const ribbons = createEnergyStreamPool({ capacity, segments });
121
+ scene?.add(ribbons.group);
122
+ const beams = [];
123
+ const _from = new THREE.Vector3(),
124
+ _to = new THREE.Vector3(),
125
+ _other = new THREE.Vector3(),
126
+ _dir = new THREE.Vector3(),
127
+ _side = new THREE.Vector3(),
128
+ _up = new THREE.Vector3(),
129
+ _p = new THREE.Vector3();
130
+ let clock = 0,
131
+ bloomBoost = 0;
132
+
133
+ const actorOf = spec => (typeof spec === 'string' && spec[0] !== '@' ? actors[spec] : null);
134
+
135
+ function castFrom(spec, out) {
136
+ const a = actorOf(spec);
137
+ if (a && a.kind === wingedKind && a.neck) return a.neck[2].localToWorld(out.set(0, 2.45, 3.3));
138
+ if (a && a.handR && a.pose !== 'dead') {
139
+ a.handR.getWorldPosition(out);
140
+ return a.pose === 'cast' ? out.lerp(a.handL.getWorldPosition(_p), 0.5) : out;
141
+ }
142
+ return resolvePos(spec, out);
143
+ }
144
+
145
+ function strikeAt(spec, out) {
146
+ const a = actorOf(spec);
147
+ return a && a.kind === wingedKind ? out.copy(a.group.position) : resolvePos(spec, out);
148
+ }
149
+
150
+ function answering(b) {
151
+ if (typeof b.from !== 'string' || typeof b.to !== 'string') return null;
152
+ for (let i = 0; i < beams.length; i++) {
153
+ const o = beams[i];
154
+ if (o !== b && o.from === b.to && o.to === b.from && o.t > o.gather && o.t < o.dur) return o;
155
+ }
156
+ return null;
157
+ }
158
+
159
+ function onPath(b, S, u, len, out) {
160
+ const pinned = Math.sin(Math.PI * u),
161
+ lift = S.arc * len * 4 * u * (1 - u),
162
+ w = S.writhe * pinned * Math.min(1, len / 20);
163
+ return out
164
+ .copy(_from)
165
+ .addScaledVector(_dir, u * len)
166
+ .addScaledVector(_up, lift + Math.sin(u * 9 - b.t * 11 + b.seed) * w * 0.6)
167
+ .addScaledVector(_side, Math.sin(u * 7 + b.t * 8 + b.seed * 2) * w);
168
+ }
169
+
170
+ function start(kind, from, to, dur) {
171
+ if (kind === 'white' && routeShortBeam?.(from, to, dur)) return;
172
+ const color = kind === darkKind ? darkTint : colors[kind] || colors.white || DEFAULT_BEAM_COLORS.white;
173
+ const peak = Math.max(color[0], color[1], color[2]);
174
+ const ribbon = ribbons.acquire();
175
+ ribbon?.color.setRGB(color[0], color[1], color[2]);
176
+ const spill = kind === darkKind || !acquireLight ? null : acquireLight(color[0] / peak, color[1] / peak, color[2] / peak);
177
+ const beam = {
178
+ kind,
179
+ from,
180
+ to,
181
+ dur,
182
+ t: 0,
183
+ acc: 0,
184
+ seed: random() * Math.PI * 2,
185
+ gather: Math.min(GATHER, dur * 0.25),
186
+ kick: false,
187
+ landed: false,
188
+ ribbon,
189
+ spill,
190
+ length: 0,
191
+ style: styles[kind] || defaultStyle,
192
+ sample: (u, out) => onPath(beam, beam.style, u, beam.length, out),
193
+ };
194
+ beams.push(beam);
195
+ }
196
+
197
+ function releaseBeam(beam) {
198
+ beam.ribbon?.release();
199
+ beam.spill?.release();
200
+ }
201
+
202
+ function clear() {
203
+ for (let i = 0; i < beams.length; i++) releaseBeam(beams[i]);
204
+ beams.length = 0;
205
+ bloomBoost = 0;
206
+ onBloomBoost?.(0);
207
+ onClear?.();
208
+ }
209
+
210
+ function gather(b, c, dt, q) {
211
+ const build = b.t / b.gather;
212
+ for (let n = Math.round((6 + 20 * build) * dt * 24 * q); n > 0; n--) {
213
+ const r = 0.5 + random() * 0.9,
214
+ ux = random() * 2 - 1,
215
+ uy = random() * 2 - 1,
216
+ uz = random() * 2 - 1,
217
+ life = 0.14 + random() * 0.14,
218
+ k = (0.25 + 0.5 * build) * (0.6 + random() * 0.6);
219
+ if (b.kind === darkKind) {
220
+ onGatherDark?.(
221
+ _from.x + ux * r,
222
+ _from.y + uy * r,
223
+ _from.z + uz * r,
224
+ (-ux * r) / life,
225
+ (-uy * r) / life,
226
+ (-uz * r) / life,
227
+ 0.08 + random() * 0.1,
228
+ life,
229
+ 0.3 + 0.3 * build,
230
+ );
231
+ continue;
232
+ }
233
+ onGatherParticle?.(
234
+ _from.x + ux * r,
235
+ _from.y + uy * r,
236
+ _from.z + uz * r,
237
+ (-ux * r) / life,
238
+ (-uy * r) / life,
239
+ (-uz * r) / life,
240
+ life,
241
+ 0.1 + random() * 0.14,
242
+ c[0] * k,
243
+ c[1] * k,
244
+ c[2] * k,
245
+ );
246
+ }
247
+ }
248
+
249
+ function land(b, sc, met) {
250
+ const over = _to.y - groundAt(_to.x, _to.z),
251
+ tg = actorOf(b.to);
252
+ onLand?.(b.kind, _to, sc, met, over < NEAR_GROUND ? over : Infinity);
253
+ if (tg && !met) {
254
+ if (tg.kind === wingedKind) tg.jolt = 1;
255
+ if (b.kind === 'fire' || b.kind === 'dark' || b.kind === 'violet') onWear?.(tg, 0.08, 0.05, 0, 0.24);
256
+ else if (b.kind === 'earth') onWear?.(tg, 0.26, 0.04, 0, 0.06);
257
+ }
258
+ }
259
+
260
+ function splash(b, c, sc, dt, q, met) {
261
+ for (let n = Math.round((met ? 16 : 7) * dt * 24 * q); n > 0; n--) {
262
+ const back = met ? random() * 2 - 1 : -(0.2 + random() * 0.9),
263
+ a = random() * Math.PI * 2,
264
+ out = (met ? 1.3 : 0.9) * (0.4 + random()),
265
+ sp = (met ? 7 : 4.5) * sc,
266
+ k = 0.4 + random() * 0.6;
267
+ _p.copy(_dir)
268
+ .multiplyScalar(back)
269
+ .addScaledVector(_side, Math.cos(a) * out)
270
+ .addScaledVector(_up, Math.sin(a) * out)
271
+ .multiplyScalar(sp);
272
+ if (b.kind === darkKind) {
273
+ onSplashDark?.(_to.x, _to.y, _to.z, _p.x * 0.3, _p.y * 0.3, _p.z * 0.3, 0.22 + random() * 0.12, 0.8 + random() * 0.5, 0.3);
274
+ } else {
275
+ onSplashParticle?.(
276
+ _to.x,
277
+ _to.y,
278
+ _to.z,
279
+ _p.x,
280
+ _p.y + 1.2,
281
+ _p.z,
282
+ 0.25 + random() * 0.4,
283
+ (0.045 + random() * 0.075) * sc,
284
+ c[0] * k,
285
+ c[1] * k,
286
+ c[2] * k,
287
+ );
288
+ }
289
+ }
290
+ }
291
+
292
+ function ward(b, tg, c, dt, q) {
293
+ const l = Math.hypot(_dir.x, _dir.z) || 1;
294
+ onBrace?.(tg, _dir.x / l, _dir.z / l);
295
+ onWear?.(
296
+ tg,
297
+ b.kind === 'earth' ? dt * 0.16 : dt * 0.02,
298
+ dt * 0.07,
299
+ 0,
300
+ b.kind === 'fire' || b.kind === 'dark' || b.kind === 'violet' ? dt * 0.18 : 0,
301
+ );
302
+ for (let n = Math.round(10 * dt * 24 * q); n > 0; n--) {
303
+ let ux = random() * 2 - 1,
304
+ uy = random() * 2 - 1,
305
+ uz = random() * 2 - 1;
306
+ const m = Math.hypot(ux, uy, uz) || 1;
307
+ ux /= m;
308
+ uy /= m;
309
+ uz /= m;
310
+ if (ux * _dir.x + uy * _dir.y + uz * _dir.z > 0) {
311
+ ux = -ux;
312
+ uy = -uy;
313
+ uz = -uz;
314
+ }
315
+ const r = 1.05,
316
+ k = 0.25 + random() * 0.35;
317
+ onWardParticle?.(
318
+ _to.x + ux * r,
319
+ _to.y + uy * r * 0.9,
320
+ _to.z + uz * r,
321
+ ux * 0.6,
322
+ uy * 0.6,
323
+ uz * 0.6,
324
+ 0.18 + random() * 0.2,
325
+ 0.045 + random() * 0.065,
326
+ c[0] * k,
327
+ c[1] * k,
328
+ c[2] * k,
329
+ );
330
+ }
331
+ }
332
+
333
+ function strain(caster, dt) {
334
+ const l = Math.hypot(_dir.x, _dir.z) || 1;
335
+ onBrace?.(caster, -_dir.x / l, -_dir.z / l);
336
+ onWear?.(caster, dt * 0.02, dt * 0.065, 0, 0);
337
+ }
338
+
339
+ function stream(b, S, c, len, sc, girth, tail, head, dt, q) {
340
+ const isDark = b.kind === darkKind,
341
+ dimmed = 0.75 / Math.sqrt(sc);
342
+ b.length = len;
343
+ b.ribbon?.update(b.sample, tail, head, sc * girth * (S.flame ? 0.58 : 0.38), b.t + b.seed);
344
+ if (b.spill) {
345
+ const wave = b.landed ? 0.5 - 0.5 * Math.cos((b.t - b.gather) * 4) : head;
346
+ onPath(b, S, lerp(tail, head, b.landed ? 0.25 + 0.75 * wave : 1), len, _p);
347
+ b.spill.move(_p, 50 * sc * girth * (1 - tail));
348
+ }
349
+ b.acc += (len * 0.15 + 4) * dt * 7 * q * (head - tail) * (0.5 + 0.5 * girth) * (isDark ? SHADOW_RATE : 1);
350
+ while (b.acc >= 1) {
351
+ b.acc -= 1;
352
+ const u = lerp(tail, head, random()),
353
+ j = 0.16 * sc * girth * (S.flame ? 0.6 + 2 * u : 1);
354
+ onPath(b, S, u, len, _p);
355
+ _p.x += (random() - 0.5) * j;
356
+ _p.y += (random() - 0.5) * j;
357
+ _p.z += (random() - 0.5) * j;
358
+ if (isDark) {
359
+ onStreamDark?.(
360
+ _p.x,
361
+ _p.y,
362
+ _p.z,
363
+ random() - 0.5,
364
+ random() - 0.5,
365
+ random() - 0.5,
366
+ (0.2 + random() * 0.2) * girth,
367
+ 0.6 + random() * 0.6,
368
+ 0.32,
369
+ );
370
+ continue;
371
+ }
372
+ const k = (0.3 + random() * 0.45) * dimmed,
373
+ run = S.speed * 0.06;
374
+ onStreamParticle?.(
375
+ _p.x,
376
+ _p.y,
377
+ _p.z,
378
+ _dir.x * run + (random() - 0.5) * 1.2,
379
+ _dir.y * run + (random() - 0.5) * 1.2 + (S.flame ? 1.5 * u : 0),
380
+ _dir.z * run + (random() - 0.5) * 1.2,
381
+ 0.2 + random() * 0.34,
382
+ (0.055 + random() * 0.12) * sc * girth * (S.flame ? 0.7 + 0.9 * u : 1),
383
+ c[0] * k,
384
+ c[1] * k,
385
+ c[2] * k,
386
+ !!S.flame,
387
+ );
388
+ }
389
+ if (head < 1 && !isDark) {
390
+ onPath(b, S, head, len, _p);
391
+ for (let n = Math.round(5 * dt * 24 * q); n > 0; n--) {
392
+ onHeadParticle?.(
393
+ _p.x,
394
+ _p.y,
395
+ _p.z,
396
+ random() - 0.5,
397
+ random() - 0.5,
398
+ random() - 0.5,
399
+ 0.1 + random() * 0.1,
400
+ (0.12 + random() * 0.14) * sc,
401
+ c[0],
402
+ c[1],
403
+ c[2],
404
+ );
405
+ }
406
+ }
407
+ }
408
+
409
+ function update(dt) {
410
+ onBeforeUpdate?.(dt);
411
+ const q = getQualityFx();
412
+ clock += dt;
413
+ for (let i = beams.length - 1; i >= 0; i--) {
414
+ const b = beams[i];
415
+ b.t += dt;
416
+ const S = b.style,
417
+ c = colors[b.kind] || colors.white || DEFAULT_BEAM_COLORS.white,
418
+ caster = actorOf(b.from),
419
+ tg = actorOf(b.to),
420
+ rival = answering(b);
421
+ castFrom(b.from, _from);
422
+ strikeAt(b.to, _to);
423
+ if (caster && caster.kind === wingedKind) {
424
+ if (!caster.aim) caster.aim = _to.clone();
425
+ else caster.aim.copy(_to);
426
+ caster.aiming = 0.3;
427
+ }
428
+ if (rival) _to.copy(_from).lerp(castFrom(rival.from, _other), 0.5 + 0.1 * Math.sin(clock * 2.3) * (b.from < b.to ? 1 : -1));
429
+ const len = _from.distanceTo(_to) || 1,
430
+ sc = clamp(len / 60, 0.6, 2.6),
431
+ run = Math.min(clamp(len / S.speed, 0.1, 0.9), b.dur * 0.4),
432
+ head = clamp((b.t - b.gather) / run, 0, 1),
433
+ tail = clamp((b.t - b.dur) / run, 0, 1);
434
+ _dir.subVectors(_to, _from).divideScalar(len);
435
+ _side.set(_dir.z, 0, -_dir.x);
436
+ if (_side.lengthSq() < 1e-6) _side.set(1, 0, 0);
437
+ _side.normalize();
438
+ _up.crossVectors(_side, _dir);
439
+ if (tail >= 1) {
440
+ if (b.kind !== darkKind && b.kind !== 'gold' && !rival) {
441
+ onScar?.(b.kind === 'violet' || b.kind === 'dark' ? 'dark' : 'fire', _to.x, _to.y, _to.z, 0.7);
442
+ }
443
+ releaseBeam(b);
444
+ beams[i] = beams[beams.length - 1];
445
+ beams.pop();
446
+ continue;
447
+ }
448
+ if (b.t < b.gather) {
449
+ b.spill?.move(_from, (25 * b.t) / b.gather);
450
+ gather(b, c, dt, q);
451
+ continue;
452
+ }
453
+ if (!b.kick) {
454
+ b.kick = true;
455
+ if (caster) {
456
+ caster.recoil = 1;
457
+ onWear?.(caster, 0, 0.04, 0, 0);
458
+ }
459
+ }
460
+ stream(b, S, c, len, sc, caster && caster.kind === wingedKind ? WINGED_BREATH * caster.group.scale.y : 1, tail, head, dt, q);
461
+ if (head < 1) continue;
462
+ if (!b.landed) {
463
+ b.landed = true;
464
+ land(b, sc, !!rival);
465
+ }
466
+ if (b.kind !== 'gold') onAlarm?.(_to.x, _to.z, 18);
467
+ splash(b, c, sc, dt, q, !!rival);
468
+ if (rival && caster && caster.kind === 'human' && caster.pose !== 'dead') strain(caster, dt);
469
+ if (rival || b.kind === darkKind || !tg) continue;
470
+ if (tg.kind === wingedKind) tg.jolt = Math.max(tg.jolt || 0, 0.45);
471
+ else if (tg.kind === 'human' && tg.pose !== 'dead') ward(b, tg, c, dt, q);
472
+ }
473
+ let brightBeams = 0;
474
+ for (let i = 0; i < beams.length; i++) if (beams[i].kind !== darkKind) brightBeams++;
475
+ const targetBoost = brightBeams > 0 ? Math.min(24, 12 + brightBeams * 6) : 0;
476
+ bloomBoost += (targetBoost - bloomBoost) * Math.min(1, dt * 10);
477
+ onBloomBoost?.(bloomBoost);
478
+ }
479
+
480
+ function dispose() {
481
+ clear();
482
+ ribbons.dispose();
483
+ }
484
+
485
+ return { start, update, clear, dispose, beams, ribbons };
486
+ }