BlueGit

projects-timfalken-com meteors

Files

meteors / test / spawn-timeline.test.js

spawn-timeline.test.js

spawn-timeline.test.js 5.5 KiB Download Raw
1'use strict';
2
3// t1189 / t1191: opening spawn burst (0.1 s for 3 s), lerp to 1 s by 6 s,
4// then the 0.3% per-spawn speed-up (only spawns after 6 s count).
5
6const { describe, it } = require('node:test');
7const assert = require('node:assert/strict');
8const { makeGame, almost, placeRow } = require('./helpers.js');
9const { GameCore } = require('../src/gameCore.js');
10
11function fresh (seed)
12{
13 const g = makeGame({ seed: seed || 1 });
14 return g;
15}
16
17describe('Spawn timeline (t1189/t1191)', () =>
18{
19 it('constants', () =>
20 {
21 assert.equal(GameCore.SPAWN_INTRO_END, 3);
22 assert.equal(GameCore.SPAWN_INTRO_INTERVAL, 0.1);
23 assert.equal(GameCore.SPAWN_RAMP_END, 6);
24 });
25
26 it('interval: 0.1 s until 3 s, linear to 1 s at 6 s, then the per-spawn rate', () =>
27 {
28 const g = fresh();
29 const at = (t) => { g.playTime = t; return g.currentSpawnInterval(); };
30 assert.equal(at(0), 0.1);
31 assert.equal(at(2.99), 0.1);
32 assert.ok(almost(at(3), 0.1));
33 assert.ok(almost(at(4.5), 0.55));
34 assert.ok(almost(at(5.999), 0.1 + 0.9 * (2.999 / 3), 1e-9));
35 assert.equal(at(6), 1);
36 g.spawnInterval = 0.8;
37 assert.equal(at(7), 0.8);
38 });
39
40 it('no speed-up from blocks spawned before 6 s; after 6 s each spawn ×0.997', () =>
41 {
42 const g = fresh(2);
43 g.playTime = 1;
44 assert.ok(g.trySpawn());
45 g.playTime = 5;
46 assert.ok(g.trySpawn());
47 assert.equal(g.spawnInterval, 1);
48 g.playTime = 6.2;
49 assert.ok(g.trySpawn());
50 assert.ok(almost(g.spawnInterval, 0.997, 1e-12));
51 });
52
53 it('the real tick follows the timeline; play time starts at the first tick', () =>
54 {
55 const g = fresh(3);
56 assert.equal(g.playTime, 0);
57 const ticks = [];
58 const orig = g.trySpawn;
59 g.trySpawn = function ()
60 {
61 ticks.push(+this.playTime.toFixed(4));
62 const b = orig.call(this);
63 if (b) { this.removeBlock(b); } // keep every column open: count spawn ticks
64 return b;
65 };
66 for (let i = 0; i < 60 * 10; i++) g.tick(1 / 60);
67 const inBurst = ticks.filter((t) => t < 3).length;
68 const inRamp = ticks.filter((t) => t >= 3 && t < 6).length;
69 const after = ticks.filter((t) => t >= 6 && t < 10).length;
70 assert.ok(inBurst >= 29 && inBurst <= 31, 'burst ticks ' + inBurst);
71 // ∫ dt / (0.1 + 0.3 (t-3)) over 3..6 = ln(10)/0.3 ≈ 7.7
72 assert.ok(inRamp >= 7 && inRamp <= 9, 'ramp ticks ' + inRamp);
73 assert.ok(after >= 4 && after <= 5, 'after ticks ' + after);
74 assert.ok(ticks.length >= 36 && ticks.length <= 40, 'spawn ticks in first 6 s+ ' + ticks.length);
75 assert.ok(almost(g.playTime, 10, 1e-6));
76 });
77
78 it('first 6 s of a normal game: how many blocks really spawn', () =>
79 {
80 const g = fresh(4);
81 let spawned = 0;
82 const orig = g.trySpawn;
83 g.trySpawn = function () { const b = orig.call(this); if (b && this.playTime < 6) spawned++; return b; };
84 for (let i = 0; i < 60 * 6; i++) g.tick(1 / 60);
85 // One falling block per column at a time limits the burst.
86 assert.ok(spawned >= 12 && spawned <= 38, 'spawned ' + spawned);
87 assert.ok(g.state === 'playing');
88 });
89
90 it('burst column choice is weighted towards emptier columns', () =>
91 {
92 const g = fresh(5);
93 g.spawnInterval = 1; // normal
94 for (let r = 0; r < 10; r++) g.place(0, r, r % 5); // column 0 almost full
95 const counts = new Array(10).fill(0);
96 for (let i = 0; i < 4000; i++)
97 {
98 counts[g.pickWeightedSpawnColumn([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]++;
99 }
100 // weight col 0 = 3, others 13 → ~2.4% vs ~10.8%
101 assert.ok(counts[0] < 160, 'full-ish column picked ' + counts[0]);
102 for (let c = 1; c < 10; c++) assert.ok(counts[c] > 330, 'col ' + c + ' ' + counts[c]);
103 });
104
105 it('the departing-rocket rule still applies during the burst', () =>
106 {
107 const g = fresh(6);
108 g.spawnInterval = 1e9; // drive manually
109 const row = placeRow(g, 0, [3, 4, 5], 1);
110 const cluster = g.launchCluster(row, 30);
111 for (let i = 0; i < 400 && Math.max(...row.map((b) => b.y)) < 10; i++) g.step(1 / 120, 1 / 120);
112 g.playTime = 1;
113 for (let i = 0; i < 200; i++)
114 {
115 const b = g.trySpawn();
116 assert.ok(b && ![3, 4, 5].includes(b.col), 'col ' + (b && b.col));
117 g.removeBlock(b);
118 }
119 assert.ok(cluster);
120 });
121
122 it('spawning switched off by tools/tests (interval above 1 s) stays off in the burst', () =>
123 {
124 const g = makeGame({ noSpawn: true });
125 for (let i = 0; i < 60 * 4; i++) g.tick(1 / 60);
126 assert.equal(g.blocks.length, 0);
127 });
128
129 it('opening: a column takes the next block once the previous one cleared a cell; later one at a time', () =>
130 {
131 const g = fresh(7);
132 g.playTime = 1;
133 for (let c = 1; c < 10; c++) g.place(c, 0, 0, 'locked');
134 const a = g.trySpawn();
135 assert.equal(a.col, 0);
136 assert.equal(g.trySpawn(), null, 'previous block still in the spawn cell');
137 a.y = 12;
138 const b = g.trySpawn();
139 assert.ok(b && b.col === 0 && b.y - a.y >= 1 - 1e-9, 'second block above, no overlap');
140 g.playTime = 6;
141 a.y = 5; b.y = 9;
142 assert.equal(g.trySpawn(), null, 'after 6 s: one falling block per column');
143 });
144
145 it('a full game through the burst keeps the physics invariants (strict mode)', () =>
146 {
147 for (const seed of [1, 2, 3, 4, 5])
148 {
149 const g = fresh(seed);
150 let spawned = 0;
151 const orig = g.trySpawn;
152 g.trySpawn = function () { const b = orig.call(this); if (b && this.playTime < 6) spawned++; return b; };
153 for (let i = 0; i < 60 * 20 && g.state === 'playing'; i++) g.tick(1 / 60);
154 assert.ok(spawned >= 25, 'seed ' + seed + ' spawned in first 6 s: ' + spawned);
155 // No two blocks overlap in a column.
156 for (let c = 0; c < g.width; c++)
157 {
158 const col = g.blocksInColumn(c);
159 for (let k = 1; k < col.length; k++)
160 { assert.ok(col[k].y - col[k - 1].y >= 1 - 1e-6, 'overlap col ' + c + ' ' + col[k - 1].y + '/' + col[k].y); }
161 }
162 }
163 });
164});
165