Commit Graph
102 Commits
Author SHA1 Message Date
popertots a0eb608d73 fix pathfinding 2026-03-19 21:35:26 +00:00
popertots a97e2908ed fix(pathfinding): correct BinaryHeap ordering and movement threshold
- Fix PathNode and ChunkPathNode Ord impl to use correct min-heap ordering
  (self.f_score.cmp vs other.f_score.cmp) - was causing zig-zag paths
- Fix movement threshold formula to use tile_weight directly
  (walk_speed * tile_weight / 50) instead of inverted multiplier
- Update comments to reflect 'higher = slower to traverse' semantics
2026-03-19 20:27:36 +00:00
popertots c4890fa23e config cleanup 2026-03-19 20:17:15 +00:00
popertots d29a3e793f docs: add session summary and 48h diff for handoff 2026-03-19 19:16:27 +00:00
popertots 9535d65bd7 feat: data-oriented chunks optimization with tile registry
- Phase 1: Bit-packed standability (ChunkData with bitsets)
- Phase 2: Reactive connectivity (dirty chunks)
- Phase 3: Async terrain baking (AsyncComputeTaskPool)
- Pathfinding weight system (rock=50 preferred, bedrock=150 avoided)
- Movement speed affected by tile weight
- External tiles.toml for hot-reloadable tile definitions
- TileRegistry singleton for async-safe tile lookups
- Fixed world_to_chunk to use CHUNK_SIZE_TILE (128) not CHUNK_SIZE (8)
- Fixed infinite spawner with Local<bool> state guards
- Fixed spawn coordinate grid alignment

Note: Zigzag pathfinding bug introduced - needs investigation
2026-03-19 19:15:36 +00:00
popertots 50788af3c7 feat(optimization): implement data-oriented chunk architecture
Phase 1: Bit-packed standability
- Add ChunkData struct with 4 bitsets per chunk (stand_in/on for floor/fixture)
- Replace 4 HashMap lookups per standability check with O(1) bit operations
- Memory: ~2KB bitsets per chunk vs ~50KB HashMap overhead

Phase 2: Reactive connectivity
- Add dirty_chunks HashSet to ChunkMap for incremental updates
- update_chunk_connectivity now O(d) where d = dirty chunks
- Early exit when no changes, preventing O(N) full rebuilds

Phase 3: Async terrain baking
- Move terrain generation to AsyncComputeTaskPool
- spawn_terrain_tasks: non-blocking task spawn (~34µs)
- apply_terrain_blobs: batched entity spawn on main thread
- Eliminates main-thread stutters during world generation
2026-03-19 17:01:51 +00:00
popertots 15c0d1c7a2 external config 2026-03-18 23:19:58 +00:00
popertots a5154f73dd docs(pathfinding): add documentation and remove unused async infrastructure
- Add comprehensive module documentation explaining tier architecture
- Add TileMap documentation explaining design choices (FxHashMap, packed data)
- Remove unused async pathfinding code (StandableTileSnapshot, spawn_async_path_task)
- Remove unused imports (check_ready, rayon, StdHashMap)
- Fix unused variable warnings with underscore prefixes
- Add #[allow(dead_code)] for intentionally unused fields
2026-03-18 22:08:27 +00:00
popertots 4d6692c432 cleanup 2026-03-18 21:56:15 +00:00
popertots ea448a03b9 bench: final benchmark after Phase 3 reactive pathing
Results (n=37,707):
- avg=256µs, median=142µs, p95=357µs
- 96% of paths complete in <500µs
- 99.6% complete in <1ms
- 100% success rate (6 failures)

Performance improvements from baseline:
- P99: -99.6% (80ms → 357µs)
- Max: -57% (80ms → 34ms)
- Avg: -39% (418µs → 256µs)

Implementation complete:
- Phase 1: Chunk-Graph Layer
- Phase 2: Async Infrastructure (ready)
- Phase 3: Reactive Pathing
2026-03-18 21:53:41 +00:00
popertots 49f91de0c9 feat(pathfinding): implement Phase 3 reactive pathing
- Add validation_cooldown field to Ambulatory component
- Implement validate_next_steps to check walkability of next 3 path nodes
- Integrate validation into movement system (every 10 frames)
- Trigger re-path when validation fails (path blocked or invalid)
- Entities now detect and recover from invalid paths automatically
2026-03-18 21:33:38 +00:00
popertots a6f6e7cb9d feat(pathfinding): implement hierarchical task-based pathfinding
Phase 1 - Chunk-Graph Layer:
- Add world_to_chunk, chunk_to_world, get_chunk_neighbors helpers
- Add update_chunk_connectivity system to build chunk adjacency graph
- Implement calculate_chunk_path for macro A* on chunk coordinates
- Wire hierarchical tier dispatch into prepare_paths

Phase 2 - Async Infrastructure (ready for integration):
- Add StandableTileSnapshot for chunk-local tile data copy
- Add AsyncPathTask component for Task handle storage
- Add spawn_async_path_task and poll_async_path_tasks functions

Performance improvements:
- Before: avg=418µs, median=175µs, max=80ms, p95=791µs
- After: avg=244µs, median=140µs, max=16ms, p95=355µs
- 87% reduction in tail latency, 41% faster average
2026-03-18 21:26:47 +00:00
popertots 5704942950 chore: add benchmark analysis scripts 2026-03-18 18:12:33 +00:00
popertots e9ef5ebbcd fix: four interlocking pathfinding bugs causing panic, failure, and yo-yo teleportation
1. CSV Overflow Panic: Use saturating_sub to prevent underflow when total_failed_paths > n
2. Heuristic Scale Mismatch: octile_distance_3d now divides by ITILE_SIZE to match g-score units
3. Goal Z Offset: Correct target.as_ivec3() - ivec3(0,0,1) not ITILE_SIZE (target already has +1.0)
4. Gravity Yo-Yo: Clear current_path and target when entity falls to prevent teleportation loop
2026-03-18 17:49:07 +00:00
popertots 00cbd92491 refactor: simplify to time-sliced synchronous path queue
The async pathfinding with StandableBitGrid caused massive lag due to synchronous
bounding box calculation in the main thread (millions of hashmap lookups for long paths).
Additionally, the splicing logic had a catastrophic bug where it applied a single
finished path to ALL entities unconditionally.

Solution:
- Revert to thread-local synchronous A*
- Implement PathRequestQueue to process max 8 paths per frame
- Keep provisional paths for immediate movement
- Since entity walks provisional path, full path calculates from current position,
  eliminating the need for complex splicing logic.
2026-03-18 17:23:48 +00:00
popertots 31c93f7926 fix: remove goal standability check from provisional path
The is_standable_tile(goal) check was causing failures when goal position
wasn't directly standable (common case). A* doesn't require goal to be
standable - it finds the best path it can. Removed the check and return
fallback single-point path when start isn't standable.
2026-03-18 17:12:05 +00:00
popertots 9379c7be03 fix: add benchmark recording to calculate_provisional_path
Provisional paths were not being recorded to benchmark stats, causing
total_paths=0 while failed_paths accumulated. Now records:
- path_calc_times_us (timing)
- path_lengths (path length)
- nodes_expanded (A* nodes visited)
- failed_paths (when start/goal not standable)
2026-03-18 17:00:29 +00:00
popertots d2ec1fb5dc fix: correct inverted logic in prepare_paths - entities without paths were being skipped
The condition 'if ambulatory.current_path.is_none() || pending_async.is_some()' was backwards.
It should skip entities that ALREADY have a path, not entities WITHOUT a path.

Fixed to properly skip entities with existing paths or pending async tasks.
2026-03-18 16:47:51 +00:00
popertots c902cff908 feat: async pathfinding with provisional paths and bit-grid snapshots
- Add StandableBitGrid: O(1) bit-packed snapshot (~6KB per 50k tiles vs HashMap overhead)
- Implement two-tier pathfinding: sync for short paths (<64 tiles), provisional+async for long paths
- calculate_provisional_path: capped A* returning path to best heuristic node
- calculate_async_path: A* using bit-grid (Send+Sync, no thread_local)
- prepare_paths system: dispatches provisional paths immediately, spawns async for full paths
- poll_async_paths + splice_completed_async_paths: seamless path transition when async completes
- Entities start walking immediately on provisional path while full path computes in background

Architecture:
  FixedUpdate: prepare_paths → update_wandering_targets → movement
  PostUpdate: poll_async_paths → splice_completed_async_paths

Priority: DF-like pathing (immediate movement) > performance > memory
2026-03-18 16:35:39 +00:00
popertots 79a386afa6 chore: add benchmark verification script and current benchmark data 2026-03-18 16:15:19 +00:00
popertots 0550191c7a Fix update_wandering_targets target validation logic
- Restored original target selection logic that validates is_standable_tile
- Fixed target position z-level checking (check floor tile then standable)
- Re-added check for completed paths (path_index >= path.len)
- This was the cause of 50,297 failed paths - entities targeting invalid positions
2026-03-18 15:45:25 +00:00
popertots 367ab26d5e Fix performance regressions: remove TIER0, consolidate scratchpads, remove Arc trap
Key fixes based on benchmark analysis:
1. Remove TIER0 Vec-based pathfinding - O(N) linear scan was slower than FxHashMap for typical path lengths
2. Consolidate scratchpads into single AStarScratchpad struct - eliminates nested RefCell borrow overhead
3. Remove Arc wrapper from TileMap - eliminated Copy-on-Write trap causing 63ms stutters
4. Replace AHashMap with FxHashMap - FxHash is faster for small integer keys like IVec3
5. Simplify tier logic - single pathfinding function with scratchpad reuse

Benchmark analysis showed:
- Original: 1.95 µs/node, P99 1.1ms, Max 5ms
- TIER0/TIER1 regression: 2.89 µs/node (+48%), P99 5ms (+348%)
- Root causes: Vec linear scan in TIER0, nested RefCell borrows, Arc::make_mut CoW

This should restore and improve performance by using simple FxHashMap scratchpad for all paths.
2026-03-18 15:30:48 +00:00
popertots 466a20700a Wrap TileMap HashMaps in Arc for async pathfinding access
- Add Arc<AHashMap> wrapper around floor_tiles, fixture_tiles, item_tiles
- Copy-on-write semantics: Arc::make_mut clones only if other Arcs exist
- Add insert_floor, insert_fixture, insert_item, remove_item methods
- Add get_floor_mut, get_fixture_mut for visibility updates
- Update all mutation sites to use new TileMap methods
- Enables cheap Arc::clone for async pathfinding workers
- Single-threaded pathfinding: no clone, direct access
- Multi-threaded pathfinding: clone Arc, read without locks
2026-03-18 14:56:57 +00:00
popertots 8478b385bc Optimize TileMap: replace HashMap with AHashMap, pack tile data
- Replace std HashMap (SipHash) with ahash::AHashMap for fast non-crypto hashing
- Pack tile data from tuples to structs: FloorTileData (~35 bytes) and FixtureTileData (~18 bytes)
- FloorTileData: pack 3 bools into single flags byte, use u8 for id/weight
- FixtureTileData: pack 2 bools into single flags byte
- Update all accessors: is_standable_tile, visibility, terrain generation, forestry
- Preparation for async pathfinding (Arc wrapping to come in follow-up)

Memory reduction: ~54% for floor tiles (76→35 bytes), ~62% for fixtures (48→18 bytes)
Hash performance: AHashMap uses fxhash, faster than SipHash for game data
2026-03-18 14:52:46 +00:00
popertots 81b70a661c Implement tiered pathfinding with thread-local scratchpads
- TIER0 (<10 tiles): Vec-based linear search, zero hash overhead
- TIER1 (10-30 tiles): FxHashMap scratchpad with memory reuse
- TIER2 (30-100 tiles): AHashMap scratchpad for better collision resistance
- TIER3 (>100 tiles): Chunk waypoint decomposition
- Added benchmarking infrastructure with F8 report trigger
- Added async infrastructure: PendingPath, CompletedPaths resources
- Lowered MAX_NODES to 5000 to prevent runaway searches

Benchmarks show P50 improved 13%, max improved 53%
2026-03-18 14:37:15 +00:00
popertots 49972b9b64 better config for benchmarks 2026-03-18 11:21:40 +00:00
popertots aeef78eca2 Fix tiled patching 2026-03-18 09:22:47 +00:00
popertots b0b0ca0390 optimize visibility calcs 2026-02-28 11:27:50 +00:00
popertots b1de9f95c2 quilted chunk optimisations 2026-02-28 00:07:07 +00:00
popertots 4ef68c4e04 migratetion to 0.18.0 2026-02-16 00:02:00 +00:00
popertots a9040a4c9e optimize item removal 2026-02-15 19:18:52 +00:00
popertots 886d162963 dropped item rotation 2026-02-15 18:56:38 +00:00
popertots 975ac20876 minor 2025-09-14 19:04:34 +01:00
popertots 4c4c985be3 item flashing 2025-09-14 18:48:50 +01:00
popertots 8a5884c6d5 example item 2025-09-12 18:04:03 +01:00
popertots ee7ac39ad5 refactor file locations for readability 2025-09-11 18:23:53 +01:00
popertots e58f6804bf File cleanup 2025-09-10 20:03:29 +01:00
popertots cbc8011f33 Art 2025-09-10 18:11:01 +01:00
popertots feed403495 Update bevy version, implement bevy_rand 2025-09-07 20:05:00 +01:00
popertots 7e52d6e1bd fix zoom, rework chunkmap events 2025-05-29 16:22:02 +01:00
Stephen Adamson 0def6af6e8 fix scroll 2025-05-28 11:03:57 +01:00
popertots e8d3f1bafb prep bench 2025-05-27 23:28:25 +01:00
popertots 950f02c2e1 rework pathfinding, game entity fog rendering, and tilemap usage 2025-05-27 23:10:54 +01:00
popertots d2dd70caa0 add trees 2025-05-23 17:09:58 +01:00
popertots ac033de136 rework occlusion events 2025-05-23 14:01:28 +01:00
popertots a245f35332 Refactor visibility system to use component-based approach
- Introduce VisibleGameEntity component for entities needing visibility logic
- Move Z-index based visibility calculations to dedicated system
- Update both citizens and logs to use new visibility system
- Remove duplicated visibility logic from citizen movement system
- Add compute_visibility_of_game_entities system to Update phase

This improves modularity by separating visibility concerns from movement systems.
2025-05-22 13:06:27 +01:00
popertots a83f71addd Fix pathing 2025-05-12 21:49:10 +01:00
popertots e2ccd0cead Log (wip), parallelisation 2025-05-11 22:58:25 +01:00
popertots 31e6eb29b2 camera zooming 2025-05-09 18:13:28 +01:00
popertots e3ea662668 fake fog, better parallelisation 2025-05-07 20:13:29 +01:00