diff --git a/CHANGELOG.md b/CHANGELOG.md index 738dc06..5971143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] +- The map of a huge network spends less time working out fiber capacities on every frame, and skips links above or below the screen. + ## [1.34.1] — 2026-10-27 - The game starts faster in Italian, Portuguese and French: their translations are about 40% smaller to download. diff --git a/src/game/10-catalog/60-reach-and-subsea.js b/src/game/10-catalog/60-reach-and-subsea.js index 2ed9bc5..9ff08a3 100644 --- a/src/game/10-catalog/60-reach-and-subsea.js +++ b/src/game/10-catalog/60-reach-and-subsea.js @@ -54,7 +54,11 @@ const subMaxTier=km=>{let t=0;LT.forEach((T,i)=>{if(!subWaveWhy(i,km,true))t=i;} // Cached per optic, span and cable state: subsea, and which submarine upgrades are in service (#78). const OPTG={};const optG=(o,l)=>{const km=spanKm(l),fl=l.sub?1+(subUpgradeReady(l,'terminal')?2:0)+(subUpgradeReady(l,'engineering')?4:0):0,c=OPTG[o]||(OPTG[o]=[]),m=c[fl]||(c[fl]=new Map());let g=m.get(km);if(g===undefined){const x=optMode(o,l);m.set(km,g=x?x.g:0);}return g;}; const regenNeed=(o,l)=>Math.max(0,Math.ceil(l.km/OPT[o].reach)-1); -const dfCap=l=>{if(l.ready>S.day||l.eqDown>S.day)return 0;const ch=l.ch||[];let g=0;for(const c of ch)g+=optG(c,l);const f=l.chFail&&l.chFail.until>S.day?l.chFail:null;if(f){const fo=f.o&&ch.includes(f.o)?f.o:ch.slice().sort((a,b)=>optG(a,l)-optG(b,l))[0];if(fo)g-=optG(fo,l);}return Math.max(0,g);}; +// The map asks for every fiber's capacity several times per frame (width, colour, label, up or down), each time over +// all its channels. Nothing changes while a frame is drawn, so draw() keeps the answers in DFC for that frame. +let DFC=null; +const dfCap=l=>{if(!DFC)return dfCap0(l);let g=DFC.get(l);if(g===undefined)DFC.set(l,g=dfCap0(l));return g;}; +const dfCap0=l=>{if(l.ready>S.day||l.eqDown>S.day)return 0;const ch=l.ch||[];let g=0;for(const c of ch)g+=optG(c,l);const f=l.chFail&&l.chFail.until>S.day?l.chFail:null;if(f){const fo=f.o&&ch.includes(f.o)?f.o:ch.slice().sort((a,b)=>optG(a,l)-optG(b,l))[0];if(fo)g-=optG(fo,l);}return Math.max(0,g);}; const capOf=l=>(l.kind==='vxc'?VXC[l.vt].g:l.kind==='df'?dfCap(l):LT[l.tier].g)*(l.deg>S.day?0.5:1)*(l.loss&&l.loss.until>S.day?l.loss.f:1); // A sick LAG (LACP trouble) keeps only part of its capacity until the incident clears. const lagF=o=>o&&o.lacp&&o.lacp.until>S.day?o.lacp.f:1,ixLagF=x=>lagF(S.ev&&S.ev.ixlacp&&{lacp:S.ev.ixlacp[x]}); diff --git a/src/game/96-map/60-draw.js b/src/game/96-map/60-draw.js index 4ed461f..75801b7 100644 --- a/src/game/96-map/60-draw.js +++ b/src/game/96-map/60-draw.js @@ -1,4 +1,5 @@ -function draw(now){ +function draw(now){DFC=new Map();try{drawFrame(now);}finally{DFC=null;}} +function drawFrame(now){ if(!ctx)return; ctx.setTransform(DPR,0,0,DPR,0,0); // sea @@ -27,13 +28,14 @@ function draw(now){ const far=lodFar(),PI=pairIdx(); for(const grp of linkGroups()){const l=grp[0],many=grp.length>1,up=many?grp.filter(x=>!linkDown(x)):null; const gm=linkGeom(l,PI.get(edgeKey(l))),cap=many?up.reduce((a,x)=>a+capOf(x),0):capOf(l),load=many?grp.reduce((a,x)=>a+(R.load[x.id]||0),0):(R.load[l.id]||0),u=cap?load/cap:0,df=l.kind==='df',bld=df&&l.ready>day,cut=!bld&&(many?!up.length:linkDown(l))&&!(df&&!(l.ch||[]).length),dark=df&&!bld&&!(l.ch||[]).length,vx=l.kind==='vxc'; - const sel=UI.sel&&UI.sel.type==='link'&&grp.some(x=>x.id===UI.sel.id); + const sel=UI.sel&&UI.sel.type==='link'&&grp.some(x=>x.id===UI.sel.id);let lab=null; const w=(vx?2.2:(2.5+Math.max(...grp.map(tierW))*1.8+(many?Math.min(1.5,(grp.length-1)*0.5):0)))*sc*(far?0.5:1),np=gm.pts.length; if(SX.lengthmx)mx=x;} - if(mx<-40||mn>CW+40)continue; + let mn=Infinity,mx=-Infinity,mny=Infinity,mxy=-Infinity;for(let i=0;imx)mx=x;if(ymxy)mxy=y;} + // Off screen (with room for the widest line and the markers at its middle): nothing of it would show. + if(mx<-40||mn>CW+40||mxy<-40||mny>CH+40)continue; const path=()=>{ctx.beginPath();ctx.moveTo(SX[0],SY[0]);for(let i=1;i0.9||sel){const t=many?groupLabel(grp,up):capName(l);ctx.font='800 11px "Baloo 2", sans-serif';let tw=LINKW.get(t);if(tw===undefined)LINKW.set(t,tw=ctx.measureText(t).width+10);ctx.fillStyle='#fff';ctx.strokeStyle='#1E2640';ctx.lineWidth=2;rr(mxx-tw/2,my-9,tw,17,8);ctx.fill();ctx.stroke();ctx.fillStyle='#1E2640';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(t,mxx,my+1);} + else if(z>0.9||sel){const t=lab||(lab=many?groupLabel(grp,up):capName(l));ctx.font='800 11px "Baloo 2", sans-serif';let tw=LINKW.get(t);if(tw===undefined)LINKW.set(t,tw=ctx.measureText(t).width+10);ctx.fillStyle='#fff';ctx.strokeStyle='#1E2640';ctx.lineWidth=2;rr(mxx-tw/2,my-9,tw,17,8);ctx.fill();ctx.stroke();ctx.fillStyle='#1E2640';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(t,mxx,my+1);} } } // drag preview diff --git a/tests/speed.test.js b/tests/speed.test.js index f977228..0ea9f41 100644 --- a/tests/speed.test.js +++ b/tests/speed.test.js @@ -100,6 +100,27 @@ test('the map draws a big network at every zoom, with a city or a link selected' assert.deepEqual(errors, []); }); +test('the map keeps fiber capacities for one frame only, and skips links above or below the screen', () => { + const { ev, errors } = load(); + bigNet(ev); + ev("S.links.push({id:'dfT',a:'fra',b:'ams',kind:'df',own:true,km:kmBetween('fra','ams'),ready:0,ch:['zr4']});compute()"); + ev('cam.x=CITY.fra.x;cam.y=CITY.fra.y;cam.z=2;draw(0)'); + assert.equal(ev('DFC'), null, 'nothing is kept between frames'); + const cap = ev("dfCap(S.links.find(l=>l.id==='dfT'))"); + ev("S.links.find(l=>l.id==='dfT').ch.push('zr4');draw(0)"); + assert.equal(ev("dfCap(S.links.find(l=>l.id==='dfT'))"), 2 * cap, 'a new channel counts on the next frame'); + ev("ctx.fillText=()=>{throw new Error('boom')}"); + assert.throws(() => ev('draw(0)')); + ev('delete ctx.fillText'); + assert.equal(ev('DFC'), null, 'not even after a frame that failed'); + // The whole network far above the screen: the links cost nothing, as if there were none. + const calls = (body) => ev(`(()=>{cam.x=CITY.fra.x;cam.y=CITY.fra.y+${'2000'};cam.z=2;${body};const n=ctx.calls.n;draw(0);return ctx.calls.n-n;})()`); + const withLinks = calls(''), links = ev('JSON.stringify(S.links)'); + assert.equal(withLinks, calls('S.links=[]')); + ev(`S.links=JSON.parse(${JSON.stringify(links)})`); + assert.deepEqual(errors, []); +}); + test('long lists in the NOC dashboard show 100 rows at a time and grow on request', () => { const { ev, errors } = load(); bigNet(ev);