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,967 @@
1
+ // Coarse movement over a whole network: every edge of a navigation graph is a queue with a capacity, and the trips
2
+ // in it are only numbers (how many, and when each may leave). Individual entities exist only on the edges of a focus
3
+ // area (around the camera of the app); there the same trips are movers of an ordinary `Movers` set.
4
+ //
5
+ // The model, per edge (a "segment"):
6
+ // - storage: at most `capacity` trips fit (length · lanes / spacing); a full segment lets nobody in, so queues spill
7
+ // back along the routes;
8
+ // - free time: a trip that enters may leave after `free` ticks at the earliest (length / speed);
9
+ // - outflow: at most `outflow` trips per second and lane leave, first in first out;
10
+ // - patience: a head that has waited for room longer than `patience` is pushed on anyway, so rings of full segments
11
+ // dissolve.
12
+ //
13
+ // Hand-over between numbers and individuals (conservation: a trip is always exactly one of the two, the totals of
14
+ // `census` never change by a hand-over):
15
+ // - a trip that moves from a coarse segment into a detailed one becomes a mover at the start of that segment, which
16
+ // lies on the rim of the focus area;
17
+ // - when the focus moves, the trips queued on a segment that turns detailed all become movers along it, placed by
18
+ // how far through the segment their leaving time puts them (so a detailed segment never holds a number); the
19
+ // movers on a segment that turns coarse go back into its queue with the leaving time their position gives;
20
+ // - a mover that drives out of the focus area is taken back into the queue of the coarse segment it has entered.
21
+ //
22
+ // Determinism: integer ticks and millimetres only. The focus is input, so it comes as a command. Routes are a pure
23
+ // function of the graph (`RegionNav`), so they are derived data, not state. Setting the model up at a clock time
24
+ // (`prepare`) is a pure function of the trips and the origin: it can be done in pieces over many frames and gives
25
+ // the same state as doing it at once.
26
+ import { hashInts } from '../gen-core/hash.js';
27
+ import { mm, mmPerTick } from './fixed.js';
28
+ import { MOVER_STATE, Movers } from './movers.js';
29
+ /** State of a trip. */
30
+ export const FLOW_STATE = { pending: 0, waiting: 1, queued: 2, individual: 3, arrived: 4, unreachable: 5 };
31
+ const NEVER = 0x7fffffff;
32
+ const MILLI = 1000;
33
+ export class FlowNetwork {
34
+ name;
35
+ nav;
36
+ routes;
37
+ hz;
38
+ /** The individual trips. Its `tag` column is the trip index. */
39
+ movers;
40
+ /** Ticks between two steps of the queues. */
41
+ every;
42
+ /** Per edge: trips that fit, and ticks a trip needs at least. */
43
+ capacity;
44
+ free;
45
+ /** Per edge: trips on it now (queued and individual), and whether it is in the focus area. */
46
+ occupancy;
47
+ detail;
48
+ /** Hand-overs so far: numbers to movers, and movers back to numbers. Not state. */
49
+ spawned = 0;
50
+ merged = 0;
51
+ edges;
52
+ mmX;
53
+ mmZ;
54
+ /** Outflow per step in thousandths of a trip, and the size of the allowance (at least one trip). */
55
+ rate;
56
+ bucket;
57
+ /** Queue per edge: first and last trip, and the leaving time of the first (NEVER when empty). */
58
+ head;
59
+ tail;
60
+ headDue;
61
+ /** Used part of the outflow allowance and the tick it was last refilled. */
62
+ deficit;
63
+ refillTick;
64
+ /** Tick since which the head has waited for room, or -1. */
65
+ blockedSince;
66
+ spacing;
67
+ patience;
68
+ horizon;
69
+ perStep;
70
+ pathEdges;
71
+ pathOptions;
72
+ /** Offset to the right per edge, metres, when the edges do not share one. */
73
+ offsets;
74
+ spawnOptions;
75
+ // Trips: what was asked for (fixed) and where each is now (state).
76
+ n = 0;
77
+ tFrom = new Int32Array(1024);
78
+ tTo = new Int32Array(1024);
79
+ tDepart = new Int32Array(1024);
80
+ tTag = new Int32Array(1024);
81
+ tState = new Uint8Array(1024);
82
+ /** Index of the current edge in the route, and the edge itself. */
83
+ tLeg = new Int32Array(1024);
84
+ tSeg = new Int32Array(1024);
85
+ /** Queued: earliest leaving tick. Waiting: tick since which it waits. */
86
+ tDue = new Int32Array(1024);
87
+ /** Queued: the trip behind it. */
88
+ tNext = new Int32Array(1024);
89
+ /** Individual: route index of the first edge of its path, and its mover. */
90
+ tBase = new Int32Array(1024);
91
+ tMover = new Float64Array(1024);
92
+ /** Routes of the trips under way (derived: looked up again when missing). */
93
+ tRoute = [];
94
+ defHash = 0x9e3779b9;
95
+ inOrder = true;
96
+ /** Trips by departure, then index; null while the trips were added in that order anyway. */
97
+ order = null;
98
+ sorted = false;
99
+ started = false;
100
+ originTick;
101
+ prepared = false;
102
+ /** Set-up progress: next and last position in `order` to place, -1 before the range is known. */
103
+ placeAt = -1;
104
+ placeEnd = 0;
105
+ /** Position in `order` of the next trip to start. */
106
+ cursor = 0;
107
+ waiting = [];
108
+ lastStep = 0;
109
+ fx = 0;
110
+ fz = 0;
111
+ fr = 0;
112
+ focusDirty = false;
113
+ cPending = 0;
114
+ cQueued = 0;
115
+ cIndividual = 0;
116
+ cArrived = 0;
117
+ cUnreachable = 0;
118
+ constructor(name, options) {
119
+ this.name = name;
120
+ this.nav = options.nav;
121
+ this.routes = options.routes;
122
+ if (this.routes.nav !== this.nav)
123
+ throw new Error('FlowNetwork: the routes belong to another graph');
124
+ this.hz = options.hz;
125
+ const nav = this.nav;
126
+ const m = nav.edgeCount;
127
+ this.edges = m;
128
+ this.every = Math.max(1, Math.round((options.period ?? 1) * this.hz));
129
+ const speed = Math.max(1, mmPerTick(options.speed ?? 10, this.hz));
130
+ this.spacing = Math.max(1, mm(options.spacing ?? 7.5));
131
+ this.patience = Math.max(this.every, Math.round((options.patience ?? 120) * this.hz));
132
+ this.horizon = Math.max(1, Math.round((options.horizon ?? 7200) * this.hz));
133
+ this.perStep = Math.max(1, Math.floor(options.departuresPerStep ?? 256));
134
+ this.pathEdges = Math.max(2, Math.floor(options.pathEdges ?? 24));
135
+ const right = options.rightOffset ?? 0;
136
+ this.pathOptions = { rightOffset: typeof right === 'number' ? right : 0 };
137
+ this.offsets = typeof right === 'number' ? null : Float64Array.from({ length: this.nav.edgeCount }, (_, e) => right[e] ?? 0);
138
+ const cruise = options.speed ?? 10;
139
+ this.spawnOptions = options.spawn ?? (() => ({ cruise }));
140
+ this.originTick = Math.round((options.origin ?? 0) * this.hz);
141
+ this.movers = new Movers(`${name}.movers`, { hz: this.hz, collision: options.collision ?? 'follow', ...(options.width !== undefined ? { width: options.width } : {}) });
142
+ this.mmX = new Int32Array(nav.nodeCount);
143
+ this.mmZ = new Int32Array(nav.nodeCount);
144
+ for (let i = 0; i < nav.nodeCount; i++) {
145
+ this.mmX[i] = mm(nav.nodeX[i]);
146
+ this.mmZ[i] = mm(nav.nodeZ[i]);
147
+ }
148
+ this.capacity = new Int32Array(m);
149
+ this.free = new Int32Array(m);
150
+ this.rate = new Int32Array(m);
151
+ this.bucket = new Int32Array(m);
152
+ const lanes = options.lanes ?? 1;
153
+ const perSecond = options.outflow ?? 0.5;
154
+ const period = this.every / this.hz;
155
+ for (let e = 0; e < m; e++) {
156
+ const l = Math.max(1, Math.round(typeof lanes === 'number' ? lanes : (lanes[e] ?? 1)));
157
+ const len = nav.edgeLength[e];
158
+ this.capacity[e] = Math.max(1, Math.floor((len * l) / this.spacing));
159
+ const limit = nav.edgeSpeed[e] > 0 ? Math.max(1, Math.min(speed, mmPerTick(nav.edgeSpeed[e], this.hz))) : speed;
160
+ this.free[e] = Math.max(this.every, Math.ceil(len / limit));
161
+ this.rate[e] = Math.max(1, Math.round(perSecond * l * period * MILLI));
162
+ this.bucket[e] = Math.max(MILLI, this.rate[e]);
163
+ }
164
+ this.occupancy = new Int32Array(m);
165
+ this.detail = new Uint8Array(m);
166
+ this.head = new Int32Array(m).fill(-1);
167
+ this.tail = new Int32Array(m).fill(-1);
168
+ this.headDue = new Int32Array(m).fill(NEVER);
169
+ this.deficit = new Float64Array(m);
170
+ this.refillTick = new Int32Array(m);
171
+ this.blockedSince = new Int32Array(m).fill(-1);
172
+ }
173
+ // --- Setup -------------------------------------------------------------------------------------------------------
174
+ /** Trips added. */
175
+ get size() {
176
+ return this.n;
177
+ }
178
+ grow() {
179
+ const cap = this.tFrom.length * 2;
180
+ const i32 = (a) => {
181
+ const b = new Int32Array(cap);
182
+ b.set(a);
183
+ return b;
184
+ };
185
+ this.tFrom = i32(this.tFrom);
186
+ this.tTo = i32(this.tTo);
187
+ this.tDepart = i32(this.tDepart);
188
+ this.tTag = i32(this.tTag);
189
+ this.tLeg = i32(this.tLeg);
190
+ this.tSeg = i32(this.tSeg);
191
+ this.tDue = i32(this.tDue);
192
+ this.tNext = i32(this.tNext);
193
+ this.tBase = i32(this.tBase);
194
+ const s = new Uint8Array(cap);
195
+ s.set(this.tState);
196
+ this.tState = s;
197
+ const f = new Float64Array(cap);
198
+ f.set(this.tMover);
199
+ this.tMover = f;
200
+ }
201
+ /** Adds a trip and returns its index. Trips are setup: add them all before the first tick. */
202
+ add(trip) {
203
+ if (this.started)
204
+ throw new Error('FlowNetwork: add trips before the simulation runs');
205
+ const nodes = this.nav.nodeCount;
206
+ if (!(trip.from >= 0 && trip.from < nodes && trip.to >= 0 && trip.to < nodes))
207
+ throw new Error(`FlowNetwork: trip ${trip.from} → ${trip.to} names a node outside the graph`);
208
+ if (this.n === this.tFrom.length)
209
+ this.grow();
210
+ const i = this.n++;
211
+ this.tFrom[i] = trip.from;
212
+ this.tTo[i] = trip.to;
213
+ this.tDepart[i] = Math.round(trip.at * this.hz);
214
+ this.tTag[i] = trip.tag ?? 0;
215
+ this.tState[i] = FLOW_STATE.pending;
216
+ if (i > 0 && this.tDepart[i] < this.tDepart[i - 1])
217
+ this.inOrder = false;
218
+ this.defHash = hashInts(this.defHash, trip.from, trip.to, this.tDepart[i], this.tTag[i]);
219
+ this.cPending++;
220
+ return i;
221
+ }
222
+ /**
223
+ * Registers the network with a simulation: its movers and itself as state, the command `<name>.focus`
224
+ * (`x`, `z`, `radius` in metres) and the two systems (movers, then the queues).
225
+ */
226
+ attach(sim) {
227
+ sim.addState(this.movers);
228
+ sim.addState(this);
229
+ sim.onCommand(`${this.name}.focus`, (cmd) => this.setFocus(Number(cmd.data.x ?? 0), Number(cmd.data.z ?? 0), Number(cmd.data.radius ?? 0)));
230
+ sim.addSystem(`${this.name}.movers`, this.movers.system);
231
+ sim.addSystem(this.name, this.system);
232
+ }
233
+ /**
234
+ * Moves the focus area: a circle in metres; edges with both ends inside are detailed. Radius 0 means no focus.
235
+ * The focus is input of the simulation: call this only from a command handler (`attach` registers one), never from
236
+ * a frame. It takes effect at the next step of the queues.
237
+ */
238
+ setFocus(x, z, radius) {
239
+ this.fx = mm(x);
240
+ this.fz = mm(z);
241
+ this.fr = Math.max(0, mm(radius));
242
+ this.focusDirty = true;
243
+ }
244
+ /** The focus area now: centre and radius in metres (radius 0: none). */
245
+ get focus() {
246
+ return { x: this.fx / 1000, z: this.fz / 1000, radius: this.fr / 1000 };
247
+ }
248
+ /** Clock second of tick 0. */
249
+ get origin() {
250
+ return this.originTick / this.hz;
251
+ }
252
+ /**
253
+ * Moves tick 0 to another clock second and empties the network; `prepare` then sets it up for that time. For a
254
+ * clock that jumps: `sim.rebase(snapshot)`, `setOrigin`, then `prepare` over a few frames before stepping.
255
+ */
256
+ setOrigin(seconds) {
257
+ this.originTick = Math.round(seconds * this.hz);
258
+ this.clear();
259
+ }
260
+ clear() {
261
+ const m = this.movers;
262
+ while (m.count > 0)
263
+ m.remove(m.table.idAt(m.count - 1));
264
+ this.tState.fill(FLOW_STATE.pending, 0, this.n);
265
+ this.tRoute.length = 0;
266
+ this.occupancy.fill(0);
267
+ this.detail.fill(0);
268
+ this.head.fill(-1);
269
+ this.tail.fill(-1);
270
+ this.headDue.fill(NEVER);
271
+ this.deficit.fill(0);
272
+ this.refillTick.fill(0);
273
+ this.blockedSince.fill(-1);
274
+ this.waiting.length = 0;
275
+ this.prepared = false;
276
+ this.placeAt = -1;
277
+ this.placeEnd = 0;
278
+ this.cursor = 0;
279
+ this.lastStep = 0;
280
+ this.fx = this.fz = this.fr = 0;
281
+ this.focusDirty = false;
282
+ this.cPending = this.n;
283
+ this.cQueued = this.cIndividual = this.cArrived = this.cUnreachable = 0;
284
+ }
285
+ at(k) {
286
+ return this.order ? this.order[k] : k;
287
+ }
288
+ /** First position in `order` whose trip departs at or after `tick`. */
289
+ firstFrom(tick) {
290
+ let lo = 0;
291
+ let hi = this.n;
292
+ while (lo < hi) {
293
+ const mid = (lo + hi) >> 1;
294
+ if (this.tDepart[this.at(mid)] < tick)
295
+ lo = mid + 1;
296
+ else
297
+ hi = mid;
298
+ }
299
+ return lo;
300
+ }
301
+ /** Is the network set up for its origin (nothing left for `prepare`)? */
302
+ get ready() {
303
+ return this.prepared;
304
+ }
305
+ /**
306
+ * Sets the network up for the clock time of tick 0, at most `units` pieces of work per call (a region of the route
307
+ * search or one trip), and returns the pieces left; 0 means ready. Trips that left before the origin are put where
308
+ * free travel has taken them by then, or are over. The result does not depend on how the calls are cut, and a
309
+ * simulation stepped before it is ready finishes the work in its first step.
310
+ */
311
+ prepare(units = Infinity) {
312
+ if (this.prepared)
313
+ return 0;
314
+ this.started = true;
315
+ let left = units;
316
+ if (!this.routes.ready) {
317
+ const before = this.routes.pending;
318
+ const rest = this.routes.prepare(left);
319
+ left -= before - rest;
320
+ if (rest > 0)
321
+ return rest + this.n;
322
+ }
323
+ this.sortTrips();
324
+ if (this.placeAt < 0) {
325
+ // Trips that left more than the horizon ago are over; the ones in between are placed one by one.
326
+ const over = this.firstFrom(this.originTick - this.horizon);
327
+ for (let k = 0; k < over; k++)
328
+ this.tState[this.at(k)] = FLOW_STATE.arrived;
329
+ this.cArrived += over;
330
+ this.cPending -= over;
331
+ this.placeAt = over;
332
+ this.placeEnd = this.firstFrom(this.originTick);
333
+ }
334
+ while (this.placeAt < this.placeEnd && left > 0) {
335
+ this.placeUnderWay(this.at(this.placeAt++));
336
+ left--;
337
+ }
338
+ if (this.placeAt < this.placeEnd)
339
+ return this.placeEnd - this.placeAt;
340
+ this.cursor = this.placeEnd;
341
+ this.prepared = true;
342
+ return 0;
343
+ }
344
+ /** Orders the trips by departure (derived from the trips, so it is not part of the saved state). */
345
+ sortTrips() {
346
+ if (this.sorted)
347
+ return;
348
+ if (!this.inOrder) {
349
+ const depart = this.tDepart;
350
+ this.order = Int32Array.from({ length: this.n }, (_, i) => i).sort((a, b) => depart[a] - depart[b] || a - b);
351
+ }
352
+ this.sorted = true;
353
+ }
354
+ /** Puts a trip that left before the origin where free travel has taken it by tick 0. */
355
+ placeUnderWay(trip) {
356
+ this.cPending--;
357
+ const route = this.routes.find(this.tFrom[trip], this.tTo[trip]);
358
+ if (route === null) {
359
+ this.tState[trip] = FLOW_STATE.unreachable;
360
+ this.cUnreachable++;
361
+ return;
362
+ }
363
+ const elapsed = this.originTick - this.tDepart[trip];
364
+ let t = 0;
365
+ for (let leg = 0; leg < route.length; leg++) {
366
+ const e = route[leg];
367
+ t += this.free[e];
368
+ if (t <= elapsed)
369
+ continue;
370
+ this.tRoute[trip] = route;
371
+ this.tLeg[trip] = leg;
372
+ this.tSeg[trip] = e;
373
+ this.tState[trip] = FLOW_STATE.queued;
374
+ this.occupancy[e] = this.occupancy[e] + 1;
375
+ this.cQueued++;
376
+ this.enqueue(e, trip, t - elapsed);
377
+ return;
378
+ }
379
+ this.tState[trip] = FLOW_STATE.arrived;
380
+ this.cArrived++;
381
+ }
382
+ // --- Queues ------------------------------------------------------------------------------------------------------
383
+ /** Puts a trip into the queue of an edge, ordered by leaving time (behind the ones that leave at the same tick). */
384
+ enqueue(e, trip, due) {
385
+ this.tDue[trip] = due;
386
+ const last = this.tail[e];
387
+ if (last < 0) {
388
+ this.head[e] = trip;
389
+ this.tail[e] = trip;
390
+ this.tNext[trip] = -1;
391
+ this.headDue[e] = due;
392
+ return;
393
+ }
394
+ if (this.tDue[last] <= due) {
395
+ this.tNext[last] = trip;
396
+ this.tNext[trip] = -1;
397
+ this.tail[e] = trip;
398
+ return;
399
+ }
400
+ // Out of order (set-up, a mover handed back): before the first one that leaves later. The tail leaves later.
401
+ let prev = -1;
402
+ let cur = this.head[e];
403
+ while (this.tDue[cur] <= due) {
404
+ prev = cur;
405
+ cur = this.tNext[cur];
406
+ }
407
+ this.tNext[trip] = cur;
408
+ if (prev < 0) {
409
+ this.head[e] = trip;
410
+ this.headDue[e] = due;
411
+ }
412
+ else
413
+ this.tNext[prev] = trip;
414
+ }
415
+ popHead(e) {
416
+ const h = this.head[e];
417
+ const next = this.tNext[h];
418
+ this.head[e] = next;
419
+ if (next < 0) {
420
+ this.tail[e] = -1;
421
+ this.headDue[e] = NEVER;
422
+ }
423
+ else
424
+ this.headDue[e] = this.tDue[next];
425
+ return h;
426
+ }
427
+ routeOf(trip) {
428
+ let r = this.tRoute[trip];
429
+ if (r === undefined) {
430
+ r = this.routes.find(this.tFrom[trip], this.tTo[trip]) ?? undefined;
431
+ if (r === undefined)
432
+ throw new Error(`FlowNetwork: trip ${trip} is under way without a route`);
433
+ this.tRoute[trip] = r;
434
+ }
435
+ return r;
436
+ }
437
+ arrive(sim, trip) {
438
+ this.tState[trip] = FLOW_STATE.arrived;
439
+ this.tRoute[trip] = undefined;
440
+ this.cArrived++;
441
+ sim.events.post('flow.arrive', 0, { net: this.name, trip });
442
+ }
443
+ /** The system: steps the queues every `every` ticks. `attach` registers it after the movers' system. */
444
+ system = (sim) => {
445
+ const now = sim.tick;
446
+ if (now % this.every !== 0)
447
+ return;
448
+ if (!this.prepared)
449
+ this.prepare();
450
+ this.lastStep = now;
451
+ if (this.focusDirty)
452
+ this.applyFocus(now);
453
+ this.sync(sim, now);
454
+ this.depart(sim, now);
455
+ this.outflow(sim, now);
456
+ };
457
+ /** Starts the trips whose time has come: the ones waiting for room first, then new ones up to the step's budget. */
458
+ depart(sim, now) {
459
+ const waiting = this.waiting;
460
+ let kept = 0;
461
+ for (let i = 0; i < waiting.length; i++) {
462
+ const trip = waiting[i];
463
+ const e = this.tSeg[trip];
464
+ if (this.occupancy[e] < this.capacity[e] || now - this.tDue[trip] >= this.patience)
465
+ this.start(trip, e, now);
466
+ else
467
+ waiting[kept++] = trip;
468
+ }
469
+ waiting.length = kept;
470
+ const clock = this.originTick + now;
471
+ let budget = this.perStep;
472
+ while (this.cursor < this.n && budget > 0) {
473
+ const trip = this.at(this.cursor);
474
+ if (this.tDepart[trip] > clock)
475
+ break;
476
+ this.cursor++;
477
+ budget--;
478
+ this.cPending--;
479
+ const route = this.routes.find(this.tFrom[trip], this.tTo[trip]);
480
+ if (route === null) {
481
+ this.tState[trip] = FLOW_STATE.unreachable;
482
+ this.cUnreachable++;
483
+ sim.events.post('flow.unreachable', 0, { net: this.name, trip });
484
+ continue;
485
+ }
486
+ if (route.length === 0) {
487
+ this.arrive(sim, trip);
488
+ continue;
489
+ }
490
+ this.tRoute[trip] = route;
491
+ const e = route[0];
492
+ this.tLeg[trip] = 0;
493
+ this.tSeg[trip] = e;
494
+ if (this.occupancy[e] < this.capacity[e])
495
+ this.start(trip, e, now);
496
+ else {
497
+ this.tState[trip] = FLOW_STATE.waiting;
498
+ this.tDue[trip] = now;
499
+ waiting.push(trip);
500
+ }
501
+ }
502
+ }
503
+ /** Lets a trip onto the first edge of its route: as a mover when the edge is detailed, else into its queue. */
504
+ start(trip, e, now) {
505
+ this.occupancy[e] = this.occupancy[e] + 1;
506
+ if (this.detail[e])
507
+ this.spawn(trip, 0, 0, false);
508
+ else {
509
+ this.tState[trip] = FLOW_STATE.queued;
510
+ this.cQueued++;
511
+ this.enqueue(e, trip, now + this.free[e]);
512
+ }
513
+ }
514
+ /** Moves the heads of the queues on: to the next edge of their route when it has room, or out at their goal. */
515
+ outflow(sim, now) {
516
+ const headDue = this.headDue;
517
+ for (let e = 0; e < this.edges; e++) {
518
+ if (headDue[e] > now)
519
+ continue;
520
+ // The allowance refills by `rate` per step, up to `bucket`.
521
+ const steps = Math.floor((now - this.refillTick[e]) / this.every);
522
+ let used = Math.max(0, this.deficit[e] - this.rate[e] * steps);
523
+ this.refillTick[e] = now;
524
+ const room = this.bucket[e];
525
+ while (headDue[e] <= now && room - used >= MILLI) {
526
+ const trip = this.head[e];
527
+ const route = this.routeOf(trip);
528
+ const leg = this.tLeg[trip];
529
+ if (leg === route.length - 1) {
530
+ this.popHead(e);
531
+ this.occupancy[e] = this.occupancy[e] - 1;
532
+ this.cQueued--;
533
+ this.blockedSince[e] = -1;
534
+ used += MILLI;
535
+ this.arrive(sim, trip);
536
+ continue;
537
+ }
538
+ const next = route[leg + 1];
539
+ if (this.occupancy[next] >= this.capacity[next] && !(this.blockedSince[e] >= 0 && now - this.blockedSince[e] >= this.patience)) {
540
+ if (this.blockedSince[e] < 0)
541
+ this.blockedSince[e] = now;
542
+ break;
543
+ }
544
+ this.popHead(e);
545
+ this.occupancy[e] = this.occupancy[e] - 1;
546
+ this.occupancy[next] = this.occupancy[next] + 1;
547
+ this.blockedSince[e] = -1;
548
+ used += MILLI;
549
+ this.tLeg[trip] = leg + 1;
550
+ this.tSeg[trip] = next;
551
+ if (this.detail[next]) {
552
+ this.cQueued--;
553
+ this.spawn(trip, leg + 1, 0, true);
554
+ }
555
+ else
556
+ this.enqueue(next, trip, now + this.free[next]);
557
+ }
558
+ this.deficit[e] = used;
559
+ }
560
+ }
561
+ // --- Hand-over ---------------------------------------------------------------------------------------------------
562
+ /** The edges of a route from `base` on, as far as a path looks ahead. */
563
+ pathFrom(route, base, count) {
564
+ const n = Math.min(count, route.length - base);
565
+ const edges = new Array(n);
566
+ for (let i = 0; i < n; i++)
567
+ edges[i] = route[base + i];
568
+ return this.movers.paths.add(this.offsets ? this.offsetPath(edges, this.offsets) : this.nav.path(edges, this.pathOptions), { owned: true });
569
+ }
570
+ /**
571
+ * A path along edges that each keep an own distance to the right: one segment per edge with the edge as its lane.
572
+ * A vertex between two edges lies on both shifted lines; where they are nearly parallel, or the meeting point
573
+ * would run away, it lies halfway between the two shifted ends.
574
+ */
575
+ offsetPath(edges, off) {
576
+ const nav = this.nav;
577
+ const points = [];
578
+ // Right-hand normal and offset of the edge before the vertex.
579
+ let pnx = 0;
580
+ let pnz = 0;
581
+ let po = 0;
582
+ for (let i = 0; i <= edges.length; i++) {
583
+ const node = i < edges.length ? nav.edgeFrom[edges[i]] : nav.edgeTo[edges[i - 1]];
584
+ let nx = 0;
585
+ let nz = 0;
586
+ let o = 0;
587
+ if (i < edges.length) {
588
+ const e = edges[i];
589
+ const a = nav.edgeFrom[e];
590
+ const b = nav.edgeTo[e];
591
+ const dx = nav.nodeX[b] - nav.nodeX[a];
592
+ const dz = nav.nodeZ[b] - nav.nodeZ[a];
593
+ const l = Math.sqrt(dx * dx + dz * dz) || 1;
594
+ nx = -dz / l;
595
+ nz = dx / l;
596
+ o = off[e];
597
+ }
598
+ let vx = nx * o;
599
+ let vz = nz * o;
600
+ if (i === edges.length) {
601
+ vx = pnx * po;
602
+ vz = pnz * po;
603
+ }
604
+ else if (i > 0) {
605
+ vx = (pnx * po + nx * o) / 2;
606
+ vz = (pnz * po + nz * o) / 2;
607
+ const det = pnx * nz - pnz * nx;
608
+ if (Math.abs(det) > 0.2) {
609
+ const mx = (po * nz - o * pnz) / det;
610
+ const mz = (o * pnx - po * nx) / det;
611
+ const reach = 3 * Math.max(Math.abs(po), Math.abs(o));
612
+ if (mx * mx + mz * mz <= reach * reach) {
613
+ vx = mx;
614
+ vz = mz;
615
+ }
616
+ }
617
+ }
618
+ points.push(nav.nodeX[node] + vx, nav.nodeZ[node] + vz);
619
+ pnx = nx;
620
+ pnz = nz;
621
+ po = o;
622
+ }
623
+ return { points, speeds: edges.map((e) => nav.edgeSpeed[e]), lanes: [...edges] };
624
+ }
625
+ /**
626
+ * Makes a trip an individual mover on edge `leg` of its route, `s` mm into the edge (of the edge's own length).
627
+ * The caller has counted the trip on the edge and taken it out of any queue.
628
+ */
629
+ spawn(trip, leg, s, moving) {
630
+ const route = this.routeOf(trip);
631
+ const e = route[leg];
632
+ const path = this.pathFrom(route, leg, this.pathEdges);
633
+ const p = this.movers.paths.get(path);
634
+ // The path runs beside the edge, so its first segment has a slightly different length: keep the proportion.
635
+ const at = Math.min(p.segStart[1] - 1, Math.floor((s * p.segStart[1]) / this.nav.edgeLength[e]));
636
+ const o = this.spawnOptions(trip);
637
+ this.tMover[trip] = this.movers.spawn({ ...o, path, at: Math.max(0, at) / 1000, tag: trip, speed: moving ? o.cruise : 0 });
638
+ this.tBase[trip] = leg;
639
+ this.tLeg[trip] = leg;
640
+ this.tSeg[trip] = e;
641
+ this.tState[trip] = FLOW_STATE.individual;
642
+ this.cIndividual++;
643
+ this.spawned++;
644
+ }
645
+ /** Applies a moved focus: marks the edges and turns the queues of newly detailed edges into movers. */
646
+ applyFocus(now) {
647
+ this.focusDirty = false;
648
+ const nav = this.nav;
649
+ const r2 = this.fr * this.fr;
650
+ for (let e = 0; e < this.edges; e++) {
651
+ let inside = 0;
652
+ if (this.fr > 0) {
653
+ const a = nav.edgeFrom[e];
654
+ const b = nav.edgeTo[e];
655
+ const ax = this.mmX[a] - this.fx;
656
+ const az = this.mmZ[a] - this.fz;
657
+ const bx = this.mmX[b] - this.fx;
658
+ const bz = this.mmZ[b] - this.fz;
659
+ if (ax * ax + az * az <= r2 && bx * bx + bz * bz <= r2)
660
+ inside = 1;
661
+ }
662
+ if (inside === this.detail[e])
663
+ continue;
664
+ this.detail[e] = inside;
665
+ if (inside && this.head[e] >= 0)
666
+ this.materialise(e, now);
667
+ }
668
+ }
669
+ /**
670
+ * Turns the whole queue of an edge into movers along it, the first to leave furthest ahead: each stands where its
671
+ * leaving time puts it at free speed, but no closer than `spacing` behind the one ahead (closer only when more
672
+ * trips are queued than fit in one file).
673
+ */
674
+ materialise(e, now) {
675
+ const len = this.nav.edgeLength[e];
676
+ const free = this.free[e];
677
+ let count = 0;
678
+ for (let trip = this.head[e]; trip >= 0; trip = this.tNext[trip])
679
+ count++;
680
+ const gap = Math.min(this.spacing, Math.floor(len / count));
681
+ let ahead = len - 1 + gap;
682
+ for (;;) {
683
+ const trip = this.head[e];
684
+ if (trip < 0)
685
+ break;
686
+ const remain = Math.min(free, Math.max(0, this.tDue[trip] - now));
687
+ const s = Math.max(0, Math.min(len - 1 - Math.floor(((len - 1) * remain) / free), ahead - gap));
688
+ this.popHead(e);
689
+ this.cQueued--;
690
+ this.spawn(trip, this.tLeg[trip], s, remain > 0);
691
+ ahead = s;
692
+ }
693
+ this.blockedSince[e] = -1;
694
+ }
695
+ /**
696
+ * Follows the movers: counts each on the edge it has reached, lets the ones at their goal arrive, takes the ones
697
+ * on a coarse edge back into that edge's queue, and gives a new path to the ones near the end of theirs.
698
+ */
699
+ sync(sim, now) {
700
+ const m = this.movers;
701
+ const t = m.table;
702
+ for (let r = t.count - 1; r >= 0; r--) {
703
+ const c = t.col;
704
+ const trip = c.tag[r];
705
+ const id = t.idAt(r);
706
+ const p = m.paths.get(c.path[r]);
707
+ const seg = c.seg[r];
708
+ const route = this.routeOf(trip);
709
+ let leg = this.tLeg[trip];
710
+ const reached = this.tBase[trip] + seg;
711
+ while (leg < reached) {
712
+ this.occupancy[route[leg]] = this.occupancy[route[leg]] - 1;
713
+ leg++;
714
+ this.occupancy[route[leg]] = this.occupancy[route[leg]] + 1;
715
+ }
716
+ let e = route[leg];
717
+ const last = leg === route.length - 1;
718
+ let pos = c.dist[r] - p.segStart[seg];
719
+ let segLen = p.segStart[seg + 1] - p.segStart[seg];
720
+ let speed = c.speed[r];
721
+ let renew = seg === p.segments - 1 && !last && this.detail[route[leg + 1]] === 1;
722
+ if (c.state[r] === MOVER_STATE.done) {
723
+ if (last) {
724
+ this.occupancy[e] = this.occupancy[e] - 1;
725
+ m.remove(id);
726
+ this.cIndividual--;
727
+ this.arrive(sim, trip);
728
+ continue;
729
+ }
730
+ // The end of a path that did not reach the goal: it stands at the start of the next edge.
731
+ this.occupancy[e] = this.occupancy[e] - 1;
732
+ leg++;
733
+ e = route[leg];
734
+ this.occupancy[e] = this.occupancy[e] + 1;
735
+ pos = 0;
736
+ segLen = 0;
737
+ speed = 0;
738
+ renew = true;
739
+ }
740
+ this.tLeg[trip] = leg;
741
+ this.tSeg[trip] = e;
742
+ if (!this.detail[e]) {
743
+ // Out of the focus area: back to a number, leaving when free travel over the rest of the edge allows.
744
+ const free = this.free[e];
745
+ const remain = segLen > 0 ? Math.ceil((free * (segLen - pos)) / segLen) : free;
746
+ m.remove(id);
747
+ this.cIndividual--;
748
+ this.tState[trip] = FLOW_STATE.queued;
749
+ this.cQueued++;
750
+ this.merged++;
751
+ this.enqueue(e, trip, now + Math.max(1, remain));
752
+ continue;
753
+ }
754
+ if (!renew)
755
+ continue;
756
+ // A new path from the edge before the current one (so the corner into it keeps its shape), same place and speed.
757
+ const base = leg > 0 ? leg - 1 : 0;
758
+ const path = this.pathFrom(route, base, this.pathEdges + (leg - base));
759
+ const np = m.paths.get(path);
760
+ const k = leg - base;
761
+ const d = Math.min(np.segStart[k] + pos, np.segStart[k + 1] - 1);
762
+ // The way covered in this tick is kept across the switch, so a pose taken between two ticks runs on smoothly
763
+ // and reports the real speed.
764
+ const moved = Math.max(0, c.dist[r] - c.prev[r]);
765
+ m.setPath(id, path, d / 1000);
766
+ const cc = t.col;
767
+ cc.speed[r] = Math.min(speed, cc.cruise[r]);
768
+ cc.prev[r] = Math.max(0, d - moved);
769
+ this.tBase[trip] = base;
770
+ }
771
+ }
772
+ // --- Reading -----------------------------------------------------------------------------------------------------
773
+ /** State of a trip (`FLOW_STATE`). */
774
+ stateOf(trip) {
775
+ return this.tState[trip];
776
+ }
777
+ /** The app's tag of a trip. */
778
+ tagOf(trip) {
779
+ return this.tTag[trip];
780
+ }
781
+ /** The edge a trip is on (or waits to enter), -1 when it is not under way. */
782
+ edgeOf(trip) {
783
+ const s = this.tState[trip];
784
+ return s === FLOW_STATE.waiting || s === FLOW_STATE.queued || s === FLOW_STATE.individual ? this.tSeg[trip] : -1;
785
+ }
786
+ /** How many trips are in each state. Hand-overs between `queued` and `individual` never change the sums. */
787
+ census() {
788
+ let load = 0;
789
+ for (let e = 0; e < this.edges; e++)
790
+ load += this.occupancy[e];
791
+ return {
792
+ trips: this.n, pending: this.cPending, waiting: this.waiting.length, queued: this.cQueued, individual: this.cIndividual,
793
+ arrived: this.cArrived, unreachable: this.cUnreachable, load,
794
+ };
795
+ }
796
+ /**
797
+ * Counts everything again from the trips, the queues and the movers and compares it with the running counters:
798
+ * returns what is wrong, or null. For tests of the conservation rule.
799
+ */
800
+ check() {
801
+ const counts = [0, 0, 0, 0, 0, 0];
802
+ const onEdge = new Int32Array(this.edges);
803
+ for (let i = 0; i < this.n; i++) {
804
+ const s = this.tState[i];
805
+ counts[s] = counts[s] + 1;
806
+ if (s === FLOW_STATE.queued || s === FLOW_STATE.individual)
807
+ onEdge[this.tSeg[i]]++;
808
+ }
809
+ const c = this.census();
810
+ const want = [c.pending, c.waiting, c.queued, c.individual, c.arrived, c.unreachable];
811
+ for (let s = 0; s < 6; s++)
812
+ if (counts[s] !== want[s])
813
+ return `state ${s}: ${counts[s]} trips, counter says ${want[s]}`;
814
+ if (c.pending + c.waiting + c.queued + c.individual + c.arrived + c.unreachable !== c.trips)
815
+ return 'the states do not add up to the trips';
816
+ if (c.load !== c.queued + c.individual)
817
+ return `load ${c.load}, but ${c.queued} queued and ${c.individual} individual`;
818
+ if (this.movers.count !== c.individual)
819
+ return `${this.movers.count} movers for ${c.individual} individual trips`;
820
+ let inQueues = 0;
821
+ for (let e = 0; e < this.edges; e++) {
822
+ if (onEdge[e] !== this.occupancy[e])
823
+ return `edge ${e}: load ${this.occupancy[e]}, trips on it ${onEdge[e]}`;
824
+ if (this.detail[e] && this.head[e] >= 0)
825
+ return `edge ${e}: a queue on a detailed edge`;
826
+ let due = -Infinity;
827
+ for (let trip = this.head[e]; trip >= 0; trip = this.tNext[trip]) {
828
+ if (this.tState[trip] !== FLOW_STATE.queued || this.tSeg[trip] !== e)
829
+ return `edge ${e}: trip ${trip} in its queue is not queued on it`;
830
+ if (this.tDue[trip] < due)
831
+ return `edge ${e}: queue out of order`;
832
+ due = this.tDue[trip];
833
+ inQueues++;
834
+ }
835
+ }
836
+ if (inQueues !== c.queued)
837
+ return `${inQueues} trips in queues, ${c.queued} queued`;
838
+ const tags = this.movers.table.col.tag;
839
+ for (let r = 0; r < this.movers.count; r++) {
840
+ const trip = tags[r];
841
+ if (this.tState[trip] !== FLOW_STATE.individual || this.tMover[trip] !== this.movers.table.idAt(r))
842
+ return `mover row ${r} does not belong to an individual trip`;
843
+ }
844
+ return null;
845
+ }
846
+ // --- State -------------------------------------------------------------------------------------------------------
847
+ save(w) {
848
+ const n = this.n;
849
+ w.u32(n);
850
+ w.u32(this.defHash);
851
+ w.i32(this.originTick);
852
+ w.u8(this.prepared ? 1 : 0);
853
+ w.i32(this.placeAt);
854
+ w.i32(this.placeEnd);
855
+ w.i32(this.cursor);
856
+ w.i32(this.lastStep);
857
+ w.i32(this.fx);
858
+ w.i32(this.fz);
859
+ w.i32(this.fr);
860
+ w.u8(this.focusDirty ? 1 : 0);
861
+ w.i32(this.cPending);
862
+ w.i32(this.cQueued);
863
+ w.i32(this.cIndividual);
864
+ w.i32(this.cArrived);
865
+ w.i32(this.cUnreachable);
866
+ w.bytes(this.tState.subarray(0, n));
867
+ // Only trips under way have more than their state.
868
+ for (let i = 0; i < n; i++) {
869
+ const s = this.tState[i];
870
+ if (s !== FLOW_STATE.waiting && s !== FLOW_STATE.queued && s !== FLOW_STATE.individual)
871
+ continue;
872
+ w.i32(this.tLeg[i]);
873
+ w.i32(this.tSeg[i]);
874
+ w.i32(s === FLOW_STATE.individual ? 0 : this.tDue[i]);
875
+ w.i32(s === FLOW_STATE.queued ? this.tNext[i] : -1);
876
+ w.i32(s === FLOW_STATE.individual ? this.tBase[i] : 0);
877
+ w.f64(s === FLOW_STATE.individual ? this.tMover[i] : 0);
878
+ }
879
+ w.u32(this.waiting.length);
880
+ for (const trip of this.waiting)
881
+ w.i32(trip);
882
+ // Only edges that hold something differ from an empty edge. The allowance is written as it stands at the last
883
+ // step, so the bytes do not depend on when an edge was last looked at.
884
+ let used = 0;
885
+ for (let e = 0; e < this.edges; e++)
886
+ if (this.edgeUsed(e))
887
+ used++;
888
+ w.u32(used);
889
+ for (let e = 0; e < this.edges; e++) {
890
+ if (!this.edgeUsed(e))
891
+ continue;
892
+ w.i32(e);
893
+ w.i32(this.occupancy[e]);
894
+ w.i32(this.head[e]);
895
+ w.i32(this.tail[e]);
896
+ w.f64(this.deficitAt(e));
897
+ w.i32(this.blockedSince[e]);
898
+ w.u8(this.detail[e]);
899
+ }
900
+ }
901
+ deficitAt(e) {
902
+ return Math.max(0, this.deficit[e] - this.rate[e] * Math.floor((this.lastStep - this.refillTick[e]) / this.every));
903
+ }
904
+ edgeUsed(e) {
905
+ return this.occupancy[e] > 0 || this.detail[e] === 1 || this.head[e] >= 0 || this.blockedSince[e] >= 0 || this.deficitAt(e) > 0;
906
+ }
907
+ load(r) {
908
+ const n = r.u32();
909
+ if (n !== this.n || r.u32() !== this.defHash >>> 0)
910
+ throw new Error('FlowNetwork: snapshot of other trips');
911
+ this.started = true;
912
+ this.sortTrips();
913
+ this.originTick = r.i32();
914
+ this.prepared = r.u8() === 1;
915
+ this.placeAt = r.i32();
916
+ this.placeEnd = r.i32();
917
+ this.cursor = r.i32();
918
+ this.lastStep = r.i32();
919
+ this.fx = r.i32();
920
+ this.fz = r.i32();
921
+ this.fr = r.i32();
922
+ this.focusDirty = r.u8() === 1;
923
+ this.cPending = r.i32();
924
+ this.cQueued = r.i32();
925
+ this.cIndividual = r.i32();
926
+ this.cArrived = r.i32();
927
+ this.cUnreachable = r.i32();
928
+ r.into(this.tState.subarray(0, n));
929
+ for (let i = 0; i < n; i++) {
930
+ const s = this.tState[i];
931
+ if (s !== FLOW_STATE.waiting && s !== FLOW_STATE.queued && s !== FLOW_STATE.individual) {
932
+ this.tRoute[i] = undefined;
933
+ continue;
934
+ }
935
+ this.tLeg[i] = r.i32();
936
+ this.tSeg[i] = r.i32();
937
+ this.tDue[i] = r.i32();
938
+ this.tNext[i] = r.i32();
939
+ this.tBase[i] = r.i32();
940
+ this.tMover[i] = r.f64();
941
+ }
942
+ const waiting = r.u32();
943
+ this.waiting.length = 0;
944
+ for (let i = 0; i < waiting; i++)
945
+ this.waiting.push(r.i32());
946
+ this.occupancy.fill(0);
947
+ this.detail.fill(0);
948
+ this.head.fill(-1);
949
+ this.tail.fill(-1);
950
+ this.headDue.fill(NEVER);
951
+ this.deficit.fill(0);
952
+ this.refillTick.fill(this.lastStep);
953
+ this.blockedSince.fill(-1);
954
+ const used = r.u32();
955
+ for (let k = 0; k < used; k++) {
956
+ const e = r.i32();
957
+ this.occupancy[e] = r.i32();
958
+ this.head[e] = r.i32();
959
+ this.tail[e] = r.i32();
960
+ this.deficit[e] = r.f64();
961
+ this.blockedSince[e] = r.i32();
962
+ this.detail[e] = r.u8();
963
+ if (this.head[e] >= 0)
964
+ this.headDue[e] = this.tDue[this.head[e]];
965
+ }
966
+ }
967
+ }