/* 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) }; }; 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) { 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; } 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'), we = part(build, 'weapon'), 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) { const p = part(build, slot); if (p) value += p.cost; } 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, 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: we, 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);