BlueGit

projects-timfalken-com meteors

Files

meteors / test / spawn-speedup.test.js

spawn-speedup.test.js

spawn-speedup.test.js 2.7 KiB Download Raw
1'use strict';
2
3// t1187 / t1188: spawn interval shrinks 0.3% per spawned block, floor 0.1 s.
4
5const { describe, it } = require('node:test');
6const assert = require('node:assert/strict');
7const { makeGame, almost, Planet } = require('./helpers.js');
8const { GameCore } = require('../src/gameCore.js');
9
10describe('Spawn speed-up (t1187/t1188)', () =>
11{
12 it('constants: start 1 s, factor 0.997, floor 0.1 s', () =>
13 {
14 assert.equal(Planet.SPAWN_INTERVAL, 1);
15 assert.equal(GameCore.SPAWN_SPEEDUP, 0.997);
16 assert.equal(GameCore.SPAWN_INTERVAL_MIN, 0.1);
17 const g = makeGame({ seed: 1 });
18 assert.equal(g.spawnInterval, 1);
19 });
20
21 it('every spawned block multiplies the interval by 0.997', () =>
22 {
23 const g = makeGame({ seed: 2 });
24 g.playTime = 6;
25 const b1 = g.trySpawn();
26 assert.ok(b1);
27 assert.ok(almost(g.spawnInterval, 0.997, 1e-12));
28 g.trySpawn();
29 assert.ok(almost(g.spawnInterval, 0.997 * 0.997, 1e-12));
30 });
31
32 it('no spawn (no open column) → no speed-up', () =>
33 {
34 const g = makeGame({ seed: 3 });
35 g.playTime = 6;
36 for (let c = 0; c < g.width; c++)
37 { g.createBlock(c, 12, 0, 'falling'); }
38 assert.equal(g.trySpawn(), null);
39 assert.equal(g.spawnInterval, 1);
40 });
41
42 it('floors at 0.1 s after ~767 spawns', () =>
43 {
44 const g = makeGame({ seed: 4 });
45 let n = 0;
46 while (g.spawnInterval > GameCore.SPAWN_INTERVAL_MIN + 1e-12 && n < 5000)
47 {
48 g.speedUpSpawning();
49 n++;
50 }
51 assert.equal(g.spawnInterval, 0.1);
52 assert.ok(n >= 766 && n <= 768, 'spawns to floor: ' + n);
53 g.speedUpSpawning();
54 assert.equal(g.spawnInterval, 0.1, 'never below the floor');
55 // Total time to the floor ≈ sum of intervals ≈ 300 s.
56 let total = 0, iv = 1;
57 for (let i = 0; i < n; i++) { total += iv; iv = Math.max(0.1, iv * 0.997); }
58 assert.ok(total > 290 && total < 310, 'seconds to floor ' + total.toFixed(1));
59 });
60
61 it('the game tick spawns faster over time (driven by the same logic)', () =>
62 {
63 const g = makeGame({ seed: 5 });
64 g.playTime = 6;
65 g.trySpawn = (function (orig) { let count = 0; const f = function () { const b = orig.call(this); if (b) { count++; this.removeBlock(b); } return b; }; f.count = () => count; return f; })(g.trySpawn);
66 for (let i = 0; i < 60 * 120; i++) g.tick(1 / 60);
67 const spawned = g.trySpawn.count();
68 assert.ok(spawned >= 140 && spawned <= 155, '120 s gives ~149 spawns instead of 120: ' + spawned);
69 assert.ok(almost(g.spawnInterval, Math.max(0.1, Math.pow(0.997, spawned)), 1e-9));
70 });
71
72 it('a new game (new core) starts again at 1 s', () =>
73 {
74 const g = makeGame({ seed: 6 });
75 for (let i = 0; i < 100; i++) g.speedUpSpawning();
76 assert.ok(g.spawnInterval < 0.75);
77 assert.equal(makeGame({ seed: 6 }).spawnInterval, 1);
78 });
79});