Status: done (2026-11-12)
Priority: 2
Size: S
Arch: between
Phase: implement
Design ref: Inheritance, expansion and other bodies (Between villages: Distance, Near and far). Survey and numbers: backlog/VB-281.md
Goal: Every act that reaches another village reads a different distance today, or none: hex steps as the crow flies between centres (claims, CLAIMS.farmReach 15; service, SERVICE.reach 15; the nearest village for an orphan’s service home), an unweighted search of up to 160 steps (the tie to the culture’s centre), breadth-first steps (settlement), a route search bounded at 12 hours from each guest’s house to the farm (an occasion’s guests, hoursFrom, src/sim/occasions.js:47-52, VB-126, which keeps it “until VB-286’s table”), and nothing at all (the matcher’s women, kin help, guardians). Kim decided on 10 November that the distance between villages governs what passes between them. Give the game one measure: the walking hours between two villages’ greens by the route search’s costs (terrain, slope, worn paths), compiled into a small table that every later task reads. This task builds the table and its reader only; nothing reads it yet, so the run is unchanged.
Acceptance criteria:
reachCosts(w, from, maxCost, targets) in src/sim/routes.js: a Dijkstra search from tile from by stepCost, over passable tiles (a target tile may be closed, as findRoute’s ends may), stopping at maxCost or when every target is settled; it returns the cost to each target reached. It reuses the module’s scratch buffers and heap (no allocation per tile), counts its settled tiles in a declared counter VB.count.reachSettled, and agrees with findRoute + routeCost on every pair of greens at medium-ref and small-y40 (a test, to 1e-9).st.villageHours: one row per village id, row[u] the walking hours from village v’s green to u’s (the cost over SPEED, 3 meadow tiles an hour), null beyond ROUTE.reach (200 meadow steps, 67 hours) or with no route by land. Plain data (arrays of numbers and null) in st, not in st.w, saved with the run, owner sim/routes (src/owners.json), @state line. Costs are the same both ways (stepCost is symmetric), so writing v’s row also writes column v of every other row.hoursBetween(st, a, b): 0 for the same village, else the table’s entry (Infinity for null). Allocates nothing.settle.js, found) or a split gets its row at once. The seed builds the whole table, and so does a load of a save without it (all saves before this task); the save format stays.tests/lib/invariants.js): one row per village, each of length the number of villages; the diagonal 0; symmetric to 1e-9; every other entry null or a number in (0, 67]. Equality with a fresh search is a test at the refresh, not an invariant, since costs drift between refreshes.node tools/sim.js --report reach [--json] prints, for the table: villages, pairs, pairs by land and within reach, the hours’ spread (least, quartiles, most), each village’s nearest neighbour, and pairs within 12 hours. The numbers at medium-ref and small-y40 go in this file (VB-281 measured: 29 villages, 406 pairs, 268 by land, 247 within 67 h; median 41 h, nearest neighbour median 5.2 h, at most 16.0; and 23 villages, 253 pairs, 193 by land, all within 67 h; median 27 h, nearest neighbour median 6.2 h, at most 9.6).tests/golden.json changes only by the new key villageHours in each state entry, listed in the PR; tests/counters.json only by the new counter. tests/saves.js and tests/spread.js (a save between two villages’ rows goes on as the unbroken run) pass.medium-ref, measured by VB-281’s prototype, 2.4 ms median and 19 ms at most (1.3 and 6.2 at small-y40), against 60 ms a step; the whole table 95 ms at medium-ref (43 at small-y40), once a year spread over 29 steps, so about 2 ms a game day of the year’s mean. The table holds villages² numbers (841 at medium-ref; 3,600 at 60 villages). node tools/sim.js --load on each fixture --years 1 --report perf.
Notes:hoursFrom, sim/occasions) added to the distance measures, as a reader that moves onto the table.medium-ref), so a dense table is cheaper than any index. Why not the route cache: it holds tile-to-tile paths under an LRU of 60,000 and drops them daily where a tile’s cost changed; a lookup between two greens would search, or miss, at every use. Why not findRoute per pair: a pair with no land route explores the whole island before it fails; 406 searches took 414 to 930 ms at medium-ref. One Dijkstra a green finds all the greens it reaches at once.sim/clock.js’s table on autumn days 9 and 10. VB-287, VB-288 and VB-290 read the table; VB-126’s occasions are a reader that moves onto it, in place of hoursFrom (no task holds that move yet, and the table’s hours are green to green, not house to farm).Implementation notes (VB-286):
reachCosts(w, from, maxCost, targets, out) in src/sim/routes.js; scratch buffers per world (a tgt mark array and one reused heap added), counter reachSettled
(declared in src/vb.js; its baseline in tests/counters.json is 0, since the 30 spring days of that fixture and of tests/scaling.js hold no autumn;
both skip it as they skip other counters their window cannot reach, and tests/villagehours.js exercises it).st.villageHours (owner sim/routes, @state line in the routes module); hoursBetween, buildHours (seed in makeState, and a load of a save without
the table, sim/saves), villageAdded (called by settle.js found once the farm is made: no other path adds a village; the task’s “split” does not exist yet),
refreshHoursStep (the calendar’s entry, every: 'year', season 2, sday 8, hour 12, tick 0-59: “autumn day 9” is sday 8, as the meetings’ day 10 is sday 9).
Window rule as the task states it: village k on step k up to 60 villages, else on step floor(k * 60 / villages); the rows of one step are the integer range
ceil(j * V / 60) to ceil((j + 1) * V / 60) - 1.SPEED from one search per pair of ends, written to row and column together, so the table is exactly symmetric; a fresh search from
the other end differs in the last digits (sums in another order), hence the 1e-9 tolerances.tests/lib/invariants.js (checkState). Test tests/villagehours.js (in npm test): tiny island with a fifth village across the sea,
agreement, hoursBetween, a row a step from noon of day 9 (a worn road between two villages, only that row and column fresh at its step), a save between rows,
an older save, a village added, 70 villages in 60 steps. The agreement with findRoute + routeCost on every pair of greens is tests/lib/reach.js,
run by tests/fixtures.js on every fixture made (verify): 435 pairs at medium-ref, 276 at small-y40, all agree to 1e-9.node tools/sim.js --report reach [--json] (tools/reach.js); “by land” comes from an unbounded search from each green.tests/clock.js ORDER has the new entry (after the end of the people’s entries, before the meeting’s call); node tools/sim.js --report calendar shows it.Validation notes (VB-286), fixtures made for these sources (key e6881ef387817072):
node tests/golden.js --dump against the same dump of the base commit (2003881), diff -r: of 16 state entries every difference is a new file
villageHours.txt; nothing else differs. tests/golden.json is NOT re-recorded in this branch (the orchestrator re-records at the merge), so the golden check fails here.tests/counters.json: only reachSettled: 0 added. npm test: all pass but the golden-independent checks listed in the report; tests/saves.js, tests/spread.js,
tests/plaindata.js, tests/clock.js, tests/invariants.js pass.medium-ref (year 12, 1,006 living): 30 villages, 435 pairs, 274 by land, 270 within reach (67 h); hours least 3.5, quartiles 10.8, 20.8, 47.6, most 66.3 (median
20.8 h); nearest neighbour median 4.5 h, at most 11.4; 77 pairs within 12 h. A row: median 3.8 ms, at most 9.0, the table 109 ms (against 60 ms a step: rows are one a step).small-y40 (year 40, 685 living): 24 villages, 276 pairs, 196 by land, all within reach; least 4.0, quartiles 12.7, 22.7, 37.6, most 54.7 (median 22.7 h); nearest
neighbour median 4.9, at most 13.1; 45 pairs within 12 h. A row: median 2.1 ms, at most 2.6, the table 43 ms. (VB-281’s 29 and 23 villages were at other sources;
the median, 41 h there against 20.8 here, moved with the villages’ places.)--load FILE --years 1 --report perf, 48-day year): small-y40 222.6 ms/day, medium-ref 225.5 ms/day, the same system rows as before
(the refresh is not a row of that report; its 30 searches a year are the 109 ms above, about 2 ms a game day of 48, to be seen in the run’s total, not apart from it).
Targets: the 60 ms step: a row costs at most 9 ms (in), the budget lines are not moved by this change.medium-ref).Orchestrator’s decisions for Kim to overturn:
sday 8 of autumn (days counted from 1), the day before the meetings’ sday 9, as the task says.cenv guard: the table does not depend on the culture environment. refreshHoursRow returns, doing nothing, for a state with no table or a table of
the wrong size, so a hand-built state that is advanced through autumn is not broken.byLand in the report is found by an unbounded search from each green (tool cost only), since the table nulls both beyond reach and no route.villageAdded runs after settleFarm, so the row is found with the farm’s house in place; a village is not read by anything until VB-287.tests/scaling.js and tests/counters.js skip reachSettled (no autumn in 30 days), with baseline 0.node tests/golden.js --dump before and after on the merged tree, the 16 state entries gain villageHours and no other leaf differs; tests/counters.json gains reachSettled.