WebGL map: keep every sprite, the attack ring, an empty title map and a 2D fallback
- A sprite or disc drawn as the queue grows was written into the old array and lost: the record is now taken before the array is read. - A city's picture without an attack shared its cache key with step 0 of a DDoS ring (always, with animations off): no attack is now phase -1, and cityStatic() reads only what the key holds. - Back at the title (or on a damaged save), drawFrame() returned before hiding the card's layers, so the last network stayed behind the title: the layers are now shown or hidden on entry. - An error from the card (frame start, tube upload, atlas, sprite flush) now gives WebGL up and draws the same frame in 2D, once; the 2D context, the sprite queue and GLS.frame are reset whatever fails. Other errors still reach tickErr() and leave WebGL on. - Selecting a tube no longer rebuilds and uploads every tube, packet and point (about 230 KB on the big-network fixture): its halo is its own small buffer.
This commit is contained in:
parent
cf9ab5e774
commit
8491b1a8f6
7 changed files with 170 additions and 37 deletions
|
|
@ -9,7 +9,7 @@ All notable changes to this project are documented here. The project follows
|
|||
- The NOC charts have tooltips, on the dashboard and on the wallboard in the NOC window: hover (or touch) a chart to see every series on that day, with a total. Click an entry in the legend to hide or show that series, and the scale follows what is left. The "how it arrives" bars on the Traffic tab show the same kind of tooltip, and its legend picks which ways in (caches, PNIs, IX peering, transit) the table counts.
|
||||
- Bilbao joins the map, where the transatlantic cables really come ashore: Marea Carey, Grace Hopscotch and Sol Survivor now land there instead of Madrid. It has no exchange of its own, so traffic heads inland to Madrid, and Drizzle Electric, Tangerine Telecom and the big carriers sell transit there. A link you already run on the old Madrid landings keeps working as a plain subsea link, and the Mid-Atlantic Accent scenario now goes Azores, Bilbao, Madrid, and Marea Carey now leaves the American coast at Virginia Beach instead of crossing the northeast to New Jersey. Columbus III-and-a-half now comes ashore near Cádiz, where it really landed, on its way to Madrid.
|
||||
- Puzzles are easier to read, above all on phones: the three stars are now three goals in plain words, and the one you are working on says what is wrong and what to do, with a Show me button. The map marks it too: a red ring around a city that still needs traffic, and a pulsing ✂ on a link whose cut would hurt. On phones the sheet starts small so the map gets the screen; swipe it up for the rest. Cities far from the puzzle are left off the map, and stepping stones you are not using are small dots without a name until you tap one, build there or zoom in.
|
||||
- The map is now drawn by the graphics card wherever the browser can, so panning, zooming and fast play stay smooth on big networks and big screens. The map looks the same; Settings › Map drawing › Always 2D keeps the old drawing, and browsers without WebGL2 keep it on their own.
|
||||
- The map is now drawn by the graphics card wherever the browser can, so panning, zooming and fast play stay smooth on big networks and big screens. The map looks the same; Settings › Map drawing › Always 2D keeps the old drawing, and browsers without WebGL2 keep it on their own, as does a game whose graphics card fails while you play.
|
||||
- Cities, labels, building cones, ships, offer bubbles and money pop-ups are painted once and then reused, so the usual map also draws far fewer shapes each frame.
|
||||
|
||||
## [2.9.1] — 2026-11-04
|
||||
|
|
|
|||
|
|
@ -6,13 +6,15 @@
|
|||
packets (58) and the pictures of cities, labels and markers (59); and #mapfx, a transparent 2D canvas over it for
|
||||
the few things still painted shape by shape (the drag preview, the chaos monkey), cleared only after it was used.
|
||||
Puzzles, and any browser without WebGL2 or whose context fails or is lost, keep the 2D map. */
|
||||
const GLX={want:true,dead:false,gl:null,c:null,f:null,fx:null,key:[],nSeg:0,nPk:0,sel:false,shown:false,gpu:''};
|
||||
const GLX={want:true,dead:false,gl:null,c:null,f:null,fx:null,key:[],nSeg:0,nPk:0,nHalo:0,shown:false,gpu:''};
|
||||
try{GLX.want=localStorage.getItem('sot-map')!=='2d';}catch(e){}
|
||||
const glOn=()=>GLX.want&&!GLX.dead&&!!S&&!pzOn()&&glInit();
|
||||
// Switches the WebGL map on or off: the setting keeps the choice in this browser (keep), the GPU benchmark does not.
|
||||
function mapGl(on,keep=true){GLX.want=!!on;if(keep)try{on?localStorage.removeItem('sot-map'):localStorage.setItem('sot-map','2d');}catch(e){}
|
||||
if(!on)glShow(false);GLX.key=[];UI.drawnAt=0;return on&&!GLX.dead;}
|
||||
function glShow(on){on=!!on;if(GLX.shown===on)return;GLX.shown=on;const e=document.getElementById('mapgl'),fx=document.getElementById('mapfx');if(e)e.hidden=!on;if(fx)fx.hidden=!on;}
|
||||
// Marks an error as the card's own, so draw() falls back to 2D instead of trying WebGL again every frame.
|
||||
function glErr(e){const x=e instanceof Error?e:new Error(String(e));x.glErr=true;return x;}
|
||||
function glKill(why){console.warn('[tubes] the map stays 2D:',why&&why.message||why);GLX.dead=true;GLX.gl=null;glShow(false);}
|
||||
const GL_HEAD='#version 300 es\nprecision highp float;precision highp int;\n';
|
||||
// Clip space from CSS px (the shaders work in CSS px, as the 2D map does, and use uDpr only for anti-aliasing).
|
||||
|
|
@ -42,7 +44,7 @@ function glInit(){if(GLX.gl)return true;if(GLX.dead)return false;
|
|||
const fx=f.getContext('2d'),CP=CanvasRenderingContext2D.prototype;
|
||||
for(const m of ['fill','stroke','fillRect','strokeRect','fillText','strokeText','drawImage','putImageData']){const fn=CP[m];fx[m]=function(){GLX.fxUsed=true;return fn.apply(this,arguments);};}
|
||||
const V=s=>glVao(gl,s);
|
||||
Object.assign(GLX,progs,{gl,c,f,fx,fxUsed:true,tex,land:{},lv:V([4,4,4]),pv:V([4,4]),cb:V([4,4,4]),wv:V([4,4,4]),cl:V([4]),empty:gl.createVertexArray()});return true;}
|
||||
Object.assign(GLX,progs,{gl,c,f,fx,fxUsed:true,tex,land:{},lv:V([4,4,4]),hv:V([4,4,4]),pv:V([4,4]),cb:V([4,4,4]),wv:V([4,4,4]),cl:V([4]),empty:gl.createVertexArray()});return true;}
|
||||
catch(e){glKill(e);return false;}}
|
||||
// Colours as [r,g,b,a] from '#rrggbb', '#rgb' or 'rgba(r,g,b,a)', kept by string.
|
||||
const GLCOL=new Map();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
/* The tubes on the card: every tube is one instanced draw per layer (halo, casing, colour, fiber dots, packets), and
|
||||
the packets travel their paths in the vertex shader, so a frame costs a few uniforms however many tubes there are.
|
||||
The buffers are rebuilt only when the network, the routing, the day or the selection change. Depth stands in for
|
||||
The buffers are rebuilt only when the network, the routing or the day change; a selection only rebuilds the halo,
|
||||
a buffer of the selected tube's own segments. Depth stands in for
|
||||
the 2D painting order: a later tube's casing still covers an earlier tube's colour and packets. */
|
||||
// The line program's passes: a tube's layers in drawFrame()'s order, and plain lines (coasts, waves, cables).
|
||||
const LP={halo:0,casing:1,colour:2,dots:3,plain:4};
|
||||
|
|
@ -58,29 +59,37 @@ void main(){float d=length(vLoc),out_=clamp((vR+vLw*.5-d)*uDpr+.5,0.,1.);if(out_
|
|||
float inn=clamp((vR-vLw*.5-d)*uDpr+.5,0.,1.);vec3 c=mix(vec3(30./255.,38./255.,64./255.),vec3(1.),inn);o=vec4(c*out_,out_);}`;
|
||||
/* The tubes as instance data: their segments (see LINE_STRIDE; later tubes are nearer), their packets (first point,
|
||||
points, length, packets | which, base width, depth, -), and every tube's points in the texture. */
|
||||
function glBuild(LG,GM,looks,selId){const gl=GLX.gl,N=LG.length;let ns=0,np=0,nk=0;
|
||||
const glDepth=(gi,N)=>1-(gi+1)/(N+2);
|
||||
function glBuild(LG,GM,looks){const gl=GLX.gl,N=LG.length;let ns=0,np=0,nk=0;
|
||||
for(let gi=0;gi<N;gi++){ns+=GM[gi].pts.length-1;np+=GM[gi].pts.length;nk+=looks[gi].pk;}
|
||||
const SEG=new Float32Array(ns*LINE_STRIDE),PK=new Float32Array(nk*8),TH=Math.max(1,Math.ceil(np/GL_PTS_W)),PT=new Float32Array(GL_PTS_W*TH*4);
|
||||
let si=0,ki=0,pi=0,sel=false;
|
||||
for(let gi=0;gi<N;gi++){const L=looks[gi],{pts,cum,len}=GM[gi],dep=1-(gi+1)/(N+2),col=glColor(L.col);
|
||||
const hot=!!selId&&LG[gi].some(x=>x.id===selId);if(hot)sel=true;const fl=(hot?LINE_SEL:0)|(L.df&&!L.bld&&!L.dark?LINE_FIBER:0),p0=pi;
|
||||
let si=0,ki=0,pi=0;
|
||||
for(let gi=0;gi<N;gi++){const L=looks[gi],{pts,cum,len}=GM[gi],dep=glDepth(gi,N),col=glColor(L.col);
|
||||
const fl=L.df&&!L.bld&&!L.dark?LINE_FIBER:0,p0=pi;
|
||||
for(let j=0;j<pts.length;j++){PT[pi*4]=pts[j][0];PT[pi*4+1]=pts[j][1];PT[pi*4+2]=cum[j];pi++;}
|
||||
for(let j=0;j+1<pts.length;j++)glSegAt(SEG,si++,pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,fl,dep,col);
|
||||
for(let i=0;i<L.pk;i++){const o=ki*8;PK[o]=p0;PK[o+1]=pts.length;PK[o+2]=len;PK[o+3]=L.pk;PK[o+4]=i;PK[o+5]=L.w0;PK[o+6]=dep;ki++;}}
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.lv.b);gl.bufferData(gl.ARRAY_BUFFER,SEG,gl.DYNAMIC_DRAW);
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.pv.b);gl.bufferData(gl.ARRAY_BUFFER,PK,gl.DYNAMIC_DRAW);
|
||||
gl.bindTexture(gl.TEXTURE_2D,GLX.tex);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA32F,GL_PTS_W,TH,0,gl.RGBA,gl.FLOAT,PT);
|
||||
Object.assign(GLX,{nSeg:ns,nPk:nk,sel});return {SEG,PK,PT};}
|
||||
Object.assign(GLX,{nSeg:ns,nPk:nk});return {SEG,PK,PT};}
|
||||
// The selected tube's halo: its segments again, at its own depth (so later tubes still cover it), and nothing else.
|
||||
function glHalo(LG,GM,looks,selId){const gl=GLX.gl,N=LG.length,A=[];
|
||||
if(selId)for(let gi=0;gi<N;gi++){if(!LG[gi].some(x=>x.id===selId))continue;const L=looks[gi],{pts,cum,len}=GM[gi],dep=glDepth(gi,N),col=glColor(L.col);
|
||||
for(let j=0;j+1<pts.length;j++)A.push(pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,LINE_SEL,dep,col[0],col[1],col[2]);}
|
||||
const F=new Float32Array(A);if(F.length){gl.bindBuffer(gl.ARRAY_BUFFER,GLX.hv.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);}
|
||||
GLX.nHalo=F.length/LINE_STRIDE;return F;}
|
||||
// Draws the tubes and their packets over the background glBegin() painted (ML: mapLinks(), sc: the icon scale).
|
||||
function glFrame(ML,ks,far,busy,now,sc){const gl=GLX.gl;
|
||||
const selId=UI.sel&&UI.sel.type==='link'?UI.sel.id:'',key=[ML,selId];
|
||||
if(key.some((v,i)=>v!==GLX.key[i])){glBuild(ML.LG,ML.GM,ML.looks,selId);GLX.key=key;}
|
||||
const selId=UI.sel&&UI.sel.type==='link'?UI.sel.id:'',net=GLX.key[0]!==ML;
|
||||
if(net)glBuild(ML.LG,ML.GM,ML.looks);
|
||||
if(net||GLX.key[1]!==selId){glHalo(ML.LG,ML.GM,ML.looks,selId);GLX.key=[ML,selId];}
|
||||
gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.LEQUAL);gl.depthMask(true);
|
||||
const L=GLX.line,K=GLX.pk,wm=sc*(far?0.5:1),lines=(pass)=>{gl.useProgram(L.p);gl.bindVertexArray(GLX.lv.v);gl.uniform1f(L.u.uBusy,busy?1:0);gl.uniform1i(L.u.uPass,pass);
|
||||
for(const k of ks){glSet(gl,L,k,wm);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,GLX.nSeg);}};
|
||||
const L=GLX.line,K=GLX.pk,wm=sc*(far?0.5:1),lines=(pass,v=GLX.lv.v,n=GLX.nSeg)=>{gl.useProgram(L.p);gl.bindVertexArray(v);gl.uniform1f(L.u.uBusy,busy?1:0);gl.uniform1i(L.u.uPass,pass);
|
||||
for(const k of ks){glSet(gl,L,k,wm);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,n);}};
|
||||
if(GLX.nSeg)for(const pass of [LP.casing,LP.colour,LP.dots])lines(pass);
|
||||
if(GLX.nPk){gl.useProgram(K.p);gl.bindVertexArray(GLX.pv.v);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLX.tex);gl.uniform1i(K.u.uPts,0);
|
||||
gl.uniform1f(K.u.uT,now/1000);gl.uniform1f(K.u.uFar,far?1:0);for(const k of ks){glSet(gl,K,k,wm);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,GLX.nPk);}}
|
||||
// The selection's halo goes over earlier tubes but under its own line and later ones, as in 2D.
|
||||
if(GLX.sel&&GLX.nSeg){gl.depthFunc(gl.LESS);gl.depthMask(false);lines(LP.halo);gl.depthMask(true);}
|
||||
if(GLX.nHalo){gl.depthFunc(gl.LESS);gl.depthMask(false);lines(LP.halo,GLX.hv.v,GLX.nHalo);gl.depthMask(true);}
|
||||
gl.bindVertexArray(null);}
|
||||
|
|
|
|||
|
|
@ -36,21 +36,22 @@ function spriteEntry(key,w,h,ax,ay,paint){key=DPR+'|'+key;let e=SPRS.get(key);if
|
|||
e={cv,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1};SPRS.set(key,e);return e;}
|
||||
// Puts a picture into the atlas (once), shelf by shelf. A full atlas starts again; this picture then waits a frame.
|
||||
function glsEnsure(e){if(e.up)return true;const gl=GLX.gl;if(!gl||!GLS.prog)return false;
|
||||
const pw=e.cv.width,ph=e.cv.height,A=GLS.size;if(pw>A||ph>A)return false;
|
||||
try{return glsPut(gl,e);}catch(x){throw glErr(x);}}
|
||||
function glsPut(gl,e){const pw=e.cv.width,ph=e.cv.height,A=GLS.size;if(pw>A||ph>A)return false;
|
||||
if(GLS.shx+pw>A){GLS.shx=0;GLS.shy+=GLS.shh;GLS.shh=0;}
|
||||
if(GLS.shy+ph>A){spritesReset();return false;}
|
||||
const x=GLS.shx,y=GLS.shy;GLS.shx+=pw;GLS.shh=Math.max(GLS.shh,ph);
|
||||
gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true);
|
||||
gl.texSubImage2D(gl.TEXTURE_2D,0,x,y,gl.RGBA,gl.UNSIGNED_BYTE,e.cv);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,false);
|
||||
Object.assign(e,{up:true,u0:x/A,v0:y/A,u1:(x+pw)/A,v1:(y+ph)/A});return true;}
|
||||
// The next record in GLS.data, which grows as needed.
|
||||
// The next record in GLS.data, which grows as needed: take it before reading GLS.data, which it may replace.
|
||||
function glsNext(){if((GLS.n+1)*SPR_STRIDE>GLS.data.length){const nd=new Float32Array(GLS.data.length*2);nd.set(GLS.data);GLS.data=nd;}return GLS.n++*SPR_STRIDE;}
|
||||
// Draws a picture at (x,y) of w×h CSS px, on the card for a frame drawn there, else with the 2D canvas.
|
||||
function blit(e,x,y,w,h){if(GLS.frame&&glsEnsure(e)){const d=GLS.data,o=glsNext();
|
||||
function blit(e,x,y,w,h){if(GLS.frame&&glsEnsure(e)){const o=glsNext(),d=GLS.data;
|
||||
d[o]=x;d[o+1]=y;d[o+2]=w;d[o+3]=h;d[o+4]=e.u0;d[o+5]=e.v0;d[o+6]=e.u1;d[o+7]=e.v1;d[o+8]=d[o+9]=d[o+10]=0;d[o+11]=ctx.globalAlpha;d[o+12]=0;}
|
||||
else ctx.drawImage(e.cv,x,y,w,h);}
|
||||
// A round dot of radius r at (x,y), in colour col (CSS colour string) at opacity a, on the card or the 2D canvas.
|
||||
function dot(x,y,r,col,a){if(GLS.frame){const c=glColor(col),d=GLS.data,o=glsNext();
|
||||
function dot(x,y,r,col,a){if(GLS.frame){const c=glColor(col),o=glsNext(),d=GLS.data;
|
||||
d[o]=x;d[o+1]=y;d[o+2]=r;d[o+3]=d[o+4]=d[o+5]=d[o+6]=d[o+7]=0;d[o+8]=c[0];d[o+9]=c[1];d[o+10]=c[2];d[o+11]=(a===undefined?ctx.globalAlpha:a)*c[3];d[o+12]=1;}
|
||||
else{const sa=ctx.globalAlpha;if(a!==undefined)ctx.globalAlpha=a;ctx.fillStyle=col;ctx.beginPath();ctx.arc(x,y,r,0,7);ctx.fill();ctx.globalAlpha=sa;}}
|
||||
// A small picture w×h px at scale k with its anchor at (ax,ay), painted once by paint(g) with the origin on the anchor
|
||||
|
|
@ -65,12 +66,13 @@ function textStamp(t,font,px,tw,fill,stroke,lw,x,y){const W=Math.ceil(tw+lw+4),H
|
|||
if(lw){g.lineWidth=lw;g.lineJoin='round';g.strokeStyle=stroke;g.strokeText(t,0,0);}g.fillStyle=fill;g.fillText(t,0,0);});
|
||||
blit(e,Math.round((x-e.w/2)*DPR)/DPR,Math.round((y-e.h/2)*DPR)/DPR,e.w,e.h);}
|
||||
// Sends the queued pictures to the card in one draw.
|
||||
function glsDrawNow(){const gl=GLX.gl;if(!gl||!GLS.prog||!GLS.n){GLS.n=0;return;}
|
||||
gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl.bindBuffer(gl.ARRAY_BUFFER,GLS.buf);
|
||||
gl.bufferData(gl.ARRAY_BUFFER,GLS.data.subarray(0,GLS.n*SPR_STRIDE),gl.STREAM_DRAW);
|
||||
function glsDrawNow(){const gl=GLX.gl,n=GLS.n;GLS.n=0;if(!gl||!GLS.prog||!n)return;
|
||||
try{glsDraw(gl,n);}catch(x){throw glErr(x);}}
|
||||
function glsDraw(gl,n){gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl.bindBuffer(gl.ARRAY_BUFFER,GLS.buf);
|
||||
gl.bufferData(gl.ARRAY_BUFFER,GLS.data.subarray(0,n*SPR_STRIDE),gl.STREAM_DRAW);
|
||||
gl.uniform2f(GLS.prog.u.uDev,GLX.c.width,GLX.c.height);gl.uniform1f(GLS.prog.u.uDpr,DPR);gl.uniform1i(GLS.prog.u.uTex,0);
|
||||
gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);
|
||||
gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);gl.disable(gl.DEPTH_TEST);
|
||||
gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,GLS.n);gl.bindVertexArray(null);GLS.n=0;}
|
||||
// The end of a frame on the card: the pictures go up over the tubes.
|
||||
function glsFrameEnd(){if(GLS.frame){glsDrawNow();GLS.frame=false;}}
|
||||
gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,n);gl.bindVertexArray(null);}
|
||||
// The end of a frame on the card: the pictures go up over the tubes. The frame is over even if that draw fails.
|
||||
function glsFrameEnd(){if(!GLS.frame)return;GLS.frame=false;glsDrawNow();}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
function draw(now){DFC=new Map();try{drawFrame(now);}finally{DFC=null;glsFrameEnd();ctx=CTX0;}}
|
||||
/* A frame. An error from the card itself (glErr()) gives WebGL up for this session and draws the same frame again in
|
||||
2D, once; any other error goes on to the loop's tickErr(). Either way the 2D context and the sprite queue are reset. */
|
||||
function draw(now){for(let pass=0;;pass++){let err=null;DFC=new Map();
|
||||
try{drawFrame(now);}catch(e){err=e;}
|
||||
try{glsFrameEnd();}catch(e){if(!err)err=e;}finally{DFC=null;GLS.frame=false;GLS.n=0;ctx=CTX0;}
|
||||
if(!err)return;if(!err.glErr||pass)throw err;glKill(err);}}
|
||||
// Rectangles on a 64 px grid: a new one is compared only with the few that share its cells (a plain list made the
|
||||
// label placement quadratic in the number of cities).
|
||||
class RGrid{constructor(){this.m=new Map();}
|
||||
|
|
@ -7,10 +12,11 @@ class RGrid{constructor(){this.m=new Map();}
|
|||
hits(r){let h=false;this.cells(r,k=>{if(h)return;const a=this.m.get(k);if(a)for(const q of a)if(r.x<q.x+q.w&&q.x<r.x+r.w&&r.y<q.y+q.h&&q.y<r.y+r.h){h=true;return;}});return h;}}
|
||||
function drawFrame(now){
|
||||
if(!ctx)return;
|
||||
const ks=visibleKs(),z=cam.z;
|
||||
const ks=visibleKs(),z=cam.z,G=glOn();
|
||||
// WebGL on: the sea, land, waves, clouds and cables are on the card (glBegin); the 2D canvas below them stays empty.
|
||||
if(glOn())glBegin(now,ks);
|
||||
else{GLX.c0clean=false;const wl=worldLayer(z);
|
||||
// Off (2D chosen, no game, a puzzle, the card given up), its layers are hidden here, before anything can return early.
|
||||
if(G){try{glBegin(now,ks);}catch(e){throw glErr(e);}}
|
||||
else{glShow(false);GLX.c0clean=false;const wl=worldLayer(z);
|
||||
ctx.setTransform(DPR,0,0,DPR,0,0);
|
||||
// sea
|
||||
const g=ctx.createLinearGradient(0,0,0,CH);g.addColorStop(0,'#62CBEA');g.addColorStop(1,'#43AED6');ctx.fillStyle=g;ctx.fillRect(0,0,CW,CH);
|
||||
|
|
@ -37,7 +43,7 @@ function drawFrame(now){
|
|||
AR_PILLS.length=0;
|
||||
const far=lodFar(),ML=mapLinks(day),LG=ML.LG,GM=ML.GM,PI=ML.PI,busy=linesOnScreen(GM,ks)>MAP_BUSY;
|
||||
// WebGL on: the card draws every line and packet now, and the rest (labels, markers, cities…) goes on the overlay.
|
||||
const G=glOn();if(G)glFrame(ML,ks,far,busy,now,sc);else glShow(false);
|
||||
if(G)try{glFrame(ML,ks,far,busy,now,sc);}catch(e){throw glErr(e);}
|
||||
for(let gi=0;gi<LG.length;gi++){const grp=LG[gi],{l,many,up,cap,load,df,bld,cut,dark,vx,w0,col,dash,pk}=ML.looks[gi],gm=GM[gi];
|
||||
const pz=pzOn(),arPz=pz&&!!S.ar,sel=(UI.sel&&UI.sel.type==='link'&&grp.some(x=>x.id===UI.sel.id))||(pz&&pzLinkHot(grp));let lab=null;
|
||||
// Only what sits at the middle of a line is still painted here when the card draws the lines.
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ function cityFarStatic(C,p,down,sel,hov,ph){ctx.strokeStyle='#1E2640';
|
|||
// (cityMoving): the lights, the steam, a flashing IX, the hot badges and the offer bubble. The rings and the glow move too,
|
||||
// but in steps (cityPhase), so their sprite changes only at a step.
|
||||
const CSPR_STEP=24;
|
||||
function cityPhase(C,p,sel,now){const ph={sel:0,dd:0,gl:-1};
|
||||
// dd is -1 without an attack: the attack's ring at step 0 must not share a sprite with no ring at all.
|
||||
function cityPhase(C,p,sel,now){const ph={sel:0,dd:-1,gl:-1};
|
||||
if(sel)ph.sel=Math.floor(now/40)%12; // dash period 12 px, lineDashOffset -now/40
|
||||
if(p&&R.ddosHot&&R.ddosHot[C.id])ph.dd=Math.floor(now/30)%8; // dash period 8 px, lineDashOffset now/30
|
||||
if(p&&!(p.down>S.day)&&R.cityDem[C.id]>0&&sfOf(R,C.id)<0.9)ph.gl=Math.round((Math.sin(now/200)+1)*3.5); // 8 levels of .35+.25·sin
|
||||
|
|
@ -43,11 +44,12 @@ function cityPhase(C,p,sel,now){const ph={sel:0,dd:0,gl:-1};
|
|||
function cityIX(C,p,t,h,hot,now){const mine=!!S.myIx[C.id],joined=hasIxAt(C.id),q=!p&&!hot&&ixQuiet(C.id),Rr=q?6.5:8.5,ix=p?19:SKY_W[t]/2+(q?2:4);
|
||||
ctx.fillStyle=hot?(Math.sin(now/180)>0?'#FF4D5E':'#FF8A3D'):mine?'#FF4F9A':joined?'#FFD23F':'#FFFFFF';ctx.beginPath();for(let i=0;i<6;i++){const a=Math.PI/3*i+Math.PI/6;ctx.lineTo(ix+Rr*Math.cos(a),-h+2+Rr*Math.sin(a));}ctx.closePath();ctx.fill();ctx.lineWidth=q?1.4:2;ctx.stroke();
|
||||
ctx.fillStyle=mine||hot?'#fff':'#1E2640';ctx.font=`900 ${q?6.5:8}px Nunito, sans-serif`;ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText('IX',ix,-h+2.5);}
|
||||
// Everything about the city that does not move, in the city's own frame (origin on the city). Drawn into a sprite.
|
||||
// Everything about the city that does not move, in the city's own frame (origin on the city). Drawn into a sprite, so
|
||||
// it reads only what citySprite() puts in the key.
|
||||
function cityStatic(C,p,t,cs,k,ph,st){const down=st.down;
|
||||
if(st.feed){ctx.strokeStyle='#FF8A3D';ctx.lineWidth=4;ctx.setLineDash([3,5]);ctx.beginPath();ctx.arc(0,-6*k,36*k,0,7);ctx.stroke();ctx.setLineDash([]);}
|
||||
if(st.sel){ctx.strokeStyle='#FF4F9A';ctx.lineWidth=3;ctx.setLineDash([6,6]);ctx.lineDashOffset=-ph.sel;ctx.beginPath();ctx.arc(0,-6*k,30*k,0,7);ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;}
|
||||
if(p&&R.ddosHot&&R.ddosHot[C.id]){ctx.strokeStyle='#FF4D5E';ctx.lineWidth=3;ctx.setLineDash([4,4]);ctx.lineDashOffset=ph.dd;ctx.beginPath();ctx.arc(0,-6*k,33*k,0,7);ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;}
|
||||
if(ph.dd>=0){ctx.strokeStyle='#FF4D5E';ctx.lineWidth=3;ctx.setLineDash([4,4]);ctx.lineDashOffset=ph.dd;ctx.beginPath();ctx.arc(0,-6*k,33*k,0,7);ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;}
|
||||
ctx.scale(k*cs,k*cs);
|
||||
ctx.fillStyle='rgba(30,38,64,.2)';ctx.beginPath();ctx.ellipse(0,9,p?20:SKY_W[t]/2+5,5,0,0,7);ctx.fill();
|
||||
const h=p?18:SKY_H[t];
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in
|
|||
return (...a)=>{calls.push([k,...a]);return {};};}});
|
||||
const c=document.getElementById('mapgl'),f=document.getElementById('mapfx');
|
||||
const P=()=>({p:{},u:{}}),B=()=>({v:{},b:{}});
|
||||
Object.assign(GLX,{want:true,dead:false,gl,c,f,fx:f.getContext('2d'),line:P(),pk:P(),fill:P(),fan:P(),ell:P(),land:{},circ:P(),tex:{},lv:B(),pv:B(),cb:B(),wv:B(),cl:B(),empty:{},key:[],gpu:'stub'});return gl;})()`;
|
||||
Object.assign(GLX,{want:true,dead:false,gl,c,f,fx:f.getContext('2d'),line:P(),pk:P(),fill:P(),fan:P(),ell:P(),land:{},circ:P(),tex:{},lv:B(),hv:B(),pv:B(),cb:B(),wv:B(),cl:B(),empty:{},key:[],gpu:'stub'});return gl;})()`;
|
||||
|
||||
test('without WebGL2 the map stays 2D, and asking for WebGL changes nothing', () => {
|
||||
const { ev, errors } = open();
|
||||
|
|
@ -38,23 +38,24 @@ test('the instance data says what the 2D map draws: points, colours, dashes, pac
|
|||
const { ev } = open();
|
||||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{const PI=pairIdx(),LG=linkGroups(),GM=LG.map(g=>linkGeom(g[0],PI.get(edgeKey(g[0]))));
|
||||
const sel=LG[3][0].id,looks=LG.map(g=>linkLook(g,S.day)),{SEG,PK,PT}=glBuild(LG,GM,looks,sel);let si=0,ki=0,pi=0,bad=[];const deps=[];
|
||||
const sel=LG[3][0].id,looks=LG.map(g=>linkLook(g,S.day)),{SEG,PK,PT}=glBuild(LG,GM,looks),H=glHalo(LG,GM,looks,sel);let si=0,ki=0,pi=0,hi=0,bad=[];const deps=[];
|
||||
for(let gi=0;gi<LG.length;gi++){const gm=GM[gi],L=looks[gi],[r,g,b]=glColor(L.col),hot=LG[gi].some(x=>x.id===sel);
|
||||
for(let j=0;j<gm.pts.length;j++){if(PT[pi*4]!==Math.fround(gm.pts[j][0])||PT[pi*4+2]!==Math.fround(gm.cum[j]))bad.push('pt '+gi);pi++;}
|
||||
for(let j=0;j+1<gm.pts.length;j++){const o=si*12;
|
||||
if(SEG[o]!==Math.fround(gm.pts[j][0])||SEG[o+3]!==Math.fround(gm.pts[j+1][1]))bad.push('seg '+gi);
|
||||
if(SEG[o+6]!==L.dash||SEG[o+5]!==Math.fround(L.w0)||SEG[o+9]!==Math.fround(r)||SEG[o+11]!==Math.fround(b))bad.push('look '+gi);
|
||||
if(((SEG[o+7]&1)===1)!==hot||((SEG[o+7]&2)===2)!==(L.df&&!L.bld&&!L.dark))bad.push('flags '+gi);
|
||||
if((SEG[o+7]&1)===1||((SEG[o+7]&2)===2)!==(L.df&&!L.bld&&!L.dark))bad.push('flags '+gi);
|
||||
if(hot){const h=hi*12;for(let x=0;x<12;x++)if(H[h+x]!==(x===7?1:SEG[o+x]))bad.push('halo '+gi);hi++;}
|
||||
if(j===0)deps.push(SEG[o+8]);si++;}
|
||||
const n=!L.cut&&L.load>0.01?clamp(Math.round(2+L.u*10),2,12):0;for(let i=0;i<n;i++){if(PK[ki*8+3]!==n||PK[ki*8+4]!==i)bad.push('pk '+gi);ki++;}}
|
||||
return JSON.stringify({bad:bad.slice(0,5),si,segs:SEG.length/12,ki,pks:PK.length/8,groups:LG.length,
|
||||
order:deps.every((d,i)=>!i||d<deps[i-1]),inRange:deps.every(d=>d>0&&d<1),sel:GLX.sel,moving:looks.filter(L=>!L.cut&&L.load>0.01).length});})()`));
|
||||
order:deps.every((d,i)=>!i||d<deps[i-1]),inRange:deps.every(d=>d>0&&d<1),sel:hi>0&&hi===GLX.nHalo,moving:looks.filter(L=>!L.cut&&L.load>0.01).length});})()`));
|
||||
assert.deepEqual(r.bad, []);
|
||||
assert.equal(r.si, r.segs, 'one instance per segment');
|
||||
assert.equal(r.ki, r.pks, 'one instance per packet');
|
||||
assert.ok(r.groups > 50 && r.moving > 20, 'a big map with traffic');
|
||||
assert.ok(r.order && r.inRange, 'later tubes are nearer, so they paint over earlier ones as in 2D');
|
||||
assert.equal(r.sel, true, 'the selected tube gets its halo');
|
||||
assert.equal(r.sel, true, 'the selected tube, and only it, gets its halo: its own segments at its own depth');
|
||||
});
|
||||
|
||||
test('with WebGL the lines go to the card, the rest to the overlay, and the map gets its canvas back', () => {
|
||||
|
|
@ -193,3 +194,114 @@ test('Settings › Map drawing: Auto uses the card, Always 2D keeps the 2D map a
|
|||
assert.equal(ev(`document.getElementById('mapgl').hidden`), false);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a sprite record that makes the queue grow is kept, for pictures and for discs', () => {
|
||||
const { ev, errors } = open();
|
||||
// Pictures and discs alternate, so both cross a growth (2048, then 4096 records); each record says which one it is.
|
||||
const bad = JSON.parse(ev(`(()=>{const e={up:true,u0:.1,v0:.2,u1:.3,v1:.4,cv:null};GLS.frame=true;GLS.n=0;GLS.data=new Float32Array(SPR_STRIDE*2048);
|
||||
for(let i=0;i<4100;i++){ctx.globalAlpha=(i%7+1)/8;if(i%2)dot(i,i*2,3+i%5,'#FF4D5E',(i%3+1)/4);else blit(e,i,i*2,10+i%9,20+i%11);}
|
||||
ctx.globalAlpha=1;const D=GLS.data,n=GLS.n,cap=D.length/SPR_STRIDE,bad=[];GLS.frame=false;GLS.n=0;
|
||||
for(let i=0;i<n;i++){const o=i*SPR_STRIDE,want=i%2?[i,i*2,3+i%5,1,(i%3+1)/4]:[i,i*2,10+i%9,0,(i%7+1)/8];
|
||||
const got=i%2?[D[o],D[o+1],D[o+2],D[o+12],D[o+11]]:[D[o],D[o+1],D[o+2],D[o+12],D[o+11]];
|
||||
if(i%2===0&&(D[o+3]!==20+i%11||D[o+4]!==Math.fround(.1)||D[o+7]!==Math.fround(.4)))bad.push(i);
|
||||
else if(i%2&&D[o+8]!==1)bad.push(i);
|
||||
else if(want.some((v,j)=>Math.fround(v)!==got[j]))bad.push(i);}
|
||||
return JSON.stringify({n,cap,bad:bad.slice(0,8),edges:[2047,2048,2049,4096,4097].filter(i=>bad.includes(i))});})()`));
|
||||
assert.equal(bad.n, 4100);
|
||||
assert.ok(bad.cap >= 4100, 'the queue grew past 4096 records');
|
||||
assert.deepEqual(bad.edges, [], 'the records at each growth are where they were drawn');
|
||||
assert.deepEqual(bad.bad, [], 'and so is every other record');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a city under attack never reuses its picture without the attack ring, and gets it back after', () => {
|
||||
const { ev, errors } = open();
|
||||
const r = JSON.parse(ev(`(()=>{const id=Object.keys(S.pops).find(c=>!S.pops[c].down||S.pops[c].down<=S.day),C=CITY[id],p=S.pops[id],t=cTier(id);
|
||||
const st={down:false,feed:false,sel:false,inc:false,ixDraw:false,ixHot:false,off:false,big:false};
|
||||
const spr=now=>{const ph=cityPhase(C,p,false,now);return [ph.dd,citySprite(C,p,t,1,1,ph,st)];};
|
||||
R.ddosHot={};const [d0,quiet]=spr(0);
|
||||
R.ddosHot={[id]:1};const [d1,hit0]=spr(0),[d2,hit3]=spr(90);
|
||||
R.ddosHot={};const [d3,after]=spr(0);
|
||||
// Animations off: the map's clock stays at 0, so the attack is always at step 0.
|
||||
R.ddosHot={[id]:1};const [d4,still]=spr(0);
|
||||
return JSON.stringify({dd:[d0,d1,d2,d3,d4],sameAtZero:quiet===hit0,step:hit0===hit3,back:after===quiet,still:still===hit0});})()`));
|
||||
assert.deepEqual(r.dd, [-1, 0, 3, -1, 0]);
|
||||
assert.equal(r.sameAtZero, false, 'step 0 of an attack is not the picture without one');
|
||||
assert.equal(r.step, false, 'the ring turns');
|
||||
assert.equal(r.back, true, 'once over, the plain picture is back');
|
||||
assert.equal(r.still, true, 'with animations off the attack keeps its own picture');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('leaving a game hides the card’s layers, and the next game shows them again', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
const shown = () => JSON.parse(ev(`JSON.stringify([GLX.shown,!document.getElementById('mapgl').hidden,!document.getElementById('mapfx').hidden])`));
|
||||
ev('draw(1000)');
|
||||
assert.deepEqual(shown(), [true, true, true], 'a game on the card');
|
||||
ev('ACT.toTitle();draw(1016)');
|
||||
assert.deepEqual(shown(), [false, false, false], 'back to the title: the old network is not left behind it');
|
||||
assert.ok(ev('ctx===CTX0'));
|
||||
ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;compute();draw(1032)`);
|
||||
assert.deepEqual(shown(), [true, true, true], 'the next game is on the card again');
|
||||
ev(`slotBroke(UI.slot||'x');draw(1048)`);
|
||||
assert.deepEqual(shown(), [false, false, false], 'a damaged save leaves the title with an empty map too');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
// Faults injected into the card at each step of a frame: WebGL is given up once, the same frame is drawn in 2D, the
|
||||
// layers are hidden, and the next frames are 2D without another try or another error.
|
||||
for (const [where, inject] of [
|
||||
['the start of a frame', `GLX.gl.clear=()=>{throw new Error('boom')};`],
|
||||
['an upload of the tubes', `GLX.key=[];GLX.gl.texImage2D=()=>{throw new Error('boom')};`],
|
||||
['the pictures’ draw', `GLS.prog={p:{},u:{}};GLS.vao={};GLS.tex={};glsDraw=()=>{throw new Error('boom')};`],
|
||||
['a picture going into the atlas', `GLS.prog={p:{},u:{}};GLS.vao={};GLS.tex={};SPRS.clear();GLX.gl.texSubImage2D=()=>{throw new Error('boom')};`],
|
||||
]) {
|
||||
test(`a fault in ${where} falls back to 2D once and for all`, () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
ev(`draw(1000);${inject}window.__tries=0;const gb=glBegin;glBegin=function(){__tries++;return gb.apply(this,arguments);};
|
||||
window.__warn=0;console.warn=function(){__warn++;};`);
|
||||
const c0 = ev('CTX0.calls.n');
|
||||
ev('draw(1016);draw(1032);draw(1048)');
|
||||
assert.equal(ev('GLX.dead'), true, 'WebGL is given up');
|
||||
assert.equal(ev('__tries'), 1, 'and not tried again every frame');
|
||||
assert.equal(ev('__warn'), 1, 'said once');
|
||||
assert.ok(ev('ctx===CTX0'), 'the 2D context is back');
|
||||
assert.equal(ev('GLS.frame'), false);
|
||||
assert.equal(ev('GLS.n'), 0);
|
||||
assert.equal(ev(`document.getElementById('mapgl').hidden&&document.getElementById('mapfx').hidden`), true, 'the layers are hidden');
|
||||
assert.ok(ev('CTX0.calls.n') - c0 > 1000, 'the 2D map is drawn, from the frame that failed on');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
test('an error outside the card still reaches the loop, and leaves the frame clean', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
ev(`draw(1000);window.__dc=drawCity;drawCity=function(){throw new Error('not the card');};`);
|
||||
assert.throws(() => ev('draw(1016)'), /not the card/);
|
||||
assert.equal(ev('GLX.dead'), false, 'WebGL stays on: the card did nothing wrong');
|
||||
assert.ok(ev('ctx===CTX0'));
|
||||
assert.equal(ev('GLS.frame'), false);
|
||||
ev('drawCity=__dc;draw(1032)');
|
||||
assert.equal(ev('GLX.shown'), true);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('selecting a tube sends its halo to the card, not the network again', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;draw(1000);const c=GLX.gl.calls,nSeg=GLX.nSeg;
|
||||
const sent=()=>{const b=c.filter(x=>x[0]==='bufferData').map(x=>x[2]&&x[2].length||0),t=c.filter(x=>x[0]==='texImage2D').length;c.length=0;return {b,t};};
|
||||
sent();const l=S.links.find(l=>l.kind!=='vxc'&&!linkDown(l));UI.sel={type:'link',id:l.id};draw(1016);const on=sent(),halo=GLX.nHalo;
|
||||
UI.sel=null;draw(1032);const off=sent();
|
||||
return JSON.stringify({nSeg,on,off,halo,haloDraw:GLX.nHalo});})()`));
|
||||
const tubes = r.nSeg * 12;
|
||||
assert.ok(r.halo > 0, 'the halo has the selected tube’s segments');
|
||||
assert.ok(!r.on.b.includes(tubes) && r.on.t === 0, 'selecting sends no tube, packet or point again');
|
||||
assert.ok(r.on.b.includes(r.halo * 12), 'only the halo goes up');
|
||||
assert.ok(!r.off.b.includes(tubes) && r.off.t === 0, 'nor does unselecting');
|
||||
assert.equal(r.haloDraw, 0, 'and the halo is gone');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue