'use strict'; /** * Offline Strijd: a client-side single-player mode that plays like * multiplayer (shots, miss chance, opponent minis) against local bots * (t1119). Pure simulation — no server. */ var Offline = (function () { var BOT_INTERVAL = 0.5; var SKILL_START = 0.05; var SKILL_STEP = 0.01; var SKILL_CAP = 1; var FLIGHT_DURATION = 1.2; var ROUND_WON_DELAY = 1.5; /** * N opponents for Nāˆ’1 rounds when N >= 2; 1 opponent for 1 round. * Documented in docs/ASSUMPTIONS.md (t1119). */ function roundsAtOpponentCount (n) { if (n <= 1) { return 1; } return n - 1; } function skillForRound (round) { var r = Math.max(1, Math.floor(Number(round) || 1)); var pct = 5 + (r - 1); if (pct > 100) { pct = 100; } return pct / 100; } function scheduleForRound (round) { var r = Math.max(1, Math.floor(Number(round) || 1)); var cursor = 1; var n = 1; for (;;) { var count = roundsAtOpponentCount(n); if (r < cursor + count) { return { round: r, opponents: n, skill: skillForRound(r), roundsAtLevel: count, indexInLevel: r - cursor + 1 }; } cursor += count; n++; if (n > 10000) { return { round: r, opponents: n, skill: SKILL_CAP, roundsAtLevel: roundsAtOpponentCount(n), indexInLevel: 1 }; } } } function GameCoreCtor () { if (typeof GameCore !== 'undefined') { return GameCore; } if (typeof module !== 'undefined') { return require('./gameCore.js').GameCore; } return null; } function PlanetMod () { if (typeof Planet !== 'undefined') { return Planet; } if (typeof module !== 'undefined') { return require('./planet.js'); } return null; } /** * Integer-row colour grid of grounded (still / settling / locked) * blocks, including greys (color < 0) so a swap can shift them. */ function snapshotGrid (core) { var width = core.width; var height = core.height; var grid = []; for (var c = 0; c < width; c++) { grid[c] = []; for (var r = 0; r < height; r++) { grid[c][r] = null; } } for (var i = 0; i < core.blocks.length; i++) { var b = core.blocks[i]; if (!core.isGrounded(b)) { continue; } var row = Math.round(b.y); if (row < 0 || row >= height || b.col < 0 || b.col >= width) { continue; } if (Math.abs(b.y - row) > 0.2) { continue; } grid[b.col][row] = b.color; } return grid; } function cloneGrid (grid) { var out = []; for (var c = 0; c < grid.length; c++) { out[c] = grid[c].slice(); } return out; } /** * Apply swapToward's net result: the block at fromRow moves to toRow, * and the blocks it passed through each shift one cell toward fromRow. */ function applyColumnMove (grid, col, fromRow, toRow) { if (fromRow === toRow) { return; } var moving = grid[col][fromRow]; if (toRow > fromRow) { for (var y = fromRow; y < toRow; y++) { grid[col][y] = grid[col][y + 1]; } grid[col][toRow] = moving; } else { for (var y = fromRow; y > toRow; y--) { grid[col][y] = grid[col][y - 1]; } grid[col][toRow] = moving; } } function hasMatch (grid, width, height) { var c, r, run, prev; for (r = 0; r < height; r++) { run = 0; prev = null; for (c = 0; c < width; c++) { var color = grid[c][r]; if (color != null && color >= 0 && color === prev) { run++; if (run >= 3) { return true; } } else { run = (color != null && color >= 0) ? 1 : 0; prev = (color != null && color >= 0) ? color : null; } } } for (c = 0; c < width; c++) { run = 0; prev = null; for (r = 0; r < height; r++) { var color = grid[c][r]; if (color != null && color >= 0 && color === prev) { run++; if (run >= 3) { return true; } } else { run = (color != null && color >= 0) ? 1 : 0; prev = (color != null && color >= 0) ? color : null; } } } return false; } /** * Contiguous segments of draggable, nearly-integer-y blocks in a * column. Gaps break a segment the same way swapToward stops at gaps. */ function contiguousSegments (core, col) { if (!core.columnAllowsDrag(col)) { return []; } var list = core.blocksInColumn(col).filter(function (b) { return core.isDraggableBlock(b); }); list.sort(function (a, b) { return a.y - b.y; }); var segs = []; var cur = []; for (var i = 0; i < list.length; i++) { var y = list[i].y; if (Math.abs(y - Math.round(y)) > 0.15) { if (cur.length) { segs.push(cur); cur = []; } continue; } if (cur.length && Math.abs(y - (cur[cur.length - 1].y + 1)) > 0.15) { segs.push(cur); cur = [list[i]]; } else { cur.push(list[i]); } } if (cur.length) { segs.push(cur); } return segs; } /** * The shortest vertical move that would create a 3+ match, or null. * Ties break on lowest column, then lowest fromY, then lowest toY. */ function findBotMove (core) { var grid = snapshotGrid(core); if (hasMatch(grid, core.width, core.height)) { return null; } var best = null; for (var col = 0; col < core.width; col++) { var segs = contiguousSegments(core, col); for (var s = 0; s < segs.length; s++) { var seg = segs[s]; if (seg.length < 2) { continue; } for (var i = 0; i < seg.length; i++) { var fromY = Math.round(seg[i].y); for (var j = 0; j < seg.length; j++) { if (i === j) { continue; } var toY = Math.round(seg[j].y); var dist = Math.abs(toY - fromY); if (best && dist > best.dist) { continue; } var trial = cloneGrid(grid); applyColumnMove(trial, col, fromY, toY); if (!hasMatch(trial, core.width, core.height)) { continue; } var cand = { col: col, fromY: fromY, toY: toY, dist: dist, blockId: seg[i].id }; if (!best || dist < best.dist || (dist === best.dist && col < best.col) || (dist === best.dist && col === best.col && fromY < best.fromY) || (dist === best.dist && col === best.col && fromY === best.fromY && toY < best.toY)) { best = cand; } } } } } return best; } function executeBotMove (core, move) { if (!core || !move) { return false; } var list = core.blocksInColumn(move.col); var block = null; for (var i = 0; i < list.length; i++) { if (move.blockId != null && list[i].id === move.blockId) { block = list[i]; break; } if (Math.abs(list[i].y - move.fromY) < 0.2) { block = list[i]; } } if (!block || !core.isDraggableBlock(block)) { return false; } core.swapToward(block, move.toY); return true; } /** * One AI tick. `rng` is a () => [0,1) function. Success if roll < skill. * At skill 0 the bot never moves; at skill 1 it always looks for a move. */ function tryBotRoll (core, skill, rng) { rng = rng || Math.random; var roll = rng(); if (!(roll < skill)) { return { rolled: true, success: false, moved: false, roll: roll, move: null }; } var move = findBotMove(core); if (!move) { return { rolled: true, success: true, moved: false, roll: roll, move: null }; } var ok = executeBotMove(core, move); return { rolled: true, success: true, moved: !!ok, roll: roll, move: move }; } function pickPlanet (rng, P) { var all = P.allPlanets(); if (!all.length) { return P.defaultPlanet(); } var i = Math.floor(rng() * all.length) % all.length; return all[i]; } function OfflineBattle (opts) { opts = opts || {}; var P = PlanetMod(); this.playerPlanet = opts.playerPlanet || (P && P.defaultPlanet()); this.rng = opts.rng || Math.random; this.round = opts.round || 1; this.schedule = scheduleForRound(this.round); this.clock = 0; this.state = 'playing'; this.pending = []; this.flightResults = []; this.shotLocals = []; this.bots = []; this.playerId = 'player'; this._initBots(); } OfflineBattle.prototype._initBots = function () { var P = PlanetMod(); var Ctor = GameCoreCtor(); var n = this.schedule.opponents; this.bots = []; for (var i = 0; i < n; i++) { var planet = pickPlanet(this.rng, P); var miss = P.missChance(planet); var core = new Ctor(planet, this.rng, miss); this.bots.push({ id: 'bot-' + (i + 1), name: 'Bot ' + (i + 1), planet: planet, core: core, eliminated: false, score: 0, aiAcc: 0 }); } }; OfflineBattle.prototype.aliveBots = function () { var out = []; for (var i = 0; i < this.bots.length; i++) { if (!this.bots[i].eliminated && this.bots[i].core && this.bots[i].core.state !== 'gameover') { out.push(this.bots[i]); } } return out; }; OfflineBattle.prototype.coreFor = function (id, playerCore) { if (id === this.playerId) { return playerCore; } for (var i = 0; i < this.bots.length; i++) { if (this.bots[i].id === id) { return this.bots[i].core; } } return null; }; OfflineBattle.prototype._targetsFor = function (fromId, playerCore) { var targets = []; if (fromId !== this.playerId && playerCore && playerCore.state === 'playing') { targets.push({ id: this.playerId, core: playerCore }); } for (var i = 0; i < this.bots.length; i++) { var bot = this.bots[i]; if (bot.id === fromId || bot.eliminated) { continue; } if (bot.core && bot.core.state === 'playing') { targets.push({ id: bot.id, core: bot.core }); } } return targets; }; OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerCore) { if (!core || !core.consumeOutgoingShots) { return; } var shots = core.consumeOutgoingShots(); if (!shots.length) { return; } var P = PlanetMod(); var missChance = (P && planet) ? P.missChance(planet) : 0.30; var targets = this._targetsFor(fromId, playerCore); for (var i = 0; i < shots.length; i++) { var shot = shots[i]; var miss = targets.length === 0 || this.rng() < missChance; var target = null; if (!miss) { var idx = Math.floor(this.rng() * targets.length) % targets.length; target = targets[idx]; } var clientId = fromId + '-' + shot.id + '-' + this.clock; var landCol = null; if (!miss && target && target.core && target.core.pickGarbageColumn) { landCol = target.core.pickGarbageColumn(); } var result = { client_id: clientId, miss: miss, target_id: target ? target.id : null, color: shot.color, flight_duration: FLIGHT_DURATION, from_id: fromId, col: shot.col, y: shot.y, land_col: landCol }; this.flightResults.push(result); this.shotLocals.push({ client_id: clientId, col: shot.col, y: shot.y, color: shot.color, from_id: fromId, land_col: landCol }); if (!miss && target) { this.pending.push({ at: this.clock + FLIGHT_DURATION, targetId: target.id, color: shot.color, fromId: fromId, landCol: landCol }); } } }; OfflineBattle.prototype._deliver = function (playerCore) { var keep = []; for (var i = 0; i < this.pending.length; i++) { var ev = this.pending[i]; if (this.clock + 1e-9 < ev.at) { keep.push(ev); continue; } var core = this.coreFor(ev.targetId, playerCore); if (core && core.state === 'playing' && core.ingestGarbage) { core.ingestGarbage(ev.landCol); } } this.pending = keep; }; OfflineBattle.prototype.tick = function (dt, playerCore) { this.flightResults = []; this.shotLocals = []; if (this.state !== 'playing') { return { flights: [], locals: [] }; } if (dt < 0) { dt = 0; } this.clock += dt; var skill = this.schedule.skill; for (var i = 0; i < this.bots.length; i++) { var bot = this.bots[i]; if (bot.eliminated) { continue; } if (bot.core.state === 'gameover') { bot.eliminated = true; bot.score = bot.core.score; continue; } bot.core.tick(dt); bot.score = bot.core.score; bot.aiAcc += dt; while (bot.aiAcc >= BOT_INTERVAL) { bot.aiAcc -= BOT_INTERVAL; if (bot.core.state === 'playing' && !bot.core.drag) { tryBotRoll(bot.core, skill, this.rng); } } } this._dispatchShots(this.playerId, playerCore, this.playerPlanet, playerCore); for (var i = 0; i < this.bots.length; i++) { var bot = this.bots[i]; if (bot.eliminated) { continue; } this._dispatchShots(bot.id, bot.core, bot.planet, playerCore); } this._deliver(playerCore); for (var i = 0; i < this.bots.length; i++) { if (this.bots[i].core && this.bots[i].core.state === 'gameover') { this.bots[i].eliminated = true; this.bots[i].score = this.bots[i].core.score; } } if (playerCore && playerCore.state === 'gameover') { this.state = 'lost'; } else if (this.aliveBots().length === 0 && this.bots.length > 0) { this.state = 'round_won'; } return { flights: this.flightResults, locals: this.shotLocals }; }; /** * Start the next round (fresh bots, new skill and opponent count). * The caller must also give the player a fresh GameCore. */ OfflineBattle.prototype.advanceRound = function () { this.round += 1; this.schedule = scheduleForRound(this.round); this.state = 'playing'; this.pending = []; this.flightResults = []; this.shotLocals = []; this.clock = 0; this._initBots(); return this.schedule; }; function createBattle (opts) { return new OfflineBattle(opts); } var api = { BOT_INTERVAL: BOT_INTERVAL, SKILL_START: SKILL_START, SKILL_STEP: SKILL_STEP, SKILL_CAP: SKILL_CAP, FLIGHT_DURATION: FLIGHT_DURATION, ROUND_WON_DELAY: ROUND_WON_DELAY, roundsAtOpponentCount: roundsAtOpponentCount, skillForRound: skillForRound, scheduleForRound: scheduleForRound, findBotMove: findBotMove, tryBotRoll: tryBotRoll, executeBotMove: executeBotMove, snapshotGrid: snapshotGrid, applyColumnMove: applyColumnMove, hasMatch: hasMatch, createBattle: createBattle, OfflineBattle: OfflineBattle }; if (typeof module !== 'undefined' && module.exports) { module.exports = api; } else if (typeof globalThis !== 'undefined') { globalThis.Offline = api; } return api; })(); if (typeof module !== 'undefined' && module.exports && typeof Offline !== 'undefined') { module.exports = Offline; }