BlueGit

projects-timfalken-com meteors

Files

meteors / test / falling-cluster.test.js

falling-cluster.test.js

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