Files
Robot-Clash/game/ai.js
2026-11-18 15:18:38 +01:00

203 lines
15 KiB
JavaScript

/* 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 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 = s.arena || RC.ARENA, 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;
for (const r of ar.rams || []) if (RC.hazardOn(r, s.t + 0.4) && Math.abs(x - r.x) < r.hw + m && Math.abs(z - r.z) < r.hd + m) return 2;
// solid cover and slicks: drive round them, they are not deadly
for (const b of ar.blocks || []) if (Math.abs(x - b.x) < b.hw + me.st.rad && Math.abs(z - b.z) < b.hd + me.st.rad) return 1;
for (const o of ar.oil || []) if (Math.hypot(x - o.x, z - o.z) < o.r) return 1;
for (const mn of s.mines || []) if (mn.arm <= 0 && Math.hypot(x - mn.x, z - mn.z) < mn.r + me.st.rad) 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
// aim=true: orient so the weapon (which may sit on the back/side) faces the target, else aim the front
function steer(me, tx, tz, speed = 1, aim = false) {
const mA = aim ? (me.st.mountA || 0) : 0;
const want = wrap(Math.atan2(tz - me.z, tx - me.x) - mA), diff = wrap(want - me.a);
const turn = clamp(diff * 2.2, -1, 1);
const ad = Math.abs(diff);
const dir = Math.cos(mA) < -0.5 ? -1 : 1; // rear-mounted weapon: reverse to bring it onto the enemy
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) * dir;
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 = s.arena, 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) { // face the enemy (fixed turret), hold the range
tx = e.x; tz = e.z;
}
let cmd = steer(me, tx, tz, 0.75 + skill * 0.25, style !== 'pusher');
// gunner too close: back off but keep the nose (and the turret) on the enemy
if (style === 'gunner' && t && dist < ((t.type === 'flamer' || t.type === 'zapper') ? 1.4 : 2.8)) cmd = { turn: cmd.turn, throttle: -0.7 };
// 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); // includes blocks + oil now
// 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;
// only back off with room behind (no spike wall, no trap)
const half = s.arena.half - me.st.rad - 0.4;
const bx = me.x - fx * 1.4, bz = me.z - fz * 1.4;
const roomBehind = Math.abs(bx) < half && Math.abs(bz) < half && !danger(s, bx, bz, me);
if (contact > 0.45 && retreat <= 0 && roomBehind && rand() < 0.25 + skill * 0.7) { retreat = 0.35 + rand() * 0.3; contact = 0; }
if (retreat > 0 && !roomBehind) retreat = 0;
if (retreat > 0) { retreat -= react; cmd = { throttle: -0.85, 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' || w.type === 'vspinner') out.fire = dist < 6;
else if (w.type === 'lifter') out.fire = inZ && rand() < 0.5 + skill * 0.5;
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) {
const ahead = Math.abs(wrap(Math.atan2(e.z - me.z, e.x - me.x) - me.a));
const close = t.type === 'flamer' || t.type === 'zapper';
out.fire2 = dist < t.range * (close ? 1.05 : 0.9) && ahead < (close ? 0.5 : 0.3);
}
if (me.st.gadget) out.fire3 = RC.Combat.wantGadget(s, me, e);
return out;
}
return { think };
}
const pal = (body, accent) => ({ body, accent });
RC.LEAGUE = [
{ id: 'rustbucket', name: 'Rustbucket', style: 'brawler', skill: 0.1, prize: 250,
build: { chassis: 'cage', 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.2, prize: 330,
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.3, prize: 430,
build: { chassis: 'hexpod', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: null, weapon: 'saw', tower: null }, paint: pal(0xd8b020, 0x202020) },
{ id: 'flapjack', name: 'Flapjack', style: 'flanker', skill: 0.38, prize: 560,
build: { chassis: 'fang', motor: 'sparky', wheels: 'bigtyre', battery: 'lead', armour: null, weapon: 'flipper', tower: null }, paint: pal(0x2a6fd8, 0xffffff) },
{ id: 'pincer', name: 'Pincer', style: 'pusher', skill: 0.55, prize: 700,
build: { chassis: 'boiler', motor: 'bulldog', wheels: 'tracks', battery: 'lipo', armour: 'steel', weapon: 'crusher', tower: null, gadget: 'repair' }, paint: pal(0xc0392b, 0xf0c040) },
{ id: 'thunderclap', name: 'Thunderclap', style: 'brawler', skill: 0.65, prize: 880,
build: { chassis: 'plough', motor: 'hornet', wheels: 'bigtyre', battery: 'lipo', armour: 'titan', weapon: 'hammer', tower: 'flamer', gadget: 'shield' }, paint: pal(0x6a3fb0, 0xffa020) },
{ id: 'gravedigger', name: 'Gravedigger', style: 'brawler', skill: 0.8, prize: 1100,
build: { chassis: 'scarab', motor: 'hornet', wheels: 'tracks', battery: 'lipo', armour: 'titan', weapon: 'flipper', tower: 'rockets', gadget: 'turbo' }, paint: pal(0x2b2f36, 0x50e080) },
{ id: 'overlord', name: 'Overlord', style: 'flanker', skill: 0.95, prize: 1450,
build: { chassis: 'discus', motor: 'comet', wheels: 'rubber', battery: 'lipo', armour: 'steel', weapon: 'spinner', tower: null, gadget: 'righter' }, paint: pal(0x101418, 0xff3030) },
];
// --- lightweight league (max 60 kg): quick, fragile, cheap ---
RC.LEAGUE_LIGHT = [
{ id: 'tinflea', name: 'Tin Flea', style: 'brawler', skill: 0.1, prize: 200,
build: { chassis: 'cage', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: null, weapon: 'wedge', tower: null }, paint: pal(0x9aa3ad, 0xf2c230) },
{ id: 'sparrow', name: 'Sparrow', style: 'flanker', skill: 0.18, prize: 280,
build: { chassis: 'crate', motor: 'sparky', wheels: 'rubber', battery: 'lead', armour: null, weapon: 'lance', tower: null }, paint: pal(0x2e9c8f, 0xffffff) },
{ id: 'needle', name: 'Needle', style: 'brawler', skill: 0.3, prize: 370,
build: { chassis: 'cage', motor: 'hornet', wheels: 'omni', battery: 'nicd', armour: 'mesh', weapon: 'saw', tower: null }, paint: pal(0xd8452b, 0x15171b) },
{ id: 'jackrabbit', name: 'Jackrabbit', style: 'pusher', skill: 0.4, prize: 470,
build: { chassis: 'doorstop', motor: 'sparky', wheels: 'bigtyre', battery: 'nicd', armour: null, weapon: 'lifter', tower: null }, paint: pal(0x5fae3b, 0xf2c230) },
{ id: 'switchblade', name: 'Switchblade', style: 'flanker', skill: 0.52, prize: 590,
build: { chassis: 'hexpod', motor: 'hornet', wheels: 'omni', battery: 'lipo', armour: 'titan', weapon: 'wedge', tower: null }, paint: pal(0x6a3fb0, 0xe6e6e6) },
{ id: 'wasp', name: 'Wasp', style: 'brawler', skill: 0.64, prize: 720,
build: { chassis: 'fang', motor: 'hornet', wheels: 'rubber', battery: 'lipo', armour: null, weapon: 'lance', tower: null, gadget: 'righter' }, paint: pal(0xf2c230, 0x15171b) },
{ id: 'firefly', name: 'Firefly', style: 'gunner', skill: 0.78, prize: 900,
build: { chassis: 'crate', motor: 'comet', wheels: 'omni', battery: 'lipo', armour: null, weapon: 'saw', tower: 'flamer' }, paint: pal(0xff7a1a, 0x3a3d44) },
{ id: 'pinpoint', name: 'Pinpoint', style: 'flanker', skill: 0.92, prize: 1150,
build: { chassis: 'fang', motor: 'comet', wheels: 'rubber', battery: 'lipo', armour: null, weapon: 'mace', tower: null }, paint: pal(0x101418, 0x4fa8ff) },
];
// --- heavyweight league (max 130 kg): slow, armoured, brutal ---
RC.LEAGUE_HEAVY = [
{ id: 'bulldozer', name: 'Bulldozer', style: 'pusher', skill: 0.14, prize: 420,
build: { chassis: 'bulwark', motor: 'bulldog', wheels: 'tracks', battery: 'lead', armour: 'steel', weapon: 'wedge', tower: null }, paint: pal(0xf2c230, 0x15171b) },
{ id: 'anvil', name: 'Anvil', style: 'brawler', skill: 0.26, prize: 540,
build: { chassis: 'plough', motor: 'bulldog', wheels: 'studs', battery: 'nicd', armour: 'steel', weapon: 'lance', tower: null }, paint: pal(0x8c8c8c, 0xd8452b) },
{ id: 'boilerplate', name: 'Boilerplate', style: 'brawler', skill: 0.38, prize: 680,
build: { chassis: 'boiler', motor: 'hornet', wheels: 'drumwh', battery: 'nicd', armour: 'spiked', weapon: 'hammer', tower: null }, paint: pal(0x5a5048, 0xff7a1a) },
{ id: 'grinder', name: 'Grinder', style: 'brawler', skill: 0.5, prize: 850,
build: { chassis: 'discus', motor: 'comet', wheels: 'tracks', battery: 'lipo', armour: 'ceramic', weapon: 'drum', tower: null }, paint: pal(0x3a3d44, 0x6dff9e) },
{ id: 'juggernaut', name: 'Juggernaut', style: 'pusher', skill: 0.62, prize: 1050,
build: { chassis: 'bulwark', motor: 'bulldog', wheels: 'tracks', battery: 'liion', armour: 'hardox', weapon: 'crusher', tower: null, gadget: 'repair' }, paint: pal(0xc0392b, 0xf2c230) },
{ id: 'warhound', name: 'Warhound', style: 'gunner', skill: 0.74, prize: 1300,
build: { chassis: 'plough', motor: 'hornet', wheels: 'studs', battery: 'liion', armour: 'ceramic', weapon: 'mace', tower: 'flamer', gadget: 'shield' }, paint: pal(0x6a3fb0, 0xffa020) },
{ id: 'colossus', name: 'Colossus', style: 'brawler', skill: 0.86, prize: 1600,
build: { chassis: 'scarab', motor: 'comet', wheels: 'tracks', battery: 'supercap', armour: 'hardox', weapon: 'spinner', tower: 'zapper' }, paint: pal(0x2b2f36, 0x4fa8ff) },
{ id: 'doomsday', name: 'Doomsday', style: 'flanker', skill: 0.97, prize: 2000,
build: { chassis: 'plough', motor: 'bulldog', wheels: 'tracks', battery: 'supercap', armour: 'hardox', weapon: 'vdisc', tower: 'swarm', gadget: 'mines' }, paint: pal(0x101418, 0xff3030) },
];
RC.LEAGUES = { light: RC.LEAGUE_LIGHT, medium: RC.LEAGUE, heavy: RC.LEAGUE_HEAVY };
// Swap the active class: RC.LEAGUE and the weight limit follow, like RC.setArena does for arenas.
RC.setClass = id => {
const c = RC.CLASSES.find(x => x.id === id) || RC.CLASSES[1];
RC.CLASS = c.id;
RC.WEIGHT_LIMIT = c.max;
RC.LEAGUE = RC.LEAGUES[c.id];
return c;
};
RC.CLASS = 'medium';
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);