| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Offline Strijd: a client-side single-player mode that plays like |
| 5 | * multiplayer (shots, miss chance, opponent minis) against local bots |
| 6 | * (t1119). Pure simulation — no server. |
| 7 | */ |
| 8 | |
| 9 | var Offline = (function () |
| 10 | { |
| 11 | |
| 12 | var BOT_INTERVAL = 0.5; |
| 13 | var SKILL_START = 0.05; |
| 14 | var SKILL_STEP = 0.01; |
| 15 | var SKILL_CAP = 1; |
| 16 | var FLIGHT_DURATION = 1.2; |
| 17 | var ROUND_WON_DELAY = 1.5; |
| 18 | |
| 19 | /** |
| 20 | * N opponents for N−1 rounds when N >= 2; 1 opponent for 1 round. |
| 21 | * Documented in docs/ASSUMPTIONS.md (t1119). |
| 22 | */ |
| 23 | function roundsAtOpponentCount (n) |
| 24 | { |
| 25 | if (n <= 1) |
| 26 | { return 1; } |
| 27 | return n - 1; |
| 28 | } |
| 29 | |
| 30 | function skillForRound (round) |
| 31 | { |
| 32 | var r = Math.max(1, Math.floor(Number(round) || 1)); |
| 33 | var pct = 5 + (r - 1); |
| 34 | if (pct > 100) |
| 35 | { pct = 100; } |
| 36 | return pct / 100; |
| 37 | } |
| 38 | |
| 39 | function scheduleForRound (round) |
| 40 | { |
| 41 | var r = Math.max(1, Math.floor(Number(round) || 1)); |
| 42 | var cursor = 1; |
| 43 | var n = 1; |
| 44 | for (;;) |
| 45 | { |
| 46 | var count = roundsAtOpponentCount(n); |
| 47 | if (r < cursor + count) |
| 48 | { |
| 49 | return { |
| 50 | round: r, |
| 51 | opponents: n, |
| 52 | skill: skillForRound(r), |
| 53 | roundsAtLevel: count, |
| 54 | indexInLevel: r - cursor + 1 |
| 55 | }; |
| 56 | } |
| 57 | cursor += count; |
| 58 | n++; |
| 59 | if (n > 10000) |
| 60 | { |
| 61 | return { |
| 62 | round: r, |
| 63 | opponents: n, |
| 64 | skill: SKILL_CAP, |
| 65 | roundsAtLevel: roundsAtOpponentCount(n), |
| 66 | indexInLevel: 1 |
| 67 | }; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function GameCoreCtor () |
| 73 | { |
| 74 | if (typeof GameCore !== 'undefined') |
| 75 | { return GameCore; } |
| 76 | if (typeof module !== 'undefined') |
| 77 | { return require('./gameCore.js').GameCore; } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | function PlanetMod () |
| 82 | { |
| 83 | if (typeof Planet !== 'undefined') |
| 84 | { return Planet; } |
| 85 | if (typeof module !== 'undefined') |
| 86 | { return require('./planet.js'); } |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Integer-row colour grid of grounded (still / settling / locked) |
| 92 | * blocks, including greys (color < 0) so a swap can shift them. |
| 93 | */ |
| 94 | function snapshotGrid (core) |
| 95 | { |
| 96 | var width = core.width; |
| 97 | var height = core.height; |
| 98 | var grid = []; |
| 99 | for (var c = 0; c < width; c++) |
| 100 | { |
| 101 | grid[c] = []; |
| 102 | for (var r = 0; r < height; r++) |
| 103 | { grid[c][r] = null; } |
| 104 | } |
| 105 | for (var i = 0; i < core.blocks.length; i++) |
| 106 | { |
| 107 | var b = core.blocks[i]; |
| 108 | if (!core.isGrounded(b)) |
| 109 | { continue; } |
| 110 | var row = Math.round(b.y); |
| 111 | if (row < 0 || row >= height || b.col < 0 || b.col >= width) |
| 112 | { continue; } |
| 113 | if (Math.abs(b.y - row) > 0.2) |
| 114 | { continue; } |
| 115 | grid[b.col][row] = b.color; |
| 116 | } |
| 117 | return grid; |
| 118 | } |
| 119 | |
| 120 | function cloneGrid (grid) |
| 121 | { |
| 122 | var out = []; |
| 123 | for (var c = 0; c < grid.length; c++) |
| 124 | { out[c] = grid[c].slice(); } |
| 125 | return out; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Apply swapToward's net result: the block at fromRow moves to toRow, |
| 130 | * and the blocks it passed through each shift one cell toward fromRow. |
| 131 | */ |
| 132 | function applyColumnMove (grid, col, fromRow, toRow) |
| 133 | { |
| 134 | if (fromRow === toRow) |
| 135 | { return; } |
| 136 | var moving = grid[col][fromRow]; |
| 137 | if (toRow > fromRow) |
| 138 | { |
| 139 | for (var y = fromRow; y < toRow; y++) |
| 140 | { grid[col][y] = grid[col][y + 1]; } |
| 141 | grid[col][toRow] = moving; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | for (var y = fromRow; y > toRow; y--) |
| 146 | { grid[col][y] = grid[col][y - 1]; } |
| 147 | grid[col][toRow] = moving; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | function hasMatch (grid, width, height) |
| 152 | { |
| 153 | var c, r, run, prev; |
| 154 | for (r = 0; r < height; r++) |
| 155 | { |
| 156 | run = 0; |
| 157 | prev = null; |
| 158 | for (c = 0; c < width; c++) |
| 159 | { |
| 160 | var color = grid[c][r]; |
| 161 | if (color != null && color >= 0 && color === prev) |
| 162 | { |
| 163 | run++; |
| 164 | if (run >= 3) |
| 165 | { return true; } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | run = (color != null && color >= 0) ? 1 : 0; |
| 170 | prev = (color != null && color >= 0) ? color : null; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | for (c = 0; c < width; c++) |
| 175 | { |
| 176 | run = 0; |
| 177 | prev = null; |
| 178 | for (r = 0; r < height; r++) |
| 179 | { |
| 180 | var color = grid[c][r]; |
| 181 | if (color != null && color >= 0 && color === prev) |
| 182 | { |
| 183 | run++; |
| 184 | if (run >= 3) |
| 185 | { return true; } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | run = (color != null && color >= 0) ? 1 : 0; |
| 190 | prev = (color != null && color >= 0) ? color : null; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Contiguous segments of draggable, nearly-integer-y blocks in a |
| 199 | * column. Gaps break a segment the same way swapToward stops at gaps. |
| 200 | */ |
| 201 | function contiguousSegments (core, col) |
| 202 | { |
| 203 | if (!core.columnAllowsDrag(col)) |
| 204 | { return []; } |
| 205 | var list = core.blocksInColumn(col).filter(function (b) |
| 206 | { |
| 207 | return core.isDraggableBlock(b); |
| 208 | }); |
| 209 | list.sort(function (a, b) { return a.y - b.y; }); |
| 210 | var segs = []; |
| 211 | var cur = []; |
| 212 | for (var i = 0; i < list.length; i++) |
| 213 | { |
| 214 | var y = list[i].y; |
| 215 | if (Math.abs(y - Math.round(y)) > 0.15) |
| 216 | { |
| 217 | if (cur.length) |
| 218 | { |
| 219 | segs.push(cur); |
| 220 | cur = []; |
| 221 | } |
| 222 | continue; |
| 223 | } |
| 224 | if (cur.length && Math.abs(y - (cur[cur.length - 1].y + 1)) > 0.15) |
| 225 | { |
| 226 | segs.push(cur); |
| 227 | cur = [list[i]]; |
| 228 | } |
| 229 | else |
| 230 | { cur.push(list[i]); } |
| 231 | } |
| 232 | if (cur.length) |
| 233 | { segs.push(cur); } |
| 234 | return segs; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * The shortest vertical move that would create a 3+ match, or null. |
| 239 | * Ties break on lowest column, then lowest fromY, then lowest toY. |
| 240 | */ |
| 241 | function findBotMove (core) |
| 242 | { |
| 243 | var grid = snapshotGrid(core); |
| 244 | if (hasMatch(grid, core.width, core.height)) |
| 245 | { return null; } |
| 246 | var best = null; |
| 247 | for (var col = 0; col < core.width; col++) |
| 248 | { |
| 249 | var segs = contiguousSegments(core, col); |
| 250 | for (var s = 0; s < segs.length; s++) |
| 251 | { |
| 252 | var seg = segs[s]; |
| 253 | if (seg.length < 2) |
| 254 | { continue; } |
| 255 | for (var i = 0; i < seg.length; i++) |
| 256 | { |
| 257 | var fromY = Math.round(seg[i].y); |
| 258 | for (var j = 0; j < seg.length; j++) |
| 259 | { |
| 260 | if (i === j) |
| 261 | { continue; } |
| 262 | var toY = Math.round(seg[j].y); |
| 263 | var dist = Math.abs(toY - fromY); |
| 264 | if (best && dist > best.dist) |
| 265 | { continue; } |
| 266 | var trial = cloneGrid(grid); |
| 267 | applyColumnMove(trial, col, fromY, toY); |
| 268 | if (!hasMatch(trial, core.width, core.height)) |
| 269 | { continue; } |
| 270 | var cand = { |
| 271 | col: col, |
| 272 | fromY: fromY, |
| 273 | toY: toY, |
| 274 | dist: dist, |
| 275 | blockId: seg[i].id |
| 276 | }; |
| 277 | if (!best || dist < best.dist |
| 278 | || (dist === best.dist && col < best.col) |
| 279 | || (dist === best.dist && col === best.col && fromY < best.fromY) |
| 280 | || (dist === best.dist && col === best.col && fromY === best.fromY |
| 281 | && toY < best.toY)) |
| 282 | { best = cand; } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | return best; |
| 288 | } |
| 289 | |
| 290 | function executeBotMove (core, move) |
| 291 | { |
| 292 | if (!core || !move) |
| 293 | { return false; } |
| 294 | var list = core.blocksInColumn(move.col); |
| 295 | var block = null; |
| 296 | for (var i = 0; i < list.length; i++) |
| 297 | { |
| 298 | if (move.blockId != null && list[i].id === move.blockId) |
| 299 | { |
| 300 | block = list[i]; |
| 301 | break; |
| 302 | } |
| 303 | if (Math.abs(list[i].y - move.fromY) < 0.2) |
| 304 | { block = list[i]; } |
| 305 | } |
| 306 | if (!block || !core.isDraggableBlock(block)) |
| 307 | { return false; } |
| 308 | core.swapToward(block, move.toY); |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Max board evaluations (apply + hasMatch + undo) for a depth-2 search |
| 314 | * in one AI tick. A 10×13 field with typical stacks stays well under this. |
| 315 | */ |
| 316 | var PLAN_BUDGET = 4000; |
| 317 | /** Skill at or below this: never start a 2-step setup (early rounds). */ |
| 318 | var LOOKAHEAD_START = 0.10; |
| 319 | /** Skill at or above this: always look for a 2-step setup when no immediate match. */ |
| 320 | var LOOKAHEAD_FULL = 0.40; |
| 321 | |
| 322 | /** |
| 323 | * Probability of searching a 2-step setup after a successful roll |
| 324 | * that found no immediate match. 0 through 10% skill (rondes 1–6), |
| 325 | * then linear up to 1 at 40% skill (ronde 36). |
| 326 | */ |
| 327 | function lookaheadChance (skill) |
| 328 | { |
| 329 | skill = Number(skill) || 0; |
| 330 | if (skill <= LOOKAHEAD_START) |
| 331 | { return 0; } |
| 332 | if (skill >= LOOKAHEAD_FULL) |
| 333 | { return 1; } |
| 334 | return (skill - LOOKAHEAD_START) / (LOOKAHEAD_FULL - LOOKAHEAD_START); |
| 335 | } |
| 336 | |
| 337 | function copyMove (m) |
| 338 | { |
| 339 | return { |
| 340 | col: m.col, |
| 341 | fromY: m.fromY, |
| 342 | toY: m.toY, |
| 343 | dist: m.dist, |
| 344 | blockId: m.blockId |
| 345 | }; |
| 346 | } |
| 347 | |
| 348 | function movesEqual (a, b) |
| 349 | { |
| 350 | return a && b && a.col === b.col && a.fromY === b.fromY && a.toY === b.toY; |
| 351 | } |
| 352 | |
| 353 | function betterImmediate (cand, best) |
| 354 | { |
| 355 | if (!best) |
| 356 | { return true; } |
| 357 | if (cand.dist !== best.dist) |
| 358 | { return cand.dist < best.dist; } |
| 359 | if (cand.col !== best.col) |
| 360 | { return cand.col < best.col; } |
| 361 | if (cand.fromY !== best.fromY) |
| 362 | { return cand.fromY < best.fromY; } |
| 363 | return cand.toY < best.toY; |
| 364 | } |
| 365 | |
| 366 | function betterPlan (cand, best) |
| 367 | { |
| 368 | if (!best) |
| 369 | { return true; } |
| 370 | if (cand.total !== best.total) |
| 371 | { return cand.total < best.total; } |
| 372 | var a0 = cand.steps[0]; |
| 373 | var b0 = best.steps[0]; |
| 374 | if (a0.dist !== b0.dist) |
| 375 | { return a0.dist < b0.dist; } |
| 376 | if (a0.col !== b0.col || a0.fromY !== b0.fromY || a0.toY !== b0.toY) |
| 377 | { return betterImmediate(a0, b0); } |
| 378 | return betterImmediate(cand.steps[1], best.steps[1]); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Legal vertical swaps from a colour grid: contiguous non-null cells |
| 383 | * in a column (gaps break a segment, same as swapToward). |
| 384 | */ |
| 385 | function listMovesFromGrid (grid, width, height) |
| 386 | { |
| 387 | var moves = []; |
| 388 | for (var col = 0; col < width; col++) |
| 389 | { |
| 390 | var start = -1; |
| 391 | for (var r = 0; r <= height; r++) |
| 392 | { |
| 393 | var filled = r < height && grid[col][r] != null; |
| 394 | if (filled) |
| 395 | { |
| 396 | if (start < 0) |
| 397 | { start = r; } |
| 398 | } |
| 399 | else if (start >= 0) |
| 400 | { |
| 401 | var end = r; |
| 402 | if (end - start >= 2) |
| 403 | { |
| 404 | for (var i = start; i < end; i++) |
| 405 | { |
| 406 | for (var j = start; j < end; j++) |
| 407 | { |
| 408 | if (i === j) |
| 409 | { continue; } |
| 410 | moves.push({ |
| 411 | col: col, |
| 412 | fromY: i, |
| 413 | toY: j, |
| 414 | dist: Math.abs(j - i), |
| 415 | blockId: null |
| 416 | }); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | start = -1; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | return moves; |
| 425 | } |
| 426 | |
| 427 | function listMovesFromCore (core) |
| 428 | { |
| 429 | var moves = []; |
| 430 | for (var col = 0; col < core.width; col++) |
| 431 | { |
| 432 | var segs = contiguousSegments(core, col); |
| 433 | for (var s = 0; s < segs.length; s++) |
| 434 | { |
| 435 | var seg = segs[s]; |
| 436 | if (seg.length < 2) |
| 437 | { continue; } |
| 438 | for (var i = 0; i < seg.length; i++) |
| 439 | { |
| 440 | var fromY = Math.round(seg[i].y); |
| 441 | for (var j = 0; j < seg.length; j++) |
| 442 | { |
| 443 | if (i === j) |
| 444 | { continue; } |
| 445 | var toY = Math.round(seg[j].y); |
| 446 | moves.push({ |
| 447 | col: col, |
| 448 | fromY: fromY, |
| 449 | toY: toY, |
| 450 | dist: Math.abs(toY - fromY), |
| 451 | blockId: seg[i].id |
| 452 | }); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | return moves; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * A 2-step setup: first swap creates no match, second swap does. |
| 462 | * Prefers smaller total distance, then shorter first step, then the |
| 463 | * same column / fromY / toY ties as findBotMove. First moves come |
| 464 | * from the live core (legal segments only). Budget-capped. |
| 465 | */ |
| 466 | function findSetupPlan (core, budget) |
| 467 | { |
| 468 | if (!core) |
| 469 | { return null; } |
| 470 | var width = core.width; |
| 471 | var height = core.height; |
| 472 | var grid = snapshotGrid(core); |
| 473 | if (hasMatch(grid, width, height)) |
| 474 | { return null; } |
| 475 | budget = budget == null ? PLAN_BUDGET : budget; |
| 476 | var firstMoves = listMovesFromCore(core); |
| 477 | firstMoves.sort(function (a, b) |
| 478 | { |
| 479 | if (a.dist !== b.dist) |
| 480 | { return a.dist - b.dist; } |
| 481 | if (a.col !== b.col) |
| 482 | { return a.col - b.col; } |
| 483 | if (a.fromY !== b.fromY) |
| 484 | { return a.fromY - b.fromY; } |
| 485 | return a.toY - b.toY; |
| 486 | }); |
| 487 | var best = null; |
| 488 | var evals = 0; |
| 489 | for (var i = 0; i < firstMoves.length; i++) |
| 490 | { |
| 491 | var first = firstMoves[i]; |
| 492 | if (best && first.dist + 1 > best.total) |
| 493 | { break; } |
| 494 | applyColumnMove(grid, first.col, first.fromY, first.toY); |
| 495 | if (hasMatch(grid, width, height)) |
| 496 | { |
| 497 | applyColumnMove(grid, first.col, first.toY, first.fromY); |
| 498 | continue; |
| 499 | } |
| 500 | var seconds = listMovesFromGrid(grid, width, height); |
| 501 | for (var j = 0; j < seconds.length; j++) |
| 502 | { |
| 503 | var second = seconds[j]; |
| 504 | var total = first.dist + second.dist; |
| 505 | if (best && total > best.total) |
| 506 | { continue; } |
| 507 | if (evals++ >= budget) |
| 508 | { break; } |
| 509 | applyColumnMove(grid, second.col, second.fromY, second.toY); |
| 510 | var ok = hasMatch(grid, width, height); |
| 511 | applyColumnMove(grid, second.col, second.toY, second.fromY); |
| 512 | if (!ok) |
| 513 | { continue; } |
| 514 | var cand = { |
| 515 | steps: [copyMove(first), copyMove(second)], |
| 516 | total: total |
| 517 | }; |
| 518 | if (betterPlan(cand, best)) |
| 519 | { best = cand; } |
| 520 | } |
| 521 | applyColumnMove(grid, first.col, first.toY, first.fromY); |
| 522 | if (evals >= budget) |
| 523 | { break; } |
| 524 | // 1+1 is the shortest possible setup; firstMoves is sorted so |
| 525 | // the first such plan is already the tie-break winner. |
| 526 | if (best && best.total === 2) |
| 527 | { break; } |
| 528 | } |
| 529 | return best; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * True when `step` is still a legal vertical swap on this core, and |
| 534 | * (when mustMatch) would create a 3+ match. |
| 535 | */ |
| 536 | function stepIsValid (core, step, mustMatch) |
| 537 | { |
| 538 | if (!core || !step) |
| 539 | { return false; } |
| 540 | var moves = listMovesFromCore(core); |
| 541 | var found = null; |
| 542 | for (var i = 0; i < moves.length; i++) |
| 543 | { |
| 544 | if (movesEqual(moves[i], step)) |
| 545 | { |
| 546 | found = moves[i]; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | if (!found) |
| 551 | { return false; } |
| 552 | step.blockId = found.blockId; |
| 553 | if (!mustMatch) |
| 554 | { return true; } |
| 555 | var grid = snapshotGrid(core); |
| 556 | applyColumnMove(grid, step.col, step.fromY, step.toY); |
| 557 | return hasMatch(grid, core.width, core.height); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * One AI tick. `rng` is a () => [0,1) function. Success if roll < skill. |
| 562 | * At skill 0 the bot never moves; at skill 1 it always looks for a move. |
| 563 | * `memory` is an optional object that stores a 2-step plan across ticks |
| 564 | * (`memory.plan`). On a later successful roll the bot follows the |
| 565 | * remaining step if it is still legal and still makes a match; otherwise |
| 566 | * it re-plans. |
| 567 | */ |
| 568 | function tryBotRoll (core, skill, rng, memory) |
| 569 | { |
| 570 | rng = rng || Math.random; |
| 571 | var roll = rng(); |
| 572 | if (!(roll < skill)) |
| 573 | { |
| 574 | return { rolled: true, success: false, moved: false, roll: roll, move: null }; |
| 575 | } |
| 576 | var grid = snapshotGrid(core); |
| 577 | if (hasMatch(grid, core.width, core.height)) |
| 578 | { |
| 579 | if (memory) |
| 580 | { memory.plan = null; } |
| 581 | return { rolled: true, success: true, moved: false, roll: roll, move: null }; |
| 582 | } |
| 583 | if (memory && memory.plan && memory.plan.steps |
| 584 | && memory.plan.index < memory.plan.steps.length) |
| 585 | { |
| 586 | var step = memory.plan.steps[memory.plan.index]; |
| 587 | var last = memory.plan.index === memory.plan.steps.length - 1; |
| 588 | if (stepIsValid(core, step, last)) |
| 589 | { |
| 590 | var followed = executeBotMove(core, step); |
| 591 | if (followed) |
| 592 | { |
| 593 | memory.plan.index++; |
| 594 | if (memory.plan.index >= memory.plan.steps.length) |
| 595 | { memory.plan = null; } |
| 596 | return { |
| 597 | rolled: true, |
| 598 | success: true, |
| 599 | moved: true, |
| 600 | roll: roll, |
| 601 | move: step, |
| 602 | followed: true |
| 603 | }; |
| 604 | } |
| 605 | } |
| 606 | memory.plan = null; |
| 607 | } |
| 608 | var move = findBotMove(core); |
| 609 | if (move) |
| 610 | { |
| 611 | if (memory) |
| 612 | { memory.plan = null; } |
| 613 | var ok = executeBotMove(core, move); |
| 614 | return { rolled: true, success: true, moved: !!ok, roll: roll, move: move }; |
| 615 | } |
| 616 | var chance = lookaheadChance(skill); |
| 617 | if (chance <= 0 || !(rng() < chance)) |
| 618 | { |
| 619 | return { rolled: true, success: true, moved: false, roll: roll, move: null }; |
| 620 | } |
| 621 | var plan = findSetupPlan(core); |
| 622 | if (!plan || !plan.steps || !plan.steps[0]) |
| 623 | { |
| 624 | return { rolled: true, success: true, moved: false, roll: roll, move: null }; |
| 625 | } |
| 626 | var first = plan.steps[0]; |
| 627 | var started = executeBotMove(core, first); |
| 628 | if (started && memory) |
| 629 | { |
| 630 | memory.plan = { steps: plan.steps, index: 1, total: plan.total }; |
| 631 | } |
| 632 | else if (memory) |
| 633 | { memory.plan = null; } |
| 634 | return { |
| 635 | rolled: true, |
| 636 | success: true, |
| 637 | moved: !!started, |
| 638 | roll: roll, |
| 639 | move: first, |
| 640 | planned: true, |
| 641 | plan: plan |
| 642 | }; |
| 643 | } |
| 644 | |
| 645 | function pickPlanet (rng, P) |
| 646 | { |
| 647 | var all = P.allPlanets(); |
| 648 | if (!all.length) |
| 649 | { return P.defaultPlanet(); } |
| 650 | var i = Math.floor(rng() * all.length) % all.length; |
| 651 | return all[i]; |
| 652 | } |
| 653 | |
| 654 | function OfflineBattle (opts) |
| 655 | { |
| 656 | opts = opts || {}; |
| 657 | var P = PlanetMod(); |
| 658 | this.playerPlanet = opts.playerPlanet || (P && P.defaultPlanet()); |
| 659 | this.rng = opts.rng || Math.random; |
| 660 | this.round = opts.round || 1; |
| 661 | this.schedule = scheduleForRound(this.round); |
| 662 | this.clock = 0; |
| 663 | this.state = 'playing'; |
| 664 | this.pending = []; |
| 665 | this.flightResults = []; |
| 666 | this.shotLocals = []; |
| 667 | this.bots = []; |
| 668 | this.playerId = 'player'; |
| 669 | /** Opponents knocked out over all rounds of this battle (t1198). */ |
| 670 | this.defeated = 0; |
| 671 | this._initBots(); |
| 672 | } |
| 673 | |
| 674 | /** Mark a bot as out once; counts towards `defeated` (t1198). */ |
| 675 | OfflineBattle.prototype._eliminate = function (bot) |
| 676 | { |
| 677 | if (bot.eliminated) |
| 678 | { return; } |
| 679 | bot.eliminated = true; |
| 680 | bot.score = bot.core ? bot.core.score : bot.score; |
| 681 | this.defeated += 1; |
| 682 | }; |
| 683 | |
| 684 | OfflineBattle.prototype._initBots = function () |
| 685 | { |
| 686 | var P = PlanetMod(); |
| 687 | var Ctor = GameCoreCtor(); |
| 688 | var n = this.schedule.opponents; |
| 689 | this.bots = []; |
| 690 | for (var i = 0; i < n; i++) |
| 691 | { |
| 692 | var planet = pickPlanet(this.rng, P); |
| 693 | var miss = P.missChance(planet); |
| 694 | var core = new Ctor(planet, this.rng, miss); |
| 695 | this.bots.push({ |
| 696 | id: 'bot-' + (i + 1), |
| 697 | name: 'Bot ' + (i + 1), |
| 698 | planet: planet, |
| 699 | core: core, |
| 700 | eliminated: false, |
| 701 | score: 0, |
| 702 | aiAcc: 0, |
| 703 | plan: null |
| 704 | }); |
| 705 | } |
| 706 | }; |
| 707 | |
| 708 | OfflineBattle.prototype.aliveBots = function () |
| 709 | { |
| 710 | var out = []; |
| 711 | for (var i = 0; i < this.bots.length; i++) |
| 712 | { |
| 713 | if (!this.bots[i].eliminated && this.bots[i].core |
| 714 | && this.bots[i].core.state !== 'gameover') |
| 715 | { out.push(this.bots[i]); } |
| 716 | } |
| 717 | return out; |
| 718 | }; |
| 719 | |
| 720 | OfflineBattle.prototype.coreFor = function (id, playerCore) |
| 721 | { |
| 722 | if (id === this.playerId) |
| 723 | { return playerCore; } |
| 724 | for (var i = 0; i < this.bots.length; i++) |
| 725 | { |
| 726 | if (this.bots[i].id === id) |
| 727 | { return this.bots[i].core; } |
| 728 | } |
| 729 | return null; |
| 730 | }; |
| 731 | |
| 732 | OfflineBattle.prototype._targetsFor = function (fromId, playerCore) |
| 733 | { |
| 734 | var targets = []; |
| 735 | if (fromId !== this.playerId && playerCore && playerCore.state === 'playing') |
| 736 | { targets.push({ id: this.playerId, core: playerCore }); } |
| 737 | for (var i = 0; i < this.bots.length; i++) |
| 738 | { |
| 739 | var bot = this.bots[i]; |
| 740 | if (bot.id === fromId || bot.eliminated) |
| 741 | { continue; } |
| 742 | if (bot.core && bot.core.state === 'playing') |
| 743 | { targets.push({ id: bot.id, core: bot.core }); } |
| 744 | } |
| 745 | return targets; |
| 746 | }; |
| 747 | |
| 748 | OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerCore) |
| 749 | { |
| 750 | if (!core || !core.consumeOutgoingShots) |
| 751 | { return; } |
| 752 | var shots = core.consumeOutgoingShots(); |
| 753 | if (!shots.length) |
| 754 | { return; } |
| 755 | var P = PlanetMod(); |
| 756 | var missChance = (P && planet) ? P.missChance(planet) : 0.30; |
| 757 | var targets = this._targetsFor(fromId, playerCore); |
| 758 | for (var i = 0; i < shots.length; i++) |
| 759 | { |
| 760 | var shot = shots[i]; |
| 761 | var miss = targets.length === 0 || this.rng() < missChance; |
| 762 | var target = null; |
| 763 | if (!miss) |
| 764 | { |
| 765 | var idx = Math.floor(this.rng() * targets.length) % targets.length; |
| 766 | target = targets[idx]; |
| 767 | } |
| 768 | var clientId = fromId + '-' + shot.id + '-' + this.clock; |
| 769 | var landCol = null; |
| 770 | if (!miss && target && target.core && target.core.pickGarbageColumn) |
| 771 | { landCol = target.core.pickGarbageColumn(); } |
| 772 | var result = { |
| 773 | client_id: clientId, |
| 774 | miss: miss, |
| 775 | target_id: target ? target.id : null, |
| 776 | color: shot.color, |
| 777 | flight_duration: FLIGHT_DURATION, |
| 778 | from_id: fromId, |
| 779 | col: shot.col, |
| 780 | y: shot.y, |
| 781 | land_col: landCol |
| 782 | }; |
| 783 | this.flightResults.push(result); |
| 784 | this.shotLocals.push({ |
| 785 | client_id: clientId, |
| 786 | col: shot.col, |
| 787 | y: shot.y, |
| 788 | color: shot.color, |
| 789 | from_id: fromId, |
| 790 | land_col: landCol |
| 791 | }); |
| 792 | if (!miss && target) |
| 793 | { |
| 794 | this.pending.push({ |
| 795 | at: this.clock + FLIGHT_DURATION, |
| 796 | targetId: target.id, |
| 797 | color: shot.color, |
| 798 | fromId: fromId, |
| 799 | landCol: landCol |
| 800 | }); |
| 801 | } |
| 802 | } |
| 803 | }; |
| 804 | |
| 805 | OfflineBattle.prototype._deliver = function (playerCore) |
| 806 | { |
| 807 | var keep = []; |
| 808 | for (var i = 0; i < this.pending.length; i++) |
| 809 | { |
| 810 | var ev = this.pending[i]; |
| 811 | if (this.clock + 1e-9 < ev.at) |
| 812 | { |
| 813 | keep.push(ev); |
| 814 | continue; |
| 815 | } |
| 816 | var core = this.coreFor(ev.targetId, playerCore); |
| 817 | if (core && core.state === 'playing' && core.ingestGarbage) |
| 818 | { core.ingestGarbage(ev.landCol); } |
| 819 | } |
| 820 | this.pending = keep; |
| 821 | }; |
| 822 | |
| 823 | OfflineBattle.prototype.tick = function (dt, playerCore) |
| 824 | { |
| 825 | this.flightResults = []; |
| 826 | this.shotLocals = []; |
| 827 | if (this.state !== 'playing') |
| 828 | { return { flights: [], locals: [] }; } |
| 829 | if (dt < 0) |
| 830 | { dt = 0; } |
| 831 | this.clock += dt; |
| 832 | |
| 833 | var skill = this.schedule.skill; |
| 834 | for (var i = 0; i < this.bots.length; i++) |
| 835 | { |
| 836 | var bot = this.bots[i]; |
| 837 | if (bot.eliminated) |
| 838 | { continue; } |
| 839 | if (bot.core.state === 'gameover') |
| 840 | { |
| 841 | this._eliminate(bot); |
| 842 | continue; |
| 843 | } |
| 844 | bot.core.tick(dt); |
| 845 | bot.score = bot.core.score; |
| 846 | bot.aiAcc += dt; |
| 847 | while (bot.aiAcc >= BOT_INTERVAL) |
| 848 | { |
| 849 | bot.aiAcc -= BOT_INTERVAL; |
| 850 | if (bot.core.state === 'playing' && !bot.core.drag) |
| 851 | { tryBotRoll(bot.core, skill, this.rng, bot); } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | this._dispatchShots(this.playerId, playerCore, this.playerPlanet, playerCore); |
| 856 | for (var i = 0; i < this.bots.length; i++) |
| 857 | { |
| 858 | var bot = this.bots[i]; |
| 859 | if (bot.eliminated) |
| 860 | { continue; } |
| 861 | this._dispatchShots(bot.id, bot.core, bot.planet, playerCore); |
| 862 | } |
| 863 | this._deliver(playerCore); |
| 864 | |
| 865 | for (var i = 0; i < this.bots.length; i++) |
| 866 | { |
| 867 | if (this.bots[i].core && this.bots[i].core.state === 'gameover') |
| 868 | { this._eliminate(this.bots[i]); } |
| 869 | } |
| 870 | |
| 871 | if (playerCore && playerCore.state === 'gameover') |
| 872 | { this.state = 'lost'; } |
| 873 | else if (this.aliveBots().length === 0 && this.bots.length > 0) |
| 874 | { this.state = 'round_won'; } |
| 875 | |
| 876 | return { flights: this.flightResults, locals: this.shotLocals }; |
| 877 | }; |
| 878 | |
| 879 | /** |
| 880 | * Start the next round (fresh bots, new skill and opponent count). |
| 881 | * The caller must also give the player a fresh GameCore. |
| 882 | */ |
| 883 | OfflineBattle.prototype.advanceRound = function () |
| 884 | { |
| 885 | this.round += 1; |
| 886 | this.schedule = scheduleForRound(this.round); |
| 887 | this.state = 'playing'; |
| 888 | this.pending = []; |
| 889 | this.flightResults = []; |
| 890 | this.shotLocals = []; |
| 891 | this.clock = 0; |
| 892 | this._initBots(); |
| 893 | return this.schedule; |
| 894 | }; |
| 895 | |
| 896 | function createBattle (opts) |
| 897 | { |
| 898 | return new OfflineBattle(opts); |
| 899 | } |
| 900 | |
| 901 | var api = { |
| 902 | BOT_INTERVAL: BOT_INTERVAL, |
| 903 | SKILL_START: SKILL_START, |
| 904 | SKILL_STEP: SKILL_STEP, |
| 905 | SKILL_CAP: SKILL_CAP, |
| 906 | FLIGHT_DURATION: FLIGHT_DURATION, |
| 907 | ROUND_WON_DELAY: ROUND_WON_DELAY, |
| 908 | roundsAtOpponentCount: roundsAtOpponentCount, |
| 909 | skillForRound: skillForRound, |
| 910 | scheduleForRound: scheduleForRound, |
| 911 | findBotMove: findBotMove, |
| 912 | findSetupPlan: findSetupPlan, |
| 913 | tryBotRoll: tryBotRoll, |
| 914 | executeBotMove: executeBotMove, |
| 915 | stepIsValid: stepIsValid, |
| 916 | lookaheadChance: lookaheadChance, |
| 917 | listMovesFromCore: listMovesFromCore, |
| 918 | listMovesFromGrid: listMovesFromGrid, |
| 919 | PLAN_BUDGET: PLAN_BUDGET, |
| 920 | LOOKAHEAD_START: LOOKAHEAD_START, |
| 921 | LOOKAHEAD_FULL: LOOKAHEAD_FULL, |
| 922 | snapshotGrid: snapshotGrid, |
| 923 | applyColumnMove: applyColumnMove, |
| 924 | hasMatch: hasMatch, |
| 925 | createBattle: createBattle, |
| 926 | OfflineBattle: OfflineBattle |
| 927 | }; |
| 928 | |
| 929 | if (typeof module !== 'undefined' && module.exports) |
| 930 | { module.exports = api; } |
| 931 | else if (typeof globalThis !== 'undefined') |
| 932 | { globalThis.Offline = api; } |
| 933 | |
| 934 | return api; |
| 935 | |
| 936 | })(); |
| 937 | |
| 938 | if (typeof module !== 'undefined' && module.exports && typeof Offline !== 'undefined') |
| 939 | { module.exports = Offline; } |
projects-timfalken-com meteors