BlueGit

projects-timfalken-com meteors

Files

meteors / src / planet.js

planet.js

planet.js 12.0 KiB Download Raw
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): 2 (easy, high jump) .. 4 (hard, short jump).
11 * Minimum raised from 1 to 2 in t1199 (GRAVITY_MIN). Shown as a
12 * percentage, linear in the value: pct = (v − 1)·100, so 2 → 100 %
13 * (9.81 g), 3 → 200 % (19.62 g), 4 → 300 % (29.43 g).
14 * A horizontal match of 3 jumps JUMP_HEIGHT_SCALE * FIELD_HEIGHT
15 * / jumpsToClear tiles (JUMP_HEIGHT_SCALE is 1.5 since t1119: the
16 * minimum jump is 50% higher). At gravity 1 that is 19.5 tiles, so
17 * one jump from the floor clears the 13-tile field with room to spare;
18 * at 4 you still need more than one jump, but each is 50% taller.
19 * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall).
20 * Spawn, unsupported and cargo blocks fall at FIELD_HEIGHT / fallTime
21 * tiles/s. After a jump peaks, a flying cluster floats down at
22 * DESCENT_SPEED_SCALE * that speed (0.5 since t1122 / t1124: only the
23 * cluster float-down is halved). Relative differences between planets
24 * (1 / fallTime) are unchanged. Upward launch is not affected.
25 * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump).
26 * Time to reach the apex, independent of jump height.
27 *
28 * Jump-height multipliers (never explained in the game):
29 * A vertical match of 3 is 120 % of a horizontal row of 3.
30 * Each extra block beyond 3 adds 5 %.
31 *
32 * Power is the average of the three difficulty axes (0 easy .. 1 hard)
33 * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at
34 * power 0 to 5 % at power 1.
35 */
36
37var FIELD_WIDTH = 10;
38var FIELD_HEIGHT = 13;
39/** t1119: every match jump (the gravity-applied minimum and the
40 * vertical / extra-block bonuses derived from it) is 50% higher. */
41var JUMP_HEIGHT_SCALE = 1.5;
42/** t1122 / t1124: only a flying cluster's post-apex float-down
43 * (terminal speed and the apex ease into it) is halved. Spawn fall,
44 * unsupported / released singles and dropped cargo use the full
45 * fallSpeed. Upward launch is unchanged. */
46var DESCENT_SPEED_SCALE = 0.5;
47
48
49function clamp (value, lo, hi)
50{
51 return Math.max(lo, Math.min(hi, value));
52}
53
54function defaultColors ()
55{
56 return [
57 { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' },
58 { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' },
59 { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' },
60 { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' },
61 { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' }
62 ];
63}
64
65/** t1199: gravity (jumpsToClear) range; values below the minimum clamp up. */
66var GRAVITY_MIN = 2;
67var GRAVITY_MAX = 4;
68/** t1199: g at 100 % (jumpsToClear 2). */
69var EARTH_G = 9.81;
70
71/** t1199: displayed gravity percentage, linear: (v − 1)·100. */
72function gravityPercent (jumpsToClear)
73{
74 return Math.round((clamp(jumpsToClear, GRAVITY_MIN, GRAVITY_MAX) - 1) * 100);
75}
76
77/** t1199: displayed g, consistent with the percentage: 9.81·(v − 1). */
78function gravityG (jumpsToClear)
79{
80 return EARTH_G * (clamp(jumpsToClear, GRAVITY_MIN, GRAVITY_MAX) - 1);
81}
82
83/** t1199: "100% (9.81g)" style text (i18n template stats.gravity). */
84function formatGravity (jumpsToClear)
85{
86 var params = { pct: gravityPercent(jumpsToClear), g: gravityG(jumpsToClear).toFixed(2) };
87 if (typeof I18n !== 'undefined' && I18n.t)
88 {
89 var s = I18n.t('stats.gravity', params);
90 if (s && s !== 'stats.gravity')
91 { return s; }
92 }
93 return params.pct + '% (' + params.g + 'g)';
94}
95
96function createPlanet (opts)
97{
98 opts = opts || {};
99 var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, GRAVITY_MIN, GRAVITY_MAX);
100 var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5);
101 var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3);
102 var colors = opts.colors || defaultColors();
103 var planet = {
104 id: opts.id || null,
105 name: opts.name || 'Terra',
106 people: opts.people || 'Terrans',
107 jumpsToClear: jumpsToClear,
108 fallTime: fallTime,
109 riseTime: riseTime,
110 colors: colors,
111 baseJumpHeight: JUMP_HEIGHT_SCALE * FIELD_HEIGHT / jumpsToClear,
112 // Single-block fall (spawn, unsupported, dropped cargo).
113 fallSpeed: FIELD_HEIGHT / fallTime,
114 // Time to ease from vy=0 at the apex to terminal speed.
115 // Shorter when air resistance is low (small fallTime).
116 apexEaseTime: 0.15 * fallTime,
117 fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
118 // t1122 / t1124: flying-cluster float-down only. Same ease
119 // duration; accel scales with the halved terminal speed.
120 clusterFallSpeed: DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime,
121 clusterFallAccel: (DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
122 // Visual landing bounce: faster at low air resistance. Stage-3 params.
123 bounceDuration: 0.12 * fallTime,
124 bounceSquash: 0.16
125 };
126 return planet;
127}
128
129function loadCatalog ()
130{
131 if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets)
132 { return PLANET_CATALOG; }
133 if (typeof require !== 'undefined')
134 {
135 try
136 { return require('../server/lib/planets.json'); }
137 catch (e) {}
138 }
139 return { planets: [] };
140}
141
142/** Player-owned custom planets (from the profile) and any remembered
143 * snapshots (opponents in a lobby), not part of the official catalog. */
144var _customPlanets = [];
145var _remembered = Object.create(null);
146
147function setCustomPlanets (list)
148{
149 _customPlanets = [];
150 if (!list || !list.length)
151 { return; }
152 for (var i = 0; i < list.length; i++)
153 { _customPlanets.push(planetFromData(list[i])); }
154}
155
156function rememberPlanet (data)
157{
158 if (!data || !data.id)
159 { return null; }
160 var pl = planetFromData(data);
161 _remembered[pl.id] = pl;
162 return pl;
163}
164
165function forgetRemembered ()
166{
167 _remembered = Object.create(null);
168}
169
170function planetFromData (data)
171{
172 var planet = createPlanet({
173 name: data.name,
174 people: data.people,
175 jumpsToClear: data.jumpsToClear,
176 fallTime: data.fallTime,
177 riseTime: data.riseTime,
178 colors: data.colors
179 });
180 planet.id = data.id;
181 if (data.custom)
182 { planet.custom = true; }
183 if (data.owner_id)
184 { planet.owner_id = data.owner_id; }
185 return planet;
186}
187
188function allPlanets ()
189{
190 var raw = loadCatalog().planets || [];
191 var out = [];
192 for (var i = 0; i < raw.length; i++)
193 { out.push(planetFromData(raw[i])); }
194 if (out.length === 0)
195 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
196 for (var c = 0; c < _customPlanets.length; c++)
197 { out.push(_customPlanets[c]); }
198 return out;
199}
200
201function planetById (id)
202{
203 var all = allPlanets();
204 for (var i = 0; i < all.length; i++)
205 {
206 if (all[i].id === id)
207 { return all[i]; }
208 }
209 if (id && _remembered[id])
210 { return _remembered[id]; }
211 return defaultPlanet();
212}
213
214function officialPlanets ()
215{
216 var raw = loadCatalog().planets || [];
217 var out = [];
218 for (var i = 0; i < raw.length; i++)
219 { out.push(planetFromData(raw[i])); }
220 return out;
221}
222
223function customPlanets ()
224{
225 return _customPlanets.slice();
226}
227
228function pickerPlanets ()
229{
230 return officialPlanets().concat(_customPlanets);
231}
232
233function defaultPlanet ()
234{
235 var all = allPlanets();
236 for (var i = 0; i < all.length; i++)
237 {
238 if (all[i].id === 'terra')
239 { return all[i]; }
240 }
241 return all[0];
242}
243
244function formatStats (planet)
245{
246 var d = displayValues(planet);
247 var power = (typeof I18n !== 'undefined')
248 ? I18n.t('stats.powerLabel') : 'Power';
249 return 'g ' + formatGravity(planet.jumpsToClear)
250 + ' d ' + d.density.toFixed(2)
251 + ' F ' + Math.round(d.newtons) + ' N'
252 + ' ' + power + ' ' + d.power + '%';
253}
254
255function spriteKey (planet, colorIndex)
256{
257 var colors = planet && planet.colors;
258 var c = colors && colors[colorIndex];
259 if (!c)
260 { return 'gray'; }
261 // Custom planets (and any color that names a registry block) share
262 // the preloaded Blocks.images() sprite, so late-loaded customs and
263 // opponent snapshots do not need a second preload.
264 if (c.block)
265 { return 'block:' + c.block; }
266 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
267}
268
269/**
270 * Jump height in tiles for a match line.
271 * @param {object} planet
272 * @param {number} matchLength number of blocks in the line (>= 3)
273 * @param {boolean} vertical true for a vertical line
274 */
275function jumpHeight (planet, matchLength, vertical)
276{
277 var extra = Math.max(0, (matchLength | 0) - 3);
278 var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra;
279 return planet.baseJumpHeight * multiplier;
280}
281
282/** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */
283function launchVelocity (planet, height)
284{
285 if (height <= 0)
286 { return 0; }
287 return (2 * height) / planet.riseTime;
288}
289
290/** Constant deceleration (tiles/s^2) paired with launchVelocity. */
291function launchDeceleration (planet, height)
292{
293 if (height <= 0)
294 { return 0; }
295 return (2 * height) / (planet.riseTime * planet.riseTime);
296}
297
298/**
299 * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower
300 * chance that a shot-off block misses the opponent).
301 */
302function power (planet)
303{
304 // t1199: scale kept on the old 1..4 axis on purpose, so unchanged
305 // planets keep their Power/miss chance; the 2.0 minimum means the
306 // gravity term is now at least 1/3 (lowest possible Power ≈ 0.11).
307 var gravity = (planet.jumpsToClear - 1) / 3;
308 var air = (5 - planet.fallTime) / 4;
309 var mass = (3 - planet.riseTime) / 2.5;
310 return (gravity + air + mass) / 3;
311}
312
313/** Maximum miss chance, used by the gameplay formula and the displayed Power. */
314var MAX_MISS_CHANCE = 0.60;
315var MIN_MISS_CHANCE = 0.05;
316
317/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
318function missChance (planet)
319{
320 return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet);
321}
322
323/**
324 * Displayed Power, as a 0–100 percentage. This is the complement of the
325 * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss
326 * → 0% Power. The gameplay power() and missChance() formulas are unchanged.
327 */
328function displayPower (planet)
329{
330 return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100);
331}
332
333/**
334 * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
335 * launch force (N), and displayed Power as a percentage. The numbers do
336 * not have to explain themselves.
337 */
338function displayValues (planet)
339{
340 var gravity = gravityG(planet.jumpsToClear);
341 var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60;
342 var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160;
343 return {
344 gravity: gravity,
345 gravityPct: gravityPercent(planet.jumpsToClear),
346 density: density,
347 newtons: newtons,
348 power: displayPower(planet),
349 missChance: missChance(planet)
350 };
351}
352
353var Planet = {
354 FIELD_WIDTH: FIELD_WIDTH,
355 FIELD_HEIGHT: FIELD_HEIGHT,
356 JUMP_HEIGHT_SCALE: JUMP_HEIGHT_SCALE,
357 DESCENT_SPEED_SCALE: DESCENT_SPEED_SCALE,
358 COLOR_COUNT: 5,
359 SETTLE_TIME: 3,
360 SPAWN_INTERVAL: 1,
361 JUMP_VERTICAL_BONUS: 1.2,
362 JUMP_EXTRA_BLOCK: 0.05,
363 GRAVITY_MIN: GRAVITY_MIN,
364 GRAVITY_MAX: GRAVITY_MAX,
365 EARTH_G: EARTH_G,
366 gravityPercent: gravityPercent,
367 gravityG: gravityG,
368 formatGravity: formatGravity,
369 createPlanet: createPlanet,
370 defaultPlanet: defaultPlanet,
371 defaultColors: defaultColors,
372 allPlanets: allPlanets,
373 officialPlanets: officialPlanets,
374 customPlanets: customPlanets,
375 pickerPlanets: pickerPlanets,
376 setCustomPlanets: setCustomPlanets,
377 rememberPlanet: rememberPlanet,
378 forgetRemembered: forgetRemembered,
379 planetById: planetById,
380 planetFromData: planetFromData,
381 formatStats: formatStats,
382 spriteKey: spriteKey,
383 jumpHeight: jumpHeight,
384 launchVelocity: launchVelocity,
385 launchDeceleration: launchDeceleration,
386 MAX_MISS_CHANCE: MAX_MISS_CHANCE,
387 MIN_MISS_CHANCE: MIN_MISS_CHANCE,
388 power: power,
389 missChance: missChance,
390 displayPower: displayPower,
391 displayValues: displayValues
392};
393
394if (typeof module !== 'undefined' && module.exports)
395{ module.exports = Planet; }
396else if (typeof globalThis !== 'undefined')
397{ globalThis.Planet = Planet; }