Skip to content

Keep the costs inside untouched chunks when the navmesh is carved again - #135

Merged
larelgit merged 1 commit into
mainfrom
claude/door-carve-freeze
Nov 26, 2026
Merged

larelgit merged 1 commit into
mainfrom
claude/door-carve-freeze

Conversation

@larelgit

Copy link
Copy Markdown
Owner

A door, a shutter or a lift that starts or stops no longer stalls the tick on a large navmesh.

A mover is static at rest and kinematic in motion, so its start and its stop each change the static solids refresh tracks. Carving then rebuilt every body class's tile graph, one class after another. Nearly all of that time (about 40 of 45 ms per class) went into build_graph running Dijkstra between the key nodes of every instance, even the ones the door never touched. On Cranium's four placed levels (44 chunks, four classes), every such tick took about 200 ms, which players felt as a hitch at each door.

Changes in shards_npc::nav::region:

  • Cached costs. NavRegion keeps each instance's key-to-key costs (InstanceCosts). build_graph measures again only the instances whose bake (Arc identity), switched-off nodes or graph key nodes changed. Positions are global ids, and base is fixed for a region's life, so every new region starts with an empty cache.
  • Rows built per node. The compressed rows are made node by node (adjacency): count, scatter, then sort each row on its own. The old code sorted every edge at once. The result is the same: rows by to, the cheaper of two duplicates.
  • Classes in parallel. refresh carves the classes at once on the compute pool, as build already makes them. Nothing on the carve path reads another class.

The graph is the same as before, so routes, replays and snapshots do not change.

Measured on Cranium, headless release at Level 1's hall:

Event Before After
One door starts / comes to rest 206 / 195 ms 9 / 11 ms
Three doors at once 214 ms 19 ms
First carve after load (tick 3) 227 ms 13 ms

Tests:

  • a_navmesh_carved_again_routes_as_one_built_now uses a 2×2 grid. Walls are put up and taken down one by one, and after each change the routes must equal those of a region built with the same walls from the start. It was checked against a broken cache that ignored the switched-off nodes: it fails there, because a stale cost sends a route through the walled tile.
  • rows_sorted_one_by_one_are_every_edge_sorted_at_once compares adjacency against the old global sort on 5000 random edges.

Checked locally:

  • cargo test -p shards_npc: 72 unit and 225 integration tests pass.
  • cargo clippy -p shards_npc --all-targets -- -D warnings is clean.
  • Cranium built against this branch: its reference replays verify identical (combat room, hub, interlude, Level 1 with 35 136 ticks, start room). The sixth replay diverges the same way on the unpatched engine, from content not yet committed there.

The workspace and sandbox suites were left to CI.

🤖 Generated with Claude Code

A door, a shutter or a lift that starts or stops changes the static solids, and
carving rebuilt every body class's tile graph from scratch, one class after
another, measuring the costs inside every tile or chunk again. On a game with
four hand-made levels (44 chunks, four classes) each such tick took ~200 ms.

- `NavRegion::build_graph` keeps each instance's key-to-key costs
  (`InstanceCosts`) and measures again only an instance whose bake, switched-off
  nodes or graph nodes changed.
- The compressed rows are made node by node (`adjacency`) instead of sorting
  every edge at once.
- `refresh` carves the classes at once on the compute pool.

The graph is the same as before, so routes, replays and snapshots do not
change. A door's tick on those levels: ~200 ms -> ~9-11 ms; three doors at
once 214 -> 19 ms.

Tests: a_navmesh_carved_again_routes_as_one_built_now (fails when the cache
ignores the switched-off nodes), rows_sorted_one_by_one_are_every_edge_sorted_at_once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@larelgit
larelgit merged commit 7427721 into main Nov 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants