BlueGit

projects-timfalken-com meteors

Files

meteors / src / gameCore.js

gameCore.js

gameCore.js 57.0 KiB Download Raw
1'use strict';
2
3/**
4 * Deterministic Meteors game core. No DOM, no rendering, no input devices.
5 * The view layer reads this state and translates pointer events into
6 * beginDrag / moveDrag / endDrag.
7 */
8
9var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined')
10 ? require('./planet.js')
11 : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet);
12
13var COLOR_GRAY = -1;
14
15var STATE_STILL = 'still';
16var STATE_FALLING = 'falling';
17var STATE_FLYING = 'flying';
18var STATE_SETTLING = 'settling';
19var STATE_LOCKED = 'locked';
20var STATE_MANUAL = 'manual';
21
22var ALIGN_EPS = 0.15;
23var SUBSTEP = 1 / 120;
24var DRAG_LAUNCH_SLACK = 0.65;
25// Upward pull (tiles past the press point) that launches an already-topmost block.
26var LAUNCH_PULL = 0.35;
27// Minimal accumulated vertical pointer travel (tiles) to swap one step.
28// No hold delay: the first swap fires as soon as this distance is reached.
29var SWAP_DISTANCE = 0.25;
30// Fallback snap (tiles) when the press cell is empty: nearest block in the column.
31var SNAP_Y = 0.5;
32// Horizontal slop (tiles) for snapping to a neighbouring column on press.
33var HORIZONTAL_SLOP = 0.35;
34
35function UnionFind ()
36{
37 this.parent = Object.create(null);
38}
39
40UnionFind.prototype.add = function (id)
41{
42 if (this.parent[id] == null)
43 { this.parent[id] = id; }
44};
45
46UnionFind.prototype.find = function (id)
47{
48 if (this.parent[id] !== id)
49 { this.parent[id] = this.find(this.parent[id]); }
50 return this.parent[id];
51};
52
53UnionFind.prototype.union = function (a, b)
54{
55 this.add(a);
56 this.add(b);
57 var ra = this.find(a);
58 var rb = this.find(b);
59 if (ra !== rb)
60 { this.parent[rb] = ra; }
61};
62
63function GameCore (planet, rng)
64{
65 this.planet = planet || PlanetMod.defaultPlanet();
66 this.rng = rng || Math.random;
67 this.width = PlanetMod.FIELD_WIDTH;
68 this.height = PlanetMod.FIELD_HEIGHT;
69 this.blocks = [];
70 this.clusters = Object.create(null);
71 this.nextBlockId = 1;
72 this.nextClusterId = 1;
73 this.spawnTimer = 0;
74 this.spawnInterval = PlanetMod.SPAWN_INTERVAL;
75 this.settleTime = PlanetMod.SETTLE_TIME;
76 this.score = 0;
77 this.state = 'playing';
78 this.drag = null;
79 this.gameOverTime = 0;
80 this.outgoingShots = [];
81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
82 this.missChance = optsMissChance(arguments);
83}
84
85function optsMissChance (args)
86{
87 // GameCore(planet, rng, missChance) — third arg is optional.
88 if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2]))
89 { return Math.max(0, Math.min(1, args[2])); }
90 var planet = args[0];
91 if (planet && typeof PlanetMod.missChance === 'function')
92 { return PlanetMod.missChance(planet); }
93 return GameCore.DEFAULT_MISS_CHANCE;
94}
95
96GameCore.COLOR_GRAY = COLOR_GRAY;
97GameCore.LAUNCH_PULL = LAUNCH_PULL;
98GameCore.SWAP_DISTANCE = SWAP_DISTANCE;
99GameCore.SNAP_Y = SNAP_Y;
100GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP;
101GameCore.DEFAULT_MISS_CHANCE = 0.30;
102GameCore.STATE_STILL = STATE_STILL;
103GameCore.STATE_FALLING = STATE_FALLING;
104GameCore.STATE_FLYING = STATE_FLYING;
105GameCore.STATE_SETTLING = STATE_SETTLING;
106GameCore.STATE_LOCKED = STATE_LOCKED;
107GameCore.STATE_MANUAL = STATE_MANUAL;
108
109GameCore.prototype.randomColor = function ()
110{
111 return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length;
112};
113
114GameCore.prototype.createBlock = function (col, y, color, state)
115{
116 var block = {
117 id: this.nextBlockId++,
118 col: col,
119 y: y,
120 color: color,
121 state: state || STATE_STILL,
122 vy: 0,
123 clusterId: null,
124 settleTime: 0,
125 flame: false,
126 pushedClusters: null,
127 manualTarget: 0,
128 manualDecel: 0,
129 manualPhase: null,
130 bouncing: false,
131 bounceAge: 0
132 };
133 this.blocks.push(block);
134 return block;
135};
136
137GameCore.prototype.removeBlock = function (block)
138{
139 if (block.clusterId != null)
140 { this.leaveCluster(block); }
141 var index = this.blocks.indexOf(block);
142 if (index >= 0)
143 { this.blocks.splice(index, 1); }
144};
145
146GameCore.prototype.leaveCluster = function (block)
147{
148 var id = block.clusterId;
149 block.clusterId = null;
150 block.flame = false;
151 if (id == null || !this.clusters[id])
152 { return; }
153 var still = 0;
154 for (var i = 0; i < this.blocks.length; i++)
155 {
156 if (this.blocks[i].clusterId === id)
157 { still++; }
158 }
159 if (still === 0)
160 { delete this.clusters[id]; }
161};
162
163GameCore.prototype.blocksInColumn = function (col)
164{
165 var out = [];
166 for (var i = 0; i < this.blocks.length; i++)
167 {
168 if (this.blocks[i].col === col)
169 { out.push(this.blocks[i]); }
170 }
171 out.sort(function (a, b) { return a.y - b.y; });
172 return out;
173};
174
175GameCore.prototype.isGrounded = function (block)
176{
177 return block.state === STATE_STILL
178 || block.state === STATE_SETTLING
179 || block.state === STATE_LOCKED;
180};
181
182GameCore.prototype.stackTop = function (col, ignore)
183{
184 var top = 0;
185 for (var i = 0; i < this.blocks.length; i++)
186 {
187 var block = this.blocks[i];
188 if (block.col !== col)
189 { continue; }
190 if (ignore && ignore(block))
191 { continue; }
192 if (this.isGrounded(block))
193 {
194 var surface = block.y + 1;
195 if (surface > top)
196 { top = surface; }
197 }
198 }
199 return top;
200};
201
202GameCore.prototype.isColumnLocked = function (col)
203{
204 for (var i = 0; i < this.blocks.length; i++)
205 {
206 var block = this.blocks[i];
207 if (block.col === col && block.state === STATE_LOCKED)
208 { return true; }
209 }
210 return false;
211};
212
213GameCore.prototype.isColumnStill = function (col)
214{
215 if (this.isColumnLocked(col))
216 { return false; }
217 for (var i = 0; i < this.blocks.length; i++)
218 {
219 var block = this.blocks[i];
220 if (block.col !== col)
221 { continue; }
222 if (block.state === STATE_FLYING
223 || block.state === STATE_FALLING
224 || block.state === STATE_MANUAL
225 || block.state === STATE_SETTLING)
226 { return false; }
227 }
228 return true;
229};
230
231GameCore.prototype.groundedCount = function (col)
232{
233 var n = 0;
234 for (var i = 0; i < this.blocks.length; i++)
235 {
236 if (this.blocks[i].col === col && this.isGrounded(this.blocks[i]))
237 { n++; }
238 }
239 return n;
240};
241
242GameCore.prototype.blockAt = function (col, row)
243{
244 for (var i = 0; i < this.blocks.length; i++)
245 {
246 var block = this.blocks[i];
247 if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS)
248 { return block; }
249 }
250 return null;
251};
252
253GameCore.prototype.place = function (col, row, color, state)
254{
255 return this.createBlock(col, row, color, state || STATE_STILL);
256};
257
258GameCore.prototype.lockedColumnCount = function ()
259{
260 var n = 0;
261 for (var c = 0; c < this.width; c++)
262 {
263 if (this.isColumnLocked(c))
264 { n++; }
265 }
266 return n;
267};
268
269GameCore.prototype.startBounce = function (block)
270{
271 if (!block)
272 { return; }
273 block.bouncing = true;
274 block.bounceAge = 0;
275};
276
277GameCore.prototype.advanceBounces = function (dt)
278{
279 var duration = this.planet.bounceDuration;
280 for (var i = 0; i < this.blocks.length; i++)
281 {
282 var block = this.blocks[i];
283 if (!block.bouncing)
284 { continue; }
285 block.bounceAge += dt;
286 if (duration <= 0 || block.bounceAge >= duration)
287 {
288 block.bouncing = false;
289 block.bounceAge = duration;
290 }
291 }
292};
293
294/**
295 * True if `block` rests on the floor or on a solid (still / settling /
296 * locked) block immediately below it in the same column.
297 */
298GameCore.prototype.hasSupport = function (block)
299{
300 if (block.y <= 1e-9)
301 { return true; }
302 for (var i = 0; i < this.blocks.length; i++)
303 {
304 var other = this.blocks[i];
305 if (other === block || other.col !== block.col)
306 { continue; }
307 if (!this.isGrounded(other))
308 { continue; }
309 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
310 { return true; }
311 }
312 return false;
313};
314
315/** Highest flying block in this column whose top is at or below `block.y + 1`. */
316GameCore.prototype.highestFlyingBelow = function (block)
317{
318 var best = null;
319 for (var i = 0; i < this.blocks.length; i++)
320 {
321 var other = this.blocks[i];
322 if (other === block || other.col !== block.col)
323 { continue; }
324 if (other.state !== STATE_FLYING || other.clusterId == null)
325 { continue; }
326 var cluster = this.clusters[other.clusterId];
327 if (!cluster || cluster.phase === 'landed')
328 { continue; }
329 // The falling block is at or above this flyer (or already overlapping).
330 if (block.y + 1e-9 < other.y)
331 { continue; }
332 if (!best || other.y > best.y)
333 { best = other; }
334 }
335 return best;
336};
337
338GameCore.prototype.highestFallingBelow = function (block)
339{
340 var best = null;
341 for (var i = 0; i < this.blocks.length; i++)
342 {
343 var other = this.blocks[i];
344 if (other === block || other.col !== block.col)
345 { continue; }
346 if (other.state !== STATE_FALLING)
347 { continue; }
348 if (other.y > block.y - 1e-9)
349 { continue; }
350 if (!best || other.y > best.y)
351 { best = other; }
352 }
353 return best;
354};
355
356/**
357 * Make an unsupported still block start falling from rest, so it eases
358 * into fallSpeed under normal gravity. Skips the block being dragged.
359 */
360/**
361 * Strip leftover launch / manual kinematics so a block that has just
362 * become still or falling starts from rest. Used after a landed grey
363 * cluster recolors, and whenever an unsupported still block drops.
364 */
365GameCore.prototype.clearFlightState = function (block)
366{
367 block.vy = 0;
368 block.flame = false;
369 block.manualPhase = null;
370 block.manualTarget = null;
371 block.manualDecel = 0;
372 block.pushedClusters = null;
373};
374
375GameCore.prototype.releaseUnsupported = function ()
376{
377 for (var i = 0; i < this.blocks.length; i++)
378 {
379 var block = this.blocks[i];
380 if (block.state !== STATE_STILL)
381 { continue; }
382 if (this.drag && this.drag.blockId === block.id)
383 { continue; }
384 if (this.hasSupport(block))
385 { continue; }
386 this.leaveCluster(block);
387 this.clearFlightState(block);
388 block.state = STATE_FALLING;
389 }
390};
391
392GameCore.prototype.accelerateFall = function (block, dt)
393{
394 var target = -this.planet.fallSpeed;
395 if (block.vy > target)
396 { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); }
397 else
398 { block.vy = target; }
399};
400
401GameCore.prototype.landFallingBlock = function (block)
402{
403 block.vy = 0;
404 block.flame = false;
405 this.startBounce(block);
406 if (block.color === COLOR_GRAY)
407 {
408 block.state = STATE_SETTLING;
409 block.settleTime = 0;
410 }
411 else
412 { block.state = STATE_STILL; }
413};
414
415/**
416 * Sit `block` on top of the flying cluster that `flyer` belongs to, and
417 * carry it along as cargo (no flame). Returns true if attached.
418 */
419GameCore.prototype.attachToFlyingCluster = function (block, flyer)
420{
421 var cluster = this.clusters[flyer.clusterId];
422 if (!cluster || cluster.phase === 'landed')
423 { return false; }
424 var roof = flyer.y + 1;
425 for (var i = 0; i < this.blocks.length; i++)
426 {
427 var other = this.blocks[i];
428 if (other === block || other.col !== block.col)
429 { continue; }
430 if (other.clusterId !== cluster.id)
431 { continue; }
432 if (other.y + 1 > roof)
433 { roof = other.y + 1; }
434 }
435 var oldN = this.clusterMembers(cluster.id).length;
436 var fallingVy = block.vy;
437 block.y = roof;
438 block.state = STATE_FLYING;
439 block.clusterId = cluster.id;
440 block.flame = false;
441 block.settleTime = 0;
442 // A cluster born from a thrown block averages in falling cargo
443 // the same way two flying clusters merge (t1108). Other rockets
444 // keep their velocity and just pick the cargo up.
445 if (cluster.fromManual)
446 {
447 var newN = oldN + 1;
448 cluster.vy = (cluster.vy * oldN + fallingVy) / newN;
449 cluster.decel = ((cluster.decel || 0) * oldN) / newN;
450 cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
451 cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';
452 var members = this.clusterMembers(cluster.id);
453 for (var m = 0; m < members.length; m++)
454 { members[m].vy = cluster.vy; }
455 }
456 else
457 { block.vy = cluster.vy; }
458 return true;
459};
460
461GameCore.prototype.catchFallingOntoCluster = function (cluster, members)
462{
463 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
464 { return; }
465 // Snapshot falling blocks; attaching mutates state.
466 var falling = [];
467 for (var i = 0; i < this.blocks.length; i++)
468 {
469 if (this.blocks[i].state === STATE_FALLING)
470 { falling.push(this.blocks[i]); }
471 }
472 for (var i = 0; i < falling.length; i++)
473 {
474 var block = falling[i];
475 var flyer = null;
476 for (var m = 0; m < members.length; m++)
477 {
478 var other = members[m];
479 if (other.col !== block.col)
480 { continue; }
481 // Overlap or rest on this member's top.
482 if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9)
483 {
484 if (!flyer || other.y > flyer.y)
485 { flyer = other; }
486 }
487 }
488 if (flyer)
489 { this.attachToFlyingCluster(block, flyer); }
490 }
491};
492
493GameCore.prototype.tick = function (dt)
494{
495 if (this.state === 'gameover')
496 {
497 this.gameOverTime += Math.max(0, dt);
498 return;
499 }
500 if (dt <= 0)
501 { return; }
502 dt = Math.min(dt, 0.1);
503
504 this.spawnTimer += dt;
505 while (this.spawnTimer >= this.spawnInterval)
506 {
507 this.spawnTimer -= this.spawnInterval;
508 this.trySpawn();
509 }
510
511 var left = dt;
512 while (left > 1e-12)
513 {
514 var step = Math.min(SUBSTEP, left);
515 this.integrateFalling(step);
516 this.resolveFlyingMatches();
517 this.integrateClusters(step);
518 this.mergeFlyingClusters();
519 this.integrateManual(step);
520 this.mergeAnyManualFallingOverlaps();
521 this.cullOffTop();
522 left -= step;
523 }
524
525 this.releaseUnsupported();
526 this.advanceSettle(dt);
527 // After a landed grey group is released, blocks that were sitting on
528 // hanging members are still STILL (the earlier pass ran while those
529 // members counted as support). Drop them from rest before matching,
530 // so they cannot form a mid-air match and shoot off the top.
531 this.releaseUnsupported();
532 this.advanceBounces(dt);
533 if (!this.drag)
534 { this.resolveMatches(); }
535 this.checkLocks();
536 this.checkGameOver();
537};
538
539/** Step a stretch of time in fixed substeps. Useful for tests. */
540GameCore.prototype.step = function (seconds, dt)
541{
542 dt = dt || SUBSTEP;
543 var t = 0;
544 while (t < seconds - 1e-15)
545 {
546 var s = Math.min(dt, seconds - t);
547 this.tick(s);
548 t += s;
549 }
550};
551
552GameCore.prototype.trySpawn = function ()
553{
554 if (this.state !== 'playing')
555 { return null; }
556 var eligible = [];
557 for (var col = 0; col < this.width; col++)
558 {
559 if (this.isColumnLocked(col))
560 { continue; }
561 if (this.groundedCount(col) >= this.height)
562 { continue; }
563 var alreadyFalling = false;
564 var list = this.blocksInColumn(col);
565 for (var i = 0; i < list.length; i++)
566 {
567 if (list[i].state === STATE_FALLING)
568 {
569 alreadyFalling = true;
570 break;
571 }
572 }
573 if (!alreadyFalling)
574 { eligible.push(col); }
575 }
576 if (!eligible.length)
577 { return null; }
578 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
579 var color = this.randomColor();
580 var block = this.createBlock(col, this.height, color, STATE_FALLING);
581 block.vy = -this.planet.fallSpeed;
582 return block;
583};
584
585GameCore.prototype.integrateFalling = function (dt)
586{
587 var falling = [];
588 for (var i = 0; i < this.blocks.length; i++)
589 {
590 if (this.blocks[i].state === STATE_FALLING)
591 { falling.push(this.blocks[i]); }
592 }
593 // Lowest first, so a stack of falling blocks rests on itself.
594 falling.sort(function (a, b) { return a.y - b.y; });
595 for (var i = 0; i < falling.length; i++)
596 {
597 var block = falling[i];
598 if (block.state !== STATE_FALLING)
599 { continue; }
600 this.accelerateFall(block, dt);
601 var nextY = block.y + block.vy * dt;
602 var manual = this.manualInColumnHit(block, block.y, nextY);
603 if (manual)
604 {
605 this.mergeManualWithFalling(manual, this.contiguousFallingInColumn(manual.col, manual, [block]));
606 continue;
607 }
608 var flyer = this.highestFlyingBelow(block);
609 if (flyer && nextY <= flyer.y + 1 + 1e-9)
610 {
611 if (this.attachToFlyingCluster(block, flyer))
612 { continue; }
613 }
614 var top = this.stackTop(block.col);
615 var below = this.highestFallingBelow(block);
616 if (below && below.y + 1 > top)
617 { top = below.y + 1; }
618 if (nextY <= top)
619 {
620 block.y = top;
621 this.landFallingBlock(block);
622 }
623 else
624 { block.y = nextY; }
625 }
626};
627
628GameCore.prototype.clusterMembers = function (clusterId)
629{
630 var out = [];
631 for (var i = 0; i < this.blocks.length; i++)
632 {
633 if (this.blocks[i].clusterId === clusterId)
634 { out.push(this.blocks[i]); }
635 }
636 return out;
637};
638
639
640/**
641 * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a
642 * jump of height H peaks at exactly H after riseTime. Returns dy.
643 * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the
644 * upward deceleration. After the apex, vy eases from 0 to -fallSpeed
645 * at planet.fallAccel, then stays at terminal fallSpeed.
646 */
647GameCore.prototype.advanceJump = function (body, dt)
648{
649 var dy = 0;
650 var left = dt;
651 if (body.phase === 'up')
652 {
653 var decel = body.decel;
654 var vy = body.vy;
655 var tApex = decel > 1e-12 ? vy / decel : Infinity;
656 if (tApex <= left + 1e-15)
657 {
658 dy += vy * tApex - 0.5 * decel * tApex * tApex;
659 body.vy = 0;
660 body.phase = 'down';
661 left -= tApex;
662 }
663 else
664 {
665 var vyNew = vy - decel * left;
666 dy += (vy + vyNew) * 0.5 * left;
667 body.vy = vyNew;
668 left = 0;
669 }
670 }
671 if (body.phase === 'up' && body.vy <= 1e-10)
672 {
673 body.vy = 0;
674 body.phase = 'down';
675 }
676 if (body.phase === 'down' && left > 0)
677 {
678 var target = -this.planet.fallSpeed;
679 var accel = this.planet.fallAccel;
680 if (accel < 1e-12)
681 {
682 body.vy = target;
683 dy += body.vy * left;
684 }
685 else if (body.vy > target + 1e-12)
686 {
687 var tReach = (body.vy - target) / accel;
688 if (tReach <= left + 1e-15)
689 {
690 dy += (body.vy + target) * 0.5 * tReach;
691 body.vy = target;
692 left -= tReach;
693 dy += body.vy * left;
694 }
695 else
696 {
697 var vyNew = body.vy - accel * left;
698 dy += (body.vy + vyNew) * 0.5 * left;
699 body.vy = vyNew;
700 }
701 }
702 else
703 {
704 body.vy = target;
705 dy += body.vy * left;
706 }
707 }
708 return dy;
709};
710
711GameCore.prototype.integrateClusters = function (dt)
712{
713 var ids = Object.keys(this.clusters);
714 for (var n = 0; n < ids.length; n++)
715 {
716 var cluster = this.clusters[ids[n]];
717 if (!cluster)
718 { continue; }
719 var members = this.clusterMembers(cluster.id);
720 if (!members.length)
721 {
722 delete this.clusters[cluster.id];
723 continue;
724 }
725 if (cluster.phase === 'landed' || cluster.phase === 'hold')
726 { continue; }
727
728 var dy = this.advanceJump(cluster, dt);
729 for (var i = 0; i < members.length; i++)
730 {
731 members[i].y += dy;
732 members[i].vy = cluster.vy;
733 }
734
735 this.catchFallingOntoCluster(cluster, members);
736 this.checkChainRelaunch(cluster);
737
738 if (cluster.phase === 'down' && this.clusters[cluster.id])
739 { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }
740 }
741};
742
743
744/**
745 * If two or more flying clusters overlap in the same column, merge them
746 * into one cluster. Velocity (and deceleration and jumpHeight) is the
747 * mass-weighted average by block count. Phase is 'up' when the merged
748 * vy is positive, otherwise 'down'. Overlapping members in a column
749 * are packed so the higher one sits flush on the lower.
750 */
751GameCore.prototype.mergeFlyingClusters = function ()
752{
753 var flying = [];
754 var ids = Object.keys(this.clusters);
755 for (var n = 0; n < ids.length; n++)
756 {
757 var cluster = this.clusters[ids[n]];
758 if (!cluster)
759 { continue; }
760 if (cluster.phase === 'landed' || cluster.phase === 'hold')
761 { continue; }
762 flying.push(cluster);
763 }
764 if (flying.length < 2)
765 { return; }
766
767 var uf = new UnionFind();
768 for (var i = 0; i < flying.length; i++)
769 { uf.add(flying[i].id); }
770 for (var i = 0; i < flying.length; i++)
771 {
772 for (var j = i + 1; j < flying.length; j++)
773 {
774 if (this.flyingClustersOverlap(flying[i], flying[j]))
775 { uf.union(flying[i].id, flying[j].id); }
776 }
777 }
778
779 var groups = Object.create(null);
780 for (var i = 0; i < flying.length; i++)
781 {
782 var root = uf.find(flying[i].id);
783 if (!groups[root])
784 { groups[root] = []; }
785 groups[root].push(flying[i]);
786 }
787 for (var root in groups)
788 {
789 if (groups[root].length >= 2)
790 { this.mergeClusterGroup(groups[root]); }
791 }
792};
793
794GameCore.prototype.flyingClustersOverlap = function (a, b)
795{
796 var ma = this.clusterMembers(a.id);
797 var mb = this.clusterMembers(b.id);
798 for (var i = 0; i < ma.length; i++)
799 {
800 for (var j = 0; j < mb.length; j++)
801 {
802 if (ma[i].col !== mb[j].col)
803 { continue; }
804 if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9)
805 { return true; }
806 }
807 }
808 return false;
809};
810
811GameCore.prototype.mergeClusterGroup = function (clusters)
812{
813 clusters.sort(function (a, b) { return a.id - b.id; });
814 var keep = clusters[0];
815 var n = 0;
816 var vySum = 0;
817 var decelSum = 0;
818 var hSum = 0;
819 for (var c = 0; c < clusters.length; c++)
820 {
821 var mem = this.clusterMembers(clusters[c].id);
822 var m = mem.length;
823 n += m;
824 vySum += clusters[c].vy * m;
825 decelSum += (clusters[c].decel || 0) * m;
826 hSum += (clusters[c].jumpHeight || 0) * m;
827 }
828 if (n === 0)
829 { return; }
830 keep.vy = vySum / n;
831 keep.decel = decelSum / n;
832 keep.jumpHeight = hSum / n;
833 keep.phase = keep.vy > 1e-10 ? 'up' : 'down';
834 for (var c = 0; c < clusters.length; c++)
835 {
836 var mem = this.clusterMembers(clusters[c].id);
837 for (var i = 0; i < mem.length; i++)
838 {
839 mem[i].clusterId = keep.id;
840 mem[i].vy = keep.vy;
841 mem[i].state = STATE_FLYING;
842 }
843 if (clusters[c] !== keep)
844 { delete this.clusters[clusters[c].id]; }
845 }
846 this.packClusterOverlaps(keep);
847 this.checkChainRelaunch(keep);
848 if (keep.phase === 'down' && this.clusters[keep.id])
849 { this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
850};
851
852GameCore.prototype.packClusterOverlaps = function (cluster)
853{
854 var members = this.clusterMembers(cluster.id);
855 var byCol = Object.create(null);
856 for (var i = 0; i < members.length; i++)
857 {
858 var col = members[i].col;
859 if (!byCol[col])
860 { byCol[col] = []; }
861 byCol[col].push(members[i]);
862 }
863 var cols = Object.keys(byCol);
864 for (var c = 0; c < cols.length; c++)
865 {
866 var list = byCol[cols[c]];
867 list.sort(function (a, b) { return a.y - b.y; });
868 for (var i = 1; i < list.length; i++)
869 {
870 var minY = list[i - 1].y + 1;
871 if (list[i].y < minY - 1e-12)
872 { list[i].y = minY; }
873 }
874 }
875};
876
877GameCore.prototype.tryLandCluster = function (cluster, members)
878{
879 if (!members.length)
880 { return; }
881 var shift = 0;
882 var hit = false;
883 for (var i = 0; i < members.length; i++)
884 {
885 var block = members[i];
886 var top = this.stackTop(block.col, function (other)
887 {
888 return other.clusterId === cluster.id;
889 });
890 if (block.y <= top + 1e-9)
891 {
892 hit = true;
893 var need = top - block.y;
894 if (need > shift)
895 { shift = need; }
896 }
897 }
898 if (!hit)
899 { return; }
900 // Lift the whole rigid body so the deepest contact sits flush.
901 for (var i = 0; i < members.length; i++)
902 { members[i].y += shift; }
903 this.landCluster(cluster, members);
904};
905
906/**
907 * Land as a rigid group: relative positions are kept, so part of the
908 * cluster may hang above a gap. Grey members start the 3s settle timer;
909 * coloured cargo stays put with them until every grey has recolored.
910 * Members that actually rest on the floor or on another block bounce.
911 */
912GameCore.prototype.landCluster = function (cluster, members)
913{
914 for (var i = 0; i < members.length; i++)
915 {
916 var block = members[i];
917 block.vy = 0;
918 block.flame = false;
919 block.clusterId = cluster.id;
920 if (block.color === COLOR_GRAY)
921 {
922 block.state = STATE_SETTLING;
923 block.settleTime = 0;
924 }
925 else
926 {
927 // Cargo stays in the rigid group (not matchable) until release.
928 block.state = STATE_SETTLING;
929 block.settleTime = 0;
930 }
931 if (this.hasSupport(block))
932 { this.startBounce(block); }
933 }
934 cluster.phase = 'landed';
935 cluster.vy = 0;
936};
937
938/**
939 * After every grey in a landed cluster has recolored, let the group go.
940 * Unsupported blocks fall individually from rest; supported ones stay.
941 */
942GameCore.prototype.releaseLandedCluster = function (cluster)
943{
944 var members = this.clusterMembers(cluster.id);
945 var releasing = Object.create(null);
946 for (var i = 0; i < members.length; i++)
947 { releasing[members[i].id] = true; }
948
949 for (var i = 0; i < members.length; i++)
950 {
951 var block = members[i];
952 if (block.color === COLOR_GRAY)
953 {
954 block.color = this.randomColor();
955 block.settleTime = 0;
956 }
957 this.leaveCluster(block);
958 // Always drop leftover launch / manual velocity. A stale upward
959 // vy here is what sends blocks off the top "very fast" after
960 // a grey group recolors.
961 this.clearFlightState(block);
962 // Support from outside the group (floor or a foreign block). Fellow
963 // members that will also fall do not count, so a hanging stack
964 // drops together without the top one staying put.
965 var supported = this.hasSupportOutside(block, releasing);
966 block.state = supported ? STATE_STILL : STATE_FALLING;
967 }
968 delete this.clusters[cluster.id];
969};
970
971GameCore.prototype.hasSupportOutside = function (block, ignoreIds)
972{
973 if (block.y <= 1e-9)
974 { return true; }
975 for (var i = 0; i < this.blocks.length; i++)
976 {
977 var other = this.blocks[i];
978 if (other === block || other.col !== block.col)
979 { continue; }
980 if (ignoreIds && ignoreIds[other.id])
981 { continue; }
982 if (!this.isGrounded(other))
983 { continue; }
984 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
985 { return true; }
986 }
987 return false;
988};
989
990GameCore.prototype.integrateManual = function (dt)
991{
992 for (var i = 0; i < this.blocks.length; i++)
993 {
994 var block = this.blocks[i];
995 if (block.state !== STATE_MANUAL)
996 { continue; }
997
998 if (block.manualPhase === 'up')
999 {
1000 var oldY = block.y;
1001 var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
1002 var dy = this.advanceJump(proxy, dt);
1003 var nextY = oldY + dy;
1004 this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));
1005 block.vy = proxy.vy;
1006 block.manualPhase = proxy.phase;
1007 block.y = Math.min(nextY, block.manualTarget);
1008 if (this.tryMergeManualWithFalling(block, oldY, block.y))
1009 { continue; }
1010 if (block.y >= block.manualTarget - 1e-12)
1011 {
1012 block.y = block.manualTarget;
1013 block.vy = 0;
1014 block.manualPhase = 'down';
1015 }
1016 if (block.y > this.height - 1)
1017 {
1018 block.y = this.height - 1;
1019 block.vy = 0;
1020 block.manualPhase = 'down';
1021 }
1022 }
1023
1024 if (block.manualPhase === 'down')
1025 {
1026 var proxy = { phase: 'down', vy: block.vy, decel: 0 };
1027 var dy = this.advanceJump(proxy, dt);
1028 block.vy = proxy.vy;
1029 var nextY = block.y + dy;
1030 var top = this.stackTop(block.col);
1031 if (nextY <= top)
1032 {
1033 block.y = top;
1034 block.state = STATE_STILL;
1035 block.vy = 0;
1036 block.manualPhase = null;
1037 block.flame = false;
1038 this.startBounce(block);
1039 }
1040 else
1041 {
1042 block.y = Math.min(nextY, this.height - 1);
1043 if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1044 { continue; }
1045 }
1046 }
1047 }
1048};
1049
1050/**
1051 * If the manual throw (or its path this step) hits falling blocks, merge
1052 * them into a flying cluster with mass-weighted velocity, the same rule
1053 * as two flying clusters colliding (t1108). Returns true if merged.
1054 */
1055GameCore.prototype.tryMergeManualWithFalling = function (manual, oldY, newY)
1056{
1057 if (!manual || manual.state !== STATE_MANUAL)
1058 { return false; }
1059 var hits = this.fallingHitsOnManualPath(manual, oldY, newY);
1060 if (!hits.length)
1061 { return false; }
1062 this.mergeManualWithFalling(manual, hits);
1063 return true;
1064};
1065
1066GameCore.prototype.manualInColumnHit = function (falling, oldY, newY)
1067{
1068 var lo = Math.min(oldY, newY);
1069 var hi = Math.max(oldY, newY) + 1;
1070 for (var i = 0; i < this.blocks.length; i++)
1071 {
1072 var other = this.blocks[i];
1073 if (other === falling || other.col !== falling.col)
1074 { continue; }
1075 if (other.state !== STATE_MANUAL)
1076 { continue; }
1077 if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)
1078 { return other; }
1079 }
1080 return null;
1081};
1082
1083GameCore.prototype.fallingHitsOnManualPath = function (manual, oldY, newY)
1084{
1085 var lo = Math.min(oldY, newY);
1086 var hi = Math.max(oldY, newY) + 1;
1087 var hits = [];
1088 for (var i = 0; i < this.blocks.length; i++)
1089 {
1090 var other = this.blocks[i];
1091 if (other === manual || other.col !== manual.col)
1092 { continue; }
1093 if (other.state !== STATE_FALLING)
1094 { continue; }
1095 if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)
1096 { hits.push(other); }
1097 }
1098 if (!hits.length)
1099 { return hits; }
1100 return this.contiguousFallingInColumn(manual.col, manual, hits);
1101};
1102
1103/**
1104 * Every falling block in `col` that overlaps `anchor` or a hit already
1105 * in `seed`, plus a contiguous falling stack sitting on them.
1106 */
1107GameCore.prototype.contiguousFallingInColumn = function (col, anchor, seed)
1108{
1109 var falling = [];
1110 for (var i = 0; i < this.blocks.length; i++)
1111 {
1112 if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING)
1113 { falling.push(this.blocks[i]); }
1114 }
1115 falling.sort(function (a, b) { return a.y - b.y; });
1116 var include = Object.create(null);
1117 var queue = [];
1118 function enqueue (b)
1119 {
1120 if (!b || include[b.id])
1121 { return; }
1122 include[b.id] = true;
1123 queue.push(b);
1124 }
1125 if (seed)
1126 {
1127 for (var s = 0; s < seed.length; s++)
1128 { enqueue(seed[s]); }
1129 }
1130 // Also include falling that already overlap the anchor.
1131 if (anchor)
1132 {
1133 for (var i = 0; i < falling.length; i++)
1134 {
1135 if (falling[i].y < anchor.y + 1 - 1e-9 && anchor.y < falling[i].y + 1 - 1e-9)
1136 { enqueue(falling[i]); }
1137 }
1138 }
1139 for (var q = 0; q < queue.length; q++)
1140 {
1141 var a = queue[q];
1142 for (var i = 0; i < falling.length; i++)
1143 {
1144 var b = falling[i];
1145 if (include[b.id])
1146 { continue; }
1147 if (a.y < b.y + 1 + 1e-9 && b.y < a.y + 1 + 1e-9)
1148 { enqueue(b); }
1149 }
1150 }
1151 var out = [];
1152 for (var i = 0; i < falling.length; i++)
1153 {
1154 if (include[falling[i].id])
1155 { out.push(falling[i]); }
1156 }
1157 return out;
1158};
1159
1160/**
1161 * Merge a thrown (manual) block with falling blocks into one flying
1162 * cluster. Velocity, deceleration and jumpHeight are the mass-weighted
1163 * averages by block count, identical to mergeClusterGroup. The thrown
1164 * block stops being manual and the group follows normal cluster physics.
1165 * fromManual marks the cluster so later falling cargo also averages in.
1166 */
1167GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)
1168{
1169 if (!manual || manual.state !== STATE_MANUAL)
1170 { return null; }
1171 var blocks = [manual];
1172 for (var i = 0; i < fallingList.length; i++)
1173 {
1174 if (fallingList[i] && fallingList[i].state === STATE_FALLING && blocks.indexOf(fallingList[i]) < 0)
1175 { blocks.push(fallingList[i]); }
1176 }
1177 if (blocks.length < 2)
1178 { return null; }
1179 var n = blocks.length;
1180 var vySum = 0;
1181 var decelSum = 0;
1182 var hSum = 0;
1183 for (var i = 0; i < n; i++)
1184 {
1185 var b = blocks[i];
1186 vySum += b.vy;
1187 if (b === manual)
1188 {
1189 decelSum += manual.manualDecel || 0;
1190 hSum += manual.manualJumpHeight != null ? manual.manualJumpHeight
1191 : Math.max(0, (manual.manualTarget || this.height - 1) - manual.y);
1192 }
1193 }
1194 var cluster = {
1195 id: this.nextClusterId++,
1196 jumpHeight: hSum / n,
1197 vy: vySum / n,
1198 decel: decelSum / n,
1199 phase: (vySum / n) > 1e-10 ? 'up' : 'down',
1200 fromManual: true
1201 };
1202 this.clusters[cluster.id] = cluster;
1203 for (var i = 0; i < n; i++)
1204 {
1205 var b = blocks[i];
1206 b.clusterId = cluster.id;
1207 b.state = STATE_FLYING;
1208 b.vy = cluster.vy;
1209 b.flame = b.color === COLOR_GRAY;
1210 b.manualPhase = null;
1211 b.manualTarget = null;
1212 b.manualDecel = 0;
1213 b.manualJumpHeight = 0;
1214 b.pushedClusters = null;
1215 b.settleTime = 0;
1216 }
1217 if (this.drag && this.drag.blockId === manual.id)
1218 { this.drag = null; }
1219 this.packClusterOverlaps(cluster);
1220 this.checkChainRelaunch(cluster);
1221 return cluster;
1222};
1223
1224/** Safety net: any remaining manual/falling overlaps after the substep. */
1225GameCore.prototype.mergeAnyManualFallingOverlaps = function ()
1226{
1227 var manuals = [];
1228 for (var i = 0; i < this.blocks.length; i++)
1229 {
1230 if (this.blocks[i].state === STATE_MANUAL)
1231 { manuals.push(this.blocks[i]); }
1232 }
1233 for (var m = 0; m < manuals.length; m++)
1234 {
1235 var manual = manuals[m];
1236 if (manual.state !== STATE_MANUAL)
1237 { continue; }
1238 var hits = this.contiguousFallingInColumn(manual.col, manual);
1239 if (hits.length)
1240 { this.mergeManualWithFalling(manual, hits); }
1241 }
1242};
1243
1244GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1245{
1246 if (!manual.pushedClusters)
1247 { manual.pushedClusters = Object.create(null); }
1248
1249 var manualTopOld = oldY + 1;
1250 var manualTopNew = nextY + 1;
1251 var low = Math.min(oldY, nextY);
1252 var high = Math.max(oldY, nextY) + 1;
1253
1254 for (var i = 0; i < this.blocks.length; i++)
1255 {
1256 var other = this.blocks[i];
1257 if (other === manual)
1258 { continue; }
1259 if (other.col !== manual.col)
1260 { continue; }
1261 if (other.state !== STATE_FLYING || other.clusterId == null)
1262 { continue; }
1263 if (manual.pushedClusters[other.clusterId])
1264 { continue; }
1265 var otherBot = other.y;
1266 var otherTop = other.y + 1;
1267 var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;
1268 var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;
1269 if (overlapping || crossing)
1270 {
1271 if (this.pushClusterColumn(other.clusterId, manual.col))
1272 {
1273 manual.pushedClusters[other.clusterId] = true;
1274 return;
1275 }
1276 }
1277 }
1278};
1279
1280
1281/**
1282 * Push every flying block of the cluster that sits in `col` one tile up.
1283 * Other columns stay put (they remain linked). Then look for a new
1284 * same-color line of 3+ in the flying cluster; if there is one, those
1285 * blocks turn gray, join the link, and the whole cluster re-launches
1286 * with the same jump height from its current position.
1287 */
1288GameCore.prototype.pushClusterColumn = function (clusterId, col)
1289{
1290 var cluster = this.clusters[clusterId];
1291 if (!cluster)
1292 { return false; }
1293 var members = this.clusterMembers(clusterId);
1294 for (var i = 0; i < members.length; i++)
1295 {
1296 if (members[i].col === col)
1297 { members[i].y += 1; }
1298 }
1299 this.cullOffTop();
1300 if (!this.clusters[clusterId])
1301 { return true; }
1302 this.checkChainRelaunch(cluster);
1303 return true;
1304};
1305
1306/**
1307 * After a cargo attach or a cluster merge (or any other mid-air state
1308 * change), turn spontaneous lines of 3+ into rockets and give the whole
1309 * already-flying cluster an extra launch from its current position —
1310 * the same re-launch as a player push that completes a row.
1311 */
1312GameCore.prototype.resolveFlyingMatches = function ()
1313{
1314 var ids = Object.keys(this.clusters);
1315 for (var n = 0; n < ids.length; n++)
1316 {
1317 var cluster = this.clusters[ids[n]];
1318 if (!cluster || cluster.phase === 'landed')
1319 { continue; }
1320 this.checkChainRelaunch(cluster);
1321 }
1322};
1323
1324GameCore.prototype.checkChainRelaunch = function (cluster)
1325{
1326 if (!this.clusters[cluster.id])
1327 { return; }
1328 var lines = this.findFlyingMatchLines(cluster.id);
1329 if (!lines.length)
1330 { return; }
1331 var newly = [];
1332 var seen = Object.create(null);
1333 for (var i = 0; i < lines.length; i++)
1334 {
1335 for (var j = 0; j < lines[i].blocks.length; j++)
1336 {
1337 var block = lines[i].blocks[j];
1338 if (seen[block.id])
1339 { continue; }
1340 seen[block.id] = true;
1341 newly.push(block);
1342 }
1343 }
1344 for (var i = 0; i < newly.length; i++)
1345 {
1346 var block = newly[i];
1347 block.color = COLOR_GRAY;
1348 block.flame = true;
1349 block.clusterId = cluster.id;
1350 block.state = STATE_FLYING;
1351 }
1352 this.relaunchCluster(cluster, cluster.jumpHeight);
1353};
1354
1355GameCore.prototype.relaunchCluster = function (cluster, height)
1356{
1357 cluster.jumpHeight = height;
1358 cluster.vy = PlanetMod.launchVelocity(this.planet, height);
1359 cluster.decel = PlanetMod.launchDeceleration(this.planet, height);
1360 cluster.phase = 'up';
1361 var members = this.clusterMembers(cluster.id);
1362 for (var i = 0; i < members.length; i++)
1363 {
1364 members[i].vy = cluster.vy;
1365 members[i].state = STATE_FLYING;
1366 if (members[i].color === COLOR_GRAY)
1367 { members[i].flame = true; }
1368 }
1369};
1370
1371GameCore.prototype.startManualLaunch = function (block)
1372{
1373 var target = this.height - 1;
1374 var height = Math.max(0.05, target - block.y);
1375 this.leaveCluster(block);
1376 block.state = STATE_MANUAL;
1377 block.pushedClusters = Object.create(null);
1378 block.manualTarget = target;
1379 block.manualPhase = 'up';
1380 block.manualJumpHeight = height;
1381 block.vy = PlanetMod.launchVelocity(this.planet, height);
1382 block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
1383 block.flame = false;
1384 if (this.drag && this.drag.blockId === block.id)
1385 { this.drag = null; }
1386};
1387
1388GameCore.prototype.cullOffTop = function ()
1389{
1390 var gone = [];
1391 for (var i = 0; i < this.blocks.length; i++)
1392 {
1393 var block = this.blocks[i];
1394 if (block.state === STATE_MANUAL)
1395 {
1396 if (block.y > this.height - 1)
1397 { block.y = this.height - 1; }
1398 continue;
1399 }
1400 if (block.y >= this.height - 1e-9)
1401 { gone.push(block); }
1402 }
1403 for (var i = 0; i < gone.length; i++)
1404 {
1405 var block = gone[i];
1406 this.outgoingShots.push({
1407 id: block.id,
1408 color: block.color,
1409 col: block.col,
1410 y: block.y
1411 });
1412 this.score += 1;
1413 this.removeBlock(block);
1414 }
1415};
1416
1417GameCore.prototype.consumeOutgoingShots = function ()
1418{
1419 var shots = this.outgoingShots;
1420 this.outgoingShots = [];
1421 return shots;
1422};
1423
1424/**
1425 * A compact field picture for the multiplayer snapshot (miniatures).
1426 * Includes every block's col, y, color, state and flame.
1427 */
1428GameCore.prototype.fieldSnapshot = function ()
1429{
1430 var blocks = [];
1431 for (var i = 0; i < this.blocks.length; i++)
1432 {
1433 var b = this.blocks[i];
1434 blocks.push({
1435 id: b.id,
1436 col: b.col,
1437 y: Math.round(b.y * 1000) / 1000,
1438 color: b.color,
1439 state: b.state,
1440 flame: !!b.flame
1441 });
1442 }
1443 return {
1444 score: this.score,
1445 state: this.state,
1446 width: this.width,
1447 height: this.height,
1448 locked: this.lockedColumnCount(),
1449 blocks: blocks
1450 };
1451};
1452
1453/**
1454 * Incoming garbage from another player: a dark-grey block that falls
1455 * normally from the top of an open column. After landing it uses the
1456 * usual 3s settle and then a random colour (landFallingBlock).
1457 * `col` is optional; a random eligible column is used otherwise.
1458 * Returns the new block, or null if every column is locked or full.
1459 */
1460GameCore.prototype.ingestGarbage = function (col)
1461{
1462 if (this.state !== 'playing')
1463 { return null; }
1464 var chosen = null;
1465 if (col != null && col >= 0 && col < this.width
1466 && !this.isColumnLocked(col) && this.groundedCount(col) < this.height)
1467 { chosen = col | 0; }
1468 else
1469 {
1470 var eligible = [];
1471 for (var c = 0; c < this.width; c++)
1472 {
1473 if (this.isColumnLocked(c))
1474 { continue; }
1475 if (this.groundedCount(c) >= this.height)
1476 { continue; }
1477 eligible.push(c);
1478 }
1479 if (!eligible.length)
1480 { return null; }
1481 chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
1482 }
1483 var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING);
1484 block.vy = -this.planet.fallSpeed;
1485 block.flame = false;
1486 return block;
1487};
1488
1489GameCore.prototype.advanceSettle = function (dt)
1490{
1491 for (var i = 0; i < this.blocks.length; i++)
1492 {
1493 var block = this.blocks[i];
1494 if (block.state !== STATE_SETTLING)
1495 { continue; }
1496 var inLanded = block.clusterId != null
1497 && this.clusters[block.clusterId]
1498 && this.clusters[block.clusterId].phase === 'landed';
1499 if (block.color === COLOR_GRAY)
1500 {
1501 block.settleTime += dt;
1502 if (block.settleTime >= this.settleTime)
1503 {
1504 block.color = this.randomColor();
1505 block.settleTime = 0;
1506 block.flame = false;
1507 if (inLanded)
1508 {
1509 // Stay in the rigid group; releaseLandedCluster runs below.
1510 block.state = STATE_SETTLING;
1511 }
1512 else
1513 {
1514 this.leaveCluster(block);
1515 this.clearFlightState(block);
1516 block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
1517 }
1518 }
1519 }
1520 else if (!inLanded)
1521 {
1522 // Coloured settling that is not in a landed cluster (shouldn't
1523 // normally happen): just become still or fall, from rest.
1524 this.leaveCluster(block);
1525 this.clearFlightState(block);
1526 block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
1527 }
1528 }
1529
1530 var ids = Object.keys(this.clusters);
1531 for (var n = 0; n < ids.length; n++)
1532 {
1533 var cluster = this.clusters[ids[n]];
1534 if (!cluster || cluster.phase !== 'landed')
1535 { continue; }
1536 var members = this.clusterMembers(cluster.id);
1537 var greySettling = false;
1538 for (var i = 0; i < members.length; i++)
1539 {
1540 if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING)
1541 { greySettling = true; }
1542 }
1543 if (!greySettling)
1544 { this.releaseLandedCluster(cluster); }
1545 }
1546};
1547
1548GameCore.prototype.findMatchLines = function ()
1549{
1550 var candidates = [];
1551 for (var i = 0; i < this.blocks.length; i++)
1552 {
1553 var block = this.blocks[i];
1554 // Coloured cargo in a just-landed cluster (SETTLING) can match
1555 // with already-resting blocks immediately (t1110). Greys have no
1556 // colour and wait until they recolor; after release they are STILL.
1557 if ((block.state === STATE_STILL || block.state === STATE_SETTLING)
1558 && block.color >= 0)
1559 { candidates.push(block); }
1560 }
1561 return this.collectLines(candidates, true);
1562};
1563
1564GameCore.prototype.findFlyingMatchLines = function (clusterId)
1565{
1566 var candidates = [];
1567 for (var i = 0; i < this.blocks.length; i++)
1568 {
1569 var block = this.blocks[i];
1570 if (block.clusterId === clusterId && block.color >= 0
1571 && (block.state === STATE_FLYING))
1572 { candidates.push(block); }
1573 }
1574 return this.collectLines(candidates, false);
1575};
1576
1577GameCore.prototype.collectLines = function (candidates, snapInteger)
1578{
1579 var lines = [];
1580 if (!candidates.length)
1581 { return lines; }
1582
1583 var byRow = Object.create(null);
1584 for (var i = 0; i < candidates.length; i++)
1585 {
1586 var block = candidates[i];
1587 var row = Math.round(block.y);
1588 if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5))
1589 { continue; }
1590 if (!byRow[row])
1591 { byRow[row] = []; }
1592 byRow[row].push(block);
1593 }
1594 var rows = Object.keys(byRow);
1595 for (var r = 0; r < rows.length; r++)
1596 {
1597 var list = byRow[rows[r]];
1598 list.sort(function (a, b) { return a.col - b.col; });
1599 this.pushRuns(lines, list, false, function (a, b)
1600 {
1601 return a.color === b.color && a.col === b.col - 1 && a.color >= 0;
1602 });
1603 }
1604
1605 var byCol = Object.create(null);
1606 for (var i = 0; i < candidates.length; i++)
1607 {
1608 var block = candidates[i];
1609 if (!byCol[block.col])
1610 { byCol[block.col] = []; }
1611 byCol[block.col].push(block);
1612 }
1613 var cols = Object.keys(byCol);
1614 for (var c = 0; c < cols.length; c++)
1615 {
1616 var list = byCol[cols[c]];
1617 list.sort(function (a, b) { return a.y - b.y; });
1618 this.pushRuns(lines, list, true, function (a, b)
1619 {
1620 return a.color === b.color && a.color >= 0
1621 && Math.abs((b.y - a.y) - 1) < ALIGN_EPS;
1622 });
1623 }
1624 return lines;
1625};
1626
1627GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent)
1628{
1629 var start = 0;
1630 for (var i = 1; i <= list.length; i++)
1631 {
1632 var same = i < list.length && adjacent(list[i - 1], list[i]);
1633 if (!same)
1634 {
1635 var length = i - start;
1636 if (length >= 3)
1637 {
1638 lines.push({
1639 blocks: list.slice(start, i),
1640 length: length,
1641 vertical: vertical
1642 });
1643 }
1644 start = i;
1645 }
1646 }
1647};
1648
1649GameCore.prototype.resolveMatches = function ()
1650{
1651 if (this.state !== 'playing' || this.drag)
1652 { return []; }
1653 var lines = this.findMatchLines();
1654 if (!lines.length)
1655 { return []; }
1656
1657 var uf = new UnionFind();
1658 var blockById = Object.create(null);
1659 for (var i = 0; i < lines.length; i++)
1660 {
1661 var line = lines[i];
1662 for (var j = 0; j < line.blocks.length; j++)
1663 {
1664 uf.add(line.blocks[j].id);
1665 blockById[line.blocks[j].id] = line.blocks[j];
1666 if (j > 0)
1667 { uf.union(line.blocks[0].id, line.blocks[j].id); }
1668 }
1669 }
1670 var matched = [];
1671 var seen = Object.create(null);
1672 for (var id in blockById)
1673 {
1674 if (!seen[id])
1675 {
1676 seen[id] = true;
1677 matched.push(blockById[id]);
1678 }
1679 }
1680 for (var i = 0; i < matched.length; i++)
1681 {
1682 for (var j = i + 1; j < matched.length; j++)
1683 {
1684 if (matched[i].col === matched[j].col)
1685 { uf.union(matched[i].id, matched[j].id); }
1686 }
1687 }
1688
1689 var groups = Object.create(null);
1690 for (var i = 0; i < matched.length; i++)
1691 {
1692 var root = uf.find(matched[i].id);
1693 if (!groups[root])
1694 { groups[root] = []; }
1695 groups[root].push(matched[i]);
1696 }
1697
1698 var launched = [];
1699 for (var root in groups)
1700 {
1701 var group = groups[root];
1702 var height = 0;
1703 for (var i = 0; i < lines.length; i++)
1704 {
1705 var line = lines[i];
1706 var overlaps = false;
1707 for (var j = 0; j < line.blocks.length; j++)
1708 {
1709 for (var k = 0; k < group.length; k++)
1710 {
1711 if (line.blocks[j].id === group[k].id)
1712 { overlaps = true; }
1713 }
1714 }
1715 if (overlaps)
1716 {
1717 var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical);
1718 if (h > height)
1719 { height = h; }
1720 }
1721 }
1722 this.launchCluster(group, height);
1723 launched.push({ blocks: group, jumpHeight: height });
1724 }
1725 return launched;
1726};
1727
1728/**
1729 * Gray the matched blocks, take everything sitting on top of them in
1730 * those columns, link them, and launch with jump height `height`.
1731 */
1732GameCore.prototype.launchCluster = function (matchedBlocks, height)
1733{
1734 var lowestInCol = Object.create(null);
1735 var matchedIds = Object.create(null);
1736 for (var i = 0; i < matchedBlocks.length; i++)
1737 {
1738 var block = matchedBlocks[i];
1739 matchedIds[block.id] = true;
1740 if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col])
1741 { lowestInCol[block.col] = block.y; }
1742 }
1743
1744 var clusterId = this.nextClusterId++;
1745 var cluster = {
1746 id: clusterId,
1747 jumpHeight: height,
1748 vy: PlanetMod.launchVelocity(this.planet, height),
1749 decel: PlanetMod.launchDeceleration(this.planet, height),
1750 phase: 'up'
1751 };
1752 this.clusters[clusterId] = cluster;
1753
1754 var riders = [];
1755 var cols = Object.keys(lowestInCol);
1756 for (var c = 0; c < cols.length; c++)
1757 {
1758 var col = +cols[c];
1759 var lowest = lowestInCol[col];
1760 var list = this.blocksInColumn(col);
1761 for (var i = 0; i < list.length; i++)
1762 {
1763 var block = list[i];
1764 if (!this.isGrounded(block) && block.state !== STATE_STILL)
1765 {
1766 if (block.state !== STATE_SETTLING)
1767 { continue; }
1768 }
1769 if (block.y + ALIGN_EPS >= lowest)
1770 { riders.push(block); }
1771 }
1772 }
1773
1774 for (var i = 0; i < riders.length; i++)
1775 {
1776 var block = riders[i];
1777 var isMatch = !!matchedIds[block.id];
1778 if (isMatch || block.color === COLOR_GRAY)
1779 {
1780 block.color = COLOR_GRAY;
1781 block.flame = true;
1782 }
1783 else
1784 { block.flame = false; }
1785 block.state = STATE_FLYING;
1786 block.clusterId = clusterId;
1787 block.vy = cluster.vy;
1788 block.settleTime = 0;
1789 }
1790 return cluster;
1791};
1792
1793GameCore.prototype.checkLocks = function ()
1794{
1795 for (var col = 0; col < this.width; col++)
1796 {
1797 if (this.isColumnLocked(col))
1798 { continue; }
1799 if (this.groundedCount(col) < this.height)
1800 { continue; }
1801 // Must fill the column from floor to top: 13 grounded blocks at y 0..12.
1802 var filled = true;
1803 for (var row = 0; row < this.height; row++)
1804 {
1805 var block = this.blockAt(col, row);
1806 if (!block || !this.isGrounded(block))
1807 {
1808 filled = false;
1809 break;
1810 }
1811 }
1812 if (!filled)
1813 { continue; }
1814 var list = this.blocksInColumn(col);
1815 for (var i = 0; i < list.length; i++)
1816 {
1817 var block = list[i];
1818 if (!this.isGrounded(block))
1819 { continue; }
1820 block.state = STATE_LOCKED;
1821 block.color = COLOR_GRAY;
1822 block.flame = false;
1823 block.vy = 0;
1824 block.settleTime = 0;
1825 if (block.clusterId != null)
1826 { this.leaveCluster(block); }
1827 }
1828 }
1829};
1830
1831GameCore.prototype.checkGameOver = function ()
1832{
1833 if (this.state !== 'playing')
1834 { return; }
1835 for (var col = 0; col < this.width; col++)
1836 {
1837 if (!this.isColumnLocked(col))
1838 { return; }
1839 }
1840 this.state = 'gameover';
1841 this.gameOverTime = 0;
1842 this.drag = null;
1843};
1844
1845/* ---------- dragging -------------------------------------------------- */
1846
1847GameCore.prototype.beginDrag = function (col, y)
1848{
1849 if (this.state !== 'playing')
1850 { return false; }
1851 var block = this.snapDragBlock(col, y);
1852 if (!block)
1853 { return false; }
1854 var top = this.columnStackTopRow(block.col);
1855 var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS;
1856 this.drag = {
1857 blockId: block.id,
1858 pointerY: y,
1859 startY: y,
1860 accum: 0,
1861 launchIntent: false,
1862 startedAsTop: startedAsTop
1863 };
1864 return true;
1865};
1866
1867/**
1868 * Instant grab, cell-based (t1109). The press is mapped to the grid cell
1869 * floor(col), floor(y); any press inside that cell — corners, padding,
1870 * gaps between sprites, a visually tweened or bouncing sprite — grabs
1871 * the block that occupies it. This uses simulation cells, not the drawn
1872 * sprite bounds. If no block sits in that cell (mid-animation, a block
1873 * whose y is off the integer), fall back to the nearest grabbable block
1874 * in the same column within SNAP_Y (about half a tile). A small
1875 * horizontal slop still consults a neighbouring column when the press
1876 * is near the edge and the primary cell is empty.
1877 */
1878GameCore.prototype.snapDragBlock = function (col, y)
1879{
1880 var raw = col;
1881 var c0 = Math.floor(raw + 1e-9);
1882 var row = Math.floor(y + 1e-9);
1883 var frac = raw - c0;
1884
1885 if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0))
1886 {
1887 var exact = this.draggableInCell(c0, row);
1888 if (exact)
1889 { return exact; }
1890 var near = this.nearestDraggableInColumn(c0, y, SNAP_Y);
1891 if (near)
1892 { return near; }
1893 }
1894
1895 // Primary cell empty: allow a small horizontal slop into a neighbour.
1896 var neighbours = [];
1897 if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)
1898 { neighbours.push(c0 - 1); }
1899 if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)
1900 { neighbours.push(c0 + 1); }
1901 var best = null;
1902 var bestDist = Infinity;
1903 for (var n = 0; n < neighbours.length; n++)
1904 {
1905 var cc = neighbours[n];
1906 if (!this.columnAllowsDrag(cc))
1907 { continue; }
1908 var hit = this.draggableInCell(cc, row) || this.nearestDraggableInColumn(cc, y, SNAP_Y);
1909 if (!hit)
1910 { continue; }
1911 var dist = this.pointerDistToBlock(hit, raw, y);
1912 if (dist < bestDist)
1913 {
1914 bestDist = dist;
1915 best = hit;
1916 }
1917 }
1918 return best;
1919};
1920
1921/** The draggable block that occupies grid cell (col, row), if any. */
1922GameCore.prototype.draggableInCell = function (col, row)
1923{
1924 var best = null;
1925 var bestOverlap = 0;
1926 var list = this.blocksInColumn(col);
1927 for (var i = 0; i < list.length; i++)
1928 {
1929 var block = list[i];
1930 if (!this.isDraggableBlock(block))
1931 { continue; }
1932 var lo = Math.max(block.y, row);
1933 var hi = Math.min(block.y + 1, row + 1);
1934 var overlap = hi - lo;
1935 if (overlap > bestOverlap + 1e-12)
1936 {
1937 bestOverlap = overlap;
1938 best = block;
1939 }
1940 }
1941 if (best && bestOverlap > 1e-9)
1942 { return best; }
1943 return null;
1944};
1945
1946/** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */
1947GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist)
1948{
1949 var best = null;
1950 var bestDist = Infinity;
1951 var list = this.blocksInColumn(col);
1952 for (var i = 0; i < list.length; i++)
1953 {
1954 var block = list[i];
1955 if (!this.isDraggableBlock(block))
1956 { continue; }
1957 var dist = this.pointerDistToBlock(block, col + 0.5, y);
1958 if (dist < bestDist)
1959 {
1960 bestDist = dist;
1961 best = block;
1962 }
1963 }
1964 if (!best || bestDist > maxDist)
1965 { return null; }
1966 return best;
1967};
1968
1969GameCore.prototype.pointerDistToBlock = function (block, col, y)
1970{
1971 var dx;
1972 if (col >= block.col && col < block.col + 1)
1973 { dx = 0; }
1974 else if (col < block.col)
1975 { dx = block.col - col; }
1976 else
1977 { dx = col - (block.col + 1); }
1978 var dy;
1979 if (y >= block.y && y < block.y + 1)
1980 { dy = 0; }
1981 else if (y < block.y)
1982 { dy = block.y - y; }
1983 else
1984 { dy = y - (block.y + 1); }
1985 return Math.sqrt(dx * dx + dy * dy);
1986};
1987
1988GameCore.prototype.moveDrag = function (y)
1989{
1990 if (!this.drag)
1991 { return; }
1992 var block = this.blockById(this.drag.blockId);
1993 if (!block || !this.isDraggableBlock(block))
1994 {
1995 this.drag = null;
1996 return;
1997 }
1998 if (!this.columnAllowsDrag(block.col))
1999 {
2000 this.drag = null;
2001 return;
2002 }
2003 this.drag.pointerY = y;
2004 // 1:1 tracking: the target row is the one under the pointer, clamped
2005 // to the still stack. Swap step by step with each neighbour, and stop
2006 // exactly there. When the pointer stops, nothing else moves.
2007 // Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold
2008 // used to jump a full cell on a short pull, and one large event
2009 // could walk the entire column (the "jumps all the way" bug).
2010 var top = this.columnStackTopRow(block.col);
2011 var bottom = this.columnStackBottomRow(block.col);
2012 var target = Math.floor(y + 1e-12);
2013 if (bottom < 0)
2014 { bottom = 0; }
2015 if (target < bottom)
2016 { target = bottom; }
2017 if (target > top)
2018 { target = top; }
2019 this.swapToward(block, target);
2020
2021 top = this.columnStackTopRow(block.col);
2022 var isTop = Math.abs(block.y - top) < ALIGN_EPS;
2023 if (this.drag.startedAsTop)
2024 {
2025 // Already the topmost block: pulling upward past its own position
2026 // (LAUNCH_PULL tiles above the press point) starts a manual jump.
2027 this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL);
2028 }
2029 else
2030 {
2031 // Started mid-stack: swap first, and only launch once the pointer
2032 // is pulled into the empty space above the (new) top.
2033 this.drag.launchIntent = isTop && (y > top + 1 + 0.25);
2034 }
2035};
2036
2037/** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */
2038GameCore.prototype.swapOne = function (block, dir)
2039{
2040 var neighbor = this.blockAt(block.col, block.y + dir);
2041 if (neighbor && this.isGrounded(neighbor) && neighbor !== block)
2042 {
2043 this.swapBlocks(block, neighbor);
2044 return true;
2045 }
2046 return false;
2047};
2048
2049GameCore.prototype.endDrag = function ()
2050{
2051 if (!this.drag)
2052 { return; }
2053 var block = this.blockById(this.drag.blockId);
2054 var launch = this.drag.launchIntent;
2055 this.drag = null;
2056 if (launch && block && block.state === STATE_STILL)
2057 { this.startManualLaunch(block); }
2058 else
2059 { this.resolveMatches(); }
2060};
2061
2062GameCore.prototype.blockById = function (id)
2063{
2064 for (var i = 0; i < this.blocks.length; i++)
2065 {
2066 if (this.blocks[i].id === id)
2067 { return this.blocks[i]; }
2068 }
2069 return null;
2070};
2071
2072GameCore.prototype.columnStackTopRow = function (col)
2073{
2074 var top = -1;
2075 var list = this.blocksInColumn(col);
2076 for (var i = 0; i < list.length; i++)
2077 {
2078 if (this.isGrounded(list[i]) && list[i].y > top)
2079 { top = list[i].y; }
2080 }
2081 return top;
2082};
2083
2084GameCore.prototype.columnStackBottomRow = function (col)
2085{
2086 var bottom = Infinity;
2087 var list = this.blocksInColumn(col);
2088 for (var i = 0; i < list.length; i++)
2089 {
2090 if (this.isGrounded(list[i]) && list[i].y < bottom)
2091 { bottom = list[i].y; }
2092 }
2093 return bottom === Infinity ? 0 : bottom;
2094};
2095
2096GameCore.prototype.swapToward = function (block, targetY)
2097{
2098 var guard = 0;
2099 while (guard++ < this.height + 2)
2100 {
2101 var current = block.y;
2102 if (targetY > current + 0.5)
2103 {
2104 var above = this.blockAt(block.col, current + 1);
2105 if (above && this.isGrounded(above) && above !== block)
2106 { this.swapBlocks(block, above); }
2107 else
2108 { break; }
2109 }
2110 else if (targetY < current - 0.5)
2111 {
2112 var below = this.blockAt(block.col, current - 1);
2113 if (below && this.isGrounded(below) && below !== block)
2114 { this.swapBlocks(block, below); }
2115 else
2116 { break; }
2117 }
2118 else
2119 { break; }
2120 }
2121};
2122
2123GameCore.prototype.swapBlocks = function (a, b)
2124{
2125 var y = a.y;
2126 a.y = b.y;
2127 b.y = y;
2128 this.transferLandedMembership(a, b);
2129};
2130
2131/**
2132 * A landed cluster (grey rockets or linked cargo resting on the ground)
2133 * is a swappable part of the contiguous grounded stack. Flying, falling
2134 * and manual blocks are not. A locked column is not.
2135 */
2136GameCore.prototype.isDraggableBlock = function (block)
2137{
2138 if (!block)
2139 { return false; }
2140 if (block.state === STATE_LOCKED || block.state === STATE_FLYING
2141 || block.state === STATE_FALLING || block.state === STATE_MANUAL)
2142 { return false; }
2143 if (block.clusterId != null)
2144 {
2145 var cluster = this.clusters[block.clusterId];
2146 // Flying (rising / descending) clusters stay non-draggable.
2147 if (cluster && cluster.phase !== 'landed')
2148 { return false; }
2149 }
2150 return block.state === STATE_STILL || block.state === STATE_SETTLING;
2151};
2152
2153/**
2154 * The grounded contiguous stack in this column can be dragged, even if a
2155 * flying cluster or a falling spawn occupies the air above it. Only a
2156 * locked column is blocked as a whole.
2157 */
2158GameCore.prototype.columnAllowsDrag = function (col)
2159{
2160 return !this.isColumnLocked(col);
2161};
2162
2163GameCore.prototype.landedClusterId = function (block)
2164{
2165 if (!block || block.clusterId == null)
2166 { return null; }
2167 var cluster = this.clusters[block.clusterId];
2168 if (!cluster || cluster.phase !== 'landed')
2169 { return null; }
2170 return block.clusterId;
2171};
2172
2173/**
2174 * After a swap: leaving a landed cluster clears membership; joining one
2175 * makes the block part of that rigid group. Same-cluster swaps only
2176 * exchange positions. See docs/ASSUMPTIONS.md (t1107).
2177 */
2178GameCore.prototype.transferLandedMembership = function (a, b)
2179{
2180 var ca = this.landedClusterId(a);
2181 var cb = this.landedClusterId(b);
2182 if (ca === cb)
2183 { return; }
2184 // Join first so leaveCluster does not delete a cluster that still
2185 // has the incoming member.
2186 this.applyLandedMembership(b, ca);
2187 this.applyLandedMembership(a, cb);
2188};
2189
2190GameCore.prototype.applyLandedMembership = function (block, clusterId)
2191{
2192 if (clusterId == null)
2193 {
2194 var wasGrey = block.color === COLOR_GRAY;
2195 var wasSettling = block.state === STATE_SETTLING;
2196 this.leaveCluster(block);
2197 if (wasGrey && wasSettling)
2198 {
2199 // Keep settling and recolor on its own, same remaining timer.
2200 block.state = STATE_SETTLING;
2201 }
2202 else if (wasSettling)
2203 {
2204 block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
2205 }
2206 return;
2207 }
2208 if (!this.clusters[clusterId])
2209 { return; }
2210 block.clusterId = clusterId;
2211 block.flame = false;
2212 if (block.state === STATE_STILL || block.state === STATE_SETTLING)
2213 { block.state = STATE_SETTLING; }
2214};
2215
2216/**
2217 * Test helper: stepwise-swap the block at (col, fromRow) to toRow.
2218 * Returns the moved block, or null if the column does not allow a drag.
2219 */
2220GameCore.prototype.swapTo = function (col, fromRow, toRow)
2221{
2222 if (!this.columnAllowsDrag(col))
2223 { return null; }
2224 var block = this.blockAt(col, fromRow);
2225 if (!block)
2226 { return null; }
2227 this.swapToward(block, toRow);
2228 return block;
2229};
2230
2231/**
2232 * Test helper: a compact picture of grounded/falling/flying blocks.
2233 * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling,
2234 * L locked, f falling, m manual, . empty. Flying colored blocks use
2235 * their color with a marker in `flying`.
2236 */
2237GameCore.prototype.snapshot = function ()
2238{
2239 var grid = [];
2240 for (var r = 0; r < this.height; r++)
2241 {
2242 var row = [];
2243 for (var c = 0; c < this.width; c++)
2244 { row.push('.'); }
2245 grid.push(row);
2246 }
2247 var airborne = [];
2248 for (var i = 0; i < this.blocks.length; i++)
2249 {
2250 var block = this.blocks[i];
2251 var ch;
2252 if (block.state === STATE_LOCKED)
2253 { ch = 'L'; }
2254 else if (block.color === COLOR_GRAY)
2255 { ch = 'g'; }
2256 else
2257 { ch = String(block.color); }
2258 if (this.isGrounded(block) || block.state === STATE_FALLING)
2259 {
2260 var row = Math.round(block.y);
2261 if (row >= 0 && row < this.height)
2262 { grid[row][block.col] = ch; }
2263 }
2264 else
2265 {
2266 airborne.push({
2267 id: block.id,
2268 col: block.col,
2269 y: block.y,
2270 color: block.color,
2271 state: block.state,
2272 clusterId: block.clusterId,
2273 flame: block.flame,
2274 vy: block.vy
2275 });
2276 }
2277 }
2278 var lines = [];
2279 for (var r = this.height - 1; r >= 0; r--)
2280 { lines.push(grid[r].join('')); }
2281 return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state };
2282};
2283
2284var GameCoreAPI = {
2285 GameCore: GameCore,
2286 COLOR_GRAY: COLOR_GRAY,
2287 STATE_STILL: STATE_STILL,
2288 STATE_FALLING: STATE_FALLING,
2289 STATE_FLYING: STATE_FLYING,
2290 STATE_SETTLING: STATE_SETTLING,
2291 STATE_LOCKED: STATE_LOCKED,
2292 STATE_MANUAL: STATE_MANUAL
2293};
2294
2295if (typeof module !== 'undefined' && module.exports)
2296{ module.exports = GameCoreAPI; }
2297else if (typeof globalThis !== 'undefined')
2298{
2299 globalThis.GameCore = GameCore;
2300 globalThis.GameCoreAPI = GameCoreAPI;
2301}