BlueGit

projects-timfalken-com meteors

feat: cluster drags, thrown-falling merges, cell grabs and landing matches

Still stacks under a flying cluster are draggable; the flyer itself is
not. Landed clusters can be rearranged, and swapping in or out updates
membership (t1107). A thrown block collides with falling blocks the same
way two flying clusters do: they merge with velocity averaged by count
and cannot pass through each other (t1108). Grabbing uses the grid cell
under the pointer, with a half-tile fallback for mid-animation blocks
(t1109). Coloured cargo that lands into a mixed row with resting blocks
launches immediately; greys wait until they recolor (t1110).

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
681cc658f05f3fb08b9c8c0d285283712207f4c9
Parent
570b69c5
1 file changed +403 −34
src/gameCore.js +403 −34
@@ -27,8 +27,8 @@ var LAUNCH_PULL = 0.35;
27 27 // Minimal accumulated vertical pointer travel (tiles) to swap one step.
28 28 // No hold delay: the first swap fires as soon as this distance is reached.
29 29 var SWAP_DISTANCE = 0.25;
30 // Grab a still block if the pointer is at most this far (tiles) from it.
31 var SNAP_Y = 0.65;
30 // Fallback snap (tiles) when the press cell is empty: nearest block in the column.
31 var SNAP_Y = 0.5;
32 32 // Horizontal slop (tiles) for snapping to a neighbouring column on press.
33 33 var HORIZONTAL_SLOP = 0.35;
34 34
@@ -432,12 +432,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
432 432 if (other.y + 1 > roof)
433 433 { roof = other.y + 1; }
434 434 }
435 var oldN = this.clusterMembers(cluster.id).length;
436 var fallingVy = block.vy;
435 437 block.y = roof;
436 438 block.state = STATE_FLYING;
437 439 block.clusterId = cluster.id;
438 block.vy = cluster.vy;
439 440 block.flame = false;
440 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; }
441 458 return true;
442 459 };
443 460
@@ -500,6 +517,7 @@ GameCore.prototype.tick = function (dt)
500 517 this.integrateClusters(step);
501 518 this.mergeFlyingClusters();
502 519 this.integrateManual(step);
520 this.mergeAnyManualFallingOverlaps();
503 521 this.cullOffTop();
504 522 left -= step;
505 523 }
@@ -581,6 +599,12 @@ GameCore.prototype.integrateFalling = function (dt)
581 599 { continue; }
582 600 this.accelerateFall(block, dt);
583 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 }
584 608 var flyer = this.highestFlyingBelow(block);
585 609 if (flyer && nextY <= flyer.y + 1 + 1e-9)
586 610 {
@@ -981,6 +1005,8 @@ GameCore.prototype.integrateManual = function (dt)
981 1005 block.vy = proxy.vy;
982 1006 block.manualPhase = proxy.phase;
983 1007 block.y = Math.min(nextY, block.manualTarget);
1008 if (this.tryMergeManualWithFalling(block, oldY, block.y))
1009 { continue; }
984 1010 if (block.y >= block.manualTarget - 1e-12)
985 1011 {
986 1012 block.y = block.manualTarget;
@@ -1014,11 +1040,207 @@ GameCore.prototype.integrateManual = function (dt)
1014 1040 else
1015 1041 {
1016 1042 block.y = Math.min(nextY, this.height - 1);
1043 if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1044 { continue; }
1017 1045 }
1018 1046 }
1019 1047 }
1020 1048 };
1021 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 */
1055 GameCore.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
1066 GameCore.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
1083 GameCore.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 */
1107 GameCore.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 */
1167 GameCore.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. */
1225 GameCore.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
1022 1244 GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1023 1245 {
1024 1246 if (!manual.pushedClusters)
@@ -1150,10 +1372,12 @@ GameCore.prototype.startManualLaunch = function (block)
1150 1372 {
1151 1373 var target = this.height - 1;
1152 1374 var height = Math.max(0.05, target - block.y);
1375 this.leaveCluster(block);
1153 1376 block.state = STATE_MANUAL;
1154 1377 block.pushedClusters = Object.create(null);
1155 1378 block.manualTarget = target;
1156 1379 block.manualPhase = 'up';
1380 block.manualJumpHeight = height;
1157 1381 block.vy = PlanetMod.launchVelocity(this.planet, height);
1158 1382 block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
1159 1383 block.flame = false;
@@ -1327,7 +1551,11 @@ GameCore.prototype.findMatchLines = function ()
1327 1551 for (var i = 0; i < this.blocks.length; i++)
1328 1552 {
1329 1553 var block = this.blocks[i];
1330 if (block.state === STATE_STILL && block.color >= 0)
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)
1331 1559 { candidates.push(block); }
1332 1560 }
1333 1561 return this.collectLines(candidates, true);
@@ -1637,48 +1865,103 @@ GameCore.prototype.beginDrag = function (col, y)
1637 1865 };
1638 1866
1639 1867 /**
1640 * Instant grab: the nearest still, coloured block to (col, y), allowing
1641 * a small vertical miss and a horizontal slop into a neighbouring
1642 * column. `col` may be fractional (pointer x in tiles). No hold delay.
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.
1643 1877 */
1644 1878 GameCore.prototype.snapDragBlock = function (col, y)
1645 1879 {
1646 1880 var raw = col;
1647 1881 var c0 = Math.floor(raw + 1e-9);
1882 var row = Math.floor(y + 1e-9);
1648 1883 var frac = raw - c0;
1649 var candidates = [];
1650 if (c0 >= 0 && c0 < this.width)
1651 { candidates.push(c0); }
1652 // Only consult a neighbour when the pointer is actually near the edge
1653 // (a whole-number col, as in unit tests, stays in that column).
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 = [];
1654 1897 if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)
1655 { candidates.push(c0 - 1); }
1898 { neighbours.push(c0 - 1); }
1656 1899 if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)
1657 { candidates.push(c0 + 1); }
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. */
1922 GameCore.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 };
1658 1945
1946 /** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */
1947 GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist)
1948 {
1659 1949 var best = null;
1660 1950 var bestDist = Infinity;
1661 for (var c = 0; c < candidates.length; c++)
1951 var list = this.blocksInColumn(col);
1952 for (var i = 0; i < list.length; i++)
1662 1953 {
1663 var cc = candidates[c];
1664 if (!this.isColumnStill(cc))
1954 var block = list[i];
1955 if (!this.isDraggableBlock(block))
1665 1956 { continue; }
1666 var list = this.blocksInColumn(cc);
1667 for (var i = 0; i < list.length; i++)
1957 var dist = this.pointerDistToBlock(block, col + 0.5, y);
1958 if (dist < bestDist)
1668 1959 {
1669 var block = list[i];
1670 if (block.state !== STATE_STILL || block.color < 0)
1671 { continue; }
1672 var dist = this.pointerDistToBlock(block, raw, y);
1673 // Prefer the column the pointer is actually in on a tie.
1674 if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0))
1675 {
1676 bestDist = dist;
1677 best = block;
1678 }
1960 bestDist = dist;
1961 best = block;
1679 1962 }
1680 1963 }
1681 if (!best || bestDist > SNAP_Y)
1964 if (!best || bestDist > maxDist)
1682 1965 { return null; }
1683 1966 return best;
1684 1967 };
@@ -1707,12 +1990,12 @@ GameCore.prototype.moveDrag = function (y)
1707 1990 if (!this.drag)
1708 1991 { return; }
1709 1992 var block = this.blockById(this.drag.blockId);
1710 if (!block || block.state !== STATE_STILL)
1993 if (!block || !this.isDraggableBlock(block))
1711 1994 {
1712 1995 this.drag = null;
1713 1996 return;
1714 1997 }
1715 if (!this.isColumnStill(block.col))
1998 if (!this.columnAllowsDrag(block.col))
1716 1999 {
1717 2000 this.drag = null;
1718 2001 return;
@@ -1842,15 +2125,101 @@ GameCore.prototype.swapBlocks = function (a, b)
1842 2125 var y = a.y;
1843 2126 a.y = b.y;
1844 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 */
2136 GameCore.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 */
2158 GameCore.prototype.columnAllowsDrag = function (col)
2159 {
2160 return !this.isColumnLocked(col);
2161 };
2162
2163 GameCore.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 */
2178 GameCore.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
2190 GameCore.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; }
1845 2214 };
1846 2215
1847 2216 /**
1848 2217 * Test helper: stepwise-swap the block at (col, fromRow) to toRow.
1849 * Returns the moved block, or null if the column is not still.
2218 * Returns the moved block, or null if the column does not allow a drag.
1850 2219 */
1851 2220 GameCore.prototype.swapTo = function (col, fromRow, toRow)
1852 2221 {
1853 if (!this.isColumnStill(col))
2222 if (!this.columnAllowsDrag(col))
1854 2223 { return null; }
1855 2224 var block = this.blockAt(col, fromRow);
1856 2225 if (!block)
@@ -27,8 +27,8 @@ var LAUNCH_PULL = 0.35;
27// Minimal accumulated vertical pointer travel (tiles) to swap one step.
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.
28// No hold delay: the first swap fires as soon as this distance is reached.
29var SWAP_DISTANCE = 0.25;
29var SWAP_DISTANCE = 0.25;
30// Grab a still block if the pointer is at most this far (tiles) from it.
30// Fallback snap (tiles) when the press cell is empty: nearest block in the column.
31var SNAP_Y = 0.65;
31var SNAP_Y = 0.5;
32// Horizontal slop (tiles) for snapping to a neighbouring column on press.
32// Horizontal slop (tiles) for snapping to a neighbouring column on press.
33var HORIZONTAL_SLOP = 0.35;
33var HORIZONTAL_SLOP = 0.35;
34
34
@@ -432,12 +432,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
432 if (other.y + 1 > roof)
432 if (other.y + 1 > roof)
433 { roof = other.y + 1; }
433 { roof = other.y + 1; }
434 }
434 }
435 var oldN = this.clusterMembers(cluster.id).length;
436 var fallingVy = block.vy;
435 block.y = roof;
437 block.y = roof;
436 block.state = STATE_FLYING;
438 block.state = STATE_FLYING;
437 block.clusterId = cluster.id;
439 block.clusterId = cluster.id;
438 block.vy = cluster.vy;
439 block.flame = false;
440 block.flame = false;
440 block.settleTime = 0;
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; }
441 return true;
458 return true;
442};
459};
443
460
@@ -500,6 +517,7 @@ GameCore.prototype.tick = function (dt)
500 this.integrateClusters(step);
517 this.integrateClusters(step);
501 this.mergeFlyingClusters();
518 this.mergeFlyingClusters();
502 this.integrateManual(step);
519 this.integrateManual(step);
520 this.mergeAnyManualFallingOverlaps();
503 this.cullOffTop();
521 this.cullOffTop();
504 left -= step;
522 left -= step;
505 }
523 }
@@ -581,6 +599,12 @@ GameCore.prototype.integrateFalling = function (dt)
581 { continue; }
599 { continue; }
582 this.accelerateFall(block, dt);
600 this.accelerateFall(block, dt);
583 var nextY = block.y + block.vy * 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 }
584 var flyer = this.highestFlyingBelow(block);
608 var flyer = this.highestFlyingBelow(block);
585 if (flyer && nextY <= flyer.y + 1 + 1e-9)
609 if (flyer && nextY <= flyer.y + 1 + 1e-9)
586 {
610 {
@@ -981,6 +1005,8 @@ GameCore.prototype.integrateManual = function (dt)
981 block.vy = proxy.vy;
1005 block.vy = proxy.vy;
982 block.manualPhase = proxy.phase;
1006 block.manualPhase = proxy.phase;
983 block.y = Math.min(nextY, block.manualTarget);
1007 block.y = Math.min(nextY, block.manualTarget);
1008 if (this.tryMergeManualWithFalling(block, oldY, block.y))
1009 { continue; }
984 if (block.y >= block.manualTarget - 1e-12)
1010 if (block.y >= block.manualTarget - 1e-12)
985 {
1011 {
986 block.y = block.manualTarget;
1012 block.y = block.manualTarget;
@@ -1014,11 +1040,207 @@ GameCore.prototype.integrateManual = function (dt)
1014 else
1040 else
1015 {
1041 {
1016 block.y = Math.min(nextY, this.height - 1);
1042 block.y = Math.min(nextY, this.height - 1);
1043 if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1044 { continue; }
1017 }
1045 }
1018 }
1046 }
1019 }
1047 }
1020};
1048};
1021
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
1022GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1244GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1023{
1245{
1024 if (!manual.pushedClusters)
1246 if (!manual.pushedClusters)
@@ -1150,10 +1372,12 @@ GameCore.prototype.startManualLaunch = function (block)
1150{
1372{
1151 var target = this.height - 1;
1373 var target = this.height - 1;
1152 var height = Math.max(0.05, target - block.y);
1374 var height = Math.max(0.05, target - block.y);
1375 this.leaveCluster(block);
1153 block.state = STATE_MANUAL;
1376 block.state = STATE_MANUAL;
1154 block.pushedClusters = Object.create(null);
1377 block.pushedClusters = Object.create(null);
1155 block.manualTarget = target;
1378 block.manualTarget = target;
1156 block.manualPhase = 'up';
1379 block.manualPhase = 'up';
1380 block.manualJumpHeight = height;
1157 block.vy = PlanetMod.launchVelocity(this.planet, height);
1381 block.vy = PlanetMod.launchVelocity(this.planet, height);
1158 block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
1382 block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
1159 block.flame = false;
1383 block.flame = false;
@@ -1327,7 +1551,11 @@ GameCore.prototype.findMatchLines = function ()
1327 for (var i = 0; i < this.blocks.length; i++)
1551 for (var i = 0; i < this.blocks.length; i++)
1328 {
1552 {
1329 var block = this.blocks[i];
1553 var block = this.blocks[i];
1330 if (block.state === STATE_STILL && block.color >= 0)
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)
1331 { candidates.push(block); }
1559 { candidates.push(block); }
1332 }
1560 }
1333 return this.collectLines(candidates, true);
1561 return this.collectLines(candidates, true);
@@ -1637,48 +1865,103 @@ GameCore.prototype.beginDrag = function (col, y)
1637};
1865};
1638
1866
1639/**
1867/**
1640 * Instant grab: the nearest still, coloured block to (col, y), allowing
1868 * Instant grab, cell-based (t1109). The press is mapped to the grid cell
1641 * a small vertical miss and a horizontal slop into a neighbouring
1869 * floor(col), floor(y); any press inside that cell — corners, padding,
1642 * column. `col` may be fractional (pointer x in tiles). No hold delay.
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.
1643 */
1877 */
1644GameCore.prototype.snapDragBlock = function (col, y)
1878GameCore.prototype.snapDragBlock = function (col, y)
1645{
1879{
1646 var raw = col;
1880 var raw = col;
1647 var c0 = Math.floor(raw + 1e-9);
1881 var c0 = Math.floor(raw + 1e-9);
1882 var row = Math.floor(y + 1e-9);
1648 var frac = raw - c0;
1883 var frac = raw - c0;
1649 var candidates = [];
1884
1650 if (c0 >= 0 && c0 < this.width)
1885 if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0))
1651 { candidates.push(c0); }
1886 {
1652 // Only consult a neighbour when the pointer is actually near the edge
1887 var exact = this.draggableInCell(c0, row);
1653 // (a whole-number col, as in unit tests, stays in that column).
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 = [];
1654 if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)
1897 if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)
1655 { candidates.push(c0 - 1); }
1898 { neighbours.push(c0 - 1); }
1656 if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)
1899 if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)
1657 { candidates.push(c0 + 1); }
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};
1658
1945
1946/** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */
1947GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist)
1948{
1659 var best = null;
1949 var best = null;
1660 var bestDist = Infinity;
1950 var bestDist = Infinity;
1661 for (var c = 0; c < candidates.length; c++)
1951 var list = this.blocksInColumn(col);
1952 for (var i = 0; i < list.length; i++)
1662 {
1953 {
1663 var cc = candidates[c];
1954 var block = list[i];
1664 if (!this.isColumnStill(cc))
1955 if (!this.isDraggableBlock(block))
1665 { continue; }
1956 { continue; }
1666 var list = this.blocksInColumn(cc);
1957 var dist = this.pointerDistToBlock(block, col + 0.5, y);
1667 for (var i = 0; i < list.length; i++)
1958 if (dist < bestDist)
1668 {
1959 {
1669 var block = list[i];
1960 bestDist = dist;
1670 if (block.state !== STATE_STILL || block.color < 0)
1961 best = block;
1671 { continue; }
1672 var dist = this.pointerDistToBlock(block, raw, y);
1673 // Prefer the column the pointer is actually in on a tie.
1674 if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0))
1675 {
1676 bestDist = dist;
1677 best = block;
1678 }
1679 }
1962 }
1680 }
1963 }
1681 if (!best || bestDist > SNAP_Y)
1964 if (!best || bestDist > maxDist)
1682 { return null; }
1965 { return null; }
1683 return best;
1966 return best;
1684};
1967};
@@ -1707,12 +1990,12 @@ GameCore.prototype.moveDrag = function (y)
1707 if (!this.drag)
1990 if (!this.drag)
1708 { return; }
1991 { return; }
1709 var block = this.blockById(this.drag.blockId);
1992 var block = this.blockById(this.drag.blockId);
1710 if (!block || block.state !== STATE_STILL)
1993 if (!block || !this.isDraggableBlock(block))
1711 {
1994 {
1712 this.drag = null;
1995 this.drag = null;
1713 return;
1996 return;
1714 }
1997 }
1715 if (!this.isColumnStill(block.col))
1998 if (!this.columnAllowsDrag(block.col))
1716 {
1999 {
1717 this.drag = null;
2000 this.drag = null;
1718 return;
2001 return;
@@ -1842,15 +2125,101 @@ GameCore.prototype.swapBlocks = function (a, b)
1842 var y = a.y;
2125 var y = a.y;
1843 a.y = b.y;
2126 a.y = b.y;
1844 b.y = 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; }
1845};
2214};
1846
2215
1847/**
2216/**
1848 * Test helper: stepwise-swap the block at (col, fromRow) to toRow.
2217 * Test helper: stepwise-swap the block at (col, fromRow) to toRow.
1849 * Returns the moved block, or null if the column is not still.
2218 * Returns the moved block, or null if the column does not allow a drag.
1850 */
2219 */
1851GameCore.prototype.swapTo = function (col, fromRow, toRow)
2220GameCore.prototype.swapTo = function (col, fromRow, toRow)
1852{
2221{
1853 if (!this.isColumnStill(col))
2222 if (!this.columnAllowsDrag(col))
1854 { return null; }
2223 { return null; }
1855 var block = this.blockAt(col, fromRow);
2224 var block = this.blockAt(col, fromRow);
1856 if (!block)
2225 if (!block)