BlueGit

projects-timfalken-com meteors

Files

meteors / test / cluster-merge.test.js

cluster-merge.test.js

cluster-merge.test.js 4.7 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, almost, GameCore } = 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('Flying cluster merge (t1102)', () =>
31{
32 it('merges two overlapping flying clusters and averages vy by block count', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 // 3-block cluster going up, 1-block cluster going down, same column, overlapping.
36 const a = flyingCluster(g, [[3, 5], [3, 6], [3, 7]], { vy: 6, decel: 4, jumpHeight: 4, phase: 'up' });
37 const b = flyingCluster(g, [[3, 6.5]], { vy: -2, decel: 0, jumpHeight: 2, phase: 'down' });
38 assert.ok(a.cluster.id !== b.cluster.id);
39
40 g.mergeFlyingClusters();
41
42 const ids = Object.keys(g.clusters);
43 assert.equal(ids.length, 1, 'one cluster remains');
44 const merged = g.clusters[ids[0]];
45 assert.equal(g.clusterMembers(merged.id).length, 4);
46 // Mass-weighted: (3*6 + 1*(-2)) / 4 = 16/4 = 4
47 assert.ok(almost(merged.vy, 4, 1e-12), 'vy=' + merged.vy);
48 assert.equal(merged.phase, 'up', 'positive vy keeps phase up');
49 // decel (3*4 + 1*0)/4 = 3, jumpHeight (3*4 + 1*2)/4 = 3.5
50 assert.ok(almost(merged.decel, 3, 1e-12), 'decel=' + merged.decel);
51 assert.ok(almost(merged.jumpHeight, 3.5, 1e-12), 'jumpHeight=' + merged.jumpHeight);
52 for (const block of g.blocks)
53 {
54 assert.equal(block.clusterId, merged.id);
55 assert.ok(almost(block.vy, 4, 1e-12));
56 assert.equal(block.state, 'flying');
57 }
58 // Overlap in col 3 is packed: four blocks stacked flush from the lowest (y=5).
59 const ys = g.blocks.filter((bl) => bl.col === 3).map((bl) => bl.y).sort((x, y) => x - y);
60 assert.ok(almost(ys[0], 5, 1e-9));
61 for (let i = 1; i < ys.length; i++)
62 { assert.ok(almost(ys[i], ys[i - 1] + 1, 1e-9), 'packed ys=' + ys); }
63 });
64
65 it('merged cluster then moves as one with the averaged velocity', () =>
66 {
67 const g = makeGame({ noSpawn: true });
68 flyingCluster(g, [[1, 4], [1, 5]], { vy: 8, decel: 0, phase: 'up' });
69 flyingCluster(g, [[1, 4.5], [2, 4.5]], { vy: 2, decel: 0, phase: 'up' });
70 g.mergeFlyingClusters();
71 const merged = Object.values(g.clusters)[0];
72 // (2*8 + 2*2)/4 = 5
73 assert.ok(almost(merged.vy, 5, 1e-12), 'vy=' + merged.vy);
74 const before = g.blocks.map((b) => ({ id: b.id, y: b.y, col: b.col }));
75 g.integrateClusters(0.1);
76 // decel 0, phase up: they rise by vy*dt = 0.5
77 for (const prev of before)
78 {
79 const block = g.blockById(prev.id);
80 assert.ok(almost(block.y - prev.y, 5 * 0.1, 1e-9), 'id=' + prev.id + ' dy=' + (block.y - prev.y));
81 assert.ok(almost(block.vy, 5, 1e-12));
82 }
83 });
84
85 it('merged vy <= 0 sets phase to down', () =>
86 {
87 const g = makeGame({ noSpawn: true });
88 flyingCluster(g, [[0, 8]], { vy: 1, phase: 'up' });
89 flyingCluster(g, [[0, 8]], { vy: -5, phase: 'down' });
90 g.mergeFlyingClusters();
91 const merged = Object.values(g.clusters)[0];
92 assert.ok(almost(merged.vy, (1 + -5) / 2, 1e-12));
93 assert.equal(merged.phase, 'down');
94 });
95
96 it('does not merge clusters that do not overlap', () =>
97 {
98 const g = makeGame({ noSpawn: true });
99 flyingCluster(g, [[0, 3]], { vy: 4, phase: 'up' });
100 flyingCluster(g, [[5, 8]], { vy: -3, phase: 'down' });
101 g.mergeFlyingClusters();
102 assert.equal(Object.keys(g.clusters).length, 2);
103 });
104
105 it('two approaching clusters merge mid-flight during a tick', () =>
106 {
107 const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3, riseTime: 1.5, jumpsToClear: 2 } });
108 // Lower cluster going up quickly, upper one going down, same column, a gap
109 // that closes within a short step.
110 flyingCluster(g, [[4, 3]], { vy: 8, decel: 0, phase: 'up' });
111 flyingCluster(g, [[4, 6]], { vy: -8, decel: 0, phase: 'down' });
112 assert.equal(Object.keys(g.clusters).length, 2);
113 g.step(0.3, 1 / 120);
114 assert.equal(Object.keys(g.clusters).length, 1, 'they must have merged once their y-ranges overlapped');
115 const merged = Object.values(g.clusters)[0];
116 const members = g.clusterMembers(merged.id);
117 assert.equal(members.length, 2);
118 assert.equal(members[0].clusterId, members[1].clusterId);
119 assert.ok(almost(members[0].vy, members[1].vy, 1e-12));
120 });
121});