| 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 | // Upward pull (tiles past the press point) that launches an already-topmost block. |
| 26 | var LAUNCH_PULL = 0.35; |
| 27 | // Minimal accumulated vertical pointer travel (tiles) to swap one step. |
| 28 | // No hold delay: the first swap fires as soon as this distance is reached. |
| 29 | var SWAP_DISTANCE = 0.25; |
| 30 | // Fallback snap (tiles) when the press cell is empty: nearest block in the column. |
| 31 | var SNAP_Y = 0.5; |
| 32 | // Horizontal slop (tiles) for snapping to a neighbouring column on press. |
| 33 | var HORIZONTAL_SLOP = 0.35; |
| 34 | /** t1120: additive jump bonus per landing-chain step. A landing that |
| 35 | * immediately makes a match launches at (1 + 0.25 * chain) times the |
| 36 | * usual height, with chain 1, 2, 3, … → ×1.25, ×1.50, ×1.75. */ |
| 37 | var LANDING_CHAIN_BONUS = 0.25; |
| 38 | /** t1123: a column pulses red at or above this many grounded rows |
| 39 | * (10 of 13 is just over 70%). */ |
| 40 | var COLUMN_WARN_ROWS = 10; |
| 41 | |
| 42 | function UnionFind () |
| 43 | { |
| 44 | this.parent = Object.create(null); |
| 45 | } |
| 46 | |
| 47 | UnionFind.prototype.add = function (id) |
| 48 | { |
| 49 | if (this.parent[id] == null) |
| 50 | { this.parent[id] = id; } |
| 51 | }; |
| 52 | |
| 53 | UnionFind.prototype.find = function (id) |
| 54 | { |
| 55 | if (this.parent[id] !== id) |
| 56 | { this.parent[id] = this.find(this.parent[id]); } |
| 57 | return this.parent[id]; |
| 58 | }; |
| 59 | |
| 60 | UnionFind.prototype.union = function (a, b) |
| 61 | { |
| 62 | this.add(a); |
| 63 | this.add(b); |
| 64 | var ra = this.find(a); |
| 65 | var rb = this.find(b); |
| 66 | if (ra !== rb) |
| 67 | { this.parent[rb] = ra; } |
| 68 | }; |
| 69 | |
| 70 | function GameCore (planet, rng) |
| 71 | { |
| 72 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 73 | this.rng = rng || Math.random; |
| 74 | this.width = PlanetMod.FIELD_WIDTH; |
| 75 | this.height = PlanetMod.FIELD_HEIGHT; |
| 76 | this.blocks = []; |
| 77 | this.clusters = Object.create(null); |
| 78 | this.nextBlockId = 1; |
| 79 | this.nextClusterId = 1; |
| 80 | this.spawnTimer = 0; |
| 81 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 82 | /** Seconds of actual play in this match / round (t1189). */ |
| 83 | this.playTime = 0; |
| 84 | /** t1197: matches in the current combo and seconds since the last one. */ |
| 85 | this.combo = 0; |
| 86 | this.comboAge = 0; |
| 87 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 88 | this.score = 0; |
| 89 | this.state = 'playing'; |
| 90 | this.drag = null; |
| 91 | this.gameOverTime = 0; |
| 92 | this.outgoingShots = []; |
| 93 | /** Received garbage waiting for room (t1192). */ |
| 94 | this.garbageQueue = []; |
| 95 | // Stage-2 default; stage 3 replaces this with Planet.missChance (Power). |
| 96 | this.missChance = optsMissChance(arguments); |
| 97 | // t1112: invariant checker. strictInvariants throws (tests); |
| 98 | // debugInvariants records violations for the ?debug overlay. |
| 99 | this.strictInvariants = false; |
| 100 | this.debugInvariants = false; |
| 101 | this.lastInvariantViolations = []; |
| 102 | // t1121: visual-only swap events, consumed by the view each frame. |
| 103 | this.swapEvents = []; |
| 104 | // t1120: last landing-chain indicator { chain, bonusPct }, or null. |
| 105 | this.lastChainIndicator = null; |
| 106 | } |
| 107 | |
| 108 | function optsMissChance (args) |
| 109 | { |
| 110 | // GameCore(planet, rng, missChance) — third arg is optional. |
| 111 | if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2])) |
| 112 | { return Math.max(0, Math.min(1, args[2])); } |
| 113 | var planet = args[0]; |
| 114 | if (planet && typeof PlanetMod.missChance === 'function') |
| 115 | { return PlanetMod.missChance(planet); } |
| 116 | return GameCore.DEFAULT_MISS_CHANCE; |
| 117 | } |
| 118 | |
| 119 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 120 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 121 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 122 | GameCore.SNAP_Y = SNAP_Y; |
| 123 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 124 | GameCore.LANDING_CHAIN_BONUS = LANDING_CHAIN_BONUS; |
| 125 | /** t1197/t1198: each earlier match in a combo adds this much jump height (additive). */ |
| 126 | GameCore.COMBO_STEP = 0.01; |
| 127 | /** t1198: combo steps that count at most (bonus capped at +30 %). */ |
| 128 | GameCore.COMBO_MAX_STEPS = 30; |
| 129 | /** t1197: the combo resets when no match is made for this long (s). */ |
| 130 | GameCore.COMBO_RESET_S = 5; |
| 131 | GameCore.COLUMN_WARN_ROWS = COLUMN_WARN_ROWS; |
| 132 | GameCore.DEFAULT_MISS_CHANCE = 0.30; |
| 133 | GameCore.STATE_STILL = STATE_STILL; |
| 134 | GameCore.STATE_FALLING = STATE_FALLING; |
| 135 | GameCore.STATE_FLYING = STATE_FLYING; |
| 136 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 137 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 138 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 139 | |
| 140 | GameCore.prototype.randomColor = function () |
| 141 | { |
| 142 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 143 | }; |
| 144 | |
| 145 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 146 | { |
| 147 | var block = { |
| 148 | id: this.nextBlockId++, |
| 149 | col: col, |
| 150 | y: y, |
| 151 | color: color, |
| 152 | state: state || STATE_STILL, |
| 153 | vy: 0, |
| 154 | clusterId: null, |
| 155 | settleTime: 0, |
| 156 | flame: false, |
| 157 | pushedClusters: null, |
| 158 | manualTarget: 0, |
| 159 | manualDecel: 0, |
| 160 | manualPhase: null, |
| 161 | bouncing: false, |
| 162 | bounceAge: 0 |
| 163 | }; |
| 164 | this.blocks.push(block); |
| 165 | return block; |
| 166 | }; |
| 167 | |
| 168 | GameCore.prototype.removeBlock = function (block) |
| 169 | { |
| 170 | if (block.clusterId != null) |
| 171 | { this.leaveCluster(block); } |
| 172 | var index = this.blocks.indexOf(block); |
| 173 | if (index >= 0) |
| 174 | { this.blocks.splice(index, 1); } |
| 175 | }; |
| 176 | |
| 177 | GameCore.prototype.leaveCluster = function (block) |
| 178 | { |
| 179 | var id = block.clusterId; |
| 180 | block.clusterId = null; |
| 181 | block.flame = false; |
| 182 | if (id == null || !this.clusters[id]) |
| 183 | { return; } |
| 184 | var still = 0; |
| 185 | for (var i = 0; i < this.blocks.length; i++) |
| 186 | { |
| 187 | if (this.blocks[i].clusterId === id) |
| 188 | { still++; } |
| 189 | } |
| 190 | if (still === 0) |
| 191 | { delete this.clusters[id]; } |
| 192 | }; |
| 193 | |
| 194 | GameCore.prototype.blocksInColumn = function (col) |
| 195 | { |
| 196 | var out = []; |
| 197 | for (var i = 0; i < this.blocks.length; i++) |
| 198 | { |
| 199 | if (this.blocks[i].col === col) |
| 200 | { out.push(this.blocks[i]); } |
| 201 | } |
| 202 | out.sort(function (a, b) { return a.y - b.y; }); |
| 203 | return out; |
| 204 | }; |
| 205 | |
| 206 | GameCore.prototype.isGrounded = function (block) |
| 207 | { |
| 208 | return block.state === STATE_STILL |
| 209 | || block.state === STATE_SETTLING |
| 210 | || block.state === STATE_LOCKED; |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Highest grounded landing surface in `col`. If `belowY` is given, only |
| 215 | * blocks strictly below that y count — a hanging still block above a |
| 216 | * gap must never pull a falling or thrown block upward through the gap. |
| 217 | */ |
| 218 | GameCore.prototype.stackTop = function (col, ignore, belowY) |
| 219 | { |
| 220 | var top = 0; |
| 221 | for (var i = 0; i < this.blocks.length; i++) |
| 222 | { |
| 223 | var block = this.blocks[i]; |
| 224 | if (block.col !== col) |
| 225 | { continue; } |
| 226 | if (ignore && ignore(block)) |
| 227 | { continue; } |
| 228 | if (belowY != null && block.y + 1e-9 >= belowY) |
| 229 | { continue; } |
| 230 | if (this.isGrounded(block)) |
| 231 | { |
| 232 | var surface = block.y + 1; |
| 233 | if (surface > top) |
| 234 | { top = surface; } |
| 235 | } |
| 236 | } |
| 237 | return top; |
| 238 | }; |
| 239 | |
| 240 | GameCore.prototype.isColumnLocked = function (col) |
| 241 | { |
| 242 | for (var i = 0; i < this.blocks.length; i++) |
| 243 | { |
| 244 | var block = this.blocks[i]; |
| 245 | if (block.col === col && block.state === STATE_LOCKED) |
| 246 | { return true; } |
| 247 | } |
| 248 | return false; |
| 249 | }; |
| 250 | |
| 251 | GameCore.prototype.isColumnStill = function (col) |
| 252 | { |
| 253 | if (this.isColumnLocked(col)) |
| 254 | { return false; } |
| 255 | for (var i = 0; i < this.blocks.length; i++) |
| 256 | { |
| 257 | var block = this.blocks[i]; |
| 258 | if (block.col !== col) |
| 259 | { continue; } |
| 260 | if (block.state === STATE_FLYING |
| 261 | || block.state === STATE_FALLING |
| 262 | || block.state === STATE_MANUAL |
| 263 | || block.state === STATE_SETTLING) |
| 264 | { return false; } |
| 265 | } |
| 266 | return true; |
| 267 | }; |
| 268 | |
| 269 | GameCore.prototype.groundedCount = function (col) |
| 270 | { |
| 271 | var n = 0; |
| 272 | for (var i = 0; i < this.blocks.length; i++) |
| 273 | { |
| 274 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 275 | { n++; } |
| 276 | } |
| 277 | return n; |
| 278 | }; |
| 279 | |
| 280 | GameCore.prototype.columnNeedsWarn = function (col) |
| 281 | { |
| 282 | return this.groundedCount(col) >= COLUMN_WARN_ROWS; |
| 283 | }; |
| 284 | |
| 285 | /** |
| 286 | * Which columns of a field snapshot should pulse (t1123). Flying, |
| 287 | * falling and manual (airborne) blocks do not count: they are |
| 288 | * temporary and would flicker the warning. Still, settling and |
| 289 | * locked (grounded) blocks do. |
| 290 | */ |
| 291 | GameCore.warnColumnsFromSnapshot = function (snapshot, threshold) |
| 292 | { |
| 293 | threshold = threshold == null ? COLUMN_WARN_ROWS : threshold; |
| 294 | var width = (snapshot && snapshot.width) || 10; |
| 295 | var flags = []; |
| 296 | var counts = []; |
| 297 | for (var c = 0; c < width; c++) |
| 298 | { |
| 299 | counts[c] = 0; |
| 300 | flags[c] = false; |
| 301 | } |
| 302 | var blocks = (snapshot && snapshot.blocks) || []; |
| 303 | for (var i = 0; i < blocks.length; i++) |
| 304 | { |
| 305 | var b = blocks[i]; |
| 306 | if (b.col < 0 || b.col >= width) |
| 307 | { continue; } |
| 308 | if (b.state === 'still' || b.state === 'settling' || b.state === 'locked') |
| 309 | { counts[b.col]++; } |
| 310 | } |
| 311 | for (var c = 0; c < width; c++) |
| 312 | { flags[c] = counts[c] >= threshold; } |
| 313 | return flags; |
| 314 | }; |
| 315 | |
| 316 | GameCore.columnWarnPulse = function (timeSec) |
| 317 | { |
| 318 | // Smooth sine, ~0.5 Hz, alpha 0.16–0.44. Shared by the player |
| 319 | // field and opponent / bot minis so they pulse in sync. |
| 320 | var t = timeSec; |
| 321 | if (t == null) |
| 322 | { |
| 323 | t = (typeof performance !== 'undefined' && performance.now) |
| 324 | ? performance.now() / 1000 |
| 325 | : Date.now() / 1000; |
| 326 | } |
| 327 | return 0.16 + 0.28 * (0.5 + 0.5 * Math.sin(t * Math.PI * 2 * 0.55)); |
| 328 | }; |
| 329 | |
| 330 | GameCore.prototype.blockAt = function (col, row) |
| 331 | { |
| 332 | for (var i = 0; i < this.blocks.length; i++) |
| 333 | { |
| 334 | var block = this.blocks[i]; |
| 335 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 336 | { return block; } |
| 337 | } |
| 338 | return null; |
| 339 | }; |
| 340 | |
| 341 | GameCore.prototype.place = function (col, row, color, state) |
| 342 | { |
| 343 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 344 | }; |
| 345 | |
| 346 | GameCore.prototype.lockedColumnCount = function () |
| 347 | { |
| 348 | var n = 0; |
| 349 | for (var c = 0; c < this.width; c++) |
| 350 | { |
| 351 | if (this.isColumnLocked(c)) |
| 352 | { n++; } |
| 353 | } |
| 354 | return n; |
| 355 | }; |
| 356 | |
| 357 | GameCore.prototype.startBounce = function (block) |
| 358 | { |
| 359 | if (!block) |
| 360 | { return; } |
| 361 | block.bouncing = true; |
| 362 | block.bounceAge = 0; |
| 363 | }; |
| 364 | |
| 365 | GameCore.prototype.advanceBounces = function (dt) |
| 366 | { |
| 367 | var duration = this.planet.bounceDuration; |
| 368 | for (var i = 0; i < this.blocks.length; i++) |
| 369 | { |
| 370 | var block = this.blocks[i]; |
| 371 | if (!block.bouncing) |
| 372 | { continue; } |
| 373 | block.bounceAge += dt; |
| 374 | if (duration <= 0 || block.bounceAge >= duration) |
| 375 | { |
| 376 | block.bouncing = false; |
| 377 | block.bounceAge = duration; |
| 378 | } |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | /** |
| 383 | * True if `block` rests on the floor or on a solid (still / settling / |
| 384 | * locked) block immediately below it in the same column. |
| 385 | */ |
| 386 | GameCore.prototype.hasSupport = function (block) |
| 387 | { |
| 388 | if (block.y <= 1e-9) |
| 389 | { return true; } |
| 390 | for (var i = 0; i < this.blocks.length; i++) |
| 391 | { |
| 392 | var other = this.blocks[i]; |
| 393 | if (other === block || other.col !== block.col) |
| 394 | { continue; } |
| 395 | if (!this.isGrounded(other)) |
| 396 | { continue; } |
| 397 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 398 | { return true; } |
| 399 | } |
| 400 | return false; |
| 401 | }; |
| 402 | |
| 403 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 404 | GameCore.prototype.highestFlyingBelow = function (block) |
| 405 | { |
| 406 | var best = null; |
| 407 | for (var i = 0; i < this.blocks.length; i++) |
| 408 | { |
| 409 | var other = this.blocks[i]; |
| 410 | if (other === block || other.col !== block.col) |
| 411 | { continue; } |
| 412 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 413 | { continue; } |
| 414 | var cluster = this.clusters[other.clusterId]; |
| 415 | if (!cluster || cluster.phase === 'landed') |
| 416 | { continue; } |
| 417 | // The falling block is at or above this flyer (or already overlapping). |
| 418 | if (block.y + 1e-9 < other.y) |
| 419 | { continue; } |
| 420 | if (!best || other.y > best.y) |
| 421 | { best = other; } |
| 422 | } |
| 423 | return best; |
| 424 | }; |
| 425 | |
| 426 | GameCore.prototype.highestFallingBelow = function (block) |
| 427 | { |
| 428 | var best = null; |
| 429 | for (var i = 0; i < this.blocks.length; i++) |
| 430 | { |
| 431 | var other = this.blocks[i]; |
| 432 | if (other === block || other.col !== block.col) |
| 433 | { continue; } |
| 434 | if (other.state !== STATE_FALLING) |
| 435 | { continue; } |
| 436 | if (other.y > block.y - 1e-9) |
| 437 | { continue; } |
| 438 | if (!best || other.y > best.y) |
| 439 | { best = other; } |
| 440 | } |
| 441 | return best; |
| 442 | }; |
| 443 | |
| 444 | /** |
| 445 | * Make an unsupported still block start falling from rest, so it eases |
| 446 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 447 | */ |
| 448 | /** |
| 449 | * Strip leftover launch / manual kinematics so a block that has just |
| 450 | * become still or falling starts from rest. Used after a landed grey |
| 451 | * cluster recolors, and whenever an unsupported still block drops. |
| 452 | */ |
| 453 | GameCore.prototype.clearFlightState = function (block) |
| 454 | { |
| 455 | block.vy = 0; |
| 456 | block.flame = false; |
| 457 | block.manualPhase = null; |
| 458 | block.manualTarget = null; |
| 459 | block.manualDecel = 0; |
| 460 | block.pushedClusters = null; |
| 461 | }; |
| 462 | |
| 463 | GameCore.prototype.releaseUnsupported = function () |
| 464 | { |
| 465 | // Repeat until stable: a still sitting on a still that itself just |
| 466 | // became falling must also drop. Two fixed passes are not enough |
| 467 | // for a chain of hanging blocks (t1112). |
| 468 | var guard = 0; |
| 469 | var changed = true; |
| 470 | while (changed && guard++ < this.height + 2) |
| 471 | { |
| 472 | changed = false; |
| 473 | for (var i = 0; i < this.blocks.length; i++) |
| 474 | { |
| 475 | var block = this.blocks[i]; |
| 476 | if (block.state !== STATE_STILL) |
| 477 | { continue; } |
| 478 | if (this.drag && this.drag.blockId === block.id) |
| 479 | { continue; } |
| 480 | if (this.hasSupport(block)) |
| 481 | { continue; } |
| 482 | this.leaveCluster(block); |
| 483 | this.clearFlightState(block); |
| 484 | block.state = STATE_FALLING; |
| 485 | changed = true; |
| 486 | } |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | GameCore.prototype.accelerateFall = function (block, dt) |
| 491 | { |
| 492 | var target = -this.planet.fallSpeed; |
| 493 | if (block.vy > target) |
| 494 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 495 | else |
| 496 | { block.vy = target; } |
| 497 | }; |
| 498 | |
| 499 | GameCore.prototype.landFallingBlock = function (block) |
| 500 | { |
| 501 | block.vy = 0; |
| 502 | block.flame = false; |
| 503 | this.startBounce(block); |
| 504 | if (block.color === COLOR_GRAY) |
| 505 | { |
| 506 | block.state = STATE_SETTLING; |
| 507 | block.settleTime = 0; |
| 508 | } |
| 509 | else |
| 510 | { block.state = STATE_STILL; } |
| 511 | }; |
| 512 | |
| 513 | /** |
| 514 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 515 | * carry it along as cargo (no flame). Returns true if attached. |
| 516 | */ |
| 517 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 518 | { |
| 519 | var cluster = this.clusters[flyer.clusterId]; |
| 520 | if (!cluster || cluster.phase === 'landed') |
| 521 | { return false; } |
| 522 | this.prepareClusterBody(cluster); |
| 523 | var maxOff = null; |
| 524 | var members = this.clusterMembers(cluster.id); |
| 525 | for (var i = 0; i < members.length; i++) |
| 526 | { |
| 527 | if (members[i].col !== block.col) |
| 528 | { continue; } |
| 529 | if (maxOff == null || members[i].clusterOffset > maxOff) |
| 530 | { maxOff = members[i].clusterOffset; } |
| 531 | } |
| 532 | var oldN = members.length; |
| 533 | var fallingVy = block.vy; |
| 534 | if (block.clusterId != null && block.clusterId !== cluster.id) |
| 535 | { this.leaveCluster(block); } |
| 536 | block.state = STATE_FLYING; |
| 537 | block.clusterId = cluster.id; |
| 538 | block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1; |
| 539 | block.flame = false; |
| 540 | block.settleTime = 0; |
| 541 | block.manualPhase = null; |
| 542 | block.manualTarget = null; |
| 543 | block.manualDecel = 0; |
| 544 | block.pushedClusters = null; |
| 545 | // A cluster born from a thrown block averages in falling cargo |
| 546 | // the same way two flying clusters merge (t1108). Other rockets |
| 547 | // keep their velocity and just pick the cargo up. |
| 548 | if (cluster.fromManual) |
| 549 | { |
| 550 | var newN = oldN + 1; |
| 551 | cluster.vy = (cluster.vy * oldN + fallingVy) / newN; |
| 552 | cluster.decel = ((cluster.decel || 0) * oldN) / newN; |
| 553 | cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN; |
| 554 | cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down'; |
| 555 | } |
| 556 | this.syncClusterMembers(cluster); |
| 557 | return true; |
| 558 | }; |
| 559 | |
| 560 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 561 | { |
| 562 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 563 | { return; } |
| 564 | // Snapshot falling blocks; attaching mutates state. |
| 565 | var falling = []; |
| 566 | for (var i = 0; i < this.blocks.length; i++) |
| 567 | { |
| 568 | if (this.blocks[i].state === STATE_FALLING) |
| 569 | { falling.push(this.blocks[i]); } |
| 570 | } |
| 571 | for (var i = 0; i < falling.length; i++) |
| 572 | { |
| 573 | var block = falling[i]; |
| 574 | var flyer = null; |
| 575 | for (var m = 0; m < members.length; m++) |
| 576 | { |
| 577 | var other = members[m]; |
| 578 | if (other.col !== block.col) |
| 579 | { continue; } |
| 580 | // Overlap or rest on this member's top. |
| 581 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 582 | { |
| 583 | if (!flyer || other.y > flyer.y) |
| 584 | { flyer = other; } |
| 585 | } |
| 586 | } |
| 587 | if (flyer) |
| 588 | { this.attachToFlyingCluster(block, flyer); } |
| 589 | } |
| 590 | }; |
| 591 | |
| 592 | GameCore.prototype.tick = function (dt) |
| 593 | { |
| 594 | if (this.state === 'gameover') |
| 595 | { |
| 596 | this.gameOverTime += Math.max(0, dt); |
| 597 | return; |
| 598 | } |
| 599 | if (dt <= 0) |
| 600 | { return; } |
| 601 | dt = Math.min(dt, 0.1); |
| 602 | |
| 603 | // t1189: spawn timeline by elapsed play time (intro burst, ramp, then |
| 604 | // the per-spawn speed-up). Interval is re-read after every spawn. |
| 605 | // t1192: waiting received blocks drop first, as soon as there is room. |
| 606 | if (this.garbageQueue.length) |
| 607 | { this.drainGarbageQueue(); } |
| 608 | // t1197: combo expires after COMBO_RESET_S without a match. |
| 609 | this.comboAge += dt; |
| 610 | if (this.combo > 0 && this.comboAge >= GameCore.COMBO_RESET_S) |
| 611 | { |
| 612 | this.combo = 0; |
| 613 | if (!this.hasActiveLandingChain()) |
| 614 | { this.lastChainIndicator = null; } |
| 615 | } |
| 616 | this.spawnTimer += dt; |
| 617 | var iv = this.currentSpawnInterval(); |
| 618 | while (this.spawnTimer >= iv) |
| 619 | { |
| 620 | this.spawnTimer -= iv; |
| 621 | this.trySpawn(); |
| 622 | iv = this.currentSpawnInterval(); |
| 623 | } |
| 624 | this.playTime += dt; |
| 625 | |
| 626 | var left = dt; |
| 627 | while (left > 1e-12) |
| 628 | { |
| 629 | var step = Math.min(SUBSTEP, left); |
| 630 | this.snapshotPrevY(); |
| 631 | this.integrateFalling(step); |
| 632 | this.resolveFlyingMatches(); |
| 633 | this.integrateClusters(step); |
| 634 | this.mergeFlyingClusters(); |
| 635 | this.integrateManual(step); |
| 636 | this.mergeAnyManualFallingOverlaps(); |
| 637 | this.resolveAirborneCollisions(); |
| 638 | this.cullOffTop(); |
| 639 | left -= step; |
| 640 | } |
| 641 | |
| 642 | this.releaseUnsupported(); |
| 643 | this.advanceSettle(dt); |
| 644 | // After a landed grey group is released, blocks that were sitting on |
| 645 | // hanging members are still STILL (the earlier pass ran while those |
| 646 | // members counted as support). Drop them from rest before matching, |
| 647 | // so they cannot form a mid-air match and shoot off the top. |
| 648 | this.releaseUnsupported(); |
| 649 | this.advanceBounces(dt); |
| 650 | if (!this.drag) |
| 651 | { this.resolveMatches(); } |
| 652 | this.checkLocks(); |
| 653 | this.checkGameOver(); |
| 654 | this.assertInvariants('tick'); |
| 655 | }; |
| 656 | |
| 657 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 658 | GameCore.prototype.step = function (seconds, dt) |
| 659 | { |
| 660 | dt = dt || SUBSTEP; |
| 661 | var t = 0; |
| 662 | while (t < seconds - 1e-15) |
| 663 | { |
| 664 | var s = Math.min(dt, seconds - t); |
| 665 | this.tick(s); |
| 666 | t += s; |
| 667 | } |
| 668 | }; |
| 669 | |
| 670 | /** Share of the field height a rising stack must pass (t1184). */ |
| 671 | GameCore.DEPART_SHARE = 0.75; |
| 672 | |
| 673 | /** |
| 674 | * Columns with a rising rocket stack that will certainly leave the |
| 675 | * screen (t1184). A flying cluster counts when it is still going up, |
| 676 | * its highest block is past DEPART_SHARE of the field height, its apex |
| 677 | * (y + vy^2 / (2 * decel)) is at or above the top, and nothing grounded |
| 678 | * hangs above it in its columns. Every column that stack occupies is |
| 679 | * departing: newly spawned blocks and received garbage pick another |
| 680 | * column (t1185/t1186). Returns { col: true }. |
| 681 | */ |
| 682 | GameCore.prototype.departingColumns = function () |
| 683 | { |
| 684 | var out = Object.create(null); |
| 685 | var H = this.height; |
| 686 | var ids = Object.keys(this.clusters); |
| 687 | for (var n = 0; n < ids.length; n++) |
| 688 | { |
| 689 | var cluster = this.clusters[ids[n]]; |
| 690 | if (!cluster || cluster.phase !== 'up' || !(cluster.vy > 1e-9)) |
| 691 | { continue; } |
| 692 | var members = this.clusterMembers(cluster.id); |
| 693 | if (!members.length) |
| 694 | { continue; } |
| 695 | var top = -Infinity; |
| 696 | var lowest = Object.create(null); |
| 697 | for (var i = 0; i < members.length; i++) |
| 698 | { |
| 699 | var m = members[i]; |
| 700 | if (m.state !== STATE_FLYING) |
| 701 | { continue; } |
| 702 | if (m.y > top) |
| 703 | { top = m.y; } |
| 704 | if (lowest[m.col] == null || m.y < lowest[m.col]) |
| 705 | { lowest[m.col] = m.y; } |
| 706 | } |
| 707 | if (top < this.DEPART_LINE()) |
| 708 | { continue; } |
| 709 | var decel = cluster.decel || 0; |
| 710 | var rise = decel > 1e-12 ? (cluster.vy * cluster.vy) / (2 * decel) : Infinity; |
| 711 | if (top + rise < H - 1e-9) |
| 712 | { continue; } |
| 713 | var blocked = false; |
| 714 | for (var col in lowest) |
| 715 | { |
| 716 | var c = col | 0; |
| 717 | for (var j = 0; j < this.blocks.length; j++) |
| 718 | { |
| 719 | var b = this.blocks[j]; |
| 720 | if (b.col === c && b.clusterId !== cluster.id && this.isGrounded(b) && b.y > lowest[col]) |
| 721 | { |
| 722 | blocked = true; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | if (blocked) |
| 727 | { break; } |
| 728 | } |
| 729 | if (blocked) |
| 730 | { continue; } |
| 731 | // t1185/t1186: every column the departing stack occupies is off |
| 732 | // limits; new and received blocks pick another column. |
| 733 | for (var col2 in lowest) |
| 734 | { out[col2 | 0] = true; } |
| 735 | } |
| 736 | return out; |
| 737 | }; |
| 738 | |
| 739 | /** Height (tiles from the ground) a rising stack must pass: 75% of the field. */ |
| 740 | GameCore.prototype.DEPART_LINE = function () |
| 741 | { |
| 742 | return this.height * GameCore.DEPART_SHARE; |
| 743 | }; |
| 744 | |
| 745 | GameCore.prototype.trySpawn = function () |
| 746 | { |
| 747 | if (this.state !== 'playing') |
| 748 | { return null; } |
| 749 | var eligible = []; |
| 750 | var departing = this.departingColumns(); |
| 751 | for (var col = 0; col < this.width; col++) |
| 752 | { |
| 753 | if (this.isColumnLocked(col)) |
| 754 | { continue; } |
| 755 | if (this.groundedCount(col) >= this.height) |
| 756 | { continue; } |
| 757 | // t1184: never drop a new block onto a stack that is leaving. |
| 758 | if (departing[col]) |
| 759 | { continue; } |
| 760 | var alreadyFalling = false; |
| 761 | var list = this.blocksInColumn(col); |
| 762 | // t1189/t1191: during the opening burst and ramp a column may take |
| 763 | // another block as soon as the previous falling one has cleared a |
| 764 | // full cell below the spawn row (no overlap); later the classic |
| 765 | // one-falling-block-per-column rule applies. |
| 766 | var opening = this.playTime < GameCore.SPAWN_RAMP_END; |
| 767 | for (var i = 0; i < list.length; i++) |
| 768 | { |
| 769 | if (list[i].state !== STATE_FALLING) |
| 770 | { continue; } |
| 771 | if (opening && list[i].y <= this.height - 1 + 1e-9) |
| 772 | { continue; } |
| 773 | alreadyFalling = true; |
| 774 | break; |
| 775 | } |
| 776 | if (!alreadyFalling) |
| 777 | { eligible.push(col); } |
| 778 | } |
| 779 | if (!eligible.length) |
| 780 | { return null; } |
| 781 | var col = this.playTime < GameCore.SPAWN_INTRO_END |
| 782 | ? this.pickWeightedSpawnColumn(eligible) |
| 783 | : eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 784 | var color = this.randomColor(); |
| 785 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 786 | block.vy = -this.planet.fallSpeed; |
| 787 | // t1187/t1188 speed-up only counts blocks spawned after the intro (t1189). |
| 788 | if (this.playTime >= GameCore.SPAWN_RAMP_END) |
| 789 | { this.speedUpSpawning(); } |
| 790 | return block; |
| 791 | }; |
| 792 | |
| 793 | /** |
| 794 | * Burst-phase column choice (t1191): weight each eligible column by its |
| 795 | * free cells (height − grounded − already falling blocks, min 1), so the fast opening burst fills |
| 796 | * the field evenly instead of piling onto one column. One rng() draw, like |
| 797 | * the uniform pick. |
| 798 | */ |
| 799 | GameCore.prototype.fallingCount = function (col) |
| 800 | { |
| 801 | var n = 0; |
| 802 | for (var i = 0; i < this.blocks.length; i++) |
| 803 | { |
| 804 | if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING) |
| 805 | { n++; } |
| 806 | } |
| 807 | return n; |
| 808 | }; |
| 809 | |
| 810 | GameCore.prototype.pickWeightedSpawnColumn = function (eligible) |
| 811 | { |
| 812 | var weights = []; |
| 813 | var total = 0; |
| 814 | for (var i = 0; i < eligible.length; i++) |
| 815 | { |
| 816 | var w = Math.max(1, this.height - this.groundedCount(eligible[i]) - this.fallingCount(eligible[i])); |
| 817 | weights.push(w); |
| 818 | total += w; |
| 819 | } |
| 820 | var r = this.rng() * total; |
| 821 | for (var j = 0; j < eligible.length; j++) |
| 822 | { |
| 823 | r -= weights[j]; |
| 824 | if (r < 0) |
| 825 | { return eligible[j]; } |
| 826 | } |
| 827 | return eligible[eligible.length - 1]; |
| 828 | }; |
| 829 | |
| 830 | /** t1189/t1191: first seconds of a match spawn fast ... */ |
| 831 | GameCore.SPAWN_INTRO_END = 3; |
| 832 | /** ... at this interval (seconds) ... */ |
| 833 | GameCore.SPAWN_INTRO_INTERVAL = 0.1; |
| 834 | /** ... then lerp back to PlanetMod.SPAWN_INTERVAL (1 s) until this time. */ |
| 835 | GameCore.SPAWN_RAMP_END = 6; |
| 836 | |
| 837 | /** |
| 838 | * Spawn interval right now (t1189): |
| 839 | * playTime < 3 s → 0.1 s (burst, weighted column choice) |
| 840 | * 3 s – 6 s → linear 0.1 s → 1.0 s |
| 841 | * ≥ 6 s → spawnInterval (starts at 1 s, ×0.997 per spawn |
| 842 | * after 6 s, floor 0.1 s) |
| 843 | * An interval set above the 1 s start (tests / tools use 1e9 to switch |
| 844 | * spawning off) is always respected. |
| 845 | */ |
| 846 | GameCore.prototype.currentSpawnInterval = function () |
| 847 | { |
| 848 | var base = PlanetMod.SPAWN_INTERVAL; |
| 849 | if (this.spawnInterval > base + 1e-9) |
| 850 | { return this.spawnInterval; } |
| 851 | var t = this.playTime || 0; |
| 852 | if (t < GameCore.SPAWN_INTRO_END) |
| 853 | { return GameCore.SPAWN_INTRO_INTERVAL; } |
| 854 | if (t < GameCore.SPAWN_RAMP_END) |
| 855 | { |
| 856 | var k = (t - GameCore.SPAWN_INTRO_END) / (GameCore.SPAWN_RAMP_END - GameCore.SPAWN_INTRO_END); |
| 857 | return GameCore.SPAWN_INTRO_INTERVAL + (base - GameCore.SPAWN_INTRO_INTERVAL) * k; |
| 858 | } |
| 859 | return this.spawnInterval; |
| 860 | }; |
| 861 | |
| 862 | /** Each spawned block shrinks the spawn interval by 0.3% (t1187 / t1188). */ |
| 863 | GameCore.SPAWN_SPEEDUP = 0.997; |
| 864 | /** The spawn interval never goes below this (seconds, t1187). */ |
| 865 | GameCore.SPAWN_INTERVAL_MIN = 0.1; |
| 866 | |
| 867 | /** |
| 868 | * After every real spawn: spawnInterval *= SPAWN_SPEEDUP, floored at |
| 869 | * SPAWN_INTERVAL_MIN. Starts at PlanetMod.SPAWN_INTERVAL (1 s) for every |
| 870 | * new core, i.e. every game and every round, for players and bots alike. |
| 871 | * Reaches the floor after ~767 spawns (~5 min of continuous play). |
| 872 | */ |
| 873 | GameCore.prototype.speedUpSpawning = function () |
| 874 | { |
| 875 | var next = this.spawnInterval * GameCore.SPAWN_SPEEDUP; |
| 876 | this.spawnInterval = Math.max(GameCore.SPAWN_INTERVAL_MIN, next); |
| 877 | return this.spawnInterval; |
| 878 | }; |
| 879 | |
| 880 | GameCore.prototype.integrateFalling = function (dt) |
| 881 | { |
| 882 | var falling = []; |
| 883 | for (var i = 0; i < this.blocks.length; i++) |
| 884 | { |
| 885 | if (this.blocks[i].state === STATE_FALLING) |
| 886 | { falling.push(this.blocks[i]); } |
| 887 | } |
| 888 | // Lowest first, so a stack of falling blocks rests on itself. |
| 889 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 890 | for (var i = 0; i < falling.length; i++) |
| 891 | { |
| 892 | var block = falling[i]; |
| 893 | if (block.state !== STATE_FALLING) |
| 894 | { continue; } |
| 895 | this.accelerateFall(block, dt); |
| 896 | var nextY = block.y + block.vy * dt; |
| 897 | var manual = this.manualInColumnHit(block, block.y, nextY); |
| 898 | if (manual) |
| 899 | { |
| 900 | this.mergeManualWithFalling(manual, this.contiguousFallingInColumn(manual.col, manual, [block])); |
| 901 | continue; |
| 902 | } |
| 903 | var flyer = this.highestFlyingBelow(block); |
| 904 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 905 | { |
| 906 | if (this.attachToFlyingCluster(block, flyer)) |
| 907 | { continue; } |
| 908 | } |
| 909 | var top = this.stackTop(block.col, null, block.y); |
| 910 | var below = this.highestFallingBelow(block); |
| 911 | if (below && below.y + 1 > top) |
| 912 | { top = below.y + 1; } |
| 913 | if (nextY <= top) |
| 914 | { |
| 915 | block.y = top; |
| 916 | this.landFallingBlock(block); |
| 917 | } |
| 918 | else |
| 919 | { block.y = nextY; } |
| 920 | } |
| 921 | }; |
| 922 | |
| 923 | GameCore.prototype.clusterMembers = function (clusterId) |
| 924 | { |
| 925 | var out = []; |
| 926 | for (var i = 0; i < this.blocks.length; i++) |
| 927 | { |
| 928 | if (this.blocks[i].clusterId === clusterId) |
| 929 | { out.push(this.blocks[i]); } |
| 930 | } |
| 931 | return out; |
| 932 | }; |
| 933 | |
| 934 | |
| 935 | /** |
| 936 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 937 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 938 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 939 | * upward deceleration. After the apex, vy eases from 0 to the |
| 940 | * terminal descent speed at the matching accel, then stays there. |
| 941 | * Flying clusters (forCluster) use the halved cluster float-down |
| 942 | * (t1122 / t1124); manual jumps use the full single-block fallSpeed. |
| 943 | */ |
| 944 | GameCore.prototype.descentParams = function (forCluster) |
| 945 | { |
| 946 | var p = this.planet; |
| 947 | if (forCluster) |
| 948 | { |
| 949 | return { |
| 950 | fallSpeed: p.clusterFallSpeed, |
| 951 | fallAccel: p.clusterFallAccel |
| 952 | }; |
| 953 | } |
| 954 | return { fallSpeed: p.fallSpeed, fallAccel: p.fallAccel }; |
| 955 | }; |
| 956 | |
| 957 | GameCore.prototype.advanceJump = function (body, dt, forCluster) |
| 958 | { |
| 959 | var dy = 0; |
| 960 | var left = dt; |
| 961 | if (body.phase === 'up') |
| 962 | { |
| 963 | var decel = body.decel; |
| 964 | var vy = body.vy; |
| 965 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 966 | if (tApex <= left + 1e-15) |
| 967 | { |
| 968 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 969 | body.vy = 0; |
| 970 | body.phase = 'down'; |
| 971 | left -= tApex; |
| 972 | } |
| 973 | else |
| 974 | { |
| 975 | var vyNew = vy - decel * left; |
| 976 | dy += (vy + vyNew) * 0.5 * left; |
| 977 | body.vy = vyNew; |
| 978 | left = 0; |
| 979 | } |
| 980 | } |
| 981 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 982 | { |
| 983 | body.vy = 0; |
| 984 | body.phase = 'down'; |
| 985 | } |
| 986 | if (body.phase === 'down' && left > 0) |
| 987 | { |
| 988 | var desc = this.descentParams(forCluster); |
| 989 | var target = -desc.fallSpeed; |
| 990 | var accel = desc.fallAccel; |
| 991 | if (accel < 1e-12) |
| 992 | { |
| 993 | body.vy = target; |
| 994 | dy += body.vy * left; |
| 995 | } |
| 996 | else if (body.vy > target + 1e-12) |
| 997 | { |
| 998 | var tReach = (body.vy - target) / accel; |
| 999 | if (tReach <= left + 1e-15) |
| 1000 | { |
| 1001 | dy += (body.vy + target) * 0.5 * tReach; |
| 1002 | body.vy = target; |
| 1003 | left -= tReach; |
| 1004 | dy += body.vy * left; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | var vyNew = body.vy - accel * left; |
| 1009 | dy += (body.vy + vyNew) * 0.5 * left; |
| 1010 | body.vy = vyNew; |
| 1011 | } |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | body.vy = target; |
| 1016 | dy += body.vy * left; |
| 1017 | } |
| 1018 | } |
| 1019 | return dy; |
| 1020 | }; |
| 1021 | |
| 1022 | GameCore.prototype.integrateClusters = function (dt) |
| 1023 | { |
| 1024 | var ids = Object.keys(this.clusters); |
| 1025 | for (var n = 0; n < ids.length; n++) |
| 1026 | { |
| 1027 | var cluster = this.clusters[ids[n]]; |
| 1028 | if (!cluster) |
| 1029 | { continue; } |
| 1030 | var members = this.clusterMembers(cluster.id); |
| 1031 | if (!members.length) |
| 1032 | { |
| 1033 | delete this.clusters[cluster.id]; |
| 1034 | continue; |
| 1035 | } |
| 1036 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1037 | { continue; } |
| 1038 | |
| 1039 | this.prepareClusterBody(cluster); |
| 1040 | var dy = this.advanceJump(cluster, dt, true); |
| 1041 | cluster.anchorY += dy; |
| 1042 | this.syncClusterMembers(cluster); |
| 1043 | |
| 1044 | this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id)); |
| 1045 | this.checkChainRelaunch(cluster); |
| 1046 | |
| 1047 | if (cluster.phase === 'down' && this.clusters[cluster.id]) |
| 1048 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1049 | } |
| 1050 | }; |
| 1051 | |
| 1052 | |
| 1053 | /** |
| 1054 | * If two or more flying clusters overlap in the same column, merge them |
| 1055 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 1056 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 1057 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 1058 | * are packed so the higher one sits flush on the lower. |
| 1059 | */ |
| 1060 | GameCore.prototype.mergeFlyingClusters = function () |
| 1061 | { |
| 1062 | var flying = []; |
| 1063 | var ids = Object.keys(this.clusters); |
| 1064 | for (var n = 0; n < ids.length; n++) |
| 1065 | { |
| 1066 | var cluster = this.clusters[ids[n]]; |
| 1067 | if (!cluster) |
| 1068 | { continue; } |
| 1069 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1070 | { continue; } |
| 1071 | flying.push(cluster); |
| 1072 | } |
| 1073 | if (flying.length < 2) |
| 1074 | { return; } |
| 1075 | |
| 1076 | var uf = new UnionFind(); |
| 1077 | for (var i = 0; i < flying.length; i++) |
| 1078 | { uf.add(flying[i].id); } |
| 1079 | for (var i = 0; i < flying.length; i++) |
| 1080 | { |
| 1081 | for (var j = i + 1; j < flying.length; j++) |
| 1082 | { |
| 1083 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 1084 | { uf.union(flying[i].id, flying[j].id); } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | var groups = Object.create(null); |
| 1089 | for (var i = 0; i < flying.length; i++) |
| 1090 | { |
| 1091 | var root = uf.find(flying[i].id); |
| 1092 | if (!groups[root]) |
| 1093 | { groups[root] = []; } |
| 1094 | groups[root].push(flying[i]); |
| 1095 | } |
| 1096 | for (var root in groups) |
| 1097 | { |
| 1098 | if (groups[root].length >= 2) |
| 1099 | { this.mergeClusterGroup(groups[root]); } |
| 1100 | } |
| 1101 | }; |
| 1102 | |
| 1103 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 1104 | { |
| 1105 | var ma = this.clusterMembers(a.id); |
| 1106 | var mb = this.clusterMembers(b.id); |
| 1107 | for (var i = 0; i < ma.length; i++) |
| 1108 | { |
| 1109 | for (var j = 0; j < mb.length; j++) |
| 1110 | { |
| 1111 | if (ma[i].col !== mb[j].col) |
| 1112 | { continue; } |
| 1113 | if (this.sweptIntervalsOverlap(ma[i], mb[j])) |
| 1114 | { return true; } |
| 1115 | } |
| 1116 | } |
| 1117 | return false; |
| 1118 | }; |
| 1119 | |
| 1120 | /** |
| 1121 | * Swept [y, y+1) of two blocks this substep, using _prevY when present. |
| 1122 | * Catches pairs that crossed without sharing a cell at either endpoint. |
| 1123 | */ |
| 1124 | GameCore.prototype.sweptIntervalsOverlap = function (a, b) |
| 1125 | { |
| 1126 | var a0 = (a._prevY != null) ? a._prevY : a.y; |
| 1127 | var a1 = a.y; |
| 1128 | var b0 = (b._prevY != null) ? b._prevY : b.y; |
| 1129 | var b1 = b.y; |
| 1130 | var aLo = Math.min(a0, a1); |
| 1131 | var aHi = Math.max(a0, a1) + 1; |
| 1132 | var bLo = Math.min(b0, b1); |
| 1133 | var bHi = Math.max(b0, b1) + 1; |
| 1134 | return aLo < bHi - 1e-9 && bLo < aHi - 1e-9; |
| 1135 | }; |
| 1136 | |
| 1137 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 1138 | { |
| 1139 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 1140 | var keep = clusters[0]; |
| 1141 | var n = 0; |
| 1142 | var vySum = 0; |
| 1143 | var decelSum = 0; |
| 1144 | var hSum = 0; |
| 1145 | for (var c = 0; c < clusters.length; c++) |
| 1146 | { |
| 1147 | var mem = this.clusterMembers(clusters[c].id); |
| 1148 | var m = mem.length; |
| 1149 | n += m; |
| 1150 | vySum += clusters[c].vy * m; |
| 1151 | decelSum += (clusters[c].decel || 0) * m; |
| 1152 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 1153 | } |
| 1154 | if (n === 0) |
| 1155 | { return; } |
| 1156 | keep.vy = vySum / n; |
| 1157 | keep.decel = decelSum / n; |
| 1158 | keep.jumpHeight = hSum / n; |
| 1159 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 1160 | var maxChain = 0; |
| 1161 | for (var c = 0; c < clusters.length; c++) |
| 1162 | { |
| 1163 | var ch = clusters[c].landingChain || 0; |
| 1164 | if (ch > maxChain) |
| 1165 | { maxChain = ch; } |
| 1166 | } |
| 1167 | keep.landingChain = maxChain; |
| 1168 | for (var c = 0; c < clusters.length; c++) |
| 1169 | { |
| 1170 | var mem = this.clusterMembers(clusters[c].id); |
| 1171 | for (var i = 0; i < mem.length; i++) |
| 1172 | { |
| 1173 | mem[i].clusterId = keep.id; |
| 1174 | mem[i].vy = keep.vy; |
| 1175 | mem[i].state = STATE_FLYING; |
| 1176 | } |
| 1177 | if (clusters[c] !== keep) |
| 1178 | { delete this.clusters[clusters[c].id]; } |
| 1179 | } |
| 1180 | this.prepareClusterBody(keep); |
| 1181 | this.checkChainRelaunch(keep); |
| 1182 | if (keep.phase === 'down' && this.clusters[keep.id]) |
| 1183 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 1184 | }; |
| 1185 | |
| 1186 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 1187 | { |
| 1188 | var members = this.clusterMembers(cluster.id); |
| 1189 | var byCol = Object.create(null); |
| 1190 | for (var i = 0; i < members.length; i++) |
| 1191 | { |
| 1192 | var col = members[i].col; |
| 1193 | if (!byCol[col]) |
| 1194 | { byCol[col] = []; } |
| 1195 | byCol[col].push(members[i]); |
| 1196 | } |
| 1197 | var cols = Object.keys(byCol); |
| 1198 | for (var c = 0; c < cols.length; c++) |
| 1199 | { |
| 1200 | var list = byCol[cols[c]]; |
| 1201 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1202 | for (var i = 1; i < list.length; i++) |
| 1203 | { |
| 1204 | var minY = list[i - 1].y + 1; |
| 1205 | if (list[i].y < minY - 1e-12) |
| 1206 | { list[i].y = minY; } |
| 1207 | } |
| 1208 | } |
| 1209 | }; |
| 1210 | |
| 1211 | |
| 1212 | /** |
| 1213 | * Derive integer cell offsets from current member positions and a |
| 1214 | * single anchorY, packing overlaps in each column while preserving |
| 1215 | * order (lower y stays lower; ties break on id). Then sync. |
| 1216 | * Flying clusters are one rigid body: one y, one vy, integer offsets. |
| 1217 | */ |
| 1218 | GameCore.prototype.prepareClusterBody = function (cluster) |
| 1219 | { |
| 1220 | if (!cluster) |
| 1221 | { return; } |
| 1222 | var members = this.clusterMembers(cluster.id); |
| 1223 | if (!members.length) |
| 1224 | { return; } |
| 1225 | this.rebuildClusterOffsets(cluster, members); |
| 1226 | }; |
| 1227 | |
| 1228 | GameCore.prototype.rebuildClusterOffsets = function (cluster, members) |
| 1229 | { |
| 1230 | members = members || this.clusterMembers(cluster.id); |
| 1231 | if (!members.length) |
| 1232 | { return; } |
| 1233 | var byCol = Object.create(null); |
| 1234 | for (var i = 0; i < members.length; i++) |
| 1235 | { |
| 1236 | var col = members[i].col; |
| 1237 | if (!byCol[col]) |
| 1238 | { byCol[col] = []; } |
| 1239 | byCol[col].push(members[i]); |
| 1240 | } |
| 1241 | var minY = Infinity; |
| 1242 | for (var i = 0; i < members.length; i++) |
| 1243 | { |
| 1244 | if (members[i].y < minY) |
| 1245 | { minY = members[i].y; } |
| 1246 | } |
| 1247 | if (cluster.anchorY == null || !isFinite(cluster.anchorY)) |
| 1248 | { cluster.anchorY = minY; } |
| 1249 | var cols = Object.keys(byCol); |
| 1250 | for (var c = 0; c < cols.length; c++) |
| 1251 | { |
| 1252 | var list = byCol[cols[c]]; |
| 1253 | list.sort(function (a, b) |
| 1254 | { |
| 1255 | if (a.y !== b.y) |
| 1256 | { return a.y - b.y; } |
| 1257 | return a.id - b.id; |
| 1258 | }); |
| 1259 | var prevOff = -Infinity; |
| 1260 | for (var i = 0; i < list.length; i++) |
| 1261 | { |
| 1262 | var off = Math.round(list[i].y - cluster.anchorY); |
| 1263 | if (off <= prevOff) |
| 1264 | { off = prevOff + 1; } |
| 1265 | list[i].clusterOffset = off; |
| 1266 | prevOff = off; |
| 1267 | } |
| 1268 | } |
| 1269 | // Re-base so the lowest offset is 0, keeping relative integers. |
| 1270 | var minOff = Infinity; |
| 1271 | for (var i = 0; i < members.length; i++) |
| 1272 | { |
| 1273 | if (members[i].clusterOffset < minOff) |
| 1274 | { minOff = members[i].clusterOffset; } |
| 1275 | } |
| 1276 | if (minOff !== 0 && isFinite(minOff)) |
| 1277 | { |
| 1278 | cluster.anchorY += minOff; |
| 1279 | for (var i = 0; i < members.length; i++) |
| 1280 | { members[i].clusterOffset -= minOff; } |
| 1281 | } |
| 1282 | this.syncClusterMembers(cluster); |
| 1283 | }; |
| 1284 | |
| 1285 | GameCore.prototype.syncClusterMembers = function (cluster) |
| 1286 | { |
| 1287 | if (!cluster) |
| 1288 | { return; } |
| 1289 | var members = this.clusterMembers(cluster.id); |
| 1290 | if (cluster.anchorY == null) |
| 1291 | { |
| 1292 | var minY = Infinity; |
| 1293 | for (var i = 0; i < members.length; i++) |
| 1294 | { |
| 1295 | if (members[i].y < minY) |
| 1296 | { minY = members[i].y; } |
| 1297 | } |
| 1298 | cluster.anchorY = isFinite(minY) ? minY : 0; |
| 1299 | } |
| 1300 | for (var i = 0; i < members.length; i++) |
| 1301 | { |
| 1302 | var block = members[i]; |
| 1303 | if (block.clusterOffset == null) |
| 1304 | { block.clusterOffset = Math.round(block.y - cluster.anchorY); } |
| 1305 | block.y = cluster.anchorY + block.clusterOffset; |
| 1306 | if (cluster.phase !== 'landed') |
| 1307 | { block.vy = cluster.vy; } |
| 1308 | } |
| 1309 | }; |
| 1310 | |
| 1311 | GameCore.prototype.snapshotPrevY = function () |
| 1312 | { |
| 1313 | for (var i = 0; i < this.blocks.length; i++) |
| 1314 | { this.blocks[i]._prevY = this.blocks[i].y; } |
| 1315 | }; |
| 1316 | |
| 1317 | /** |
| 1318 | * Safety net after the specific integrates: any remaining mid-air |
| 1319 | * overlap or swept crossing is resolved with the same rules, then |
| 1320 | * every flying cluster is rebuilt as a rigid body. Repeats a few |
| 1321 | * times because one attach can create a new contact. |
| 1322 | */ |
| 1323 | GameCore.prototype.resolveAirborneCollisions = function () |
| 1324 | { |
| 1325 | var guard = 0; |
| 1326 | while (guard++ < this.height + 4) |
| 1327 | { |
| 1328 | var changed = false; |
| 1329 | |
| 1330 | // Manual vs falling / cluster / manual. |
| 1331 | var manuals = []; |
| 1332 | for (var i = 0; i < this.blocks.length; i++) |
| 1333 | { |
| 1334 | if (this.blocks[i].state === STATE_MANUAL) |
| 1335 | { manuals.push(this.blocks[i]); } |
| 1336 | } |
| 1337 | manuals.sort(function (a, b) { return b.y - a.y; }); |
| 1338 | for (var m = 0; m < manuals.length; m++) |
| 1339 | { |
| 1340 | var manual = manuals[m]; |
| 1341 | if (manual.state !== STATE_MANUAL) |
| 1342 | { continue; } |
| 1343 | var oldY = (manual._prevY != null) ? manual._prevY : manual.y; |
| 1344 | if (this.resolveManualCollisions(manual, oldY, manual.y)) |
| 1345 | { changed = true; } |
| 1346 | } |
| 1347 | |
| 1348 | // Falling vs flying (sit on the roof). |
| 1349 | for (var i = 0; i < this.blocks.length; i++) |
| 1350 | { |
| 1351 | var block = this.blocks[i]; |
| 1352 | if (block.state !== STATE_FALLING) |
| 1353 | { continue; } |
| 1354 | var flyer = this.highestFlyingBelow(block); |
| 1355 | if (!flyer) |
| 1356 | { |
| 1357 | // Also a flyer whose swept path crosses this falling block. |
| 1358 | flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y); |
| 1359 | } |
| 1360 | if (flyer && this.sweptIntervalsOverlap(block, flyer)) |
| 1361 | { |
| 1362 | if (this.attachToFlyingCluster(block, flyer)) |
| 1363 | { changed = true; } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // Cluster vs cluster (swept, including after a column push). |
| 1368 | var before = Object.keys(this.clusters).length; |
| 1369 | this.mergeFlyingClusters(); |
| 1370 | if (Object.keys(this.clusters).length !== before) |
| 1371 | { changed = true; } |
| 1372 | |
| 1373 | // Rebuild every flying body so offsets stay integer and unique. |
| 1374 | var ids = Object.keys(this.clusters); |
| 1375 | for (var n = 0; n < ids.length; n++) |
| 1376 | { |
| 1377 | var cluster = this.clusters[ids[n]]; |
| 1378 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1379 | { continue; } |
| 1380 | this.prepareClusterBody(cluster); |
| 1381 | } |
| 1382 | |
| 1383 | if (!changed) |
| 1384 | { break; } |
| 1385 | } |
| 1386 | }; |
| 1387 | |
| 1388 | /** |
| 1389 | * Invariant checker (t1112). No two blocks occupy the same cell or |
| 1390 | * overlap their [y, y+1) intervals. Flying-cluster members are |
| 1391 | * grid-aligned to a single anchor. Returns a list of violation |
| 1392 | * strings (empty if clean). |
| 1393 | */ |
| 1394 | GameCore.prototype.checkInvariants = function () |
| 1395 | { |
| 1396 | var issues = []; |
| 1397 | var byCol = Object.create(null); |
| 1398 | var ids = Object.create(null); |
| 1399 | for (var i = 0; i < this.blocks.length; i++) |
| 1400 | { |
| 1401 | var block = this.blocks[i]; |
| 1402 | if (ids[block.id]) |
| 1403 | { issues.push('duplicate id ' + block.id); } |
| 1404 | ids[block.id] = true; |
| 1405 | if (!byCol[block.col]) |
| 1406 | { byCol[block.col] = []; } |
| 1407 | byCol[block.col].push(block); |
| 1408 | if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet |
| 1409 | && block.color >= this.planet.colors.length)) |
| 1410 | { issues.push('block ' + block.id + ' has invalid color ' + block.color); } |
| 1411 | } |
| 1412 | var cols = Object.keys(byCol); |
| 1413 | for (var c = 0; c < cols.length; c++) |
| 1414 | { |
| 1415 | var list = byCol[cols[c]]; |
| 1416 | list.sort(function (a, b) |
| 1417 | { |
| 1418 | if (a.y !== b.y) |
| 1419 | { return a.y - b.y; } |
| 1420 | return a.id - b.id; |
| 1421 | }); |
| 1422 | for (var i = 0; i < list.length; i++) |
| 1423 | { |
| 1424 | var a = list[i]; |
| 1425 | if (i > 0) |
| 1426 | { |
| 1427 | var prev = list[i - 1]; |
| 1428 | if (a.y < prev.y + 1 - 1e-6) |
| 1429 | { |
| 1430 | issues.push('overlap col=' + a.col |
| 1431 | + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a) |
| 1432 | + ' gap=' + (a.y - prev.y)); |
| 1433 | } |
| 1434 | var cellA = Math.floor(a.y + 1e-9); |
| 1435 | var cellP = Math.floor(prev.y + 1e-9); |
| 1436 | if (cellA === cellP) |
| 1437 | { |
| 1438 | issues.push('duplicate cell col=' + a.col + ' row=' + cellA |
| 1439 | + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | var cids = Object.keys(this.clusters); |
| 1445 | for (var n = 0; n < cids.length; n++) |
| 1446 | { |
| 1447 | var cluster = this.clusters[cids[n]]; |
| 1448 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1449 | { continue; } |
| 1450 | var mem = this.clusterMembers(cluster.id); |
| 1451 | var seenOff = Object.create(null); |
| 1452 | for (var i = 0; i < mem.length; i++) |
| 1453 | { |
| 1454 | var b = mem[i]; |
| 1455 | if (cluster.anchorY != null && b.clusterOffset != null) |
| 1456 | { |
| 1457 | var expected = cluster.anchorY + b.clusterOffset; |
| 1458 | if (Math.abs(b.y - expected) > 1e-6) |
| 1459 | { |
| 1460 | issues.push('cluster ' + cluster.id + ' member ' + b.id |
| 1461 | + ' y=' + b.y + ' != anchor+off ' + expected); |
| 1462 | } |
| 1463 | if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9) |
| 1464 | { |
| 1465 | issues.push('cluster ' + cluster.id + ' member ' + b.id |
| 1466 | + ' non-integer offset ' + b.clusterOffset); |
| 1467 | } |
| 1468 | var key = b.col + ':' + b.clusterOffset; |
| 1469 | if (seenOff[key]) |
| 1470 | { |
| 1471 | issues.push('cluster ' + cluster.id + ' duplicate offset ' + key); |
| 1472 | } |
| 1473 | seenOff[key] = true; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | return issues; |
| 1478 | }; |
| 1479 | |
| 1480 | GameCore.prototype._invBlock = function (b) |
| 1481 | { |
| 1482 | return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y=' |
| 1483 | + (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')'; |
| 1484 | }; |
| 1485 | |
| 1486 | GameCore.prototype.assertInvariants = function (label) |
| 1487 | { |
| 1488 | if (!this.strictInvariants && !this.debugInvariants) |
| 1489 | { return; } |
| 1490 | var issues = this.checkInvariants(); |
| 1491 | this.lastInvariantViolations = issues; |
| 1492 | if (!issues.length) |
| 1493 | { return; } |
| 1494 | var msg = 'physics invariants failed' |
| 1495 | + (label ? ' at ' + label : '') |
| 1496 | + ':\n ' + issues.join('\n ') |
| 1497 | + '\n' + (this.snapshot ? this.snapshot().picture : ''); |
| 1498 | if (this.strictInvariants) |
| 1499 | { throw new Error(msg); } |
| 1500 | }; |
| 1501 | |
| 1502 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1503 | { |
| 1504 | if (!members.length) |
| 1505 | { return; } |
| 1506 | var shift = 0; |
| 1507 | var hit = false; |
| 1508 | for (var i = 0; i < members.length; i++) |
| 1509 | { |
| 1510 | var block = members[i]; |
| 1511 | var top = this.stackTop(block.col, function (other) |
| 1512 | { |
| 1513 | return other.clusterId === cluster.id; |
| 1514 | }, block.y); |
| 1515 | if (block.y <= top + 1e-9) |
| 1516 | { |
| 1517 | hit = true; |
| 1518 | var need = top - block.y; |
| 1519 | if (need > shift) |
| 1520 | { shift = need; } |
| 1521 | } |
| 1522 | } |
| 1523 | if (!hit) |
| 1524 | { return; } |
| 1525 | // Lift the whole rigid body so the deepest contact sits flush. |
| 1526 | this.prepareClusterBody(cluster); |
| 1527 | cluster.anchorY += shift; |
| 1528 | this.syncClusterMembers(cluster); |
| 1529 | this.landCluster(cluster, this.clusterMembers(cluster.id)); |
| 1530 | }; |
| 1531 | |
| 1532 | /** |
| 1533 | * Land as a rigid group: relative positions are kept, so part of the |
| 1534 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 1535 | * coloured cargo stays put with them until every grey has recolored. |
| 1536 | * Members that actually rest on the floor or on another block bounce. |
| 1537 | */ |
| 1538 | GameCore.prototype.landCluster = function (cluster, members) |
| 1539 | { |
| 1540 | for (var i = 0; i < members.length; i++) |
| 1541 | { |
| 1542 | var block = members[i]; |
| 1543 | block.vy = 0; |
| 1544 | block.flame = false; |
| 1545 | block.clusterId = cluster.id; |
| 1546 | if (block.color === COLOR_GRAY) |
| 1547 | { |
| 1548 | block.state = STATE_SETTLING; |
| 1549 | block.settleTime = 0; |
| 1550 | } |
| 1551 | else |
| 1552 | { |
| 1553 | // Cargo stays in the rigid group (not matchable) until release. |
| 1554 | block.state = STATE_SETTLING; |
| 1555 | block.settleTime = 0; |
| 1556 | } |
| 1557 | if (this.hasSupport(block)) |
| 1558 | { this.startBounce(block); } |
| 1559 | } |
| 1560 | cluster.phase = 'landed'; |
| 1561 | cluster.vy = 0; |
| 1562 | cluster.landedPending = true; |
| 1563 | if (cluster.landingChain == null) |
| 1564 | { cluster.landingChain = 0; } |
| 1565 | }; |
| 1566 | |
| 1567 | /** |
| 1568 | * After every grey in a landed cluster has recolored, let the group go. |
| 1569 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 1570 | */ |
| 1571 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 1572 | { |
| 1573 | var members = this.clusterMembers(cluster.id); |
| 1574 | var releasing = Object.create(null); |
| 1575 | for (var i = 0; i < members.length; i++) |
| 1576 | { releasing[members[i].id] = true; } |
| 1577 | |
| 1578 | for (var i = 0; i < members.length; i++) |
| 1579 | { |
| 1580 | var block = members[i]; |
| 1581 | if (block.color === COLOR_GRAY) |
| 1582 | { |
| 1583 | block.color = this.randomColor(); |
| 1584 | block.settleTime = 0; |
| 1585 | } |
| 1586 | // Persist the chain across release so a post-recolor match in |
| 1587 | // this same tick still gets the landing bonus (t1120). |
| 1588 | block.justLanded = !!cluster.landedPending; |
| 1589 | block.landingChain = cluster.landingChain || 0; |
| 1590 | this.leaveCluster(block); |
| 1591 | // Always drop leftover launch / manual velocity. A stale upward |
| 1592 | // vy here is what sends blocks off the top "very fast" after |
| 1593 | // a grey group recolors. |
| 1594 | this.clearFlightState(block); |
| 1595 | // Support from outside the group (floor or a foreign block). Fellow |
| 1596 | // members that will also fall do not count, so a hanging stack |
| 1597 | // drops together without the top one staying put. |
| 1598 | var supported = this.hasSupportOutside(block, releasing); |
| 1599 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 1600 | } |
| 1601 | delete this.clusters[cluster.id]; |
| 1602 | }; |
| 1603 | |
| 1604 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 1605 | { |
| 1606 | if (block.y <= 1e-9) |
| 1607 | { return true; } |
| 1608 | for (var i = 0; i < this.blocks.length; i++) |
| 1609 | { |
| 1610 | var other = this.blocks[i]; |
| 1611 | if (other === block || other.col !== block.col) |
| 1612 | { continue; } |
| 1613 | if (ignoreIds && ignoreIds[other.id]) |
| 1614 | { continue; } |
| 1615 | if (!this.isGrounded(other)) |
| 1616 | { continue; } |
| 1617 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 1618 | { return true; } |
| 1619 | } |
| 1620 | return false; |
| 1621 | }; |
| 1622 | |
| 1623 | GameCore.prototype.integrateManual = function (dt) |
| 1624 | { |
| 1625 | for (var i = 0; i < this.blocks.length; i++) |
| 1626 | { |
| 1627 | var block = this.blocks[i]; |
| 1628 | if (block.state !== STATE_MANUAL) |
| 1629 | { continue; } |
| 1630 | |
| 1631 | if (block.manualPhase === 'up') |
| 1632 | { |
| 1633 | var oldY = block.y; |
| 1634 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 1635 | var dy = this.advanceJump(proxy, dt); |
| 1636 | var nextY = oldY + dy; |
| 1637 | block.vy = proxy.vy; |
| 1638 | block.manualPhase = proxy.phase; |
| 1639 | if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget))) |
| 1640 | { continue; } |
| 1641 | block.y = Math.min(nextY, block.manualTarget); |
| 1642 | if (block.y >= block.manualTarget - 1e-12) |
| 1643 | { |
| 1644 | block.y = block.manualTarget; |
| 1645 | block.vy = 0; |
| 1646 | block.manualPhase = 'down'; |
| 1647 | } |
| 1648 | if (block.y > this.height - 1) |
| 1649 | { |
| 1650 | block.y = this.height - 1; |
| 1651 | block.vy = 0; |
| 1652 | block.manualPhase = 'down'; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if (block.manualPhase === 'down') |
| 1657 | { |
| 1658 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 1659 | var dy = this.advanceJump(proxy, dt); |
| 1660 | block.vy = proxy.vy; |
| 1661 | var nextY = block.y + dy; |
| 1662 | var top = this.stackTop(block.col, null, block.y); |
| 1663 | if (nextY <= top) |
| 1664 | { |
| 1665 | block.y = top; |
| 1666 | block.state = STATE_STILL; |
| 1667 | block.vy = 0; |
| 1668 | block.manualPhase = null; |
| 1669 | block.flame = false; |
| 1670 | this.startBounce(block); |
| 1671 | } |
| 1672 | else |
| 1673 | { |
| 1674 | var downNext = Math.min(nextY, this.height - 1); |
| 1675 | if (this.resolveManualCollisions(block, block.y, downNext)) |
| 1676 | { continue; } |
| 1677 | block.y = downNext; |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | }; |
| 1682 | |
| 1683 | /** |
| 1684 | * If the manual throw (or its path this step) hits falling blocks, merge |
| 1685 | * them into a flying cluster with mass-weighted velocity, the same rule |
| 1686 | * as two flying clusters colliding (t1108). Returns true if merged. |
| 1687 | */ |
| 1688 | GameCore.prototype.tryMergeManualWithFalling = function (manual, oldY, newY) |
| 1689 | { |
| 1690 | if (!manual || manual.state !== STATE_MANUAL) |
| 1691 | { return false; } |
| 1692 | var hits = this.fallingHitsOnManualPath(manual, oldY, newY); |
| 1693 | if (!hits.length) |
| 1694 | { return false; } |
| 1695 | this.mergeManualWithFalling(manual, hits); |
| 1696 | return true; |
| 1697 | }; |
| 1698 | |
| 1699 | GameCore.prototype.manualInColumnHit = function (falling, oldY, newY) |
| 1700 | { |
| 1701 | var lo = Math.min(oldY, newY); |
| 1702 | var hi = Math.max(oldY, newY) + 1; |
| 1703 | for (var i = 0; i < this.blocks.length; i++) |
| 1704 | { |
| 1705 | var other = this.blocks[i]; |
| 1706 | if (other === falling || other.col !== falling.col) |
| 1707 | { continue; } |
| 1708 | if (other.state !== STATE_MANUAL) |
| 1709 | { continue; } |
| 1710 | if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9) |
| 1711 | { return other; } |
| 1712 | } |
| 1713 | return null; |
| 1714 | }; |
| 1715 | |
| 1716 | GameCore.prototype.fallingHitsOnManualPath = function (manual, oldY, newY) |
| 1717 | { |
| 1718 | var lo = Math.min(oldY, newY); |
| 1719 | var hi = Math.max(oldY, newY) + 1; |
| 1720 | var hits = []; |
| 1721 | for (var i = 0; i < this.blocks.length; i++) |
| 1722 | { |
| 1723 | var other = this.blocks[i]; |
| 1724 | if (other === manual || other.col !== manual.col) |
| 1725 | { continue; } |
| 1726 | if (other.state !== STATE_FALLING) |
| 1727 | { continue; } |
| 1728 | if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9) |
| 1729 | { hits.push(other); } |
| 1730 | } |
| 1731 | if (!hits.length) |
| 1732 | { return hits; } |
| 1733 | return this.contiguousFallingInColumn(manual.col, manual, hits); |
| 1734 | }; |
| 1735 | |
| 1736 | /** |
| 1737 | * Every falling block in `col` that overlaps `anchor` or a hit already |
| 1738 | * in `seed`, plus a contiguous falling stack sitting on them. |
| 1739 | */ |
| 1740 | GameCore.prototype.contiguousFallingInColumn = function (col, anchor, seed) |
| 1741 | { |
| 1742 | var falling = []; |
| 1743 | for (var i = 0; i < this.blocks.length; i++) |
| 1744 | { |
| 1745 | if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING) |
| 1746 | { falling.push(this.blocks[i]); } |
| 1747 | } |
| 1748 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 1749 | var include = Object.create(null); |
| 1750 | var queue = []; |
| 1751 | function enqueue (b) |
| 1752 | { |
| 1753 | if (!b || include[b.id]) |
| 1754 | { return; } |
| 1755 | include[b.id] = true; |
| 1756 | queue.push(b); |
| 1757 | } |
| 1758 | if (seed) |
| 1759 | { |
| 1760 | for (var s = 0; s < seed.length; s++) |
| 1761 | { enqueue(seed[s]); } |
| 1762 | } |
| 1763 | // Also include falling that already overlap the anchor. |
| 1764 | if (anchor) |
| 1765 | { |
| 1766 | for (var i = 0; i < falling.length; i++) |
| 1767 | { |
| 1768 | if (falling[i].y < anchor.y + 1 - 1e-9 && anchor.y < falling[i].y + 1 - 1e-9) |
| 1769 | { enqueue(falling[i]); } |
| 1770 | } |
| 1771 | } |
| 1772 | for (var q = 0; q < queue.length; q++) |
| 1773 | { |
| 1774 | var a = queue[q]; |
| 1775 | for (var i = 0; i < falling.length; i++) |
| 1776 | { |
| 1777 | var b = falling[i]; |
| 1778 | if (include[b.id]) |
| 1779 | { continue; } |
| 1780 | if (a.y < b.y + 1 + 1e-9 && b.y < a.y + 1 + 1e-9) |
| 1781 | { enqueue(b); } |
| 1782 | } |
| 1783 | } |
| 1784 | var out = []; |
| 1785 | for (var i = 0; i < falling.length; i++) |
| 1786 | { |
| 1787 | if (include[falling[i].id]) |
| 1788 | { out.push(falling[i]); } |
| 1789 | } |
| 1790 | return out; |
| 1791 | }; |
| 1792 | |
| 1793 | /** |
| 1794 | * Merge a thrown (manual) block with falling blocks into one flying |
| 1795 | * cluster. Velocity, deceleration and jumpHeight are the mass-weighted |
| 1796 | * averages by block count, identical to mergeClusterGroup. The thrown |
| 1797 | * block stops being manual and the group follows normal cluster physics. |
| 1798 | * fromManual marks the cluster so later falling cargo also averages in. |
| 1799 | */ |
| 1800 | GameCore.prototype.mergeManualWithFalling = function (manual, fallingList) |
| 1801 | { |
| 1802 | if (!manual || manual.state !== STATE_MANUAL) |
| 1803 | { return null; } |
| 1804 | var blocks = [manual]; |
| 1805 | for (var i = 0; i < fallingList.length; i++) |
| 1806 | { |
| 1807 | if (fallingList[i] && fallingList[i].state === STATE_FALLING && blocks.indexOf(fallingList[i]) < 0) |
| 1808 | { blocks.push(fallingList[i]); } |
| 1809 | } |
| 1810 | if (blocks.length < 2) |
| 1811 | { return null; } |
| 1812 | var n = blocks.length; |
| 1813 | var vySum = 0; |
| 1814 | var decelSum = 0; |
| 1815 | var hSum = 0; |
| 1816 | for (var i = 0; i < n; i++) |
| 1817 | { |
| 1818 | var b = blocks[i]; |
| 1819 | vySum += b.vy; |
| 1820 | if (b === manual) |
| 1821 | { |
| 1822 | decelSum += manual.manualDecel || 0; |
| 1823 | hSum += manual.manualJumpHeight != null ? manual.manualJumpHeight |
| 1824 | : Math.max(0, (manual.manualTarget || this.height - 1) - manual.y); |
| 1825 | } |
| 1826 | } |
| 1827 | var cluster = { |
| 1828 | id: this.nextClusterId++, |
| 1829 | jumpHeight: hSum / n, |
| 1830 | vy: vySum / n, |
| 1831 | decel: decelSum / n, |
| 1832 | phase: (vySum / n) > 1e-10 ? 'up' : 'down', |
| 1833 | fromManual: true |
| 1834 | }; |
| 1835 | this.clusters[cluster.id] = cluster; |
| 1836 | for (var i = 0; i < n; i++) |
| 1837 | { |
| 1838 | var b = blocks[i]; |
| 1839 | b.clusterId = cluster.id; |
| 1840 | b.state = STATE_FLYING; |
| 1841 | b.vy = cluster.vy; |
| 1842 | b.flame = b.color === COLOR_GRAY; |
| 1843 | b.manualPhase = null; |
| 1844 | b.manualTarget = null; |
| 1845 | b.manualDecel = 0; |
| 1846 | b.manualJumpHeight = 0; |
| 1847 | b.pushedClusters = null; |
| 1848 | b.settleTime = 0; |
| 1849 | } |
| 1850 | if (this.drag && this.drag.blockId === manual.id) |
| 1851 | { this.drag = null; } |
| 1852 | this.prepareClusterBody(cluster); |
| 1853 | this.checkChainRelaunch(cluster); |
| 1854 | return cluster; |
| 1855 | }; |
| 1856 | |
| 1857 | /** Safety net: any remaining manual/falling overlaps after the substep. */ |
| 1858 | GameCore.prototype.mergeAnyManualFallingOverlaps = function () |
| 1859 | { |
| 1860 | var manuals = []; |
| 1861 | for (var i = 0; i < this.blocks.length; i++) |
| 1862 | { |
| 1863 | if (this.blocks[i].state === STATE_MANUAL) |
| 1864 | { manuals.push(this.blocks[i]); } |
| 1865 | } |
| 1866 | for (var m = 0; m < manuals.length; m++) |
| 1867 | { |
| 1868 | var manual = manuals[m]; |
| 1869 | if (manual.state !== STATE_MANUAL) |
| 1870 | { continue; } |
| 1871 | var hits = this.contiguousFallingInColumn(manual.col, manual); |
| 1872 | if (hits.length) |
| 1873 | { this.mergeManualWithFalling(manual, hits); } |
| 1874 | } |
| 1875 | }; |
| 1876 | |
| 1877 | /** |
| 1878 | * Resolve collisions of a thrown (manual) block along [oldY, nextY]. |
| 1879 | * Falling blocks merge (t1108). A flying cluster is pushed one cell up |
| 1880 | * in the hit column and the shot attaches at the bottom (t1111). |
| 1881 | * Another thrown block is promoted to a 1-block cluster and then the |
| 1882 | * same attach-at-bottom rule applies. Returns true if `manual` is no |
| 1883 | * longer in the MANUAL state. |
| 1884 | */ |
| 1885 | GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY) |
| 1886 | { |
| 1887 | if (!manual || manual.state !== STATE_MANUAL) |
| 1888 | { return true; } |
| 1889 | |
| 1890 | var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY); |
| 1891 | if (fallingHits.length) |
| 1892 | { |
| 1893 | this.mergeManualWithFalling(manual, fallingHits); |
| 1894 | return manual.state !== STATE_MANUAL; |
| 1895 | } |
| 1896 | |
| 1897 | var flyer = this.flyingHitOnManualPath(manual, oldY, nextY); |
| 1898 | if (flyer) |
| 1899 | { |
| 1900 | var cluster = this.clusters[flyer.clusterId]; |
| 1901 | if (cluster && cluster.phase !== 'landed') |
| 1902 | { |
| 1903 | if (nextY + 1e-12 < oldY) |
| 1904 | { this.attachToFlyingCluster(manual, flyer); } |
| 1905 | else |
| 1906 | { this.attachShotUnderCluster(manual, cluster, manual.col); } |
| 1907 | return manual.state !== STATE_MANUAL; |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | var other = this.manualHitOnManualPath(manual, oldY, nextY); |
| 1912 | if (other) |
| 1913 | { |
| 1914 | var host = this.promoteManualToCluster(other); |
| 1915 | if (host) |
| 1916 | { |
| 1917 | if (nextY + 1e-12 < oldY) |
| 1918 | { this.attachToFlyingCluster(manual, other); } |
| 1919 | else |
| 1920 | { this.attachShotUnderCluster(manual, host, manual.col); } |
| 1921 | return manual.state !== STATE_MANUAL; |
| 1922 | } |
| 1923 | } |
| 1924 | return false; |
| 1925 | }; |
| 1926 | |
| 1927 | /** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */ |
| 1928 | GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY) |
| 1929 | { |
| 1930 | var lo = Math.min(oldY, nextY); |
| 1931 | var hi = Math.max(oldY, nextY) + 1; |
| 1932 | var best = null; |
| 1933 | var bestY = Infinity; |
| 1934 | for (var i = 0; i < this.blocks.length; i++) |
| 1935 | { |
| 1936 | var other = this.blocks[i]; |
| 1937 | if (other === manual || other.col !== manual.col) |
| 1938 | { continue; } |
| 1939 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1940 | { continue; } |
| 1941 | var cluster = this.clusters[other.clusterId]; |
| 1942 | if (!cluster || cluster.phase === 'landed') |
| 1943 | { continue; } |
| 1944 | var o0 = (other._prevY != null) ? other._prevY : other.y; |
| 1945 | var o1 = other.y; |
| 1946 | var oLo = Math.min(o0, o1); |
| 1947 | var oHi = Math.max(o0, o1) + 1; |
| 1948 | if (lo < oHi - 1e-9 && oLo < hi - 1e-9) |
| 1949 | { |
| 1950 | if (other.y < bestY) |
| 1951 | { |
| 1952 | bestY = other.y; |
| 1953 | best = other; |
| 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | return best; |
| 1958 | }; |
| 1959 | |
| 1960 | /** Another thrown block in the same column on the path (prefer the lower). */ |
| 1961 | GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY) |
| 1962 | { |
| 1963 | var lo = Math.min(oldY, nextY); |
| 1964 | var hi = Math.max(oldY, nextY) + 1; |
| 1965 | var best = null; |
| 1966 | for (var i = 0; i < this.blocks.length; i++) |
| 1967 | { |
| 1968 | var other = this.blocks[i]; |
| 1969 | if (other === manual || other.col !== manual.col) |
| 1970 | { continue; } |
| 1971 | if (other.state !== STATE_MANUAL) |
| 1972 | { continue; } |
| 1973 | var o0 = (other._prevY != null) ? other._prevY : other.y; |
| 1974 | var o1 = other.y; |
| 1975 | var oLo = Math.min(o0, o1); |
| 1976 | var oHi = Math.max(o0, o1) + 1; |
| 1977 | if (lo < oHi - 1e-9 && oLo < hi - 1e-9) |
| 1978 | { |
| 1979 | if (!best || other.y < best.y) |
| 1980 | { best = other; } |
| 1981 | } |
| 1982 | } |
| 1983 | return best; |
| 1984 | }; |
| 1985 | |
| 1986 | /** |
| 1987 | * t1111: push every member of `cluster` in `col` up by one cell, then |
| 1988 | * attach `manual` at the bottom of that column, grid-aligned, as part |
| 1989 | * of the cluster. The cluster keeps its velocity; the shot takes it. |
| 1990 | * Other columns are untouched. Same rule for every successive shot. |
| 1991 | */ |
| 1992 | GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col) |
| 1993 | { |
| 1994 | if (!manual || !cluster || !this.clusters[cluster.id]) |
| 1995 | { return false; } |
| 1996 | if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id) |
| 1997 | { |
| 1998 | // Already belonging elsewhere: leave first. |
| 1999 | if (manual.clusterId != null) |
| 2000 | { this.leaveCluster(manual); } |
| 2001 | } |
| 2002 | this.prepareClusterBody(cluster); |
| 2003 | var members = this.clusterMembers(cluster.id); |
| 2004 | var minOff = null; |
| 2005 | for (var i = 0; i < members.length; i++) |
| 2006 | { |
| 2007 | if (members[i].col !== col) |
| 2008 | { continue; } |
| 2009 | if (minOff == null || members[i].clusterOffset < minOff) |
| 2010 | { minOff = members[i].clusterOffset; } |
| 2011 | } |
| 2012 | if (minOff == null) |
| 2013 | { minOff = 0; } |
| 2014 | else |
| 2015 | { |
| 2016 | for (var i = 0; i < members.length; i++) |
| 2017 | { |
| 2018 | if (members[i].col === col) |
| 2019 | { members[i].clusterOffset += 1; } |
| 2020 | } |
| 2021 | } |
| 2022 | if (this.drag && this.drag.blockId === manual.id) |
| 2023 | { this.drag = null; } |
| 2024 | manual.clusterId = cluster.id; |
| 2025 | manual.clusterOffset = minOff; |
| 2026 | manual.state = STATE_FLYING; |
| 2027 | manual.flame = manual.color === COLOR_GRAY; |
| 2028 | manual.settleTime = 0; |
| 2029 | manual.manualPhase = null; |
| 2030 | manual.manualTarget = null; |
| 2031 | manual.manualDecel = 0; |
| 2032 | manual.manualJumpHeight = 0; |
| 2033 | manual.pushedClusters = null; |
| 2034 | this.syncClusterMembers(cluster); |
| 2035 | this.cullOffTop(); |
| 2036 | if (this.clusters[cluster.id]) |
| 2037 | { this.checkChainRelaunch(cluster); } |
| 2038 | return true; |
| 2039 | }; |
| 2040 | |
| 2041 | /** |
| 2042 | * Turn a lone thrown block into a 1-block flying cluster so a later |
| 2043 | * shot can use the t1111 attach-at-bottom rule. A single throw stays |
| 2044 | * MANUAL (and is capped at the top of the field) until it collides. |
| 2045 | */ |
| 2046 | GameCore.prototype.promoteManualToCluster = function (manual) |
| 2047 | { |
| 2048 | if (!manual) |
| 2049 | { return null; } |
| 2050 | if (manual.state === STATE_FLYING && manual.clusterId != null) |
| 2051 | { |
| 2052 | var existing = this.clusters[manual.clusterId]; |
| 2053 | if (existing && existing.phase !== 'landed') |
| 2054 | { return existing; } |
| 2055 | } |
| 2056 | if (manual.state !== STATE_MANUAL) |
| 2057 | { return null; } |
| 2058 | var height = manual.manualJumpHeight != null ? manual.manualJumpHeight |
| 2059 | : Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y); |
| 2060 | var cluster = { |
| 2061 | id: this.nextClusterId++, |
| 2062 | jumpHeight: height, |
| 2063 | vy: manual.vy, |
| 2064 | decel: manual.manualDecel || 0, |
| 2065 | phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up', |
| 2066 | fromManual: true, |
| 2067 | anchorY: manual.y |
| 2068 | }; |
| 2069 | this.clusters[cluster.id] = cluster; |
| 2070 | manual.state = STATE_FLYING; |
| 2071 | manual.clusterId = cluster.id; |
| 2072 | manual.clusterOffset = 0; |
| 2073 | manual.flame = manual.color === COLOR_GRAY; |
| 2074 | manual.settleTime = 0; |
| 2075 | manual.manualPhase = null; |
| 2076 | manual.manualTarget = null; |
| 2077 | manual.manualDecel = 0; |
| 2078 | manual.manualJumpHeight = 0; |
| 2079 | manual.pushedClusters = null; |
| 2080 | this.syncClusterMembers(cluster); |
| 2081 | return cluster; |
| 2082 | }; |
| 2083 | |
| 2084 | /** Kept for older tests / call sites: push a cluster column by one cell. */ |
| 2085 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 2086 | { |
| 2087 | return this.resolveManualCollisions(manual, oldY, nextY); |
| 2088 | }; |
| 2089 | |
| 2090 | |
| 2091 | /** |
| 2092 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 2093 | * Other columns stay put (they remain linked). Then look for a new |
| 2094 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 2095 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 2096 | * with the same jump height from its current position. |
| 2097 | */ |
| 2098 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 2099 | { |
| 2100 | var cluster = this.clusters[clusterId]; |
| 2101 | if (!cluster) |
| 2102 | { return false; } |
| 2103 | var members = this.clusterMembers(clusterId); |
| 2104 | for (var i = 0; i < members.length; i++) |
| 2105 | { |
| 2106 | if (members[i].col === col) |
| 2107 | { members[i].y += 1; } |
| 2108 | } |
| 2109 | this.cullOffTop(); |
| 2110 | if (!this.clusters[clusterId]) |
| 2111 | { return true; } |
| 2112 | this.checkChainRelaunch(cluster); |
| 2113 | return true; |
| 2114 | }; |
| 2115 | |
| 2116 | /** |
| 2117 | * After a cargo attach or a cluster merge (or any other mid-air state |
| 2118 | * change), turn spontaneous lines of 3+ into rockets and give the whole |
| 2119 | * already-flying cluster an extra launch from its current position — |
| 2120 | * the same re-launch as a player push that completes a row. |
| 2121 | */ |
| 2122 | GameCore.prototype.resolveFlyingMatches = function () |
| 2123 | { |
| 2124 | var ids = Object.keys(this.clusters); |
| 2125 | for (var n = 0; n < ids.length; n++) |
| 2126 | { |
| 2127 | var cluster = this.clusters[ids[n]]; |
| 2128 | if (!cluster || cluster.phase === 'landed') |
| 2129 | { continue; } |
| 2130 | this.checkChainRelaunch(cluster); |
| 2131 | } |
| 2132 | }; |
| 2133 | |
| 2134 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 2135 | { |
| 2136 | if (!this.clusters[cluster.id]) |
| 2137 | { return; } |
| 2138 | var lines = this.findFlyingMatchLines(cluster.id); |
| 2139 | if (!lines.length) |
| 2140 | { return; } |
| 2141 | var newly = []; |
| 2142 | var seen = Object.create(null); |
| 2143 | for (var i = 0; i < lines.length; i++) |
| 2144 | { |
| 2145 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 2146 | { |
| 2147 | var block = lines[i].blocks[j]; |
| 2148 | if (seen[block.id]) |
| 2149 | { continue; } |
| 2150 | seen[block.id] = true; |
| 2151 | newly.push(block); |
| 2152 | } |
| 2153 | } |
| 2154 | for (var i = 0; i < newly.length; i++) |
| 2155 | { |
| 2156 | var block = newly[i]; |
| 2157 | block.color = COLOR_GRAY; |
| 2158 | block.flame = true; |
| 2159 | block.clusterId = cluster.id; |
| 2160 | block.state = STATE_FLYING; |
| 2161 | } |
| 2162 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 2163 | }; |
| 2164 | |
| 2165 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 2166 | { |
| 2167 | cluster.jumpHeight = height; |
| 2168 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 2169 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 2170 | cluster.phase = 'up'; |
| 2171 | var members = this.clusterMembers(cluster.id); |
| 2172 | for (var i = 0; i < members.length; i++) |
| 2173 | { |
| 2174 | members[i].vy = cluster.vy; |
| 2175 | members[i].state = STATE_FLYING; |
| 2176 | if (members[i].color === COLOR_GRAY) |
| 2177 | { members[i].flame = true; } |
| 2178 | } |
| 2179 | }; |
| 2180 | |
| 2181 | GameCore.prototype.startManualLaunch = function (block) |
| 2182 | { |
| 2183 | var target = this.height - 1; |
| 2184 | var height = Math.max(0.05, target - block.y); |
| 2185 | this.leaveCluster(block); |
| 2186 | block.state = STATE_MANUAL; |
| 2187 | block.pushedClusters = Object.create(null); |
| 2188 | block.manualTarget = target; |
| 2189 | block.manualPhase = 'up'; |
| 2190 | block.manualJumpHeight = height; |
| 2191 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 2192 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 2193 | block.flame = false; |
| 2194 | if (this.drag && this.drag.blockId === block.id) |
| 2195 | { this.drag = null; } |
| 2196 | }; |
| 2197 | |
| 2198 | GameCore.prototype.cullOffTop = function () |
| 2199 | { |
| 2200 | var gone = []; |
| 2201 | for (var i = 0; i < this.blocks.length; i++) |
| 2202 | { |
| 2203 | var block = this.blocks[i]; |
| 2204 | if (block.state === STATE_MANUAL) |
| 2205 | { |
| 2206 | if (block.y > this.height - 1) |
| 2207 | { block.y = this.height - 1; } |
| 2208 | continue; |
| 2209 | } |
| 2210 | if (block.y >= this.height - 1e-9) |
| 2211 | { gone.push(block); } |
| 2212 | } |
| 2213 | for (var i = 0; i < gone.length; i++) |
| 2214 | { |
| 2215 | var block = gone[i]; |
| 2216 | this.outgoingShots.push({ |
| 2217 | id: block.id, |
| 2218 | color: block.color, |
| 2219 | col: block.col, |
| 2220 | y: block.y, |
| 2221 | vy: block.vy |
| 2222 | }); |
| 2223 | this.score += 1; |
| 2224 | this.removeBlock(block); |
| 2225 | } |
| 2226 | }; |
| 2227 | |
| 2228 | GameCore.prototype.consumeOutgoingShots = function () |
| 2229 | { |
| 2230 | var shots = this.outgoingShots; |
| 2231 | this.outgoingShots = []; |
| 2232 | return shots; |
| 2233 | }; |
| 2234 | |
| 2235 | /** |
| 2236 | * A compact field picture for the multiplayer snapshot (miniatures). |
| 2237 | * Includes every block's col, y, color, state and flame. |
| 2238 | */ |
| 2239 | GameCore.prototype.fieldSnapshot = function () |
| 2240 | { |
| 2241 | var blocks = []; |
| 2242 | for (var i = 0; i < this.blocks.length; i++) |
| 2243 | { |
| 2244 | var b = this.blocks[i]; |
| 2245 | blocks.push({ |
| 2246 | id: b.id, |
| 2247 | col: b.col, |
| 2248 | y: Math.round(b.y * 1000) / 1000, |
| 2249 | color: b.color, |
| 2250 | state: b.state, |
| 2251 | flame: !!b.flame |
| 2252 | }); |
| 2253 | } |
| 2254 | return { |
| 2255 | score: this.score, |
| 2256 | state: this.state, |
| 2257 | width: this.width, |
| 2258 | height: this.height, |
| 2259 | locked: this.lockedColumnCount(), |
| 2260 | blocks: blocks |
| 2261 | }; |
| 2262 | }; |
| 2263 | |
| 2264 | /** |
| 2265 | * Incoming garbage from another player: a dark-grey block that falls |
| 2266 | * normally from the top of an open column. After landing it uses the |
| 2267 | * usual 3s settle and then a random colour (landFallingBlock). |
| 2268 | * `col` is optional; a random eligible column is used otherwise. |
| 2269 | * Returns the new block, or null if every column is locked or full. |
| 2270 | */ |
| 2271 | /** |
| 2272 | * Open columns a garbage block can fall into (not locked, not full). |
| 2273 | */ |
| 2274 | GameCore.prototype.eligibleGarbageColumns = function () |
| 2275 | { |
| 2276 | var eligible = []; |
| 2277 | for (var c = 0; c < this.width; c++) |
| 2278 | { |
| 2279 | if (this.isColumnLocked(c)) |
| 2280 | { continue; } |
| 2281 | if (this.groundedCount(c) >= this.height) |
| 2282 | { continue; } |
| 2283 | eligible.push(c); |
| 2284 | } |
| 2285 | return eligible; |
| 2286 | }; |
| 2287 | |
| 2288 | /** |
| 2289 | * Choose a garbage column. If `preferred` is still open, it wins; |
| 2290 | * otherwise a random eligible column (t1126). Returns null if none. |
| 2291 | */ |
| 2292 | GameCore.prototype.pickGarbageColumn = function (preferred) |
| 2293 | { |
| 2294 | var eligible = this.eligibleGarbageColumns(); |
| 2295 | if (!eligible.length) |
| 2296 | { return null; } |
| 2297 | // t1184: skip columns whose rocket stack is certainly leaving; only |
| 2298 | // if every open column is departing, fall back so no garbage is lost. |
| 2299 | var departing = this.departingColumns(); |
| 2300 | var staying = []; |
| 2301 | for (var k = 0; k < eligible.length; k++) |
| 2302 | { |
| 2303 | if (!departing[eligible[k]]) |
| 2304 | { staying.push(eligible[k]); } |
| 2305 | } |
| 2306 | if (staying.length) |
| 2307 | { eligible = staying; } |
| 2308 | if (preferred != null && isFinite(preferred)) |
| 2309 | { |
| 2310 | var want = preferred | 0; |
| 2311 | for (var i = 0; i < eligible.length; i++) |
| 2312 | { |
| 2313 | if (eligible[i] === want) |
| 2314 | { return want; } |
| 2315 | } |
| 2316 | } |
| 2317 | return eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 2318 | }; |
| 2319 | |
| 2320 | /** |
| 2321 | * Receive one garbage block (t1192). It is queued and dropped as soon as |
| 2322 | * there is room; received blocks are never lost. Returns the block when |
| 2323 | * it could drop right away, otherwise null (it waits in garbageQueue). |
| 2324 | * |
| 2325 | * Before t1192 every received block was created at once at the spawn |
| 2326 | * row of its column. Several blocks aimed at the same column overlapped |
| 2327 | * there: the second one "landed" on the first at y ≥ 13, was culled off |
| 2328 | * the top and even counted as the receiver's own shot. |
| 2329 | */ |
| 2330 | GameCore.prototype.ingestGarbage = function (col) |
| 2331 | { |
| 2332 | if (this.state !== 'playing') |
| 2333 | { return null; } |
| 2334 | var want = (col != null && isFinite(col)) ? (col | 0) : null; |
| 2335 | var item = { col: want, block: null }; |
| 2336 | this.garbageQueue.push(item); |
| 2337 | this.drainGarbageQueue(); |
| 2338 | return item.block; |
| 2339 | }; |
| 2340 | |
| 2341 | /** |
| 2342 | * Can a block enter `col` at the spawn row now? Same rule as the opening |
| 2343 | * burst: every block already in the column must be at least a full cell |
| 2344 | * below the spawn row (no overlap). |
| 2345 | */ |
| 2346 | GameCore.prototype.spawnCellClear = function (col) |
| 2347 | { |
| 2348 | for (var i = 0; i < this.blocks.length; i++) |
| 2349 | { |
| 2350 | var b = this.blocks[i]; |
| 2351 | if (b.col === col && b.y > this.height - 1 + 1e-9) |
| 2352 | { return false; } |
| 2353 | } |
| 2354 | return true; |
| 2355 | }; |
| 2356 | |
| 2357 | /** Columns a queued garbage block can drop into right now (t1192). */ |
| 2358 | GameCore.prototype.garbageDropColumns = function () |
| 2359 | { |
| 2360 | var open = this.eligibleGarbageColumns(); |
| 2361 | var departing = this.departingColumns(); |
| 2362 | var out = []; |
| 2363 | for (var i = 0; i < open.length; i++) |
| 2364 | { |
| 2365 | var c = open[i]; |
| 2366 | if (departing[c]) |
| 2367 | { continue; } |
| 2368 | // Room left for one more block once everything in flight lands. |
| 2369 | if (this.groundedCount(c) + this.fallingCount(c) >= this.height) |
| 2370 | { continue; } |
| 2371 | if (!this.spawnCellClear(c)) |
| 2372 | { continue; } |
| 2373 | out.push(c); |
| 2374 | } |
| 2375 | return out; |
| 2376 | }; |
| 2377 | |
| 2378 | /** |
| 2379 | * Drop queued garbage in arrival order while there is room. The wanted |
| 2380 | * column (server land_col / bot pick) wins when it can take a block, |
| 2381 | * otherwise a random open one. When nothing is open the rest waits. |
| 2382 | * Returns how many blocks dropped. |
| 2383 | */ |
| 2384 | GameCore.prototype.drainGarbageQueue = function () |
| 2385 | { |
| 2386 | var dropped = 0; |
| 2387 | while (this.garbageQueue.length && this.state === 'playing') |
| 2388 | { |
| 2389 | var cols = this.garbageDropColumns(); |
| 2390 | if (!cols.length) |
| 2391 | { break; } |
| 2392 | var item = this.garbageQueue[0]; |
| 2393 | var chosen = null; |
| 2394 | if (item.col != null && cols.indexOf(item.col) >= 0) |
| 2395 | { chosen = item.col; } |
| 2396 | else |
| 2397 | { chosen = cols[Math.floor(this.rng() * cols.length) % cols.length]; } |
| 2398 | this.garbageQueue.shift(); |
| 2399 | var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING); |
| 2400 | block.vy = -this.planet.fallSpeed; |
| 2401 | block.flame = false; |
| 2402 | item.block = block; |
| 2403 | dropped++; |
| 2404 | } |
| 2405 | return dropped; |
| 2406 | }; |
| 2407 | |
| 2408 | /** Received blocks still waiting for room (t1192). */ |
| 2409 | GameCore.prototype.pendingGarbage = function () |
| 2410 | { |
| 2411 | return this.garbageQueue.length; |
| 2412 | }; |
| 2413 | |
| 2414 | GameCore.prototype.advanceSettle = function (dt) |
| 2415 | { |
| 2416 | for (var i = 0; i < this.blocks.length; i++) |
| 2417 | { |
| 2418 | var block = this.blocks[i]; |
| 2419 | if (block.state !== STATE_SETTLING) |
| 2420 | { continue; } |
| 2421 | var inLanded = block.clusterId != null |
| 2422 | && this.clusters[block.clusterId] |
| 2423 | && this.clusters[block.clusterId].phase === 'landed'; |
| 2424 | if (block.color === COLOR_GRAY) |
| 2425 | { |
| 2426 | block.settleTime += dt; |
| 2427 | if (block.settleTime >= this.settleTime) |
| 2428 | { |
| 2429 | block.color = this.randomColor(); |
| 2430 | block.settleTime = 0; |
| 2431 | block.flame = false; |
| 2432 | if (inLanded) |
| 2433 | { |
| 2434 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 2435 | block.state = STATE_SETTLING; |
| 2436 | } |
| 2437 | else |
| 2438 | { |
| 2439 | this.leaveCluster(block); |
| 2440 | this.clearFlightState(block); |
| 2441 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | else if (!inLanded) |
| 2446 | { |
| 2447 | // Coloured settling that is not in a landed cluster (shouldn't |
| 2448 | // normally happen): just become still or fall, from rest. |
| 2449 | this.leaveCluster(block); |
| 2450 | this.clearFlightState(block); |
| 2451 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | var ids = Object.keys(this.clusters); |
| 2456 | for (var n = 0; n < ids.length; n++) |
| 2457 | { |
| 2458 | var cluster = this.clusters[ids[n]]; |
| 2459 | if (!cluster || cluster.phase !== 'landed') |
| 2460 | { continue; } |
| 2461 | var members = this.clusterMembers(cluster.id); |
| 2462 | var greySettling = false; |
| 2463 | for (var i = 0; i < members.length; i++) |
| 2464 | { |
| 2465 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 2466 | { greySettling = true; } |
| 2467 | } |
| 2468 | if (!greySettling) |
| 2469 | { this.releaseLandedCluster(cluster); } |
| 2470 | } |
| 2471 | }; |
| 2472 | |
| 2473 | GameCore.prototype.findMatchLines = function () |
| 2474 | { |
| 2475 | var candidates = []; |
| 2476 | for (var i = 0; i < this.blocks.length; i++) |
| 2477 | { |
| 2478 | var block = this.blocks[i]; |
| 2479 | // Coloured cargo in a just-landed cluster (SETTLING) can match |
| 2480 | // with already-resting blocks immediately (t1110). Greys have no |
| 2481 | // colour and wait until they recolor; after release they are STILL. |
| 2482 | if ((block.state === STATE_STILL || block.state === STATE_SETTLING) |
| 2483 | && block.color >= 0) |
| 2484 | { candidates.push(block); } |
| 2485 | } |
| 2486 | return this.collectLines(candidates, true); |
| 2487 | }; |
| 2488 | |
| 2489 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 2490 | { |
| 2491 | var candidates = []; |
| 2492 | for (var i = 0; i < this.blocks.length; i++) |
| 2493 | { |
| 2494 | var block = this.blocks[i]; |
| 2495 | if (block.clusterId === clusterId && block.color >= 0 |
| 2496 | && (block.state === STATE_FLYING)) |
| 2497 | { candidates.push(block); } |
| 2498 | } |
| 2499 | return this.collectLines(candidates, false); |
| 2500 | }; |
| 2501 | |
| 2502 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 2503 | { |
| 2504 | var lines = []; |
| 2505 | if (!candidates.length) |
| 2506 | { return lines; } |
| 2507 | |
| 2508 | var byRow = Object.create(null); |
| 2509 | for (var i = 0; i < candidates.length; i++) |
| 2510 | { |
| 2511 | var block = candidates[i]; |
| 2512 | var row = Math.round(block.y); |
| 2513 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 2514 | { continue; } |
| 2515 | if (!byRow[row]) |
| 2516 | { byRow[row] = []; } |
| 2517 | byRow[row].push(block); |
| 2518 | } |
| 2519 | var rows = Object.keys(byRow); |
| 2520 | for (var r = 0; r < rows.length; r++) |
| 2521 | { |
| 2522 | var list = byRow[rows[r]]; |
| 2523 | list.sort(function (a, b) { return a.col - b.col; }); |
| 2524 | this.pushRuns(lines, list, false, function (a, b) |
| 2525 | { |
| 2526 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 2527 | }); |
| 2528 | } |
| 2529 | |
| 2530 | var byCol = Object.create(null); |
| 2531 | for (var i = 0; i < candidates.length; i++) |
| 2532 | { |
| 2533 | var block = candidates[i]; |
| 2534 | if (!byCol[block.col]) |
| 2535 | { byCol[block.col] = []; } |
| 2536 | byCol[block.col].push(block); |
| 2537 | } |
| 2538 | var cols = Object.keys(byCol); |
| 2539 | for (var c = 0; c < cols.length; c++) |
| 2540 | { |
| 2541 | var list = byCol[cols[c]]; |
| 2542 | list.sort(function (a, b) { return a.y - b.y; }); |
| 2543 | this.pushRuns(lines, list, true, function (a, b) |
| 2544 | { |
| 2545 | return a.color === b.color && a.color >= 0 |
| 2546 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 2547 | }); |
| 2548 | } |
| 2549 | return lines; |
| 2550 | }; |
| 2551 | |
| 2552 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 2553 | { |
| 2554 | var start = 0; |
| 2555 | for (var i = 1; i <= list.length; i++) |
| 2556 | { |
| 2557 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 2558 | if (!same) |
| 2559 | { |
| 2560 | var length = i - start; |
| 2561 | if (length >= 3) |
| 2562 | { |
| 2563 | lines.push({ |
| 2564 | blocks: list.slice(start, i), |
| 2565 | length: length, |
| 2566 | vertical: vertical |
| 2567 | }); |
| 2568 | } |
| 2569 | start = i; |
| 2570 | } |
| 2571 | } |
| 2572 | }; |
| 2573 | |
| 2574 | GameCore.prototype.resolveMatches = function () |
| 2575 | { |
| 2576 | if (this.state !== 'playing' || this.drag) |
| 2577 | { return []; } |
| 2578 | var lines = this.findMatchLines(); |
| 2579 | if (!lines.length) |
| 2580 | { |
| 2581 | this.finishLandingChains([]); |
| 2582 | return []; |
| 2583 | } |
| 2584 | |
| 2585 | var uf = new UnionFind(); |
| 2586 | var blockById = Object.create(null); |
| 2587 | for (var i = 0; i < lines.length; i++) |
| 2588 | { |
| 2589 | var line = lines[i]; |
| 2590 | for (var j = 0; j < line.blocks.length; j++) |
| 2591 | { |
| 2592 | uf.add(line.blocks[j].id); |
| 2593 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 2594 | if (j > 0) |
| 2595 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 2596 | } |
| 2597 | } |
| 2598 | var matched = []; |
| 2599 | var seen = Object.create(null); |
| 2600 | for (var id in blockById) |
| 2601 | { |
| 2602 | if (!seen[id]) |
| 2603 | { |
| 2604 | seen[id] = true; |
| 2605 | matched.push(blockById[id]); |
| 2606 | } |
| 2607 | } |
| 2608 | for (var i = 0; i < matched.length; i++) |
| 2609 | { |
| 2610 | for (var j = i + 1; j < matched.length; j++) |
| 2611 | { |
| 2612 | if (matched[i].col === matched[j].col) |
| 2613 | { uf.union(matched[i].id, matched[j].id); } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | var groups = Object.create(null); |
| 2618 | for (var i = 0; i < matched.length; i++) |
| 2619 | { |
| 2620 | var root = uf.find(matched[i].id); |
| 2621 | if (!groups[root]) |
| 2622 | { groups[root] = []; } |
| 2623 | groups[root].push(matched[i]); |
| 2624 | } |
| 2625 | |
| 2626 | var launched = []; |
| 2627 | for (var root in groups) |
| 2628 | { |
| 2629 | var group = groups[root]; |
| 2630 | var height = 0; |
| 2631 | for (var i = 0; i < lines.length; i++) |
| 2632 | { |
| 2633 | var line = lines[i]; |
| 2634 | var overlaps = false; |
| 2635 | for (var j = 0; j < line.blocks.length; j++) |
| 2636 | { |
| 2637 | for (var k = 0; k < group.length; k++) |
| 2638 | { |
| 2639 | if (line.blocks[j].id === group[k].id) |
| 2640 | { overlaps = true; } |
| 2641 | } |
| 2642 | } |
| 2643 | if (overlaps) |
| 2644 | { |
| 2645 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 2646 | if (h > height) |
| 2647 | { height = h; } |
| 2648 | } |
| 2649 | } |
| 2650 | var chain = this.landingChainFor(group); |
| 2651 | // t1197/t1198: combo bonus = COMBO_STEP per earlier match in this combo |
| 2652 | // (at most COMBO_MAX_STEPS steps), |
| 2653 | // added to the landing-chain bonus (one shown "+x%"). |
| 2654 | var comboBonus = GameCore.COMBO_STEP * Math.min(this.combo, GameCore.COMBO_MAX_STEPS); |
| 2655 | var bonusMul = 1 + LANDING_CHAIN_BONUS * chain + comboBonus; |
| 2656 | height *= bonusMul; |
| 2657 | this.launchCluster(group, height, { landingChain: chain }); |
| 2658 | launched.push({ |
| 2659 | blocks: group, |
| 2660 | jumpHeight: height, |
| 2661 | landingChain: chain, |
| 2662 | comboBonus: comboBonus, |
| 2663 | chainBonus: bonusMul |
| 2664 | }); |
| 2665 | this.combo += 1; |
| 2666 | this.comboAge = 0; |
| 2667 | } |
| 2668 | this.finishLandingChains(launched); |
| 2669 | this.updateChainIndicator(launched); |
| 2670 | return launched; |
| 2671 | }; |
| 2672 | |
| 2673 | /** |
| 2674 | * Gray the matched blocks, take everything sitting on top of them in |
| 2675 | * those columns, link them, and launch with jump height `height`. |
| 2676 | */ |
| 2677 | GameCore.prototype.launchCluster = function (matchedBlocks, height, opts) |
| 2678 | { |
| 2679 | opts = opts || {}; |
| 2680 | var lowestInCol = Object.create(null); |
| 2681 | var matchedIds = Object.create(null); |
| 2682 | for (var i = 0; i < matchedBlocks.length; i++) |
| 2683 | { |
| 2684 | var block = matchedBlocks[i]; |
| 2685 | matchedIds[block.id] = true; |
| 2686 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 2687 | { lowestInCol[block.col] = block.y; } |
| 2688 | } |
| 2689 | |
| 2690 | var clusterId = this.nextClusterId++; |
| 2691 | var cluster = { |
| 2692 | id: clusterId, |
| 2693 | jumpHeight: height, |
| 2694 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 2695 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 2696 | phase: 'up', |
| 2697 | landingChain: opts.landingChain || 0, |
| 2698 | landedPending: false |
| 2699 | }; |
| 2700 | this.clusters[clusterId] = cluster; |
| 2701 | |
| 2702 | var riders = []; |
| 2703 | var cols = Object.keys(lowestInCol); |
| 2704 | for (var c = 0; c < cols.length; c++) |
| 2705 | { |
| 2706 | var col = +cols[c]; |
| 2707 | var lowest = lowestInCol[col]; |
| 2708 | var list = this.blocksInColumn(col); |
| 2709 | for (var i = 0; i < list.length; i++) |
| 2710 | { |
| 2711 | var block = list[i]; |
| 2712 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 2713 | { |
| 2714 | if (block.state !== STATE_SETTLING) |
| 2715 | { continue; } |
| 2716 | } |
| 2717 | if (block.y + ALIGN_EPS >= lowest) |
| 2718 | { riders.push(block); } |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | for (var i = 0; i < riders.length; i++) |
| 2723 | { |
| 2724 | var block = riders[i]; |
| 2725 | var isMatch = !!matchedIds[block.id]; |
| 2726 | if (isMatch || block.color === COLOR_GRAY) |
| 2727 | { |
| 2728 | block.color = COLOR_GRAY; |
| 2729 | block.flame = true; |
| 2730 | } |
| 2731 | else |
| 2732 | { block.flame = false; } |
| 2733 | block.state = STATE_FLYING; |
| 2734 | block.clusterId = clusterId; |
| 2735 | block.vy = cluster.vy; |
| 2736 | block.settleTime = 0; |
| 2737 | } |
| 2738 | this.prepareClusterBody(cluster); |
| 2739 | return cluster; |
| 2740 | }; |
| 2741 | |
| 2742 | GameCore.prototype.checkLocks = function () |
| 2743 | { |
| 2744 | for (var col = 0; col < this.width; col++) |
| 2745 | { |
| 2746 | if (this.isColumnLocked(col)) |
| 2747 | { continue; } |
| 2748 | if (this.groundedCount(col) < this.height) |
| 2749 | { continue; } |
| 2750 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 2751 | var filled = true; |
| 2752 | for (var row = 0; row < this.height; row++) |
| 2753 | { |
| 2754 | var block = this.blockAt(col, row); |
| 2755 | if (!block || !this.isGrounded(block)) |
| 2756 | { |
| 2757 | filled = false; |
| 2758 | break; |
| 2759 | } |
| 2760 | } |
| 2761 | if (!filled) |
| 2762 | { continue; } |
| 2763 | var list = this.blocksInColumn(col); |
| 2764 | for (var i = 0; i < list.length; i++) |
| 2765 | { |
| 2766 | var block = list[i]; |
| 2767 | if (!this.isGrounded(block)) |
| 2768 | { continue; } |
| 2769 | block.state = STATE_LOCKED; |
| 2770 | block.color = COLOR_GRAY; |
| 2771 | block.flame = false; |
| 2772 | block.vy = 0; |
| 2773 | block.settleTime = 0; |
| 2774 | if (block.clusterId != null) |
| 2775 | { this.leaveCluster(block); } |
| 2776 | } |
| 2777 | } |
| 2778 | }; |
| 2779 | |
| 2780 | GameCore.prototype.checkGameOver = function () |
| 2781 | { |
| 2782 | if (this.state !== 'playing') |
| 2783 | { return; } |
| 2784 | for (var col = 0; col < this.width; col++) |
| 2785 | { |
| 2786 | if (!this.isColumnLocked(col)) |
| 2787 | { return; } |
| 2788 | } |
| 2789 | this.state = 'gameover'; |
| 2790 | this.gameOverTime = 0; |
| 2791 | this.drag = null; |
| 2792 | }; |
| 2793 | |
| 2794 | /* ---------- dragging -------------------------------------------------- */ |
| 2795 | |
| 2796 | GameCore.prototype.beginDrag = function (col, y) |
| 2797 | { |
| 2798 | if (this.state !== 'playing') |
| 2799 | { return false; } |
| 2800 | var block = this.snapDragBlock(col, y); |
| 2801 | if (!block) |
| 2802 | { return false; } |
| 2803 | var top = this.columnStackTopRow(block.col); |
| 2804 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2805 | this.drag = { |
| 2806 | blockId: block.id, |
| 2807 | pointerY: y, |
| 2808 | startY: y, |
| 2809 | accum: 0, |
| 2810 | launchIntent: false, |
| 2811 | startedAsTop: startedAsTop |
| 2812 | }; |
| 2813 | return true; |
| 2814 | }; |
| 2815 | |
| 2816 | /** |
| 2817 | * Instant grab, cell-based (t1109). The press is mapped to the grid cell |
| 2818 | * floor(col), floor(y); any press inside that cell — corners, padding, |
| 2819 | * gaps between sprites, a visually tweened or bouncing sprite — grabs |
| 2820 | * the block that occupies it. This uses simulation cells, not the drawn |
| 2821 | * sprite bounds. If no block sits in that cell (mid-animation, a block |
| 2822 | * whose y is off the integer), fall back to the nearest grabbable block |
| 2823 | * in the same column within SNAP_Y (about half a tile). A small |
| 2824 | * horizontal slop still consults a neighbouring column when the press |
| 2825 | * is near the edge and the primary cell is empty. |
| 2826 | */ |
| 2827 | GameCore.prototype.snapDragBlock = function (col, y) |
| 2828 | { |
| 2829 | var raw = col; |
| 2830 | var c0 = Math.floor(raw + 1e-9); |
| 2831 | var row = Math.floor(y + 1e-9); |
| 2832 | var frac = raw - c0; |
| 2833 | |
| 2834 | if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0)) |
| 2835 | { |
| 2836 | var exact = this.draggableInCell(c0, row); |
| 2837 | if (exact) |
| 2838 | { return exact; } |
| 2839 | var near = this.nearestDraggableInColumn(c0, y, SNAP_Y); |
| 2840 | if (near) |
| 2841 | { return near; } |
| 2842 | } |
| 2843 | |
| 2844 | // Primary cell empty: allow a small horizontal slop into a neighbour. |
| 2845 | var neighbours = []; |
| 2846 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 2847 | { neighbours.push(c0 - 1); } |
| 2848 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 2849 | { neighbours.push(c0 + 1); } |
| 2850 | var best = null; |
| 2851 | var bestDist = Infinity; |
| 2852 | for (var n = 0; n < neighbours.length; n++) |
| 2853 | { |
| 2854 | var cc = neighbours[n]; |
| 2855 | if (!this.columnAllowsDrag(cc)) |
| 2856 | { continue; } |
| 2857 | var hit = this.draggableInCell(cc, row) || this.nearestDraggableInColumn(cc, y, SNAP_Y); |
| 2858 | if (!hit) |
| 2859 | { continue; } |
| 2860 | var dist = this.pointerDistToBlock(hit, raw, y); |
| 2861 | if (dist < bestDist) |
| 2862 | { |
| 2863 | bestDist = dist; |
| 2864 | best = hit; |
| 2865 | } |
| 2866 | } |
| 2867 | return best; |
| 2868 | }; |
| 2869 | |
| 2870 | /** The draggable block that occupies grid cell (col, row), if any. */ |
| 2871 | GameCore.prototype.draggableInCell = function (col, row) |
| 2872 | { |
| 2873 | var best = null; |
| 2874 | var bestOverlap = 0; |
| 2875 | var list = this.blocksInColumn(col); |
| 2876 | for (var i = 0; i < list.length; i++) |
| 2877 | { |
| 2878 | var block = list[i]; |
| 2879 | if (!this.isDraggableBlock(block)) |
| 2880 | { continue; } |
| 2881 | var lo = Math.max(block.y, row); |
| 2882 | var hi = Math.min(block.y + 1, row + 1); |
| 2883 | var overlap = hi - lo; |
| 2884 | if (overlap > bestOverlap + 1e-12) |
| 2885 | { |
| 2886 | bestOverlap = overlap; |
| 2887 | best = block; |
| 2888 | } |
| 2889 | } |
| 2890 | if (best && bestOverlap > 1e-9) |
| 2891 | { return best; } |
| 2892 | return null; |
| 2893 | }; |
| 2894 | |
| 2895 | /** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */ |
| 2896 | GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist) |
| 2897 | { |
| 2898 | var best = null; |
| 2899 | var bestDist = Infinity; |
| 2900 | var list = this.blocksInColumn(col); |
| 2901 | for (var i = 0; i < list.length; i++) |
| 2902 | { |
| 2903 | var block = list[i]; |
| 2904 | if (!this.isDraggableBlock(block)) |
| 2905 | { continue; } |
| 2906 | var dist = this.pointerDistToBlock(block, col + 0.5, y); |
| 2907 | if (dist < bestDist) |
| 2908 | { |
| 2909 | bestDist = dist; |
| 2910 | best = block; |
| 2911 | } |
| 2912 | } |
| 2913 | if (!best || bestDist > maxDist) |
| 2914 | { return null; } |
| 2915 | return best; |
| 2916 | }; |
| 2917 | |
| 2918 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 2919 | { |
| 2920 | var dx; |
| 2921 | if (col >= block.col && col < block.col + 1) |
| 2922 | { dx = 0; } |
| 2923 | else if (col < block.col) |
| 2924 | { dx = block.col - col; } |
| 2925 | else |
| 2926 | { dx = col - (block.col + 1); } |
| 2927 | var dy; |
| 2928 | if (y >= block.y && y < block.y + 1) |
| 2929 | { dy = 0; } |
| 2930 | else if (y < block.y) |
| 2931 | { dy = block.y - y; } |
| 2932 | else |
| 2933 | { dy = y - (block.y + 1); } |
| 2934 | return Math.sqrt(dx * dx + dy * dy); |
| 2935 | }; |
| 2936 | |
| 2937 | GameCore.prototype.moveDrag = function (y) |
| 2938 | { |
| 2939 | if (!this.drag) |
| 2940 | { return; } |
| 2941 | var block = this.blockById(this.drag.blockId); |
| 2942 | if (!block || !this.isDraggableBlock(block)) |
| 2943 | { |
| 2944 | this.drag = null; |
| 2945 | return; |
| 2946 | } |
| 2947 | if (!this.columnAllowsDrag(block.col)) |
| 2948 | { |
| 2949 | this.drag = null; |
| 2950 | return; |
| 2951 | } |
| 2952 | this.drag.pointerY = y; |
| 2953 | // 1:1 tracking: the target row is the one under the pointer, clamped |
| 2954 | // to the still stack. Swap step by step with each neighbour, and stop |
| 2955 | // exactly there. When the pointer stops, nothing else moves. |
| 2956 | // Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold |
| 2957 | // used to jump a full cell on a short pull, and one large event |
| 2958 | // could walk the entire column (the "jumps all the way" bug). |
| 2959 | var top = this.columnStackTopRow(block.col); |
| 2960 | var bottom = this.columnStackBottomRow(block.col); |
| 2961 | var target = Math.floor(y + 1e-12); |
| 2962 | if (bottom < 0) |
| 2963 | { bottom = 0; } |
| 2964 | if (target < bottom) |
| 2965 | { target = bottom; } |
| 2966 | if (target > top) |
| 2967 | { target = top; } |
| 2968 | this.swapToward(block, target); |
| 2969 | |
| 2970 | top = this.columnStackTopRow(block.col); |
| 2971 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2972 | if (this.drag.startedAsTop) |
| 2973 | { |
| 2974 | // Already the topmost block: pulling upward past its own position |
| 2975 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 2976 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 2977 | } |
| 2978 | else |
| 2979 | { |
| 2980 | // Started mid-stack: swap first, and only launch once the pointer |
| 2981 | // is pulled into the empty space above the (new) top. |
| 2982 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 2983 | } |
| 2984 | }; |
| 2985 | |
| 2986 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 2987 | GameCore.prototype.swapOne = function (block, dir) |
| 2988 | { |
| 2989 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 2990 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 2991 | { |
| 2992 | this.swapBlocks(block, neighbor); |
| 2993 | return true; |
| 2994 | } |
| 2995 | return false; |
| 2996 | }; |
| 2997 | |
| 2998 | GameCore.prototype.endDrag = function () |
| 2999 | { |
| 3000 | if (!this.drag) |
| 3001 | { return; } |
| 3002 | var block = this.blockById(this.drag.blockId); |
| 3003 | var launch = this.drag.launchIntent; |
| 3004 | this.drag = null; |
| 3005 | if (launch && block && block.state === STATE_STILL) |
| 3006 | { this.startManualLaunch(block); } |
| 3007 | else |
| 3008 | { this.resolveMatches(); } |
| 3009 | }; |
| 3010 | |
| 3011 | GameCore.prototype.blockById = function (id) |
| 3012 | { |
| 3013 | for (var i = 0; i < this.blocks.length; i++) |
| 3014 | { |
| 3015 | if (this.blocks[i].id === id) |
| 3016 | { return this.blocks[i]; } |
| 3017 | } |
| 3018 | return null; |
| 3019 | }; |
| 3020 | |
| 3021 | GameCore.prototype.columnStackTopRow = function (col) |
| 3022 | { |
| 3023 | var top = -1; |
| 3024 | var list = this.blocksInColumn(col); |
| 3025 | for (var i = 0; i < list.length; i++) |
| 3026 | { |
| 3027 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 3028 | { top = list[i].y; } |
| 3029 | } |
| 3030 | return top; |
| 3031 | }; |
| 3032 | |
| 3033 | GameCore.prototype.columnStackBottomRow = function (col) |
| 3034 | { |
| 3035 | var bottom = Infinity; |
| 3036 | var list = this.blocksInColumn(col); |
| 3037 | for (var i = 0; i < list.length; i++) |
| 3038 | { |
| 3039 | if (this.isGrounded(list[i]) && list[i].y < bottom) |
| 3040 | { bottom = list[i].y; } |
| 3041 | } |
| 3042 | return bottom === Infinity ? 0 : bottom; |
| 3043 | }; |
| 3044 | |
| 3045 | GameCore.prototype.swapToward = function (block, targetY) |
| 3046 | { |
| 3047 | var guard = 0; |
| 3048 | while (guard++ < this.height + 2) |
| 3049 | { |
| 3050 | var current = block.y; |
| 3051 | if (targetY > current + 0.5) |
| 3052 | { |
| 3053 | var above = this.blockAt(block.col, current + 1); |
| 3054 | if (above && this.isGrounded(above) && above !== block) |
| 3055 | { this.swapBlocks(block, above); } |
| 3056 | else |
| 3057 | { break; } |
| 3058 | } |
| 3059 | else if (targetY < current - 0.5) |
| 3060 | { |
| 3061 | var below = this.blockAt(block.col, current - 1); |
| 3062 | if (below && this.isGrounded(below) && below !== block) |
| 3063 | { this.swapBlocks(block, below); } |
| 3064 | else |
| 3065 | { break; } |
| 3066 | } |
| 3067 | else |
| 3068 | { break; } |
| 3069 | } |
| 3070 | }; |
| 3071 | |
| 3072 | GameCore.prototype.swapBlocks = function (a, b) |
| 3073 | { |
| 3074 | var ay = a.y; |
| 3075 | var by = b.y; |
| 3076 | a.y = by; |
| 3077 | b.y = ay; |
| 3078 | this.transferLandedMembership(a, b); |
| 3079 | if (!this.swapEvents) |
| 3080 | { this.swapEvents = []; } |
| 3081 | this.swapEvents.push({ id: a.id, fromY: ay, toY: by, col: a.col }); |
| 3082 | this.swapEvents.push({ id: b.id, fromY: by, toY: ay, col: b.col }); |
| 3083 | }; |
| 3084 | |
| 3085 | GameCore.prototype.consumeSwapEvents = function () |
| 3086 | { |
| 3087 | var ev = this.swapEvents || []; |
| 3088 | this.swapEvents = []; |
| 3089 | return ev; |
| 3090 | }; |
| 3091 | |
| 3092 | /** |
| 3093 | * Highest landing-chain counter among just-landed members of this |
| 3094 | * match group, plus one. Returns 0 when the match did not come from |
| 3095 | * a cluster landing (player/bot swap, mid-air, etc.). |
| 3096 | */ |
| 3097 | GameCore.prototype.landingChainFor = function (group) |
| 3098 | { |
| 3099 | var maxChain = 0; |
| 3100 | var fromLanding = false; |
| 3101 | for (var i = 0; i < group.length; i++) |
| 3102 | { |
| 3103 | var block = group[i]; |
| 3104 | if (block.justLanded) |
| 3105 | { |
| 3106 | fromLanding = true; |
| 3107 | if ((block.landingChain || 0) > maxChain) |
| 3108 | { maxChain = block.landingChain || 0; } |
| 3109 | } |
| 3110 | if (block.clusterId != null) |
| 3111 | { |
| 3112 | var cluster = this.clusters[block.clusterId]; |
| 3113 | if (cluster && cluster.landedPending) |
| 3114 | { |
| 3115 | fromLanding = true; |
| 3116 | if ((cluster.landingChain || 0) > maxChain) |
| 3117 | { maxChain = cluster.landingChain || 0; } |
| 3118 | } |
| 3119 | } |
| 3120 | } |
| 3121 | if (!fromLanding) |
| 3122 | { return 0; } |
| 3123 | return maxChain + 1; |
| 3124 | }; |
| 3125 | |
| 3126 | /** |
| 3127 | * After resolveMatches: clear the landing-pending flag and reset the |
| 3128 | * chain on landings that did not make a match. A cluster that is still |
| 3129 | * settling (greys not yet recolored) keeps landedPending so a later |
| 3130 | * post-recolor match in this same landing still gets the bonus. |
| 3131 | */ |
| 3132 | GameCore.prototype.finishLandingChains = function (launched) |
| 3133 | { |
| 3134 | var launchedIds = Object.create(null); |
| 3135 | launched = launched || []; |
| 3136 | for (var i = 0; i < launched.length; i++) |
| 3137 | { |
| 3138 | var blocks = launched[i].blocks || []; |
| 3139 | for (var j = 0; j < blocks.length; j++) |
| 3140 | { launchedIds[blocks[j].id] = true; } |
| 3141 | } |
| 3142 | |
| 3143 | for (var i = 0; i < this.blocks.length; i++) |
| 3144 | { |
| 3145 | var block = this.blocks[i]; |
| 3146 | if (!block.justLanded) |
| 3147 | { continue; } |
| 3148 | if (!launchedIds[block.id]) |
| 3149 | { block.landingChain = 0; } |
| 3150 | block.justLanded = false; |
| 3151 | } |
| 3152 | |
| 3153 | var ids = Object.keys(this.clusters); |
| 3154 | for (var c = 0; c < ids.length; c++) |
| 3155 | { |
| 3156 | var cluster = this.clusters[ids[c]]; |
| 3157 | if (!cluster || !cluster.landedPending) |
| 3158 | { continue; } |
| 3159 | var members = this.clusterMembers(cluster.id); |
| 3160 | var anyLaunched = false; |
| 3161 | var stillSettling = false; |
| 3162 | for (var i = 0; i < members.length; i++) |
| 3163 | { |
| 3164 | if (launchedIds[members[i].id]) |
| 3165 | { anyLaunched = true; } |
| 3166 | if (members[i].state === STATE_SETTLING) |
| 3167 | { stillSettling = true; } |
| 3168 | } |
| 3169 | if (anyLaunched) |
| 3170 | { |
| 3171 | cluster.landingChain = 0; |
| 3172 | cluster.landedPending = false; |
| 3173 | } |
| 3174 | else if (!stillSettling) |
| 3175 | { |
| 3176 | // Released leftovers or a fully-still landed group that |
| 3177 | // already had its match opportunity and missed it. |
| 3178 | cluster.landingChain = 0; |
| 3179 | cluster.landedPending = false; |
| 3180 | } |
| 3181 | // else: still settling, no match yet — keep landedPending. |
| 3182 | } |
| 3183 | }; |
| 3184 | |
| 3185 | GameCore.prototype.updateChainIndicator = function (launched) |
| 3186 | { |
| 3187 | var maxChain = 0; |
| 3188 | var maxPct = 0; |
| 3189 | for (var i = 0; i < launched.length; i++) |
| 3190 | { |
| 3191 | var ch = launched[i].landingChain || 0; |
| 3192 | if (ch > maxChain) |
| 3193 | { maxChain = ch; } |
| 3194 | var pct = Math.round(((launched[i].chainBonus || 1) - 1) * 100); |
| 3195 | if (pct > maxPct) |
| 3196 | { maxPct = pct; } |
| 3197 | } |
| 3198 | if (maxPct > 0) |
| 3199 | { |
| 3200 | this.lastChainIndicator = { |
| 3201 | chain: maxChain, |
| 3202 | combo: this.combo, |
| 3203 | bonusPct: maxPct |
| 3204 | }; |
| 3205 | return; |
| 3206 | } |
| 3207 | if (!this.hasActiveLandingChain() && !(launched.length && this.combo > 0)) |
| 3208 | { this.lastChainIndicator = null; } |
| 3209 | }; |
| 3210 | |
| 3211 | /** |
| 3212 | * Combo bonus in percent, 0.01·min(combo, 30)·100: what the next match gets |
| 3213 | * from the combo alone. This is the only number the HUD "+x%" shows (t1200). |
| 3214 | */ |
| 3215 | GameCore.prototype.comboBonusPct = function () |
| 3216 | { |
| 3217 | return Math.round(GameCore.COMBO_STEP * Math.min(this.combo, GameCore.COMBO_MAX_STEPS) * 100); |
| 3218 | }; |
| 3219 | |
| 3220 | GameCore.prototype.hasActiveLandingChain = function () |
| 3221 | { |
| 3222 | var ids = Object.keys(this.clusters); |
| 3223 | for (var i = 0; i < ids.length; i++) |
| 3224 | { |
| 3225 | var cluster = this.clusters[ids[i]]; |
| 3226 | if (cluster && (cluster.landingChain || 0) >= 1 |
| 3227 | && cluster.phase !== 'landed') |
| 3228 | { return true; } |
| 3229 | } |
| 3230 | return false; |
| 3231 | }; |
| 3232 | |
| 3233 | /** |
| 3234 | * A landed cluster (grey rockets or linked cargo resting on the ground) |
| 3235 | * is a swappable part of the contiguous grounded stack. Flying, falling |
| 3236 | * and manual blocks are not. A locked column is not. |
| 3237 | */ |
| 3238 | GameCore.prototype.isDraggableBlock = function (block) |
| 3239 | { |
| 3240 | if (!block) |
| 3241 | { return false; } |
| 3242 | if (block.state === STATE_LOCKED || block.state === STATE_FLYING |
| 3243 | || block.state === STATE_FALLING || block.state === STATE_MANUAL) |
| 3244 | { return false; } |
| 3245 | if (block.clusterId != null) |
| 3246 | { |
| 3247 | var cluster = this.clusters[block.clusterId]; |
| 3248 | // Flying (rising / descending) clusters stay non-draggable. |
| 3249 | if (cluster && cluster.phase !== 'landed') |
| 3250 | { return false; } |
| 3251 | } |
| 3252 | return block.state === STATE_STILL || block.state === STATE_SETTLING; |
| 3253 | }; |
| 3254 | |
| 3255 | /** |
| 3256 | * The grounded contiguous stack in this column can be dragged, even if a |
| 3257 | * flying cluster or a falling spawn occupies the air above it. Only a |
| 3258 | * locked column is blocked as a whole. |
| 3259 | */ |
| 3260 | GameCore.prototype.columnAllowsDrag = function (col) |
| 3261 | { |
| 3262 | return !this.isColumnLocked(col); |
| 3263 | }; |
| 3264 | |
| 3265 | GameCore.prototype.landedClusterId = function (block) |
| 3266 | { |
| 3267 | if (!block || block.clusterId == null) |
| 3268 | { return null; } |
| 3269 | var cluster = this.clusters[block.clusterId]; |
| 3270 | if (!cluster || cluster.phase !== 'landed') |
| 3271 | { return null; } |
| 3272 | return block.clusterId; |
| 3273 | }; |
| 3274 | |
| 3275 | /** |
| 3276 | * After a swap: leaving a landed cluster clears membership; joining one |
| 3277 | * makes the block part of that rigid group. Same-cluster swaps only |
| 3278 | * exchange positions. See docs/ASSUMPTIONS.md (t1107). |
| 3279 | */ |
| 3280 | GameCore.prototype.transferLandedMembership = function (a, b) |
| 3281 | { |
| 3282 | var ca = this.landedClusterId(a); |
| 3283 | var cb = this.landedClusterId(b); |
| 3284 | if (ca === cb) |
| 3285 | { return; } |
| 3286 | // Join first so leaveCluster does not delete a cluster that still |
| 3287 | // has the incoming member. |
| 3288 | this.applyLandedMembership(b, ca); |
| 3289 | this.applyLandedMembership(a, cb); |
| 3290 | }; |
| 3291 | |
| 3292 | GameCore.prototype.applyLandedMembership = function (block, clusterId) |
| 3293 | { |
| 3294 | if (clusterId == null) |
| 3295 | { |
| 3296 | var wasGrey = block.color === COLOR_GRAY; |
| 3297 | var wasSettling = block.state === STATE_SETTLING; |
| 3298 | this.leaveCluster(block); |
| 3299 | if (wasGrey && wasSettling) |
| 3300 | { |
| 3301 | // Keep settling and recolor on its own, same remaining timer. |
| 3302 | block.state = STATE_SETTLING; |
| 3303 | } |
| 3304 | else if (wasSettling) |
| 3305 | { |
| 3306 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 3307 | } |
| 3308 | return; |
| 3309 | } |
| 3310 | if (!this.clusters[clusterId]) |
| 3311 | { return; } |
| 3312 | block.clusterId = clusterId; |
| 3313 | block.flame = false; |
| 3314 | if (block.state === STATE_STILL || block.state === STATE_SETTLING) |
| 3315 | { block.state = STATE_SETTLING; } |
| 3316 | }; |
| 3317 | |
| 3318 | /** |
| 3319 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 3320 | * Returns the moved block, or null if the column does not allow a drag. |
| 3321 | */ |
| 3322 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 3323 | { |
| 3324 | if (!this.columnAllowsDrag(col)) |
| 3325 | { return null; } |
| 3326 | var block = this.blockAt(col, fromRow); |
| 3327 | if (!block) |
| 3328 | { return null; } |
| 3329 | this.swapToward(block, toRow); |
| 3330 | return block; |
| 3331 | }; |
| 3332 | |
| 3333 | /** |
| 3334 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 3335 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 3336 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 3337 | * their color with a marker in `flying`. |
| 3338 | */ |
| 3339 | GameCore.prototype.snapshot = function () |
| 3340 | { |
| 3341 | var grid = []; |
| 3342 | for (var r = 0; r < this.height; r++) |
| 3343 | { |
| 3344 | var row = []; |
| 3345 | for (var c = 0; c < this.width; c++) |
| 3346 | { row.push('.'); } |
| 3347 | grid.push(row); |
| 3348 | } |
| 3349 | var airborne = []; |
| 3350 | for (var i = 0; i < this.blocks.length; i++) |
| 3351 | { |
| 3352 | var block = this.blocks[i]; |
| 3353 | var ch; |
| 3354 | if (block.state === STATE_LOCKED) |
| 3355 | { ch = 'L'; } |
| 3356 | else if (block.color === COLOR_GRAY) |
| 3357 | { ch = 'g'; } |
| 3358 | else |
| 3359 | { ch = String(block.color); } |
| 3360 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 3361 | { |
| 3362 | var row = Math.round(block.y); |
| 3363 | if (row >= 0 && row < this.height) |
| 3364 | { grid[row][block.col] = ch; } |
| 3365 | } |
| 3366 | else |
| 3367 | { |
| 3368 | airborne.push({ |
| 3369 | id: block.id, |
| 3370 | col: block.col, |
| 3371 | y: block.y, |
| 3372 | color: block.color, |
| 3373 | state: block.state, |
| 3374 | clusterId: block.clusterId, |
| 3375 | flame: block.flame, |
| 3376 | vy: block.vy |
| 3377 | }); |
| 3378 | } |
| 3379 | } |
| 3380 | var lines = []; |
| 3381 | for (var r = this.height - 1; r >= 0; r--) |
| 3382 | { lines.push(grid[r].join('')); } |
| 3383 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 3384 | }; |
| 3385 | |
| 3386 | var GameCoreAPI = { |
| 3387 | GameCore: GameCore, |
| 3388 | COLOR_GRAY: COLOR_GRAY, |
| 3389 | STATE_STILL: STATE_STILL, |
| 3390 | STATE_FALLING: STATE_FALLING, |
| 3391 | STATE_FLYING: STATE_FLYING, |
| 3392 | STATE_SETTLING: STATE_SETTLING, |
| 3393 | STATE_LOCKED: STATE_LOCKED, |
| 3394 | STATE_MANUAL: STATE_MANUAL |
| 3395 | }; |
| 3396 | |
| 3397 | if (typeof module !== 'undefined' && module.exports) |
| 3398 | { module.exports = GameCoreAPI; } |
| 3399 | else if (typeof globalThis !== 'undefined') |
| 3400 | { |
| 3401 | globalThis.GameCore = GameCore; |
| 3402 | globalThis.GameCoreAPI = GameCoreAPI; |
| 3403 | } |
projects-timfalken-com meteors