Map: a link's curve only measures the cities that could be in its way
bezGeom() measured every city against every candidate curve, for every link the first time the map drew it. A curve stays inside the box of its end and control points, so a city further than the clearance it needs outside that box always measures as clear; leaving it out gives the same curve. About 4x faster, and the curves on the big fixture are byte-identical.
This commit is contained in:
parent
adc312f27b
commit
5c2b3a4758
2 changed files with 25 additions and 2 deletions
|
|
@ -39,13 +39,18 @@ function bezGeom(l,at){const A=CITY[l.a],B=CITY[l.b];const i=at!==undefined?at:l
|
|||
const bx=A.x+wrapDx(A.x,B.x),by=B.y;const dx=bx-A.x,dy=by-A.y,len=Math.hypot(dx,dy)||1;let nx=-dy/len,ny=dx/len;if(ny>0){nx=-nx;ny=-ny;}
|
||||
const base=Math.max(4,Math.min(len*0.1,90)),step=Math.min(16,len*.06),off0=i<=0?base:(i%2?-1:1)*(base*(i%2?0.2:1)+Math.ceil(i/2)*step);
|
||||
const mx=(A.x+bx)/2,my=(A.y+by)/2;
|
||||
const obs=CITIES.filter(c=>c.id!==l.a&&c.id!==l.b).map(c=>[mx+wrapDx(mx,c.x),c.y]);
|
||||
const clear=(cx,cy)=>{let m=Infinity;const g={ax:A.x,ay:A.y,bx,by,cx,cy};for(let t=0.08;t<0.93;t+=0.06){const [px,py]=qpt(g,t);for(const [ox,oy] of obs){const d=Math.hypot(px-ox,py-oy);if(d<m)m=d;}}return m;};
|
||||
const sg=Math.sign(off0)||1,mag=Math.abs(off0);
|
||||
// Bend around cities in the way, but gently: short links need less room, no curve bows out more than about a
|
||||
// third of its length, and when nothing clears, a modest curve beats a giant loop around the neighbourhood.
|
||||
const need=Math.min(24,Math.max(7,len*0.2)),cap=Math.max(mag,len*0.32),lim=o=>Math.sign(o)*Math.min(Math.abs(o),cap);
|
||||
const cand=[1,1.5,2.1,2.9,3.8].map(k=>lim(sg*mag*k)).concat([1,1.5,2.1,2.9,3.8].map(k=>lim(-sg*Math.max(mag,base)*k)));
|
||||
/* Only cities that could come within `need` of a candidate curve count: a curve stays inside the box of its end and
|
||||
control points, so a city further than `need` outside the box of all of them only ever measures as clear, and
|
||||
min(clearance, need) comes out the same without it (the map's first frame did this for every city and link). */
|
||||
let x0=Math.min(A.x,bx),x1=Math.max(A.x,bx),y0=Math.min(A.y,by),y1=Math.max(A.y,by);
|
||||
for(const off of cand){const cx=mx+nx*off,cy=my+ny*off;if(cx<x0)x0=cx;if(cx>x1)x1=cx;if(cy<y0)y0=cy;if(cy>y1)y1=cy;}
|
||||
const obs=[];for(const c of CITIES){if(c.id===l.a||c.id===l.b)continue;const ox=mx+wrapDx(mx,c.x),oy=c.y;if(ox>=x0-need&&ox<=x1+need&&oy>=y0-need&&oy<=y1+need)obs.push([ox,oy]);}
|
||||
const clear=(cx,cy)=>{let m=Infinity;const g={ax:A.x,ay:A.y,bx,by,cx,cy};for(let t=0.08;t<0.93;t+=0.06){const [px,py]=qpt(g,t);for(const [ox,oy] of obs){const d=Math.hypot(px-ox,py-oy);if(d<m)m=d;}}return m;};
|
||||
let best=null,bs=-Infinity;
|
||||
for(const off of cand){const cx=mx+nx*off,cy=my+ny*off,c=clear(cx,cy);if(c>=need){best=[cx,cy];break;}const sc=Math.min(c,need)-Math.abs(off)*0.35;if(sc>bs){bs=sc;best=[cx,cy];}}
|
||||
const g={ax:A.x,ay:A.y,bx,by,cx:best[0],cy:best[1],len};GEO.set(key,g);return g;}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,24 @@ 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('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({})');
|
||||
// bezGeom() as it was: every other city is measured against every candidate curve.
|
||||
ev(`window.plainGeom=(l,i)=>{const A=CITY[l.a],B=CITY[l.b];const bx=A.x+wrapDx(A.x,B.x),by=B.y;const dx=bx-A.x,dy=by-A.y,len=Math.hypot(dx,dy)||1;let nx=-dy/len,ny=dx/len;if(ny>0){nx=-nx;ny=-ny;}
|
||||
const base=Math.max(4,Math.min(len*0.1,90)),step=Math.min(16,len*.06),off0=i<=0?base:(i%2?-1:1)*(base*(i%2?0.2:1)+Math.ceil(i/2)*step);const mx=(A.x+bx)/2,my=(A.y+by)/2;
|
||||
const obs=CITIES.filter(c=>c.id!==l.a&&c.id!==l.b).map(c=>[mx+wrapDx(mx,c.x),c.y]);
|
||||
const clear=(cx,cy)=>{let m=Infinity;const g={ax:A.x,ay:A.y,bx,by,cx,cy};for(let t=0.08;t<0.93;t+=0.06){const [px,py]=qpt(g,t);for(const [ox,oy] of obs){const d=Math.hypot(px-ox,py-oy);if(d<m)m=d;}}return m;};
|
||||
const sg=Math.sign(off0)||1,mag=Math.abs(off0);const need=Math.min(24,Math.max(7,len*0.2)),cap=Math.max(mag,len*0.32),lim=o=>Math.sign(o)*Math.min(Math.abs(o),cap);
|
||||
const cand=[1,1.5,2.1,2.9,3.8].map(k=>lim(sg*mag*k)).concat([1,1.5,2.1,2.9,3.8].map(k=>lim(-sg*Math.max(mag,base)*k)));let best=null,bs=-Infinity;
|
||||
for(const off of cand){const cx=mx+nx*off,cy=my+ny*off,c=clear(cx,cy);if(c>=need){best=[cx,cy];break;}const sc=Math.min(c,need)-Math.abs(off)*0.35;if(sc>bs){bs=sc;best=[cx,cy];}}
|
||||
return {ax:A.x,ay:A.y,bx,by,cx:best[0],cy:best[1],len};}`);
|
||||
const bad = JSON.parse(ev(`(()=>{const ids=CITIES.map(c=>c.id),bad=[];for(let i=0;i<ids.length;i+=6)for(let j=i+1;j<ids.length;j+=5)for(const k of [0,1,4]){
|
||||
const l={id:'x'+i+'_'+j,a:ids[i],b:ids[j]};if(JSON.stringify(bezGeom(l,k))!==JSON.stringify(plainGeom(l,k)))bad.push(l.a+'-'+l.b+'#'+k);}return JSON.stringify(bad)})()`));
|
||||
assert.deepEqual(bad, []);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('land test: the bounding box shortcut agrees with walking every coastline', (t) => {
|
||||
const { ev, dom, errors } = load(); t.after(() => dom.window.close());
|
||||
const bad = ev(`(()=>{const plain=(x,y,pts)=>{let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const [xi,yi]=pts[i],[xj,yj]=pts[j];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue