| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Deterministic Meteors game core. No DOM, no rendering, no input devices. |
| 5 | * The view layer reads this state and translates pointer events into |
| 6 | * beginDrag / moveDrag / endDrag. |
| 7 | */ |
| 8 | |
| 9 | var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined') |
| 10 | ? require('./planet.js') |
| 11 | : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet); |
| 12 | |
| 13 | var COLOR_GRAY = -1; |
| 14 | |
| 15 | var STATE_STILL = 'still'; |
| 16 | var STATE_FALLING = 'falling'; |
| 17 | var STATE_FLYING = 'flying'; |
| 18 | var STATE_SETTLING = 'settling'; |
| 19 | var STATE_LOCKED = 'locked'; |
| 20 | var STATE_MANUAL = 'manual'; |
| 21 | |
| 22 | var ALIGN_EPS = 0.15; |
| 23 | var SUBSTEP = 1 / 120; |
| 24 | var DRAG_LAUNCH_SLACK = 0.65; |
| 25 | |
| 26 | function UnionFind () |
| 27 | { |
| 28 | this.parent = Object.create(null); |
| 29 | } |
| 30 | |
| 31 | UnionFind.prototype.add = function (id) |
| 32 | { |
| 33 | if (this.parent[id] == null) |
| 34 | { this.parent[id] = id; } |
| 35 | }; |
| 36 | |
| 37 | UnionFind.prototype.find = function (id) |
| 38 | { |
| 39 | if (this.parent[id] !== id) |
| 40 | { this.parent[id] = this.find(this.parent[id]); } |
| 41 | return this.parent[id]; |
| 42 | }; |
| 43 | |
| 44 | UnionFind.prototype.union = function (a, b) |
| 45 | { |
| 46 | this.add(a); |
| 47 | this.add(b); |
| 48 | var ra = this.find(a); |
| 49 | var rb = this.find(b); |
| 50 | if (ra !== rb) |
| 51 | { this.parent[rb] = ra; } |
| 52 | }; |
| 53 | |
| 54 | function GameCore (planet, rng) |
| 55 | { |
| 56 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 57 | this.rng = rng || Math.random; |
| 58 | this.width = PlanetMod.FIELD_WIDTH; |
| 59 | this.height = PlanetMod.FIELD_HEIGHT; |
| 60 | this.blocks = []; |
| 61 | this.clusters = Object.create(null); |
| 62 | this.nextBlockId = 1; |
| 63 | this.nextClusterId = 1; |
| 64 | this.spawnTimer = 0; |
| 65 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 66 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 67 | this.score = 0; |
| 68 | this.state = 'playing'; |
| 69 | this.drag = null; |
| 70 | this.gameOverTime = 0; |
| 71 | } |
| 72 | |
| 73 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 74 | GameCore.STATE_STILL = STATE_STILL; |
| 75 | GameCore.STATE_FALLING = STATE_FALLING; |
| 76 | GameCore.STATE_FLYING = STATE_FLYING; |
| 77 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 78 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 79 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 80 | |
| 81 | GameCore.prototype.randomColor = function () |
| 82 | { |
| 83 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 84 | }; |
| 85 | |
| 86 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 87 | { |
| 88 | var block = { |
| 89 | id: this.nextBlockId++, |
| 90 | col: col, |
| 91 | y: y, |
| 92 | color: color, |
| 93 | state: state || STATE_STILL, |
| 94 | vy: 0, |
| 95 | clusterId: null, |
| 96 | settleTime: 0, |
| 97 | flame: false, |
| 98 | pushedClusters: null, |
| 99 | manualTarget: 0, |
| 100 | manualDecel: 0, |
| 101 | manualPhase: null |
| 102 | }; |
| 103 | this.blocks.push(block); |
| 104 | return block; |
| 105 | }; |
| 106 | |
| 107 | GameCore.prototype.removeBlock = function (block) |
| 108 | { |
| 109 | if (block.clusterId != null) |
| 110 | { this.leaveCluster(block); } |
| 111 | var index = this.blocks.indexOf(block); |
| 112 | if (index >= 0) |
| 113 | { this.blocks.splice(index, 1); } |
| 114 | }; |
| 115 | |
| 116 | GameCore.prototype.leaveCluster = function (block) |
| 117 | { |
| 118 | var id = block.clusterId; |
| 119 | block.clusterId = null; |
| 120 | block.flame = false; |
| 121 | if (id == null || !this.clusters[id]) |
| 122 | { return; } |
| 123 | var still = 0; |
| 124 | for (var i = 0; i < this.blocks.length; i++) |
| 125 | { |
| 126 | if (this.blocks[i].clusterId === id) |
| 127 | { still++; } |
| 128 | } |
| 129 | if (still === 0) |
| 130 | { delete this.clusters[id]; } |
| 131 | }; |
| 132 | |
| 133 | GameCore.prototype.blocksInColumn = function (col) |
| 134 | { |
| 135 | var out = []; |
| 136 | for (var i = 0; i < this.blocks.length; i++) |
| 137 | { |
| 138 | if (this.blocks[i].col === col) |
| 139 | { out.push(this.blocks[i]); } |
| 140 | } |
| 141 | out.sort(function (a, b) { return a.y - b.y; }); |
| 142 | return out; |
| 143 | }; |
| 144 | |
| 145 | GameCore.prototype.isGrounded = function (block) |
| 146 | { |
| 147 | return block.state === STATE_STILL |
| 148 | || block.state === STATE_SETTLING |
| 149 | || block.state === STATE_LOCKED; |
| 150 | }; |
| 151 | |
| 152 | GameCore.prototype.stackTop = function (col, ignore) |
| 153 | { |
| 154 | var top = 0; |
| 155 | for (var i = 0; i < this.blocks.length; i++) |
| 156 | { |
| 157 | var block = this.blocks[i]; |
| 158 | if (block.col !== col) |
| 159 | { continue; } |
| 160 | if (ignore && ignore(block)) |
| 161 | { continue; } |
| 162 | if (this.isGrounded(block)) |
| 163 | { |
| 164 | var surface = block.y + 1; |
| 165 | if (surface > top) |
| 166 | { top = surface; } |
| 167 | } |
| 168 | } |
| 169 | return top; |
| 170 | }; |
| 171 | |
| 172 | GameCore.prototype.isColumnLocked = function (col) |
| 173 | { |
| 174 | for (var i = 0; i < this.blocks.length; i++) |
| 175 | { |
| 176 | var block = this.blocks[i]; |
| 177 | if (block.col === col && block.state === STATE_LOCKED) |
| 178 | { return true; } |
| 179 | } |
| 180 | return false; |
| 181 | }; |
| 182 | |
| 183 | GameCore.prototype.isColumnStill = function (col) |
| 184 | { |
| 185 | if (this.isColumnLocked(col)) |
| 186 | { return false; } |
| 187 | for (var i = 0; i < this.blocks.length; i++) |
| 188 | { |
| 189 | var block = this.blocks[i]; |
| 190 | if (block.col !== col) |
| 191 | { continue; } |
| 192 | if (block.state === STATE_FLYING |
| 193 | || block.state === STATE_FALLING |
| 194 | || block.state === STATE_MANUAL |
| 195 | || block.state === STATE_SETTLING) |
| 196 | { return false; } |
| 197 | } |
| 198 | return true; |
| 199 | }; |
| 200 | |
| 201 | GameCore.prototype.groundedCount = function (col) |
| 202 | { |
| 203 | var n = 0; |
| 204 | for (var i = 0; i < this.blocks.length; i++) |
| 205 | { |
| 206 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 207 | { n++; } |
| 208 | } |
| 209 | return n; |
| 210 | }; |
| 211 | |
| 212 | GameCore.prototype.blockAt = function (col, row) |
| 213 | { |
| 214 | for (var i = 0; i < this.blocks.length; i++) |
| 215 | { |
| 216 | var block = this.blocks[i]; |
| 217 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 218 | { return block; } |
| 219 | } |
| 220 | return null; |
| 221 | }; |
| 222 | |
| 223 | GameCore.prototype.place = function (col, row, color, state) |
| 224 | { |
| 225 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 226 | }; |
| 227 | |
| 228 | GameCore.prototype.lockedColumnCount = function () |
| 229 | { |
| 230 | var n = 0; |
| 231 | for (var c = 0; c < this.width; c++) |
| 232 | { |
| 233 | if (this.isColumnLocked(c)) |
| 234 | { n++; } |
| 235 | } |
| 236 | return n; |
| 237 | }; |
| 238 | |
| 239 | GameCore.prototype.tick = function (dt) |
| 240 | { |
| 241 | if (this.state === 'gameover') |
| 242 | { |
| 243 | this.gameOverTime += Math.max(0, dt); |
| 244 | return; |
| 245 | } |
| 246 | if (dt <= 0) |
| 247 | { return; } |
| 248 | dt = Math.min(dt, 0.1); |
| 249 | |
| 250 | this.spawnTimer += dt; |
| 251 | while (this.spawnTimer >= this.spawnInterval) |
| 252 | { |
| 253 | this.spawnTimer -= this.spawnInterval; |
| 254 | this.trySpawn(); |
| 255 | } |
| 256 | |
| 257 | var left = dt; |
| 258 | while (left > 1e-12) |
| 259 | { |
| 260 | var step = Math.min(SUBSTEP, left); |
| 261 | this.integrateFalling(step); |
| 262 | this.integrateClusters(step); |
| 263 | this.integrateManual(step); |
| 264 | this.cullOffTop(); |
| 265 | left -= step; |
| 266 | } |
| 267 | |
| 268 | this.advanceSettle(dt); |
| 269 | if (!this.drag) |
| 270 | { this.resolveMatches(); } |
| 271 | this.checkLocks(); |
| 272 | this.checkGameOver(); |
| 273 | }; |
| 274 | |
| 275 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 276 | GameCore.prototype.step = function (seconds, dt) |
| 277 | { |
| 278 | dt = dt || SUBSTEP; |
| 279 | var t = 0; |
| 280 | while (t < seconds - 1e-15) |
| 281 | { |
| 282 | var s = Math.min(dt, seconds - t); |
| 283 | this.tick(s); |
| 284 | t += s; |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | GameCore.prototype.trySpawn = function () |
| 289 | { |
| 290 | if (this.state !== 'playing') |
| 291 | { return null; } |
| 292 | var eligible = []; |
| 293 | for (var col = 0; col < this.width; col++) |
| 294 | { |
| 295 | if (this.isColumnLocked(col)) |
| 296 | { continue; } |
| 297 | if (this.groundedCount(col) >= this.height) |
| 298 | { continue; } |
| 299 | var alreadyFalling = false; |
| 300 | var list = this.blocksInColumn(col); |
| 301 | for (var i = 0; i < list.length; i++) |
| 302 | { |
| 303 | if (list[i].state === STATE_FALLING) |
| 304 | { |
| 305 | alreadyFalling = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | if (!alreadyFalling) |
| 310 | { eligible.push(col); } |
| 311 | } |
| 312 | if (!eligible.length) |
| 313 | { return null; } |
| 314 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 315 | var color = this.randomColor(); |
| 316 | return this.createBlock(col, this.height, color, STATE_FALLING); |
| 317 | }; |
| 318 | |
| 319 | GameCore.prototype.integrateFalling = function (dt) |
| 320 | { |
| 321 | var speed = this.planet.fallSpeed; |
| 322 | for (var i = 0; i < this.blocks.length; i++) |
| 323 | { |
| 324 | var block = this.blocks[i]; |
| 325 | if (block.state !== STATE_FALLING) |
| 326 | { continue; } |
| 327 | var top = this.stackTop(block.col); |
| 328 | var nextY = block.y - speed * dt; |
| 329 | if (nextY <= top) |
| 330 | { |
| 331 | block.y = top; |
| 332 | block.state = STATE_STILL; |
| 333 | block.vy = 0; |
| 334 | } |
| 335 | else |
| 336 | { block.y = nextY; } |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | GameCore.prototype.clusterMembers = function (clusterId) |
| 341 | { |
| 342 | var out = []; |
| 343 | for (var i = 0; i < this.blocks.length; i++) |
| 344 | { |
| 345 | if (this.blocks[i].clusterId === clusterId) |
| 346 | { out.push(this.blocks[i]); } |
| 347 | } |
| 348 | return out; |
| 349 | }; |
| 350 | |
| 351 | |
| 352 | /** |
| 353 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 354 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 355 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 356 | * upward deceleration; falling uses planet.fallSpeed. |
| 357 | */ |
| 358 | GameCore.prototype.advanceJump = function (body, dt) |
| 359 | { |
| 360 | var dy = 0; |
| 361 | var left = dt; |
| 362 | if (body.phase === 'up') |
| 363 | { |
| 364 | var decel = body.decel; |
| 365 | var vy = body.vy; |
| 366 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 367 | if (tApex <= left + 1e-15) |
| 368 | { |
| 369 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 370 | body.vy = 0; |
| 371 | body.phase = 'down'; |
| 372 | left -= tApex; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | var vyNew = vy - decel * left; |
| 377 | dy += (vy + vyNew) * 0.5 * left; |
| 378 | body.vy = vyNew; |
| 379 | left = 0; |
| 380 | } |
| 381 | } |
| 382 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 383 | { |
| 384 | body.vy = 0; |
| 385 | body.phase = 'down'; |
| 386 | } |
| 387 | if (body.phase === 'down' && left > 0) |
| 388 | { |
| 389 | body.vy = -this.planet.fallSpeed; |
| 390 | dy += body.vy * left; |
| 391 | } |
| 392 | return dy; |
| 393 | }; |
| 394 | |
| 395 | GameCore.prototype.integrateClusters = function (dt) |
| 396 | { |
| 397 | var ids = Object.keys(this.clusters); |
| 398 | for (var n = 0; n < ids.length; n++) |
| 399 | { |
| 400 | var cluster = this.clusters[ids[n]]; |
| 401 | if (!cluster) |
| 402 | { continue; } |
| 403 | var members = this.clusterMembers(cluster.id); |
| 404 | if (!members.length) |
| 405 | { |
| 406 | delete this.clusters[cluster.id]; |
| 407 | continue; |
| 408 | } |
| 409 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 410 | { continue; } |
| 411 | |
| 412 | var dy = this.advanceJump(cluster, dt); |
| 413 | for (var i = 0; i < members.length; i++) |
| 414 | { |
| 415 | members[i].y += dy; |
| 416 | members[i].vy = cluster.vy; |
| 417 | } |
| 418 | |
| 419 | if (cluster.phase === 'down') |
| 420 | { this.tryLandCluster(cluster, members); } |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 425 | { |
| 426 | var self = this; |
| 427 | var hit = false; |
| 428 | for (var i = 0; i < members.length; i++) |
| 429 | { |
| 430 | var block = members[i]; |
| 431 | var top = this.stackTop(block.col, function (other) |
| 432 | { |
| 433 | return other.clusterId === cluster.id; |
| 434 | }); |
| 435 | if (block.y <= top + 1e-9) |
| 436 | { hit = true; } |
| 437 | } |
| 438 | if (!hit) |
| 439 | { return; } |
| 440 | this.landCluster(cluster, members); |
| 441 | }; |
| 442 | |
| 443 | GameCore.prototype.landCluster = function (cluster, members) |
| 444 | { |
| 445 | var byCol = Object.create(null); |
| 446 | for (var i = 0; i < members.length; i++) |
| 447 | { |
| 448 | var block = members[i]; |
| 449 | if (!byCol[block.col]) |
| 450 | { byCol[block.col] = []; } |
| 451 | byCol[block.col].push(block); |
| 452 | } |
| 453 | var cols = Object.keys(byCol); |
| 454 | for (var c = 0; c < cols.length; c++) |
| 455 | { |
| 456 | var col = +cols[c]; |
| 457 | var list = byCol[col]; |
| 458 | list.sort(function (a, b) { return a.y - b.y; }); |
| 459 | var self = this; |
| 460 | var top = this.stackTop(col, function (other) |
| 461 | { |
| 462 | return other.clusterId === cluster.id; |
| 463 | }); |
| 464 | for (var i = 0; i < list.length; i++) |
| 465 | { |
| 466 | var block = list[i]; |
| 467 | block.y = top; |
| 468 | block.vy = 0; |
| 469 | block.flame = false; |
| 470 | top = block.y + 1; |
| 471 | if (block.color === COLOR_GRAY) |
| 472 | { |
| 473 | block.state = STATE_SETTLING; |
| 474 | block.settleTime = 0; |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | block.state = STATE_STILL; |
| 479 | block.clusterId = null; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | cluster.phase = 'landed'; |
| 484 | cluster.vy = 0; |
| 485 | var remaining = 0; |
| 486 | for (var i = 0; i < this.blocks.length; i++) |
| 487 | { |
| 488 | if (this.blocks[i].clusterId === cluster.id) |
| 489 | { remaining++; } |
| 490 | } |
| 491 | if (remaining === 0) |
| 492 | { delete this.clusters[cluster.id]; } |
| 493 | }; |
| 494 | |
| 495 | GameCore.prototype.integrateManual = function (dt) |
| 496 | { |
| 497 | for (var i = 0; i < this.blocks.length; i++) |
| 498 | { |
| 499 | var block = this.blocks[i]; |
| 500 | if (block.state !== STATE_MANUAL) |
| 501 | { continue; } |
| 502 | |
| 503 | if (block.manualPhase === 'up') |
| 504 | { |
| 505 | var oldY = block.y; |
| 506 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 507 | var dy = this.advanceJump(proxy, dt); |
| 508 | var nextY = oldY + dy; |
| 509 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 510 | block.vy = proxy.vy; |
| 511 | block.manualPhase = proxy.phase; |
| 512 | block.y = Math.min(nextY, block.manualTarget); |
| 513 | if (block.y >= block.manualTarget - 1e-12) |
| 514 | { |
| 515 | block.y = block.manualTarget; |
| 516 | block.vy = 0; |
| 517 | block.manualPhase = 'down'; |
| 518 | } |
| 519 | if (block.y > this.height - 1) |
| 520 | { |
| 521 | block.y = this.height - 1; |
| 522 | block.vy = 0; |
| 523 | block.manualPhase = 'down'; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (block.manualPhase === 'down') |
| 528 | { |
| 529 | var top = this.stackTop(block.col); |
| 530 | var nextY = block.y - this.planet.fallSpeed * dt; |
| 531 | if (nextY <= top) |
| 532 | { |
| 533 | block.y = top; |
| 534 | block.state = STATE_STILL; |
| 535 | block.vy = 0; |
| 536 | block.manualPhase = null; |
| 537 | block.flame = false; |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | block.y = Math.min(nextY, this.height - 1); |
| 542 | block.vy = -this.planet.fallSpeed; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 549 | { |
| 550 | if (!manual.pushedClusters) |
| 551 | { manual.pushedClusters = Object.create(null); } |
| 552 | |
| 553 | var manualTopOld = oldY + 1; |
| 554 | var manualTopNew = nextY + 1; |
| 555 | var low = Math.min(oldY, nextY); |
| 556 | var high = Math.max(oldY, nextY) + 1; |
| 557 | |
| 558 | for (var i = 0; i < this.blocks.length; i++) |
| 559 | { |
| 560 | var other = this.blocks[i]; |
| 561 | if (other === manual) |
| 562 | { continue; } |
| 563 | if (other.col !== manual.col) |
| 564 | { continue; } |
| 565 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 566 | { continue; } |
| 567 | if (manual.pushedClusters[other.clusterId]) |
| 568 | { continue; } |
| 569 | var otherBot = other.y; |
| 570 | var otherTop = other.y + 1; |
| 571 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 572 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 573 | if (overlapping || crossing) |
| 574 | { |
| 575 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 576 | { |
| 577 | manual.pushedClusters[other.clusterId] = true; |
| 578 | return; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | }; |
| 583 | |
| 584 | |
| 585 | /** |
| 586 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 587 | * Other columns stay put (they remain linked). Then look for a new |
| 588 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 589 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 590 | * with the same jump height from its current position. |
| 591 | */ |
| 592 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 593 | { |
| 594 | var cluster = this.clusters[clusterId]; |
| 595 | if (!cluster) |
| 596 | { return false; } |
| 597 | var members = this.clusterMembers(clusterId); |
| 598 | for (var i = 0; i < members.length; i++) |
| 599 | { |
| 600 | if (members[i].col === col) |
| 601 | { members[i].y += 1; } |
| 602 | } |
| 603 | this.cullOffTop(); |
| 604 | if (!this.clusters[clusterId]) |
| 605 | { return true; } |
| 606 | this.checkChainRelaunch(cluster); |
| 607 | return true; |
| 608 | }; |
| 609 | |
| 610 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 611 | { |
| 612 | if (!this.clusters[cluster.id]) |
| 613 | { return; } |
| 614 | var lines = this.findFlyingMatchLines(cluster.id); |
| 615 | if (!lines.length) |
| 616 | { return; } |
| 617 | var newly = []; |
| 618 | var seen = Object.create(null); |
| 619 | for (var i = 0; i < lines.length; i++) |
| 620 | { |
| 621 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 622 | { |
| 623 | var block = lines[i].blocks[j]; |
| 624 | if (seen[block.id]) |
| 625 | { continue; } |
| 626 | seen[block.id] = true; |
| 627 | newly.push(block); |
| 628 | } |
| 629 | } |
| 630 | for (var i = 0; i < newly.length; i++) |
| 631 | { |
| 632 | var block = newly[i]; |
| 633 | block.color = COLOR_GRAY; |
| 634 | block.flame = true; |
| 635 | block.clusterId = cluster.id; |
| 636 | block.state = STATE_FLYING; |
| 637 | } |
| 638 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 639 | }; |
| 640 | |
| 641 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 642 | { |
| 643 | cluster.jumpHeight = height; |
| 644 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 645 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 646 | cluster.phase = 'up'; |
| 647 | var members = this.clusterMembers(cluster.id); |
| 648 | for (var i = 0; i < members.length; i++) |
| 649 | { |
| 650 | members[i].vy = cluster.vy; |
| 651 | members[i].state = STATE_FLYING; |
| 652 | if (members[i].color === COLOR_GRAY) |
| 653 | { members[i].flame = true; } |
| 654 | } |
| 655 | }; |
| 656 | |
| 657 | GameCore.prototype.startManualLaunch = function (block) |
| 658 | { |
| 659 | var target = this.height - 1; |
| 660 | var height = Math.max(0.05, target - block.y); |
| 661 | block.state = STATE_MANUAL; |
| 662 | block.pushedClusters = Object.create(null); |
| 663 | block.manualTarget = target; |
| 664 | block.manualPhase = 'up'; |
| 665 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 666 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 667 | block.flame = false; |
| 668 | if (this.drag && this.drag.blockId === block.id) |
| 669 | { this.drag = null; } |
| 670 | }; |
| 671 | |
| 672 | GameCore.prototype.cullOffTop = function () |
| 673 | { |
| 674 | var gone = []; |
| 675 | for (var i = 0; i < this.blocks.length; i++) |
| 676 | { |
| 677 | var block = this.blocks[i]; |
| 678 | if (block.state === STATE_MANUAL) |
| 679 | { |
| 680 | if (block.y > this.height - 1) |
| 681 | { block.y = this.height - 1; } |
| 682 | continue; |
| 683 | } |
| 684 | if (block.y >= this.height - 1e-9) |
| 685 | { gone.push(block); } |
| 686 | } |
| 687 | for (var i = 0; i < gone.length; i++) |
| 688 | { |
| 689 | this.score += 1; |
| 690 | this.removeBlock(gone[i]); |
| 691 | } |
| 692 | }; |
| 693 | |
| 694 | GameCore.prototype.advanceSettle = function (dt) |
| 695 | { |
| 696 | for (var i = 0; i < this.blocks.length; i++) |
| 697 | { |
| 698 | var block = this.blocks[i]; |
| 699 | if (block.state !== STATE_SETTLING) |
| 700 | { continue; } |
| 701 | block.settleTime += dt; |
| 702 | if (block.settleTime >= this.settleTime) |
| 703 | { |
| 704 | block.color = this.randomColor(); |
| 705 | block.state = STATE_STILL; |
| 706 | block.settleTime = 0; |
| 707 | block.flame = false; |
| 708 | this.leaveCluster(block); |
| 709 | } |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | GameCore.prototype.findMatchLines = function () |
| 714 | { |
| 715 | var candidates = []; |
| 716 | for (var i = 0; i < this.blocks.length; i++) |
| 717 | { |
| 718 | var block = this.blocks[i]; |
| 719 | if (block.state === STATE_STILL && block.color >= 0) |
| 720 | { candidates.push(block); } |
| 721 | } |
| 722 | return this.collectLines(candidates, true); |
| 723 | }; |
| 724 | |
| 725 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 726 | { |
| 727 | var candidates = []; |
| 728 | for (var i = 0; i < this.blocks.length; i++) |
| 729 | { |
| 730 | var block = this.blocks[i]; |
| 731 | if (block.clusterId === clusterId && block.color >= 0 |
| 732 | && (block.state === STATE_FLYING)) |
| 733 | { candidates.push(block); } |
| 734 | } |
| 735 | return this.collectLines(candidates, false); |
| 736 | }; |
| 737 | |
| 738 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 739 | { |
| 740 | var lines = []; |
| 741 | if (!candidates.length) |
| 742 | { return lines; } |
| 743 | |
| 744 | var byRow = Object.create(null); |
| 745 | for (var i = 0; i < candidates.length; i++) |
| 746 | { |
| 747 | var block = candidates[i]; |
| 748 | var row = Math.round(block.y); |
| 749 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 750 | { continue; } |
| 751 | if (!byRow[row]) |
| 752 | { byRow[row] = []; } |
| 753 | byRow[row].push(block); |
| 754 | } |
| 755 | var rows = Object.keys(byRow); |
| 756 | for (var r = 0; r < rows.length; r++) |
| 757 | { |
| 758 | var list = byRow[rows[r]]; |
| 759 | list.sort(function (a, b) { return a.col - b.col; }); |
| 760 | this.pushRuns(lines, list, false, function (a, b) |
| 761 | { |
| 762 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 763 | }); |
| 764 | } |
| 765 | |
| 766 | var byCol = Object.create(null); |
| 767 | for (var i = 0; i < candidates.length; i++) |
| 768 | { |
| 769 | var block = candidates[i]; |
| 770 | if (!byCol[block.col]) |
| 771 | { byCol[block.col] = []; } |
| 772 | byCol[block.col].push(block); |
| 773 | } |
| 774 | var cols = Object.keys(byCol); |
| 775 | for (var c = 0; c < cols.length; c++) |
| 776 | { |
| 777 | var list = byCol[cols[c]]; |
| 778 | list.sort(function (a, b) { return a.y - b.y; }); |
| 779 | this.pushRuns(lines, list, true, function (a, b) |
| 780 | { |
| 781 | return a.color === b.color && a.color >= 0 |
| 782 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 783 | }); |
| 784 | } |
| 785 | return lines; |
| 786 | }; |
| 787 | |
| 788 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 789 | { |
| 790 | var start = 0; |
| 791 | for (var i = 1; i <= list.length; i++) |
| 792 | { |
| 793 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 794 | if (!same) |
| 795 | { |
| 796 | var length = i - start; |
| 797 | if (length >= 3) |
| 798 | { |
| 799 | lines.push({ |
| 800 | blocks: list.slice(start, i), |
| 801 | length: length, |
| 802 | vertical: vertical |
| 803 | }); |
| 804 | } |
| 805 | start = i; |
| 806 | } |
| 807 | } |
| 808 | }; |
| 809 | |
| 810 | GameCore.prototype.resolveMatches = function () |
| 811 | { |
| 812 | if (this.state !== 'playing' || this.drag) |
| 813 | { return []; } |
| 814 | var lines = this.findMatchLines(); |
| 815 | if (!lines.length) |
| 816 | { return []; } |
| 817 | |
| 818 | var uf = new UnionFind(); |
| 819 | var blockById = Object.create(null); |
| 820 | for (var i = 0; i < lines.length; i++) |
| 821 | { |
| 822 | var line = lines[i]; |
| 823 | for (var j = 0; j < line.blocks.length; j++) |
| 824 | { |
| 825 | uf.add(line.blocks[j].id); |
| 826 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 827 | if (j > 0) |
| 828 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 829 | } |
| 830 | } |
| 831 | var matched = []; |
| 832 | var seen = Object.create(null); |
| 833 | for (var id in blockById) |
| 834 | { |
| 835 | if (!seen[id]) |
| 836 | { |
| 837 | seen[id] = true; |
| 838 | matched.push(blockById[id]); |
| 839 | } |
| 840 | } |
| 841 | for (var i = 0; i < matched.length; i++) |
| 842 | { |
| 843 | for (var j = i + 1; j < matched.length; j++) |
| 844 | { |
| 845 | if (matched[i].col === matched[j].col) |
| 846 | { uf.union(matched[i].id, matched[j].id); } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | var groups = Object.create(null); |
| 851 | for (var i = 0; i < matched.length; i++) |
| 852 | { |
| 853 | var root = uf.find(matched[i].id); |
| 854 | if (!groups[root]) |
| 855 | { groups[root] = []; } |
| 856 | groups[root].push(matched[i]); |
| 857 | } |
| 858 | |
| 859 | var launched = []; |
| 860 | for (var root in groups) |
| 861 | { |
| 862 | var group = groups[root]; |
| 863 | var height = 0; |
| 864 | for (var i = 0; i < lines.length; i++) |
| 865 | { |
| 866 | var line = lines[i]; |
| 867 | var overlaps = false; |
| 868 | for (var j = 0; j < line.blocks.length; j++) |
| 869 | { |
| 870 | for (var k = 0; k < group.length; k++) |
| 871 | { |
| 872 | if (line.blocks[j].id === group[k].id) |
| 873 | { overlaps = true; } |
| 874 | } |
| 875 | } |
| 876 | if (overlaps) |
| 877 | { |
| 878 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 879 | if (h > height) |
| 880 | { height = h; } |
| 881 | } |
| 882 | } |
| 883 | this.launchCluster(group, height); |
| 884 | launched.push({ blocks: group, jumpHeight: height }); |
| 885 | } |
| 886 | return launched; |
| 887 | }; |
| 888 | |
| 889 | /** |
| 890 | * Gray the matched blocks, take everything sitting on top of them in |
| 891 | * those columns, link them, and launch with jump height `height`. |
| 892 | */ |
| 893 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 894 | { |
| 895 | var lowestInCol = Object.create(null); |
| 896 | var matchedIds = Object.create(null); |
| 897 | for (var i = 0; i < matchedBlocks.length; i++) |
| 898 | { |
| 899 | var block = matchedBlocks[i]; |
| 900 | matchedIds[block.id] = true; |
| 901 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 902 | { lowestInCol[block.col] = block.y; } |
| 903 | } |
| 904 | |
| 905 | var clusterId = this.nextClusterId++; |
| 906 | var cluster = { |
| 907 | id: clusterId, |
| 908 | jumpHeight: height, |
| 909 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 910 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 911 | phase: 'up' |
| 912 | }; |
| 913 | this.clusters[clusterId] = cluster; |
| 914 | |
| 915 | var riders = []; |
| 916 | var cols = Object.keys(lowestInCol); |
| 917 | for (var c = 0; c < cols.length; c++) |
| 918 | { |
| 919 | var col = +cols[c]; |
| 920 | var lowest = lowestInCol[col]; |
| 921 | var list = this.blocksInColumn(col); |
| 922 | for (var i = 0; i < list.length; i++) |
| 923 | { |
| 924 | var block = list[i]; |
| 925 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 926 | { |
| 927 | if (block.state !== STATE_SETTLING) |
| 928 | { continue; } |
| 929 | } |
| 930 | if (block.y + ALIGN_EPS >= lowest) |
| 931 | { riders.push(block); } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | for (var i = 0; i < riders.length; i++) |
| 936 | { |
| 937 | var block = riders[i]; |
| 938 | var isMatch = !!matchedIds[block.id]; |
| 939 | if (isMatch || block.color === COLOR_GRAY) |
| 940 | { |
| 941 | block.color = COLOR_GRAY; |
| 942 | block.flame = true; |
| 943 | } |
| 944 | else |
| 945 | { block.flame = false; } |
| 946 | block.state = STATE_FLYING; |
| 947 | block.clusterId = clusterId; |
| 948 | block.vy = cluster.vy; |
| 949 | block.settleTime = 0; |
| 950 | } |
| 951 | return cluster; |
| 952 | }; |
| 953 | |
| 954 | GameCore.prototype.checkLocks = function () |
| 955 | { |
| 956 | for (var col = 0; col < this.width; col++) |
| 957 | { |
| 958 | if (this.isColumnLocked(col)) |
| 959 | { continue; } |
| 960 | if (this.groundedCount(col) < this.height) |
| 961 | { continue; } |
| 962 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 963 | var filled = true; |
| 964 | for (var row = 0; row < this.height; row++) |
| 965 | { |
| 966 | var block = this.blockAt(col, row); |
| 967 | if (!block || !this.isGrounded(block)) |
| 968 | { |
| 969 | filled = false; |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | if (!filled) |
| 974 | { continue; } |
| 975 | var list = this.blocksInColumn(col); |
| 976 | for (var i = 0; i < list.length; i++) |
| 977 | { |
| 978 | var block = list[i]; |
| 979 | if (!this.isGrounded(block)) |
| 980 | { continue; } |
| 981 | block.state = STATE_LOCKED; |
| 982 | block.color = COLOR_GRAY; |
| 983 | block.flame = false; |
| 984 | block.vy = 0; |
| 985 | block.settleTime = 0; |
| 986 | if (block.clusterId != null) |
| 987 | { this.leaveCluster(block); } |
| 988 | } |
| 989 | } |
| 990 | }; |
| 991 | |
| 992 | GameCore.prototype.checkGameOver = function () |
| 993 | { |
| 994 | if (this.state !== 'playing') |
| 995 | { return; } |
| 996 | for (var col = 0; col < this.width; col++) |
| 997 | { |
| 998 | if (!this.isColumnLocked(col)) |
| 999 | { return; } |
| 1000 | } |
| 1001 | this.state = 'gameover'; |
| 1002 | this.gameOverTime = 0; |
| 1003 | this.drag = null; |
| 1004 | }; |
| 1005 | |
| 1006 | /* ---------- dragging -------------------------------------------------- */ |
| 1007 | |
| 1008 | GameCore.prototype.beginDrag = function (col, y) |
| 1009 | { |
| 1010 | if (this.state !== 'playing') |
| 1011 | { return false; } |
| 1012 | col = col | 0; |
| 1013 | if (col < 0 || col >= this.width) |
| 1014 | { return false; } |
| 1015 | if (!this.isColumnStill(col)) |
| 1016 | { return false; } |
| 1017 | var row = Math.floor(y + 1e-6); |
| 1018 | if (row < 0) |
| 1019 | { return false; } |
| 1020 | var block = this.blockAt(col, row); |
| 1021 | if (!block || block.state !== STATE_STILL || block.color < 0) |
| 1022 | { return false; } |
| 1023 | this.drag = { blockId: block.id, pointerY: y, launchIntent: false }; |
| 1024 | return true; |
| 1025 | }; |
| 1026 | |
| 1027 | GameCore.prototype.moveDrag = function (y) |
| 1028 | { |
| 1029 | if (!this.drag) |
| 1030 | { return; } |
| 1031 | var block = this.blockById(this.drag.blockId); |
| 1032 | if (!block || block.state !== STATE_STILL) |
| 1033 | { |
| 1034 | this.drag = null; |
| 1035 | return; |
| 1036 | } |
| 1037 | if (!this.isColumnStill(block.col)) |
| 1038 | { |
| 1039 | this.drag = null; |
| 1040 | return; |
| 1041 | } |
| 1042 | this.drag.pointerY = y; |
| 1043 | this.swapToward(block, y); |
| 1044 | var top = this.columnStackTopRow(block.col); |
| 1045 | // The top block occupies [top, top+1). Launch only when the pointer |
| 1046 | // is pulled into the empty space above the stack. |
| 1047 | this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25); |
| 1048 | }; |
| 1049 | |
| 1050 | GameCore.prototype.endDrag = function () |
| 1051 | { |
| 1052 | if (!this.drag) |
| 1053 | { return; } |
| 1054 | var block = this.blockById(this.drag.blockId); |
| 1055 | var launch = this.drag.launchIntent; |
| 1056 | this.drag = null; |
| 1057 | if (launch && block && block.state === STATE_STILL) |
| 1058 | { this.startManualLaunch(block); } |
| 1059 | else |
| 1060 | { this.resolveMatches(); } |
| 1061 | }; |
| 1062 | |
| 1063 | GameCore.prototype.blockById = function (id) |
| 1064 | { |
| 1065 | for (var i = 0; i < this.blocks.length; i++) |
| 1066 | { |
| 1067 | if (this.blocks[i].id === id) |
| 1068 | { return this.blocks[i]; } |
| 1069 | } |
| 1070 | return null; |
| 1071 | }; |
| 1072 | |
| 1073 | GameCore.prototype.columnStackTopRow = function (col) |
| 1074 | { |
| 1075 | var top = -1; |
| 1076 | var list = this.blocksInColumn(col); |
| 1077 | for (var i = 0; i < list.length; i++) |
| 1078 | { |
| 1079 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 1080 | { top = list[i].y; } |
| 1081 | } |
| 1082 | return top; |
| 1083 | }; |
| 1084 | |
| 1085 | GameCore.prototype.swapToward = function (block, targetY) |
| 1086 | { |
| 1087 | var guard = 0; |
| 1088 | while (guard++ < this.height + 2) |
| 1089 | { |
| 1090 | var current = block.y; |
| 1091 | if (targetY > current + 0.5) |
| 1092 | { |
| 1093 | var above = this.blockAt(block.col, current + 1); |
| 1094 | if (above && this.isGrounded(above) && above !== block) |
| 1095 | { this.swapBlocks(block, above); } |
| 1096 | else |
| 1097 | { break; } |
| 1098 | } |
| 1099 | else if (targetY < current - 0.5) |
| 1100 | { |
| 1101 | var below = this.blockAt(block.col, current - 1); |
| 1102 | if (below && this.isGrounded(below) && below !== block) |
| 1103 | { this.swapBlocks(block, below); } |
| 1104 | else |
| 1105 | { break; } |
| 1106 | } |
| 1107 | else |
| 1108 | { break; } |
| 1109 | } |
| 1110 | }; |
| 1111 | |
| 1112 | GameCore.prototype.swapBlocks = function (a, b) |
| 1113 | { |
| 1114 | var y = a.y; |
| 1115 | a.y = b.y; |
| 1116 | b.y = y; |
| 1117 | }; |
| 1118 | |
| 1119 | /** |
| 1120 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 1121 | * Returns the moved block, or null if the column is not still. |
| 1122 | */ |
| 1123 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 1124 | { |
| 1125 | if (!this.isColumnStill(col)) |
| 1126 | { return null; } |
| 1127 | var block = this.blockAt(col, fromRow); |
| 1128 | if (!block) |
| 1129 | { return null; } |
| 1130 | this.swapToward(block, toRow); |
| 1131 | return block; |
| 1132 | }; |
| 1133 | |
| 1134 | /** |
| 1135 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 1136 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 1137 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 1138 | * their color with a marker in `flying`. |
| 1139 | */ |
| 1140 | GameCore.prototype.snapshot = function () |
| 1141 | { |
| 1142 | var grid = []; |
| 1143 | for (var r = 0; r < this.height; r++) |
| 1144 | { |
| 1145 | var row = []; |
| 1146 | for (var c = 0; c < this.width; c++) |
| 1147 | { row.push('.'); } |
| 1148 | grid.push(row); |
| 1149 | } |
| 1150 | var airborne = []; |
| 1151 | for (var i = 0; i < this.blocks.length; i++) |
| 1152 | { |
| 1153 | var block = this.blocks[i]; |
| 1154 | var ch; |
| 1155 | if (block.state === STATE_LOCKED) |
| 1156 | { ch = 'L'; } |
| 1157 | else if (block.color === COLOR_GRAY) |
| 1158 | { ch = 'g'; } |
| 1159 | else |
| 1160 | { ch = String(block.color); } |
| 1161 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 1162 | { |
| 1163 | var row = Math.round(block.y); |
| 1164 | if (row >= 0 && row < this.height) |
| 1165 | { grid[row][block.col] = ch; } |
| 1166 | } |
| 1167 | else |
| 1168 | { |
| 1169 | airborne.push({ |
| 1170 | id: block.id, |
| 1171 | col: block.col, |
| 1172 | y: block.y, |
| 1173 | color: block.color, |
| 1174 | state: block.state, |
| 1175 | clusterId: block.clusterId, |
| 1176 | flame: block.flame, |
| 1177 | vy: block.vy |
| 1178 | }); |
| 1179 | } |
| 1180 | } |
| 1181 | var lines = []; |
| 1182 | for (var r = this.height - 1; r >= 0; r--) |
| 1183 | { lines.push(grid[r].join('')); } |
| 1184 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 1185 | }; |
| 1186 | |
| 1187 | var GameCoreAPI = { |
| 1188 | GameCore: GameCore, |
| 1189 | COLOR_GRAY: COLOR_GRAY, |
| 1190 | STATE_STILL: STATE_STILL, |
| 1191 | STATE_FALLING: STATE_FALLING, |
| 1192 | STATE_FLYING: STATE_FLYING, |
| 1193 | STATE_SETTLING: STATE_SETTLING, |
| 1194 | STATE_LOCKED: STATE_LOCKED, |
| 1195 | STATE_MANUAL: STATE_MANUAL |
| 1196 | }; |
| 1197 | |
| 1198 | if (typeof module !== 'undefined' && module.exports) |
| 1199 | { module.exports = GameCoreAPI; } |
| 1200 | else if (typeof globalThis !== 'undefined') |
| 1201 | { |
| 1202 | globalThis.GameCore = GameCore; |
| 1203 | globalThis.GameCoreAPI = GameCoreAPI; |
| 1204 | } |
projects-timfalken-com meteors