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