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,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Roamwright's public rendering, materials and drawn systems, grouped by subsystem. Re-exported whole from `roamwright`.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ── Context, math & core geometry ────────────────────────────────────────
|
|
7
|
+
export { RENDER_LAYERS, createRenderContext } from './context.js';
|
|
8
|
+
export {
|
|
9
|
+
UNSIGNED_HDR_SENTINEL,
|
|
10
|
+
createMatchedRenderTarget,
|
|
11
|
+
createMatchingRenderTarget,
|
|
12
|
+
describeRenderTargetFormat,
|
|
13
|
+
isUnsignedFloatInternalFormat,
|
|
14
|
+
} from './target-format.js';
|
|
15
|
+
export { createPipelineWarmup } from './warm-pipelines.js';
|
|
16
|
+
export { createDaylightSky, createSkyPanorama, createSkyTexture } from './daylight-sky.js';
|
|
17
|
+
export { bindKeyboardOrbit, createOrbitCamera } from './orbit-camera.js';
|
|
18
|
+
export { createDayCycle, luminanceOf } from './day-cycle.js';
|
|
19
|
+
export { DEFAULT_DAY_CYCLE, DEFAULT_SKY_PALETTE } from './day-cycle-defaults.js';
|
|
20
|
+
export { createSurfaceMaterialFactory } from './surface-material.js';
|
|
21
|
+
export { createClothMaterial } from './cloth-material.js';
|
|
22
|
+
export { createFacadeGlassMaterial, DEFAULT_ROOM_SHADE } from './facade-glass-material.js';
|
|
23
|
+
export { createStainMaterial } from './stain-material.js';
|
|
24
|
+
|
|
25
|
+
// ── Terrain & land baking ────────────────────────────────────────────────
|
|
26
|
+
export { createSimpleWater } from './water-simple.js';
|
|
27
|
+
export { createStrataCliffs } from './strata-cliffs.js';
|
|
28
|
+
export { meshTalusStones } from './strata-talus.js';
|
|
29
|
+
export { MOUND_RUINS_LOD, raiseMoundRuins, registerMoundRuins } from './ruins-mound.js';
|
|
30
|
+
export { LIGHT_POOL, addLampStand, addLightPool, addStillPool, addWallLantern, createLightPoolTexture } from './ruins-fittings.js';
|
|
31
|
+
export { GROUND_DRAPE_FRAGMENT, createGroundDrape, createGroundDrapeMaterial } from './ground-drape.js';
|
|
32
|
+
export { meshStreamBanks } from './stream-banks.js';
|
|
33
|
+
export { meshSprings } from './stream-springs.js';
|
|
34
|
+
export { layRoads } from './road-ribbons.js';
|
|
35
|
+
export { createAvenueHandover, farRoadPavingMaterial, layPavedRoad, layRoadAvenue } from './paved-road.js';
|
|
36
|
+
export { PAVED_GROUND_LIMITS, createPavedGroundMaterial } from './paved-ground.js';
|
|
37
|
+
export { TUFT_SHADER, createGrassTuft, createTuftMaterial } from './plant-tuft.js';
|
|
38
|
+
export { createTrailMarks, trailMarksGLSL } from './land-trail-surface.js';
|
|
39
|
+
export { createIrrigatedFieldSurface, IRRIGATED_FIELD_KIND, IRRIGATED_FIELD_SURFACE } from './irrigated-field-surface.js';
|
|
40
|
+
export { scatterInstances, scatterTreeGrove, scatterTrees, scatterTussocks } from './scatter-instances.js';
|
|
41
|
+
export { meshFarmCells, scatterFarmLots } from './farm-cells.js';
|
|
42
|
+
export { DEFAULT_LIVESTOCK_CASTS, createLivestockCache } from './farm-livestock.js';
|
|
43
|
+
export {
|
|
44
|
+
DEFAULT_OX_COATS,
|
|
45
|
+
DEFAULT_PLOUGHMAN_LOOK,
|
|
46
|
+
PLOUGHMAN_SEEN_DISTANCE,
|
|
47
|
+
PLOUGHMAN_STANCE,
|
|
48
|
+
buildPloughman,
|
|
49
|
+
createPloughTeam,
|
|
50
|
+
posePloughman,
|
|
51
|
+
} from './farm-plough.js';
|
|
52
|
+
|
|
53
|
+
// ── Settlements, houses, fortifications & interiors ──────────────────────
|
|
54
|
+
export { createHouseKitMaterials, createHouseKitMeshes } from './house-kit-meshes.js';
|
|
55
|
+
export { createQuarterStream } from './settlement-quarter.js';
|
|
56
|
+
export { createTowns } from './settlement-town.js';
|
|
57
|
+
export { createSettlementApproach } from './settlement-approach.js';
|
|
58
|
+
export { createSettlementSwitch } from './settlement-switch.js';
|
|
59
|
+
export { createGardenPlants } from './plant-garden.js';
|
|
60
|
+
export { CITY_GLOW_FALLOFF, createCityGlow, createStreetLampNight, createStreetLamps } from './street-lamps.js';
|
|
61
|
+
export { createLampLightMap } from './lamp-light-map.js';
|
|
62
|
+
export { createCityLamplight, createNearestLampMaps, lampMapTexture } from './lamp-maps.js';
|
|
63
|
+
export { createRoomMaterials } from './room-materials.js';
|
|
64
|
+
export { finishRoom } from './room-space.js';
|
|
65
|
+
|
|
66
|
+
// ── Vessels ──────────────────────────────────────────────────────────────
|
|
67
|
+
export { createShipMeshes } from './ship-meshes.js';
|
|
68
|
+
|
|
69
|
+
// ── Figures, animals & fliers ────────────────────────────────────────────
|
|
70
|
+
export { createFigureAssembler } from './figure-skeleton.js';
|
|
71
|
+
export { createShields } from './figure-shield.js';
|
|
72
|
+
export { createCastStage, createStandIn, uploadCastMeshes, warmFigureShaders } from './cast-stage.js';
|
|
73
|
+
export { createNameplateGroups } from './nameplate-groups.js';
|
|
74
|
+
export { createNameplates, renderNameplateCanvas, wrapSubtitle } from './nameplates.js';
|
|
75
|
+
export { rigHorse } from './animal-horse-figure.js';
|
|
76
|
+
export { buildCattle } from './animal-cattle-figure.js';
|
|
77
|
+
export { buildCamel } from './animal-camel-figure.js';
|
|
78
|
+
export { buildCanine } from './animal-canine-figure.js';
|
|
79
|
+
export { createFlierShapes, createSwarm, FLY_WINGS } from './flier-swarm.js';
|
|
80
|
+
export { CARPET_LEVELS, canineStrideSteps, createCanineStrideCast, createCarpetSwarm } from './flier-carpet.js';
|
|
81
|
+
export { createCrawlerPack, createGiantSpiderPack } from './crawler-pack.js';
|
|
82
|
+
export { updateNearGroups } from './detail-range.js';
|
|
83
|
+
export { createPenFlocks, packPenAnimals } from './pen-flocks.js';
|
|
84
|
+
export { createLongFlierCompany } from './flier-long-body-company.js';
|
|
85
|
+
export { createCorvidFigure } from './flier-corvid-figure.js';
|
|
86
|
+
|
|
87
|
+
// ── Crowds ───────────────────────────────────────────────────────────────
|
|
88
|
+
export { createCrowdVisibility } from './crowd-visibility.js';
|
|
89
|
+
export { createHerdRenderer, createHerdSkins } from './crowd-herd.js';
|
|
90
|
+
export {
|
|
91
|
+
DEFAULT_BEARERS,
|
|
92
|
+
DEFAULT_BY_ROLE,
|
|
93
|
+
DEFAULT_CROWD_HAIR,
|
|
94
|
+
DEFAULT_CROWD_LOOKS,
|
|
95
|
+
DEFAULT_CROWD_SKINS,
|
|
96
|
+
DEFAULT_SOLDIER_BEARER,
|
|
97
|
+
DEFAULT_UNIFORMS,
|
|
98
|
+
bakeCrowdWalks,
|
|
99
|
+
createCrowdBatches,
|
|
100
|
+
createLookPicker,
|
|
101
|
+
} from './crowd-batches.js';
|
|
102
|
+
export { createCrowdUpdater, registerCrowd } from './crowd-update.js';
|
|
103
|
+
export { createCrowdLifecycle } from './crowd-lifecycle.js';
|
|
104
|
+
export { ContactShadows, createFootShadows } from './contact-shadows.js';
|
|
105
|
+
export { createCrowdSystem } from './crowd-system.js';
|
|
106
|
+
export { TROOP_HORSE_COATS, TROOP_HORSE_PACES, TROOP_MOUNT_KINDS, createTroopMounts } from './troop-mounts.js';
|
|
107
|
+
export {
|
|
108
|
+
STANDARD_BELOW,
|
|
109
|
+
STANDARD_CARRIED,
|
|
110
|
+
STANDARD_CLEAR,
|
|
111
|
+
STANDARD_GRIP_UP,
|
|
112
|
+
STANDARD_LAID_FLAT,
|
|
113
|
+
createTroopStandards,
|
|
114
|
+
poleDroppedMatrix,
|
|
115
|
+
poleInFistMatrix,
|
|
116
|
+
} from './troop-standards.js';
|
|
117
|
+
export { createTroopVolleys } from './troop-volleys.js';
|
|
118
|
+
export { createTroopDirector } from './troop-director.js';
|
|
119
|
+
|
|
120
|
+
// ── Effects, audio, pacing & runtime ─────────────────────────────────────
|
|
121
|
+
export { DEFAULT_PARTICLE_LOOK, createParticleNoiseTexture, createParticleSystem } from './particles.js';
|
|
122
|
+
export { createFireLightWatch } from './fire-light-watch.js';
|
|
123
|
+
export { createEffectsFrame, syncEffectsUniforms } from './effects-frame.js';
|
|
124
|
+
export { createRippleRingMaterial, createWaterRises, fishGeometry } from './effects-water-rises.js';
|
|
125
|
+
export { STORM_WALL_COLORS, createStormWall, stormWallGeometry, stormWallNoise } from './storm-wall.js';
|
|
126
|
+
export { createPracticalFlame, createSceneLights } from './scene-lights.js';
|
|
127
|
+
export { createPerfBox } from './perf-box.js';
|
|
128
|
+
export { PERF_BOX_DEFAULTS, attachPerfBoxWorker, createPerfBoxRenderer } from './perf-box-worker.js';
|
|
129
|
+
export { flyProbeRoute, runSelfProbe } from './perf-probe.js';
|
|
130
|
+
|
|
131
|
+
// ── Scene playback, melee, projectiles, beams, props & markers ───────────
|
|
132
|
+
export { createBladeTrailPool } from './blade-trails.js';
|
|
133
|
+
export { createProjectilePool } from './scene-projectiles.js';
|
|
134
|
+
export { DEFAULT_BEAM_COLORS, DEFAULT_BEAM_STYLE, DEFAULT_BEAM_STYLES, createEnergyBeams } from './scene-beams.js';
|
|
135
|
+
export { DEFAULT_POSE_SHOTS, actorHeadTop, createSceneCoverage } from './scene-coverage.js';
|
|
136
|
+
export { createSceneShots, gallopTremorLevel, protectMasterHeadroom, trackOrigin } from './scene-shots.js';
|
|
137
|
+
export { createSceneProps } from './scene-props.js';
|
|
138
|
+
export { createSceneFlash } from './scene-flash.js';
|
|
139
|
+
export { createSceneMarkers, createShaftLabel, DEFAULT_MARKER_COLORS, projectWaypoint } from './scene-markers.js';
|
|
140
|
+
|
|
141
|
+
// ── Map kit ──────────────────────────────────────────────────────────────
|
|
142
|
+
export {
|
|
143
|
+
DEFAULT_MAP_FONT,
|
|
144
|
+
MAP_COLORS,
|
|
145
|
+
MAP_GLYPHS,
|
|
146
|
+
drawDune,
|
|
147
|
+
drawMapArch,
|
|
148
|
+
drawMapCompass,
|
|
149
|
+
drawMapDisc,
|
|
150
|
+
drawMapGlyph,
|
|
151
|
+
drawMapLabel,
|
|
152
|
+
drawMapNote,
|
|
153
|
+
drawMapPoly,
|
|
154
|
+
drawMapScaleBar,
|
|
155
|
+
drawPeak,
|
|
156
|
+
drawTree,
|
|
157
|
+
} from './map-glyphs.js';
|
|
158
|
+
export { createMapLabelSet, spacedCaps, turnAlongLine } from './map-labels.js';
|
|
159
|
+
export { paintMapPick, paintMapVisitor } from './map-signs.js';
|
|
160
|
+
export { createAtlasRelief, paintTerrainSheet } from './map-relief.js';
|
|
161
|
+
export { bindMapInput, fitMapCanvas } from './map-input.js';
|
|
162
|
+
|
|
163
|
+
// ── Controls, HUD & wayfinding ───────────────────────────────────────────
|
|
164
|
+
export {
|
|
165
|
+
DEFAULT_BACKDROP_RANGES,
|
|
166
|
+
DEFAULT_BACKDROP_SKY,
|
|
167
|
+
DEFAULT_BACKDROP_VEIL,
|
|
168
|
+
applyBackdropFinish,
|
|
169
|
+
createCanvasSprite,
|
|
170
|
+
createTitleBackdrop,
|
|
171
|
+
paintCumulusSprite,
|
|
172
|
+
paintMountainRange,
|
|
173
|
+
paintSkyGradient,
|
|
174
|
+
ridgeNoise,
|
|
175
|
+
} from './title-backdrop.js';
|
|
176
|
+
export { DEFAULT_PLACE_TAG_STYLES, DEFAULT_SMALL_TAG_STYLES, createPlaceNames } from './place-names.js';
|
|
177
|
+
export { DEFAULT_PORTRAIT_GROUND, convertPortraitPixels, createPortraitStudio } from './portrait-studio.js';
|
|
178
|
+
export { pickScreenActor, pickScreenTag } from './screen-pick.js';
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Troop director rendering binding: raises scene hosts into instanced crowd batches, mounts,
|
|
3
|
+
* herds, wagons, carried standards, and ballistic volley shafts, stepping formations and effects
|
|
4
|
+
* each frame with zero per-frame allocations.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { mulberry32 } from '../math/random.js';
|
|
10
|
+
import { clamp } from '../math/scalar.js';
|
|
11
|
+
import {
|
|
12
|
+
TROOP_ARMED_KINDS,
|
|
13
|
+
TROOP_DUST_DEFAULTS,
|
|
14
|
+
TROOP_RANGED_NAME,
|
|
15
|
+
TROOP_SADDLE,
|
|
16
|
+
TROOP_SEEN_FROM,
|
|
17
|
+
TROOP_TEAM,
|
|
18
|
+
TROOP_UNSEEN,
|
|
19
|
+
applyTroopCue,
|
|
20
|
+
musterTroop,
|
|
21
|
+
refreshTroopOpponents,
|
|
22
|
+
standTroop,
|
|
23
|
+
stepTroopMember,
|
|
24
|
+
} from '../motion/troop-director.js';
|
|
25
|
+
import { TROOP_VOLLEY_DEFAULTS } from '../motion/troop-volleys.js';
|
|
26
|
+
import { createTroopVolleys } from './troop-volleys.js';
|
|
27
|
+
|
|
28
|
+
const _tint = new THREE.Color();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates a troop director bound to a world's crowd batches, mounts, herds, wagons, standards,
|
|
32
|
+
* camera, clock, visibility checks, and effect/foley hooks.
|
|
33
|
+
*
|
|
34
|
+
* @param {object} config
|
|
35
|
+
* @param {number} [config.seed=1] World seed used to derive deterministic per-set troop and volley PRNGs.
|
|
36
|
+
* @param {{ position: { x: number, z: number } }} config.camera Active camera for sight, LOD and dust range checks.
|
|
37
|
+
* @param {() => number} config.getTime Returns elapsed simulation time in seconds.
|
|
38
|
+
* @param {() => boolean} [config.getShadowsEnabled] Returns true when directional sun shadows are enabled.
|
|
39
|
+
* @param {() => number} [config.getCrowdShare] Returns `[0, 1]` quality multiplier for visible mounted/beast counts.
|
|
40
|
+
* @param {() => number} [config.getFxShare] Returns `[0, 1]` particle-effect budget share.
|
|
41
|
+
* @param {(x: number, y: number, z: number, r: number, nearOnly?: boolean, casts?: boolean) => boolean} [config.inView] Frustum visibility predicate.
|
|
42
|
+
* @param {(x: number, y: number, z: number, r: number) => boolean} [config.shadowCasts] Shadow-frustum inclusion predicate.
|
|
43
|
+
* @param {(list: object[], tones: number[], cx: number, cz: number, near: number, kind: string, bodyId: string|undefined, spaceId: string) => object} config.addCrowd Registers a human troop into crowd batches.
|
|
44
|
+
* @param {(bodyId: string|undefined, troop: object) => [number, number]|null|undefined} [config.standardOf] Resolves `[fieldHex, edgeHex]` for a troop's standard.
|
|
45
|
+
* @param {(mesh: THREE.Object3D) => Promise<void>} [config.warmShaders] Async shader warmer for volley meshes.
|
|
46
|
+
* @param {() => THREE.Material} [config.volleyMaterial] Tinted material factory for volley shafts.
|
|
47
|
+
* @param {((group: THREE.Object3D, n: number, lances: number) => object)|null} [config.makeMounts] Horse and lance instancing builder (`createTroopMounts`).
|
|
48
|
+
* @param {((group: THREE.Object3D, n: number) => object)|null} [config.makeHerd] Cattle/ox instancing builder (`createTroopMounts`).
|
|
49
|
+
* @param {((group: THREE.Object3D, n: number, rng: () => number) => object)|null} [config.makeWagons] Wagon instancing builder (`createTroopMounts`).
|
|
50
|
+
* @param {((group: THREE.Object3D, troops: object[]) => object)|null} [config.makeStandards] Carried standard builder (`createTroopStandards`).
|
|
51
|
+
* @param {((x: number, y: number, z: number, puffs: number, size: number, speed: number) => void)|null} [config.onDust] Localized battle/fall dust callback.
|
|
52
|
+
* @param {((x: number, y: number, z: number, count: number) => void)|null} [config.onVolley] Volley launch foley callback.
|
|
53
|
+
* @param {((x: number, y: number, z: number, intensity: number) => void)|null} [config.onClash] Ongoing scrimmage battle-din foley callback.
|
|
54
|
+
* @param {Set<string>} [config.armedKinds] Crowd kinds defaulting to `'ready'` stance.
|
|
55
|
+
* @param {RegExp} [config.rangedPattern] Pattern identifying ranged troop names.
|
|
56
|
+
* @param {number} [config.seenFrom=900] Default sight distance in metres for a raised host.
|
|
57
|
+
* @param {number} [config.saddle=1.2] Mounted rider seat height in metres.
|
|
58
|
+
* @param {{ ahead: number, apart: number }} [config.team] Wagon ox-team offsets in metres.
|
|
59
|
+
* @param {number} [config.unseen=-1] Hidden `home` sentinel.
|
|
60
|
+
* @returns {{ raiseHost: Function, cue: Function, resetHost: Function, resetAll: Function, hostTroop: Function, getHost: Function }}
|
|
61
|
+
*/
|
|
62
|
+
export function createTroopDirector({
|
|
63
|
+
seed = 1,
|
|
64
|
+
camera,
|
|
65
|
+
getTime,
|
|
66
|
+
getShadowsEnabled = () => true,
|
|
67
|
+
getCrowdShare = () => 1,
|
|
68
|
+
getFxShare = () => 1,
|
|
69
|
+
inView = () => true,
|
|
70
|
+
shadowCasts = () => true,
|
|
71
|
+
addCrowd,
|
|
72
|
+
standardOf = (_bodyId, troop) => troop?.spec?.standard || null,
|
|
73
|
+
warmShaders = () => Promise.resolve(),
|
|
74
|
+
volleyMaterial = () => new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.9 }),
|
|
75
|
+
makeMounts = null,
|
|
76
|
+
makeHerd = null,
|
|
77
|
+
makeWagons = null,
|
|
78
|
+
makeStandards = null,
|
|
79
|
+
onDust = null,
|
|
80
|
+
onVolley = null,
|
|
81
|
+
onClash = null,
|
|
82
|
+
armedKinds = TROOP_ARMED_KINDS,
|
|
83
|
+
rangedPattern = TROOP_RANGED_NAME,
|
|
84
|
+
seenFrom = TROOP_SEEN_FROM,
|
|
85
|
+
saddle = TROOP_SADDLE,
|
|
86
|
+
team = TROOP_TEAM,
|
|
87
|
+
unseen = TROOP_UNSEEN,
|
|
88
|
+
}) {
|
|
89
|
+
const hosts = new Map();
|
|
90
|
+
const dustViewDist = TROOP_DUST_DEFAULTS.viewDist;
|
|
91
|
+
const dustViewDist2 = dustViewDist * dustViewDist;
|
|
92
|
+
const fallDustDist2 = TROOP_DUST_DEFAULTS.fallDist * TROOP_DUST_DEFAULTS.fallDist;
|
|
93
|
+
const volleyViewDist2 = TROOP_VOLLEY_DEFAULTS.viewDist * TROOP_VOLLEY_DEFAULTS.viewDist;
|
|
94
|
+
const clashFoleyInterval = TROOP_DUST_DEFAULTS.clashFoleyInterval;
|
|
95
|
+
const standOptions = { saddle, armedKinds, unseen };
|
|
96
|
+
const cueOptions = { armedKinds, unseen };
|
|
97
|
+
|
|
98
|
+
function raiseHost(sp, setId, entries, near = seenFrom) {
|
|
99
|
+
const rng = mulberry32(seed * 131 + setId.length * 17 + setId.charCodeAt(0)),
|
|
100
|
+
volleyRng = mulberry32(seed * 197 + setId.length * 31 + setId.charCodeAt(0)),
|
|
101
|
+
host = { troops: {}, opponentsDirty: true },
|
|
102
|
+
troops = [],
|
|
103
|
+
bodies = new Set();
|
|
104
|
+
let cx = 0,
|
|
105
|
+
cz = 0,
|
|
106
|
+
riders = 0,
|
|
107
|
+
lancers = 0,
|
|
108
|
+
cattle = 0,
|
|
109
|
+
wagons = 0;
|
|
110
|
+
|
|
111
|
+
for (let ei = 0; ei < entries.length; ei++) {
|
|
112
|
+
const entry = entries[ei];
|
|
113
|
+
const ox = entry.originX,
|
|
114
|
+
oz = entry.originZ;
|
|
115
|
+
cx = ox;
|
|
116
|
+
cz = oz;
|
|
117
|
+
for (const name in entry.troops) {
|
|
118
|
+
const spec = entry.troops[name],
|
|
119
|
+
face = spec.face || [spec.at[0], spec.at[1] + 1],
|
|
120
|
+
hasStandard = !spec.beast && !!standardOf(spec.body, { spec }),
|
|
121
|
+
mustered = musterTroop(spec, rng, { hasStandard }),
|
|
122
|
+
troop = {
|
|
123
|
+
name,
|
|
124
|
+
spec,
|
|
125
|
+
list: mustered.list,
|
|
126
|
+
wide: mustered.wide,
|
|
127
|
+
loose: mustered.loose,
|
|
128
|
+
halfWidth: mustered.halfWidth,
|
|
129
|
+
ranks: mustered.ranks,
|
|
130
|
+
files: mustered.files,
|
|
131
|
+
rankNorms: mustered.rankNorms,
|
|
132
|
+
edgeNorms: mustered.edgeNorms,
|
|
133
|
+
ranged: rangedPattern.test(name),
|
|
134
|
+
at: { x: ox + spec.at[0], z: oz + spec.at[1] },
|
|
135
|
+
yaw: Math.atan2(face[0] - spec.at[0], face[1] - spec.at[1]),
|
|
136
|
+
facing: 0,
|
|
137
|
+
cx: 0,
|
|
138
|
+
cz: 0,
|
|
139
|
+
busy: false,
|
|
140
|
+
fighting: false,
|
|
141
|
+
visible: !spec.hide,
|
|
142
|
+
engageDepth: 0,
|
|
143
|
+
volleyTarget: null,
|
|
144
|
+
nextVolleyAt: 0,
|
|
145
|
+
nextDustAt: 0,
|
|
146
|
+
dustCursor: 0,
|
|
147
|
+
phase: rng() * 6.28,
|
|
148
|
+
};
|
|
149
|
+
host.troops[name] = troop;
|
|
150
|
+
troops.push(troop);
|
|
151
|
+
if (!spec.beast && spec.body) bodies.add(spec.body);
|
|
152
|
+
if (spec.mounted || spec.beast === 'horse') riders += spec.n;
|
|
153
|
+
if (spec.lance) lancers += spec.n;
|
|
154
|
+
if (spec.beast === 'cattle') cattle += spec.n;
|
|
155
|
+
if (spec.beast === 'wagon') {
|
|
156
|
+
wagons += spec.n;
|
|
157
|
+
cattle += spec.n * 2;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (let i = 0; i < troops.length; i++) {
|
|
163
|
+
const troop = troops[i];
|
|
164
|
+
standTroop(troop, standOptions);
|
|
165
|
+
if (!troop.spec.beast) {
|
|
166
|
+
addCrowd(troop.list, troop.spec.tones, cx, cz, near, troop.spec.kind, troop.spec.body, sp.id);
|
|
167
|
+
} else {
|
|
168
|
+
for (let j = 0; j < troop.list.length; j++) {
|
|
169
|
+
troop.list[j].c1 = _tint.setHex(troop.spec.tones[troop.list[j].coat % troop.spec.tones.length]).clone();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const group = new THREE.Group();
|
|
175
|
+
sp.group.add(group);
|
|
176
|
+
const mounts = riders && makeMounts ? makeMounts(group, riders, lancers) : null,
|
|
177
|
+
herd = cattle && makeHerd ? makeHerd(group, cattle) : null,
|
|
178
|
+
train = wagons && makeWagons ? makeWagons(group, wagons, rng) : null,
|
|
179
|
+
standards = makeStandards ? makeStandards(group, troops) : null,
|
|
180
|
+
volleys =
|
|
181
|
+
bodies.size >= 2
|
|
182
|
+
? createTroopVolleys({
|
|
183
|
+
group,
|
|
184
|
+
material: volleyMaterial,
|
|
185
|
+
warmShaders,
|
|
186
|
+
inView,
|
|
187
|
+
camera,
|
|
188
|
+
getFxShare,
|
|
189
|
+
onDust,
|
|
190
|
+
onVolley,
|
|
191
|
+
saddle,
|
|
192
|
+
unseen,
|
|
193
|
+
})
|
|
194
|
+
: null,
|
|
195
|
+
// Hold mounted riders unseen until their horses' shaders compile so nobody floats in midair.
|
|
196
|
+
heldBack = mounts ? troops.flatMap(troop => (troop.spec.mounted ? troop.list.filter(g => g.home === undefined) : [])) : [];
|
|
197
|
+
for (let i = 0; i < heldBack.length; i++) heldBack[i].home = unseen;
|
|
198
|
+
|
|
199
|
+
hosts.set(setId, host);
|
|
200
|
+
host.reset = () => {
|
|
201
|
+
for (let i = 0; i < troops.length; i++) standTroop(troops[i], standOptions);
|
|
202
|
+
volleys?.reset();
|
|
203
|
+
host.opponentsDirty = true;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
let wasSeen = false,
|
|
207
|
+
nextClashFoleyAt = 0,
|
|
208
|
+
frameNearCamera = false;
|
|
209
|
+
|
|
210
|
+
function handleFallLand(x, z) {
|
|
211
|
+
if (frameNearCamera && getFxShare() > 0 && onDust) {
|
|
212
|
+
const dx = x - camera.position.x,
|
|
213
|
+
dz = z - camera.position.z;
|
|
214
|
+
if (dx * dx + dz * dz < fallDustDist2) onDust(x, sp.ground(x, z) + 0.1, z, 2, 0.75, 0.7);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return dt => {
|
|
219
|
+
const t = getTime(),
|
|
220
|
+
camX = camera.position.x,
|
|
221
|
+
camZ = camera.position.z,
|
|
222
|
+
seen = Math.hypot(camX - cx, camZ - cz) < near;
|
|
223
|
+
if (!seen && !wasSeen) return;
|
|
224
|
+
wasSeen = seen;
|
|
225
|
+
if (heldBack.length && mounts.ready()) {
|
|
226
|
+
for (let i = 0; i < heldBack.length; i++)
|
|
227
|
+
if (heldBack[i].home === unseen) {
|
|
228
|
+
heldBack[i].home = undefined;
|
|
229
|
+
heldBack[i].out = -1;
|
|
230
|
+
}
|
|
231
|
+
heldBack.length = 0;
|
|
232
|
+
}
|
|
233
|
+
if (host.opponentsDirty) {
|
|
234
|
+
refreshTroopOpponents(troops);
|
|
235
|
+
host.opponentsDirty = false;
|
|
236
|
+
}
|
|
237
|
+
mounts?.begin();
|
|
238
|
+
herd?.begin();
|
|
239
|
+
train?.begin();
|
|
240
|
+
let clashX = 0,
|
|
241
|
+
clashZ = 0,
|
|
242
|
+
clashWeight = 0;
|
|
243
|
+
frameNearCamera = Math.hypot(camX - cx, camZ - cz) < dustViewDist + 140;
|
|
244
|
+
const shadowsOn = getShadowsEnabled(),
|
|
245
|
+
fxShare = getFxShare(),
|
|
246
|
+
crowdShare = getCrowdShare();
|
|
247
|
+
|
|
248
|
+
for (let ti = 0; ti < troops.length; ti++) {
|
|
249
|
+
const troop = troops[ti],
|
|
250
|
+
{ spec, list } = troop;
|
|
251
|
+
let speedSum = 0,
|
|
252
|
+
movingCount = 0;
|
|
253
|
+
if (troop.busy) {
|
|
254
|
+
let busy = false;
|
|
255
|
+
const engageDepth = troop.engageDepth;
|
|
256
|
+
for (let i = 0; i < list.length; i++) {
|
|
257
|
+
const g = list[i];
|
|
258
|
+
if (stepTroopMember(g, t, dt, engageDepth, handleFallLand)) busy = true;
|
|
259
|
+
if (g.speed > 0.5 && !g.fall) {
|
|
260
|
+
speedSum += g.speed;
|
|
261
|
+
movingCount++;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
troop.busy = busy;
|
|
265
|
+
if (troop.fighting) {
|
|
266
|
+
clashX += troop.cx * list.length;
|
|
267
|
+
clashZ += troop.cz * list.length;
|
|
268
|
+
clashWeight += list.length;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// Localized kicked-up battle dust under charging mounts, rushing infantry, and active scrimmage lines.
|
|
272
|
+
if (seen && fxShare > 0 && onDust && troop.visible && t >= troop.nextDustAt) {
|
|
273
|
+
const avgSpeed = movingCount > 0 ? speedSum / movingCount : 0;
|
|
274
|
+
const dusty = troop.fighting || (spec.mounted && avgSpeed > 2.4) || avgSpeed > 2.1;
|
|
275
|
+
if (dusty) {
|
|
276
|
+
troop.nextDustAt = t + (spec.mounted ? 0.14 : 0.18) + (ti % 3) * 0.03;
|
|
277
|
+
const span = troop.fighting ? Math.min(list.length, troop.wide * 2) : list.length;
|
|
278
|
+
if (span > 0) {
|
|
279
|
+
troop.dustCursor = (troop.dustCursor + 7) % span;
|
|
280
|
+
const g = list[troop.dustCursor];
|
|
281
|
+
if (g && !g.fall && g.home !== unseen) {
|
|
282
|
+
const dx = g.ax - camX,
|
|
283
|
+
dz = g.az - camZ;
|
|
284
|
+
if (dx * dx + dz * dz < dustViewDist2) {
|
|
285
|
+
const y = sp.ground(g.ax, g.az);
|
|
286
|
+
if (inView(g.ax, y + 1, g.az, 3, false, false)) {
|
|
287
|
+
const puffs = spec.mounted ? 4 : 3,
|
|
288
|
+
puffSize = spec.mounted ? 2.5 : troop.fighting ? 2.1 : 1.75,
|
|
289
|
+
puffSpeed = spec.mounted ? 1.85 : 1.35;
|
|
290
|
+
onDust(g.ax, y + 0.16, g.az, puffs, puffSize, puffSpeed);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
// Procedural arrow/javelin volleys arcing between opposing ranks or skirmishers.
|
|
298
|
+
if (seen && volleys && troop.volleyTarget && troop.visible && t >= troop.nextVolleyAt) {
|
|
299
|
+
const dx = troop.cx - camX,
|
|
300
|
+
dz = troop.cz - camZ;
|
|
301
|
+
if (dx * dx + dz * dz < volleyViewDist2) {
|
|
302
|
+
volleys.launch(troop, troop.volleyTarget, sp, t, volleyRng);
|
|
303
|
+
}
|
|
304
|
+
troop.nextVolleyAt = t + (troop.ranged ? 1.35 : 1.9) + volleyRng() * 1.1;
|
|
305
|
+
}
|
|
306
|
+
if (!seen || !(spec.mounted || spec.beast)) continue;
|
|
307
|
+
const count = Math.ceil(list.length * crowdShare);
|
|
308
|
+
for (let i = 0; i < count; i++) {
|
|
309
|
+
const g = list[i];
|
|
310
|
+
if (g.home === unseen) continue;
|
|
311
|
+
if (g.ax !== g.hgx || g.az !== g.hgz) {
|
|
312
|
+
g.hgx = g.ax;
|
|
313
|
+
g.hgz = g.az;
|
|
314
|
+
g.hy = sp.ground(g.ax, g.az);
|
|
315
|
+
}
|
|
316
|
+
const y = g.hy,
|
|
317
|
+
casts = shadowsOn && shadowCasts(g.ax, y + 1, g.az, 2.6);
|
|
318
|
+
if (!inView(g.ax, y + 1, g.az, 2.6, false, casts)) continue;
|
|
319
|
+
if (spec.beast === 'cattle') herd?.beast(g.ax, y, g.az, g.yaw, g.speed, g.travel, g.c1, g.s, casts);
|
|
320
|
+
else if (spec.beast === 'wagon') {
|
|
321
|
+
const fx = Math.sin(g.yaw),
|
|
322
|
+
fz = Math.cos(g.yaw);
|
|
323
|
+
train?.wagon(g.ax, y, g.az, g.yaw, g.travel, casts);
|
|
324
|
+
for (let side = -1; side <= 1; side += 2) {
|
|
325
|
+
const ox = g.ax + fx * team.ahead + fz * team.apart * side,
|
|
326
|
+
oz = g.az + fz * team.ahead - fx * team.apart * side;
|
|
327
|
+
herd?.beast(ox, sp.ground(ox, oz), oz, g.yaw, g.speed, g.travel + side * 0.3, g.c1, 1, casts);
|
|
328
|
+
}
|
|
329
|
+
} else if (mounts) {
|
|
330
|
+
mounts.horse(g.ax, y, g.az, g.yaw, g.speed, g.travel + g.ph, g.coat, g.s, g.fall || 0, casts);
|
|
331
|
+
if (spec.mounted) {
|
|
332
|
+
g.lift = saddle * (1 - 0.8 * Math.abs(g.fall || 0)) + mounts.seat.rise;
|
|
333
|
+
g.rock = mounts.seat.lean;
|
|
334
|
+
}
|
|
335
|
+
if (spec.lance && !g.fall) mounts.lance(g.ax, y + g.lift, g.az, g.yaw, g.speed, casts);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (seen && clashWeight > 0 && t >= nextClashFoleyAt) {
|
|
340
|
+
nextClashFoleyAt = t + clashFoleyInterval;
|
|
341
|
+
if (onClash) {
|
|
342
|
+
const mx = clashX / clashWeight,
|
|
343
|
+
mz = clashZ / clashWeight;
|
|
344
|
+
onClash(mx, sp.ground(mx, mz) + 1.4, mz, clamp(clashWeight / 160, 0.25, 1));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
mounts?.end();
|
|
348
|
+
herd?.end();
|
|
349
|
+
train?.end();
|
|
350
|
+
if (seen) {
|
|
351
|
+
standards?.draw(sp);
|
|
352
|
+
volleys?.draw(t, sp);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function cue(setId, step, anchor) {
|
|
358
|
+
const host = hosts.get(setId);
|
|
359
|
+
if (host) applyTroopCue(host, step, anchor, getTime(), cueOptions);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function resetHost(setId) {
|
|
363
|
+
hosts.get(setId)?.reset();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function resetAll() {
|
|
367
|
+
for (const host of hosts.values()) host.reset();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const hostTroop = (setId, name) => hosts.get(setId)?.troops[name]?.list;
|
|
371
|
+
const getHost = setId => hosts.get(setId) ?? null;
|
|
372
|
+
|
|
373
|
+
return { raiseHost, cue, resetHost, resetAll, hostTroop, getHost };
|
|
374
|
+
}
|