Architect runs: a chaos monkey runs round the map snipping tubes

The review is now an animated monkey that hops from cut to cut and sprays
chaos where it snips, instead of the backhoe.
This commit is contained in:
Frederic Jung 2026-10-30 09:25:20 +01:00
parent ab05c80d68
commit b3351fb7d9
No known key found for this signature in database
13 changed files with 96 additions and 60 deletions

View file

@ -1,14 +1,14 @@
/* ================= ARCHITECT RUNS (prototype) =================
One network, built brief by brief. A brief adds sites, makes some of them gold (a gold site must stay connected to HQ
whichever single trench or cable is cut), sets a latency cap, or closes a city. The build carries over: what earlier
briefs built is paid for, so removing it gives nothing back. ▶ sends the backhoe: it cuts every trench and cable one
briefs built is paid for, so removing it gives nothing back. ▶ lets the chaos monkey loose: it cuts every trench and cable one
at a time, and the brief is done when every gold site survives. Runs on the puzzle machinery (S.mode 'puzzle' with
S.ar), so there is no clock, no bills, no incidents, and the main save is never touched.
Capacity: every site pulls its traffic from HQ, and the review also checks that the detour has room: after any single
cut, everything still connected gets all of its traffic. Tubes come in three sizes; routers follow by themselves (the
cheapest model that takes the tubes), and traffic finds the detour by itself (Auto-TE): models, ports and metrics are
someone else's job. */
const AR_KEY='sot-architect',AR_TIER=1,AR_TIERS=[0,1,2],AR_STEP=650;
const AR_KEY='sot-architect',AR_TIER=1,AR_TIERS=[0,1,2],AR_STEP=1000;
// A brief: add (new sites), gold (sites that must survive any single cut), ring (sites that must sit on one ring), drop
// (close a city: no POP allowed there any more), dem {city: Gbps} (what a site pulls from HQ, 10G unless set; a later
// brief can raise it), lat [a,b,ms] (the fastest path between two sites under ms), tubes (the
@ -136,23 +136,23 @@ function arReset(){const o=JSON.parse(S.ar.start);S.pops=o.p;S.links=o.l;S.pairM
const arOver=()=>S.ar.over+arTubes().over;
function arStars(sp){return 1+(arOver()?0:1)+(sp<=arRun().budget+1e-6?1:0);}
function arShareText(st,sp){const sh=arShape();return tr('architect.share_line',{what:arRun().n,stars:pzStarStr(st),spend:fmt$(sp),shape:sh?sh.i+' '+sh.n:''});}
// ---------- ▶ the backhoe review ----------
// ---------- ▶ the chaos monkey review ----------
// Every trench and cable is cut in turn, routed by compute() so the traffic visibly moves; a gold site losing HQ stops
// the review with the cut still on the map, until the next edit.
function arStop(quiet){const sh=UI.arShow;if(sh&&sh.t)clearTimeout(sh.t);UI.arShow=null;if(!S||!S.links)return;let any=false;for(const l of S.links)if(l.cut){l.cut=0;any=true;}
function arStop(quiet){mkStop();const sh=UI.arShow;if(sh&&sh.t)clearTimeout(sh.t);UI.arShow=null;if(!S||!S.links)return;let any=false;for(const l of S.links)if(l.cut){l.cut=0;any=true;}
if(UI.arFail){UI.arFail=null;any=true;}if(any&&!quiet)compute();}
function arShowStart(){if(!arOn()||UI.arShow)return;const e=arEval();if(!e.ready){toast(esc(e.why),'bad');sfx('err');return;}
arStop(true);UI.sel=null;closeModal();UI.arBaseGot=arGot();UI.arShow={units:[...new Set(S.links.map(pzUnit))],i:-1,t:0,dark:[],short:[]};arStep();}
arStop(true);UI.sel=null;closeModal();UI.arBaseGot=arGot();UI.arShow={units:[...new Set(S.links.map(pzUnit))],i:-1,t:0,dark:[],short:[]};mkStart(S.ar.hq);arStep();}
// One cut, routed: who goes dark, which gold sites with it, and who is still connected but short of room.
function arCut(u){const ls=S.links.filter(l=>pzUnit(l)===u);for(const l of ls)l.cut=1;compute();const dark=arDark(u);return {ls,dark,gold:dark.filter(c=>S.ar.gold.includes(c)),short:arShort(dark)};}
function arStep(){const sh=UI.arShow;if(!sh||!arOn())return;for(const l of S.links)l.cut=0;sh.i++;
if(sh.i>=sh.units.length){UI.arShow=null;compute();S.ar.passed=pzSig();renderAll();sfx('star');haptic(15);
toast(`✅ <b>${tr('architect.review_passed')}</b><br>${arLast()?tr('architect.last_brief_done'):tr('architect.next_brief_waiting')}`,'good');if(arLast())setTimeout(()=>{if(arOn()&&S.ar.passed===pzSig())arResults(true);},900);return;}
mkEnd(true);toast(`✅ <b>${tr('architect.review_passed')}</b><br>${arLast()?tr('architect.last_brief_done'):tr('architect.next_brief_waiting')}`,'good');if(arLast())setTimeout(()=>{if(arOn()&&S.ar.passed===pzSig())arResults(true);},900);return;}
const u=sh.units[sh.i],{ls,dark,gold,short}=arCut(u),l0=ls[0],got=arGot();sh.dark=dark;sh.short=short;sh.u=u;
floater((CITY[l0.a].x+CITY[l0.a].x+wrapDx(CITY[l0.a].x,CITY[l0.b].x))/2,(CITY[l0.a].y+CITY[l0.b].y)/2-26,l0.sub?tr('architect.anchor'):tr('architect.backhoe'),'#FF8A3D');
for(const c of dark)floater(CITY[c].x,CITY[c].y-34,gold.includes(c)?'💥 '+CITY[c].name:tr('architect.city_goes_dark',{city:CITY[c].name}),gold.includes(c)?'#FF4D5E':'#9AA3BE');
for(const c of short)floater(CITY[c].x,CITY[c].y-34,tr('architect.city_short_g',{city:CITY[c].name,g:arG(arDem(c)-(got[c]||0))}),'#FF4D5E');
const bad=gold.length||short.length;sfx(bad?'err':'click');
const bad=gold.length||short.length;mkRun(l0,dark.length?AR_STEP*1.6:AR_STEP,bad);sfx(bad?'err':'click');
if(bad){UI.arShow=null;UI.arFail={u,gold,dark,short};renderAll();haptic(30);return;}
renderAll();sh.t=setTimeout(arStep,dark.length?AR_STEP*1.6:AR_STEP);}
// Skip the show: check every cut at once, and stop at the first that breaks a gold site.

View file

@ -12,7 +12,7 @@ function arRender(el){const A=S.ar,run=arRun(),b=arBrief(),e=arEval(),sh=UI.arSh
for(const c of S.pz.avoid)h+=`<div class="arline ${S.pops[c]?'bad':'ok'}">🚫 ${tr('architect.closed_city',{city:nm(c)})}</div>`;
const T=e.tubes;if(T.max!=null)h+=`<div class="artubes${T.over?' over':''}"><span>${tr('architect.tubes_this_brief')}</span><b>${'●'.repeat(Math.min(T.u,T.max))}${'○'.repeat(Math.max(0,T.max-T.u))}${T.over?` <em>${tr('architect.n_over_plan',{n:T.over})}</em>`:''}</b></div>`;
h+=`<div class="pzbud"><div class="kv"><span>${tr('architect.spent_x_of_y',{x:fmt$(e.spend),y:fmt$(e.budget)})}</span><b>${e.cheap?'⭐':'☆'}</b></div><div class="bar ${e.cheap?(e.spend>e.budget*0.9?'warn':''):'bad'}"><i style="width:${Math.min(100,e.spend/e.budget*100).toFixed(1)}%"></i></div>${A.sunk?`<div class="meta">${tr('architect.x_paid_for_and_gone',{x:fmt$(A.sunk)})}</div>`:''}</div>`;
if(sh)h+=`<div class="arrev on">🚜 ${tr('architect.cut_n_of_m',{n:sh.i+1,m:sh.units.length})}${sh.dark.length?`<br><small>${tr('architect.dark_list',{list:sh.dark.map(nm).join(', ')})}</small>`:''}</div>`;
if(sh)h+=`<div class="arrev on">🐒 ${tr('architect.cut_n_of_m',{n:sh.i+1,m:sh.units.length})}${sh.dark.length?`<br><small>${tr('architect.dark_list',{list:sh.dark.map(nm).join(', ')})}</small>`:''}</div>`;
else if(f)h+=`<div class="arrev bad">💥 ${f.gold.length?tr('architect.cut_on_x_cuts_off_y',{x:pzUnitName(f.u),y:f.gold.map(nm).join(', ')}):tr('architect.cut_on_x_no_room',{x:pzUnitName(f.u),y:f.short.map(nm).join(', ')})}<br><small>${tr('architect.fix_it_and_send_it_again')}</small></div>`;
else if(e.passed)h+=`<div class="arrev ok">✅ ${tr('architect.review_passed')}</div>`;
else if(!e.ready)h+=`<div class="arrev">${esc(e.why)}</div>`;

View file

@ -86,6 +86,7 @@ function drawFrame(now){
CM.off=new Set();CM.big=new Set();for(const o of S.offers){const big=BIZ_T[o.type].lv>=3||BIZ_T[o.type].event;for(const c of bCities(o)){CM.off.add(c);if(big)CM.big.add(c);}}
CM.inc=new Set(S.inc.filter(i=>i.type!=='leak').map(i=>i.ref));CM.far=farL;
if(pzOn()){for(const v of vis.slice().reverse())pzDrawCity(v,sc,now);if(S.ar)for(const v of vis)arDrawTags(v,sc);}else for(const v of vis.slice().reverse())drawCity(v.C,v.sx,v.sy,sc,now,v.ly);
if(UI.mk)mkDraw(ks);
// floaters
for(let i=FLOATS.length-1;i>=0;i--){const f=FLOATS[i],a=(now-f.t)/1600;if(a>1){FLOATS.splice(i,1);continue;}
for(const k of ks){const [sx,sy]=toS(f.x,f.y,k);ctx.globalAlpha=1-a*a;ctx.font='800 17px "Baloo 2", sans-serif';ctx.textAlign='center';ctx.textBaseline='middle';ctx.lineWidth=4;ctx.strokeStyle='#fff';ctx.strokeText(f.text,sx,sy-a*40);ctx.fillStyle=f.col;ctx.fillText(f.text,sx,sy-a*40);ctx.globalAlpha=1;}}

View file

@ -0,0 +1,35 @@
/* ---------- the chaos monkey: Architect's review, as a monkey running round the map ----------
It hops from cut to cut, sprays chaos where it snips, and dances on the spot that breaks a mission-critical site.
Pure decoration on the animation clock: the review's result never depends on it, and with animations off the monkey
just sits at the cut. */
const MK_RUN=0.62; // share of a review step the monkey spends running to the next cut
const MK_COLS=['#FF4F9A','#FFD23F','#22C47A','#2F8CFF','#FF8A3D','#7B61FF'];
// Where a link's middle is, in world coordinates (the shorter way round the date line).
const mkMid=l=>[(CITY[l.a].x+CITY[l.a].x+wrapDx(CITY[l.a].x,CITY[l.b].x))/2,(CITY[l.a].y+CITY[l.b].y)/2];
function mkStart(c){const C=CITY[c];UI.mk={x:C.x,y:C.y,fx:C.x,fy:C.y,tx:C.x,ty:C.y,t0:animT,dur:1,face:1,mood:'run',spray:[],sprayAt:null,bad:false};}
// Off to the next cut: arrive, then spray.
function mkRun(l,dur,bad){const m=UI.mk;if(!m)return;const [x,y]=mkMid(l);m.fx=m.x;m.fy=m.y;m.tx=x;m.ty=y;m.t0=animT;m.dur=Math.max(1,dur*MK_RUN);
m.face=wrapDx(m.fx,x)<0?-1:1;m.mood='run';m.sprayAt=animT+m.dur;m.bad=!!bad;if(!animOn()){m.x=x;m.y=y;}}
function mkSpray(m){for(let i=0;i<(m.bad?26:16);i++){const a=Math.PI*2*i/(m.bad?26:16)+((i*37)%10)/10,v=0.05+((i*53)%10)/180;
m.spray.push({x:m.tx,y:m.ty,vx:Math.cos(a)*v,vy:Math.sin(a)*v-0.06,c:m.bad?(i%3?'#FF4D5E':'#FFD23F'):MK_COLS[i%MK_COLS.length],t:animT,s:i%4===0?(m.bad?'💥':'✂️'):null});}}
// The review is over: a cheer when everything held, a tantrum where it broke.
function mkEnd(ok){const m=UI.mk;if(!m)return;m.mood=ok?'cheer':'dance';m.t0=animT;}
function mkStop(){UI.mk=null;}
function mkDraw(ks){const m=UI.mk;if(!m)return;
const p=Math.min(1,(animT-m.t0)/m.dur),e=p<0.5?2*p*p:1-Math.pow(-2*p+2,2)/2;
if(m.mood==='run'){m.x=m.fx+wrapDx(m.fx,m.tx)*e;m.y=m.fy+(m.ty-m.fy)*e;}
if(m.sprayAt!=null&&animT>=m.sprayAt){m.sprayAt=null;if(animOn())mkSpray(m);m.mood=m.bad?'dance':'snip';m.t0=animT;}
const sz=Math.round(36*Math.max(0.85,Math.min(1.4,Math.sqrt(cam.z))));
// Hops while running, bounces on the spot otherwise.
const hop=m.mood==='run'&&p<1?Math.abs(Math.sin((animT-m.t0)/70))*sz*0.45:m.mood==='dance'?Math.abs(Math.sin((animT-m.t0)/90))*sz*0.6:m.mood==='cheer'?Math.abs(Math.sin((animT-m.t0)/120))*sz*0.35:0;
ctx.save();ctx.textAlign='center';ctx.textBaseline='middle';
for(let i=m.spray.length-1;i>=0;i--){const q=m.spray[i],a=(animT-q.t)/900;if(a>1){m.spray.splice(i,1);continue;}
const dt=animT-q.t,wx=q.x+q.vx*dt/cam.z,wy=q.y+(q.vy*dt+0.00012*dt*dt)/cam.z;
for(const k of ks){const [sx,sy]=toS(wx,wy,k);ctx.globalAlpha=1-a;if(q.s){ctx.font=`${Math.round(sz*0.7)}px sans-serif`;ctx.fillText(q.s,sx,sy);}else{ctx.fillStyle=q.c;ctx.beginPath();ctx.arc(sx,sy,Math.max(2,sz*0.13*(1-a*0.5)),0,Math.PI*2);ctx.fill();}}}
ctx.globalAlpha=1;ctx.font=`${sz}px sans-serif`;
for(const k of ks){const [sx,sy]=toS(m.x,m.y,k);ctx.save();ctx.translate(sx,sy-sz*0.7-hop);
const tilt=m.mood==='snip'?Math.sin((animT-m.t0)/60)*0.35:m.mood==='dance'?Math.sin((animT-m.t0)/80)*0.5:0;ctx.rotate(tilt);ctx.scale(m.face,1);
ctx.fillStyle='rgba(30,38,64,.18)';ctx.beginPath();ctx.ellipse(0,sz*0.62+hop,sz*0.38,sz*0.12,0,0,Math.PI*2);ctx.fill();
// Emoji take the fill's alpha, so reset it after the shadow.
ctx.fillStyle='#000';ctx.fillText('🐒',0,0);if(m.mood==='cheer')ctx.fillText('🎉',sz*0.7,-sz*0.5);ctx.restore();}
ctx.restore();}

View file

@ -23,7 +23,7 @@ function mapHidden(){for(const id of ['dashCard','bankCard']){const c=$(id);if(c
a second anyway, and every frame while the land picture is still catching up with a zoom. */
const FRAME=[];
function mapStill(t,now){const sl=UI.sel,dr=UI.drag;
const v=[t,cam.x,cam.y,cam.z,CW,CH,DPR,S,R,S&&S.day,S&&S.links.length,S&&S.offers.length,S&&S.inc.length,sl&&sl.type,sl&&sl.id,UI.hover,UI.snap&&UI.snap.id,dr&&dr.mode,dr&&dr.cx,dr&&dr.cy,FLOATS.length];
const v=[t,cam.x,cam.y,cam.z,CW,CH,DPR,S,R,S&&S.day,S&&S.links.length,S&&S.offers.length,S&&S.inc.length,sl&&sl.type,sl&&sl.id,UI.hover,UI.snap&&UI.snap.id,dr&&dr.mode,dr&&dr.cx,dr&&dr.cy,FLOATS.length,UI.mk&&UI.mk.tx,UI.mk&&UI.mk.mood];
let same=v.length===FRAME.length;for(let i=0;i<v.length;i++)if(FRAME[i]!==v[i]){same=false;FRAME[i]=v[i];}
return same&&now-(UI.drawnAt||0)<1000&&!!(WL.cv&&WL.key===(cam.z*DPR).toFixed(4)&&wlCovers(cam.z));}
function tickErr(e){console.error('[tubes] tick failed',e);if(UI.tickErr)return;UI.tickErr=true;track('tick_error',true);