/* Robot Clash - knockout tournament (no DOM, runs in Node for the tests). Needs parts.js, robot.js, combat.js, ai.js. An eight robot bracket: you plus seven league robots. Quarter final, semi final, final - three fights if you keep winning. The other matches of each round are really fought out with the normal simulation, so the field behaves like the league does. const st = RC.Tourney.create({ me: { name, build, paint }, seed }); RC.Tourney.pair(st) -> [a, b] for your match this round (null once it is over) RC.Tourney.simOthers(st) -> fights every other match of the round, fills st.log RC.Tourney.advance(st, won) -> your result goes in, the round closes, next field is built st.round 0..2, st.field (entries left), st.done, st.won, st.out (you were knocked out) Entries: league robots as they are, your own has player: true. */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || (typeof require === 'function' ? require('./parts.js') : {})); const ROUND_PRIZE = [600, 1200, 2500]; // won the quarter / semi / final const ENTRY_FEE = 400; const ROUND_KEY = ['quarter', 'semi', 'final']; function rng(seed) { let s = (seed || 1) >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; } // one AI against another, fought out properly; returns the winner (0 or 1) function simFight(a, b, seed, arenaId) { const s = RC.Combat.create({ robots: [RC.stats(a.build), RC.stats(b.build)], seed, names: [a.name, b.name], arena: arenaId }); const ba = RC.AI.create({ style: a.style || 'brawler', skill: a.skill == null ? 0.5 : a.skill, seed }); const bb = RC.AI.create({ style: b.style || 'brawler', skill: b.skill == null ? 0.5 : b.skill, seed: seed + 7 }); const dt = 1 / 60; for (let i = 0; i < 60 * 125 && !s.result; i++) { const [p, q] = s.robots; RC.Combat.step(s, dt, [ba.think(s, p, q, dt), bb.think(s, q, p, dt)]); s.events.length = 0; } if (s.result && s.result.winner >= 0) return s.result.winner; // a draw is decided by damage, then by the stronger robot const [p, q] = s.robots; if (Math.abs(p.dealt - q.dealt) > 0.5) return p.dealt > q.dealt ? 0 : 1; return (a.skill || 0) >= (b.skill || 0) ? 0 : 1; } function create(opt) { const seed = (opt.seed || 1) >>> 0, rand = rng(seed); const pool = RC.LEAGUE.slice(); // seven opponents, drawn at random for (let i = pool.length - 1; i > 0; i--) { const j = (rand() * (i + 1)) | 0; const t = pool[i]; pool[i] = pool[j]; pool[j] = t; } const field = pool.slice(0, 7); const me = { id: 'you', player: true, name: opt.me.name, build: opt.me.build, paint: opt.me.paint, style: 'brawler', skill: 0.5 }; field.splice((rand() * 8) | 0, 0, me); // you can start anywhere in the bracket return { seed, round: 0, field, rounds: [field.slice()], log: [], done: false, won: false, out: false, prize: 0 }; } const myIndex = st => st.field.findIndex(e => e && e.player); // your match of this round: pairs are (0,1), (2,3), ... function pair(st) { if (st.done) return null; const i = myIndex(st); if (i < 0) return null; const other = i % 2 === 0 ? i + 1 : i - 1; return [st.field[i], st.field[other]]; } // fight every match of this round that you are not in function simOthers(st) { const rand = rng(st.seed + st.round * 101); const mine = myIndex(st), ids = RC.ARENAS.map(a => a.id); st.pending = []; for (let i = 0; i < st.field.length; i += 2) { const a = st.field[i], b = st.field[i + 1]; if (!b) { st.pending.push(a); continue; } // odd one out walks on if (i === mine || i + 1 === mine) { st.pending.push(null); continue; } // yours is played for real const seed = ((rand() * 1e9) | 0) || 1; const w = simFight(a, b, seed, ids[seed % ids.length]); const win = w === 0 ? a : b, lose = w === 0 ? b : a; st.pending.push(win); st.log.push({ round: st.round, a: a.name, b: b.name, winner: win.name, loser: lose.name }); } return st.pending; } // your result closes the round and builds the next field function advance(st, playerWon) { if (st.done) return st; if (!st.pending) simOthers(st); const mine = myIndex(st), slot = (mine / 2) | 0; const me = st.field[mine], other = st.field[mine % 2 === 0 ? mine + 1 : mine - 1]; st.pending[slot] = playerWon ? me : other; st.log.push({ round: st.round, a: me.name, b: other.name, winner: (playerWon ? me : other).name, loser: (playerWon ? other : me).name, mine: true }); if (playerWon) st.prize += ROUND_PRIZE[st.round] || 0; st.field = st.pending.filter(Boolean); st.pending = null; st.round++; if (st.field.length) st.rounds[st.round] = st.field.slice(); // snapshot for the bracket view if (!playerWon) { st.done = true; st.out = true; } else if (st.field.length <= 1) { st.done = true; st.won = true; } return st; } RC.Tourney = { create, pair, simOthers, advance, simFight, ROUND_PRIZE, ENTRY_FEE, ROUND_KEY, SIZE: 8 }; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);