From 732c32984446f540f2041db54bc5f916411e93f5 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Thu, 29 Oct 2026 07:05:23 +0100 Subject: [PATCH] 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. --- src/game/80-simulation/60-requirements.js | 33 +++++++++++++++-------- tests/load-time.test.js | 23 ++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/game/80-simulation/60-requirements.js b/src/game/80-simulation/60-requirements.js index ab9f52c..53306c0 100644 --- a/src/game/80-simulation/60-requirements.js +++ b/src/game/80-simulation/60-requirements.js @@ -37,20 +37,31 @@ function diversityPossible(a,b,state=S){ if(!CITY[a]||!CITY[b]||a===b)return false; const rules=state.scn&&state.scn.rules||{},key=JSON.stringify([!!state.won,rules]); if(!diversityMap||diversityMap.key!==key){ - const links=[],cities=CITIES.filter(c=>!(rules.banCities||[]).includes(c.id)).map(c=>c.id); - const add=(a,b,id)=>links.push({a,b,id}); - for(let i=0;ip===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); + /* Built once per ruleset, over every city pair, so it runs on the first load of every save: on city indices, with + the cables between each pair from a table. A link is the number of its risk: each land trench and your own cable + on its own, every segment of one cable (or one fabric) the same; adj[i] lists [neighbour, risk] in the order the + links were added, as a graph built from a list of them would. */ + const cities=CITIES.filter(c=>!(rules.banCities||[]).includes(c.id)).map(c=>c.id),N=cities.length,ix=new Map(cities.map((c,i)=>[c,i])); + const cab=new Map();for(const c of CABLE_PAIRS){const k=c.alands(CITY[c])),land=cities.map(landing),cn=cities.map(cnCity),fab=cities.map(c=>FABS.filter(f=>f.cities.includes(c))),ban=rules.banPairs||[]; + const adj=cities.map(()=>[]),grp=new Map();let n=0,k=0;const add=(i,j,g)=>{let r;if(g===undefined)r=k++;else{r=grp.get(g);if(r===undefined)grp.set(g,r=k++);}adj[i].push(j,r);adj[j].push(i,r);n++;}; + for(let i=0;iLS[j].includes(x)); + if(!sub){add(i,j);add(i,j);} + if(cs)for(const c of cs)if(!ban.some(p=>p===c.cid+':'+a+'-'+b||p===c.cid+':'+b+'-'+a))add(i,j,'cab:'+c.cid); + if(state.won&&land[i]&&land[j]&&(sub||cs)&&!cn[i]&&!cn[j])add(i,j); + if(fab[i].length&&fab[j].length)for(const f of FABS)if(fab[i].includes(f)&&fab[j].includes(f))add(i,j,'fab:'+f.id); } - diversityMap={key,links,answers:{}}; + diversityMap={key,ix,adj,links:n,answers:{}}; } - const pair=[a,b].sort().join(':'); - return diversityMap.answers[pair]??(diversityMap.answers[pair]=disjointRoutes(a,b,diversityMap.links)); + const pair=[a,b].sort().join(':'),M=diversityMap; + return M.answers[pair]??(M.answers[pair]=M.ix.has(a)&&M.ix.has(b)&&disjointOn(M.adj,M.ix.get(a),M.ix.get(b))); } +// disjointRoutes() on such a graph: a shortest path by hops, then another one that shares none of its risks. +function disjointOn(adj,a,b){const N=adj.length,bfs=skip=>{const prev=new Int32Array(N).fill(-1),pl=new Int32Array(N),q=[a];prev[a]=a; + for(let h=0;h{const m=new Map();for(const l of S.links)if(!m.has(l.id))m.set(l.id,l);return m;}; diff --git a/tests/load-time.test.js b/tests/load-time.test.js index b1a7a68..9106cab 100644 --- a/tests/load-time.test.js +++ b/tests/load-time.test.js @@ -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;ip===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;ix===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({})');