Pie in the Sky: rocket company foundation

Catalog (rockets, crusts, prices), state with newGame default and a full
sanitize repair, the one-time unlock on the Tier 1 win (sandbox from day 0),
company, factory, pads, R&D, launches, dishes, landing rights, the daily tick,
the Founder feed, three badges, a plain Pie Control modal from the NOC tab,
and hooks for routing, spats and the globe. Translated in every language.
This commit is contained in:
Frederic Jung 2026-11-01 16:31:09 +01:00
parent d2c7294ed3
commit 7fb7be305d
No known key found for this signature in database
17 changed files with 1019 additions and 4 deletions

View file

@ -0,0 +1,127 @@
/* ---------- Pie in the Sky: your own satellite constellation (docs/plans/pie-in-the-sky.md) ----------
Unlocks once, on becoming Tier 1 in a save started on this version (S.pieV>=1), or from day 0 with open gates.
Pies are counted in cohorts per crust, never one object per satellite. Saves without S.pie pay nothing:
pieDay() returns at once, compute() adds one falsy check.
Hooks for the parts that come later (routing, incidents and spats, the globe):
- pieDishes() -> [{id,city,cc,ant,g}]: dishes that work today (POP there, not down, landing rights held)
- pieHubs() -> ['short',…]: crusts that are baked (>= 60%) and up (not in safe mode, not down)
- pieLicOk(cc) -> true when landing rights in that country are held and in force
- pieCov(id), pieBaked(id), pieUp(id), pieLive(id), pieLiveLas(id) (10-catalog/70-pie.js and below)
- PIE_ON.day / .launch / .fail: arrays of f(d) / f(L,ok) / f(L), called by pieDay() and pieFly()
- S.pie.spat[id] with PIE_SPATS[id]={fix(o,s),day?(o,d)}: a spat's state, repaired by pieSanitize(), ticked and
dropped when o.until<=d by pieSpatDay(d)
- S.pie.crusts[id].down: day a crust is out until (a solar storm); S.pie.dishes[i].down likewise (a backhoe)
- pieGlobeData() -> {crusts:[{id,alt,inc,n,las}],dishes:[{city,lat,lon}],pads:[{city,lat,lon,next}],
launches:[{id,rk,cr,n,day,pad}]}
- pieFounder(k,{n,c}) -> a Founder post (k from FOUNDER_POSTS); pieCharge(v) bills a cost (book kept)
- book.earned: revenue from orbit, for the customers that come with routing. */
function pieBlank(day){const crusts={};for(const id in CRUSTS)crusts[id]={cohorts:[],queued:0};
return {born:day,co:null,fac:false,stock:0,rd:{reuse:false,heavy:false,heavyTests:0},pads:[],crusts,dishes:[],lic:{},q:[],
founder:{posts:[],promises:0,broken:0,due:[]},book:{spent:0,earned:0,wouldHave:0},spat:{},safe:false};}
function pieInit(){if(!S.pie)S.pie=pieBlank(S.day);return S.pie;}
// The unlock: only on the Tier 1 transition of a save of this version, and only in a normal game.
function pieUnlock(){if(S.pie||!(S.pieV>=1)||S.mode!=='normal'||S.scn)return;pieInit();track('pie_unlock');
toast(`<b>🥧 ${tr('pie.unlocked_title')}</b><br>${tr('pie.unlocked_body')}`,'goal',[{l:tr('pie.open_pie_control'),act:'pieOpen',args:{}}]);}
// Sandbox: there from day 0, created on the first action.
const pieGet=()=>pieOn()?S.pie||pieInit():null;
const pieSand=()=>S.mode==='sandbox';
const pieCoName=()=>{const n=S.pie&&S.pie.co&&S.pie.co.name;return !n?tr(PIE_NAMES[0]):PIE_NAMES.includes(n)?tr(n):n;};
// What it costs: sandbox pays nothing real but keeps count of what it would have paid.
function pieSpend(v){const P=S.pie;if(pieSand()){P.book.wouldHave+=v;sfx('build');return true;}if(!spend(v))return false;P.book.spent+=v;return true;}
function pieCharge(v){const P=S.pie;if(!P||!(v>0))return;if(pieSand())P.book.wouldHave+=v;else{S.cash-=v;P.book.spent+=v;}}
function pieMo(){const P=S.pie;if(!P||!P.co)return 0;return PIE_CO_MO+(P.fac?PIE_FAC_MO:0)+P.pads.length*PIE_PAD_MO+P.dishes.length*PIE_DISH_MO;}
const pieLiveLas=id=>{const c=S.pie&&S.pie.crusts[id];let n=0;if(c)for(const k of c.cohorts)if(k.las&&S.day-k.day<CRUSTS[id].life)n+=k.n;return n;};
const pieLicOk=cc=>{const l=S.pie&&S.pie.lic[cc];return !!l&&l.state==='ok'&&l.until>S.day;};
function pieDishes(){const P=S&&S.pie;if(!P)return [];const o=[];for(const x of P.dishes){if(!S.pops[x.city]||x.down>S.day)continue;const cc=CITY[x.city].cc;if(pieLicOk(cc))o.push({id:x.id,city:x.city,cc,ant:x.ant,g:x.ant*PIE_ANT_G});}return o;}
function pieHubs(){return S&&S.pie?Object.keys(CRUSTS).filter(pieUp):[];}
function pieGlobeData(){const P=S&&S.pie;if(!P)return {crusts:[],dishes:[],pads:[],launches:[]};const ll=c=>({city:c,lat:CITY[c].lat,lon:CITY[c].lon});
return {crusts:Object.keys(CRUSTS).map(id=>({id,alt:CRUSTS[id].alt,inc:CRUSTS[id].inc,n:pieLive(id),las:pieLiveLas(id)})),
dishes:P.dishes.map(x=>ll(x.city)),pads:P.pads.map(p=>({...ll(p.city),next:p.next})),launches:P.q.map(L=>({id:L.id,rk:L.rk,cr:L.cr,n:L.n,day:L.day,pad:L.pad}))};}
// Spats (geopolitics) register in PIE_SPATS and keep their state in S.pie.spat; this ticks and ends them.
function pieSpatDay(d){const sp=S.pie.spat;for(const k of Object.keys(sp)){const o=sp[k],D=PIE_SPATS[k];if(!D||o.until<=d){delete sp[k];continue;}if(D.day)D.day(o,d);}}
const pieYear=d=>1+Math.floor(d/360);
function pieFounder(k,v={}){const F=S.pie.founder,p={d:S.day,k};if(v.n!==undefined)p.n=v.n;if(v.c)p.c=v.c;F.posts.unshift(p);if(F.posts.length>PIE_POSTS)F.posts.length=PIE_POSTS;}
// Every promise slips by a year: made today, due in a year, broken then.
function piePromise(){const F=S.pie.founder;F.promises++;const due=S.day+360;F.due.push(due);if(F.due.length>8)F.due.shift();pieFounder(N_('pie.fp_promise'),{n:pieYear(due)});}
function pieFpText(p){const C=own(CRUSTS,p.c||'')?tr(CRUSTS[p.c].n):'',n=p.n||0;return tr(p.k,{co:pieCoName(),n,crust:C});}
// Pads that can reach a crust: an orbit's inclination is at least the pad's latitude.
const piePadOk=(c,cr)=>CRUSTS[cr].polar||Math.abs(CITY[c].lat)<=CRUSTS[cr].inc;
const pieRkOk=rk=>{const R0=ROCKETS[rk];return !R0.rd||!!S.pie.rd[R0.rd];};
const pieTests=()=>S.pie.rd.heavyTests+S.pie.q.filter(L=>L.test).length;
// A quote for a launch: why not, or what it costs, when and from where. The panel and the action both use it.
function pieQuote(rk,cr,o={}){const P=S.pie;if(!P||!own(ROCKETS,rk)||!own(CRUSTS,cr))return {why:tr('pie.why_pick')};const R0=ROCKETS[rk],C=CRUSTS[cr];
if(!pieCanBuild())return {why:tr('pie.why_not_t1')};
if(!P.co)return {why:tr('pie.why_no_company')};if(!P.fac)return {why:tr('pie.why_no_factory')};
if(!pieRkOk(rk))return {why:tr('pie.why_no_rd',{rd:tr(PIE_RD[R0.rd].n)})};
const pads=P.pads.filter(p=>piePadOk(p.city,cr));if(!P.pads.length)return {why:tr('pie.why_no_pad')};if(!pads.length)return {why:tr('pie.why_pad_lat',{inc:C.inc})};
if(P.q.length>=PIE_Q_MAX)return {why:tr('pie.why_queue_full')};
const test=rk==='heavy'&&pieTests()<PIE_HEAVY_TESTS,n=test?0:R0.pies;
if(!test&&P.stock<n)return {why:tr('pie.why_stock',{n,have:Math.floor(P.stock)})};
if(!test&&pieLive(cr)+P.crusts[cr].queued+n>C.full*PIE_SPARE)return {why:tr('pie.why_full')};
const t0=S.day+leadDays(PIE_PREP),pad=pads.slice().sort((a,b)=>Math.max(t0,a.next)-Math.max(t0,b.next)||Math.abs(CITY[a.city].lat)-Math.abs(CITY[b.city].lat))[0];
const rocket=piePrice(R0.cost)*(1-pieEq(pad.city)),boost=rk==='again'&&!pad.boosters.length?piePrice(PIE_BOOST):0,pies=n*piePrice(PIE_PIE+(o.las?PIE_LAS:0));
const ins=o.ins&&!test?Math.round((rocket+pies)*PIE_INS):0;
return {ok:true,test,n,pad:pad.city,day:Math.max(t0,pad.next),cost:Math.round(rocket+boost+pies+ins),insured:Math.round(rocket+pies),ins:!!ins};}
// The cheapest a crust can be filled from nothing (one-off costs only): rule O7.
function pieFullCost(cr){const C=CRUSTS[cr],base=PIE_CO+PIE_FAC+PIE_PAD,pies=C.full*PIE_PIE;
return base+pies+Math.min(...Object.keys(ROCKETS).map(rk=>{const R0=ROCKETS[rk],n=Math.ceil(C.full/R0.pies);return (R0.rd?PIE_RD[R0.rd].cost:0)+(n+(rk==='heavy'?PIE_HEAVY_TESTS:0))*R0.cost;}));}
function pieCohort(cr,n,las){const c=S.pie.crusts[cr],m=Math.floor(S.day/30),k=c.cohorts.find(x=>x.las===las&&Math.floor(x.day/30)===m);if(k)k.n+=n;else c.cohorts.push({n,day:S.day,las});
if(c.cohorts.length>PIE_COHORTS)c.cohorts.splice(0,c.cohorts.length-PIE_COHORTS);}
// A launch: every one costs a party; a test always ends in a fireball; the rest fail by the rocket's odds and the chaos rule,
// never in the first 90 days after the unlock.
function pieFly(L){const P=S.pie,R0=ROCKETS[L.rk],C=P.crusts[L.cr],pad=P.pads.find(p=>p.city===L.pad);C.queued=Math.max(0,C.queued-L.n);
pieCharge(PIE_PARTY);S.stats.pieLaunch=(S.stats.pieLaunch||0)+1;
const quiet=S.day-P.born<PIE_QUIET,cm=quiet?0:chaosMul();let b=null;
if(L.rk==='again'&&pad){if(!pad.boosters.length){pad.boosters.push({id:uid(),flights:0});}b=pad.boosters.slice().sort((x,y)=>x.flights-y.flights)[0];}
let ok;
if(L.test){ok=false;P.rd.heavyTests=Math.min(PIE_HEAVY_TESTS,P.rd.heavyTests+1);pieFounder(N_('pie.fp_test'),{n:P.rd.heavyTests});
toast(tr('pie.t_test',{n:P.rd.heavyTests,left:PIE_HEAVY_TESTS-P.rd.heavyTests}),'bad');}
else{ok=!(cm>0&&Math.random()<R0.fail*cm);
if(ok){pieCohort(L.cr,L.n,!!L.las);pieFounder(N_('pie.fp_launch'),{n:L.n,c:L.cr});toast(tr('pie.t_launch',{n:L.n,crust:tr(CRUSTS[L.cr].n),p:pct(pieCov(L.cr))}),'good');}
else{S.stats.pieFail=(S.stats.pieFail||0)+1;if(L.ins)pieRefund(L.cost);pieFounder(N_('pie.fp_fail'));
toast(tr('pie.t_fail',{n:L.n})+(L.ins?' '+tr('pie.t_insured',{amt:fmt$(L.cost)}):''),'bad');for(const f of PIE_ON.fail)f(L);}}
if(b&&pad){if(ok&&!(cm>0&&Math.random()<(R0.land||0)*cm)){b.flights++;if(b.flights>=PIE_BOOST_MAX)pad.boosters=pad.boosters.filter(x=>x!==b);}
else if(!L.test){pad.boosters=pad.boosters.filter(x=>x!==b);if(ok){pieFounder(N_('pie.fp_booster'));toast(tr('pie.t_booster'),'bad');}}}
for(const f of PIE_ON.launch)f(L,ok);}
function pieRefund(v){const P=S.pie;if(pieSand())P.book.wouldHave=Math.max(0,P.book.wouldHave-v);else{S.cash+=v;P.book.spent=Math.max(0,P.book.spent-v);}}
function pieDay(d){const P=S.pie;if(!P)return;
if(P.fac)P.stock=Math.min(PIE_STOCK_MAX,P.stock+PIE_FAC_MONTH/30);
if(P.q.length){const due=P.q.filter(L=>L.day<=d);if(due.length){P.q=P.q.filter(L=>L.day>d);for(const L of due)pieFly(L);}}
for(const id in CRUSTS){const c=P.crusts[id],life=CRUSTS[id].life;if(c.cohorts.length&&d-c.cohorts[0].day>=life)c.cohorts=c.cohorts.filter(k=>d-k.day<life);
if(!c.bk&&pieBaked(id)){c.bk=1;pieFounder(N_('pie.fp_baked'),{c:id});toast(tr('pie.t_baked',{crust:tr(CRUSTS[id].n)}),'goal');piePromise();}}
{const F=P.founder;if(F.due.length&&F.due[0]<=d){F.due.shift();F.broken++;pieFounder(N_('pie.fp_slip'),{n:pieYear(d+360)});}}
// Running costs are billed with the rest each day (compute()); the book keeps the tally, and the sandbox its shame.
{const mo=pieMo()/30;if(mo){if(pieSand())P.book.wouldHave+=mo;else P.book.spent+=mo;}}
pieSpatDay(d);for(const f of PIE_ON.day)f(d);}
// Badge: lost Tier 1 while a launch was still on the pad.
function pieTier(){const P=S.pie,t1=pieT1();if(P.t1&&!t1&&P.q.length&&!S.stats.pieTour){S.stats.pieTour=1;pieFounder(N_('pie.fp_tour'));}P.t1=t1;}
/* The save gate for S.pie: everything clamped, unknown crusts, rockets and cities dropped, pads and dishes only where a
POP is. A save without pieV (older versions) never unlocks it; puzzles, scenarios and the Architect never have it. */
function pieSanitize(s){s.pieV=int(s.pieV,0,1,0);const p=s.pie,ob=x=>x&&typeof x==='object'&&!Array.isArray(x);
if(!ob(p)||(s.pieV<1&&s.mode!=='sandbox')||s.scn||s.mode==='puzzle'){s.pie=null;return;}
const o=pieBlank(int(p.born,0,s.day,s.day)),day=s.day,n=(v,hi)=>int(v,0,hi,0),m=(v,hi)=>clamp(num(v,0),0,hi);
if(ob(p.co)){const nm=p.co.name;o.co={name:typeof nm==='string'&&nm.trim()?(PIE_NAMES.includes(nm)?nm:nm.replace(/\s+/g,' ').trim().slice(0,40)):PIE_NAMES[0]};}
o.fac=!!p.fac&&!!o.co;o.stock=o.fac?m(p.stock,PIE_STOCK_MAX):0;o.safe=!!p.safe;o.t1=!!p.t1;
if(ob(p.rd)){o.rd.reuse=!!p.rd.reuse;o.rd.heavy=!!p.rd.heavy;o.rd.heavyTests=o.rd.heavy?n(p.rd.heavyTests,PIE_HEAVY_TESTS):0;}
const seen=new Set();
o.pads=(Array.isArray(p.pads)?p.pads:[]).filter(x=>ob(x)&&isCity(x.city)&&s.pops[x.city]&&pieCoast(x.city)&&!seen.has(x.city)&&seen.add(x.city)).slice(0,PIE_PAD_MAX)
.map(x=>({city:x.city,next:int(x.next,0,day+3650,day),boosters:(Array.isArray(x.boosters)?x.boosters:[]).filter(ob).slice(0,PIE_BOOSTERS).map(b=>({id:sid0(b.id),flights:n(b.flights,PIE_BOOST_MAX-1)}))}));
if(ob(p.crusts))for(const id in CRUSTS){const c=p.crusts[id];if(!ob(c))continue;const C=CRUSTS[id];
o.crusts[id].cohorts=(Array.isArray(c.cohorts)?c.cohorts:[]).filter(k=>ob(k)&&num(k.n,0)>=1).map(k=>({n:n(k.n,C.full*2),day:int(k.day,0,day,day),las:!!k.las})).filter(k=>day-k.day<C.life).slice(-PIE_COHORTS);
if(c.down!==undefined&&num(c.down,0)>day)o.crusts[id].down=int(c.down,day,day+365,day);if(c.bk)o.crusts[id].bk=1;}
const ds=new Set();
o.dishes=(Array.isArray(p.dishes)?p.dishes:[]).filter(x=>ob(x)&&isCity(x.city)&&s.pops[x.city]&&!ds.has(x.city)&&ds.add(x.city)).slice(0,PIE_DISH_MAX)
.map(x=>({id:sid0(x.id),city:x.city,ant:int(x.ant,1,PIE_ANT_MAX,1),down:int(x.down,0,day+365,0)}));
const ccs=new Set(CITIES.map(c=>c.cc));
if(ob(p.lic))for(const cc of Object.keys(p.lic)){const l=p.lic[cc];if(!ccs.has(cc)||!ob(l))continue;o.lic[cc]={state:PIE_LIC_STATES.includes(l.state)?l.state:'ok',until:int(l.until,0,day+PIE_LIC_DAYS*10,0)};}
o.q=(Array.isArray(p.q)?p.q:[]).filter(L=>ob(L)&&own(ROCKETS,L.rk)&&own(CRUSTS,L.cr)&&o.pads.some(x=>x.city===L.pad)&&(!ROCKETS[L.rk].rd||o.rd[ROCKETS[L.rk].rd])).slice(0,PIE_Q_MAX)
.map(L=>{const t=L.rk==='heavy'&&!!L.test;return {id:sid0(L.id),rk:L.rk,cr:L.cr,pad:L.pad,n:t?0:n(L.n,ROCKETS[L.rk].pies),day:int(L.day,0,day+3650,day),cost:m(L.cost,1e10),ins:!!L.ins,las:!!L.las,test:t};});
for(const L of o.q)o.crusts[L.cr].queued+=L.n;
if(ob(p.founder)){const F=p.founder;o.founder.promises=n(F.promises,1e6);o.founder.broken=n(F.broken,o.founder.promises);
o.founder.posts=(Array.isArray(F.posts)?F.posts:[]).filter(x=>ob(x)&&FOUNDER_POSTS.includes(x.k)).slice(0,PIE_POSTS).map(x=>{const q={d:int(x.d,0,day,day),k:x.k};if(x.n!==undefined)q.n=n(x.n,1e7);if(own(CRUSTS,x.c||''))q.c=x.c;return q;});
o.founder.due=(Array.isArray(F.due)?F.due:[]).map(v=>int(v,0,day+720,day)).sort((a,b)=>a-b).slice(-8);}
if(ob(p.book))for(const k of ['spent','earned','wouldHave'])o.book[k]=m(p.book[k],1e15);
if(ob(p.spat))for(const k of Object.keys(p.spat)){const D=PIE_SPATS[k];if(!D||!ob(p.spat[k]))continue;const v=D.fix?D.fix(p.spat[k],s):null;if(v)o.spat[k]=v;}
s.pie=o;}
const sid0=v=>typeof v==='string'&&v&&v.length<=40?v:uid();