BlueGit

projects-timfalken-com meteors

Files

meteors / test / spontaneous-match.test.js

spontaneous-match.test.js

spontaneous-match.test.js 5.8 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 flyingCluster (game, colsAndY, opts)
8{
9 opts = opts || {};
10 const cluster = {
11 id: game.nextClusterId++,
12 jumpHeight: opts.jumpHeight != null ? opts.jumpHeight : 3,
13 vy: opts.vy != null ? opts.vy : 4,
14 decel: opts.decel != null ? opts.decel : 2,
15 phase: opts.phase || (opts.vy > 0 ? 'up' : 'down')
16 };
17 game.clusters[cluster.id] = cluster;
18 const members = [];
19 for (const [col, y, color] of colsAndY)
20 {
21 const b = game.createBlock(col, y, color == null ? GameCore.COLOR_GRAY : color, 'flying');
22 b.clusterId = cluster.id;
23 b.vy = cluster.vy;
24 b.flame = color == null || color === GameCore.COLOR_GRAY;
25 members.push(b);
26 }
27 return { cluster, members };
28}
29
30describe('Spontaneous matches always become rockets (t1104)', () =>
31{
32 it('a row that forms on the ground after a block falls launches like a player match', () =>
33 {
34 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
35 g.place(0, 0, 1);
36 g.place(1, 0, 1);
37 const falling = g.createBlock(2, 6, 1, 'falling');
38 falling.vy = -g.planet.fallSpeed;
39 let launched = false;
40 for (let i = 0; i < 800; i++)
41 {
42 g.tick(1 / 120);
43 const cluster = Object.values(g.clusters).find((c) => c.phase === 'up');
44 if (cluster && falling.color === GameCore.COLOR_GRAY)
45 {
46 launched = true;
47 break;
48 }
49 }
50 assert.ok(launched, 'falling block completing a ground row must become a rocket\n' + g.snapshot().picture);
51 assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 3);
52 const cluster = Object.values(g.clusters).find((c) => c.phase === 'up');
53 assert.ok(cluster.vy > 0);
54 const expectedH = Planet.jumpHeight(g.planet, 3, false);
55 assert.ok(almost(cluster.jumpHeight, expectedH, 1e-9), 'jumpHeight=' + cluster.jumpHeight);
56 });
57
58 it('a row that appears after grey settle and recolor launches', () =>
59 {
60 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 }, seed: 1 });
61 // Non-matching supports so only the recolored greys form a line.
62 g.place(0, 0, 4);
63 g.place(1, 0, 3);
64 g.place(2, 0, 2);
65 const greys = placeRow(g, 1, [0, 1, 2], GameCore.COLOR_GRAY);
66 for (const b of greys)
67 {
68 b.state = 'settling';
69 b.settleTime = 0;
70 b.flame = false;
71 }
72 g.randomColor = () => 0;
73 g.step(g.settleTime + 0.05, 1 / 120);
74 const cluster = Object.values(g.clusters).find((c) => c.phase === 'up');
75 assert.ok(cluster, 'recolored grounded row must launch\n' + g.snapshot().picture
76 + '\nblocks=' + JSON.stringify(g.blocks.map((b) => ({ col: b.col, y: b.y, color: b.color, state: b.state }))));
77 assert.ok(cluster.vy > 0);
78 assert.ok(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY && b.flame).length >= 3);
79 });
80
81 it('a match after cargo attaches to a flying cluster greys the new line and re-launches the whole cluster', () =>
82 {
83 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
84 // Two color-1 blocks at y=6 with a platform in the gap column, plus a
85 // distant grey so the original cluster is already flying.
86 const { cluster, members } = flyingCluster(g, [
87 [0, 6, 1],
88 [1, 5, 2],
89 [2, 6, 1],
90 [4, 6, GameCore.COLOR_GRAY]
91 ], { vy: 2, decel: 0, jumpHeight: 4, phase: 'up' });
92 const originalHeight = cluster.jumpHeight;
93 const originalVy = cluster.vy;
94 const falling = g.createBlock(1, 10, 1, 'falling');
95 falling.vy = -g.planet.fallSpeed;
96 let relaunched = false;
97 for (let i = 0; i < 800; i++)
98 {
99 g.tick(1 / 120);
100 if (falling.clusterId === cluster.id
101 && falling.color === GameCore.COLOR_GRAY
102 && cluster.phase === 'up'
103 && cluster.vy > originalVy + 0.5)
104 {
105 relaunched = true;
106 break;
107 }
108 }
109 assert.ok(relaunched, 'cargo completing a flying row must re-launch, falling.color='
110 + falling.color + ' state=' + falling.state + ' clusterId=' + falling.clusterId
111 + ' phase=' + cluster.phase + ' vy=' + cluster.vy + '\n' + g.snapshot().picture);
112 assert.equal(members[0].color, GameCore.COLOR_GRAY);
113 assert.equal(members[2].color, GameCore.COLOR_GRAY);
114 assert.equal(falling.color, GameCore.COLOR_GRAY);
115 assert.equal(falling.clusterId, cluster.id);
116 assert.equal(members[3].clusterId, cluster.id, 'existing grey stays linked');
117 assert.equal(cluster.jumpHeight, originalHeight, 'same height as the player-push re-launch');
118 assert.ok(cluster.vy > 0);
119 });
120
121 it('a match that appears when two flying clusters merge greys the line and re-launches', () =>
122 {
123 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
124 const a = flyingCluster(g, [
125 [0, 6, 1],
126 [1, 6, 1],
127 [3, 6, GameCore.COLOR_GRAY]
128 ], { vy: 5, decel: 2, jumpHeight: 4, phase: 'up' });
129 const b = flyingCluster(g, [
130 [3, 6.4, GameCore.COLOR_GRAY],
131 [2, 6.1, 1]
132 ], { vy: 1, decel: 2, jumpHeight: 3, phase: 'up' });
133 assert.ok(a.cluster.id !== b.cluster.id);
134 const keepId = Math.min(a.cluster.id, b.cluster.id);
135
136 g.mergeFlyingClusters();
137
138 const merged = g.clusters[keepId] || Object.values(g.clusters)[0];
139 assert.ok(merged, 'clusters should have merged');
140 const color1 = g.blocks.filter((bl) => bl.col <= 2);
141 assert.ok(color1.every((bl) => bl.color === GameCore.COLOR_GRAY),
142 'the completed row must have turned grey: ' + color1.map((bl) => bl.color).join(','));
143 assert.ok(color1.every((bl) => bl.clusterId === merged.id));
144 assert.equal(merged.phase, 'up');
145 assert.ok(merged.vy > 1, 're-launched from the current position, vy=' + merged.vy);
146 const expectedVy = Planet.launchVelocity(g.planet, merged.jumpHeight);
147 assert.ok(almost(merged.vy, expectedVy, 0.15), 'vy=' + merged.vy + ' expected ' + expectedVy);
148 });
149});