chore: rebuild game.js with rigid-body cluster and collision fixes
@@ -721,6 +721,11 @@ function GameCore (planet, rng)
721
721
this.outgoingShots = [];
722
722
// Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
723
723
this.missChance = optsMissChance(arguments);
724
// t1112: invariant checker. strictInvariants throws (tests);
725
// debugInvariants records violations for the ?debug overlay.
726
this.strictInvariants = false;
727
this.debugInvariants = false;
728
this.lastInvariantViolations = [];
724
729
}
725
730
726
731
function optsMissChance (args)
@@ -820,7 +825,12 @@ GameCore.prototype.isGrounded = function (block)
820
825
|| block.state === STATE_LOCKED;
821
826
};
822
827
823
GameCore.prototype.stackTop = function (col, ignore)
828
/**
829
* Highest grounded landing surface in `col`. If `belowY` is given, only
830
* blocks strictly below that y count — a hanging still block above a
831
* gap must never pull a falling or thrown block upward through the gap.
832
*/
833
GameCore.prototype.stackTop = function (col, ignore, belowY)
824
834
{
825
835
var top = 0;
826
836
for (var i = 0; i < this.blocks.length; i++)
@@ -830,6 +840,8 @@ GameCore.prototype.stackTop = function (col, ignore)
830
840
{ continue; }
831
841
if (ignore && ignore(block))
832
842
{ continue; }
843
if (belowY != null && block.y + 1e-9 >= belowY)
844
{ continue; }
833
845
if (this.isGrounded(block))
834
846
{
835
847
var surface = block.y + 1;
@@ -1015,18 +1027,28 @@ GameCore.prototype.clearFlightState = function (block)
1015
1027
1016
1028
GameCore.prototype.releaseUnsupported = function ()
1017
1029
{
1018
for (var i = 0; i < this.blocks.length; i++)
1030
// Repeat until stable: a still sitting on a still that itself just
1031
// became falling must also drop. Two fixed passes are not enough
1032
// for a chain of hanging blocks (t1112).
1033
var guard = 0;
1034
var changed = true;
1035
while (changed && guard++ < this.height + 2)
1019
1036
{
1020
var block = this.blocks[i];
1021
if (block.state !== STATE_STILL)
1022
{ continue; }
1023
if (this.drag && this.drag.blockId === block.id)
1024
{ continue; }
1025
if (this.hasSupport(block))
1026
{ continue; }
1027
this.leaveCluster(block);
1028
this.clearFlightState(block);
1029
block.state = STATE_FALLING;
1037
changed = false;
1038
for (var i = 0; i < this.blocks.length; i++)
1039
{
1040
var block = this.blocks[i];
1041
if (block.state !== STATE_STILL)
1042
{ continue; }
1043
if (this.drag && this.drag.blockId === block.id)
1044
{ continue; }
1045
if (this.hasSupport(block))
1046
{ continue; }
1047
this.leaveCluster(block);
1048
this.clearFlightState(block);
1049
block.state = STATE_FALLING;
1050
changed = true;
1051
}
1030
1052
}
1031
1053
};
1032
1054
@@ -1062,24 +1084,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
1062
1084
var cluster = this.clusters[flyer.clusterId];
1063
1085
if (!cluster || cluster.phase === 'landed')
1064
1086
{ return false; }
1065
var roof = flyer.y + 1;
1066
for (var i = 0; i < this.blocks.length; i++)
1087
this.prepareClusterBody(cluster);
1088
var maxOff = null;
1089
var members = this.clusterMembers(cluster.id);
1090
for (var i = 0; i < members.length; i++)
1067
1091
{
1068
var other = this.blocks[i];
1069
if (other === block || other.col !== block.col)
1092
if (members[i].col !== block.col)
1070
1093
{ continue; }
1071
if (other.clusterId !== cluster.id)
1072
{ continue; }
1073
if (other.y + 1 > roof)
1074
{ roof = other.y + 1; }
1094
if (maxOff == null || members[i].clusterOffset > maxOff)
1095
{ maxOff = members[i].clusterOffset; }
1075
1096
}
1076
var oldN = this.clusterMembers(cluster.id).length;
1097
var oldN = members.length;
1077
1098
var fallingVy = block.vy;
1078
block.y = roof;
1099
if (block.clusterId != null && block.clusterId !== cluster.id)
1100
{ this.leaveCluster(block); }
1079
1101
block.state = STATE_FLYING;
1080
1102
block.clusterId = cluster.id;
1103
block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1;
1081
1104
block.flame = false;
1082
1105
block.settleTime = 0;
1106
block.manualPhase = null;
1107
block.manualTarget = null;
1108
block.manualDecel = 0;
1109
block.pushedClusters = null;
1083
1110
// A cluster born from a thrown block averages in falling cargo
1084
1111
// the same way two flying clusters merge (t1108). Other rockets
1085
1112
// keep their velocity and just pick the cargo up.
@@ -1090,12 +1117,8 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
1090
1117
cluster.decel = ((cluster.decel || 0) * oldN) / newN;
1091
1118
cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
1092
1119
cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';
1093
var members = this.clusterMembers(cluster.id);
1094
for (var m = 0; m < members.length; m++)
1095
{ members[m].vy = cluster.vy; }
1096
1120
}
1097
else
1098
{ block.vy = cluster.vy; }
1121
this.syncClusterMembers(cluster);
1099
1122
return true;
1100
1123
};
1101
1124
@@ -1153,12 +1176,14 @@ GameCore.prototype.tick = function (dt)
1153
1176
while (left > 1e-12)
1154
1177
{
1155
1178
var step = Math.min(SUBSTEP, left);
1179
this.snapshotPrevY();
1156
1180
this.integrateFalling(step);
1157
1181
this.resolveFlyingMatches();
1158
1182
this.integrateClusters(step);
1159
1183
this.mergeFlyingClusters();
1160
1184
this.integrateManual(step);
1161
1185
this.mergeAnyManualFallingOverlaps();
1186
this.resolveAirborneCollisions();
1162
1187
this.cullOffTop();
1163
1188
left -= step;
1164
1189
}
@@ -1175,6 +1200,7 @@ GameCore.prototype.tick = function (dt)
1175
1200
{ this.resolveMatches(); }
1176
1201
this.checkLocks();
1177
1202
this.checkGameOver();
1203
this.assertInvariants('tick');
1178
1204
};
1179
1205
1180
1206
/** Step a stretch of time in fixed substeps. Useful for tests. */
@@ -1252,7 +1278,7 @@ GameCore.prototype.integrateFalling = function (dt)
1252
1278
if (this.attachToFlyingCluster(block, flyer))
1253
1279
{ continue; }
1254
1280
}
1255
var top = this.stackTop(block.col);
1281
var top = this.stackTop(block.col, null, block.y);
1256
1282
var below = this.highestFallingBelow(block);
1257
1283
if (below && below.y + 1 > top)
1258
1284
{ top = below.y + 1; }
@@ -1366,14 +1392,12 @@ GameCore.prototype.integrateClusters = function (dt)
1366
1392
if (cluster.phase === 'landed' || cluster.phase === 'hold')
1367
1393
{ continue; }
1368
1394
1395
this.prepareClusterBody(cluster);
1369
1396
var dy = this.advanceJump(cluster, dt);
1370
for (var i = 0; i < members.length; i++)
1371
{
1372
members[i].y += dy;
1373
members[i].vy = cluster.vy;
1374
}
1397
cluster.anchorY += dy;
1398
this.syncClusterMembers(cluster);
1375
1399
1376
this.catchFallingOntoCluster(cluster, members);
1400
this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id));
1377
1401
this.checkChainRelaunch(cluster);
1378
1402
1379
1403
if (cluster.phase === 'down' && this.clusters[cluster.id])
@@ -1442,13 +1466,30 @@ GameCore.prototype.flyingClustersOverlap = function (a, b)
1442
1466
{
1443
1467
if (ma[i].col !== mb[j].col)
1444
1468
{ continue; }
1445
if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9)
1469
if (this.sweptIntervalsOverlap(ma[i], mb[j]))
1446
1470
{ return true; }
1447
1471
}
1448
1472
}
1449
1473
return false;
1450
1474
};
1451
1475
1476
/**
1477
* Swept [y, y+1) of two blocks this substep, using _prevY when present.
1478
* Catches pairs that crossed without sharing a cell at either endpoint.
1479
*/
1480
GameCore.prototype.sweptIntervalsOverlap = function (a, b)
1481
{
1482
var a0 = (a._prevY != null) ? a._prevY : a.y;
1483
var a1 = a.y;
1484
var b0 = (b._prevY != null) ? b._prevY : b.y;
1485
var b1 = b.y;
1486
var aLo = Math.min(a0, a1);
1487
var aHi = Math.max(a0, a1) + 1;
1488
var bLo = Math.min(b0, b1);
1489
var bHi = Math.max(b0, b1) + 1;
1490
return aLo < bHi - 1e-9 && bLo < aHi - 1e-9;
1491
};
1492
1452
1493
GameCore.prototype.mergeClusterGroup = function (clusters)
1453
1494
{
1454
1495
clusters.sort(function (a, b) { return a.id - b.id; });
@@ -1484,7 +1525,7 @@ GameCore.prototype.mergeClusterGroup = function (clusters)
1484
1525
if (clusters[c] !== keep)
1485
1526
{ delete this.clusters[clusters[c].id]; }
1486
1527
}
1487
this.packClusterOverlaps(keep);
1528
this.prepareClusterBody(keep);
1488
1529
this.checkChainRelaunch(keep);
1489
1530
if (keep.phase === 'down' && this.clusters[keep.id])
1490
1531
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
@@ -1515,6 +1556,297 @@ GameCore.prototype.packClusterOverlaps = function (cluster)
1515
1556
}
1516
1557
};
1517
1558
1559
1560
/**
1561
* Derive integer cell offsets from current member positions and a
1562
* single anchorY, packing overlaps in each column while preserving
1563
* order (lower y stays lower; ties break on id). Then sync.
1564
* Flying clusters are one rigid body: one y, one vy, integer offsets.
1565
*/
1566
GameCore.prototype.prepareClusterBody = function (cluster)
1567
{
1568
if (!cluster)
1569
{ return; }
1570
var members = this.clusterMembers(cluster.id);
1571
if (!members.length)
1572
{ return; }
1573
this.rebuildClusterOffsets(cluster, members);
1574
};
1575
1576
GameCore.prototype.rebuildClusterOffsets = function (cluster, members)
1577
{
1578
members = members || this.clusterMembers(cluster.id);
1579
if (!members.length)
1580
{ return; }
1581
var byCol = Object.create(null);
1582
for (var i = 0; i < members.length; i++)
1583
{
1584
var col = members[i].col;
1585
if (!byCol[col])
1586
{ byCol[col] = []; }
1587
byCol[col].push(members[i]);
1588
}
1589
var minY = Infinity;
1590
for (var i = 0; i < members.length; i++)
1591
{
1592
if (members[i].y < minY)
1593
{ minY = members[i].y; }
1594
}
1595
if (cluster.anchorY == null || !isFinite(cluster.anchorY))
1596
{ cluster.anchorY = minY; }
1597
var cols = Object.keys(byCol);
1598
for (var c = 0; c < cols.length; c++)
1599
{
1600
var list = byCol[cols[c]];
1601
list.sort(function (a, b)
1602
{
1603
if (a.y !== b.y)
1604
{ return a.y - b.y; }
1605
return a.id - b.id;
1606
});
1607
var prevOff = -Infinity;
1608
for (var i = 0; i < list.length; i++)
1609
{
1610
var off = Math.round(list[i].y - cluster.anchorY);
1611
if (off <= prevOff)
1612
{ off = prevOff + 1; }
1613
list[i].clusterOffset = off;
1614
prevOff = off;
1615
}
1616
}
1617
// Re-base so the lowest offset is 0, keeping relative integers.
1618
var minOff = Infinity;
1619
for (var i = 0; i < members.length; i++)
1620
{
1621
if (members[i].clusterOffset < minOff)
1622
{ minOff = members[i].clusterOffset; }
1623
}
1624
if (minOff !== 0 && isFinite(minOff))
1625
{
1626
cluster.anchorY += minOff;
1627
for (var i = 0; i < members.length; i++)
1628
{ members[i].clusterOffset -= minOff; }
1629
}
1630
this.syncClusterMembers(cluster);
1631
};
1632
1633
GameCore.prototype.syncClusterMembers = function (cluster)
1634
{
1635
if (!cluster)
1636
{ return; }
1637
var members = this.clusterMembers(cluster.id);
1638
if (cluster.anchorY == null)
1639
{
1640
var minY = Infinity;
1641
for (var i = 0; i < members.length; i++)
1642
{
1643
if (members[i].y < minY)
1644
{ minY = members[i].y; }
1645
}
1646
cluster.anchorY = isFinite(minY) ? minY : 0;
1647
}
1648
for (var i = 0; i < members.length; i++)
1649
{
1650
var block = members[i];
1651
if (block.clusterOffset == null)
1652
{ block.clusterOffset = Math.round(block.y - cluster.anchorY); }
1653
block.y = cluster.anchorY + block.clusterOffset;
1654
if (cluster.phase !== 'landed')
1655
{ block.vy = cluster.vy; }
1656
}
1657
};
1658
1659
GameCore.prototype.snapshotPrevY = function ()
1660
{
1661
for (var i = 0; i < this.blocks.length; i++)
1662
{ this.blocks[i]._prevY = this.blocks[i].y; }
1663
};
1664
1665
/**
1666
* Safety net after the specific integrates: any remaining mid-air
1667
* overlap or swept crossing is resolved with the same rules, then
1668
* every flying cluster is rebuilt as a rigid body. Repeats a few
1669
* times because one attach can create a new contact.
1670
*/
1671
GameCore.prototype.resolveAirborneCollisions = function ()
1672
{
1673
var guard = 0;
1674
while (guard++ < this.height + 4)
1675
{
1676
var changed = false;
1677
1678
// Manual vs falling / cluster / manual.
1679
var manuals = [];
1680
for (var i = 0; i < this.blocks.length; i++)
1681
{
1682
if (this.blocks[i].state === STATE_MANUAL)
1683
{ manuals.push(this.blocks[i]); }
1684
}
1685
manuals.sort(function (a, b) { return b.y - a.y; });
1686
for (var m = 0; m < manuals.length; m++)
1687
{
1688
var manual = manuals[m];
1689
if (manual.state !== STATE_MANUAL)
1690
{ continue; }
1691
var oldY = (manual._prevY != null) ? manual._prevY : manual.y;
1692
if (this.resolveManualCollisions(manual, oldY, manual.y))
1693
{ changed = true; }
1694
}
1695
1696
// Falling vs flying (sit on the roof).
1697
for (var i = 0; i < this.blocks.length; i++)
1698
{
1699
var block = this.blocks[i];
1700
if (block.state !== STATE_FALLING)
1701
{ continue; }
1702
var flyer = this.highestFlyingBelow(block);
1703
if (!flyer)
1704
{
1705
// Also a flyer whose swept path crosses this falling block.
1706
flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y);
1707
}
1708
if (flyer && this.sweptIntervalsOverlap(block, flyer))
1709
{
1710
if (this.attachToFlyingCluster(block, flyer))
1711
{ changed = true; }
1712
}
1713
}
1714
1715
// Cluster vs cluster (swept, including after a column push).
1716
var before = Object.keys(this.clusters).length;
1717
this.mergeFlyingClusters();
1718
if (Object.keys(this.clusters).length !== before)
1719
{ changed = true; }
1720
1721
// Rebuild every flying body so offsets stay integer and unique.
1722
var ids = Object.keys(this.clusters);
1723
for (var n = 0; n < ids.length; n++)
1724
{
1725
var cluster = this.clusters[ids[n]];
1726
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1727
{ continue; }
1728
this.prepareClusterBody(cluster);
1729
}
1730
1731
if (!changed)
1732
{ break; }
1733
}
1734
};
1735
1736
/**
1737
* Invariant checker (t1112). No two blocks occupy the same cell or
1738
* overlap their [y, y+1) intervals. Flying-cluster members are
1739
* grid-aligned to a single anchor. Returns a list of violation
1740
* strings (empty if clean).
1741
*/
1742
GameCore.prototype.checkInvariants = function ()
1743
{
1744
var issues = [];
1745
var byCol = Object.create(null);
1746
var ids = Object.create(null);
1747
for (var i = 0; i < this.blocks.length; i++)
1748
{
1749
var block = this.blocks[i];
1750
if (ids[block.id])
1751
{ issues.push('duplicate id ' + block.id); }
1752
ids[block.id] = true;
1753
if (!byCol[block.col])
1754
{ byCol[block.col] = []; }
1755
byCol[block.col].push(block);
1756
if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet
1757
&& block.color >= this.planet.colors.length))
1758
{ issues.push('block ' + block.id + ' has invalid color ' + block.color); }
1759
}
1760
var cols = Object.keys(byCol);
1761
for (var c = 0; c < cols.length; c++)
1762
{
1763
var list = byCol[cols[c]];
1764
list.sort(function (a, b)
1765
{
1766
if (a.y !== b.y)
1767
{ return a.y - b.y; }
1768
return a.id - b.id;
1769
});
1770
for (var i = 0; i < list.length; i++)
1771
{
1772
var a = list[i];
1773
if (i > 0)
1774
{
1775
var prev = list[i - 1];
1776
if (a.y < prev.y + 1 - 1e-6)
1777
{
1778
issues.push('overlap col=' + a.col
1779
+ ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)
1780
+ ' gap=' + (a.y - prev.y));
1781
}
1782
var cellA = Math.floor(a.y + 1e-9);
1783
var cellP = Math.floor(prev.y + 1e-9);
1784
if (cellA === cellP)
1785
{
1786
issues.push('duplicate cell col=' + a.col + ' row=' + cellA
1787
+ ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a));
1788
}
1789
}
1790
}
1791
}
1792
var cids = Object.keys(this.clusters);
1793
for (var n = 0; n < cids.length; n++)
1794
{
1795
var cluster = this.clusters[cids[n]];
1796
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1797
{ continue; }
1798
var mem = this.clusterMembers(cluster.id);
1799
var seenOff = Object.create(null);
1800
for (var i = 0; i < mem.length; i++)
1801
{
1802
var b = mem[i];
1803
if (cluster.anchorY != null && b.clusterOffset != null)
1804
{
1805
var expected = cluster.anchorY + b.clusterOffset;
1806
if (Math.abs(b.y - expected) > 1e-6)
1807
{
1808
issues.push('cluster ' + cluster.id + ' member ' + b.id
1809
+ ' y=' + b.y + ' != anchor+off ' + expected);
1810
}
1811
if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9)
1812
{
1813
issues.push('cluster ' + cluster.id + ' member ' + b.id
1814
+ ' non-integer offset ' + b.clusterOffset);
1815
}
1816
var key = b.col + ':' + b.clusterOffset;
1817
if (seenOff[key])
1818
{
1819
issues.push('cluster ' + cluster.id + ' duplicate offset ' + key);
1820
}
1821
seenOff[key] = true;
1822
}
1823
}
1824
}
1825
return issues;
1826
};
1827
1828
GameCore.prototype._invBlock = function (b)
1829
{
1830
return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y='
1831
+ (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')';
1832
};
1833
1834
GameCore.prototype.assertInvariants = function (label)
1835
{
1836
if (!this.strictInvariants && !this.debugInvariants)
1837
{ return; }
1838
var issues = this.checkInvariants();
1839
this.lastInvariantViolations = issues;
1840
if (!issues.length)
1841
{ return; }
1842
var msg = 'physics invariants failed'
1843
+ (label ? ' at ' + label : '')
1844
+ ':\n ' + issues.join('\n ')
1845
+ '\n' + (this.snapshot ? this.snapshot().picture : '');
1846
if (this.strictInvariants)
1847
{ throw new Error(msg); }
1848
};
1849
1518
1850
GameCore.prototype.tryLandCluster = function (cluster, members)
1519
1851
{
1520
1852
if (!members.length)
@@ -1527,7 +1859,7 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
1527
1859
var top = this.stackTop(block.col, function (other)
1528
1860
{
1529
1861
return other.clusterId === cluster.id;
1530
});
1862
}, block.y);
1531
1863
if (block.y <= top + 1e-9)
1532
1864
{
1533
1865
hit = true;
@@ -1539,9 +1871,10 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
1539
1871
if (!hit)
1540
1872
{ return; }
1541
1873
// Lift the whole rigid body so the deepest contact sits flush.
1542
for (var i = 0; i < members.length; i++)
1543
{ members[i].y += shift; }
1544
this.landCluster(cluster, members);
1874
this.prepareClusterBody(cluster);
1875
cluster.anchorY += shift;
1876
this.syncClusterMembers(cluster);
1877
this.landCluster(cluster, this.clusterMembers(cluster.id));
1545
1878
};
1546
1879
1547
1880
/**
@@ -1642,12 +1975,11 @@ GameCore.prototype.integrateManual = function (dt)
1642
1975
var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
1643
1976
var dy = this.advanceJump(proxy, dt);
1644
1977
var nextY = oldY + dy;
1645
this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));
1646
1978
block.vy = proxy.vy;
1647
1979
block.manualPhase = proxy.phase;
1648
block.y = Math.min(nextY, block.manualTarget);
1649
if (this.tryMergeManualWithFalling(block, oldY, block.y))
1980
if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget)))
1650
1981
{ continue; }
1982
block.y = Math.min(nextY, block.manualTarget);
1651
1983
if (block.y >= block.manualTarget - 1e-12)
1652
1984
{
1653
1985
block.y = block.manualTarget;
@@ -1668,7 +2000,7 @@ GameCore.prototype.integrateManual = function (dt)
1668
2000
var dy = this.advanceJump(proxy, dt);
1669
2001
block.vy = proxy.vy;
1670
2002
var nextY = block.y + dy;
1671
var top = this.stackTop(block.col);
2003
var top = this.stackTop(block.col, null, block.y);
1672
2004
if (nextY <= top)
1673
2005
{
1674
2006
block.y = top;
@@ -1680,9 +2012,10 @@ GameCore.prototype.integrateManual = function (dt)
1680
2012
}
1681
2013
else
1682
2014
{
1683
block.y = Math.min(nextY, this.height - 1);
1684
if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
2015
var downNext = Math.min(nextY, this.height - 1);
2016
if (this.resolveManualCollisions(block, block.y, downNext))
1685
2017
{ continue; }
2018
block.y = downNext;
1686
2019
}
1687
2020
}
1688
2021
}
@@ -1857,7 +2190,7 @@ GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)
1857
2190
}
1858
2191
if (this.drag && this.drag.blockId === manual.id)
1859
2192
{ this.drag = null; }
1860
this.packClusterOverlaps(cluster);
2193
this.prepareClusterBody(cluster);
1861
2194
this.checkChainRelaunch(cluster);
1862
2195
return cluster;
1863
2196
};
@@ -1882,40 +2215,217 @@ GameCore.prototype.mergeAnyManualFallingOverlaps = function ()
1882
2215
}
1883
2216
};
1884
2217
1885
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
2218
/**
2219
* Resolve collisions of a thrown (manual) block along [oldY, nextY].
2220
* Falling blocks merge (t1108). A flying cluster is pushed one cell up
2221
* in the hit column and the shot attaches at the bottom (t1111).
2222
* Another thrown block is promoted to a 1-block cluster and then the
2223
* same attach-at-bottom rule applies. Returns true if `manual` is no
2224
* longer in the MANUAL state.
2225
*/
2226
GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY)
1886
2227
{
1887
if (!manual.pushedClusters)
1888
{ manual.pushedClusters = Object.create(null); }
2228
if (!manual || manual.state !== STATE_MANUAL)
2229
{ return true; }
1889
2230
1890
var manualTopOld = oldY + 1;
1891
var manualTopNew = nextY + 1;
1892
var low = Math.min(oldY, nextY);
1893
var high = Math.max(oldY, nextY) + 1;
2231
var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY);
2232
if (fallingHits.length)
2233
{
2234
this.mergeManualWithFalling(manual, fallingHits);
2235
return manual.state !== STATE_MANUAL;
2236
}
1894
2237
2238
var flyer = this.flyingHitOnManualPath(manual, oldY, nextY);
2239
if (flyer)
2240
{
2241
var cluster = this.clusters[flyer.clusterId];
2242
if (cluster && cluster.phase !== 'landed')
2243
{
2244
if (nextY + 1e-12 < oldY)
2245
{ this.attachToFlyingCluster(manual, flyer); }
2246
else
2247
{ this.attachShotUnderCluster(manual, cluster, manual.col); }
2248
return manual.state !== STATE_MANUAL;
2249
}
2250
}
2251
2252
var other = this.manualHitOnManualPath(manual, oldY, nextY);
2253
if (other)
2254
{
2255
var host = this.promoteManualToCluster(other);
2256
if (host)
2257
{
2258
if (nextY + 1e-12 < oldY)
2259
{ this.attachToFlyingCluster(manual, other); }
2260
else
2261
{ this.attachShotUnderCluster(manual, host, manual.col); }
2262
return manual.state !== STATE_MANUAL;
2263
}
2264
}
2265
return false;
2266
};
2267
2268
/** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */
2269
GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY)
2270
{
2271
var lo = Math.min(oldY, nextY);
2272
var hi = Math.max(oldY, nextY) + 1;
2273
var best = null;
2274
var bestY = Infinity;
1895
2275
for (var i = 0; i < this.blocks.length; i++)
1896
2276
{
1897
2277
var other = this.blocks[i];
1898
if (other === manual)
1899
{ continue; }
1900
if (other.col !== manual.col)
2278
if (other === manual || other.col !== manual.col)
1901
2279
{ continue; }
1902
2280
if (other.state !== STATE_FLYING || other.clusterId == null)
1903
2281
{ continue; }
1904
if (manual.pushedClusters[other.clusterId])
2282
var cluster = this.clusters[other.clusterId];
2283
if (!cluster || cluster.phase === 'landed')
1905
2284
{ continue; }
1906
var otherBot = other.y;
1907
var otherTop = other.y + 1;
1908
var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;
1909
var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;
1910
if (overlapping || crossing)
2285
var o0 = (other._prevY != null) ? other._prevY : other.y;
2286
var o1 = other.y;
2287
var oLo = Math.min(o0, o1);
2288
var oHi = Math.max(o0, o1) + 1;
2289
if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
1911
2290
{
1912
if (this.pushClusterColumn(other.clusterId, manual.col))
2291
if (other.y < bestY)
1913
2292
{
1914
manual.pushedClusters[other.clusterId] = true;
1915
return;
2293
bestY = other.y;
2294
best = other;
1916
2295
}
1917
2296
}
1918
2297
}
2298
return best;
2299
};
2300
2301
/** Another thrown block in the same column on the path (prefer the lower). */
2302
GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY)
2303
{
2304
var lo = Math.min(oldY, nextY);
2305
var hi = Math.max(oldY, nextY) + 1;
2306
var best = null;
2307
for (var i = 0; i < this.blocks.length; i++)
2308
{
2309
var other = this.blocks[i];
2310
if (other === manual || other.col !== manual.col)
2311
{ continue; }
2312
if (other.state !== STATE_MANUAL)
2313
{ continue; }
2314
var o0 = (other._prevY != null) ? other._prevY : other.y;
2315
var o1 = other.y;
2316
var oLo = Math.min(o0, o1);
2317
var oHi = Math.max(o0, o1) + 1;
2318
if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
2319
{
2320
if (!best || other.y < best.y)
2321
{ best = other; }
2322
}
2323
}
2324
return best;
2325
};
2326
2327
/**
2328
* t1111: push every member of `cluster` in `col` up by one cell, then
2329
* attach `manual` at the bottom of that column, grid-aligned, as part
2330
* of the cluster. The cluster keeps its velocity; the shot takes it.
2331
* Other columns are untouched. Same rule for every successive shot.
2332
*/
2333
GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col)
2334
{
2335
if (!manual || !cluster || !this.clusters[cluster.id])
2336
{ return false; }
2337
if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id)
2338
{
2339
// Already belonging elsewhere: leave first.
2340
if (manual.clusterId != null)
2341
{ this.leaveCluster(manual); }
2342
}
2343
this.prepareClusterBody(cluster);
2344
var members = this.clusterMembers(cluster.id);
2345
var minOff = null;
2346
for (var i = 0; i < members.length; i++)
2347
{
2348
if (members[i].col !== col)
2349
{ continue; }
2350
if (minOff == null || members[i].clusterOffset < minOff)
2351
{ minOff = members[i].clusterOffset; }
2352
}
2353
if (minOff == null)
2354
{ minOff = 0; }
2355
else
2356
{
2357
for (var i = 0; i < members.length; i++)
2358
{
2359
if (members[i].col === col)
2360
{ members[i].clusterOffset += 1; }
2361
}
2362
}
2363
if (this.drag && this.drag.blockId === manual.id)
2364
{ this.drag = null; }
2365
manual.clusterId = cluster.id;
2366
manual.clusterOffset = minOff;
2367
manual.state = STATE_FLYING;
2368
manual.flame = manual.color === COLOR_GRAY;
2369
manual.settleTime = 0;
2370
manual.manualPhase = null;
2371
manual.manualTarget = null;
2372
manual.manualDecel = 0;
2373
manual.manualJumpHeight = 0;
2374
manual.pushedClusters = null;
2375
this.syncClusterMembers(cluster);
2376
this.cullOffTop();
2377
if (this.clusters[cluster.id])
2378
{ this.checkChainRelaunch(cluster); }
2379
return true;
2380
};
2381
2382
/**
2383
* Turn a lone thrown block into a 1-block flying cluster so a later
2384
* shot can use the t1111 attach-at-bottom rule. A single throw stays
2385
* MANUAL (and is capped at the top of the field) until it collides.
2386
*/
2387
GameCore.prototype.promoteManualToCluster = function (manual)
2388
{
2389
if (!manual)
2390
{ return null; }
2391
if (manual.state === STATE_FLYING && manual.clusterId != null)
2392
{
2393
var existing = this.clusters[manual.clusterId];
2394
if (existing && existing.phase !== 'landed')
2395
{ return existing; }
2396
}
2397
if (manual.state !== STATE_MANUAL)
2398
{ return null; }
2399
var height = manual.manualJumpHeight != null ? manual.manualJumpHeight
2400
: Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y);
2401
var cluster = {
2402
id: this.nextClusterId++,
2403
jumpHeight: height,
2404
vy: manual.vy,
2405
decel: manual.manualDecel || 0,
2406
phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up',
2407
fromManual: true,
2408
anchorY: manual.y
2409
};
2410
this.clusters[cluster.id] = cluster;
2411
manual.state = STATE_FLYING;
2412
manual.clusterId = cluster.id;
2413
manual.clusterOffset = 0;
2414
manual.flame = manual.color === COLOR_GRAY;
2415
manual.settleTime = 0;
2416
manual.manualPhase = null;
2417
manual.manualTarget = null;
2418
manual.manualDecel = 0;
2419
manual.manualJumpHeight = 0;
2420
manual.pushedClusters = null;
2421
this.syncClusterMembers(cluster);
2422
return cluster;
2423
};
2424
2425
/** Kept for older tests / call sites: push a cluster column by one cell. */
2426
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
2427
{
2428
return this.resolveManualCollisions(manual, oldY, nextY);
1919
2429
};
1920
2430
1921
2431
@@ -2428,6 +2938,7 @@ GameCore.prototype.launchCluster = function (matchedBlocks, height)
2428
2938
block.vy = cluster.vy;
2429
2939
block.settleTime = 0;
2430
2940
}
2941
this.prepareClusterBody(cluster);
2431
2942
return cluster;
2432
2943
};
2433
2944
@@ -3568,6 +4079,7 @@ var app = {
3568
4079
pointerType: null,
3569
4080
activeTouchId: null,
3570
4081
debugDrag: false,
4082
debugPhysics: false,
3571
4083
gameOverShown: false,
3572
4084
selectTween: null,
3573
4085
online: false,
@@ -3581,6 +4093,7 @@ var app = {
3581
4093
window.onload = function ()
3582
4094
{
3583
4095
app.debugDrag = isDragDebugEnabled();
4096
app.debugPhysics = isPhysicsDebugEnabled();
3584
4097
var play = document.getElementById('btn-play');
3585
4098
play.addEventListener('click', onPlay);
3586
4099
play.addEventListener('pointerup', function (e)
@@ -3629,6 +4142,8 @@ function startPlay (opts)
3629
4142
if (typeof miss !== 'number' || !isFinite(miss))
3630
4143
{ miss = planetMod.missChance(app.planet); }
3631
4144
app.core = new GameCore(app.planet, Math.random, miss);
4145
if (app.debugPhysics)
4146
{ app.core.debugInvariants = true; }
3632
4147
app.mode = 'playing';
3633
4148
app.online = !!opts.online;
3634
4149
app.gameOverShown = false;
@@ -4001,6 +4516,8 @@ function tickView (dt)
4001
4516
syncSprites();
4002
4517
refreshHud();
4003
4518
tickOnline(dt);
4519
if (app.debugPhysics)
4520
{ logPhysicsDebug(); }
4004
4521
4005
4522
if (app.core.state === 'gameover' && !app.gameOverShown)
4006
4523
{
@@ -4057,6 +4574,22 @@ function isDragDebugEnabled ()
4057
4574
return false;
4058
4575
}
4059
4576
4577
/** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */
4578
function isPhysicsDebugEnabled ()
4579
{
4580
try
4581
{
4582
var q = window.location && window.location.search;
4583
if (q && /(?:\?|&)debug(?:=|&|$)/.test(q))
4584
{ return true; }
4585
if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1'
4586
|| window.localStorage.getItem('meteors-debug-drag') === '1'))
4587
{ return true; }
4588
}
4589
catch (err) { /* ignore */ }
4590
return false;
4591
}
4592
4060
4593
function eventClient (e)
4061
4594
{
4062
4595
var t = null;
@@ -4366,6 +4899,42 @@ function onTouchCancel (e)
4366
4899
finishDrag({ noLaunch: true });
4367
4900
}
4368
4901
4902
function logPhysicsDebug ()
4903
{
4904
if (!app.debugPhysics || !app.core)
4905
{ return; }
4906
ensureDragDebugOverlay();
4907
var el = document.getElementById('drag-debug');
4908
if (!el)
4909
{ return; }
4910
var issues = app.core.lastInvariantViolations || [];
4911
var flying = 0;
4912
var manuals = 0;
4913
var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0;
4914
for (var i = 0; i < app.core.blocks.length; i++)
4915
{
4916
if (app.core.blocks[i].state === 'flying')
4917
{ flying++; }
4918
if (app.core.blocks[i].state === 'manual')
4919
{ manuals++; }
4920
}
4921
var status = issues.length
4922
? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n'))
4923
: ('physics ok blocks=' + app.core.blocks.length
4924
+ ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters);
4925
// Keep any live drag line above the physics status.
4926
var dragLine = '';
4927
if (app.debugDrag && lastDragDebug)
4928
{
4929
dragLine = el.textContent && el.textContent.indexOf('drag ') === 0
4930
? el.textContent.split('\n')[0] + '\n'
4931
: '';
4932
}
4933
el.textContent = dragLine + status;
4934
el.hidden = false;
4935
el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8';
4936
}
4937
4369
4938
function ensureDragDebugOverlay ()
4370
4939
{
4371
4940
if (document.getElementById('drag-debug'))
@@ -721,6 +721,11 @@ function GameCore (planet, rng)721
this.outgoingShots = [];721
this.outgoingShots = [];722
// Stage-2 default; stage 3 replaces this with Planet.missChance (Power).722
// Stage-2 default; stage 3 replaces this with Planet.missChance (Power).723
this.missChance = optsMissChance(arguments);723
this.missChance = optsMissChance(arguments);724
// t1112: invariant checker. strictInvariants throws (tests);725
// debugInvariants records violations for the ?debug overlay.726
this.strictInvariants = false;727
this.debugInvariants = false;728
this.lastInvariantViolations = [];724
}729
}725
730
726
function optsMissChance (args)731
function optsMissChance (args)@@ -820,7 +825,12 @@ GameCore.prototype.isGrounded = function (block)820
|| block.state === STATE_LOCKED;825
|| block.state === STATE_LOCKED;821
};826
};822
827
823
GameCore.prototype.stackTop = function (col, ignore)828
/**829
* Highest grounded landing surface in `col`. If `belowY` is given, only830
* blocks strictly below that y count — a hanging still block above a831
* gap must never pull a falling or thrown block upward through the gap.832
*/833
GameCore.prototype.stackTop = function (col, ignore, belowY)824
{834
{825
var top = 0;835
var top = 0;826
for (var i = 0; i < this.blocks.length; i++)836
for (var i = 0; i < this.blocks.length; i++)@@ -830,6 +840,8 @@ GameCore.prototype.stackTop = function (col, ignore)830
{ continue; }840
{ continue; }831
if (ignore && ignore(block))841
if (ignore && ignore(block))832
{ continue; }842
{ continue; }843
if (belowY != null && block.y + 1e-9 >= belowY)844
{ continue; }833
if (this.isGrounded(block))845
if (this.isGrounded(block))834
{846
{835
var surface = block.y + 1;847
var surface = block.y + 1;@@ -1015,18 +1027,28 @@ GameCore.prototype.clearFlightState = function (block)1015
1027
1016
GameCore.prototype.releaseUnsupported = function ()1028
GameCore.prototype.releaseUnsupported = function ()1017
{1029
{1018
for (var i = 0; i < this.blocks.length; i++)1030
// Repeat until stable: a still sitting on a still that itself just1031
// became falling must also drop. Two fixed passes are not enough1032
// for a chain of hanging blocks (t1112).1033
var guard = 0;1034
var changed = true;1035
while (changed && guard++ < this.height + 2)1019
{1036
{1020
var block = this.blocks[i];1037
changed = false;1021
if (block.state !== STATE_STILL)1038
for (var i = 0; i < this.blocks.length; i++)1022
{ continue; }1039
{1023
if (this.drag && this.drag.blockId === block.id)1040
var block = this.blocks[i];1024
{ continue; }1041
if (block.state !== STATE_STILL)1025
if (this.hasSupport(block))1042
{ continue; }1026
{ continue; }1043
if (this.drag && this.drag.blockId === block.id)1027
this.leaveCluster(block);1044
{ continue; }1028
this.clearFlightState(block);1045
if (this.hasSupport(block))1029
block.state = STATE_FALLING;1046
{ continue; }1047
this.leaveCluster(block);1048
this.clearFlightState(block);1049
block.state = STATE_FALLING;1050
changed = true;1051
}1030
}1052
}1031
};1053
};1032
1054
@@ -1062,24 +1084,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)1062
var cluster = this.clusters[flyer.clusterId];1084
var cluster = this.clusters[flyer.clusterId];1063
if (!cluster || cluster.phase === 'landed')1085
if (!cluster || cluster.phase === 'landed')1064
{ return false; }1086
{ return false; }1065
var roof = flyer.y + 1;1087
this.prepareClusterBody(cluster);1066
for (var i = 0; i < this.blocks.length; i++)1088
var maxOff = null;1089
var members = this.clusterMembers(cluster.id);1090
for (var i = 0; i < members.length; i++)1067
{1091
{1068
var other = this.blocks[i];1092
if (members[i].col !== block.col)1069
if (other === block || other.col !== block.col)1070
{ continue; }1093
{ continue; }1071
if (other.clusterId !== cluster.id)1094
if (maxOff == null || members[i].clusterOffset > maxOff)1072
{ continue; }1095
{ maxOff = members[i].clusterOffset; }1073
if (other.y + 1 > roof)1074
{ roof = other.y + 1; }1075
}1096
}1076
var oldN = this.clusterMembers(cluster.id).length;1097
var oldN = members.length;1077
var fallingVy = block.vy;1098
var fallingVy = block.vy;1078
block.y = roof;1099
if (block.clusterId != null && block.clusterId !== cluster.id)1100
{ this.leaveCluster(block); }1079
block.state = STATE_FLYING;1101
block.state = STATE_FLYING;1080
block.clusterId = cluster.id;1102
block.clusterId = cluster.id;1103
block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1;1081
block.flame = false;1104
block.flame = false;1082
block.settleTime = 0;1105
block.settleTime = 0;1106
block.manualPhase = null;1107
block.manualTarget = null;1108
block.manualDecel = 0;1109
block.pushedClusters = null;1083
// A cluster born from a thrown block averages in falling cargo1110
// A cluster born from a thrown block averages in falling cargo1084
// the same way two flying clusters merge (t1108). Other rockets1111
// the same way two flying clusters merge (t1108). Other rockets1085
// keep their velocity and just pick the cargo up.1112
// keep their velocity and just pick the cargo up.@@ -1090,12 +1117,8 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)1090
cluster.decel = ((cluster.decel || 0) * oldN) / newN;1117
cluster.decel = ((cluster.decel || 0) * oldN) / newN;1091
cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;1118
cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;1092
cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';1119
cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';1093
var members = this.clusterMembers(cluster.id);1094
for (var m = 0; m < members.length; m++)1095
{ members[m].vy = cluster.vy; }1096
}1120
}1097
else1121
this.syncClusterMembers(cluster);1098
{ block.vy = cluster.vy; }1099
return true;1122
return true;1100
};1123
};1101
1124
@@ -1153,12 +1176,14 @@ GameCore.prototype.tick = function (dt)1153
while (left > 1e-12)1176
while (left > 1e-12)1154
{1177
{1155
var step = Math.min(SUBSTEP, left);1178
var step = Math.min(SUBSTEP, left);1179
this.snapshotPrevY();1156
this.integrateFalling(step);1180
this.integrateFalling(step);1157
this.resolveFlyingMatches();1181
this.resolveFlyingMatches();1158
this.integrateClusters(step);1182
this.integrateClusters(step);1159
this.mergeFlyingClusters();1183
this.mergeFlyingClusters();1160
this.integrateManual(step);1184
this.integrateManual(step);1161
this.mergeAnyManualFallingOverlaps();1185
this.mergeAnyManualFallingOverlaps();1186
this.resolveAirborneCollisions();1162
this.cullOffTop();1187
this.cullOffTop();1163
left -= step;1188
left -= step;1164
}1189
}@@ -1175,6 +1200,7 @@ GameCore.prototype.tick = function (dt)1175
{ this.resolveMatches(); }1200
{ this.resolveMatches(); }1176
this.checkLocks();1201
this.checkLocks();1177
this.checkGameOver();1202
this.checkGameOver();1203
this.assertInvariants('tick');1178
};1204
};1179
1205
1180
/** Step a stretch of time in fixed substeps. Useful for tests. */1206
/** Step a stretch of time in fixed substeps. Useful for tests. */@@ -1252,7 +1278,7 @@ GameCore.prototype.integrateFalling = function (dt)1252
if (this.attachToFlyingCluster(block, flyer))1278
if (this.attachToFlyingCluster(block, flyer))1253
{ continue; }1279
{ continue; }1254
}1280
}1255
var top = this.stackTop(block.col);1281
var top = this.stackTop(block.col, null, block.y);1256
var below = this.highestFallingBelow(block);1282
var below = this.highestFallingBelow(block);1257
if (below && below.y + 1 > top)1283
if (below && below.y + 1 > top)1258
{ top = below.y + 1; }1284
{ top = below.y + 1; }@@ -1366,14 +1392,12 @@ GameCore.prototype.integrateClusters = function (dt)1366
if (cluster.phase === 'landed' || cluster.phase === 'hold')1392
if (cluster.phase === 'landed' || cluster.phase === 'hold')1367
{ continue; }1393
{ continue; }1368
1394
1395
this.prepareClusterBody(cluster);1369
var dy = this.advanceJump(cluster, dt);1396
var dy = this.advanceJump(cluster, dt);1370
for (var i = 0; i < members.length; i++)1397
cluster.anchorY += dy;1371
{1398
this.syncClusterMembers(cluster);1372
members[i].y += dy;1373
members[i].vy = cluster.vy;1374
}1375
1399
1376
this.catchFallingOntoCluster(cluster, members);1400
this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id));1377
this.checkChainRelaunch(cluster);1401
this.checkChainRelaunch(cluster);1378
1402
1379
if (cluster.phase === 'down' && this.clusters[cluster.id])1403
if (cluster.phase === 'down' && this.clusters[cluster.id])@@ -1442,13 +1466,30 @@ GameCore.prototype.flyingClustersOverlap = function (a, b)1442
{1466
{1443
if (ma[i].col !== mb[j].col)1467
if (ma[i].col !== mb[j].col)1444
{ continue; }1468
{ continue; }1445
if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9)1469
if (this.sweptIntervalsOverlap(ma[i], mb[j]))1446
{ return true; }1470
{ return true; }1447
}1471
}1448
}1472
}1449
return false;1473
return false;1450
};1474
};1451
1475
1476
/**1477
* Swept [y, y+1) of two blocks this substep, using _prevY when present.1478
* Catches pairs that crossed without sharing a cell at either endpoint.1479
*/1480
GameCore.prototype.sweptIntervalsOverlap = function (a, b)1481
{1482
var a0 = (a._prevY != null) ? a._prevY : a.y;1483
var a1 = a.y;1484
var b0 = (b._prevY != null) ? b._prevY : b.y;1485
var b1 = b.y;1486
var aLo = Math.min(a0, a1);1487
var aHi = Math.max(a0, a1) + 1;1488
var bLo = Math.min(b0, b1);1489
var bHi = Math.max(b0, b1) + 1;1490
return aLo < bHi - 1e-9 && bLo < aHi - 1e-9;1491
};1492
1452
GameCore.prototype.mergeClusterGroup = function (clusters)1493
GameCore.prototype.mergeClusterGroup = function (clusters)1453
{1494
{1454
clusters.sort(function (a, b) { return a.id - b.id; });1495
clusters.sort(function (a, b) { return a.id - b.id; });@@ -1484,7 +1525,7 @@ GameCore.prototype.mergeClusterGroup = function (clusters)1484
if (clusters[c] !== keep)1525
if (clusters[c] !== keep)1485
{ delete this.clusters[clusters[c].id]; }1526
{ delete this.clusters[clusters[c].id]; }1486
}1527
}1487
this.packClusterOverlaps(keep);1528
this.prepareClusterBody(keep);1488
this.checkChainRelaunch(keep);1529
this.checkChainRelaunch(keep);1489
if (keep.phase === 'down' && this.clusters[keep.id])1530
if (keep.phase === 'down' && this.clusters[keep.id])1490
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }1531
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }@@ -1515,6 +1556,297 @@ GameCore.prototype.packClusterOverlaps = function (cluster)1515
}1556
}1516
};1557
};1517
1558
1559
1560
/**1561
* Derive integer cell offsets from current member positions and a1562
* single anchorY, packing overlaps in each column while preserving1563
* order (lower y stays lower; ties break on id). Then sync.1564
* Flying clusters are one rigid body: one y, one vy, integer offsets.1565
*/1566
GameCore.prototype.prepareClusterBody = function (cluster)1567
{1568
if (!cluster)1569
{ return; }1570
var members = this.clusterMembers(cluster.id);1571
if (!members.length)1572
{ return; }1573
this.rebuildClusterOffsets(cluster, members);1574
};1575
1576
GameCore.prototype.rebuildClusterOffsets = function (cluster, members)1577
{1578
members = members || this.clusterMembers(cluster.id);1579
if (!members.length)1580
{ return; }1581
var byCol = Object.create(null);1582
for (var i = 0; i < members.length; i++)1583
{1584
var col = members[i].col;1585
if (!byCol[col])1586
{ byCol[col] = []; }1587
byCol[col].push(members[i]);1588
}1589
var minY = Infinity;1590
for (var i = 0; i < members.length; i++)1591
{1592
if (members[i].y < minY)1593
{ minY = members[i].y; }1594
}1595
if (cluster.anchorY == null || !isFinite(cluster.anchorY))1596
{ cluster.anchorY = minY; }1597
var cols = Object.keys(byCol);1598
for (var c = 0; c < cols.length; c++)1599
{1600
var list = byCol[cols[c]];1601
list.sort(function (a, b)1602
{1603
if (a.y !== b.y)1604
{ return a.y - b.y; }1605
return a.id - b.id;1606
});1607
var prevOff = -Infinity;1608
for (var i = 0; i < list.length; i++)1609
{1610
var off = Math.round(list[i].y - cluster.anchorY);1611
if (off <= prevOff)1612
{ off = prevOff + 1; }1613
list[i].clusterOffset = off;1614
prevOff = off;1615
}1616
}1617
// Re-base so the lowest offset is 0, keeping relative integers.1618
var minOff = Infinity;1619
for (var i = 0; i < members.length; i++)1620
{1621
if (members[i].clusterOffset < minOff)1622
{ minOff = members[i].clusterOffset; }1623
}1624
if (minOff !== 0 && isFinite(minOff))1625
{1626
cluster.anchorY += minOff;1627
for (var i = 0; i < members.length; i++)1628
{ members[i].clusterOffset -= minOff; }1629
}1630
this.syncClusterMembers(cluster);1631
};1632
1633
GameCore.prototype.syncClusterMembers = function (cluster)1634
{1635
if (!cluster)1636
{ return; }1637
var members = this.clusterMembers(cluster.id);1638
if (cluster.anchorY == null)1639
{1640
var minY = Infinity;1641
for (var i = 0; i < members.length; i++)1642
{1643
if (members[i].y < minY)1644
{ minY = members[i].y; }1645
}1646
cluster.anchorY = isFinite(minY) ? minY : 0;1647
}1648
for (var i = 0; i < members.length; i++)1649
{1650
var block = members[i];1651
if (block.clusterOffset == null)1652
{ block.clusterOffset = Math.round(block.y - cluster.anchorY); }1653
block.y = cluster.anchorY + block.clusterOffset;1654
if (cluster.phase !== 'landed')1655
{ block.vy = cluster.vy; }1656
}1657
};1658
1659
GameCore.prototype.snapshotPrevY = function ()1660
{1661
for (var i = 0; i < this.blocks.length; i++)1662
{ this.blocks[i]._prevY = this.blocks[i].y; }1663
};1664
1665
/**1666
* Safety net after the specific integrates: any remaining mid-air1667
* overlap or swept crossing is resolved with the same rules, then1668
* every flying cluster is rebuilt as a rigid body. Repeats a few1669
* times because one attach can create a new contact.1670
*/1671
GameCore.prototype.resolveAirborneCollisions = function ()1672
{1673
var guard = 0;1674
while (guard++ < this.height + 4)1675
{1676
var changed = false;1677
1678
// Manual vs falling / cluster / manual.1679
var manuals = [];1680
for (var i = 0; i < this.blocks.length; i++)1681
{1682
if (this.blocks[i].state === STATE_MANUAL)1683
{ manuals.push(this.blocks[i]); }1684
}1685
manuals.sort(function (a, b) { return b.y - a.y; });1686
for (var m = 0; m < manuals.length; m++)1687
{1688
var manual = manuals[m];1689
if (manual.state !== STATE_MANUAL)1690
{ continue; }1691
var oldY = (manual._prevY != null) ? manual._prevY : manual.y;1692
if (this.resolveManualCollisions(manual, oldY, manual.y))1693
{ changed = true; }1694
}1695
1696
// Falling vs flying (sit on the roof).1697
for (var i = 0; i < this.blocks.length; i++)1698
{1699
var block = this.blocks[i];1700
if (block.state !== STATE_FALLING)1701
{ continue; }1702
var flyer = this.highestFlyingBelow(block);1703
if (!flyer)1704
{1705
// Also a flyer whose swept path crosses this falling block.1706
flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y);1707
}1708
if (flyer && this.sweptIntervalsOverlap(block, flyer))1709
{1710
if (this.attachToFlyingCluster(block, flyer))1711
{ changed = true; }1712
}1713
}1714
1715
// Cluster vs cluster (swept, including after a column push).1716
var before = Object.keys(this.clusters).length;1717
this.mergeFlyingClusters();1718
if (Object.keys(this.clusters).length !== before)1719
{ changed = true; }1720
1721
// Rebuild every flying body so offsets stay integer and unique.1722
var ids = Object.keys(this.clusters);1723
for (var n = 0; n < ids.length; n++)1724
{1725
var cluster = this.clusters[ids[n]];1726
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')1727
{ continue; }1728
this.prepareClusterBody(cluster);1729
}1730
1731
if (!changed)1732
{ break; }1733
}1734
};1735
1736
/**1737
* Invariant checker (t1112). No two blocks occupy the same cell or1738
* overlap their [y, y+1) intervals. Flying-cluster members are1739
* grid-aligned to a single anchor. Returns a list of violation1740
* strings (empty if clean).1741
*/1742
GameCore.prototype.checkInvariants = function ()1743
{1744
var issues = [];1745
var byCol = Object.create(null);1746
var ids = Object.create(null);1747
for (var i = 0; i < this.blocks.length; i++)1748
{1749
var block = this.blocks[i];1750
if (ids[block.id])1751
{ issues.push('duplicate id ' + block.id); }1752
ids[block.id] = true;1753
if (!byCol[block.col])1754
{ byCol[block.col] = []; }1755
byCol[block.col].push(block);1756
if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet1757
&& block.color >= this.planet.colors.length))1758
{ issues.push('block ' + block.id + ' has invalid color ' + block.color); }1759
}1760
var cols = Object.keys(byCol);1761
for (var c = 0; c < cols.length; c++)1762
{1763
var list = byCol[cols[c]];1764
list.sort(function (a, b)1765
{1766
if (a.y !== b.y)1767
{ return a.y - b.y; }1768
return a.id - b.id;1769
});1770
for (var i = 0; i < list.length; i++)1771
{1772
var a = list[i];1773
if (i > 0)1774
{1775
var prev = list[i - 1];1776
if (a.y < prev.y + 1 - 1e-6)1777
{1778
issues.push('overlap col=' + a.col1779
+ ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)1780
+ ' gap=' + (a.y - prev.y));1781
}1782
var cellA = Math.floor(a.y + 1e-9);1783
var cellP = Math.floor(prev.y + 1e-9);1784
if (cellA === cellP)1785
{1786
issues.push('duplicate cell col=' + a.col + ' row=' + cellA1787
+ ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a));1788
}1789
}1790
}1791
}1792
var cids = Object.keys(this.clusters);1793
for (var n = 0; n < cids.length; n++)1794
{1795
var cluster = this.clusters[cids[n]];1796
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')1797
{ continue; }1798
var mem = this.clusterMembers(cluster.id);1799
var seenOff = Object.create(null);1800
for (var i = 0; i < mem.length; i++)1801
{1802
var b = mem[i];1803
if (cluster.anchorY != null && b.clusterOffset != null)1804
{1805
var expected = cluster.anchorY + b.clusterOffset;1806
if (Math.abs(b.y - expected) > 1e-6)1807
{1808
issues.push('cluster ' + cluster.id + ' member ' + b.id1809
+ ' y=' + b.y + ' != anchor+off ' + expected);1810
}1811
if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9)1812
{1813
issues.push('cluster ' + cluster.id + ' member ' + b.id1814
+ ' non-integer offset ' + b.clusterOffset);1815
}1816
var key = b.col + ':' + b.clusterOffset;1817
if (seenOff[key])1818
{1819
issues.push('cluster ' + cluster.id + ' duplicate offset ' + key);1820
}1821
seenOff[key] = true;1822
}1823
}1824
}1825
return issues;1826
};1827
1828
GameCore.prototype._invBlock = function (b)1829
{1830
return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y='1831
+ (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')';1832
};1833
1834
GameCore.prototype.assertInvariants = function (label)1835
{1836
if (!this.strictInvariants && !this.debugInvariants)1837
{ return; }1838
var issues = this.checkInvariants();1839
this.lastInvariantViolations = issues;1840
if (!issues.length)1841
{ return; }1842
var msg = 'physics invariants failed'1843
+ (label ? ' at ' + label : '')1844
+ ':\n ' + issues.join('\n ')1845
+ '\n' + (this.snapshot ? this.snapshot().picture : '');1846
if (this.strictInvariants)1847
{ throw new Error(msg); }1848
};1849
1518
GameCore.prototype.tryLandCluster = function (cluster, members)1850
GameCore.prototype.tryLandCluster = function (cluster, members)1519
{1851
{1520
if (!members.length)1852
if (!members.length)@@ -1527,7 +1859,7 @@ GameCore.prototype.tryLandCluster = function (cluster, members)1527
var top = this.stackTop(block.col, function (other)1859
var top = this.stackTop(block.col, function (other)1528
{1860
{1529
return other.clusterId === cluster.id;1861
return other.clusterId === cluster.id;1530
});1862
}, block.y);1531
if (block.y <= top + 1e-9)1863
if (block.y <= top + 1e-9)1532
{1864
{1533
hit = true;1865
hit = true;@@ -1539,9 +1871,10 @@ GameCore.prototype.tryLandCluster = function (cluster, members)1539
if (!hit)1871
if (!hit)1540
{ return; }1872
{ return; }1541
// Lift the whole rigid body so the deepest contact sits flush.1873
// Lift the whole rigid body so the deepest contact sits flush.1542
for (var i = 0; i < members.length; i++)1874
this.prepareClusterBody(cluster);1543
{ members[i].y += shift; }1875
cluster.anchorY += shift;1544
this.landCluster(cluster, members);1876
this.syncClusterMembers(cluster);1877
this.landCluster(cluster, this.clusterMembers(cluster.id));1545
};1878
};1546
1879
1547
/**1880
/**@@ -1642,12 +1975,11 @@ GameCore.prototype.integrateManual = function (dt)1642
var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };1975
var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };1643
var dy = this.advanceJump(proxy, dt);1976
var dy = this.advanceJump(proxy, dt);1644
var nextY = oldY + dy;1977
var nextY = oldY + dy;1645
this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));1646
block.vy = proxy.vy;1978
block.vy = proxy.vy;1647
block.manualPhase = proxy.phase;1979
block.manualPhase = proxy.phase;1648
block.y = Math.min(nextY, block.manualTarget);1980
if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget)))1649
if (this.tryMergeManualWithFalling(block, oldY, block.y))1650
{ continue; }1981
{ continue; }1982
block.y = Math.min(nextY, block.manualTarget);1651
if (block.y >= block.manualTarget - 1e-12)1983
if (block.y >= block.manualTarget - 1e-12)1652
{1984
{1653
block.y = block.manualTarget;1985
block.y = block.manualTarget;@@ -1668,7 +2000,7 @@ GameCore.prototype.integrateManual = function (dt)1668
var dy = this.advanceJump(proxy, dt);2000
var dy = this.advanceJump(proxy, dt);1669
block.vy = proxy.vy;2001
block.vy = proxy.vy;1670
var nextY = block.y + dy;2002
var nextY = block.y + dy;1671
var top = this.stackTop(block.col);2003
var top = this.stackTop(block.col, null, block.y);1672
if (nextY <= top)2004
if (nextY <= top)1673
{2005
{1674
block.y = top;2006
block.y = top;@@ -1680,9 +2012,10 @@ GameCore.prototype.integrateManual = function (dt)1680
}2012
}1681
else2013
else1682
{2014
{1683
block.y = Math.min(nextY, this.height - 1);2015
var downNext = Math.min(nextY, this.height - 1);1684
if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))2016
if (this.resolveManualCollisions(block, block.y, downNext))1685
{ continue; }2017
{ continue; }2018
block.y = downNext;1686
}2019
}1687
}2020
}1688
}2021
}@@ -1857,7 +2190,7 @@ GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)1857
}2190
}1858
if (this.drag && this.drag.blockId === manual.id)2191
if (this.drag && this.drag.blockId === manual.id)1859
{ this.drag = null; }2192
{ this.drag = null; }1860
this.packClusterOverlaps(cluster);2193
this.prepareClusterBody(cluster);1861
this.checkChainRelaunch(cluster);2194
this.checkChainRelaunch(cluster);1862
return cluster;2195
return cluster;1863
};2196
};@@ -1882,40 +2215,217 @@ GameCore.prototype.mergeAnyManualFallingOverlaps = function ()1882
}2215
}1883
};2216
};1884
2217
1885
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)2218
/**2219
* Resolve collisions of a thrown (manual) block along [oldY, nextY].2220
* Falling blocks merge (t1108). A flying cluster is pushed one cell up2221
* in the hit column and the shot attaches at the bottom (t1111).2222
* Another thrown block is promoted to a 1-block cluster and then the2223
* same attach-at-bottom rule applies. Returns true if `manual` is no2224
* longer in the MANUAL state.2225
*/2226
GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY)1886
{2227
{1887
if (!manual.pushedClusters)2228
if (!manual || manual.state !== STATE_MANUAL)1888
{ manual.pushedClusters = Object.create(null); }2229
{ return true; }1889
2230
1890
var manualTopOld = oldY + 1;2231
var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY);1891
var manualTopNew = nextY + 1;2232
if (fallingHits.length)1892
var low = Math.min(oldY, nextY);2233
{1893
var high = Math.max(oldY, nextY) + 1;2234
this.mergeManualWithFalling(manual, fallingHits);2235
return manual.state !== STATE_MANUAL;2236
}1894
2237
2238
var flyer = this.flyingHitOnManualPath(manual, oldY, nextY);2239
if (flyer)2240
{2241
var cluster = this.clusters[flyer.clusterId];2242
if (cluster && cluster.phase !== 'landed')2243
{2244
if (nextY + 1e-12 < oldY)2245
{ this.attachToFlyingCluster(manual, flyer); }2246
else2247
{ this.attachShotUnderCluster(manual, cluster, manual.col); }2248
return manual.state !== STATE_MANUAL;2249
}2250
}2251
2252
var other = this.manualHitOnManualPath(manual, oldY, nextY);2253
if (other)2254
{2255
var host = this.promoteManualToCluster(other);2256
if (host)2257
{2258
if (nextY + 1e-12 < oldY)2259
{ this.attachToFlyingCluster(manual, other); }2260
else2261
{ this.attachShotUnderCluster(manual, host, manual.col); }2262
return manual.state !== STATE_MANUAL;2263
}2264
}2265
return false;2266
};2267
2268
/** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */2269
GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY)2270
{2271
var lo = Math.min(oldY, nextY);2272
var hi = Math.max(oldY, nextY) + 1;2273
var best = null;2274
var bestY = Infinity;1895
for (var i = 0; i < this.blocks.length; i++)2275
for (var i = 0; i < this.blocks.length; i++)1896
{2276
{1897
var other = this.blocks[i];2277
var other = this.blocks[i];1898
if (other === manual)2278
if (other === manual || other.col !== manual.col)1899
{ continue; }1900
if (other.col !== manual.col)1901
{ continue; }2279
{ continue; }1902
if (other.state !== STATE_FLYING || other.clusterId == null)2280
if (other.state !== STATE_FLYING || other.clusterId == null)1903
{ continue; }2281
{ continue; }1904
if (manual.pushedClusters[other.clusterId])2282
var cluster = this.clusters[other.clusterId];2283
if (!cluster || cluster.phase === 'landed')1905
{ continue; }2284
{ continue; }1906
var otherBot = other.y;2285
var o0 = (other._prevY != null) ? other._prevY : other.y;1907
var otherTop = other.y + 1;2286
var o1 = other.y;1908
var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;2287
var oLo = Math.min(o0, o1);1909
var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;2288
var oHi = Math.max(o0, o1) + 1;1910
if (overlapping || crossing)2289
if (lo < oHi - 1e-9 && oLo < hi - 1e-9)1911
{2290
{1912
if (this.pushClusterColumn(other.clusterId, manual.col))2291
if (other.y < bestY)1913
{2292
{1914
manual.pushedClusters[other.clusterId] = true;2293
bestY = other.y;1915
return;2294
best = other;1916
}2295
}1917
}2296
}1918
}2297
}2298
return best;2299
};2300
2301
/** Another thrown block in the same column on the path (prefer the lower). */2302
GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY)2303
{2304
var lo = Math.min(oldY, nextY);2305
var hi = Math.max(oldY, nextY) + 1;2306
var best = null;2307
for (var i = 0; i < this.blocks.length; i++)2308
{2309
var other = this.blocks[i];2310
if (other === manual || other.col !== manual.col)2311
{ continue; }2312
if (other.state !== STATE_MANUAL)2313
{ continue; }2314
var o0 = (other._prevY != null) ? other._prevY : other.y;2315
var o1 = other.y;2316
var oLo = Math.min(o0, o1);2317
var oHi = Math.max(o0, o1) + 1;2318
if (lo < oHi - 1e-9 && oLo < hi - 1e-9)2319
{2320
if (!best || other.y < best.y)2321
{ best = other; }2322
}2323
}2324
return best;2325
};2326
2327
/**2328
* t1111: push every member of `cluster` in `col` up by one cell, then2329
* attach `manual` at the bottom of that column, grid-aligned, as part2330
* of the cluster. The cluster keeps its velocity; the shot takes it.2331
* Other columns are untouched. Same rule for every successive shot.2332
*/2333
GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col)2334
{2335
if (!manual || !cluster || !this.clusters[cluster.id])2336
{ return false; }2337
if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id)2338
{2339
// Already belonging elsewhere: leave first.2340
if (manual.clusterId != null)2341
{ this.leaveCluster(manual); }2342
}2343
this.prepareClusterBody(cluster);2344
var members = this.clusterMembers(cluster.id);2345
var minOff = null;2346
for (var i = 0; i < members.length; i++)2347
{2348
if (members[i].col !== col)2349
{ continue; }2350
if (minOff == null || members[i].clusterOffset < minOff)2351
{ minOff = members[i].clusterOffset; }2352
}2353
if (minOff == null)2354
{ minOff = 0; }2355
else2356
{2357
for (var i = 0; i < members.length; i++)2358
{2359
if (members[i].col === col)2360
{ members[i].clusterOffset += 1; }2361
}2362
}2363
if (this.drag && this.drag.blockId === manual.id)2364
{ this.drag = null; }2365
manual.clusterId = cluster.id;2366
manual.clusterOffset = minOff;2367
manual.state = STATE_FLYING;2368
manual.flame = manual.color === COLOR_GRAY;2369
manual.settleTime = 0;2370
manual.manualPhase = null;2371
manual.manualTarget = null;2372
manual.manualDecel = 0;2373
manual.manualJumpHeight = 0;2374
manual.pushedClusters = null;2375
this.syncClusterMembers(cluster);2376
this.cullOffTop();2377
if (this.clusters[cluster.id])2378
{ this.checkChainRelaunch(cluster); }2379
return true;2380
};2381
2382
/**2383
* Turn a lone thrown block into a 1-block flying cluster so a later2384
* shot can use the t1111 attach-at-bottom rule. A single throw stays2385
* MANUAL (and is capped at the top of the field) until it collides.2386
*/2387
GameCore.prototype.promoteManualToCluster = function (manual)2388
{2389
if (!manual)2390
{ return null; }2391
if (manual.state === STATE_FLYING && manual.clusterId != null)2392
{2393
var existing = this.clusters[manual.clusterId];2394
if (existing && existing.phase !== 'landed')2395
{ return existing; }2396
}2397
if (manual.state !== STATE_MANUAL)2398
{ return null; }2399
var height = manual.manualJumpHeight != null ? manual.manualJumpHeight2400
: Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y);2401
var cluster = {2402
id: this.nextClusterId++,2403
jumpHeight: height,2404
vy: manual.vy,2405
decel: manual.manualDecel || 0,2406
phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up',2407
fromManual: true,2408
anchorY: manual.y2409
};2410
this.clusters[cluster.id] = cluster;2411
manual.state = STATE_FLYING;2412
manual.clusterId = cluster.id;2413
manual.clusterOffset = 0;2414
manual.flame = manual.color === COLOR_GRAY;2415
manual.settleTime = 0;2416
manual.manualPhase = null;2417
manual.manualTarget = null;2418
manual.manualDecel = 0;2419
manual.manualJumpHeight = 0;2420
manual.pushedClusters = null;2421
this.syncClusterMembers(cluster);2422
return cluster;2423
};2424
2425
/** Kept for older tests / call sites: push a cluster column by one cell. */2426
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)2427
{2428
return this.resolveManualCollisions(manual, oldY, nextY);1919
};2429
};1920
2430
1921
2431
@@ -2428,6 +2938,7 @@ GameCore.prototype.launchCluster = function (matchedBlocks, height)2428
block.vy = cluster.vy;2938
block.vy = cluster.vy;2429
block.settleTime = 0;2939
block.settleTime = 0;2430
}2940
}2941
this.prepareClusterBody(cluster);2431
return cluster;2942
return cluster;2432
};2943
};2433
2944
@@ -3568,6 +4079,7 @@ var app = {3568
pointerType: null,4079
pointerType: null,3569
activeTouchId: null,4080
activeTouchId: null,3570
debugDrag: false,4081
debugDrag: false,4082
debugPhysics: false,3571
gameOverShown: false,4083
gameOverShown: false,3572
selectTween: null,4084
selectTween: null,3573
online: false,4085
online: false,@@ -3581,6 +4093,7 @@ var app = {3581
window.onload = function ()4093
window.onload = function ()3582
{4094
{3583
app.debugDrag = isDragDebugEnabled();4095
app.debugDrag = isDragDebugEnabled();4096
app.debugPhysics = isPhysicsDebugEnabled();3584
var play = document.getElementById('btn-play');4097
var play = document.getElementById('btn-play');3585
play.addEventListener('click', onPlay);4098
play.addEventListener('click', onPlay);3586
play.addEventListener('pointerup', function (e)4099
play.addEventListener('pointerup', function (e)@@ -3629,6 +4142,8 @@ function startPlay (opts)3629
if (typeof miss !== 'number' || !isFinite(miss))4142
if (typeof miss !== 'number' || !isFinite(miss))3630
{ miss = planetMod.missChance(app.planet); }4143
{ miss = planetMod.missChance(app.planet); }3631
app.core = new GameCore(app.planet, Math.random, miss);4144
app.core = new GameCore(app.planet, Math.random, miss);4145
if (app.debugPhysics)4146
{ app.core.debugInvariants = true; }3632
app.mode = 'playing';4147
app.mode = 'playing';3633
app.online = !!opts.online;4148
app.online = !!opts.online;3634
app.gameOverShown = false;4149
app.gameOverShown = false;@@ -4001,6 +4516,8 @@ function tickView (dt)4001
syncSprites();4516
syncSprites();4002
refreshHud();4517
refreshHud();4003
tickOnline(dt);4518
tickOnline(dt);4519
if (app.debugPhysics)4520
{ logPhysicsDebug(); }4004
4521
4005
if (app.core.state === 'gameover' && !app.gameOverShown)4522
if (app.core.state === 'gameover' && !app.gameOverShown)4006
{4523
{@@ -4057,6 +4574,22 @@ function isDragDebugEnabled ()4057
return false;4574
return false;4058
}4575
}4059
4576
4577
/** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */4578
function isPhysicsDebugEnabled ()4579
{4580
try4581
{4582
var q = window.location && window.location.search;4583
if (q && /(?:\?|&)debug(?:=|&|$)/.test(q))4584
{ return true; }4585
if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1'4586
|| window.localStorage.getItem('meteors-debug-drag') === '1'))4587
{ return true; }4588
}4589
catch (err) { /* ignore */ }4590
return false;4591
}4592
4060
function eventClient (e)4593
function eventClient (e)4061
{4594
{4062
var t = null;4595
var t = null;@@ -4366,6 +4899,42 @@ function onTouchCancel (e)4366
finishDrag({ noLaunch: true });4899
finishDrag({ noLaunch: true });4367
}4900
}4368
4901
4902
function logPhysicsDebug ()4903
{4904
if (!app.debugPhysics || !app.core)4905
{ return; }4906
ensureDragDebugOverlay();4907
var el = document.getElementById('drag-debug');4908
if (!el)4909
{ return; }4910
var issues = app.core.lastInvariantViolations || [];4911
var flying = 0;4912
var manuals = 0;4913
var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0;4914
for (var i = 0; i < app.core.blocks.length; i++)4915
{4916
if (app.core.blocks[i].state === 'flying')4917
{ flying++; }4918
if (app.core.blocks[i].state === 'manual')4919
{ manuals++; }4920
}4921
var status = issues.length4922
? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n'))4923
: ('physics ok blocks=' + app.core.blocks.length4924
+ ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters);4925
// Keep any live drag line above the physics status.4926
var dragLine = '';4927
if (app.debugDrag && lastDragDebug)4928
{4929
dragLine = el.textContent && el.textContent.indexOf('drag ') === 04930
? el.textContent.split('\n')[0] + '\n'4931
: '';4932
}4933
el.textContent = dragLine + status;4934
el.hidden = false;4935
el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8';4936
}4937
4369
function ensureDragDebugOverlay ()4938
function ensureDragDebugOverlay ()4370
{4939
{4371
if (document.getElementById('drag-debug'))4940
if (document.getElementById('drag-debug'))