Skip to content

PWA1: re-query only the pins a navigation change can reach - #6

Merged
kramsey458 merged 3 commits into
mainfrom
claude/pin-refresh-planner
Sep 22, 2026
Merged

kramsey458 merged 3 commits into
mainfrom
claude/pin-refresh-planner

Conversation

@kramsey458

Copy link
Copy Markdown
Collaborator

Summary

Before this change, any navigation update, selection change, pin change or visible-level change marked every pin dirty. The next pass then re-ran a range query for every pinned building and rebuilt the whole outline mesh. The game's range service refills one shared flow field per query, so with two or more pins every one of those queries was a full refill. Selection changes also skipped the refresh throttle.

This PR adds PinRefreshPlanner, which has no Unity dependency and is linked into tests/Checks.csproj. Each pin now keeps its cached cells, the key its query depends on (access point, live or preview graph, terrain or road spill) and a box of access ± (NavigationDistance.ResourceBuildings + 2). That is the box BuildingTerrainRange.UpdateBoundingBox uses; in 1.1.2.4 it is 22 cells.

  • Navigation update: only pins whose box meets NavMeshUpdate.Bounds are re-queried. Road-spill ranges follow the district road network, so they have no bound and are always re-queried.
  • Every pass: each pin's key is read again, and a pin whose access or graph changed is re-queried. This is how a pinned construction site that finishes gets picked up.
  • Outline rebuild: happens only when the combined cells change, or when an update touches the cached cells. A blocking object or preview there changes the drawing even if the cells stay the same.
  • Selecting or deselecting a pinned building: rebuilds the outline from the cache with 0 queries. Selecting an unpinned building does no work. A selected pin's query waits until it is deselected.
  • Construction-mode toggle: still re-queries every pin, because it moves buildings between the live and preview graphs.
  • Visible-level change: only redraws. The range services never read ILevelVisibilityService; only the drawing code does.

All queries still run in UpdateSingleton. Passes started by navigation changes stay capped at 5 per second. Pin, selection, construction-mode and visible-level events still start the next pass right away, as in 1.0.0, but those passes now query only stale pins. The navigation listeners only mark the mod's own per-pin records. No tick, save or persistence interfaces were added, and the checks still enforce that. This is display only.

Items

PWA1: any navigation change re-queries every pin and rebuilds the whole outline mesh (fixed)

  • Commits:
    • 7a1c32b adds the planner, moves WorkAreaService onto it and adds 23 checks.
    • bb694dd adds checks of the service wiring, added after review.
  • Tests: in tests/Program.cs.
    • The pure section drives the planner through a fake navigation world that counts range queries and outline rebuilds:
      • a navigation change that misses every pin: (0 queries, 0 rebuilds);
      • a change near pin A only: (1, 0), or (1, 1) when its range changes;
      • selecting a pinned building: (0, 1); deselecting it: (0, 1);
      • selecting an unpinned building does nothing;
      • construction toggle: (3, 1) for 3 pins; visible level: (0, 1);
      • a key change caused by an update elsewhere: (1, 0);
      • also covered: stale selected pins wait, road spill is unbounded, unpinning, dropped pins, identity, clear.
    • The game section:
      • runs the service's real Touches delegate against the game's BoundingBox with an asymmetric box, which catches swapped axes and checks that edges are inclusive;
      • builds a real WorkAreaService outside the game and checks the wiring through reflection: reach = ResourceBuildings + 2; visible-level events redraw without a query; construction-mode events mark every pin; live and preview navigation updates mark a pin only inside its reach and stay throttled; deselecting rebuilds from the cache.
  • Fails before:
    • With the planner file missing, the tests don't build (CS2001).
    • With a stub planner that has the same API but the 6197119 policy (every event re-queries every non-selected pin and redraws), 15 of the 21 planner checks fail on their query or rebuild counts. For example, a change near pin A only gave (3, 1), and selecting a pin gave (2, 1). The first failure is FAIL: Navigation change missing every pin: 0 queries and 0 rebuilds.
    • I mutated each piece of service wiring and every one fails its check: reach 1 instead of ResourceBuildings + 2; no redraw on visible level; Redraw instead of InvalidateAll on construction; an empty preview listener; no Select(null) on deselect; the Touches result inverted.
  • Passes after: 67/67.

Tests

Checks: build.ps1 -GameDir <Timberborn>, then dotnet run --project tests/Checks.csproj -c Release -- <Timberborn> source/bin/Release/netstandard2.1/PersistentWorkAreas.dll packaging/PersistentWorkAreas/version-1.1.

Suite Before (6197119) After (bb694dd)
Checks 38/38 67/67
Mod build 0 warnings, 0 errors 0 warnings, 0 errors
Packaging ok ok

Of the 29 new checks, 21 are pure planner checks, 1 is the game BoundingBox check and 7 check the service wiring.

Co-op impact

None. This is display only.

  • It adds no simulation, save, settings or wire-format change, and no Harmony patches.
  • Range queries run only in UpdateSingleton, as before, and there are fewer of them.
  • The simulation's range results don't depend on query history. AccessFlowField.OnNodesChanged clears the shared field on any change it can reach, and each fill is a fresh search.
  • The navigation listeners only change the mod's own fields and cannot throw. BoundingBox.Builder.Build throws only when nothing was added, and ToBoundingBox always adds two points.
  • Co-op players do not need to update together. BeaverBuddies only logs a warning when mod versions differ.

Proposed CHANGELOG lines

  • Pinned outlines refresh faster with many pins: a path or terrain change now re-checks only the pinned buildings it can reach, and selecting a pinned building no longer recalculates every pin. (Display only; co-op players do not need to update together.)

Release-time follow-ups (not in this PR)

  • README.md ("38 automated lifecycle/API checks") and VALIDATION.md ("38 checks passed") describe the 1.0.0 release, so they are unchanged here. At the next release, set the count once, after PWA2 has landed too, since it adds more checks. This PR alone makes it 67.
  • docs/troubleshooting.html and docs/faq.html describe the released 1.0.0 behaviour, so they are left alone. Two lines there need updating when this ships:
    • "Outlines refresh when ... your selection change[s], at most five times per second" could become: "Outlines recalculate when terrain, paths or construction mode change near a pinned building; selection and visible-level changes redraw from the last result."
    • The 5-per-second cap applies only to passes started by navigation changes; other events start the next pass right away, both before and after this change.
  • PWA2 (claude/localize-ui) will also change the WorkAreaService constructor and tests/Program.cs. Whichever PR lands second needs a small rebase.

Needs in-game testing (solo; two-player not needed)

  • Performance: pin about 10 farmhouses and foresters. Drag long path previews far from every pin, then near one pin, and compare frame time with 1.0.0. There should be no range queries while dragging away from every pin.
  • A path built through a pinned forester's range updates its outline.
  • A pinned construction site that finishes switches to its finished range.
  • Selecting and deselecting a pinned building: the outline passes to the game's own outline and back instantly.
  • Raising and lowering the visible level redraws the pinned outline correctly.
  • Toggling construction mode switches pinned outlines to the preview graph and back.
  • Placing a blocking building inside a pinned range updates the drawn outline.
  • Unpinning or deleting a pinned building removes its range. Clear-all still works.
  • If you have a building with a road-spill range pinned, its outline still follows road changes anywhere in its district.

Review

Three adversarial reviewers approved 7a1c32b.

  • Determinism and lockstep co-op (approve, no findings).
    • A differential fuzz compared the planner with brute-force re-querying of every pin: 3000 seeds × 400 steps, with 0 differences and 945k queries instead of 3.99M.
    • Mutations to the redraw and stale-marking logic were caught within 15 to 113 steps.
    • The reviewer confirmed that query order cannot affect simulation and that the listeners cannot throw.
  • Save compatibility and cross-mod interaction (approve, 4 nits).
    • The reviewer confirmed no save or settings change. The NavigationDistance constructor argument resolves in the Game context.
    • It also confirmed that MultiColony's InstantNavMeshFix and LateGamePerformance's terrain flow fields don't interfere.
    • Fixed: the check that pinned ResourceBuildings + 2 == 22 could block packaging after a game update, even though the service reads the value at run time. It is replaced by a check of the service's own reach.
    • Moved to the release follow-ups above: the README and VALIDATION check counts, the troubleshooting wording, and the PWA2 rebase.
  • Is the test real (approve, 1 minor, 2 nits).
    • Fixed, minor: the service wiring was untested, and 8 mutations of it still passed 61/61. bb694dd adds 7 checks that build a real WorkAreaService and call its handlers; the listed mutations now fail. Three pieces still need Unity components and stay on the in-game checklist: OnSelected (the event's GetComponent needs the game's component cache), the Describe branches (road spill and WorldToGrid), and whether the service acts on the planner's redraw result.
    • Fixed, nit: the check "Cell boxes are inclusive like the game's BoundingBox" tested a helper that only the fake world uses. It is renamed; the game BoundingBox check covers production code.
    • Release follow-up, nit: the README and VALIDATION counts.
  • Declined: none.

🤖 Generated with Claude Code

kramsey458 and others added 2 commits September 22, 2026 01:49
Every navigation update, selection change, pin change and visibility
change marked the whole pin set dirty, so the next refresh re-ran a range
query for every pin and rebuilt the combined outline mesh. The game's
range service reuses a single flow field, so with two or more pins each
of those queries was a full refill. Selection changes were not throttled.

Add PinRefreshPlanner, a Unity-free planner linked into tests/Checks.csproj.
Each pin keeps its cached cells, the key its query depends on (access
point, live or preview graph, terrain or road spill) and a bounding box of
access +/- (NavigationDistance.ResourceBuildings + 2), the same box
BuildingTerrainRange uses to skip navigation updates (22 cells in 1.1.2.4).

- A navigation update re-queries only pins whose box meets
  NavMeshUpdate.Bounds. Road-spill ranges follow the district road network,
  so they have no bound and are always re-queried.
- Every pass re-reads each pin's key and re-queries pins whose access or
  graph changed, which catches a finished construction site cheaply.
- The outline is rebuilt only when the combined cells change, or when an
  update meets the cached cells (blocking objects and previews there change
  the drawn outline even when the cells do not).
- Selecting or deselecting a pinned building rebuilds the outline from the
  cache with no queries. Selecting an unpinned building does nothing. A
  selected pin's query waits until it is deselected.
- Construction-mode toggles still re-query every pin; visible-level changes
  only redraw, because range queries do not depend on the visible level.

All queries still run in UpdateSingleton at most five times per second.
The navigation listeners only mark the mod's own per-pin records. No tick,
save or persistence interfaces are added. Display only.

Checks: 23 new, 61 total. The planner checks count queries and rebuilds in
a fake navigation world, and a game check drives the service's real
BoundingBox test with an asymmetric box.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review found that the planner checks drive PinRefreshPlanner through a
fake world, so a regression in WorkAreaService's glue (the reach margin,
which planner call each event makes, the preview navigation listener)
still passed every check.

Build a WorkAreaService outside the game (its constructor only stores its
dependencies and reads NavigationDistance), add an entity to its planner,
and call each handler through reflection:
- the pin reach is ResourceBuildings + 2, read from the game at run time;
- a visible-level change redraws without marking the pin for a query;
- a construction-mode change marks every pin for a query;
- live and preview navigation updates mark the pin only inside its reach
  and keep the 0.2 s throttle;
- deselecting a pinned building rebuilds the outline from the cache.

This replaces the check that NavigationDistance.ResourceBuildings + 2 is
exactly 22: it pinned a game constant the service already reads at run
time, so a game change there would have blocked packaging a working build
while a dropped margin in the service would still have passed. The
fake-world CellBox check is renamed, since production code tests bounds
with the game's BoundingBox.

Each new check fails when its handler is mutated (reach 1, no redraw on
visible level, redraw instead of invalidate on construction, empty
preview listener, no deselect). Checks: 67 (was 61).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- WorkAreaService constructor: keep PWA1's NavigationDistance parameter,
  _reach and _planner alongside PWA2's ILoc.
- tests/Program.cs: keep the planner checks above the game-free split so
  CI runs them, and raise GameChecks from 34 to 42. Full run: 74/74;
  no-argument run: 32 passed, 42 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kramsey458
kramsey458 marked this pull request as ready for review September 22, 2026 16:40
@kramsey458
kramsey458 merged commit 1a35862 into main Sep 22, 2026
1 check passed
kramsey458 added a commit that referenced this pull request Sep 22, 2026
- WorkAreaService constructor: keep PWA4's EntityComponentRegistry and
  PWA2's ILoc.
- tests/Program.cs: move the planting-rule and planner Show checks above
  the game-free split so CI runs them, and raise GameChecks to 56.
  Full run: 104/104; no-argument run: 48 passed, 56 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kramsey458 added a commit that referenced this pull request Sep 22, 2026
- WorkAreaService constructor: keep PWA2's ILoc alongside PWA3's
  EntityRegistry and SettlementReferenceService, and keep the localized
  clear button next to the pin-file restore in PostLoad.
- tests/Program.cs: keep the PinStore checks above the game-free split,
  raise GameChecks to 65, and allow the pin file's format, file names
  and argument-check text in the hard-coded UI text check. Full run:
  142/142; no-argument run: 77 passed, 65 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kramsey458 kramsey458 mentioned this pull request Sep 22, 2026
@kramsey458
kramsey458 deleted the claude/pin-refresh-planner branch September 22, 2026 16:50
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.

1 participant