'use strict'; /** * Planet physics and visuals for Meteors. * * Stage 1 uses a single default planet. Stage 3 lets the player pick a * planet; the numbers and jump-height rules are already applied here so * that later planets only need new data, not new physics code. * * Gravity (jumpsToClear): 2 (easy, high jump) .. 4 (hard, short jump). * Minimum raised from 1 to 2 in t1199 (GRAVITY_MIN). Shown as a * percentage, linear in the value: pct = (v − 1)·100, so 2 → 100 % * (9.81 g), 3 → 200 % (19.62 g), 4 → 300 % (29.43 g). * A horizontal match of 3 jumps JUMP_HEIGHT_SCALE * FIELD_HEIGHT * / jumpsToClear tiles (JUMP_HEIGHT_SCALE is 1.5 since t1119: the * minimum jump is 50% higher). At gravity 1 that is 19.5 tiles, so * one jump from the floor clears the 13-tile field with room to spare; * at 4 you still need more than one jump, but each is 50% taller. * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). * Spawn, unsupported and cargo blocks fall at FIELD_HEIGHT / fallTime * tiles/s. After a jump peaks, a flying cluster floats down at * DESCENT_SPEED_SCALE * that speed (0.5 since t1122 / t1124: only the * cluster float-down is halved). Relative differences between planets * (1 / fallTime) are unchanged. Upward launch is not affected. * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). * Time to reach the apex, independent of jump height. * * Jump-height multipliers (never explained in the game): * A vertical match of 3 is 120 % of a horizontal row of 3. * Each extra block beyond 3 adds 5 %. * * Power is the average of the three difficulty axes (0 easy .. 1 hard) * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at * power 0 to 5 % at power 1. */ var FIELD_WIDTH = 10; var FIELD_HEIGHT = 13; /** t1119: every match jump (the gravity-applied minimum and the * vertical / extra-block bonuses derived from it) is 50% higher. */ var JUMP_HEIGHT_SCALE = 1.5; /** t1122 / t1124: only a flying cluster's post-apex float-down * (terminal speed and the apex ease into it) is halved. Spawn fall, * unsupported / released singles and dropped cargo use the full * fallSpeed. Upward launch is unchanged. */ var DESCENT_SPEED_SCALE = 0.5; function clamp (value, lo, hi) { return Math.max(lo, Math.min(hi, value)); } function defaultColors () { return [ { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } ]; } /** t1199: gravity (jumpsToClear) range; values below the minimum clamp up. */ var GRAVITY_MIN = 2; var GRAVITY_MAX = 4; /** t1199: g at 100 % (jumpsToClear 2). */ var EARTH_G = 9.81; /** t1199: displayed gravity percentage, linear: (v − 1)·100. */ function gravityPercent (jumpsToClear) { return Math.round((clamp(jumpsToClear, GRAVITY_MIN, GRAVITY_MAX) - 1) * 100); } /** t1199: displayed g, consistent with the percentage: 9.81·(v − 1). */ function gravityG (jumpsToClear) { return EARTH_G * (clamp(jumpsToClear, GRAVITY_MIN, GRAVITY_MAX) - 1); } /** t1199: "100% (9.81g)" style text (i18n template stats.gravity). */ function formatGravity (jumpsToClear) { var params = { pct: gravityPercent(jumpsToClear), g: gravityG(jumpsToClear).toFixed(2) }; if (typeof I18n !== 'undefined' && I18n.t) { var s = I18n.t('stats.gravity', params); if (s && s !== 'stats.gravity') { return s; } } return params.pct + '% (' + params.g + 'g)'; } function createPlanet (opts) { opts = opts || {}; var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, GRAVITY_MIN, GRAVITY_MAX); var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); var colors = opts.colors || defaultColors(); var planet = { id: opts.id || null, name: opts.name || 'Terra', people: opts.people || 'Terrans', jumpsToClear: jumpsToClear, fallTime: fallTime, riseTime: riseTime, colors: colors, baseJumpHeight: JUMP_HEIGHT_SCALE * FIELD_HEIGHT / jumpsToClear, // Single-block fall (spawn, unsupported, dropped cargo). fallSpeed: FIELD_HEIGHT / fallTime, // Time to ease from vy=0 at the apex to terminal speed. // Shorter when air resistance is low (small fallTime). apexEaseTime: 0.15 * fallTime, fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime), // t1122 / t1124: flying-cluster float-down only. Same ease // duration; accel scales with the halved terminal speed. clusterFallSpeed: DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime, clusterFallAccel: (DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime) / (0.15 * fallTime), // Visual landing bounce: faster at low air resistance. Stage-3 params. bounceDuration: 0.12 * fallTime, bounceSquash: 0.16 }; return planet; } function loadCatalog () { if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets) { return PLANET_CATALOG; } if (typeof require !== 'undefined') { try { return require('../server/lib/planets.json'); } catch (e) {} } return { planets: [] }; } /** Player-owned custom planets (from the profile) and any remembered * snapshots (opponents in a lobby), not part of the official catalog. */ var _customPlanets = []; var _remembered = Object.create(null); function setCustomPlanets (list) { _customPlanets = []; if (!list || !list.length) { return; } for (var i = 0; i < list.length; i++) { _customPlanets.push(planetFromData(list[i])); } } function rememberPlanet (data) { if (!data || !data.id) { return null; } var pl = planetFromData(data); _remembered[pl.id] = pl; return pl; } function forgetRemembered () { _remembered = Object.create(null); } function planetFromData (data) { var planet = createPlanet({ name: data.name, people: data.people, jumpsToClear: data.jumpsToClear, fallTime: data.fallTime, riseTime: data.riseTime, colors: data.colors }); planet.id = data.id; if (data.custom) { planet.custom = true; } if (data.owner_id) { planet.owner_id = data.owner_id; } return planet; } function allPlanets () { var raw = loadCatalog().planets || []; var out = []; for (var i = 0; i < raw.length; i++) { out.push(planetFromData(raw[i])); } if (out.length === 0) { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; } for (var c = 0; c < _customPlanets.length; c++) { out.push(_customPlanets[c]); } return out; } function planetById (id) { var all = allPlanets(); for (var i = 0; i < all.length; i++) { if (all[i].id === id) { return all[i]; } } if (id && _remembered[id]) { return _remembered[id]; } return defaultPlanet(); } function officialPlanets () { var raw = loadCatalog().planets || []; var out = []; for (var i = 0; i < raw.length; i++) { out.push(planetFromData(raw[i])); } return out; } function customPlanets () { return _customPlanets.slice(); } function pickerPlanets () { return officialPlanets().concat(_customPlanets); } function defaultPlanet () { var all = allPlanets(); for (var i = 0; i < all.length; i++) { if (all[i].id === 'terra') { return all[i]; } } return all[0]; } function formatStats (planet) { var d = displayValues(planet); var power = (typeof I18n !== 'undefined') ? I18n.t('stats.powerLabel') : 'Power'; return 'g ' + formatGravity(planet.jumpsToClear) + ' d ' + d.density.toFixed(2) + ' F ' + Math.round(d.newtons) + ' N' + ' ' + power + ' ' + d.power + '%'; } function spriteKey (planet, colorIndex) { var colors = planet && planet.colors; var c = colors && colors[colorIndex]; if (!c) { return 'gray'; } // Custom planets (and any color that names a registry block) share // the preloaded Blocks.images() sprite, so late-loaded customs and // opponent snapshots do not need a second preload. if (c.block) { return 'block:' + c.block; } return (planet.id || 'planet') + '-' + (c.name || String(colorIndex)); } /** * Jump height in tiles for a match line. * @param {object} planet * @param {number} matchLength number of blocks in the line (>= 3) * @param {boolean} vertical true for a vertical line */ function jumpHeight (planet, matchLength, vertical) { var extra = Math.max(0, (matchLength | 0) - 3); var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; return planet.baseJumpHeight * multiplier; } /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ function launchVelocity (planet, height) { if (height <= 0) { return 0; } return (2 * height) / planet.riseTime; } /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ function launchDeceleration (planet, height) { if (height <= 0) { return 0; } return (2 * height) / (planet.riseTime * planet.riseTime); } /** * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower * chance that a shot-off block misses the opponent). */ function power (planet) { // t1199: scale kept on the old 1..4 axis on purpose, so unchanged // planets keep their Power/miss chance; the 2.0 minimum means the // gravity term is now at least 1/3 (lowest possible Power ≈ 0.11). var gravity = (planet.jumpsToClear - 1) / 3; var air = (5 - planet.fallTime) / 4; var mass = (3 - planet.riseTime) / 2.5; return (gravity + air + mass) / 3; } /** Maximum miss chance, used by the gameplay formula and the displayed Power. */ var MAX_MISS_CHANCE = 0.60; var MIN_MISS_CHANCE = 0.05; /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ function missChance (planet) { return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet); } /** * Displayed Power, as a 0–100 percentage. This is the complement of the * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss * → 0% Power. The gameplay power() and missChance() formulas are unchanged. */ function displayPower (planet) { return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100); } /** * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), * launch force (N), and displayed Power as a percentage. The numbers do * not have to explain themselves. */ function displayValues (planet) { var gravity = gravityG(planet.jumpsToClear); var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; return { gravity: gravity, gravityPct: gravityPercent(planet.jumpsToClear), density: density, newtons: newtons, power: displayPower(planet), missChance: missChance(planet) }; } var Planet = { FIELD_WIDTH: FIELD_WIDTH, FIELD_HEIGHT: FIELD_HEIGHT, JUMP_HEIGHT_SCALE: JUMP_HEIGHT_SCALE, DESCENT_SPEED_SCALE: DESCENT_SPEED_SCALE, COLOR_COUNT: 5, SETTLE_TIME: 3, SPAWN_INTERVAL: 1, JUMP_VERTICAL_BONUS: 1.2, JUMP_EXTRA_BLOCK: 0.05, GRAVITY_MIN: GRAVITY_MIN, GRAVITY_MAX: GRAVITY_MAX, EARTH_G: EARTH_G, gravityPercent: gravityPercent, gravityG: gravityG, formatGravity: formatGravity, createPlanet: createPlanet, defaultPlanet: defaultPlanet, defaultColors: defaultColors, allPlanets: allPlanets, officialPlanets: officialPlanets, customPlanets: customPlanets, pickerPlanets: pickerPlanets, setCustomPlanets: setCustomPlanets, rememberPlanet: rememberPlanet, forgetRemembered: forgetRemembered, planetById: planetById, planetFromData: planetFromData, formatStats: formatStats, spriteKey: spriteKey, jumpHeight: jumpHeight, launchVelocity: launchVelocity, launchDeceleration: launchDeceleration, MAX_MISS_CHANCE: MAX_MISS_CHANCE, MIN_MISS_CHANCE: MIN_MISS_CHANCE, power: power, missChance: missChance, displayPower: displayPower, displayValues: displayValues }; if (typeof module !== 'undefined' && module.exports) { module.exports = Planet; } else if (typeof globalThis !== 'undefined') { globalThis.Planet = Planet; }