/* 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 || {}); const ARENA = (RC.ARENA = { half: 8, // floor from -8..8 duration: 120, // seconds pitOpensAt: 60, // seconds left when the pit opens pit: { x: 0, z: 4.6, hw: 1.35, hd: 1.35 }, saws: [{ x: -3.2, z: -3.4, r: 0.95, period: 6, on: 2.2, phase: 0 }, { x: 3.2, z: -3.4, r: 0.95, period: 6, on: 2.2, phase: 3 }], flames: [{ x: -2.7, z: 3.3, hw: 0.9, hd: 0.9, period: 7, on: 1.8, phase: 1 }, { x: 2.7, z: 3.3, hw: 0.9, hd: 0.9, period: 7, on: 1.8, phase: 4.5 }], press: { x: 6.25, z: -6.25, hw: 1.6, hd: 1.6 }, // guardian 1: forge press (corner) crane: { x: -6.25, z: 6.25, hw: 1.6, hd: 1.6 }, // guardian 2: magnet crane (corner) starts: [{ x: -5, z: 1.5, a: 0 }, { x: 5, z: 1.5, a: Math.PI }], }); 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; }; } 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) { const p = ARENA.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, 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 s = { t: 0, left: ARENA.duration, pitOpen: 0, rand: rng(opt.seed || 1), robots: opt.robots.map((st, i) => makeRobot(st, i, (opt.names || [])[i] || 'Robot ' + (i + 1))), rockets: [], 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: ARENA.crane.x, z: ARENA.crane.z }, pitAlarm: false, }; return s; } 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 = me.st.weapon; 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 - target.st.reduce * (1 - (opt.pierce || 0))); 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' }); 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, ARENA.duration - s.t); const R = s.robots; // pit: opens at half time if (s.left <= 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++) weapons(s, R[k], R[1 - k], (s.result ? null : inputs[k]) || {}, dt); rockets(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); let top = st.top * (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.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.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 * 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 const lim = ARENA.half - st.rad * 0.9; for (const ax of ['x', 'z']) { const vk = ax === 'x' ? 'vx' : 'vz'; if (Math.abs(r[ax]) > lim) { const sgn = Math.sign(r[ax]), vn = r[vk] * sgn; r[ax] = sgn * lim; 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' }); } r[vk] = -r[vk] * 0.45; } } } } 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 = me.st.weapon; const ram = w && w.type === 'wedge' ? w.ram : 1; damage(s, e, (impact - 1) * 2.2 * ram, me, { kind: 'ram' }); if (w && w.type === 'wedge' && e.y < 0.05 && !e.st.lowProfile) { e.vy = 2.6; e.stun = Math.max(e.stun, 0.25); } } } } function weapons(s, r, e, inp, dt) { const ok = !r.dead && !r.out && r.lifted <= 0 && r.grabbedBy === null && r.flipped <= 0; const w = r.st.weapon, [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.3 : 0.55)) e.flipped = 3.5; } } 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 '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') r.spin = Math.max(0, r.spin - dt * 0.5); // turret: turns towards the enemy by itself const t = r.st.tower; if (!t) return; const want = Math.atan2(e.z - r.z, e.x - r.x), diff = wrap(want - r.turretA); r.turretA = wrap(r.turretA + clamp(diff, -3.2 * dt, 3.2 * dt)); if (!ok || !inp.fire2) return; 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 = aimed < 0.25; if (r.gunOn && 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 === 'rockets' && r.cd2 <= 0) { r.cd2 = t.cd; s.rockets.push({ owner: r.i, x: r.x, z: r.z, y: r.st.h + 0.45, a: r.turretA, t: 3.2, sp: 6.5 }); 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; } } 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 want = Math.atan2(tgt.z - k.z, tgt.x - k.x); if (!tgt.out) k.a = wrap(k.a + clamp(wrap(want - k.a), -2.4 * dt, 2.4 * dt)); k.sp = Math.min(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; const wall = Math.abs(k.x) > ARENA.half || Math.abs(k.z) > ARENA.half; 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, own.st.tower.dmg, 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 = 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 }); // 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.5) r.flipped = 3.5; c.who = -1; } } } } RC.Combat = { create, step, inZone, local }; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);