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,137 @@
1
+ /**
2
+ * Scene script coordinate resolution and step-analysis helpers: resolves anchor-relative positions,
3
+ * named places, `['ground', offset]` heights, continuous walks, prop-carried moves, and blast wear.
4
+ * Pure runtime module with zero Three.js or DOM dependencies.
5
+ * @module
6
+ */
7
+
8
+ const DEFAULT_WALKS_ON = 0.15;
9
+
10
+ /**
11
+ * Resolves a scene height specification relative to `anchorY`, ground height, or a named place.
12
+ * Accepts a numeric offset from `anchorY`, `'ground'`, a named place key, or `['ground' | place, offset]`.
13
+ * @param {number | string | [string, number]} value Height specification from a scene script.
14
+ * @param {number} anchorY Scene anchor world Y height.
15
+ * @param {number} x World X coordinate used when sampling `'ground'`.
16
+ * @param {number} z World Z coordinate used when sampling `'ground'`.
17
+ * @param {(x: number, z: number) => number} groundAt Ground height sampler.
18
+ * @param {(name: string) => number | { y: number }} [resolveNamed] Named-place lookup.
19
+ * @returns {number} Resolved world Y coordinate in metres.
20
+ */
21
+ export function resolveSceneY(value, anchorY, x, z, groundAt, resolveNamed) {
22
+ if (Array.isArray(value)) {
23
+ const base = value[0] === 'ground' ? groundAt(x, z) : resolveSceneY(value[0], anchorY, x, z, groundAt, resolveNamed);
24
+ return base + (value[1] || 0);
25
+ }
26
+ if (typeof value === 'string') {
27
+ if (value === 'ground') return groundAt(x, z);
28
+ const place = resolveNamed?.(value);
29
+ return typeof place === 'number' ? place : place.y;
30
+ }
31
+ return anchorY + value;
32
+ }
33
+
34
+ /**
35
+ * Resolves a scene position specification into `out` (`{ x, y, z }`): a special `'@…'` target,
36
+ * an actor id (at chest height `y + height * 0.72`), or an anchor-relative `[dx, ySpec, dz]` triple.
37
+ * @param {string | [number, number | string | [string, number], number]} spec Position specification.
38
+ * @param {{ x: number, y: number, z: number }} anchor Scene anchor world position.
39
+ * @param {Record<string, { x: number, y: number, z: number, height: number }>} actors Actor table by id.
40
+ * @param {(x: number, z: number) => number} groundAt Ground height sampler.
41
+ * @param {(name: string) => number | { y: number }} [resolveNamed] Named-place lookup.
42
+ * @param {(name: string, out: { x: number, y: number, z: number }) => boolean} [resolveSpecial] Handler for special string targets (e.g. `'@…'`).
43
+ * @param {{ x: number, y: number, z: number, set?: Function }} out Destination vector or coordinate object.
44
+ * @returns {{ x: number, y: number, z: number }} The populated `out` object.
45
+ */
46
+ export function resolveScenePos(spec, anchor, actors, groundAt, resolveNamed, resolveSpecial, out) {
47
+ if (typeof spec === 'string') {
48
+ if (resolveSpecial && resolveSpecial(spec, out)) return out;
49
+ const a = actors[spec];
50
+ const x = a.x,
51
+ y = a.y + a.height * 0.72,
52
+ z = a.z;
53
+ if (out.set) return out.set(x, y, z);
54
+ out.x = x;
55
+ out.y = y;
56
+ out.z = z;
57
+ return out;
58
+ }
59
+ const wx = anchor.x + spec[0],
60
+ wz = anchor.z + spec[2],
61
+ wy = resolveSceneY(spec[1], anchor.y, wx, wz, groundAt, resolveNamed);
62
+ if (out.set) return out.set(wx, wy, wz);
63
+ out.x = wx;
64
+ out.y = wy;
65
+ out.z = wz;
66
+ return out;
67
+ }
68
+
69
+ /**
70
+ * Returns true if the same actor has another `'move'` step beginning within `windowSeconds` of
71
+ * `step`'s completion, so locomotion flows smoothly without halting between segments.
72
+ * @param {readonly any[]} steps Chronological scene steps array.
73
+ * @param {number} nextIndex Index of the next step after `step`.
74
+ * @param {readonly any[]} step Current `'move'` step `[time, 'move', actorId, x, z, dur, ...]`.
75
+ * @param {number} [windowSeconds] Maximum gap in seconds to treat as continuous walking (default `0.15`).
76
+ * @returns {boolean} True when the actor continues walking into the next move.
77
+ */
78
+ export function stepWalksOn(steps, nextIndex, step, windowSeconds = DEFAULT_WALKS_ON) {
79
+ const ends = step[0] + step[5];
80
+ for (let i = nextIndex; i < steps.length && steps[i][0] <= ends + windowSeconds; i++) {
81
+ if (steps[i][1] === 'move' && steps[i][2] === step[2] && steps[i][0] >= ends - windowSeconds) return true;
82
+ }
83
+ return false;
84
+ }
85
+
86
+ /**
87
+ * Returns true if a `'move'` step matches the start time and duration of a `'prop' ... 'to'` step
88
+ * (such as passengers carried aboard a moving boat), indicating the actor is carried rather than walking.
89
+ * @param {readonly any[]} steps Scene steps array.
90
+ * @param {readonly any[]} step Current `'move'` step.
91
+ * @returns {boolean} True when carried by a synchronized moving prop.
92
+ */
93
+ export function stepRidesProp(steps, step) {
94
+ for (let i = 0; i < steps.length; i++) {
95
+ const other = steps[i];
96
+ if (other[1] === 'prop' && other[3] === 'to' && Math.abs(other[0] - step[0]) < 0.05 && other[8] === step[5]) return true;
97
+ }
98
+ return false;
99
+ }
100
+
101
+ /**
102
+ * Applies blood splatter, blast scorch, or dust wear to active cast members near a scripted burst.
103
+ * @param {Record<string, object> | null | undefined} cast Scene cast definition map.
104
+ * @param {Record<string, object>} actors Actor table by id.
105
+ * @param {string} kind Burst kind (`'blood'`, `'fire'`, `'dark'`, `'violet'`, `'earth'`, `'dust'`, etc.).
106
+ * @param {any} targetSpec Burst target spec (actor id or position).
107
+ * @param {number} x Burst world X coordinate.
108
+ * @param {number} z Burst world Z coordinate.
109
+ * @param {number} [size] Burst radius scale (default `1`).
110
+ * @param {(actor: object, dust: number, sweat: number, blood: number, scorch: number) => void} [addWear] Wear accumulator callback.
111
+ */
112
+ export function applyBurstWear(cast, actors, kind, targetSpec, x, z, size = 1, addWear = () => {}) {
113
+ if (!cast) return;
114
+ if (kind === 'blood') {
115
+ const direct = typeof targetSpec === 'string' ? actors[targetSpec] : null;
116
+ if (direct) addWear(direct, 0, 0, 0.44, 0);
117
+ for (const id in cast) {
118
+ const c = actors[id];
119
+ if (!c?.active || c === direct) continue;
120
+ const d = Math.hypot(c.x - x, c.z - z);
121
+ if (d < 2.2) addWear(c, 0, 0, 0.18 * (1 - d / 2.2), 0);
122
+ }
123
+ return;
124
+ }
125
+ const scorchBlast = kind === 'fire' || kind === 'dark' || kind === 'violet',
126
+ dustBlast = kind === 'earth' || kind === 'dust';
127
+ if (!scorchBlast && !dustBlast) return;
128
+ const reach = Math.max(3, size * 2.4);
129
+ for (const id in cast) {
130
+ const c = actors[id];
131
+ if (!c?.active) continue;
132
+ const d = Math.hypot(c.x - x, c.z - z);
133
+ if (d >= reach) continue;
134
+ const falloff = 1 - d / reach;
135
+ addWear(c, (dustBlast ? 0.34 : 0.14) * falloff, 0.04 * falloff, 0, scorchBlast ? 0.46 * falloff : 0);
136
+ }
137
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Scene anchor resolution, multi-zone pre-streaming collector, and caption timing helpers.
3
+ * Pure runtime module with zero Three.js or DOM dependencies.
4
+ * @module
5
+ */
6
+
7
+ /**
8
+ * Default minimum distance in metres between distinct scene camera or actor zones queued for pre-streaming.
9
+ */
10
+ export const DEFAULT_ZONE_STRIDE = 90;
11
+
12
+ /**
13
+ * Resolves a scene's world anchor into `out` (`{ x, y, z }`) from either a named place (with
14
+ * optional opening offset `ev.off`) or the scene space's origin.
15
+ * @param {object} ev Scene event record with `space`, optional `at`, and optional `off`.
16
+ * @param {string | null | undefined} key Optional override place key (e.g. from an `'anchor'` step).
17
+ * @param {Record<string, { origin: { x: number, z: number }, ground: (x: number, z: number) => number }>} spaces Map of spaces by id.
18
+ * @param {(name: string) => { x: number, z: number }} resolveNamed Named-place lookup.
19
+ * @param {{ x: number, y: number, z: number }} out Destination anchor object.
20
+ * @returns {{ x: number, y: number, z: number }} The populated `out` object.
21
+ */
22
+ export function resolveSceneAnchor(ev, key, spaces, resolveNamed, out) {
23
+ const sp = spaces[ev.space];
24
+ if (!ev.at && !key) {
25
+ const o = sp.origin;
26
+ out.x = o.x;
27
+ out.z = o.z;
28
+ out.y = sp.ground(o.x, o.z);
29
+ return out;
30
+ }
31
+ const p = resolveNamed(key || ev.at),
32
+ off = key ? null : ev.off;
33
+ out.x = p.x + (off ? off[0] : 0);
34
+ out.z = p.z + (off ? off[1] : 0);
35
+ out.y = sp.ground(out.x, out.z);
36
+ return out;
37
+ }
38
+
39
+ /**
40
+ * Collects distinct `(x, z)` zones visited by a scene's opening anchor, secondary `'anchor'` shifts,
41
+ * `'place'`/`'move'` actor marks, and camera track keyframes spaced at least `stride` metres apart.
42
+ * @param {object} ev Scene event record with `steps` and `cam`.
43
+ * @param {{ x: number, z: number }} openingAnchor Opening world anchor.
44
+ * @param {(key: string, out: { x: number, y: number, z: number }) => { x: number, y: number, z: number }} resolveAnchor Resolves a secondary anchor key.
45
+ * @param {number} [stride] Minimum separation in metres between queued zones (default `90`).
46
+ * @returns {[number, number][]} Array of `[x, z]` zone coordinates.
47
+ */
48
+ export function collectSceneZones(ev, openingAnchor, resolveAnchor, stride = DEFAULT_ZONE_STRIDE) {
49
+ const zones = [[openingAnchor.x, openingAnchor.z]];
50
+ const zoneAnchor = { x: 0, y: 0, z: 0 };
51
+ const addZone = (x, z) => {
52
+ if (!Number.isFinite(x) || !Number.isFinite(z)) return;
53
+ for (let i = 0; i < zones.length; i++) if (Math.hypot(x - zones[i][0], z - zones[i][1]) < stride) return;
54
+ zones.push([x, z]);
55
+ };
56
+ let ax = openingAnchor.x,
57
+ az = openingAnchor.z;
58
+ const anchors = [[0, ax, az]];
59
+ for (const s of ev.steps ?? []) {
60
+ if (s[1] === 'anchor') {
61
+ resolveAnchor(s[2], zoneAnchor);
62
+ ax = zoneAnchor.x;
63
+ az = zoneAnchor.z;
64
+ anchors.push([s[0], ax, az]);
65
+ addZone(ax, az);
66
+ } else if (s[1] === 'place' || s[1] === 'move') addZone(ax + s[3], az + s[4]);
67
+ }
68
+ let ai = 0;
69
+ for (const k of ev.cam ?? []) {
70
+ while (ai + 1 < anchors.length && anchors[ai + 1][0] <= k[0]) ai++;
71
+ addZone(anchors[ai][1] + k[1], anchors[ai][2] + k[3]);
72
+ }
73
+ return zones;
74
+ }
75
+
76
+ /**
77
+ * Pre-streams ambient figures and near settlement/set detail across every zone a scene visits.
78
+ * @param {object} ev Scene event record.
79
+ * @param {{ x: number, z: number }} openingAnchor Opening world anchor.
80
+ * @param {object} options
81
+ * @param {(key: string, out: { x: number, y: number, z: number }) => { x: number, y: number, z: number }} options.resolveAnchor Secondary anchor resolver.
82
+ * @param {(x: number, z: number) => number} options.groundAt Ground height sampler for the scene's space.
83
+ * @param {(spaceId: string, x: number, y: number, z: number) => void} [options.orderSeenFrom] Orders ambient cast visible from a zone point.
84
+ * @param {(spaceId: string, x: number, z: number) => Promise<any> | null | undefined} [options.nearDetailAt] Requests near settlement/set detail at `(x, z)`.
85
+ * @param {number} [options.stride] Minimum zone spacing in metres (default `90`).
86
+ * @returns {Promise<any[]> | null} Combined promise for pending detail loads, or `null` when nothing async was queued.
87
+ */
88
+ export function prepareSceneZones(
89
+ ev,
90
+ openingAnchor,
91
+ { resolveAnchor, groundAt, orderSeenFrom, nearDetailAt, stride = DEFAULT_ZONE_STRIDE },
92
+ ) {
93
+ const zones = collectSceneZones(ev, openingAnchor, resolveAnchor, stride);
94
+ const waits = [];
95
+ for (let i = 0; i < zones.length; i++) {
96
+ const [zx, zz] = zones[i];
97
+ orderSeenFrom?.(ev.space, zx, groundAt(zx, zz) + 2, zz);
98
+ const detail = nearDetailAt?.(ev.space, zx, zz);
99
+ if (detail) waits.push(detail);
100
+ }
101
+ return waits.length ? Promise.all(waits) : null;
102
+ }
103
+
104
+ /**
105
+ * Returns the active caption index (`-1` before `leadIn`) sharing a scene's duration equally across `count` captions.
106
+ * @param {number} elapsed Scene elapsed time in seconds.
107
+ * @param {number} duration Scene total duration in seconds.
108
+ * @param {number} count Number of caption lines.
109
+ * @param {number} [leadIn] Opening delay in seconds before the first caption appears (default `1.6`).
110
+ * @param {number} [margin] Combined lead-in/out margin subtracted from duration (default `2.4`).
111
+ * @returns {number} Active caption index in `-1 .. count - 1`.
112
+ */
113
+ export function captionIndexAt(elapsed, duration, count, leadIn = 1.6, margin = 2.4) {
114
+ if (count <= 0 || elapsed < leadIn) return -1;
115
+ const slot = (duration - margin) / count;
116
+ return Math.min(count - 1, Math.floor((elapsed - leadIn) / slot));
117
+ }