roamwright 0.61.26 → 0.61.28

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,309 @@
1
+ import * as THREE from 'three';
2
+ import {
3
+ createCrowdSystem,
4
+ createDaylightSky,
5
+ createHouseKit,
6
+ createHouseKitMaterials,
7
+ createHouseKitMeshes,
8
+ createOrbitCamera,
9
+ createSpaces,
10
+ createTroopDirector,
11
+ createTroopMounts,
12
+ createTroopStandards,
13
+ scatterTrees,
14
+ scatterTussocks,
15
+ } from 'roamwright';
16
+ import { createHeightfieldMesh } from 'roamwright/geometry/heightfield.js';
17
+ import { createProps } from 'roamwright/geometry/props.js';
18
+ import { sstep } from 'roamwright/math/scalar.js';
19
+ import { createVehicleTrains } from 'roamwright/rendering/vehicle-train.js';
20
+ import { initExample } from './stage.js';
21
+
22
+ const {
23
+ canvas,
24
+ renderer,
25
+ camera,
26
+ scene,
27
+ worldGroup,
28
+ seed,
29
+ random: rng,
30
+ noise,
31
+ hash,
32
+ shapes,
33
+ } = initExample({
34
+ fallbackSeed: 42,
35
+ fov: 38,
36
+ });
37
+
38
+ // Gentle sunlit meadow plain ringed by rolling highland ridges so formations read clearly.
39
+ function groundHeight(x, z) {
40
+ const r = Math.hypot(x, z);
41
+ const rim = sstep(46, 150, r);
42
+ const ridge = rim * (14 * noise.fbm(x * 0.0085, z * 0.0085, 4, 29) + 5 * noise.ridged(x * 0.006, z * 0.006, 3, 17));
43
+ return 0.22 * Math.sin(x * 0.035 + seed * 0.1) * Math.cos(z * 0.03) + ridge;
44
+ }
45
+
46
+ const meadowCol = new THREE.Color(0x7c8652),
47
+ dryGrassCol = new THREE.Color(0x9a8f63),
48
+ trackCol = new THREE.Color(0xa69473);
49
+ const terrain = createHeightfieldMesh({
50
+ size: 420,
51
+ segs: 136,
52
+ heightAt: groundHeight,
53
+ colorAt: (x, y, z, up, out) => {
54
+ const f = noise.fbm(x * 0.038, z * 0.038, 3, 11);
55
+ const onTrack = 1 - sstep(4.5, 11, Math.abs(z + 17));
56
+ out.copy(meadowCol).lerp(dryGrassCol, f);
57
+ out.lerp(trackCol, onTrack * 0.65);
58
+ out.multiplyScalar(0.88 + 0.22 * noise.vnoise(x * 0.15, z * 0.15, 7));
59
+ },
60
+ });
61
+ worldGroup.add(terrain);
62
+
63
+ // Trees and tussocks framing the distant ridge while keeping the parade plain clear.
64
+ const { material: leafMat } = scatterTrees({
65
+ parent: worldGroup,
66
+ noise,
67
+ hash,
68
+ seed,
69
+ random: rng,
70
+ groundAt: groundHeight,
71
+ attempts: 150,
72
+ maxPerSpecies: 48,
73
+ species: ['broadleaf', 'conifer'],
74
+ scaleRange: [0.85, 0.65],
75
+ sink: 0.15,
76
+ sample: () => {
77
+ const angle = rng() * Math.PI * 2;
78
+ const dist = 42 + rng() * 95;
79
+ const tx = Math.cos(angle) * dist;
80
+ const tz = Math.sin(angle) * dist;
81
+ return Math.abs(tz + 17) < 12 ? null : [tx, tz];
82
+ },
83
+ });
84
+
85
+ scatterTussocks({
86
+ parent: worldGroup,
87
+ seed: seed * 17,
88
+ random: rng,
89
+ groundAt: groundHeight,
90
+ count: 85,
91
+ attempts: 130,
92
+ scaleRange: [0.8, 0.55],
93
+ material: leafMat,
94
+ sample: () => {
95
+ const tx = (rng() - 0.5) * 120;
96
+ const tz = (rng() - 0.5) * 100;
97
+ return Math.hypot(tx, tz) < 22 ? null : [tx, tz];
98
+ },
99
+ });
100
+
101
+ // Kits for standards, supply wagons, and crowd batches.
102
+ const houseKit = createHouseKit({ shapes });
103
+ const props = createProps({ houseKit, shapes, vnoise: noise.vnoise, hash, horse: () => {} });
104
+ const kitMeshes = createHouseKitMeshes({ materials: createHouseKitMaterials({ doubleSide: true }) });
105
+ const { makeVehicles } = createVehicleTrains({
106
+ kitNew: houseKit.kitNew,
107
+ kitAt: houseKit.kitAt,
108
+ kitInstanced: kitMeshes.kitInstanced,
109
+ propCart: props.propCart,
110
+ propWagon: props.propWagon,
111
+ propWheel: props.propWheel,
112
+ });
113
+
114
+ const { lightDir } = createDaylightSky({
115
+ scene,
116
+ renderer,
117
+ noise,
118
+ sky: { sunDir: [0.64, 0.36, 0.68] },
119
+ sunPosition: [58, 48, 62],
120
+ shadowRadius: 72,
121
+ shadowFar: 220,
122
+ fogColor: 0xd4dfd9,
123
+ fogDensity: 0.002,
124
+ });
125
+
126
+ const spaceKit = createSpaces({ id: 'plain', ground: groundHeight });
127
+ const crowdSystem = createCrowdSystem({ scene: worldGroup, groundAt: groundHeight, spaceKit, camera, renderer });
128
+
129
+ let elapsed = 0;
130
+ let frame = 0;
131
+ const updater = crowdSystem.createUpdater({
132
+ getTime: () => elapsed,
133
+ getFrame: () => frame,
134
+ getLightDir: () => lightDir,
135
+ shadowRange: 72,
136
+ shadowFar: 220,
137
+ });
138
+
139
+ const STANDARDS = {
140
+ crimson: [0x762626, 0xd9c28c],
141
+ cobalt: [0x26486c, 0xd8d2c0],
142
+ };
143
+
144
+ const troopMounts = createTroopMounts({ camera, makeVehicles });
145
+ const troopStandards = createTroopStandards({
146
+ kitNew: houseKit.kitNew,
147
+ kitAt: houseKit.kitAt,
148
+ kitInstanced: kitMeshes.kitInstanced,
149
+ propStandard: props.propStandard,
150
+ getFrame: () => frame,
151
+ standardOf: id => STANDARDS[id] || null,
152
+ });
153
+
154
+ const director = createTroopDirector({
155
+ seed,
156
+ camera,
157
+ getTime: () => elapsed,
158
+ addCrowd: (list, tones, cx, cz, near, kind, _bodyId, spaceId) => crowdSystem.addCrowd(list, tones, cx, cz, near, kind, null, spaceId),
159
+ standardOf: id => STANDARDS[id] || null,
160
+ makeMounts: troopMounts.makeMounts,
161
+ makeHerd: troopMounts.makeHerd,
162
+ makeWagons: troopMounts.makeWagons,
163
+ makeStandards: troopStandards.makeStandards,
164
+ });
165
+
166
+ const plainSpace = { id: 'plain', group: worldGroup, ground: groundHeight };
167
+ const tickHost = director.raiseHost(
168
+ plainSpace,
169
+ 'field',
170
+ [
171
+ {
172
+ originX: 0,
173
+ originZ: 0,
174
+ troops: {
175
+ crimsonFoot: {
176
+ body: 'crimson',
177
+ kind: 'trooper',
178
+ tones: [0x762626, 0x622222, 0x5c362c],
179
+ n: 36,
180
+ wide: 9,
181
+ gap: [1.15, 1.3],
182
+ loose: 0.22,
183
+ at: [-9, 0],
184
+ face: [20, 0],
185
+ },
186
+ crimsonBows: {
187
+ body: 'crimson',
188
+ kind: 'guard',
189
+ tones: [0x6e3a2c, 0x583226],
190
+ n: 16,
191
+ wide: 8,
192
+ gap: [1.2, 1.3],
193
+ loose: 0.25,
194
+ at: [-17, 1],
195
+ face: [20, 1],
196
+ },
197
+ cobaltFoot: {
198
+ body: 'cobalt',
199
+ kind: 'raider',
200
+ tones: [0x2a4a6e, 0x38587a, 0x4b5b6b],
201
+ n: 36,
202
+ wide: 9,
203
+ gap: [1.15, 1.3],
204
+ loose: 0.28,
205
+ at: [8, -1],
206
+ face: [-20, -1],
207
+ },
208
+ cobaltLancers: {
209
+ body: 'cobalt',
210
+ kind: 'plain',
211
+ tones: [0x2a4a6e, 0x3b5878, 0x606c7a],
212
+ n: 9,
213
+ wide: 3,
214
+ gap: [2.2, 3.0],
215
+ loose: 0.2,
216
+ mounted: true,
217
+ lance: true,
218
+ at: [7.5, 8.5],
219
+ face: [-20, 6],
220
+ },
221
+ wagons: {
222
+ beast: 'wagon',
223
+ tones: [0x8a6a4a, 0xa89878, 0x5a4636],
224
+ n: 2,
225
+ wide: 1,
226
+ gap: [4.5, 11],
227
+ loose: 0.08,
228
+ at: [-2, -16],
229
+ face: [40, -16],
230
+ },
231
+ herd: {
232
+ beast: 'cattle',
233
+ tones: [0x8a6a4a, 0xa89878, 0x5a4636, 0xc8b89a],
234
+ n: 10,
235
+ wide: 5,
236
+ gap: [1.8, 2.2],
237
+ loose: 0.4,
238
+ at: [11, -16],
239
+ face: [40, -16],
240
+ },
241
+ },
242
+ },
243
+ ],
244
+ 400,
245
+ );
246
+
247
+ crowdSystem.build();
248
+
249
+ const views = {
250
+ line: { eye: [-1, 7.6, 28], target: [0.5, 1.9, -1] },
251
+ cavalry: { eye: [-2, 4.6, 19], target: [7, 1.8, 8] },
252
+ train: { eye: [-16, 5.8, -3], target: [-1, 1.6, -16] },
253
+ overview: { eye: [0, 38, 56], target: [0, 1, -2] },
254
+ };
255
+
256
+ const rig = createOrbitCamera({
257
+ camera,
258
+ canvas,
259
+ renderer,
260
+ scene,
261
+ views,
262
+ initialView: 'line',
263
+ groundAt: groundHeight,
264
+ minClearance: 1.2,
265
+ minDistance: 6,
266
+ maxDistance: 170,
267
+ onDraw: () => {},
268
+ });
269
+
270
+ const anchor = { x: 0, z: 0 };
271
+ const fireCue = (verb, targets, ...args) => director.cue('field', [elapsed, 'cue', 'field', verb, targets, ...args], anchor);
272
+
273
+ document.querySelector('#march').addEventListener('click', () => {
274
+ fireCue('go', 'crimsonFoot', -5, 0, 5, 20, 0);
275
+ fireCue('go', 'cobaltFoot', 5, 0, 5, -20, 0);
276
+ fireCue('go', 'cobaltLancers', 4, 8, 4.5, -20, 4);
277
+ fireCue('go', 'wagons', 8, -17, 10, 40, -17);
278
+ fireCue('go', 'herd', 24, -18, 10, 40, -18);
279
+ });
280
+ document.querySelector('#fight').addEventListener('click', () => {
281
+ fireCue('fight', ['crimsonFoot', 'cobaltFoot', 'cobaltLancers'], 1.25);
282
+ });
283
+ document.querySelector('#fall').addEventListener('click', () => {
284
+ fireCue('fall', ['crimsonFoot', 'cobaltFoot'], 0.35, 3);
285
+ });
286
+ document.querySelector('#reset').addEventListener('click', () => {
287
+ director.resetAll();
288
+ });
289
+
290
+ // Start with a brief opening advance so the first frames show active ranks, lancers, and rolling wagons.
291
+ fireCue('go', 'crimsonFoot', -5, 0, 6, 20, 0);
292
+ fireCue('go', 'cobaltFoot', 5, -1, 6, -20, -1);
293
+ fireCue('go', 'cobaltLancers', 3.5, 7.5, 6, -20, 5);
294
+ fireCue('go', 'wagons', 4, -16, 12, 40, -16);
295
+ fireCue('go', 'herd', 17, -16, 12, 40, -16);
296
+
297
+ const clock = new THREE.Timer();
298
+ function draw() {
299
+ clock.update();
300
+ const dt = Math.min(clock.getDelta(), 0.1);
301
+ elapsed += dt;
302
+ frame++;
303
+ rig.clampCamera();
304
+ tickHost(dt);
305
+ updater.updateCrowds();
306
+ renderer.render(scene, camera);
307
+ requestAnimationFrame(draw);
308
+ }
309
+ draw();
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Military and caravan camp layout builders: pitched tent grids with interspersed cookfires and
3
+ * frame-budgeted wagon lines with box colliders. Pure layout calling caller-supplied prop kits;
4
+ * worker-safe (no Three.js imports).
5
+ * @module
6
+ */
7
+
8
+ import { TAU } from '../math/scalar.js';
9
+ import { C as defaultColor } from './primitives.js';
10
+
11
+ /** Default spacing, gap probabilities and sun-bleached sailcloth palette for camp layouts. */
12
+ export const CAMP_DEFAULTS = Object.freeze({
13
+ canvasPalette: Object.freeze([0xb8a988, 0xa89878, 0xc4b494, 0x9a8a6c]),
14
+ tentSkip: 0.12,
15
+ fileSpacing: 6.2,
16
+ rowSpacing: 8.4,
17
+ tentCollider: Object.freeze({ hx: 1.7, hz: 2.3 }),
18
+ wagonCollider: Object.freeze({ hx: 1.4, hz: 2.6 }),
19
+ coveredChance: 0.7,
20
+ wagonsPerBreath: 2,
21
+ });
22
+
23
+ /**
24
+ * Lay out a rotated `rows` × `files` grid of pitched tents around `(x, z)` in a local spot frame,
25
+ * skipping occasional gaps, pushing box colliders into `solid`, and placing a cookfire beside every
26
+ * fourth tent.
27
+ *
28
+ * @param {object} options
29
+ * @param {object} options.kit - Destination kit builders passed to `placeKit`.
30
+ * @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} options.spot - Local spot frame (`createSpotFrame`).
31
+ * @param {() => number} options.random - Deterministic PRNG in `[0, 1)`.
32
+ * @param {number} [options.x=0] - Grid centre local X in metres.
33
+ * @param {number} [options.z=0] - Grid centre local Z in metres.
34
+ * @param {number} [options.turn=0] - Grid yaw bearing in radians.
35
+ * @param {number} options.rows - Number of tent rows.
36
+ * @param {number} options.files - Number of tent files per row.
37
+ * @param {Array<object> | null} [options.solid=null] - Optional collider output list.
38
+ * @param {(kit: object, x: number, y: number, z: number, yaw: number, plain: boolean, prop: Function, rng: () => number) => void} options.placeKit - Prop placement helper (`kitAt`).
39
+ * @param {(B: object, rng: () => number, canvas: number[]) => void} options.buildTent - Single tent geometry builder.
40
+ * @param {(B: object, rng: () => number) => void} [options.buildCookfire] - Optional cookfire geometry builder.
41
+ * @param {((x: number, y: number, z: number, size: number, smoke: number) => void) | null} [options.onFire=null] - Callback invoked for each placed cookfire in world coordinates.
42
+ * @param {readonly number[]} [options.canvasPalette=CAMP_DEFAULTS.canvasPalette] - Tent canvas sRGB hex tones.
43
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
44
+ */
45
+ export function buildTentLines({
46
+ kit,
47
+ spot,
48
+ random,
49
+ x = 0,
50
+ z = 0,
51
+ turn = 0,
52
+ rows,
53
+ files,
54
+ solid = null,
55
+ placeKit,
56
+ buildTent,
57
+ buildCookfire = null,
58
+ onFire = null,
59
+ canvasPalette = CAMP_DEFAULTS.canvasPalette,
60
+ color = defaultColor,
61
+ }) {
62
+ const c = Math.cos(turn);
63
+ const s = Math.sin(turn);
64
+ for (let i = 0; i < rows; i++)
65
+ for (let j = 0; j < files; j++) {
66
+ if (random() < CAMP_DEFAULTS.tentSkip) continue;
67
+ const dx = (j - (files - 1) / 2) * CAMP_DEFAULTS.fileSpacing + (random() - 0.5);
68
+ const dz = (i - (rows - 1) / 2) * CAMP_DEFAULTS.rowSpacing + (random() - 0.5);
69
+ const px = x + dx * c + dz * s;
70
+ const pz = z - dx * s + dz * c;
71
+ const py = spot.Y(px, pz);
72
+ placeKit(
73
+ kit,
74
+ spot.x0 + px,
75
+ py,
76
+ spot.z0 + pz,
77
+ turn + (random() - 0.5) * 0.12,
78
+ false,
79
+ (B, r) => buildTent(B, r, color(canvasPalette[(random() * canvasPalette.length) | 0], 0.06, random)),
80
+ random,
81
+ );
82
+ if (solid)
83
+ solid.push({ x: spot.x0 + px, z: spot.z0 + pz, hx: CAMP_DEFAULTS.tentCollider.hx, hz: CAMP_DEFAULTS.tentCollider.hz, rot: turn });
84
+ if ((i * files + j) % 4 === 1) {
85
+ const qx = px + 3.1 * c + 0.4 * s;
86
+ const qz = pz - 3.1 * s + 0.4 * c;
87
+ if (buildCookfire) placeKit(kit, spot.x0 + qx, spot.Y(qx, qz), spot.z0 + qz, random() * TAU, false, buildCookfire, random);
88
+ if (onFire) onFire(spot.x0 + qx, spot.Y(qx, qz), spot.z0 + qz, 0.3, 0.6);
89
+ }
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Draw up a line of `count` wagons starting at `(x, z)` spaced `gap` metres apart along bearing
95
+ * `turn`, pushing box colliders into `solid` and optionally yielding between small batches via
96
+ * `breathe` so streaming frames stay smooth.
97
+ *
98
+ * @param {object} options
99
+ * @param {object} options.kit - Destination kit builders passed to `placeKit`.
100
+ * @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} options.spot - Local spot frame (`createSpotFrame`).
101
+ * @param {() => number} options.random - Deterministic PRNG in `[0, 1)`.
102
+ * @param {number} [options.x=0] - Start local X in metres.
103
+ * @param {number} [options.z=0] - Start local Z in metres.
104
+ * @param {number} [options.turn=0] - Line heading in radians.
105
+ * @param {number} options.count - Number of wagons to place.
106
+ * @param {number} options.gap - Spacing between wagons in metres.
107
+ * @param {Array<object> | null} [options.solid=null] - Optional collider output list.
108
+ * @param {(kit: object, x: number, y: number, z: number, yaw: number, plain: boolean, prop: Function, rng: () => number) => void} options.placeKit - Prop placement helper (`kitAt`).
109
+ * @param {(B: object, rng: () => number, covered: boolean) => void} options.buildWagon - Single wagon geometry builder.
110
+ * @param {(() => Promise<void>) | null} [options.breathe=null] - Optional async frame-yield callback between batches.
111
+ * @param {number} [options.batchSize=CAMP_DEFAULTS.wagonsPerBreath] - Wagons built before calling `breathe`.
112
+ * @returns {Promise<void>}
113
+ */
114
+ export async function buildWagonLine({
115
+ kit,
116
+ spot,
117
+ random,
118
+ x = 0,
119
+ z = 0,
120
+ turn = 0,
121
+ count,
122
+ gap,
123
+ solid = null,
124
+ placeKit,
125
+ buildWagon,
126
+ breathe = null,
127
+ batchSize = CAMP_DEFAULTS.wagonsPerBreath,
128
+ }) {
129
+ for (let first = 0; first < count; first += batchSize) {
130
+ const past = Math.min(first + batchSize, count);
131
+ for (let i = first; i < past; i++) {
132
+ const px = x + Math.sin(turn) * i * gap + (random() - 0.5) * 0.8;
133
+ const pz = z + Math.cos(turn) * i * gap + (random() - 0.5) * 0.8;
134
+ const yaw = turn + (random() - 0.5) * 0.25;
135
+ placeKit(
136
+ kit,
137
+ spot.x0 + px,
138
+ spot.Y(px, pz),
139
+ spot.z0 + pz,
140
+ yaw,
141
+ false,
142
+ (B, r) => buildWagon(B, r, random() < CAMP_DEFAULTS.coveredChance),
143
+ random,
144
+ );
145
+ if (solid)
146
+ solid.push({ x: spot.x0 + px, z: spot.z0 + pz, hx: CAMP_DEFAULTS.wagonCollider.hx, hz: CAMP_DEFAULTS.wagonCollider.hz, rot: yaw });
147
+ }
148
+ if (breathe) await breathe();
149
+ }
150
+ }