From 46ba4e04d297b64b936159eb3cc099b195bd2f89 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Wed, 4 Nov 2026 10:20:55 +0100 Subject: [PATCH] Pie in the Sky: a terminal lands where all of it fits pieLand() prefers the nearest dish with room for the whole terminal and only then the nearest with any room left, so a nearly full dish no longer serves a sliver of a customer who then cancels while other dishes sit idle. --- src/game/80-simulation/35-pie-paths.js | 8 ++++---- tests/pie-routing.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/game/80-simulation/35-pie-paths.js b/src/game/80-simulation/35-pie-paths.js index ff45aeb..6a5baf8 100644 --- a/src/game/80-simulation/35-pie-paths.js +++ b/src/game/80-simulation/35-pie-paths.js @@ -32,12 +32,12 @@ const pieSkyMs=(km,alt)=>2*(alt/PIE_C+PIE_HOP)+km*(PIE_RE+alt)/PIE_RE/PIE_C; // May a customer in country ca land on a dish in country dc? (spats) const pieLandOk=(ca,dc)=>!(pieSpatHome(ca)&&dc!==ca)&&!pieAirBlock(ca,dc); /* Where a terminal customer in city c lands: {k (index in ds), cr} or null. ds: dishes with .ok (on the backbone). - The nearest dish with room left that a baked crust over c also covers: within PIE_BENT km (bent pipe, any dish), else with lasers on that + The nearest dish with room left (for all of bw if any has it) that a crust over c also covers: within PIE_BENT km (bent pipe, any dish), else with lasers on that crust any dish on the backbone. Some customers need lasers (flywifi, marsorb: T.las). */ -function pieLand(c,t,ds,hubs){const T=BIZ_T[t],cc=CITY[c].cc;let best=null,bk=Infinity; +function pieLand(c,t,ds,hubs,bw=0){const T=BIZ_T[t],cc=CITY[c].cc;let best=null,bk=Infinity; for(const id of hubs){if(!pieCovers(id,c))continue;const las=pieLased(id);if(T.las&&!las)continue; for(let k=0;k0&&(km<=PIE_BENT||(las&&d.ok))){const v=km+(d.ok?0:1);if(v0&&(km<=PIE_BENT||(las&&d.ok))){const v=km+(d.ok?0:1)+(d.leftmn||(n===mn&&k===hk)){main=k;mn=n;}X.main=main; for(const d of ds){const ci=CIDX[d.city];d.ok=COMP[ci]>=0&&COMP[ci]===main;d.left=d.g;if(!d.ok)X.isl.push(d.city);} // Terminals first: each takes what its dish and its crust have left, in S.biz order. - if(hubs.length)for(const b of S.biz){const T=BIZ_T[b.type];if(!T.orbit)continue;const L=pieLand(b.city,b.type,ds,hubs);if(!L)continue;const d=ds[L.k]; + if(hubs.length)for(const b of S.biz){const T=BIZ_T[b.type];if(!T.orbit)continue;const L=pieLand(b.city,b.type,ds,hubs,b.bw);if(!L)continue;const d=ds[L.k]; const a=Math.max(0,Math.min(b.bw,d.left,X.cr[L.cr]));d.left-=a;X.cr[L.cr]-=a;X.term[b.id]=a;X.gs[b.id]=d.city;X.gsBw[d.city]=(X.gsBw[d.city]||0)+a;} // Backhaul for cut-off POPs, after terminals and before sky circuits (see the top). if(X.isl.length)for(const d of ds){if(d.ok)continue;const ci=CIDX[d.city];if(!adj[ci]||!pops.includes(d.city))continue;const L=pieBhLand(d,ds,hubs,X.cr); diff --git a/tests/pie-routing.test.js b/tests/pie-routing.test.js index 3f240ae..2e84f38 100644 --- a/tests/pie-routing.test.js +++ b/tests/pie-routing.test.js @@ -197,3 +197,15 @@ test('orbit customers sit out a bad month: at most PIE_PATIENCE happiness a day, assert.equal(ev("S.biz.some(b=>b.id==='pz')"), false, 'a month and more of nothing: cancelled'); assert.deepEqual(errors, []); }); + +test('a terminal lands on the nearest dish with room for all of it, not a sliver of a full one', () => { + const { ev, errors } = load(); + world(ev); + ev("S.pie.dishes.find(d=>d.city==='lon').ant=1;compute()"); + term(ev, 'a', 'dub', 'pizza', 15); + assert.equal(ev("R.pie.gs.a"), 'lon', 'the nearest dish first'); + term(ev, 'b', 'dub', 'pizza', 50); + assert.notEqual(ev("R.pie.gs.b"), 'lon', 'London has 5 G left: the next dish takes all 50'); + assert.ok(ev('R.pie.sf.b') > 0.95, `served in full (${ev('R.pie.sf.b')})`); + assert.deepEqual(errors, []); +});