Versus mode: attack vs defend (sim, bots, server, client)

One player defends, the other plays the Internet: earns credits over time and
spends them on packets, upgrades and sabotage. Two rounds with swapped roles,
timed (the faster fall wins). Built end to end, NOT yet QA'd or balanced.

Sim (public/js/sim.js, TD.VS table): attacker economy (income, lifetime bonus,
leak payback), spawn / stage / launch convoys, 7 tech tracks, abilities (EMP,
decoy, surge, DDoS), defender tools (scan, patch, repair, quarantine), role-checked
commands, round result, role-private state. Co-op/solo unchanged (24 scripted
games replay identically).

Bots (public/js/bot.js, tools/versus.js): defender (maze + turrets) and attacker
(budgeted spender reading the tower mix); headless bot-vs-bot runner.

Server (server/versus.js, protocol doc): versus rooms with lobby / countdown /
playing / roundover / matchover, role stamping, cross-role refusal, role-private
priv messages at 4 Hz, computer seat, freeze/forfeit rules, no pause/speed.

Client (public/js/versus.js): setup dialog (role, length, vs computer or online),
countdown, defender tools row, attacker unit shop with convoy + launch, tech and
abilities, gateway choice, round/match screens, sounds, phone-first dock rows.

Status and next steps (balance: the attacker currently wins ~100% of bot rounds;
QA with two browsers; layout matrix rerun) are in docs/NEXT.md and docs/VERSUS.md.
The full planned workflow incl. balance/QA prompts is in docs/workflows/versus-full.js.

253 unit tests pass; one smoke e2e run; no other e2e per the user's request.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
Claude 2026-10-11 10:22:29 +00:00
parent b4ae6cb57d
commit 2098d59c1f
23 changed files with 6166 additions and 90 deletions

View file

@ -329,6 +329,8 @@
this.rot = 0; // 0, or PI/2 when the board is drawn rotated for portrait screens
this.fx = (root.matchMedia && root.matchMedia('(pointer: coarse)').matches) ? 0.55 : 1; // fewer particles on phones
this.names = {};
// versus cues (all stay empty in solo / co-op): screen effects with seconds left, Scan zones, gateway pulses, the tower list for events that name a tower
this.fxT = { surge: 0, ddos: 0, freeze: 0 }; this.scans = []; this.gatePulse = new Map(); this._towers = [];
}
setBoard(board) {
@ -336,6 +338,7 @@
this.wallGrid = new Uint8Array(board.cols * board.rows);
this.parts.length = 0; this.rings.length = 0; this.lines.length = 0; this.texts.length = 0; this.marks.length = 0; this.pulses.length = 0;
this.aim.clear(); this.trk.clear(); this.glow.clear();
this.fxT.surge = this.fxT.ddos = this.fxT.freeze = 0; this.scans.length = 0; this.gatePulse.clear();
this.resetCamera();
}
resize(w, h, dpr) {
@ -453,6 +456,22 @@
break;
}
case 'heal': this._pulse(e.x * C, e.y * C, 10, (e.r || 2.2) * C, '#7ee08a', 0.7, 2.2, 0.07); break; // soft green wave out to the daemon's range
// ---- versus
case 'emp': { // the attacker disabled a tower: a yellow burst on it (the disabled look itself follows tower.empT)
const tw = this._towers.find((x) => x.id === e.id);
if (tw) { const cx = (tw.x + 0.5) * C, cy = (tw.y + 0.5) * C; this._ring(cx, cy, 6, 44, '#ffe27a', 0.55, 3); this._ring(cx, cy, 4, 26, '#ffffff', 0.3, 2); this._burst(cx, cy, '#ffe27a', 12, 120, 0.5); this.shake = Math.min(2, this.shake + 0.3); }
break;
}
case 'surge': this.fxT.surge = e.d || 6; break;
case 'ddos': this.fxT.ddos = e.d || 4; break;
case 'quarantine': this.fxT.freeze = e.d || 2; break;
case 'scan': {
const sc = TD.VS && TD.VS.TOOLS.scan, rd = (sc ? sc.radius : 4), rv = sc ? sc.reveal : 3;
this.scans.push({ x: e.x, y: e.y, t: rv, max: rv, r: rd });
this._pulse(e.x * C, e.y * C, 8, rd * C, '#7ee8ff', 0.6, 3, 0.1);
break;
}
case 'repair': { const b = this.board; if (b) { this._ring((b.core.x + 0.5) * C, (b.core.y + 0.5) * C, 10, 52, '#35e0a1', 0.6, 3); this._burst((b.core.x + 0.5) * C, (b.core.y + 0.5) * C, '#35e0a1', 8, 80, 0.5); } break; }
default: break;
}
}
@ -466,6 +485,7 @@
const t = now / 1000;
const dt = clamp((now - (this.lastNow || now)) / 1000, 0, 0.1); this.lastNow = now;
const sc = this.scale;
const vsui = ui && ui.vs ? ui.vs : null; // versus-only cues (null in solo / co-op)
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = BG; ctx.fillRect(0, 0, this.W, this.H);
@ -502,11 +522,11 @@
}
this._drawRoutes(ctx, b, ui, t);
this._drawEndpoints(ctx, b, view, t, dt, rot);
this._drawEndpoints(ctx, b, view, t, dt, rot, vsui);
// walls (with trace links) then turrets
const wg = this.wallGrid; wg.fill(0);
const towers = view.towers;
const towers = view.towers; this._towers = towers;
for (let i = 0; i < towers.length; i++) if (towers[i].type === 'wall') wg[towers[i].y * cols + towers[i].x] = 1;
ctx.lineCap = 'round';
ctx.strokeStyle = 'rgba(90,169,255,.55)'; ctx.lineWidth = 3.2;
@ -545,6 +565,8 @@
const cx = (tw.x + 0.5) * C, cy = (tw.y + 0.5) * C;
const gl = tw.type === 'sandbox' && this.glow.size ? this.glow.get(tw.y * cols + tw.x) : 0;
drawGlyph(ctx, tw.type, tw.level, cx, cy, C, ang, t, 1, rot, gl ? Math.min(1, gl / 0.25) : 0);
if (tw.empT > 0) this._drawEmp(ctx, tw, cx, cy, t); // versus: disabled by an EMP
else if (vsui && vsui.empArm && tw.type !== 'wall') { ctx.strokeStyle = 'rgba(255,226,122,' + (0.5 + 0.4 * Math.sin(t * 8)) + ')'; ctx.lineWidth = 2.5; rr(ctx, tw.x * C + 2, tw.y * C + 2, C - 4, C - 4, 6); ctx.stroke(); } // EMP armed: every tower it can hit
if (coop && tw.type !== 'wall') { // owner corner flag: at least ~5 CSS px, dark outline so it shows on a tile of the same colour
const k = Math.max(12, 5.5 / sc), x0 = cx - C * 0.46, y0 = cy - C * 0.46;
ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x0 + k, y0); ctx.lineTo(x0, y0 + k); ctx.closePath();
@ -569,8 +591,8 @@
}
// ground monsters, then flyers on top
this._drawMonsters(ctx, view, t, dt, false, rot);
this._drawMonsters(ctx, view, t, dt, true, rot);
this._drawMonsters(ctx, view, t, dt, false, rot, vsui);
this._drawMonsters(ctx, view, t, dt, true, rot, vsui);
// bullets
for (const bu of view.bullets) {
@ -584,6 +606,7 @@
}
this._drawFx(ctx, dt, sc, rot);
if (vsui || this.scans.length || this.fxT.surge > 0 || this.fxT.ddos > 0 || this.fxT.freeze > 0) this._drawVsFx(ctx, dt, sc, cols, rows, t, ui);
// placement ghost
if (ui.hover && ui.selType && !ui.noGhost && b.inBounds(ui.hover.x, ui.hover.y)) {
@ -692,15 +715,17 @@
ctx.setLineDash([]);
}
_drawEndpoints(ctx, b, view, t, dt, rot) {
_drawEndpoints(ctx, b, view, t, dt, rot, vsui) {
// spawns: red gateways
for (const sp of b.spawns) {
for (let gi = 0; gi < b.spawns.length; gi++) {
const sp = b.spawns[gi];
const p = (t * 0.7) % 1;
ctx.save(); ctx.translate((sp.x + 0.5) * C, (sp.y + 0.5) * C); if (rot) ctx.rotate(-rot);
ctx.strokeStyle = rgba('#ff5c7a', 0.5 * (1 - p)); ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(0, 0, 12 + p * 16, 0, TAU); ctx.stroke();
ctx.fillStyle = '#2a0c18'; ctx.beginPath(); ctx.arc(0, 0, 13, 0, TAU); ctx.fill();
ctx.strokeStyle = '#ff5c7a'; ctx.lineWidth = 2; ctx.stroke();
ctx.lineWidth = 1.3; ctx.beginPath(); ctx.ellipse(0, 0, 5.5, 13, 0, 0, TAU); ctx.moveTo(-13, 0); ctx.lineTo(13, 0); ctx.stroke();
if (vsui) this._vsGate(ctx, gi, vsui, t, dt, b.spawns.length);
ctx.restore();
}
// core: server rack with an integrity ring
@ -721,7 +746,7 @@
ctx.restore();
}
_drawMonsters(ctx, view, t, dt, air, rot) {
_drawMonsters(ctx, view, t, dt, air, rot, vsui) {
const trk = this.trk;
if (!air) { // a Daemon's heal range, faint, all in one stroke
let any = false;
@ -737,7 +762,10 @@
if (!!m.air !== air) continue;
const def = TD.ENEMIES[m.kind];
let k = trk.get(m.id);
if (!k) { k = { x: m.x, y: m.y, ang: 0, hp: m.hp, flash: 0, still: 0, sv: null }; trk.set(m.id, k); }
if (!k) {
k = { x: m.x, y: m.y, ang: 0, hp: m.hp, flash: 0, still: 0, sv: null }; trk.set(m.id, k);
if (vsui) this._vsBorn(k, m, vsui, t);
}
const dm = def.shield && m.maxShield === undefined ? this._shieldView(k, m, def, dt) : m; // co-op: tell hp and shield apart (see there)
const v = vitals(dm, def), cur = v.hp + v.sh;
const dx = m.x - k.x, dy = m.y - k.y;
@ -749,6 +777,7 @@
// stunned: solo knows (stunT); in co-op the wire only says "slowed or stunned", and a slowed packet still moves, so a flagged packet that stands still is stunned
const stun = m.stunT !== undefined ? m.stunT > 0 : m.slow > 0 && k.still > 0.2 && !view.paused;
if (air && !SWARM[m.kind]) { const er = def.r * C; ctx.fillStyle = 'rgba(0,0,0,.28)'; ctx.save(); ctx.translate(m.x * C, m.y * C); if (rot) ctx.rotate(-rot); ctx.beginPath(); ctx.ellipse(0, 5, er, er * 0.45, 0, 0, TAU); ctx.fill(); ctx.restore(); }
if (vsui) { this._vsUnit(ctx, dm, k, def, vsui, t, rot, stun); continue; }
drawMonster(ctx, dm, k.ang, t, k.flash, rot, stun);
}
if (air && trk.size > view.monsters.length + 60) { // drop stale trackers
@ -778,6 +807,114 @@
return s;
}
// ---------------------------------------------------------------- versus cues
// a tower an EMP has switched off: dimmed, hazard stripes, flickering sparks and an arc that runs out with the time left
_drawEmp(ctx, tw, cx, cy, t) {
const total = (TD.VS && TD.VS.ABILITIES.emp.dur) || 6, h = C * 0.43;
ctx.save(); ctx.translate(cx, cy);
ctx.fillStyle = 'rgba(4,7,14,.66)'; rr(ctx, -h, -h, h * 2, h * 2, C * 0.12); ctx.fill();
ctx.save(); ctx.clip();
ctx.strokeStyle = 'rgba(255,210,77,.5)'; ctx.lineWidth = 3;
for (let i = -3; i <= 3; i++) { ctx.beginPath(); ctx.moveTo(i * 9 - 18, 18); ctx.lineTo(i * 9 + 18, -18); ctx.stroke(); }
ctx.restore();
ctx.strokeStyle = '#ffe27a'; ctx.lineWidth = 3; ctx.lineCap = 'round';
ctx.beginPath(); ctx.arc(0, 0, C * 0.5, -Math.PI / 2, -Math.PI / 2 + TAU * clamp(tw.empT / total, 0, 1)); ctx.stroke();
if (Math.sin(t * 26 + tw.id * 3) > 0.2) { // a spark
ctx.strokeStyle = '#fff6c9'; ctx.lineWidth = 2; ctx.beginPath();
const a = (Math.floor(t * 9 + tw.id) % 4) * 1.57;
ctx.moveTo(Math.cos(a) * 6, Math.sin(a) * 6); ctx.lineTo(Math.cos(a + 0.4) * 12, Math.sin(a + 0.4) * 12); ctx.lineTo(Math.cos(a - 0.1) * 15, Math.sin(a - 0.1) * 15); ctx.stroke();
}
ctx.restore();
}
// the gateway the attacker picked (amber ring), numbers on the gateways when there is a choice, and a pulse where a unit has just entered
_vsGate(ctx, gi, vsui, t, dt, n) {
const pl = this.gatePulse.get(gi);
if (pl > 0) {
const k = 1 - pl / 0.7;
ctx.strokeStyle = rgba('#ffffff', 0.85 * (1 - k)); ctx.lineWidth = 3; ctx.beginPath(); ctx.arc(0, 0, 13 + k * 26, 0, TAU); ctx.stroke();
this.gatePulse.set(gi, pl - dt); if (pl - dt <= 0) this.gatePulse.delete(gi);
}
if (vsui.gates > 1 && vsui.role === 'attacker') {
const on = vsui.gate === gi;
if (on) { ctx.strokeStyle = '#ffd24d'; ctx.lineWidth = 3.5; ctx.beginPath(); ctx.arc(0, 0, 18 + Math.sin(t * 6) * 1.5, 0, TAU); ctx.stroke(); }
ctx.fillStyle = on ? '#ffd24d' : 'rgba(8,13,26,.9)'; ctx.beginPath(); ctx.arc(11, -11, 7.5, 0, TAU); ctx.fill();
ctx.lineWidth = 1.5; ctx.strokeStyle = on ? '#ffd24d' : '#ff5c7a'; ctx.stroke();
ctx.fillStyle = on ? '#241a02' : '#ffd1da'; ctx.font = '700 10px system-ui, sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(String(gi + 1), 11, -10.5); ctx.textAlign = 'start';
}
}
// what the attacker knows about its own units, remembered when a unit first shows up (the wire carries neither stealth nor tech): stealth seconds and shield capacity
_vsBorn(k, m, vsui, t) {
k.born = t; k.stl = 0; k.shl = 0;
// a spawn pulse at the gateway it enters from (every client sees it: the defender too)
const b = this.board;
if (b) for (let i = 0; i < b.spawns.length; i++) { const sp = b.spawns[i], dx = m.x - (sp.x + 0.5), dy = m.y - (sp.y + 0.5); if (dx * dx + dy * dy < 1.6) { this.gatePulse.set(i, 0.7); break; } }
if (vsui.role === 'attacker' && vsui.tech && TD.VS) {
const X = TD.VS.TECH;
if (vsui.tech.stealth) k.stl = X.stealth.secs[vsui.tech.stealth - 1] || 0;
if (vsui.tech.shield) k.shl = X.shield.per * vsui.tech.shield;
}
}
_vsUnit(ctx, m, k, def, vsui, t, rot, stun) {
const r = def.r * C, now = t * 1000;
const dec = !!m.decoy || (vsui.decoys && vsui.decoys.size > 0 && (vsui.decoys.get(m.id) || 0) > now);
const hid = vsui.role === 'attacker' && (m.stealthT !== undefined ? m.stealthT > 0 : k.stl > 0 && t - k.born < k.stl);
const lift = m.kind === 'ghost' ? -7 : 0;
ctx.save();
if (dec) ctx.globalAlpha = 0.5; else if (hid) ctx.globalAlpha = 0.38;
drawMonster(ctx, m, k.ang, t, k.flash, rot, stun);
ctx.restore();
if (dec || hid || k.shl > 0 || (m.maxShield > 0 && vsui.role === 'attacker' && !def.shield)) {
ctx.save(); ctx.translate(m.x * C, m.y * C + lift); if (rot) ctx.rotate(-rot);
if (dec) { // bait: a spinning dashed ring
ctx.strokeStyle = 'rgba(126,232,255,.95)'; ctx.lineWidth = 2; ctx.setLineDash([5, 4]); ctx.lineDashOffset = -t * 20;
ctx.beginPath(); ctx.arc(0, 0, r + 7, 0, TAU); ctx.stroke(); ctx.setLineDash([]);
}
if (hid) { // cloaked: a faint shimmer ring
ctx.strokeStyle = 'rgba(190,200,255,' + (0.35 + 0.25 * Math.sin(t * 12 + m.id)) + ')'; ctx.lineWidth = 1.5; ctx.setLineDash([3, 4]); ctx.lineDashOffset = t * 10;
ctx.beginPath(); ctx.arc(0, 0, r + 4, 0, TAU); ctx.stroke(); ctx.setLineDash([]);
}
const ms = m.maxShield !== undefined && !def.shield ? m.maxShield : k.shl; // tech shield (kinds that carry their own shield draw it themselves)
if (ms > 0 && vsui.role === 'attacker' && !def.shield) {
const sh = m.shield !== undefined ? m.shield : clamp(m.hp - (m.maxhp - ms), 0, ms);
if (sh > 0.5) { ctx.strokeStyle = 'rgba(150,200,255,' + (0.3 + 0.55 * clamp(sh / ms, 0, 1)) + ')'; ctx.lineWidth = 1.2 + 1.8 * clamp(sh / ms, 0, 1); ctx.beginPath(); ctx.arc(0, 0, r + 3, 0, TAU); ctx.stroke(); }
}
ctx.restore();
}
}
// screen cues in board space: Quarantine (icy), DDoS (violet scan lines), Surge (hot edge), Scan zones, and the ring of an armed Scan
_drawVsFx(ctx, dt, sc, cols, rows, t, ui) {
const f = this.fxT, W = cols * C, H = rows * C;
if (f.freeze > 0) {
f.freeze -= dt;
ctx.fillStyle = 'rgba(160,220,255,' + (0.1 + 0.05 * Math.sin(t * 9)) + ')'; ctx.fillRect(0, 0, W, H);
ctx.strokeStyle = 'rgba(225,246,255,.85)'; ctx.lineWidth = 6 / sc; ctx.strokeRect(0, 0, W, H);
}
if (f.ddos > 0) {
f.ddos -= dt;
ctx.fillStyle = 'rgba(150,80,255,.07)'; ctx.fillRect(0, 0, W, H);
ctx.strokeStyle = 'rgba(190,130,255,.35)'; ctx.lineWidth = 1.5 / sc; ctx.beginPath();
for (let y = (t * 60) % 24; y < H; y += 24) { ctx.moveTo(0, y); ctx.lineTo(W, y); }
ctx.stroke();
}
if (f.surge > 0) {
f.surge -= dt;
ctx.strokeStyle = 'rgba(255,150,60,' + (0.45 + 0.3 * Math.sin(t * 14)) + ')'; ctx.lineWidth = 8 / sc; ctx.strokeRect(0, 0, W, H);
}
for (let i = this.scans.length - 1; i >= 0; i--) {
const z = this.scans[i]; z.t -= dt;
if (z.t <= 0) { this.scans.splice(i, 1); continue; }
const a = clamp(z.t / z.max, 0, 1), cx = z.x * C, cy = z.y * C;
ctx.fillStyle = 'rgba(126,232,255,' + 0.07 * a + ')'; ctx.beginPath(); ctx.arc(cx, cy, z.r * C, 0, TAU); ctx.fill();
ctx.strokeStyle = 'rgba(126,232,255,' + (0.35 + 0.4 * a) + ')'; ctx.lineWidth = 2; ctx.setLineDash([8, 6]); ctx.lineDashOffset = -t * 18;
ctx.beginPath(); ctx.arc(cx, cy, z.r * C, 0, TAU); ctx.stroke(); ctx.setLineDash([]);
}
const v = ui && ui.vs;
if (v && v.scanArm && ui.hover && this.board && this.board.inBounds(ui.hover.x, ui.hover.y)) {
const rd = (TD.VS && TD.VS.TOOLS.scan.radius) || 4;
this._rangeRing(ctx, (ui.hover.x + 0.5) * C, (ui.hover.y + 0.5) * C, rd * C, '#7ee8ff');
}
}
_drawFx(ctx, dt, sc, rot) {
// lines (tracers, arcs)
ctx.lineCap = 'round'; ctx.lineJoin = 'round';