BlueGit

projects-timfalken-com meteors

t1189/t1191: opening spawn burst (0.1 s for 3 s, weighted columns), lerp to 1 s by 6 s, speed-up after 6 s

timfa Grok Bot · pushed by timfa

Authored 2026-11-14 · 3 days ago

Hash
2bdc614f5ae23e7f9e424708e287eec7524c3cdb
Parent
fe5a790e
4 files changed +264 −9
src/gameCore.js +95 −9
@@ -79,6 +79,8 @@ function GameCore (planet, rng)
79 79 this.nextClusterId = 1;
80 80 this.spawnTimer = 0;
81 81 this.spawnInterval = PlanetMod.SPAWN_INTERVAL;
82 /** Seconds of actual play in this match / round (t1189). */
83 this.playTime = 0;
82 84 this.settleTime = PlanetMod.SETTLE_TIME;
83 85 this.score = 0;
84 86 this.state = 'playing';
@@ -587,12 +589,17 @@ GameCore.prototype.tick = function (dt)
587 589 { return; }
588 590 dt = Math.min(dt, 0.1);
589 591
592 // t1189: spawn timeline by elapsed play time (intro burst, ramp, then
593 // the per-spawn speed-up). Interval is re-read after every spawn.
590 594 this.spawnTimer += dt;
591 while (this.spawnTimer >= this.spawnInterval)
595 var iv = this.currentSpawnInterval();
596 while (this.spawnTimer >= iv)
592 597 {
593 this.spawnTimer -= this.spawnInterval;
598 this.spawnTimer -= iv;
594 599 this.trySpawn();
600 iv = this.currentSpawnInterval();
595 601 }
602 this.playTime += dt;
596 603
597 604 var left = dt;
598 605 while (left > 1e-12)
@@ -730,27 +737,106 @@ GameCore.prototype.trySpawn = function ()
730 737 { continue; }
731 738 var alreadyFalling = false;
732 739 var list = this.blocksInColumn(col);
740 // t1189/t1191: during the opening burst and ramp a column may take
741 // another block as soon as the previous falling one has cleared a
742 // full cell below the spawn row (no overlap); later the classic
743 // one-falling-block-per-column rule applies.
744 var opening = this.playTime < GameCore.SPAWN_RAMP_END;
733 745 for (var i = 0; i < list.length; i++)
734 746 {
735 if (list[i].state === STATE_FALLING)
736 {
737 alreadyFalling = true;
738 break;
739 }
747 if (list[i].state !== STATE_FALLING)
748 { continue; }
749 if (opening && list[i].y <= this.height - 1 + 1e-9)
750 { continue; }
751 alreadyFalling = true;
752 break;
740 753 }
741 754 if (!alreadyFalling)
742 755 { eligible.push(col); }
743 756 }
744 757 if (!eligible.length)
745 758 { return null; }
746 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
759 var col = this.playTime < GameCore.SPAWN_INTRO_END
760 ? this.pickWeightedSpawnColumn(eligible)
761 : eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
747 762 var color = this.randomColor();
748 763 var block = this.createBlock(col, this.height, color, STATE_FALLING);
749 764 block.vy = -this.planet.fallSpeed;
750 this.speedUpSpawning();
765 // t1187/t1188 speed-up only counts blocks spawned after the intro (t1189).
766 if (this.playTime >= GameCore.SPAWN_RAMP_END)
767 { this.speedUpSpawning(); }
751 768 return block;
752 769 };
753 770
771 /**
772 * Burst-phase column choice (t1191): weight each eligible column by its
773 * free cells (height − grounded − already falling blocks, min 1), so the fast opening burst fills
774 * the field evenly instead of piling onto one column. One rng() draw, like
775 * the uniform pick.
776 */
777 GameCore.prototype.fallingCount = function (col)
778 {
779 var n = 0;
780 for (var i = 0; i < this.blocks.length; i++)
781 {
782 if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING)
783 { n++; }
784 }
785 return n;
786 };
787
788 GameCore.prototype.pickWeightedSpawnColumn = function (eligible)
789 {
790 var weights = [];
791 var total = 0;
792 for (var i = 0; i < eligible.length; i++)
793 {
794 var w = Math.max(1, this.height - this.groundedCount(eligible[i]) - this.fallingCount(eligible[i]));
795 weights.push(w);
796 total += w;
797 }
798 var r = this.rng() * total;
799 for (var j = 0; j < eligible.length; j++)
800 {
801 r -= weights[j];
802 if (r < 0)
803 { return eligible[j]; }
804 }
805 return eligible[eligible.length - 1];
806 };
807
808 /** t1189/t1191: first seconds of a match spawn fast ... */
809 GameCore.SPAWN_INTRO_END = 3;
810 /** ... at this interval (seconds) ... */
811 GameCore.SPAWN_INTRO_INTERVAL = 0.1;
812 /** ... then lerp back to PlanetMod.SPAWN_INTERVAL (1 s) until this time. */
813 GameCore.SPAWN_RAMP_END = 6;
814
815 /**
816 * Spawn interval right now (t1189):
817 * playTime < 3 s → 0.1 s (burst, weighted column choice)
818 * 3 s – 6 s → linear 0.1 s → 1.0 s
819 * ≥ 6 s → spawnInterval (starts at 1 s, ×0.997 per spawn
820 * after 6 s, floor 0.1 s)
821 * An interval set above the 1 s start (tests / tools use 1e9 to switch
822 * spawning off) is always respected.
823 */
824 GameCore.prototype.currentSpawnInterval = function ()
825 {
826 var base = PlanetMod.SPAWN_INTERVAL;
827 if (this.spawnInterval > base + 1e-9)
828 { return this.spawnInterval; }
829 var t = this.playTime || 0;
830 if (t < GameCore.SPAWN_INTRO_END)
831 { return GameCore.SPAWN_INTRO_INTERVAL; }
832 if (t < GameCore.SPAWN_RAMP_END)
833 {
834 var k = (t - GameCore.SPAWN_INTRO_END) / (GameCore.SPAWN_RAMP_END - GameCore.SPAWN_INTRO_END);
835 return GameCore.SPAWN_INTRO_INTERVAL + (base - GameCore.SPAWN_INTRO_INTERVAL) * k;
836 }
837 return this.spawnInterval;
838 };
839
754 840 /** Each spawned block shrinks the spawn interval by 0.3% (t1187 / t1188). */
755 841 GameCore.SPAWN_SPEEDUP = 0.997;
756 842 /** The spawn interval never goes below this (seconds, t1187). */
@@ -79,6 +79,8 @@ function GameCore (planet, rng)
79 this.nextClusterId = 1;
79 this.nextClusterId = 1;
80 this.spawnTimer = 0;
80 this.spawnTimer = 0;
81 this.spawnInterval = PlanetMod.SPAWN_INTERVAL;
81 this.spawnInterval = PlanetMod.SPAWN_INTERVAL;
82 /** Seconds of actual play in this match / round (t1189). */
83 this.playTime = 0;
82 this.settleTime = PlanetMod.SETTLE_TIME;
84 this.settleTime = PlanetMod.SETTLE_TIME;
83 this.score = 0;
85 this.score = 0;
84 this.state = 'playing';
86 this.state = 'playing';
@@ -587,12 +589,17 @@ GameCore.prototype.tick = function (dt)
587 { return; }
589 { return; }
588 dt = Math.min(dt, 0.1);
590 dt = Math.min(dt, 0.1);
589
591
592 // t1189: spawn timeline by elapsed play time (intro burst, ramp, then
593 // the per-spawn speed-up). Interval is re-read after every spawn.
590 this.spawnTimer += dt;
594 this.spawnTimer += dt;
591 while (this.spawnTimer >= this.spawnInterval)
595 var iv = this.currentSpawnInterval();
596 while (this.spawnTimer >= iv)
592 {
597 {
593 this.spawnTimer -= this.spawnInterval;
598 this.spawnTimer -= iv;
594 this.trySpawn();
599 this.trySpawn();
600 iv = this.currentSpawnInterval();
595 }
601 }
602 this.playTime += dt;
596
603
597 var left = dt;
604 var left = dt;
598 while (left > 1e-12)
605 while (left > 1e-12)
@@ -730,27 +737,106 @@ GameCore.prototype.trySpawn = function ()
730 { continue; }
737 { continue; }
731 var alreadyFalling = false;
738 var alreadyFalling = false;
732 var list = this.blocksInColumn(col);
739 var list = this.blocksInColumn(col);
740 // t1189/t1191: during the opening burst and ramp a column may take
741 // another block as soon as the previous falling one has cleared a
742 // full cell below the spawn row (no overlap); later the classic
743 // one-falling-block-per-column rule applies.
744 var opening = this.playTime < GameCore.SPAWN_RAMP_END;
733 for (var i = 0; i < list.length; i++)
745 for (var i = 0; i < list.length; i++)
734 {
746 {
735 if (list[i].state === STATE_FALLING)
747 if (list[i].state !== STATE_FALLING)
736 {
748 { continue; }
737 alreadyFalling = true;
749 if (opening && list[i].y <= this.height - 1 + 1e-9)
738 break;
750 { continue; }
739 }
751 alreadyFalling = true;
752 break;
740 }
753 }
741 if (!alreadyFalling)
754 if (!alreadyFalling)
742 { eligible.push(col); }
755 { eligible.push(col); }
743 }
756 }
744 if (!eligible.length)
757 if (!eligible.length)
745 { return null; }
758 { return null; }
746 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
759 var col = this.playTime < GameCore.SPAWN_INTRO_END
760 ? this.pickWeightedSpawnColumn(eligible)
761 : eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
747 var color = this.randomColor();
762 var color = this.randomColor();
748 var block = this.createBlock(col, this.height, color, STATE_FALLING);
763 var block = this.createBlock(col, this.height, color, STATE_FALLING);
749 block.vy = -this.planet.fallSpeed;
764 block.vy = -this.planet.fallSpeed;
750 this.speedUpSpawning();
765 // t1187/t1188 speed-up only counts blocks spawned after the intro (t1189).
766 if (this.playTime >= GameCore.SPAWN_RAMP_END)
767 { this.speedUpSpawning(); }
751 return block;
768 return block;
752};
769};
753
770
771/**
772 * Burst-phase column choice (t1191): weight each eligible column by its
773 * free cells (height − grounded − already falling blocks, min 1), so the fast opening burst fills
774 * the field evenly instead of piling onto one column. One rng() draw, like
775 * the uniform pick.
776 */
777GameCore.prototype.fallingCount = function (col)
778{
779 var n = 0;
780 for (var i = 0; i < this.blocks.length; i++)
781 {
782 if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING)
783 { n++; }
784 }
785 return n;
786};
787
788GameCore.prototype.pickWeightedSpawnColumn = function (eligible)
789{
790 var weights = [];
791 var total = 0;
792 for (var i = 0; i < eligible.length; i++)
793 {
794 var w = Math.max(1, this.height - this.groundedCount(eligible[i]) - this.fallingCount(eligible[i]));
795 weights.push(w);
796 total += w;
797 }
798 var r = this.rng() * total;
799 for (var j = 0; j < eligible.length; j++)
800 {
801 r -= weights[j];
802 if (r < 0)
803 { return eligible[j]; }
804 }
805 return eligible[eligible.length - 1];
806};
807
808/** t1189/t1191: first seconds of a match spawn fast ... */
809GameCore.SPAWN_INTRO_END = 3;
810/** ... at this interval (seconds) ... */
811GameCore.SPAWN_INTRO_INTERVAL = 0.1;
812/** ... then lerp back to PlanetMod.SPAWN_INTERVAL (1 s) until this time. */
813GameCore.SPAWN_RAMP_END = 6;
814
815/**
816 * Spawn interval right now (t1189):
817 * playTime < 3 s → 0.1 s (burst, weighted column choice)
818 * 3 s – 6 s → linear 0.1 s → 1.0 s
819 * ≥ 6 s → spawnInterval (starts at 1 s, ×0.997 per spawn
820 * after 6 s, floor 0.1 s)
821 * An interval set above the 1 s start (tests / tools use 1e9 to switch
822 * spawning off) is always respected.
823 */
824GameCore.prototype.currentSpawnInterval = function ()
825{
826 var base = PlanetMod.SPAWN_INTERVAL;
827 if (this.spawnInterval > base + 1e-9)
828 { return this.spawnInterval; }
829 var t = this.playTime || 0;
830 if (t < GameCore.SPAWN_INTRO_END)
831 { return GameCore.SPAWN_INTRO_INTERVAL; }
832 if (t < GameCore.SPAWN_RAMP_END)
833 {
834 var k = (t - GameCore.SPAWN_INTRO_END) / (GameCore.SPAWN_RAMP_END - GameCore.SPAWN_INTRO_END);
835 return GameCore.SPAWN_INTRO_INTERVAL + (base - GameCore.SPAWN_INTRO_INTERVAL) * k;
836 }
837 return this.spawnInterval;
838};
839
754/** Each spawned block shrinks the spawn interval by 0.3% (t1187 / t1188). */
840/** Each spawned block shrinks the spawn interval by 0.3% (t1187 / t1188). */
755GameCore.SPAWN_SPEEDUP = 0.997;
841GameCore.SPAWN_SPEEDUP = 0.997;
756/** The spawn interval never goes below this (seconds, t1187). */
842/** The spawn interval never goes below this (seconds, t1187). */
test/points.test.js +1 −0
@@ -100,6 +100,7 @@ describe('Points, landing and recolor', () =>
100 100 it('spawn drops a random color into a random open column every second', () =>
101 101 {
102 102 const g = makeGame({ seed: 3 });
103 g.playTime = 6; // after the opening burst / ramp (t1189)
103 104 assert.equal(g.blocks.length, 0);
104 105 g.step(0.99);
105 106 assert.equal(g.blocks.length, 0);
@@ -100,6 +100,7 @@ describe('Points, landing and recolor', () =>
100 it('spawn drops a random color into a random open column every second', () =>
100 it('spawn drops a random color into a random open column every second', () =>
101 {
101 {
102 const g = makeGame({ seed: 3 });
102 const g = makeGame({ seed: 3 });
103 g.playTime = 6; // after the opening burst / ramp (t1189)
103 assert.equal(g.blocks.length, 0);
104 assert.equal(g.blocks.length, 0);
104 g.step(0.99);
105 g.step(0.99);
105 assert.equal(g.blocks.length, 0);
106 assert.equal(g.blocks.length, 0);
test/spawn-speedup.test.js +3 −0
@@ -21,6 +21,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
21 21 it('every spawned block multiplies the interval by 0.997', () =>
22 22 {
23 23 const g = makeGame({ seed: 2 });
24 g.playTime = 6;
24 25 const b1 = g.trySpawn();
25 26 assert.ok(b1);
26 27 assert.ok(almost(g.spawnInterval, 0.997, 1e-12));
@@ -31,6 +32,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
31 32 it('no spawn (no open column) → no speed-up', () =>
32 33 {
33 34 const g = makeGame({ seed: 3 });
35 g.playTime = 6;
34 36 for (let c = 0; c < g.width; c++)
35 37 { g.createBlock(c, 12, 0, 'falling'); }
36 38 assert.equal(g.trySpawn(), null);
@@ -59,6 +61,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
59 61 it('the game tick spawns faster over time (driven by the same logic)', () =>
60 62 {
61 63 const g = makeGame({ seed: 5 });
64 g.playTime = 6;
62 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);
63 66 for (let i = 0; i < 60 * 120; i++) g.tick(1 / 60);
64 67 const spawned = g.trySpawn.count();
@@ -21,6 +21,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
21 it('every spawned block multiplies the interval by 0.997', () =>
21 it('every spawned block multiplies the interval by 0.997', () =>
22 {
22 {
23 const g = makeGame({ seed: 2 });
23 const g = makeGame({ seed: 2 });
24 g.playTime = 6;
24 const b1 = g.trySpawn();
25 const b1 = g.trySpawn();
25 assert.ok(b1);
26 assert.ok(b1);
26 assert.ok(almost(g.spawnInterval, 0.997, 1e-12));
27 assert.ok(almost(g.spawnInterval, 0.997, 1e-12));
@@ -31,6 +32,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
31 it('no spawn (no open column) → no speed-up', () =>
32 it('no spawn (no open column) → no speed-up', () =>
32 {
33 {
33 const g = makeGame({ seed: 3 });
34 const g = makeGame({ seed: 3 });
35 g.playTime = 6;
34 for (let c = 0; c < g.width; c++)
36 for (let c = 0; c < g.width; c++)
35 { g.createBlock(c, 12, 0, 'falling'); }
37 { g.createBlock(c, 12, 0, 'falling'); }
36 assert.equal(g.trySpawn(), null);
38 assert.equal(g.trySpawn(), null);
@@ -59,6 +61,7 @@ describe('Spawn speed-up (t1187/t1188)', () =>
59 it('the game tick spawns faster over time (driven by the same logic)', () =>
61 it('the game tick spawns faster over time (driven by the same logic)', () =>
60 {
62 {
61 const g = makeGame({ seed: 5 });
63 const g = makeGame({ seed: 5 });
64 g.playTime = 6;
62 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);
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);
63 for (let i = 0; i < 60 * 120; i++) g.tick(1 / 60);
66 for (let i = 0; i < 60 * 120; i++) g.tick(1 / 60);
64 const spawned = g.trySpawn.count();
67 const spawned = g.trySpawn.count();
test/spawn-timeline.test.js added +165 −0
@@ -0,0 +1,165 @@
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
6 const { describe, it } = require('node:test');
7 const assert = require('node:assert/strict');
8 const { makeGame, almost, placeRow } = require('./helpers.js');
9 const { GameCore } = require('../src/gameCore.js');
10
11 function fresh (seed)
12 {
13 const g = makeGame({ seed: seed || 1 });
14 return g;
15 }
16
17 describe('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
@@ -0,0 +1,165 @@
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