| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js'); |
| 6 | |
| 7 | function flyingGrayRow (game, y, cols) |
| 8 | { |
| 9 | const cluster = { |
| 10 | id: game.nextClusterId++, |
| 11 | jumpHeight: 3, |
| 12 | vy: -game.planet.fallSpeed, |
| 13 | decel: 0, |
| 14 | phase: 'down' |
| 15 | }; |
| 16 | game.clusters[cluster.id] = cluster; |
| 17 | const members = []; |
| 18 | for (const col of cols) |
| 19 | { |
| 20 | const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying'); |
| 21 | b.clusterId = cluster.id; |
| 22 | b.vy = cluster.vy; |
| 23 | b.flame = true; |
| 24 | members.push(b); |
| 25 | } |
| 26 | return { cluster, members }; |
| 27 | } |
| 28 | |
| 29 | describe('Falling blocks land on flying clusters', () => |
| 30 | { |
| 31 | it('a falling spawn lands on a flying cluster and flies along as cargo', () => |
| 32 | { |
| 33 | const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); |
| 34 | const { cluster, members } = flyingGrayRow(g, 6, [2, 3, 4]); |
| 35 | // Freeze ascent/descent so the falling block can meet them cleanly, |
| 36 | // then unfreeze so they move together. |
| 37 | cluster.phase = 'hold'; |
| 38 | cluster.vy = 0; |
| 39 | const falling = g.createBlock(3, 10, 1, 'falling'); |
| 40 | falling.vy = -g.planet.fallSpeed; |
| 41 | // Fall from 10 onto 6+1=7: distance 3, at full spawn fallSpeed |
| 42 | // 13/3 ≈ 4.33 → ~0.7s (t1124: singles are not halved). |
| 43 | g.step(1.0, 1 / 120); |
| 44 | assert.equal(falling.state, 'flying', 'should have attached, state=' + falling.state + ' y=' + falling.y); |
| 45 | assert.equal(falling.clusterId, cluster.id); |
| 46 | assert.equal(falling.flame, false, 'cargo has no flame'); |
| 47 | assert.equal(falling.color, 1, 'keeps its color'); |
| 48 | assert.ok(almost(falling.y, members[1].y + 1, 0.05), 'sits on top, y=' + falling.y + ' flyer=' + members[1].y); |
| 49 | |
| 50 | // Now let the cluster descend: cargo must move with it, same dy. |
| 51 | cluster.phase = 'down'; |
| 52 | cluster.vy = 0; // will ease into clusterFallSpeed |
| 53 | const yCargo = falling.y; |
| 54 | const yFlyer = members[1].y; |
| 55 | g.step(0.4, 1 / 120); |
| 56 | assert.equal(falling.clusterId, cluster.id); |
| 57 | assert.equal(falling.state, 'flying'); |
| 58 | assert.ok(almost(falling.y - members[1].y, yCargo - yFlyer, 0.05), |
| 59 | 'relative offset kept: cargo=' + falling.y + ' flyer=' + members[1].y); |
| 60 | assert.ok(falling.y < yCargo, 'must have moved down with the cluster'); |
| 61 | assert.ok(almost(falling.vy, members[1].vy, 1e-9), 'same vy'); |
| 62 | }); |
| 63 | |
| 64 | it('a falling block does not pass through a descending or rising cluster', () => |
| 65 | { |
| 66 | const g = makeGame({ noSpawn: true }); |
| 67 | const { cluster } = flyingGrayRow(g, 5, [1]); |
| 68 | cluster.phase = 'down'; |
| 69 | cluster.vy = -g.planet.fallSpeed; |
| 70 | const falling = g.createBlock(1, 8, 2, 'falling'); |
| 71 | falling.vy = -g.planet.fallSpeed; |
| 72 | // The falling single is at full fallSpeed; the cluster floats |
| 73 | // at the halved clusterFallSpeed (t1124), so the single catches |
| 74 | // up. 2 s is plenty for a 3-tile gap. |
| 75 | g.step(2.0, 1 / 120); |
| 76 | assert.notEqual(falling.state, 'falling'); |
| 77 | assert.ok(falling.state === 'flying' || falling.state === 'settling' || falling.state === 'still', |
| 78 | 'must not still be falling through, state=' + falling.state + ' y=' + falling.y); |
| 79 | // Never went below the flyer without joining (flyer may have landed). |
| 80 | const flyer = g.blocks.find((b) => b.color === GameCore.COLOR_GRAY); |
| 81 | if (falling.state === 'flying' && flyer) |
| 82 | { assert.ok(falling.y >= flyer.y - 1e-6, 'cargo not below its flyer'); } |
| 83 | }); |
| 84 | |
| 85 | it('a rising cluster that meets a falling block picks it up', () => |
| 86 | { |
| 87 | const g = makeGame({ noSpawn: true }); |
| 88 | const { cluster, members } = flyingGrayRow(g, 3, [5]); |
| 89 | cluster.phase = 'up'; |
| 90 | cluster.jumpHeight = 6; |
| 91 | cluster.vy = Planet.launchVelocity(g.planet, 6); |
| 92 | cluster.decel = Planet.launchDeceleration(g.planet, 6); |
| 93 | members[0].vy = cluster.vy; |
| 94 | const falling = g.createBlock(5, 7, 3, 'falling'); |
| 95 | falling.vy = -g.planet.fallSpeed; |
| 96 | g.step(1.6, 1 / 120); |
| 97 | assert.equal(falling.state, 'flying', 'picked up, state=' + falling.state + ' y=' + falling.y); |
| 98 | assert.equal(falling.clusterId, cluster.id); |
| 99 | assert.ok(falling.y >= members[0].y, 'sits at or above the flyer, cargo=' + falling.y + ' flyer=' + members[0].y); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe('Rigid grey landing', () => |
| 104 | { |
| 105 | it('keeps relative positions, so part of the group can hang above a gap', () => |
| 106 | { |
| 107 | const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } }); |
| 108 | // Horizontal match on a pedestal in col 1 only: cols 0 and 2 will hang. |
| 109 | g.place(1, 0, 4); // pedestal |
| 110 | placeRow(g, 1, [0, 1, 2], 0); |
| 111 | g.resolveMatches(); |
| 112 | assert.ok(g.blocks.some((b) => b.state === 'flying')); |
| 113 | let landed = false; |
| 114 | for (let i = 0; i < 3000; i++) |
| 115 | { |
| 116 | g.tick(1 / 120); |
| 117 | const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); |
| 118 | if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) |
| 119 | { |
| 120 | landed = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | assert.ok(landed, 'should have landed rigid\n' + g.snapshot().picture); |
| 125 | const at = (col) => g.blocks.find((b) => b.col === col && b.color === GameCore.COLOR_GRAY); |
| 126 | const a = at(0); |
| 127 | const b = at(1); |
| 128 | const c = at(2); |
| 129 | assert.ok(a && b && c, 'all three greys present'); |
| 130 | // The middle one rests on the pedestal (y=1). The sides hang at the same y. |
| 131 | assert.ok(almost(b.y, 1, 0.05), 'middle sits on pedestal, y=' + b.y); |
| 132 | assert.ok(almost(a.y, b.y, 0.05), 'left hangs at the same height, y=' + a.y); |
| 133 | assert.ok(almost(c.y, b.y, 0.05), 'right hangs at the same height, y=' + c.y); |
| 134 | assert.equal(g.blockAt(0, 0), null, 'gap under the left hang remains'); |
| 135 | assert.equal(g.blockAt(2, 0), null, 'gap under the right hang remains'); |
| 136 | // They stay there through the settle wait. |
| 137 | g.step(2.9, 1 / 60); |
| 138 | assert.ok(almost(a.y, 1, 0.05) && a.state === 'settling'); |
| 139 | }); |
| 140 | |
| 141 | it('after conversion, hanging blocks fall individually and supported ones stay', () => |
| 142 | { |
| 143 | const g = makeGame({ |
| 144 | noSpawn: true, |
| 145 | planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }, |
| 146 | seed: 7 |
| 147 | }); |
| 148 | g.place(1, 0, 4); |
| 149 | placeRow(g, 1, [0, 1, 2], 0); |
| 150 | g.resolveMatches(); |
| 151 | for (let i = 0; i < 3000; i++) |
| 152 | { |
| 153 | g.tick(1 / 120); |
| 154 | const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); |
| 155 | if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) |
| 156 | { break; } |
| 157 | } |
| 158 | const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY); |
| 159 | const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY); |
| 160 | const right = g.blocks.find((b) => b.col === 2 && b.color === GameCore.COLOR_GRAY); |
| 161 | const leftY = left.y; |
| 162 | g.step(3.0, 1 / 120); |
| 163 | // Conversion happens at 3s; take a short step so the fall is visible |
| 164 | // but not finished (from rest, Terra-like ease is not instant). |
| 165 | g.step(0.25, 1 / 120); |
| 166 | assert.ok(mid.color >= 0 && mid.state === 'still', 'supported middle stays, state=' + mid.state); |
| 167 | assert.ok(almost(mid.y, 1, 0.05), 'middle stays on the pedestal, y=' + mid.y); |
| 168 | assert.equal(mid.clusterId, null); |
| 169 | // Hanging sides fall from rest — they must not teleport to the floor. |
| 170 | assert.equal(left.state, 'falling', 'left should still be falling after 0.25s, state=' + left.state + ' y=' + left.y); |
| 171 | assert.ok(left.y < leftY - 0.05, 'left has started falling smoothly, y=' + left.y + ' from ' + leftY); |
| 172 | assert.ok(left.y > 0.25, 'must not have teleported to the floor, y=' + left.y); |
| 173 | assert.ok(Math.abs(left.vy) <= g.planet.fallSpeed + 1e-6); |
| 174 | assert.ok(right.state === 'falling' || (right.state === 'still' && almost(right.y, 0, 0.05)), |
| 175 | 'right falls, state=' + right.state); |
| 176 | // Give them time to land individually. |
| 177 | g.step(g.planet.fallTime + 0.5, 1 / 60); |
| 178 | assert.equal(left.state, 'still'); |
| 179 | assert.ok(almost(left.y, 0, 0.05), 'left lands on the floor, y=' + left.y); |
| 180 | assert.equal(right.state, 'still'); |
| 181 | assert.ok(almost(right.y, 0, 0.05)); |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | describe('Landing bounce planet parameter', () => |
| 186 | { |
| 187 | it('sets bouncing on a landed block, faster at low air resistance', () => |
| 188 | { |
| 189 | const slow = Planet.createPlanet({ fallTime: 5, jumpsToClear: 2, riseTime: 1.5 }); |
| 190 | const fast = Planet.createPlanet({ fallTime: 1, jumpsToClear: 2, riseTime: 1.5 }); |
| 191 | assert.ok(fast.bounceDuration < slow.bounceDuration, |
| 192 | 'low air (fallTime=1) must bounce faster: ' + fast.bounceDuration + ' vs ' + slow.bounceDuration); |
| 193 | assert.ok(almost(fast.bounceDuration, 0.12 * 1, 1e-9)); |
| 194 | assert.ok(almost(slow.bounceDuration, 0.12 * 5, 1e-9)); |
| 195 | assert.equal(fast.bounceSquash, 0.16); |
| 196 | assert.ok(fast.apexEaseTime < slow.apexEaseTime); |
| 197 | assert.ok(fast.fallAccel > slow.fallAccel, 'low air eases into fall faster'); |
| 198 | |
| 199 | const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3 } }); |
| 200 | const block = g.createBlock(0, 5, 1, 'falling'); |
| 201 | block.vy = -g.planet.fallSpeed; |
| 202 | let landed = false; |
| 203 | for (let i = 0; i < 2000; i++) |
| 204 | { |
| 205 | g.tick(1 / 120); |
| 206 | if (block.state === 'still') |
| 207 | { |
| 208 | landed = true; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | assert.ok(landed, 'should have landed'); |
| 213 | assert.ok(almost(block.y, 0, 0.05)); |
| 214 | assert.equal(block.bouncing, true); |
| 215 | assert.ok(block.bounceAge >= 0 && block.bounceAge < g.planet.bounceDuration, |
| 216 | 'bounce just started, age=' + block.bounceAge); |
| 217 | g.step(g.planet.bounceDuration + 0.05, 1 / 60); |
| 218 | assert.equal(block.bouncing, false); |
| 219 | }); |
| 220 | |
| 221 | it('a rigid cluster bounce only on members that actually rest on something', () => |
| 222 | { |
| 223 | const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } }); |
| 224 | g.place(1, 0, 4); |
| 225 | placeRow(g, 1, [0, 1, 2], 0); |
| 226 | g.resolveMatches(); |
| 227 | for (let i = 0; i < 3000; i++) |
| 228 | { |
| 229 | g.tick(1 / 120); |
| 230 | const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); |
| 231 | if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) |
| 232 | { break; } |
| 233 | } |
| 234 | const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY); |
| 235 | const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY); |
| 236 | assert.equal(mid.bouncing, true, 'the member on the pedestal bounces'); |
| 237 | assert.equal(left.bouncing, false, 'the hanging member does not bounce'); |
| 238 | }); |
| 239 | }); |
| 240 | |
| 241 | function landUntilSettling (g, minGrey) |
| 242 | { |
| 243 | minGrey = minGrey || 3; |
| 244 | let landed = false; |
| 245 | for (let i = 0; i < 4000; i++) |
| 246 | { |
| 247 | g.tick(1 / 120); |
| 248 | const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); |
| 249 | const cluster = Object.values(g.clusters)[0]; |
| 250 | if (greys.length >= minGrey && greys.every((b) => b.state === 'settling') |
| 251 | && cluster && cluster.phase === 'landed') |
| 252 | { |
| 253 | landed = true; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | assert.ok(landed, 'cluster should have landed as a settling group\n' + g.snapshot().picture); |
| 258 | } |
| 259 | |
| 260 | describe('Recolor must not shoot blocks off the top (t1102)', () => |
| 261 | { |
| 262 | it('clears leftover upward velocity: hanging cargo falls from rest after recolor, and does not fly off', () => |
| 263 | { |
| 264 | const g = makeGame({ |
| 265 | noSpawn: true, |
| 266 | planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }, |
| 267 | seed: 3 |
| 268 | }); |
| 269 | // Pedestal in col 1, hanging sides; cargo stacked on the left hang |
| 270 | // and on the middle (in-cluster riders). |
| 271 | g.place(1, 0, 4); |
| 272 | placeRow(g, 1, [0, 1, 2], 0); |
| 273 | g.place(0, 2, 3); |
| 274 | g.place(0, 3, 4); |
| 275 | g.place(1, 2, 3); |
| 276 | g.resolveMatches(); |
| 277 | landUntilSettling(g, 3); |
| 278 | |
| 279 | const cargo = g.blocks.filter((b) => b.color >= 0 && b.state === 'settling'); |
| 280 | assert.ok(cargo.length >= 2, 'expected in-cluster cargo, got ' + cargo.length); |
| 281 | const yBefore = {}; |
| 282 | const members = g.blocks.filter((b) => b.state === 'settling'); |
| 283 | for (const b of members) |
| 284 | { |
| 285 | // Inject a leftover upward launch velocity — the kind that |
| 286 | // would shoot blocks off the top if it survived recolor. |
| 287 | b.vy = 40; |
| 288 | yBefore[b.id] = b.y; |
| 289 | } |
| 290 | for (const id of Object.keys(g.clusters)) |
| 291 | { g.clusters[id].vy = 40; } |
| 292 | |
| 293 | // Force non-matching recolor (0,1,2 cycling) so any upward motion |
| 294 | // is a leftover-velocity bug, not a new match. |
| 295 | let n = 0; |
| 296 | g.randomColor = () => (n++ % 3); |
| 297 | |
| 298 | g.step(g.settleTime + 0.05, 1 / 120); |
| 299 | |
| 300 | for (const b of g.blocks) |
| 301 | { |
| 302 | assert.notEqual(b.state, 'flying', 'no new launch without a match, id=' + b.id); |
| 303 | } |
| 304 | for (const b of members) |
| 305 | { |
| 306 | assert.ok(b.vy <= 1e-9, 'vy must not stay upward after recolor, vy=' + b.vy + ' id=' + b.id + ' state=' + b.state); |
| 307 | } |
| 308 | assert.equal(g.score, 0, 'nothing should have been culled off the top'); |
| 309 | assert.equal(Object.keys(g.clusters).length, 0, 'landed cluster is released'); |
| 310 | |
| 311 | // Hanging left cargo (and the left grey) must start falling down, |
| 312 | // never climb above their pre-recolor height. |
| 313 | g.step(0.3, 1 / 120); |
| 314 | const lefts = g.blocks.filter((b) => b.col === 0); |
| 315 | assert.ok(lefts.length >= 2, 'left column still has blocks'); |
| 316 | for (const b of lefts) |
| 317 | { |
| 318 | assert.ok(b.y <= yBefore[b.id] + 0.02, 'left block must not rise, y=' + b.y + ' from ' + yBefore[b.id]); |
| 319 | assert.ok(b.vy <= 1e-9, 'falling from rest, vy=' + b.vy); |
| 320 | assert.ok(b.y > -0.05, 'must stay in the field, y=' + b.y); |
| 321 | } |
| 322 | const maxY = Math.max(...g.blocks.map((b) => b.y)); |
| 323 | assert.ok(maxY < 8, 'nothing raced toward the top, maxY=' + maxY); |
| 324 | assert.equal(g.score, 0); |
| 325 | }); |
| 326 | |
| 327 | it('a genuine match after recolor launches with normal jump physics, not leftover speed', () => |
| 328 | { |
| 329 | const planetOpts = { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 }; |
| 330 | const g = makeGame({ noSpawn: true, planetOpts, seed: 1 }); |
| 331 | // Full support under a 3-wide match so all three stay still after recolor. |
| 332 | g.place(0, 0, 4); |
| 333 | g.place(1, 0, 4); |
| 334 | g.place(2, 0, 4); |
| 335 | placeRow(g, 1, [0, 1, 2], 0); |
| 336 | const cargoA = g.place(1, 2, 3); |
| 337 | const cargoB = g.place(1, 3, 2); |
| 338 | g.resolveMatches(); |
| 339 | landUntilSettling(g, 3); |
| 340 | |
| 341 | for (const b of g.blocks) |
| 342 | { b.vy = 40; } |
| 343 | for (const id of Object.keys(g.clusters)) |
| 344 | { g.clusters[id].vy = 40; } |
| 345 | |
| 346 | // Recolor every grey to color 0: the three supported blocks match. |
| 347 | g.randomColor = () => 0; |
| 348 | const yCargo = cargoB.y; |
| 349 | |
| 350 | g.step(g.settleTime + 0.02, 1 / 120); |
| 351 | |
| 352 | const cluster = Object.values(g.clusters).find((c) => c.phase === 'up'); |
| 353 | assert.ok(cluster, 'a new match must launch\n' + g.snapshot().picture + '\nair=' + JSON.stringify(g.snapshot().airborne)); |
| 354 | // Recolor-on-landing is a landing-chain match (t1120): +25%. |
| 355 | const expectedH = Planet.jumpHeight(g.planet, 3, false) * (1 + GameCore.LANDING_CHAIN_BONUS); |
| 356 | const expectedVy = Planet.launchVelocity(g.planet, expectedH); |
| 357 | const expectedDecel = Planet.launchDeceleration(g.planet, expectedH); |
| 358 | assert.ok(almost(cluster.jumpHeight, expectedH, 1e-9), 'jumpHeight=' + cluster.jumpHeight + ' expected ' + expectedH); |
| 359 | assert.equal(cluster.landingChain, 1); |
| 360 | assert.ok(almost(cluster.vy, expectedVy, 0.35), 'vy must be the normal launch speed ' + expectedVy + ', got ' + cluster.vy + ' (leftover 40 would be wrong)'); |
| 361 | assert.ok(almost(cluster.decel, expectedDecel, 0.15), 'decel=' + cluster.decel); |
| 362 | assert.ok(cluster.vy < 40, 'must not keep the leftover vy'); |
| 363 | // Riders share the same (normal) velocity. |
| 364 | assert.equal(cargoA.state, 'flying'); |
| 365 | assert.equal(cargoB.state, 'flying'); |
| 366 | assert.ok(almost(cargoA.vy, cluster.vy, 1e-9)); |
| 367 | assert.ok(almost(cargoB.vy, cluster.vy, 1e-9)); |
| 368 | |
| 369 | // Apex of a normal jump from the pre-launch y, not a double or leftover boost. |
| 370 | const startY = yCargo; |
| 371 | let apex = Math.max(cargoA.y, cargoB.y); |
| 372 | for (let i = 0; i < 800; i++) |
| 373 | { |
| 374 | g.tick(1 / 240); |
| 375 | apex = Math.max(apex, cargoA.y, cargoB.y); |
| 376 | } |
| 377 | assert.ok(apex <= startY + expectedH + 0.35, 'apex must follow normal jump height, apex=' + apex + ' start=' + startY + ' H=' + expectedH); |
| 378 | // With t1119 (+50% jump) and t1120 (landing-chain +25%), a chain-1 |
| 379 | // horizontal-3 from y≈3 clears the 13-tile field, so the apex may |
| 380 | // be the top of the field rather than startY+H. |
| 381 | assert.ok(apex >= Math.min(g.height - 1, startY + expectedH - 0.85) - 0.05, |
| 382 | 'should actually rise, apex=' + apex + ' start=' + startY + ' H=' + expectedH); |
| 383 | }); |
| 384 | }); |
projects-timfalken-com meteors