BlueGit

projects-timfalken-com meteors

Merge branch 'feature/planet-physics'

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
64c10202b1e236a1d30a519d46e8217d9a6ee1f4
4 files changed +409 −0
src/planet.js added +165 −0
@@ -0,0 +1,165 @@
1 'use strict';
2
3 /**
4 * Planet physics and visuals for Meteors.
5 *
6 * Stage 1 uses a single default planet. Stage 3 lets the player pick a
7 * planet; the numbers and jump-height rules are already applied here so
8 * that later planets only need new data, not new physics code.
9 *
10 * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump).
11 * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles —
12 * at 1, one jump from the floor clears the field; at 4, you need four.
13 * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall).
14 * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s.
15 * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump).
16 * Time to reach the apex, independent of jump height.
17 *
18 * Jump-height multipliers (never explained in the game):
19 * A vertical match of 3 is 120 % of a horizontal row of 3.
20 * Each extra block beyond 3 adds 5 %.
21 *
22 * Power is the average of the three difficulty axes (0 easy .. 1 hard)
23 * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at
24 * power 0 to 5 % at power 1.
25 */
26
27 var FIELD_WIDTH = 10;
28 var FIELD_HEIGHT = 13;
29
30 function clamp (value, lo, hi)
31 {
32 return Math.max(lo, Math.min(hi, value));
33 }
34
35 function defaultColors ()
36 {
37 return [
38 { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' },
39 { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' },
40 { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' },
41 { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' },
42 { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' }
43 ];
44 }
45
46 function createPlanet (opts)
47 {
48 opts = opts || {};
49 var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4);
50 var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5);
51 var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3);
52 var colors = opts.colors || defaultColors();
53 var planet = {
54 name: opts.name || 'Terra',
55 people: opts.people || 'Terrans',
56 jumpsToClear: jumpsToClear,
57 fallTime: fallTime,
58 riseTime: riseTime,
59 colors: colors,
60 baseJumpHeight: FIELD_HEIGHT / jumpsToClear,
61 fallSpeed: FIELD_HEIGHT / fallTime
62 };
63 return planet;
64 }
65
66 function defaultPlanet ()
67 {
68 return createPlanet({
69 name: 'Terra',
70 people: 'Terrans',
71 jumpsToClear: 2,
72 fallTime: 3,
73 riseTime: 1.5
74 });
75 }
76
77 /**
78 * Jump height in tiles for a match line.
79 * @param {object} planet
80 * @param {number} matchLength number of blocks in the line (>= 3)
81 * @param {boolean} vertical true for a vertical line
82 */
83 function jumpHeight (planet, matchLength, vertical)
84 {
85 var extra = Math.max(0, (matchLength | 0) - 3);
86 var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra;
87 return planet.baseJumpHeight * multiplier;
88 }
89
90 /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */
91 function launchVelocity (planet, height)
92 {
93 if (height <= 0)
94 { return 0; }
95 return (2 * height) / planet.riseTime;
96 }
97
98 /** Constant deceleration (tiles/s^2) paired with launchVelocity. */
99 function launchDeceleration (planet, height)
100 {
101 if (height <= 0)
102 { return 0; }
103 return (2 * height) / (planet.riseTime * planet.riseTime);
104 }
105
106 /**
107 * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower
108 * chance that a shot-off block misses the opponent).
109 */
110 function power (planet)
111 {
112 var gravity = (planet.jumpsToClear - 1) / 3;
113 var air = (5 - planet.fallTime) / 4;
114 var mass = (3 - planet.riseTime) / 2.5;
115 return (gravity + air + mass) / 3;
116 }
117
118 /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
119 function missChance (planet)
120 {
121 return 0.60 - 0.55 * power(planet);
122 }
123
124 /**
125 * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
126 * launch force (N), and power as a percentage. The numbers do not have to
127 * explain themselves.
128 */
129 function displayValues (planet)
130 {
131 var gravity = 9.81 * planet.jumpsToClear / 2;
132 var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60;
133 var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160;
134 return {
135 gravity: gravity,
136 density: density,
137 newtons: newtons,
138 power: Math.round(power(planet) * 100),
139 missChance: missChance(planet)
140 };
141 }
142
143 var Planet = {
144 FIELD_WIDTH: FIELD_WIDTH,
145 FIELD_HEIGHT: FIELD_HEIGHT,
146 COLOR_COUNT: 5,
147 SETTLE_TIME: 3,
148 SPAWN_INTERVAL: 1,
149 JUMP_VERTICAL_BONUS: 1.2,
150 JUMP_EXTRA_BLOCK: 0.05,
151 createPlanet: createPlanet,
152 defaultPlanet: defaultPlanet,
153 defaultColors: defaultColors,
154 jumpHeight: jumpHeight,
155 launchVelocity: launchVelocity,
156 launchDeceleration: launchDeceleration,
157 power: power,
158 missChance: missChance,
159 displayValues: displayValues
160 };
161
162 if (typeof module !== 'undefined' && module.exports)
163 { module.exports = Planet; }
164 else if (typeof globalThis !== 'undefined')
165 { globalThis.Planet = Planet; }
@@ -0,0 +1,165 @@
1'use strict';
2
3/**
4 * Planet physics and visuals for Meteors.
5 *
6 * Stage 1 uses a single default planet. Stage 3 lets the player pick a
7 * planet; the numbers and jump-height rules are already applied here so
8 * that later planets only need new data, not new physics code.
9 *
10 * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump).
11 * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles —
12 * at 1, one jump from the floor clears the field; at 4, you need four.
13 * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall).
14 * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s.
15 * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump).
16 * Time to reach the apex, independent of jump height.
17 *
18 * Jump-height multipliers (never explained in the game):
19 * A vertical match of 3 is 120 % of a horizontal row of 3.
20 * Each extra block beyond 3 adds 5 %.
21 *
22 * Power is the average of the three difficulty axes (0 easy .. 1 hard)
23 * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at
24 * power 0 to 5 % at power 1.
25 */
26
27var FIELD_WIDTH = 10;
28var FIELD_HEIGHT = 13;
29
30function clamp (value, lo, hi)
31{
32 return Math.max(lo, Math.min(hi, value));
33}
34
35function defaultColors ()
36{
37 return [
38 { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' },
39 { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' },
40 { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' },
41 { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' },
42 { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' }
43 ];
44}
45
46function createPlanet (opts)
47{
48 opts = opts || {};
49 var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4);
50 var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5);
51 var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3);
52 var colors = opts.colors || defaultColors();
53 var planet = {
54 name: opts.name || 'Terra',
55 people: opts.people || 'Terrans',
56 jumpsToClear: jumpsToClear,
57 fallTime: fallTime,
58 riseTime: riseTime,
59 colors: colors,
60 baseJumpHeight: FIELD_HEIGHT / jumpsToClear,
61 fallSpeed: FIELD_HEIGHT / fallTime
62 };
63 return planet;
64}
65
66function defaultPlanet ()
67{
68 return createPlanet({
69 name: 'Terra',
70 people: 'Terrans',
71 jumpsToClear: 2,
72 fallTime: 3,
73 riseTime: 1.5
74 });
75}
76
77/**
78 * Jump height in tiles for a match line.
79 * @param {object} planet
80 * @param {number} matchLength number of blocks in the line (>= 3)
81 * @param {boolean} vertical true for a vertical line
82 */
83function jumpHeight (planet, matchLength, vertical)
84{
85 var extra = Math.max(0, (matchLength | 0) - 3);
86 var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra;
87 return planet.baseJumpHeight * multiplier;
88}
89
90/** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */
91function launchVelocity (planet, height)
92{
93 if (height <= 0)
94 { return 0; }
95 return (2 * height) / planet.riseTime;
96}
97
98/** Constant deceleration (tiles/s^2) paired with launchVelocity. */
99function launchDeceleration (planet, height)
100{
101 if (height <= 0)
102 { return 0; }
103 return (2 * height) / (planet.riseTime * planet.riseTime);
104}
105
106/**
107 * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower
108 * chance that a shot-off block misses the opponent).
109 */
110function power (planet)
111{
112 var gravity = (planet.jumpsToClear - 1) / 3;
113 var air = (5 - planet.fallTime) / 4;
114 var mass = (3 - planet.riseTime) / 2.5;
115 return (gravity + air + mass) / 3;
116}
117
118/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
119function missChance (planet)
120{
121 return 0.60 - 0.55 * power(planet);
122}
123
124/**
125 * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
126 * launch force (N), and power as a percentage. The numbers do not have to
127 * explain themselves.
128 */
129function displayValues (planet)
130{
131 var gravity = 9.81 * planet.jumpsToClear / 2;
132 var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60;
133 var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160;
134 return {
135 gravity: gravity,
136 density: density,
137 newtons: newtons,
138 power: Math.round(power(planet) * 100),
139 missChance: missChance(planet)
140 };
141}
142
143var Planet = {
144 FIELD_WIDTH: FIELD_WIDTH,
145 FIELD_HEIGHT: FIELD_HEIGHT,
146 COLOR_COUNT: 5,
147 SETTLE_TIME: 3,
148 SPAWN_INTERVAL: 1,
149 JUMP_VERTICAL_BONUS: 1.2,
150 JUMP_EXTRA_BLOCK: 0.05,
151 createPlanet: createPlanet,
152 defaultPlanet: defaultPlanet,
153 defaultColors: defaultColors,
154 jumpHeight: jumpHeight,
155 launchVelocity: launchVelocity,
156 launchDeceleration: launchDeceleration,
157 power: power,
158 missChance: missChance,
159 displayValues: displayValues
160};
161
162if (typeof module !== 'undefined' && module.exports)
163{ module.exports = Planet; }
164else if (typeof globalThis !== 'undefined')
165{ globalThis.Planet = Planet; }
src/rng.js added +44 −0
@@ -0,0 +1,44 @@
1 'use strict';
2
3 /**
4 * Seedable mulberry32 RNG, so the game core stays deterministic in tests
5 * and (later) for multiplayer replays.
6 */
7 function createRng (seed)
8 {
9 var a = (seed >>> 0) || 1;
10 return function rng ()
11 {
12 a |= 0;
13 a = a + 0x6D2B79F5 | 0;
14 var t = Math.imul(a ^ a >>> 15, 1 | a);
15 t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
16 return ((t ^ t >>> 14) >>> 0) / 4294967296;
17 };
18 }
19
20 function pickIndex (rng, count)
21 {
22 var n = count | 0;
23 if (n <= 0)
24 { return 0; }
25 return Math.floor(rng() * n) % n;
26 }
27
28 function pickFrom (rng, list)
29 {
30 if (!list || !list.length)
31 { return undefined; }
32 return list[pickIndex(rng, list.length)];
33 }
34
35 var Rng = {
36 create: createRng,
37 pickIndex: pickIndex,
38 pickFrom: pickFrom
39 };
40
41 if (typeof module !== 'undefined' && module.exports)
42 { module.exports = Rng; }
43 else if (typeof globalThis !== 'undefined')
44 { globalThis.Rng = Rng; }
@@ -0,0 +1,44 @@
1'use strict';
2
3/**
4 * Seedable mulberry32 RNG, so the game core stays deterministic in tests
5 * and (later) for multiplayer replays.
6 */
7function createRng (seed)
8{
9 var a = (seed >>> 0) || 1;
10 return function rng ()
11 {
12 a |= 0;
13 a = a + 0x6D2B79F5 | 0;
14 var t = Math.imul(a ^ a >>> 15, 1 | a);
15 t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
16 return ((t ^ t >>> 14) >>> 0) / 4294967296;
17 };
18}
19
20function pickIndex (rng, count)
21{
22 var n = count | 0;
23 if (n <= 0)
24 { return 0; }
25 return Math.floor(rng() * n) % n;
26}
27
28function pickFrom (rng, list)
29{
30 if (!list || !list.length)
31 { return undefined; }
32 return list[pickIndex(rng, list.length)];
33}
34
35var Rng = {
36 create: createRng,
37 pickIndex: pickIndex,
38 pickFrom: pickFrom
39};
40
41if (typeof module !== 'undefined' && module.exports)
42{ module.exports = Rng; }
43else if (typeof globalThis !== 'undefined')
44{ globalThis.Rng = Rng; }
test/planet.test.js added +159 −0
@@ -0,0 +1,159 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const Planet = require('../src/planet.js');
6
7 describe('Planet field and defaults', () =>
8 {
9 it('has a 10 by 13 field', () =>
10 {
11 assert.equal(Planet.FIELD_WIDTH, 10);
12 assert.equal(Planet.FIELD_HEIGHT, 13);
13 });
14
15 it('default planet is Terra with mid-range physics', () =>
16 {
17 const p = Planet.defaultPlanet();
18 assert.equal(p.name, 'Terra');
19 assert.equal(p.people, 'Terrans');
20 assert.equal(p.jumpsToClear, 2);
21 assert.equal(p.fallTime, 3);
22 assert.equal(p.riseTime, 1.5);
23 assert.equal(p.colors.length, 5);
24 assert.equal(p.baseJumpHeight, 13 / 2);
25 assert.equal(p.fallSpeed, 13 / 3);
26 });
27
28 it('clamps gravity, air and mass to the stage-3 ranges', () =>
29 {
30 const lo = Planet.createPlanet({ jumpsToClear: 0, fallTime: 0, riseTime: 0 });
31 assert.equal(lo.jumpsToClear, 1);
32 assert.equal(lo.fallTime, 1);
33 assert.equal(lo.riseTime, 0.5);
34
35 const hi = Planet.createPlanet({ jumpsToClear: 9, fallTime: 99, riseTime: 99 });
36 assert.equal(hi.jumpsToClear, 4);
37 assert.equal(hi.fallTime, 5);
38 assert.equal(hi.riseTime, 3);
39 });
40
41 it('keeps colors and shapes data-driven with a unique shape per color', () =>
42 {
43 const colors = Planet.defaultColors();
44 assert.equal(colors.length, 5);
45 const shapes = new Set(colors.map((c) => c.shape));
46 const images = new Set(colors.map((c) => c.image));
47 assert.equal(shapes.size, 5);
48 assert.equal(images.size, 5);
49 for (const c of colors)
50 {
51 assert.ok(c.hex.startsWith('#'));
52 assert.ok(c.image.startsWith('images/'));
53 assert.ok(typeof c.name === 'string' && c.name.length > 0);
54 }
55 });
56 });
57
58 describe('Jump height rules', () =>
59 {
60 const planet = Planet.defaultPlanet();
61 const base = planet.baseJumpHeight;
62
63 it('a horizontal match of 3 uses the base jump height', () =>
64 {
65 assert.equal(Planet.jumpHeight(planet, 3, false), base);
66 });
67
68 it('a vertical match of 3 is 120% of a horizontal row', () =>
69 {
70 assert.equal(Planet.jumpHeight(planet, 3, true), base * 1.2);
71 });
72
73 it('each extra block beyond 3 adds 5% jump height', () =>
74 {
75 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, false) - base * 1.05) < 1e-12);
76 assert.ok(Math.abs(Planet.jumpHeight(planet, 5, false) - base * 1.10) < 1e-12);
77 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, true) - base * (1.2 + 0.05)) < 1e-12);
78 assert.ok(Math.abs(Planet.jumpHeight(planet, 6, true) - base * (1.2 + 0.15)) < 1e-12);
79 });
80
81 it('at min gravity one horizontal-3 jump equals the field height', () =>
82 {
83 const easy = Planet.createPlanet({ jumpsToClear: 1 });
84 assert.equal(Planet.jumpHeight(easy, 3, false), 13);
85 });
86
87 it('at max gravity four horizontal-3 jumps equal the field height', () =>
88 {
89 const hard = Planet.createPlanet({ jumpsToClear: 4 });
90 assert.equal(Planet.jumpHeight(hard, 3, false) * 4, 13);
91 });
92 });
93
94 describe('Launch and fall physics parameters', () =>
95 {
96 it('a jump of height H peaks in riseTime at constant deceleration', () =>
97 {
98 const p = Planet.defaultPlanet();
99 const H = 6.5;
100 const v0 = Planet.launchVelocity(p, H);
101 const a = Planet.launchDeceleration(p, H);
102 assert.equal(v0, 2 * H / p.riseTime);
103 assert.equal(a, 2 * H / (p.riseTime * p.riseTime));
104 // v = v0 - a t; v=0 at t=riseTime; s = v0 t - 0.5 a t^2 = H
105 const t = p.riseTime;
106 assert.ok(Math.abs((v0 - a * t)) < 1e-12);
107 assert.ok(Math.abs((v0 * t - 0.5 * a * t * t) - H) < 1e-12);
108 });
109
110 it('apex time is independent of jump height (mass)', () =>
111 {
112 const p = Planet.defaultPlanet();
113 const a1 = Planet.launchDeceleration(p, 4);
114 const v1 = Planet.launchVelocity(p, 4);
115 const a2 = Planet.launchDeceleration(p, 10);
116 const v2 = Planet.launchVelocity(p, 10);
117 assert.ok(Math.abs(v1 / a1 - p.riseTime) < 1e-12);
118 assert.ok(Math.abs(v2 / a2 - p.riseTime) < 1e-12);
119 });
120
121 it('fall speed is field height over fallTime (air resistance)', () =>
122 {
123 const slow = Planet.createPlanet({ fallTime: 5 });
124 const fast = Planet.createPlanet({ fallTime: 1 });
125 assert.equal(slow.fallSpeed, 13 / 5);
126 assert.equal(fast.fallSpeed, 13);
127 assert.ok(fast.fallSpeed > slow.fallSpeed);
128 });
129 });
130
131 describe('Power and display values', () =>
132 {
133 it('easiest planet has power 0 and 60% miss chance', () =>
134 {
135 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3 });
136 assert.equal(Planet.power(easy), 0);
137 assert.equal(Planet.missChance(easy), 0.60);
138 });
139
140 it('hardest planet has power 1 and 5% miss chance', () =>
141 {
142 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
143 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
144 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
145 });
146
147 it('display values are numeric gravity, density, newtons and power %', () =>
148 {
149 const p = Planet.defaultPlanet();
150 const d = Planet.displayValues(p);
151 assert.ok(d.gravity > 0);
152 assert.ok(d.density > 0);
153 assert.ok(d.newtons > 0);
154 assert.equal(typeof d.power, 'number');
155 assert.ok(d.power >= 0 && d.power <= 100);
156 // Mid-range Terra (jumps=2) shows Earth-like 9.81
157 assert.ok(Math.abs(d.gravity - 9.81) < 1e-9);
158 });
159 });
@@ -0,0 +1,159 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const Planet = require('../src/planet.js');
6
7describe('Planet field and defaults', () =>
8{
9 it('has a 10 by 13 field', () =>
10 {
11 assert.equal(Planet.FIELD_WIDTH, 10);
12 assert.equal(Planet.FIELD_HEIGHT, 13);
13 });
14
15 it('default planet is Terra with mid-range physics', () =>
16 {
17 const p = Planet.defaultPlanet();
18 assert.equal(p.name, 'Terra');
19 assert.equal(p.people, 'Terrans');
20 assert.equal(p.jumpsToClear, 2);
21 assert.equal(p.fallTime, 3);
22 assert.equal(p.riseTime, 1.5);
23 assert.equal(p.colors.length, 5);
24 assert.equal(p.baseJumpHeight, 13 / 2);
25 assert.equal(p.fallSpeed, 13 / 3);
26 });
27
28 it('clamps gravity, air and mass to the stage-3 ranges', () =>
29 {
30 const lo = Planet.createPlanet({ jumpsToClear: 0, fallTime: 0, riseTime: 0 });
31 assert.equal(lo.jumpsToClear, 1);
32 assert.equal(lo.fallTime, 1);
33 assert.equal(lo.riseTime, 0.5);
34
35 const hi = Planet.createPlanet({ jumpsToClear: 9, fallTime: 99, riseTime: 99 });
36 assert.equal(hi.jumpsToClear, 4);
37 assert.equal(hi.fallTime, 5);
38 assert.equal(hi.riseTime, 3);
39 });
40
41 it('keeps colors and shapes data-driven with a unique shape per color', () =>
42 {
43 const colors = Planet.defaultColors();
44 assert.equal(colors.length, 5);
45 const shapes = new Set(colors.map((c) => c.shape));
46 const images = new Set(colors.map((c) => c.image));
47 assert.equal(shapes.size, 5);
48 assert.equal(images.size, 5);
49 for (const c of colors)
50 {
51 assert.ok(c.hex.startsWith('#'));
52 assert.ok(c.image.startsWith('images/'));
53 assert.ok(typeof c.name === 'string' && c.name.length > 0);
54 }
55 });
56});
57
58describe('Jump height rules', () =>
59{
60 const planet = Planet.defaultPlanet();
61 const base = planet.baseJumpHeight;
62
63 it('a horizontal match of 3 uses the base jump height', () =>
64 {
65 assert.equal(Planet.jumpHeight(planet, 3, false), base);
66 });
67
68 it('a vertical match of 3 is 120% of a horizontal row', () =>
69 {
70 assert.equal(Planet.jumpHeight(planet, 3, true), base * 1.2);
71 });
72
73 it('each extra block beyond 3 adds 5% jump height', () =>
74 {
75 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, false) - base * 1.05) < 1e-12);
76 assert.ok(Math.abs(Planet.jumpHeight(planet, 5, false) - base * 1.10) < 1e-12);
77 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, true) - base * (1.2 + 0.05)) < 1e-12);
78 assert.ok(Math.abs(Planet.jumpHeight(planet, 6, true) - base * (1.2 + 0.15)) < 1e-12);
79 });
80
81 it('at min gravity one horizontal-3 jump equals the field height', () =>
82 {
83 const easy = Planet.createPlanet({ jumpsToClear: 1 });
84 assert.equal(Planet.jumpHeight(easy, 3, false), 13);
85 });
86
87 it('at max gravity four horizontal-3 jumps equal the field height', () =>
88 {
89 const hard = Planet.createPlanet({ jumpsToClear: 4 });
90 assert.equal(Planet.jumpHeight(hard, 3, false) * 4, 13);
91 });
92});
93
94describe('Launch and fall physics parameters', () =>
95{
96 it('a jump of height H peaks in riseTime at constant deceleration', () =>
97 {
98 const p = Planet.defaultPlanet();
99 const H = 6.5;
100 const v0 = Planet.launchVelocity(p, H);
101 const a = Planet.launchDeceleration(p, H);
102 assert.equal(v0, 2 * H / p.riseTime);
103 assert.equal(a, 2 * H / (p.riseTime * p.riseTime));
104 // v = v0 - a t; v=0 at t=riseTime; s = v0 t - 0.5 a t^2 = H
105 const t = p.riseTime;
106 assert.ok(Math.abs((v0 - a * t)) < 1e-12);
107 assert.ok(Math.abs((v0 * t - 0.5 * a * t * t) - H) < 1e-12);
108 });
109
110 it('apex time is independent of jump height (mass)', () =>
111 {
112 const p = Planet.defaultPlanet();
113 const a1 = Planet.launchDeceleration(p, 4);
114 const v1 = Planet.launchVelocity(p, 4);
115 const a2 = Planet.launchDeceleration(p, 10);
116 const v2 = Planet.launchVelocity(p, 10);
117 assert.ok(Math.abs(v1 / a1 - p.riseTime) < 1e-12);
118 assert.ok(Math.abs(v2 / a2 - p.riseTime) < 1e-12);
119 });
120
121 it('fall speed is field height over fallTime (air resistance)', () =>
122 {
123 const slow = Planet.createPlanet({ fallTime: 5 });
124 const fast = Planet.createPlanet({ fallTime: 1 });
125 assert.equal(slow.fallSpeed, 13 / 5);
126 assert.equal(fast.fallSpeed, 13);
127 assert.ok(fast.fallSpeed > slow.fallSpeed);
128 });
129});
130
131describe('Power and display values', () =>
132{
133 it('easiest planet has power 0 and 60% miss chance', () =>
134 {
135 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3 });
136 assert.equal(Planet.power(easy), 0);
137 assert.equal(Planet.missChance(easy), 0.60);
138 });
139
140 it('hardest planet has power 1 and 5% miss chance', () =>
141 {
142 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
143 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
144 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
145 });
146
147 it('display values are numeric gravity, density, newtons and power %', () =>
148 {
149 const p = Planet.defaultPlanet();
150 const d = Planet.displayValues(p);
151 assert.ok(d.gravity > 0);
152 assert.ok(d.density > 0);
153 assert.ok(d.newtons > 0);
154 assert.equal(typeof d.power, 'number');
155 assert.ok(d.power >= 0 && d.power <= 100);
156 // Mid-range Terra (jumps=2) shows Earth-like 9.81
157 assert.ok(Math.abs(d.gravity - 9.81) < 1e-9);
158 });
159});
test/rng.test.js added +41 −0
@@ -0,0 +1,41 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const Rng = require('../src/rng.js');
6
7 describe('Rng', () =>
8 {
9 it('is deterministic for the same seed', () =>
10 {
11 const a = Rng.create(42);
12 const b = Rng.create(42);
13 const seqA = [a(), a(), a(), a(), a()];
14 const seqB = [b(), b(), b(), b(), b()];
15 assert.deepEqual(seqA, seqB);
16 });
17
18 it('differs across seeds and stays in [0, 1)', () =>
19 {
20 const a = Rng.create(1);
21 const b = Rng.create(2);
22 assert.notEqual(a(), b());
23 const r = Rng.create(99);
24 for (let i = 0; i < 1000; i++)
25 {
26 const x = r();
27 assert.ok(x >= 0 && x < 1);
28 }
29 });
30
31 it('pickIndex stays inside the range', () =>
32 {
33 const r = Rng.create(7);
34 for (let i = 0; i < 200; i++)
35 {
36 const idx = Rng.pickIndex(r, 5);
37 assert.ok(idx >= 0 && idx < 5);
38 }
39 assert.equal(Rng.pickIndex(r, 0), 0);
40 });
41 });
@@ -0,0 +1,41 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const Rng = require('../src/rng.js');
6
7describe('Rng', () =>
8{
9 it('is deterministic for the same seed', () =>
10 {
11 const a = Rng.create(42);
12 const b = Rng.create(42);
13 const seqA = [a(), a(), a(), a(), a()];
14 const seqB = [b(), b(), b(), b(), b()];
15 assert.deepEqual(seqA, seqB);
16 });
17
18 it('differs across seeds and stays in [0, 1)', () =>
19 {
20 const a = Rng.create(1);
21 const b = Rng.create(2);
22 assert.notEqual(a(), b());
23 const r = Rng.create(99);
24 for (let i = 0; i < 1000; i++)
25 {
26 const x = r();
27 assert.ok(x >= 0 && x < 1);
28 }
29 });
30
31 it('pickIndex stays inside the range', () =>
32 {
33 const r = Rng.create(7);
34 for (let i = 0; i < 200; i++)
35 {
36 const idx = Rng.pickIndex(r, 5);
37 assert.ok(idx >= 0 && idx < 5);
38 }
39 assert.equal(Rng.pickIndex(r, 0), 0);
40 });
41});