Files
Robot-Clash/game/combat.js
2026-11-17 14:17:50 +01:00

760 lines
39 KiB
JavaScript

/* Robot Clash - combat simulation (no DOM, deterministic with a seed). Needs parts.js + robot.js.
const s = RC.Combat.create({ robots: [statsA, statsB], seed: 1, names: ['A','B'] });
RC.Combat.step(s, dt, [inputA, inputB]) input = { throttle: -1..1, turn: -1..1, fire: bool, fire2: bool }
s.events -> [{ type, x, z, ... }] appended each step; the caller empties it after reading
s.result -> null while running, then { winner: 0 | 1 | -1 (draw), reason: 'ko' | 'pit' | 'time' }
RC.Combat.inZone(s, me, enemy) -> enemy inside my weapon reach (used by the AI)
RC.ARENA -> layout (half size, hazards, guardians, pit), read by render3d.js
World: x/z floor plane, y up, metres. Robot heading a: forward = (cos a, sin a). */
(function (root) {
'use strict';
const RC = (root.RC = root.RC || {});
// Arena definitions. `half` = floor from -half..half. Hazards scale with the size. The active arena is
// RC.ARENA; a fight snapshots it into s.arena so several arenas can exist. render3d.js reads s.arena.
function arena(o) {
const h = o.half, shape = o.shape || 'square';
// guardians sit in the corners of a square arena; on a round/octagon floor they move inwards
// along the diagonal, otherwise they would stand outside the wall
const g = (h - 1.75) * (shape === 'square' ? 1 : shape === 'round' ? 0.7 : 0.66);
return {
id: o.id, name: o.name, theme: o.theme || 'steel', half: h,
shape: o.shape || 'square', // square | octagon | round
blocks: o.blocks || [], belts: o.belts || [], oil: o.oil || [], rams: o.rams || [],
duration: 120, pitOpensAt: o.pitOpensAt || 60,
pit: { x: 0, z: o.pitZ != null ? o.pitZ : h * 0.52, hw: o.pitW || 1.35, hd: o.pitW || 1.35 },
saws: o.saws, flames: o.flames,
press: { x: g, z: -g, hw: 1.6, hd: 1.6 }, // guardian 1: forge press (corner)
crane: { x: -g, z: g, hw: 1.6, hd: 1.6 }, // guardian 2: magnet crane (corner)
starts: [{ x: -h * 0.6, z: h * 0.2, a: 0 }, { x: h * 0.6, z: h * 0.2, a: Math.PI }],
};
}
const saw = (x, z, phase) => ({ x, z, r: 0.95, period: 6, on: 2.2, phase });
const flame = (x, z, phase) => ({ x, z, hw: 0.9, hd: 0.9, period: 7, on: 1.8, phase });
const saw2 = saw, flame2 = flame;
const block = (kind, x, z, hw, hd, h2) => ({ kind, x, z, hw, hd, h: h2 || 0.8 });
const belt = (x, z, hw, hd, ax, az, force) => ({ x, z, hw, hd, ax, az, force });
const ram = (x, z, hw, hd, nx, nz, phase) => ({ x, z, hw, hd, nx, nz, period: 5, on: 1.3, phase });
RC.ARENAS = [
// tight starter pit: two scrap heaps are the only cover
arena({ id: 'workshop', name: 'Workshop Pit', theme: 'steel', half: 8, shape: 'square',
saws: [saw(-3.2, -3.4, 0), saw(3.2, -3.4, 3)],
flames: [flame(-2.7, 3.3, 1), flame(2.7, 3.3, 4.5)],
blocks: [block('scrap', -2.7, -1.2, 0.8, 0.8), block('scrap', 2.7, -1.2, 0.8, 0.8)] }),
// wreck yard: lots of cover and two oil slicks that kill your steering
arena({ id: 'scrapyard', name: 'Scrapyard', theme: 'steel', half: 10, shape: 'square', pitW: 1.5,
saws: [saw(-4, -4, 0), saw(4, -4, 2.5), saw(0, 4.5, 1.5)],
flames: [flame(-5.5, 2, 0), flame(5.5, 2, 3), flame(0, -6, 2)],
blocks: [block('scrap', -3.8, -1.5, 1.0, 0.9, 1.0), block('scrap', 3.8, -1.5, 1.0, 0.9, 1.0),
block('scrap', 0, -5.2, 1.4, 0.8, 0.9), block('crate', -2.8, 3.0, 0.6, 0.6, 0.7), block('crate', 2.8, 3.0, 0.6, 0.6, 0.7)],
oil: [{ x: -2.4, z: 1.0, r: 1.6 }, { x: 2.6, z: -3.4, r: 1.5 }] }),
// foundry: a belt drags you sideways into the flame channel
arena({ id: 'foundry', name: 'The Foundry', theme: 'lava', half: 12, shape: 'octagon', pitW: 1.7,
saws: [saw(-5, -4.5, 0), saw(5, -4.5, 3), saw(0, 4.5, 1.5)],
flames: [flame(-4.5, 1, 0), flame(4.5, 1, 3.5), flame(0, -7.5, 2), flame(-7, 6, 5)],
blocks: [block('barrier', -6.2, -5, 1.3, 0.4, 0.6), block('barrier', 6.2, -5, 1.3, 0.4, 0.6)],
belts: [belt(0, 1, 4.0, 1.1, 1, 0, 9)] }),
// crucible: one pillar in the middle, two wall rams punching inwards
arena({ id: 'crucible', name: 'The Crucible', theme: 'lava', half: 14, shape: 'round', pitW: 1.9,
saws: [saw(-5.5, -5, 0), saw(5.5, -5, 3), saw(-5.5, 5, 1.5), saw(5.5, 5, 4.5)],
flames: [flame(-6, 1.5, 0), flame(6, 1.5, 3), flame(0, 8, 2), flame(0, -8, 4.5)],
blocks: [block('pillar', 0, 0, 1.1, 1.1, 1.8)],
rams: [ram(-11.8, 0, 1.5, 2.2, 1, 0, 0), ram(11.8, 0, 1.5, 2.2, -1, 0, 2.5)] }),
// dome: four pillars to duck behind, and the pit opens early
arena({ id: 'arena64', name: 'Mega Dome', theme: 'blue', half: 16, shape: 'octagon', pitW: 2.1, pitOpensAt: 80,
saws: [saw(-6.5, -6, 0), saw(6.5, -6, 3), saw(-6.5, 6, 1.5), saw(6.5, 6, 4.5), saw(0, 0, 2)],
flames: [flame(-8, 2, 0), flame(8, 2, 2), flame(0, 9, 4), flame(0, -9, 1), flame(-10, -4, 3), flame(10, -4, 5)],
blocks: [block('pillar', -5, -5, 0.9, 0.9, 2.0), block('pillar', 5, -5, 0.9, 0.9, 2.0),
block('pillar', -5, 5, 0.9, 0.9, 2.0), block('pillar', 5, 5, 0.9, 0.9, 2.0)] }),
// colosseum: huge, a wreck mound in the centre and two belts feeding the pit
arena({ id: 'colosseum', name: 'Grand Colosseum', theme: 'blue', half: 20, shape: 'round', pitW: 2.4,
saws: [saw(-8, -7, 0), saw(8, -7, 3), saw(-8, 7, 1.5), saw(8, 7, 4.5), saw(0, -11, 3.5)],
flames: [flame(-10, 2, 0), flame(10, 2, 2), flame(0, 13, 4), flame(0, -13, 1), flame(-13, -5, 3), flame(13, -5, 5)],
blocks: [block('scrap', 0, 0, 2.0, 1.6, 1.3), block('crate', -7, -2, 0.8, 0.8, 0.9), block('crate', 7, -2, 0.8, 0.8, 0.9)],
belts: [belt(-3.6, 6.5, 1.3, 3.0, 0, 1, 9), belt(3.6, 6.5, 1.3, 3.0, 0, 1, 9)] }),
];
let ARENA = RC.ARENAS[0];
RC.ARENA = ARENA;
RC.setArena = id => { ARENA = RC.ARENAS.find(a => a.id === id) || RC.ARENAS[0]; RC.ARENA = ARENA; return ARENA; };
const G = 22; // gravity
const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
const wrap = a => { while (a > Math.PI) a -= 2 * Math.PI; while (a < -Math.PI) a += 2 * Math.PI; return a; };
function rng(seed) { // mulberry32
let s = seed >>> 0 || 1;
return () => { s = (s + 0x6d2b79f5) >>> 0; let t = s; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
}
// Keep a point inside the arena outline. null = clear of the wall, otherwise the corrected
// spot plus the wall normal pointing INTO the arena. Used by the physics, the AI and the map.
function wallPush(A, x, z, rad) {
const lim = A.half - rad;
if (A.shape === 'round') {
const d = Math.hypot(x, z);
if (d <= lim || d < 1e-6) return null;
return { x: (x / d) * lim, z: (z / d) * lim, nx: -x / d, nz: -z / d };
}
let cx = x, cz = z, nx = 0, nz = 0, hit = false;
if (cx > lim) { cx = lim; nx = -1; hit = true; } else if (cx < -lim) { cx = -lim; nx = 1; hit = true; }
if (cz > lim) { cz = lim; nz = -1; hit = true; } else if (cz < -lim) { cz = -lim; nz = 1; hit = true; }
if (A.shape === 'octagon') { // four corner cuts: |x| + |z| <= dlim
const dlim = lim * 1.34, sx = Math.sign(cx) || 1, sz = Math.sign(cz) || 1;
const over = Math.abs(cx) + Math.abs(cz) - dlim;
if (over > 0) {
cx -= sx * over / 2; cz -= sz * over / 2;
nx = -sx * Math.SQRT1_2; nz = -sz * Math.SQRT1_2; hit = true;
}
}
if (!hit) return null;
const len = Math.hypot(nx, nz) || 1;
return { x: cx, z: cz, nx: nx / len, nz: nz / len };
}
// Is this spot inside a static obstacle? Returns the push-out normal, or null.
function blockPush(A, r, rad) {
for (const b of A.blocks) {
const dx = r.x - b.x, dz = r.z - b.z;
const ox = b.hw + rad - Math.abs(dx), oz = b.hd + rad - Math.abs(dz);
if (ox <= 0 || oz <= 0) continue;
if (ox < oz) { const sx = Math.sign(dx) || 1; r.x = b.x + sx * (b.hw + rad); return { nx: sx, nz: 0 }; }
const sz = Math.sign(dz) || 1; r.z = b.z + sz * (b.hd + rad); return { nx: 0, nz: sz };
}
return null;
}
RC.wallPush = wallPush;
RC.inBlock = (A, x, z, m = 0) => A.blocks.some(b => Math.abs(x - b.x) < b.hw + m && Math.abs(z - b.z) < b.hd + m);
const inRect = (o, x, z, m = 0) => Math.abs(x - o.x) < o.hw + m && Math.abs(z - o.z) < o.hd + m;
const hazardOn = (h, t) => ((t + h.phase) % h.period) < h.on;
RC.hazardOn = hazardOn;
function makeRobot(st, i, name, A) {
const p = A.starts[i];
return {
i, name, st, x: p.x, z: p.z, a: p.a, vx: 0, vz: 0, av: 0, y: 0, vy: 0,
hp: st.hp, maxHp: st.hp, dead: false, out: null, fall: 0,
flipped: 0, stun: 0, lifted: 0, grabbedBy: null, grab: 0,
shield: 0, gcd: 0, gUses: st.gadget && st.gadget.charges || 0,
lost: { armour: false, wheels: false, weapon: false, tower: false }, // parts torn off during the fight
onOil: false, // standing in a slick: less grip, lazy steering
g3: false, // last gadget button state (for one-shot gadgets)
cd: 0, cd2: 0, spin: 0, swing: 0, flipAnim: 0, boost: 0, sawOn: false, flameOn: false, gunOn: false,
turretA: p.a, dealt: 0, taken: 0, pressT: 0, craneT: 0, fx: 0,
};
}
function create(opt) {
const A = opt.arena ? (RC.ARENAS.find(a => a.id === opt.arena) || RC.ARENA) : RC.ARENA;
const s = {
arena: A, t: 0, left: A.duration, pitOpen: 0, rand: rng(opt.seed || 1),
robots: opt.robots.map((st, i) => makeRobot(st, i, (opt.names || [])[i] || 'Robot ' + (i + 1), A)),
rockets: [], mines: [], events: [], result: null, endIn: 0,
press: { st: 'idle', t: 0, y: 1 }, // idle | warn | slam | up ; y 1 = top, 0 = floor
crane: { st: 'idle', t: 0, who: -1, tx: 0, tz: 0, x: A.crane.x, z: A.crane.z },
pitAlarm: false,
};
return s;
}
// a part that broke off no longer works (and bare metal takes more damage)
const weaponOf = r => (r.lost.weapon ? null : r.st.weapon);
const towerOf = r => (r.lost.tower ? null : r.st.tower);
const reduceOf = r => (r.lost.armour ? r.st.reduce * 0.3 : r.st.reduce);
// battle damage: at these shares of health the next hit tears a part off (no randomness,
// so both sides of an online fight and the tests see exactly the same thing)
const BREAK_AT = [['armour', 0.6], ['wheels', 0.4], ['weapon', 0.25], ['tower', 0.18]];
function breakCheck(s, r) {
if (r.dead || r.out) return;
const f = r.hp / r.maxHp;
for (const [part, at] of BREAK_AT) {
if (r.lost[part] || f >= at) continue;
if (part === 'armour' && !r.st.armour) continue;
if (part === 'weapon' && !r.st.weapon) continue;
if (part === 'tower' && !r.st.tower) continue;
r.lost[part] = true;
if (part === 'weapon') { r.spin = 0; r.swing = 0; if (r.grab > 0) release(s, r, s.robots[1 - r.i]); }
s.events.push({ type: 'broke', who: r.i, part, x: r.x, z: r.z });
}
}
const fwd = r => [Math.cos(r.a), Math.sin(r.a)];
// enemy position in my frame: [forward, side]
function local(me, e) {
const dx = e.x - me.x, dz = e.z - me.z, c = Math.cos(me.a), sn = Math.sin(me.a);
return [dx * c + dz * sn, -dx * sn + dz * c];
}
function inZone(s, me, e, extra = 0) {
if (e.out || me.out) return false;
const w = weaponOf(me); if (!w) return false;
const [lf, ls] = local(me, e);
return lf > 0 && lf < me.st.len / 2 + w.reach + e.st.rad * 0.75 + extra && Math.abs(ls) < me.st.wid / 2 + e.st.rad * 0.55;
}
// where on the target the hit lands: front 0.8 (+chassis front armour), side 1, rear 1.3
function zoneMul(target, sx, sz) {
const ang = Math.abs(wrap(Math.atan2(sz - target.z, sx - target.x) - target.a));
if (ang < 0.8) return 0.8 * (1 - target.st.frontArmour);
if (ang > 2.35) return 1.3;
return 1;
}
function damage(s, target, amount, src, opt = {}) {
if (target.dead || target.out || amount <= 0 || s.result) return 0;
let d = amount * (1 - reduceOf(target) * (1 - (opt.pierce || 0)));
if (target.shield > 0 && target.st.gadget) d *= (1 - (target.st.gadget.reduce || 0.6)); // deflector field
const from = opt.from || src; // where the hit comes from (rockets: impact point)
if (from && !opt.noZone) d *= zoneMul(target, from.x, from.z);
d = Math.min(d, target.hp);
target.hp -= d; target.taken += d;
// judges: hazard damage counts for the opponent (pushing into traps pays off)
if (src) src.dealt += d; else s.robots[1 - target.i].dealt += d;
if (d >= 0.5 || opt.tick) s.events.push({ type: 'dmg', who: target.i, amount: d, x: target.x, z: target.z, tick: !!opt.tick, kind: opt.kind || 'hit' });
breakCheck(s, target);
if (target.hp <= 0.001) {
target.hp = 0; target.dead = true;
s.events.push({ type: 'ko', who: target.i, x: target.x, z: target.z });
finish(s, 1 - target.i, 'ko');
}
return d;
}
function finish(s, winner, reason) {
if (s.result) return;
s.result = { winner, reason };
s.endIn = 2.2;
s.events.push({ type: 'end', winner, reason });
}
function step(s, dt, inputs) {
if (s.endIn > 0) { s.endIn -= dt; }
s.t += dt;
if (!s.result) s.left = Math.max(0, s.arena.duration - s.t);
const R = s.robots;
// pit: opens at half time
if (s.left <= s.arena.pitOpensAt) {
if (!s.pitAlarm) { s.pitAlarm = true; s.events.push({ type: 'pit_open' }); }
s.pitOpen = Math.min(1, s.pitOpen + dt * 0.8);
}
for (let k = 0; k < 2; k++) drive(s, R[k], R[1 - k], (s.result ? null : inputs[k]) || {}, dt);
collide(s, R[0], R[1], dt);
for (let k = 0; k < 2; k++) gadgets(s, R[k], R[1 - k], (s.result ? null : inputs[k]) || {}, dt);
for (let k = 0; k < 2; k++) weapons(s, R[k], R[1 - k], (s.result ? null : inputs[k]) || {}, dt);
rockets(s, dt);
mines(s, dt);
hazards(s, dt);
if (!s.result && s.left <= 0) {
const [a, b] = R;
let w = a.dealt > b.dealt + 0.5 ? 0 : b.dealt > a.dealt + 0.5 ? 1 : -1;
if (w === -1) { const pa = a.hp / a.maxHp, pb = b.hp / b.maxHp; w = pa > pb + 0.01 ? 0 : pb > pa + 0.01 ? 1 : -1; }
finish(s, w, 'time');
}
}
function drive(s, r, e, inp, dt) {
r.cd = Math.max(0, r.cd - dt); r.cd2 = Math.max(0, r.cd2 - dt);
r.stun = Math.max(0, r.stun - dt); r.boost = Math.max(0, r.boost - dt); r.flipAnim = Math.max(0, r.flipAnim - dt);
if (r.out) { r.fall += dt; r.y -= dt * (1 + r.fall * 6); return; }
if (r.flipped > 0) {
r.flipped -= dt;
if (r.flipped <= 0) { r.flipped = 0; s.events.push({ type: 'righted', who: r.i, x: r.x, z: r.z }); }
}
const st = r.st, grounded = r.y <= 0.001 && r.vy <= 0;
const held = r.lifted > 0 || r.grabbedBy !== null;
const canDrive = !r.dead && !held && r.stun <= 0 && grounded && (r.flipped <= 0 || st.invertible);
const [fx, fz] = fwd(r);
let vf = r.vx * fx + r.vz * fz, vs = -r.vx * fz + r.vz * fx;
if (canDrive) {
const thr = clamp(inp.throttle || 0, -1, 1), trn = clamp(inp.turn || 0, -1, 1);
const hurt = r.lost.wheels ? 0.75 : 1; // burst wheel: slower, clumsier
const oil = r.onOil ? 0.55 : 1; // oil slick: hardly any steering
let top = st.top * hurt * (r.boost > 0 ? 1.65 : 1) * (r.grab > 0 ? 0.6 : 1) * (thr < 0 ? 0.75 : 1);
const target = thr * top, acc = st.accel * (r.lost.wheels ? 0.85 : 1) * (r.boost > 0 ? 2 : 1);
const braking = Math.abs(target) < Math.abs(vf) || target * vf < 0;
const rate = acc * (braking ? 1.6 : 1) * dt;
vf += clamp(target - vf, -rate, rate);
r.av += (trn * st.turn * (r.lost.wheels ? 0.8 : 1) * oil - r.av) * Math.min(1, 12 * dt);
} else {
r.av *= Math.max(0, 1 - 6 * dt);
}
if (grounded && !held) {
vs *= Math.max(0, 1 - st.grip * (r.onOil ? 0.3 : 1) * dt);
if (!canDrive) vf *= Math.max(0, 1 - 2.5 * dt);
else if (!(inp.throttle)) vf *= Math.max(0, 1 - 3 * dt);
}
r.vx = vf * fx - vs * fz; r.vz = vf * fz + vs * fx;
if (!held) { r.a = wrap(r.a + r.av * dt); r.x += r.vx * dt; r.z += r.vz * dt; }
// vertical (flips, crane drops)
if (!held && (r.y > 0 || r.vy > 0)) {
r.vy -= G * dt; r.y += r.vy * dt;
if (r.y <= 0) {
const hard = -r.vy;
r.y = 0; r.vy = 0;
if (hard > 4) {
s.events.push({ type: 'land', who: r.i, x: r.x, z: r.z, hard });
damage(s, r, (hard - 4) * 1.6, null, { kind: 'land' });
}
}
}
// spike walls (follow the arena outline: square, octagon or round)
const rad = st.rad * 0.9;
const w = wallPush(s.arena, r.x, r.z, rad);
if (w) {
r.x = w.x; r.z = w.z;
const vn = -(r.vx * w.nx + r.vz * w.nz); // speed straight into the wall
if (vn > 0) {
if (vn > 1.8 && !r.out) {
s.events.push({ type: 'wall', who: r.i, x: r.x, z: r.z, speed: vn });
damage(s, r, (vn - 1.8) * 4.5, null, { kind: 'spikes' });
// slammed into the spikes hard -> bounces up and can land on its back
if (vn > 3.5 && r.y < 0.05 && !r.st.invertible && !r.st.lowProfile && s.rand() < 0.3 * (r.st.topHeavy || 1)) { r.vy = Math.max(r.vy, 2); r.flipped = 2.2; r.av += (s.rand() - 0.5) * 6; }
}
r.vx += w.nx * vn * 1.45; r.vz += w.nz * vn * 1.45; // bounce (0.45 restitution)
}
}
// static obstacles: drive into one and you stop, hit it hard and it hurts
const bp = blockPush(s.arena, r, rad);
if (bp) {
const vn = -(r.vx * bp.nx + r.vz * bp.nz);
if (vn > 0) {
if (vn > 2.2 && !r.out) {
s.events.push({ type: 'wall', who: r.i, x: r.x, z: r.z, speed: vn });
damage(s, r, (vn - 2.2) * 2.6, null, { kind: 'ram' });
}
r.vx += bp.nx * vn * 1.3; r.vz += bp.nz * vn * 1.3;
}
}
}
function collide(s, a, b) {
if (a.out || b.out || Math.abs(a.y - b.y) > 0.5) return;
if (a.grabbedBy !== null || b.grabbedBy !== null) return;
let dx = b.x - a.x, dz = b.z - a.z, d = Math.hypot(dx, dz);
const minD = a.st.rad + b.st.rad;
if (d >= minD) return;
if (d < 1e-4) { dx = 1; dz = 0; d = 1; }
const nx = dx / d, nz = dz / d;
const ma = a.st.mass * a.st.push * (a.lifted || a.flipped ? 0.4 : 1), mb = b.st.mass * b.st.push * (b.lifted || b.flipped ? 0.4 : 1);
const over = minD - d, ia = 1 / ma, ib = 1 / mb, sum = ia + ib;
a.x -= nx * over * ia / sum; a.z -= nz * over * ia / sum;
b.x += nx * over * ib / sum; b.z += nz * over * ib / sum;
const vrel = (b.vx - a.vx) * nx + (b.vz - a.vz) * nz;
if (vrel >= 0) return;
const j = -(1 + 0.3) * vrel / sum;
a.vx -= j * ia * nx; a.vz -= j * ia * nz;
b.vx += j * ib * nx; b.vz += j * ib * nz;
const impact = -vrel;
if (impact < 1.2) return;
s.events.push({ type: 'bump', x: (a.x + b.x) / 2, z: (a.z + b.z) / 2, speed: impact });
// ramming: the robot whose front drives into the other does the damage
for (const [me, e, sx, sz] of [[a, b, nx, nz], [b, a, -nx, -nz]]) {
const [fx, fz] = fwd(me);
const facing = fx * sx + fz * sz;
const myV = me.vx * sx + me.vz * sz;
if (facing > 0.55 && myV > 1.2) {
const w = weaponOf(me);
const ram = w && w.type === 'wedge' ? w.ram : 1;
damage(s, e, (impact - 1) * 2.2 * ram, me, { kind: 'ram' });
// a wedge scoops lighter robots up; any hard enough ram can send one tumbling onto its roof
if (w && w.type === 'wedge' && e.y < 0.05 && !e.st.lowProfile) {
const light = Math.sqrt(me.st.mass / e.st.mass);
e.vy = Math.max(e.vy, 2.3 * light); e.stun = Math.max(e.stun, 0.25);
if (!e.st.invertible && !e.st.lowProfile && s.rand() < 0.3 * light * (e.st.topHeavy || 1) && impact > 3) { e.flipped = 2.2; e.av += (s.rand() - 0.5) * 6; }
} else if (impact > 4.5 && e.y < 0.05 && !e.st.lowProfile && !e.st.invertible) {
const light = Math.sqrt(me.st.mass / e.st.mass);
if (s.rand() < 0.22 * light * (e.st.topHeavy || 1)) { e.vy = Math.max(e.vy, 1.8 * light); e.flipped = 2.2; e.av += (s.rand() - 0.5) * 5; s.events.push({ type: 'flip', who: e.i, x: e.x, z: e.z }); }
}
}
}
}
function weapons(s, r, e, inp, dt) {
const ok = !r.dead && !r.out && r.lifted <= 0 && r.grabbedBy === null && r.flipped <= 0;
const w = weaponOf(r), [fx, fz] = fwd(r);
r.sawOn = false; r.flameOn = false; r.gunOn = false;
if (r.swing > 0) { // hammer comes down
r.swing -= dt;
if (r.swing <= 0 && ok) {
s.events.push({ type: 'smash', who: r.i, x: r.x + fx * (r.st.len / 2 + 0.6), z: r.z + fz * (r.st.len / 2 + 0.6) });
if (inZone(s, r, e, 0.15)) { damage(s, e, w.dmg, r, { pierce: 0.5, noZone: true, kind: 'smash' }); e.stun = Math.max(e.stun, 0.35); }
}
}
if (r.grab > 0) { // crusher holds the enemy
r.grab -= dt;
if (!ok || e.out || r.grab <= 0) release(s, r, e);
else {
const d = r.st.len / 2 + e.st.rad * 0.85;
e.x = r.x + fx * d; e.z = r.z + fz * d; e.vx = r.vx; e.vz = r.vz; e.y = 0;
damage(s, e, w.dps * dt, r, { kind: 'crush', tick: true });
}
}
if (w && ok) {
switch (w.type) {
case 'wedge':
if (inp.fire && r.cd <= 0) { r.boost = 0.8; r.cd = w.cd; s.events.push({ type: 'boost', who: r.i, x: r.x, z: r.z }); }
break;
case 'flipper':
if (inp.fire && r.cd <= 0) {
r.cd = w.cd; r.flipAnim = 0.35;
s.events.push({ type: 'flip', who: r.i, x: r.x + fx, z: r.z + fz });
if (inZone(s, r, e) && e.y < 0.2 && e.grabbedBy === null && e.lifted <= 0) {
const light = Math.sqrt(70 / e.st.mass);
e.vy = w.launch * light * (e.st.lowProfile ? 0.65 : 1);
e.vx += fx * 3 * light; e.vz += fz * 3 * light;
e.av += (s.rand() - 0.5) * 6;
damage(s, e, w.dmg, r, { kind: 'flip' });
if (!e.st.invertible && s.rand() < (e.st.lowProfile ? 0.25 : 0.45) * (e.st.topHeavy || 1)) e.flipped = 2.2;
}
}
break;
case 'hammer':
if (inp.fire && r.cd <= 0 && r.swing <= 0) { r.swing = 0.28; r.cd = w.cd; s.events.push({ type: 'swing', who: r.i }); }
break;
case 'saw':
if (inp.fire) {
r.sawOn = true;
if (inZone(s, r, e)) {
damage(s, e, w.dps * dt, r, { kind: 'saw', tick: true });
r.fx -= dt; if (r.fx <= 0) { r.fx = 0.06; s.events.push({ type: 'sparks', x: r.x + fx * (r.st.len / 2 + 0.3), z: r.z + fz * (r.st.len / 2 + 0.3), n: 6 }); }
}
}
break;
case 'crusher':
if (inp.fire && r.cd <= 0 && r.grab <= 0 && inZone(s, r, e) && e.grabbedBy === null && e.y < 0.2 && e.lifted <= 0) {
r.grab = w.hold; r.cd = w.cd; e.grabbedBy = r.i;
s.events.push({ type: 'grab', who: r.i, x: e.x, z: e.z });
}
break;
case 'lifter':
// scoop: tips the enemy up again and again (short cooldown) - made for shoving into the pit
if (inp.fire && r.cd <= 0) {
r.cd = w.cd; r.flipAnim = 0.3;
s.events.push({ type: 'flip', who: r.i, x: r.x + fx, z: r.z + fz });
if (inZone(s, r, e) && e.y < 0.2 && e.grabbedBy === null && e.lifted <= 0) {
const light = Math.sqrt(70 / e.st.mass);
e.vy = Math.max(e.vy, w.launch * light * 0.8);
e.vx += fx * 1.6 * light; e.vz += fz * 1.6 * light;
e.stun = Math.max(e.stun, 0.3); // wheels off the floor: no steering
damage(s, e, w.dmg, r, { kind: 'flip' });
}
}
break;
case 'vspinner':
// vertical disc: throws the enemy into the air instead of sideways
r.spin = inp.fire ? Math.min(1, r.spin + dt / w.spinup) : Math.max(0, r.spin - dt * 0.35);
if (r.spin > 0.25 && r.cd <= 0 && inZone(s, r, e)) {
const k = r.spin, light = Math.sqrt(70 / e.st.mass);
damage(s, e, w.dmg * k, r, { kind: 'spin' });
e.vy = Math.max(e.vy, 6.5 * k * light);
e.vx += fx * 1.5 * k; e.vz += fz * 1.5 * k; e.av += 7 * k;
if (!e.st.invertible && s.rand() < 0.5 * k * (e.st.topHeavy || 1)) e.flipped = 2.2;
r.spin *= 0.3; r.cd = 0.35;
s.events.push({ type: 'spinhit', who: r.i, x: (r.x + e.x) / 2, z: (r.z + e.z) / 2, k });
}
break;
case 'spinner':
r.spin = inp.fire ? Math.min(1, r.spin + dt / w.spinup) : Math.max(0, r.spin - dt * 0.35);
if (r.spin > 0.25 && r.cd <= 0 && inZone(s, r, e)) {
const k = r.spin, light = Math.sqrt(70 / e.st.mass);
damage(s, e, w.dmg * k, r, { kind: 'spin' });
e.vx += (fx * 5 + -fz * 2) * k * light; e.vz += (fz * 5 + fx * 2) * k * light;
e.vy = Math.max(e.vy, 2.5 * k * light); e.av += 5 * k;
r.vx -= fx * 2.5 * k; r.vz -= fz * 2.5 * k;
r.spin *= 0.35; r.cd = 0.3;
s.events.push({ type: 'spinhit', who: r.i, x: (r.x + e.x) / 2, z: (r.z + e.z) / 2, k });
}
break;
}
} else if (w && (w.type === 'spinner' || w.type === 'vspinner')) r.spin = Math.max(0, r.spin - dt * 0.5);
// turret is bolted facing forward: you aim it by turning the robot, it does not track the enemy
const t = towerOf(r);
if (!t) return;
r.turretA = r.a;
if (!ok || !inp.fire2) return;
const want = Math.atan2(e.z - r.z, e.x - r.x);
const dist = Math.hypot(e.x - r.x, e.z - r.z), aimed = Math.abs(wrap(want - r.turretA));
if (t.type === 'flamer') {
r.flameOn = true;
if (dist < t.range + e.st.rad && aimed < t.cone && !e.out) damage(s, e, t.dps * dt, r, { pierce: 0.5, noZone: true, kind: 'fire', tick: true });
} else if (t.type === 'minigun') {
r.gunOn = true; // always fires straight ahead, hits only what is in front
if (aimed < 0.25 && dist < t.range && !e.out) {
damage(s, e, t.dps * dt, r, { kind: 'bullet', tick: true });
r.fx -= dt; if (r.fx <= 0) { r.fx = 0.08; s.events.push({ type: 'sparks', x: e.x, z: e.z, n: 2, y: 0.4 }); }
}
} else if (t.type === 'zapper') {
r.gunOn = true; // short electric arc straight ahead
if (aimed < 0.4 && dist < t.range + e.st.rad && !e.out) {
damage(s, e, t.dps * dt, r, { pierce: 0.8, noZone: true, kind: 'bullet', tick: true });
e.stun = Math.max(e.stun, 0.12);
}
} else if (t.type === 'mortar' && r.cd2 <= 0) {
r.cd2 = t.cd;
// lobbed shell: flies a real arc and bursts where it lands
const flight = clamp(dist / 9, 0.5, 1.8);
s.rockets.push({ kind: 'mortar', owner: r.i, x: r.x, z: r.z, y: r.st.h + 0.5, a: r.turretA,
sp: Math.min(dist / flight, 14), vy: 9 * flight, t: 5, dmg: t.dmg, splash: t.splash });
s.events.push({ type: 'launch', who: r.i, x: r.x, z: r.z });
} else if ((t.type === 'rockets' || t.type === 'swarm') && r.cd2 <= 0) {
r.cd2 = t.cd;
const swarm = t.type === 'swarm', n = swarm ? (t.shots || 3) : 1;
for (let k = 0; k < n; k++) {
const spread = swarm ? (k - (n - 1) / 2) * 0.35 : 0;
s.rockets.push({ kind: 'rocket', owner: r.i, x: r.x, z: r.z, y: r.st.h + 0.45, a: r.turretA + spread,
t: swarm ? 8 : 5, sp: 6.5, max: swarm ? 13 : 11, turn: swarm ? 3.4 : 2.4, dmg: t.dmg });
}
s.events.push({ type: 'launch', who: r.i, x: r.x, z: r.z });
}
}
function release(s, r, e) {
r.grab = 0;
if (e.grabbedBy === r.i) { e.grabbedBy = null; }
}
// gadgets (input.fire3): one-shot abilities on a cooldown
function gadgets(s, r, e, inp, dt) {
r.gcd = Math.max(0, r.gcd - dt);
if (r.shield > 0) r.shield = Math.max(0, r.shield - dt);
const g = r.st.gadget;
const pressed = !!inp.fire3 && !r.g3; // rising edge: one action per press
r.g3 = !!inp.fire3;
if (!g || r.out || r.dead || !inp.fire3 || r.gcd > 0) return;
if (g.type === 'mines' && !pressed) return; // one mine per press, never a whole trail
const held = r.lifted > 0 || r.grabbedBy !== null;
switch (g.type) {
case 'righter': // flip back upright
if (r.flipped > 0) { r.flipped = 0; r.av += (s.rand() - 0.5) * 2; }
r.gcd = g.cd;
s.events.push({ type: 'righted', who: r.i, x: r.x, z: r.z });
break;
case 'turbo': // speed burst (reuses the boost mechanic)
if (held || r.flipped > 0 || r.stun > 0) return;
r.boost = Math.max(r.boost, g.dur); r.gcd = g.cd;
break;
case 'shield': // temporary damage field
r.shield = g.dur; r.gcd = g.cd;
break;
case 'repair': // one charge = flat heal
if (r.gUses <= 0) return;
r.hp = Math.min(r.maxHp, r.hp + r.maxHp * g.heal); r.gUses--; r.gcd = g.cd;
break;
case 'mines': { // drop a mine behind you
if (r.gUses <= 0) return;
const [mx, mz] = fwd(r);
const d = r.st.len / 2 + 0.9; // well behind, not under your own tail
r.gUses--; r.gcd = g.cd;
// own: the layer is safe for a moment - drive back over it later and it still gets you
s.mines.push({ x: r.x - mx * d, z: r.z - mz * d, owner: r.i, arm: g.arm, own: 3,
r: g.r, dmg: g.dmg, launch: g.launch });
s.events.push({ type: 'mine_drop', who: r.i, x: r.x - mx * d, z: r.z - mz * d });
break;
}
case 'emp': // stun the enemy if close
r.gcd = g.cd;
if (!e.out && Math.hypot(e.x - r.x, e.z - r.z) < g.range + e.st.rad) {
e.stun = Math.max(e.stun, g.stun); e.spin = 0; e.boost = 0; e.shield = 0;
s.events.push({ type: 'emp_hit', who: e.i, x: e.x, z: e.z });
}
break;
}
s.events.push({ type: 'gadget', who: r.i, g: g.type, x: r.x, z: r.z });
}
// should the AI / autofire helper fire the gadget now?
function wantGadget(s, me, e) {
const g = me.st.gadget;
if (!g || me.gcd > 0 || me.out || me.dead) return false;
const dist = e.out ? 999 : Math.hypot(e.x - me.x, e.z - me.z);
switch (g.type) {
case 'righter': return me.flipped > 0;
case 'turbo': return me.flipped <= 0 && me.stun <= 0 && dist > 2 && dist < 9;
case 'shield': return dist < 3 && me.flipped <= 0;
case 'repair': return me.gUses > 0 && me.hp < me.maxHp * 0.45;
case 'emp': return !e.out && dist < g.range * 0.9 && (e.boost > 0 || e.spin > 0.2 || dist < 2.5);
case 'mines': return me.gUses > 0 && dist > 1.4 && dist < 7; // drop one while they chase you
}
return false;
}
function mines(s, dt) {
for (let i = s.mines.length - 1; i >= 0; i--) {
const mn = s.mines[i];
if (mn.own > 0) mn.own -= dt;
if (mn.arm > 0) { mn.arm -= dt; continue; } // short delay so you can drive off it
for (const r of s.robots) {
if (r.out || r.dead || r.y > 0.35) continue;
if (mn.owner === r.i && mn.own > 0) continue; // grace period for whoever dropped it
if (Math.hypot(r.x - mn.x, r.z - mn.z) > mn.r + r.st.rad * 0.6) continue;
const light = Math.sqrt(70 / r.st.mass);
// your own mine pays the opponent, like any other hazard
damage(s, r, mn.dmg, mn.owner === r.i ? null : s.robots[mn.owner], { from: { x: mn.x, z: mn.z }, kind: 'mine' });
r.vy = Math.max(r.vy, mn.launch * light);
r.av += (s.rand() - 0.5) * 7;
if (!r.st.invertible && s.rand() < 0.5) r.flipped = 2.2;
s.events.push({ type: 'boom', x: mn.x, z: mn.z, y: 0.22, big: true });
s.mines.splice(i, 1);
break;
}
}
}
function rockets(s, dt) {
for (let i = s.rockets.length - 1; i >= 0; i--) {
const k = s.rockets[i], tgt = s.robots[1 - k.owner], own = s.robots[k.owner];
k.t -= dt;
const wall = () => Math.abs(k.x) > s.arena.half || Math.abs(k.z) > s.arena.half;
if (k.kind === 'mortar') {
// thrown in an arc, bursts on the floor with a blast radius
k.vy -= 17 * dt;
k.x += Math.cos(k.a) * k.sp * dt; k.z += Math.sin(k.a) * k.sp * dt; k.y += k.vy * dt;
const direct = !tgt.out && k.y < 0.9 && Math.hypot(tgt.x - k.x, tgt.z - k.z) < tgt.st.rad + 0.2;
if (direct || k.y <= 0.06 || wall() || k.t <= 0) {
s.events.push({ type: 'boom', x: k.x, z: k.z, y: Math.max(0.12, k.y), big: true });
if (!tgt.out) {
const d = Math.hypot(tgt.x - k.x, tgt.z - k.z);
if (d < k.splash) damage(s, tgt, k.dmg * (1 - (d / k.splash) * 0.6), own, { from: { x: k.x, z: k.z }, kind: 'rocket' });
}
s.rockets.splice(i, 1);
}
continue;
}
// seeker: turns after the target and can chase it across the arena
const want = Math.atan2(tgt.z - k.z, tgt.x - k.x);
if (!tgt.out) k.a = wrap(k.a + clamp(wrap(want - k.a), -(k.turn || 2.4) * dt, (k.turn || 2.4) * dt));
k.sp = Math.min(k.max || 11, k.sp + dt * 6);
k.x += Math.cos(k.a) * k.sp * dt; k.z += Math.sin(k.a) * k.sp * dt;
k.y += ((tgt.y + 0.35) - k.y) * Math.min(1, dt * 3);
const hit = !tgt.out && Math.hypot(tgt.x - k.x, tgt.z - k.z) < tgt.st.rad + 0.1;
if (hit || wall() || k.t <= 0) {
s.events.push({ type: 'boom', x: k.x, z: k.z, y: k.y });
if (hit) {
damage(s, tgt, k.dmg != null ? k.dmg : 17, own, { from: { x: k.x - Math.cos(k.a), z: k.z - Math.sin(k.a) }, kind: 'rocket' });
tgt.vx += Math.cos(k.a) * 1.6; tgt.vz += Math.sin(k.a) * 1.6;
}
s.rockets.splice(i, 1);
}
}
}
function hazards(s, dt) {
const A = s.arena, R = s.robots;
for (const r of R) {
if (r.out) continue;
const ground = r.y < 0.15 && r.lifted <= 0;
// floor saws
for (const h of A.saws) {
if (ground && hazardOn(h, s.t) && Math.hypot(r.x - h.x, r.z - h.z) < h.r + r.st.rad * 0.45) {
damage(s, r, 22 * dt, null, { pierce: 0.3, kind: 'saw', tick: true });
r.vx += (s.rand() - 0.5) * 18 * dt; r.vz += (s.rand() - 0.5) * 18 * dt;
r.fx -= dt; if (r.fx <= 0) { r.fx = 0.07; s.events.push({ type: 'sparks', x: r.x, z: r.z, n: 5 }); }
}
}
// flame grilles
for (const h of A.flames) if (ground && hazardOn(h, s.t) && inRect(h, r.x, r.z, r.st.rad * 0.3)) damage(s, r, 14 * dt, null, { pierce: 0.5, kind: 'fire', tick: true });
// conveyor belts drag everything that stands on them
for (const b of A.belts) if (ground && inRect(b, r.x, r.z, r.st.rad * 0.2)) { r.vx += b.ax * b.force * dt; r.vz += b.az * b.force * dt; }
// oil slicks (checked here, used by drive() on the next step)
r.onOil = ground && A.oil.some(o => Math.hypot(r.x - o.x, r.z - o.z) < o.r + r.st.rad * 0.4);
// wall rams shove anyone loitering in front of them
for (const m of A.rams) if (ground && hazardOn(m, s.t) && inRect(m, r.x, r.z, r.st.rad * 0.3)) {
r.vx += m.nx * 26 * dt; r.vz += m.nz * 26 * dt;
damage(s, r, 9 * dt, null, { kind: 'ram', tick: true });
}
// pit
if (s.pitOpen >= 1 && ground && r.grabbedBy === null && inRect(A.pit, r.x, r.z, -0.15)) {
r.out = 'pit'; r.vx *= 0.2; r.vz *= 0.2;
s.events.push({ type: 'pit', who: r.i, x: r.x, z: r.z });
const other = R[1 - r.i];
if (other.grabbedBy === r.i) other.grabbedBy = null;
if (r.grab > 0) release(s, r, other);
finish(s, 1 - r.i, 'pit');
}
r.pressT = inRect(A.press, r.x, r.z) && ground ? r.pressT + dt : 0;
r.craneT = inRect(A.crane, r.x, r.z) && ground && r.grabbedBy === null ? r.craneT + dt : 0;
}
// guardian 1: forge press - stay in its corner too long and it comes down
const p = s.press;
p.t -= dt;
if (p.st === 'idle' && R.some(r => r.pressT > 0.6 && !r.out)) { p.st = 'warn'; p.t = 0.7; s.events.push({ type: 'press_warn' }); }
else if (p.st === 'warn' && p.t <= 0) { p.st = 'slam'; p.t = 0.12; }
else if (p.st === 'slam') {
p.y = Math.max(0, p.t / 0.12);
if (p.t <= 0) {
p.y = 0; p.st = 'up'; p.t = 1.6;
s.events.push({ type: 'press', x: A.press.x, z: A.press.z });
for (const r of R) if (!r.out && inRect(A.press, r.x, r.z, r.st.rad * 0.3) && r.y < 0.4) {
damage(s, r, 28, null, { pierce: 0.5, kind: 'press' }); r.stun = Math.max(r.stun, 1.1);
}
}
} else if (p.st === 'up') { p.y = Math.min(1, 1 - p.t / 1.6 + 0.05); if (p.t <= 0) { p.st = 'idle'; p.y = 1; } }
// guardian 2: magnet crane - picks a robot up and drops it somewhere (maybe over the pit)
const c = s.crane;
c.t -= dt;
if (c.st === 'idle') {
c.x += (A.crane.x - c.x) * Math.min(1, dt * 2); c.z += (A.crane.z - c.z) * Math.min(1, dt * 2);
const r = R.find(q => q.craneT > 0.8 && !q.out && !q.dead && q.grab <= 0);
if (r && c.t <= 0) {
c.st = 'lift'; c.t = 0.6; c.who = r.i; r.lifted = 1; r.vx = r.vz = 0;
const toPit = s.pitOpen >= 1 && s.rand() < 0.35;
c.tx = toPit ? A.pit.x + (s.rand() - 0.5) * 3.5 : (s.rand() - 0.5) * 8;
c.tz = toPit ? A.pit.z - 2.4 : (s.rand() - 0.5) * 8 - 1;
s.events.push({ type: 'crane', who: r.i, x: r.x, z: r.z });
}
} else {
const r = R[c.who];
if (c.st === 'lift') { r.y = Math.min(1.7, r.y + dt * 3); c.x = r.x; c.z = r.z; if (c.t <= 0) { c.st = 'carry'; c.t = 1.4; } }
else if (c.st === 'carry') {
const k = Math.min(1, dt * 1.6);
r.x += (c.tx - r.x) * k; r.z += (c.tz - r.z) * k; r.a = wrap(r.a + dt * 1.5); c.x = r.x; c.z = r.z;
if (c.t <= 0) {
c.st = 'idle'; c.t = 2.5; r.lifted = 0; r.vy = 0; r.craneT = 0;
s.events.push({ type: 'drop', who: r.i, x: r.x, z: r.z });
damage(s, r, 8, null, { kind: 'drop' });
if (!r.st.invertible && s.rand() < 0.4) r.flipped = 2.2;
c.who = -1;
}
}
}
}
// What the HUD shows per equipped slot (no DOM, so Node tests can check it):
// p = share of the cooldown still running (1 = just used, 0 = ready), on = doing its thing right now,
// n = charges left where a part has them (repair kit).
function cooldowns(r) {
const out = [], w = r.st.weapon, t = r.st.tower, g = r.st.gadget;
const broke = k => (r.lost && r.lost[k]) || false;
const frac = (left, full) => (full > 0 ? clamp(left / full, 0, 1) : 0);
if (w) {
const e = { slot: 'weapon', id: w.id, p: 0, on: false, broke: broke('weapon') };
if (w.type === 'saw') e.on = !!r.sawOn;
else if (w.type === 'spinner' || w.type === 'vspinner') { e.p = 1 - clamp(r.spin, 0, 1); e.on = r.spin > 0.25; }
else {
e.p = frac(r.cd, w.cd);
e.on = (w.type === 'hammer' && r.swing > 0) || (w.type === 'crusher' && r.grab > 0) || (w.type === 'wedge' && r.boost > 0);
}
out.push(e);
}
if (t) {
const e = { slot: 'tower', id: t.id, p: 0, on: false, broke: broke('tower') };
if (t.type === 'rockets' || t.type === 'mortar' || t.type === 'swarm') e.p = frac(r.cd2, t.cd);
else e.on = t.type === 'flamer' ? !!r.flameOn : !!r.gunOn; // minigun + zapper
out.push(e);
}
if (g) out.push({ slot: 'gadget', id: g.id, p: frac(r.gcd, g.cd), on: r.shield > 0 || (g.type === 'turbo' && r.boost > 0), n: g.charges ? r.gUses : undefined });
return out;
}
RC.Combat = { create, step, inZone, local, wantGadget, cooldowns };
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
})(typeof window !== 'undefined' ? window : globalThis);