Skip to content

Make the world tick: provinces, expansion and time controls - #36

Merged
CodeByBryant merged 7 commits into
mainfrom
claude/kind-edison-2locd1-provinces
Nov 24, 2026
Merged

CodeByBryant merged 7 commits into
mainfrom
claude/kind-edison-2locd1-provinces

Conversation

@CodeByBryant

Copy link
Copy Markdown
Owner

Summary

Roadmap phase 3 core: World.step() is no longer empty. The land is cut into provinces by travel cost, nations own provinces and grow into free ones beside them (no randomness), and the app has play / pause, four speeds and a date. Exit criterion met: one seed grows borders that settle, and replaying it (or saving in the middle and restoring) gives the same hash. Design, numbers and limits: docs/decisions/0007-provinces-and-expansion.md.

Changes

  • Provinces (terrain/provinces.ts): seeds on cheap ground a provinces.spacing apart, one tile-level Dijkstra in travel cost, small provinces merge into the neighbour with the longest border. About 2,100 provinces on 2000 x 2000 (median ~465 tiles). Derived from terrain + config, never saved, rebuilt on create and restore.
  • Nations own provinces: Nation.provinces is province ids in claim order; a nation's profile is the sum of its provinces' (provinceProfile.ts), stats re-derived on every change. Starting territories grow over the province graph. Fixed a stranded-province bug found by an invariant test.
  • Province facts follow resource stocks (extract / regrow mark provinces dirty), fixing a divergence between a created world and one restored after an extraction.
  • ExpansionSystem: points from rate, expansionism and population; buys the best value-per-price free province on its border; rotating service order; saturates when nothing reachable is free (World.saturated).
  • App: worker run / ticked protocol, TickRunner (overlay throttled to 250 ms, flushed on pause / view switch / settle), Pacer, transport bar in @sovereign/ui, Space and +/- keys, province lines in the political overlay, nation panel shows provinces and area. CLI summary reports provinces and whether borders settled.
  • Versions: hash schema 4, save format 5, IndexedDB 6 (older saves dropped, pre-1.0). Config gains provinces and expansion.
  • Goldens regenerated in their own commit (847757d) because the hash layout and every nation changed; two new entries pin a world still growing and one that has settled.
  • Guard + docs: tests/determinism/provinces-expansion.test.ts (in a CI shard), ADR 0007, terrain / nations / save-format / roadmap / development docs, README.

Related Issues

None.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change) — every world, save, hash and golden changes (pre-1.0)
  • Refactor / cleanup (no functional changes)
  • Documentation update

Checklist

  • Builds (pnpm build) — not run
  • Linting and the dependency rules pass (pnpm lint)
  • Type checking passes (pnpm typecheck)
  • Tests pass (pnpm test; and pnpm test:determinism if the engine changed)
  • Tested locally in the web app (and the desktop shell if it changed) — Playwright in software GL: new e2e:time plus existing e2e and e2e:mobile; desktop shell not exercised
  • If a golden hash moved, it is regenerated in its own commit and the reason is above
  • Updated documentation / JSDoc where applicable

CI has not run on this branch yet; everything above was run locally.

Verification and measurements

  • Cross-engine: Node 22, Chromium 157 and QuickJS reproduce all 27 pinned values (goldens, depletion golden, save round-trips, and a world saved mid-growth that continues exactly like the original). The full 2000 x 2000 world, seed 1337, after 3,000 ticks hashes to 7641ec2a09f9110eb62ffa6d586f2aac on Node and Chromium.
  • 2000 x 2000, 12 nations: a tick costs ~0.006 to 0.008 ms; nations settle after ~20,800 ticks (57 years) holding 89% of claimable provinces (the rest is on landmasses nobody can reach by land).
  • Mutation checked; the few survivors are equivalent no-ops or performance-only.

Honest limits

  • Borders are pulled toward barriers, not drawn on them. Against a control (same seeds, plain distance), rivers are ~1.4 to 1.6x as common beside a border (control ~1.0) and steep ground up to ~1.3x; mountain biomes are not enriched. Provinces are even-sized, so a border slides by at most half a province. The guard tests the pull against the control.
  • Creation is ~1.6 s slower (6.5 s to ~8 s, over the +1 s budget I set) and loading a save ~1.3 s instead of 0.16 s, because the derived province layer is rebuilt each time.
  • Nations never fight or cross water yet; the economy (phase 4) is out of scope.
  • The political overlay is rebuilt whole (16 MB) at most every 250 ms while that view is on screen; e2e runs in software GL so it says nothing about real frame rate.
  • Expansion rate, price and value weights are a first design tuned with a probe.

Generated by Claude Code

…vel cost)

On 2000 x 2000 worlds a tile-level multi-source Dijkstra takes about 0.45 s; a 20 tile seed spacing gives
about 2,100 provinces of a median 460 tiles (p10 320, p90 665); at 30 tiles islands start to go unseeded.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…el cost

buildProvinces runs one multi-source Dijkstra over land tiles (own heap, total order) from seeds spread on
cheap ground, so every province is connected and borders fall on mountains, rivers and coasts. Every landmass
gets a seed; provinces under minArea merge, smallest first, into the neighbour with the longest shared border;
a speck with no neighbour belongs to nobody. Provinces carry CSR tile lists, area, centroid, mean cost and
neighbours with border lengths. Config gains provinces { spacing 20, minArea 40, costCap 12 }.

Tests on drawn worlds, mutation checked (every mutant killed except a no-op).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
A nation now owns provinces (ids, in the order it claimed them) instead of tiles. The world derives its
province layer (the cut and what each province is like) from the terrain on create and on restore; it is
never saved.

- provinceProfile: per-province partial profiles (sums, plus a bit mask of resource kinds for diversity),
  and combineProfile, so a nation's profile is a sum over its provinces. Tested equal to measureProfile on
  the union of tiles, and mutation checked.
- territory: starting territories grow over the province graph (centre distance x mean cost), all nations at
  once, each stopping at the first province that takes it to nations.startArea. Found and fixed a stranded
  province: when the nation reaching for it filled up, a nation with room beside it was never offered it.
- spawner: provinces that are mostly ice are not claimable; two capitals in one province keep the better hub.
- World: provinceOwner, ownership written through, validate() checks province ids, double holders, capital,
  ownership per tile, profile = sum of provinces, stats = deriveStats(profile), expansion points.
- Nation gains expansionPoints; snapshots gain provinceCount.
- Hash schema 4 (province fingerprint, expansion points, province ids; vectors recomputed with the
  independent Python reference) and save format 5.

Goldens are not regenerated yet: they are pinned once, after expansion lands. Measured at 2000 x 2000:
about 2,100 provinces (median 465 tiles), nations start with 2 to 4 provinces (1,200 to 2,000 tiles),
creation 7.0 to 7.8 s (was 6.5 s).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
ExpansionSystem runs first in World.step(). Each tick a nation earns expansion points (config
expansion.rate, scaled by 0.5 + expansionism and the square root of population/100) and spends them on the
free, claimable province beside its land that is the best value for the price (price = costPerTile x area x
mean cost). No chance anywhere: ties go to the lower province id and nations are served in a rotating order
(start = tick mod count), so who wins a contested province alternates. Claims keep the province lists, the
owner of each province and tile, and each nation's profile and stats (re-summed and re-derived) in step.
Once every reachable province is taken nothing changes any more (saturated).

Province facts now follow resource stocks: a stock change (extract or regrow) marks the province and those
beside a changed water tile dirty and they are measured again, so facts always equal a fresh computation
from the current state. This fixes a real divergence a restored world showed (facts snapshotted at
creation vs. at restore) once nations started to change.

claimableProvinces (mostly-ice rule) is shared by the spawner and the expansion system.

At 2000 x 2000 (seed 1337, 12 nations): a tick costs 0.006 ms; nations saturate after 20,825 ticks (57
years) holding 89% of the claimable provinces, the rest being on landmasses nobody can reach by land;
validate() is clean throughout.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…nes on the map

The worker answers run { ticks } with the clock, the nations as they are and, when provinces changed hands, the
ownership layer and (only while the political view is on screen) the political overlay. host/tickRunner.ts
throttles the overlay to every 250 ms and flushes held-back updates on pause, on a switch to the political
view and when the borders settle; host/Pacer.ts turns frame time into whole ticks (a stall catches up by at
most 250 ms, a request is capped), one request in flight at a time.

The store keeps paused (a new world starts paused), speed (1, 10, 100, 1000 days a second), the clock and
whether the borders have settled. @sovereign/ui has a transport bar (date in 365-day years, play / pause,
speeds, 'Borders settled') that wraps on narrow screens, and the nation panel shows provinces and area. Space,
+ and - work from the keyboard; a mouse or touch click releases the button's focus so Space still pauses.
politicalRgba draws the lines between a nation's provinces faintly inside it. The snapshot carries the
timestep, IndexedDB is version 6, and the CLI summary reports provinces and whether the borders have settled.

Checked in a real browser (software GL): pnpm --filter @sovereign/web e2e:time (desktop play / pause / speeds
/ keys, a nation growing in its panel until the borders settle; phone portrait and landscape layout and
touch), and the existing e2e and e2e:mobile still pass.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…ces and grow

Hash schema 4. Every entry gains the province sizes of a test-sized map; two entries are new: a world whose
nations are still expanding (seed 11, 300 ticks) and one whose borders have settled (seed 7, 2000 ticks), with
tests that they are what they say. The depletion golden is re-pinned (the same 7,358 units are taken).

Node 22, Chromium 157 and QuickJS reproduce all 27 values (these entries, the depletion golden, a save
round-trip of each, and a world saved mid-growth that continues exactly like the original); the full
2000 x 2000 world for seed 1337 after 3,000 ticks hashes to 7641ec2a09f9110eb62ffa6d586f2aac on Node and
Chromium.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…at moved

tests/determinism/provinces-expansion.test.ts (in a CI shard) holds three 1000 x 1000 worlds to the design:
provinces are connected and sized sensibly; their borders are pulled toward costly ground more than the same
seeds cut by plain distance (a control: rivers 1.4 to 1.6 times as common beside a border, against 1.0 to
1.06); the nations settle, and then nothing changes; a replay and a save in the middle give the same hash.

ADR 0007 records the design and its honest limits: the pull on borders is modest (provinces are even-sized, a
border can slide by half a province; mountain biomes are not enriched), creation and load are about 1.6 s
slower (the layer is derived and rebuilt), and nations never fight or cross water. The terrain, nations,
save-format, roadmap, development and README docs and the docs index are updated, and the performance table
is re-measured.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@CodeByBryant
CodeByBryant merged commit 5490460 into main Nov 24, 2026
14 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.

3 participants