From d971ded259e2e6a7ee7886d11bcfe5a46998eaab Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Sun, 8 Nov 2026 17:57:27 +0000 Subject: [PATCH] Hold the lay ship through a construction delay instead of restarting the lay (#322) A delay that hit your own subsea cable while it was being laid moved the whole laying stretch later: the ship vanished, the laid km went back to zero and the survey and factory could be hurried and paid for again. The delay now holds the ship where it is (l.lay: start, share laid, resume day), so laying picks up after the delay. Old saves without the field lay as before; sanitize() repairs or drops it. --- CHANGELOG.md | 1 + src/game/10-catalog/60-reach-and-subsea.js | 2 +- src/game/80-simulation/50-sanitize.js | 5 +- src/game/85-incidents/50-cable-ships.js | 8 ++- src/game/88-days/10-day-tick.js | 2 +- tests/lay-ship-delay.test.js | 75 ++++++++++++++++++++++ 6 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/lay-ship-delay.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d0fb62a..36a135e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] +- A delay while your own subsea cable is being laid now holds the lay ship where it is, instead of sending it back to port and starting the laying over. The cable already on the seabed stays there, the build simply finishes later, and there is nothing to pay twice (#322). - While nothing moves on the map but its animations (packets, waves, clouds), it is drawn 30 times a second instead of at the screen's full rate, half the work on most screens and a quarter on 120 Hz phones. Moving the map, pointing at something or anything changing on the network still gets every frame, and the animations keep their speed. - A paused game no longer rewrites its save every 5 seconds when nothing has changed. On a big network each of those writes held the page for 20 to 30 milliseconds. - At ×8 a big network's autosave no longer holds the page: the game is packed, turned into text and compressed in the background, and less of each day's routing has to travel back to the page. The game plays exactly as before. diff --git a/src/game/10-catalog/60-reach-and-subsea.js b/src/game/10-catalog/60-reach-and-subsea.js index 8d63b91..a00410d 100644 --- a/src/game/10-catalog/60-reach-and-subsea.js +++ b/src/game/10-catalog/60-reach-and-subsea.js @@ -15,7 +15,7 @@ const subUpgradeReady=(l,k)=>l.kind==='df'&&l.sub&&l.own&&Number.isFinite(l['sub const subUpgradeCost=(l,k)=>SUB_UPGRADES[k].cost+(k==='engineering'?Math.round(l.km*1000):0); // Pending transmission work follows the construction schedule. Keep the actual // shift (the last crew purchase can save fewer than 30 days) and completed work. -function setCableReady(l,ready,day=S.day){const shift=ready-l.ready; +function setCableReady(l,ready,day=S.day){const shift=ready-l.ready;layHold(l,shift,day); if(l.kind==='df'&&l.own&&l.sub)for(const k of Object.keys(SUB_UPGRADES)){ const f='sub'+k;if(Number.isFinite(l[f])&&l[f]>day)l[f]=Math.max(day+1,l[f]+shift); }l.ready=ready; diff --git a/src/game/80-simulation/50-sanitize.js b/src/game/80-simulation/50-sanitize.js index fff9a44..7e33b48 100644 --- a/src/game/80-simulation/50-sanitize.js +++ b/src/game/80-simulation/50-sanitize.js @@ -76,7 +76,10 @@ function sanitize(s){ for(const i of s.inc){if(i.type!=='cut'||i.st==null){for(const k of ['ship','seaq','st','sw','sp','sl'])delete i[k];continue;} i.st=clamp(num(i.st,0.5),0.03,0.97);i.sw=int(i.sw,1,30,4);i.sp=int(i.sp,0,60,0);i.sl=typeof i.sl==='string'?i.sl.slice(0,40):'';i.rushed=!!i.rushed||undefined; if(i.ship!=null&&!(own(SHIP,i.ship)&&o[i.ship]&&o[i.ship].inc===i.id))i.ship=null;if(i.ship==null){i.ship=null;i.seaq=1;}else delete i.seaq;} - for(const l of s.links)if(l.ship2&&l.kind==='df'&&l.own&&l.sub)l.ship2=1;else delete l.ship2;} + for(const l of s.links)if(l.ship2&&l.kind==='df'&&l.own&&l.sub)l.ship2=1;else delete l.ship2; + // A lay ship held by a delay (#322): only on your own subsea cable still being laid. + for(const l of s.links){const v=l.lay;if(v===undefined)continue;if(!(v&&typeof v==='object'&&!Array.isArray(v)&&l.kind==='df'&&l.own&&l.sub&&l.ready>s.day)){delete l.lay;continue;} + const t=Math.round(clamp(num(v.t,s.day),0,l.ready));l.lay={s:Math.round(clamp(num(v.s,s.day),0,Math.min(s.day,t))),q:clamp(num(v.q,0),0,1),t};}} s.loans=s.loans.filter(l=>l&&own(BANKS,l.bank));for(const l of s.loans){if(l.model!==undefined&&!['balloon','storm','transit','astro'].includes(l.model))delete l.model;for(const k of ['apr0','cut','share'])if(l[k]!==undefined&&!isFinite(+l[k]))delete l[k];if(!Array.isArray(l.covs))l.covs=[];l.covs=l.covs.filter(c=>own(COVS,c));}s.loanOffers=s.loanOffers.filter(l=>l&&own(BANKS,l.bank)); s.usedNames=[...new Set(s.usedNames.filter(x=>typeof x==='string'))];s.log=s.log.filter(e=>e&&typeof e.t==='string').slice(0,LOG_KEEP).map(e=>{const o=e.o&&typeof e.o==='object'&&e.o.a==='inbox'?{a:'tab',v:'nets'}:e&&e.o&&typeof e.o==='object'&&LOG_OPEN.includes(e.o.a)?(e.o.a==='bank'||e.o.a==='ops'?{a:e.o.a}:typeof e.o.v==='string'&&e.o.v.length<=64&&(e.o.a!=='city'||isCity(e.o.v))&&(e.o.a!=='tab'||LOG_TABS.includes(e.o.v))?{a:e.o.a,v:e.o.v}:null):null; return {id:typeof e.id==='string'&&e.id.length<=40?e.id:uid(),d:num(e.d,0),t:e.t.slice(0,600),h:typeof e.h==='string'?e.h.slice(0,200):'',k:LOG_KINDS.includes(e.k)?e.k:'',at:Number.isFinite(+e.at)?+e.at:0,o,n:int(e.n,1,9999,1)};}); diff --git a/src/game/85-incidents/50-cable-ships.js b/src/game/85-incidents/50-cable-ships.js index 02febde..d83ca62 100644 --- a/src/game/85-incidents/50-cable-ships.js +++ b/src/game/85-incidents/50-cable-ships.js @@ -73,7 +73,11 @@ function shipStatus(i){if(!shipSea(i))return '';const l=shipLink(i);if(!l)return /* Laying your own subsea cable: the last part of the build is a lay ship at sea, about 150 km a day. A second ship, booked before laying starts, works from the other end and halves that part; nothing at sea speeds up the survey, the permits or the cable factory. */ -const layDays=l=>Math.max(1,Math.ceil((l.km||0)/LAY_KMD)),layLen=l=>l.ship2?Math.ceil(layDays(l)/2):layDays(l),layStart=l=>l.ready-layLen(l); +const layDays=l=>Math.max(1,Math.ceil((l.km||0)/LAY_KMD)),layLen=l=>l.ship2?Math.ceil(layDays(l)/2):layDays(l),layStart=l=>l.lay?l.lay.s:l.ready-layLen(l); +/* A delay once the ship is at sea (#322) holds it where it is: l.lay = {s: the day laying began, q: the share laid, + t: the day it lays on}. Without it, laying is simply the last layLen() days before ready. */ +function layHold(l,shift,day){if(!layOwn(l)||!(shift>0)||l.ready<=day||dayl.kind==='df'&&l.own&&l.sub; const layName=(l,k=0)=>{let h=0;for(const ch of String(l.id))h=(h*31+ch.charCodeAt(0))>>>0;return LAY_SHIPS[(h+k)%LAY_SHIPS.length];}; function layQuote(l){if(!layOwn(l)||l.ship2||S.day>=layStart(l)||l.ready<=S.day+1)return null;const save=Math.floor(layDays(l)/2);if(save<1)return null;return {save,cost:SHIP_MOB+save*LAY_DAY};} @@ -83,4 +87,4 @@ function layQuote(l){if(!layOwn(l)||l.ship2||S.day>=layStart(l)||l.ready<=S.day+ const PREP_STEP=30,prepMin=l=>leadDays(Math.round(60+(l.km||0)*0.012)),prepCost=l=>Math.round((500000+(l.km||0)*60)*hwMul('build')), prepFloor=l=>Math.max(S.day+1,(l.day||0)+prepMin(l))+layLen(l), prepSteps=l=>layOwn(l)&&l.ready>S.day&&S.dayclamp((T-layStart(l))/layLen(l),0,1); +const layDone=(l,T)=>l.lay?l.lay.q+(1-l.lay.q)*clamp((T-l.lay.t)/Math.max(1,l.ready-l.lay.t),0,1):clamp((T-layStart(l))/layLen(l),0,1); diff --git a/src/game/88-days/10-day-tick.js b/src/game/88-days/10-day-tick.js index 24e0c78..572e7ed 100644 --- a/src/game/88-days/10-day-tick.js +++ b/src/game/88-days/10-day-tick.js @@ -61,7 +61,7 @@ function* dayPost(d){ S.hist=S.hist||[];S.hist.push({d,dem:+R.demand.toFixed(2),srv:+R.served.toFixed(2),c:+(R.egT.c||0).toFixed(2),p:+(R.egT.p||0).toFixed(2),ix:+(R.egT.ix||0).toFixed(2),t:+(R.egT.t||0).toFixed(2),l2:+(R.egT.l2||0).toFixed(2),rev:Math.round(R.revMo),cost:Math.round(R.costMo),cash:Math.round(S.cash),inc:S.inc.length});if(S.hist.length>180)S.hist.shift(); {const top=custLevel();if(top>(S.lvl||1)){for(let lv=(S.lvl||1)+1;lv<=top;lv++){track('level_'+lv);toast(tr('day_tick.customer_level_lv_name_new_customers',{lv,name:LEVELS[lv-1].n,unlock:LEVELS[lv-1].unlock})+`
${esc(LEVELS[lv-1].q)}`,'goal');}S.lvl=top;sfx('coin');}} for(const l of S.links){if(l.kind!=='df')continue; - if(l.ready===d){toast(`${l.own?tr('day_tick.your_fiber_is_finished_a_b',{a:CITY[l.a].name,b:CITY[l.b].name}):tr('day_tick.dark_fiber_handed_over_a_b',{a:CITY[l.a].name,b:CITY[l.b].name})}
${(l.ch||[]).length?tr('day_tick.channels_are_lighting_up_now'):tr('day_tick.it_is_dark_add_optics_to')}`,'goal',[{l:tr('common.open'),act:'selLink',args:{id:l.id}}]);sfx('coin');} + if(l.ready===d){delete l.lay;toast(`${l.own?tr('day_tick.your_fiber_is_finished_a_b',{a:CITY[l.a].name,b:CITY[l.b].name}):tr('day_tick.dark_fiber_handed_over_a_b',{a:CITY[l.a].name,b:CITY[l.b].name})}
${(l.ch||[]).length?tr('day_tick.channels_are_lighting_up_now'):tr('day_tick.it_is_dark_add_optics_to')}`,'goal',[{l:tr('common.open'),act:'selLink',args:{id:l.id}}]);sfx('coin');} if(l.own&&l.ready>d&&Math.random()<0.012){const extra=10+Math.floor(Math.random()*40);setCableReady(l,l.ready+leadDays(extra));toast(tr('day_tick.construction_delay_n_days_why_a',{n:leadDays(extra),why:pick(l.sub?SEA_DELAYS.map(x=>tr(x)):[tr('day_tick.archaeologists_found_a_roman'),tr('day_tick.a_protected_hamster_species_lives'),tr('day_tick.the_railway_changed_its_mind_about'),tr('day_tick.the_drilling_crew_hit_a_granite'),tr('day_tick.a_town_council_wants_a_public'),tr('day_tick.somebody_else_s_gas_main_was'),tr('day_tick.the_permit_office_lost_your_permit')]),a:CITY[l.a].name,b:CITY[l.b].name}),'bad');}} // DDoS protection SLA: a protected customer under attack who goes down, or is blackholed, is paid out (once per attack). for(const a of ddosAll()){ diff --git a/tests/lay-ship-delay.test.js b/tests/lay-ship-delay.test.js new file mode 100644 index 0000000..336da12 --- /dev/null +++ b/tests/lay-ship-delay.test.js @@ -0,0 +1,75 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { load } = require('./harness'); + +// #322: a construction delay that hits your own subsea cable while the lay ship is at sea holds the ship where it is. +// It used to move the whole laying stretch later: the ship vanished, the laid km went back to zero and the survey and +// factory could be hurried (and paid for) all over again. +const start = "ACT.start({});S.cash=1e9;S.lvl=13;S.day=200;S.nextFail=S.nextEvent=1e8;for(const g of GOALS)S.done[g.id]=0;"; +const own = "S.links=[{id:'own',a:'cpt',b:'bue',kind:'df',own:true,sub:true,sys:'roadm',km:6000,ch:[],day:200,ready:800,cut:0,mo:1}];"; + +test('#322 a delay while laying holds the lay ship; it does not start over', () => { + const { ev, dom, errors } = load(); + ev(start + own); + assert.equal(ev('layStart(S.links[0])'), 760); + ev("S.day=770"); + const laid = ev('layDone(S.links[0],S.day)'); + assert.equal(laid, 0.25); + // A storm: 20 days late, as the daily construction delay does it. + ev("setCableReady(S.links[0],S.links[0].ready+20)"); + assert.equal(ev('S.links[0].ready'), 820); + assert.ok(ev('S.day>=layStart(S.links[0])'), 'still laying: the ship stays on the map'); + assert.equal(ev('layDone(S.links[0],S.day)'), laid, 'nothing laid is lost'); + assert.equal(ev('prepSteps(S.links[0])'), 0, 'no paying for the survey and factory again'); + assert.equal(ev('layQuote(S.links[0])'), null); + ev("UI.sel={type:'link',id:'own'};renderPanel()"); + const panel = () => dom.window.document.querySelector('#panelBody').textContent; + assert.match(panel(), /at sea, laying/); + assert.match(panel(), /1,500 of 6,000 km/); + assert.doesNotMatch(panel(), /Survey, permits and the cable factory|Hurry/); + // The ship waits out the delay, then lays the other 30 days' worth, never going backwards. + let prev = laid; + for (let d = 770; d < 820; d++) { + ev('S.day=' + d); + const q = ev('layDone(S.links[0],S.day)'); + assert.ok(q >= prev, `day ${d}: ${q} < ${prev}`); + assert.ok(ev('S.day>=layStart(S.links[0])'), `day ${d}: the ship is at sea`); + if (d <= 790) assert.equal(q, laid, `day ${d}: waiting out the storm`); + prev = q; + } + assert.equal(ev('layDone(S.links[0],819.5)') < 1, true); + assert.equal(ev('layDone(S.links[0],820)'), 1); + // A second delay during the wait adds to it. + ev(start + own + "S.day=770;setCableReady(S.links[0],810);S.day=780;setCableReady(S.links[0],820)"); + assert.equal(ev('S.links[0].lay.t'), 790, 'held until 780, then 10 days more'); + assert.equal(ev('layDone(S.links[0],790)'), 0.25); + assert.ok(ev('layDone(S.links[0],791)') > 0.25); + assert.equal(ev('layDone(S.links[0],820)'), 1); + assert.deepEqual(errors, []); +}); + +test('#322 a held lay ship survives a save, and bad saved values are repaired', () => { + const { ev, errors } = load(); + ev(start + own + "S.day=770;setCableReady(S.links[0],820)"); + const q = ev('layDone(S.links[0],795)'); + ev('S=migrate(JSON.parse(JSON.stringify(S)))'); + assert.equal(ev('layDone(S.links[0],795)'), q); + assert.equal(ev('layDone(S.links[0],770)'), 0.25); + // Junk from an edited save, or on a cable that is not your own subsea build, goes away. + ev("S.links[0].lay={s:'x',q:7,t:-5};S=migrate(JSON.parse(JSON.stringify(S)))"); + const l = JSON.parse(ev('JSON.stringify(S.links[0].lay)')); + assert.ok(l.q >= 0 && l.q <= 1 && l.s <= 770 && l.t <= 820, JSON.stringify(l)); + ev("S.links[0].lay=[1];S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.equal(ev('S.links[0].lay'), undefined); + ev("S.links[0].sub=false;S.links[0].lay={s:760,q:.5,t:790};S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.equal(ev('S.links[0].lay'), undefined); + // A finished cable drops it. + ev(start + own + "S.day=770;setCableReady(S.links[0],820);S.day=819;dayTick()"); + assert.equal(ev('S.links[0].ready<=S.day'), true); + assert.equal(ev('S.links[0].lay'), undefined); + // Old saves (no lay field) lay as before. + ev(start + own + "S=migrate(JSON.parse(JSON.stringify(S)))"); + assert.equal(ev('layStart(S.links[0])'), 760); + assert.equal(ev('layDone(S.links[0],780)'), 0.5); + assert.deepEqual(errors, []); +});