/* Robot Clash - client side of a server-run live fight. No simulation here: the server steps RC.Combat; this sends the local input and shows the server's snapshots (already mapped to "robot 0 = me"). const nf = RC.NetFight({ net, seat }) nf.frame(s, input, dt) send input (on change, max 30/s, at least every 0.25 s), ease the local state toward the newest snapshot, return the events of snapshots not seen yet nf.oppAway true while the opponent is disconnected (server tells us) nf.silentFor seconds since the last snapshot (connection trouble) nf.stop() */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || {}); const SEND_MIN = 1 / 30, SEND_KEEP = 0.25; const EASE = ['x', 'z', 'y'], ANG = ['a', 'turretA']; const wrap = a => { while (a > Math.PI) a -= 2 * Math.PI; while (a < -Math.PI) a += 2 * Math.PI; return a; }; RC.NetFight = (opt) => { const sock = opt.net.socket, seat = opt.seat; let latest = null, seenTick = -1, seq = 0, sendT = 0, keepT = 0, prev = null, lastSnap = Date.now(); const off = sock.on('snap', m => { latest = RC.Proto.toLocal(seat, m); lastSnap = Date.now(); }); const changed = i => !prev || i.throttle !== prev.throttle || i.turn !== prev.turn || i.fire !== prev.fire || i.fire2 !== prev.fire2 || i.fire3 !== prev.fire3; function sendInput(inp, dt) { sendT -= dt; keepT -= dt; if (sendT > 0 || (!changed(inp) && keepT > 0)) return; const i = RC.Proto.input(inp); if (sock.send(Object.assign({ t: 'input', seq: ++seq }, i))) { prev = i; sendT = SEND_MIN; keepT = SEND_KEEP; } } function applyRobot(r, d, k) { for (const f of EASE) if (typeof d[f] === 'number') r[f] += (d[f] - r[f]) * k; for (const f of ANG) if (typeof d[f] === 'number') r[f] += wrap(d[f] - r[f]) * k; for (const f of RC.Proto.RF) if (!EASE.includes(f) && !ANG.includes(f) && d[f] !== undefined) r[f] = d[f]; if (d.lost) r.lost = Object.assign(r.lost || {}, d.lost); if (d.lostW) r.lostW = d.lostW.slice(); if (d.W && r.W) d.W.forEach((w, i) => { if (r.W[i]) Object.assign(r.W[i], w); }); } const syncList = (dst, src, def) => { dst.length = src.length; for (let i = 0; i < src.length; i++) dst[i] = Object.assign(dst[i] || Object.assign({}, def), src[i]); }; function frame(s, inp, dt) { sendInput(inp, dt); if (!latest) return []; const n = latest, k = Math.min(1, dt * 14); s.t = n.time; s.left = n.left; s.pitOpen = n.pitOpen; s.pitAlarm = n.pitAlarm; s.endIn = n.endIn; s.press.st = n.press.st; s.press.y = n.press.y; Object.assign(s.crane, n.crane); syncList(s.rockets, n.rockets, { sp: 6, t: 3 }); syncList(s.mines, n.mines, { r: 0.85 }); applyRobot(s.robots[0], n.robots[0], k); applyRobot(s.robots[1], n.robots[1], k); if (n.result) s.result = n.result; if (n.tick === seenTick) return []; seenTick = n.tick; return n.ev || []; } return { frame, get oppAway() { return !!(latest && latest.away && latest.away[1]); }, get silentFor() { return (Date.now() - lastSnap) / 1000; }, stop() { off(); }, }; }; })(typeof window !== 'undefined' ? window : globalThis);