BlueGit

projects-timfalken-com meteors

Files

meteors / test / manual-falling.test.js

manual-falling.test.js

manual-falling.test.js 7.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
7describe('Thrown blocks collide with falling blocks (t1108)', () =>
8{
9 it('mergeManualWithFalling averages velocity, decel and jumpHeight by block count', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const thrown = g.place(4, 4, 1);
13 g.startManualLaunch(thrown);
14 thrown.vy = 10;
15 thrown.manualDecel = 6;
16 thrown.manualJumpHeight = 9;
17 const a = g.createBlock(4, 5, 2, 'falling');
18 a.vy = -4;
19 const b = g.createBlock(4, 6, 3, 'falling');
20 b.vy = -2;
21 const cluster = g.mergeManualWithFalling(thrown, [a, b]);
22 assert.ok(cluster);
23 assert.equal(cluster.fromManual, true);
24 assert.ok(almost(cluster.vy, (10 + -4 + -2) / 3, 1e-12), 'vy=' + cluster.vy);
25 assert.ok(almost(cluster.decel, 6 / 3, 1e-12), 'decel=' + cluster.decel);
26 assert.ok(almost(cluster.jumpHeight, 9 / 3, 1e-12), 'jumpHeight=' + cluster.jumpHeight);
27 assert.equal(thrown.state, 'flying');
28 assert.equal(a.state, 'flying');
29 assert.equal(b.state, 'flying');
30 assert.equal(a.clusterId, cluster.id);
31 assert.equal(b.clusterId, cluster.id);
32 assert.ok(almost(thrown.vy, cluster.vy, 1e-12));
33 assert.ok(almost(a.vy, cluster.vy, 1e-12));
34 });
35
36 it('a thrown block merges with a falling block and they never pass through', () =>
37 {
38 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
39 const thrown = g.place(4, 1, 1);
40 g.startManualLaunch(thrown);
41 assert.equal(thrown.state, 'manual');
42 assert.ok(thrown.vy > 0);
43 const falling = g.createBlock(4, 8, 2, 'falling');
44 falling.vy = -g.planet.fallSpeed;
45 let merged = false;
46 for (let i = 0; i < 400; i++)
47 {
48 g.tick(1 / 120);
49 if (thrown.state === 'flying' && falling.state === 'flying')
50 {
51 assert.equal(thrown.clusterId, falling.clusterId);
52 const cluster = g.clusters[thrown.clusterId];
53 assert.ok(cluster, 'a flying cluster was created');
54 assert.equal(cluster.fromManual, true);
55 assert.ok(Math.abs(Math.abs(falling.y - thrown.y) - 1) < 0.05,
56 'members packed one tile apart, thrown=' + thrown.y + ' falling=' + falling.y);
57 merged = true;
58 break;
59 }
60 assert.notEqual(falling.state, 'still',
61 'falling must not land through the thrown block, y=' + falling.y + ' thrown=' + thrown.y);
62 }
63 assert.ok(merged, 'they must have merged, thrown=' + thrown.state + '@' + thrown.y
64 + ' falling=' + falling.state + '@' + falling.y);
65 });
66
67 it('a stack of two falling blocks merges with the thrown block as one cluster of three', () =>
68 {
69 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
70 const thrown = g.place(3, 0, 0);
71 g.startManualLaunch(thrown);
72 const a = g.createBlock(3, 7, 1, 'falling');
73 const b = g.createBlock(3, 8, 2, 'falling');
74 a.vy = -g.planet.fallSpeed;
75 b.vy = -g.planet.fallSpeed;
76 let merged = false;
77 for (let i = 0; i < 400; i++)
78 {
79 g.tick(1 / 120);
80 if (thrown.state === 'flying' && a.state === 'flying' && b.state === 'flying')
81 {
82 assert.equal(a.clusterId, thrown.clusterId);
83 assert.equal(b.clusterId, thrown.clusterId);
84 const cluster = g.clusters[thrown.clusterId];
85 assert.equal(cluster.fromManual, true);
86 assert.equal(g.clusterMembers(cluster.id).length, 3);
87 merged = true;
88 break;
89 }
90 }
91 assert.ok(merged, 'all three must merge, states='
92 + [thrown, a, b].map((x) => x.state + '@' + x.y).join(', '));
93 });
94
95 it('later falling cargo on a fromManual cluster also averages and does not pass through', () =>
96 {
97 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
98 const thrown = g.place(2, 2, 3);
99 g.startManualLaunch(thrown);
100 const first = g.createBlock(2, 6, 1, 'falling');
101 first.vy = -g.planet.fallSpeed;
102 for (let i = 0; i < 400 && thrown.state !== 'flying'; i++)
103 { g.tick(1 / 120); }
104 assert.equal(thrown.state, 'flying');
105 assert.equal(first.state, 'flying');
106 const cluster = g.clusters[thrown.clusterId];
107 assert.equal(cluster.fromManual, true);
108 const roof = Math.max(thrown.y, first.y);
109 const second = g.createBlock(2, roof + 2.5, 4, 'falling');
110 second.vy = -g.planet.fallSpeed;
111 let attached = false;
112 for (let i = 0; i < 400; i++)
113 {
114 const n0 = g.clusterMembers(cluster.id).length;
115 const v0 = cluster.vy;
116 const vFall = second.vy;
117 g.tick(1 / 120);
118 if (second.state === 'flying' && second.clusterId === cluster.id)
119 {
120 // Attach averages, then this same tick's later substeps / cluster
121 // integrate may ease vy a little. The new vy must be between the
122 // two inputs (or just after a small decel from that average).
123 const expected = (v0 * n0 + vFall) / (n0 + 1);
124 assert.ok(Math.abs(cluster.vy - expected) < 0.5
125 || (cluster.vy - expected) * Math.sign(cluster.decel || 1) <= 0,
126 'second cargo averages, vy=' + cluster.vy + ' expected ~' + expected
127 + ' from v0=' + v0 + ' n0=' + n0 + ' vFall=' + vFall);
128 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
129 'second cargo did not pass through, y=' + second.y);
130 assert.equal(g.clusterMembers(cluster.id).length, n0 + 1);
131 attached = true;
132 break;
133 }
134 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
135 'must not pass through before attach, second=' + second.y
136 + ' cluster min=' + Math.min(thrown.y, first.y));
137 }
138 assert.ok(attached, 'second falling block must attach, state=' + second.state + ' y=' + second.y);
139
140 // Direct attach average, isolated from integrate.
141 const g2 = makeGame({ noSpawn: true });
142 const cl = {
143 id: g2.nextClusterId++,
144 jumpHeight: 6,
145 vy: 8,
146 decel: 4,
147 phase: 'up',
148 fromManual: true
149 };
150 g2.clusters[cl.id] = cl;
151 const r = g2.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
152 r.clusterId = cl.id;
153 r.vy = 8;
154 const c1 = g2.createBlock(1, 5, 1, 'flying');
155 c1.clusterId = cl.id;
156 c1.vy = 8;
157 const extra = g2.createBlock(1, 6.05, 2, 'falling');
158 extra.vy = -4;
159 assert.ok(g2.attachToFlyingCluster(extra, c1));
160 assert.ok(almost(cl.vy, (8 * 2 + -4) / 3, 1e-12), 'isolated average, vy=' + cl.vy);
161 assert.ok(almost(extra.vy, cl.vy, 1e-12));
162 assert.ok(almost(r.vy, cl.vy, 1e-12));
163 assert.ok(almost(cl.decel, 4 * 2 / 3, 1e-12));
164 assert.ok(almost(cl.jumpHeight, 6 * 2 / 3, 1e-12));
165 });
166
167 it('a fast substep that would cross still merges; they never pass through', () =>
168 {
169 const g = makeGame({ noSpawn: true });
170 const thrown = g.place(5, 5, 1);
171 g.startManualLaunch(thrown);
172 thrown.vy = 80;
173 thrown.manualDecel = 0;
174 const falling = g.createBlock(5, 6.2, 2, 'falling');
175 falling.vy = -80;
176 g.tick(1 / 120);
177 assert.equal(thrown.state, 'flying', 'merged, thrown=' + thrown.state);
178 assert.equal(falling.state, 'flying');
179 assert.equal(thrown.clusterId, falling.clusterId);
180 assert.ok(Math.abs(falling.y - thrown.y) >= 1 - 0.05,
181 'packed without overlapping, thrown=' + thrown.y + ' falling=' + falling.y);
182 });
183
184 it('a normal match-rocket does not average falling cargo (existing attach rule)', () =>
185 {
186 const g = makeGame({ noSpawn: true });
187 const cluster = {
188 id: g.nextClusterId++,
189 jumpHeight: 4,
190 vy: 6,
191 decel: 3,
192 phase: 'hold'
193 };
194 g.clusters[cluster.id] = cluster;
195 const rocket = g.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
196 rocket.clusterId = cluster.id;
197 rocket.vy = 6;
198 rocket.flame = true;
199 const cargo = g.createBlock(1, 5.05, 3, 'falling');
200 cargo.vy = -g.planet.fallSpeed;
201 assert.ok(g.attachToFlyingCluster(cargo, rocket));
202 assert.equal(cargo.state, 'flying');
203 assert.equal(cargo.clusterId, cluster.id);
204 assert.ok(almost(cluster.vy, 6, 1e-12), 'rocket vy unchanged, vy=' + cluster.vy);
205 assert.ok(almost(cargo.vy, 6, 1e-12));
206 assert.equal(cluster.fromManual, undefined);
207 });
208});