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

105 lines
5.8 KiB
JavaScript

/* Robot Clash - workshop rules and robot stats from parts (no DOM). Needs parts.js.
RC.validate(build) -> { ok, errors: ['need_chassis' | 'overweight' | 'no_power' ...], weight, power, supply }
RC.stats(build) -> combat stats { hp, reduce, mass, accel, top, turn, grip, push, len, wid, h, rad,
weapon, tower, invertible, lowProfile, frontArmour, value }
build = { chassis: id, motor: id, wheels: id, battery: id, armour: id|null, weapon: id|null, tower: id|null } */
(function (root) {
'use strict';
const RC = (root.RC = root.RC || (typeof require === 'function' ? require('./parts.js') : {}));
const part = (b, slot) => (b && b[slot] ? RC.PARTS[b[slot]] || null : null);
const clamp = (v, a, c) => (v < a ? a : v > c ? c : v);
// per-robot proportions: height / width / length factor (default 1). Bigger = heavier + tougher + slower.
RC.bodyOf = b => {
const d = (b && b.body) || {};
return { hx: clamp(+d.hx || 1, 0.7, 1.6), wx: clamp(+d.wx || 1, 0.8, 1.4), lx: clamp(+d.lx || 1, 0.8, 1.3) };
};
// where the main weapon is bolted on: front (default), back, left or right.
// a = angle offset from the robot's forward (+x); axis = which body half-extent reaches that way.
RC.MOUNTS = { front: { a: 0, axis: 'len' }, back: { a: Math.PI, axis: 'len' }, left: { a: Math.PI / 2, axis: 'wid' }, right: { a: -Math.PI / 2, axis: 'wid' } };
RC.mountOf = b => {
const d = (b && b.mount && b.mount.weapon) || {};
const side = RC.MOUNTS[d.side] ? d.side : 'front';
return { side, a: RC.MOUNTS[side].a, axis: RC.MOUNTS[side].axis, up: clamp(+d.up || 0, -0.3, 0.6) };
};
RC.MAX_WEAPONS = 3;
// a robot carries up to 3 weapons (same id allowed), each with its own side + height.
// new builds use build.weapons = [{id,side,up}]; old builds (single build.weapon + build.mount) are read too.
RC.weaponsOf = b => {
let list = [];
if (b && Array.isArray(b.weapons)) list = b.weapons;
else if (b && b.weapon) { const m = b.mount && b.mount.weapon; list = [{ id: b.weapon, side: m && m.side, up: m && m.up }]; }
const out = [];
for (const w of list) {
const p = w && RC.PARTS[w.id];
if (!p || p.slot !== 'weapon') continue;
const side = RC.MOUNTS[w.side] ? w.side : 'front';
out.push({ id: w.id, part: p, side, up: clamp(+w.up || 0, -0.3, 0.6), a: RC.MOUNTS[side].a, axis: RC.MOUNTS[side].axis });
if (out.length >= RC.MAX_WEAPONS) break;
}
return out;
};
RC.weaponCount = b => (b && Array.isArray(b.weapons) ? b.weapons.filter(w => w && RC.PARTS[w.id] && RC.PARTS[w.id].slot === 'weapon').length : (b && b.weapon ? 1 : 0));
const sizeFactor = bo => bo.hx * bo.wx * bo.lx;
RC.HP_SCALE = 3; // catalogue hp x this = hit points in the arena
RC.validate = build => {
const errors = [];
let weight = 0, power = 0, supply = 0;
for (const slot of RC.SLOTS) {
if (slot === 'weapon') continue; // weapons are a list now, summed below
const p = part(build, slot);
if (!p) { if (RC.REQUIRED[slot]) errors.push('need_' + slot); continue; }
if (p.slot !== slot) { errors.push('bad_' + slot); continue; }
weight += slot === 'chassis' ? Math.round(p.weight * (0.4 + 0.6 * sizeFactor(RC.bodyOf(build)))) : p.weight;
if (slot === 'battery') supply += p.power; else power += p.power || 0;
}
for (const inst of RC.weaponsOf(build)) { weight += inst.part.weight; power += inst.part.power || 0; }
if (RC.weaponCount(build) > RC.MAX_WEAPONS) errors.push('too_many_weapons');
if (weight > RC.WEIGHT_LIMIT) errors.push('overweight');
if (supply && power > supply) errors.push('no_power');
return { ok: errors.length === 0, errors, weight, power, supply };
};
RC.stats = build => {
const ch = part(build, 'chassis'), mo = part(build, 'motor'), wh = part(build, 'wheels');
const ar = part(build, 'armour'), to = part(build, 'tower'), gd = part(build, 'gadget');
const v = RC.validate(build);
const mass = Math.max(30, v.weight);
let value = 0;
for (const slot of RC.SLOTS) { if (slot === 'weapon') continue; const p = part(build, slot); if (p) value += p.cost; }
const weps = RC.weaponsOf(build);
for (const w of weps) value += w.part.cost;
// per-weapon mount info; combat/render iterate this list, lead weapon kept on st.weapon/mountA for compat
const weapons = weps.map(w => ({ id: w.id, part: w.part, side: w.side, mountA: w.a, mountAxis: w.axis, mountUp: w.up }));
const lead = weapons[0] || null;
const bo = RC.bodyOf(build), size = sizeFactor(bo);
const len = (ch ? ch.len : 1.3) * bo.lx, wid = (ch ? ch.wid : 1.0) * bo.wx;
return {
body: bo,
weapons,
mountA: lead ? lead.mountA : 0, mountAxis: lead ? lead.mountAxis : 'len', mountUp: lead ? lead.mountUp : 0,
topHeavy: bo.hx / ((bo.wx + bo.lx) / 2), // tall + narrow = easier to tip over
hp: (Math.round((ch ? ch.hp : 80) * (0.6 + 0.4 * size)) + (ar ? ar.hp : 0)) * RC.HP_SCALE,
reduce: ar ? ar.reduce : 0,
mass,
accel: (mo ? mo.force : 500) / mass * (wh ? Math.min(1.2, wh.grip / 8) : 1),
top: (mo ? mo.speed : 3.5) * (wh ? wh.speedMul : 1) * (1.12 - mass / 900),
turn: (wh ? wh.turn : 2.5) * (1.15 - mass / 650),
grip: wh ? wh.grip : 6,
push: wh ? wh.push : 1,
len, wid, h: (ch ? ch.h : 0.42) * bo.hx,
rad: Math.hypot(len, wid) / 2 * 0.95, // body corners - bigger = robots keep more distance, less clipping
weapon: lead ? lead.part : null, tower: to, gadget: gd, armour: ar,
invertible: !!(wh && wh.invertible),
lowProfile: !!(ch && ch.lowProfile),
frontArmour: ch && ch.frontArmour ? ch.frontArmour : 0,
value,
};
};
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
})(typeof window !== 'undefined' ? window : globalThis);