wallhammer-web 0.0.0-stage → 0.4.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 (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -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,1083 @@
1
+ // Entities that move along paths: speed up, cruise, slow down for lower limits ahead, stop at stops and dwell there,
2
+ // loop or stop at the end. One dense table per mover set, integer millimetres and ticks throughout, and a system
3
+ // that is a plain function. Arrivals and departures go to the event queue:
4
+ //
5
+ // move.arrive { path, stop } reached stop `stop` (index along the path) and dwells
6
+ // move.depart { path, stop } left that stop
7
+ // move.end { path } reached the end of an open path and stands
8
+ //
9
+ // Collision is a parameter of the set:
10
+ // - `none`: entities pass through each other.
11
+ // - `follow`: keep a distance to the entity ahead on the same lane, looking one segment ahead.
12
+ // - `static`: no interaction between entities, but obstacles (`addObstacle`), known moving obstacles
13
+ // (`setKnownObstacles`) and shared areas of an `Interlock` stop them.
14
+ // - `lanes`: `follow`, plus conflict zones where paths of different lanes come close (`buildZones`): a zone is held
15
+ // by one entity at a time, granted in a fixed order and only when there is room behind it, plus everything of
16
+ // `static`. For vehicles on a network.
17
+ // - `agents`: local avoidance for crowds on shared ways (a spatial hash over integer positions: slow down behind
18
+ // someone, step aside for oncoming and standing ones, push apart when standing), plus everything of `static`.
19
+ //
20
+ // Every limit is computed from the state at the start of the tick, so the order rows are updated in does not matter.
21
+ // `none` and `follow` keep the snapshot layout they always had; the other modes add columns and sections.
22
+ import { ByteReader, ByteWriter } from './bytes.js';
23
+ import { approachSpeed, brakeSpeed, isqrt, mm, mmPerTick, mmPerTick2, stoppingDistance } from './fixed.js';
24
+ import { PathSet } from './paths.js';
25
+ import { EntityTable } from './table.js';
26
+ import { buildZoneIntervals } from './zones.js';
27
+ export const MOVER_STATE = { moving: 0, dwelling: 1, done: 2 };
28
+ const MOVER_COLUMNS = {
29
+ path: 'i32',
30
+ seg: 'i32',
31
+ /** Distance along the path in mm, and its value one tick earlier (unwrapped, for interpolation). */
32
+ dist: 'i32',
33
+ prev: 'i32',
34
+ /** Speed now and wanted, mm per tick; acceleration and braking, mm per tick². */
35
+ speed: 'i32',
36
+ cruise: 'i32',
37
+ accel: 'i32',
38
+ decel: 'i32',
39
+ /** Distance to keep to the entity ahead, mm (centre to centre). */
40
+ gap: 'i32',
41
+ /** Index of the next stop along the path (equal to the number of stops: the first stop of the next lap). */
42
+ stop: 'i32',
43
+ dwell: 'i32',
44
+ state: 'u8',
45
+ /** Free integer for the app (kind, colour, group). */
46
+ tag: 'i32',
47
+ };
48
+ /** Columns of the modes `static`, `lanes` and `agents`. */
49
+ const EXTRA_COLUMNS = {
50
+ /** Body reach ahead of and behind the anchor, and half the width, mm. */
51
+ front: 'i32',
52
+ back: 'i32',
53
+ half: 'i32',
54
+ /** Offset from the path point (agents), mm, now and one tick earlier. */
55
+ ox: 'i32',
56
+ oz: 'i32',
57
+ pox: 'i32',
58
+ poz: 'i32',
59
+ /** Ticks spent waiting for a zone or an area (older requests are granted first). */
60
+ wait: 'i32',
61
+ /** Agents: ticks standing still while meant to walk; after a while standing and oncoming ones no longer stop it. */
62
+ stuck: 'i32',
63
+ };
64
+ /** Floats per entity written by `extract`: x, z, direction x, direction z, metres along the path, speed m/s. */
65
+ export const TRANSFORM_STRIDE = 6;
66
+ /**
67
+ * Floats per entity written by `extractPose`: the six of `extract`, then the turn rate in rad/s (positive turns
68
+ * right, clockwise seen from above) and the state (`MOVER_STATE`). Enough to animate a rig.
69
+ */
70
+ export const POSE_STRIDE = 8;
71
+ /** Direction is taken over this distance around the position (mm), so corners turn smoothly. */
72
+ const LOOK_MM = 800;
73
+ /** Grid cell of the static obstacle lookup, mm. */
74
+ const OBSTACLE_CELL = 8000;
75
+ /** Sampling step along a path when testing it against obstacles, mm. */
76
+ const SWEEP_MM = 250;
77
+ /** Distance kept before a blocked point, a zone or an area, mm. */
78
+ const HOLD_MM = 500;
79
+ const NO_LIMIT = 0x7fffffff;
80
+ /** Agents: neighbourhood radius, lateral corridor, sideways speed. */
81
+ const AGENT_CELL = 2000;
82
+ const AGENT_LOOK = 1800;
83
+ const AGENT_CORRIDOR = 650;
84
+ /** Neighbours closer sideways than this block the way; the side step goes this far out. */
85
+ const AGENT_BLOCK = 480;
86
+ const AGENT_STEP = 760;
87
+ /** Seconds of standing still after which an agent squeezes past standing and oncoming ones (no gridlock). */
88
+ const AGENT_PATIENCE_S = 3;
89
+ const AGENT_SIDE_MM_S = 600;
90
+ /** Standing agents are pushed apart until this far from each other, and stay within this radius of their point. */
91
+ const AGENT_STAND_GAP = 560;
92
+ const AGENT_STAND_RADIUS = 2000;
93
+ /** Collision features per mode. */
94
+ const F_FOLLOW = 1;
95
+ const F_ZONES = 2;
96
+ const F_OBSTACLES = 4;
97
+ const F_AGENTS = 8;
98
+ const FEATURES = {
99
+ none: 0,
100
+ follow: F_FOLLOW,
101
+ static: F_OBSTACLES,
102
+ lanes: F_FOLLOW | F_ZONES | F_OBSTACLES,
103
+ agents: F_AGENTS | F_OBSTACLES,
104
+ };
105
+ export class Movers {
106
+ name;
107
+ hz;
108
+ collision;
109
+ table;
110
+ paths;
111
+ features;
112
+ extended;
113
+ defaultHalf;
114
+ follow = new Int32Array(0);
115
+ order = [];
116
+ point = [0, 0];
117
+ point2 = [0, 0];
118
+ frame = new Float64Array(5);
119
+ /** Static obstacles: flat capsules in mm (ax, az, bx, bz, radius). Simulation state. */
120
+ obstacles = [];
121
+ /** Obstacle indices by grid cell (not state: rebuilt when the obstacles change). */
122
+ obstacleGrid = null;
123
+ cache = new Map();
124
+ /** Conflict zones (`lanes`): intervals per path and the entity holding each zone (0 = free). */
125
+ zones = { byPath: new Map(), count: 0 };
126
+ holder = new Float64Array(0);
127
+ known = null;
128
+ /** Shared areas with another set. */
129
+ interlock = null;
130
+ /** Lane groups of the last follow pass: lane → rows sorted by position on it. */
131
+ laneRows = new Map();
132
+ lanePos = new Int32Array(0);
133
+ constructor(name, options) {
134
+ this.name = name;
135
+ this.hz = options.hz;
136
+ this.collision = options.collision ?? 'none';
137
+ if (!(this.collision in FEATURES))
138
+ throw new Error(`Movers: unknown collision "${this.collision}"`);
139
+ this.features = FEATURES[this.collision];
140
+ this.extended = this.collision !== 'none' && this.collision !== 'follow';
141
+ this.defaultHalf = mm((options.width ?? 1.8) / 2);
142
+ const spec = this.extended ? { ...MOVER_COLUMNS, ...EXTRA_COLUMNS } : MOVER_COLUMNS;
143
+ this.table = new EntityTable(name, spec, options.namespace !== undefined ? { namespace: options.namespace } : {});
144
+ this.paths = new PathSet(`${name}.paths`, this.hz);
145
+ }
146
+ get count() {
147
+ return this.table.count;
148
+ }
149
+ /** Default half width of the entities, mm. */
150
+ get halfWidth() {
151
+ return this.defaultHalf;
152
+ }
153
+ get ext() {
154
+ return this.table.col;
155
+ }
156
+ /** Adds an entity on a path and returns its id. */
157
+ spawn(o) {
158
+ const id = this.table.create();
159
+ const row = this.table.rowOf(id);
160
+ const c = this.table.col;
161
+ c.cruise[row] = mmPerTick(o.cruise, this.hz);
162
+ c.accel[row] = mmPerTick2(o.accel ?? 1, this.hz);
163
+ c.decel[row] = mmPerTick2(o.decel ?? 2, this.hz);
164
+ if (c.decel[row] === 0)
165
+ throw new Error('Movers: braking must be positive');
166
+ c.gap[row] = mm(o.gap ?? 2);
167
+ c.tag[row] = o.tag ?? 0;
168
+ if (this.extended) {
169
+ const e = this.ext;
170
+ e.front[row] = mm(o.front ?? (o.gap ?? 2) / 4);
171
+ e.back[row] = mm(o.back ?? (o.gap ?? 2) / 4);
172
+ e.half[row] = o.width !== undefined ? mm(o.width / 2) : this.defaultHalf;
173
+ }
174
+ this.place(row, o.path, mm(o.at ?? 0));
175
+ c.speed[row] = Math.min(c.cruise[row], mmPerTick(o.speed ?? 0, this.hz));
176
+ return id;
177
+ }
178
+ /** Moves an entity onto another path (no interpolation across the switch). An owned path it leaves is freed. */
179
+ setPath(id, path, at = 0) {
180
+ const row = this.mustRow(id);
181
+ const old = this.table.col.path[row];
182
+ this.place(row, path, mm(at));
183
+ if (old !== path)
184
+ this.releaseIfOwned(old);
185
+ }
186
+ /** Adds a path owned by this entity (freed when it leaves it) and moves the entity onto it. */
187
+ send(id, def, at = 0) {
188
+ const path = this.paths.add(def, { owned: true });
189
+ this.setPath(id, path, at);
190
+ return path;
191
+ }
192
+ remove(id) {
193
+ const row = this.mustRow(id);
194
+ const old = this.table.col.path[row];
195
+ this.table.remove(id);
196
+ this.releaseIfOwned(old);
197
+ }
198
+ /** State of one entity in app units. */
199
+ info(id) {
200
+ const row = this.mustRow(id);
201
+ const c = this.table.col;
202
+ return { state: c.state[row], path: c.path[row], along: c.dist[row] / 1000, speed: (c.speed[row] * this.hz) / 1000, tag: c.tag[row] };
203
+ }
204
+ /**
205
+ * Ticks an entity has waited in a row for a zone, an area or an obstacle (0 when it moves freely or only follows the
206
+ * one ahead). Extended modes only. A ring of queues that all wait on each other never clears by itself; the caller
207
+ * can watch this and move one of them (`setPath`) to break it.
208
+ */
209
+ waited(id) {
210
+ this.requireExtended('waited');
211
+ return this.ext.wait[this.mustRow(id)];
212
+ }
213
+ /** Where an entity is now (at the last tick, with its offset), metres. */
214
+ position(id) {
215
+ const row = this.mustRow(id);
216
+ const c = this.table.col;
217
+ const pt = [0, 0];
218
+ this.paths.pointAt(this.paths.get(c.path[row]), c.dist[row], pt);
219
+ if (this.features & F_AGENTS) {
220
+ const e = this.ext;
221
+ pt[0] += e.ox[row] / 1000;
222
+ pt[1] += e.oz[row] / 1000;
223
+ }
224
+ return pt;
225
+ }
226
+ mustRow(id) {
227
+ const row = this.table.rowOf(id);
228
+ if (row < 0)
229
+ throw new Error(`Movers: no entity ${id}`);
230
+ return row;
231
+ }
232
+ releaseIfOwned(path) {
233
+ if (!this.paths.has(path) || !this.paths.get(path).owned)
234
+ return;
235
+ const c = this.table.col;
236
+ for (let r = 0; r < this.table.count; r++)
237
+ if (c.path[r] === path)
238
+ return;
239
+ this.paths.release(path);
240
+ this.cache.delete(path);
241
+ this.interlock?.il.forget(this.interlock.side, path);
242
+ }
243
+ place(row, pathId, at) {
244
+ const p = this.paths.get(pathId);
245
+ const c = this.table.col;
246
+ const d = p.loop ? ((at % p.length) + p.length) % p.length : Math.min(p.length, Math.max(0, at));
247
+ c.path[row] = pathId;
248
+ c.dist[row] = d;
249
+ c.prev[row] = d;
250
+ c.speed[row] = 0;
251
+ c.dwell[row] = 0;
252
+ c.state[row] = MOVER_STATE.moving;
253
+ let seg = 0;
254
+ while (seg + 1 < p.segments && d >= p.segStart[seg + 1])
255
+ seg++;
256
+ c.seg[row] = seg;
257
+ // Stops at the starting point are behind the entity.
258
+ let k = 0;
259
+ while (k < p.stopAt.length && p.stopAt[k] <= d)
260
+ k++;
261
+ c.stop[row] = k;
262
+ if (this.extended) {
263
+ const e = this.ext;
264
+ e.wait[row] = 0;
265
+ // The offset is kept across a path switch (an agent steps on from where it stood); `pox` follows it.
266
+ e.pox[row] = e.ox[row];
267
+ e.poz[row] = e.oz[row];
268
+ }
269
+ }
270
+ // --- Obstacles, zones, known obstacles ---------------------------------------------------------------------------
271
+ requireExtended(what) {
272
+ if (!this.extended)
273
+ throw new Error(`Movers: ${what} needs collision "static", "lanes" or "agents" (this set is "${this.collision}")`);
274
+ }
275
+ /** Adds a static obstacle (a capsule in metres) and returns its index. Entities stop before it. */
276
+ addObstacle(o) {
277
+ this.requireExtended('addObstacle');
278
+ this.obstacles.push(mm(o.ax), mm(o.az), mm(o.bx), mm(o.bz), mm(o.radius));
279
+ this.cache.clear();
280
+ this.obstacleGrid = null;
281
+ return this.obstacles.length / 5 - 1;
282
+ }
283
+ get obstacleCount() {
284
+ return this.obstacles.length / 5;
285
+ }
286
+ /**
287
+ * Stretches of a path that an entity of the default width cannot pass because of static obstacles, metres along
288
+ * the path. Meant for build time: a path that is blocked is better not used at all, since an entity would stand
289
+ * before the obstacle for ever and everybody behind it too.
290
+ */
291
+ blocked(path) {
292
+ const pc = this.pathCache(this.paths.get(path));
293
+ return Array.from(pc.block0, (s, i) => ({ from: s / 1000, to: pc.block1[i] / 1000 }));
294
+ }
295
+ /** Obstacles by grid cell: every cell a capsule (swollen by the default half width) touches lists its offset. */
296
+ obstacleCells() {
297
+ if (this.obstacleGrid)
298
+ return this.obstacleGrid;
299
+ const grid = new Map();
300
+ const ob = this.obstacles;
301
+ for (let k = 0; k < ob.length; k += 5) {
302
+ const r = ob[k + 4] + this.defaultHalf;
303
+ for (let cx = Math.floor((Math.min(ob[k], ob[k + 2]) - r) / OBSTACLE_CELL); cx <= Math.floor((Math.max(ob[k], ob[k + 2]) + r) / OBSTACLE_CELL); cx++) {
304
+ for (let cz = Math.floor((Math.min(ob[k + 1], ob[k + 3]) - r) / OBSTACLE_CELL); cz <= Math.floor((Math.max(ob[k + 1], ob[k + 3]) + r) / OBSTACLE_CELL); cz++) {
305
+ const key = cellKey(cx, cz);
306
+ const list = grid.get(key);
307
+ if (list)
308
+ list.push(k);
309
+ else
310
+ grid.set(key, [k]);
311
+ }
312
+ }
313
+ }
314
+ this.obstacleGrid = grid;
315
+ return grid;
316
+ }
317
+ pathCache(p) {
318
+ let pc = this.cache.get(p.id);
319
+ if (pc)
320
+ return pc;
321
+ const b0 = [];
322
+ const b1 = [];
323
+ const ob = this.obstacles;
324
+ if (ob.length > 0) {
325
+ const grid = this.obstacleCells();
326
+ const pt = this.point;
327
+ let inside = false;
328
+ for (let d = 0; d <= p.length; d += SWEEP_MM) {
329
+ this.paths.pointAt(p, d, pt);
330
+ const x = Math.round(pt[0] * 1000);
331
+ const z = Math.round(pt[1] * 1000);
332
+ let hit = false;
333
+ for (const k of grid.get(cellKey(Math.floor(x / OBSTACLE_CELL), Math.floor(z / OBSTACLE_CELL))) ?? []) {
334
+ const r = ob[k + 4] + this.defaultHalf;
335
+ if (capsuleDist2(x, z, ob[k], ob[k + 1], ob[k + 2], ob[k + 3]) < r * r) {
336
+ hit = true;
337
+ break;
338
+ }
339
+ }
340
+ if (hit && !inside)
341
+ b0.push(d);
342
+ if (!hit && inside)
343
+ b1.push(d);
344
+ inside = hit;
345
+ }
346
+ if (inside)
347
+ b1.push(p.length);
348
+ }
349
+ pc = { block0: Int32Array.from(b0), block1: Int32Array.from(b1) };
350
+ this.cache.set(p.id, pc);
351
+ return pc;
352
+ }
353
+ /**
354
+ * Builds the conflict zones of the `lanes` mode from the paths in the set now: where segments of different lanes
355
+ * come closer than `clearance` metres, the stretches on both become one zone. Segments running side by side
356
+ * (within about 18 degrees of parallel or antiparallel, e.g. the two directions of a street) only conflict closer
357
+ * than `parallelClearance`. Returns the number of zones. Call it after adding the network's paths and before the
358
+ * first tick; paths added later have no zones. Defaults 3 and 2 m (bodies about 1.8 m wide; a rigid body cuts the
359
+ * inside of a corner, hence the margin).
360
+ */
361
+ buildZones(options = {}) {
362
+ if ((this.features & F_ZONES) === 0)
363
+ throw new Error('Movers: zones need collision "lanes"');
364
+ const list = [];
365
+ for (let id = 1; list.length < this.paths.size; id++)
366
+ if (this.paths.has(id))
367
+ list.push(this.paths.get(id));
368
+ this.zones = buildZoneIntervals(list.filter((p) => !p.owned), mm(options.clearance ?? 3), mm(options.parallelClearance ?? 2));
369
+ this.holder = new Float64Array(this.zones.count);
370
+ return this.zones.count;
371
+ }
372
+ get zoneCount() {
373
+ return this.zones.count;
374
+ }
375
+ /** Zone stretches of a path, metres along it. */
376
+ zonesOf(path) {
377
+ const z = this.zones.byPath.get(path);
378
+ if (!z)
379
+ return [];
380
+ return Array.from(z.s0, (s, i) => ({ from: s / 1000, to: z.s1[i] / 1000, zone: z.zone[i] }));
381
+ }
382
+ /** Known moving obstacles (a replayed schedule). Not state: set it again after setting the simulation up anew. */
383
+ setKnownObstacles(fn) {
384
+ this.requireExtended('setKnownObstacles');
385
+ this.known = fn;
386
+ }
387
+ /** Body of an entity along its path: rear and front distance (mm), and half the width. For `Interlock`. */
388
+ body(row) {
389
+ const c = this.table.col;
390
+ if (!this.extended)
391
+ return [c.dist[row], c.dist[row], this.defaultHalf];
392
+ const e = this.ext;
393
+ return [c.dist[row] - e.back[row], c.dist[row] + e.front[row], e.half[row]];
394
+ }
395
+ // --- The system ----------------------------------------------------------------------------------------------------
396
+ /** The system: one tick of movement. Register it with `sim.addSystem(name, movers.system)`. */
397
+ system = (sim) => {
398
+ const t = this.table;
399
+ const n = t.count;
400
+ const c = t.col;
401
+ if (this.follow.length < n)
402
+ this.follow = new Int32Array(Math.max(n, this.follow.length * 2));
403
+ if (this.features & F_FOLLOW)
404
+ this.computeFollow();
405
+ else
406
+ this.follow.fill(NO_LIMIT, 0, n);
407
+ if (this.extended)
408
+ this.limits(sim);
409
+ for (let r = 0; r < n; r++) {
410
+ c.prev[r] = c.dist[r];
411
+ const state = c.state[r];
412
+ if (state === MOVER_STATE.done) {
413
+ c.speed[r] = 0;
414
+ continue;
415
+ }
416
+ const p = this.paths.get(c.path[r]);
417
+ if (state === MOVER_STATE.dwelling) {
418
+ c.dwell[r] = c.dwell[r] - 1;
419
+ if (c.dwell[r] <= 0) {
420
+ c.state[r] = MOVER_STATE.moving;
421
+ sim.events.post('move.depart', t.idAt(r), { path: p.id, stop: c.stop[r] });
422
+ c.stop[r] = c.stop[r] + 1;
423
+ }
424
+ continue;
425
+ }
426
+ this.move(sim, r, p);
427
+ }
428
+ };
429
+ move(sim, r, p) {
430
+ const c = this.table.col;
431
+ const dist = c.dist[r];
432
+ const decel = c.decel[r];
433
+ const len = p.length;
434
+ const nStops = p.stopAt.length;
435
+ const k = c.stop[r];
436
+ // Where the entity must stand still next: a stop, or the end of an open path.
437
+ let hard = Infinity;
438
+ if (k < nStops)
439
+ hard = p.stopAt[k] - dist;
440
+ else if (p.loop && nStops > 0)
441
+ hard = p.stopAt[0] + len - dist;
442
+ else if (!p.loop)
443
+ hard = len - dist;
444
+ let target = c.cruise[r];
445
+ const seg = c.seg[r];
446
+ if (p.segLimit[seg] > 0)
447
+ target = Math.min(target, p.segLimit[seg]);
448
+ // Lower limits ahead, within the distance needed to brake from the current target.
449
+ const horizon = stoppingDistance(target, decel) + target;
450
+ for (let i = 1; i < p.segments; i++) {
451
+ let j = seg + i;
452
+ let start;
453
+ if (j < p.segments)
454
+ start = p.segStart[j];
455
+ else if (p.loop) {
456
+ j -= p.segments;
457
+ start = p.segStart[j] + len;
458
+ }
459
+ else
460
+ break;
461
+ const ahead = start - dist;
462
+ if (ahead > horizon)
463
+ break;
464
+ const lim = p.segLimit[j];
465
+ if (lim > 0)
466
+ target = Math.min(target, approachSpeed(lim, ahead, decel));
467
+ }
468
+ if (hard !== Infinity)
469
+ target = Math.min(target, brakeSpeed(hard, decel));
470
+ const free = this.follow[r];
471
+ if (free < NO_LIMIT)
472
+ target = Math.min(target, brakeSpeed(free, decel));
473
+ const v = c.speed[r];
474
+ let nv = Math.min(target, v + c.accel[r]);
475
+ nv = Math.max(nv, v - decel);
476
+ nv = Math.max(0, Math.min(nv, hard, free));
477
+ let d = dist + nv;
478
+ c.speed[r] = nv;
479
+ const arrived = hard !== Infinity && nv === hard;
480
+ let stop = k;
481
+ if (p.loop && d >= len) {
482
+ d -= len;
483
+ c.prev[r] = c.prev[r] - len;
484
+ if (stop >= nStops)
485
+ stop = 0;
486
+ c.seg[r] = 0;
487
+ }
488
+ let s = c.seg[r];
489
+ while (s + 1 < p.segments && d >= p.segStart[s + 1])
490
+ s++;
491
+ c.seg[r] = s;
492
+ c.dist[r] = d;
493
+ c.stop[r] = stop;
494
+ if (!arrived)
495
+ return;
496
+ c.speed[r] = 0;
497
+ const id = this.table.idAt(r);
498
+ if (stop < nStops) {
499
+ c.state[r] = MOVER_STATE.dwelling;
500
+ c.dwell[r] = p.stopDwell[stop];
501
+ sim.events.post('move.arrive', id, { path: p.id, stop });
502
+ }
503
+ else {
504
+ c.state[r] = MOVER_STATE.done;
505
+ sim.events.post('move.end', id, { path: p.id });
506
+ }
507
+ }
508
+ /** Free distance (mm) each entity may cover before it gets closer than its gap to the entity ahead on its lane. */
509
+ computeFollow() {
510
+ const t = this.table;
511
+ const n = t.count;
512
+ const c = t.col;
513
+ const lane = new Float64Array(n);
514
+ if (this.lanePos.length < n)
515
+ this.lanePos = new Int32Array(Math.max(n, this.lanePos.length * 2));
516
+ const pos = this.lanePos;
517
+ const order = this.order;
518
+ order.length = 0;
519
+ for (let r = 0; r < n; r++) {
520
+ const p = this.paths.get(c.path[r]);
521
+ const s = c.seg[r];
522
+ lane[r] = p.segLane[s];
523
+ pos[r] = p.segOrigin[s] + c.dist[r] - p.segStart[s];
524
+ order.push(r);
525
+ }
526
+ // Sorted by lane, then position, then id: a total order that does not depend on row order.
527
+ order.sort((a, b) => lane[a] - lane[b] || pos[a] - pos[b] || t.idAt(a) - t.idAt(b));
528
+ const firstOnLane = new Map();
529
+ for (let i = n - 1; i >= 0; i--)
530
+ firstOnLane.set(lane[order[i]], i);
531
+ if (this.extended) {
532
+ this.laneRows.clear();
533
+ for (let i = 0; i < n; i++) {
534
+ const r = order[i];
535
+ const l = lane[r];
536
+ const list = this.laneRows.get(l);
537
+ if (list)
538
+ list.push(r);
539
+ else
540
+ this.laneRows.set(l, [r]);
541
+ }
542
+ }
543
+ for (let i = 0; i < n; i++) {
544
+ const r = order[i];
545
+ let gapMm = Infinity;
546
+ const next = order[i + 1];
547
+ if (next !== undefined && lane[next] === lane[r])
548
+ gapMm = pos[next] - pos[r];
549
+ else {
550
+ // Nobody ahead on this lane: look at the start of the next segment's lane.
551
+ const p = this.paths.get(c.path[r]);
552
+ const s = c.seg[r];
553
+ const ns = s + 1 < p.segments ? s + 1 : p.loop ? 0 : -1;
554
+ if (ns >= 0) {
555
+ const j = firstOnLane.get(p.segLane[ns]);
556
+ if (j !== undefined && order[j] !== r)
557
+ gapMm = (s + 1 < p.segments ? p.segStart[s + 1] : p.length) - c.dist[r] + pos[order[j]] - p.segOrigin[ns];
558
+ }
559
+ }
560
+ this.follow[r] = gapMm === Infinity ? NO_LIMIT : Math.max(0, gapMm - c.gap[r]);
561
+ }
562
+ }
563
+ /** Is anybody but `self` anchored on `lane` between `from` and `to` (mm from the segment start)? */
564
+ laneTaken(lane, from, to, self) {
565
+ const rows = this.laneRows.get(lane);
566
+ if (!rows)
567
+ return false;
568
+ const pos = this.lanePos;
569
+ for (const r of rows) {
570
+ if (r === self)
571
+ continue;
572
+ const q = pos[r];
573
+ if (q >= from && q <= to)
574
+ return true;
575
+ if (q > to)
576
+ break;
577
+ }
578
+ return false;
579
+ }
580
+ /** Limits of the extended modes, folded into `follow`: obstacles, known obstacles, zones, areas, agents. */
581
+ limits(sim) {
582
+ const n = this.table.count;
583
+ const c = this.table.col;
584
+ const e = this.ext;
585
+ if (this.features & F_AGENTS)
586
+ this.agents();
587
+ // Known moving obstacles of this tick, quantised to mm.
588
+ let known = null;
589
+ if (this.known) {
590
+ const k = this.known(sim.tick);
591
+ if (k.length > 0)
592
+ known = Int32Array.from(k, (v) => Math.round(v * 1000));
593
+ }
594
+ const zoneReq = this.features & F_ZONES && this.zones.count > 0 ? this.zoneRequests(known) : null;
595
+ const il = this.interlock;
596
+ for (let r = 0; r < n; r++) {
597
+ if (c.state[r] !== MOVER_STATE.moving)
598
+ continue;
599
+ const p = this.paths.get(c.path[r]);
600
+ const dist = c.dist[r];
601
+ const front = dist + e.front[r];
602
+ const reach = stoppingDistance(Math.max(c.cruise[r], c.speed[r]), c.decel[r]) + c.cruise[r] + HOLD_MM;
603
+ let free = this.follow[r];
604
+ // Static obstacles: the first blocked stretch ahead that the body is not in already.
605
+ if (this.obstacles.length > 0) {
606
+ const pc = this.pathCache(p);
607
+ for (let i = 0; i < pc.block0.length; i++) {
608
+ let s0 = pc.block0[i];
609
+ let s1 = pc.block1[i];
610
+ if (p.loop && s1 < dist - e.back[r]) {
611
+ s0 += p.length;
612
+ s1 += p.length;
613
+ }
614
+ if (s1 < dist - e.back[r])
615
+ continue;
616
+ if (s0 <= front)
617
+ continue;
618
+ free = Math.min(free, Math.max(0, s0 - front - HOLD_MM));
619
+ break;
620
+ }
621
+ }
622
+ if (known) {
623
+ // Inside a held zone the entity clears it (it entered only with the way clear); known obstacles count after.
624
+ const from = zoneReq ? Math.max(front, zoneReq.clearTo[r]) : front;
625
+ const hit = this.sweep(p, r, from, front + reach, known);
626
+ if (hit >= 0)
627
+ free = Math.min(free, Math.max(0, hit - front - HOLD_MM));
628
+ }
629
+ if (zoneReq) {
630
+ const z = zoneReq.limit[r];
631
+ if (z < NO_LIMIT)
632
+ free = Math.min(free, z);
633
+ }
634
+ if (il) {
635
+ const a = il.il.limitFor(this, il.side, r, p, reach);
636
+ if (a < NO_LIMIT)
637
+ free = Math.min(free, a);
638
+ }
639
+ if (free < this.follow[r] && free < reach)
640
+ e.wait[r] = e.wait[r] + 1;
641
+ else
642
+ e.wait[r] = 0;
643
+ this.follow[r] = free;
644
+ }
645
+ }
646
+ /**
647
+ * First distance along the path in [from, to] (mm) where the body of row `r` would touch one of the capsules, or
648
+ * -1. Capsules the body is in already are left out, so an entity inside one can leave it.
649
+ */
650
+ sweep(p, r, from, to, caps) {
651
+ const half = this.ext.half[r];
652
+ const pt = this.point;
653
+ const c = this.table.col;
654
+ this.paths.pointAt(p, c.dist[r], pt);
655
+ const x0 = Math.round(pt[0] * 1000);
656
+ const z0 = Math.round(pt[1] * 1000);
657
+ const span = to - c.dist[r] + 1000;
658
+ let best = -1;
659
+ for (let k = 0; k < caps.length; k += 5) {
660
+ const rad = caps[k + 4] + half;
661
+ // Far away: skip without sampling.
662
+ const bx0 = Math.min(caps[k], caps[k + 2]) - rad - span;
663
+ const bx1 = Math.max(caps[k], caps[k + 2]) + rad + span;
664
+ const bz0 = Math.min(caps[k + 1], caps[k + 3]) - rad - span;
665
+ const bz1 = Math.max(caps[k + 1], caps[k + 3]) + rad + span;
666
+ if (x0 < bx0 || x0 > bx1 || z0 < bz0 || z0 > bz1)
667
+ continue;
668
+ const r2 = rad * rad;
669
+ // Already inside: free to leave.
670
+ if (capsuleDist2(x0, z0, caps[k], caps[k + 1], caps[k + 2], caps[k + 3]) < r2)
671
+ continue;
672
+ const limit = best >= 0 ? best : to;
673
+ for (let d = from; d <= limit; d += SWEEP_MM) {
674
+ this.paths.pointAt(p, d, pt);
675
+ if (capsuleDist2(Math.round(pt[0] * 1000), Math.round(pt[1] * 1000), caps[k], caps[k + 1], caps[k + 2], caps[k + 3]) < r2) {
676
+ best = d;
677
+ break;
678
+ }
679
+ }
680
+ }
681
+ return best;
682
+ }
683
+ /**
684
+ * Zones: who holds what, and how far everybody else may go. A holder keeps its zone while the zone's stretch is
685
+ * still ahead or under its body; then the zone is free. A free zone goes to the requester that has waited longest
686
+ * (then the lowest id), and only when the lane behind the zone has room for it (it must not stop inside).
687
+ */
688
+ zoneRequests(known) {
689
+ const n = this.table.count;
690
+ const c = this.table.col;
691
+ const e = this.ext;
692
+ const t = this.table;
693
+ const limit = new Int32Array(n).fill(NO_LIMIT);
694
+ // End of the held stretch under each body (mm along its path), or -1.
695
+ const clearTo = new Int32Array(n).fill(-1);
696
+ const holder = this.holder;
697
+ const wants = new Uint8Array(this.zones.count);
698
+ // Per row: the first zone stretch it is in or approaching within its reach.
699
+ const reqZone = new Int32Array(n).fill(-1);
700
+ const reqAt = new Int32Array(n);
701
+ const reqEnd = new Int32Array(n);
702
+ // Per zone: one that is in it without holding it (it followed the holder in), lowest id.
703
+ const inside = new Float64Array(this.zones.count);
704
+ for (let r = 0; r < n; r++) {
705
+ const zi = this.zones.byPath.get(c.path[r]);
706
+ if (!zi || c.state[r] === MOVER_STATE.done)
707
+ continue;
708
+ const p = this.paths.get(c.path[r]);
709
+ const dist = c.dist[r];
710
+ const back = dist - e.back[r];
711
+ const front = dist + e.front[r];
712
+ const reach = stoppingDistance(Math.max(c.cruise[r], c.speed[r]), c.decel[r]) + c.cruise[r] + HOLD_MM;
713
+ const id = t.idAt(r);
714
+ const m = zi.s0.length;
715
+ // Stretches under the body: their zones are held (taken if free, kept if held by this row). Then the first
716
+ // stretch ahead within reach is requested.
717
+ let done = false;
718
+ for (let lap = 0; lap < (p.loop ? 2 : 1) && !done; lap++) {
719
+ const off = lap * p.length;
720
+ for (let i = 0; i < m; i++) {
721
+ const s0 = zi.s0[i] + off;
722
+ const s1 = zi.s1[i] + off;
723
+ if (s1 < back)
724
+ continue;
725
+ const z = zi.zone[i];
726
+ if (s0 <= front) {
727
+ wants[z] = 1;
728
+ if (holder[z] === 0)
729
+ holder[z] = id;
730
+ if (holder[z] === id)
731
+ clearTo[r] = Math.max(clearTo[r], s1);
732
+ else if (inside[z] === 0 || id < inside[z])
733
+ inside[z] = id;
734
+ continue;
735
+ }
736
+ done = true;
737
+ if (s0 - front > reach)
738
+ break;
739
+ // Only the first in its queue asks: somebody between it and the zone on its lane goes first (a zone held by
740
+ // one stuck behind a waiting one would block both).
741
+ if (this.follow[r] < s0 - front - HOLD_MM)
742
+ break;
743
+ reqZone[r] = z;
744
+ reqAt[r] = s0 - front;
745
+ reqEnd[r] = s1;
746
+ break;
747
+ }
748
+ }
749
+ if (reqZone[r] >= 0 && holder[reqZone[r]] === id)
750
+ wants[reqZone[r]] = 1;
751
+ }
752
+ // Free zones whose holder neither is in them nor heads for them.
753
+ for (let z = 0; z < holder.length; z++) {
754
+ const h = holder[z];
755
+ if (h !== 0 && (!wants[z] || t.rowOf(h) < 0))
756
+ holder[z] = 0;
757
+ // A zone the holder has left passes to one still in it before anybody outside may ask.
758
+ if (holder[z] === 0 && inside[z] !== 0)
759
+ holder[z] = inside[z];
760
+ }
761
+ // Grants: requesters of free zones, longest wait first, then lowest id.
762
+ const req = [];
763
+ for (let r = 0; r < n; r++)
764
+ if (reqZone[r] >= 0 && holder[reqZone[r]] === 0)
765
+ req.push(r);
766
+ req.sort((a, b) => e.wait[b] - e.wait[a] || t.idAt(a) - t.idAt(b));
767
+ for (const r of req) {
768
+ const z = reqZone[r];
769
+ if (holder[z] !== 0)
770
+ continue;
771
+ if (!this.exitFree(r, reqEnd[r]))
772
+ continue;
773
+ // Known moving obstacles: enter only when the whole stretch can be cleared before they come.
774
+ if (known && this.sweep(this.paths.get(c.path[r]), r, c.dist[r] + e.front[r], reqEnd[r] + e.back[r], known) >= 0)
775
+ continue;
776
+ holder[z] = t.idAt(r);
777
+ }
778
+ for (let r = 0; r < n; r++) {
779
+ const z = reqZone[r];
780
+ if (z >= 0 && holder[z] !== t.idAt(r))
781
+ limit[r] = Math.max(0, reqAt[r] - HOLD_MM);
782
+ }
783
+ return { limit, clearTo };
784
+ }
785
+ /** Room on the lane right behind a zone stretch ending at `s1` (mm along the row's path) for the whole body. */
786
+ exitFree(r, s1) {
787
+ const c = this.table.col;
788
+ const e = this.ext;
789
+ const p = this.paths.get(c.path[r]);
790
+ let at = s1;
791
+ if (p.loop)
792
+ at = ((at % p.length) + p.length) % p.length;
793
+ else if (at >= p.length)
794
+ return true;
795
+ let seg = 0;
796
+ while (seg + 1 < p.segments && at >= p.segStart[seg + 1])
797
+ seg++;
798
+ const base = p.segOrigin[seg] + at - p.segStart[seg];
799
+ const from = base - e.back[r];
800
+ const to = base + c.gap[r] + e.front[r];
801
+ return !this.laneTaken(p.segLane[seg], from, to, r);
802
+ }
803
+ /**
804
+ * Agents: local avoidance on integer positions. Neighbours come from a spatial hash of 2 m cells; every decision
805
+ * uses the positions at the start of the tick (a Jacobi step), so the result does not depend on the row order.
806
+ */
807
+ agents() {
808
+ const n = this.table.count;
809
+ const c = this.table.col;
810
+ const e = this.ext;
811
+ const X = new Int32Array(n);
812
+ const Z = new Int32Array(n);
813
+ const DX = new Int32Array(n);
814
+ const DZ = new Int32Array(n);
815
+ const pt = this.point;
816
+ const pt2 = this.point2;
817
+ const cells = new Map();
818
+ for (let r = 0; r < n; r++) {
819
+ const p = this.paths.get(c.path[r]);
820
+ const d = c.dist[r];
821
+ this.paths.pointAt(p, d, pt);
822
+ X[r] = Math.round(pt[0] * 1000) + e.ox[r];
823
+ Z[r] = Math.round(pt[1] * 1000) + e.oz[r];
824
+ this.paths.pointAt(p, d - 400, pt);
825
+ this.paths.pointAt(p, d + 400, pt2);
826
+ const dx = pt2[0] - pt[0];
827
+ const dz = pt2[1] - pt[1];
828
+ const l = Math.sqrt(dx * dx + dz * dz);
829
+ DX[r] = l > 1e-6 ? Math.round((dx / l) * 1024) : 0;
830
+ DZ[r] = l > 1e-6 ? Math.round((dz / l) * 1024) : 1024;
831
+ const key = cellKey(Math.floor(X[r] / AGENT_CELL), Math.floor(Z[r] / AGENT_CELL));
832
+ const list = cells.get(key);
833
+ if (list)
834
+ list.push(r);
835
+ else
836
+ cells.set(key, [r]);
837
+ }
838
+ const side = Math.max(1, Math.round(AGENT_SIDE_MM_S / this.hz));
839
+ const patience = AGENT_PATIENCE_S * this.hz;
840
+ const nox = new Int32Array(n);
841
+ const noz = new Int32Array(n);
842
+ for (let r = 0; r < n; r++) {
843
+ const standing = c.state[r] !== MOVER_STATE.moving;
844
+ const cx = Math.floor(X[r] / AGENT_CELL);
845
+ const cz = Math.floor(Z[r] / AGENT_CELL);
846
+ // Right-hand normal of the walking direction: (−dz, dx).
847
+ const rx = -DZ[r];
848
+ const rz = DX[r];
849
+ // Votes for a side to step to (summed, so the neighbour order does not matter): + right, − left.
850
+ let vote = 0;
851
+ let free = NO_LIMIT;
852
+ let pushX = 0;
853
+ let pushZ = 0;
854
+ // Close to the end of an open path, standing ones do not stop it: it arrives and is pushed into the group.
855
+ const pr = this.paths.get(c.path[r]);
856
+ const arriving = !pr.loop && pr.length - c.dist[r] < AGENT_STAND_RADIUS;
857
+ if (!standing)
858
+ e.stuck[r] = c.speed[r] === 0 ? e.stuck[r] + 1 : 0;
859
+ const patient = e.stuck[r] < patience;
860
+ for (let gx = cx - 1; gx <= cx + 1; gx++) {
861
+ for (let gz = cz - 1; gz <= cz + 1; gz++) {
862
+ const list = cells.get(cellKey(gx, gz));
863
+ if (!list)
864
+ continue;
865
+ for (const j of list) {
866
+ if (j === r)
867
+ continue;
868
+ const qx = X[j] - X[r];
869
+ const qz = Z[j] - Z[r];
870
+ const d2 = qx * qx + qz * qz;
871
+ if (d2 > AGENT_LOOK * AGENT_LOOK)
872
+ continue;
873
+ // Too close: pushed apart (standing: both ways; walking: sideways only, below).
874
+ if (d2 < AGENT_STAND_GAP * AGENT_STAND_GAP) {
875
+ const d = Math.max(1, isqrt(d2));
876
+ const k = AGENT_STAND_GAP - d;
877
+ if (d2 === 0) {
878
+ // Same point: the lower id stays, the other steps to its right.
879
+ if (this.table.idAt(r) > this.table.idAt(j)) {
880
+ pushX += (rx * k) >> 11;
881
+ pushZ += (rz * k) >> 11;
882
+ }
883
+ }
884
+ else {
885
+ pushX -= Math.trunc((qx * k) / (2 * d));
886
+ pushZ -= Math.trunc((qz * k) / (2 * d));
887
+ }
888
+ }
889
+ if (standing)
890
+ continue;
891
+ const f = (qx * DX[r] + qz * DZ[r]) >> 10;
892
+ const s = (qx * rx + qz * rz) >> 10;
893
+ if (f <= 0 || f > AGENT_LOOK || Math.abs(s) > AGENT_CORRIDOR)
894
+ continue;
895
+ // Same direction: within about 30 degrees (cos 30° · 1024²).
896
+ const same = DX[r] * DX[j] + DZ[r] * DZ[j] > 908000;
897
+ const still = c.state[j] !== MOVER_STATE.moving;
898
+ if (still) {
899
+ // Standing: pass on the side away from it, slow down when close.
900
+ vote += s > 0 ? -2 : 2;
901
+ if (f < 700 && Math.abs(s) < AGENT_BLOCK && !arriving && patient)
902
+ free = Math.min(free, Math.max(0, f - 450));
903
+ }
904
+ else if (!same) {
905
+ // Oncoming or across: both keep to their right; only the higher id waits, so waits cannot go in a
906
+ // circle (the lowest id of any group always walks on).
907
+ vote += 2;
908
+ if (f < 700 && Math.abs(s) < AGENT_BLOCK && patient && this.table.idAt(j) < this.table.idAt(r))
909
+ free = Math.min(free, Math.max(0, f - 450));
910
+ }
911
+ else {
912
+ // Same direction: keep the gap; overtake on the left when the one ahead is slower.
913
+ // When each is ahead of the other (bent ways), only the higher id waits.
914
+ const fj = -(qx * DX[j] + qz * DZ[j]) >> 10;
915
+ const mutual = fj > 0 && this.table.idAt(j) > this.table.idAt(r);
916
+ if (Math.abs(s) < AGENT_BLOCK && !mutual)
917
+ free = Math.min(free, Math.max(0, f - c.gap[r]));
918
+ if (c.speed[j] < c.cruise[r])
919
+ vote -= 1;
920
+ }
921
+ }
922
+ }
923
+ }
924
+ if (standing) {
925
+ // Standing: pushed apart, kept within reach of its point.
926
+ let ox = e.ox[r] + pushX;
927
+ let oz = e.oz[r] + pushZ;
928
+ const l2 = ox * ox + oz * oz;
929
+ if (l2 > AGENT_STAND_RADIUS * AGENT_STAND_RADIUS) {
930
+ const l = isqrt(l2);
931
+ ox = Math.trunc((ox * AGENT_STAND_RADIUS) / l);
932
+ oz = Math.trunc((oz * AGENT_STAND_RADIUS) / l);
933
+ }
934
+ nox[r] = ox;
935
+ noz[r] = oz;
936
+ continue;
937
+ }
938
+ // Walking: the offset drifts toward the wanted side (or back to the path), at a walking pace sideways.
939
+ const cur = (e.ox[r] * rx + e.oz[r] * rz) >> 10;
940
+ const lateral = vote === 0 ? 0 : vote > 0 ? AGENT_STEP : -AGENT_STEP;
941
+ const pushS = (pushX * rx + pushZ * rz) >> 10;
942
+ let next = cur + Math.max(-side, Math.min(side, lateral - cur)) + Math.max(-side, Math.min(side, pushS));
943
+ next = Math.max(-900, Math.min(900, next));
944
+ nox[r] = (rx * next) >> 10;
945
+ noz[r] = (rz * next) >> 10;
946
+ if (free < this.follow[r])
947
+ this.follow[r] = free;
948
+ }
949
+ for (let r = 0; r < n; r++) {
950
+ e.pox[r] = e.ox[r];
951
+ e.poz[r] = e.oz[r];
952
+ e.ox[r] = nox[r];
953
+ e.oz[r] = noz[r];
954
+ }
955
+ }
956
+ // --- Extraction ---------------------------------------------------------------------------------------------------
957
+ /**
958
+ * Extraction: writes every entity's interpolated transform into `out` (`TRANSFORM_STRIDE` floats each, in row
959
+ * order; `table.ids` gives the id of each row) and returns the count. `alpha` is the fraction between the last two
960
+ * ticks. This is the only way the image reads movement; it does not change the simulation.
961
+ */
962
+ extract(alpha, out) {
963
+ const t = this.table;
964
+ const n = t.count;
965
+ const c = t.col;
966
+ if (out.length < n * TRANSFORM_STRIDE)
967
+ throw new Error(`Movers.extract: output holds ${out.length / TRANSFORM_STRIDE} entities, there are ${n}`);
968
+ const a = this.point;
969
+ const b = this.point2;
970
+ for (let r = 0; r < n; r++) {
971
+ const p = this.paths.get(c.path[r]);
972
+ const prev = c.prev[r];
973
+ let d = prev + (c.dist[r] - prev) * alpha;
974
+ if (d < 0)
975
+ d += p.length;
976
+ this.paths.pointAt(p, d, a);
977
+ const o = r * TRANSFORM_STRIDE;
978
+ out[o] = a[0] + this.offsetX(r, alpha);
979
+ out[o + 1] = a[1] + this.offsetZ(r, alpha);
980
+ this.paths.pointAt(p, d - LOOK_MM, a);
981
+ this.paths.pointAt(p, d + LOOK_MM, b);
982
+ let dx = b[0] - a[0];
983
+ let dz = b[1] - a[1];
984
+ const l = Math.sqrt(dx * dx + dz * dz);
985
+ if (l > 1e-6) {
986
+ dx /= l;
987
+ dz /= l;
988
+ }
989
+ else {
990
+ dx = 0;
991
+ dz = -1;
992
+ }
993
+ out[o + 2] = dx;
994
+ out[o + 3] = dz;
995
+ out[o + 4] = d / 1000;
996
+ out[o + 5] = ((c.dist[r] - prev) * this.hz) / 1000;
997
+ }
998
+ return n;
999
+ }
1000
+ offsetX(r, alpha) {
1001
+ if ((this.features & F_AGENTS) === 0)
1002
+ return 0;
1003
+ const e = this.ext;
1004
+ return (e.pox[r] + (e.ox[r] - e.pox[r]) * alpha) / 1000;
1005
+ }
1006
+ offsetZ(r, alpha) {
1007
+ if ((this.features & F_AGENTS) === 0)
1008
+ return 0;
1009
+ const e = this.ext;
1010
+ return (e.poz[r] + (e.oz[r] - e.poz[r]) * alpha) / 1000;
1011
+ }
1012
+ /**
1013
+ * Extraction with the turn rate and state as well (`POSE_STRIDE` floats per entity, row order). The turn rate is
1014
+ * the path curvature around the position times the interpolated speed. Does not change the simulation.
1015
+ */
1016
+ extractPose(alpha, out) {
1017
+ const t = this.table;
1018
+ const n = t.count;
1019
+ const c = t.col;
1020
+ if (out.length < n * POSE_STRIDE)
1021
+ throw new Error(`Movers.extractPose: output holds ${out.length / POSE_STRIDE} entities, there are ${n}`);
1022
+ const f = this.frame;
1023
+ for (let r = 0; r < n; r++) {
1024
+ const p = this.paths.get(c.path[r]);
1025
+ const prev = c.prev[r];
1026
+ let d = prev + (c.dist[r] - prev) * alpha;
1027
+ if (d < 0)
1028
+ d += p.length;
1029
+ this.paths.frameAt(p, d, LOOK_MM, f);
1030
+ const o = r * POSE_STRIDE;
1031
+ const speed = ((c.dist[r] - prev) * this.hz) / 1000;
1032
+ out[o] = f[0] + this.offsetX(r, alpha);
1033
+ out[o + 1] = f[1] + this.offsetZ(r, alpha);
1034
+ out[o + 2] = f[2];
1035
+ out[o + 3] = f[3];
1036
+ out[o + 4] = d / 1000;
1037
+ out[o + 5] = speed;
1038
+ out[o + 6] = f[4] * speed;
1039
+ out[o + 7] = c.state[r];
1040
+ }
1041
+ return n;
1042
+ }
1043
+ save(w) {
1044
+ this.table.save(w);
1045
+ this.paths.save(w);
1046
+ if (!this.extended)
1047
+ return;
1048
+ w.u32(this.obstacles.length);
1049
+ w.bytes(Int32Array.from(this.obstacles));
1050
+ w.u32(this.holder.length);
1051
+ w.bytes(this.holder);
1052
+ }
1053
+ load(r) {
1054
+ this.table.load(r);
1055
+ this.paths.load(r);
1056
+ this.cache.clear();
1057
+ // Path ids of the snapshot can name other paths than the ones cached by an interlock.
1058
+ this.interlock?.il.forgetAll(this.interlock.side);
1059
+ if (!this.extended)
1060
+ return;
1061
+ const ob = new Int32Array(r.u32());
1062
+ r.into(ob);
1063
+ this.obstacles = Array.from(ob);
1064
+ this.obstacleGrid = null;
1065
+ const h = new Float64Array(r.u32());
1066
+ r.into(h);
1067
+ this.holder = h;
1068
+ }
1069
+ }
1070
+ function cellKey(cx, cz) {
1071
+ return (cx + 0x8000) * 0x10000 + (cz + 0x8000);
1072
+ }
1073
+ /** Squared distance from a point to the segment a–b (integer mm in, float out; IEEE operations only). */
1074
+ export function capsuleDist2(x, z, ax, az, bx, bz) {
1075
+ const dx = bx - ax;
1076
+ const dz = bz - az;
1077
+ const l2 = dx * dx + dz * dz;
1078
+ let t = l2 > 0 ? ((x - ax) * dx + (z - az) * dz) / l2 : 0;
1079
+ t = t < 0 ? 0 : t > 1 ? 1 : t;
1080
+ const px = ax + dx * t - x;
1081
+ const pz = az + dz * t - z;
1082
+ return px * px + pz * pz;
1083
+ }