@jgengine/core 0.6.0 → 0.7.0

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.
Files changed (222) hide show
  1. package/CHANGELOG.md +167 -2
  2. package/dist/ai/crowd.d.ts +36 -0
  3. package/dist/ai/crowd.js +213 -0
  4. package/dist/ai/jobBoard.d.ts +49 -0
  5. package/dist/ai/jobBoard.js +155 -0
  6. package/dist/ai/spawnDirector.d.ts +56 -0
  7. package/dist/ai/spawnDirector.js +150 -0
  8. package/dist/ai/threat.d.ts +25 -0
  9. package/dist/ai/threat.js +67 -0
  10. package/dist/audio/audioFalloff.d.ts +38 -0
  11. package/dist/audio/audioFalloff.js +32 -0
  12. package/dist/board/laneBoard.d.ts +63 -0
  13. package/dist/board/laneBoard.js +114 -0
  14. package/dist/board/timelineBoard.d.ts +36 -0
  15. package/dist/board/timelineBoard.js +70 -0
  16. package/dist/cards/cardPile.d.ts +63 -0
  17. package/dist/cards/cardPile.js +166 -0
  18. package/dist/cards/modifierPipeline.d.ts +33 -0
  19. package/dist/cards/modifierPipeline.js +48 -0
  20. package/dist/combat/abilityKit.d.ts +42 -0
  21. package/dist/combat/abilityKit.js +129 -0
  22. package/dist/combat/animationState.d.ts +47 -0
  23. package/dist/combat/animationState.js +101 -0
  24. package/dist/combat/attackTags.d.ts +14 -0
  25. package/dist/combat/attackTags.js +26 -0
  26. package/dist/combat/breakMeters.d.ts +32 -0
  27. package/dist/combat/breakMeters.js +51 -0
  28. package/dist/combat/comboString.d.ts +35 -0
  29. package/dist/combat/comboString.js +59 -0
  30. package/dist/combat/death.d.ts +8 -0
  31. package/dist/combat/death.js +5 -2
  32. package/dist/combat/defensiveWindow.d.ts +34 -0
  33. package/dist/combat/defensiveWindow.js +66 -0
  34. package/dist/combat/downed.d.ts +35 -0
  35. package/dist/combat/downed.js +104 -0
  36. package/dist/combat/effects.d.ts +11 -0
  37. package/dist/combat/effects.js +22 -13
  38. package/dist/combat/hitReaction.d.ts +23 -0
  39. package/dist/combat/hitReaction.js +20 -0
  40. package/dist/combat/resistance.d.ts +13 -0
  41. package/dist/combat/resistance.js +46 -0
  42. package/dist/combat/telegraph.d.ts +27 -0
  43. package/dist/combat/telegraph.js +37 -0
  44. package/dist/crafting/crop.d.ts +59 -0
  45. package/dist/crafting/crop.js +181 -0
  46. package/dist/crafting/production.d.ts +64 -0
  47. package/dist/crafting/production.js +130 -0
  48. package/dist/crafting/recipe.d.ts +62 -0
  49. package/dist/crafting/recipe.js +101 -0
  50. package/dist/economy/sharedWallet.d.ts +28 -0
  51. package/dist/economy/sharedWallet.js +62 -0
  52. package/dist/economy/techTree.d.ts +40 -0
  53. package/dist/economy/techTree.js +66 -0
  54. package/dist/game/cosmetics.d.ts +25 -0
  55. package/dist/game/cosmetics.js +51 -0
  56. package/dist/game/defineGame.d.ts +2 -0
  57. package/dist/game/events.d.ts +62 -0
  58. package/dist/game/lootFilter.d.ts +33 -0
  59. package/dist/game/lootFilter.js +48 -0
  60. package/dist/game/ping.d.ts +67 -0
  61. package/dist/game/ping.js +86 -0
  62. package/dist/game/playableGame.d.ts +281 -4
  63. package/dist/game/race.d.ts +95 -0
  64. package/dist/game/race.js +216 -0
  65. package/dist/game/runDraft.d.ts +34 -0
  66. package/dist/game/runDraft.js +97 -0
  67. package/dist/game/social.d.ts +27 -0
  68. package/dist/game/social.js +16 -0
  69. package/dist/game/worldItem.d.ts +92 -0
  70. package/dist/game/worldItem.js +132 -0
  71. package/dist/input/axisInput.d.ts +43 -0
  72. package/dist/input/axisInput.js +70 -0
  73. package/dist/input/pointer.d.ts +24 -0
  74. package/dist/input/pointer.js +17 -0
  75. package/dist/interaction/contextMenu.d.ts +30 -0
  76. package/dist/interaction/contextMenu.js +22 -0
  77. package/dist/interaction/qte.d.ts +20 -0
  78. package/dist/interaction/qte.js +24 -0
  79. package/dist/interaction/skillCheck.d.ts +20 -0
  80. package/dist/interaction/skillCheck.js +26 -0
  81. package/dist/inventory/shapedGrid.d.ts +56 -0
  82. package/dist/inventory/shapedGrid.js +176 -0
  83. package/dist/inventory/storageTier.d.ts +52 -0
  84. package/dist/inventory/storageTier.js +77 -0
  85. package/dist/item/affix.d.ts +54 -0
  86. package/dist/item/affix.js +133 -0
  87. package/dist/item/durability.d.ts +51 -0
  88. package/dist/item/durability.js +78 -0
  89. package/dist/item/modularItem.d.ts +46 -0
  90. package/dist/item/modularItem.js +75 -0
  91. package/dist/meta/changelog.d.ts +1 -1
  92. package/dist/meta/changelog.js +35 -1
  93. package/dist/movement/dash.d.ts +37 -0
  94. package/dist/movement/dash.js +65 -0
  95. package/dist/multiplayer/combatSnapshot.d.ts +45 -0
  96. package/dist/multiplayer/combatSnapshot.js +89 -0
  97. package/dist/multiplayer/lagCompensation.d.ts +43 -0
  98. package/dist/multiplayer/lagCompensation.js +154 -0
  99. package/dist/multiplayer/matchmaking.d.ts +33 -0
  100. package/dist/multiplayer/matchmaking.js +60 -0
  101. package/dist/multiplayer/simultaneousCommit.d.ts +34 -0
  102. package/dist/multiplayer/simultaneousCommit.js +53 -0
  103. package/dist/nav/navGrid.d.ts +46 -0
  104. package/dist/nav/navGrid.js +248 -0
  105. package/dist/nav/pathFollow.d.ts +29 -0
  106. package/dist/nav/pathFollow.js +67 -0
  107. package/dist/physics/buoyancy.d.ts +49 -0
  108. package/dist/physics/buoyancy.js +112 -0
  109. package/dist/physics/carryable.d.ts +46 -0
  110. package/dist/physics/carryable.js +110 -0
  111. package/dist/physics/damageZones.d.ts +56 -0
  112. package/dist/physics/damageZones.js +107 -0
  113. package/dist/physics/forceVolume.d.ts +48 -0
  114. package/dist/physics/forceVolume.js +119 -0
  115. package/dist/physics/physicsWorld.d.ts +83 -0
  116. package/dist/physics/physicsWorld.js +261 -1
  117. package/dist/physics/ragdoll.d.ts +49 -0
  118. package/dist/physics/ragdoll.js +103 -0
  119. package/dist/physics/spatialGrid.d.ts +41 -0
  120. package/dist/physics/spatialGrid.js +138 -0
  121. package/dist/physics/structure.d.ts +83 -0
  122. package/dist/physics/structure.js +0 -0
  123. package/dist/physics/traversal.d.ts +75 -0
  124. package/dist/physics/traversal.js +138 -0
  125. package/dist/physics/vehicleBody.d.ts +93 -0
  126. package/dist/physics/vehicleBody.js +234 -0
  127. package/dist/runtime/gameContext.d.ts +64 -2
  128. package/dist/runtime/gameContext.js +154 -4
  129. package/dist/runtime/hostPersistence.d.ts +18 -0
  130. package/dist/runtime/hostPersistence.js +4 -0
  131. package/dist/runtime/persistenceScope.d.ts +32 -0
  132. package/dist/runtime/persistenceScope.js +64 -0
  133. package/dist/scene/autoTarget.d.ts +15 -0
  134. package/dist/scene/autoTarget.js +54 -0
  135. package/dist/scene/behaviors.d.ts +13 -1
  136. package/dist/scene/behaviors.js +3 -0
  137. package/dist/scene/captureCheck.d.ts +7 -0
  138. package/dist/scene/captureCheck.js +11 -0
  139. package/dist/scene/entityStore.d.ts +1 -1
  140. package/dist/scene/form.d.ts +40 -0
  141. package/dist/scene/form.js +56 -0
  142. package/dist/scene/mount.d.ts +62 -0
  143. package/dist/scene/mount.js +107 -0
  144. package/dist/scene/possession.d.ts +30 -0
  145. package/dist/scene/possession.js +51 -0
  146. package/dist/scene/roster.d.ts +25 -0
  147. package/dist/scene/roster.js +57 -0
  148. package/dist/scene/selection.d.ts +30 -0
  149. package/dist/scene/selection.js +38 -0
  150. package/dist/scene/stationClaim.d.ts +43 -0
  151. package/dist/scene/stationClaim.js +109 -0
  152. package/dist/sensor/frustumSensor.d.ts +60 -0
  153. package/dist/sensor/frustumSensor.js +104 -0
  154. package/dist/sensor/hiddenStateProbe.d.ts +31 -0
  155. package/dist/sensor/hiddenStateProbe.js +40 -0
  156. package/dist/sensor/recordingBuffer.d.ts +29 -0
  157. package/dist/sensor/recordingBuffer.js +61 -0
  158. package/dist/sensor/revealQuery.d.ts +22 -0
  159. package/dist/sensor/revealQuery.js +36 -0
  160. package/dist/session/contestedChannel.d.ts +36 -0
  161. package/dist/session/contestedChannel.js +100 -0
  162. package/dist/session/extraction.d.ts +54 -0
  163. package/dist/session/extraction.js +108 -0
  164. package/dist/session/ring.d.ts +36 -0
  165. package/dist/session/ring.js +88 -0
  166. package/dist/session/roundState.d.ts +55 -0
  167. package/dist/session/roundState.js +129 -0
  168. package/dist/stats/accumulatorMeter.d.ts +31 -0
  169. package/dist/stats/accumulatorMeter.js +81 -0
  170. package/dist/stats/eventMeter.d.ts +34 -0
  171. package/dist/stats/eventMeter.js +76 -0
  172. package/dist/stats/rollCheck.d.ts +15 -0
  173. package/dist/stats/rollCheck.js +15 -0
  174. package/dist/survival/decayMeter.d.ts +59 -0
  175. package/dist/survival/decayMeter.js +95 -0
  176. package/dist/survival/moodle.d.ts +48 -0
  177. package/dist/survival/moodle.js +91 -0
  178. package/dist/survival/regionHealth.d.ts +72 -0
  179. package/dist/survival/regionHealth.js +129 -0
  180. package/dist/tactics/predictiveQuery.d.ts +34 -0
  181. package/dist/tactics/predictiveQuery.js +44 -0
  182. package/dist/tactics/snapshot.d.ts +18 -0
  183. package/dist/tactics/snapshot.js +65 -0
  184. package/dist/tactics/surface.d.ts +52 -0
  185. package/dist/tactics/surface.js +135 -0
  186. package/dist/tactics/tacticalGrid.d.ts +57 -0
  187. package/dist/tactics/tacticalGrid.js +244 -0
  188. package/dist/time/beatClock.d.ts +40 -0
  189. package/dist/time/beatClock.js +73 -0
  190. package/dist/turn/commit.d.ts +27 -0
  191. package/dist/turn/commit.js +72 -0
  192. package/dist/turn/turnLoop.d.ts +58 -0
  193. package/dist/turn/turnLoop.js +216 -0
  194. package/dist/world/buildPermissions.d.ts +47 -0
  195. package/dist/world/buildPermissions.js +134 -0
  196. package/dist/world/carve.d.ts +105 -0
  197. package/dist/world/carve.js +175 -0
  198. package/dist/world/connectors.d.ts +40 -0
  199. package/dist/world/connectors.js +67 -0
  200. package/dist/world/envField.d.ts +71 -0
  201. package/dist/world/envField.js +96 -0
  202. package/dist/world/fog.d.ts +40 -0
  203. package/dist/world/fog.js +137 -0
  204. package/dist/world/markers.d.ts +52 -0
  205. package/dist/world/markers.js +96 -0
  206. package/dist/world/minimap.d.ts +42 -0
  207. package/dist/world/minimap.js +72 -0
  208. package/dist/world/placedStructureStore.d.ts +39 -0
  209. package/dist/world/placedStructureStore.js +95 -0
  210. package/dist/world/placementController.d.ts +48 -0
  211. package/dist/world/placementController.js +97 -0
  212. package/dist/world/realm.d.ts +61 -0
  213. package/dist/world/realm.js +66 -0
  214. package/dist/world/support.d.ts +27 -0
  215. package/dist/world/support.js +75 -0
  216. package/dist/world/terraform.d.ts +54 -0
  217. package/dist/world/terraform.js +186 -0
  218. package/dist/world/walls.d.ts +61 -0
  219. package/dist/world/walls.js +221 -0
  220. package/dist/world/weather.d.ts +92 -0
  221. package/dist/world/weather.js +169 -0
  222. package/package.json +1 -1
@@ -0,0 +1,75 @@
1
+ export function solveSupport(pieces, links, config = {}) {
2
+ const maxDistance = config.maxDistance ?? Infinity;
3
+ const ids = new Set(pieces.map((piece) => piece.id));
4
+ const adjacency = new Map();
5
+ for (const id of ids)
6
+ adjacency.set(id, []);
7
+ for (const link of links) {
8
+ if (!ids.has(link.a) || !ids.has(link.b))
9
+ continue;
10
+ adjacency.get(link.a).push(link.b);
11
+ adjacency.get(link.b).push(link.a);
12
+ }
13
+ const distance = {};
14
+ const queue = [];
15
+ for (const piece of pieces) {
16
+ if (piece.grounded === true) {
17
+ distance[piece.id] = 0;
18
+ queue.push(piece.id);
19
+ }
20
+ }
21
+ let head = 0;
22
+ while (head < queue.length) {
23
+ const current = queue[head];
24
+ head += 1;
25
+ const currentDistance = distance[current];
26
+ if (currentDistance >= maxDistance)
27
+ continue;
28
+ for (const neighbor of adjacency.get(current) ?? []) {
29
+ const next = currentDistance + 1;
30
+ if (distance[neighbor] === undefined || next < distance[neighbor]) {
31
+ distance[neighbor] = next;
32
+ queue.push(neighbor);
33
+ }
34
+ }
35
+ }
36
+ const supported = [];
37
+ const unsupported = [];
38
+ for (const piece of pieces) {
39
+ const d = distance[piece.id];
40
+ if (d !== undefined && d <= maxDistance)
41
+ supported.push(piece.id);
42
+ else
43
+ unsupported.push(piece.id);
44
+ }
45
+ return { supported, unsupported, distance };
46
+ }
47
+ function scatterHash(seed, index) {
48
+ let h = (seed ^ Math.imul(index + 1, 0x9e3779b1)) >>> 0;
49
+ h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
50
+ h ^= h >>> 13;
51
+ return ((h >>> 0) / 4294967295) * 2 - 1;
52
+ }
53
+ export function toDebrisBodies(pieces, collapsedIds, options = {}) {
54
+ const scatter = options.scatter ?? 1.5;
55
+ const seed = options.seed ?? 1;
56
+ const collapse = new Set(collapsedIds);
57
+ const bodies = [];
58
+ let index = 0;
59
+ for (const piece of pieces) {
60
+ if (!collapse.has(piece.id))
61
+ continue;
62
+ const position = piece.position ?? [0, 0, 0];
63
+ const halfExtents = piece.halfExtents ?? [0.5, 0.5, 0.5];
64
+ const vx = scatterHash(seed, index) * scatter;
65
+ const vz = scatterHash(seed, index + 977) * scatter;
66
+ bodies.push({
67
+ position,
68
+ halfExtents,
69
+ velocity: [vx, 0, vz],
70
+ ...(piece.mass === undefined ? {} : { mass: piece.mass }),
71
+ });
72
+ index += 1;
73
+ }
74
+ return bodies;
75
+ }
@@ -0,0 +1,54 @@
1
+ import type { Aabb, Vec2 } from "./geometry.js";
2
+ import { type TerrainField } from "./terrain.js";
3
+ export type TerraformMode = "raise" | "lower" | "flatten" | "paint";
4
+ export type TerraformFalloff = "smooth" | "linear" | "none";
5
+ export interface TerraformEdit {
6
+ mode: TerraformMode;
7
+ center: Vec2;
8
+ radius: number;
9
+ strength?: number;
10
+ target?: number;
11
+ surface?: string;
12
+ falloff?: TerraformFalloff;
13
+ }
14
+ export interface EditableTerrainConfig {
15
+ bounds: Aabb;
16
+ base?: TerrainField;
17
+ cellSize?: number;
18
+ }
19
+ export interface TerraformSnapshot {
20
+ bounds: Aabb;
21
+ cellSize: number;
22
+ cols: number;
23
+ rows: number;
24
+ offsets: readonly number[];
25
+ surfaces: readonly (string | null)[];
26
+ }
27
+ export interface EditableTerrain extends TerrainField {
28
+ apply(edit: TerraformEdit): number;
29
+ heightOffsetAt(x: number, z: number): number;
30
+ surfaceAt(x: number, z: number): string | null;
31
+ reset(): void;
32
+ snapshot(): TerraformSnapshot;
33
+ restore(snapshot: TerraformSnapshot): void;
34
+ }
35
+ export declare function brushWeight(distance: number, radius: number, falloff: TerraformFalloff): number;
36
+ export declare function createEditableTerrain(config: EditableTerrainConfig): EditableTerrain;
37
+ export interface TerraformBrushConfig {
38
+ radius?: number;
39
+ strength?: number;
40
+ falloff?: TerraformFalloff;
41
+ surface?: string;
42
+ }
43
+ export interface TerraformBrush {
44
+ raise(center: Vec2): number;
45
+ lower(center: Vec2): number;
46
+ flatten(center: Vec2, target?: number): number;
47
+ paint(center: Vec2, surface?: string): number;
48
+ setRadius(radius: number): void;
49
+ setStrength(strength: number): void;
50
+ config(): Required<Omit<TerraformBrushConfig, "surface">> & {
51
+ surface: string | undefined;
52
+ };
53
+ }
54
+ export declare function createTerraformBrush(terrain: Pick<EditableTerrain, "apply">, config?: TerraformBrushConfig): TerraformBrush;
@@ -0,0 +1,186 @@
1
+ import { FLAT_FIELD, withNormal } from "./terrain.js";
2
+ export function brushWeight(distance, radius, falloff) {
3
+ if (radius <= 0)
4
+ return distance <= 0 ? 1 : 0;
5
+ const t = distance / radius;
6
+ if (t >= 1)
7
+ return 0;
8
+ if (falloff === "none")
9
+ return 1;
10
+ if (falloff === "linear")
11
+ return 1 - t;
12
+ const s = 1 - t;
13
+ return s * s * (3 - 2 * s);
14
+ }
15
+ function clampIndex(value, max) {
16
+ return value < 0 ? 0 : value > max ? max : value;
17
+ }
18
+ export function createEditableTerrain(config) {
19
+ const base = config.base ?? FLAT_FIELD;
20
+ const bounds = config.bounds;
21
+ const cellSize = config.cellSize && config.cellSize > 0 ? config.cellSize : 1;
22
+ const spanX = Math.max(bounds.maxX - bounds.minX, cellSize);
23
+ const spanZ = Math.max(bounds.maxZ - bounds.minZ, cellSize);
24
+ const cols = Math.max(1, Math.round(spanX / cellSize));
25
+ const rows = Math.max(1, Math.round(spanZ / cellSize));
26
+ const vertsX = cols + 1;
27
+ const vertsZ = rows + 1;
28
+ const offsets = new Float32Array(vertsX * vertsZ);
29
+ const surfaces = new Array(cols * rows).fill(null);
30
+ function gridX(x) {
31
+ return ((x - bounds.minX) / spanX) * cols;
32
+ }
33
+ function gridZ(z) {
34
+ return ((z - bounds.minZ) / spanZ) * rows;
35
+ }
36
+ function heightOffsetAt(x, z) {
37
+ const fx = gridX(x);
38
+ const fz = gridZ(z);
39
+ const x0 = clampIndex(Math.floor(fx), cols);
40
+ const z0 = clampIndex(Math.floor(fz), rows);
41
+ const x1 = clampIndex(x0 + 1, cols);
42
+ const z1 = clampIndex(z0 + 1, rows);
43
+ const tx = fx - x0 < 0 ? 0 : fx - x0 > 1 ? 1 : fx - x0;
44
+ const tz = fz - z0 < 0 ? 0 : fz - z0 > 1 ? 1 : fz - z0;
45
+ const v00 = offsets[z0 * vertsX + x0];
46
+ const v10 = offsets[z0 * vertsX + x1];
47
+ const v01 = offsets[z1 * vertsX + x0];
48
+ const v11 = offsets[z1 * vertsX + x1];
49
+ const top = v00 + (v10 - v00) * tx;
50
+ const bottom = v01 + (v11 - v01) * tx;
51
+ return top + (bottom - top) * tz;
52
+ }
53
+ const sampleHeight = (x, z) => base.sampleHeight(x, z) + heightOffsetAt(x, z);
54
+ const sampleNormal = withNormal(sampleHeight);
55
+ function editHeight(edit) {
56
+ const falloff = edit.falloff ?? "smooth";
57
+ const strength = edit.strength ?? 1;
58
+ const target = edit.mode === "flatten" ? edit.target ?? sampleHeight(edit.center[0], edit.center[1]) : 0;
59
+ const rCells = Math.ceil(edit.radius / cellSize) + 1;
60
+ const cx = gridX(edit.center[0]);
61
+ const cz = gridZ(edit.center[1]);
62
+ let changed = 0;
63
+ for (let iz = -rCells; iz <= rCells; iz += 1) {
64
+ const gz = Math.round(cz) + iz;
65
+ if (gz < 0 || gz >= vertsZ)
66
+ continue;
67
+ for (let ix = -rCells; ix <= rCells; ix += 1) {
68
+ const gx = Math.round(cx) + ix;
69
+ if (gx < 0 || gx >= vertsX)
70
+ continue;
71
+ const wx = bounds.minX + (gx / cols) * spanX;
72
+ const wz = bounds.minZ + (gz / rows) * spanZ;
73
+ const dist = Math.hypot(wx - edit.center[0], wz - edit.center[1]);
74
+ const weight = brushWeight(dist, edit.radius, falloff);
75
+ if (weight <= 0)
76
+ continue;
77
+ const index = gz * vertsX + gx;
78
+ if (edit.mode === "raise")
79
+ offsets[index] += strength * weight;
80
+ else if (edit.mode === "lower")
81
+ offsets[index] -= strength * weight;
82
+ else {
83
+ const current = base.sampleHeight(wx, wz) + offsets[index];
84
+ offsets[index] += (target - current) * weight * strength;
85
+ }
86
+ changed += 1;
87
+ }
88
+ }
89
+ return changed;
90
+ }
91
+ function paintSurface(edit) {
92
+ const surface = edit.surface ?? null;
93
+ const rCells = Math.ceil(edit.radius / cellSize) + 1;
94
+ const cx = gridX(edit.center[0]);
95
+ const cz = gridZ(edit.center[1]);
96
+ let painted = 0;
97
+ for (let iz = -rCells; iz <= rCells; iz += 1) {
98
+ const gz = Math.floor(cz) + iz;
99
+ if (gz < 0 || gz >= rows)
100
+ continue;
101
+ for (let ix = -rCells; ix <= rCells; ix += 1) {
102
+ const gx = Math.floor(cx) + ix;
103
+ if (gx < 0 || gx >= cols)
104
+ continue;
105
+ const wx = bounds.minX + ((gx + 0.5) / cols) * spanX;
106
+ const wz = bounds.minZ + ((gz + 0.5) / rows) * spanZ;
107
+ if (Math.hypot(wx - edit.center[0], wz - edit.center[1]) > edit.radius)
108
+ continue;
109
+ surfaces[gz * cols + gx] = surface;
110
+ painted += 1;
111
+ }
112
+ }
113
+ return painted;
114
+ }
115
+ function surfaceAt(x, z) {
116
+ const gx = Math.floor(gridX(x));
117
+ const gz = Math.floor(gridZ(z));
118
+ if (gx < 0 || gx >= cols || gz < 0 || gz >= rows)
119
+ return null;
120
+ return surfaces[gz * cols + gx] ?? null;
121
+ }
122
+ const field = {
123
+ sampleHeight,
124
+ sampleNormal,
125
+ ...(base.bounds === undefined ? {} : { bounds: base.bounds }),
126
+ ...(base.waterLevel === undefined ? {} : { waterLevel: base.waterLevel }),
127
+ apply(edit) {
128
+ return edit.mode === "paint" ? paintSurface(edit) : editHeight(edit);
129
+ },
130
+ heightOffsetAt,
131
+ surfaceAt,
132
+ reset() {
133
+ offsets.fill(0);
134
+ surfaces.fill(null);
135
+ },
136
+ snapshot() {
137
+ return {
138
+ bounds: { ...bounds },
139
+ cellSize,
140
+ cols,
141
+ rows,
142
+ offsets: Array.from(offsets),
143
+ surfaces: surfaces.slice(),
144
+ };
145
+ },
146
+ restore(snapshot) {
147
+ const count = Math.min(offsets.length, snapshot.offsets.length);
148
+ for (let index = 0; index < count; index += 1)
149
+ offsets[index] = snapshot.offsets[index];
150
+ const surfaceCount = Math.min(surfaces.length, snapshot.surfaces.length);
151
+ for (let index = 0; index < surfaceCount; index += 1)
152
+ surfaces[index] = snapshot.surfaces[index] ?? null;
153
+ },
154
+ };
155
+ return field;
156
+ }
157
+ export function createTerraformBrush(terrain, config = {}) {
158
+ let radius = config.radius ?? 3;
159
+ let strength = config.strength ?? 0.5;
160
+ const falloff = config.falloff ?? "smooth";
161
+ const surface = config.surface;
162
+ return {
163
+ raise(center) {
164
+ return terrain.apply({ mode: "raise", center, radius, strength, falloff });
165
+ },
166
+ lower(center) {
167
+ return terrain.apply({ mode: "lower", center, radius, strength, falloff });
168
+ },
169
+ flatten(center, target) {
170
+ return terrain.apply({ mode: "flatten", center, radius, strength, falloff, ...(target === undefined ? {} : { target }) });
171
+ },
172
+ paint(center, paintSurface) {
173
+ const chosen = paintSurface ?? surface;
174
+ return terrain.apply({ mode: "paint", center, radius, ...(chosen === undefined ? {} : { surface: chosen }) });
175
+ },
176
+ setRadius(next) {
177
+ radius = next;
178
+ },
179
+ setStrength(next) {
180
+ strength = next;
181
+ },
182
+ config() {
183
+ return { radius, strength, falloff, surface };
184
+ },
185
+ };
186
+ }
@@ -0,0 +1,61 @@
1
+ import { type Aabb, type Vec2 } from "./geometry.js";
2
+ export type WallVec3 = readonly [number, number, number];
3
+ export interface WallSegment {
4
+ from: Vec2;
5
+ to: Vec2;
6
+ length: number;
7
+ angle: number;
8
+ }
9
+ export interface EnclosedFootprint {
10
+ polygon: readonly Vec2[];
11
+ aabb: Aabb;
12
+ area: number;
13
+ perimeter: number;
14
+ closed: boolean;
15
+ }
16
+ export type RoofStyle = "hip" | "gable" | "flat";
17
+ export interface RoofPlan {
18
+ style: RoofStyle;
19
+ ridge: readonly [Vec2, Vec2];
20
+ apexHeight: number;
21
+ eaveHeight: number;
22
+ faces: readonly (readonly WallVec3[])[];
23
+ }
24
+ export interface RoofConfig {
25
+ style?: RoofStyle;
26
+ eaveHeight?: number;
27
+ pitch?: number;
28
+ overhang?: number;
29
+ }
30
+ export declare function wallSegments(points: readonly Vec2[], closed: boolean): WallSegment[];
31
+ export declare function isEnclosed(points: readonly Vec2[], tolerance?: number): boolean;
32
+ export declare function enclosePath(points: readonly Vec2[], tolerance?: number): readonly Vec2[];
33
+ export declare function polygonArea(polygon: readonly Vec2[]): number;
34
+ export declare function footprintFromWalls(points: readonly Vec2[], tolerance?: number): EnclosedFootprint | null;
35
+ export declare function autoRoof(footprint: EnclosedFootprint, config?: RoofConfig): RoofPlan;
36
+ export type PaintTarget = "floor" | "wall";
37
+ export interface SurfacePaintStore {
38
+ paint(target: PaintTarget, key: string, surface: string): void;
39
+ clear(target: PaintTarget, key: string): void;
40
+ get(target: PaintTarget, key: string): string | null;
41
+ entries(target: PaintTarget): readonly (readonly [string, string])[];
42
+ snapshot(): Record<PaintTarget, Record<string, string>>;
43
+ restore(snapshot: Record<PaintTarget, Record<string, string>>): void;
44
+ }
45
+ export declare function createSurfacePaint(): SurfacePaintStore;
46
+ export interface WallDrawTool {
47
+ addPoint(point: Vec2, snap?: number): Vec2;
48
+ undo(): void;
49
+ close(): void;
50
+ clear(): void;
51
+ points(): readonly Vec2[];
52
+ segments(): WallSegment[];
53
+ isClosed(): boolean;
54
+ footprint(): EnclosedFootprint | null;
55
+ roof(config?: RoofConfig): RoofPlan | null;
56
+ }
57
+ export declare function createWallDrawTool(config?: {
58
+ snap?: number;
59
+ closeTolerance?: number;
60
+ }): WallDrawTool;
61
+ export declare function wallSegmentBounds(segment: WallSegment, thickness: number): Aabb;
@@ -0,0 +1,221 @@
1
+ import { footprintAabb } from "./geometry.js";
2
+ export function wallSegments(points, closed) {
3
+ const segments = [];
4
+ const count = closed ? points.length : points.length - 1;
5
+ for (let index = 0; index < count; index += 1) {
6
+ const from = points[index];
7
+ const to = points[(index + 1) % points.length];
8
+ const dx = to[0] - from[0];
9
+ const dz = to[1] - from[1];
10
+ segments.push({ from, to, length: Math.hypot(dx, dz), angle: Math.atan2(dz, dx) });
11
+ }
12
+ return segments;
13
+ }
14
+ export function isEnclosed(points, tolerance = 0.5) {
15
+ if (points.length < 3)
16
+ return false;
17
+ const first = points[0];
18
+ const last = points[points.length - 1];
19
+ return Math.hypot(first[0] - last[0], first[1] - last[1]) <= tolerance;
20
+ }
21
+ export function enclosePath(points, tolerance = 0.5) {
22
+ if (points.length < 3)
23
+ return points;
24
+ if (!isEnclosed(points, tolerance))
25
+ return points;
26
+ return points.slice(0, points.length - 1);
27
+ }
28
+ export function polygonArea(polygon) {
29
+ let sum = 0;
30
+ for (let index = 0; index < polygon.length; index += 1) {
31
+ const a = polygon[index];
32
+ const b = polygon[(index + 1) % polygon.length];
33
+ sum += a[0] * b[1] - b[0] * a[1];
34
+ }
35
+ return Math.abs(sum) / 2;
36
+ }
37
+ export function footprintFromWalls(points, tolerance = 0.5) {
38
+ const closed = isEnclosed(points, tolerance);
39
+ const polygon = enclosePath(points, tolerance);
40
+ if (polygon.length < 3)
41
+ return null;
42
+ let minX = Infinity;
43
+ let minZ = Infinity;
44
+ let maxX = -Infinity;
45
+ let maxZ = -Infinity;
46
+ let perimeter = 0;
47
+ for (let index = 0; index < polygon.length; index += 1) {
48
+ const point = polygon[index];
49
+ if (point[0] < minX)
50
+ minX = point[0];
51
+ if (point[0] > maxX)
52
+ maxX = point[0];
53
+ if (point[1] < minZ)
54
+ minZ = point[1];
55
+ if (point[1] > maxZ)
56
+ maxZ = point[1];
57
+ const next = polygon[(index + 1) % polygon.length];
58
+ perimeter += Math.hypot(next[0] - point[0], next[1] - point[1]);
59
+ }
60
+ return {
61
+ polygon,
62
+ aabb: { minX, minZ, maxX, maxZ },
63
+ area: polygonArea(polygon),
64
+ perimeter,
65
+ closed,
66
+ };
67
+ }
68
+ export function autoRoof(footprint, config = {}) {
69
+ const style = config.style ?? "hip";
70
+ const eaveHeight = config.eaveHeight ?? 3;
71
+ const pitch = config.pitch ?? 0.5;
72
+ const overhang = config.overhang ?? 0.4;
73
+ const aabb = footprint.aabb;
74
+ const minX = aabb.minX - overhang;
75
+ const maxX = aabb.maxX + overhang;
76
+ const minZ = aabb.minZ - overhang;
77
+ const maxZ = aabb.maxZ + overhang;
78
+ const width = maxX - minX;
79
+ const depth = maxZ - minZ;
80
+ const cx = (minX + maxX) / 2;
81
+ const cz = (minZ + maxZ) / 2;
82
+ const ridgeAlongX = width >= depth;
83
+ const halfSpan = (ridgeAlongX ? depth : width) / 2;
84
+ const apexHeight = style === "flat" ? eaveHeight : eaveHeight + halfSpan * pitch;
85
+ if (style === "flat") {
86
+ const face = [
87
+ [minX, eaveHeight, minZ],
88
+ [maxX, eaveHeight, minZ],
89
+ [maxX, eaveHeight, maxZ],
90
+ [minX, eaveHeight, maxZ],
91
+ ];
92
+ const flatRidge = [
93
+ [minX, cz],
94
+ [maxX, cz],
95
+ ];
96
+ return { style, ridge: flatRidge, apexHeight, eaveHeight, faces: [face] };
97
+ }
98
+ const ridge = ridgeAlongX
99
+ ? [
100
+ [minX + (style === "hip" ? halfSpan : 0), cz],
101
+ [maxX - (style === "hip" ? halfSpan : 0), cz],
102
+ ]
103
+ : [
104
+ [cx, minZ + (style === "hip" ? halfSpan : 0)],
105
+ [cx, maxZ - (style === "hip" ? halfSpan : 0)],
106
+ ];
107
+ const r0 = [ridge[0][0], apexHeight, ridge[0][1]];
108
+ const r1 = [ridge[1][0], apexHeight, ridge[1][1]];
109
+ const corners = {
110
+ nw: [minX, eaveHeight, minZ],
111
+ ne: [maxX, eaveHeight, minZ],
112
+ se: [maxX, eaveHeight, maxZ],
113
+ sw: [minX, eaveHeight, maxZ],
114
+ };
115
+ const faces = ridgeAlongX
116
+ ? [
117
+ [corners.nw, corners.ne, r1, r0],
118
+ [corners.se, corners.sw, r0, r1],
119
+ ]
120
+ : [
121
+ [corners.ne, corners.se, r1, r0],
122
+ [corners.sw, corners.nw, r0, r1],
123
+ ];
124
+ if (style === "hip") {
125
+ faces.push(ridgeAlongX
126
+ ? [corners.sw, corners.nw, r0]
127
+ : [corners.nw, corners.ne, r0]);
128
+ faces.push(ridgeAlongX
129
+ ? [corners.ne, corners.se, r1]
130
+ : [corners.se, corners.sw, r1]);
131
+ }
132
+ return { style, ridge, apexHeight, eaveHeight, faces };
133
+ }
134
+ export function createSurfacePaint() {
135
+ const maps = {
136
+ floor: new Map(),
137
+ wall: new Map(),
138
+ };
139
+ return {
140
+ paint(target, key, surface) {
141
+ maps[target].set(key, surface);
142
+ },
143
+ clear(target, key) {
144
+ maps[target].delete(key);
145
+ },
146
+ get(target, key) {
147
+ return maps[target].get(key) ?? null;
148
+ },
149
+ entries(target) {
150
+ return Array.from(maps[target].entries());
151
+ },
152
+ snapshot() {
153
+ return {
154
+ floor: Object.fromEntries(maps.floor),
155
+ wall: Object.fromEntries(maps.wall),
156
+ };
157
+ },
158
+ restore(snapshot) {
159
+ maps.floor = new Map(Object.entries(snapshot.floor ?? {}));
160
+ maps.wall = new Map(Object.entries(snapshot.wall ?? {}));
161
+ },
162
+ };
163
+ }
164
+ function snapPoint(point, grid) {
165
+ if (!(grid > 0))
166
+ return point;
167
+ return [Math.round(point[0] / grid) * grid, Math.round(point[1] / grid) * grid];
168
+ }
169
+ export function createWallDrawTool(config = {}) {
170
+ const defaultSnap = config.snap ?? 0;
171
+ const closeTolerance = config.closeTolerance ?? 0.5;
172
+ let pts = [];
173
+ let closed = false;
174
+ return {
175
+ addPoint(point, snap) {
176
+ const grid = snap ?? defaultSnap;
177
+ const next = snapPoint(point, grid);
178
+ if (pts.length >= 3 && Math.hypot(next[0] - pts[0][0], next[1] - pts[0][1]) <= closeTolerance) {
179
+ closed = true;
180
+ return pts[0];
181
+ }
182
+ pts.push(next);
183
+ return next;
184
+ },
185
+ undo() {
186
+ pts.pop();
187
+ closed = false;
188
+ },
189
+ close() {
190
+ if (pts.length >= 3)
191
+ closed = true;
192
+ },
193
+ clear() {
194
+ pts = [];
195
+ closed = false;
196
+ },
197
+ points() {
198
+ return pts;
199
+ },
200
+ segments() {
201
+ return wallSegments(pts, closed);
202
+ },
203
+ isClosed() {
204
+ return closed;
205
+ },
206
+ footprint() {
207
+ if (!closed && !isEnclosed(pts, closeTolerance))
208
+ return null;
209
+ return footprintFromWalls(pts, closeTolerance);
210
+ },
211
+ roof(roofConfig) {
212
+ const fp = this.footprint();
213
+ return fp === null ? null : autoRoof(fp, roofConfig);
214
+ },
215
+ };
216
+ }
217
+ export function wallSegmentBounds(segment, thickness) {
218
+ const center = [(segment.from[0] + segment.to[0]) / 2, (segment.from[1] + segment.to[1]) / 2];
219
+ const quarterTurns = Math.abs(Math.round(segment.angle / (Math.PI / 2))) % 2;
220
+ return footprintAabb(center, { w: segment.length, d: thickness }, quarterTurns);
221
+ }
@@ -0,0 +1,92 @@
1
+ import type { WindVector } from "./wind.js";
2
+ export type WeatherKind = string;
3
+ export interface WeatherState {
4
+ /** Game-defined kind: "clear", "rain", "storm", "snow", "fog", "sandstorm". */
5
+ kind: WeatherKind;
6
+ /** 0..1 severity — scales every modifier. */
7
+ intensity: number;
8
+ wind?: WindVector;
9
+ }
10
+ /** Per-kind gameplay effect values at full intensity. Intensity interpolates from neutral. */
11
+ export interface WeatherModifier {
12
+ /** Movement traction multiplier at full intensity — <1 = mud/ice slows, 1 = normal. */
13
+ grip?: number;
14
+ /** View distance multiplier at full intensity — 0..1, 1 = clear. */
15
+ visibility?: number;
16
+ /** Structure HP lost per game-second at full intensity (hail, storm). */
17
+ structureDamage?: number;
18
+ /** Temperature delta at full intensity (a blizzard chills). */
19
+ chill?: number;
20
+ /** Lightning ignition chance per game-second at full intensity. */
21
+ ignition?: number;
22
+ /** Fire spread-rate multiplier at full intensity — rain suppresses (<1). Default neutral 1. */
23
+ spread?: number;
24
+ }
25
+ export type WeatherModifierTable = Record<WeatherKind, WeatherModifier>;
26
+ export interface ResolvedWeather {
27
+ grip: number;
28
+ visibility: number;
29
+ structureDamage: number;
30
+ chill: number;
31
+ ignition: number;
32
+ spread: number;
33
+ }
34
+ /**
35
+ * Resolve a weather state against a game-owned modifier table into concrete gameplay
36
+ * numbers. Multiplier-style effects (grip, visibility, spread) interpolate from the
37
+ * neutral 1 by intensity; rate-style effects (structureDamage, ignition, chill) scale
38
+ * linearly. Games read `grip`/`visibility` in movement and AI, `structureDamage` on a
39
+ * building tick, and `spread`/`ignition` feed the fire grid.
40
+ */
41
+ export declare function resolveWeather(state: WeatherState, table: WeatherModifierTable): ResolvedWeather;
42
+ export type FireCellState = "unburnt" | "burning" | "burnt";
43
+ export interface FireCell {
44
+ fuel: number;
45
+ heat: number;
46
+ state: FireCellState;
47
+ }
48
+ export interface FireGridConfig {
49
+ cols: number;
50
+ rows: number;
51
+ cellSize: number;
52
+ /** World x,z of the center of cell (0,0). Default [0,0]. */
53
+ origin?: readonly [number, number];
54
+ /** Initial fuel 0..1 per cell; default 1 everywhere. */
55
+ fuelAt?: (col: number, row: number) => number;
56
+ /** Accumulated heat needed to ignite a fuelled cell. Default 1. */
57
+ ignitionThreshold?: number;
58
+ /** Heat pushed to each neighbour per second from a burning cell. Default 0.6. */
59
+ spreadRate?: number;
60
+ /** Fuel consumed per second while burning. Default 0.25. */
61
+ burnRate?: number;
62
+ /** Prevailing wind biasing spread direction. */
63
+ wind?: WindVector;
64
+ /** How strongly wind biases downwind vs upwind spread, 0..1. Default 0.6. */
65
+ windBias?: number;
66
+ }
67
+ export interface FireStepOptions {
68
+ /** Global spread multiplier this step (rain suppression from resolveWeather().spread). Default 1. */
69
+ spread?: number;
70
+ /** Per-cell wetness 0..1 that resists ignition — from an environment field. */
71
+ wetnessAt?: (col: number, row: number) => number;
72
+ }
73
+ export interface FireGrid {
74
+ readonly cols: number;
75
+ readonly rows: number;
76
+ /** Advance the cellular propagation by game-time `dt`. */
77
+ step(dt: number, options?: FireStepOptions): void;
78
+ igniteCell(col: number, row: number): void;
79
+ /** Ignite the cell under a world position; no-op if outside the grid. */
80
+ ignite(x: number, z: number): boolean;
81
+ cell(col: number, row: number): FireCell;
82
+ cellAt(x: number, z: number): FireCell | null;
83
+ cellCoord(x: number, z: number): {
84
+ col: number;
85
+ row: number;
86
+ } | null;
87
+ /** Count of currently burning cells. */
88
+ readonly burning: number;
89
+ /** Flat snapshot (row-major) for rendering. */
90
+ snapshot(): FireCell[];
91
+ }
92
+ export declare function createFireGrid(config: FireGridConfig): FireGrid;