Files
Robot-Clash/game/missions.js

97 lines
5.1 KiB
JavaScript
Raw Normal View History

2026-11-19 00:55:39 +01:00
/* Robot Clash - campaign missions (no DOM, runs in Node for the tests). Needs parts.js.
Rotating objectives: 3 daily + 1 weekly, stored in the save (per mode, so campaign keeps its own).
Reward = credits + fame, some weekly ones unlock a special paint / scheme.
RC.Missions.ensure(save, now, baseSeed) -> rolls/resets save.missions when the day/week changed
RC.Missions.apply(m, ctx) -> advance one mission from a fight ctx, true if newly done
RC.Missions.active(save) -> [ ...daily, weekly ] for UI / iteration
ctx (same as awardAch): { won, reason, hpPct, lost, dealt, arenaId, weaponType }
Texts: lang.js keys 'mission_<id>' (name) and 'missd_<id>' (what to do). */
(function (root) {
'use strict';
const RC = (root.RC = root.RC || (typeof require === 'function' ? require('./parts.js') : {}));
const DAY = 86400000, WEEK = 7 * DAY;
const dayIndex = now => Math.floor((now || 0) / DAY);
const weekIndex = now => Math.floor((now || 0) / WEEK);
// deterministic RNG (mulberry32) so a given seed always rolls the same set
function rng(seed) {
let a = (seed >>> 0) || 1;
return () => { a |= 0; a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
}
const mix = (a, b) => (Math.imul((a >>> 0) ^ 0x9e3779b9, 0x85ebca6b) ^ ((b >>> 0) + 0x165667b1)) >>> 0;
// daily pool (small goals). type decides how a fight advances it.
const DAILY = [
{ id: 'd_win3', type: 'win', goal: 3, reward: 180, fame: 30 },
{ id: 'd_play5', type: 'play', goal: 5, reward: 120, fame: 15 },
{ id: 'd_dmg800', type: 'damage', goal: 800, reward: 160, fame: 20 },
{ id: 'd_ko2', type: 'ko', goal: 2, reward: 200, fame: 30 },
{ id: 'd_pit1', type: 'pit', goal: 1, reward: 180, fame: 30 },
{ id: 'd_flaw1', type: 'flawless', goal: 1, reward: 220, fame: 40 },
{ id: 'd_foundry', type: 'arena:foundry', goal: 1, reward: 200, fame: 30 },
{ id: 'd_colosseum', type: 'arena:colosseum', goal: 1, reward: 220, fame: 35 },
{ id: 'd_spin', type: 'weapon:spinner', goal: 1, reward: 200, fame: 30 },
{ id: 'd_hammer', type: 'weapon:hammer', goal: 1, reward: 200, fame: 30 },
];
// weekly pool (bigger goals, often an unlock). unlock: paint hex or 'scheme:<idx>'
const WEEKLY = [
{ id: 'w_win15', type: 'win', goal: 15, reward: 700, fame: 120, unlock: 0x39ff14 },
{ id: 'w_dmg6000', type: 'damage', goal: 6000, reward: 600, fame: 100, unlock: 0xc0c0c0 },
{ id: 'w_ko8', type: 'ko', goal: 8, reward: 800, fame: 140, unlock: 0xff1e8e },
{ id: 'w_flaw3', type: 'flawless', goal: 3, reward: 900, fame: 160, unlock: 0x7b2fff },
];
const inst = o => ({ id: o.id, type: o.type, goal: o.goal, reward: o.reward, fame: o.fame, unlock: o.unlock || null, prog: 0, done: false });
// pick n distinct objectives from a pool, deterministically from seed
function pick(pool, seed, n) {
const r = rng(seed), idx = pool.map((_, i) => i);
for (let i = idx.length - 1; i > 0; i--) { const j = (r() * (i + 1)) | 0; const t = idx[i]; idx[i] = idx[j]; idx[j] = t; }
return idx.slice(0, Math.min(n, pool.length)).map(i => inst(pool[i]));
}
const daily = (seed, n = 3) => pick(DAILY, seed, n);
const weekly = (seed, n = 1) => pick(WEEKLY, seed, n);
// roll or reset the mission set when the calendar day / week rolled over
function ensure(save, now, baseSeed) {
const d = dayIndex(now), w = weekIndex(now), base = (baseSeed >>> 0) || 1;
let m = save.missions;
if (!m || m.day !== d) m = Object.assign(m || {}, { day: d, daily: daily(mix(base, d)) });
if (m.week !== w) { m.week = w; m.weekly = weekly(mix(base, 1000003 + w)); }
save.missions = m;
return m;
}
// how much a single fight advances a mission of this type
function gain(type, ctx) {
const c = ctx || {};
if (type === 'play') return 1;
if (type === 'damage') return Math.max(0, Math.round(c.dealt || 0));
if (!c.won) return 0; // the rest only count on a win
if (type === 'win') return 1;
if (type === 'ko') return c.reason === 'ko' ? 1 : 0;
if (type === 'pit') return c.reason === 'pit' ? 1 : 0;
if (type === 'flawless') return (c.lost === 0 && (c.hpPct || 0) >= 0.9) ? 1 : 0;
if (type.startsWith('arena:')) return c.arenaId === type.slice(6) ? 1 : 0;
if (type.startsWith('weapon:')) return c.weaponType === type.slice(7) ? 1 : 0;
return 0;
}
// advance one mission; returns true the moment it is newly completed
function apply(m, ctx) {
if (!m || m.done) return false;
const add = gain(m.type, ctx);
if (add <= 0) return false;
m.prog = Math.min(m.goal, (m.prog || 0) + add);
if (m.prog >= m.goal) { m.done = true; return true; }
return false;
}
const active = save => (save && save.missions) ? [].concat(save.missions.daily || [], save.missions.weekly || []) : [];
RC.Missions = { DAILY, WEEKLY, dayIndex, weekIndex, daily, weekly, ensure, apply, gain, active };
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
})(typeof window !== 'undefined' ? window : globalThis);