Skip to content

Overhaul resources: 22 resources in two slots, placed by geology, with depletion - #34

Merged
CodeByBryant merged 13 commits into
mainfrom
claude/kind-edison-2locd1-resources
Nov 20, 2026
Merged

CodeByBryant merged 13 commits into
mainfrom
claude/kind-edison-2locd1-resources

Conversation

@CodeByBryant

Copy link
Copy Markdown
Owner

Summary

Resources were noise. Every land tile held at most one resource, mostly a recolour of its biome (every forest was timber, every grassland fertile soil) at a density from a noise layer that related to nothing; iron and gold were one threshold over mountain biomes; there was no coal, oil, copper or any luxury; fish existed only on land beside water; and there were no quantities. About 64% of land carried a resource, and fish clustered at 0.29 (speckle).

This PR replaces that with 22 resources in two slots per tile, placed by geology and ecology rules, with stocks that can be drawn down. Decisions made with the owner: several resources per tile, a big catalogue with luxuries, natural scarcity plus depletion. See docs/decisions/0005-resources-by-geology.md.

Changes

  • Catalogue (@sovereign/contracts): RESOURCE_CATALOG with 22 resources (timber, grain, pasture, horses, fur, cotton, spices, wine, tea, ivory; stone, iron, copper, coal, oil, gold, silver, salt, gems; fish, pearls, whales).
  • Two slots per tile: a surface slot (land crops/forests/herds, or a fishery) and a ground slot (minerals; oil and salt may also lie under shallow sea). Each has type, richness, stock.
  • Placement engine (terrain/placement.ts, terrain/features.ts): per-resource rules in @sovereign/config (hard gates, soft preferences for ridge/basin/slope/height/river/coast, a regional geology factor). The top share by rank gets the resource, so abundance is exact on every seed and patches are coherent; rarest first; richness from rank.
  • Depletion: World.extractResource(layer, tile, amount); renewable surface stocks regrow from step() (integer arithmetic, only drawn-down tiles tracked); minerals never regrow.
  • Nations start where food, timber and stone are within about 12 tiles (a preference, not a guarantee).
  • UI: a resource legend that doubles as a focus selector (heat map of one resource), stock bars and renewable/finite in the tile inspector; on a phone the legend is a scrolling strip under the top controls.
  • Calibration and fixes found by measuring: ridge/slope scales were 5x/2x too large for real terrain; offshore oil/salt now use their own gates (the land rule's elevation gate had excluded every sea tile); placement sped up from 3.7 s to 1.2 s with output verified bit-identical.
  • Guards: tests/determinism/resource-coverage.test.ts (in a Linux/Windows CI determinism shard), a depletion golden, tests/probe/resource-quality.probe.ts, unit tests for placement, stocks, features, the access grid, palettes and the UI, and phone e2e checks for the legend.
  • Docs: ADR 0005, determinism spec (hash schema 2, vectors recomputed with the independent Python reference), save format 3, terrain, architecture, README, development.

Measured over 10 worlds of 2000 x 2000: all 22 resources present in every world at exactly their configured shares; 99 to 100% of metals and gems on ridges; coal and oil 100% in basins; clustering 0.86 to 0.97 (fish 0.29 to 0.97); creation 6.5 s (was 5.9 s); a world and its save about 88 MB (was 72 MB).

Related Issues

Follows #32 (terrain retune).

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)
  • Refactor / cleanup (no functional changes)
  • Documentation update

Breaking: every existing world, hash and golden changes (pre-1.0). HASH_SCHEMA_VERSION is 2, SAVE_FORMAT_VERSION is 3 (old saves are refused), the web IndexedDB is version 4 (older saves are dropped), and the ore and resourceDensity random streams are replaced by geology.

Checklist

  • Builds (pnpm build): not run in this session
  • Linting and the dependency rules pass (pnpm lint)
  • Type checking passes (pnpm typecheck)
  • Tests pass (pnpm test: 12 contracts, 119 config, 47 renderer, 9 ui, 578 simulation, 48 web, 37 cli, 9 desktop, 47 tool tests; pnpm test:determinism: 33)
  • Tested locally in the web app: both Playwright e2e scripts pass (the phone one now covers the legend)
  • If a golden hash moved, it is regenerated in its own commit and the reason is above (test: regenerate goldens for the resource overhaul)
  • Updated documentation / JSDoc where applicable

Determinism: Node 22, Chromium 157 and QuickJS (a non-V8 engine) all reproduce all 19 pinned values (including save round trips and the new depletion golden). The full 2000 x 2000 world for seed 1337 hashes to 38f584c299f3d4068decfc206ed40b7e in both Node and Chromium.

Not verified

  • CI has not run on this branch. The new resource guard is in a Linux and Windows determinism shard; Windows is untested.
  • No economy exists yet, so shares, richness scale, stock units and regrowth rates are provisional design values; nothing calls extractResource yet.
  • About half of all land carries a surface resource (timber 17%, grain 13%, pasture 8%, fur 6%, plus luxuries), so the "all resources" view is busy. Shares are config values and easy to cut; say if you want them sparser.
  • Real iOS/Android devices are untested (Chromium emulation only), and the stock arrays are sent to the app once at world creation.


Generated by Claude Code

…s resources

Measures, over 10 full-size worlds, each resource's share of land, its clustering (the fraction of a
resource tile's 4 neighbours holding the same resource) and its scarcity (how many landmasses of at
least 20 000 tiles have none of it).

Baseline on today's generator: timber 23.3% of land, fertile 17.2, stone 13.6, fur 7.0, iron 2.7, fish
0.89, gold 0.055; clustering 0.97 / 0.96 / 0.97 / 0.96 / 0.945 / 0.29 / 0.79; about 64% of land carries
a resource at all. That corrects an assumption: iron is coherent, not speckle (its noise is smooth);
the real problems are that most resources are biome recolors, nothing follows geology, there is one
resource per tile and no stock.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
RESOURCE_CATALOG describes every resource a tile can hold, with its id, key, label, icon, colour,
layer (surface or ground), domain (land, water, or oil and salt under both), tier (basic, industrial,
luxury) and display unit. Surface resources are renewable and ground resources finite. The ten surface
resources on land are timber, grain, pasture, horses, fur, cotton, spices, wine, tea and ivory; the
nine in the ground are stone, iron, copper, coal, oil, gold, silver, salt and gems; fish, pearls and
whales are on the water. resourceDef(id) looks one up.

The first seven ids are unchanged (timber 1, stone 2, iron 3, gold 4, grain 5 (was fertile), fish 6,
fur 7) so no world, save or golden changes in this commit. RESOURCE_META is replaced by the catalogue.
Only what never changes lives in contracts; where each resource is found, how much of it exists and how
fast it regrows are balance and go in the config.

Contracts gets its first tests (12): ids, enum agreement, unique labels, colours that can be told
apart, layer and domain rules, and the basic/industrial/luxury split.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
distanceToMask (4-neighbour steps to the nearest masked tile, capped at a limit: for distance to a
river or the coast), slopeField (elevation gradient magnitude) and topographicPosition (how far a
tile stands above or below the mean of its surroundings: positive on ridges and peaks, negative in
valleys and basins, averaged over the tiles that exist at the map edge). Pure functions over typed
arrays, no randomness; nothing calls them yet, so no world changes.

22 tests on tiny synthetic terrains (a single source, several sources, a plane, a cone, a one-tile
ridge, a pit, the map edge, a 1-wide column, bad arguments). Mutation-checked; the survivors found
gaps (a column walked from the second row, invalid limits, radius 1), which are now covered, and the
rest are equivalent mutants.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
placeResources decides where every resource is found, from rules (data in the config) instead of
noise thresholds. A rule has hard gates (the biomes that can hold it, and ranges of elevation,
temperature, humidity and distance to the coast), soft preferences (ridges, basins, slopes, height,
rivers, the coast, wet and warm ground) and a regional factor, smooth noise from the new `geology`
stream, that makes whole regions rich or barren so scarcity is geographic. Every tile that passes the
gates is scored and the highest-scoring tiles, up to the rule's share of the land (or of the water),
get the resource: taking the top share by rank makes the abundance exact on every seed, and smooth
scores give coherent patches and veins. Resources claim tiles rarest first (ties by catalogue id), so
a rare one gets its best sites before a common one takes them. Each tile has two slots, surface and
ground; a placed tile gets a richness of 1 to 255 from how high it ranked, so the core of a patch is
richest, and a full stock. Water resources use water tiles, and oil and salt can also lie under
shallow sea. Nothing calls it yet, so no world changes.

topographicPosition gained an include mask: position is measured against the surrounding land only,
because averaging with the sea made every coast look like a ridge (found by a test of the engine).

39 tests on synthetic 96 x 96 worlds with a known sea, plain, ridge, basin and river: exact shares,
rounding, every hard range at its boundary, each preference's direction, rarest-first and id
tie-breaking, slot independence, water and offshore, richness (monotone in rank, 1 to 255, full
stock), reproducibility, seed-dependence of regions, input checks, and the regional field. Mutation
checked over 45 mutants: each one that changes behaviour is killed; the survivors are equivalent
(histogram bin rounding, a cut-loop boundary that selects the same tiles, a stride cap). The
determinism lint rules ran on the new code and caught an Object.entries, now iterating the fixed biome list.

The `geology` stream joins the fixed list (test and spec updated).

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

ResourceStocks takes stock out of a tile's slot (extract returns what was actually taken, clamped to
what is there) and regrows renewable stocks toward their richness. A drawn-down renewable tile
regrows by its catalogue rate every `period` ticks on the tick where (tick + tile) % period === 0, so
tiles do not all grow on the same tick. Ground resources and any resource without a rate never regrow,
and a depleted mine keeps its type and richness.

Only drawn-down tiles are tracked and visited, so a world nobody has extracted from costs nothing per
tick and a long run stays cheap; the tracked set is derived from the stocks, so it is rebuilt after a
load. Integer arithmetic only, no randomness, and each tile's update is independent, so the result
cannot depend on the visiting order. buildRegrowthTable maps catalogue keys to rates and refuses
unknown or finite resources and out-of-range rates. Nothing calls it yet, so no world changes.

15 tests (clamping, nothing from an empty tile, phase and rate, caps at richness, finite stocks,
rebuild after load, 3000 tracked tiles past the first list, determinism, argument checks).
Mutation-checked: all 18 behaviour-changing mutants are killed; one survivor (extracting from an
empty tile) is equivalent, and one found a real gap (the list's growth step), now covered.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
WorldConfig gains `resourcePlacement` (alongside the old `resources` section, which the pipeline still
uses until the switch): feature settings (ridge radius and scale, slope scale), one rule per resource and
a regrowth rate for every renewable one. The defaults are a first design: timber in forests and
swamps weighted by wetness, grain on river floodplains and lowlands, pasture and horses on steppe,
fur in cold forests, cotton, spices, wine and tea by their climates (spices near the coast, wine on
slopes, tea in the highlands), iron, copper, gold, silver and gems on ridges (gold also in river
gravels), coal in basins, oil in basins and under the shallow sea, salt in deserts and on coasts and
in shallow sea, fish on the shelf, pearls in warm shallows and whales in cold deep water. Every
rule has a regional factor, so whole regions are rich or barren. The numbers are provisional and are
tuned against the probe next.

validateWorldConfig checks all of it (known resources and biomes, ranges, weights, shares, an
offshore deposit only for oil and salt, regrowth only for renewable resources with sane rates), and
that the shares competing for a slot do not add up to more than exists. resolveWorldConfig now deep-
copies nested objects, not only arrays, so a resolved config cannot alias the default rules.

36 tests, mutation-checked (the survivors found two checks that masked each other; now isolated).

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

Every tile now has a surface slot and a ground slot, each with a type, a richness and a stock.
The pipeline places the 22 catalogue resources with the placement engine; the old noise
generator and the ore and resourceDensity streams are gone. World gains extractResource and
regrows renewable stocks from step(); validate() checks the slots; the state hash covers them
(hash schema 2); the save format is 3 (thirteen layers); the web database is version 4.
The renderer overlay shows each tile's dominant resource (and can focus one resource).

The golden hashes are stale after this commit and are regenerated in their own commit.

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

The spawner scores each start by the food (grain, pasture, fish), timber and stone within about
12 tiles, as a preference worth up to half a biome: nothing is guaranteed, but nobody picks bare
ground while better ground is left. Access is counted per 4x4 cell, so it costs a sum over 49 cells
per candidate.

Goldens that depend on the spawner are regenerated in the next commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…walks only its domain

Building a whole-map regional array for each of 25 passes cost 1.6 s on a 2000 x 2000 map; the
sampler now evaluates it only for tiles that pass a rule's gates, and the passes walk the land or
sea tile list instead of every tile. Placement of a 2000 x 2000 world: 3.7 s to 1.2 s, with output
verified bit-identical to the previous version.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cn5qp59UwGNst3umuaQf6u
…eir own gates, resource guard

The ridge scale (0.04) and slope scale (0.02) were 5x and 2x too large for real terrain, so ridge and
basin preferences did almost nothing; measured on 2000 x 2000 worlds the ridge window is now 16 tiles
with a full ridge at 0.015 and a steep slope at 0.006. Metals now sit 98 to 100% on ridges, coal and
oil 100% in basins. Oil and salt offshore deposits ignored their own rule: a land rule's elevation,
temperature and humidity gates excluded every sea tile (offshore oil was 0 tiles); an offshore deposit
now uses only its coast distance and an optional elevation (depth) range.

Adds tests/determinism/resource-coverage.test.ts (all 22 present, exact shares, domains, geology,
clustering) and geology columns in the resource probe.

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

Hash schema 2 hashes the six resource arrays; the spec's test vectors are recomputed with the
independent Python reference (which also reproduces every version 1 vector). Terrain, save format,
architecture and the README describe the 22 resources, the two slots and depletion.

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

World-state goldens (schema 2, default placement rules in place of the retired oreScale override),
terrain goldens, the nation golden, and a new pin for drawing stocks down and letting them regrow.
Mutation-checked: switching off regrowth fails the depletion golden and two World tests.

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

The resource view gets a legend (all 22 resources by tier, with their colours) that doubles as a
focus selector: pick a resource to see only where it is (a heat map, the rest dimmed) or all of them
to see each tile's dominant one. The tile inspector lists both slots with a stock bar and whether the
resource is renewable. On a phone the legend is a scrolling strip under the top controls so it never
covers the inspector sheet or the toolbar. The new determinism guard is added to a CI shard (the shard
test caught the omission). The phone e2e now checks the legend's layout, tap size and focusing.

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 20, 2026 21:21

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 cf59ec1 into main Nov 20, 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