/* Cable ships on the map (85-incidents/50-cable-ships.js): club ships at anchor off their ports, sailing out to a cut, splicing on site and sailing home; lay ships along a cable you are building. Where they are comes from S.fleet and the clock, so nothing here is saved; their routes are worked out once per voyage and kept in SHIP_PATH. */ const SHIP_PATH=new Map(),SHIP_HITS=[]; // Off the port: the first point of a cable leaving it, so a ship rides at anchor at sea and not in town. function shipHomePt(s){const k='h:'+s.id;let p=SHIP_PATH.get(k);if(p)return p;const C=CITY[s.home];p=[C.x,C.y]; for(const cp of CABLE_PAIRS){if(cp.a===s.home){p=cp.pts[1].slice();break;}if(cp.b===s.home){p=cp.pts[cp.pts.length-2].slice();break;}} const n=SHIPS.filter(x=>x.home===s.home).indexOf(s);p[0]+=n*7;p[1]+=n*4;SHIP_PATH.set(k,p);return p;} // Out along the cable routes to the cable's end, then along the cable to the fault. function shipRoute(s,f){const key=s.id+'|'+f.dep+'|'+f.link+'|'+f.e+'|'+f.t;let r=SHIP_PATH.get(key);if(r)return r;if(SHIP_PATH.size>300)SHIP_PATH.clear(); const pts=[shipHomePt(s).slice()],push=q=>{const b=pts[pts.length-1];pts.push([q[0]+W*Math.round((b[0]-q[0])/W),q[1]]);},l=S.links.find(x=>x.id===f.link); if(l){const end=f.e==='b'?l.b:l.a,D=shipDij({[end]:0});let c=s.home; for(let n=0;n<999&&c!==end;n++){const p=D.prev.get(c);if(!p)break;const cp=CABLE_PAIRS[p.e.i],q=cp.a===c?cp.pts:cp.pts.slice().reverse();for(let j=1;j0&&g.cum[j]>f.t;j--)push(g.pts[j]); push(polyAt(g.pts,g.cum,f.t));} else push([f.fx,f.fy]); const cum=[0];let L=0;for(let j=1;j=f.back)return {x:H[0],y:H[1],m:'port',dx:1}; const r=shipRoute(s,f),m=f.dep+SHIP_MOB_D;let p,mode; if(f.rs!=null&&T>=f.rs){p=f.rt*(1-clamp((T-f.rs)/Math.max(0.01,f.back-f.rs),0,1));mode='back';} else if(TS.day&&T>=layStart(l)){const g=linkGeom(l),q=layDone(l,T),two=!!l.ship2; const put=(t,k,dir)=>{const [x,y]=polyAt(g.pts,g.cum,clamp(t,0,1)),[x2]=polyAt(g.pts,g.cum,clamp(t+dir*0.01,0,1));list.push({p:{x,y,m:'lay',dx:x2-x||dir},lay:l,k,col:'#16A866'});}; if(two){put(q/2,0,1);put(1-q/2,1,-1);}else put(q,0,1);} for(const it of list)for(const k of ks){const [sx,sy]=toS(it.p.x,it.p.y,k);if(sx<-30||sx>CW+30||sy<-30||sy>CH+30)continue; const oy=it.p.m==='work'?-18*sc:0;shipSprite(sx,sy+oy,it.p.dx<0?-1:1,sc,it.col,now,it.p.m);SHIP_HITS.push({x:sx,y:sy+oy,r:14*sc,it});}} function shipHit(cx,cy){const r=cv.getBoundingClientRect(),x=cx-r.left,y=cy-r.top;let best=null,bd=Infinity; for(const h of SHIP_HITS){const d=Math.hypot(h.x-x,h.y-y);if(d<=h.r+6&&dx.id===f.inc&&x.ship===s.id),l=f&&S.links.find(x=>x.id===f.link); const what=!f||S.day>=f.back?(s.club==='prv'?tr('cable_ships.private_at_anchor',{home}):tr('cable_ships.at_anchor_off_home',{home})):i?esc(shipStatus(i)):trn('cable_ships.sailing_home_to_home',Math.max(1,f.back-S.day),{home}); toast(`${esc(s.n)}
${esc(club)} · ${what}`,'',l&&i?[{l:tr('common.open'),act:'selLink',args:{id:l.id}}]:[]);}