BlueGit

projects-timfalken-com meteors

feat: outgoing shots, incoming grey garbage, and missChance parameter

Shot-off blocks are recorded for the multiplayer layer. Incoming garbage
falls as dark grey and recolors after the usual 3s settle. missChance
defaults to 0.30 and is a GameCore parameter for stage 3 Power.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
30c802482dec69f43f5f4159acf4b183d71aa17d
Parent
1717333b
2 files changed +189 −1
src/gameCore.js +92 −1
@@ -77,6 +77,17 @@ function GameCore (planet, rng)
77 77 this.state = 'playing';
78 78 this.drag = null;
79 79 this.gameOverTime = 0;
80 this.outgoingShots = [];
81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
82 this.missChance = optsMissChance(arguments);
83 }
84
85 function optsMissChance (args)
86 {
87 // GameCore(planet, rng, missChance) — third arg is optional.
88 if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2]))
89 { return Math.max(0, Math.min(1, args[2])); }
90 return GameCore.DEFAULT_MISS_CHANCE;
80 91 }
81 92
82 93 GameCore.COLOR_GRAY = COLOR_GRAY;
@@ -84,6 +95,7 @@ GameCore.LAUNCH_PULL = LAUNCH_PULL;
84 95 GameCore.SWAP_DISTANCE = SWAP_DISTANCE;
85 96 GameCore.SNAP_Y = SNAP_Y;
86 97 GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP;
98 GameCore.DEFAULT_MISS_CHANCE = 0.30;
87 99 GameCore.STATE_STILL = STATE_STILL;
88 100 GameCore.STATE_FALLING = STATE_FALLING;
89 101 GameCore.STATE_FLYING = STATE_FLYING;
@@ -1142,9 +1154,88 @@ GameCore.prototype.cullOffTop = function ()
1142 1154 }
1143 1155 for (var i = 0; i < gone.length; i++)
1144 1156 {
1157 var block = gone[i];
1158 this.outgoingShots.push({
1159 id: block.id,
1160 color: block.color,
1161 col: block.col,
1162 y: block.y
1163 });
1145 1164 this.score += 1;
1146 this.removeBlock(gone[i]);
1165 this.removeBlock(block);
1166 }
1167 };
1168
1169 GameCore.prototype.consumeOutgoingShots = function ()
1170 {
1171 var shots = this.outgoingShots;
1172 this.outgoingShots = [];
1173 return shots;
1174 };
1175
1176 /**
1177 * A compact field picture for the multiplayer snapshot (miniatures).
1178 * Includes every block's col, y, color, state and flame.
1179 */
1180 GameCore.prototype.fieldSnapshot = function ()
1181 {
1182 var blocks = [];
1183 for (var i = 0; i < this.blocks.length; i++)
1184 {
1185 var b = this.blocks[i];
1186 blocks.push({
1187 id: b.id,
1188 col: b.col,
1189 y: Math.round(b.y * 1000) / 1000,
1190 color: b.color,
1191 state: b.state,
1192 flame: !!b.flame
1193 });
1147 1194 }
1195 return {
1196 score: this.score,
1197 state: this.state,
1198 width: this.width,
1199 height: this.height,
1200 locked: this.lockedColumnCount(),
1201 blocks: blocks
1202 };
1203 };
1204
1205 /**
1206 * Incoming garbage from another player: a dark-grey block that falls
1207 * normally from the top of an open column. After landing it uses the
1208 * usual 3s settle and then a random colour (landFallingBlock).
1209 * `col` is optional; a random eligible column is used otherwise.
1210 * Returns the new block, or null if every column is locked or full.
1211 */
1212 GameCore.prototype.ingestGarbage = function (col)
1213 {
1214 if (this.state !== 'playing')
1215 { return null; }
1216 var chosen = null;
1217 if (col != null && col >= 0 && col < this.width
1218 && !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
1219 { chosen = col | 0; }
1220 else
1221 {
1222 var eligible = [];
1223 for (var c = 0; c < this.width; c++)
1224 {
1225 if (this.isColumnLocked(c))
1226 { continue; }
1227 if (this.groundedCount(c) >= this.height)
1228 { continue; }
1229 eligible.push(c);
1230 }
1231 if (!eligible.length)
1232 { return null; }
1233 chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
1234 }
1235 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
1236 block.vy = -this.planet.fallSpeed;
1237 block.flame = false;
1238 return block;
1148 1239 };
1149 1240
1150 1241 GameCore.prototype.advanceSettle = function (dt)
@@ -77,6 +77,17 @@ function GameCore (planet, rng)
77 this.state = 'playing';
77 this.state = 'playing';
78 this.drag = null;
78 this.drag = null;
79 this.gameOverTime = 0;
79 this.gameOverTime = 0;
80 this.outgoingShots = [];
81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
82 this.missChance = optsMissChance(arguments);
83}
84
85function optsMissChance (args)
86{
87 // GameCore(planet, rng, missChance) — third arg is optional.
88 if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2]))
89 { return Math.max(0, Math.min(1, args[2])); }
90 return GameCore.DEFAULT_MISS_CHANCE;
80}
91}
81
92
82GameCore.COLOR_GRAY = COLOR_GRAY;
93GameCore.COLOR_GRAY = COLOR_GRAY;
@@ -84,6 +95,7 @@ GameCore.LAUNCH_PULL = LAUNCH_PULL;
84GameCore.SWAP_DISTANCE = SWAP_DISTANCE;
95GameCore.SWAP_DISTANCE = SWAP_DISTANCE;
85GameCore.SNAP_Y = SNAP_Y;
96GameCore.SNAP_Y = SNAP_Y;
86GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP;
97GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP;
98GameCore.DEFAULT_MISS_CHANCE = 0.30;
87GameCore.STATE_STILL = STATE_STILL;
99GameCore.STATE_STILL = STATE_STILL;
88GameCore.STATE_FALLING = STATE_FALLING;
100GameCore.STATE_FALLING = STATE_FALLING;
89GameCore.STATE_FLYING = STATE_FLYING;
101GameCore.STATE_FLYING = STATE_FLYING;
@@ -1142,9 +1154,88 @@ GameCore.prototype.cullOffTop = function ()
1142 }
1154 }
1143 for (var i = 0; i < gone.length; i++)
1155 for (var i = 0; i < gone.length; i++)
1144 {
1156 {
1157 var block = gone[i];
1158 this.outgoingShots.push({
1159 id: block.id,
1160 color: block.color,
1161 col: block.col,
1162 y: block.y
1163 });
1145 this.score += 1;
1164 this.score += 1;
1146 this.removeBlock(gone[i]);
1165 this.removeBlock(block);
1166 }
1167};
1168
1169GameCore.prototype.consumeOutgoingShots = function ()
1170{
1171 var shots = this.outgoingShots;
1172 this.outgoingShots = [];
1173 return shots;
1174};
1175
1176/**
1177 * A compact field picture for the multiplayer snapshot (miniatures).
1178 * Includes every block's col, y, color, state and flame.
1179 */
1180GameCore.prototype.fieldSnapshot = function ()
1181{
1182 var blocks = [];
1183 for (var i = 0; i < this.blocks.length; i++)
1184 {
1185 var b = this.blocks[i];
1186 blocks.push({
1187 id: b.id,
1188 col: b.col,
1189 y: Math.round(b.y * 1000) / 1000,
1190 color: b.color,
1191 state: b.state,
1192 flame: !!b.flame
1193 });
1147 }
1194 }
1195 return {
1196 score: this.score,
1197 state: this.state,
1198 width: this.width,
1199 height: this.height,
1200 locked: this.lockedColumnCount(),
1201 blocks: blocks
1202 };
1203};
1204
1205/**
1206 * Incoming garbage from another player: a dark-grey block that falls
1207 * normally from the top of an open column. After landing it uses the
1208 * usual 3s settle and then a random colour (landFallingBlock).
1209 * `col` is optional; a random eligible column is used otherwise.
1210 * Returns the new block, or null if every column is locked or full.
1211 */
1212GameCore.prototype.ingestGarbage = function (col)
1213{
1214 if (this.state !== 'playing')
1215 { return null; }
1216 var chosen = null;
1217 if (col != null && col >= 0 && col < this.width
1218 && !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
1219 { chosen = col | 0; }
1220 else
1221 {
1222 var eligible = [];
1223 for (var c = 0; c < this.width; c++)
1224 {
1225 if (this.isColumnLocked(c))
1226 { continue; }
1227 if (this.groundedCount(c) >= this.height)
1228 { continue; }
1229 eligible.push(c);
1230 }
1231 if (!eligible.length)
1232 { return null; }
1233 chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
1234 }
1235 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
1236 block.vy = -this.planet.fallSpeed;
1237 block.flame = false;
1238 return block;
1148};
1239};
1149
1240
1150GameCore.prototype.advanceSettle = function (dt)
1241GameCore.prototype.advanceSettle = function (dt)
test/multiplayer-core.test.js added +97 −0
@@ -0,0 +1,97 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js');
6
7 describe('Outgoing shots and incoming garbage', () =>
8 {
9 it('blocks that leave the top are recorded as outgoing shots and can be consumed', () =>
10 {
11 const g = makeGame({
12 noSpawn: true,
13 planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }
14 });
15 placeRow(g, 0, [0, 1, 2], 0);
16 g.place(1, 1, 4);
17 g.resolveMatches();
18 assert.equal(g.consumeOutgoingShots().length, 0);
19 g.step(2, 1 / 120);
20 const shots = g.consumeOutgoingShots();
21 assert.equal(shots.length, 4, '3 rockets + 1 cargo, got ' + shots.length);
22 assert.ok(shots.every((s) => 'id' in s && 'color' in s && 'col' in s));
23 assert.equal(g.consumeOutgoingShots().length, 0, 'consume clears the list');
24 assert.equal(g.score, 4);
25 });
26
27 it('ingestGarbage drops a falling grey that settles 3s then recolors', () =>
28 {
29 const g = makeGame({ noSpawn: true, seed: 4 });
30 const block = g.ingestGarbage(3);
31 assert.ok(block);
32 assert.equal(block.col, 3);
33 assert.equal(block.color, GameCore.COLOR_GRAY);
34 assert.equal(block.state, 'falling');
35 assert.ok(almost(block.y, g.height, 0.05));
36 let landed = false;
37 for (let i = 0; i < 3000; i++)
38 {
39 g.tick(1 / 120);
40 if (block.state === 'settling')
41 {
42 landed = true;
43 break;
44 }
45 }
46 assert.ok(landed, 'should settle after landing, state=' + block.state + ' y=' + block.y + ' color=' + block.color);
47 assert.ok(almost(block.y, 0, 0.08), 'landed on the floor, y=' + block.y);
48 assert.equal(block.color, GameCore.COLOR_GRAY);
49 assert.ok(block.settleTime < 0.05, 'timer starts at landing');
50 g.step(2.9, 1 / 120);
51 assert.equal(block.state, 'settling', 'still settling at 2.9s, t=' + block.settleTime);
52 g.step(0.2, 1 / 120);
53 assert.equal(block.state, 'still');
54 assert.ok(block.color >= 0 && block.color < 5, 'recolored, color=' + block.color);
55 });
56
57 it('ingestGarbage skips locked and full columns, and returns null when none are open', () =>
58 {
59 const g = makeGame({ noSpawn: true });
60 for (let c = 0; c < 10; c++)
61 {
62 for (let r = 0; r < 13; r++)
63 { g.place(c, r, r % 5); }
64 }
65 g.checkLocks();
66 assert.equal(g.lockedColumnCount(), 10);
67 assert.equal(g.ingestGarbage(), null);
68 });
69
70 it('missChance is a parameter defaulting to 0.30, independent of planet Power', () =>
71 {
72 const g = makeGame({ noSpawn: true });
73 assert.equal(g.missChance, 0.30);
74 assert.equal(GameCore.DEFAULT_MISS_CHANCE, 0.30);
75 const custom = new GameCore(Planet.defaultPlanet(), Math.random, 0.05);
76 assert.equal(custom.missChance, 0.05);
77 const clamped = new GameCore(Planet.defaultPlanet(), Math.random, 2);
78 assert.equal(clamped.missChance, 1);
79 // Planet.missChance stays the stage-3 Power formula, unused here.
80 const terraMiss = Planet.missChance(Planet.defaultPlanet());
81 assert.ok(Math.abs(terraMiss - 0.30) > 0.01, 'Terra Power miss is not the stage-2 30%');
82 });
83
84 it('fieldSnapshot lists every block for opponent miniatures', () =>
85 {
86 const g = makeGame({ noSpawn: true });
87 g.place(0, 0, 1);
88 g.place(1, 2, 3);
89 const snap = g.fieldSnapshot();
90 assert.equal(snap.score, 0);
91 assert.equal(snap.state, 'playing');
92 assert.equal(snap.blocks.length, 2);
93 assert.equal(snap.blocks[0].col, 0);
94 assert.equal(snap.blocks[1].y, 2);
95 assert.equal(snap.locked, 0);
96 });
97 });
@@ -0,0 +1,97 @@
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
7describe('Outgoing shots and incoming garbage', () =>
8{
9 it('blocks that leave the top are recorded as outgoing shots and can be consumed', () =>
10 {
11 const g = makeGame({
12 noSpawn: true,
13 planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }
14 });
15 placeRow(g, 0, [0, 1, 2], 0);
16 g.place(1, 1, 4);
17 g.resolveMatches();
18 assert.equal(g.consumeOutgoingShots().length, 0);
19 g.step(2, 1 / 120);
20 const shots = g.consumeOutgoingShots();
21 assert.equal(shots.length, 4, '3 rockets + 1 cargo, got ' + shots.length);
22 assert.ok(shots.every((s) => 'id' in s && 'color' in s && 'col' in s));
23 assert.equal(g.consumeOutgoingShots().length, 0, 'consume clears the list');
24 assert.equal(g.score, 4);
25 });
26
27 it('ingestGarbage drops a falling grey that settles 3s then recolors', () =>
28 {
29 const g = makeGame({ noSpawn: true, seed: 4 });
30 const block = g.ingestGarbage(3);
31 assert.ok(block);
32 assert.equal(block.col, 3);
33 assert.equal(block.color, GameCore.COLOR_GRAY);
34 assert.equal(block.state, 'falling');
35 assert.ok(almost(block.y, g.height, 0.05));
36 let landed = false;
37 for (let i = 0; i < 3000; i++)
38 {
39 g.tick(1 / 120);
40 if (block.state === 'settling')
41 {
42 landed = true;
43 break;
44 }
45 }
46 assert.ok(landed, 'should settle after landing, state=' + block.state + ' y=' + block.y + ' color=' + block.color);
47 assert.ok(almost(block.y, 0, 0.08), 'landed on the floor, y=' + block.y);
48 assert.equal(block.color, GameCore.COLOR_GRAY);
49 assert.ok(block.settleTime < 0.05, 'timer starts at landing');
50 g.step(2.9, 1 / 120);
51 assert.equal(block.state, 'settling', 'still settling at 2.9s, t=' + block.settleTime);
52 g.step(0.2, 1 / 120);
53 assert.equal(block.state, 'still');
54 assert.ok(block.color >= 0 && block.color < 5, 'recolored, color=' + block.color);
55 });
56
57 it('ingestGarbage skips locked and full columns, and returns null when none are open', () =>
58 {
59 const g = makeGame({ noSpawn: true });
60 for (let c = 0; c < 10; c++)
61 {
62 for (let r = 0; r < 13; r++)
63 { g.place(c, r, r % 5); }
64 }
65 g.checkLocks();
66 assert.equal(g.lockedColumnCount(), 10);
67 assert.equal(g.ingestGarbage(), null);
68 });
69
70 it('missChance is a parameter defaulting to 0.30, independent of planet Power', () =>
71 {
72 const g = makeGame({ noSpawn: true });
73 assert.equal(g.missChance, 0.30);
74 assert.equal(GameCore.DEFAULT_MISS_CHANCE, 0.30);
75 const custom = new GameCore(Planet.defaultPlanet(), Math.random, 0.05);
76 assert.equal(custom.missChance, 0.05);
77 const clamped = new GameCore(Planet.defaultPlanet(), Math.random, 2);
78 assert.equal(clamped.missChance, 1);
79 // Planet.missChance stays the stage-3 Power formula, unused here.
80 const terraMiss = Planet.missChance(Planet.defaultPlanet());
81 assert.ok(Math.abs(terraMiss - 0.30) > 0.01, 'Terra Power miss is not the stage-2 30%');
82 });
83
84 it('fieldSnapshot lists every block for opponent miniatures', () =>
85 {
86 const g = makeGame({ noSpawn: true });
87 g.place(0, 0, 1);
88 g.place(1, 2, 3);
89 const snap = g.fieldSnapshot();
90 assert.equal(snap.score, 0);
91 assert.equal(snap.state, 'playing');
92 assert.equal(snap.blocks.length, 2);
93 assert.equal(snap.blocks[0].col, 0);
94 assert.equal(snap.blocks[1].y, 2);
95 assert.equal(snap.locked, 0);
96 });
97});