Requirements: build the possible-diversity map on city indices

The first diversityPossible() call after a save loads (migrate() checks every
customer's requirements) built a graph of every buildable link between all
~28k city pairs, scanning all 475 cable segments for each pair, with string ids
and objects for each of ~19k links: about 0.8 s on a fast machine, ~0.9 s of a
throttled phone's boot. Now the cables come from a table per pair and the graph
holds city indices and one number per risk (each land trench and own cable on
its own; every segment of a cable, or a fabric, sharing one). The answers match
disjointRoutes() over the old link list for all 14,042 sampled pairs, won or not
and with scenario bans.
This commit is contained in:
Frederic Jung 2026-10-29 07:05:23 +01:00
parent 5c2b3a4758
commit 732c329844
No known key found for this signature in database
2 changed files with 45 additions and 11 deletions

View file

@ -5,6 +5,29 @@ const { load } = require('./harness');
// Work done once when a game loads (the possible-diversity map, link curves, the map's sea and hills) has shortcuts;
// each must give exactly what the plain version gives.
test('diversity map: the indexed graph answers like disjointRoutes() over every possible link', (t) => {
const { ev, dom, errors } = load(); t.after(() => dom.window.close());
ev('ACT.start({})');
// The plain map: one record per possible link, in the order the indexed one adds them.
ev(`window.plainLinks=state=>{const rules=state.scn&&state.scn.rules||{},links=[],cities=CITIES.filter(c=>!(rules.banCities||[]).includes(c.id)).map(c=>c.id),add=(a,b,id)=>links.push({a,b,id});
for(let i=0;i<cities.length;i++)for(let j=i+1;j<cities.length;j++){const a=cities[i],b=cities[j];
if(!isSub(a,b)){add(a,b,'land:'+a+':'+b+':1');add(a,b,'land:'+a+':'+b+':2');}
for(const c of cablesFor(a,b))if(!(rules.banPairs||[]).some(p=>p===c.cid+':'+a+'-'+b||p===c.cid+':'+b+'-'+a))add(a,b,'cab:'+c.cid);
if(state.won&&seaCab(a,b)&&landing(a)&&landing(b)&&!cnCity(a)&&!cnCity(b))add(a,b,'own:'+a+':'+b);
for(const f of FABS)if(f.cities.includes(a)&&f.cities.includes(b))add(a,b,'fab:'+f.id);}return links;}`);
const cp = ev('CABLE_PAIRS[0].cid+":"+CABLE_PAIRS[0].a+"-"+CABLE_PAIRS[0].b');
for (const st of [{ won: false }, { won: true }, { won: false, scn: { rules: { banCities: ['jam', 'lon'], banPairs: [cp] } } }]) {
const r = JSON.parse(ev(`(()=>{const st=${JSON.stringify(st)},L=plainLinks(st);diversityMap=null;diversityPossible('fra','nyc',st);
const ids=CITIES.map(c=>c.id),bad=[];let n=0;for(let i=0;i<ids.length;i+=3)for(let j=i+1;j<ids.length;j+=3){n++;const a=ids[i],b=ids[j];
const want=(st.scn&&st.scn.rules.banCities.some(x=>x===a||x===b))?diversityPossible(a,b,st):disjointRoutes(a,b,L);if(diversityPossible(a,b,st)!==want)bad.push(a+'-'+b);}
return JSON.stringify({links:L.length,mine:diversityMap.links,n,bad})})()`));
assert.equal(r.mine, r.links, 'the same possible links');
assert.ok(r.n > 2000);
assert.deepEqual(r.bad, [], JSON.stringify(st));
}
assert.deepEqual(errors, []);
});
test('link curves: skipping far-away cities gives the same bend around the ones in the way', (t) => {
const { ev, dom, errors } = load(); t.after(() => dom.window.close());
ev('ACT.start({})');