| 1 | /* |
| 2 | * AUTO-GENERATED FILE — DO NOT EDIT BY HAND. |
| 3 | * Built by src/build.sh from the sources in src/. |
| 4 | */ |
| 5 | var PLANET_CATALOG = { |
| 6 | "planets": [ |
| 7 | { |
| 8 | "id": "terra", |
| 9 | "name": "Terra", |
| 10 | "people": "Terrans", |
| 11 | "jumpsToClear": 2, |
| 12 | "fallTime": 3, |
| 13 | "riseTime": 1.5, |
| 14 | "colors": [ |
| 15 | {"id": 0, "name": "ember", "hex": "#e85d3a", "shape": "triangle", "image": "images/block-terra-ember.png"}, |
| 16 | {"id": 1, "name": "aqua", "hex": "#3db8e8", "shape": "circle", "image": "images/block-terra-aqua.png"}, |
| 17 | {"id": 2, "name": "leaf", "hex": "#4cbf4c", "shape": "square", "image": "images/block-terra-leaf.png"}, |
| 18 | {"id": 3, "name": "sun", "hex": "#f0c53a", "shape": "diamond", "image": "images/block-terra-sun.png"}, |
| 19 | {"id": 4, "name": "orchid", "hex": "#c45ad4", "shape": "star", "image": "images/block-terra-orchid.png"} |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "id": "aurel", |
| 24 | "name": "Aurel", |
| 25 | "people": "Aurelians", |
| 26 | "jumpsToClear": 1, |
| 27 | "fallTime": 5, |
| 28 | "riseTime": 3, |
| 29 | "colors": [ |
| 30 | {"id": 0, "name": "gold", "hex": "#f4c860", "shape": "hexagon", "image": "images/block-aurel-gold.png"}, |
| 31 | {"id": 1, "name": "cream", "hex": "#f6e7c1", "shape": "circle", "image": "images/block-aurel-cream.png"}, |
| 32 | {"id": 2, "name": "amber", "hex": "#e09a3e", "shape": "teardrop", "image": "images/block-aurel-amber.png"}, |
| 33 | {"id": 3, "name": "sage", "hex": "#8fb86f", "shape": "plus", "image": "images/block-aurel-sage.png"}, |
| 34 | {"id": 4, "name": "rose", "hex": "#e8a0a8", "shape": "pentagon", "image": "images/block-aurel-rose.png"} |
| 35 | ] |
| 36 | }, |
| 37 | { |
| 38 | "id": "cinder", |
| 39 | "name": "Cinder", |
| 40 | "people": "Cindari", |
| 41 | "jumpsToClear": 4, |
| 42 | "fallTime": 1, |
| 43 | "riseTime": 0.5, |
| 44 | "colors": [ |
| 45 | {"id": 0, "name": "magma", "hex": "#ff3b1f", "shape": "triangle", "image": "images/block-cinder-magma.png"}, |
| 46 | {"id": 1, "name": "coal", "hex": "#3a3232", "shape": "square", "image": "images/block-cinder-coal.png"}, |
| 47 | {"id": 2, "name": "spark", "hex": "#ffb347", "shape": "diamond", "image": "images/block-cinder-spark.png"}, |
| 48 | {"id": 3, "name": "ash", "hex": "#8a7a76", "shape": "circle", "image": "images/block-cinder-ash.png"}, |
| 49 | {"id": 4, "name": "core", "hex": "#ff6a00", "shape": "star", "image": "images/block-cinder-core.png"} |
| 50 | ] |
| 51 | }, |
| 52 | { |
| 53 | "id": "lunara", |
| 54 | "name": "Lunara", |
| 55 | "people": "Lunarites", |
| 56 | "jumpsToClear": 1, |
| 57 | "fallTime": 2, |
| 58 | "riseTime": 1.2, |
| 59 | "colors": [ |
| 60 | {"id": 0, "name": "silver", "hex": "#c5d0e0", "shape": "circle", "image": "images/block-lunara-silver.png"}, |
| 61 | {"id": 1, "name": "dust", "hex": "#8b90a0", "shape": "diamond", "image": "images/block-lunara-dust.png"}, |
| 62 | {"id": 2, "name": "pale", "hex": "#e8d9a8", "shape": "crescent", "image": "images/block-lunara-pale.png"}, |
| 63 | {"id": 3, "name": "void", "hex": "#2c3348", "shape": "square", "image": "images/block-lunara-void.png"}, |
| 64 | {"id": 4, "name": "lilac", "hex": "#b9a4d4", "shape": "star", "image": "images/block-lunara-lilac.png"} |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "id": "mire", |
| 69 | "name": "Mire", |
| 70 | "people": "Mirians", |
| 71 | "jumpsToClear": 2, |
| 72 | "fallTime": 5, |
| 73 | "riseTime": 2.5, |
| 74 | "colors": [ |
| 75 | {"id": 0, "name": "moss", "hex": "#3d7a45", "shape": "hexagon", "image": "images/block-mire-moss.png"}, |
| 76 | {"id": 1, "name": "bog", "hex": "#1f4d3a", "shape": "teardrop", "image": "images/block-mire-bog.png"}, |
| 77 | {"id": 2, "name": "rust", "hex": "#8b5a2b", "shape": "pentagon", "image": "images/block-mire-rust.png"}, |
| 78 | {"id": 3, "name": "spore", "hex": "#9cbf5a", "shape": "circle", "image": "images/block-mire-spore.png"}, |
| 79 | {"id": 4, "name": "murk", "hex": "#2e3b2f", "shape": "diamond", "image": "images/block-mire-murk.png"} |
| 80 | ] |
| 81 | }, |
| 82 | { |
| 83 | "id": "galeon", |
| 84 | "name": "Galeon", |
| 85 | "people": "Galeoni", |
| 86 | "jumpsToClear": 3, |
| 87 | "fallTime": 1, |
| 88 | "riseTime": 1.8, |
| 89 | "colors": [ |
| 90 | {"id": 0, "name": "sky", "hex": "#4aa3ff", "shape": "arrow", "image": "images/block-galeon-sky.png"}, |
| 91 | {"id": 1, "name": "cloud", "hex": "#e8eef6", "shape": "circle", "image": "images/block-galeon-cloud.png"}, |
| 92 | {"id": 2, "name": "storm", "hex": "#3d4d6e", "shape": "square", "image": "images/block-galeon-storm.png"}, |
| 93 | {"id": 3, "name": "sand", "hex": "#d4b483", "shape": "diamond","image": "images/block-galeon-sand.png"}, |
| 94 | {"id": 4, "name": "kite", "hex": "#7fd0c0", "shape": "star", "image": "images/block-galeon-kite.png"} |
| 95 | ] |
| 96 | }, |
| 97 | { |
| 98 | "id": "obsid", |
| 99 | "name": "Obsid", |
| 100 | "people": "Obsidians", |
| 101 | "jumpsToClear": 4, |
| 102 | "fallTime": 3.5, |
| 103 | "riseTime": 2.2, |
| 104 | "colors": [ |
| 105 | {"id": 0, "name": "glass", "hex": "#1a1a22", "shape": "gem", "image": "images/block-obsid-glass.png"}, |
| 106 | {"id": 1, "name": "violet","hex": "#5a3d8a", "shape": "hexagon", "image": "images/block-obsid-violet.png"}, |
| 107 | {"id": 2, "name": "shard", "hex": "#8a8aa0", "shape": "triangle", "image": "images/block-obsid-shard.png"}, |
| 108 | {"id": 3, "name": "vein", "hex": "#c45a3a", "shape": "diamond", "image": "images/block-obsid-vein.png"}, |
| 109 | {"id": 4, "name": "basalt","hex": "#3a3a42", "shape": "square", "image": "images/block-obsid-basalt.png"} |
| 110 | ] |
| 111 | }, |
| 112 | { |
| 113 | "id": "wisp", |
| 114 | "name": "Wisp", |
| 115 | "people": "Wisplings", |
| 116 | "jumpsToClear": 2, |
| 117 | "fallTime": 3.5, |
| 118 | "riseTime": 0.5, |
| 119 | "colors": [ |
| 120 | {"id": 0, "name": "glow", "hex": "#c8ffe0", "shape": "circle", "image": "images/block-wisp-glow.png"}, |
| 121 | {"id": 1, "name": "mist", "hex": "#a8d4e8", "shape": "diamond", "image": "images/block-wisp-mist.png"}, |
| 122 | {"id": 2, "name": "sparkle", "hex": "#fff6a8", "shape": "star", "image": "images/block-wisp-sparkle.png"}, |
| 123 | {"id": 3, "name": "veil", "hex": "#d0b8f0", "shape": "teardrop", "image": "images/block-wisp-veil.png"}, |
| 124 | {"id": 4, "name": "gleam", "hex": "#f4f7ff", "shape": "plus", "image": "images/block-wisp-gleam.png"} |
| 125 | ] |
| 126 | }, |
| 127 | { |
| 128 | "id": "thorn", |
| 129 | "name": "Thorn", |
| 130 | "people": "Thornfolk", |
| 131 | "jumpsToClear": 3, |
| 132 | "fallTime": 2, |
| 133 | "riseTime": 1.0, |
| 134 | "colors": [ |
| 135 | {"id": 0, "name": "briar", "hex": "#5a7a30", "shape": "triangle", "image": "images/block-thorn-briar.png"}, |
| 136 | {"id": 1, "name": "berry", "hex": "#8b1a4a", "shape": "circle", "image": "images/block-thorn-berry.png"}, |
| 137 | {"id": 2, "name": "bark", "hex": "#5a3a20", "shape": "square", "image": "images/block-thorn-bark.png"}, |
| 138 | {"id": 3, "name": "petal", "hex": "#e89ab0", "shape": "diamond", "image": "images/block-thorn-petal.png"}, |
| 139 | {"id": 4, "name": "gold", "hex": "#c8a030", "shape": "star", "image": "images/block-thorn-gold.png"} |
| 140 | ] |
| 141 | }, |
| 142 | { |
| 143 | "id": "zephyr", |
| 144 | "name": "Zephyr", |
| 145 | "people": "Zephyrites", |
| 146 | "jumpsToClear": 1, |
| 147 | "fallTime": 1.2, |
| 148 | "riseTime": 2.8, |
| 149 | "colors": [ |
| 150 | {"id": 0, "name": "azure", "hex": "#6ec8ff", "shape": "crescent", "image": "images/block-zephyr-azure.png"}, |
| 151 | {"id": 1, "name": "breeze","hex": "#b8e8ff", "shape": "circle", "image": "images/block-zephyr-breeze.png"}, |
| 152 | {"id": 2, "name": "cirrus","hex": "#f0f7ff", "shape": "plus", "image": "images/block-zephyr-cirrus.png"}, |
| 153 | {"id": 3, "name": "dusk", "hex": "#7a6ab0", "shape": "diamond", "image": "images/block-zephyr-dusk.png"}, |
| 154 | {"id": 4, "name": "haze", "hex": "#ffe08a", "shape": "star", "image": "images/block-zephyr-haze.png"} |
| 155 | ] |
| 156 | }, |
| 157 | { |
| 158 | "id": "frore", |
| 159 | "name": "Frore", |
| 160 | "people": "Frorians", |
| 161 | "jumpsToClear": 3, |
| 162 | "fallTime": 4.5, |
| 163 | "riseTime": 0.8, |
| 164 | "colors": [ |
| 165 | {"id": 0, "name": "ice", "hex": "#a8d8f0", "shape": "hexagon", "image": "images/block-frore-ice.png"}, |
| 166 | {"id": 1, "name": "frost", "hex": "#eef7ff", "shape": "circle", "image": "images/block-frore-frost.png"}, |
| 167 | {"id": 2, "name": "deep", "hex": "#3a6a8a", "shape": "diamond", "image": "images/block-frore-deep.png"}, |
| 168 | {"id": 3, "name": "aurora", "hex": "#5ae8c0", "shape": "star", "image": "images/block-frore-aurora.png"}, |
| 169 | {"id": 4, "name": "rime", "hex": "#d0e4f0", "shape": "triangle", "image": "images/block-frore-rime.png"} |
| 170 | ] |
| 171 | }, |
| 172 | { |
| 173 | "id": "forge", |
| 174 | "name": "Forge", |
| 175 | "people": "Forgelings", |
| 176 | "jumpsToClear": 4, |
| 177 | "fallTime": 2, |
| 178 | "riseTime": 1.5, |
| 179 | "colors": [ |
| 180 | {"id": 0, "name": "iron", "hex": "#6a6a72", "shape": "square", "image": "images/block-forge-iron.png"}, |
| 181 | {"id": 1, "name": "slag", "hex": "#3a3028", "shape": "hexagon", "image": "images/block-forge-slag.png"}, |
| 182 | {"id": 2, "name": "spark", "hex": "#ff7a1a", "shape": "triangle", "image": "images/block-forge-spark.png"}, |
| 183 | {"id": 3, "name": "brass", "hex": "#c8a048", "shape": "diamond", "image": "images/block-forge-brass.png"}, |
| 184 | {"id": 4, "name": "soot", "hex": "#2a2a28", "shape": "circle", "image": "images/block-forge-soot.png"} |
| 185 | ] |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | ; |
| 190 | |
| 191 | /* |
| 192 | * Start File: |
| 193 | * /workspace/meteors/src/rng.js |
| 194 | */ |
| 195 | 'use strict'; |
| 196 | |
| 197 | /** |
| 198 | * Seedable mulberry32 RNG, so the game core stays deterministic in tests |
| 199 | * and (later) for multiplayer replays. |
| 200 | */ |
| 201 | function createRng (seed) |
| 202 | { |
| 203 | var a = (seed >>> 0) || 1; |
| 204 | return function rng () |
| 205 | { |
| 206 | a |= 0; |
| 207 | a = a + 0x6D2B79F5 | 0; |
| 208 | var t = Math.imul(a ^ a >>> 15, 1 | a); |
| 209 | t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; |
| 210 | return ((t ^ t >>> 14) >>> 0) / 4294967296; |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | function pickIndex (rng, count) |
| 215 | { |
| 216 | var n = count | 0; |
| 217 | if (n <= 0) |
| 218 | { return 0; } |
| 219 | return Math.floor(rng() * n) % n; |
| 220 | } |
| 221 | |
| 222 | function pickFrom (rng, list) |
| 223 | { |
| 224 | if (!list || !list.length) |
| 225 | { return undefined; } |
| 226 | return list[pickIndex(rng, list.length)]; |
| 227 | } |
| 228 | |
| 229 | var Rng = { |
| 230 | create: createRng, |
| 231 | pickIndex: pickIndex, |
| 232 | pickFrom: pickFrom |
| 233 | }; |
| 234 | |
| 235 | if (typeof module !== 'undefined' && module.exports) |
| 236 | { module.exports = Rng; } |
| 237 | else if (typeof globalThis !== 'undefined') |
| 238 | { globalThis.Rng = Rng; } |
| 239 | |
| 240 | /* |
| 241 | * End File: |
| 242 | * /workspace/meteors/src/rng.js |
| 243 | */ |
| 244 | |
| 245 | /* |
| 246 | * Start File: |
| 247 | * /workspace/meteors/src/planet.js |
| 248 | */ |
| 249 | 'use strict'; |
| 250 | |
| 251 | /** |
| 252 | * Planet physics and visuals for Meteors. |
| 253 | * |
| 254 | * Stage 1 uses a single default planet. Stage 3 lets the player pick a |
| 255 | * planet; the numbers and jump-height rules are already applied here so |
| 256 | * that later planets only need new data, not new physics code. |
| 257 | * |
| 258 | * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump). |
| 259 | * A horizontal match of 3 jumps JUMP_HEIGHT_SCALE * FIELD_HEIGHT |
| 260 | * / jumpsToClear tiles (JUMP_HEIGHT_SCALE is 1.5 since t1119: the |
| 261 | * minimum jump is 50% higher). At gravity 1 that is 19.5 tiles, so |
| 262 | * one jump from the floor clears the 13-tile field with room to spare; |
| 263 | * at 4 you still need more than one jump, but each is 50% taller. |
| 264 | * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). |
| 265 | * Spawn, unsupported and cargo blocks fall at FIELD_HEIGHT / fallTime |
| 266 | * tiles/s. After a jump peaks, a flying cluster floats down at |
| 267 | * DESCENT_SPEED_SCALE * that speed (0.5 since t1122 / t1124: only the |
| 268 | * cluster float-down is halved). Relative differences between planets |
| 269 | * (1 / fallTime) are unchanged. Upward launch is not affected. |
| 270 | * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). |
| 271 | * Time to reach the apex, independent of jump height. |
| 272 | * |
| 273 | * Jump-height multipliers (never explained in the game): |
| 274 | * A vertical match of 3 is 120 % of a horizontal row of 3. |
| 275 | * Each extra block beyond 3 adds 5 %. |
| 276 | * |
| 277 | * Power is the average of the three difficulty axes (0 easy .. 1 hard) |
| 278 | * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at |
| 279 | * power 0 to 5 % at power 1. |
| 280 | */ |
| 281 | |
| 282 | var FIELD_WIDTH = 10; |
| 283 | var FIELD_HEIGHT = 13; |
| 284 | /** t1119: every match jump (the gravity-applied minimum and the |
| 285 | * vertical / extra-block bonuses derived from it) is 50% higher. */ |
| 286 | var JUMP_HEIGHT_SCALE = 1.5; |
| 287 | /** t1122 / t1124: only a flying cluster's post-apex float-down |
| 288 | * (terminal speed and the apex ease into it) is halved. Spawn fall, |
| 289 | * unsupported / released singles and dropped cargo use the full |
| 290 | * fallSpeed. Upward launch is unchanged. */ |
| 291 | var DESCENT_SPEED_SCALE = 0.5; |
| 292 | |
| 293 | |
| 294 | function clamp (value, lo, hi) |
| 295 | { |
| 296 | return Math.max(lo, Math.min(hi, value)); |
| 297 | } |
| 298 | |
| 299 | function defaultColors () |
| 300 | { |
| 301 | return [ |
| 302 | { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, |
| 303 | { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, |
| 304 | { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, |
| 305 | { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, |
| 306 | { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } |
| 307 | ]; |
| 308 | } |
| 309 | |
| 310 | function createPlanet (opts) |
| 311 | { |
| 312 | opts = opts || {}; |
| 313 | var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4); |
| 314 | var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); |
| 315 | var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); |
| 316 | var colors = opts.colors || defaultColors(); |
| 317 | var planet = { |
| 318 | id: opts.id || null, |
| 319 | name: opts.name || 'Terra', |
| 320 | people: opts.people || 'Terrans', |
| 321 | jumpsToClear: jumpsToClear, |
| 322 | fallTime: fallTime, |
| 323 | riseTime: riseTime, |
| 324 | colors: colors, |
| 325 | baseJumpHeight: JUMP_HEIGHT_SCALE * FIELD_HEIGHT / jumpsToClear, |
| 326 | // Single-block fall (spawn, unsupported, dropped cargo). |
| 327 | fallSpeed: FIELD_HEIGHT / fallTime, |
| 328 | // Time to ease from vy=0 at the apex to terminal speed. |
| 329 | // Shorter when air resistance is low (small fallTime). |
| 330 | apexEaseTime: 0.15 * fallTime, |
| 331 | fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime), |
| 332 | // t1122 / t1124: flying-cluster float-down only. Same ease |
| 333 | // duration; accel scales with the halved terminal speed. |
| 334 | clusterFallSpeed: DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime, |
| 335 | clusterFallAccel: (DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime) / (0.15 * fallTime), |
| 336 | // Visual landing bounce: faster at low air resistance. Stage-3 params. |
| 337 | bounceDuration: 0.12 * fallTime, |
| 338 | bounceSquash: 0.16 |
| 339 | }; |
| 340 | return planet; |
| 341 | } |
| 342 | |
| 343 | function loadCatalog () |
| 344 | { |
| 345 | if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets) |
| 346 | { return PLANET_CATALOG; } |
| 347 | if (typeof require !== 'undefined') |
| 348 | { |
| 349 | try |
| 350 | { return require('../server/lib/planets.json'); } |
| 351 | catch (e) {} |
| 352 | } |
| 353 | return { planets: [] }; |
| 354 | } |
| 355 | |
| 356 | function planetFromData (data) |
| 357 | { |
| 358 | var planet = createPlanet({ |
| 359 | name: data.name, |
| 360 | people: data.people, |
| 361 | jumpsToClear: data.jumpsToClear, |
| 362 | fallTime: data.fallTime, |
| 363 | riseTime: data.riseTime, |
| 364 | colors: data.colors |
| 365 | }); |
| 366 | planet.id = data.id; |
| 367 | return planet; |
| 368 | } |
| 369 | |
| 370 | function allPlanets () |
| 371 | { |
| 372 | var raw = loadCatalog().planets || []; |
| 373 | var out = []; |
| 374 | for (var i = 0; i < raw.length; i++) |
| 375 | { out.push(planetFromData(raw[i])); } |
| 376 | if (out.length === 0) |
| 377 | { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; } |
| 378 | return out; |
| 379 | } |
| 380 | |
| 381 | function planetById (id) |
| 382 | { |
| 383 | var all = allPlanets(); |
| 384 | for (var i = 0; i < all.length; i++) |
| 385 | { |
| 386 | if (all[i].id === id) |
| 387 | { return all[i]; } |
| 388 | } |
| 389 | return defaultPlanet(); |
| 390 | } |
| 391 | |
| 392 | function defaultPlanet () |
| 393 | { |
| 394 | var all = allPlanets(); |
| 395 | for (var i = 0; i < all.length; i++) |
| 396 | { |
| 397 | if (all[i].id === 'terra') |
| 398 | { return all[i]; } |
| 399 | } |
| 400 | return all[0]; |
| 401 | } |
| 402 | |
| 403 | function formatStats (planet) |
| 404 | { |
| 405 | var d = displayValues(planet); |
| 406 | return 'g ' + d.gravity.toFixed(2) |
| 407 | + ' d ' + d.density.toFixed(2) |
| 408 | + ' F ' + Math.round(d.newtons) + ' N' |
| 409 | + ' Power ' + d.power + '%'; |
| 410 | } |
| 411 | |
| 412 | function spriteKey (planet, colorIndex) |
| 413 | { |
| 414 | var colors = planet && planet.colors; |
| 415 | var c = colors && colors[colorIndex]; |
| 416 | if (!c) |
| 417 | { return 'gray'; } |
| 418 | return (planet.id || 'planet') + '-' + (c.name || String(colorIndex)); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Jump height in tiles for a match line. |
| 423 | * @param {object} planet |
| 424 | * @param {number} matchLength number of blocks in the line (>= 3) |
| 425 | * @param {boolean} vertical true for a vertical line |
| 426 | */ |
| 427 | function jumpHeight (planet, matchLength, vertical) |
| 428 | { |
| 429 | var extra = Math.max(0, (matchLength | 0) - 3); |
| 430 | var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; |
| 431 | return planet.baseJumpHeight * multiplier; |
| 432 | } |
| 433 | |
| 434 | /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ |
| 435 | function launchVelocity (planet, height) |
| 436 | { |
| 437 | if (height <= 0) |
| 438 | { return 0; } |
| 439 | return (2 * height) / planet.riseTime; |
| 440 | } |
| 441 | |
| 442 | /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ |
| 443 | function launchDeceleration (planet, height) |
| 444 | { |
| 445 | if (height <= 0) |
| 446 | { return 0; } |
| 447 | return (2 * height) / (planet.riseTime * planet.riseTime); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower |
| 452 | * chance that a shot-off block misses the opponent). |
| 453 | */ |
| 454 | function power (planet) |
| 455 | { |
| 456 | var gravity = (planet.jumpsToClear - 1) / 3; |
| 457 | var air = (5 - planet.fallTime) / 4; |
| 458 | var mass = (3 - planet.riseTime) / 2.5; |
| 459 | return (gravity + air + mass) / 3; |
| 460 | } |
| 461 | |
| 462 | /** Maximum miss chance, used by the gameplay formula and the displayed Power. */ |
| 463 | var MAX_MISS_CHANCE = 0.60; |
| 464 | var MIN_MISS_CHANCE = 0.05; |
| 465 | |
| 466 | /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ |
| 467 | function missChance (planet) |
| 468 | { |
| 469 | return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Displayed Power, as a 0–100 percentage. This is the complement of the |
| 474 | * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss |
| 475 | * → 0% Power. The gameplay power() and missChance() formulas are unchanged. |
| 476 | */ |
| 477 | function displayPower (planet) |
| 478 | { |
| 479 | return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), |
| 484 | * launch force (N), and displayed Power as a percentage. The numbers do |
| 485 | * not have to explain themselves. |
| 486 | */ |
| 487 | function displayValues (planet) |
| 488 | { |
| 489 | var gravity = 9.81 * planet.jumpsToClear / 2; |
| 490 | var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; |
| 491 | var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; |
| 492 | return { |
| 493 | gravity: gravity, |
| 494 | density: density, |
| 495 | newtons: newtons, |
| 496 | power: displayPower(planet), |
| 497 | missChance: missChance(planet) |
| 498 | }; |
| 499 | } |
| 500 | |
| 501 | var Planet = { |
| 502 | FIELD_WIDTH: FIELD_WIDTH, |
| 503 | FIELD_HEIGHT: FIELD_HEIGHT, |
| 504 | JUMP_HEIGHT_SCALE: JUMP_HEIGHT_SCALE, |
| 505 | DESCENT_SPEED_SCALE: DESCENT_SPEED_SCALE, |
| 506 | COLOR_COUNT: 5, |
| 507 | SETTLE_TIME: 3, |
| 508 | SPAWN_INTERVAL: 1, |
| 509 | JUMP_VERTICAL_BONUS: 1.2, |
| 510 | JUMP_EXTRA_BLOCK: 0.05, |
| 511 | createPlanet: createPlanet, |
| 512 | defaultPlanet: defaultPlanet, |
| 513 | defaultColors: defaultColors, |
| 514 | allPlanets: allPlanets, |
| 515 | planetById: planetById, |
| 516 | planetFromData: planetFromData, |
| 517 | formatStats: formatStats, |
| 518 | spriteKey: spriteKey, |
| 519 | jumpHeight: jumpHeight, |
| 520 | launchVelocity: launchVelocity, |
| 521 | launchDeceleration: launchDeceleration, |
| 522 | MAX_MISS_CHANCE: MAX_MISS_CHANCE, |
| 523 | MIN_MISS_CHANCE: MIN_MISS_CHANCE, |
| 524 | power: power, |
| 525 | missChance: missChance, |
| 526 | displayPower: displayPower, |
| 527 | displayValues: displayValues |
| 528 | }; |
| 529 | |
| 530 | if (typeof module !== 'undefined' && module.exports) |
| 531 | { module.exports = Planet; } |
| 532 | else if (typeof globalThis !== 'undefined') |
| 533 | { globalThis.Planet = Planet; } |
| 534 | |
| 535 | /* |
| 536 | * End File: |
| 537 | * /workspace/meteors/src/planet.js |
| 538 | */ |
| 539 | |
| 540 | /* |
| 541 | * Start File: |
| 542 | * /workspace/meteors/src/easing.js |
| 543 | */ |
| 544 | 'use strict'; |
| 545 | |
| 546 | /** |
| 547 | * Easing curves matching Qt QEasingCurve defaults (the same values Tim's |
| 548 | * KWin Click Tween effect uses). Formulas and default parameters come from |
| 549 | * Qt 6's QEasingCurve and https://easings.net/. |
| 550 | * |
| 551 | * Source of the click/release defaults: |
| 552 | * /workspace/click-tween/settings.h (clickTime, releaseTime, easing names) |
| 553 | * /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0) |
| 554 | * /workspace/click-tween/README.md |
| 555 | */ |
| 556 | |
| 557 | /** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */ |
| 558 | var BACK_OVERSHOOT = 1.70158; |
| 559 | |
| 560 | /** Qt QEasingCurve default period and amplitude for Elastic curves. */ |
| 561 | var ELASTIC_PERIOD = 0.3; |
| 562 | var ELASTIC_AMPLITUDE = 1.0; |
| 563 | |
| 564 | function clamp01 (t) |
| 565 | { |
| 566 | if (t <= 0) |
| 567 | { return 0; } |
| 568 | if (t >= 1) |
| 569 | { return 1; } |
| 570 | return t; |
| 571 | } |
| 572 | |
| 573 | /** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */ |
| 574 | function easeOutBack (t) |
| 575 | { |
| 576 | t = clamp01(t); |
| 577 | var c1 = BACK_OVERSHOOT; |
| 578 | var c3 = c1 + 1; |
| 579 | var u = t - 1; |
| 580 | return 1 + c3 * u * u * u + c1 * u * u; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * easeOutElastic: decaying sine overshoot past the end value. |
| 585 | * Qt default amplitude 1, period 0.3 (equivalent to easings.net). |
| 586 | */ |
| 587 | function easeOutElastic (t) |
| 588 | { |
| 589 | t = clamp01(t); |
| 590 | if (t === 0 || t === 1) |
| 591 | { return t; } |
| 592 | var p = ELASTIC_PERIOD; |
| 593 | var s = p / 4; |
| 594 | return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1; |
| 595 | } |
| 596 | |
| 597 | /** easeOutCubic: 1 − (1−t)³. Smooth, no overshoot (easings.net). */ |
| 598 | function easeOutCubic (t) |
| 599 | { |
| 600 | t = clamp01(t); |
| 601 | var u = 1 - t; |
| 602 | return 1 - u * u * u; |
| 603 | } |
| 604 | |
| 605 | function tween (from, to, t, ease) |
| 606 | { |
| 607 | return from + (to - from) * ease(clamp01(t)); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Visual-only block swap tween (t1121). Game logic and the grid update |
| 612 | * immediately; this only moves the displaced sprite. 100 ms sits in the |
| 613 | * requested 80–120 ms range. |
| 614 | */ |
| 615 | var SWAP_TWEEN = { |
| 616 | duration: 0.100, |
| 617 | ease: easeOutCubic |
| 618 | }; |
| 619 | |
| 620 | /** |
| 621 | * DEFAULT settings of Tim's KWin Click Tween |
| 622 | * (git.timfalken.com/timfa/click-tween). |
| 623 | * |
| 624 | * Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held. |
| 625 | * Release: grow back to 100% in 720 ms with easeOutElastic. |
| 626 | * Mid-animation release starts from the current scale, like QVariantAnimation. |
| 627 | */ |
| 628 | var CLICK_TWEEN = { |
| 629 | pressScale: 0.8, |
| 630 | releaseScale: 1.0, |
| 631 | pressDuration: 0.150, |
| 632 | releaseDuration: 0.720, |
| 633 | pressEase: easeOutBack, |
| 634 | releaseEase: easeOutElastic, |
| 635 | backOvershoot: BACK_OVERSHOOT, |
| 636 | elasticPeriod: ELASTIC_PERIOD, |
| 637 | elasticAmplitude: ELASTIC_AMPLITUDE, |
| 638 | source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)' |
| 639 | }; |
| 640 | |
| 641 | /** |
| 642 | * Visual landing bounce, u in 0..1. Subtle squash with a slight stretch |
| 643 | * on the other axis and a tiny upward rebound (in tiles, visual only). |
| 644 | */ |
| 645 | function bouncePose (u, squash) |
| 646 | { |
| 647 | if (u <= 0 || u >= 1) |
| 648 | { return { sx: 1, sy: 1, dy: 0 }; } |
| 649 | var wave = Math.sin(Math.PI * u); |
| 650 | return { |
| 651 | sx: 1 + squash * 0.6 * wave, |
| 652 | sy: 1 - squash * wave, |
| 653 | dy: 0.07 * wave * (1 - u) |
| 654 | }; |
| 655 | } |
| 656 | |
| 657 | var Easing = { |
| 658 | BACK_OVERSHOOT: BACK_OVERSHOOT, |
| 659 | ELASTIC_PERIOD: ELASTIC_PERIOD, |
| 660 | ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE, |
| 661 | easeOutBack: easeOutBack, |
| 662 | easeOutElastic: easeOutElastic, |
| 663 | easeOutCubic: easeOutCubic, |
| 664 | tween: tween, |
| 665 | CLICK_TWEEN: CLICK_TWEEN, |
| 666 | SWAP_TWEEN: SWAP_TWEEN, |
| 667 | clamp01: clamp01, |
| 668 | bouncePose: bouncePose |
| 669 | }; |
| 670 | |
| 671 | if (typeof module !== 'undefined' && module.exports) |
| 672 | { module.exports = Easing; } |
| 673 | else if (typeof globalThis !== 'undefined') |
| 674 | { globalThis.Easing = Easing; } |
| 675 | |
| 676 | /* |
| 677 | * End File: |
| 678 | * /workspace/meteors/src/easing.js |
| 679 | */ |
| 680 | |
| 681 | /* |
| 682 | * Start File: |
| 683 | * /workspace/meteors/src/gameCore.js |
| 684 | */ |
| 685 | 'use strict'; |
| 686 | |
| 687 | /** |
| 688 | * Deterministic Meteors game core. No DOM, no rendering, no input devices. |
| 689 | * The view layer reads this state and translates pointer events into |
| 690 | * beginDrag / moveDrag / endDrag. |
| 691 | */ |
| 692 | |
| 693 | var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined') |
| 694 | ? require('./planet.js') |
| 695 | : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet); |
| 696 | |
| 697 | var COLOR_GRAY = -1; |
| 698 | |
| 699 | var STATE_STILL = 'still'; |
| 700 | var STATE_FALLING = 'falling'; |
| 701 | var STATE_FLYING = 'flying'; |
| 702 | var STATE_SETTLING = 'settling'; |
| 703 | var STATE_LOCKED = 'locked'; |
| 704 | var STATE_MANUAL = 'manual'; |
| 705 | |
| 706 | var ALIGN_EPS = 0.15; |
| 707 | var SUBSTEP = 1 / 120; |
| 708 | var DRAG_LAUNCH_SLACK = 0.65; |
| 709 | // Upward pull (tiles past the press point) that launches an already-topmost block. |
| 710 | var LAUNCH_PULL = 0.35; |
| 711 | // Minimal accumulated vertical pointer travel (tiles) to swap one step. |
| 712 | // No hold delay: the first swap fires as soon as this distance is reached. |
| 713 | var SWAP_DISTANCE = 0.25; |
| 714 | // Fallback snap (tiles) when the press cell is empty: nearest block in the column. |
| 715 | var SNAP_Y = 0.5; |
| 716 | // Horizontal slop (tiles) for snapping to a neighbouring column on press. |
| 717 | var HORIZONTAL_SLOP = 0.35; |
| 718 | /** t1120: additive jump bonus per landing-chain step. A landing that |
| 719 | * immediately makes a match launches at (1 + 0.25 * chain) times the |
| 720 | * usual height, with chain 1, 2, 3, … → ×1.25, ×1.50, ×1.75. */ |
| 721 | var LANDING_CHAIN_BONUS = 0.25; |
| 722 | /** t1123: a column pulses red at or above this many grounded rows |
| 723 | * (10 of 13 is just over 70%). */ |
| 724 | var COLUMN_WARN_ROWS = 10; |
| 725 | |
| 726 | function UnionFind () |
| 727 | { |
| 728 | this.parent = Object.create(null); |
| 729 | } |
| 730 | |
| 731 | UnionFind.prototype.add = function (id) |
| 732 | { |
| 733 | if (this.parent[id] == null) |
| 734 | { this.parent[id] = id; } |
| 735 | }; |
| 736 | |
| 737 | UnionFind.prototype.find = function (id) |
| 738 | { |
| 739 | if (this.parent[id] !== id) |
| 740 | { this.parent[id] = this.find(this.parent[id]); } |
| 741 | return this.parent[id]; |
| 742 | }; |
| 743 | |
| 744 | UnionFind.prototype.union = function (a, b) |
| 745 | { |
| 746 | this.add(a); |
| 747 | this.add(b); |
| 748 | var ra = this.find(a); |
| 749 | var rb = this.find(b); |
| 750 | if (ra !== rb) |
| 751 | { this.parent[rb] = ra; } |
| 752 | }; |
| 753 | |
| 754 | function GameCore (planet, rng) |
| 755 | { |
| 756 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 757 | this.rng = rng || Math.random; |
| 758 | this.width = PlanetMod.FIELD_WIDTH; |
| 759 | this.height = PlanetMod.FIELD_HEIGHT; |
| 760 | this.blocks = []; |
| 761 | this.clusters = Object.create(null); |
| 762 | this.nextBlockId = 1; |
| 763 | this.nextClusterId = 1; |
| 764 | this.spawnTimer = 0; |
| 765 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 766 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 767 | this.score = 0; |
| 768 | this.state = 'playing'; |
| 769 | this.drag = null; |
| 770 | this.gameOverTime = 0; |
| 771 | this.outgoingShots = []; |
| 772 | // Stage-2 default; stage 3 replaces this with Planet.missChance (Power). |
| 773 | this.missChance = optsMissChance(arguments); |
| 774 | // t1112: invariant checker. strictInvariants throws (tests); |
| 775 | // debugInvariants records violations for the ?debug overlay. |
| 776 | this.strictInvariants = false; |
| 777 | this.debugInvariants = false; |
| 778 | this.lastInvariantViolations = []; |
| 779 | // t1121: visual-only swap events, consumed by the view each frame. |
| 780 | this.swapEvents = []; |
| 781 | // t1120: last landing-chain indicator { chain, bonusPct }, or null. |
| 782 | this.lastChainIndicator = null; |
| 783 | } |
| 784 | |
| 785 | function optsMissChance (args) |
| 786 | { |
| 787 | // GameCore(planet, rng, missChance) — third arg is optional. |
| 788 | if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2])) |
| 789 | { return Math.max(0, Math.min(1, args[2])); } |
| 790 | var planet = args[0]; |
| 791 | if (planet && typeof PlanetMod.missChance === 'function') |
| 792 | { return PlanetMod.missChance(planet); } |
| 793 | return GameCore.DEFAULT_MISS_CHANCE; |
| 794 | } |
| 795 | |
| 796 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 797 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 798 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 799 | GameCore.SNAP_Y = SNAP_Y; |
| 800 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 801 | GameCore.LANDING_CHAIN_BONUS = LANDING_CHAIN_BONUS; |
| 802 | GameCore.COLUMN_WARN_ROWS = COLUMN_WARN_ROWS; |
| 803 | GameCore.DEFAULT_MISS_CHANCE = 0.30; |
| 804 | GameCore.STATE_STILL = STATE_STILL; |
| 805 | GameCore.STATE_FALLING = STATE_FALLING; |
| 806 | GameCore.STATE_FLYING = STATE_FLYING; |
| 807 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 808 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 809 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 810 | |
| 811 | GameCore.prototype.randomColor = function () |
| 812 | { |
| 813 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 814 | }; |
| 815 | |
| 816 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 817 | { |
| 818 | var block = { |
| 819 | id: this.nextBlockId++, |
| 820 | col: col, |
| 821 | y: y, |
| 822 | color: color, |
| 823 | state: state || STATE_STILL, |
| 824 | vy: 0, |
| 825 | clusterId: null, |
| 826 | settleTime: 0, |
| 827 | flame: false, |
| 828 | pushedClusters: null, |
| 829 | manualTarget: 0, |
| 830 | manualDecel: 0, |
| 831 | manualPhase: null, |
| 832 | bouncing: false, |
| 833 | bounceAge: 0 |
| 834 | }; |
| 835 | this.blocks.push(block); |
| 836 | return block; |
| 837 | }; |
| 838 | |
| 839 | GameCore.prototype.removeBlock = function (block) |
| 840 | { |
| 841 | if (block.clusterId != null) |
| 842 | { this.leaveCluster(block); } |
| 843 | var index = this.blocks.indexOf(block); |
| 844 | if (index >= 0) |
| 845 | { this.blocks.splice(index, 1); } |
| 846 | }; |
| 847 | |
| 848 | GameCore.prototype.leaveCluster = function (block) |
| 849 | { |
| 850 | var id = block.clusterId; |
| 851 | block.clusterId = null; |
| 852 | block.flame = false; |
| 853 | if (id == null || !this.clusters[id]) |
| 854 | { return; } |
| 855 | var still = 0; |
| 856 | for (var i = 0; i < this.blocks.length; i++) |
| 857 | { |
| 858 | if (this.blocks[i].clusterId === id) |
| 859 | { still++; } |
| 860 | } |
| 861 | if (still === 0) |
| 862 | { delete this.clusters[id]; } |
| 863 | }; |
| 864 | |
| 865 | GameCore.prototype.blocksInColumn = function (col) |
| 866 | { |
| 867 | var out = []; |
| 868 | for (var i = 0; i < this.blocks.length; i++) |
| 869 | { |
| 870 | if (this.blocks[i].col === col) |
| 871 | { out.push(this.blocks[i]); } |
| 872 | } |
| 873 | out.sort(function (a, b) { return a.y - b.y; }); |
| 874 | return out; |
| 875 | }; |
| 876 | |
| 877 | GameCore.prototype.isGrounded = function (block) |
| 878 | { |
| 879 | return block.state === STATE_STILL |
| 880 | || block.state === STATE_SETTLING |
| 881 | || block.state === STATE_LOCKED; |
| 882 | }; |
| 883 | |
| 884 | /** |
| 885 | * Highest grounded landing surface in `col`. If `belowY` is given, only |
| 886 | * blocks strictly below that y count — a hanging still block above a |
| 887 | * gap must never pull a falling or thrown block upward through the gap. |
| 888 | */ |
| 889 | GameCore.prototype.stackTop = function (col, ignore, belowY) |
| 890 | { |
| 891 | var top = 0; |
| 892 | for (var i = 0; i < this.blocks.length; i++) |
| 893 | { |
| 894 | var block = this.blocks[i]; |
| 895 | if (block.col !== col) |
| 896 | { continue; } |
| 897 | if (ignore && ignore(block)) |
| 898 | { continue; } |
| 899 | if (belowY != null && block.y + 1e-9 >= belowY) |
| 900 | { continue; } |
| 901 | if (this.isGrounded(block)) |
| 902 | { |
| 903 | var surface = block.y + 1; |
| 904 | if (surface > top) |
| 905 | { top = surface; } |
| 906 | } |
| 907 | } |
| 908 | return top; |
| 909 | }; |
| 910 | |
| 911 | GameCore.prototype.isColumnLocked = function (col) |
| 912 | { |
| 913 | for (var i = 0; i < this.blocks.length; i++) |
| 914 | { |
| 915 | var block = this.blocks[i]; |
| 916 | if (block.col === col && block.state === STATE_LOCKED) |
| 917 | { return true; } |
| 918 | } |
| 919 | return false; |
| 920 | }; |
| 921 | |
| 922 | GameCore.prototype.isColumnStill = function (col) |
| 923 | { |
| 924 | if (this.isColumnLocked(col)) |
| 925 | { return false; } |
| 926 | for (var i = 0; i < this.blocks.length; i++) |
| 927 | { |
| 928 | var block = this.blocks[i]; |
| 929 | if (block.col !== col) |
| 930 | { continue; } |
| 931 | if (block.state === STATE_FLYING |
| 932 | || block.state === STATE_FALLING |
| 933 | || block.state === STATE_MANUAL |
| 934 | || block.state === STATE_SETTLING) |
| 935 | { return false; } |
| 936 | } |
| 937 | return true; |
| 938 | }; |
| 939 | |
| 940 | GameCore.prototype.groundedCount = function (col) |
| 941 | { |
| 942 | var n = 0; |
| 943 | for (var i = 0; i < this.blocks.length; i++) |
| 944 | { |
| 945 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 946 | { n++; } |
| 947 | } |
| 948 | return n; |
| 949 | }; |
| 950 | |
| 951 | GameCore.prototype.columnNeedsWarn = function (col) |
| 952 | { |
| 953 | return this.groundedCount(col) >= COLUMN_WARN_ROWS; |
| 954 | }; |
| 955 | |
| 956 | /** |
| 957 | * Which columns of a field snapshot should pulse (t1123). Flying, |
| 958 | * falling and manual (airborne) blocks do not count: they are |
| 959 | * temporary and would flicker the warning. Still, settling and |
| 960 | * locked (grounded) blocks do. |
| 961 | */ |
| 962 | GameCore.warnColumnsFromSnapshot = function (snapshot, threshold) |
| 963 | { |
| 964 | threshold = threshold == null ? COLUMN_WARN_ROWS : threshold; |
| 965 | var width = (snapshot && snapshot.width) || 10; |
| 966 | var flags = []; |
| 967 | var counts = []; |
| 968 | for (var c = 0; c < width; c++) |
| 969 | { |
| 970 | counts[c] = 0; |
| 971 | flags[c] = false; |
| 972 | } |
| 973 | var blocks = (snapshot && snapshot.blocks) || []; |
| 974 | for (var i = 0; i < blocks.length; i++) |
| 975 | { |
| 976 | var b = blocks[i]; |
| 977 | if (b.col < 0 || b.col >= width) |
| 978 | { continue; } |
| 979 | if (b.state === 'still' || b.state === 'settling' || b.state === 'locked') |
| 980 | { counts[b.col]++; } |
| 981 | } |
| 982 | for (var c = 0; c < width; c++) |
| 983 | { flags[c] = counts[c] >= threshold; } |
| 984 | return flags; |
| 985 | }; |
| 986 | |
| 987 | GameCore.columnWarnPulse = function (timeSec) |
| 988 | { |
| 989 | // Smooth sine, ~0.5 Hz, alpha 0.16–0.44. Shared by the player |
| 990 | // field and opponent / bot minis so they pulse in sync. |
| 991 | var t = timeSec; |
| 992 | if (t == null) |
| 993 | { |
| 994 | t = (typeof performance !== 'undefined' && performance.now) |
| 995 | ? performance.now() / 1000 |
| 996 | : Date.now() / 1000; |
| 997 | } |
| 998 | return 0.16 + 0.28 * (0.5 + 0.5 * Math.sin(t * Math.PI * 2 * 0.55)); |
| 999 | }; |
| 1000 | |
| 1001 | GameCore.prototype.blockAt = function (col, row) |
| 1002 | { |
| 1003 | for (var i = 0; i < this.blocks.length; i++) |
| 1004 | { |
| 1005 | var block = this.blocks[i]; |
| 1006 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 1007 | { return block; } |
| 1008 | } |
| 1009 | return null; |
| 1010 | }; |
| 1011 | |
| 1012 | GameCore.prototype.place = function (col, row, color, state) |
| 1013 | { |
| 1014 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 1015 | }; |
| 1016 | |
| 1017 | GameCore.prototype.lockedColumnCount = function () |
| 1018 | { |
| 1019 | var n = 0; |
| 1020 | for (var c = 0; c < this.width; c++) |
| 1021 | { |
| 1022 | if (this.isColumnLocked(c)) |
| 1023 | { n++; } |
| 1024 | } |
| 1025 | return n; |
| 1026 | }; |
| 1027 | |
| 1028 | GameCore.prototype.startBounce = function (block) |
| 1029 | { |
| 1030 | if (!block) |
| 1031 | { return; } |
| 1032 | block.bouncing = true; |
| 1033 | block.bounceAge = 0; |
| 1034 | }; |
| 1035 | |
| 1036 | GameCore.prototype.advanceBounces = function (dt) |
| 1037 | { |
| 1038 | var duration = this.planet.bounceDuration; |
| 1039 | for (var i = 0; i < this.blocks.length; i++) |
| 1040 | { |
| 1041 | var block = this.blocks[i]; |
| 1042 | if (!block.bouncing) |
| 1043 | { continue; } |
| 1044 | block.bounceAge += dt; |
| 1045 | if (duration <= 0 || block.bounceAge >= duration) |
| 1046 | { |
| 1047 | block.bouncing = false; |
| 1048 | block.bounceAge = duration; |
| 1049 | } |
| 1050 | } |
| 1051 | }; |
| 1052 | |
| 1053 | /** |
| 1054 | * True if `block` rests on the floor or on a solid (still / settling / |
| 1055 | * locked) block immediately below it in the same column. |
| 1056 | */ |
| 1057 | GameCore.prototype.hasSupport = function (block) |
| 1058 | { |
| 1059 | if (block.y <= 1e-9) |
| 1060 | { return true; } |
| 1061 | for (var i = 0; i < this.blocks.length; i++) |
| 1062 | { |
| 1063 | var other = this.blocks[i]; |
| 1064 | if (other === block || other.col !== block.col) |
| 1065 | { continue; } |
| 1066 | if (!this.isGrounded(other)) |
| 1067 | { continue; } |
| 1068 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 1069 | { return true; } |
| 1070 | } |
| 1071 | return false; |
| 1072 | }; |
| 1073 | |
| 1074 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 1075 | GameCore.prototype.highestFlyingBelow = function (block) |
| 1076 | { |
| 1077 | var best = null; |
| 1078 | for (var i = 0; i < this.blocks.length; i++) |
| 1079 | { |
| 1080 | var other = this.blocks[i]; |
| 1081 | if (other === block || other.col !== block.col) |
| 1082 | { continue; } |
| 1083 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1084 | { continue; } |
| 1085 | var cluster = this.clusters[other.clusterId]; |
| 1086 | if (!cluster || cluster.phase === 'landed') |
| 1087 | { continue; } |
| 1088 | // The falling block is at or above this flyer (or already overlapping). |
| 1089 | if (block.y + 1e-9 < other.y) |
| 1090 | { continue; } |
| 1091 | if (!best || other.y > best.y) |
| 1092 | { best = other; } |
| 1093 | } |
| 1094 | return best; |
| 1095 | }; |
| 1096 | |
| 1097 | GameCore.prototype.highestFallingBelow = function (block) |
| 1098 | { |
| 1099 | var best = null; |
| 1100 | for (var i = 0; i < this.blocks.length; i++) |
| 1101 | { |
| 1102 | var other = this.blocks[i]; |
| 1103 | if (other === block || other.col !== block.col) |
| 1104 | { continue; } |
| 1105 | if (other.state !== STATE_FALLING) |
| 1106 | { continue; } |
| 1107 | if (other.y > block.y - 1e-9) |
| 1108 | { continue; } |
| 1109 | if (!best || other.y > best.y) |
| 1110 | { best = other; } |
| 1111 | } |
| 1112 | return best; |
| 1113 | }; |
| 1114 | |
| 1115 | /** |
| 1116 | * Make an unsupported still block start falling from rest, so it eases |
| 1117 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 1118 | */ |
| 1119 | /** |
| 1120 | * Strip leftover launch / manual kinematics so a block that has just |
| 1121 | * become still or falling starts from rest. Used after a landed grey |
| 1122 | * cluster recolors, and whenever an unsupported still block drops. |
| 1123 | */ |
| 1124 | GameCore.prototype.clearFlightState = function (block) |
| 1125 | { |
| 1126 | block.vy = 0; |
| 1127 | block.flame = false; |
| 1128 | block.manualPhase = null; |
| 1129 | block.manualTarget = null; |
| 1130 | block.manualDecel = 0; |
| 1131 | block.pushedClusters = null; |
| 1132 | }; |
| 1133 | |
| 1134 | GameCore.prototype.releaseUnsupported = function () |
| 1135 | { |
| 1136 | // Repeat until stable: a still sitting on a still that itself just |
| 1137 | // became falling must also drop. Two fixed passes are not enough |
| 1138 | // for a chain of hanging blocks (t1112). |
| 1139 | var guard = 0; |
| 1140 | var changed = true; |
| 1141 | while (changed && guard++ < this.height + 2) |
| 1142 | { |
| 1143 | changed = false; |
| 1144 | for (var i = 0; i < this.blocks.length; i++) |
| 1145 | { |
| 1146 | var block = this.blocks[i]; |
| 1147 | if (block.state !== STATE_STILL) |
| 1148 | { continue; } |
| 1149 | if (this.drag && this.drag.blockId === block.id) |
| 1150 | { continue; } |
| 1151 | if (this.hasSupport(block)) |
| 1152 | { continue; } |
| 1153 | this.leaveCluster(block); |
| 1154 | this.clearFlightState(block); |
| 1155 | block.state = STATE_FALLING; |
| 1156 | changed = true; |
| 1157 | } |
| 1158 | } |
| 1159 | }; |
| 1160 | |
| 1161 | GameCore.prototype.accelerateFall = function (block, dt) |
| 1162 | { |
| 1163 | var target = -this.planet.fallSpeed; |
| 1164 | if (block.vy > target) |
| 1165 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 1166 | else |
| 1167 | { block.vy = target; } |
| 1168 | }; |
| 1169 | |
| 1170 | GameCore.prototype.landFallingBlock = function (block) |
| 1171 | { |
| 1172 | block.vy = 0; |
| 1173 | block.flame = false; |
| 1174 | this.startBounce(block); |
| 1175 | if (block.color === COLOR_GRAY) |
| 1176 | { |
| 1177 | block.state = STATE_SETTLING; |
| 1178 | block.settleTime = 0; |
| 1179 | } |
| 1180 | else |
| 1181 | { block.state = STATE_STILL; } |
| 1182 | }; |
| 1183 | |
| 1184 | /** |
| 1185 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 1186 | * carry it along as cargo (no flame). Returns true if attached. |
| 1187 | */ |
| 1188 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 1189 | { |
| 1190 | var cluster = this.clusters[flyer.clusterId]; |
| 1191 | if (!cluster || cluster.phase === 'landed') |
| 1192 | { return false; } |
| 1193 | this.prepareClusterBody(cluster); |
| 1194 | var maxOff = null; |
| 1195 | var members = this.clusterMembers(cluster.id); |
| 1196 | for (var i = 0; i < members.length; i++) |
| 1197 | { |
| 1198 | if (members[i].col !== block.col) |
| 1199 | { continue; } |
| 1200 | if (maxOff == null || members[i].clusterOffset > maxOff) |
| 1201 | { maxOff = members[i].clusterOffset; } |
| 1202 | } |
| 1203 | var oldN = members.length; |
| 1204 | var fallingVy = block.vy; |
| 1205 | if (block.clusterId != null && block.clusterId !== cluster.id) |
| 1206 | { this.leaveCluster(block); } |
| 1207 | block.state = STATE_FLYING; |
| 1208 | block.clusterId = cluster.id; |
| 1209 | block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1; |
| 1210 | block.flame = false; |
| 1211 | block.settleTime = 0; |
| 1212 | block.manualPhase = null; |
| 1213 | block.manualTarget = null; |
| 1214 | block.manualDecel = 0; |
| 1215 | block.pushedClusters = null; |
| 1216 | // A cluster born from a thrown block averages in falling cargo |
| 1217 | // the same way two flying clusters merge (t1108). Other rockets |
| 1218 | // keep their velocity and just pick the cargo up. |
| 1219 | if (cluster.fromManual) |
| 1220 | { |
| 1221 | var newN = oldN + 1; |
| 1222 | cluster.vy = (cluster.vy * oldN + fallingVy) / newN; |
| 1223 | cluster.decel = ((cluster.decel || 0) * oldN) / newN; |
| 1224 | cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN; |
| 1225 | cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down'; |
| 1226 | } |
| 1227 | this.syncClusterMembers(cluster); |
| 1228 | return true; |
| 1229 | }; |
| 1230 | |
| 1231 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 1232 | { |
| 1233 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1234 | { return; } |
| 1235 | // Snapshot falling blocks; attaching mutates state. |
| 1236 | var falling = []; |
| 1237 | for (var i = 0; i < this.blocks.length; i++) |
| 1238 | { |
| 1239 | if (this.blocks[i].state === STATE_FALLING) |
| 1240 | { falling.push(this.blocks[i]); } |
| 1241 | } |
| 1242 | for (var i = 0; i < falling.length; i++) |
| 1243 | { |
| 1244 | var block = falling[i]; |
| 1245 | var flyer = null; |
| 1246 | for (var m = 0; m < members.length; m++) |
| 1247 | { |
| 1248 | var other = members[m]; |
| 1249 | if (other.col !== block.col) |
| 1250 | { continue; } |
| 1251 | // Overlap or rest on this member's top. |
| 1252 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 1253 | { |
| 1254 | if (!flyer || other.y > flyer.y) |
| 1255 | { flyer = other; } |
| 1256 | } |
| 1257 | } |
| 1258 | if (flyer) |
| 1259 | { this.attachToFlyingCluster(block, flyer); } |
| 1260 | } |
| 1261 | }; |
| 1262 | |
| 1263 | GameCore.prototype.tick = function (dt) |
| 1264 | { |
| 1265 | if (this.state === 'gameover') |
| 1266 | { |
| 1267 | this.gameOverTime += Math.max(0, dt); |
| 1268 | return; |
| 1269 | } |
| 1270 | if (dt <= 0) |
| 1271 | { return; } |
| 1272 | dt = Math.min(dt, 0.1); |
| 1273 | |
| 1274 | this.spawnTimer += dt; |
| 1275 | while (this.spawnTimer >= this.spawnInterval) |
| 1276 | { |
| 1277 | this.spawnTimer -= this.spawnInterval; |
| 1278 | this.trySpawn(); |
| 1279 | } |
| 1280 | |
| 1281 | var left = dt; |
| 1282 | while (left > 1e-12) |
| 1283 | { |
| 1284 | var step = Math.min(SUBSTEP, left); |
| 1285 | this.snapshotPrevY(); |
| 1286 | this.integrateFalling(step); |
| 1287 | this.resolveFlyingMatches(); |
| 1288 | this.integrateClusters(step); |
| 1289 | this.mergeFlyingClusters(); |
| 1290 | this.integrateManual(step); |
| 1291 | this.mergeAnyManualFallingOverlaps(); |
| 1292 | this.resolveAirborneCollisions(); |
| 1293 | this.cullOffTop(); |
| 1294 | left -= step; |
| 1295 | } |
| 1296 | |
| 1297 | this.releaseUnsupported(); |
| 1298 | this.advanceSettle(dt); |
| 1299 | // After a landed grey group is released, blocks that were sitting on |
| 1300 | // hanging members are still STILL (the earlier pass ran while those |
| 1301 | // members counted as support). Drop them from rest before matching, |
| 1302 | // so they cannot form a mid-air match and shoot off the top. |
| 1303 | this.releaseUnsupported(); |
| 1304 | this.advanceBounces(dt); |
| 1305 | if (!this.drag) |
| 1306 | { this.resolveMatches(); } |
| 1307 | this.checkLocks(); |
| 1308 | this.checkGameOver(); |
| 1309 | this.assertInvariants('tick'); |
| 1310 | }; |
| 1311 | |
| 1312 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 1313 | GameCore.prototype.step = function (seconds, dt) |
| 1314 | { |
| 1315 | dt = dt || SUBSTEP; |
| 1316 | var t = 0; |
| 1317 | while (t < seconds - 1e-15) |
| 1318 | { |
| 1319 | var s = Math.min(dt, seconds - t); |
| 1320 | this.tick(s); |
| 1321 | t += s; |
| 1322 | } |
| 1323 | }; |
| 1324 | |
| 1325 | GameCore.prototype.trySpawn = function () |
| 1326 | { |
| 1327 | if (this.state !== 'playing') |
| 1328 | { return null; } |
| 1329 | var eligible = []; |
| 1330 | for (var col = 0; col < this.width; col++) |
| 1331 | { |
| 1332 | if (this.isColumnLocked(col)) |
| 1333 | { continue; } |
| 1334 | if (this.groundedCount(col) >= this.height) |
| 1335 | { continue; } |
| 1336 | var alreadyFalling = false; |
| 1337 | var list = this.blocksInColumn(col); |
| 1338 | for (var i = 0; i < list.length; i++) |
| 1339 | { |
| 1340 | if (list[i].state === STATE_FALLING) |
| 1341 | { |
| 1342 | alreadyFalling = true; |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | if (!alreadyFalling) |
| 1347 | { eligible.push(col); } |
| 1348 | } |
| 1349 | if (!eligible.length) |
| 1350 | { return null; } |
| 1351 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 1352 | var color = this.randomColor(); |
| 1353 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 1354 | block.vy = -this.planet.fallSpeed; |
| 1355 | return block; |
| 1356 | }; |
| 1357 | |
| 1358 | GameCore.prototype.integrateFalling = function (dt) |
| 1359 | { |
| 1360 | var falling = []; |
| 1361 | for (var i = 0; i < this.blocks.length; i++) |
| 1362 | { |
| 1363 | if (this.blocks[i].state === STATE_FALLING) |
| 1364 | { falling.push(this.blocks[i]); } |
| 1365 | } |
| 1366 | // Lowest first, so a stack of falling blocks rests on itself. |
| 1367 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 1368 | for (var i = 0; i < falling.length; i++) |
| 1369 | { |
| 1370 | var block = falling[i]; |
| 1371 | if (block.state !== STATE_FALLING) |
| 1372 | { continue; } |
| 1373 | this.accelerateFall(block, dt); |
| 1374 | var nextY = block.y + block.vy * dt; |
| 1375 | var manual = this.manualInColumnHit(block, block.y, nextY); |
| 1376 | if (manual) |
| 1377 | { |
| 1378 | this.mergeManualWithFalling(manual, this.contiguousFallingInColumn(manual.col, manual, [block])); |
| 1379 | continue; |
| 1380 | } |
| 1381 | var flyer = this.highestFlyingBelow(block); |
| 1382 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 1383 | { |
| 1384 | if (this.attachToFlyingCluster(block, flyer)) |
| 1385 | { continue; } |
| 1386 | } |
| 1387 | var top = this.stackTop(block.col, null, block.y); |
| 1388 | var below = this.highestFallingBelow(block); |
| 1389 | if (below && below.y + 1 > top) |
| 1390 | { top = below.y + 1; } |
| 1391 | if (nextY <= top) |
| 1392 | { |
| 1393 | block.y = top; |
| 1394 | this.landFallingBlock(block); |
| 1395 | } |
| 1396 | else |
| 1397 | { block.y = nextY; } |
| 1398 | } |
| 1399 | }; |
| 1400 | |
| 1401 | GameCore.prototype.clusterMembers = function (clusterId) |
| 1402 | { |
| 1403 | var out = []; |
| 1404 | for (var i = 0; i < this.blocks.length; i++) |
| 1405 | { |
| 1406 | if (this.blocks[i].clusterId === clusterId) |
| 1407 | { out.push(this.blocks[i]); } |
| 1408 | } |
| 1409 | return out; |
| 1410 | }; |
| 1411 | |
| 1412 | |
| 1413 | /** |
| 1414 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 1415 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 1416 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 1417 | * upward deceleration. After the apex, vy eases from 0 to the |
| 1418 | * terminal descent speed at the matching accel, then stays there. |
| 1419 | * Flying clusters (forCluster) use the halved cluster float-down |
| 1420 | * (t1122 / t1124); manual jumps use the full single-block fallSpeed. |
| 1421 | */ |
| 1422 | GameCore.prototype.descentParams = function (forCluster) |
| 1423 | { |
| 1424 | var p = this.planet; |
| 1425 | if (forCluster) |
| 1426 | { |
| 1427 | return { |
| 1428 | fallSpeed: p.clusterFallSpeed, |
| 1429 | fallAccel: p.clusterFallAccel |
| 1430 | }; |
| 1431 | } |
| 1432 | return { fallSpeed: p.fallSpeed, fallAccel: p.fallAccel }; |
| 1433 | }; |
| 1434 | |
| 1435 | GameCore.prototype.advanceJump = function (body, dt, forCluster) |
| 1436 | { |
| 1437 | var dy = 0; |
| 1438 | var left = dt; |
| 1439 | if (body.phase === 'up') |
| 1440 | { |
| 1441 | var decel = body.decel; |
| 1442 | var vy = body.vy; |
| 1443 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 1444 | if (tApex <= left + 1e-15) |
| 1445 | { |
| 1446 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 1447 | body.vy = 0; |
| 1448 | body.phase = 'down'; |
| 1449 | left -= tApex; |
| 1450 | } |
| 1451 | else |
| 1452 | { |
| 1453 | var vyNew = vy - decel * left; |
| 1454 | dy += (vy + vyNew) * 0.5 * left; |
| 1455 | body.vy = vyNew; |
| 1456 | left = 0; |
| 1457 | } |
| 1458 | } |
| 1459 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 1460 | { |
| 1461 | body.vy = 0; |
| 1462 | body.phase = 'down'; |
| 1463 | } |
| 1464 | if (body.phase === 'down' && left > 0) |
| 1465 | { |
| 1466 | var desc = this.descentParams(forCluster); |
| 1467 | var target = -desc.fallSpeed; |
| 1468 | var accel = desc.fallAccel; |
| 1469 | if (accel < 1e-12) |
| 1470 | { |
| 1471 | body.vy = target; |
| 1472 | dy += body.vy * left; |
| 1473 | } |
| 1474 | else if (body.vy > target + 1e-12) |
| 1475 | { |
| 1476 | var tReach = (body.vy - target) / accel; |
| 1477 | if (tReach <= left + 1e-15) |
| 1478 | { |
| 1479 | dy += (body.vy + target) * 0.5 * tReach; |
| 1480 | body.vy = target; |
| 1481 | left -= tReach; |
| 1482 | dy += body.vy * left; |
| 1483 | } |
| 1484 | else |
| 1485 | { |
| 1486 | var vyNew = body.vy - accel * left; |
| 1487 | dy += (body.vy + vyNew) * 0.5 * left; |
| 1488 | body.vy = vyNew; |
| 1489 | } |
| 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | body.vy = target; |
| 1494 | dy += body.vy * left; |
| 1495 | } |
| 1496 | } |
| 1497 | return dy; |
| 1498 | }; |
| 1499 | |
| 1500 | GameCore.prototype.integrateClusters = function (dt) |
| 1501 | { |
| 1502 | var ids = Object.keys(this.clusters); |
| 1503 | for (var n = 0; n < ids.length; n++) |
| 1504 | { |
| 1505 | var cluster = this.clusters[ids[n]]; |
| 1506 | if (!cluster) |
| 1507 | { continue; } |
| 1508 | var members = this.clusterMembers(cluster.id); |
| 1509 | if (!members.length) |
| 1510 | { |
| 1511 | delete this.clusters[cluster.id]; |
| 1512 | continue; |
| 1513 | } |
| 1514 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1515 | { continue; } |
| 1516 | |
| 1517 | this.prepareClusterBody(cluster); |
| 1518 | var dy = this.advanceJump(cluster, dt, true); |
| 1519 | cluster.anchorY += dy; |
| 1520 | this.syncClusterMembers(cluster); |
| 1521 | |
| 1522 | this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id)); |
| 1523 | this.checkChainRelaunch(cluster); |
| 1524 | |
| 1525 | if (cluster.phase === 'down' && this.clusters[cluster.id]) |
| 1526 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1527 | } |
| 1528 | }; |
| 1529 | |
| 1530 | |
| 1531 | /** |
| 1532 | * If two or more flying clusters overlap in the same column, merge them |
| 1533 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 1534 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 1535 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 1536 | * are packed so the higher one sits flush on the lower. |
| 1537 | */ |
| 1538 | GameCore.prototype.mergeFlyingClusters = function () |
| 1539 | { |
| 1540 | var flying = []; |
| 1541 | var ids = Object.keys(this.clusters); |
| 1542 | for (var n = 0; n < ids.length; n++) |
| 1543 | { |
| 1544 | var cluster = this.clusters[ids[n]]; |
| 1545 | if (!cluster) |
| 1546 | { continue; } |
| 1547 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1548 | { continue; } |
| 1549 | flying.push(cluster); |
| 1550 | } |
| 1551 | if (flying.length < 2) |
| 1552 | { return; } |
| 1553 | |
| 1554 | var uf = new UnionFind(); |
| 1555 | for (var i = 0; i < flying.length; i++) |
| 1556 | { uf.add(flying[i].id); } |
| 1557 | for (var i = 0; i < flying.length; i++) |
| 1558 | { |
| 1559 | for (var j = i + 1; j < flying.length; j++) |
| 1560 | { |
| 1561 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 1562 | { uf.union(flying[i].id, flying[j].id); } |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | var groups = Object.create(null); |
| 1567 | for (var i = 0; i < flying.length; i++) |
| 1568 | { |
| 1569 | var root = uf.find(flying[i].id); |
| 1570 | if (!groups[root]) |
| 1571 | { groups[root] = []; } |
| 1572 | groups[root].push(flying[i]); |
| 1573 | } |
| 1574 | for (var root in groups) |
| 1575 | { |
| 1576 | if (groups[root].length >= 2) |
| 1577 | { this.mergeClusterGroup(groups[root]); } |
| 1578 | } |
| 1579 | }; |
| 1580 | |
| 1581 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 1582 | { |
| 1583 | var ma = this.clusterMembers(a.id); |
| 1584 | var mb = this.clusterMembers(b.id); |
| 1585 | for (var i = 0; i < ma.length; i++) |
| 1586 | { |
| 1587 | for (var j = 0; j < mb.length; j++) |
| 1588 | { |
| 1589 | if (ma[i].col !== mb[j].col) |
| 1590 | { continue; } |
| 1591 | if (this.sweptIntervalsOverlap(ma[i], mb[j])) |
| 1592 | { return true; } |
| 1593 | } |
| 1594 | } |
| 1595 | return false; |
| 1596 | }; |
| 1597 | |
| 1598 | /** |
| 1599 | * Swept [y, y+1) of two blocks this substep, using _prevY when present. |
| 1600 | * Catches pairs that crossed without sharing a cell at either endpoint. |
| 1601 | */ |
| 1602 | GameCore.prototype.sweptIntervalsOverlap = function (a, b) |
| 1603 | { |
| 1604 | var a0 = (a._prevY != null) ? a._prevY : a.y; |
| 1605 | var a1 = a.y; |
| 1606 | var b0 = (b._prevY != null) ? b._prevY : b.y; |
| 1607 | var b1 = b.y; |
| 1608 | var aLo = Math.min(a0, a1); |
| 1609 | var aHi = Math.max(a0, a1) + 1; |
| 1610 | var bLo = Math.min(b0, b1); |
| 1611 | var bHi = Math.max(b0, b1) + 1; |
| 1612 | return aLo < bHi - 1e-9 && bLo < aHi - 1e-9; |
| 1613 | }; |
| 1614 | |
| 1615 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 1616 | { |
| 1617 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 1618 | var keep = clusters[0]; |
| 1619 | var n = 0; |
| 1620 | var vySum = 0; |
| 1621 | var decelSum = 0; |
| 1622 | var hSum = 0; |
| 1623 | for (var c = 0; c < clusters.length; c++) |
| 1624 | { |
| 1625 | var mem = this.clusterMembers(clusters[c].id); |
| 1626 | var m = mem.length; |
| 1627 | n += m; |
| 1628 | vySum += clusters[c].vy * m; |
| 1629 | decelSum += (clusters[c].decel || 0) * m; |
| 1630 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 1631 | } |
| 1632 | if (n === 0) |
| 1633 | { return; } |
| 1634 | keep.vy = vySum / n; |
| 1635 | keep.decel = decelSum / n; |
| 1636 | keep.jumpHeight = hSum / n; |
| 1637 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 1638 | var maxChain = 0; |
| 1639 | for (var c = 0; c < clusters.length; c++) |
| 1640 | { |
| 1641 | var ch = clusters[c].landingChain || 0; |
| 1642 | if (ch > maxChain) |
| 1643 | { maxChain = ch; } |
| 1644 | } |
| 1645 | keep.landingChain = maxChain; |
| 1646 | for (var c = 0; c < clusters.length; c++) |
| 1647 | { |
| 1648 | var mem = this.clusterMembers(clusters[c].id); |
| 1649 | for (var i = 0; i < mem.length; i++) |
| 1650 | { |
| 1651 | mem[i].clusterId = keep.id; |
| 1652 | mem[i].vy = keep.vy; |
| 1653 | mem[i].state = STATE_FLYING; |
| 1654 | } |
| 1655 | if (clusters[c] !== keep) |
| 1656 | { delete this.clusters[clusters[c].id]; } |
| 1657 | } |
| 1658 | this.prepareClusterBody(keep); |
| 1659 | this.checkChainRelaunch(keep); |
| 1660 | if (keep.phase === 'down' && this.clusters[keep.id]) |
| 1661 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 1662 | }; |
| 1663 | |
| 1664 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 1665 | { |
| 1666 | var members = this.clusterMembers(cluster.id); |
| 1667 | var byCol = Object.create(null); |
| 1668 | for (var i = 0; i < members.length; i++) |
| 1669 | { |
| 1670 | var col = members[i].col; |
| 1671 | if (!byCol[col]) |
| 1672 | { byCol[col] = []; } |
| 1673 | byCol[col].push(members[i]); |
| 1674 | } |
| 1675 | var cols = Object.keys(byCol); |
| 1676 | for (var c = 0; c < cols.length; c++) |
| 1677 | { |
| 1678 | var list = byCol[cols[c]]; |
| 1679 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1680 | for (var i = 1; i < list.length; i++) |
| 1681 | { |
| 1682 | var minY = list[i - 1].y + 1; |
| 1683 | if (list[i].y < minY - 1e-12) |
| 1684 | { list[i].y = minY; } |
| 1685 | } |
| 1686 | } |
| 1687 | }; |
| 1688 | |
| 1689 | |
| 1690 | /** |
| 1691 | * Derive integer cell offsets from current member positions and a |
| 1692 | * single anchorY, packing overlaps in each column while preserving |
| 1693 | * order (lower y stays lower; ties break on id). Then sync. |
| 1694 | * Flying clusters are one rigid body: one y, one vy, integer offsets. |
| 1695 | */ |
| 1696 | GameCore.prototype.prepareClusterBody = function (cluster) |
| 1697 | { |
| 1698 | if (!cluster) |
| 1699 | { return; } |
| 1700 | var members = this.clusterMembers(cluster.id); |
| 1701 | if (!members.length) |
| 1702 | { return; } |
| 1703 | this.rebuildClusterOffsets(cluster, members); |
| 1704 | }; |
| 1705 | |
| 1706 | GameCore.prototype.rebuildClusterOffsets = function (cluster, members) |
| 1707 | { |
| 1708 | members = members || this.clusterMembers(cluster.id); |
| 1709 | if (!members.length) |
| 1710 | { return; } |
| 1711 | var byCol = Object.create(null); |
| 1712 | for (var i = 0; i < members.length; i++) |
| 1713 | { |
| 1714 | var col = members[i].col; |
| 1715 | if (!byCol[col]) |
| 1716 | { byCol[col] = []; } |
| 1717 | byCol[col].push(members[i]); |
| 1718 | } |
| 1719 | var minY = Infinity; |
| 1720 | for (var i = 0; i < members.length; i++) |
| 1721 | { |
| 1722 | if (members[i].y < minY) |
| 1723 | { minY = members[i].y; } |
| 1724 | } |
| 1725 | if (cluster.anchorY == null || !isFinite(cluster.anchorY)) |
| 1726 | { cluster.anchorY = minY; } |
| 1727 | var cols = Object.keys(byCol); |
| 1728 | for (var c = 0; c < cols.length; c++) |
| 1729 | { |
| 1730 | var list = byCol[cols[c]]; |
| 1731 | list.sort(function (a, b) |
| 1732 | { |
| 1733 | if (a.y !== b.y) |
| 1734 | { return a.y - b.y; } |
| 1735 | return a.id - b.id; |
| 1736 | }); |
| 1737 | var prevOff = -Infinity; |
| 1738 | for (var i = 0; i < list.length; i++) |
| 1739 | { |
| 1740 | var off = Math.round(list[i].y - cluster.anchorY); |
| 1741 | if (off <= prevOff) |
| 1742 | { off = prevOff + 1; } |
| 1743 | list[i].clusterOffset = off; |
| 1744 | prevOff = off; |
| 1745 | } |
| 1746 | } |
| 1747 | // Re-base so the lowest offset is 0, keeping relative integers. |
| 1748 | var minOff = Infinity; |
| 1749 | for (var i = 0; i < members.length; i++) |
| 1750 | { |
| 1751 | if (members[i].clusterOffset < minOff) |
| 1752 | { minOff = members[i].clusterOffset; } |
| 1753 | } |
| 1754 | if (minOff !== 0 && isFinite(minOff)) |
| 1755 | { |
| 1756 | cluster.anchorY += minOff; |
| 1757 | for (var i = 0; i < members.length; i++) |
| 1758 | { members[i].clusterOffset -= minOff; } |
| 1759 | } |
| 1760 | this.syncClusterMembers(cluster); |
| 1761 | }; |
| 1762 | |
| 1763 | GameCore.prototype.syncClusterMembers = function (cluster) |
| 1764 | { |
| 1765 | if (!cluster) |
| 1766 | { return; } |
| 1767 | var members = this.clusterMembers(cluster.id); |
| 1768 | if (cluster.anchorY == null) |
| 1769 | { |
| 1770 | var minY = Infinity; |
| 1771 | for (var i = 0; i < members.length; i++) |
| 1772 | { |
| 1773 | if (members[i].y < minY) |
| 1774 | { minY = members[i].y; } |
| 1775 | } |
| 1776 | cluster.anchorY = isFinite(minY) ? minY : 0; |
| 1777 | } |
| 1778 | for (var i = 0; i < members.length; i++) |
| 1779 | { |
| 1780 | var block = members[i]; |
| 1781 | if (block.clusterOffset == null) |
| 1782 | { block.clusterOffset = Math.round(block.y - cluster.anchorY); } |
| 1783 | block.y = cluster.anchorY + block.clusterOffset; |
| 1784 | if (cluster.phase !== 'landed') |
| 1785 | { block.vy = cluster.vy; } |
| 1786 | } |
| 1787 | }; |
| 1788 | |
| 1789 | GameCore.prototype.snapshotPrevY = function () |
| 1790 | { |
| 1791 | for (var i = 0; i < this.blocks.length; i++) |
| 1792 | { this.blocks[i]._prevY = this.blocks[i].y; } |
| 1793 | }; |
| 1794 | |
| 1795 | /** |
| 1796 | * Safety net after the specific integrates: any remaining mid-air |
| 1797 | * overlap or swept crossing is resolved with the same rules, then |
| 1798 | * every flying cluster is rebuilt as a rigid body. Repeats a few |
| 1799 | * times because one attach can create a new contact. |
| 1800 | */ |
| 1801 | GameCore.prototype.resolveAirborneCollisions = function () |
| 1802 | { |
| 1803 | var guard = 0; |
| 1804 | while (guard++ < this.height + 4) |
| 1805 | { |
| 1806 | var changed = false; |
| 1807 | |
| 1808 | // Manual vs falling / cluster / manual. |
| 1809 | var manuals = []; |
| 1810 | for (var i = 0; i < this.blocks.length; i++) |
| 1811 | { |
| 1812 | if (this.blocks[i].state === STATE_MANUAL) |
| 1813 | { manuals.push(this.blocks[i]); } |
| 1814 | } |
| 1815 | manuals.sort(function (a, b) { return b.y - a.y; }); |
| 1816 | for (var m = 0; m < manuals.length; m++) |
| 1817 | { |
| 1818 | var manual = manuals[m]; |
| 1819 | if (manual.state !== STATE_MANUAL) |
| 1820 | { continue; } |
| 1821 | var oldY = (manual._prevY != null) ? manual._prevY : manual.y; |
| 1822 | if (this.resolveManualCollisions(manual, oldY, manual.y)) |
| 1823 | { changed = true; } |
| 1824 | } |
| 1825 | |
| 1826 | // Falling vs flying (sit on the roof). |
| 1827 | for (var i = 0; i < this.blocks.length; i++) |
| 1828 | { |
| 1829 | var block = this.blocks[i]; |
| 1830 | if (block.state !== STATE_FALLING) |
| 1831 | { continue; } |
| 1832 | var flyer = this.highestFlyingBelow(block); |
| 1833 | if (!flyer) |
| 1834 | { |
| 1835 | // Also a flyer whose swept path crosses this falling block. |
| 1836 | flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y); |
| 1837 | } |
| 1838 | if (flyer && this.sweptIntervalsOverlap(block, flyer)) |
| 1839 | { |
| 1840 | if (this.attachToFlyingCluster(block, flyer)) |
| 1841 | { changed = true; } |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | // Cluster vs cluster (swept, including after a column push). |
| 1846 | var before = Object.keys(this.clusters).length; |
| 1847 | this.mergeFlyingClusters(); |
| 1848 | if (Object.keys(this.clusters).length !== before) |
| 1849 | { changed = true; } |
| 1850 | |
| 1851 | // Rebuild every flying body so offsets stay integer and unique. |
| 1852 | var ids = Object.keys(this.clusters); |
| 1853 | for (var n = 0; n < ids.length; n++) |
| 1854 | { |
| 1855 | var cluster = this.clusters[ids[n]]; |
| 1856 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1857 | { continue; } |
| 1858 | this.prepareClusterBody(cluster); |
| 1859 | } |
| 1860 | |
| 1861 | if (!changed) |
| 1862 | { break; } |
| 1863 | } |
| 1864 | }; |
| 1865 | |
| 1866 | /** |
| 1867 | * Invariant checker (t1112). No two blocks occupy the same cell or |
| 1868 | * overlap their [y, y+1) intervals. Flying-cluster members are |
| 1869 | * grid-aligned to a single anchor. Returns a list of violation |
| 1870 | * strings (empty if clean). |
| 1871 | */ |
| 1872 | GameCore.prototype.checkInvariants = function () |
| 1873 | { |
| 1874 | var issues = []; |
| 1875 | var byCol = Object.create(null); |
| 1876 | var ids = Object.create(null); |
| 1877 | for (var i = 0; i < this.blocks.length; i++) |
| 1878 | { |
| 1879 | var block = this.blocks[i]; |
| 1880 | if (ids[block.id]) |
| 1881 | { issues.push('duplicate id ' + block.id); } |
| 1882 | ids[block.id] = true; |
| 1883 | if (!byCol[block.col]) |
| 1884 | { byCol[block.col] = []; } |
| 1885 | byCol[block.col].push(block); |
| 1886 | if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet |
| 1887 | && block.color >= this.planet.colors.length)) |
| 1888 | { issues.push('block ' + block.id + ' has invalid color ' + block.color); } |
| 1889 | } |
| 1890 | var cols = Object.keys(byCol); |
| 1891 | for (var c = 0; c < cols.length; c++) |
| 1892 | { |
| 1893 | var list = byCol[cols[c]]; |
| 1894 | list.sort(function (a, b) |
| 1895 | { |
| 1896 | if (a.y !== b.y) |
| 1897 | { return a.y - b.y; } |
| 1898 | return a.id - b.id; |
| 1899 | }); |
| 1900 | for (var i = 0; i < list.length; i++) |
| 1901 | { |
| 1902 | var a = list[i]; |
| 1903 | if (i > 0) |
| 1904 | { |
| 1905 | var prev = list[i - 1]; |
| 1906 | if (a.y < prev.y + 1 - 1e-6) |
| 1907 | { |
| 1908 | issues.push('overlap col=' + a.col |
| 1909 | + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a) |
| 1910 | + ' gap=' + (a.y - prev.y)); |
| 1911 | } |
| 1912 | var cellA = Math.floor(a.y + 1e-9); |
| 1913 | var cellP = Math.floor(prev.y + 1e-9); |
| 1914 | if (cellA === cellP) |
| 1915 | { |
| 1916 | issues.push('duplicate cell col=' + a.col + ' row=' + cellA |
| 1917 | + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)); |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | var cids = Object.keys(this.clusters); |
| 1923 | for (var n = 0; n < cids.length; n++) |
| 1924 | { |
| 1925 | var cluster = this.clusters[cids[n]]; |
| 1926 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1927 | { continue; } |
| 1928 | var mem = this.clusterMembers(cluster.id); |
| 1929 | var seenOff = Object.create(null); |
| 1930 | for (var i = 0; i < mem.length; i++) |
| 1931 | { |
| 1932 | var b = mem[i]; |
| 1933 | if (cluster.anchorY != null && b.clusterOffset != null) |
| 1934 | { |
| 1935 | var expected = cluster.anchorY + b.clusterOffset; |
| 1936 | if (Math.abs(b.y - expected) > 1e-6) |
| 1937 | { |
| 1938 | issues.push('cluster ' + cluster.id + ' member ' + b.id |
| 1939 | + ' y=' + b.y + ' != anchor+off ' + expected); |
| 1940 | } |
| 1941 | if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9) |
| 1942 | { |
| 1943 | issues.push('cluster ' + cluster.id + ' member ' + b.id |
| 1944 | + ' non-integer offset ' + b.clusterOffset); |
| 1945 | } |
| 1946 | var key = b.col + ':' + b.clusterOffset; |
| 1947 | if (seenOff[key]) |
| 1948 | { |
| 1949 | issues.push('cluster ' + cluster.id + ' duplicate offset ' + key); |
| 1950 | } |
| 1951 | seenOff[key] = true; |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | return issues; |
| 1956 | }; |
| 1957 | |
| 1958 | GameCore.prototype._invBlock = function (b) |
| 1959 | { |
| 1960 | return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y=' |
| 1961 | + (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')'; |
| 1962 | }; |
| 1963 | |
| 1964 | GameCore.prototype.assertInvariants = function (label) |
| 1965 | { |
| 1966 | if (!this.strictInvariants && !this.debugInvariants) |
| 1967 | { return; } |
| 1968 | var issues = this.checkInvariants(); |
| 1969 | this.lastInvariantViolations = issues; |
| 1970 | if (!issues.length) |
| 1971 | { return; } |
| 1972 | var msg = 'physics invariants failed' |
| 1973 | + (label ? ' at ' + label : '') |
| 1974 | + ':\n ' + issues.join('\n ') |
| 1975 | + '\n' + (this.snapshot ? this.snapshot().picture : ''); |
| 1976 | if (this.strictInvariants) |
| 1977 | { throw new Error(msg); } |
| 1978 | }; |
| 1979 | |
| 1980 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1981 | { |
| 1982 | if (!members.length) |
| 1983 | { return; } |
| 1984 | var shift = 0; |
| 1985 | var hit = false; |
| 1986 | for (var i = 0; i < members.length; i++) |
| 1987 | { |
| 1988 | var block = members[i]; |
| 1989 | var top = this.stackTop(block.col, function (other) |
| 1990 | { |
| 1991 | return other.clusterId === cluster.id; |
| 1992 | }, block.y); |
| 1993 | if (block.y <= top + 1e-9) |
| 1994 | { |
| 1995 | hit = true; |
| 1996 | var need = top - block.y; |
| 1997 | if (need > shift) |
| 1998 | { shift = need; } |
| 1999 | } |
| 2000 | } |
| 2001 | if (!hit) |
| 2002 | { return; } |
| 2003 | // Lift the whole rigid body so the deepest contact sits flush. |
| 2004 | this.prepareClusterBody(cluster); |
| 2005 | cluster.anchorY += shift; |
| 2006 | this.syncClusterMembers(cluster); |
| 2007 | this.landCluster(cluster, this.clusterMembers(cluster.id)); |
| 2008 | }; |
| 2009 | |
| 2010 | /** |
| 2011 | * Land as a rigid group: relative positions are kept, so part of the |
| 2012 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 2013 | * coloured cargo stays put with them until every grey has recolored. |
| 2014 | * Members that actually rest on the floor or on another block bounce. |
| 2015 | */ |
| 2016 | GameCore.prototype.landCluster = function (cluster, members) |
| 2017 | { |
| 2018 | for (var i = 0; i < members.length; i++) |
| 2019 | { |
| 2020 | var block = members[i]; |
| 2021 | block.vy = 0; |
| 2022 | block.flame = false; |
| 2023 | block.clusterId = cluster.id; |
| 2024 | if (block.color === COLOR_GRAY) |
| 2025 | { |
| 2026 | block.state = STATE_SETTLING; |
| 2027 | block.settleTime = 0; |
| 2028 | } |
| 2029 | else |
| 2030 | { |
| 2031 | // Cargo stays in the rigid group (not matchable) until release. |
| 2032 | block.state = STATE_SETTLING; |
| 2033 | block.settleTime = 0; |
| 2034 | } |
| 2035 | if (this.hasSupport(block)) |
| 2036 | { this.startBounce(block); } |
| 2037 | } |
| 2038 | cluster.phase = 'landed'; |
| 2039 | cluster.vy = 0; |
| 2040 | cluster.landedPending = true; |
| 2041 | if (cluster.landingChain == null) |
| 2042 | { cluster.landingChain = 0; } |
| 2043 | }; |
| 2044 | |
| 2045 | /** |
| 2046 | * After every grey in a landed cluster has recolored, let the group go. |
| 2047 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 2048 | */ |
| 2049 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 2050 | { |
| 2051 | var members = this.clusterMembers(cluster.id); |
| 2052 | var releasing = Object.create(null); |
| 2053 | for (var i = 0; i < members.length; i++) |
| 2054 | { releasing[members[i].id] = true; } |
| 2055 | |
| 2056 | for (var i = 0; i < members.length; i++) |
| 2057 | { |
| 2058 | var block = members[i]; |
| 2059 | if (block.color === COLOR_GRAY) |
| 2060 | { |
| 2061 | block.color = this.randomColor(); |
| 2062 | block.settleTime = 0; |
| 2063 | } |
| 2064 | // Persist the chain across release so a post-recolor match in |
| 2065 | // this same tick still gets the landing bonus (t1120). |
| 2066 | block.justLanded = !!cluster.landedPending; |
| 2067 | block.landingChain = cluster.landingChain || 0; |
| 2068 | this.leaveCluster(block); |
| 2069 | // Always drop leftover launch / manual velocity. A stale upward |
| 2070 | // vy here is what sends blocks off the top "very fast" after |
| 2071 | // a grey group recolors. |
| 2072 | this.clearFlightState(block); |
| 2073 | // Support from outside the group (floor or a foreign block). Fellow |
| 2074 | // members that will also fall do not count, so a hanging stack |
| 2075 | // drops together without the top one staying put. |
| 2076 | var supported = this.hasSupportOutside(block, releasing); |
| 2077 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 2078 | } |
| 2079 | delete this.clusters[cluster.id]; |
| 2080 | }; |
| 2081 | |
| 2082 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 2083 | { |
| 2084 | if (block.y <= 1e-9) |
| 2085 | { return true; } |
| 2086 | for (var i = 0; i < this.blocks.length; i++) |
| 2087 | { |
| 2088 | var other = this.blocks[i]; |
| 2089 | if (other === block || other.col !== block.col) |
| 2090 | { continue; } |
| 2091 | if (ignoreIds && ignoreIds[other.id]) |
| 2092 | { continue; } |
| 2093 | if (!this.isGrounded(other)) |
| 2094 | { continue; } |
| 2095 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 2096 | { return true; } |
| 2097 | } |
| 2098 | return false; |
| 2099 | }; |
| 2100 | |
| 2101 | GameCore.prototype.integrateManual = function (dt) |
| 2102 | { |
| 2103 | for (var i = 0; i < this.blocks.length; i++) |
| 2104 | { |
| 2105 | var block = this.blocks[i]; |
| 2106 | if (block.state !== STATE_MANUAL) |
| 2107 | { continue; } |
| 2108 | |
| 2109 | if (block.manualPhase === 'up') |
| 2110 | { |
| 2111 | var oldY = block.y; |
| 2112 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 2113 | var dy = this.advanceJump(proxy, dt); |
| 2114 | var nextY = oldY + dy; |
| 2115 | block.vy = proxy.vy; |
| 2116 | block.manualPhase = proxy.phase; |
| 2117 | if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget))) |
| 2118 | { continue; } |
| 2119 | block.y = Math.min(nextY, block.manualTarget); |
| 2120 | if (block.y >= block.manualTarget - 1e-12) |
| 2121 | { |
| 2122 | block.y = block.manualTarget; |
| 2123 | block.vy = 0; |
| 2124 | block.manualPhase = 'down'; |
| 2125 | } |
| 2126 | if (block.y > this.height - 1) |
| 2127 | { |
| 2128 | block.y = this.height - 1; |
| 2129 | block.vy = 0; |
| 2130 | block.manualPhase = 'down'; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | if (block.manualPhase === 'down') |
| 2135 | { |
| 2136 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 2137 | var dy = this.advanceJump(proxy, dt); |
| 2138 | block.vy = proxy.vy; |
| 2139 | var nextY = block.y + dy; |
| 2140 | var top = this.stackTop(block.col, null, block.y); |
| 2141 | if (nextY <= top) |
| 2142 | { |
| 2143 | block.y = top; |
| 2144 | block.state = STATE_STILL; |
| 2145 | block.vy = 0; |
| 2146 | block.manualPhase = null; |
| 2147 | block.flame = false; |
| 2148 | this.startBounce(block); |
| 2149 | } |
| 2150 | else |
| 2151 | { |
| 2152 | var downNext = Math.min(nextY, this.height - 1); |
| 2153 | if (this.resolveManualCollisions(block, block.y, downNext)) |
| 2154 | { continue; } |
| 2155 | block.y = downNext; |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | }; |
| 2160 | |
| 2161 | /** |
| 2162 | * If the manual throw (or its path this step) hits falling blocks, merge |
| 2163 | * them into a flying cluster with mass-weighted velocity, the same rule |
| 2164 | * as two flying clusters colliding (t1108). Returns true if merged. |
| 2165 | */ |
| 2166 | GameCore.prototype.tryMergeManualWithFalling = function (manual, oldY, newY) |
| 2167 | { |
| 2168 | if (!manual || manual.state !== STATE_MANUAL) |
| 2169 | { return false; } |
| 2170 | var hits = this.fallingHitsOnManualPath(manual, oldY, newY); |
| 2171 | if (!hits.length) |
| 2172 | { return false; } |
| 2173 | this.mergeManualWithFalling(manual, hits); |
| 2174 | return true; |
| 2175 | }; |
| 2176 | |
| 2177 | GameCore.prototype.manualInColumnHit = function (falling, oldY, newY) |
| 2178 | { |
| 2179 | var lo = Math.min(oldY, newY); |
| 2180 | var hi = Math.max(oldY, newY) + 1; |
| 2181 | for (var i = 0; i < this.blocks.length; i++) |
| 2182 | { |
| 2183 | var other = this.blocks[i]; |
| 2184 | if (other === falling || other.col !== falling.col) |
| 2185 | { continue; } |
| 2186 | if (other.state !== STATE_MANUAL) |
| 2187 | { continue; } |
| 2188 | if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9) |
| 2189 | { return other; } |
| 2190 | } |
| 2191 | return null; |
| 2192 | }; |
| 2193 | |
| 2194 | GameCore.prototype.fallingHitsOnManualPath = function (manual, oldY, newY) |
| 2195 | { |
| 2196 | var lo = Math.min(oldY, newY); |
| 2197 | var hi = Math.max(oldY, newY) + 1; |
| 2198 | var hits = []; |
| 2199 | for (var i = 0; i < this.blocks.length; i++) |
| 2200 | { |
| 2201 | var other = this.blocks[i]; |
| 2202 | if (other === manual || other.col !== manual.col) |
| 2203 | { continue; } |
| 2204 | if (other.state !== STATE_FALLING) |
| 2205 | { continue; } |
| 2206 | if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9) |
| 2207 | { hits.push(other); } |
| 2208 | } |
| 2209 | if (!hits.length) |
| 2210 | { return hits; } |
| 2211 | return this.contiguousFallingInColumn(manual.col, manual, hits); |
| 2212 | }; |
| 2213 | |
| 2214 | /** |
| 2215 | * Every falling block in `col` that overlaps `anchor` or a hit already |
| 2216 | * in `seed`, plus a contiguous falling stack sitting on them. |
| 2217 | */ |
| 2218 | GameCore.prototype.contiguousFallingInColumn = function (col, anchor, seed) |
| 2219 | { |
| 2220 | var falling = []; |
| 2221 | for (var i = 0; i < this.blocks.length; i++) |
| 2222 | { |
| 2223 | if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING) |
| 2224 | { falling.push(this.blocks[i]); } |
| 2225 | } |
| 2226 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 2227 | var include = Object.create(null); |
| 2228 | var queue = []; |
| 2229 | function enqueue (b) |
| 2230 | { |
| 2231 | if (!b || include[b.id]) |
| 2232 | { return; } |
| 2233 | include[b.id] = true; |
| 2234 | queue.push(b); |
| 2235 | } |
| 2236 | if (seed) |
| 2237 | { |
| 2238 | for (var s = 0; s < seed.length; s++) |
| 2239 | { enqueue(seed[s]); } |
| 2240 | } |
| 2241 | // Also include falling that already overlap the anchor. |
| 2242 | if (anchor) |
| 2243 | { |
| 2244 | for (var i = 0; i < falling.length; i++) |
| 2245 | { |
| 2246 | if (falling[i].y < anchor.y + 1 - 1e-9 && anchor.y < falling[i].y + 1 - 1e-9) |
| 2247 | { enqueue(falling[i]); } |
| 2248 | } |
| 2249 | } |
| 2250 | for (var q = 0; q < queue.length; q++) |
| 2251 | { |
| 2252 | var a = queue[q]; |
| 2253 | for (var i = 0; i < falling.length; i++) |
| 2254 | { |
| 2255 | var b = falling[i]; |
| 2256 | if (include[b.id]) |
| 2257 | { continue; } |
| 2258 | if (a.y < b.y + 1 + 1e-9 && b.y < a.y + 1 + 1e-9) |
| 2259 | { enqueue(b); } |
| 2260 | } |
| 2261 | } |
| 2262 | var out = []; |
| 2263 | for (var i = 0; i < falling.length; i++) |
| 2264 | { |
| 2265 | if (include[falling[i].id]) |
| 2266 | { out.push(falling[i]); } |
| 2267 | } |
| 2268 | return out; |
| 2269 | }; |
| 2270 | |
| 2271 | /** |
| 2272 | * Merge a thrown (manual) block with falling blocks into one flying |
| 2273 | * cluster. Velocity, deceleration and jumpHeight are the mass-weighted |
| 2274 | * averages by block count, identical to mergeClusterGroup. The thrown |
| 2275 | * block stops being manual and the group follows normal cluster physics. |
| 2276 | * fromManual marks the cluster so later falling cargo also averages in. |
| 2277 | */ |
| 2278 | GameCore.prototype.mergeManualWithFalling = function (manual, fallingList) |
| 2279 | { |
| 2280 | if (!manual || manual.state !== STATE_MANUAL) |
| 2281 | { return null; } |
| 2282 | var blocks = [manual]; |
| 2283 | for (var i = 0; i < fallingList.length; i++) |
| 2284 | { |
| 2285 | if (fallingList[i] && fallingList[i].state === STATE_FALLING && blocks.indexOf(fallingList[i]) < 0) |
| 2286 | { blocks.push(fallingList[i]); } |
| 2287 | } |
| 2288 | if (blocks.length < 2) |
| 2289 | { return null; } |
| 2290 | var n = blocks.length; |
| 2291 | var vySum = 0; |
| 2292 | var decelSum = 0; |
| 2293 | var hSum = 0; |
| 2294 | for (var i = 0; i < n; i++) |
| 2295 | { |
| 2296 | var b = blocks[i]; |
| 2297 | vySum += b.vy; |
| 2298 | if (b === manual) |
| 2299 | { |
| 2300 | decelSum += manual.manualDecel || 0; |
| 2301 | hSum += manual.manualJumpHeight != null ? manual.manualJumpHeight |
| 2302 | : Math.max(0, (manual.manualTarget || this.height - 1) - manual.y); |
| 2303 | } |
| 2304 | } |
| 2305 | var cluster = { |
| 2306 | id: this.nextClusterId++, |
| 2307 | jumpHeight: hSum / n, |
| 2308 | vy: vySum / n, |
| 2309 | decel: decelSum / n, |
| 2310 | phase: (vySum / n) > 1e-10 ? 'up' : 'down', |
| 2311 | fromManual: true |
| 2312 | }; |
| 2313 | this.clusters[cluster.id] = cluster; |
| 2314 | for (var i = 0; i < n; i++) |
| 2315 | { |
| 2316 | var b = blocks[i]; |
| 2317 | b.clusterId = cluster.id; |
| 2318 | b.state = STATE_FLYING; |
| 2319 | b.vy = cluster.vy; |
| 2320 | b.flame = b.color === COLOR_GRAY; |
| 2321 | b.manualPhase = null; |
| 2322 | b.manualTarget = null; |
| 2323 | b.manualDecel = 0; |
| 2324 | b.manualJumpHeight = 0; |
| 2325 | b.pushedClusters = null; |
| 2326 | b.settleTime = 0; |
| 2327 | } |
| 2328 | if (this.drag && this.drag.blockId === manual.id) |
| 2329 | { this.drag = null; } |
| 2330 | this.prepareClusterBody(cluster); |
| 2331 | this.checkChainRelaunch(cluster); |
| 2332 | return cluster; |
| 2333 | }; |
| 2334 | |
| 2335 | /** Safety net: any remaining manual/falling overlaps after the substep. */ |
| 2336 | GameCore.prototype.mergeAnyManualFallingOverlaps = function () |
| 2337 | { |
| 2338 | var manuals = []; |
| 2339 | for (var i = 0; i < this.blocks.length; i++) |
| 2340 | { |
| 2341 | if (this.blocks[i].state === STATE_MANUAL) |
| 2342 | { manuals.push(this.blocks[i]); } |
| 2343 | } |
| 2344 | for (var m = 0; m < manuals.length; m++) |
| 2345 | { |
| 2346 | var manual = manuals[m]; |
| 2347 | if (manual.state !== STATE_MANUAL) |
| 2348 | { continue; } |
| 2349 | var hits = this.contiguousFallingInColumn(manual.col, manual); |
| 2350 | if (hits.length) |
| 2351 | { this.mergeManualWithFalling(manual, hits); } |
| 2352 | } |
| 2353 | }; |
| 2354 | |
| 2355 | /** |
| 2356 | * Resolve collisions of a thrown (manual) block along [oldY, nextY]. |
| 2357 | * Falling blocks merge (t1108). A flying cluster is pushed one cell up |
| 2358 | * in the hit column and the shot attaches at the bottom (t1111). |
| 2359 | * Another thrown block is promoted to a 1-block cluster and then the |
| 2360 | * same attach-at-bottom rule applies. Returns true if `manual` is no |
| 2361 | * longer in the MANUAL state. |
| 2362 | */ |
| 2363 | GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY) |
| 2364 | { |
| 2365 | if (!manual || manual.state !== STATE_MANUAL) |
| 2366 | { return true; } |
| 2367 | |
| 2368 | var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY); |
| 2369 | if (fallingHits.length) |
| 2370 | { |
| 2371 | this.mergeManualWithFalling(manual, fallingHits); |
| 2372 | return manual.state !== STATE_MANUAL; |
| 2373 | } |
| 2374 | |
| 2375 | var flyer = this.flyingHitOnManualPath(manual, oldY, nextY); |
| 2376 | if (flyer) |
| 2377 | { |
| 2378 | var cluster = this.clusters[flyer.clusterId]; |
| 2379 | if (cluster && cluster.phase !== 'landed') |
| 2380 | { |
| 2381 | if (nextY + 1e-12 < oldY) |
| 2382 | { this.attachToFlyingCluster(manual, flyer); } |
| 2383 | else |
| 2384 | { this.attachShotUnderCluster(manual, cluster, manual.col); } |
| 2385 | return manual.state !== STATE_MANUAL; |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | var other = this.manualHitOnManualPath(manual, oldY, nextY); |
| 2390 | if (other) |
| 2391 | { |
| 2392 | var host = this.promoteManualToCluster(other); |
| 2393 | if (host) |
| 2394 | { |
| 2395 | if (nextY + 1e-12 < oldY) |
| 2396 | { this.attachToFlyingCluster(manual, other); } |
| 2397 | else |
| 2398 | { this.attachShotUnderCluster(manual, host, manual.col); } |
| 2399 | return manual.state !== STATE_MANUAL; |
| 2400 | } |
| 2401 | } |
| 2402 | return false; |
| 2403 | }; |
| 2404 | |
| 2405 | /** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */ |
| 2406 | GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY) |
| 2407 | { |
| 2408 | var lo = Math.min(oldY, nextY); |
| 2409 | var hi = Math.max(oldY, nextY) + 1; |
| 2410 | var best = null; |
| 2411 | var bestY = Infinity; |
| 2412 | for (var i = 0; i < this.blocks.length; i++) |
| 2413 | { |
| 2414 | var other = this.blocks[i]; |
| 2415 | if (other === manual || other.col !== manual.col) |
| 2416 | { continue; } |
| 2417 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 2418 | { continue; } |
| 2419 | var cluster = this.clusters[other.clusterId]; |
| 2420 | if (!cluster || cluster.phase === 'landed') |
| 2421 | { continue; } |
| 2422 | var o0 = (other._prevY != null) ? other._prevY : other.y; |
| 2423 | var o1 = other.y; |
| 2424 | var oLo = Math.min(o0, o1); |
| 2425 | var oHi = Math.max(o0, o1) + 1; |
| 2426 | if (lo < oHi - 1e-9 && oLo < hi - 1e-9) |
| 2427 | { |
| 2428 | if (other.y < bestY) |
| 2429 | { |
| 2430 | bestY = other.y; |
| 2431 | best = other; |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | return best; |
| 2436 | }; |
| 2437 | |
| 2438 | /** Another thrown block in the same column on the path (prefer the lower). */ |
| 2439 | GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY) |
| 2440 | { |
| 2441 | var lo = Math.min(oldY, nextY); |
| 2442 | var hi = Math.max(oldY, nextY) + 1; |
| 2443 | var best = null; |
| 2444 | for (var i = 0; i < this.blocks.length; i++) |
| 2445 | { |
| 2446 | var other = this.blocks[i]; |
| 2447 | if (other === manual || other.col !== manual.col) |
| 2448 | { continue; } |
| 2449 | if (other.state !== STATE_MANUAL) |
| 2450 | { continue; } |
| 2451 | var o0 = (other._prevY != null) ? other._prevY : other.y; |
| 2452 | var o1 = other.y; |
| 2453 | var oLo = Math.min(o0, o1); |
| 2454 | var oHi = Math.max(o0, o1) + 1; |
| 2455 | if (lo < oHi - 1e-9 && oLo < hi - 1e-9) |
| 2456 | { |
| 2457 | if (!best || other.y < best.y) |
| 2458 | { best = other; } |
| 2459 | } |
| 2460 | } |
| 2461 | return best; |
| 2462 | }; |
| 2463 | |
| 2464 | /** |
| 2465 | * t1111: push every member of `cluster` in `col` up by one cell, then |
| 2466 | * attach `manual` at the bottom of that column, grid-aligned, as part |
| 2467 | * of the cluster. The cluster keeps its velocity; the shot takes it. |
| 2468 | * Other columns are untouched. Same rule for every successive shot. |
| 2469 | */ |
| 2470 | GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col) |
| 2471 | { |
| 2472 | if (!manual || !cluster || !this.clusters[cluster.id]) |
| 2473 | { return false; } |
| 2474 | if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id) |
| 2475 | { |
| 2476 | // Already belonging elsewhere: leave first. |
| 2477 | if (manual.clusterId != null) |
| 2478 | { this.leaveCluster(manual); } |
| 2479 | } |
| 2480 | this.prepareClusterBody(cluster); |
| 2481 | var members = this.clusterMembers(cluster.id); |
| 2482 | var minOff = null; |
| 2483 | for (var i = 0; i < members.length; i++) |
| 2484 | { |
| 2485 | if (members[i].col !== col) |
| 2486 | { continue; } |
| 2487 | if (minOff == null || members[i].clusterOffset < minOff) |
| 2488 | { minOff = members[i].clusterOffset; } |
| 2489 | } |
| 2490 | if (minOff == null) |
| 2491 | { minOff = 0; } |
| 2492 | else |
| 2493 | { |
| 2494 | for (var i = 0; i < members.length; i++) |
| 2495 | { |
| 2496 | if (members[i].col === col) |
| 2497 | { members[i].clusterOffset += 1; } |
| 2498 | } |
| 2499 | } |
| 2500 | if (this.drag && this.drag.blockId === manual.id) |
| 2501 | { this.drag = null; } |
| 2502 | manual.clusterId = cluster.id; |
| 2503 | manual.clusterOffset = minOff; |
| 2504 | manual.state = STATE_FLYING; |
| 2505 | manual.flame = manual.color === COLOR_GRAY; |
| 2506 | manual.settleTime = 0; |
| 2507 | manual.manualPhase = null; |
| 2508 | manual.manualTarget = null; |
| 2509 | manual.manualDecel = 0; |
| 2510 | manual.manualJumpHeight = 0; |
| 2511 | manual.pushedClusters = null; |
| 2512 | this.syncClusterMembers(cluster); |
| 2513 | this.cullOffTop(); |
| 2514 | if (this.clusters[cluster.id]) |
| 2515 | { this.checkChainRelaunch(cluster); } |
| 2516 | return true; |
| 2517 | }; |
| 2518 | |
| 2519 | /** |
| 2520 | * Turn a lone thrown block into a 1-block flying cluster so a later |
| 2521 | * shot can use the t1111 attach-at-bottom rule. A single throw stays |
| 2522 | * MANUAL (and is capped at the top of the field) until it collides. |
| 2523 | */ |
| 2524 | GameCore.prototype.promoteManualToCluster = function (manual) |
| 2525 | { |
| 2526 | if (!manual) |
| 2527 | { return null; } |
| 2528 | if (manual.state === STATE_FLYING && manual.clusterId != null) |
| 2529 | { |
| 2530 | var existing = this.clusters[manual.clusterId]; |
| 2531 | if (existing && existing.phase !== 'landed') |
| 2532 | { return existing; } |
| 2533 | } |
| 2534 | if (manual.state !== STATE_MANUAL) |
| 2535 | { return null; } |
| 2536 | var height = manual.manualJumpHeight != null ? manual.manualJumpHeight |
| 2537 | : Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y); |
| 2538 | var cluster = { |
| 2539 | id: this.nextClusterId++, |
| 2540 | jumpHeight: height, |
| 2541 | vy: manual.vy, |
| 2542 | decel: manual.manualDecel || 0, |
| 2543 | phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up', |
| 2544 | fromManual: true, |
| 2545 | anchorY: manual.y |
| 2546 | }; |
| 2547 | this.clusters[cluster.id] = cluster; |
| 2548 | manual.state = STATE_FLYING; |
| 2549 | manual.clusterId = cluster.id; |
| 2550 | manual.clusterOffset = 0; |
| 2551 | manual.flame = manual.color === COLOR_GRAY; |
| 2552 | manual.settleTime = 0; |
| 2553 | manual.manualPhase = null; |
| 2554 | manual.manualTarget = null; |
| 2555 | manual.manualDecel = 0; |
| 2556 | manual.manualJumpHeight = 0; |
| 2557 | manual.pushedClusters = null; |
| 2558 | this.syncClusterMembers(cluster); |
| 2559 | return cluster; |
| 2560 | }; |
| 2561 | |
| 2562 | /** Kept for older tests / call sites: push a cluster column by one cell. */ |
| 2563 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 2564 | { |
| 2565 | return this.resolveManualCollisions(manual, oldY, nextY); |
| 2566 | }; |
| 2567 | |
| 2568 | |
| 2569 | /** |
| 2570 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 2571 | * Other columns stay put (they remain linked). Then look for a new |
| 2572 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 2573 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 2574 | * with the same jump height from its current position. |
| 2575 | */ |
| 2576 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 2577 | { |
| 2578 | var cluster = this.clusters[clusterId]; |
| 2579 | if (!cluster) |
| 2580 | { return false; } |
| 2581 | var members = this.clusterMembers(clusterId); |
| 2582 | for (var i = 0; i < members.length; i++) |
| 2583 | { |
| 2584 | if (members[i].col === col) |
| 2585 | { members[i].y += 1; } |
| 2586 | } |
| 2587 | this.cullOffTop(); |
| 2588 | if (!this.clusters[clusterId]) |
| 2589 | { return true; } |
| 2590 | this.checkChainRelaunch(cluster); |
| 2591 | return true; |
| 2592 | }; |
| 2593 | |
| 2594 | /** |
| 2595 | * After a cargo attach or a cluster merge (or any other mid-air state |
| 2596 | * change), turn spontaneous lines of 3+ into rockets and give the whole |
| 2597 | * already-flying cluster an extra launch from its current position — |
| 2598 | * the same re-launch as a player push that completes a row. |
| 2599 | */ |
| 2600 | GameCore.prototype.resolveFlyingMatches = function () |
| 2601 | { |
| 2602 | var ids = Object.keys(this.clusters); |
| 2603 | for (var n = 0; n < ids.length; n++) |
| 2604 | { |
| 2605 | var cluster = this.clusters[ids[n]]; |
| 2606 | if (!cluster || cluster.phase === 'landed') |
| 2607 | { continue; } |
| 2608 | this.checkChainRelaunch(cluster); |
| 2609 | } |
| 2610 | }; |
| 2611 | |
| 2612 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 2613 | { |
| 2614 | if (!this.clusters[cluster.id]) |
| 2615 | { return; } |
| 2616 | var lines = this.findFlyingMatchLines(cluster.id); |
| 2617 | if (!lines.length) |
| 2618 | { return; } |
| 2619 | var newly = []; |
| 2620 | var seen = Object.create(null); |
| 2621 | for (var i = 0; i < lines.length; i++) |
| 2622 | { |
| 2623 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 2624 | { |
| 2625 | var block = lines[i].blocks[j]; |
| 2626 | if (seen[block.id]) |
| 2627 | { continue; } |
| 2628 | seen[block.id] = true; |
| 2629 | newly.push(block); |
| 2630 | } |
| 2631 | } |
| 2632 | for (var i = 0; i < newly.length; i++) |
| 2633 | { |
| 2634 | var block = newly[i]; |
| 2635 | block.color = COLOR_GRAY; |
| 2636 | block.flame = true; |
| 2637 | block.clusterId = cluster.id; |
| 2638 | block.state = STATE_FLYING; |
| 2639 | } |
| 2640 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 2641 | }; |
| 2642 | |
| 2643 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 2644 | { |
| 2645 | cluster.jumpHeight = height; |
| 2646 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 2647 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 2648 | cluster.phase = 'up'; |
| 2649 | var members = this.clusterMembers(cluster.id); |
| 2650 | for (var i = 0; i < members.length; i++) |
| 2651 | { |
| 2652 | members[i].vy = cluster.vy; |
| 2653 | members[i].state = STATE_FLYING; |
| 2654 | if (members[i].color === COLOR_GRAY) |
| 2655 | { members[i].flame = true; } |
| 2656 | } |
| 2657 | }; |
| 2658 | |
| 2659 | GameCore.prototype.startManualLaunch = function (block) |
| 2660 | { |
| 2661 | var target = this.height - 1; |
| 2662 | var height = Math.max(0.05, target - block.y); |
| 2663 | this.leaveCluster(block); |
| 2664 | block.state = STATE_MANUAL; |
| 2665 | block.pushedClusters = Object.create(null); |
| 2666 | block.manualTarget = target; |
| 2667 | block.manualPhase = 'up'; |
| 2668 | block.manualJumpHeight = height; |
| 2669 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 2670 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 2671 | block.flame = false; |
| 2672 | if (this.drag && this.drag.blockId === block.id) |
| 2673 | { this.drag = null; } |
| 2674 | }; |
| 2675 | |
| 2676 | GameCore.prototype.cullOffTop = function () |
| 2677 | { |
| 2678 | var gone = []; |
| 2679 | for (var i = 0; i < this.blocks.length; i++) |
| 2680 | { |
| 2681 | var block = this.blocks[i]; |
| 2682 | if (block.state === STATE_MANUAL) |
| 2683 | { |
| 2684 | if (block.y > this.height - 1) |
| 2685 | { block.y = this.height - 1; } |
| 2686 | continue; |
| 2687 | } |
| 2688 | if (block.y >= this.height - 1e-9) |
| 2689 | { gone.push(block); } |
| 2690 | } |
| 2691 | for (var i = 0; i < gone.length; i++) |
| 2692 | { |
| 2693 | var block = gone[i]; |
| 2694 | this.outgoingShots.push({ |
| 2695 | id: block.id, |
| 2696 | color: block.color, |
| 2697 | col: block.col, |
| 2698 | y: block.y, |
| 2699 | vy: block.vy |
| 2700 | }); |
| 2701 | this.score += 1; |
| 2702 | this.removeBlock(block); |
| 2703 | } |
| 2704 | }; |
| 2705 | |
| 2706 | GameCore.prototype.consumeOutgoingShots = function () |
| 2707 | { |
| 2708 | var shots = this.outgoingShots; |
| 2709 | this.outgoingShots = []; |
| 2710 | return shots; |
| 2711 | }; |
| 2712 | |
| 2713 | /** |
| 2714 | * A compact field picture for the multiplayer snapshot (miniatures). |
| 2715 | * Includes every block's col, y, color, state and flame. |
| 2716 | */ |
| 2717 | GameCore.prototype.fieldSnapshot = function () |
| 2718 | { |
| 2719 | var blocks = []; |
| 2720 | for (var i = 0; i < this.blocks.length; i++) |
| 2721 | { |
| 2722 | var b = this.blocks[i]; |
| 2723 | blocks.push({ |
| 2724 | id: b.id, |
| 2725 | col: b.col, |
| 2726 | y: Math.round(b.y * 1000) / 1000, |
| 2727 | color: b.color, |
| 2728 | state: b.state, |
| 2729 | flame: !!b.flame |
| 2730 | }); |
| 2731 | } |
| 2732 | return { |
| 2733 | score: this.score, |
| 2734 | state: this.state, |
| 2735 | width: this.width, |
| 2736 | height: this.height, |
| 2737 | locked: this.lockedColumnCount(), |
| 2738 | blocks: blocks |
| 2739 | }; |
| 2740 | }; |
| 2741 | |
| 2742 | /** |
| 2743 | * Incoming garbage from another player: a dark-grey block that falls |
| 2744 | * normally from the top of an open column. After landing it uses the |
| 2745 | * usual 3s settle and then a random colour (landFallingBlock). |
| 2746 | * `col` is optional; a random eligible column is used otherwise. |
| 2747 | * Returns the new block, or null if every column is locked or full. |
| 2748 | */ |
| 2749 | /** |
| 2750 | * Open columns a garbage block can fall into (not locked, not full). |
| 2751 | */ |
| 2752 | GameCore.prototype.eligibleGarbageColumns = function () |
| 2753 | { |
| 2754 | var eligible = []; |
| 2755 | for (var c = 0; c < this.width; c++) |
| 2756 | { |
| 2757 | if (this.isColumnLocked(c)) |
| 2758 | { continue; } |
| 2759 | if (this.groundedCount(c) >= this.height) |
| 2760 | { continue; } |
| 2761 | eligible.push(c); |
| 2762 | } |
| 2763 | return eligible; |
| 2764 | }; |
| 2765 | |
| 2766 | /** |
| 2767 | * Choose a garbage column. If `preferred` is still open, it wins; |
| 2768 | * otherwise a random eligible column (t1126). Returns null if none. |
| 2769 | */ |
| 2770 | GameCore.prototype.pickGarbageColumn = function (preferred) |
| 2771 | { |
| 2772 | var eligible = this.eligibleGarbageColumns(); |
| 2773 | if (!eligible.length) |
| 2774 | { return null; } |
| 2775 | if (preferred != null && isFinite(preferred)) |
| 2776 | { |
| 2777 | var want = preferred | 0; |
| 2778 | for (var i = 0; i < eligible.length; i++) |
| 2779 | { |
| 2780 | if (eligible[i] === want) |
| 2781 | { return want; } |
| 2782 | } |
| 2783 | } |
| 2784 | return eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 2785 | }; |
| 2786 | |
| 2787 | GameCore.prototype.ingestGarbage = function (col) |
| 2788 | { |
| 2789 | if (this.state !== 'playing') |
| 2790 | { return null; } |
| 2791 | var chosen = this.pickGarbageColumn(col); |
| 2792 | if (chosen == null) |
| 2793 | { return null; } |
| 2794 | var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING); |
| 2795 | block.vy = -this.planet.fallSpeed; |
| 2796 | block.flame = false; |
| 2797 | return block; |
| 2798 | }; |
| 2799 | |
| 2800 | GameCore.prototype.advanceSettle = function (dt) |
| 2801 | { |
| 2802 | for (var i = 0; i < this.blocks.length; i++) |
| 2803 | { |
| 2804 | var block = this.blocks[i]; |
| 2805 | if (block.state !== STATE_SETTLING) |
| 2806 | { continue; } |
| 2807 | var inLanded = block.clusterId != null |
| 2808 | && this.clusters[block.clusterId] |
| 2809 | && this.clusters[block.clusterId].phase === 'landed'; |
| 2810 | if (block.color === COLOR_GRAY) |
| 2811 | { |
| 2812 | block.settleTime += dt; |
| 2813 | if (block.settleTime >= this.settleTime) |
| 2814 | { |
| 2815 | block.color = this.randomColor(); |
| 2816 | block.settleTime = 0; |
| 2817 | block.flame = false; |
| 2818 | if (inLanded) |
| 2819 | { |
| 2820 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 2821 | block.state = STATE_SETTLING; |
| 2822 | } |
| 2823 | else |
| 2824 | { |
| 2825 | this.leaveCluster(block); |
| 2826 | this.clearFlightState(block); |
| 2827 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 2828 | } |
| 2829 | } |
| 2830 | } |
| 2831 | else if (!inLanded) |
| 2832 | { |
| 2833 | // Coloured settling that is not in a landed cluster (shouldn't |
| 2834 | // normally happen): just become still or fall, from rest. |
| 2835 | this.leaveCluster(block); |
| 2836 | this.clearFlightState(block); |
| 2837 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | var ids = Object.keys(this.clusters); |
| 2842 | for (var n = 0; n < ids.length; n++) |
| 2843 | { |
| 2844 | var cluster = this.clusters[ids[n]]; |
| 2845 | if (!cluster || cluster.phase !== 'landed') |
| 2846 | { continue; } |
| 2847 | var members = this.clusterMembers(cluster.id); |
| 2848 | var greySettling = false; |
| 2849 | for (var i = 0; i < members.length; i++) |
| 2850 | { |
| 2851 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 2852 | { greySettling = true; } |
| 2853 | } |
| 2854 | if (!greySettling) |
| 2855 | { this.releaseLandedCluster(cluster); } |
| 2856 | } |
| 2857 | }; |
| 2858 | |
| 2859 | GameCore.prototype.findMatchLines = function () |
| 2860 | { |
| 2861 | var candidates = []; |
| 2862 | for (var i = 0; i < this.blocks.length; i++) |
| 2863 | { |
| 2864 | var block = this.blocks[i]; |
| 2865 | // Coloured cargo in a just-landed cluster (SETTLING) can match |
| 2866 | // with already-resting blocks immediately (t1110). Greys have no |
| 2867 | // colour and wait until they recolor; after release they are STILL. |
| 2868 | if ((block.state === STATE_STILL || block.state === STATE_SETTLING) |
| 2869 | && block.color >= 0) |
| 2870 | { candidates.push(block); } |
| 2871 | } |
| 2872 | return this.collectLines(candidates, true); |
| 2873 | }; |
| 2874 | |
| 2875 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 2876 | { |
| 2877 | var candidates = []; |
| 2878 | for (var i = 0; i < this.blocks.length; i++) |
| 2879 | { |
| 2880 | var block = this.blocks[i]; |
| 2881 | if (block.clusterId === clusterId && block.color >= 0 |
| 2882 | && (block.state === STATE_FLYING)) |
| 2883 | { candidates.push(block); } |
| 2884 | } |
| 2885 | return this.collectLines(candidates, false); |
| 2886 | }; |
| 2887 | |
| 2888 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 2889 | { |
| 2890 | var lines = []; |
| 2891 | if (!candidates.length) |
| 2892 | { return lines; } |
| 2893 | |
| 2894 | var byRow = Object.create(null); |
| 2895 | for (var i = 0; i < candidates.length; i++) |
| 2896 | { |
| 2897 | var block = candidates[i]; |
| 2898 | var row = Math.round(block.y); |
| 2899 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 2900 | { continue; } |
| 2901 | if (!byRow[row]) |
| 2902 | { byRow[row] = []; } |
| 2903 | byRow[row].push(block); |
| 2904 | } |
| 2905 | var rows = Object.keys(byRow); |
| 2906 | for (var r = 0; r < rows.length; r++) |
| 2907 | { |
| 2908 | var list = byRow[rows[r]]; |
| 2909 | list.sort(function (a, b) { return a.col - b.col; }); |
| 2910 | this.pushRuns(lines, list, false, function (a, b) |
| 2911 | { |
| 2912 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 2913 | }); |
| 2914 | } |
| 2915 | |
| 2916 | var byCol = Object.create(null); |
| 2917 | for (var i = 0; i < candidates.length; i++) |
| 2918 | { |
| 2919 | var block = candidates[i]; |
| 2920 | if (!byCol[block.col]) |
| 2921 | { byCol[block.col] = []; } |
| 2922 | byCol[block.col].push(block); |
| 2923 | } |
| 2924 | var cols = Object.keys(byCol); |
| 2925 | for (var c = 0; c < cols.length; c++) |
| 2926 | { |
| 2927 | var list = byCol[cols[c]]; |
| 2928 | list.sort(function (a, b) { return a.y - b.y; }); |
| 2929 | this.pushRuns(lines, list, true, function (a, b) |
| 2930 | { |
| 2931 | return a.color === b.color && a.color >= 0 |
| 2932 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 2933 | }); |
| 2934 | } |
| 2935 | return lines; |
| 2936 | }; |
| 2937 | |
| 2938 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 2939 | { |
| 2940 | var start = 0; |
| 2941 | for (var i = 1; i <= list.length; i++) |
| 2942 | { |
| 2943 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 2944 | if (!same) |
| 2945 | { |
| 2946 | var length = i - start; |
| 2947 | if (length >= 3) |
| 2948 | { |
| 2949 | lines.push({ |
| 2950 | blocks: list.slice(start, i), |
| 2951 | length: length, |
| 2952 | vertical: vertical |
| 2953 | }); |
| 2954 | } |
| 2955 | start = i; |
| 2956 | } |
| 2957 | } |
| 2958 | }; |
| 2959 | |
| 2960 | GameCore.prototype.resolveMatches = function () |
| 2961 | { |
| 2962 | if (this.state !== 'playing' || this.drag) |
| 2963 | { return []; } |
| 2964 | var lines = this.findMatchLines(); |
| 2965 | if (!lines.length) |
| 2966 | { |
| 2967 | this.finishLandingChains([]); |
| 2968 | return []; |
| 2969 | } |
| 2970 | |
| 2971 | var uf = new UnionFind(); |
| 2972 | var blockById = Object.create(null); |
| 2973 | for (var i = 0; i < lines.length; i++) |
| 2974 | { |
| 2975 | var line = lines[i]; |
| 2976 | for (var j = 0; j < line.blocks.length; j++) |
| 2977 | { |
| 2978 | uf.add(line.blocks[j].id); |
| 2979 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 2980 | if (j > 0) |
| 2981 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 2982 | } |
| 2983 | } |
| 2984 | var matched = []; |
| 2985 | var seen = Object.create(null); |
| 2986 | for (var id in blockById) |
| 2987 | { |
| 2988 | if (!seen[id]) |
| 2989 | { |
| 2990 | seen[id] = true; |
| 2991 | matched.push(blockById[id]); |
| 2992 | } |
| 2993 | } |
| 2994 | for (var i = 0; i < matched.length; i++) |
| 2995 | { |
| 2996 | for (var j = i + 1; j < matched.length; j++) |
| 2997 | { |
| 2998 | if (matched[i].col === matched[j].col) |
| 2999 | { uf.union(matched[i].id, matched[j].id); } |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | var groups = Object.create(null); |
| 3004 | for (var i = 0; i < matched.length; i++) |
| 3005 | { |
| 3006 | var root = uf.find(matched[i].id); |
| 3007 | if (!groups[root]) |
| 3008 | { groups[root] = []; } |
| 3009 | groups[root].push(matched[i]); |
| 3010 | } |
| 3011 | |
| 3012 | var launched = []; |
| 3013 | for (var root in groups) |
| 3014 | { |
| 3015 | var group = groups[root]; |
| 3016 | var height = 0; |
| 3017 | for (var i = 0; i < lines.length; i++) |
| 3018 | { |
| 3019 | var line = lines[i]; |
| 3020 | var overlaps = false; |
| 3021 | for (var j = 0; j < line.blocks.length; j++) |
| 3022 | { |
| 3023 | for (var k = 0; k < group.length; k++) |
| 3024 | { |
| 3025 | if (line.blocks[j].id === group[k].id) |
| 3026 | { overlaps = true; } |
| 3027 | } |
| 3028 | } |
| 3029 | if (overlaps) |
| 3030 | { |
| 3031 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 3032 | if (h > height) |
| 3033 | { height = h; } |
| 3034 | } |
| 3035 | } |
| 3036 | var chain = this.landingChainFor(group); |
| 3037 | var bonusMul = 1 + LANDING_CHAIN_BONUS * chain; |
| 3038 | height *= bonusMul; |
| 3039 | this.launchCluster(group, height, { landingChain: chain }); |
| 3040 | launched.push({ |
| 3041 | blocks: group, |
| 3042 | jumpHeight: height, |
| 3043 | landingChain: chain, |
| 3044 | chainBonus: bonusMul |
| 3045 | }); |
| 3046 | } |
| 3047 | this.finishLandingChains(launched); |
| 3048 | this.updateChainIndicator(launched); |
| 3049 | return launched; |
| 3050 | }; |
| 3051 | |
| 3052 | /** |
| 3053 | * Gray the matched blocks, take everything sitting on top of them in |
| 3054 | * those columns, link them, and launch with jump height `height`. |
| 3055 | */ |
| 3056 | GameCore.prototype.launchCluster = function (matchedBlocks, height, opts) |
| 3057 | { |
| 3058 | opts = opts || {}; |
| 3059 | var lowestInCol = Object.create(null); |
| 3060 | var matchedIds = Object.create(null); |
| 3061 | for (var i = 0; i < matchedBlocks.length; i++) |
| 3062 | { |
| 3063 | var block = matchedBlocks[i]; |
| 3064 | matchedIds[block.id] = true; |
| 3065 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 3066 | { lowestInCol[block.col] = block.y; } |
| 3067 | } |
| 3068 | |
| 3069 | var clusterId = this.nextClusterId++; |
| 3070 | var cluster = { |
| 3071 | id: clusterId, |
| 3072 | jumpHeight: height, |
| 3073 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 3074 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 3075 | phase: 'up', |
| 3076 | landingChain: opts.landingChain || 0, |
| 3077 | landedPending: false |
| 3078 | }; |
| 3079 | this.clusters[clusterId] = cluster; |
| 3080 | |
| 3081 | var riders = []; |
| 3082 | var cols = Object.keys(lowestInCol); |
| 3083 | for (var c = 0; c < cols.length; c++) |
| 3084 | { |
| 3085 | var col = +cols[c]; |
| 3086 | var lowest = lowestInCol[col]; |
| 3087 | var list = this.blocksInColumn(col); |
| 3088 | for (var i = 0; i < list.length; i++) |
| 3089 | { |
| 3090 | var block = list[i]; |
| 3091 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 3092 | { |
| 3093 | if (block.state !== STATE_SETTLING) |
| 3094 | { continue; } |
| 3095 | } |
| 3096 | if (block.y + ALIGN_EPS >= lowest) |
| 3097 | { riders.push(block); } |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | for (var i = 0; i < riders.length; i++) |
| 3102 | { |
| 3103 | var block = riders[i]; |
| 3104 | var isMatch = !!matchedIds[block.id]; |
| 3105 | if (isMatch || block.color === COLOR_GRAY) |
| 3106 | { |
| 3107 | block.color = COLOR_GRAY; |
| 3108 | block.flame = true; |
| 3109 | } |
| 3110 | else |
| 3111 | { block.flame = false; } |
| 3112 | block.state = STATE_FLYING; |
| 3113 | block.clusterId = clusterId; |
| 3114 | block.vy = cluster.vy; |
| 3115 | block.settleTime = 0; |
| 3116 | } |
| 3117 | this.prepareClusterBody(cluster); |
| 3118 | return cluster; |
| 3119 | }; |
| 3120 | |
| 3121 | GameCore.prototype.checkLocks = function () |
| 3122 | { |
| 3123 | for (var col = 0; col < this.width; col++) |
| 3124 | { |
| 3125 | if (this.isColumnLocked(col)) |
| 3126 | { continue; } |
| 3127 | if (this.groundedCount(col) < this.height) |
| 3128 | { continue; } |
| 3129 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 3130 | var filled = true; |
| 3131 | for (var row = 0; row < this.height; row++) |
| 3132 | { |
| 3133 | var block = this.blockAt(col, row); |
| 3134 | if (!block || !this.isGrounded(block)) |
| 3135 | { |
| 3136 | filled = false; |
| 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | if (!filled) |
| 3141 | { continue; } |
| 3142 | var list = this.blocksInColumn(col); |
| 3143 | for (var i = 0; i < list.length; i++) |
| 3144 | { |
| 3145 | var block = list[i]; |
| 3146 | if (!this.isGrounded(block)) |
| 3147 | { continue; } |
| 3148 | block.state = STATE_LOCKED; |
| 3149 | block.color = COLOR_GRAY; |
| 3150 | block.flame = false; |
| 3151 | block.vy = 0; |
| 3152 | block.settleTime = 0; |
| 3153 | if (block.clusterId != null) |
| 3154 | { this.leaveCluster(block); } |
| 3155 | } |
| 3156 | } |
| 3157 | }; |
| 3158 | |
| 3159 | GameCore.prototype.checkGameOver = function () |
| 3160 | { |
| 3161 | if (this.state !== 'playing') |
| 3162 | { return; } |
| 3163 | for (var col = 0; col < this.width; col++) |
| 3164 | { |
| 3165 | if (!this.isColumnLocked(col)) |
| 3166 | { return; } |
| 3167 | } |
| 3168 | this.state = 'gameover'; |
| 3169 | this.gameOverTime = 0; |
| 3170 | this.drag = null; |
| 3171 | }; |
| 3172 | |
| 3173 | /* ---------- dragging -------------------------------------------------- */ |
| 3174 | |
| 3175 | GameCore.prototype.beginDrag = function (col, y) |
| 3176 | { |
| 3177 | if (this.state !== 'playing') |
| 3178 | { return false; } |
| 3179 | var block = this.snapDragBlock(col, y); |
| 3180 | if (!block) |
| 3181 | { return false; } |
| 3182 | var top = this.columnStackTopRow(block.col); |
| 3183 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 3184 | this.drag = { |
| 3185 | blockId: block.id, |
| 3186 | pointerY: y, |
| 3187 | startY: y, |
| 3188 | accum: 0, |
| 3189 | launchIntent: false, |
| 3190 | startedAsTop: startedAsTop |
| 3191 | }; |
| 3192 | return true; |
| 3193 | }; |
| 3194 | |
| 3195 | /** |
| 3196 | * Instant grab, cell-based (t1109). The press is mapped to the grid cell |
| 3197 | * floor(col), floor(y); any press inside that cell — corners, padding, |
| 3198 | * gaps between sprites, a visually tweened or bouncing sprite — grabs |
| 3199 | * the block that occupies it. This uses simulation cells, not the drawn |
| 3200 | * sprite bounds. If no block sits in that cell (mid-animation, a block |
| 3201 | * whose y is off the integer), fall back to the nearest grabbable block |
| 3202 | * in the same column within SNAP_Y (about half a tile). A small |
| 3203 | * horizontal slop still consults a neighbouring column when the press |
| 3204 | * is near the edge and the primary cell is empty. |
| 3205 | */ |
| 3206 | GameCore.prototype.snapDragBlock = function (col, y) |
| 3207 | { |
| 3208 | var raw = col; |
| 3209 | var c0 = Math.floor(raw + 1e-9); |
| 3210 | var row = Math.floor(y + 1e-9); |
| 3211 | var frac = raw - c0; |
| 3212 | |
| 3213 | if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0)) |
| 3214 | { |
| 3215 | var exact = this.draggableInCell(c0, row); |
| 3216 | if (exact) |
| 3217 | { return exact; } |
| 3218 | var near = this.nearestDraggableInColumn(c0, y, SNAP_Y); |
| 3219 | if (near) |
| 3220 | { return near; } |
| 3221 | } |
| 3222 | |
| 3223 | // Primary cell empty: allow a small horizontal slop into a neighbour. |
| 3224 | var neighbours = []; |
| 3225 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 3226 | { neighbours.push(c0 - 1); } |
| 3227 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 3228 | { neighbours.push(c0 + 1); } |
| 3229 | var best = null; |
| 3230 | var bestDist = Infinity; |
| 3231 | for (var n = 0; n < neighbours.length; n++) |
| 3232 | { |
| 3233 | var cc = neighbours[n]; |
| 3234 | if (!this.columnAllowsDrag(cc)) |
| 3235 | { continue; } |
| 3236 | var hit = this.draggableInCell(cc, row) || this.nearestDraggableInColumn(cc, y, SNAP_Y); |
| 3237 | if (!hit) |
| 3238 | { continue; } |
| 3239 | var dist = this.pointerDistToBlock(hit, raw, y); |
| 3240 | if (dist < bestDist) |
| 3241 | { |
| 3242 | bestDist = dist; |
| 3243 | best = hit; |
| 3244 | } |
| 3245 | } |
| 3246 | return best; |
| 3247 | }; |
| 3248 | |
| 3249 | /** The draggable block that occupies grid cell (col, row), if any. */ |
| 3250 | GameCore.prototype.draggableInCell = function (col, row) |
| 3251 | { |
| 3252 | var best = null; |
| 3253 | var bestOverlap = 0; |
| 3254 | var list = this.blocksInColumn(col); |
| 3255 | for (var i = 0; i < list.length; i++) |
| 3256 | { |
| 3257 | var block = list[i]; |
| 3258 | if (!this.isDraggableBlock(block)) |
| 3259 | { continue; } |
| 3260 | var lo = Math.max(block.y, row); |
| 3261 | var hi = Math.min(block.y + 1, row + 1); |
| 3262 | var overlap = hi - lo; |
| 3263 | if (overlap > bestOverlap + 1e-12) |
| 3264 | { |
| 3265 | bestOverlap = overlap; |
| 3266 | best = block; |
| 3267 | } |
| 3268 | } |
| 3269 | if (best && bestOverlap > 1e-9) |
| 3270 | { return best; } |
| 3271 | return null; |
| 3272 | }; |
| 3273 | |
| 3274 | /** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */ |
| 3275 | GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist) |
| 3276 | { |
| 3277 | var best = null; |
| 3278 | var bestDist = Infinity; |
| 3279 | var list = this.blocksInColumn(col); |
| 3280 | for (var i = 0; i < list.length; i++) |
| 3281 | { |
| 3282 | var block = list[i]; |
| 3283 | if (!this.isDraggableBlock(block)) |
| 3284 | { continue; } |
| 3285 | var dist = this.pointerDistToBlock(block, col + 0.5, y); |
| 3286 | if (dist < bestDist) |
| 3287 | { |
| 3288 | bestDist = dist; |
| 3289 | best = block; |
| 3290 | } |
| 3291 | } |
| 3292 | if (!best || bestDist > maxDist) |
| 3293 | { return null; } |
| 3294 | return best; |
| 3295 | }; |
| 3296 | |
| 3297 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 3298 | { |
| 3299 | var dx; |
| 3300 | if (col >= block.col && col < block.col + 1) |
| 3301 | { dx = 0; } |
| 3302 | else if (col < block.col) |
| 3303 | { dx = block.col - col; } |
| 3304 | else |
| 3305 | { dx = col - (block.col + 1); } |
| 3306 | var dy; |
| 3307 | if (y >= block.y && y < block.y + 1) |
| 3308 | { dy = 0; } |
| 3309 | else if (y < block.y) |
| 3310 | { dy = block.y - y; } |
| 3311 | else |
| 3312 | { dy = y - (block.y + 1); } |
| 3313 | return Math.sqrt(dx * dx + dy * dy); |
| 3314 | }; |
| 3315 | |
| 3316 | GameCore.prototype.moveDrag = function (y) |
| 3317 | { |
| 3318 | if (!this.drag) |
| 3319 | { return; } |
| 3320 | var block = this.blockById(this.drag.blockId); |
| 3321 | if (!block || !this.isDraggableBlock(block)) |
| 3322 | { |
| 3323 | this.drag = null; |
| 3324 | return; |
| 3325 | } |
| 3326 | if (!this.columnAllowsDrag(block.col)) |
| 3327 | { |
| 3328 | this.drag = null; |
| 3329 | return; |
| 3330 | } |
| 3331 | this.drag.pointerY = y; |
| 3332 | // 1:1 tracking: the target row is the one under the pointer, clamped |
| 3333 | // to the still stack. Swap step by step with each neighbour, and stop |
| 3334 | // exactly there. When the pointer stops, nothing else moves. |
| 3335 | // Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold |
| 3336 | // used to jump a full cell on a short pull, and one large event |
| 3337 | // could walk the entire column (the "jumps all the way" bug). |
| 3338 | var top = this.columnStackTopRow(block.col); |
| 3339 | var bottom = this.columnStackBottomRow(block.col); |
| 3340 | var target = Math.floor(y + 1e-12); |
| 3341 | if (bottom < 0) |
| 3342 | { bottom = 0; } |
| 3343 | if (target < bottom) |
| 3344 | { target = bottom; } |
| 3345 | if (target > top) |
| 3346 | { target = top; } |
| 3347 | this.swapToward(block, target); |
| 3348 | |
| 3349 | top = this.columnStackTopRow(block.col); |
| 3350 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 3351 | if (this.drag.startedAsTop) |
| 3352 | { |
| 3353 | // Already the topmost block: pulling upward past its own position |
| 3354 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 3355 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 3356 | } |
| 3357 | else |
| 3358 | { |
| 3359 | // Started mid-stack: swap first, and only launch once the pointer |
| 3360 | // is pulled into the empty space above the (new) top. |
| 3361 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 3362 | } |
| 3363 | }; |
| 3364 | |
| 3365 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 3366 | GameCore.prototype.swapOne = function (block, dir) |
| 3367 | { |
| 3368 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 3369 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 3370 | { |
| 3371 | this.swapBlocks(block, neighbor); |
| 3372 | return true; |
| 3373 | } |
| 3374 | return false; |
| 3375 | }; |
| 3376 | |
| 3377 | GameCore.prototype.endDrag = function () |
| 3378 | { |
| 3379 | if (!this.drag) |
| 3380 | { return; } |
| 3381 | var block = this.blockById(this.drag.blockId); |
| 3382 | var launch = this.drag.launchIntent; |
| 3383 | this.drag = null; |
| 3384 | if (launch && block && block.state === STATE_STILL) |
| 3385 | { this.startManualLaunch(block); } |
| 3386 | else |
| 3387 | { this.resolveMatches(); } |
| 3388 | }; |
| 3389 | |
| 3390 | GameCore.prototype.blockById = function (id) |
| 3391 | { |
| 3392 | for (var i = 0; i < this.blocks.length; i++) |
| 3393 | { |
| 3394 | if (this.blocks[i].id === id) |
| 3395 | { return this.blocks[i]; } |
| 3396 | } |
| 3397 | return null; |
| 3398 | }; |
| 3399 | |
| 3400 | GameCore.prototype.columnStackTopRow = function (col) |
| 3401 | { |
| 3402 | var top = -1; |
| 3403 | var list = this.blocksInColumn(col); |
| 3404 | for (var i = 0; i < list.length; i++) |
| 3405 | { |
| 3406 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 3407 | { top = list[i].y; } |
| 3408 | } |
| 3409 | return top; |
| 3410 | }; |
| 3411 | |
| 3412 | GameCore.prototype.columnStackBottomRow = function (col) |
| 3413 | { |
| 3414 | var bottom = Infinity; |
| 3415 | var list = this.blocksInColumn(col); |
| 3416 | for (var i = 0; i < list.length; i++) |
| 3417 | { |
| 3418 | if (this.isGrounded(list[i]) && list[i].y < bottom) |
| 3419 | { bottom = list[i].y; } |
| 3420 | } |
| 3421 | return bottom === Infinity ? 0 : bottom; |
| 3422 | }; |
| 3423 | |
| 3424 | GameCore.prototype.swapToward = function (block, targetY) |
| 3425 | { |
| 3426 | var guard = 0; |
| 3427 | while (guard++ < this.height + 2) |
| 3428 | { |
| 3429 | var current = block.y; |
| 3430 | if (targetY > current + 0.5) |
| 3431 | { |
| 3432 | var above = this.blockAt(block.col, current + 1); |
| 3433 | if (above && this.isGrounded(above) && above !== block) |
| 3434 | { this.swapBlocks(block, above); } |
| 3435 | else |
| 3436 | { break; } |
| 3437 | } |
| 3438 | else if (targetY < current - 0.5) |
| 3439 | { |
| 3440 | var below = this.blockAt(block.col, current - 1); |
| 3441 | if (below && this.isGrounded(below) && below !== block) |
| 3442 | { this.swapBlocks(block, below); } |
| 3443 | else |
| 3444 | { break; } |
| 3445 | } |
| 3446 | else |
| 3447 | { break; } |
| 3448 | } |
| 3449 | }; |
| 3450 | |
| 3451 | GameCore.prototype.swapBlocks = function (a, b) |
| 3452 | { |
| 3453 | var ay = a.y; |
| 3454 | var by = b.y; |
| 3455 | a.y = by; |
| 3456 | b.y = ay; |
| 3457 | this.transferLandedMembership(a, b); |
| 3458 | if (!this.swapEvents) |
| 3459 | { this.swapEvents = []; } |
| 3460 | this.swapEvents.push({ id: a.id, fromY: ay, toY: by, col: a.col }); |
| 3461 | this.swapEvents.push({ id: b.id, fromY: by, toY: ay, col: b.col }); |
| 3462 | }; |
| 3463 | |
| 3464 | GameCore.prototype.consumeSwapEvents = function () |
| 3465 | { |
| 3466 | var ev = this.swapEvents || []; |
| 3467 | this.swapEvents = []; |
| 3468 | return ev; |
| 3469 | }; |
| 3470 | |
| 3471 | /** |
| 3472 | * Highest landing-chain counter among just-landed members of this |
| 3473 | * match group, plus one. Returns 0 when the match did not come from |
| 3474 | * a cluster landing (player/bot swap, mid-air, etc.). |
| 3475 | */ |
| 3476 | GameCore.prototype.landingChainFor = function (group) |
| 3477 | { |
| 3478 | var maxChain = 0; |
| 3479 | var fromLanding = false; |
| 3480 | for (var i = 0; i < group.length; i++) |
| 3481 | { |
| 3482 | var block = group[i]; |
| 3483 | if (block.justLanded) |
| 3484 | { |
| 3485 | fromLanding = true; |
| 3486 | if ((block.landingChain || 0) > maxChain) |
| 3487 | { maxChain = block.landingChain || 0; } |
| 3488 | } |
| 3489 | if (block.clusterId != null) |
| 3490 | { |
| 3491 | var cluster = this.clusters[block.clusterId]; |
| 3492 | if (cluster && cluster.landedPending) |
| 3493 | { |
| 3494 | fromLanding = true; |
| 3495 | if ((cluster.landingChain || 0) > maxChain) |
| 3496 | { maxChain = cluster.landingChain || 0; } |
| 3497 | } |
| 3498 | } |
| 3499 | } |
| 3500 | if (!fromLanding) |
| 3501 | { return 0; } |
| 3502 | return maxChain + 1; |
| 3503 | }; |
| 3504 | |
| 3505 | /** |
| 3506 | * After resolveMatches: clear the landing-pending flag and reset the |
| 3507 | * chain on landings that did not make a match. A cluster that is still |
| 3508 | * settling (greys not yet recolored) keeps landedPending so a later |
| 3509 | * post-recolor match in this same landing still gets the bonus. |
| 3510 | */ |
| 3511 | GameCore.prototype.finishLandingChains = function (launched) |
| 3512 | { |
| 3513 | var launchedIds = Object.create(null); |
| 3514 | launched = launched || []; |
| 3515 | for (var i = 0; i < launched.length; i++) |
| 3516 | { |
| 3517 | var blocks = launched[i].blocks || []; |
| 3518 | for (var j = 0; j < blocks.length; j++) |
| 3519 | { launchedIds[blocks[j].id] = true; } |
| 3520 | } |
| 3521 | |
| 3522 | for (var i = 0; i < this.blocks.length; i++) |
| 3523 | { |
| 3524 | var block = this.blocks[i]; |
| 3525 | if (!block.justLanded) |
| 3526 | { continue; } |
| 3527 | if (!launchedIds[block.id]) |
| 3528 | { block.landingChain = 0; } |
| 3529 | block.justLanded = false; |
| 3530 | } |
| 3531 | |
| 3532 | var ids = Object.keys(this.clusters); |
| 3533 | for (var c = 0; c < ids.length; c++) |
| 3534 | { |
| 3535 | var cluster = this.clusters[ids[c]]; |
| 3536 | if (!cluster || !cluster.landedPending) |
| 3537 | { continue; } |
| 3538 | var members = this.clusterMembers(cluster.id); |
| 3539 | var anyLaunched = false; |
| 3540 | var stillSettling = false; |
| 3541 | for (var i = 0; i < members.length; i++) |
| 3542 | { |
| 3543 | if (launchedIds[members[i].id]) |
| 3544 | { anyLaunched = true; } |
| 3545 | if (members[i].state === STATE_SETTLING) |
| 3546 | { stillSettling = true; } |
| 3547 | } |
| 3548 | if (anyLaunched) |
| 3549 | { |
| 3550 | cluster.landingChain = 0; |
| 3551 | cluster.landedPending = false; |
| 3552 | } |
| 3553 | else if (!stillSettling) |
| 3554 | { |
| 3555 | // Released leftovers or a fully-still landed group that |
| 3556 | // already had its match opportunity and missed it. |
| 3557 | cluster.landingChain = 0; |
| 3558 | cluster.landedPending = false; |
| 3559 | } |
| 3560 | // else: still settling, no match yet — keep landedPending. |
| 3561 | } |
| 3562 | }; |
| 3563 | |
| 3564 | GameCore.prototype.updateChainIndicator = function (launched) |
| 3565 | { |
| 3566 | var maxChain = 0; |
| 3567 | for (var i = 0; i < launched.length; i++) |
| 3568 | { |
| 3569 | var ch = launched[i].landingChain || 0; |
| 3570 | if (ch > maxChain) |
| 3571 | { maxChain = ch; } |
| 3572 | } |
| 3573 | if (maxChain >= 1) |
| 3574 | { |
| 3575 | this.lastChainIndicator = { |
| 3576 | chain: maxChain, |
| 3577 | bonusPct: Math.round(maxChain * LANDING_CHAIN_BONUS * 100) |
| 3578 | }; |
| 3579 | return; |
| 3580 | } |
| 3581 | if (!this.hasActiveLandingChain()) |
| 3582 | { this.lastChainIndicator = null; } |
| 3583 | }; |
| 3584 | |
| 3585 | GameCore.prototype.hasActiveLandingChain = function () |
| 3586 | { |
| 3587 | var ids = Object.keys(this.clusters); |
| 3588 | for (var i = 0; i < ids.length; i++) |
| 3589 | { |
| 3590 | var cluster = this.clusters[ids[i]]; |
| 3591 | if (cluster && (cluster.landingChain || 0) >= 1 |
| 3592 | && cluster.phase !== 'landed') |
| 3593 | { return true; } |
| 3594 | } |
| 3595 | return false; |
| 3596 | }; |
| 3597 | |
| 3598 | /** |
| 3599 | * A landed cluster (grey rockets or linked cargo resting on the ground) |
| 3600 | * is a swappable part of the contiguous grounded stack. Flying, falling |
| 3601 | * and manual blocks are not. A locked column is not. |
| 3602 | */ |
| 3603 | GameCore.prototype.isDraggableBlock = function (block) |
| 3604 | { |
| 3605 | if (!block) |
| 3606 | { return false; } |
| 3607 | if (block.state === STATE_LOCKED || block.state === STATE_FLYING |
| 3608 | || block.state === STATE_FALLING || block.state === STATE_MANUAL) |
| 3609 | { return false; } |
| 3610 | if (block.clusterId != null) |
| 3611 | { |
| 3612 | var cluster = this.clusters[block.clusterId]; |
| 3613 | // Flying (rising / descending) clusters stay non-draggable. |
| 3614 | if (cluster && cluster.phase !== 'landed') |
| 3615 | { return false; } |
| 3616 | } |
| 3617 | return block.state === STATE_STILL || block.state === STATE_SETTLING; |
| 3618 | }; |
| 3619 | |
| 3620 | /** |
| 3621 | * The grounded contiguous stack in this column can be dragged, even if a |
| 3622 | * flying cluster or a falling spawn occupies the air above it. Only a |
| 3623 | * locked column is blocked as a whole. |
| 3624 | */ |
| 3625 | GameCore.prototype.columnAllowsDrag = function (col) |
| 3626 | { |
| 3627 | return !this.isColumnLocked(col); |
| 3628 | }; |
| 3629 | |
| 3630 | GameCore.prototype.landedClusterId = function (block) |
| 3631 | { |
| 3632 | if (!block || block.clusterId == null) |
| 3633 | { return null; } |
| 3634 | var cluster = this.clusters[block.clusterId]; |
| 3635 | if (!cluster || cluster.phase !== 'landed') |
| 3636 | { return null; } |
| 3637 | return block.clusterId; |
| 3638 | }; |
| 3639 | |
| 3640 | /** |
| 3641 | * After a swap: leaving a landed cluster clears membership; joining one |
| 3642 | * makes the block part of that rigid group. Same-cluster swaps only |
| 3643 | * exchange positions. See docs/ASSUMPTIONS.md (t1107). |
| 3644 | */ |
| 3645 | GameCore.prototype.transferLandedMembership = function (a, b) |
| 3646 | { |
| 3647 | var ca = this.landedClusterId(a); |
| 3648 | var cb = this.landedClusterId(b); |
| 3649 | if (ca === cb) |
| 3650 | { return; } |
| 3651 | // Join first so leaveCluster does not delete a cluster that still |
| 3652 | // has the incoming member. |
| 3653 | this.applyLandedMembership(b, ca); |
| 3654 | this.applyLandedMembership(a, cb); |
| 3655 | }; |
| 3656 | |
| 3657 | GameCore.prototype.applyLandedMembership = function (block, clusterId) |
| 3658 | { |
| 3659 | if (clusterId == null) |
| 3660 | { |
| 3661 | var wasGrey = block.color === COLOR_GRAY; |
| 3662 | var wasSettling = block.state === STATE_SETTLING; |
| 3663 | this.leaveCluster(block); |
| 3664 | if (wasGrey && wasSettling) |
| 3665 | { |
| 3666 | // Keep settling and recolor on its own, same remaining timer. |
| 3667 | block.state = STATE_SETTLING; |
| 3668 | } |
| 3669 | else if (wasSettling) |
| 3670 | { |
| 3671 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 3672 | } |
| 3673 | return; |
| 3674 | } |
| 3675 | if (!this.clusters[clusterId]) |
| 3676 | { return; } |
| 3677 | block.clusterId = clusterId; |
| 3678 | block.flame = false; |
| 3679 | if (block.state === STATE_STILL || block.state === STATE_SETTLING) |
| 3680 | { block.state = STATE_SETTLING; } |
| 3681 | }; |
| 3682 | |
| 3683 | /** |
| 3684 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 3685 | * Returns the moved block, or null if the column does not allow a drag. |
| 3686 | */ |
| 3687 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 3688 | { |
| 3689 | if (!this.columnAllowsDrag(col)) |
| 3690 | { return null; } |
| 3691 | var block = this.blockAt(col, fromRow); |
| 3692 | if (!block) |
| 3693 | { return null; } |
| 3694 | this.swapToward(block, toRow); |
| 3695 | return block; |
| 3696 | }; |
| 3697 | |
| 3698 | /** |
| 3699 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 3700 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 3701 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 3702 | * their color with a marker in `flying`. |
| 3703 | */ |
| 3704 | GameCore.prototype.snapshot = function () |
| 3705 | { |
| 3706 | var grid = []; |
| 3707 | for (var r = 0; r < this.height; r++) |
| 3708 | { |
| 3709 | var row = []; |
| 3710 | for (var c = 0; c < this.width; c++) |
| 3711 | { row.push('.'); } |
| 3712 | grid.push(row); |
| 3713 | } |
| 3714 | var airborne = []; |
| 3715 | for (var i = 0; i < this.blocks.length; i++) |
| 3716 | { |
| 3717 | var block = this.blocks[i]; |
| 3718 | var ch; |
| 3719 | if (block.state === STATE_LOCKED) |
| 3720 | { ch = 'L'; } |
| 3721 | else if (block.color === COLOR_GRAY) |
| 3722 | { ch = 'g'; } |
| 3723 | else |
| 3724 | { ch = String(block.color); } |
| 3725 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 3726 | { |
| 3727 | var row = Math.round(block.y); |
| 3728 | if (row >= 0 && row < this.height) |
| 3729 | { grid[row][block.col] = ch; } |
| 3730 | } |
| 3731 | else |
| 3732 | { |
| 3733 | airborne.push({ |
| 3734 | id: block.id, |
| 3735 | col: block.col, |
| 3736 | y: block.y, |
| 3737 | color: block.color, |
| 3738 | state: block.state, |
| 3739 | clusterId: block.clusterId, |
| 3740 | flame: block.flame, |
| 3741 | vy: block.vy |
| 3742 | }); |
| 3743 | } |
| 3744 | } |
| 3745 | var lines = []; |
| 3746 | for (var r = this.height - 1; r >= 0; r--) |
| 3747 | { lines.push(grid[r].join('')); } |
| 3748 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 3749 | }; |
| 3750 | |
| 3751 | var GameCoreAPI = { |
| 3752 | GameCore: GameCore, |
| 3753 | COLOR_GRAY: COLOR_GRAY, |
| 3754 | STATE_STILL: STATE_STILL, |
| 3755 | STATE_FALLING: STATE_FALLING, |
| 3756 | STATE_FLYING: STATE_FLYING, |
| 3757 | STATE_SETTLING: STATE_SETTLING, |
| 3758 | STATE_LOCKED: STATE_LOCKED, |
| 3759 | STATE_MANUAL: STATE_MANUAL |
| 3760 | }; |
| 3761 | |
| 3762 | if (typeof module !== 'undefined' && module.exports) |
| 3763 | { module.exports = GameCoreAPI; } |
| 3764 | else if (typeof globalThis !== 'undefined') |
| 3765 | { |
| 3766 | globalThis.GameCore = GameCore; |
| 3767 | globalThis.GameCoreAPI = GameCoreAPI; |
| 3768 | } |
| 3769 | |
| 3770 | /* |
| 3771 | * End File: |
| 3772 | * /workspace/meteors/src/gameCore.js |
| 3773 | */ |
| 3774 | |
| 3775 | /* |
| 3776 | * Start File: |
| 3777 | * /workspace/meteors/src/miniLayout.js |
| 3778 | */ |
| 3779 | 'use strict'; |
| 3780 | |
| 3781 | /** |
| 3782 | * Opponent miniature layout for online and Offline Strijd |
| 3783 | * (t1125, t1127, t1129, t1131, t1133). |
| 3784 | * Wide screens: minis sit beside the field, anchored to the outer |
| 3785 | * screen edges, filling inward (1 = left outer, 2 = right outer, |
| 3786 | * 3 = left next inward, …). Extra minis wrap to a new row upward |
| 3787 | * only when a side's row is full. Narrow / portrait: a row above |
| 3788 | * the field. |
| 3789 | */ |
| 3790 | |
| 3791 | var MiniLayout = (function () |
| 3792 | { |
| 3793 | |
| 3794 | var GAP = 12; |
| 3795 | var VGAP = 8; |
| 3796 | var EDGE = 16; |
| 3797 | var MIN_TILE_FOR_SIDES = 18; |
| 3798 | var LABEL_H = 32; |
| 3799 | /* Extra space above the field in the wide (sides) layout so |
| 3800 | enemy↔player arcs can route around the top edge (t1129). */ |
| 3801 | var ARC_TOP_PAD = 100; |
| 3802 | /* Below this width, or in a narrow portrait, keep the top row (t1131). */ |
| 3803 | var PORTRAIT_NARROW = 700; |
| 3804 | var PORTRAIT_MAX = 850; |
| 3805 | |
| 3806 | function preferredMini () |
| 3807 | { |
| 3808 | return { w: 80, h: 104, outerW: 90, outerH: 104 + LABEL_H }; |
| 3809 | } |
| 3810 | |
| 3811 | function canvasSize (n, mode, availH, rows) |
| 3812 | { |
| 3813 | n = n || 0; |
| 3814 | if (mode === 'sides') |
| 3815 | { |
| 3816 | rows = Math.max(1, rows || 1); |
| 3817 | if (availH && rows > 1) |
| 3818 | { |
| 3819 | var maxOuter = Math.floor((availH - (rows - 1) * VGAP) / rows); |
| 3820 | var ch = Math.max(40, Math.min(104, maxOuter - LABEL_H)); |
| 3821 | var cw = Math.max(32, Math.round(ch * 10 / 13)); |
| 3822 | return { |
| 3823 | w: cw, |
| 3824 | h: ch, |
| 3825 | outerW: Math.max(cw + 8, 56), |
| 3826 | outerH: ch + LABEL_H |
| 3827 | }; |
| 3828 | } |
| 3829 | return preferredMini(); |
| 3830 | } |
| 3831 | if (n >= 10) |
| 3832 | { return { w: 40, h: 52, outerW: 56, outerH: 52 + LABEL_H }; } |
| 3833 | if (n >= 6) |
| 3834 | { return { w: 56, h: 73, outerW: 68, outerH: 73 + LABEL_H }; } |
| 3835 | return { w: 80, h: 104, outerW: 90, outerH: 104 + LABEL_H }; |
| 3836 | } |
| 3837 | |
| 3838 | /** |
| 3839 | * How many minis fit in one row on a side, between the screen edge |
| 3840 | * and the field: EDGE + n*outerW + n*GAP <= sideWidth. |
| 3841 | */ |
| 3842 | function colsThatFit (sideWidth, mini) |
| 3843 | { |
| 3844 | if (!mini || !mini.outerW) |
| 3845 | { return 0; } |
| 3846 | var stride = mini.outerW + GAP; |
| 3847 | if (stride <= 0) |
| 3848 | { return 0; } |
| 3849 | return Math.max(0, Math.floor((sideWidth - EDGE) / stride)); |
| 3850 | } |
| 3851 | |
| 3852 | /** |
| 3853 | * Place n opponents. Anchored to the OUTER screen edges, filling |
| 3854 | * inward toward the field (t1133). Row 0 is bottom-aligned with the |
| 3855 | * field; further rows stack upward only after a side's row is full |
| 3856 | * (t1127). |
| 3857 | * |
| 3858 | * Index 0 → left outer, 1 → right outer, 2 → left next inward, … |
| 3859 | */ |
| 3860 | function sidePositions (n, fieldX, fieldY, fieldW, fieldH, mini, hudH, vw, colsPerSide) |
| 3861 | { |
| 3862 | var positions = []; |
| 3863 | if (n <= 0) |
| 3864 | { return positions; } |
| 3865 | vw = vw || (fieldX * 2 + fieldW); |
| 3866 | var leftSpace = fieldX; |
| 3867 | var rightSpace = vw - (fieldX + fieldW); |
| 3868 | var colsL = colsPerSide != null ? colsPerSide : Math.max(1, colsThatFit(leftSpace, mini)); |
| 3869 | var colsR = colsPerSide != null ? colsPerSide : Math.max(1, colsThatFit(rightSpace, mini)); |
| 3870 | if (colsL < 1) |
| 3871 | { colsL = 1; } |
| 3872 | if (colsR < 1) |
| 3873 | { colsR = 1; } |
| 3874 | |
| 3875 | var leftCount = Math.ceil(n / 2); |
| 3876 | var rightCount = Math.floor(n / 2); |
| 3877 | var rows = Math.max( |
| 3878 | Math.ceil(leftCount / colsL), |
| 3879 | rightCount ? Math.ceil(rightCount / colsR) : 1 |
| 3880 | ); |
| 3881 | var stackH = rows * mini.outerH + Math.max(0, rows - 1) * VGAP; |
| 3882 | var bottom = fieldY + fieldH; |
| 3883 | var top = bottom - stackH; |
| 3884 | if (top < hudH + 4) |
| 3885 | { |
| 3886 | top = hudH + 4; |
| 3887 | bottom = top + stackH; |
| 3888 | } |
| 3889 | |
| 3890 | for (var i = 0; i < n; i++) |
| 3891 | { |
| 3892 | var isLeft = (i % 2) === 0; |
| 3893 | var slot = Math.floor(i / 2); |
| 3894 | var cols = isLeft ? colsL : colsR; |
| 3895 | var row = Math.floor(slot / cols); |
| 3896 | var fromOuter = slot % cols; |
| 3897 | var x = isLeft |
| 3898 | ? EDGE + fromOuter * (mini.outerW + GAP) |
| 3899 | : vw - EDGE - mini.outerW - fromOuter * (mini.outerW + GAP); |
| 3900 | var y = bottom - (row + 1) * mini.outerH - row * VGAP; |
| 3901 | positions.push({ |
| 3902 | index: i, |
| 3903 | side: isLeft ? 'left' : 'right', |
| 3904 | row: row, |
| 3905 | fromOuter: fromOuter, |
| 3906 | colsPerSide: cols, |
| 3907 | x: x, |
| 3908 | y: y, |
| 3909 | w: mini.outerW, |
| 3910 | h: mini.outerH, |
| 3911 | canvasW: mini.w, |
| 3912 | canvasH: mini.h |
| 3913 | }); |
| 3914 | } |
| 3915 | return positions; |
| 3916 | } |
| 3917 | |
| 3918 | function isPortraitNarrow (vw, vh) |
| 3919 | { |
| 3920 | if (vw < PORTRAIT_NARROW) |
| 3921 | { return true; } |
| 3922 | if (vw < vh && vw < PORTRAIT_MAX) |
| 3923 | { return true; } |
| 3924 | return false; |
| 3925 | } |
| 3926 | |
| 3927 | /** |
| 3928 | * Compute the field and mini plan for a viewport. |
| 3929 | * `n` is the number of opponents (0 = solo, no minis). |
| 3930 | */ |
| 3931 | function plan (vw, vh, n, hudH, margin) |
| 3932 | { |
| 3933 | vw = vw || 320; |
| 3934 | vh = vh || 480; |
| 3935 | n = n || 0; |
| 3936 | hudH = hudH == null ? 56 : hudH; |
| 3937 | margin = margin == null ? Math.max(6, Math.round(Math.min(vw, vh) * 0.015)) : margin; |
| 3938 | |
| 3939 | function finish (mode, tile, fieldX, fieldY, miniH, mini, positions, cols) |
| 3940 | { |
| 3941 | return { |
| 3942 | mode: mode, |
| 3943 | tile: tile, |
| 3944 | fieldW: tile * 10, |
| 3945 | fieldH: tile * 13, |
| 3946 | fieldX: fieldX, |
| 3947 | fieldY: fieldY, |
| 3948 | miniH: miniH, |
| 3949 | mini: mini, |
| 3950 | positions: positions || [], |
| 3951 | colsPerSide: cols || 0, |
| 3952 | n: n |
| 3953 | }; |
| 3954 | } |
| 3955 | |
| 3956 | if (n > 0 && !isPortraitNarrow(vw, vh)) |
| 3957 | { |
| 3958 | var availHSide = Math.max(80, vh - hudH - margin); |
| 3959 | var probe = preferredMini(); |
| 3960 | var availWSide = Math.max(80, vw - margin * 2 - 2 * (probe.outerW + GAP)); |
| 3961 | var availHForField = Math.max(80, availHSide - ARC_TOP_PAD); |
| 3962 | var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHForField / 13))); |
| 3963 | var fieldWSide = tileSide * 10; |
| 3964 | var leftover = (vw - fieldWSide) / 2; |
| 3965 | var cols = colsThatFit(leftover, probe); |
| 3966 | // If a large mini does not fit, shrink it so a small one still can (t1131). |
| 3967 | if (cols < 1) |
| 3968 | { |
| 3969 | var shrinkStride = leftover - EDGE; |
| 3970 | if (shrinkStride >= 56 + GAP) |
| 3971 | { |
| 3972 | var ow = Math.max(56, shrinkStride - GAP); |
| 3973 | var oh = Math.round(ow * (104 + LABEL_H) / 90); |
| 3974 | var ch = Math.max(40, oh - LABEL_H); |
| 3975 | var cw = Math.max(32, Math.round(ch * 10 / 13)); |
| 3976 | probe = { w: cw, h: ch, outerW: ow, outerH: ch + LABEL_H }; |
| 3977 | cols = colsThatFit(leftover, probe); |
| 3978 | } |
| 3979 | } |
| 3980 | var need = probe.outerW + GAP + EDGE; |
| 3981 | if (leftover >= need && tileSide >= MIN_TILE_FOR_SIDES && cols >= 1) |
| 3982 | { |
| 3983 | var fieldX = Math.floor((vw - fieldWSide) / 2); |
| 3984 | var fieldHSide = tileSide * 13; |
| 3985 | var fieldY = hudH + ARC_TOP_PAD |
| 3986 | + Math.floor((availHForField - fieldHSide) / 2); |
| 3987 | if (fieldY < hudH + ARC_TOP_PAD) |
| 3988 | { fieldY = hudH + ARC_TOP_PAD; } |
| 3989 | |
| 3990 | var leftCount = Math.ceil(n / 2); |
| 3991 | var rows = Math.max(1, Math.ceil(leftCount / cols)); |
| 3992 | var stackAvail = fieldY + fieldHSide - (hudH + 4); |
| 3993 | if (rows > 1) |
| 3994 | { |
| 3995 | probe = canvasSize(n, 'sides', stackAvail, rows); |
| 3996 | cols = Math.max(1, colsThatFit(leftover, probe)); |
| 3997 | rows = Math.max(1, Math.ceil(leftCount / cols)); |
| 3998 | } |
| 3999 | |
| 4000 | return finish('sides', tileSide, fieldX, fieldY, 0, probe, |
| 4001 | sidePositions(n, fieldX, fieldY, fieldWSide, fieldHSide, probe, hudH, vw, cols), |
| 4002 | cols); |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | var miniTop = canvasSize(n, 'top', 0); |
| 4007 | var miniH = 0; |
| 4008 | if (n > 0) |
| 4009 | { |
| 4010 | var share = n > 6 ? 0.26 : 0.20; |
| 4011 | miniH = Math.min(n > 6 ? 180 : 150, Math.max(86, Math.round(vh * share))); |
| 4012 | } |
| 4013 | var availW = Math.max(80, vw - margin * 2); |
| 4014 | var availH = Math.max(80, vh - hudH - miniH - margin); |
| 4015 | var tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 4016 | var fieldW = tile * 10; |
| 4017 | var fieldH = tile * 13; |
| 4018 | var fieldX = Math.floor((vw - fieldW) / 2); |
| 4019 | var topBand = hudH + miniH; |
| 4020 | var fieldY = topBand + Math.floor((vh - topBand - fieldH - margin / 2) / 2); |
| 4021 | if (fieldY < topBand) |
| 4022 | { fieldY = topBand; } |
| 4023 | return finish('top', tile, fieldX, fieldY, miniH, miniTop, [], 0); |
| 4024 | } |
| 4025 | |
| 4026 | var api = { |
| 4027 | GAP: GAP, |
| 4028 | VGAP: VGAP, |
| 4029 | EDGE: EDGE, |
| 4030 | MIN_TILE_FOR_SIDES: MIN_TILE_FOR_SIDES, |
| 4031 | ARC_TOP_PAD: ARC_TOP_PAD, |
| 4032 | PORTRAIT_NARROW: PORTRAIT_NARROW, |
| 4033 | LABEL_H: LABEL_H, |
| 4034 | canvasSize: canvasSize, |
| 4035 | colsThatFit: colsThatFit, |
| 4036 | sidePositions: sidePositions, |
| 4037 | plan: plan |
| 4038 | }; |
| 4039 | |
| 4040 | if (typeof module !== 'undefined' && module.exports) |
| 4041 | { module.exports = api; } |
| 4042 | else if (typeof globalThis !== 'undefined') |
| 4043 | { globalThis.MiniLayout = api; } |
| 4044 | |
| 4045 | return api; |
| 4046 | |
| 4047 | })(); |
| 4048 | |
| 4049 | if (typeof module !== 'undefined' && module.exports && typeof MiniLayout !== 'undefined') |
| 4050 | { module.exports = MiniLayout; } |
| 4051 | |
| 4052 | /* |
| 4053 | * End File: |
| 4054 | * /workspace/meteors/src/miniLayout.js |
| 4055 | */ |
| 4056 | |
| 4057 | /* |
| 4058 | * Start File: |
| 4059 | * /workspace/meteors/src/online.js |
| 4060 | */ |
| 4061 | 'use strict'; |
| 4062 | |
| 4063 | /** |
| 4064 | * Online play: talks to api.php (cookie session), polls lobby and game |
| 4065 | * state, and draws opponent miniatures plus flying-block tweens. |
| 4066 | * Real-time sync is not required; a 0.75 s poll is enough. |
| 4067 | */ |
| 4068 | |
| 4069 | var Online = { |
| 4070 | POLL_MS: 750, |
| 4071 | DEFAULT_MISS_CHANCE: 0.30, |
| 4072 | FLIGHT_DURATION: 1.2, |
| 4073 | configured: false, |
| 4074 | local: false, |
| 4075 | csrf: '', |
| 4076 | me: null, |
| 4077 | lobby: null, |
| 4078 | incoming: [], |
| 4079 | shotResults: [], |
| 4080 | error: '', |
| 4081 | _timer: null, |
| 4082 | _pendingShots: [], |
| 4083 | _flights: [], |
| 4084 | _lastStatus: null, |
| 4085 | onChange: null, |
| 4086 | onGameStart: null, |
| 4087 | onIncoming: null, |
| 4088 | onFinished: null, |
| 4089 | onLobbyReturn: null |
| 4090 | }; |
| 4091 | |
| 4092 | Online._headers = function (json) |
| 4093 | { |
| 4094 | var h = { 'Accept': 'application/json' }; |
| 4095 | if (json) |
| 4096 | { h['Content-Type'] = 'application/json'; } |
| 4097 | if (this.csrf) |
| 4098 | { h['X-CSRF-Token'] = this.csrf; } |
| 4099 | return h; |
| 4100 | }; |
| 4101 | |
| 4102 | Online._apply = function (data) |
| 4103 | { |
| 4104 | if (!data) |
| 4105 | { return; } |
| 4106 | if (typeof data.configured === 'boolean') |
| 4107 | { this.configured = data.configured; } |
| 4108 | if (typeof data.local === 'boolean') |
| 4109 | { this.local = data.local; } |
| 4110 | if (data.csrf) |
| 4111 | { this.csrf = data.csrf; } |
| 4112 | if (data.me !== undefined) |
| 4113 | { this.me = data.me; } |
| 4114 | if (data.lobby !== undefined) |
| 4115 | { this.lobby = data.lobby; } |
| 4116 | if (data.incoming) |
| 4117 | { this.incoming = data.incoming; } |
| 4118 | if (data.shot_results) |
| 4119 | { this.shotResults = data.shot_results; } |
| 4120 | if (data.miss_chance_default != null) |
| 4121 | { this.DEFAULT_MISS_CHANCE = data.miss_chance_default; } |
| 4122 | if (data.flight_duration != null) |
| 4123 | { this.FLIGHT_DURATION = data.flight_duration; } |
| 4124 | if (data.error) |
| 4125 | { this.error = data.error; } |
| 4126 | else if (data.ok) |
| 4127 | { this.error = ''; } |
| 4128 | }; |
| 4129 | |
| 4130 | Online.request = function (action, body) |
| 4131 | { |
| 4132 | var self = this; |
| 4133 | var isGet = action === 'me' || action === ''; |
| 4134 | var url = 'api.php?action=' + encodeURIComponent(action || 'me'); |
| 4135 | var opts = { |
| 4136 | method: isGet && !body ? 'GET' : 'POST', |
| 4137 | credentials: 'same-origin', |
| 4138 | headers: this._headers(!!body || !isGet) |
| 4139 | }; |
| 4140 | if (opts.method === 'POST') |
| 4141 | { |
| 4142 | var payload = body ? Object.assign({ action: action }, body) : { action: action }; |
| 4143 | opts.body = JSON.stringify(payload); |
| 4144 | } |
| 4145 | return fetch(url, opts).then(function (res) |
| 4146 | { |
| 4147 | return res.json().then(function (data) |
| 4148 | { |
| 4149 | data = data || {}; |
| 4150 | if (!res.ok && !data.error) |
| 4151 | { data.error = 'De server gaf ' + res.status; data.ok = false; } |
| 4152 | self._apply(data); |
| 4153 | return data; |
| 4154 | }, function () |
| 4155 | { |
| 4156 | var fail = { ok: false, error: 'Geen geldig antwoord van de server.' }; |
| 4157 | self.error = fail.error; |
| 4158 | return fail; |
| 4159 | }); |
| 4160 | }).catch(function (err) |
| 4161 | { |
| 4162 | var fail = { ok: false, error: 'Geen verbinding met de server.' }; |
| 4163 | self.error = fail.error; |
| 4164 | return fail; |
| 4165 | }); |
| 4166 | }; |
| 4167 | |
| 4168 | Online.refresh = function () |
| 4169 | { |
| 4170 | return this.request('me'); |
| 4171 | }; |
| 4172 | |
| 4173 | Online.testLogin = function (who) |
| 4174 | { |
| 4175 | return this.request('test_login', { user: who }); |
| 4176 | }; |
| 4177 | |
| 4178 | Online.createLobby = function (planetId) |
| 4179 | { |
| 4180 | return this.request('lobby_create', { |
| 4181 | miss_chance: this.DEFAULT_MISS_CHANCE, |
| 4182 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 4183 | }); |
| 4184 | }; |
| 4185 | |
| 4186 | Online.joinLobby = function (code, planetId) |
| 4187 | { |
| 4188 | return this.request('lobby_join', { |
| 4189 | code: code, |
| 4190 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 4191 | }); |
| 4192 | }; |
| 4193 | |
| 4194 | Online.setPlanet = function (planetId) |
| 4195 | { |
| 4196 | return this.request('lobby_set_planet', { planet_id: planetId }); |
| 4197 | }; |
| 4198 | |
| 4199 | Online.leaveLobby = function () |
| 4200 | { |
| 4201 | return this.request('lobby_leave'); |
| 4202 | }; |
| 4203 | |
| 4204 | Online.setReady = function (ready) |
| 4205 | { |
| 4206 | return this.request('lobby_ready', { ready: !!ready }); |
| 4207 | }; |
| 4208 | |
| 4209 | Online.startGame = function () |
| 4210 | { |
| 4211 | var self = this; |
| 4212 | var prev = this.lobby && this.lobby.status; |
| 4213 | return this.request('lobby_start').then(function (data) |
| 4214 | { |
| 4215 | self._handleTransitions(prev, data); |
| 4216 | return data; |
| 4217 | }); |
| 4218 | }; |
| 4219 | |
| 4220 | Online.queueShots = function (shots) |
| 4221 | { |
| 4222 | for (var i = 0; i < shots.length; i++) |
| 4223 | { |
| 4224 | this._pendingShots.push({ |
| 4225 | client_id: String(shots[i].id), |
| 4226 | color: shots[i].color, |
| 4227 | col: shots[i].col, |
| 4228 | y: shots[i].y |
| 4229 | }); |
| 4230 | } |
| 4231 | }; |
| 4232 | |
| 4233 | Online.poll = function (opts) |
| 4234 | { |
| 4235 | opts = opts || {}; |
| 4236 | var body = {}; |
| 4237 | if (opts.snapshot) |
| 4238 | { body.snapshot = opts.snapshot; } |
| 4239 | if (opts.score != null) |
| 4240 | { body.score = opts.score; } |
| 4241 | if (opts.eliminated) |
| 4242 | { body.eliminated = true; } |
| 4243 | if (this._pendingShots.length) |
| 4244 | { |
| 4245 | body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length); |
| 4246 | } |
| 4247 | var self = this; |
| 4248 | var prevStatus = this.lobby && this.lobby.status; |
| 4249 | return this.request('poll', body).then(function (data) |
| 4250 | { |
| 4251 | self._handleTransitions(prevStatus, data); |
| 4252 | if (self.onChange) |
| 4253 | { self.onChange(self); } |
| 4254 | return data; |
| 4255 | }); |
| 4256 | }; |
| 4257 | |
| 4258 | Online._handleTransitions = function (prevStatus, data) |
| 4259 | { |
| 4260 | var lobby = this.lobby; |
| 4261 | var status = lobby && lobby.status; |
| 4262 | if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart) |
| 4263 | { this.onGameStart(lobby); } |
| 4264 | if (data && data.incoming && data.incoming.length && this.onIncoming) |
| 4265 | { this.onIncoming(data.incoming); } |
| 4266 | // Own outgoing results (immediate) and lobby-wide recent flights |
| 4267 | // (so every client can draw opponent and enemy-to-enemy arcs, t1125). |
| 4268 | var flights = []; |
| 4269 | if (data && data.shot_results && data.shot_results.length) |
| 4270 | { flights = flights.concat(data.shot_results); } |
| 4271 | if (data && data.shot_flights && data.shot_flights.length) |
| 4272 | { flights = flights.concat(data.shot_flights); } |
| 4273 | if (flights.length) |
| 4274 | { this.spawnFlights(flights, this._flightLocals); } |
| 4275 | if (status === 'finished' && prevStatus !== 'finished' && this.onFinished) |
| 4276 | { this.onFinished(lobby); } |
| 4277 | if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn) |
| 4278 | { this.onLobbyReturn(lobby); } |
| 4279 | }; |
| 4280 | |
| 4281 | Online.startPolling = function (getPayload) |
| 4282 | { |
| 4283 | this.stopPolling(); |
| 4284 | var self = this; |
| 4285 | var tick = function () |
| 4286 | { |
| 4287 | var payload = getPayload ? getPayload() : {}; |
| 4288 | self.poll(payload); |
| 4289 | }; |
| 4290 | tick(); |
| 4291 | this._timer = setInterval(tick, this.POLL_MS); |
| 4292 | }; |
| 4293 | |
| 4294 | Online.stopPolling = function () |
| 4295 | { |
| 4296 | if (this._timer) |
| 4297 | { |
| 4298 | clearInterval(this._timer); |
| 4299 | this._timer = null; |
| 4300 | } |
| 4301 | }; |
| 4302 | |
| 4303 | Online.loginUrl = function () |
| 4304 | { |
| 4305 | return 'auth.php'; |
| 4306 | }; |
| 4307 | |
| 4308 | Online.logoutUrl = function () |
| 4309 | { |
| 4310 | return 'logout.php'; |
| 4311 | }; |
| 4312 | |
| 4313 | Online.opponents = function () |
| 4314 | { |
| 4315 | if (!this.lobby || !this.lobby.players) |
| 4316 | { return []; } |
| 4317 | return this.lobby.players.filter(function (p) { return !p.is_me; }); |
| 4318 | }; |
| 4319 | |
| 4320 | Online.aliveOpponents = function () |
| 4321 | { |
| 4322 | return this.opponents().filter(function (p) { return !p.eliminated; }); |
| 4323 | }; |
| 4324 | |
| 4325 | Online.mePlayer = function () |
| 4326 | { |
| 4327 | if (!this.lobby || !this.lobby.players) |
| 4328 | { return null; } |
| 4329 | for (var i = 0; i < this.lobby.players.length; i++) |
| 4330 | { |
| 4331 | if (this.lobby.players[i].is_me) |
| 4332 | { return this.lobby.players[i]; } |
| 4333 | } |
| 4334 | return null; |
| 4335 | }; |
| 4336 | |
| 4337 | /* ---------- miniatures --------------------------------------------- */ |
| 4338 | |
| 4339 | Online.renderMinis = function (container) |
| 4340 | { |
| 4341 | if (!container) |
| 4342 | { return; } |
| 4343 | var opponents = this.opponents(); |
| 4344 | var existing = {}; |
| 4345 | var kids = container.querySelectorAll('[data-player]'); |
| 4346 | for (var i = 0; i < kids.length; i++) |
| 4347 | { existing[kids[i].getAttribute('data-player')] = kids[i]; } |
| 4348 | |
| 4349 | var seen = Object.create(null); |
| 4350 | for (var i = 0; i < opponents.length; i++) |
| 4351 | { |
| 4352 | var p = opponents[i]; |
| 4353 | seen[p.id] = true; |
| 4354 | var el = existing[p.id]; |
| 4355 | if (!el) |
| 4356 | { |
| 4357 | el = document.createElement('div'); |
| 4358 | el.className = 'mini'; |
| 4359 | el.setAttribute('data-player', p.id); |
| 4360 | el.innerHTML = '<div class="mini-name"></div>' |
| 4361 | + '<canvas class="mini-canvas" width="80" height="104"></canvas>' |
| 4362 | + '<div class="mini-meta"></div>'; |
| 4363 | container.appendChild(el); |
| 4364 | } |
| 4365 | var mz = (typeof app !== 'undefined' && app.miniLayout && app.miniLayout.mini) |
| 4366 | ? app.miniLayout.mini |
| 4367 | : { w: 80, h: 104 }; |
| 4368 | var canvas = el.querySelector('canvas'); |
| 4369 | if (canvas && (canvas.width !== mz.w || canvas.height !== mz.h)) |
| 4370 | { |
| 4371 | canvas.width = mz.w; |
| 4372 | canvas.height = mz.h; |
| 4373 | } |
| 4374 | if (canvas) |
| 4375 | { |
| 4376 | canvas.style.width = mz.w + 'px'; |
| 4377 | canvas.style.height = mz.h + 'px'; |
| 4378 | } |
| 4379 | el.classList.toggle('eliminated', !!p.eliminated); |
| 4380 | el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : ''); |
| 4381 | el.querySelector('.mini-meta').textContent = p.eliminated |
| 4382 | ? ('Uit' + (p.place ? ' · ' + p.place + 'e' : '')) |
| 4383 | : ('Punten ' + (p.score || 0)); |
| 4384 | this._drawMini(el.querySelector('canvas'), p.snapshot, p.planet); |
| 4385 | } |
| 4386 | for (var id in existing) |
| 4387 | { |
| 4388 | if (!seen[id]) |
| 4389 | { existing[id].remove(); } |
| 4390 | } |
| 4391 | if (typeof applyMinisLayout === 'function') |
| 4392 | { applyMinisLayout(); } |
| 4393 | }; |
| 4394 | |
| 4395 | var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4']; |
| 4396 | |
| 4397 | function miniColor (planet, color) |
| 4398 | { |
| 4399 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].hex) |
| 4400 | { return planet.colors[color].hex; } |
| 4401 | return MINI_COLORS[color] || '#888'; |
| 4402 | } |
| 4403 | |
| 4404 | |
| 4405 | Online._drawMini = function (canvas, snapshot, planet) |
| 4406 | { |
| 4407 | if (!canvas) |
| 4408 | { return; } |
| 4409 | var ctx = canvas.getContext('2d'); |
| 4410 | var w = 10; |
| 4411 | var h = 13; |
| 4412 | var tw = canvas.width / w; |
| 4413 | var th = canvas.height / h; |
| 4414 | ctx.fillStyle = '#0c1020'; |
| 4415 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 4416 | ctx.strokeStyle = 'rgba(255,255,255,0.06)'; |
| 4417 | ctx.lineWidth = 1; |
| 4418 | for (var c = 0; c <= w; c++) |
| 4419 | { |
| 4420 | ctx.beginPath(); |
| 4421 | ctx.moveTo(c * tw + 0.5, 0); |
| 4422 | ctx.lineTo(c * tw + 0.5, canvas.height); |
| 4423 | ctx.stroke(); |
| 4424 | } |
| 4425 | for (var r = 0; r <= h; r++) |
| 4426 | { |
| 4427 | ctx.beginPath(); |
| 4428 | ctx.moveTo(0, r * th + 0.5); |
| 4429 | ctx.lineTo(canvas.width, r * th + 0.5); |
| 4430 | ctx.stroke(); |
| 4431 | } |
| 4432 | if (!snapshot || !snapshot.blocks) |
| 4433 | { return; } |
| 4434 | // t1123: cheap sine pulse behind blocks of nearly-full columns. |
| 4435 | if (typeof GameCore !== 'undefined' && GameCore.warnColumnsFromSnapshot) |
| 4436 | { |
| 4437 | var warns = GameCore.warnColumnsFromSnapshot(snapshot); |
| 4438 | var pulse = GameCore.columnWarnPulse(); |
| 4439 | for (var wc = 0; wc < warns.length; wc++) |
| 4440 | { |
| 4441 | if (!warns[wc]) |
| 4442 | { continue; } |
| 4443 | ctx.fillStyle = 'rgba(220, 36, 36,' + pulse + ')'; |
| 4444 | ctx.fillRect(wc * tw, 0, tw, canvas.height); |
| 4445 | } |
| 4446 | } |
| 4447 | var blocks = snapshot.blocks; |
| 4448 | for (var i = 0; i < blocks.length; i++) |
| 4449 | { |
| 4450 | var b = blocks[i]; |
| 4451 | var x = b.col * tw; |
| 4452 | var y = canvas.height - (b.y + 1) * th; |
| 4453 | if (b.state === 'locked') |
| 4454 | { ctx.fillStyle = '#3a3d4a'; } |
| 4455 | else if (b.color < 0) |
| 4456 | { ctx.fillStyle = '#5a5d66'; } |
| 4457 | else |
| 4458 | { ctx.fillStyle = miniColor(planet, b.color); } |
| 4459 | ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1); |
| 4460 | } |
| 4461 | }; |
| 4462 | |
| 4463 | Online.miniElement = function (playerId) |
| 4464 | { |
| 4465 | return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]'); |
| 4466 | }; |
| 4467 | |
| 4468 | /* ---------- flight tweens ------------------------------------------ */ |
| 4469 | |
| 4470 | function colorSpriteUrl (color, planetId) |
| 4471 | { |
| 4472 | if (color < 0) |
| 4473 | { return 'images/block-gray.png'; } |
| 4474 | var planet = null; |
| 4475 | if (typeof Planet !== 'undefined') |
| 4476 | { planet = Planet.planetById(planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra')); } |
| 4477 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].image) |
| 4478 | { return planet.colors[color].image; } |
| 4479 | return 'images/block-gray.png'; |
| 4480 | } |
| 4481 | |
| 4482 | /** |
| 4483 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 4484 | * startX, startY} in viewport pixels, filled by the view just after |
| 4485 | * consumeOutgoingShots. |
| 4486 | */ |
| 4487 | Online.playerId = function () |
| 4488 | { |
| 4489 | if (typeof app !== 'undefined' && app.offline) |
| 4490 | { return 'player'; } |
| 4491 | if (this.lobby && this.lobby.players) |
| 4492 | { |
| 4493 | for (var i = 0; i < this.lobby.players.length; i++) |
| 4494 | { |
| 4495 | if (this.lobby.players[i].is_me) |
| 4496 | { return this.lobby.players[i].id; } |
| 4497 | } |
| 4498 | } |
| 4499 | return 'player'; |
| 4500 | }; |
| 4501 | |
| 4502 | /** |
| 4503 | * Classify a shot for visual treatment (t1125). |
| 4504 | * player-to-enemy, enemy-to-player, enemy-to-enemy, |
| 4505 | * player-miss, enemy-miss. |
| 4506 | */ |
| 4507 | Online.flightKind = function (fromId, targetId, miss) |
| 4508 | { |
| 4509 | var me = this.playerId(); |
| 4510 | var fromMe = !fromId || fromId === me || fromId === 'player'; |
| 4511 | if (miss) |
| 4512 | { return fromMe ? 'player-miss' : 'enemy-miss'; } |
| 4513 | if (fromMe) |
| 4514 | { return 'player-to-enemy'; } |
| 4515 | if (targetId === me || targetId === 'player') |
| 4516 | { return 'enemy-to-player'; } |
| 4517 | return 'enemy-to-enemy'; |
| 4518 | }; |
| 4519 | |
| 4520 | /** |
| 4521 | * Parabolic arc: linear in x, a rise of `height` pixels above the |
| 4522 | * chord at mid-flight (screen y grows downward, so we subtract). |
| 4523 | */ |
| 4524 | Online.arcPoint = function (start, end, u, height) |
| 4525 | { |
| 4526 | var t = u < 0 ? 0 : (u > 1 ? 1 : u); |
| 4527 | var x = start.x + (end.x - start.x) * t; |
| 4528 | var y = start.y + (end.y - start.y) * t - 4 * height * t * (1 - t); |
| 4529 | return { x: x, y: y }; |
| 4530 | }; |
| 4531 | |
| 4532 | Online.arcHeight = function (start, end) |
| 4533 | { |
| 4534 | var dx = end.x - start.x; |
| 4535 | var dy = end.y - start.y; |
| 4536 | var dist = Math.sqrt(dx * dx + dy * dy); |
| 4537 | return Math.max(48, Math.min(220, dist * 0.28)); |
| 4538 | }; |
| 4539 | |
| 4540 | Online.easeInOutCubic = function (t) |
| 4541 | { |
| 4542 | if (t < 0.5) |
| 4543 | { return 4 * t * t * t; } |
| 4544 | var u = -2 * t + 2; |
| 4545 | return 1 - (u * u * u) / 2; |
| 4546 | }; |
| 4547 | |
| 4548 | /** Bounding box of the player's field in viewport pixels. */ |
| 4549 | Online.playerFieldRect = function () |
| 4550 | { |
| 4551 | var appRef = typeof app !== 'undefined' ? app : null; |
| 4552 | var canvas = appRef && appRef.scene && appRef.scene.canvas; |
| 4553 | if (canvas && canvas.getBoundingClientRect) |
| 4554 | { return canvas.getBoundingClientRect(); } |
| 4555 | if (appRef && appRef.fieldW) |
| 4556 | { |
| 4557 | return { |
| 4558 | left: appRef.fieldX, |
| 4559 | top: appRef.fieldY, |
| 4560 | right: appRef.fieldX + appRef.fieldW, |
| 4561 | bottom: appRef.fieldY + appRef.fieldH, |
| 4562 | width: appRef.fieldW, |
| 4563 | height: appRef.fieldH |
| 4564 | }; |
| 4565 | } |
| 4566 | return null; |
| 4567 | }; |
| 4568 | |
| 4569 | /** |
| 4570 | * True if (x, y) is inside the field's interior. The top edge (a few |
| 4571 | * pixels) is not interior: launch and landing may use that band. |
| 4572 | */ |
| 4573 | Online.pointInFieldInterior = function (x, y, field, slop) |
| 4574 | { |
| 4575 | if (!field) |
| 4576 | { return false; } |
| 4577 | slop = slop == null ? 6 : slop; |
| 4578 | return x > field.left + 0.5 && x < field.right - 0.5 |
| 4579 | && y > field.top + slop && y < field.bottom - 0.5; |
| 4580 | }; |
| 4581 | |
| 4582 | /** Cubic Bezier C(t), t ∈ [0, 1]. C¹ (in fact C^∞) on (0, 1) — no corners (t1134). */ |
| 4583 | Online.cubicBezier = function (p0, p1, p2, p3, t) |
| 4584 | { |
| 4585 | if (t < 0) |
| 4586 | { t = 0; } |
| 4587 | if (t > 1) |
| 4588 | { t = 1; } |
| 4589 | var u = 1 - t; |
| 4590 | var a = u * u * u; |
| 4591 | var b = 3 * u * u * t; |
| 4592 | var c = 3 * u * t * t; |
| 4593 | var d = t * t * t; |
| 4594 | return { |
| 4595 | x: a * p0.x + b * p1.x + c * p2.x + d * p3.x, |
| 4596 | y: a * p0.y + b * p1.y + c * p2.y + d * p3.y |
| 4597 | }; |
| 4598 | }; |
| 4599 | |
| 4600 | /** C'(t) = 3(1−t)²(P1−P0) + 6(1−t)t(P2−P1) + 3t²(P3−P2). */ |
| 4601 | Online.cubicBezierDeriv = function (p0, p1, p2, p3, t) |
| 4602 | { |
| 4603 | if (t < 0) |
| 4604 | { t = 0; } |
| 4605 | if (t > 1) |
| 4606 | { t = 1; } |
| 4607 | var u = 1 - t; |
| 4608 | return { |
| 4609 | x: 3 * u * u * (p1.x - p0.x) + 6 * u * t * (p2.x - p1.x) + 3 * t * t * (p3.x - p2.x), |
| 4610 | y: 3 * u * u * (p1.y - p0.y) + 6 * u * t * (p2.y - p1.y) + 3 * t * t * (p3.y - p2.y) |
| 4611 | }; |
| 4612 | }; |
| 4613 | |
| 4614 | /** |
| 4615 | * Hermite parameter with p(0)=0, p(1)=1, p'(0)=v0, p'(1)=v1. |
| 4616 | * v0=v1=1 is linear (constant speed in t, no endpoint halt). |
| 4617 | */ |
| 4618 | Online.smoothParam = function (t, v0, v1) |
| 4619 | { |
| 4620 | if (t < 0) |
| 4621 | { return 0; } |
| 4622 | if (t > 1) |
| 4623 | { return 1; } |
| 4624 | if (v0 == null) |
| 4625 | { v0 = 1; } |
| 4626 | if (v1 == null) |
| 4627 | { v1 = 1; } |
| 4628 | var b = 3 - 2 * v0 - v1; |
| 4629 | var a = v0 + v1 - 2; |
| 4630 | return ((a * t + b) * t + v0) * t; |
| 4631 | }; |
| 4632 | |
| 4633 | /** |
| 4634 | * A simple up-and-over cubic, used when there is no field to route |
| 4635 | * around (enemy→enemy) or both ends already sit above the apex. |
| 4636 | */ |
| 4637 | Online._singleCubic = function (start, end, apexY, opts) |
| 4638 | { |
| 4639 | opts = opts || {}; |
| 4640 | var rise = Math.max(40, Math.abs(start.y - apexY) * 0.9); |
| 4641 | if (opts.startSpeedPx) |
| 4642 | { rise = Math.max(rise, opts.startSpeedPx); } |
| 4643 | var fall = Math.max(40, Math.abs(end.y - apexY) * 0.9); |
| 4644 | if (opts.endSpeedPx) |
| 4645 | { fall = Math.max(40, opts.endSpeedPx); } |
| 4646 | var p1 = { x: start.x, y: Math.min(start.y - rise, apexY) }; |
| 4647 | var p2 = { x: end.x, y: Math.min(end.y - fall, apexY) }; |
| 4648 | return { p0: start, p1: p1, p2: p2, p3: end, apexY: apexY }; |
| 4649 | }; |
| 4650 | |
| 4651 | /** |
| 4652 | * Smooth C¹ path: rise up (launch column or outside), cruise above |
| 4653 | * the field, dive into the landing column (t1134 / t1135). Joints |
| 4654 | * are quarter-turn cubics with matching tangents, so there are no |
| 4655 | * corners and no speed jerks. The curve may use the launch and |
| 4656 | * landing corridors but does not cut across the field interior. |
| 4657 | */ |
| 4658 | Online.makeAroundCurve = function (start, end, field, opts) |
| 4659 | { |
| 4660 | opts = opts || {}; |
| 4661 | if (!start) |
| 4662 | { start = { x: 0, y: 0 }; } |
| 4663 | if (!end) |
| 4664 | { end = { x: 0, y: 0 }; } |
| 4665 | var clearance = opts.clearance || 80; |
| 4666 | if (!field) |
| 4667 | { |
| 4668 | var looseApex = Math.min(start.y, end.y) - Math.max(40, clearance * 0.5); |
| 4669 | if (looseApex < 8) |
| 4670 | { looseApex = Math.min(start.y, end.y) - 24; } |
| 4671 | return this._singleCubic(start, end, looseApex, opts); |
| 4672 | } |
| 4673 | var top = field.top; |
| 4674 | var left = field.left; |
| 4675 | var right = field.right; |
| 4676 | var apexY = top - clearance; |
| 4677 | if (apexY < 8) |
| 4678 | { apexY = Math.max(8, top - Math.max(24, clearance * 0.5)); } |
| 4679 | |
| 4680 | if (start.y <= apexY + 8 && end.y <= apexY + 8) |
| 4681 | { return this._singleCubic(start, end, apexY, opts); } |
| 4682 | |
| 4683 | var pad = 14; |
| 4684 | var dir = end.x >= start.x ? 1 : -1; |
| 4685 | var span = Math.abs(end.x - start.x); |
| 4686 | var horiz = Math.max(28, Math.min(80, span * 0.15 + 8)); |
| 4687 | function outsideX (x) |
| 4688 | { |
| 4689 | if (x < left + 8) |
| 4690 | { return Math.min(x, left - pad); } |
| 4691 | if (x > right - 8) |
| 4692 | { return Math.max(x, right + pad); } |
| 4693 | return x; |
| 4694 | } |
| 4695 | var riseX = outsideX(start.x); |
| 4696 | var diveX = outsideX(end.x); |
| 4697 | var segments = []; |
| 4698 | |
| 4699 | // Rise ends already turned toward the cruise, at apex. |
| 4700 | var riseEnd = { x: riseX + dir * horiz, y: apexY }; |
| 4701 | if (start.y > apexY + 4) |
| 4702 | { |
| 4703 | var p1 = { x: start.x, y: apexY }; |
| 4704 | var p2 = { x: riseX, y: apexY }; |
| 4705 | segments.push({ p0: start, p1: p1, p2: p2, p3: riseEnd }); |
| 4706 | } |
| 4707 | else |
| 4708 | { riseEnd = { x: start.x, y: Math.min(start.y, apexY) }; } |
| 4709 | |
| 4710 | // Cruise stays on the apex line (convex hull is above the field). |
| 4711 | var diveStart = { x: diveX - dir * horiz, y: apexY }; |
| 4712 | if (Math.abs(riseEnd.x - diveStart.x) > 4 || Math.abs(riseEnd.y - diveStart.y) > 4) |
| 4713 | { |
| 4714 | // Keep the cruise direction consistent; if the two joints |
| 4715 | // would cross, just connect them directly. |
| 4716 | var cdir = diveStart.x >= riseEnd.x ? 1 : -1; |
| 4717 | var c1 = { x: riseEnd.x + cdir * horiz, y: riseEnd.y }; |
| 4718 | if (c1.y > apexY) |
| 4719 | { c1.y = apexY; } |
| 4720 | var c2 = { x: diveStart.x - cdir * horiz, y: diveStart.y }; |
| 4721 | segments.push({ p0: { x: riseEnd.x, y: riseEnd.y }, p1: c1, p2: c2, p3: diveStart }); |
| 4722 | } |
| 4723 | |
| 4724 | if (end.y > apexY + 4 || Math.abs(end.x - (diveStart.x + dir * horiz)) > 2) |
| 4725 | { |
| 4726 | var u0 = (Math.abs(riseEnd.x - diveStart.x) > 4 || segments.length === 0) |
| 4727 | ? diveStart |
| 4728 | : riseEnd; |
| 4729 | if (segments.length && segments[segments.length - 1].p3.x === u0.x |
| 4730 | && segments[segments.length - 1].p3.y === u0.y) |
| 4731 | { /* already joined */ } |
| 4732 | else if (segments.length === 0) |
| 4733 | { u0 = riseEnd; } |
| 4734 | var u1 = { x: u0.x + dir * horiz, y: u0.y }; |
| 4735 | var fall = Math.max(24, Math.abs(end.y - apexY) * 0.45); |
| 4736 | if (opts.endSpeedPx) |
| 4737 | { |
| 4738 | // |P3-P2| so arrival ≈ fallSpeed after equal-param scaling. |
| 4739 | // velocity = 3*|P3-P2|*n*v1/duration; endSpeedPx is |
| 4740 | // (cells*px*duration)/3, so |P3-P2| = endSpeedPx/(n*v1). |
| 4741 | var nEst = Math.max(1, segments.length + 1); |
| 4742 | fall = Math.max(20, opts.endSpeedPx / (nEst * 0.85)); |
| 4743 | } |
| 4744 | var u2 = { x: end.x, y: end.y - fall }; |
| 4745 | // P2 may sit between the apex and the end (slower arrival); |
| 4746 | // only clamp if it would pass the end or fly far past the apex. |
| 4747 | if (u2.y < apexY - 240) |
| 4748 | { u2.y = apexY - 240; } |
| 4749 | if (u2.y > end.y - 8) |
| 4750 | { u2.y = end.y - 8; } |
| 4751 | segments.push({ p0: u0, p1: u1, p2: u2, p3: end }); |
| 4752 | } |
| 4753 | |
| 4754 | if (segments.length === 0) |
| 4755 | { return this._singleCubic(start, end, apexY, opts); } |
| 4756 | if (segments.length === 1 && !segments[0].p0) |
| 4757 | { return this._singleCubic(start, end, apexY, opts); } |
| 4758 | |
| 4759 | return { segments: segments, apexY: apexY, p0: start, p3: end }; |
| 4760 | }; |
| 4761 | |
| 4762 | Online._measureCurve = function (curve) |
| 4763 | { |
| 4764 | if (!curve.segments) |
| 4765 | { return curve; } |
| 4766 | var lens = []; |
| 4767 | var total = 0; |
| 4768 | for (var i = 0; i < curve.segments.length; i++) |
| 4769 | { |
| 4770 | var L = this._segLen(curve.segments[i]); |
| 4771 | lens.push(L); |
| 4772 | total += L; |
| 4773 | } |
| 4774 | curve.lens = lens; |
| 4775 | curve.totalLen = Math.max(total, 1); |
| 4776 | return curve; |
| 4777 | }; |
| 4778 | |
| 4779 | Online._segLen = function (seg, steps) |
| 4780 | { |
| 4781 | steps = steps || 10; |
| 4782 | var len = 0; |
| 4783 | var prev = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, 0); |
| 4784 | for (var i = 1; i <= steps; i++) |
| 4785 | { |
| 4786 | var p = this.cubicBezier(seg.p0, seg.p1, seg.p2, seg.p3, i / steps); |
| 4787 | var dx = p.x - prev.x; |
| 4788 | var dy = p.y - prev.y; |
| 4789 | len += Math.sqrt(dx * dx + dy * dy); |
| 4790 | prev = p; |
| 4791 | } |
| 4792 | return Math.max(len, 1); |
| 4793 | }; |
| 4794 | |
| 4795 | /** |
| 4796 | * Equal-parameter split across segments so matching local C'(t) |
| 4797 | * at joints also matches dP/du (no speed jerk). |
| 4798 | */ |
| 4799 | Online._curveSegmentAt = function (curve, u) |
| 4800 | { |
| 4801 | if (!curve) |
| 4802 | { return null; } |
| 4803 | if (!curve.segments) |
| 4804 | { |
| 4805 | return { seg: curve, t: u < 0 ? 0 : (u > 1 ? 1 : u), scale: 1 }; |
| 4806 | } |
| 4807 | var segs = curve.segments; |
| 4808 | var n = segs.length; |
| 4809 | if (u <= 0) |
| 4810 | { return { seg: segs[0], t: 0, scale: n }; } |
| 4811 | if (u >= 1) |
| 4812 | { return { seg: segs[n - 1], t: 1, scale: n }; } |
| 4813 | var at = u * n; |
| 4814 | var i = Math.floor(at); |
| 4815 | if (i >= n) |
| 4816 | { i = n - 1; } |
| 4817 | var t = at - i; |
| 4818 | return { seg: segs[i], t: t, scale: n }; |
| 4819 | }; |
| 4820 | |
| 4821 | /** Point on a (possibly multi-segment C¹) curve at u ∈ [0, 1]. */ |
| 4822 | Online.curvePoint = function (curve, u) |
| 4823 | { |
| 4824 | var at = this._curveSegmentAt(curve, u); |
| 4825 | if (!at) |
| 4826 | { return { x: 0, y: 0 }; } |
| 4827 | var s = at.seg; |
| 4828 | if (s && s.p1) |
| 4829 | { return this.cubicBezier(s.p0, s.p1, s.p2, s.p3, at.t); } |
| 4830 | if (s && s.p0) |
| 4831 | { return { x: s.p0.x, y: s.p0.y }; } |
| 4832 | return { x: 0, y: 0 }; |
| 4833 | }; |
| 4834 | |
| 4835 | /** dP/du of the curve (C¹ across joints). */ |
| 4836 | Online.curveDeriv = function (curve, u) |
| 4837 | { |
| 4838 | var at = this._curveSegmentAt(curve, u); |
| 4839 | if (!at || !at.seg || !at.seg.p1) |
| 4840 | { return { x: 0, y: 0 }; } |
| 4841 | var s = at.seg; |
| 4842 | var d = this.cubicBezierDeriv(s.p0, s.p1, s.p2, s.p3, at.t); |
| 4843 | return { x: d.x * at.scale, y: d.y * at.scale }; |
| 4844 | }; |
| 4845 | |
| 4846 | /** |
| 4847 | * Sample the smooth around-field curve at u ∈ [0, 1] (t1134). |
| 4848 | * `clearance` may be a number or an options object. |
| 4849 | */ |
| 4850 | Online.aroundFieldPoint = function (start, end, u, field, clearance) |
| 4851 | { |
| 4852 | var opts = (clearance && typeof clearance === 'object') ? clearance : { clearance: clearance }; |
| 4853 | var curve = opts.curve || this.makeAroundCurve(start, end, field, opts); |
| 4854 | return this.curvePoint(curve, u); |
| 4855 | }; |
| 4856 | |
| 4857 | /** Guide points (start, joints, end) for older tests. */ |
| 4858 | Online.waypointsAroundField = function (start, end, field, clearance) |
| 4859 | { |
| 4860 | var c = this.makeAroundCurve(start, end, field, { clearance: clearance || 64 }); |
| 4861 | if (c.segments && c.segments.length) |
| 4862 | { |
| 4863 | var pts = [{ x: c.segments[0].p0.x, y: c.segments[0].p0.y }]; |
| 4864 | for (var i = 0; i < c.segments.length; i++) |
| 4865 | { pts.push({ x: c.segments[i].p3.x, y: c.segments[i].p3.y }); } |
| 4866 | return pts; |
| 4867 | } |
| 4868 | return [c.p0, c.p1, c.p2, c.p3]; |
| 4869 | }; |
| 4870 | |
| 4871 | Online.pointOnPath = function (points, u) |
| 4872 | { |
| 4873 | // Legacy polyline; new flights use cubicBezier. |
| 4874 | u = u < 0 ? 0 : (u > 1 ? 1 : u); |
| 4875 | if (!points || !points.length) |
| 4876 | { return { x: 0, y: 0 }; } |
| 4877 | if (points.length === 4 && points[0] && points[3]) |
| 4878 | { return this.cubicBezier(points[0], points[1], points[2], points[3], u); } |
| 4879 | if (points.length === 1) |
| 4880 | { return { x: points[0].x, y: points[0].y }; } |
| 4881 | var n = points.length - 1; |
| 4882 | var at = u * n; |
| 4883 | var i = Math.floor(at); |
| 4884 | if (i >= n) |
| 4885 | { i = n - 1; } |
| 4886 | var t = at - i; |
| 4887 | var a = points[i]; |
| 4888 | var b = points[i + 1]; |
| 4889 | return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }; |
| 4890 | }; |
| 4891 | |
| 4892 | /** Viewport centre of a field cell (y=0 is the bottom row). */ |
| 4893 | Online.cellViewport = function (col, y, rect) |
| 4894 | { |
| 4895 | if (!rect) |
| 4896 | { |
| 4897 | var field = this.playerFieldRect(); |
| 4898 | if (!field) |
| 4899 | { return { x: 0, y: 0 }; } |
| 4900 | rect = field; |
| 4901 | } |
| 4902 | var w = rect.width != null ? rect.width : (rect.right - rect.left); |
| 4903 | var h = rect.height != null ? rect.height : (rect.bottom - rect.top); |
| 4904 | return { |
| 4905 | x: rect.left + ((col + 0.5) / 10) * w, |
| 4906 | y: rect.top + (1 - (y + 0.5) / 13) * h |
| 4907 | }; |
| 4908 | }; |
| 4909 | |
| 4910 | /** Handoff point of an incoming garbage block: y = 13, just into the column (t1135). */ |
| 4911 | Online.fieldEntry = function (col) |
| 4912 | { |
| 4913 | return this.cellViewport(col == null ? 4.5 : col, 13); |
| 4914 | }; |
| 4915 | |
| 4916 | Online.miniCellViewport = function (playerId, col, y) |
| 4917 | { |
| 4918 | var el = this.miniElement(playerId); |
| 4919 | var canvas = el && (el.querySelector ? el.querySelector('canvas') : null); |
| 4920 | var box = canvas || el; |
| 4921 | if (!box || !box.getBoundingClientRect) |
| 4922 | { return { x: window.innerWidth / 2, y: 48 }; } |
| 4923 | return this.cellViewport(col == null ? 4.5 : col, y == null ? 13 : y, box.getBoundingClientRect()); |
| 4924 | }; |
| 4925 | |
| 4926 | Online._fallSpeedPx = function (field, duration) |
| 4927 | { |
| 4928 | var px = 40; |
| 4929 | if (field && field.height) |
| 4930 | { px = field.height / 13; } |
| 4931 | else if (typeof app !== 'undefined' && app.tile) |
| 4932 | { px = app.tile; } |
| 4933 | var cells = 13 / 3; |
| 4934 | if (typeof app !== 'undefined' && app.planet && app.planet.fallSpeed) |
| 4935 | { cells = app.planet.fallSpeed; } |
| 4936 | duration = duration || this.FLIGHT_DURATION; |
| 4937 | // Distance of P2 from P3 so C'(1)/duration ≈ fall speed (t1135). |
| 4938 | return (cells * px * duration) / 3; |
| 4939 | }; |
| 4940 | |
| 4941 | Online.rememberLandCol = function (id, col) |
| 4942 | { |
| 4943 | if (id == null || col == null || !isFinite(col)) |
| 4944 | { return; } |
| 4945 | if (!this._pendingLandCol) |
| 4946 | { this._pendingLandCol = Object.create(null); } |
| 4947 | this._pendingLandCol[String(id)] = col | 0; |
| 4948 | }; |
| 4949 | |
| 4950 | /** Resolve the landing column for an incoming shot (t1126). */ |
| 4951 | Online.takeLandCol = function (incoming) |
| 4952 | { |
| 4953 | if (!incoming) |
| 4954 | { return null; } |
| 4955 | if (incoming.land_col != null && isFinite(incoming.land_col)) |
| 4956 | { return incoming.land_col | 0; } |
| 4957 | if (!this._pendingLandCol) |
| 4958 | { return null; } |
| 4959 | var keys = []; |
| 4960 | if (incoming.id != null) |
| 4961 | { keys.push(String(incoming.id)); } |
| 4962 | if (incoming.client_id) |
| 4963 | { keys.push(String(incoming.client_id)); } |
| 4964 | for (var i = 0; i < keys.length; i++) |
| 4965 | { |
| 4966 | if (this._pendingLandCol[keys[i]] != null) |
| 4967 | { return this._pendingLandCol[keys[i]]; } |
| 4968 | } |
| 4969 | return null; |
| 4970 | }; |
| 4971 | |
| 4972 | Online._miniCellSize = function (playerId) |
| 4973 | { |
| 4974 | var el = this.miniElement(playerId); |
| 4975 | var canvas = el && el.querySelector('canvas'); |
| 4976 | if (canvas) |
| 4977 | { |
| 4978 | var r = canvas.getBoundingClientRect(); |
| 4979 | return Math.max(6, r.width / 10); |
| 4980 | } |
| 4981 | return 10; |
| 4982 | }; |
| 4983 | |
| 4984 | Online._fullTileSize = function () |
| 4985 | { |
| 4986 | if (typeof app !== 'undefined' && app.tile) |
| 4987 | { return app.tile; } |
| 4988 | return 40; |
| 4989 | }; |
| 4990 | |
| 4991 | Online._miniAnchor = function (playerId, dir) |
| 4992 | { |
| 4993 | var el = this.miniElement(playerId); |
| 4994 | var canvas = el && (el.querySelector ? el.querySelector('canvas') : null); |
| 4995 | var box = canvas || el; |
| 4996 | if (!box) |
| 4997 | { return { x: window.innerWidth / 2, y: 48 }; } |
| 4998 | var r = box.getBoundingClientRect(); |
| 4999 | if (dir === 'out') |
| 5000 | { return { x: r.left + r.width / 2, y: r.top + 4 }; } |
| 5001 | return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; |
| 5002 | }; |
| 5003 | |
| 5004 | Online._fieldTop = function (col) |
| 5005 | { |
| 5006 | return this.fieldEntry(col); |
| 5007 | }; |
| 5008 | |
| 5009 | /** |
| 5010 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 5011 | * startX, startY, from_id} in viewport pixels. |
| 5012 | * |
| 5013 | * Visual-only: a smooth cubic that leaves the real cell, arcs around |
| 5014 | * the field, and dives into the landing column at the spawn pose |
| 5015 | * (t1134 / t1135). Enemy-to-enemy arcs stay small and are drawn |
| 5016 | * behind the player's field. A late-seen flight starts mid-arc via `age`. |
| 5017 | */ |
| 5018 | Online.spawnFlights = function (results, locals) |
| 5019 | { |
| 5020 | locals = locals || this._flightLocals || {}; |
| 5021 | var front = document.getElementById('flights'); |
| 5022 | var back = document.getElementById('flights-back') || front; |
| 5023 | if (!front) |
| 5024 | { return; } |
| 5025 | if (!this._flightSeen) |
| 5026 | { this._flightSeen = Object.create(null); } |
| 5027 | if (!this._previewIds) |
| 5028 | { this._previewIds = Object.create(null); } |
| 5029 | var now = performance.now(); |
| 5030 | var full = this._fullTileSize(); |
| 5031 | for (var i = 0; i < results.length; i++) |
| 5032 | { |
| 5033 | var r = results[i]; |
| 5034 | var sid = String(r.id != null ? r.id : (r.client_id || ('tmp-' + i))); |
| 5035 | if (this._flightSeen[sid]) |
| 5036 | { |
| 5037 | if (this._previewIds[sid]) |
| 5038 | { |
| 5039 | var existing = this._findFlight(sid); |
| 5040 | if (existing) |
| 5041 | { |
| 5042 | var meta0 = locals[r.client_id] || locals[sid] || {}; |
| 5043 | this.retargetFlight(existing, r, meta0, now); |
| 5044 | } |
| 5045 | } |
| 5046 | continue; |
| 5047 | } |
| 5048 | this._flightSeen[sid] = now; |
| 5049 | |
| 5050 | var meta = locals[r.client_id] || locals[sid] || {}; |
| 5051 | var fromId = r.from_id || r.from_user || meta.from_id || null; |
| 5052 | var targetId = r.target_id != null ? r.target_id : r.to_user; |
| 5053 | var miss = !!r.miss; |
| 5054 | var kind = this.flightKind(fromId, targetId, miss); |
| 5055 | var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000; |
| 5056 | if (r.age != null && isFinite(r.age) && r.age * 1000 >= duration) |
| 5057 | { continue; } |
| 5058 | |
| 5059 | var landCol = null; |
| 5060 | if (r.land_col != null && isFinite(r.land_col)) |
| 5061 | { landCol = r.land_col | 0; } |
| 5062 | else if (meta.land_col != null && isFinite(meta.land_col)) |
| 5063 | { landCol = meta.land_col | 0; } |
| 5064 | else if (kind === 'enemy-to-player' |
| 5065 | && typeof app !== 'undefined' && app.core && app.core.pickGarbageColumn) |
| 5066 | { landCol = app.core.pickGarbageColumn(); } |
| 5067 | if (landCol != null) |
| 5068 | { |
| 5069 | this.rememberLandCol(sid, landCol); |
| 5070 | if (r.client_id) |
| 5071 | { this.rememberLandCol(r.client_id, landCol); } |
| 5072 | } |
| 5073 | |
| 5074 | var startCol = meta.col != null ? meta.col : r.col; |
| 5075 | var startY = meta.y != null ? meta.y : r.y; |
| 5076 | var start; |
| 5077 | if (meta.startX != null && isFinite(meta.startX)) |
| 5078 | { start = { x: meta.startX, y: meta.startY }; } |
| 5079 | else if (kind === 'player-to-enemy' || kind === 'player-miss') |
| 5080 | { start = this._flightStart(startCol, startY); } |
| 5081 | else if (startCol != null || startY != null) |
| 5082 | { start = this.miniCellViewport(fromId, startCol, startY != null ? startY : 13); } |
| 5083 | else |
| 5084 | { start = this._miniAnchor(fromId, 'out'); } |
| 5085 | |
| 5086 | var end; |
| 5087 | if (miss) |
| 5088 | { end = this._missEnd(start, kind === 'enemy-miss'); } |
| 5089 | else if (kind === 'enemy-to-player') |
| 5090 | { end = this.fieldEntry(landCol != null ? landCol : 4.5); } |
| 5091 | else |
| 5092 | { end = this._targetEnd(targetId, start, landCol); } |
| 5093 | |
| 5094 | // Hits are ingested as grey garbage; draw them grey (t1130). |
| 5095 | // Misses keep the sender's colour — nothing is received. |
| 5096 | var flightColor = miss |
| 5097 | ? (r.color != null ? r.color : meta.color) |
| 5098 | : -1; |
| 5099 | |
| 5100 | var startScale; |
| 5101 | var endScale; |
| 5102 | var mini = this._miniCellSize(fromId || targetId) / full; |
| 5103 | if (mini < 0.12) |
| 5104 | { mini = 0.2; } |
| 5105 | if (kind === 'player-to-enemy') |
| 5106 | { |
| 5107 | startScale = 1; |
| 5108 | endScale = mini; |
| 5109 | } |
| 5110 | else if (kind === 'enemy-to-player') |
| 5111 | { |
| 5112 | startScale = mini; |
| 5113 | endScale = 1; |
| 5114 | } |
| 5115 | else if (kind === 'enemy-to-enemy') |
| 5116 | { |
| 5117 | startScale = mini; |
| 5118 | endScale = mini; |
| 5119 | } |
| 5120 | else if (kind === 'enemy-miss') |
| 5121 | { |
| 5122 | startScale = mini; |
| 5123 | endScale = 0; |
| 5124 | } |
| 5125 | else |
| 5126 | { |
| 5127 | startScale = 1; |
| 5128 | endScale = 0; |
| 5129 | } |
| 5130 | |
| 5131 | var layer = (kind === 'enemy-to-enemy' && back) ? back : front; |
| 5132 | var el = document.createElement('img'); |
| 5133 | el.src = colorSpriteUrl(flightColor); |
| 5134 | el.alt = ''; |
| 5135 | el.className = 'flight' + (kind === 'enemy-to-enemy' ? ' flight-behind' : ''); |
| 5136 | el.style.width = full + 'px'; |
| 5137 | el.style.height = full + 'px'; |
| 5138 | layer.appendChild(el); |
| 5139 | |
| 5140 | var t0 = now; |
| 5141 | if (r.age != null && isFinite(r.age) && r.age > 0) |
| 5142 | { |
| 5143 | var u0 = Math.min(0.95, (r.age * 1000) / duration); |
| 5144 | t0 = now - u0 * duration; |
| 5145 | } |
| 5146 | var height = miss |
| 5147 | ? Math.max(40, Math.min(160, Math.abs(end.y - start.y) * 0.35)) |
| 5148 | : this.arcHeight(start, end); |
| 5149 | var field = this.playerFieldRect(); |
| 5150 | var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy'); |
| 5151 | var clearance = Math.max(64, height * 0.45); |
| 5152 | var endSpeed = this._fallSpeedPx(field, duration / 1000); |
| 5153 | var curve = miss ? null : this.makeAroundCurve(start, end, around ? field : null, { |
| 5154 | clearance: clearance, |
| 5155 | startSpeedPx: endSpeed, |
| 5156 | endSpeedPx: endSpeed, |
| 5157 | duration: duration / 1000 |
| 5158 | }); |
| 5159 | this._flights.push({ |
| 5160 | el: el, |
| 5161 | start: start, |
| 5162 | end: end, |
| 5163 | ctrl: miss ? this._missCtrl(start, end) : null, |
| 5164 | height: height, |
| 5165 | t0: t0, |
| 5166 | duration: duration, |
| 5167 | spin: miss ? ((Math.random() * 2 - 1) * 720) : ((Math.random() * 2 - 1) * 120), |
| 5168 | miss: miss, |
| 5169 | kind: kind, |
| 5170 | startScale: startScale, |
| 5171 | endScale: endScale, |
| 5172 | angle: 0, |
| 5173 | id: sid, |
| 5174 | landCol: landCol, |
| 5175 | field: field, |
| 5176 | curve: curve, |
| 5177 | color: flightColor |
| 5178 | }); |
| 5179 | } |
| 5180 | this._flightLocals = {}; |
| 5181 | }; |
| 5182 | |
| 5183 | Online.rememberFlightLocal = function (shot, startX, startY) |
| 5184 | { |
| 5185 | if (!this._flightLocals) |
| 5186 | { this._flightLocals = {}; } |
| 5187 | this._flightLocals[String(shot.id)] = { |
| 5188 | col: shot.col, |
| 5189 | y: shot.y, |
| 5190 | color: shot.color, |
| 5191 | startX: startX, |
| 5192 | startY: startY, |
| 5193 | from_id: shot.from_id, |
| 5194 | land_col: shot.land_col, |
| 5195 | vy: shot.vy |
| 5196 | }; |
| 5197 | }; |
| 5198 | |
| 5199 | Online._flightStart = function (col, y) |
| 5200 | { |
| 5201 | if (y == null || !isFinite(y)) |
| 5202 | { y = 13; } |
| 5203 | return this.cellViewport(col == null ? 4.5 : col, y); |
| 5204 | }; |
| 5205 | |
| 5206 | Online._targetEnd = function (targetId, start, col) |
| 5207 | { |
| 5208 | var el = targetId && this.miniElement(targetId); |
| 5209 | if (el) |
| 5210 | { |
| 5211 | // Slightly inside the mini, in the landing column (t1135). |
| 5212 | return this.miniCellViewport(targetId, col, 12); |
| 5213 | } |
| 5214 | return { x: start.x + (Math.random() - 0.5) * 80, y: 40 }; |
| 5215 | }; |
| 5216 | |
| 5217 | Online._missEnd = function (start, fromMini) |
| 5218 | { |
| 5219 | var w = window.innerWidth || 800; |
| 5220 | var outward; |
| 5221 | if (start.x < w * 0.4) |
| 5222 | { outward = -(80 + Math.random() * w * 0.25); } |
| 5223 | else if (start.x > w * 0.6) |
| 5224 | { outward = (80 + Math.random() * w * 0.25); } |
| 5225 | else |
| 5226 | { outward = (Math.random() * 2 - 1) * w * 0.35; } |
| 5227 | var lift = fromMini ? (80 + Math.random() * 160) : (120 + Math.random() * 220); |
| 5228 | return { x: start.x + outward, y: start.y - lift }; |
| 5229 | }; |
| 5230 | |
| 5231 | Online._missCtrl = function (start, end) |
| 5232 | { |
| 5233 | return { |
| 5234 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160, |
| 5235 | y: Math.min(start.y, end.y) - 40 - Math.random() * 80 |
| 5236 | }; |
| 5237 | }; |
| 5238 | |
| 5239 | Online._hitCtrl = function (start, end) |
| 5240 | { |
| 5241 | return { |
| 5242 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40, |
| 5243 | y: (start.y + end.y) / 2 - 30 |
| 5244 | }; |
| 5245 | }; |
| 5246 | |
| 5247 | Online._findFlight = function (sid) |
| 5248 | { |
| 5249 | sid = String(sid); |
| 5250 | if (!this._flights) |
| 5251 | { return null; } |
| 5252 | for (var i = 0; i < this._flights.length; i++) |
| 5253 | { |
| 5254 | if (String(this._flights[i].id) === sid) |
| 5255 | { return this._flights[i]; } |
| 5256 | } |
| 5257 | return null; |
| 5258 | }; |
| 5259 | |
| 5260 | /** |
| 5261 | * True once a block has really left the field, matching cullOffTop |
| 5262 | * (y ≥ height, default 13). Used so an arc never starts for a |
| 5263 | * rocket that only peaked near the top and fell back (t1137). |
| 5264 | */ |
| 5265 | Online.EXIT_Y = 13; |
| 5266 | Online.hasLeftTheField = function (y, height) |
| 5267 | { |
| 5268 | height = height == null ? this.EXIT_Y : height; |
| 5269 | return y != null && isFinite(y) && y >= height - 1e-9; |
| 5270 | }; |
| 5271 | |
| 5272 | /** |
| 5273 | * Start a flight only at the true exit (y ≥ 13). Callers that pass |
| 5274 | * a rocket still on the field are ignored, so a fallback never |
| 5275 | * shows a phantom arc (t1137). The start pose is the exit cell, |
| 5276 | * continuing upward; retargetFlight aims it at the real target |
| 5277 | * if a preview was used in the same frame as cull. |
| 5278 | */ |
| 5279 | Online.previewExit = function (info) |
| 5280 | { |
| 5281 | if (!info || info.id == null) |
| 5282 | { return null; } |
| 5283 | if (!this.hasLeftTheField(info.y)) |
| 5284 | { return null; } |
| 5285 | var sid = String(info.id); |
| 5286 | if (!this._flights) |
| 5287 | { this._flights = []; } |
| 5288 | if (!this._flightSeen) |
| 5289 | { this._flightSeen = Object.create(null); } |
| 5290 | if (!this._previewIds) |
| 5291 | { this._previewIds = Object.create(null); } |
| 5292 | if (this._previewIds[sid] || this._flightSeen[sid]) |
| 5293 | { return; } |
| 5294 | var front = document.getElementById('flights'); |
| 5295 | if (!front || !info.start) |
| 5296 | { return; } |
| 5297 | var start = info.start; |
| 5298 | var field = this.playerFieldRect(); |
| 5299 | var clearance = 80; |
| 5300 | if (typeof MiniLayout !== 'undefined' && MiniLayout.ARC_TOP_PAD) |
| 5301 | { clearance = Math.max(clearance, MiniLayout.ARC_TOP_PAD * 0.7); } |
| 5302 | var apexY = (field ? field.top : start.y) - clearance; |
| 5303 | if (apexY < 8) |
| 5304 | { apexY = Math.max(8, start.y - 80); } |
| 5305 | var end = { x: start.x, y: apexY }; |
| 5306 | var duration = this.FLIGHT_DURATION * 1000; |
| 5307 | var full = this._fullTileSize(); |
| 5308 | var el = document.createElement('img'); |
| 5309 | el.src = colorSpriteUrl(info.color); |
| 5310 | el.alt = ''; |
| 5311 | el.className = 'flight'; |
| 5312 | el.style.width = full + 'px'; |
| 5313 | el.style.height = full + 'px'; |
| 5314 | front.appendChild(el); |
| 5315 | var endSpeed = this._fallSpeedPx(field, this.FLIGHT_DURATION); |
| 5316 | var curve = this.makeAroundCurve(start, end, info.from_id ? null : field, { |
| 5317 | clearance: clearance, |
| 5318 | startSpeedPx: endSpeed, |
| 5319 | endSpeedPx: endSpeed |
| 5320 | }); |
| 5321 | var now = performance.now(); |
| 5322 | var mini = info.from_id ? (this._miniCellSize(info.from_id) / full) : 1; |
| 5323 | if (mini < 0.12) |
| 5324 | { mini = 0.2; } |
| 5325 | this._flights.push({ |
| 5326 | el: el, |
| 5327 | start: start, |
| 5328 | end: end, |
| 5329 | ctrl: null, |
| 5330 | height: clearance, |
| 5331 | t0: now, |
| 5332 | duration: duration, |
| 5333 | spin: (Math.random() * 2 - 1) * 90, |
| 5334 | miss: false, |
| 5335 | kind: info.from_id ? 'enemy-to-enemy' : 'player-to-enemy', |
| 5336 | startScale: mini, |
| 5337 | endScale: mini, |
| 5338 | angle: 0, |
| 5339 | id: sid, |
| 5340 | landCol: null, |
| 5341 | field: field, |
| 5342 | curve: curve, |
| 5343 | preview: true, |
| 5344 | color: info.color, |
| 5345 | from_id: info.from_id || null |
| 5346 | }); |
| 5347 | this._previewIds[sid] = true; |
| 5348 | this._flightSeen[sid] = now; |
| 5349 | }; |
| 5350 | |
| 5351 | /** True while a field sprite should stay hidden — the flight is its visual. */ |
| 5352 | Online.hasExitVisual = function (id) |
| 5353 | { |
| 5354 | if (id == null) |
| 5355 | { return false; } |
| 5356 | var sid = String(id); |
| 5357 | if (this._previewIds && this._previewIds[sid]) |
| 5358 | { return true; } |
| 5359 | var f = this._findFlight(sid); |
| 5360 | if (!f || f.miss) |
| 5361 | { return false; } |
| 5362 | var raw = (performance.now() - f.t0) / f.duration; |
| 5363 | return raw < 0.4; |
| 5364 | }; |
| 5365 | |
| 5366 | /** |
| 5367 | * Once the shot is scored, pick up the preview at its current pose |
| 5368 | * and fly the rest of the way to the real end (no jump). |
| 5369 | */ |
| 5370 | Online.retargetFlight = function (f, r, meta, now) |
| 5371 | { |
| 5372 | now = now || performance.now(); |
| 5373 | meta = meta || {}; |
| 5374 | var raw = (now - f.t0) / f.duration; |
| 5375 | if (raw < 0) |
| 5376 | { raw = 0; } |
| 5377 | if (raw > 0.95) |
| 5378 | { raw = 0.95; } |
| 5379 | var u = this.smoothParam(raw, 0.9, 0.9); |
| 5380 | var pt = f.curve |
| 5381 | ? this.curvePoint(f.curve, u) |
| 5382 | : { x: f.start.x, y: f.start.y }; |
| 5383 | var curScale = f.startScale + (f.endScale - f.startScale) * u; |
| 5384 | |
| 5385 | var fromId = r.from_id || r.from_user || meta.from_id || f.from_id || null; |
| 5386 | var targetId = r.target_id != null ? r.target_id : r.to_user; |
| 5387 | var miss = !!r.miss; |
| 5388 | var kind = this.flightKind(fromId, targetId, miss); |
| 5389 | var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000; |
| 5390 | |
| 5391 | var landCol = null; |
| 5392 | if (r.land_col != null && isFinite(r.land_col)) |
| 5393 | { landCol = r.land_col | 0; } |
| 5394 | else if (meta.land_col != null && isFinite(meta.land_col)) |
| 5395 | { landCol = meta.land_col | 0; } |
| 5396 | else if (f.landCol != null) |
| 5397 | { landCol = f.landCol; } |
| 5398 | if (landCol != null) |
| 5399 | { this.rememberLandCol(String(f.id), landCol); } |
| 5400 | |
| 5401 | var end; |
| 5402 | if (miss) |
| 5403 | { end = this._missEnd(pt, kind === 'enemy-miss'); } |
| 5404 | else if (kind === 'enemy-to-player') |
| 5405 | { end = this.fieldEntry(landCol != null ? landCol : 4.5); } |
| 5406 | else |
| 5407 | { end = this._targetEnd(targetId, pt, landCol); } |
| 5408 | |
| 5409 | var flightColor = miss |
| 5410 | ? (r.color != null ? r.color : meta.color) |
| 5411 | : -1; |
| 5412 | if (f.el && flightColor !== f.color) |
| 5413 | { f.el.src = colorSpriteUrl(flightColor); } |
| 5414 | |
| 5415 | var full = this._fullTileSize(); |
| 5416 | var mini = this._miniCellSize(fromId || targetId) / full; |
| 5417 | if (mini < 0.12) |
| 5418 | { mini = 0.2; } |
| 5419 | var endScale; |
| 5420 | if (kind === 'player-to-enemy') |
| 5421 | { endScale = mini; } |
| 5422 | else if (kind === 'enemy-to-player') |
| 5423 | { endScale = 1; } |
| 5424 | else if (kind === 'enemy-to-enemy') |
| 5425 | { endScale = mini; } |
| 5426 | else |
| 5427 | { endScale = 0; } |
| 5428 | |
| 5429 | var field = this.playerFieldRect(); |
| 5430 | var around = !miss && (kind === 'enemy-to-player' || kind === 'player-to-enemy'); |
| 5431 | var height = miss |
| 5432 | ? Math.max(40, Math.min(160, Math.abs(end.y - pt.y) * 0.35)) |
| 5433 | : this.arcHeight(pt, end); |
| 5434 | var clearance = Math.max(64, height * 0.45); |
| 5435 | var endSpeed = this._fallSpeedPx(field, duration / 1000); |
| 5436 | var curve = miss ? null : this.makeAroundCurve(pt, end, around ? field : null, { |
| 5437 | clearance: clearance, |
| 5438 | startSpeedPx: endSpeed, |
| 5439 | endSpeedPx: endSpeed |
| 5440 | }); |
| 5441 | |
| 5442 | f.start = pt; |
| 5443 | f.end = end; |
| 5444 | f.t0 = now; |
| 5445 | f.duration = duration; |
| 5446 | f.curve = curve; |
| 5447 | f.ctrl = miss ? this._missCtrl(pt, end) : null; |
| 5448 | f.height = height; |
| 5449 | f.miss = miss; |
| 5450 | f.kind = kind; |
| 5451 | f.startScale = curScale; |
| 5452 | f.endScale = endScale; |
| 5453 | f.landCol = landCol; |
| 5454 | f.field = field; |
| 5455 | f.preview = false; |
| 5456 | f.color = flightColor; |
| 5457 | f.from_id = fromId; |
| 5458 | if (this._previewIds) |
| 5459 | { delete this._previewIds[String(f.id)]; } |
| 5460 | |
| 5461 | if (kind === 'enemy-to-enemy' && f.el) |
| 5462 | { |
| 5463 | var back = document.getElementById('flights-back'); |
| 5464 | if (back && f.el.parentNode !== back) |
| 5465 | { |
| 5466 | f.el.className = 'flight flight-behind'; |
| 5467 | back.appendChild(f.el); |
| 5468 | } |
| 5469 | } |
| 5470 | }; |
| 5471 | |
| 5472 | /** Point and (optional) derivative of a flight at parameter u. */ |
| 5473 | Online.flightPointAt = function (f, u) |
| 5474 | { |
| 5475 | if (!f) |
| 5476 | { return { x: 0, y: 0 }; } |
| 5477 | if (f.curve) |
| 5478 | { return this.curvePoint(f.curve, u); } |
| 5479 | if (f.miss && f.ctrl) |
| 5480 | { |
| 5481 | var e = u; |
| 5482 | var mt = 1 - e; |
| 5483 | return { |
| 5484 | x: mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x, |
| 5485 | y: mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y |
| 5486 | }; |
| 5487 | } |
| 5488 | return this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45); |
| 5489 | }; |
| 5490 | |
| 5491 | Online.pointAllowedOnArc = function (p, field, start, end, slop) |
| 5492 | { |
| 5493 | if (!this.pointInFieldInterior(p.x, p.y, field, slop)) |
| 5494 | { return true; } |
| 5495 | if (!field) |
| 5496 | { return true; } |
| 5497 | var w = (field.right - field.left) / 10; |
| 5498 | var corridor = w * 0.7; |
| 5499 | if (start && Math.abs(p.x - start.x) <= corridor) |
| 5500 | { return true; } |
| 5501 | if (end && Math.abs(p.x - end.x) <= corridor) |
| 5502 | { return true; } |
| 5503 | return false; |
| 5504 | }; |
| 5505 | |
| 5506 | Online.tickFlights = function (now) |
| 5507 | { |
| 5508 | now = now || performance.now(); |
| 5509 | var leftover = []; |
| 5510 | for (var i = 0; i < this._flights.length; i++) |
| 5511 | { |
| 5512 | var f = this._flights[i]; |
| 5513 | var raw = (now - f.t0) / f.duration; |
| 5514 | if (raw >= 1) |
| 5515 | { |
| 5516 | if (f.el && f.el.parentNode) |
| 5517 | { f.el.parentNode.removeChild(f.el); } |
| 5518 | if (this._previewIds) |
| 5519 | { delete this._previewIds[String(f.id)]; } |
| 5520 | continue; |
| 5521 | } |
| 5522 | if (raw < 0) |
| 5523 | { raw = 0; } |
| 5524 | // Non-zero endpoint speed so the handoff matches fallSpeed (t1135). |
| 5525 | var u = f.miss ? this.easeInOutCubic(raw) : this.smoothParam(raw, 0.85, 0.85); |
| 5526 | var x, y; |
| 5527 | if (f.miss && f.ctrl) |
| 5528 | { |
| 5529 | var e = raw * raw; |
| 5530 | var mt = 1 - e; |
| 5531 | x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x; |
| 5532 | y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y; |
| 5533 | } |
| 5534 | else if (f.curve) |
| 5535 | { |
| 5536 | var pt = this.curvePoint(f.curve, u); |
| 5537 | x = pt.x; |
| 5538 | y = pt.y; |
| 5539 | } |
| 5540 | else |
| 5541 | { |
| 5542 | var pt = this.aroundFieldPoint(f.start, f.end, u, f.field, (f.height || 80) * 0.45); |
| 5543 | x = pt.x; |
| 5544 | y = pt.y; |
| 5545 | } |
| 5546 | var scale = f.startScale + (f.endScale - f.startScale) * u; |
| 5547 | if (f.miss) |
| 5548 | { |
| 5549 | var fadeU = raw * raw; |
| 5550 | scale = f.startScale * (1 - fadeU); |
| 5551 | } |
| 5552 | var angle = f.angle + f.spin * raw; |
| 5553 | f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate(' |
| 5554 | + angle + 'deg) scale(' + scale + ')'; |
| 5555 | f.el.style.opacity = f.miss ? String(Math.max(0, 1 - raw * raw)) : '1'; |
| 5556 | leftover.push(f); |
| 5557 | } |
| 5558 | this._flights = leftover; |
| 5559 | if (this._flightSeen) |
| 5560 | { |
| 5561 | var cutoff = now - 8000; |
| 5562 | var ids = Object.keys(this._flightSeen); |
| 5563 | for (var k = 0; k < ids.length; k++) |
| 5564 | { |
| 5565 | if (this._flightSeen[ids[k]] < cutoff) |
| 5566 | { delete this._flightSeen[ids[k]]; } |
| 5567 | } |
| 5568 | } |
| 5569 | }; |
| 5570 | |
| 5571 | Online.clearFlights = function () |
| 5572 | { |
| 5573 | for (var i = 0; i < this._flights.length; i++) |
| 5574 | { |
| 5575 | var el = this._flights[i].el; |
| 5576 | if (el && el.parentNode) |
| 5577 | { el.parentNode.removeChild(el); } |
| 5578 | } |
| 5579 | this._flights = []; |
| 5580 | this._flightLocals = {}; |
| 5581 | this._flightSeen = Object.create(null); |
| 5582 | this._pendingLandCol = Object.create(null); |
| 5583 | this._previewIds = Object.create(null); |
| 5584 | }; |
| 5585 | |
| 5586 | if (typeof globalThis !== 'undefined') |
| 5587 | { globalThis.Online = Online; } |
| 5588 | if (typeof module !== 'undefined' && module.exports) |
| 5589 | { module.exports = Online; } |
| 5590 | |
| 5591 | /* |
| 5592 | * End File: |
| 5593 | * /workspace/meteors/src/online.js |
| 5594 | */ |
| 5595 | |
| 5596 | /* |
| 5597 | * Start File: |
| 5598 | * /workspace/meteors/src/offline.js |
| 5599 | */ |
| 5600 | 'use strict'; |
| 5601 | |
| 5602 | /** |
| 5603 | * Offline Strijd: a client-side single-player mode that plays like |
| 5604 | * multiplayer (shots, miss chance, opponent minis) against local bots |
| 5605 | * (t1119). Pure simulation — no server. |
| 5606 | */ |
| 5607 | |
| 5608 | var Offline = (function () |
| 5609 | { |
| 5610 | |
| 5611 | var BOT_INTERVAL = 0.5; |
| 5612 | var SKILL_START = 0.05; |
| 5613 | var SKILL_STEP = 0.01; |
| 5614 | var SKILL_CAP = 1; |
| 5615 | var FLIGHT_DURATION = 1.2; |
| 5616 | var ROUND_WON_DELAY = 1.5; |
| 5617 | |
| 5618 | /** |
| 5619 | * N opponents for N−1 rounds when N >= 2; 1 opponent for 1 round. |
| 5620 | * Documented in docs/ASSUMPTIONS.md (t1119). |
| 5621 | */ |
| 5622 | function roundsAtOpponentCount (n) |
| 5623 | { |
| 5624 | if (n <= 1) |
| 5625 | { return 1; } |
| 5626 | return n - 1; |
| 5627 | } |
| 5628 | |
| 5629 | function skillForRound (round) |
| 5630 | { |
| 5631 | var r = Math.max(1, Math.floor(Number(round) || 1)); |
| 5632 | var pct = 5 + (r - 1); |
| 5633 | if (pct > 100) |
| 5634 | { pct = 100; } |
| 5635 | return pct / 100; |
| 5636 | } |
| 5637 | |
| 5638 | function scheduleForRound (round) |
| 5639 | { |
| 5640 | var r = Math.max(1, Math.floor(Number(round) || 1)); |
| 5641 | var cursor = 1; |
| 5642 | var n = 1; |
| 5643 | for (;;) |
| 5644 | { |
| 5645 | var count = roundsAtOpponentCount(n); |
| 5646 | if (r < cursor + count) |
| 5647 | { |
| 5648 | return { |
| 5649 | round: r, |
| 5650 | opponents: n, |
| 5651 | skill: skillForRound(r), |
| 5652 | roundsAtLevel: count, |
| 5653 | indexInLevel: r - cursor + 1 |
| 5654 | }; |
| 5655 | } |
| 5656 | cursor += count; |
| 5657 | n++; |
| 5658 | if (n > 10000) |
| 5659 | { |
| 5660 | return { |
| 5661 | round: r, |
| 5662 | opponents: n, |
| 5663 | skill: SKILL_CAP, |
| 5664 | roundsAtLevel: roundsAtOpponentCount(n), |
| 5665 | indexInLevel: 1 |
| 5666 | }; |
| 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | |
| 5671 | function GameCoreCtor () |
| 5672 | { |
| 5673 | if (typeof GameCore !== 'undefined') |
| 5674 | { return GameCore; } |
| 5675 | if (typeof module !== 'undefined') |
| 5676 | { return require('./gameCore.js').GameCore; } |
| 5677 | return null; |
| 5678 | } |
| 5679 | |
| 5680 | function PlanetMod () |
| 5681 | { |
| 5682 | if (typeof Planet !== 'undefined') |
| 5683 | { return Planet; } |
| 5684 | if (typeof module !== 'undefined') |
| 5685 | { return require('./planet.js'); } |
| 5686 | return null; |
| 5687 | } |
| 5688 | |
| 5689 | /** |
| 5690 | * Integer-row colour grid of grounded (still / settling / locked) |
| 5691 | * blocks, including greys (color < 0) so a swap can shift them. |
| 5692 | */ |
| 5693 | function snapshotGrid (core) |
| 5694 | { |
| 5695 | var width = core.width; |
| 5696 | var height = core.height; |
| 5697 | var grid = []; |
| 5698 | for (var c = 0; c < width; c++) |
| 5699 | { |
| 5700 | grid[c] = []; |
| 5701 | for (var r = 0; r < height; r++) |
| 5702 | { grid[c][r] = null; } |
| 5703 | } |
| 5704 | for (var i = 0; i < core.blocks.length; i++) |
| 5705 | { |
| 5706 | var b = core.blocks[i]; |
| 5707 | if (!core.isGrounded(b)) |
| 5708 | { continue; } |
| 5709 | var row = Math.round(b.y); |
| 5710 | if (row < 0 || row >= height || b.col < 0 || b.col >= width) |
| 5711 | { continue; } |
| 5712 | if (Math.abs(b.y - row) > 0.2) |
| 5713 | { continue; } |
| 5714 | grid[b.col][row] = b.color; |
| 5715 | } |
| 5716 | return grid; |
| 5717 | } |
| 5718 | |
| 5719 | function cloneGrid (grid) |
| 5720 | { |
| 5721 | var out = []; |
| 5722 | for (var c = 0; c < grid.length; c++) |
| 5723 | { out[c] = grid[c].slice(); } |
| 5724 | return out; |
| 5725 | } |
| 5726 | |
| 5727 | /** |
| 5728 | * Apply swapToward's net result: the block at fromRow moves to toRow, |
| 5729 | * and the blocks it passed through each shift one cell toward fromRow. |
| 5730 | */ |
| 5731 | function applyColumnMove (grid, col, fromRow, toRow) |
| 5732 | { |
| 5733 | if (fromRow === toRow) |
| 5734 | { return; } |
| 5735 | var moving = grid[col][fromRow]; |
| 5736 | if (toRow > fromRow) |
| 5737 | { |
| 5738 | for (var y = fromRow; y < toRow; y++) |
| 5739 | { grid[col][y] = grid[col][y + 1]; } |
| 5740 | grid[col][toRow] = moving; |
| 5741 | } |
| 5742 | else |
| 5743 | { |
| 5744 | for (var y = fromRow; y > toRow; y--) |
| 5745 | { grid[col][y] = grid[col][y - 1]; } |
| 5746 | grid[col][toRow] = moving; |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | function hasMatch (grid, width, height) |
| 5751 | { |
| 5752 | var c, r, run, prev; |
| 5753 | for (r = 0; r < height; r++) |
| 5754 | { |
| 5755 | run = 0; |
| 5756 | prev = null; |
| 5757 | for (c = 0; c < width; c++) |
| 5758 | { |
| 5759 | var color = grid[c][r]; |
| 5760 | if (color != null && color >= 0 && color === prev) |
| 5761 | { |
| 5762 | run++; |
| 5763 | if (run >= 3) |
| 5764 | { return true; } |
| 5765 | } |
| 5766 | else |
| 5767 | { |
| 5768 | run = (color != null && color >= 0) ? 1 : 0; |
| 5769 | prev = (color != null && color >= 0) ? color : null; |
| 5770 | } |
| 5771 | } |
| 5772 | } |
| 5773 | for (c = 0; c < width; c++) |
| 5774 | { |
| 5775 | run = 0; |
| 5776 | prev = null; |
| 5777 | for (r = 0; r < height; r++) |
| 5778 | { |
| 5779 | var color = grid[c][r]; |
| 5780 | if (color != null && color >= 0 && color === prev) |
| 5781 | { |
| 5782 | run++; |
| 5783 | if (run >= 3) |
| 5784 | { return true; } |
| 5785 | } |
| 5786 | else |
| 5787 | { |
| 5788 | run = (color != null && color >= 0) ? 1 : 0; |
| 5789 | prev = (color != null && color >= 0) ? color : null; |
| 5790 | } |
| 5791 | } |
| 5792 | } |
| 5793 | return false; |
| 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * Contiguous segments of draggable, nearly-integer-y blocks in a |
| 5798 | * column. Gaps break a segment the same way swapToward stops at gaps. |
| 5799 | */ |
| 5800 | function contiguousSegments (core, col) |
| 5801 | { |
| 5802 | if (!core.columnAllowsDrag(col)) |
| 5803 | { return []; } |
| 5804 | var list = core.blocksInColumn(col).filter(function (b) |
| 5805 | { |
| 5806 | return core.isDraggableBlock(b); |
| 5807 | }); |
| 5808 | list.sort(function (a, b) { return a.y - b.y; }); |
| 5809 | var segs = []; |
| 5810 | var cur = []; |
| 5811 | for (var i = 0; i < list.length; i++) |
| 5812 | { |
| 5813 | var y = list[i].y; |
| 5814 | if (Math.abs(y - Math.round(y)) > 0.15) |
| 5815 | { |
| 5816 | if (cur.length) |
| 5817 | { |
| 5818 | segs.push(cur); |
| 5819 | cur = []; |
| 5820 | } |
| 5821 | continue; |
| 5822 | } |
| 5823 | if (cur.length && Math.abs(y - (cur[cur.length - 1].y + 1)) > 0.15) |
| 5824 | { |
| 5825 | segs.push(cur); |
| 5826 | cur = [list[i]]; |
| 5827 | } |
| 5828 | else |
| 5829 | { cur.push(list[i]); } |
| 5830 | } |
| 5831 | if (cur.length) |
| 5832 | { segs.push(cur); } |
| 5833 | return segs; |
| 5834 | } |
| 5835 | |
| 5836 | /** |
| 5837 | * The shortest vertical move that would create a 3+ match, or null. |
| 5838 | * Ties break on lowest column, then lowest fromY, then lowest toY. |
| 5839 | */ |
| 5840 | function findBotMove (core) |
| 5841 | { |
| 5842 | var grid = snapshotGrid(core); |
| 5843 | if (hasMatch(grid, core.width, core.height)) |
| 5844 | { return null; } |
| 5845 | var best = null; |
| 5846 | for (var col = 0; col < core.width; col++) |
| 5847 | { |
| 5848 | var segs = contiguousSegments(core, col); |
| 5849 | for (var s = 0; s < segs.length; s++) |
| 5850 | { |
| 5851 | var seg = segs[s]; |
| 5852 | if (seg.length < 2) |
| 5853 | { continue; } |
| 5854 | for (var i = 0; i < seg.length; i++) |
| 5855 | { |
| 5856 | var fromY = Math.round(seg[i].y); |
| 5857 | for (var j = 0; j < seg.length; j++) |
| 5858 | { |
| 5859 | if (i === j) |
| 5860 | { continue; } |
| 5861 | var toY = Math.round(seg[j].y); |
| 5862 | var dist = Math.abs(toY - fromY); |
| 5863 | if (best && dist > best.dist) |
| 5864 | { continue; } |
| 5865 | var trial = cloneGrid(grid); |
| 5866 | applyColumnMove(trial, col, fromY, toY); |
| 5867 | if (!hasMatch(trial, core.width, core.height)) |
| 5868 | { continue; } |
| 5869 | var cand = { |
| 5870 | col: col, |
| 5871 | fromY: fromY, |
| 5872 | toY: toY, |
| 5873 | dist: dist, |
| 5874 | blockId: seg[i].id |
| 5875 | }; |
| 5876 | if (!best || dist < best.dist |
| 5877 | || (dist === best.dist && col < best.col) |
| 5878 | || (dist === best.dist && col === best.col && fromY < best.fromY) |
| 5879 | || (dist === best.dist && col === best.col && fromY === best.fromY |
| 5880 | && toY < best.toY)) |
| 5881 | { best = cand; } |
| 5882 | } |
| 5883 | } |
| 5884 | } |
| 5885 | } |
| 5886 | return best; |
| 5887 | } |
| 5888 | |
| 5889 | function executeBotMove (core, move) |
| 5890 | { |
| 5891 | if (!core || !move) |
| 5892 | { return false; } |
| 5893 | var list = core.blocksInColumn(move.col); |
| 5894 | var block = null; |
| 5895 | for (var i = 0; i < list.length; i++) |
| 5896 | { |
| 5897 | if (move.blockId != null && list[i].id === move.blockId) |
| 5898 | { |
| 5899 | block = list[i]; |
| 5900 | break; |
| 5901 | } |
| 5902 | if (Math.abs(list[i].y - move.fromY) < 0.2) |
| 5903 | { block = list[i]; } |
| 5904 | } |
| 5905 | if (!block || !core.isDraggableBlock(block)) |
| 5906 | { return false; } |
| 5907 | core.swapToward(block, move.toY); |
| 5908 | return true; |
| 5909 | } |
| 5910 | |
| 5911 | /** |
| 5912 | * One AI tick. `rng` is a () => [0,1) function. Success if roll < skill. |
| 5913 | * At skill 0 the bot never moves; at skill 1 it always looks for a move. |
| 5914 | */ |
| 5915 | function tryBotRoll (core, skill, rng) |
| 5916 | { |
| 5917 | rng = rng || Math.random; |
| 5918 | var roll = rng(); |
| 5919 | if (!(roll < skill)) |
| 5920 | { |
| 5921 | return { rolled: true, success: false, moved: false, roll: roll, move: null }; |
| 5922 | } |
| 5923 | var move = findBotMove(core); |
| 5924 | if (!move) |
| 5925 | { |
| 5926 | return { rolled: true, success: true, moved: false, roll: roll, move: null }; |
| 5927 | } |
| 5928 | var ok = executeBotMove(core, move); |
| 5929 | return { rolled: true, success: true, moved: !!ok, roll: roll, move: move }; |
| 5930 | } |
| 5931 | |
| 5932 | function pickPlanet (rng, P) |
| 5933 | { |
| 5934 | var all = P.allPlanets(); |
| 5935 | if (!all.length) |
| 5936 | { return P.defaultPlanet(); } |
| 5937 | var i = Math.floor(rng() * all.length) % all.length; |
| 5938 | return all[i]; |
| 5939 | } |
| 5940 | |
| 5941 | function OfflineBattle (opts) |
| 5942 | { |
| 5943 | opts = opts || {}; |
| 5944 | var P = PlanetMod(); |
| 5945 | this.playerPlanet = opts.playerPlanet || (P && P.defaultPlanet()); |
| 5946 | this.rng = opts.rng || Math.random; |
| 5947 | this.round = opts.round || 1; |
| 5948 | this.schedule = scheduleForRound(this.round); |
| 5949 | this.clock = 0; |
| 5950 | this.state = 'playing'; |
| 5951 | this.pending = []; |
| 5952 | this.flightResults = []; |
| 5953 | this.shotLocals = []; |
| 5954 | this.bots = []; |
| 5955 | this.playerId = 'player'; |
| 5956 | this._initBots(); |
| 5957 | } |
| 5958 | |
| 5959 | OfflineBattle.prototype._initBots = function () |
| 5960 | { |
| 5961 | var P = PlanetMod(); |
| 5962 | var Ctor = GameCoreCtor(); |
| 5963 | var n = this.schedule.opponents; |
| 5964 | this.bots = []; |
| 5965 | for (var i = 0; i < n; i++) |
| 5966 | { |
| 5967 | var planet = pickPlanet(this.rng, P); |
| 5968 | var miss = P.missChance(planet); |
| 5969 | var core = new Ctor(planet, this.rng, miss); |
| 5970 | this.bots.push({ |
| 5971 | id: 'bot-' + (i + 1), |
| 5972 | name: 'Bot ' + (i + 1), |
| 5973 | planet: planet, |
| 5974 | core: core, |
| 5975 | eliminated: false, |
| 5976 | score: 0, |
| 5977 | aiAcc: 0 |
| 5978 | }); |
| 5979 | } |
| 5980 | }; |
| 5981 | |
| 5982 | OfflineBattle.prototype.aliveBots = function () |
| 5983 | { |
| 5984 | var out = []; |
| 5985 | for (var i = 0; i < this.bots.length; i++) |
| 5986 | { |
| 5987 | if (!this.bots[i].eliminated && this.bots[i].core |
| 5988 | && this.bots[i].core.state !== 'gameover') |
| 5989 | { out.push(this.bots[i]); } |
| 5990 | } |
| 5991 | return out; |
| 5992 | }; |
| 5993 | |
| 5994 | OfflineBattle.prototype.coreFor = function (id, playerCore) |
| 5995 | { |
| 5996 | if (id === this.playerId) |
| 5997 | { return playerCore; } |
| 5998 | for (var i = 0; i < this.bots.length; i++) |
| 5999 | { |
| 6000 | if (this.bots[i].id === id) |
| 6001 | { return this.bots[i].core; } |
| 6002 | } |
| 6003 | return null; |
| 6004 | }; |
| 6005 | |
| 6006 | OfflineBattle.prototype._targetsFor = function (fromId, playerCore) |
| 6007 | { |
| 6008 | var targets = []; |
| 6009 | if (fromId !== this.playerId && playerCore && playerCore.state === 'playing') |
| 6010 | { targets.push({ id: this.playerId, core: playerCore }); } |
| 6011 | for (var i = 0; i < this.bots.length; i++) |
| 6012 | { |
| 6013 | var bot = this.bots[i]; |
| 6014 | if (bot.id === fromId || bot.eliminated) |
| 6015 | { continue; } |
| 6016 | if (bot.core && bot.core.state === 'playing') |
| 6017 | { targets.push({ id: bot.id, core: bot.core }); } |
| 6018 | } |
| 6019 | return targets; |
| 6020 | }; |
| 6021 | |
| 6022 | OfflineBattle.prototype._dispatchShots = function (fromId, core, planet, playerCore) |
| 6023 | { |
| 6024 | if (!core || !core.consumeOutgoingShots) |
| 6025 | { return; } |
| 6026 | var shots = core.consumeOutgoingShots(); |
| 6027 | if (!shots.length) |
| 6028 | { return; } |
| 6029 | var P = PlanetMod(); |
| 6030 | var missChance = (P && planet) ? P.missChance(planet) : 0.30; |
| 6031 | var targets = this._targetsFor(fromId, playerCore); |
| 6032 | for (var i = 0; i < shots.length; i++) |
| 6033 | { |
| 6034 | var shot = shots[i]; |
| 6035 | var miss = targets.length === 0 || this.rng() < missChance; |
| 6036 | var target = null; |
| 6037 | if (!miss) |
| 6038 | { |
| 6039 | var idx = Math.floor(this.rng() * targets.length) % targets.length; |
| 6040 | target = targets[idx]; |
| 6041 | } |
| 6042 | var clientId = fromId + '-' + shot.id + '-' + this.clock; |
| 6043 | var landCol = null; |
| 6044 | if (!miss && target && target.core && target.core.pickGarbageColumn) |
| 6045 | { landCol = target.core.pickGarbageColumn(); } |
| 6046 | var result = { |
| 6047 | client_id: clientId, |
| 6048 | miss: miss, |
| 6049 | target_id: target ? target.id : null, |
| 6050 | color: shot.color, |
| 6051 | flight_duration: FLIGHT_DURATION, |
| 6052 | from_id: fromId, |
| 6053 | col: shot.col, |
| 6054 | y: shot.y, |
| 6055 | land_col: landCol |
| 6056 | }; |
| 6057 | this.flightResults.push(result); |
| 6058 | this.shotLocals.push({ |
| 6059 | client_id: clientId, |
| 6060 | col: shot.col, |
| 6061 | y: shot.y, |
| 6062 | color: shot.color, |
| 6063 | from_id: fromId, |
| 6064 | land_col: landCol |
| 6065 | }); |
| 6066 | if (!miss && target) |
| 6067 | { |
| 6068 | this.pending.push({ |
| 6069 | at: this.clock + FLIGHT_DURATION, |
| 6070 | targetId: target.id, |
| 6071 | color: shot.color, |
| 6072 | fromId: fromId, |
| 6073 | landCol: landCol |
| 6074 | }); |
| 6075 | } |
| 6076 | } |
| 6077 | }; |
| 6078 | |
| 6079 | OfflineBattle.prototype._deliver = function (playerCore) |
| 6080 | { |
| 6081 | var keep = []; |
| 6082 | for (var i = 0; i < this.pending.length; i++) |
| 6083 | { |
| 6084 | var ev = this.pending[i]; |
| 6085 | if (this.clock + 1e-9 < ev.at) |
| 6086 | { |
| 6087 | keep.push(ev); |
| 6088 | continue; |
| 6089 | } |
| 6090 | var core = this.coreFor(ev.targetId, playerCore); |
| 6091 | if (core && core.state === 'playing' && core.ingestGarbage) |
| 6092 | { core.ingestGarbage(ev.landCol); } |
| 6093 | } |
| 6094 | this.pending = keep; |
| 6095 | }; |
| 6096 | |
| 6097 | OfflineBattle.prototype.tick = function (dt, playerCore) |
| 6098 | { |
| 6099 | this.flightResults = []; |
| 6100 | this.shotLocals = []; |
| 6101 | if (this.state !== 'playing') |
| 6102 | { return { flights: [], locals: [] }; } |
| 6103 | if (dt < 0) |
| 6104 | { dt = 0; } |
| 6105 | this.clock += dt; |
| 6106 | |
| 6107 | var skill = this.schedule.skill; |
| 6108 | for (var i = 0; i < this.bots.length; i++) |
| 6109 | { |
| 6110 | var bot = this.bots[i]; |
| 6111 | if (bot.eliminated) |
| 6112 | { continue; } |
| 6113 | if (bot.core.state === 'gameover') |
| 6114 | { |
| 6115 | bot.eliminated = true; |
| 6116 | bot.score = bot.core.score; |
| 6117 | continue; |
| 6118 | } |
| 6119 | bot.core.tick(dt); |
| 6120 | bot.score = bot.core.score; |
| 6121 | bot.aiAcc += dt; |
| 6122 | while (bot.aiAcc >= BOT_INTERVAL) |
| 6123 | { |
| 6124 | bot.aiAcc -= BOT_INTERVAL; |
| 6125 | if (bot.core.state === 'playing' && !bot.core.drag) |
| 6126 | { tryBotRoll(bot.core, skill, this.rng); } |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | this._dispatchShots(this.playerId, playerCore, this.playerPlanet, playerCore); |
| 6131 | for (var i = 0; i < this.bots.length; i++) |
| 6132 | { |
| 6133 | var bot = this.bots[i]; |
| 6134 | if (bot.eliminated) |
| 6135 | { continue; } |
| 6136 | this._dispatchShots(bot.id, bot.core, bot.planet, playerCore); |
| 6137 | } |
| 6138 | this._deliver(playerCore); |
| 6139 | |
| 6140 | for (var i = 0; i < this.bots.length; i++) |
| 6141 | { |
| 6142 | if (this.bots[i].core && this.bots[i].core.state === 'gameover') |
| 6143 | { |
| 6144 | this.bots[i].eliminated = true; |
| 6145 | this.bots[i].score = this.bots[i].core.score; |
| 6146 | } |
| 6147 | } |
| 6148 | |
| 6149 | if (playerCore && playerCore.state === 'gameover') |
| 6150 | { this.state = 'lost'; } |
| 6151 | else if (this.aliveBots().length === 0 && this.bots.length > 0) |
| 6152 | { this.state = 'round_won'; } |
| 6153 | |
| 6154 | return { flights: this.flightResults, locals: this.shotLocals }; |
| 6155 | }; |
| 6156 | |
| 6157 | /** |
| 6158 | * Start the next round (fresh bots, new skill and opponent count). |
| 6159 | * The caller must also give the player a fresh GameCore. |
| 6160 | */ |
| 6161 | OfflineBattle.prototype.advanceRound = function () |
| 6162 | { |
| 6163 | this.round += 1; |
| 6164 | this.schedule = scheduleForRound(this.round); |
| 6165 | this.state = 'playing'; |
| 6166 | this.pending = []; |
| 6167 | this.flightResults = []; |
| 6168 | this.shotLocals = []; |
| 6169 | this.clock = 0; |
| 6170 | this._initBots(); |
| 6171 | return this.schedule; |
| 6172 | }; |
| 6173 | |
| 6174 | function createBattle (opts) |
| 6175 | { |
| 6176 | return new OfflineBattle(opts); |
| 6177 | } |
| 6178 | |
| 6179 | var api = { |
| 6180 | BOT_INTERVAL: BOT_INTERVAL, |
| 6181 | SKILL_START: SKILL_START, |
| 6182 | SKILL_STEP: SKILL_STEP, |
| 6183 | SKILL_CAP: SKILL_CAP, |
| 6184 | FLIGHT_DURATION: FLIGHT_DURATION, |
| 6185 | ROUND_WON_DELAY: ROUND_WON_DELAY, |
| 6186 | roundsAtOpponentCount: roundsAtOpponentCount, |
| 6187 | skillForRound: skillForRound, |
| 6188 | scheduleForRound: scheduleForRound, |
| 6189 | findBotMove: findBotMove, |
| 6190 | tryBotRoll: tryBotRoll, |
| 6191 | executeBotMove: executeBotMove, |
| 6192 | snapshotGrid: snapshotGrid, |
| 6193 | applyColumnMove: applyColumnMove, |
| 6194 | hasMatch: hasMatch, |
| 6195 | createBattle: createBattle, |
| 6196 | OfflineBattle: OfflineBattle |
| 6197 | }; |
| 6198 | |
| 6199 | if (typeof module !== 'undefined' && module.exports) |
| 6200 | { module.exports = api; } |
| 6201 | else if (typeof globalThis !== 'undefined') |
| 6202 | { globalThis.Offline = api; } |
| 6203 | |
| 6204 | return api; |
| 6205 | |
| 6206 | })(); |
| 6207 | |
| 6208 | if (typeof module !== 'undefined' && module.exports && typeof Offline !== 'undefined') |
| 6209 | { module.exports = Offline; } |
| 6210 | |
| 6211 | /* |
| 6212 | * End File: |
| 6213 | * /workspace/meteors/src/offline.js |
| 6214 | */ |
| 6215 | |
| 6216 | /* |
| 6217 | * Start File: |
| 6218 | * /workspace/meteors/src/main.js |
| 6219 | */ |
| 6220 | 'use strict'; |
| 6221 | |
| 6222 | /** |
| 6223 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 6224 | * only draws it, scales it to the viewport, and translates pointer events |
| 6225 | * into beginDrag / moveDrag / endDrag. Online play (stage 2) is wired |
| 6226 | * through Online (src/online.js) and the same GameCore. Offline Strijd |
| 6227 | * (t1119) uses Offline (src/offline.js) against local bots. |
| 6228 | */ |
| 6229 | |
| 6230 | function planetImages () |
| 6231 | { |
| 6232 | var list = [ |
| 6233 | { name: 'gray', url: 'images/block-gray.png' }, |
| 6234 | { name: 'locked', url: 'images/block-locked.png' }, |
| 6235 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 6236 | { name: 'cell', url: 'images/cell.png' }, |
| 6237 | { name: 'pixel', url: 'images/pixel.png' }, |
| 6238 | { name: 'columnWarn', url: 'images/column-warn.png' }, |
| 6239 | // Keep the original Terra placeholders as aliases (Tim may still have them). |
| 6240 | { name: 'ember', url: 'images/block-ember.png' }, |
| 6241 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 6242 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 6243 | { name: 'sun', url: 'images/block-sun.png' }, |
| 6244 | { name: 'orchid', url: 'images/block-orchid.png' } |
| 6245 | ]; |
| 6246 | var planets = (typeof Planet !== 'undefined' ? Planet.allPlanets() : []); |
| 6247 | for (var i = 0; i < planets.length; i++) |
| 6248 | { |
| 6249 | var pl = planets[i]; |
| 6250 | for (var c = 0; c < pl.colors.length; c++) |
| 6251 | { |
| 6252 | list.push({ |
| 6253 | name: Planet.spriteKey(pl, c), |
| 6254 | url: pl.colors[c].image |
| 6255 | }); |
| 6256 | } |
| 6257 | } |
| 6258 | return list; |
| 6259 | } |
| 6260 | |
| 6261 | var IMAGES = planetImages(); |
| 6262 | var GAME_OVER_DELAY = 2; |
| 6263 | |
| 6264 | var game = new Game(); |
| 6265 | var app = { |
| 6266 | mode: 'menu', |
| 6267 | core: null, |
| 6268 | scene: null, |
| 6269 | manager: null, |
| 6270 | planet: null, |
| 6271 | lastScore: 0, |
| 6272 | tile: 48, |
| 6273 | fieldX: 0, |
| 6274 | fieldY: 0, |
| 6275 | fieldW: 0, |
| 6276 | fieldH: 0, |
| 6277 | hudH: 56, |
| 6278 | sprites: Object.create(null), |
| 6279 | flames: Object.create(null), |
| 6280 | cells: [], |
| 6281 | pointerId: null, |
| 6282 | pointerType: null, |
| 6283 | activeTouchId: null, |
| 6284 | debugDrag: false, |
| 6285 | debugPhysics: false, |
| 6286 | gameOverShown: false, |
| 6287 | selectTween: null, |
| 6288 | online: false, |
| 6289 | eliminatedReported: false, |
| 6290 | spectating: false, |
| 6291 | miniH: 0, |
| 6292 | selectedPlanetId: 'terra', |
| 6293 | planetSelectContext: 'solo', |
| 6294 | offline: false, |
| 6295 | battle: null, |
| 6296 | swapTweens: null, |
| 6297 | columnWarn: null, |
| 6298 | warnClock: 0, |
| 6299 | miniAcc: 0, |
| 6300 | offlineResultShown: false, |
| 6301 | roundWonDelay: 0, |
| 6302 | roundWonHandled: false |
| 6303 | }; |
| 6304 | |
| 6305 | window.onload = function () |
| 6306 | { |
| 6307 | app.debugDrag = isDragDebugEnabled(); |
| 6308 | app.debugPhysics = isPhysicsDebugEnabled(); |
| 6309 | var play = document.getElementById('btn-play'); |
| 6310 | play.addEventListener('click', onPlay); |
| 6311 | play.addEventListener('pointerup', function (e) |
| 6312 | { |
| 6313 | // A touch-friendly path: some mobile browsers swallow the click |
| 6314 | // after a slightly sloppy tap. Ignore the following click. |
| 6315 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 6316 | { |
| 6317 | e.preventDefault(); |
| 6318 | onPlay(); |
| 6319 | } |
| 6320 | }); |
| 6321 | |
| 6322 | game.preloadImagesThenStart(IMAGES, function () |
| 6323 | { |
| 6324 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 6325 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 6326 | }); |
| 6327 | |
| 6328 | bindOnlineUi(); |
| 6329 | bindOfflineUi(); |
| 6330 | bindPlanetSelect(); |
| 6331 | restoreSelectedPlanet(); |
| 6332 | }; |
| 6333 | |
| 6334 | function onPlay () |
| 6335 | { |
| 6336 | if (app.mode === 'playing') |
| 6337 | { return; } |
| 6338 | openPlanetSelect('solo'); |
| 6339 | } |
| 6340 | |
| 6341 | function startPlay (opts) |
| 6342 | { |
| 6343 | opts = opts || {}; |
| 6344 | hideEl('menu'); |
| 6345 | hideEl('game-over'); |
| 6346 | hideEl('online-panel'); |
| 6347 | hideEl('spectator'); |
| 6348 | hideEl('podium'); |
| 6349 | hideEl('planet-select'); |
| 6350 | hideEl('offline-result'); |
| 6351 | showEl('hud'); |
| 6352 | |
| 6353 | var planetMod = typeof Planet !== 'undefined' ? Planet : PlanetMod; |
| 6354 | app.planet = opts.planet || planetMod.planetById(selectedPlanetId()); |
| 6355 | var miss = opts.missChance; |
| 6356 | if (typeof miss !== 'number' || !isFinite(miss)) |
| 6357 | { miss = planetMod.missChance(app.planet); } |
| 6358 | app.core = new GameCore(app.planet, Math.random, miss); |
| 6359 | if (app.debugPhysics) |
| 6360 | { app.core.debugInvariants = true; } |
| 6361 | app.mode = 'playing'; |
| 6362 | app.online = !!opts.online; |
| 6363 | app.offline = !!opts.offline; |
| 6364 | app.gameOverShown = false; |
| 6365 | app.eliminatedReported = false; |
| 6366 | app.spectating = false; |
| 6367 | app.sprites = Object.create(null); |
| 6368 | app.flames = Object.create(null); |
| 6369 | app.cells = []; |
| 6370 | app.pointerId = null; |
| 6371 | app.selectTween = null; |
| 6372 | app.swapTweens = Object.create(null); |
| 6373 | app.columnWarn = []; |
| 6374 | app.warnClock = 0; |
| 6375 | app.miniAcc = 1; // draw minis on the first frame |
| 6376 | app.offlineResultShown = false; |
| 6377 | if (!opts.keepBattle) |
| 6378 | { |
| 6379 | app.roundWonDelay = 0; |
| 6380 | app.roundWonHandled = false; |
| 6381 | } |
| 6382 | if (app.offline && !opts.keepBattle && typeof Offline !== 'undefined') |
| 6383 | { |
| 6384 | app.battle = Offline.createBattle({ |
| 6385 | playerPlanet: app.planet, |
| 6386 | rng: Math.random, |
| 6387 | round: opts.round || 1 |
| 6388 | }); |
| 6389 | } |
| 6390 | |
| 6391 | layout(); |
| 6392 | if (app.scene) |
| 6393 | { |
| 6394 | app.scene.destroy(); |
| 6395 | app.scene = null; |
| 6396 | } |
| 6397 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 6398 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 6399 | app.scene.renderBackground = true; |
| 6400 | game.loadScene(app.scene); |
| 6401 | |
| 6402 | buildCells(); |
| 6403 | app.manager = app.scene.addObject(makeManager()); |
| 6404 | if (!app.spectating) |
| 6405 | { bindPointer(app.scene.canvas); } |
| 6406 | refreshHud(); |
| 6407 | if (app.online || app.offline) |
| 6408 | { showEl('minis'); } |
| 6409 | else |
| 6410 | { hideEl('minis'); } |
| 6411 | buildColumnWarn(); |
| 6412 | refreshOfflineHud(); |
| 6413 | refreshChainIndicator(); |
| 6414 | } |
| 6415 | |
| 6416 | function teardownPlay () |
| 6417 | { |
| 6418 | unbindPointer(); |
| 6419 | if (app.scene) |
| 6420 | { |
| 6421 | app.scene.destroy(); |
| 6422 | app.scene = null; |
| 6423 | } |
| 6424 | app.manager = null; |
| 6425 | app.core = null; |
| 6426 | app.sprites = Object.create(null); |
| 6427 | app.flames = Object.create(null); |
| 6428 | app.cells = []; |
| 6429 | app.selectTween = null; |
| 6430 | app.swapTweens = Object.create(null); |
| 6431 | app.columnWarn = []; |
| 6432 | app.pointerId = null; |
| 6433 | app.gameOverShown = false; |
| 6434 | hideEl('hud'); |
| 6435 | hideEl('game-over'); |
| 6436 | hideEl('spectator'); |
| 6437 | hideEl('minis'); |
| 6438 | hideEl('offline-result'); |
| 6439 | hideOfflineHud(); |
| 6440 | hideChainIndicator(); |
| 6441 | if (typeof Online !== 'undefined') |
| 6442 | { Online.clearFlights(); } |
| 6443 | if (!app._keepBattleOnTeardown) |
| 6444 | { |
| 6445 | app.battle = null; |
| 6446 | app.offline = false; |
| 6447 | } |
| 6448 | app._keepBattleOnTeardown = false; |
| 6449 | } |
| 6450 | |
| 6451 | function returnToMenu () |
| 6452 | { |
| 6453 | if (app.online && typeof Online !== 'undefined') |
| 6454 | { |
| 6455 | // Online matches do not dump you back to the solo menu: |
| 6456 | // elimination becomes spectating; a finished match shows the podium. |
| 6457 | return; |
| 6458 | } |
| 6459 | teardownPlay(); |
| 6460 | app.mode = 'menu'; |
| 6461 | app.online = false; |
| 6462 | app.offline = false; |
| 6463 | app.battle = null; |
| 6464 | hideEl('planet-select'); |
| 6465 | showEl('menu'); |
| 6466 | var last = document.getElementById('last-score'); |
| 6467 | if (app.lastScore > 0) |
| 6468 | { |
| 6469 | last.hidden = false; |
| 6470 | last.querySelector('strong').textContent = String(app.lastScore); |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | function buildCells () |
| 6475 | { |
| 6476 | if (!app.scene) |
| 6477 | { return; } |
| 6478 | var img = game.images.cell; |
| 6479 | for (var row = 0; row < 13; row++) |
| 6480 | { |
| 6481 | for (var col = 0; col < 10; col++) |
| 6482 | { |
| 6483 | var obj = new GameObject(0, 0, img, 0); |
| 6484 | placeTile(obj, col, row); |
| 6485 | obj.renderer.alpha = 0.9; |
| 6486 | app.scene.addObject(obj); |
| 6487 | app.cells.push(obj); |
| 6488 | } |
| 6489 | } |
| 6490 | } |
| 6491 | |
| 6492 | function placeTile (obj, col, y) |
| 6493 | { |
| 6494 | var t = app.tile; |
| 6495 | obj.transform.position.x = col * t + t / 2; |
| 6496 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 6497 | obj.transform.size = { x: t, y: t }; |
| 6498 | } |
| 6499 | |
| 6500 | function opponentCount () |
| 6501 | { |
| 6502 | if (app.offline && app.battle && app.battle.bots) |
| 6503 | { return app.battle.bots.length; } |
| 6504 | if (app.online && typeof Online !== 'undefined' && Online.opponents) |
| 6505 | { return Online.opponents().length; } |
| 6506 | return 0; |
| 6507 | } |
| 6508 | |
| 6509 | function currentMiniSize () |
| 6510 | { |
| 6511 | if (app.miniLayout && app.miniLayout.mini) |
| 6512 | { return { w: app.miniLayout.mini.w, h: app.miniLayout.mini.h }; } |
| 6513 | var n = opponentCount(); |
| 6514 | var mode = (app.miniLayout && app.miniLayout.mode) || 'top'; |
| 6515 | if (typeof MiniLayout !== 'undefined' && MiniLayout.canvasSize) |
| 6516 | { return MiniLayout.canvasSize(n, mode, 0); } |
| 6517 | if (n >= 10) |
| 6518 | { return { w: 40, h: 52 }; } |
| 6519 | if (n >= 6) |
| 6520 | { return { w: 56, h: 73 }; } |
| 6521 | return { w: 80, h: 104 }; |
| 6522 | } |
| 6523 | |
| 6524 | function applyMinisContainer (plan) |
| 6525 | { |
| 6526 | var minis = document.getElementById('minis'); |
| 6527 | if (!minis) |
| 6528 | { return; } |
| 6529 | var n = plan ? plan.n : 0; |
| 6530 | minis.classList.toggle('crowded', n > 5); |
| 6531 | if (!plan || n === 0 || plan.mode !== 'sides') |
| 6532 | { |
| 6533 | minis.classList.remove('sides'); |
| 6534 | minis.style.top = (app.hudH || 56) + 'px'; |
| 6535 | minis.style.maxHeight = (plan && plan.miniH ? plan.miniH : 0) + 'px'; |
| 6536 | minis.style.bottom = ''; |
| 6537 | minis.style.left = ''; |
| 6538 | minis.style.right = ''; |
| 6539 | var kids = minis.querySelectorAll('.mini'); |
| 6540 | for (var i = 0; i < kids.length; i++) |
| 6541 | { |
| 6542 | kids[i].style.left = ''; |
| 6543 | kids[i].style.top = ''; |
| 6544 | kids[i].style.width = ''; |
| 6545 | kids[i].style.position = ''; |
| 6546 | } |
| 6547 | return; |
| 6548 | } |
| 6549 | minis.classList.add('sides'); |
| 6550 | minis.style.top = '0'; |
| 6551 | minis.style.bottom = '0'; |
| 6552 | minis.style.left = '0'; |
| 6553 | minis.style.right = '0'; |
| 6554 | minis.style.maxHeight = 'none'; |
| 6555 | } |
| 6556 | |
| 6557 | function applyMinisLayout () |
| 6558 | { |
| 6559 | var plan = app.miniLayout; |
| 6560 | var minis = document.getElementById('minis'); |
| 6561 | if (!minis) |
| 6562 | { return; } |
| 6563 | applyMinisContainer(plan); |
| 6564 | if (!plan || plan.mode !== 'sides') |
| 6565 | { return; } |
| 6566 | var positions = plan.positions || []; |
| 6567 | var kids = minis.querySelectorAll('.mini'); |
| 6568 | for (var i = 0; i < positions.length && i < kids.length; i++) |
| 6569 | { |
| 6570 | var p = positions[i]; |
| 6571 | var el = kids[i]; |
| 6572 | el.style.position = 'absolute'; |
| 6573 | el.style.left = p.x + 'px'; |
| 6574 | el.style.top = p.y + 'px'; |
| 6575 | el.style.width = p.w + 'px'; |
| 6576 | var canvas = el.querySelector('canvas'); |
| 6577 | if (canvas) |
| 6578 | { |
| 6579 | canvas.style.width = p.canvasW + 'px'; |
| 6580 | canvas.style.height = p.canvasH + 'px'; |
| 6581 | } |
| 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | function applyFlightsBackClip () |
| 6586 | { |
| 6587 | var back = document.getElementById('flights-back'); |
| 6588 | if (!back) |
| 6589 | { return; } |
| 6590 | var x = app.fieldX || 0; |
| 6591 | var y = app.fieldY || 0; |
| 6592 | var w = app.fieldW || 0; |
| 6593 | var h = app.fieldH || 0; |
| 6594 | if (!w || !h) |
| 6595 | { |
| 6596 | back.style.clipPath = ''; |
| 6597 | return; |
| 6598 | } |
| 6599 | // Even-odd polygon: full viewport with a hole over the player's field, |
| 6600 | // so enemy-to-enemy arcs travel behind the field (t1125). |
| 6601 | back.style.clipPath = 'polygon(evenodd, 0 0, 100% 0, 100% 100%, 0 100%, 0 0, ' |
| 6602 | + x + 'px ' + y + 'px, ' + (x + w) + 'px ' + y + 'px, ' |
| 6603 | + (x + w) + 'px ' + (y + h) + 'px, ' + x + 'px ' + (y + h) + 'px, ' |
| 6604 | + x + 'px ' + y + 'px)'; |
| 6605 | } |
| 6606 | |
| 6607 | function layoutIfNeeded () |
| 6608 | { |
| 6609 | if (app.core && app.core.drag) |
| 6610 | { return; } |
| 6611 | var vw = window.innerWidth || 320; |
| 6612 | var vh = window.innerHeight || 480; |
| 6613 | var n = (app.online || app.offline) ? opponentCount() : 0; |
| 6614 | if (app._layoutVw === vw && app._layoutVh === vh && app._layoutN === n |
| 6615 | && app.scene && app.tile) |
| 6616 | { return; } |
| 6617 | layout(); |
| 6618 | } |
| 6619 | |
| 6620 | function layout () |
| 6621 | { |
| 6622 | if (app.core && app.core.drag) |
| 6623 | { return; } |
| 6624 | var vw = window.innerWidth || 320; |
| 6625 | var vh = window.innerHeight || 480; |
| 6626 | var n = (app.online || app.offline) ? opponentCount() : 0; |
| 6627 | app._layoutVw = vw; |
| 6628 | app._layoutVh = vh; |
| 6629 | app._layoutN = n; |
| 6630 | var safeTop = 0; |
| 6631 | try |
| 6632 | { |
| 6633 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 6634 | safeTop = 0; |
| 6635 | } |
| 6636 | catch (e) { /* ignore */ } |
| 6637 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 6638 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 6639 | var plan; |
| 6640 | if (typeof MiniLayout !== 'undefined' && MiniLayout.plan) |
| 6641 | { plan = MiniLayout.plan(vw, vh, n, app.hudH, margin); } |
| 6642 | else |
| 6643 | { |
| 6644 | plan = { |
| 6645 | mode: 'top', |
| 6646 | tile: 14, |
| 6647 | fieldW: 140, |
| 6648 | fieldH: 182, |
| 6649 | fieldX: Math.floor((vw - 140) / 2), |
| 6650 | fieldY: app.hudH, |
| 6651 | miniH: n > 0 ? 100 : 0, |
| 6652 | mini: { w: 80, h: 104 }, |
| 6653 | positions: [], |
| 6654 | n: n |
| 6655 | }; |
| 6656 | } |
| 6657 | app.miniLayout = plan; |
| 6658 | app.miniH = plan.miniH; |
| 6659 | app.tile = plan.tile; |
| 6660 | app.fieldW = plan.fieldW; |
| 6661 | app.fieldH = plan.fieldH; |
| 6662 | app.fieldX = plan.fieldX; |
| 6663 | app.fieldY = plan.fieldY; |
| 6664 | applyMinisLayout(); |
| 6665 | applyFlightsBackClip(); |
| 6666 | |
| 6667 | if (app.scene) |
| 6668 | { |
| 6669 | app.scene.position.x = app.fieldX; |
| 6670 | app.scene.position.y = app.fieldY; |
| 6671 | app.scene.size.x = app.fieldW; |
| 6672 | app.scene.size.y = app.fieldH; |
| 6673 | } |
| 6674 | } |
| 6675 | |
| 6676 | function spriteFor (block) |
| 6677 | { |
| 6678 | if (block.state === 'locked') |
| 6679 | { return game.images.locked; } |
| 6680 | if (block.color < 0) |
| 6681 | { return game.images.gray; } |
| 6682 | var key = Planet.spriteKey(app.planet, block.color); |
| 6683 | return game.images[key] || game.images.gray; |
| 6684 | } |
| 6685 | |
| 6686 | function syncSprites (dt) |
| 6687 | { |
| 6688 | if (!app.core || !app.scene) |
| 6689 | { return; } |
| 6690 | dt = dt || 0; |
| 6691 | consumeAndStartSwapTweens(); |
| 6692 | advanceSwapTweens(dt); |
| 6693 | var seen = Object.create(null); |
| 6694 | var blocks = app.core.blocks; |
| 6695 | for (var i = 0; i < blocks.length; i++) |
| 6696 | { |
| 6697 | var block = blocks[i]; |
| 6698 | seen[block.id] = true; |
| 6699 | var obj = app.sprites[block.id]; |
| 6700 | if (!obj) |
| 6701 | { |
| 6702 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 6703 | app.scene.addObject(obj); |
| 6704 | app.sprites[block.id] = obj; |
| 6705 | } |
| 6706 | obj.renderer.sprite = spriteFor(block); |
| 6707 | placeTile(obj, block.col, blockVisualY(block)); |
| 6708 | applyBlockVisual(obj, block); |
| 6709 | obj.renderer.visible = true; |
| 6710 | obj.renderer.alpha = 1; |
| 6711 | if (typeof Online !== 'undefined' && Online.hasExitVisual && Online.hasExitVisual(block.id)) |
| 6712 | { |
| 6713 | obj.renderer.visible = false; |
| 6714 | obj.renderer.alpha = 0; |
| 6715 | } |
| 6716 | |
| 6717 | if (block.flame) |
| 6718 | { |
| 6719 | var flame = app.flames[block.id]; |
| 6720 | if (!flame) |
| 6721 | { |
| 6722 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 6723 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 6724 | app.scene.addObject(flame); |
| 6725 | app.flames[block.id] = flame; |
| 6726 | } |
| 6727 | flame.renderer.visible = true; |
| 6728 | var t = app.tile; |
| 6729 | // 3x as large (length and width, t1110). Keep the top of the |
| 6730 | // flame at the same overlap into the block so the extra length |
| 6731 | // extends downward under the rocket. |
| 6732 | var flameW = t * 0.85 * 3; |
| 6733 | var flameH = t * 0.85 * 3; |
| 6734 | var origCenter = app.fieldH - block.y * t + t * 0.15; |
| 6735 | var origTop = origCenter - (t * 0.85) / 2; |
| 6736 | flame.transform.position.x = block.col * t + t / 2; |
| 6737 | flame.transform.position.y = origTop + flameH / 2; |
| 6738 | flame.transform.size = { x: flameW, y: flameH }; |
| 6739 | } |
| 6740 | else if (app.flames[block.id]) |
| 6741 | { app.flames[block.id].renderer.visible = false; } |
| 6742 | if (block.flame && app.flames[block.id] |
| 6743 | && typeof Online !== 'undefined' && Online.hasExitVisual |
| 6744 | && Online.hasExitVisual(block.id)) |
| 6745 | { app.flames[block.id].renderer.visible = false; } |
| 6746 | } |
| 6747 | |
| 6748 | var ids = Object.keys(app.sprites); |
| 6749 | for (var i = 0; i < ids.length; i++) |
| 6750 | { |
| 6751 | if (!seen[ids[i]]) |
| 6752 | { |
| 6753 | app.sprites[ids[i]].destroy(); |
| 6754 | delete app.sprites[ids[i]]; |
| 6755 | if (app.flames[ids[i]]) |
| 6756 | { |
| 6757 | app.flames[ids[i]].destroy(); |
| 6758 | delete app.flames[ids[i]]; |
| 6759 | } |
| 6760 | } |
| 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | function applyBlockVisual (obj, block) |
| 6765 | { |
| 6766 | var t = app.tile; |
| 6767 | var sx = 1; |
| 6768 | var sy = 1; |
| 6769 | var dy = 0; |
| 6770 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 6771 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 6772 | { |
| 6773 | var u = block.bounceAge / app.planet.bounceDuration; |
| 6774 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 6775 | sx = pose.sx; |
| 6776 | sy = pose.sy; |
| 6777 | dy = pose.dy; |
| 6778 | } |
| 6779 | var sel = 1; |
| 6780 | var tw = app.selectTween; |
| 6781 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 6782 | { sel = tw.scale; } |
| 6783 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 6784 | if (dy) |
| 6785 | { obj.transform.position.y -= dy * t; } |
| 6786 | } |
| 6787 | |
| 6788 | function clickTween () |
| 6789 | { |
| 6790 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 6791 | } |
| 6792 | |
| 6793 | function startPressTween (blockId) |
| 6794 | { |
| 6795 | var c = clickTween(); |
| 6796 | if (!c) |
| 6797 | { return; } |
| 6798 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 6799 | app.selectTween = { |
| 6800 | blockId: blockId, |
| 6801 | from: from, |
| 6802 | to: c.pressScale, |
| 6803 | t: 0, |
| 6804 | duration: c.pressDuration, |
| 6805 | ease: c.pressEase, |
| 6806 | scale: from, |
| 6807 | phase: 'press' |
| 6808 | }; |
| 6809 | } |
| 6810 | |
| 6811 | function startReleaseTween () |
| 6812 | { |
| 6813 | var c = clickTween(); |
| 6814 | var tw = app.selectTween; |
| 6815 | if (!c || !tw || tw.phase === 'idle') |
| 6816 | { return; } |
| 6817 | app.selectTween = { |
| 6818 | blockId: tw.blockId, |
| 6819 | from: tw.scale, |
| 6820 | to: c.releaseScale, |
| 6821 | t: 0, |
| 6822 | duration: c.releaseDuration, |
| 6823 | ease: c.releaseEase, |
| 6824 | scale: tw.scale, |
| 6825 | phase: 'release' |
| 6826 | }; |
| 6827 | } |
| 6828 | |
| 6829 | function updateSelectTween (dt) |
| 6830 | { |
| 6831 | var tw = app.selectTween; |
| 6832 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 6833 | { return; } |
| 6834 | tw.t += dt; |
| 6835 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 6836 | if (u >= 1) |
| 6837 | { |
| 6838 | tw.scale = tw.to; |
| 6839 | tw.t = tw.duration; |
| 6840 | if (tw.phase === 'press') |
| 6841 | { tw.phase = 'hold'; } |
| 6842 | else |
| 6843 | { |
| 6844 | tw.phase = 'idle'; |
| 6845 | tw.scale = 1; |
| 6846 | tw.blockId = null; |
| 6847 | } |
| 6848 | return; |
| 6849 | } |
| 6850 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 6851 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 6852 | } |
| 6853 | |
| 6854 | |
| 6855 | /* ---------- visual-only swap tween (t1121) -------------------------- */ |
| 6856 | |
| 6857 | function swapTweenSpec () |
| 6858 | { |
| 6859 | if (typeof Easing !== 'undefined' && Easing.SWAP_TWEEN) |
| 6860 | { return Easing.SWAP_TWEEN; } |
| 6861 | return { duration: 0.100, ease: null }; |
| 6862 | } |
| 6863 | |
| 6864 | function swapEase (u) |
| 6865 | { |
| 6866 | if (typeof Easing !== 'undefined' && Easing.easeOutCubic) |
| 6867 | { return Easing.easeOutCubic(u); } |
| 6868 | var v = 1 - Math.max(0, Math.min(1, u)); |
| 6869 | return 1 - v * v * v; |
| 6870 | } |
| 6871 | |
| 6872 | function tweenVisualY (tw) |
| 6873 | { |
| 6874 | var u = tw.duration > 0 ? Math.min(1, tw.age / tw.duration) : 1; |
| 6875 | return tw.fromY + (tw.toY - tw.fromY) * swapEase(u); |
| 6876 | } |
| 6877 | |
| 6878 | /** |
| 6879 | * Consume GameCore swap events. The currently dragged block is skipped |
| 6880 | * (it follows the pointer). A block that is mid-tween and is swapped |
| 6881 | * again restarts from its current visual y, so rapid swaps chain |
| 6882 | * smoothly without snapping. |
| 6883 | */ |
| 6884 | function consumeAndStartSwapTweens () |
| 6885 | { |
| 6886 | if (!app.core || !app.core.consumeSwapEvents) |
| 6887 | { return; } |
| 6888 | if (!app.swapTweens) |
| 6889 | { app.swapTweens = Object.create(null); } |
| 6890 | var ev = app.core.consumeSwapEvents(); |
| 6891 | if (!ev.length) |
| 6892 | { return; } |
| 6893 | var dragId = app.core.drag && app.core.drag.blockId; |
| 6894 | var duration = swapTweenSpec().duration; |
| 6895 | for (var i = 0; i < ev.length; i++) |
| 6896 | { |
| 6897 | var e = ev[i]; |
| 6898 | if (e.id === dragId) |
| 6899 | { |
| 6900 | delete app.swapTweens[e.id]; |
| 6901 | continue; |
| 6902 | } |
| 6903 | var fromY = e.fromY; |
| 6904 | var existing = app.swapTweens[e.id]; |
| 6905 | if (existing) |
| 6906 | { fromY = tweenVisualY(existing); } |
| 6907 | app.swapTweens[e.id] = { |
| 6908 | fromY: fromY, |
| 6909 | toY: e.toY, |
| 6910 | age: 0, |
| 6911 | duration: duration |
| 6912 | }; |
| 6913 | } |
| 6914 | } |
| 6915 | |
| 6916 | function advanceSwapTweens (dt) |
| 6917 | { |
| 6918 | if (!app.swapTweens) |
| 6919 | { return; } |
| 6920 | var ids = Object.keys(app.swapTweens); |
| 6921 | for (var i = 0; i < ids.length; i++) |
| 6922 | { |
| 6923 | var tw = app.swapTweens[ids[i]]; |
| 6924 | tw.age += dt; |
| 6925 | if (tw.age >= tw.duration) |
| 6926 | { delete app.swapTweens[ids[i]]; } |
| 6927 | } |
| 6928 | } |
| 6929 | |
| 6930 | function blockVisualY (block) |
| 6931 | { |
| 6932 | if (!app.swapTweens) |
| 6933 | { return block.y; } |
| 6934 | var dragId = app.core && app.core.drag && app.core.drag.blockId; |
| 6935 | if (block.id === dragId) |
| 6936 | { return block.y; } |
| 6937 | var tw = app.swapTweens[block.id]; |
| 6938 | if (!tw) |
| 6939 | { return block.y; } |
| 6940 | return tweenVisualY(tw); |
| 6941 | } |
| 6942 | |
| 6943 | function refreshHud () |
| 6944 | { |
| 6945 | if (!app.core) |
| 6946 | { return; } |
| 6947 | document.getElementById('score').textContent = String(app.core.score); |
| 6948 | var planet = app.planet; |
| 6949 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 6950 | document.getElementById('planet-stats').textContent = Planet.formatStats(planet); |
| 6951 | } |
| 6952 | |
| 6953 | function makeManager () |
| 6954 | { |
| 6955 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 6956 | obj.renderer.visible = false; |
| 6957 | var base = obj.update.bind(obj); |
| 6958 | obj.update = function (dt) |
| 6959 | { |
| 6960 | base(dt); |
| 6961 | tickView(dt); |
| 6962 | }; |
| 6963 | return obj; |
| 6964 | } |
| 6965 | |
| 6966 | function tickView (dt) |
| 6967 | { |
| 6968 | if (app.mode !== 'playing' || !app.core) |
| 6969 | { return; } |
| 6970 | |
| 6971 | // Never resize the canvas mid-drag: a URL-bar hide would move the |
| 6972 | // field under the finger and jump the target row. Also skip work |
| 6973 | // when the viewport has not actually changed. |
| 6974 | if (!(app.core && app.core.drag)) |
| 6975 | { layoutIfNeeded(); } |
| 6976 | for (var i = 0; i < app.cells.length; i++) |
| 6977 | { |
| 6978 | var col = i % 10; |
| 6979 | var row = Math.floor(i / 10); |
| 6980 | placeTile(app.cells[i], col, row); |
| 6981 | } |
| 6982 | |
| 6983 | app.core.tick(dt); |
| 6984 | if (app.offline && app.battle) |
| 6985 | { tickOffline(dt); } |
| 6986 | updateSelectTween(dt); |
| 6987 | syncSprites(dt); |
| 6988 | updateColumnWarn(dt); |
| 6989 | refreshHud(); |
| 6990 | refreshOfflineHud(); |
| 6991 | refreshChainIndicator(); |
| 6992 | tickOnline(dt); |
| 6993 | if (app.debugPhysics) |
| 6994 | { logPhysicsDebug(); } |
| 6995 | |
| 6996 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 6997 | { |
| 6998 | app.gameOverShown = true; |
| 6999 | app.lastScore = app.core.score; |
| 7000 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 7001 | if (app.online) |
| 7002 | { onOnlineEliminated(); } |
| 7003 | else if (app.offline) |
| 7004 | { /* Offline result is shown from tickOffline. */ } |
| 7005 | else |
| 7006 | { showEl('game-over'); } |
| 7007 | } |
| 7008 | if (!app.online && !app.offline && app.core.state === 'gameover' |
| 7009 | && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 7010 | { returnToMenu(); } |
| 7011 | } |
| 7012 | |
| 7013 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 7014 | /* |
| 7015 | * Mouse uses Pointer Events. Touch uses Touch Events as the authority: |
| 7016 | * on a real phone, pointer clientX/Y can disagree with the visual |
| 7017 | * finger (zoomed visual viewport, gesture takeover, capture cancel) |
| 7018 | * while changedTouches stays correct. When a touch is active, pointer |
| 7019 | * move/up/cancel are ignored so a poisoned pointer coordinate cannot |
| 7020 | * walk the block to the top or bottom of the stack (t1106). |
| 7021 | * |
| 7022 | * After a down we also listen on the document, so a drag that leaves |
| 7023 | * the canvas (needed to launch a block at the top of a full column) |
| 7024 | * still reaches moveDrag. We do not call setPointerCapture: on mobile |
| 7025 | * that is often cancelled when the browser takes the gesture, which |
| 7026 | * would fire pointercancel mid-drag. |
| 7027 | * |
| 7028 | * A non-passive document touchstart/touchmove preventDefault during |
| 7029 | * play stops scrolling, rubber-banding and the pointercancel that |
| 7030 | * follows. pointercancel / touchcancel end the drag without launch. |
| 7031 | * |
| 7032 | * ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay. |
| 7033 | */ |
| 7034 | |
| 7035 | var boundCanvas = null; |
| 7036 | var windowListening = false; |
| 7037 | var touchListening = false; |
| 7038 | var lastDragDebug = null; |
| 7039 | |
| 7040 | function isDragDebugEnabled () |
| 7041 | { |
| 7042 | try |
| 7043 | { |
| 7044 | var q = window.location && window.location.search; |
| 7045 | if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q)) |
| 7046 | { return true; } |
| 7047 | if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1') |
| 7048 | { return true; } |
| 7049 | } |
| 7050 | catch (err) { /* ignore */ } |
| 7051 | return false; |
| 7052 | } |
| 7053 | |
| 7054 | /** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */ |
| 7055 | function isPhysicsDebugEnabled () |
| 7056 | { |
| 7057 | try |
| 7058 | { |
| 7059 | var q = window.location && window.location.search; |
| 7060 | if (q && /(?:\?|&)debug(?:=|&|$)/.test(q)) |
| 7061 | { return true; } |
| 7062 | if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1' |
| 7063 | || window.localStorage.getItem('meteors-debug-drag') === '1')) |
| 7064 | { return true; } |
| 7065 | } |
| 7066 | catch (err) { /* ignore */ } |
| 7067 | return false; |
| 7068 | } |
| 7069 | |
| 7070 | function eventClient (e) |
| 7071 | { |
| 7072 | var t = null; |
| 7073 | if (e.changedTouches && e.changedTouches.length) |
| 7074 | { t = e.changedTouches[0]; } |
| 7075 | else if (e.touches && e.touches.length) |
| 7076 | { t = e.touches[0]; } |
| 7077 | if (t) |
| 7078 | { return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; } |
| 7079 | if (e.clientX != null && e.clientY != null) |
| 7080 | { return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; } |
| 7081 | return null; |
| 7082 | } |
| 7083 | |
| 7084 | function addWindowPointer () |
| 7085 | { |
| 7086 | if (windowListening) |
| 7087 | { return; } |
| 7088 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 7089 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 7090 | window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false }); |
| 7091 | windowListening = true; |
| 7092 | } |
| 7093 | |
| 7094 | function removeWindowPointer () |
| 7095 | { |
| 7096 | if (!windowListening) |
| 7097 | { return; } |
| 7098 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 7099 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 7100 | window.removeEventListener('pointercancel', onPointerCancel, { capture: true }); |
| 7101 | windowListening = false; |
| 7102 | } |
| 7103 | |
| 7104 | function addTouchListeners () |
| 7105 | { |
| 7106 | if (touchListening) |
| 7107 | { return; } |
| 7108 | var opts = { capture: true, passive: false }; |
| 7109 | document.addEventListener('touchstart', onTouchStart, opts); |
| 7110 | document.addEventListener('touchmove', onTouchMove, opts); |
| 7111 | document.addEventListener('touchend', onTouchEnd, opts); |
| 7112 | document.addEventListener('touchcancel', onTouchCancel, opts); |
| 7113 | touchListening = true; |
| 7114 | } |
| 7115 | |
| 7116 | function removeTouchListeners () |
| 7117 | { |
| 7118 | if (!touchListening) |
| 7119 | { return; } |
| 7120 | var opts = { capture: true }; |
| 7121 | document.removeEventListener('touchstart', onTouchStart, opts); |
| 7122 | document.removeEventListener('touchmove', onTouchMove, opts); |
| 7123 | document.removeEventListener('touchend', onTouchEnd, opts); |
| 7124 | document.removeEventListener('touchcancel', onTouchCancel, opts); |
| 7125 | touchListening = false; |
| 7126 | } |
| 7127 | |
| 7128 | function bindPointer (canvas) |
| 7129 | { |
| 7130 | unbindPointer(); |
| 7131 | boundCanvas = canvas; |
| 7132 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 7133 | canvas.style.touchAction = 'none'; |
| 7134 | canvas.style.position = 'fixed'; |
| 7135 | canvas.style.cursor = 'pointer'; |
| 7136 | canvas.style.webkitUserSelect = 'none'; |
| 7137 | canvas.style.userSelect = 'none'; |
| 7138 | addTouchListeners(); |
| 7139 | if (app.debugDrag) |
| 7140 | { ensureDragDebugOverlay(); } |
| 7141 | } |
| 7142 | |
| 7143 | function unbindPointer () |
| 7144 | { |
| 7145 | removeWindowPointer(); |
| 7146 | removeTouchListeners(); |
| 7147 | if (boundCanvas) |
| 7148 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 7149 | boundCanvas = null; |
| 7150 | app.pointerId = null; |
| 7151 | app.pointerType = null; |
| 7152 | app.activeTouchId = null; |
| 7153 | } |
| 7154 | |
| 7155 | function fieldFromEvent (e) |
| 7156 | { |
| 7157 | var canvas = app.scene && app.scene.canvas; |
| 7158 | if (!canvas) |
| 7159 | { return null; } |
| 7160 | var rect = canvas.getBoundingClientRect(); |
| 7161 | if (rect.width <= 0 || rect.height <= 0) |
| 7162 | { return null; } |
| 7163 | var pt = eventClient(e); |
| 7164 | if (!pt) |
| 7165 | { return null; } |
| 7166 | // Map through the CSS box only. Using canvas.width / app.tile would |
| 7167 | // scale the pointer by the backing-store ratio whenever that differs |
| 7168 | // from the layout size (high-DPR devices), so a short finger move |
| 7169 | // would look like a long swipe. |
| 7170 | var width = Planet.FIELD_WIDTH; |
| 7171 | var height = Planet.FIELD_HEIGHT; |
| 7172 | var col = ((pt.x - rect.left) / rect.width) * width; |
| 7173 | var fieldY = (1 - (pt.y - rect.top) / rect.height) * height; |
| 7174 | return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect }; |
| 7175 | } |
| 7176 | |
| 7177 | function playingForDrag () |
| 7178 | { |
| 7179 | return app.core && app.core.state === 'playing'; |
| 7180 | } |
| 7181 | |
| 7182 | function startDragFromEvent (e) |
| 7183 | { |
| 7184 | var pos = fieldFromEvent(e); |
| 7185 | if (!pos) |
| 7186 | { return; } |
| 7187 | if (app.core.drag) |
| 7188 | { |
| 7189 | app.core.moveDrag(pos.y); |
| 7190 | logDragDebug(e, pos, 'move-already'); |
| 7191 | return; |
| 7192 | } |
| 7193 | if (app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 7194 | { startPressTween(app.core.drag.blockId); } |
| 7195 | logDragDebug(e, pos, 'down'); |
| 7196 | } |
| 7197 | |
| 7198 | function moveDragFromEvent (e) |
| 7199 | { |
| 7200 | if (!app.core || !app.core.drag) |
| 7201 | { return; } |
| 7202 | var pos = fieldFromEvent(e); |
| 7203 | if (pos) |
| 7204 | { app.core.moveDrag(pos.y); } |
| 7205 | logDragDebug(e, pos, 'move'); |
| 7206 | } |
| 7207 | |
| 7208 | function finishDrag (opts) |
| 7209 | { |
| 7210 | opts = opts || {}; |
| 7211 | app.pointerId = null; |
| 7212 | app.pointerType = null; |
| 7213 | app.activeTouchId = null; |
| 7214 | removeWindowPointer(); |
| 7215 | if (boundCanvas) |
| 7216 | { |
| 7217 | try |
| 7218 | { |
| 7219 | if (opts.pointerId != null && boundCanvas.releasePointerCapture) |
| 7220 | { boundCanvas.releasePointerCapture(opts.pointerId); } |
| 7221 | } |
| 7222 | catch (err) { /* ignore */ } |
| 7223 | } |
| 7224 | startReleaseTween(); |
| 7225 | if (app.core && app.core.drag) |
| 7226 | { |
| 7227 | if (opts.noLaunch) |
| 7228 | { app.core.drag.launchIntent = false; } |
| 7229 | app.core.endDrag(); |
| 7230 | } |
| 7231 | else if (app.core) |
| 7232 | { app.core.endDrag(); } |
| 7233 | logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up'); |
| 7234 | } |
| 7235 | |
| 7236 | function onPointerDown (e) |
| 7237 | { |
| 7238 | if (!playingForDrag()) |
| 7239 | { return; } |
| 7240 | if (app.pointerId != null && e.pointerId !== app.pointerId) |
| 7241 | { return; } |
| 7242 | if (e.preventDefault) |
| 7243 | { e.preventDefault(); } |
| 7244 | app.pointerId = e.pointerId; |
| 7245 | app.pointerType = e.pointerType || 'mouse'; |
| 7246 | addWindowPointer(); |
| 7247 | // If a touch is already in charge, do not reset the grab from pointerdown. |
| 7248 | if (app.activeTouchId != null) |
| 7249 | { return; } |
| 7250 | startDragFromEvent(e); |
| 7251 | } |
| 7252 | |
| 7253 | function onPointerMove (e) |
| 7254 | { |
| 7255 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 7256 | { return; } |
| 7257 | if (!app.core || !app.core.drag) |
| 7258 | { return; } |
| 7259 | // Touch coordinates are the authority; ignore pointer moves for a |
| 7260 | // live touch so a wrong clientY cannot walk the whole column. |
| 7261 | // Only skip when touchstart has actually arrived — some environments |
| 7262 | // deliver touch-pointer events without a TouchEvent stream. |
| 7263 | if (app.activeTouchId != null) |
| 7264 | { |
| 7265 | if (e.preventDefault) |
| 7266 | { e.preventDefault(); } |
| 7267 | logDragDebug(e, fieldFromEvent(e), 'pointer-ignored'); |
| 7268 | return; |
| 7269 | } |
| 7270 | if (e.preventDefault) |
| 7271 | { e.preventDefault(); } |
| 7272 | moveDragFromEvent(e); |
| 7273 | } |
| 7274 | |
| 7275 | function onPointerUp (e) |
| 7276 | { |
| 7277 | if (app.pointerId == null) |
| 7278 | { return; } |
| 7279 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 7280 | { return; } |
| 7281 | if (app.activeTouchId != null) |
| 7282 | { |
| 7283 | // touchend will finish the drag. |
| 7284 | if (e && e.preventDefault) |
| 7285 | { e.preventDefault(); } |
| 7286 | logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored'); |
| 7287 | return; |
| 7288 | } |
| 7289 | if (e && e.preventDefault) |
| 7290 | { e.preventDefault(); } |
| 7291 | finishDrag({ pointerId: e && e.pointerId }); |
| 7292 | } |
| 7293 | |
| 7294 | function onPointerCancel (e) |
| 7295 | { |
| 7296 | if (app.pointerId == null) |
| 7297 | { return; } |
| 7298 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 7299 | { return; } |
| 7300 | if (app.activeTouchId != null) |
| 7301 | { |
| 7302 | if (e && e.preventDefault) |
| 7303 | { e.preventDefault(); } |
| 7304 | logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored'); |
| 7305 | return; |
| 7306 | } |
| 7307 | if (e && e.preventDefault) |
| 7308 | { e.preventDefault(); } |
| 7309 | // A cancelled gesture is not a launch pull. |
| 7310 | finishDrag({ pointerId: e && e.pointerId, noLaunch: true }); |
| 7311 | } |
| 7312 | |
| 7313 | function touchById (list, id) |
| 7314 | { |
| 7315 | if (!list) |
| 7316 | { return null; } |
| 7317 | for (var i = 0; i < list.length; i++) |
| 7318 | { |
| 7319 | if (list[i].identifier === id) |
| 7320 | { return list[i]; } |
| 7321 | } |
| 7322 | return null; |
| 7323 | } |
| 7324 | |
| 7325 | function onTouchStart (e) |
| 7326 | { |
| 7327 | if (!playingForDrag()) |
| 7328 | { return; } |
| 7329 | if (app.activeTouchId != null) |
| 7330 | { |
| 7331 | if (e.preventDefault) |
| 7332 | { e.preventDefault(); } |
| 7333 | return; |
| 7334 | } |
| 7335 | var t = e.changedTouches && e.changedTouches[0]; |
| 7336 | if (!t) |
| 7337 | { return; } |
| 7338 | if (e.preventDefault) |
| 7339 | { e.preventDefault(); } |
| 7340 | app.activeTouchId = t.identifier; |
| 7341 | addWindowPointer(); |
| 7342 | startDragFromEvent(e); |
| 7343 | } |
| 7344 | |
| 7345 | function onTouchMove (e) |
| 7346 | { |
| 7347 | if (app.activeTouchId == null) |
| 7348 | { return; } |
| 7349 | if (!touchById(e.changedTouches, app.activeTouchId) |
| 7350 | && !touchById(e.touches, app.activeTouchId)) |
| 7351 | { return; } |
| 7352 | if (e.preventDefault) |
| 7353 | { e.preventDefault(); } |
| 7354 | moveDragFromEvent(e); |
| 7355 | } |
| 7356 | |
| 7357 | function onTouchEnd (e) |
| 7358 | { |
| 7359 | if (app.activeTouchId == null) |
| 7360 | { return; } |
| 7361 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 7362 | { return; } |
| 7363 | if (e.preventDefault) |
| 7364 | { e.preventDefault(); } |
| 7365 | finishDrag({}); |
| 7366 | } |
| 7367 | |
| 7368 | function onTouchCancel (e) |
| 7369 | { |
| 7370 | if (app.activeTouchId == null) |
| 7371 | { return; } |
| 7372 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 7373 | { return; } |
| 7374 | if (e.preventDefault) |
| 7375 | { e.preventDefault(); } |
| 7376 | finishDrag({ noLaunch: true }); |
| 7377 | } |
| 7378 | |
| 7379 | function logPhysicsDebug () |
| 7380 | { |
| 7381 | if (!app.debugPhysics || !app.core) |
| 7382 | { return; } |
| 7383 | ensureDragDebugOverlay(); |
| 7384 | var el = document.getElementById('drag-debug'); |
| 7385 | if (!el) |
| 7386 | { return; } |
| 7387 | var issues = app.core.lastInvariantViolations || []; |
| 7388 | var flying = 0; |
| 7389 | var manuals = 0; |
| 7390 | var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0; |
| 7391 | for (var i = 0; i < app.core.blocks.length; i++) |
| 7392 | { |
| 7393 | if (app.core.blocks[i].state === 'flying') |
| 7394 | { flying++; } |
| 7395 | if (app.core.blocks[i].state === 'manual') |
| 7396 | { manuals++; } |
| 7397 | } |
| 7398 | var status = issues.length |
| 7399 | ? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n')) |
| 7400 | : ('physics ok blocks=' + app.core.blocks.length |
| 7401 | + ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters); |
| 7402 | // Keep any live drag line above the physics status. |
| 7403 | var dragLine = ''; |
| 7404 | if (app.debugDrag && lastDragDebug) |
| 7405 | { |
| 7406 | dragLine = el.textContent && el.textContent.indexOf('drag ') === 0 |
| 7407 | ? el.textContent.split('\n')[0] + '\n' |
| 7408 | : ''; |
| 7409 | } |
| 7410 | el.textContent = dragLine + status; |
| 7411 | el.hidden = false; |
| 7412 | el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8'; |
| 7413 | } |
| 7414 | |
| 7415 | function ensureDragDebugOverlay () |
| 7416 | { |
| 7417 | if (document.getElementById('drag-debug')) |
| 7418 | { return; } |
| 7419 | var el = document.createElement('div'); |
| 7420 | el.id = 'drag-debug'; |
| 7421 | el.setAttribute('aria-live', 'polite'); |
| 7422 | document.body.appendChild(el); |
| 7423 | } |
| 7424 | |
| 7425 | function logDragDebug (e, pos, phase) |
| 7426 | { |
| 7427 | if (!app.debugDrag) |
| 7428 | { return; } |
| 7429 | ensureDragDebugOverlay(); |
| 7430 | var el = document.getElementById('drag-debug'); |
| 7431 | if (!el) |
| 7432 | { return; } |
| 7433 | var block = null; |
| 7434 | if (app.core && app.core.drag) |
| 7435 | { block = app.core.blockById(app.core.drag.blockId); } |
| 7436 | var pt = e ? eventClient(e) : null; |
| 7437 | var evType = e ? e.type : phase; |
| 7438 | var info = { |
| 7439 | phase: phase, |
| 7440 | ev: evType, |
| 7441 | src: pos && pos.src, |
| 7442 | fieldY: pos && Math.round(pos.y * 100) / 100, |
| 7443 | col: pos && Math.round(pos.col * 100) / 100, |
| 7444 | cy: pt && Math.round(pt.y * 10) / 10, |
| 7445 | blockY: block && Math.round(block.y * 100) / 100, |
| 7446 | target: block && Math.floor((pos && pos.y) + 1e-12), |
| 7447 | launch: app.core && app.core.drag && app.core.drag.launchIntent, |
| 7448 | touchId: app.activeTouchId, |
| 7449 | pid: app.pointerId, |
| 7450 | ptype: app.pointerType |
| 7451 | }; |
| 7452 | lastDragDebug = info; |
| 7453 | el.textContent = 'drag ' + phase |
| 7454 | + ' ev=' + evType |
| 7455 | + ' src=' + (info.src || '-') |
| 7456 | + ' y=' + (info.fieldY != null ? info.fieldY : '-') |
| 7457 | + ' → ' + (info.target != null ? info.target : '-') |
| 7458 | + ' block=' + (info.blockY != null ? info.blockY : '-') |
| 7459 | + (info.launch ? ' LAUNCH' : '') |
| 7460 | + ' tId=' + info.touchId |
| 7461 | + ' pId=' + info.pid; |
| 7462 | el.hidden = false; |
| 7463 | } |
| 7464 | |
| 7465 | |
| 7466 | function hideEl (id) |
| 7467 | { |
| 7468 | document.getElementById(id).hidden = true; |
| 7469 | } |
| 7470 | |
| 7471 | function showEl (id) |
| 7472 | { |
| 7473 | document.getElementById(id).hidden = false; |
| 7474 | } |
| 7475 | |
| 7476 | window.addEventListener('resize', function () |
| 7477 | { |
| 7478 | if (app.mode === 'playing') |
| 7479 | { layout(); } |
| 7480 | }); |
| 7481 | |
| 7482 | window.addEventListener('orientationchange', function () |
| 7483 | { |
| 7484 | setTimeout(function () |
| 7485 | { |
| 7486 | if (app.mode === 'playing') |
| 7487 | { layout(); } |
| 7488 | }, 120); |
| 7489 | }); |
| 7490 | |
| 7491 | /* ---------- online play (stage 2) ---------------------------------- */ |
| 7492 | |
| 7493 | function tickOnline (dt) |
| 7494 | { |
| 7495 | if (!app.online || typeof Online === 'undefined') |
| 7496 | { return; } |
| 7497 | if (app.core) |
| 7498 | { |
| 7499 | var shots = app.core.consumeOutgoingShots(); |
| 7500 | for (var i = 0; i < shots.length; i++) |
| 7501 | { |
| 7502 | var s = shots[i]; |
| 7503 | var start = fieldToViewport(s.col, s.y); |
| 7504 | Online.rememberFlightLocal(s, start.x, start.y); |
| 7505 | } |
| 7506 | Online.queueShots(shots); |
| 7507 | } |
| 7508 | Online.tickFlights(); |
| 7509 | Online.renderMinis(document.getElementById('minis')); |
| 7510 | applyMinisLayout(); |
| 7511 | } |
| 7512 | |
| 7513 | |
| 7514 | /* ---------- Offline Strijd (t1 |
| 7515 | … afgekapt |
projects-timfalken-com meteors