wallhammer-web 0.0.0-stage → 0.4.1

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 (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +92 -2
  3. package/dist/base/data.d.ts +67 -0
  4. package/dist/base/data.js +10 -0
  5. package/dist/base/destructible.d.ts +167 -0
  6. package/dist/base/destructible.js +47 -0
  7. package/dist/base/env-slot.d.ts +101 -0
  8. package/dist/base/env-slot.js +8 -0
  9. package/dist/base/frames.d.ts +138 -0
  10. package/dist/base/frames.js +459 -0
  11. package/dist/base/index.d.ts +21 -0
  12. package/dist/base/index.js +11 -0
  13. package/dist/base/mat34.d.ts +46 -0
  14. package/dist/base/mat34.js +173 -0
  15. package/dist/base/material.d.ts +87 -0
  16. package/dist/base/material.js +6 -0
  17. package/dist/base/math.d.ts +27 -0
  18. package/dist/base/math.js +60 -0
  19. package/dist/base/progress.d.ts +38 -0
  20. package/dist/base/progress.js +81 -0
  21. package/dist/base/rig.d.ts +177 -0
  22. package/dist/base/rig.js +97 -0
  23. package/dist/base/trig.d.ts +29 -0
  24. package/dist/base/trig.js +169 -0
  25. package/dist/base/up.d.ts +26 -0
  26. package/dist/base/up.js +40 -0
  27. package/dist/base/view.d.ts +128 -0
  28. package/dist/base/view.js +136 -0
  29. package/dist/camera/camera.d.ts +99 -0
  30. package/dist/camera/camera.js +228 -0
  31. package/dist/camera/follow.d.ts +120 -0
  32. package/dist/camera/follow.js +320 -0
  33. package/dist/camera/free.d.ts +153 -0
  34. package/dist/camera/free.js +359 -0
  35. package/dist/camera/index.d.ts +10 -0
  36. package/dist/camera/index.js +6 -0
  37. package/dist/camera/orbit.d.ts +93 -0
  38. package/dist/camera/orbit.js +261 -0
  39. package/dist/camera/path.d.ts +48 -0
  40. package/dist/camera/path.js +179 -0
  41. package/dist/core/ballistic.d.ts +66 -0
  42. package/dist/core/ballistic.js +104 -0
  43. package/dist/core/bodies.d.ts +77 -0
  44. package/dist/core/bodies.js +125 -0
  45. package/dist/core/clock.d.ts +37 -0
  46. package/dist/core/clock.js +87 -0
  47. package/dist/core/cover-fade.d.ts +14 -0
  48. package/dist/core/cover-fade.js +38 -0
  49. package/dist/core/index.d.ts +13 -0
  50. package/dist/core/index.js +8 -0
  51. package/dist/core/kepler.d.ts +74 -0
  52. package/dist/core/kepler.js +108 -0
  53. package/dist/core/level-fade.d.ts +66 -0
  54. package/dist/core/level-fade.js +123 -0
  55. package/dist/core/palette.d.ts +15 -0
  56. package/dist/core/palette.js +47 -0
  57. package/dist/env/atmosphere.d.ts +129 -0
  58. package/dist/env/atmosphere.js +707 -0
  59. package/dist/env/cloud-field.d.ts +44 -0
  60. package/dist/env/cloud-field.js +111 -0
  61. package/dist/env/environment.d.ts +217 -0
  62. package/dist/env/environment.js +441 -0
  63. package/dist/env/frame-block.d.ts +29 -0
  64. package/dist/env/frame-block.js +152 -0
  65. package/dist/env/glsl.d.ts +35 -0
  66. package/dist/env/glsl.js +246 -0
  67. package/dist/env/index.d.ts +13 -0
  68. package/dist/env/index.js +9 -0
  69. package/dist/env/sky.d.ts +48 -0
  70. package/dist/env/sky.js +227 -0
  71. package/dist/env/sun.d.ts +31 -0
  72. package/dist/env/sun.js +42 -0
  73. package/dist/gen-arch/build.d.ts +17 -0
  74. package/dist/gen-arch/build.js +414 -0
  75. package/dist/gen-arch/damage/damaged.d.ts +44 -0
  76. package/dist/gen-arch/damage/damaged.js +1119 -0
  77. package/dist/gen-arch/damage/fall-material.d.ts +10 -0
  78. package/dist/gen-arch/damage/fall-material.js +47 -0
  79. package/dist/gen-arch/damage/fall.d.ts +78 -0
  80. package/dist/gen-arch/damage/fall.js +797 -0
  81. package/dist/gen-arch/damage/rubble.d.ts +22 -0
  82. package/dist/gen-arch/damage/rubble.js +188 -0
  83. package/dist/gen-arch/index.d.ts +25 -0
  84. package/dist/gen-arch/index.js +16 -0
  85. package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
  86. package/dist/gen-arch/interior/interior-plan.js +615 -0
  87. package/dist/gen-arch/interior/interior.d.ts +97 -0
  88. package/dist/gen-arch/interior/interior.js +966 -0
  89. package/dist/gen-arch/structure/building-shader.d.ts +25 -0
  90. package/dist/gen-arch/structure/building-shader.js +946 -0
  91. package/dist/gen-arch/structure/buildings.d.ts +248 -0
  92. package/dist/gen-arch/structure/buildings.js +294 -0
  93. package/dist/gen-arch/structure/dims.d.ts +77 -0
  94. package/dist/gen-arch/structure/dims.js +39 -0
  95. package/dist/gen-arch/structure/distant.d.ts +62 -0
  96. package/dist/gen-arch/structure/distant.js +316 -0
  97. package/dist/gen-arch/structure/env-slots.d.ts +18 -0
  98. package/dist/gen-arch/structure/env-slots.js +47 -0
  99. package/dist/gen-arch/structure/facade.d.ts +193 -0
  100. package/dist/gen-arch/structure/facade.js +159 -0
  101. package/dist/gen-arch/structure/parts.d.ts +31 -0
  102. package/dist/gen-arch/structure/parts.js +69 -0
  103. package/dist/gen-arch/structure/roof.d.ts +76 -0
  104. package/dist/gen-arch/structure/roof.js +568 -0
  105. package/dist/gen-arch/structure/structure-material.d.ts +23 -0
  106. package/dist/gen-arch/structure/structure-material.js +55 -0
  107. package/dist/gen-arch/structure/writer.d.ts +94 -0
  108. package/dist/gen-arch/structure/writer.js +195 -0
  109. package/dist/gen-core/cell.d.ts +138 -0
  110. package/dist/gen-core/cell.js +150 -0
  111. package/dist/gen-core/cube-sphere.d.ts +57 -0
  112. package/dist/gen-core/cube-sphere.js +159 -0
  113. package/dist/gen-core/field.d.ts +55 -0
  114. package/dist/gen-core/field.js +154 -0
  115. package/dist/gen-core/geometry.d.ts +31 -0
  116. package/dist/gen-core/geometry.js +231 -0
  117. package/dist/gen-core/hash.d.ts +18 -0
  118. package/dist/gen-core/hash.js +58 -0
  119. package/dist/gen-core/height-field.d.ts +33 -0
  120. package/dist/gen-core/height-field.js +137 -0
  121. package/dist/gen-core/index.d.ts +22 -0
  122. package/dist/gen-core/index.js +17 -0
  123. package/dist/gen-core/instance-cells.d.ts +37 -0
  124. package/dist/gen-core/instance-cells.js +72 -0
  125. package/dist/gen-core/near-scatter.d.ts +56 -0
  126. package/dist/gen-core/near-scatter.js +120 -0
  127. package/dist/gen-core/noise.d.ts +23 -0
  128. package/dist/gen-core/noise.js +72 -0
  129. package/dist/gen-core/occupancy.d.ts +21 -0
  130. package/dist/gen-core/occupancy.js +104 -0
  131. package/dist/gen-core/sphere-patch.d.ts +138 -0
  132. package/dist/gen-core/sphere-patch.js +544 -0
  133. package/dist/gen-core/strip-surface.d.ts +34 -0
  134. package/dist/gen-core/strip-surface.js +246 -0
  135. package/dist/gen-core/strip.d.ts +102 -0
  136. package/dist/gen-core/strip.js +817 -0
  137. package/dist/gen-ground/env-slots.d.ts +48 -0
  138. package/dist/gen-ground/env-slots.js +78 -0
  139. package/dist/gen-ground/ground.d.ts +78 -0
  140. package/dist/gen-ground/ground.js +208 -0
  141. package/dist/gen-ground/index.d.ts +9 -0
  142. package/dist/gen-ground/index.js +7 -0
  143. package/dist/gen-ground/markings.d.ts +46 -0
  144. package/dist/gen-ground/markings.js +124 -0
  145. package/dist/gen-ground/materials.d.ts +18 -0
  146. package/dist/gen-ground/materials.js +22 -0
  147. package/dist/gen-ground/terrain-shader.d.ts +5 -0
  148. package/dist/gen-ground/terrain-shader.js +151 -0
  149. package/dist/gen-mesh/bare-shapes.d.ts +194 -0
  150. package/dist/gen-mesh/bare-shapes.js +596 -0
  151. package/dist/gen-mesh/bisect-surface.d.ts +47 -0
  152. package/dist/gen-mesh/bisect-surface.js +261 -0
  153. package/dist/gen-mesh/box.d.ts +18 -0
  154. package/dist/gen-mesh/box.js +2 -0
  155. package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
  156. package/dist/gen-mesh/cell-morph-mesh.js +258 -0
  157. package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
  158. package/dist/gen-mesh/cell-surface-mesh.js +140 -0
  159. package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
  160. package/dist/gen-mesh/chunk-mesh.js +112 -0
  161. package/dist/gen-mesh/drape-mesh.d.ts +36 -0
  162. package/dist/gen-mesh/drape-mesh.js +98 -0
  163. package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
  164. package/dist/gen-mesh/height-field-mesh.js +98 -0
  165. package/dist/gen-mesh/index.d.ts +26 -0
  166. package/dist/gen-mesh/index.js +15 -0
  167. package/dist/gen-mesh/line-mesh.d.ts +25 -0
  168. package/dist/gen-mesh/line-mesh.js +62 -0
  169. package/dist/gen-mesh/mesh-builder.d.ts +115 -0
  170. package/dist/gen-mesh/mesh-builder.js +315 -0
  171. package/dist/gen-mesh/packed-mesh.d.ts +53 -0
  172. package/dist/gen-mesh/packed-mesh.js +205 -0
  173. package/dist/gen-mesh/prism-mesh.d.ts +33 -0
  174. package/dist/gen-mesh/prism-mesh.js +176 -0
  175. package/dist/gen-mesh/rig-mesh.d.ts +9 -0
  176. package/dist/gen-mesh/rig-mesh.js +135 -0
  177. package/dist/gen-mesh/strip-mesh.d.ts +45 -0
  178. package/dist/gen-mesh/strip-mesh.js +258 -0
  179. package/dist/gen-plan/drain.d.ts +58 -0
  180. package/dist/gen-plan/drain.js +270 -0
  181. package/dist/gen-plan/grid.d.ts +37 -0
  182. package/dist/gen-plan/grid.js +182 -0
  183. package/dist/gen-plan/horizon.d.ts +50 -0
  184. package/dist/gen-plan/horizon.js +185 -0
  185. package/dist/gen-plan/index.d.ts +15 -0
  186. package/dist/gen-plan/index.js +13 -0
  187. package/dist/gen-plan/line-tiles.d.ts +35 -0
  188. package/dist/gen-plan/line-tiles.js +190 -0
  189. package/dist/gen-plan/lines.d.ts +31 -0
  190. package/dist/gen-plan/lines.js +183 -0
  191. package/dist/gen-plan/plots.d.ts +40 -0
  192. package/dist/gen-plan/plots.js +282 -0
  193. package/dist/gen-plan/route.d.ts +55 -0
  194. package/dist/gen-plan/route.js +297 -0
  195. package/dist/gen-plan/sites.d.ts +32 -0
  196. package/dist/gen-plan/sites.js +209 -0
  197. package/dist/gen-tex/code-stamp.d.ts +31 -0
  198. package/dist/gen-tex/code-stamp.js +137 -0
  199. package/dist/gen-tex/index.d.ts +7 -0
  200. package/dist/gen-tex/index.js +5 -0
  201. package/dist/gen-tex/light-horizon.d.ts +47 -0
  202. package/dist/gen-tex/light-horizon.js +180 -0
  203. package/dist/gen-tex/textures.d.ts +8 -0
  204. package/dist/gen-tex/textures.js +49 -0
  205. package/dist/gen-tex/value-textures.d.ts +46 -0
  206. package/dist/gen-tex/value-textures.js +102 -0
  207. package/dist/gen-vegetation/env-slots.d.ts +20 -0
  208. package/dist/gen-vegetation/env-slots.js +29 -0
  209. package/dist/gen-vegetation/index.d.ts +8 -0
  210. package/dist/gen-vegetation/index.js +7 -0
  211. package/dist/gen-vegetation/materials.d.ts +7 -0
  212. package/dist/gen-vegetation/materials.js +17 -0
  213. package/dist/gen-vegetation/planting.d.ts +170 -0
  214. package/dist/gen-vegetation/planting.js +363 -0
  215. package/dist/gen-vegetation/tree-shader.d.ts +23 -0
  216. package/dist/gen-vegetation/tree-shader.js +229 -0
  217. package/dist/gen-vegetation/trees.d.ts +59 -0
  218. package/dist/gen-vegetation/trees.js +329 -0
  219. package/dist/gpu/device.d.ts +286 -0
  220. package/dist/gpu/device.js +1157 -0
  221. package/dist/gpu/gl.d.ts +134 -0
  222. package/dist/gpu/gl.js +276 -0
  223. package/dist/gpu/index.d.ts +4 -0
  224. package/dist/gpu/index.js +3 -0
  225. package/dist/index.d.ts +11 -0
  226. package/dist/index.js +13 -0
  227. package/dist/motion/graph.d.ts +244 -0
  228. package/dist/motion/graph.js +739 -0
  229. package/dist/motion/index.d.ts +8 -0
  230. package/dist/motion/index.js +8 -0
  231. package/dist/motion/pose-ops.d.ts +30 -0
  232. package/dist/motion/pose-ops.js +232 -0
  233. package/dist/motion/reach.d.ts +45 -0
  234. package/dist/motion/reach.js +264 -0
  235. package/dist/motion/skeleton.d.ts +49 -0
  236. package/dist/motion/skeleton.js +99 -0
  237. package/dist/platform/byte-store.d.ts +21 -0
  238. package/dist/platform/byte-store.js +137 -0
  239. package/dist/platform/canvas.d.ts +29 -0
  240. package/dist/platform/canvas.js +76 -0
  241. package/dist/platform/context-loss.d.ts +8 -0
  242. package/dist/platform/context-loss.js +17 -0
  243. package/dist/platform/free-input.d.ts +51 -0
  244. package/dist/platform/free-input.js +100 -0
  245. package/dist/platform/index.d.ts +18 -0
  246. package/dist/platform/index.js +10 -0
  247. package/dist/platform/input.d.ts +48 -0
  248. package/dist/platform/input.js +117 -0
  249. package/dist/platform/keys.d.ts +12 -0
  250. package/dist/platform/keys.js +29 -0
  251. package/dist/platform/loop.d.ts +41 -0
  252. package/dist/platform/loop.js +84 -0
  253. package/dist/platform/walk-input.d.ts +74 -0
  254. package/dist/platform/walk-input.js +226 -0
  255. package/dist/platform/worker.d.ts +51 -0
  256. package/dist/platform/worker.js +170 -0
  257. package/dist/render/bone-instances.d.ts +24 -0
  258. package/dist/render/bone-instances.js +74 -0
  259. package/dist/render/cells.d.ts +70 -0
  260. package/dist/render/cells.js +144 -0
  261. package/dist/render/glsl.d.ts +66 -0
  262. package/dist/render/glsl.js +173 -0
  263. package/dist/render/index.d.ts +26 -0
  264. package/dist/render/index.js +16 -0
  265. package/dist/render/instance-sets.d.ts +97 -0
  266. package/dist/render/instance-sets.js +241 -0
  267. package/dist/render/near-instances.d.ts +75 -0
  268. package/dist/render/near-instances.js +219 -0
  269. package/dist/render/particles.d.ts +63 -0
  270. package/dist/render/particles.js +215 -0
  271. package/dist/render/pieces.d.ts +57 -0
  272. package/dist/render/pieces.js +151 -0
  273. package/dist/render/point-lights.d.ts +97 -0
  274. package/dist/render/point-lights.js +243 -0
  275. package/dist/render/renderer.d.ts +447 -0
  276. package/dist/render/renderer.js +1100 -0
  277. package/dist/render/rig.d.ts +74 -0
  278. package/dist/render/rig.js +576 -0
  279. package/dist/render/shadow-areas.d.ts +49 -0
  280. package/dist/render/shadow-areas.js +125 -0
  281. package/dist/render/shadow-glsl.d.ts +12 -0
  282. package/dist/render/shadow-glsl.js +146 -0
  283. package/dist/render/transient.d.ts +61 -0
  284. package/dist/render/transient.js +213 -0
  285. package/dist/render/value-glsl.d.ts +17 -0
  286. package/dist/render/value-glsl.js +208 -0
  287. package/dist/render/value-layers.d.ts +180 -0
  288. package/dist/render/value-layers.js +333 -0
  289. package/dist/runtime/atmosphere.d.ts +37 -0
  290. package/dist/runtime/atmosphere.js +304 -0
  291. package/dist/runtime/cloud-shell.d.ts +29 -0
  292. package/dist/runtime/cloud-shell.js +158 -0
  293. package/dist/runtime/engine.d.ts +155 -0
  294. package/dist/runtime/engine.js +305 -0
  295. package/dist/runtime/environment-link.d.ts +22 -0
  296. package/dist/runtime/environment-link.js +49 -0
  297. package/dist/runtime/index.d.ts +13 -0
  298. package/dist/runtime/index.js +12 -0
  299. package/dist/runtime/precipitation-glsl.d.ts +2 -0
  300. package/dist/runtime/precipitation-glsl.js +70 -0
  301. package/dist/runtime/precipitation.d.ts +11 -0
  302. package/dist/runtime/precipitation.js +78 -0
  303. package/dist/runtime/sky.d.ts +30 -0
  304. package/dist/runtime/sky.js +114 -0
  305. package/dist/runtime/streaming.d.ts +245 -0
  306. package/dist/runtime/streaming.js +602 -0
  307. package/dist/sim/agenda.d.ts +167 -0
  308. package/dist/sim/agenda.js +425 -0
  309. package/dist/sim/bytes.d.ts +34 -0
  310. package/dist/sim/bytes.js +125 -0
  311. package/dist/sim/destruction/apply.d.ts +59 -0
  312. package/dist/sim/destruction/apply.js +243 -0
  313. package/dist/sim/destruction/cell-states.d.ts +54 -0
  314. package/dist/sim/destruction/cell-states.js +198 -0
  315. package/dist/sim/destruction/index.d.ts +11 -0
  316. package/dist/sim/destruction/index.js +10 -0
  317. package/dist/sim/destruction/int-geometry.d.ts +17 -0
  318. package/dist/sim/destruction/int-geometry.js +131 -0
  319. package/dist/sim/destruction/pick.d.ts +13 -0
  320. package/dist/sim/destruction/pick.js +59 -0
  321. package/dist/sim/destruction/rubble.d.ts +8 -0
  322. package/dist/sim/destruction/rubble.js +159 -0
  323. package/dist/sim/destruction/state.d.ts +15 -0
  324. package/dist/sim/destruction/state.js +83 -0
  325. package/dist/sim/destruction/structure.d.ts +30 -0
  326. package/dist/sim/destruction/structure.js +139 -0
  327. package/dist/sim/events.d.ts +37 -0
  328. package/dist/sim/events.js +106 -0
  329. package/dist/sim/fixed.d.ts +21 -0
  330. package/dist/sim/fixed.js +64 -0
  331. package/dist/sim/flow-probe.d.ts +58 -0
  332. package/dist/sim/flow-probe.js +158 -0
  333. package/dist/sim/flow.d.ts +257 -0
  334. package/dist/sim/flow.js +967 -0
  335. package/dist/sim/index.d.ts +28 -0
  336. package/dist/sim/index.js +20 -0
  337. package/dist/sim/interlock.d.ts +47 -0
  338. package/dist/sim/interlock.js +252 -0
  339. package/dist/sim/movers.d.ts +228 -0
  340. package/dist/sim/movers.js +1083 -0
  341. package/dist/sim/nav.d.ts +72 -0
  342. package/dist/sim/nav.js +292 -0
  343. package/dist/sim/paths.d.ts +80 -0
  344. package/dist/sim/paths.js +197 -0
  345. package/dist/sim/regions.d.ts +87 -0
  346. package/dist/sim/regions.js +517 -0
  347. package/dist/sim/simulation.d.ts +114 -0
  348. package/dist/sim/simulation.js +291 -0
  349. package/dist/sim/states.d.ts +23 -0
  350. package/dist/sim/states.js +54 -0
  351. package/dist/sim/table.d.ts +49 -0
  352. package/dist/sim/table.js +126 -0
  353. package/dist/sim/zones.d.ts +11 -0
  354. package/dist/sim/zones.js +196 -0
  355. package/dist/tools/collision-lines.d.ts +12 -0
  356. package/dist/tools/collision-lines.js +50 -0
  357. package/dist/tools/data-view-glsl.d.ts +3 -0
  358. package/dist/tools/data-view-glsl.js +63 -0
  359. package/dist/tools/data-view-layers.d.ts +20 -0
  360. package/dist/tools/data-view-layers.js +60 -0
  361. package/dist/tools/debug-view.d.ts +30 -0
  362. package/dist/tools/debug-view.js +4 -0
  363. package/dist/tools/index.d.ts +10 -0
  364. package/dist/tools/index.js +7 -0
  365. package/dist/tools/mesh-checks.d.ts +67 -0
  366. package/dist/tools/mesh-checks.js +188 -0
  367. package/dist/tools/profile.d.ts +85 -0
  368. package/dist/tools/profile.js +173 -0
  369. package/dist/world/cell-cache.d.ts +32 -0
  370. package/dist/world/cell-cache.js +60 -0
  371. package/dist/world/cell-format.d.ts +25 -0
  372. package/dist/world/cell-format.js +232 -0
  373. package/dist/world/collide.d.ts +192 -0
  374. package/dist/world/collide.js +822 -0
  375. package/dist/world/index.d.ts +16 -0
  376. package/dist/world/index.js +12 -0
  377. package/dist/world/mesh-solids.d.ts +48 -0
  378. package/dist/world/mesh-solids.js +169 -0
  379. package/dist/world/pick.d.ts +34 -0
  380. package/dist/world/pick.js +177 -0
  381. package/dist/world/providers.d.ts +66 -0
  382. package/dist/world/providers.js +83 -0
  383. package/dist/world/sphere.d.ts +69 -0
  384. package/dist/world/sphere.js +271 -0
  385. package/dist/world/streaming.d.ts +288 -0
  386. package/dist/world/streaming.js +801 -0
  387. package/dist/world/walker.d.ts +145 -0
  388. package/dist/world/walker.js +502 -0
  389. package/docs/architecture.md +111 -0
  390. package/docs/cameras.md +302 -0
  391. package/docs/concepts.md +123 -0
  392. package/docs/destruction.md +127 -0
  393. package/docs/frames.md +199 -0
  394. package/docs/generators.md +575 -0
  395. package/docs/getting-started.md +123 -0
  396. package/docs/headless.md +50 -0
  397. package/docs/motion.md +160 -0
  398. package/docs/planet.md +152 -0
  399. package/docs/recipes.md +521 -0
  400. package/docs/reference.md +364 -0
  401. package/docs/rendering.md +823 -0
  402. package/docs/rigs.md +203 -0
  403. package/docs/simulation.md +235 -0
  404. package/docs/streaming.md +590 -0
  405. package/docs/system.md +166 -0
  406. package/docs/tools.md +175 -0
  407. package/docs/walking.md +262 -0
  408. package/package.json +131 -3
@@ -0,0 +1,125 @@
1
+ // Byte streams for snapshots and the state hash. Every piece of simulation state writes itself into a `ByteWriter`
2
+ // in a fixed order and reads itself back from a `ByteReader`; the state hash is a hash of exactly those bytes, so two
3
+ // states with the same hash restore to the same simulation.
4
+ const TEXT_ENCODER = new TextEncoder();
5
+ const TEXT_DECODER = new TextDecoder();
6
+ /** Growable little-endian byte buffer. */
7
+ export class ByteWriter {
8
+ buf = new Uint8Array(1024);
9
+ view = new DataView(this.buf.buffer);
10
+ at = 0;
11
+ room(n) {
12
+ if (this.at + n <= this.buf.length)
13
+ return;
14
+ let size = this.buf.length * 2;
15
+ while (size < this.at + n)
16
+ size *= 2;
17
+ const next = new Uint8Array(size);
18
+ next.set(this.buf.subarray(0, this.at));
19
+ this.buf = next;
20
+ this.view = new DataView(next.buffer);
21
+ }
22
+ get length() {
23
+ return this.at;
24
+ }
25
+ u8(v) {
26
+ this.room(1);
27
+ this.view.setUint8(this.at, v);
28
+ this.at += 1;
29
+ }
30
+ i32(v) {
31
+ this.room(4);
32
+ this.view.setInt32(this.at, v, true);
33
+ this.at += 4;
34
+ }
35
+ u32(v) {
36
+ this.room(4);
37
+ this.view.setUint32(this.at, v, true);
38
+ this.at += 4;
39
+ }
40
+ /** A 64-bit float; used for ids (integers below 2^53) and for app-supplied geometry. */
41
+ f64(v) {
42
+ this.room(8);
43
+ this.view.setFloat64(this.at, v, true);
44
+ this.at += 8;
45
+ }
46
+ str(s) {
47
+ const b = TEXT_ENCODER.encode(s);
48
+ this.u32(b.length);
49
+ this.bytes(b);
50
+ }
51
+ /** The raw bytes of a typed array (no length prefix). */
52
+ bytes(a) {
53
+ const b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
54
+ this.room(b.length);
55
+ this.buf.set(b, this.at);
56
+ this.at += b.length;
57
+ }
58
+ /** A copy of what was written. */
59
+ finish() {
60
+ return this.buf.slice(0, this.at);
61
+ }
62
+ }
63
+ export class ByteReader {
64
+ bytesIn;
65
+ view;
66
+ at = 0;
67
+ constructor(bytes) {
68
+ this.bytesIn = bytes;
69
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
70
+ }
71
+ get done() {
72
+ return this.at >= this.bytesIn.length;
73
+ }
74
+ u8() {
75
+ const v = this.view.getUint8(this.at);
76
+ this.at += 1;
77
+ return v;
78
+ }
79
+ i32() {
80
+ const v = this.view.getInt32(this.at, true);
81
+ this.at += 4;
82
+ return v;
83
+ }
84
+ u32() {
85
+ const v = this.view.getUint32(this.at, true);
86
+ this.at += 4;
87
+ return v;
88
+ }
89
+ f64() {
90
+ const v = this.view.getFloat64(this.at, true);
91
+ this.at += 8;
92
+ return v;
93
+ }
94
+ str() {
95
+ const n = this.u32();
96
+ const s = TEXT_DECODER.decode(this.bytesIn.subarray(this.at, this.at + n));
97
+ this.at += n;
98
+ return s;
99
+ }
100
+ /** Copies `target.byteLength` bytes into a typed array. */
101
+ into(target) {
102
+ const n = target.byteLength;
103
+ new Uint8Array(target.buffer, target.byteOffset, n).set(this.bytesIn.subarray(this.at, this.at + n));
104
+ this.at += n;
105
+ }
106
+ }
107
+ /** 64-bit hash of bytes as 16 hex digits: two independent 32-bit FNV-1a lanes with a final avalanche. */
108
+ export function hashBytes(bytes) {
109
+ let a = 0x811c9dc5;
110
+ let b = 0x050c5d1f;
111
+ for (let i = 0; i < bytes.length; i++) {
112
+ const c = bytes[i];
113
+ a = Math.imul(a ^ c, 0x01000193);
114
+ b = Math.imul(b ^ c ^ (i & 0xff), 0x01000193);
115
+ }
116
+ const fin = (h) => {
117
+ h ^= h >>> 16;
118
+ h = Math.imul(h, 0x85ebca6b);
119
+ h ^= h >>> 13;
120
+ h = Math.imul(h, 0xc2b2ae35);
121
+ h ^= h >>> 16;
122
+ return (h >>> 0).toString(16).padStart(8, '0');
123
+ };
124
+ return fin(a ^ bytes.length) + fin(b);
125
+ }
@@ -0,0 +1,59 @@
1
+ import type { DamageRung, DestructionEvent, RubbleFootprint, StructureDesc, StructureState } from '../../base/destructible.ts';
2
+ import type { CompiledStructure } from './structure.ts';
3
+ export type ChangeCause = 'damage' | 'support' | 'ruin';
4
+ export interface PartChange {
5
+ structure: number;
6
+ part: number;
7
+ from: DamageRung;
8
+ to: DamageRung;
9
+ cause: ChangeCause;
10
+ }
11
+ export interface DestructionResult {
12
+ state: StructureState;
13
+ changes: PartChange[];
14
+ /** Rubble that appeared with this event. */
15
+ rubble: RubbleFootprint[];
16
+ /** Per change (same order): ticks after the event at which the part lets go (`TIMELINE_TICK_S` each). */
17
+ timeline: Uint16Array;
18
+ }
19
+ /** Length of one timeline tick in seconds. */
20
+ export declare const TIMELINE_TICK_S = 0.05;
21
+ /** Timeline shape in ticks: first support loss, one stage up a resting chain, one lateral step, jitter range, latest. */
22
+ export declare const TIMELINE: {
23
+ readonly first: 10;
24
+ readonly stage: 6;
25
+ readonly lateral: 2;
26
+ readonly jitter: 4;
27
+ readonly max: 50;
28
+ };
29
+ export interface DestructionOptions {
30
+ /** Most damaged parts a structure may hold before it coarsens into a ruin. */
31
+ maxEntries?: number;
32
+ }
33
+ /**
34
+ * Builds a canonical event from metres (as a click gives them). Rounding happens here, once; from then on the
35
+ * event is integers. Energy is in abstract units (a few hundred cracks masonry, about a thousand blows it out).
36
+ */
37
+ export declare function destructionEvent(o: {
38
+ position: readonly [number, number, number];
39
+ radius: number;
40
+ energy: number;
41
+ seed: number;
42
+ tick: number;
43
+ }): DestructionEvent;
44
+ /** Applies one event to one structure. */
45
+ export declare function applyEvent(structure: StructureDesc | CompiledStructure, state: StructureState, ev: DestructionEvent, options?: DestructionOptions): DestructionResult;
46
+ /**
47
+ * When each changed part lets go (ticks after the event), see step 6 above. Pure: the changes, the bonds and the seed.
48
+ */
49
+ export declare function fallTimeline(s: CompiledStructure, changes: readonly PartChange[], seed: number): Uint16Array;
50
+ /**
51
+ * Applies one event to many structures, in structure id order. `states` is not modified; the result holds the new
52
+ * state of every structure that changed.
53
+ */
54
+ export declare function applyEventToAll(structures: readonly (StructureDesc | CompiledStructure)[], states: ReadonlyMap<number, StructureState>, ev: DestructionEvent, options?: DestructionOptions): {
55
+ states: Map<number, StructureState>;
56
+ changes: PartChange[];
57
+ rubble: RubbleFootprint[];
58
+ timeline: Uint16Array;
59
+ };
@@ -0,0 +1,243 @@
1
+ // What a destruction event breaks. Pure and deterministic: (structure, state, event) → (new state, changed parts,
2
+ // new rubble). No clock, no Math.random, integers wherever a decision is made.
3
+ //
4
+ // 1. Hit test: every destructible part whose convex piece lies within the event radius, in key order.
5
+ // 2. Damage: energy × (r² − d²) / r², against the part's material thresholds, each jittered ±20 % by the event seed.
6
+ // A part that is hit again moves at least one rung further.
7
+ // 3. Support: connectivity from the ground anchors (structure.ts). A part with no supported path falls: it is gone.
8
+ // 4. Rubble: every part that became gone gets its footprint (rubble.ts) from the fall direction and a variant chosen now.
9
+ // 5. Budget: a structure with more entries than the budget coarsens into a ruin, visibly, never silently.
10
+ // 6. Timeline: when each changed part lets go, in whole ticks after the event (`TIMELINE_TICK_S`), for presentation.
11
+ // The state is committed at once and does not depend on it: a state saved mid-collapse is the final state, and a
12
+ // second event during a collapse acts on that final state. Hit parts go at tick 0. A part that lost its support
13
+ // goes after the parts it hung on: a shortest path over the bonds from the parts the event destroyed (a step to a
14
+ // part resting on another costs a whole stage, a lateral step a little), plus a hashed jitter, squeezed into
15
+ // `TIMELINE_MAX_TICKS`. Parts with no path from a destroyed part (a ruin) go by height.
16
+ import { DAMAGE, PART_FLAG, entryRung, packEntry } from '../../base/destructible.js';
17
+ import { hashInts } from '../../gen-core/hash.js';
18
+ import { idiv, octant, pieceDistSq } from './int-geometry.js';
19
+ import { partRubble, ruinRubble } from './rubble.js';
20
+ import { compileStructure, solveSupport, UNSUPPORTED } from './structure.js';
21
+ /** Length of one timeline tick in seconds. */
22
+ export const TIMELINE_TICK_S = 0.05;
23
+ /** Timeline shape in ticks: first support loss, one stage up a resting chain, one lateral step, jitter range, latest. */
24
+ export const TIMELINE = { first: 10, stage: 6, lateral: 2, jitter: 4, max: 50 };
25
+ /** Height of one stage of the height fallback (cm). */
26
+ const STAGE_HEIGHT = 300;
27
+ const DEFAULT_MAX_ENTRIES = 600;
28
+ /** Fall direction counts as straight down within this distance of the structure centre (cm). */
29
+ const CENTRE_RADIUS = 50;
30
+ /**
31
+ * Builds a canonical event from metres (as a click gives them). Rounding happens here, once; from then on the
32
+ * event is integers. Energy is in abstract units (a few hundred cracks masonry, about a thousand blows it out).
33
+ */
34
+ export function destructionEvent(o) {
35
+ return {
36
+ x: Math.round(o.position[0] * 100),
37
+ y: Math.round(o.position[1] * 100),
38
+ z: Math.round(o.position[2] * 100),
39
+ radius: Math.max(1, Math.round(o.radius * 100)),
40
+ energy: Math.max(0, Math.round(o.energy)),
41
+ seed: o.seed >>> 0,
42
+ tick: Math.max(0, Math.round(o.tick)),
43
+ };
44
+ }
45
+ function rungFor(dmg, crack, breach, gone) {
46
+ if (dmg >= gone)
47
+ return DAMAGE.gone;
48
+ if (breach > 0 && dmg >= breach)
49
+ return DAMAGE.breached;
50
+ if (dmg >= crack)
51
+ return DAMAGE.cracked;
52
+ return DAMAGE.intact;
53
+ }
54
+ /** Applies one event to one structure. */
55
+ export function applyEvent(structure, state, ev, options = {}) {
56
+ const s = 'desc' in structure ? structure : compileStructure(structure);
57
+ const desc = s.desc;
58
+ if (desc.keepOut || state.ruin)
59
+ return { state, changes: [], rubble: [], timeline: NO_TIMELINE };
60
+ const r = ev.radius;
61
+ const r2 = r * r;
62
+ const b = s.bounds;
63
+ // Broad phase: the event sphere against the structure's box.
64
+ const gap = (v, lo, hi) => (v < lo ? lo - v : v > hi ? v - hi : 0);
65
+ const gx = gap(ev.x, b[0], b[1]);
66
+ const gy = gap(ev.y, b[2], b[3]);
67
+ const gz = gap(ev.z, b[4], b[5]);
68
+ if (gx * gx + gy * gy + gz * gz > r2)
69
+ return { state, changes: [], rubble: [], timeline: NO_TIMELINE };
70
+ const seed = hashInts(ev.seed, ev.tick, desc.id);
71
+ const parts = desc.parts;
72
+ const n = parts.length;
73
+ const next = new Map(state.parts);
74
+ const changes = [];
75
+ const rubble = [];
76
+ // --- Damage ---------------------------------------------------------------------------------------------------
77
+ for (let i = 0; i < n; i++) {
78
+ const p = parts[i];
79
+ if (p.flags & PART_FLAG.indestructible)
80
+ continue;
81
+ const old = next.get(p.key);
82
+ const oldRung = old === undefined ? DAMAGE.intact : entryRung(old);
83
+ if (oldRung === DAMAGE.gone)
84
+ continue;
85
+ const o = i * 6;
86
+ const bx = gap(ev.x, s.box[o], s.box[o + 1]);
87
+ const by = gap(ev.y, s.box[o + 2], s.box[o + 3]);
88
+ const bz = gap(ev.z, s.box[o + 4], s.box[o + 5]);
89
+ if (bx * bx + by * by + bz * bz > r2)
90
+ continue;
91
+ const d2 = pieceDistSq(p.piece, ev.x, ev.y, ev.z);
92
+ if (d2 > r2)
93
+ continue;
94
+ const dmg = idiv(ev.energy * (r2 - d2), r2);
95
+ const m = desc.materials[p.material];
96
+ if (!m)
97
+ continue;
98
+ const pct = 80 + (hashInts(seed, p.key) % 41);
99
+ const crack = idiv(m.crack * pct, 100);
100
+ const breach = idiv(m.breach * pct, 100);
101
+ const goneAt = idiv(m.gone * pct, 100);
102
+ let rung = rungFor(dmg, crack, breach, goneAt);
103
+ if (rung === DAMAGE.intact)
104
+ continue;
105
+ // A hit on a damaged part moves it at least one rung further (skipping breach where the material has none).
106
+ if (rung <= oldRung)
107
+ rung = (oldRung + 1 === DAMAGE.breached && m.breach <= 0 ? DAMAGE.gone : oldRung + 1);
108
+ const c = i * 3;
109
+ const dir = octant(s.centre[c] - ev.x, s.centre[c + 2] - ev.z, 1);
110
+ const entry = packEntry(rung, dir, hashInts(seed, p.key, 1) & 255);
111
+ next.set(p.key, entry);
112
+ changes.push({ structure: desc.id, part: p.key, from: oldRung, to: rung, cause: 'damage' });
113
+ if (rung === DAMAGE.gone)
114
+ rubble.push(partRubble(s, p, entry));
115
+ }
116
+ if (changes.length === 0)
117
+ return { state, changes, rubble, timeline: NO_TIMELINE };
118
+ // --- Support -----------------------------------------------------------------------------------------------------
119
+ const alive = new Uint8Array(n);
120
+ for (let i = 0; i < n; i++) {
121
+ const e = next.get(parts[i].key);
122
+ alive[i] = e === undefined || entryRung(e) !== DAMAGE.gone ? 1 : 0;
123
+ }
124
+ const hang = solveSupport(s, alive, s.span);
125
+ for (let i = 0; i < n; i++) {
126
+ if (!alive[i] || hang[i] !== UNSUPPORTED)
127
+ continue;
128
+ const p = parts[i];
129
+ if (p.flags & PART_FLAG.indestructible)
130
+ continue;
131
+ const old = next.get(p.key);
132
+ const oldRung = old === undefined ? DAMAGE.intact : entryRung(old);
133
+ const c = i * 3;
134
+ const dir = octant(s.centre[c] - desc.cx, s.centre[c + 2] - desc.cz, CENTRE_RADIUS);
135
+ const entry = packEntry(DAMAGE.gone, dir, hashInts(seed, p.key, 2) & 255);
136
+ next.set(p.key, entry);
137
+ changes.push({ structure: desc.id, part: p.key, from: oldRung, to: DAMAGE.gone, cause: 'support' });
138
+ rubble.push(partRubble(s, p, entry));
139
+ }
140
+ // --- Budget ------------------------------------------------------------------------------------------------------
141
+ if (next.size > (options.maxEntries ?? DEFAULT_MAX_ENTRIES)) {
142
+ const ruinChanges = [];
143
+ for (const p of parts) {
144
+ if (p.flags & PART_FLAG.indestructible)
145
+ continue;
146
+ const e = next.get(p.key);
147
+ const from = e === undefined ? DAMAGE.intact : entryRung(e);
148
+ if (from !== DAMAGE.gone)
149
+ ruinChanges.push({ structure: desc.id, part: p.key, from, to: DAMAGE.gone, cause: 'ruin' });
150
+ }
151
+ const all = [...changes, ...ruinChanges];
152
+ return { state: { structure: desc.id, ruin: true, parts: new Map() }, changes: all, rubble: [ruinRubble(s)], timeline: fallTimeline(s, all, seed) };
153
+ }
154
+ return { state: { structure: desc.id, ruin: false, parts: next }, changes, rubble, timeline: fallTimeline(s, changes, seed) };
155
+ }
156
+ const NO_TIMELINE = new Uint16Array(0);
157
+ /**
158
+ * When each changed part lets go (ticks after the event), see step 6 above. Pure: the changes, the bonds and the seed.
159
+ */
160
+ export function fallTimeline(s, changes, seed) {
161
+ const n = s.desc.parts.length;
162
+ const T = TIMELINE;
163
+ const INF = 0xffff;
164
+ const dist = new Uint16Array(n).fill(INF);
165
+ const falling = new Uint8Array(n);
166
+ const buckets = [[]];
167
+ for (const c of changes) {
168
+ const i = s.index.get(c.part);
169
+ if (i === undefined)
170
+ continue;
171
+ if (c.cause === 'damage') {
172
+ if (c.to !== DAMAGE.gone)
173
+ continue;
174
+ dist[i] = 0;
175
+ buckets[0].push(i);
176
+ }
177
+ else
178
+ falling[i] = 1;
179
+ }
180
+ // Shortest paths over the bonds, only through parts that fall in this event (bucket queue, small integer weights).
181
+ for (let d = 0; d < buckets.length; d++) {
182
+ const bucket = buckets[d];
183
+ if (!bucket)
184
+ continue;
185
+ for (let k = 0; k < bucket.length; k++) {
186
+ const i = bucket[k];
187
+ if (dist[i] !== d)
188
+ continue;
189
+ const relax = (j, w) => {
190
+ const to = d + w;
191
+ if (!falling[j] || dist[j] <= to || to >= INF)
192
+ return;
193
+ dist[j] = to;
194
+ (buckets[to] ??= []).push(j);
195
+ };
196
+ for (const j of s.above[i])
197
+ relax(j, T.stage);
198
+ for (const j of s.below[i])
199
+ relax(j, T.stage);
200
+ for (const j of s.lateral[i])
201
+ relax(j, T.lateral);
202
+ }
203
+ }
204
+ const y0 = s.bounds[2];
205
+ const raw = new Uint32Array(changes.length);
206
+ let most = 0;
207
+ changes.forEach((c, k) => {
208
+ const i = s.index.get(c.part);
209
+ if (c.cause === 'damage' || i === undefined)
210
+ return;
211
+ const height = Math.max(0, s.centre[i * 3 + 1] - y0);
212
+ const base = dist[i] !== INF ? dist[i] : idiv(height * T.stage, STAGE_HEIGHT);
213
+ raw[k] = T.first + base + (hashInts(seed, c.part, 3) % T.jitter);
214
+ most = Math.max(most, raw[k]);
215
+ });
216
+ const out = new Uint16Array(changes.length);
217
+ raw.forEach((v, k) => {
218
+ out[k] = v === 0 ? 0 : most > T.max ? T.first + idiv((v - T.first) * (T.max - T.first), most - T.first) : v;
219
+ });
220
+ return out;
221
+ }
222
+ /**
223
+ * Applies one event to many structures, in structure id order. `states` is not modified; the result holds the new
224
+ * state of every structure that changed.
225
+ */
226
+ export function applyEventToAll(structures, states, ev, options = {}) {
227
+ const compiled = structures.map((x) => ('desc' in x ? x : compileStructure(x))).sort((p, q) => p.desc.id - q.desc.id);
228
+ const out = new Map(states);
229
+ const changes = [];
230
+ const rubble = [];
231
+ const timeline = [];
232
+ for (const s of compiled) {
233
+ const before = states.get(s.desc.id) ?? { structure: s.desc.id, ruin: false, parts: new Map() };
234
+ const r = applyEvent(s, before, ev, options);
235
+ if (r.changes.length === 0)
236
+ continue;
237
+ out.set(s.desc.id, r.state);
238
+ changes.push(...r.changes);
239
+ rubble.push(...r.rubble);
240
+ timeline.push(...r.timeline);
241
+ }
242
+ return { states: out, changes, rubble, timeline: Uint16Array.from(timeline) };
243
+ }
@@ -0,0 +1,54 @@
1
+ import type { StructureState } from '../../base/destructible.ts';
2
+ import type { CellId } from '../../gen-core/cell.ts';
3
+ /** Default ceiling of a store in units. */
4
+ export declare const DEFAULT_MAX_UNITS = 20000;
5
+ /** `text()` is never longer than this many characters per unit of the ceiling. */
6
+ export declare const CELL_STATES_CHARS_PER_UNIT = 40;
7
+ export interface CommitResult {
8
+ /** False: the store is full of changed structures, nothing changed. */
9
+ accepted: boolean;
10
+ /** Structures the ceiling turned into ruins, in the order it took them. */
11
+ ruined: {
12
+ cell: CellId;
13
+ structure: number;
14
+ }[];
15
+ }
16
+ /** Store of changed structure states by home cell. */
17
+ export declare class CellStates {
18
+ readonly maxUnits: number;
19
+ private readonly records;
20
+ private unitTotal;
21
+ private structureTotal;
22
+ constructor(options?: {
23
+ maxUnits?: number;
24
+ });
25
+ /** Σ over changed structures of 1 + its number of part entries (a ruin costs 1). */
26
+ get units(): number;
27
+ /** Changed structures. */
28
+ get structures(): number;
29
+ /** States of a home cell by structure id; one shared empty map when nothing changed there. */
30
+ of(cell: CellId): ReadonlyMap<number, StructureState>;
31
+ /** Replaces the states of a home cell. Intact states are dropped; the ceiling is applied inside. */
32
+ commit(cell: CellId, states: ReadonlyMap<number, StructureState>): CommitResult;
33
+ /** Home cells with a change, in `compareCells` order. */
34
+ cells(): CellId[];
35
+ /** Home cells with a change that are `cell` or lie below it in the quadtree, in `compareCells` order. */
36
+ under(cell: CellId): CellId[];
37
+ /** Forgets one home cell, or everything. */
38
+ clear(cell?: CellId): void;
39
+ /** 64-bit hash of the whole store as 16 hex digits: each cell's bits and its `stateHash`, in cell order. */
40
+ hash(): string;
41
+ /** Canonical text of the whole store, or only of the home cells of `under(cell)`; '' when there are none. */
42
+ text(under?: CellId): string;
43
+ private sortedRecords;
44
+ }
45
+ /**
46
+ * Parses the text of `CellStates.text`. Commits cell by cell in text order; a malformed cell (or one the ceiling
47
+ * refuses, or one that repeats) is skipped and reported in `errors`, never guessed.
48
+ */
49
+ export declare function decodeCellStates(text: string, options?: {
50
+ maxUnits?: number;
51
+ }): {
52
+ store: CellStates;
53
+ errors: string[];
54
+ };
@@ -0,0 +1,198 @@
1
+ // Damage state of a streamed world: one store keyed by the home cell of a structure and the structure's id. A home
2
+ // cell is the one cell that owns a structure; which quadtree level that is, the content decides. The store holds only
3
+ // structures that changed, has a ceiling in units with one fixed rule, and saves to URL-safe text.
4
+ //
5
+ // Determinism: the store takes no seed and reads no clock. Its content, text and hash are a function of the commits;
6
+ // equal content gives equal text and hash whatever the order of the commits. Cells are ordered by `compareCells`,
7
+ // structures by id, parts by key; never by the iteration order of a map.
8
+ //
9
+ // Text form: cells in order joined by `;`, each `<face>.<level>.<x>.<y>:<encodeStates of the cell>`, numbers in base
10
+ // 36, x and y as the unsigned 28-bit fields of the cell id (the coordinate plus 2^27, so no sign is needed). Only the
11
+ // characters `0-9 a-z . : ; ! ~ , _` occur. Example: `7.9.2ihn5c.2ihn4w:0~2h4k.c3,1.3`.
12
+ //
13
+ // Ceiling (`units ≤ maxUnits` after every commit; a changed structure costs 1 plus its number of part entries, a ruin
14
+ // costs 1): (1) a commit that would leave more changed structures than `maxUnits` is refused and changes nothing;
15
+ // (2) otherwise, while the store is over the ceiling, the structure with the most part entries in the whole store
16
+ // (ties: the earlier cell, then the lower id) becomes a ruin. The rule reads only the store, never which cells are
17
+ // loaded, where a viewer is, the order of visits or time.
18
+ import { cellBits, cellId, cellKey, compareCells } from '../../gen-core/cell.js';
19
+ import { hashInts } from '../../gen-core/hash.js';
20
+ import { decodeStates, encodeStates, isIntact, stateHash } from './state.js';
21
+ /** Default ceiling of a store in units. */
22
+ export const DEFAULT_MAX_UNITS = 20000;
23
+ /** `text()` is never longer than this many characters per unit of the ceiling. */
24
+ export const CELL_STATES_CHARS_PER_UNIT = 40;
25
+ /** Offset of the unsigned x and y fields of a cell id (see gen-core/cell.ts). */
26
+ const XY_OFFSET = 2 ** 27;
27
+ const EMPTY = new Map();
28
+ function unitsOf(s) {
29
+ return 1 + (s.ruin ? 0 : s.parts.size);
30
+ }
31
+ function hex8(s, from) {
32
+ return parseInt(s.slice(from, from + 8), 16) >>> 0;
33
+ }
34
+ /** Store of changed structure states by home cell. */
35
+ export class CellStates {
36
+ maxUnits;
37
+ records = new Map();
38
+ unitTotal = 0;
39
+ structureTotal = 0;
40
+ constructor(options = {}) {
41
+ const m = options.maxUnits ?? DEFAULT_MAX_UNITS;
42
+ this.maxUnits = Math.max(1, Math.floor(m));
43
+ }
44
+ /** Σ over changed structures of 1 + its number of part entries (a ruin costs 1). */
45
+ get units() {
46
+ return this.unitTotal;
47
+ }
48
+ /** Changed structures. */
49
+ get structures() {
50
+ return this.structureTotal;
51
+ }
52
+ /** States of a home cell by structure id; one shared empty map when nothing changed there. */
53
+ of(cell) {
54
+ return this.records.get(cellKey(cell))?.states ?? EMPTY;
55
+ }
56
+ /** Replaces the states of a home cell. Intact states are dropped; the ceiling is applied inside. */
57
+ commit(cell, states) {
58
+ const key = cellKey(cell);
59
+ const ids = [...states.keys()].sort((a, b) => a - b);
60
+ const next = new Map();
61
+ let units = 0;
62
+ for (const id of ids) {
63
+ const s = states.get(id);
64
+ if (isIntact(s))
65
+ continue;
66
+ const own = s.ruin ? { structure: id, ruin: true, parts: new Map() } : { structure: id, ruin: false, parts: new Map(s.parts) };
67
+ next.set(id, own);
68
+ units += unitsOf(own);
69
+ }
70
+ const old = this.records.get(key);
71
+ const structures = this.structureTotal - (old?.states.size ?? 0) + next.size;
72
+ if (structures > this.maxUnits)
73
+ return { accepted: false, ruined: [] };
74
+ this.unitTotal += units - (old?.units ?? 0);
75
+ this.structureTotal = structures;
76
+ if (next.size === 0)
77
+ this.records.delete(key);
78
+ else
79
+ this.records.set(key, { cell: cellId(cell.face, cell.level, cell.x, cell.y), states: next, units });
80
+ const ruined = [];
81
+ while (this.unitTotal > this.maxUnits) {
82
+ // The largest structure in the whole store; ties go to the earlier cell, then the lower id.
83
+ let best = null;
84
+ for (const rec of this.sortedRecords()) {
85
+ for (const id of [...rec.states.keys()].sort((a, b) => a - b)) {
86
+ const s = rec.states.get(id);
87
+ const size = s.ruin ? 0 : s.parts.size;
88
+ if (size > 0 && (!best || size > best.size))
89
+ best = { rec, id, size };
90
+ }
91
+ }
92
+ // Cannot happen while structures ≤ maxUnits; guards the loop.
93
+ if (!best)
94
+ break;
95
+ best.rec.states.set(best.id, { structure: best.id, ruin: true, parts: new Map() });
96
+ best.rec.units -= best.size;
97
+ this.unitTotal -= best.size;
98
+ ruined.push({ cell: best.rec.cell, structure: best.id });
99
+ }
100
+ return { accepted: true, ruined };
101
+ }
102
+ /** Home cells with a change, in `compareCells` order. */
103
+ cells() {
104
+ return this.sortedRecords().map((r) => r.cell);
105
+ }
106
+ /** Home cells with a change that are `cell` or lie below it in the quadtree, in `compareCells` order. */
107
+ under(cell) {
108
+ return this.cells().filter((c) => isUnder(c, cell));
109
+ }
110
+ /** Forgets one home cell, or everything. */
111
+ clear(cell) {
112
+ if (!cell) {
113
+ this.records.clear();
114
+ this.unitTotal = 0;
115
+ this.structureTotal = 0;
116
+ return;
117
+ }
118
+ const key = cellKey(cell);
119
+ const r = this.records.get(key);
120
+ if (!r)
121
+ return;
122
+ this.unitTotal -= r.units;
123
+ this.structureTotal -= r.states.size;
124
+ this.records.delete(key);
125
+ }
126
+ /** 64-bit hash of the whole store as 16 hex digits: each cell's bits and its `stateHash`, in cell order. */
127
+ hash() {
128
+ let h1 = 0x3c6ef372;
129
+ let h2 = 0x1b873593;
130
+ for (const r of this.sortedRecords()) {
131
+ const [hi, lo] = cellBits(r.cell);
132
+ const s = stateHash([...r.states.values()]);
133
+ const a = hex8(s, 0);
134
+ const b = hex8(s, 8);
135
+ h1 = hashInts(h1, hi, lo, a, b);
136
+ h2 = hashInts(h2, lo ^ 0x5bd1e995, hi, b, a);
137
+ }
138
+ return h1.toString(16).padStart(8, '0') + h2.toString(16).padStart(8, '0');
139
+ }
140
+ /** Canonical text of the whole store, or only of the home cells of `under(cell)`; '' when there are none. */
141
+ text(under) {
142
+ const list = under ? this.sortedRecords().filter((r) => isUnder(r.cell, under)) : this.sortedRecords();
143
+ return list.map((r) => `${cellText(r.cell)}:${encodeStates(r.states.values())}`).join(';');
144
+ }
145
+ sortedRecords() {
146
+ return [...this.records.values()].sort((a, b) => compareCells(a.cell, b.cell));
147
+ }
148
+ }
149
+ /** True when `c` is `top` or lies below it in the quadtree (children of (x, y) are (2x + i, 2y + j)). */
150
+ function isUnder(c, top) {
151
+ if (c.face !== top.face || c.level < top.level)
152
+ return false;
153
+ const d = c.level - top.level;
154
+ return c.x >> d === top.x && c.y >> d === top.y;
155
+ }
156
+ function cellText(c) {
157
+ return `${c.face.toString(36)}.${c.level.toString(36)}.${(c.x + XY_OFFSET).toString(36)}.${(c.y + XY_OFFSET).toString(36)}`;
158
+ }
159
+ /**
160
+ * Parses the text of `CellStates.text`. Commits cell by cell in text order; a malformed cell (or one the ceiling
161
+ * refuses, or one that repeats) is skipped and reported in `errors`, never guessed.
162
+ */
163
+ export function decodeCellStates(text, options = {}) {
164
+ const store = new CellStates(options);
165
+ const errors = [];
166
+ const seen = new Set();
167
+ for (const chunk of text.split(';')) {
168
+ if (!chunk)
169
+ continue;
170
+ const m = /^([0-9a-z]+)\.([0-9a-z]+)\.([0-9a-z]+)\.([0-9a-z]+):([0-9a-z.,!~_]*)$/.exec(chunk);
171
+ if (!m) {
172
+ errors.push(`bad cell "${chunk.slice(0, 32)}"`);
173
+ continue;
174
+ }
175
+ let cell;
176
+ try {
177
+ cell = cellId(parseInt(m[1], 36), parseInt(m[2], 36), parseInt(m[3], 36) - XY_OFFSET, parseInt(m[4], 36) - XY_OFFSET);
178
+ }
179
+ catch {
180
+ errors.push(`bad cell id "${chunk.slice(0, 32)}"`);
181
+ continue;
182
+ }
183
+ const key = cellKey(cell);
184
+ if (seen.has(key)) {
185
+ errors.push(`cell ${key} repeats`);
186
+ continue;
187
+ }
188
+ seen.add(key);
189
+ const { states, errors: bad } = decodeStates(m[5]);
190
+ if (bad.length > 0) {
191
+ errors.push(`cell ${key}: ${bad.join('; ')}`);
192
+ continue;
193
+ }
194
+ if (!store.commit(cell, states).accepted)
195
+ errors.push(`cell ${key}: over the ceiling of ${store.maxUnits} units`);
196
+ }
197
+ return { store, errors };
198
+ }