IPv4 address space: scarce, bought, leased, leased out; IPv6 is free (#150)

A rule (ip4), on in On-call and BGP at 3 a.m., off in the gentler
presets, sandbox and puzzles; scenarios only when they opt in. You start
with a /21 from the registry, sized so the early game never notices
(a gameplay-rules F1 test plays six start cities for 50 days). Business
customers need a block by kind and size; broadband homes take what is
left and the rest go behind CGNAT, which costs a box per home and
annoys some customers. An offer that does not fit cannot be signed and
says why, and a customer that cannot grow at renewal stays its size.

The bank's Address space section buys and sells blocks on a transfer
market with a gentle trend, leases blocks in, and leases spare blocks
out for monthly income, in real CIDR sizes (/24 to /16), at most 256
blocks (what a save keeps). Old saves get what they already use, plus
room, so nothing changes on load. The dashboard's AS box now lists the
blocks you announce.
This commit is contained in:
jvanvik 2026-11-07 01:04:00 +01:00
parent 2cd6b71198
commit eac0604300
34 changed files with 957 additions and 32 deletions

View file

@ -0,0 +1,89 @@
/* ---------- Address space (#150): IPv6 is free for everyone, IPv4 ran out years ago ----------
The network holds IPv4 blocks in real CIDR sizes (/24 to /16): its first allocation from the RIR, blocks bought on
the transfer market and blocks leased in by the month. Owned blocks can be leased out (a broker finds the tenant) or
sold, once two years have passed since they last moved (the RIRs' holding period, which also stops flipping).
Business customers on IP service take a public block each, sized by kind and port (a /29 for a small DIA port, a /23
for a big hosting company); broadband homes take what is left, and the homes that do not fit share addresses behind
CGNAT at the player's ratio. CGNAT costs a little per Gbps it carries (IPv6 goes around the box) and some gamers want
their money back. An offer that needs more than is free, with every home already behind CGNAT, cannot be signed, and a
customer renewing bigger stays at its size when the bigger block is not free (clGrow).
Nothing is stored per customer: what each one needs follows from its kind and size, so growing customers and old saves
need no new fields. On in On-call and BGP at 3 a.m. (rule ip4), off in the gentler presets, sandbox and puzzles;
scenarios only when they opt in (S.scn.rules.ip4). No random draws: the market price is a function of the day.
The first allocation is a /21: one transit's worth of broadband levels off at about 1,300 homes in every start city,
so the first months fit with room for a few customers (gameplay rule F1). */
// IP4_BLK: the most blocks a network holds; buying and leasing stop there, and a save keeps that many.
const IP4_START=21,IP4_MIN=16,IP4_MAX=24,IP4_HOLD=730,IP4_NAT=[8,16,32,64],IP4_NAT0=16,IP4_BLK=256;
// Public addresses per Gbps of busy-hour broadband (a home each), and the part of it CGNAT still carries: the rest is IPv6.
const IP4_BB=10,IP4_V4=0.55,IP4_CGN=2;
// The market: dollars per address, flat for half a year, then +3% a year on a slow four-year swing. Leases are by the
// month (a broker keeps a cut of what yours earn), sales pay 92% of the price after the broker's fee.
const IP4_PX=38,IP4_FREE=180,IP4_YR=0.03,IP4_IN=0.5,IP4_OUT=0.4,IP4_SELL=0.92;
// Of the bill of a home behind CGNAT, the share that comes back as refunds (gamers, mostly): more homes per address, more
// grumbling. Homes squeezed past the ratio (port exhaustion) are worse.
const IP4_ANNOY={8:0.01,16:0.015,32:0.025,64:0.04},IP4_SQZ=0.15;
// Per kind of customer: addresses per Gbps of port, and the smallest block it takes. Circuits, waves and fiber carry no
// addresses of yours; downstream ISPs and universities bring their own (universities sit on legacy /16s); orbit
// terminals sit behind the satellite operator's own CGNAT.
const IP4_BIZ={cgame:[0.5,256],dia:[2,8],fabip:[2,8],game:[2,16],gpu:[0.5,64],host:[4,64],metav:[0.25,256],scrub:[2,8],stream:[1,32]};
const ip4Sz=len=>2**(32-len),ip4LenOk=len=>Number.isInteger(len)&&len>=IP4_MIN&&len<=IP4_MAX;
const ip4Cidr=n=>'/'+(32-Math.ceil(Math.log2(Math.max(1,n)))),ip4N=n=>Math.round(n).toLocaleString();
function ip4On(){return !!S&&!!S.ip4&&rule('ip4')===true&&(S.scn?scnRule('ip4'):S.mode==='normal');}
function ip4Need(b){const k=b&&IP4_BIZ[b.type];if(!k||!(b.bw>0))return 0;return Math.min(4096,2**Math.ceil(Math.log2(Math.max(k[1],b.bw*k[0]))));}
/* Who uses what. T: usable addresses (owned and leased in, without what is leased out); B: business customers; H: homes;
cg: homes behind CGNAT; over: the share of homes that do not fit even at the ratio; free: what a new customer can have
with every home behind CGNAT. */
function ip4Use(s=S){const I=s.ip4,r=I.nat;let own=0,inn=0,out=0,B=0,H=0;
for(const b of I.b){const n=ip4Sz(b.len);if(b.k==='in')inn+=n;else if(b.out)out+=n;else own+=n;}
for(const b of s.biz)B+=ip4Need(b);
for(const c in s.pops){const p=s.pops[c];if(p.bb)H+=p.bb.d*IP4_BB;}
H=Math.ceil(H);const T=own+inn,A=Math.max(0,T-B),min=Math.ceil(H/r);let cg=0,over=0;
if(A<H){if(A>=min)cg=Math.min(H,Math.ceil((H-A)*r/(r-1)));else{cg=H;over=min?1-A/min:0;}}
return {T,own,inn,out,B,H,cg,over,free:T-B-min,r};}
const ip4Fits=o=>!ip4On()||ip4Need(o)<=Math.max(0,ip4Use().free);
// A customer renewing at bw: does the bigger block it needs fit what is free?
const ip4GrowOk=(b,bw)=>!ip4On()||ip4Need({type:b.type,bw})-ip4Need(b)<=Math.max(0,ip4Use().free);
const ip4Full=()=>S.ip4.b.length>=IP4_BLK;
// What you announce, as the AS box shows it: your blocks by prefix length, without the ones leased out to others.
const ip4Pfx=()=>{const c={};for(const b of S.ip4.b)if(!b.out)c[b.len]=(c[b.len]||0)+1;return Object.keys(c).sort((a,b)=>a-b).map(l=>`${c[l]} × /${l}`).join(', ');};
// Would giving this block up (selling, leasing it out, handing it back) still leave room for everyone?
const ip4Spare=b=>ip4Use().free>=ip4Sz(b.len);
const ip4Held=b=>b.k!=='in'&&S.day-b.d<IP4_HOLD;
function ip4Px(d=S.day){if(!rule('market')||d<=IP4_FREE)return IP4_PX;const y=(d-IP4_FREE)/365;return IP4_PX*(1+IP4_YR*y)*(1+0.12*Math.sin(2*Math.PI*y/4));}
// Bigger blocks are a little cheaper per address, as on the real market.
const ip4Cost=len=>Math.round(ip4Sz(len)*ip4Px()*(1-0.015*(IP4_MAX-len))/100)*100;
const ip4Value=len=>Math.round(ip4Cost(len)*IP4_SELL/100)*100;
const ip4InMo=len=>Math.round(ip4Sz(len)*IP4_IN*ip4Px()/IP4_PX),ip4OutMo=len=>Math.round(ip4Sz(len)*IP4_OUT*ip4Px()/IP4_PX);
// The month's IPv4 bill: leases in and out, CGNAT boxes, refunds (eye: what broadband earns a month).
function ip4Bill(eye){const u=ip4Use();let lin=0,lout=0;for(const b of S.ip4.b){if(b.k==='in')lin+=b.mo||0;else if(b.out)lout+=b.mo||0;}
const cgS=u.H?u.cg/u.H:0;return {u,lin,lout,box:u.cg/IP4_BB*IP4_V4*IP4_CGN,refund:eye*(cgS*IP4_ANNOY[u.r]+u.over*IP4_SQZ)};}
// computeMoney: only what is there, so a network with nothing to pay keeps its money rows as they were.
function ip4Money(C,V){if(!ip4On())return;const b=ip4Bill(V.eyeballs||0),c=Math.round(b.lin+b.box+b.refund);if(c>0)C.ip4=c;if(b.lout>0)V.ip4=b.lout;}
/* A new network gets its /21 from the RIR. An older save gets what it already uses registered to it and a quarter more to
grow into (about a month for a big, fast-growing network), rounded up to /24s in real CIDR blocks (a /21 at the
least), so nothing changes on load; those blocks move like any other two years on.
Their ids are fixed (rir0, rir1…), not uid(): a new game draws no id, so every id after it stays as it was. */
function ip4Grant(s){const d=s.day||0,b=[],add=len=>b.push({id:'rir'+b.length,len,k:'rir',d});let need=0;
for(const x of s.biz||[])need+=ip4Need(x);
for(const c in s.pops||{}){const p=s.pops[c];if(p&&p.bb&&p.bb.d>0)need+=p.bb.d*IP4_BB;}
need=Math.max(ip4Sz(IP4_START),Math.ceil(need*1.25/256)*256);
while(need>=ip4Sz(IP4_MIN)&&b.length<200){add(IP4_MIN);need-=ip4Sz(IP4_MIN);}
for(let len=IP4_MIN+1;len<=IP4_MAX;len++)if(need>=ip4Sz(len)){add(len);need-=ip4Sz(len);}
return {b,nat:IP4_NAT0,w:0};}
const ip4New=()=>ip4Grant({});
function ip4Fix(s){const I=s.ip4&&typeof s.ip4==='object'&&!Array.isArray(s.ip4)?s.ip4:null;if(!I){s.ip4=ip4Grant(s);return;}
const day=Math.max(0,num(s.day,0)),mo=v=>Math.round(Math.min(1e8,Math.max(0,num(v,0))));
const b=(Array.isArray(I.b)?I.b:[]).filter(x=>x&&typeof x==='object'&&ip4LenOk(x.len)&&['rir','buy','in'].includes(x.k)).slice(0,IP4_BLK).map(x=>{
const o={id:typeof x.id==='string'&&x.id&&x.id.length<=40?x.id:uid(),len:x.len,k:x.k,d:int(x.d,0,day,0)};
if(x.k==='in')o.mo=mo(x.mo);else if(x.out===true){o.out=true;o.mo=mo(x.mo);}return o;});
s.ip4={b,nat:IP4_NAT.includes(I.nat)?I.nat:IP4_NAT0,w:int(I.w,0,3,0)};}
/* Once a day: say so when homes go behind CGNAT (a percent of them at least), and when even CGNAT cannot fit them all
(w: what was said). Each rearms only with room to spare again, so homes hovering at the limit hear it once. */
function ip4Day(d){if(!ip4On())return;const I=S.ip4,u=ip4Use(),A=u.T-u.B,go=[{l:tr('ip4.section'),act:'ip4Open'}];
if(u.cg>=Math.max(1,u.H*0.01)){if(!(I.w&1)){I.w|=1;toast(tr('ip4.toast_cgnat',{p:pct(u.cg/u.H)}),'',go);}}else if(A>=u.H*1.05)I.w&=~1;
if(u.over>0){if(!(I.w&2)){I.w|=2;toast(tr('ip4.toast_out',{r:u.r}),'bad',go);sfx('err');}}else if(A>=Math.ceil(u.H/u.r)*1.1)I.w&=~2;}
// Renewals that wanted a bigger block than is free (clGrow, [customer, bandwidth]): they stay as they are, and hear why.
function ip4News(l=[]){for(const [b,bw] of l.slice(0,3))toast(tr('ip4.no_grow',{name:esc(b.name),city:esc(clWhere(b)),bw:fmtG(bw)}),'',[{l:tr('ip4.section'),act:'ip4Open'}]);}
// The line under an offer that cannot be signed for want of addresses.
function ip4Why(o){if(ip4Fits(o))return '';const n=ip4Need(o);
return `<div class="why">${tr('ip4.offer_needs',{p:ip4Cidr(n),n:ip4N(n),free:ip4N(Math.max(0,ip4Use().free))})} ${btn(tr('ip4.section'),'ip4Open',{},'ghost sm')}</div>`;}