Keep hand-listed alien waves on the market (#340)

With the capacity market selling alien waves (or pairs), its daily pass set
every fiber's listing to what the thresholds allowed: 128 waves listed by hand
came back as 96 (after the 25% kept back), or were taken off entirely when
the route's demand was below the minimum or the price below the floor.

A listing now remembers how much was listed by hand (l.list.hw / hp). The
broker lists its own share on top, capped at what the fiber has free; sales
use up the broker's share first; switching a product or route off keeps the
hand-listed part. cmClean() checks the new fields, so old saves load as before.

Fixes #340
This commit is contained in:
Frederic Jung 2026-11-11 12:06:23 +01:00
parent c137d3552d
commit e27e9e3066
No known key found for this signature in database
7 changed files with 87 additions and 10 deletions

View file

@ -21,10 +21,15 @@ const CM_HMAX=2.5;
const cmNew=()=>({hold:25,p:JSON.parse(JSON.stringify(CM_DEF)),off:{},sh:[],news:[],seen:[]});
const cmCut=()=>broker()?0.97:0.9;
/* A fiber's listing, l.list: {w: spectrum slots, p: pairs} as it always was, plus m1/m4/m8/m16 (managed 100G to 1.6T
waves) when any are listed. cmClean() checks one (a save, or after a day of sales) and returns null when nothing is left. */
waves) when any are listed, and hw/hp: how many of w/p you listed by hand, which the broker's thresholds never take
back (#340). cmClean() checks one (a save, or after a day of sales) and returns null when nothing is left. */
const cmLs=l=>l.list||(l.list={w:0,p:0});
const CM_HAND={w:'hw',p:'hp'};
function cmClean(L){if(!L||typeof L!=='object'||Array.isArray(L))return null;const x={w:int(L.w,0,256,0),p:int(L.p,0,2,0)};let any=x.w||x.p;
for(const k in CM_HAND){const v=Math.min(x[k],int(L[CM_HAND[k]],0,256,0));if(v)x[CM_HAND[k]]=v;}
for(const p of CM_W){const v=int(L[CM_L[p]],0,256,0);if(v){x[CM_L[p]]=v;any=1;}}return any?x:null;}
// What you listed by hand of a product on a fiber (only alien waves and pairs can be).
const cmHand=(l,lk)=>(CM_HAND[lk]&&l.list&&l.list[CM_HAND[lk]])||0;
// A stable number in [0,1) for a route and a question, different in every game.
const cmU=(k,q)=>hstr(k+'|'+q+'|'+((S&&S.asn)||0))%100000/100000;
/* How hot a route is (1 = its usual self). Its own slow cycle (two waves of 4 to 10 and 1.5 to 3 months) is what a
@ -130,7 +135,8 @@ function cmShocks(d){const C=S.cm;C.sh=C.sh.filter(x=>x.until>d);
while demand is at least your minimum and the price at least your floor) or takes it off, then each listing may
find a buyer: the more room the route has left and the hotter it is, the sooner; the fastest fiber sells first. */
function cmList1(r,p,s,d){const c=S.cm.p[p],lk=CM_L[p];if(!c.on||S.cm.off[r.k])return;const ok=cmDem(r,p,d)>=c.min&&cmIdx(r,p,s,0,d)*100>=c.floor;
for(const l of r.ls){const n=ok?cmSpare(l,p):0;if(n>0)cmLs(l)[lk]=n;else if(l.list)l.list[lk]=0;}}
// What you listed by hand stays listed on top of what the thresholds allow, up to what the fiber has free.
for(const l of r.ls){const h=cmHand(l,lk),n=Math.min(ok?cmSpare(l,p)+h:h,h?(p==='spec'?freeCh(l):1):Infinity);if(n>0)cmLs(l)[lk]=n;else if(l.list)l.list[lk]=0;}}
// The listings as the thresholds want them right now (after you change a setting), without selling anything.
function cmRelist(){const rs=cmRoutes(),sold=cmSold(rs);for(const r of rs.values())for(const p of CM_P)cmList1(r,p,sold.get(r.k)[p],S.day);cmTidy();}
const cmTidy=()=>{for(const l of S.links)if(l.list){const L=cmClean(l.list);if(L)l.list=L;else delete l.list;}};
@ -146,7 +152,9 @@ function cmDay(d=S.day){const C=S.cm;cmShocks(d);const rs=cmRoutes(),sold=cmSold
while(n>0&&cmIdx(r,p,s[p],n-1,d)*100<C.p[p].floor)n--;if(!n)continue;
const cut=cmCut()*mkt(),L=cmList(r,p);let rate=0;for(let i=0;i<n;i++)rate+=L*cmIdx(r,p,s[p],i,d);
const up=p==='iru'?Math.round(cmUpList(r)*cmIdx(r,p,s[p],0,d)*cut):0;
cmSell(l,p,n,Math.round(rate*cut),up);s[p]+=n;l.list[lk]-=n;}}
cmSell(l,p,n,Math.round(rate*cut),up);s[p]+=n;l.list[lk]-=n;
// Sales take from the broker's share first: what you listed by hand goes only once that is sold.
if(cmHand(l,lk)>l.list[lk])l.list[CM_HAND[lk]]=l.list[lk];}}
cmGrow(d);cmTidy();}
/* What the next 30 and 90 days look like on a route, for a Cable broker: the route's own cycle and the news already
out, but not the noise or news still to come. */

View file

@ -1,7 +1,7 @@
Object.assign(ACT,{
// List spare capacity with a capacity broker (#203): w waves and p spare pairs, sold as the route's market wants.
listFiber:({id,w,p})=>{const l=S.links.find(x=>x.id===id);if(!l||l.kind!=='df'||l.ready>S.day)return;w=Math.max(0,Math.min(freeCh(l)-((l.list&&l.list.w)||0),w|0));p=l.own?Math.max(0,Math.min((pairFree(l)?1:0)-((l.list&&l.list.p)||0),p|0)):0;
if(!w&&!p){toast(tr('customers.nothing_spare_to_sell_on_this'),'bad');sfx('err');return;}const L=l.list||(l.list={w:0,p:0});L.w+=w;L.p+=p;
if(!w&&!p){toast(tr('customers.nothing_spare_to_sell_on_this'),'bad');sfx('err');return;}const L=l.list||(l.list={w:0,p:0});L.w+=w;L.p+=p;if(w)L.hw=(L.hw||0)+w;if(p)L.hp=(L.hp||0)+p;
toast(tr('broker.listed',{a:CITY[l.a].name,b:CITY[l.b].name}),'good');sfx('click');after();},
unlistFiber:({id})=>{const l=S.links.find(x=>x.id===id);if(!l||!l.list)return;delete l.list;sfx('click');after();},
accept:({id,auto=false})=>{const o=S.offers.find(x=>x.id===id);if(!o)return;const customer=auto?autoOffer(o):offerCustomer(o);if(!customer)return;if(!bHas(o)){toast(tr('customers.you_need_a_pop_in_every'),'bad');return;}

View file

@ -2,13 +2,14 @@
Object.assign(ACT,{
marketOpen:()=>{if(!S||pzOn())return;UI.dtab='market';$('dash').hidden=false;renderDash();},
mktHold:({v})=>{if(!CM_HOLD.includes(v))return;S.cm.hold=v;cmRelist();sfx('click');after();},
// Switching a product off takes what the broker listed of it off the market; what it already sold stays sold.
// Switching a product off takes what the broker listed of it off the market (what you listed by hand stays); what it
// already sold stays sold.
mktCfg:({p,k,v})=>{const c=CM_P.includes(p)&&S.cm.p[p];if(!c)return;
if(k==='on'){c.on=!!v;if(!c.on)for(const l of S.links)if(l.list&&l.list[CM_L[p]])l.list[CM_L[p]]=0;}
if(k==='on'){c.on=!!v;if(!c.on)for(const l of S.links)if(l.list&&l.list[CM_L[p]])l.list[CM_L[p]]=cmHand(l,CM_L[p]);}
else if(k==='min'&&CM_MIN[p].includes(v))c.min=v;else if(k==='floor'&&CM_FLOOR.includes(v))c.floor=v;else return;
cmRelist();sfx('click');after();},
// A route the broker leaves alone keeps nothing listed by the thresholds.
// A route the broker leaves alone keeps nothing listed by the thresholds, only what you listed by hand.
mktRoute:({k})=>{const r=typeof k==='string'&&cmRoutes().get(k);if(!r)return;if(S.cm.off[k]){delete S.cm.off[k];}else{S.cm.off[k]=1;
for(const l of r.ls)if(l.list)for(const p of CM_P)if(S.cm.p[p].on)l.list[CM_L[p]]=0;}
for(const l of r.ls)if(l.list)for(const p of CM_P)if(S.cm.p[p].on)l.list[CM_L[p]]=cmHand(l,CM_L[p]);}
cmRelist();sfx('click');after();},
});