big update

This commit is contained in:
2026-11-18 15:18:38 +01:00
parent e0496fa089
commit 64c5b65543
39 changed files with 1938 additions and 367 deletions

View File

@@ -141,6 +141,9 @@
g3: false, // last gadget button state (for one-shot gadgets)
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,
// per-weapon runtime (one entry per mounted weapon) + per-weapon broken flag
W: (st.weapons || []).map(() => ({ cd: 0, spin: 0, swing: 0, grab: 0, fx: 0, flipAnim: 0, sawOn: false, fire: 0 })),
lostW: (st.weapons || []).map(() => false),
};
}
@@ -158,38 +161,58 @@
}
// a part that broke off no longer works (and bare metal takes more damage)
const weaponOf = r => (r.lost.weapon ? null : r.st.weapon);
const weaponOf = r => (r.lostW && r.lostW[0] ? null : (r.st.weapons && r.st.weapons[0] && r.st.weapons[0].part) || null);
const towerOf = r => (r.lost.tower ? null : r.st.tower);
const reduceOf = r => (r.lost.armour ? r.st.reduce * 0.3 : r.st.reduce);
// battle damage: at these shares of health the next hit tears a part off (no randomness,
// so both sides of an online fight and the tests see exactly the same thing)
const BREAK_AT = [['armour', 0.6], ['wheels', 0.4], ['weapon', 0.25], ['tower', 0.18]];
const BREAK_AT = [['armour', 0.6], ['wheels', 0.4], ['tower', 0.18]];
const WEAPON_BREAK = [0.25, 0.17, 0.11]; // weapons tear off one by one as health drops
function breakCheck(s, r) {
if (r.dead || r.out) return;
const f = r.hp / r.maxHp;
for (const [part, at] of BREAK_AT) {
if (r.lost[part] || f >= at) continue;
if (part === 'armour' && !r.st.armour) continue;
if (part === 'weapon' && !r.st.weapon) continue;
if (part === 'tower' && !r.st.tower) continue;
r.lost[part] = true;
if (part === 'weapon') { r.spin = 0; r.swing = 0; if (r.grab > 0) release(s, r, s.robots[1 - r.i]); }
s.events.push({ type: 'broke', who: r.i, part, x: r.x, z: r.z });
}
const list = r.st.weapons || [];
for (let i = 0; i < list.length; i++) {
if (r.lostW[i] || f >= (WEAPON_BREAK[i] || 0.1)) continue;
r.lostW[i] = true;
const W = r.W[i]; W.spin = 0; W.swing = 0; if (W.grab > 0) release(s, r, s.robots[1 - r.i], i);
s.events.push({ type: 'broke', who: r.i, part: 'weapon' + i, x: r.x, z: r.z });
}
}
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);
// direction weapon i points: robot forward turned by that weapon's mount angle (front 0, back pi, sides +-pi/2)
const wdirI = (r, i) => { const m = r.st.weapons[i], a = r.a + (m ? m.mountA : 0); return [Math.cos(a), Math.sin(a)]; };
const alongI = (r, i) => { const m = r.st.weapons[i]; return ((m && m.mountAxis === 'wid') ? r.st.wid : r.st.len) / 2; }; // body half-extent toward weapon i
const wdir = r => wdirI(r, 0); // lead weapon (ram bonus, compat)
// enemy position in my frame about angle `a`: [along, side]
function localAt(me, e, a) {
const dx = e.x - me.x, dz = e.z - me.z, c = Math.cos(a), sn = Math.sin(a);
return [dx * c + dz * sn, -dx * sn + dz * c];
}
function inZone(s, me, e, extra = 0) {
function local(me, e) { return localAt(me, e, me.a); }
// is the enemy inside weapon i's reach (reach runs along that weapon's mounted axis)?
function inZoneI(s, me, e, i, extra = 0) {
if (e.out || me.out) return false;
const w = weaponOf(me); 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;
const m = me.st.weapons[i]; if (!m || (me.lostW && me.lostW[i])) return false;
const [lf, ls] = localAt(me, e, me.a + m.mountA);
const along = (m.mountAxis === 'wid' ? me.st.wid : me.st.len) / 2;
const perp = (m.mountAxis === 'wid' ? me.st.len : me.st.wid) / 2;
return lf > 0 && lf < along + m.part.reach + e.st.rad * 0.75 + extra && Math.abs(ls) < perp + e.st.rad * 0.55;
}
// any weapon in range (AI fire decision, player weapon-assist)
function inZone(s, me, e, extra = 0) {
const list = me.st.weapons || [];
for (let i = 0; i < list.length; i++) if (inZoneI(s, me, e, i, extra)) return true;
return false;
}
// where on the target the hit lands: front 0.8 (+chassis front armour), side 1, rear 1.3
@@ -256,8 +279,8 @@
}
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);
r.cd2 = Math.max(0, r.cd2 - dt);
r.stun = Math.max(0, r.stun - dt); r.boost = Math.max(0, r.boost - dt);
if (r.out) { r.fall += dt; r.y -= dt * (1 + r.fall * 6); return; }
if (r.flipped > 0) {
r.flipped -= dt;
@@ -272,7 +295,7 @@
const thr = clamp(inp.throttle || 0, -1, 1), trn = clamp(inp.turn || 0, -1, 1);
const hurt = r.lost.wheels ? 0.75 : 1; // burst wheel: slower, clumsier
const oil = r.onOil ? 0.55 : 1; // oil slick: hardly any steering
let top = st.top * hurt * (r.boost > 0 ? 1.65 : 1) * (r.grab > 0 ? 0.6 : 1) * (thr < 0 ? 0.75 : 1);
let top = st.top * hurt * (r.boost > 0 ? 1.65 : 1) * (grabbing(r) ? 0.6 : 1) * (thr < 0 ? 0.75 : 1);
const target = thr * top, acc = st.accel * (r.lost.wheels ? 0.85 : 1) * (r.boost > 0 ? 2 : 1);
const braking = Math.abs(target) < Math.abs(vf) || target * vf < 0;
const rate = acc * (braking ? 1.6 : 1) * dt;
@@ -352,9 +375,9 @@
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
// ramming: the robot whose weapon side 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 [fx, fz] = wdir(me);
const facing = fx * sx + fz * sz;
const myV = me.vx * sx + me.vz * sz;
if (facing > 0.55 && myV > 1.2) {
@@ -374,36 +397,49 @@
}
}
// each mounted weapon runs its own logic with its own state (r.W[i]) and mount direction
function weapons(s, r, e, inp, dt) {
r.sawOn = false; // aggregate for sound; per-weapon render flags on r.W[i]
const list = r.st.weapons || [];
for (let i = 0; i < list.length; i++) meleeWeapon(s, r, e, inp, dt, i);
towerFire(s, r, e, inp, dt);
}
function meleeWeapon(s, r, e, inp, dt, i) {
const W = r.W[i];
if (r.lostW[i]) { W.spin = 0; W.sawOn = false; return; }
const inst = r.st.weapons[i], w = inst.part;
const ok = !r.dead && !r.out && r.lifted <= 0 && r.grabbedBy === null && r.flipped <= 0;
const w = weaponOf(r), [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); }
const [fx, fz] = wdirI(r, i), reach0 = alongI(r, i);
const inZ = (ex = 0) => inZoneI(s, r, e, i, ex);
W.cd = Math.max(0, W.cd - dt); W.flipAnim = Math.max(0, W.flipAnim - dt); W.fire = Math.max(0, W.fire - dt);
W.sawOn = false;
if (W.swing > 0) { // hammer comes down
W.swing -= dt;
if (W.swing <= 0 && ok) {
s.events.push({ type: 'smash', who: r.i, x: r.x + fx * (reach0 + 0.6), z: r.z + fz * (reach0 + 0.6) });
if (inZ(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);
if (W.grab > 0) { // crusher holds the enemy
W.grab -= dt;
if (!ok || e.out || W.grab <= 0) release(s, r, e, i);
else {
const d = r.st.len / 2 + e.st.rad * 0.85;
const d = reach0 + 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) {
if (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 }); }
if (inp.fire && W.cd <= 0) { r.boost = 0.8; W.cd = w.cd; W.fire = 0.8; 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;
if (inp.fire && W.cd <= 0) {
W.cd = w.cd; W.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) {
if (inZ() && 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;
@@ -414,29 +450,29 @@
}
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 }); }
if (inp.fire && W.cd <= 0 && W.swing <= 0) { W.swing = 0.28; W.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)) {
W.sawOn = true; r.sawOn = true;
if (inZ()) {
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 }); }
W.fx -= dt; if (W.fx <= 0) { W.fx = 0.06; s.events.push({ type: 'sparks', x: r.x + fx * (reach0 + 0.3), z: r.z + fz * (reach0 + 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;
if (inp.fire && W.cd <= 0 && W.grab <= 0 && inZ() && e.grabbedBy === null && e.y < 0.2 && e.lifted <= 0) {
W.grab = w.hold; W.cd = w.cd; e.grabbedBy = r.i;
s.events.push({ type: 'grab', who: r.i, x: e.x, z: e.z });
}
break;
case 'lifter':
// scoop: tips the enemy up again and again (short cooldown) - made for shoving into the pit
if (inp.fire && r.cd <= 0) {
r.cd = w.cd; r.flipAnim = 0.3;
if (inp.fire && W.cd <= 0) {
W.cd = w.cd; W.flipAnim = 0.3;
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) {
if (inZ() && e.y < 0.2 && e.grabbedBy === null && e.lifted <= 0) {
const light = Math.sqrt(70 / e.st.mass);
e.vy = Math.max(e.vy, w.launch * light * 0.8);
e.vx += fx * 1.6 * light; e.vz += fz * 1.6 * light;
@@ -447,32 +483,36 @@
break;
case 'vspinner':
// vertical disc: throws the enemy into the air instead of sideways
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);
W.spin = inp.fire ? Math.min(1, W.spin + dt / w.spinup) : Math.max(0, W.spin - dt * 0.35);
if (W.spin > 0.25 && W.cd <= 0 && inZ()) {
const k = W.spin, light = Math.sqrt(70 / e.st.mass);
damage(s, e, w.dmg * k, r, { kind: 'spin' });
e.vy = Math.max(e.vy, 6.5 * k * light);
e.vx += fx * 1.5 * k; e.vz += fz * 1.5 * k; e.av += 7 * k;
if (!e.st.invertible && s.rand() < 0.5 * k * (e.st.topHeavy || 1)) e.flipped = 2.2;
r.spin *= 0.3; r.cd = 0.35;
W.spin *= 0.3; W.cd = 0.35;
s.events.push({ type: 'spinhit', who: r.i, x: (r.x + e.x) / 2, z: (r.z + e.z) / 2, k });
}
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);
W.spin = inp.fire ? Math.min(1, W.spin + dt / w.spinup) : Math.max(0, W.spin - dt * 0.35);
if (W.spin > 0.25 && W.cd <= 0 && inZ()) {
const k = W.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;
W.spin *= 0.35; W.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' || w.type === 'vspinner')) r.spin = Math.max(0, r.spin - dt * 0.5);
} else if (w.type === 'spinner' || w.type === 'vspinner') W.spin = Math.max(0, W.spin - dt * 0.5);
}
function towerFire(s, r, e, inp, dt) {
r.flameOn = false; r.gunOn = false;
const ok = !r.dead && !r.out && r.lifted <= 0 && r.grabbedBy === null && r.flipped <= 0;
// turret is bolted facing forward: you aim it by turning the robot, it does not track the enemy
const t = towerOf(r);
if (!t) return;
@@ -514,10 +554,11 @@
}
}
function release(s, r, e) {
r.grab = 0;
if (e.grabbedBy === r.i) { e.grabbedBy = null; }
function release(s, r, e, i) {
if (i == null) { for (const W of r.W) W.grab = 0; } else if (r.W[i]) r.W[i].grab = 0;
if (e.grabbedBy === r.i) e.grabbedBy = null;
}
const grabbing = r => r.W.some(W => W.grab > 0); // is any of my weapons holding the enemy?
// gadgets (input.fire3): one-shot abilities on a cooldown
function gadgets(s, r, e, inp, dt) {
@@ -674,7 +715,7 @@
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);
if (grabbing(r)) release(s, r, other);
finish(s, 1 - r.i, 'pit');
}
r.pressT = inRect(A.press, r.x, r.z) && ground ? r.pressT + dt : 0;
@@ -702,7 +743,7 @@
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);
const r = R.find(q => q.craneT > 0.8 && !q.out && !q.dead && !grabbing(q));
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;
@@ -731,19 +772,20 @@
// p = share of the cooldown still running (1 = just used, 0 = ready), on = doing its thing right now,
// n = charges left where a part has them (repair kit).
function cooldowns(r) {
const out = [], w = r.st.weapon, t = r.st.tower, g = r.st.gadget;
const out = [], t = r.st.tower, g = r.st.gadget;
const broke = k => (r.lost && r.lost[k]) || false;
const frac = (left, full) => (full > 0 ? clamp(left / full, 0, 1) : 0);
if (w) {
const e = { slot: 'weapon', id: w.id, p: 0, on: false, broke: broke('weapon') };
if (w.type === 'saw') e.on = !!r.sawOn;
else if (w.type === 'spinner' || w.type === 'vspinner') { e.p = 1 - clamp(r.spin, 0, 1); e.on = r.spin > 0.25; }
(r.st.weapons || []).forEach((inst, i) => {
const w = inst.part, W = r.W[i];
const c = { slot: 'weapon', idx: i, id: w.id, p: 0, on: false, broke: !!(r.lostW && r.lostW[i]) };
if (w.type === 'saw') c.on = !!W.sawOn;
else if (w.type === 'spinner' || w.type === 'vspinner') { c.p = 1 - clamp(W.spin, 0, 1); c.on = W.spin > 0.25; }
else {
e.p = frac(r.cd, w.cd);
e.on = (w.type === 'hammer' && r.swing > 0) || (w.type === 'crusher' && r.grab > 0) || (w.type === 'wedge' && r.boost > 0);
c.p = frac(W.cd, w.cd);
c.on = (w.type === 'hammer' && W.swing > 0) || (w.type === 'crusher' && W.grab > 0) || (w.type === 'wedge' && W.fire > 0);
}
out.push(e);
}
out.push(c);
});
if (t) {
const e = { slot: 'tower', id: t.id, p: 0, on: false, broke: broke('tower') };
if (t.type === 'rockets' || t.type === 'mortar' || t.type === 'swarm') e.p = frac(r.cd2, t.cd);