/* Robot Clash - opponent AI and league roster (no DOM). Needs parts.js, robot.js, combat.js. const brain = RC.AI.create({ style: 'brawler' | 'flanker' | 'pusher' | 'gunner' | 'dummy', skill: 0..1, seed }); input = brain.think(s, me, enemy, dt) -> { throttle, turn, fire, fire2 } RC.LEAGUE -> 8 opponents { id, name, style, skill, prize, build, paint } (style/description texts: lang.js) */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || {}); const A = () => RC.ARENA; 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 danger(s, x, z, me) { const ar = A(), m = 0.35; if (s.left < ar.pitOpensAt + 2 && Math.abs(x - ar.pit.x) < ar.pit.hw + me.st.rad && Math.abs(z - ar.pit.z) < ar.pit.hd + me.st.rad) return 3; for (const h of ar.saws) if (RC.hazardOn(h, s.t + 0.4) && Math.hypot(x - h.x, z - h.z) < h.r + me.st.rad) return 2; for (const h of ar.flames) if (RC.hazardOn(h, s.t + 0.4) && Math.abs(x - h.x) < h.hw + m && Math.abs(z - h.z) < h.hd + m) return 2; for (const g of [ar.press, ar.crane]) if (Math.abs(x - g.x) < g.hw + m && Math.abs(z - g.z) < g.hd + m) return 2; return 0; } function create(opt) { const style = opt.style || 'brawler', skill = clamp(opt.skill == null ? 0.5 : opt.skill, 0, 1); let seed = (opt.seed || 7) >>> 0; const rand = () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; }; let hold = { throttle: 0, turn: 0, fire: false, fire2: false }, wait = 0, stuck = 0, unstick = 0, contact = 0, retreat = 0, wob = rand() * 6; const react = 0.32 - skill * 0.26; // seconds between decisions function steer(me, tx, tz, speed = 1) { const want = Math.atan2(tz - me.z, tx - me.x), diff = wrap(want - me.a); const turn = clamp(diff * 2.2, -1, 1); const ad = Math.abs(diff); const throttle = ad < 0.5 ? speed : ad < 1.4 ? speed * 0.45 : ad > 2.6 && Math.hypot(tx - me.x, tz - me.z) < 2 ? -0.6 : 0.1; return { turn, throttle }; } function think(s, me, e, dt) { wait -= dt; const w = me.st.weapon, t = me.st.tower, dist = Math.hypot(e.x - me.x, e.z - me.z); // continuous weapon triggers (every tick, so reach checks are fresh) const inZ = RC.Combat.inZone(s, me, e); if (wait > 0) return withWeapons(hold, s, me, e, dist, inZ); wait = react * (0.6 + rand() * 0.8); if (style === 'dummy') { wob += 0.4; hold = { throttle: 0.35, turn: Math.sin(wob) * 0.6, fire: false, fire2: false }; return hold; } // skilled robots aim where the enemy will be const lead = skill * 0.35; let tx = e.x + e.vx * lead, tz = e.z + e.vz * lead; const err = (1 - skill) * 0.9; tx += (rand() - 0.5) * err; tz += (rand() - 0.5) * err; if (style === 'flanker' && dist > 2.2) { // go for side / rear const back = e.a + Math.PI + (rand() < 0.5 ? 0.9 : -0.9); tx = e.x + Math.cos(back) * 1.8; tz = e.z + Math.sin(back) * 1.8; } else if (style === 'pusher') { // line up enemy -> pit (or press corner) and shove const ar = A(), goal = s.pitOpen >= 1 ? ar.pit : ar.press; const gx = goal.x - e.x, gz = goal.z - e.z, gl = Math.hypot(gx, gz) || 1; const bx = e.x - gx / gl * 1.6, bz = e.z - gz / gl * 1.6; const lined = Math.hypot(me.x - bx, me.z - bz) < 1.0 || dist < me.st.rad + e.st.rad + 0.25; if (!lined && dist > 1.1) { tx = bx; tz = bz; } } else if (style === 'gunner' && t) { // keep distance, circle const want = t.type === 'flamer' ? 1.8 : 4.2; if (dist < want) { const away = Math.atan2(me.z - e.z, me.x - e.x) + 0.9; tx = e.x + Math.cos(away) * want; tz = e.z + Math.sin(away) * want; } } let cmd = steer(me, tx, tz, 0.75 + skill * 0.25); // avoid hazards in front (skilled robots look further) const look = 0.9 + skill * 1.0, fx = Math.cos(me.a), fz = Math.sin(me.a); const dng = danger(s, me.x + fx * look, me.z + fz * look, me); // enemy right in front and closer than the hazard: keep attacking (it lands in there, not me) const enemyAhead = dist < look + me.st.rad + e.st.rad && Math.abs(wrap(Math.atan2(e.z - me.z, e.x - me.x) - me.a)) < 0.6; if (dng && !enemyAhead && (dng === 3 || dist > 2.2) && rand() < 0.3 + skill * 0.6) { const l = danger(s, me.x + Math.cos(me.a - 0.9) * look, me.z + Math.sin(me.a - 0.9) * look, me); cmd = { throttle: dng === 3 ? -0.5 : 0.25, turn: l ? 1 : -1 }; } // unstick after pushing a wall for a while const sp = Math.hypot(me.vx, me.vz); stuck = cmd.throttle > 0.5 && sp < 0.4 ? stuck + react : 0; if (stuck > 1.2) { unstick = 0.7; stuck = 0; } if (unstick > 0) { unstick -= react; cmd = { throttle: -0.8, turn: 0.8 }; } // rammers: after a moment of shoving, back off and charge again (impact speed = damage) const rammer = !w || w.type === 'wedge'; const pushing = style === 'pusher' && s.pitOpen >= 1; contact = rammer && !pushing && dist < me.st.rad + e.st.rad + 0.3 ? contact + react : 0; if (contact > 0.45 && retreat <= 0 && rand() < 0.25 + skill * 0.7) { retreat = 0.35 + rand() * 0.3; contact = 0; } if (retreat > 0) { retreat -= react; cmd = { throttle: -1, turn: cmd.turn * 0.3 }; } hold = { throttle: cmd.throttle, turn: cmd.turn, fire: false, fire2: false }; return withWeapons(hold, s, me, e, dist, inZ); } function withWeapons(h, s, me, e, dist, inZ) { const w = me.st.weapon, t = me.st.tower; const out = { throttle: h.throttle, turn: h.turn, fire: false, fire2: false }; if (style === 'dummy') return out; if (w) { if (w.type === 'spinner') out.fire = dist < 6; else if (w.type === 'saw') out.fire = dist < 2.6; else if (w.type === 'wedge') out.fire = dist > 1.6 && dist < 4.5 && Math.abs(wrap(Math.atan2(e.z - me.z, e.x - me.x) - me.a)) < 0.25 && rand() < 0.08 + skill * 0.1; else out.fire = inZ && rand() < 0.25 + skill * 0.7; } if (t) out.fire2 = dist < t.range * (t.type === 'flamer' ? 1.05 : 0.9); return out; } return { think }; } const pal = (body, accent) => ({ body, accent }); RC.LEAGUE = [ { id: 'rustbucket', name: 'Rustbucket', style: 'brawler', skill: 0.15, prize: 180, build: { chassis: 'crate', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: null, weapon: 'wedge', tower: null }, paint: pal(0x8a5a3a, 0xd9a066) }, { id: 'tintoad', name: 'Tin Toad', style: 'pusher', skill: 0.25, prize: 240, build: { chassis: 'doorstop', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: null, weapon: 'wedge', tower: null }, paint: pal(0x5d8a3a, 0xc8e070) }, { id: 'buzzkill', name: 'Buzzkill', style: 'brawler', skill: 0.35, prize: 320, build: { chassis: 'crate', motor: 'hornet', wheels: 'rubber', battery: 'nicd', armour: 'steel', weapon: 'saw', tower: null }, paint: pal(0xd8b020, 0x202020) }, { id: 'flapjack', name: 'Flapjack', style: 'flanker', skill: 0.45, prize: 420, build: { chassis: 'doorstop', motor: 'hornet', wheels: 'bigtyre', battery: 'nicd', armour: 'steel', weapon: 'flipper', tower: null }, paint: pal(0x2a6fd8, 0xffffff) }, { id: 'pincer', name: 'Pincer', style: 'pusher', skill: 0.55, prize: 540, build: { chassis: 'bulwark', motor: 'bulldog', wheels: 'tracks', battery: 'nicd', armour: 'steel', weapon: 'crusher', tower: null }, paint: pal(0xc0392b, 0xf0c040) }, { id: 'thunderclap', name: 'Thunderclap', style: 'brawler', skill: 0.65, prize: 680, build: { chassis: 'bulwark', motor: 'hornet', wheels: 'bigtyre', battery: 'nicd', armour: 'titan', weapon: 'hammer', tower: 'flamer' }, paint: pal(0x6a3fb0, 0xffa020) }, { id: 'gravedigger', name: 'Gravedigger', style: 'gunner', skill: 0.8, prize: 860, build: { chassis: 'scarab', motor: 'hornet', wheels: 'tracks', battery: 'lipo', armour: 'titan', weapon: 'wedge', tower: 'rockets' }, paint: pal(0x2b2f36, 0x50e080) }, { id: 'overlord', name: 'Overlord', style: 'flanker', skill: 0.95, prize: 1200, build: { chassis: 'scarab', motor: 'comet', wheels: 'rubber', battery: 'lipo', armour: 'steel', weapon: 'spinner', tower: null }, paint: pal(0x101418, 0xff3030) }, ]; RC.DUMMY = { id: 'dummy', name: 'Dummy', style: 'dummy', skill: 0, prize: 0, build: { chassis: 'crate', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: 'steel', weapon: null, tower: null }, paint: pal(0x8c8c8c, 0xff7a1a) }; RC.AI = { create, danger }; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);