BlueGit

projects-timfalken-com meteors

feat: rigid-body clusters, attach-at-bottom shots and swept collisions

A flying cluster is one rigid body with a single anchorY/vy and integer
cell offsets, so members stay grid-aligned. A block shot into a cluster
pushes the hit column up by one cell and attaches at the bottom, and
the same rule applies to successive shots (t1111). Thrown blocks collide
with each other the same way. Collisions are swept between every pair
(launched vs cluster, launched vs launched, falling vs flying, cluster
vs cluster).

stackTop no longer ignores gaps, so falling blocks cannot teleport up
onto a hanging still. releaseUnsupported repeats until a hanging chain
is stable. An invariant checker (no overlap, no duplicate cells) runs
at the end of every tick in tests and under ?debug.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
a9f09cd866fee7ec8a87f513ddc461b3bf98fe8b
Parent
1ede9008
2 files changed +635 −66
src/gameCore.js +577 −66
@@ -80,6 +80,11 @@ function GameCore (planet, rng)
80 80 this.outgoingShots = [];
81 81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
82 82 this.missChance = optsMissChance(arguments);
83 // t1112: invariant checker. strictInvariants throws (tests);
84 // debugInvariants records violations for the ?debug overlay.
85 this.strictInvariants = false;
86 this.debugInvariants = false;
87 this.lastInvariantViolations = [];
83 88 }
84 89
85 90 function optsMissChance (args)
@@ -179,7 +184,12 @@ GameCore.prototype.isGrounded = function (block)
179 184 || block.state === STATE_LOCKED;
180 185 };
181 186
182 GameCore.prototype.stackTop = function (col, ignore)
187 /**
188 * Highest grounded landing surface in `col`. If `belowY` is given, only
189 * blocks strictly below that y count — a hanging still block above a
190 * gap must never pull a falling or thrown block upward through the gap.
191 */
192 GameCore.prototype.stackTop = function (col, ignore, belowY)
183 193 {
184 194 var top = 0;
185 195 for (var i = 0; i < this.blocks.length; i++)
@@ -189,6 +199,8 @@ GameCore.prototype.stackTop = function (col, ignore)
189 199 { continue; }
190 200 if (ignore && ignore(block))
191 201 { continue; }
202 if (belowY != null && block.y + 1e-9 >= belowY)
203 { continue; }
192 204 if (this.isGrounded(block))
193 205 {
194 206 var surface = block.y + 1;
@@ -374,18 +386,28 @@ GameCore.prototype.clearFlightState = function (block)
374 386
375 387 GameCore.prototype.releaseUnsupported = function ()
376 388 {
377 for (var i = 0; i < this.blocks.length; i++)
389 // Repeat until stable: a still sitting on a still that itself just
390 // became falling must also drop. Two fixed passes are not enough
391 // for a chain of hanging blocks (t1112).
392 var guard = 0;
393 var changed = true;
394 while (changed && guard++ < this.height + 2)
378 395 {
379 var block = this.blocks[i];
380 if (block.state !== STATE_STILL)
381 { continue; }
382 if (this.drag && this.drag.blockId === block.id)
383 { continue; }
384 if (this.hasSupport(block))
385 { continue; }
386 this.leaveCluster(block);
387 this.clearFlightState(block);
388 block.state = STATE_FALLING;
396 changed = false;
397 for (var i = 0; i < this.blocks.length; i++)
398 {
399 var block = this.blocks[i];
400 if (block.state !== STATE_STILL)
401 { continue; }
402 if (this.drag && this.drag.blockId === block.id)
403 { continue; }
404 if (this.hasSupport(block))
405 { continue; }
406 this.leaveCluster(block);
407 this.clearFlightState(block);
408 block.state = STATE_FALLING;
409 changed = true;
410 }
389 411 }
390 412 };
391 413
@@ -421,24 +443,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
421 443 var cluster = this.clusters[flyer.clusterId];
422 444 if (!cluster || cluster.phase === 'landed')
423 445 { return false; }
424 var roof = flyer.y + 1;
425 for (var i = 0; i < this.blocks.length; i++)
446 this.prepareClusterBody(cluster);
447 var maxOff = null;
448 var members = this.clusterMembers(cluster.id);
449 for (var i = 0; i < members.length; i++)
426 450 {
427 var other = this.blocks[i];
428 if (other === block || other.col !== block.col)
429 { continue; }
430 if (other.clusterId !== cluster.id)
451 if (members[i].col !== block.col)
431 452 { continue; }
432 if (other.y + 1 > roof)
433 { roof = other.y + 1; }
453 if (maxOff == null || members[i].clusterOffset > maxOff)
454 { maxOff = members[i].clusterOffset; }
434 455 }
435 var oldN = this.clusterMembers(cluster.id).length;
456 var oldN = members.length;
436 457 var fallingVy = block.vy;
437 block.y = roof;
458 if (block.clusterId != null && block.clusterId !== cluster.id)
459 { this.leaveCluster(block); }
438 460 block.state = STATE_FLYING;
439 461 block.clusterId = cluster.id;
462 block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1;
440 463 block.flame = false;
441 464 block.settleTime = 0;
465 block.manualPhase = null;
466 block.manualTarget = null;
467 block.manualDecel = 0;
468 block.pushedClusters = null;
442 469 // A cluster born from a thrown block averages in falling cargo
443 470 // the same way two flying clusters merge (t1108). Other rockets
444 471 // keep their velocity and just pick the cargo up.
@@ -449,12 +476,8 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
449 476 cluster.decel = ((cluster.decel || 0) * oldN) / newN;
450 477 cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
451 478 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 479 }
456 else
457 { block.vy = cluster.vy; }
480 this.syncClusterMembers(cluster);
458 481 return true;
459 482 };
460 483
@@ -512,12 +535,14 @@ GameCore.prototype.tick = function (dt)
512 535 while (left > 1e-12)
513 536 {
514 537 var step = Math.min(SUBSTEP, left);
538 this.snapshotPrevY();
515 539 this.integrateFalling(step);
516 540 this.resolveFlyingMatches();
517 541 this.integrateClusters(step);
518 542 this.mergeFlyingClusters();
519 543 this.integrateManual(step);
520 544 this.mergeAnyManualFallingOverlaps();
545 this.resolveAirborneCollisions();
521 546 this.cullOffTop();
522 547 left -= step;
523 548 }
@@ -534,6 +559,7 @@ GameCore.prototype.tick = function (dt)
534 559 { this.resolveMatches(); }
535 560 this.checkLocks();
536 561 this.checkGameOver();
562 this.assertInvariants('tick');
537 563 };
538 564
539 565 /** Step a stretch of time in fixed substeps. Useful for tests. */
@@ -611,7 +637,7 @@ GameCore.prototype.integrateFalling = function (dt)
611 637 if (this.attachToFlyingCluster(block, flyer))
612 638 { continue; }
613 639 }
614 var top = this.stackTop(block.col);
640 var top = this.stackTop(block.col, null, block.y);
615 641 var below = this.highestFallingBelow(block);
616 642 if (below && below.y + 1 > top)
617 643 { top = below.y + 1; }
@@ -725,14 +751,12 @@ GameCore.prototype.integrateClusters = function (dt)
725 751 if (cluster.phase === 'landed' || cluster.phase === 'hold')
726 752 { continue; }
727 753
754 this.prepareClusterBody(cluster);
728 755 var dy = this.advanceJump(cluster, dt);
729 for (var i = 0; i < members.length; i++)
730 {
731 members[i].y += dy;
732 members[i].vy = cluster.vy;
733 }
756 cluster.anchorY += dy;
757 this.syncClusterMembers(cluster);
734 758
735 this.catchFallingOntoCluster(cluster, members);
759 this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id));
736 760 this.checkChainRelaunch(cluster);
737 761
738 762 if (cluster.phase === 'down' && this.clusters[cluster.id])
@@ -801,13 +825,30 @@ GameCore.prototype.flyingClustersOverlap = function (a, b)
801 825 {
802 826 if (ma[i].col !== mb[j].col)
803 827 { continue; }
804 if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9)
828 if (this.sweptIntervalsOverlap(ma[i], mb[j]))
805 829 { return true; }
806 830 }
807 831 }
808 832 return false;
809 833 };
810 834
835 /**
836 * Swept [y, y+1) of two blocks this substep, using _prevY when present.
837 * Catches pairs that crossed without sharing a cell at either endpoint.
838 */
839 GameCore.prototype.sweptIntervalsOverlap = function (a, b)
840 {
841 var a0 = (a._prevY != null) ? a._prevY : a.y;
842 var a1 = a.y;
843 var b0 = (b._prevY != null) ? b._prevY : b.y;
844 var b1 = b.y;
845 var aLo = Math.min(a0, a1);
846 var aHi = Math.max(a0, a1) + 1;
847 var bLo = Math.min(b0, b1);
848 var bHi = Math.max(b0, b1) + 1;
849 return aLo < bHi - 1e-9 && bLo < aHi - 1e-9;
850 };
851
811 852 GameCore.prototype.mergeClusterGroup = function (clusters)
812 853 {
813 854 clusters.sort(function (a, b) { return a.id - b.id; });
@@ -843,7 +884,7 @@ GameCore.prototype.mergeClusterGroup = function (clusters)
843 884 if (clusters[c] !== keep)
844 885 { delete this.clusters[clusters[c].id]; }
845 886 }
846 this.packClusterOverlaps(keep);
887 this.prepareClusterBody(keep);
847 888 this.checkChainRelaunch(keep);
848 889 if (keep.phase === 'down' && this.clusters[keep.id])
849 890 { this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
@@ -874,6 +915,297 @@ GameCore.prototype.packClusterOverlaps = function (cluster)
874 915 }
875 916 };
876 917
918
919 /**
920 * Derive integer cell offsets from current member positions and a
921 * single anchorY, packing overlaps in each column while preserving
922 * order (lower y stays lower; ties break on id). Then sync.
923 * Flying clusters are one rigid body: one y, one vy, integer offsets.
924 */
925 GameCore.prototype.prepareClusterBody = function (cluster)
926 {
927 if (!cluster)
928 { return; }
929 var members = this.clusterMembers(cluster.id);
930 if (!members.length)
931 { return; }
932 this.rebuildClusterOffsets(cluster, members);
933 };
934
935 GameCore.prototype.rebuildClusterOffsets = function (cluster, members)
936 {
937 members = members || this.clusterMembers(cluster.id);
938 if (!members.length)
939 { return; }
940 var byCol = Object.create(null);
941 for (var i = 0; i < members.length; i++)
942 {
943 var col = members[i].col;
944 if (!byCol[col])
945 { byCol[col] = []; }
946 byCol[col].push(members[i]);
947 }
948 var minY = Infinity;
949 for (var i = 0; i < members.length; i++)
950 {
951 if (members[i].y < minY)
952 { minY = members[i].y; }
953 }
954 if (cluster.anchorY == null || !isFinite(cluster.anchorY))
955 { cluster.anchorY = minY; }
956 var cols = Object.keys(byCol);
957 for (var c = 0; c < cols.length; c++)
958 {
959 var list = byCol[cols[c]];
960 list.sort(function (a, b)
961 {
962 if (a.y !== b.y)
963 { return a.y - b.y; }
964 return a.id - b.id;
965 });
966 var prevOff = -Infinity;
967 for (var i = 0; i < list.length; i++)
968 {
969 var off = Math.round(list[i].y - cluster.anchorY);
970 if (off <= prevOff)
971 { off = prevOff + 1; }
972 list[i].clusterOffset = off;
973 prevOff = off;
974 }
975 }
976 // Re-base so the lowest offset is 0, keeping relative integers.
977 var minOff = Infinity;
978 for (var i = 0; i < members.length; i++)
979 {
980 if (members[i].clusterOffset < minOff)
981 { minOff = members[i].clusterOffset; }
982 }
983 if (minOff !== 0 && isFinite(minOff))
984 {
985 cluster.anchorY += minOff;
986 for (var i = 0; i < members.length; i++)
987 { members[i].clusterOffset -= minOff; }
988 }
989 this.syncClusterMembers(cluster);
990 };
991
992 GameCore.prototype.syncClusterMembers = function (cluster)
993 {
994 if (!cluster)
995 { return; }
996 var members = this.clusterMembers(cluster.id);
997 if (cluster.anchorY == null)
998 {
999 var minY = Infinity;
1000 for (var i = 0; i < members.length; i++)
1001 {
1002 if (members[i].y < minY)
1003 { minY = members[i].y; }
1004 }
1005 cluster.anchorY = isFinite(minY) ? minY : 0;
1006 }
1007 for (var i = 0; i < members.length; i++)
1008 {
1009 var block = members[i];
1010 if (block.clusterOffset == null)
1011 { block.clusterOffset = Math.round(block.y - cluster.anchorY); }
1012 block.y = cluster.anchorY + block.clusterOffset;
1013 if (cluster.phase !== 'landed')
1014 { block.vy = cluster.vy; }
1015 }
1016 };
1017
1018 GameCore.prototype.snapshotPrevY = function ()
1019 {
1020 for (var i = 0; i < this.blocks.length; i++)
1021 { this.blocks[i]._prevY = this.blocks[i].y; }
1022 };
1023
1024 /**
1025 * Safety net after the specific integrates: any remaining mid-air
1026 * overlap or swept crossing is resolved with the same rules, then
1027 * every flying cluster is rebuilt as a rigid body. Repeats a few
1028 * times because one attach can create a new contact.
1029 */
1030 GameCore.prototype.resolveAirborneCollisions = function ()
1031 {
1032 var guard = 0;
1033 while (guard++ < this.height + 4)
1034 {
1035 var changed = false;
1036
1037 // Manual vs falling / cluster / manual.
1038 var manuals = [];
1039 for (var i = 0; i < this.blocks.length; i++)
1040 {
1041 if (this.blocks[i].state === STATE_MANUAL)
1042 { manuals.push(this.blocks[i]); }
1043 }
1044 manuals.sort(function (a, b) { return b.y - a.y; });
1045 for (var m = 0; m < manuals.length; m++)
1046 {
1047 var manual = manuals[m];
1048 if (manual.state !== STATE_MANUAL)
1049 { continue; }
1050 var oldY = (manual._prevY != null) ? manual._prevY : manual.y;
1051 if (this.resolveManualCollisions(manual, oldY, manual.y))
1052 { changed = true; }
1053 }
1054
1055 // Falling vs flying (sit on the roof).
1056 for (var i = 0; i < this.blocks.length; i++)
1057 {
1058 var block = this.blocks[i];
1059 if (block.state !== STATE_FALLING)
1060 { continue; }
1061 var flyer = this.highestFlyingBelow(block);
1062 if (!flyer)
1063 {
1064 // Also a flyer whose swept path crosses this falling block.
1065 flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y);
1066 }
1067 if (flyer && this.sweptIntervalsOverlap(block, flyer))
1068 {
1069 if (this.attachToFlyingCluster(block, flyer))
1070 { changed = true; }
1071 }
1072 }
1073
1074 // Cluster vs cluster (swept, including after a column push).
1075 var before = Object.keys(this.clusters).length;
1076 this.mergeFlyingClusters();
1077 if (Object.keys(this.clusters).length !== before)
1078 { changed = true; }
1079
1080 // Rebuild every flying body so offsets stay integer and unique.
1081 var ids = Object.keys(this.clusters);
1082 for (var n = 0; n < ids.length; n++)
1083 {
1084 var cluster = this.clusters[ids[n]];
1085 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1086 { continue; }
1087 this.prepareClusterBody(cluster);
1088 }
1089
1090 if (!changed)
1091 { break; }
1092 }
1093 };
1094
1095 /**
1096 * Invariant checker (t1112). No two blocks occupy the same cell or
1097 * overlap their [y, y+1) intervals. Flying-cluster members are
1098 * grid-aligned to a single anchor. Returns a list of violation
1099 * strings (empty if clean).
1100 */
1101 GameCore.prototype.checkInvariants = function ()
1102 {
1103 var issues = [];
1104 var byCol = Object.create(null);
1105 var ids = Object.create(null);
1106 for (var i = 0; i < this.blocks.length; i++)
1107 {
1108 var block = this.blocks[i];
1109 if (ids[block.id])
1110 { issues.push('duplicate id ' + block.id); }
1111 ids[block.id] = true;
1112 if (!byCol[block.col])
1113 { byCol[block.col] = []; }
1114 byCol[block.col].push(block);
1115 if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet
1116 && block.color >= this.planet.colors.length))
1117 { issues.push('block ' + block.id + ' has invalid color ' + block.color); }
1118 }
1119 var cols = Object.keys(byCol);
1120 for (var c = 0; c < cols.length; c++)
1121 {
1122 var list = byCol[cols[c]];
1123 list.sort(function (a, b)
1124 {
1125 if (a.y !== b.y)
1126 { return a.y - b.y; }
1127 return a.id - b.id;
1128 });
1129 for (var i = 0; i < list.length; i++)
1130 {
1131 var a = list[i];
1132 if (i > 0)
1133 {
1134 var prev = list[i - 1];
1135 if (a.y < prev.y + 1 - 1e-6)
1136 {
1137 issues.push('overlap col=' + a.col
1138 + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)
1139 + ' gap=' + (a.y - prev.y));
1140 }
1141 var cellA = Math.floor(a.y + 1e-9);
1142 var cellP = Math.floor(prev.y + 1e-9);
1143 if (cellA === cellP)
1144 {
1145 issues.push('duplicate cell col=' + a.col + ' row=' + cellA
1146 + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a));
1147 }
1148 }
1149 }
1150 }
1151 var cids = Object.keys(this.clusters);
1152 for (var n = 0; n < cids.length; n++)
1153 {
1154 var cluster = this.clusters[cids[n]];
1155 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1156 { continue; }
1157 var mem = this.clusterMembers(cluster.id);
1158 var seenOff = Object.create(null);
1159 for (var i = 0; i < mem.length; i++)
1160 {
1161 var b = mem[i];
1162 if (cluster.anchorY != null && b.clusterOffset != null)
1163 {
1164 var expected = cluster.anchorY + b.clusterOffset;
1165 if (Math.abs(b.y - expected) > 1e-6)
1166 {
1167 issues.push('cluster ' + cluster.id + ' member ' + b.id
1168 + ' y=' + b.y + ' != anchor+off ' + expected);
1169 }
1170 if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9)
1171 {
1172 issues.push('cluster ' + cluster.id + ' member ' + b.id
1173 + ' non-integer offset ' + b.clusterOffset);
1174 }
1175 var key = b.col + ':' + b.clusterOffset;
1176 if (seenOff[key])
1177 {
1178 issues.push('cluster ' + cluster.id + ' duplicate offset ' + key);
1179 }
1180 seenOff[key] = true;
1181 }
1182 }
1183 }
1184 return issues;
1185 };
1186
1187 GameCore.prototype._invBlock = function (b)
1188 {
1189 return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y='
1190 + (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')';
1191 };
1192
1193 GameCore.prototype.assertInvariants = function (label)
1194 {
1195 if (!this.strictInvariants && !this.debugInvariants)
1196 { return; }
1197 var issues = this.checkInvariants();
1198 this.lastInvariantViolations = issues;
1199 if (!issues.length)
1200 { return; }
1201 var msg = 'physics invariants failed'
1202 + (label ? ' at ' + label : '')
1203 + ':\n ' + issues.join('\n ')
1204 + '\n' + (this.snapshot ? this.snapshot().picture : '');
1205 if (this.strictInvariants)
1206 { throw new Error(msg); }
1207 };
1208
877 1209 GameCore.prototype.tryLandCluster = function (cluster, members)
878 1210 {
879 1211 if (!members.length)
@@ -886,7 +1218,7 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
886 1218 var top = this.stackTop(block.col, function (other)
887 1219 {
888 1220 return other.clusterId === cluster.id;
889 });
1221 }, block.y);
890 1222 if (block.y <= top + 1e-9)
891 1223 {
892 1224 hit = true;
@@ -898,9 +1230,10 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
898 1230 if (!hit)
899 1231 { return; }
900 1232 // Lift the whole rigid body so the deepest contact sits flush.
901 for (var i = 0; i < members.length; i++)
902 { members[i].y += shift; }
903 this.landCluster(cluster, members);
1233 this.prepareClusterBody(cluster);
1234 cluster.anchorY += shift;
1235 this.syncClusterMembers(cluster);
1236 this.landCluster(cluster, this.clusterMembers(cluster.id));
904 1237 };
905 1238
906 1239 /**
@@ -1001,12 +1334,11 @@ GameCore.prototype.integrateManual = function (dt)
1001 1334 var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
1002 1335 var dy = this.advanceJump(proxy, dt);
1003 1336 var nextY = oldY + dy;
1004 this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));
1005 1337 block.vy = proxy.vy;
1006 1338 block.manualPhase = proxy.phase;
1007 block.y = Math.min(nextY, block.manualTarget);
1008 if (this.tryMergeManualWithFalling(block, oldY, block.y))
1339 if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget)))
1009 1340 { continue; }
1341 block.y = Math.min(nextY, block.manualTarget);
1010 1342 if (block.y >= block.manualTarget - 1e-12)
1011 1343 {
1012 1344 block.y = block.manualTarget;
@@ -1027,7 +1359,7 @@ GameCore.prototype.integrateManual = function (dt)
1027 1359 var dy = this.advanceJump(proxy, dt);
1028 1360 block.vy = proxy.vy;
1029 1361 var nextY = block.y + dy;
1030 var top = this.stackTop(block.col);
1362 var top = this.stackTop(block.col, null, block.y);
1031 1363 if (nextY <= top)
1032 1364 {
1033 1365 block.y = top;
@@ -1039,9 +1371,10 @@ GameCore.prototype.integrateManual = function (dt)
1039 1371 }
1040 1372 else
1041 1373 {
1042 block.y = Math.min(nextY, this.height - 1);
1043 if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1374 var downNext = Math.min(nextY, this.height - 1);
1375 if (this.resolveManualCollisions(block, block.y, downNext))
1044 1376 { continue; }
1377 block.y = downNext;
1045 1378 }
1046 1379 }
1047 1380 }
@@ -1216,7 +1549,7 @@ GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)
1216 1549 }
1217 1550 if (this.drag && this.drag.blockId === manual.id)
1218 1551 { this.drag = null; }
1219 this.packClusterOverlaps(cluster);
1552 this.prepareClusterBody(cluster);
1220 1553 this.checkChainRelaunch(cluster);
1221 1554 return cluster;
1222 1555 };
@@ -1241,40 +1574,217 @@ GameCore.prototype.mergeAnyManualFallingOverlaps = function ()
1241 1574 }
1242 1575 };
1243 1576
1244 GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1577 /**
1578 * Resolve collisions of a thrown (manual) block along [oldY, nextY].
1579 * Falling blocks merge (t1108). A flying cluster is pushed one cell up
1580 * in the hit column and the shot attaches at the bottom (t1111).
1581 * Another thrown block is promoted to a 1-block cluster and then the
1582 * same attach-at-bottom rule applies. Returns true if `manual` is no
1583 * longer in the MANUAL state.
1584 */
1585 GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY)
1245 1586 {
1246 if (!manual.pushedClusters)
1247 { manual.pushedClusters = Object.create(null); }
1587 if (!manual || manual.state !== STATE_MANUAL)
1588 { return true; }
1248 1589
1249 var manualTopOld = oldY + 1;
1250 var manualTopNew = nextY + 1;
1251 var low = Math.min(oldY, nextY);
1252 var high = Math.max(oldY, nextY) + 1;
1590 var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY);
1591 if (fallingHits.length)
1592 {
1593 this.mergeManualWithFalling(manual, fallingHits);
1594 return manual.state !== STATE_MANUAL;
1595 }
1253 1596
1597 var flyer = this.flyingHitOnManualPath(manual, oldY, nextY);
1598 if (flyer)
1599 {
1600 var cluster = this.clusters[flyer.clusterId];
1601 if (cluster && cluster.phase !== 'landed')
1602 {
1603 if (nextY + 1e-12 < oldY)
1604 { this.attachToFlyingCluster(manual, flyer); }
1605 else
1606 { this.attachShotUnderCluster(manual, cluster, manual.col); }
1607 return manual.state !== STATE_MANUAL;
1608 }
1609 }
1610
1611 var other = this.manualHitOnManualPath(manual, oldY, nextY);
1612 if (other)
1613 {
1614 var host = this.promoteManualToCluster(other);
1615 if (host)
1616 {
1617 if (nextY + 1e-12 < oldY)
1618 { this.attachToFlyingCluster(manual, other); }
1619 else
1620 { this.attachShotUnderCluster(manual, host, manual.col); }
1621 return manual.state !== STATE_MANUAL;
1622 }
1623 }
1624 return false;
1625 };
1626
1627 /** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */
1628 GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY)
1629 {
1630 var lo = Math.min(oldY, nextY);
1631 var hi = Math.max(oldY, nextY) + 1;
1632 var best = null;
1633 var bestY = Infinity;
1254 1634 for (var i = 0; i < this.blocks.length; i++)
1255 1635 {
1256 1636 var other = this.blocks[i];
1257 if (other === manual)
1258 { continue; }
1259 if (other.col !== manual.col)
1637 if (other === manual || other.col !== manual.col)
1260 1638 { continue; }
1261 1639 if (other.state !== STATE_FLYING || other.clusterId == null)
1262 1640 { continue; }
1263 if (manual.pushedClusters[other.clusterId])
1641 var cluster = this.clusters[other.clusterId];
1642 if (!cluster || cluster.phase === 'landed')
1264 1643 { continue; }
1265 var otherBot = other.y;
1266 var otherTop = other.y + 1;
1267 var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;
1268 var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;
1269 if (overlapping || crossing)
1644 var o0 = (other._prevY != null) ? other._prevY : other.y;
1645 var o1 = other.y;
1646 var oLo = Math.min(o0, o1);
1647 var oHi = Math.max(o0, o1) + 1;
1648 if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
1270 1649 {
1271 if (this.pushClusterColumn(other.clusterId, manual.col))
1650 if (other.y < bestY)
1272 1651 {
1273 manual.pushedClusters[other.clusterId] = true;
1274 return;
1652 bestY = other.y;
1653 best = other;
1275 1654 }
1276 1655 }
1277 1656 }
1657 return best;
1658 };
1659
1660 /** Another thrown block in the same column on the path (prefer the lower). */
1661 GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY)
1662 {
1663 var lo = Math.min(oldY, nextY);
1664 var hi = Math.max(oldY, nextY) + 1;
1665 var best = null;
1666 for (var i = 0; i < this.blocks.length; i++)
1667 {
1668 var other = this.blocks[i];
1669 if (other === manual || other.col !== manual.col)
1670 { continue; }
1671 if (other.state !== STATE_MANUAL)
1672 { continue; }
1673 var o0 = (other._prevY != null) ? other._prevY : other.y;
1674 var o1 = other.y;
1675 var oLo = Math.min(o0, o1);
1676 var oHi = Math.max(o0, o1) + 1;
1677 if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
1678 {
1679 if (!best || other.y < best.y)
1680 { best = other; }
1681 }
1682 }
1683 return best;
1684 };
1685
1686 /**
1687 * t1111: push every member of `cluster` in `col` up by one cell, then
1688 * attach `manual` at the bottom of that column, grid-aligned, as part
1689 * of the cluster. The cluster keeps its velocity; the shot takes it.
1690 * Other columns are untouched. Same rule for every successive shot.
1691 */
1692 GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col)
1693 {
1694 if (!manual || !cluster || !this.clusters[cluster.id])
1695 { return false; }
1696 if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id)
1697 {
1698 // Already belonging elsewhere: leave first.
1699 if (manual.clusterId != null)
1700 { this.leaveCluster(manual); }
1701 }
1702 this.prepareClusterBody(cluster);
1703 var members = this.clusterMembers(cluster.id);
1704 var minOff = null;
1705 for (var i = 0; i < members.length; i++)
1706 {
1707 if (members[i].col !== col)
1708 { continue; }
1709 if (minOff == null || members[i].clusterOffset < minOff)
1710 { minOff = members[i].clusterOffset; }
1711 }
1712 if (minOff == null)
1713 { minOff = 0; }
1714 else
1715 {
1716 for (var i = 0; i < members.length; i++)
1717 {
1718 if (members[i].col === col)
1719 { members[i].clusterOffset += 1; }
1720 }
1721 }
1722 if (this.drag && this.drag.blockId === manual.id)
1723 { this.drag = null; }
1724 manual.clusterId = cluster.id;
1725 manual.clusterOffset = minOff;
1726 manual.state = STATE_FLYING;
1727 manual.flame = manual.color === COLOR_GRAY;
1728 manual.settleTime = 0;
1729 manual.manualPhase = null;
1730 manual.manualTarget = null;
1731 manual.manualDecel = 0;
1732 manual.manualJumpHeight = 0;
1733 manual.pushedClusters = null;
1734 this.syncClusterMembers(cluster);
1735 this.cullOffTop();
1736 if (this.clusters[cluster.id])
1737 { this.checkChainRelaunch(cluster); }
1738 return true;
1739 };
1740
1741 /**
1742 * Turn a lone thrown block into a 1-block flying cluster so a later
1743 * shot can use the t1111 attach-at-bottom rule. A single throw stays
1744 * MANUAL (and is capped at the top of the field) until it collides.
1745 */
1746 GameCore.prototype.promoteManualToCluster = function (manual)
1747 {
1748 if (!manual)
1749 { return null; }
1750 if (manual.state === STATE_FLYING && manual.clusterId != null)
1751 {
1752 var existing = this.clusters[manual.clusterId];
1753 if (existing && existing.phase !== 'landed')
1754 { return existing; }
1755 }
1756 if (manual.state !== STATE_MANUAL)
1757 { return null; }
1758 var height = manual.manualJumpHeight != null ? manual.manualJumpHeight
1759 : Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y);
1760 var cluster = {
1761 id: this.nextClusterId++,
1762 jumpHeight: height,
1763 vy: manual.vy,
1764 decel: manual.manualDecel || 0,
1765 phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up',
1766 fromManual: true,
1767 anchorY: manual.y
1768 };
1769 this.clusters[cluster.id] = cluster;
1770 manual.state = STATE_FLYING;
1771 manual.clusterId = cluster.id;
1772 manual.clusterOffset = 0;
1773 manual.flame = manual.color === COLOR_GRAY;
1774 manual.settleTime = 0;
1775 manual.manualPhase = null;
1776 manual.manualTarget = null;
1777 manual.manualDecel = 0;
1778 manual.manualJumpHeight = 0;
1779 manual.pushedClusters = null;
1780 this.syncClusterMembers(cluster);
1781 return cluster;
1782 };
1783
1784 /** Kept for older tests / call sites: push a cluster column by one cell. */
1785 GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1786 {
1787 return this.resolveManualCollisions(manual, oldY, nextY);
1278 1788 };
1279 1789
1280 1790
@@ -1787,6 +2297,7 @@ GameCore.prototype.launchCluster = function (matchedBlocks, height)
1787 2297 block.vy = cluster.vy;
1788 2298 block.settleTime = 0;
1789 2299 }
2300 this.prepareClusterBody(cluster);
1790 2301 return cluster;
1791 2302 };
1792 2303
@@ -80,6 +80,11 @@ function GameCore (planet, rng)
80 this.outgoingShots = [];
80 this.outgoingShots = [];
81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
81 // Stage-2 default; stage 3 replaces this with Planet.missChance (Power).
82 this.missChance = optsMissChance(arguments);
82 this.missChance = optsMissChance(arguments);
83 // t1112: invariant checker. strictInvariants throws (tests);
84 // debugInvariants records violations for the ?debug overlay.
85 this.strictInvariants = false;
86 this.debugInvariants = false;
87 this.lastInvariantViolations = [];
83}
88}
84
89
85function optsMissChance (args)
90function optsMissChance (args)
@@ -179,7 +184,12 @@ GameCore.prototype.isGrounded = function (block)
179 || block.state === STATE_LOCKED;
184 || block.state === STATE_LOCKED;
180};
185};
181
186
182GameCore.prototype.stackTop = function (col, ignore)
187/**
188 * Highest grounded landing surface in `col`. If `belowY` is given, only
189 * blocks strictly below that y count — a hanging still block above a
190 * gap must never pull a falling or thrown block upward through the gap.
191 */
192GameCore.prototype.stackTop = function (col, ignore, belowY)
183{
193{
184 var top = 0;
194 var top = 0;
185 for (var i = 0; i < this.blocks.length; i++)
195 for (var i = 0; i < this.blocks.length; i++)
@@ -189,6 +199,8 @@ GameCore.prototype.stackTop = function (col, ignore)
189 { continue; }
199 { continue; }
190 if (ignore && ignore(block))
200 if (ignore && ignore(block))
191 { continue; }
201 { continue; }
202 if (belowY != null && block.y + 1e-9 >= belowY)
203 { continue; }
192 if (this.isGrounded(block))
204 if (this.isGrounded(block))
193 {
205 {
194 var surface = block.y + 1;
206 var surface = block.y + 1;
@@ -374,18 +386,28 @@ GameCore.prototype.clearFlightState = function (block)
374
386
375GameCore.prototype.releaseUnsupported = function ()
387GameCore.prototype.releaseUnsupported = function ()
376{
388{
377 for (var i = 0; i < this.blocks.length; i++)
389 // Repeat until stable: a still sitting on a still that itself just
390 // became falling must also drop. Two fixed passes are not enough
391 // for a chain of hanging blocks (t1112).
392 var guard = 0;
393 var changed = true;
394 while (changed && guard++ < this.height + 2)
378 {
395 {
379 var block = this.blocks[i];
396 changed = false;
380 if (block.state !== STATE_STILL)
397 for (var i = 0; i < this.blocks.length; i++)
381 { continue; }
398 {
382 if (this.drag && this.drag.blockId === block.id)
399 var block = this.blocks[i];
383 { continue; }
400 if (block.state !== STATE_STILL)
384 if (this.hasSupport(block))
401 { continue; }
385 { continue; }
402 if (this.drag && this.drag.blockId === block.id)
386 this.leaveCluster(block);
403 { continue; }
387 this.clearFlightState(block);
404 if (this.hasSupport(block))
388 block.state = STATE_FALLING;
405 { continue; }
406 this.leaveCluster(block);
407 this.clearFlightState(block);
408 block.state = STATE_FALLING;
409 changed = true;
410 }
389 }
411 }
390};
412};
391
413
@@ -421,24 +443,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
421 var cluster = this.clusters[flyer.clusterId];
443 var cluster = this.clusters[flyer.clusterId];
422 if (!cluster || cluster.phase === 'landed')
444 if (!cluster || cluster.phase === 'landed')
423 { return false; }
445 { return false; }
424 var roof = flyer.y + 1;
446 this.prepareClusterBody(cluster);
425 for (var i = 0; i < this.blocks.length; i++)
447 var maxOff = null;
448 var members = this.clusterMembers(cluster.id);
449 for (var i = 0; i < members.length; i++)
426 {
450 {
427 var other = this.blocks[i];
451 if (members[i].col !== block.col)
428 if (other === block || other.col !== block.col)
429 { continue; }
430 if (other.clusterId !== cluster.id)
431 { continue; }
452 { continue; }
432 if (other.y + 1 > roof)
453 if (maxOff == null || members[i].clusterOffset > maxOff)
433 { roof = other.y + 1; }
454 { maxOff = members[i].clusterOffset; }
434 }
455 }
435 var oldN = this.clusterMembers(cluster.id).length;
456 var oldN = members.length;
436 var fallingVy = block.vy;
457 var fallingVy = block.vy;
437 block.y = roof;
458 if (block.clusterId != null && block.clusterId !== cluster.id)
459 { this.leaveCluster(block); }
438 block.state = STATE_FLYING;
460 block.state = STATE_FLYING;
439 block.clusterId = cluster.id;
461 block.clusterId = cluster.id;
462 block.clusterOffset = (maxOff == null) ? 0 : maxOff + 1;
440 block.flame = false;
463 block.flame = false;
441 block.settleTime = 0;
464 block.settleTime = 0;
465 block.manualPhase = null;
466 block.manualTarget = null;
467 block.manualDecel = 0;
468 block.pushedClusters = null;
442 // A cluster born from a thrown block averages in falling cargo
469 // A cluster born from a thrown block averages in falling cargo
443 // the same way two flying clusters merge (t1108). Other rockets
470 // the same way two flying clusters merge (t1108). Other rockets
444 // keep their velocity and just pick the cargo up.
471 // keep their velocity and just pick the cargo up.
@@ -449,12 +476,8 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
449 cluster.decel = ((cluster.decel || 0) * oldN) / newN;
476 cluster.decel = ((cluster.decel || 0) * oldN) / newN;
450 cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
477 cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
451 cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';
478 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 }
479 }
456 else
480 this.syncClusterMembers(cluster);
457 { block.vy = cluster.vy; }
458 return true;
481 return true;
459};
482};
460
483
@@ -512,12 +535,14 @@ GameCore.prototype.tick = function (dt)
512 while (left > 1e-12)
535 while (left > 1e-12)
513 {
536 {
514 var step = Math.min(SUBSTEP, left);
537 var step = Math.min(SUBSTEP, left);
538 this.snapshotPrevY();
515 this.integrateFalling(step);
539 this.integrateFalling(step);
516 this.resolveFlyingMatches();
540 this.resolveFlyingMatches();
517 this.integrateClusters(step);
541 this.integrateClusters(step);
518 this.mergeFlyingClusters();
542 this.mergeFlyingClusters();
519 this.integrateManual(step);
543 this.integrateManual(step);
520 this.mergeAnyManualFallingOverlaps();
544 this.mergeAnyManualFallingOverlaps();
545 this.resolveAirborneCollisions();
521 this.cullOffTop();
546 this.cullOffTop();
522 left -= step;
547 left -= step;
523 }
548 }
@@ -534,6 +559,7 @@ GameCore.prototype.tick = function (dt)
534 { this.resolveMatches(); }
559 { this.resolveMatches(); }
535 this.checkLocks();
560 this.checkLocks();
536 this.checkGameOver();
561 this.checkGameOver();
562 this.assertInvariants('tick');
537};
563};
538
564
539/** Step a stretch of time in fixed substeps. Useful for tests. */
565/** Step a stretch of time in fixed substeps. Useful for tests. */
@@ -611,7 +637,7 @@ GameCore.prototype.integrateFalling = function (dt)
611 if (this.attachToFlyingCluster(block, flyer))
637 if (this.attachToFlyingCluster(block, flyer))
612 { continue; }
638 { continue; }
613 }
639 }
614 var top = this.stackTop(block.col);
640 var top = this.stackTop(block.col, null, block.y);
615 var below = this.highestFallingBelow(block);
641 var below = this.highestFallingBelow(block);
616 if (below && below.y + 1 > top)
642 if (below && below.y + 1 > top)
617 { top = below.y + 1; }
643 { top = below.y + 1; }
@@ -725,14 +751,12 @@ GameCore.prototype.integrateClusters = function (dt)
725 if (cluster.phase === 'landed' || cluster.phase === 'hold')
751 if (cluster.phase === 'landed' || cluster.phase === 'hold')
726 { continue; }
752 { continue; }
727
753
754 this.prepareClusterBody(cluster);
728 var dy = this.advanceJump(cluster, dt);
755 var dy = this.advanceJump(cluster, dt);
729 for (var i = 0; i < members.length; i++)
756 cluster.anchorY += dy;
730 {
757 this.syncClusterMembers(cluster);
731 members[i].y += dy;
732 members[i].vy = cluster.vy;
733 }
734
758
735 this.catchFallingOntoCluster(cluster, members);
759 this.catchFallingOntoCluster(cluster, this.clusterMembers(cluster.id));
736 this.checkChainRelaunch(cluster);
760 this.checkChainRelaunch(cluster);
737
761
738 if (cluster.phase === 'down' && this.clusters[cluster.id])
762 if (cluster.phase === 'down' && this.clusters[cluster.id])
@@ -801,13 +825,30 @@ GameCore.prototype.flyingClustersOverlap = function (a, b)
801 {
825 {
802 if (ma[i].col !== mb[j].col)
826 if (ma[i].col !== mb[j].col)
803 { continue; }
827 { continue; }
804 if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9)
828 if (this.sweptIntervalsOverlap(ma[i], mb[j]))
805 { return true; }
829 { return true; }
806 }
830 }
807 }
831 }
808 return false;
832 return false;
809};
833};
810
834
835/**
836 * Swept [y, y+1) of two blocks this substep, using _prevY when present.
837 * Catches pairs that crossed without sharing a cell at either endpoint.
838 */
839GameCore.prototype.sweptIntervalsOverlap = function (a, b)
840{
841 var a0 = (a._prevY != null) ? a._prevY : a.y;
842 var a1 = a.y;
843 var b0 = (b._prevY != null) ? b._prevY : b.y;
844 var b1 = b.y;
845 var aLo = Math.min(a0, a1);
846 var aHi = Math.max(a0, a1) + 1;
847 var bLo = Math.min(b0, b1);
848 var bHi = Math.max(b0, b1) + 1;
849 return aLo < bHi - 1e-9 && bLo < aHi - 1e-9;
850};
851
811GameCore.prototype.mergeClusterGroup = function (clusters)
852GameCore.prototype.mergeClusterGroup = function (clusters)
812{
853{
813 clusters.sort(function (a, b) { return a.id - b.id; });
854 clusters.sort(function (a, b) { return a.id - b.id; });
@@ -843,7 +884,7 @@ GameCore.prototype.mergeClusterGroup = function (clusters)
843 if (clusters[c] !== keep)
884 if (clusters[c] !== keep)
844 { delete this.clusters[clusters[c].id]; }
885 { delete this.clusters[clusters[c].id]; }
845 }
886 }
846 this.packClusterOverlaps(keep);
887 this.prepareClusterBody(keep);
847 this.checkChainRelaunch(keep);
888 this.checkChainRelaunch(keep);
848 if (keep.phase === 'down' && this.clusters[keep.id])
889 if (keep.phase === 'down' && this.clusters[keep.id])
849 { this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
890 { this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
@@ -874,6 +915,297 @@ GameCore.prototype.packClusterOverlaps = function (cluster)
874 }
915 }
875};
916};
876
917
918
919/**
920 * Derive integer cell offsets from current member positions and a
921 * single anchorY, packing overlaps in each column while preserving
922 * order (lower y stays lower; ties break on id). Then sync.
923 * Flying clusters are one rigid body: one y, one vy, integer offsets.
924 */
925GameCore.prototype.prepareClusterBody = function (cluster)
926{
927 if (!cluster)
928 { return; }
929 var members = this.clusterMembers(cluster.id);
930 if (!members.length)
931 { return; }
932 this.rebuildClusterOffsets(cluster, members);
933};
934
935GameCore.prototype.rebuildClusterOffsets = function (cluster, members)
936{
937 members = members || this.clusterMembers(cluster.id);
938 if (!members.length)
939 { return; }
940 var byCol = Object.create(null);
941 for (var i = 0; i < members.length; i++)
942 {
943 var col = members[i].col;
944 if (!byCol[col])
945 { byCol[col] = []; }
946 byCol[col].push(members[i]);
947 }
948 var minY = Infinity;
949 for (var i = 0; i < members.length; i++)
950 {
951 if (members[i].y < minY)
952 { minY = members[i].y; }
953 }
954 if (cluster.anchorY == null || !isFinite(cluster.anchorY))
955 { cluster.anchorY = minY; }
956 var cols = Object.keys(byCol);
957 for (var c = 0; c < cols.length; c++)
958 {
959 var list = byCol[cols[c]];
960 list.sort(function (a, b)
961 {
962 if (a.y !== b.y)
963 { return a.y - b.y; }
964 return a.id - b.id;
965 });
966 var prevOff = -Infinity;
967 for (var i = 0; i < list.length; i++)
968 {
969 var off = Math.round(list[i].y - cluster.anchorY);
970 if (off <= prevOff)
971 { off = prevOff + 1; }
972 list[i].clusterOffset = off;
973 prevOff = off;
974 }
975 }
976 // Re-base so the lowest offset is 0, keeping relative integers.
977 var minOff = Infinity;
978 for (var i = 0; i < members.length; i++)
979 {
980 if (members[i].clusterOffset < minOff)
981 { minOff = members[i].clusterOffset; }
982 }
983 if (minOff !== 0 && isFinite(minOff))
984 {
985 cluster.anchorY += minOff;
986 for (var i = 0; i < members.length; i++)
987 { members[i].clusterOffset -= minOff; }
988 }
989 this.syncClusterMembers(cluster);
990};
991
992GameCore.prototype.syncClusterMembers = function (cluster)
993{
994 if (!cluster)
995 { return; }
996 var members = this.clusterMembers(cluster.id);
997 if (cluster.anchorY == null)
998 {
999 var minY = Infinity;
1000 for (var i = 0; i < members.length; i++)
1001 {
1002 if (members[i].y < minY)
1003 { minY = members[i].y; }
1004 }
1005 cluster.anchorY = isFinite(minY) ? minY : 0;
1006 }
1007 for (var i = 0; i < members.length; i++)
1008 {
1009 var block = members[i];
1010 if (block.clusterOffset == null)
1011 { block.clusterOffset = Math.round(block.y - cluster.anchorY); }
1012 block.y = cluster.anchorY + block.clusterOffset;
1013 if (cluster.phase !== 'landed')
1014 { block.vy = cluster.vy; }
1015 }
1016};
1017
1018GameCore.prototype.snapshotPrevY = function ()
1019{
1020 for (var i = 0; i < this.blocks.length; i++)
1021 { this.blocks[i]._prevY = this.blocks[i].y; }
1022};
1023
1024/**
1025 * Safety net after the specific integrates: any remaining mid-air
1026 * overlap or swept crossing is resolved with the same rules, then
1027 * every flying cluster is rebuilt as a rigid body. Repeats a few
1028 * times because one attach can create a new contact.
1029 */
1030GameCore.prototype.resolveAirborneCollisions = function ()
1031{
1032 var guard = 0;
1033 while (guard++ < this.height + 4)
1034 {
1035 var changed = false;
1036
1037 // Manual vs falling / cluster / manual.
1038 var manuals = [];
1039 for (var i = 0; i < this.blocks.length; i++)
1040 {
1041 if (this.blocks[i].state === STATE_MANUAL)
1042 { manuals.push(this.blocks[i]); }
1043 }
1044 manuals.sort(function (a, b) { return b.y - a.y; });
1045 for (var m = 0; m < manuals.length; m++)
1046 {
1047 var manual = manuals[m];
1048 if (manual.state !== STATE_MANUAL)
1049 { continue; }
1050 var oldY = (manual._prevY != null) ? manual._prevY : manual.y;
1051 if (this.resolveManualCollisions(manual, oldY, manual.y))
1052 { changed = true; }
1053 }
1054
1055 // Falling vs flying (sit on the roof).
1056 for (var i = 0; i < this.blocks.length; i++)
1057 {
1058 var block = this.blocks[i];
1059 if (block.state !== STATE_FALLING)
1060 { continue; }
1061 var flyer = this.highestFlyingBelow(block);
1062 if (!flyer)
1063 {
1064 // Also a flyer whose swept path crosses this falling block.
1065 flyer = this.flyingHitOnManualPath(block, (block._prevY != null) ? block._prevY : block.y, block.y);
1066 }
1067 if (flyer && this.sweptIntervalsOverlap(block, flyer))
1068 {
1069 if (this.attachToFlyingCluster(block, flyer))
1070 { changed = true; }
1071 }
1072 }
1073
1074 // Cluster vs cluster (swept, including after a column push).
1075 var before = Object.keys(this.clusters).length;
1076 this.mergeFlyingClusters();
1077 if (Object.keys(this.clusters).length !== before)
1078 { changed = true; }
1079
1080 // Rebuild every flying body so offsets stay integer and unique.
1081 var ids = Object.keys(this.clusters);
1082 for (var n = 0; n < ids.length; n++)
1083 {
1084 var cluster = this.clusters[ids[n]];
1085 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1086 { continue; }
1087 this.prepareClusterBody(cluster);
1088 }
1089
1090 if (!changed)
1091 { break; }
1092 }
1093};
1094
1095/**
1096 * Invariant checker (t1112). No two blocks occupy the same cell or
1097 * overlap their [y, y+1) intervals. Flying-cluster members are
1098 * grid-aligned to a single anchor. Returns a list of violation
1099 * strings (empty if clean).
1100 */
1101GameCore.prototype.checkInvariants = function ()
1102{
1103 var issues = [];
1104 var byCol = Object.create(null);
1105 var ids = Object.create(null);
1106 for (var i = 0; i < this.blocks.length; i++)
1107 {
1108 var block = this.blocks[i];
1109 if (ids[block.id])
1110 { issues.push('duplicate id ' + block.id); }
1111 ids[block.id] = true;
1112 if (!byCol[block.col])
1113 { byCol[block.col] = []; }
1114 byCol[block.col].push(block);
1115 if (block.color < COLOR_GRAY || (block.color >= 0 && this.planet
1116 && block.color >= this.planet.colors.length))
1117 { issues.push('block ' + block.id + ' has invalid color ' + block.color); }
1118 }
1119 var cols = Object.keys(byCol);
1120 for (var c = 0; c < cols.length; c++)
1121 {
1122 var list = byCol[cols[c]];
1123 list.sort(function (a, b)
1124 {
1125 if (a.y !== b.y)
1126 { return a.y - b.y; }
1127 return a.id - b.id;
1128 });
1129 for (var i = 0; i < list.length; i++)
1130 {
1131 var a = list[i];
1132 if (i > 0)
1133 {
1134 var prev = list[i - 1];
1135 if (a.y < prev.y + 1 - 1e-6)
1136 {
1137 issues.push('overlap col=' + a.col
1138 + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a)
1139 + ' gap=' + (a.y - prev.y));
1140 }
1141 var cellA = Math.floor(a.y + 1e-9);
1142 var cellP = Math.floor(prev.y + 1e-9);
1143 if (cellA === cellP)
1144 {
1145 issues.push('duplicate cell col=' + a.col + ' row=' + cellA
1146 + ' ' + this._invBlock(prev) + ' and ' + this._invBlock(a));
1147 }
1148 }
1149 }
1150 }
1151 var cids = Object.keys(this.clusters);
1152 for (var n = 0; n < cids.length; n++)
1153 {
1154 var cluster = this.clusters[cids[n]];
1155 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
1156 { continue; }
1157 var mem = this.clusterMembers(cluster.id);
1158 var seenOff = Object.create(null);
1159 for (var i = 0; i < mem.length; i++)
1160 {
1161 var b = mem[i];
1162 if (cluster.anchorY != null && b.clusterOffset != null)
1163 {
1164 var expected = cluster.anchorY + b.clusterOffset;
1165 if (Math.abs(b.y - expected) > 1e-6)
1166 {
1167 issues.push('cluster ' + cluster.id + ' member ' + b.id
1168 + ' y=' + b.y + ' != anchor+off ' + expected);
1169 }
1170 if (Math.abs(b.clusterOffset - Math.round(b.clusterOffset)) > 1e-9)
1171 {
1172 issues.push('cluster ' + cluster.id + ' member ' + b.id
1173 + ' non-integer offset ' + b.clusterOffset);
1174 }
1175 var key = b.col + ':' + b.clusterOffset;
1176 if (seenOff[key])
1177 {
1178 issues.push('cluster ' + cluster.id + ' duplicate offset ' + key);
1179 }
1180 seenOff[key] = true;
1181 }
1182 }
1183 }
1184 return issues;
1185};
1186
1187GameCore.prototype._invBlock = function (b)
1188{
1189 return '#' + b.id + '(' + b.state + ',c=' + b.color + ',y='
1190 + (Math.round(b.y * 1000) / 1000) + ',cid=' + b.clusterId + ')';
1191};
1192
1193GameCore.prototype.assertInvariants = function (label)
1194{
1195 if (!this.strictInvariants && !this.debugInvariants)
1196 { return; }
1197 var issues = this.checkInvariants();
1198 this.lastInvariantViolations = issues;
1199 if (!issues.length)
1200 { return; }
1201 var msg = 'physics invariants failed'
1202 + (label ? ' at ' + label : '')
1203 + ':\n ' + issues.join('\n ')
1204 + '\n' + (this.snapshot ? this.snapshot().picture : '');
1205 if (this.strictInvariants)
1206 { throw new Error(msg); }
1207};
1208
877GameCore.prototype.tryLandCluster = function (cluster, members)
1209GameCore.prototype.tryLandCluster = function (cluster, members)
878{
1210{
879 if (!members.length)
1211 if (!members.length)
@@ -886,7 +1218,7 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
886 var top = this.stackTop(block.col, function (other)
1218 var top = this.stackTop(block.col, function (other)
887 {
1219 {
888 return other.clusterId === cluster.id;
1220 return other.clusterId === cluster.id;
889 });
1221 }, block.y);
890 if (block.y <= top + 1e-9)
1222 if (block.y <= top + 1e-9)
891 {
1223 {
892 hit = true;
1224 hit = true;
@@ -898,9 +1230,10 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
898 if (!hit)
1230 if (!hit)
899 { return; }
1231 { return; }
900 // Lift the whole rigid body so the deepest contact sits flush.
1232 // Lift the whole rigid body so the deepest contact sits flush.
901 for (var i = 0; i < members.length; i++)
1233 this.prepareClusterBody(cluster);
902 { members[i].y += shift; }
1234 cluster.anchorY += shift;
903 this.landCluster(cluster, members);
1235 this.syncClusterMembers(cluster);
1236 this.landCluster(cluster, this.clusterMembers(cluster.id));
904};
1237};
905
1238
906/**
1239/**
@@ -1001,12 +1334,11 @@ GameCore.prototype.integrateManual = function (dt)
1001 var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
1334 var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
1002 var dy = this.advanceJump(proxy, dt);
1335 var dy = this.advanceJump(proxy, dt);
1003 var nextY = oldY + dy;
1336 var nextY = oldY + dy;
1004 this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));
1005 block.vy = proxy.vy;
1337 block.vy = proxy.vy;
1006 block.manualPhase = proxy.phase;
1338 block.manualPhase = proxy.phase;
1007 block.y = Math.min(nextY, block.manualTarget);
1339 if (this.resolveManualCollisions(block, oldY, Math.min(nextY, block.manualTarget)))
1008 if (this.tryMergeManualWithFalling(block, oldY, block.y))
1009 { continue; }
1340 { continue; }
1341 block.y = Math.min(nextY, block.manualTarget);
1010 if (block.y >= block.manualTarget - 1e-12)
1342 if (block.y >= block.manualTarget - 1e-12)
1011 {
1343 {
1012 block.y = block.manualTarget;
1344 block.y = block.manualTarget;
@@ -1027,7 +1359,7 @@ GameCore.prototype.integrateManual = function (dt)
1027 var dy = this.advanceJump(proxy, dt);
1359 var dy = this.advanceJump(proxy, dt);
1028 block.vy = proxy.vy;
1360 block.vy = proxy.vy;
1029 var nextY = block.y + dy;
1361 var nextY = block.y + dy;
1030 var top = this.stackTop(block.col);
1362 var top = this.stackTop(block.col, null, block.y);
1031 if (nextY <= top)
1363 if (nextY <= top)
1032 {
1364 {
1033 block.y = top;
1365 block.y = top;
@@ -1039,9 +1371,10 @@ GameCore.prototype.integrateManual = function (dt)
1039 }
1371 }
1040 else
1372 else
1041 {
1373 {
1042 block.y = Math.min(nextY, this.height - 1);
1374 var downNext = Math.min(nextY, this.height - 1);
1043 if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1375 if (this.resolveManualCollisions(block, block.y, downNext))
1044 { continue; }
1376 { continue; }
1377 block.y = downNext;
1045 }
1378 }
1046 }
1379 }
1047 }
1380 }
@@ -1216,7 +1549,7 @@ GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)
1216 }
1549 }
1217 if (this.drag && this.drag.blockId === manual.id)
1550 if (this.drag && this.drag.blockId === manual.id)
1218 { this.drag = null; }
1551 { this.drag = null; }
1219 this.packClusterOverlaps(cluster);
1552 this.prepareClusterBody(cluster);
1220 this.checkChainRelaunch(cluster);
1553 this.checkChainRelaunch(cluster);
1221 return cluster;
1554 return cluster;
1222};
1555};
@@ -1241,40 +1574,217 @@ GameCore.prototype.mergeAnyManualFallingOverlaps = function ()
1241 }
1574 }
1242};
1575};
1243
1576
1244GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1577/**
1578 * Resolve collisions of a thrown (manual) block along [oldY, nextY].
1579 * Falling blocks merge (t1108). A flying cluster is pushed one cell up
1580 * in the hit column and the shot attaches at the bottom (t1111).
1581 * Another thrown block is promoted to a 1-block cluster and then the
1582 * same attach-at-bottom rule applies. Returns true if `manual` is no
1583 * longer in the MANUAL state.
1584 */
1585GameCore.prototype.resolveManualCollisions = function (manual, oldY, nextY)
1245{
1586{
1246 if (!manual.pushedClusters)
1587 if (!manual || manual.state !== STATE_MANUAL)
1247 { manual.pushedClusters = Object.create(null); }
1588 { return true; }
1248
1589
1249 var manualTopOld = oldY + 1;
1590 var fallingHits = this.fallingHitsOnManualPath(manual, oldY, nextY);
1250 var manualTopNew = nextY + 1;
1591 if (fallingHits.length)
1251 var low = Math.min(oldY, nextY);
1592 {
1252 var high = Math.max(oldY, nextY) + 1;
1593 this.mergeManualWithFalling(manual, fallingHits);
1594 return manual.state !== STATE_MANUAL;
1595 }
1253
1596
1597 var flyer = this.flyingHitOnManualPath(manual, oldY, nextY);
1598 if (flyer)
1599 {
1600 var cluster = this.clusters[flyer.clusterId];
1601 if (cluster && cluster.phase !== 'landed')
1602 {
1603 if (nextY + 1e-12 < oldY)
1604 { this.attachToFlyingCluster(manual, flyer); }
1605 else
1606 { this.attachShotUnderCluster(manual, cluster, manual.col); }
1607 return manual.state !== STATE_MANUAL;
1608 }
1609 }
1610
1611 var other = this.manualHitOnManualPath(manual, oldY, nextY);
1612 if (other)
1613 {
1614 var host = this.promoteManualToCluster(other);
1615 if (host)
1616 {
1617 if (nextY + 1e-12 < oldY)
1618 { this.attachToFlyingCluster(manual, other); }
1619 else
1620 { this.attachShotUnderCluster(manual, host, manual.col); }
1621 return manual.state !== STATE_MANUAL;
1622 }
1623 }
1624 return false;
1625};
1626
1627/** Closest flying cluster member in `manual`'s column on [oldY, nextY]. */
1628GameCore.prototype.flyingHitOnManualPath = function (manual, oldY, nextY)
1629{
1630 var lo = Math.min(oldY, nextY);
1631 var hi = Math.max(oldY, nextY) + 1;
1632 var best = null;
1633 var bestY = Infinity;
1254 for (var i = 0; i < this.blocks.length; i++)
1634 for (var i = 0; i < this.blocks.length; i++)
1255 {
1635 {
1256 var other = this.blocks[i];
1636 var other = this.blocks[i];
1257 if (other === manual)
1637 if (other === manual || other.col !== manual.col)
1258 { continue; }
1259 if (other.col !== manual.col)
1260 { continue; }
1638 { continue; }
1261 if (other.state !== STATE_FLYING || other.clusterId == null)
1639 if (other.state !== STATE_FLYING || other.clusterId == null)
1262 { continue; }
1640 { continue; }
1263 if (manual.pushedClusters[other.clusterId])
1641 var cluster = this.clusters[other.clusterId];
1642 if (!cluster || cluster.phase === 'landed')
1264 { continue; }
1643 { continue; }
1265 var otherBot = other.y;
1644 var o0 = (other._prevY != null) ? other._prevY : other.y;
1266 var otherTop = other.y + 1;
1645 var o1 = other.y;
1267 var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;
1646 var oLo = Math.min(o0, o1);
1268 var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;
1647 var oHi = Math.max(o0, o1) + 1;
1269 if (overlapping || crossing)
1648 if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
1270 {
1649 {
1271 if (this.pushClusterColumn(other.clusterId, manual.col))
1650 if (other.y < bestY)
1272 {
1651 {
1273 manual.pushedClusters[other.clusterId] = true;
1652 bestY = other.y;
1274 return;
1653 best = other;
1275 }
1654 }
1276 }
1655 }
1277 }
1656 }
1657 return best;
1658};
1659
1660/** Another thrown block in the same column on the path (prefer the lower). */
1661GameCore.prototype.manualHitOnManualPath = function (manual, oldY, nextY)
1662{
1663 var lo = Math.min(oldY, nextY);
1664 var hi = Math.max(oldY, nextY) + 1;
1665 var best = null;
1666 for (var i = 0; i < this.blocks.length; i++)
1667 {
1668 var other = this.blocks[i];
1669 if (other === manual || other.col !== manual.col)
1670 { continue; }
1671 if (other.state !== STATE_MANUAL)
1672 { continue; }
1673 var o0 = (other._prevY != null) ? other._prevY : other.y;
1674 var o1 = other.y;
1675 var oLo = Math.min(o0, o1);
1676 var oHi = Math.max(o0, o1) + 1;
1677 if (lo < oHi - 1e-9 && oLo < hi - 1e-9)
1678 {
1679 if (!best || other.y < best.y)
1680 { best = other; }
1681 }
1682 }
1683 return best;
1684};
1685
1686/**
1687 * t1111: push every member of `cluster` in `col` up by one cell, then
1688 * attach `manual` at the bottom of that column, grid-aligned, as part
1689 * of the cluster. The cluster keeps its velocity; the shot takes it.
1690 * Other columns are untouched. Same rule for every successive shot.
1691 */
1692GameCore.prototype.attachShotUnderCluster = function (manual, cluster, col)
1693{
1694 if (!manual || !cluster || !this.clusters[cluster.id])
1695 { return false; }
1696 if (manual.state !== STATE_MANUAL && manual.clusterId !== cluster.id)
1697 {
1698 // Already belonging elsewhere: leave first.
1699 if (manual.clusterId != null)
1700 { this.leaveCluster(manual); }
1701 }
1702 this.prepareClusterBody(cluster);
1703 var members = this.clusterMembers(cluster.id);
1704 var minOff = null;
1705 for (var i = 0; i < members.length; i++)
1706 {
1707 if (members[i].col !== col)
1708 { continue; }
1709 if (minOff == null || members[i].clusterOffset < minOff)
1710 { minOff = members[i].clusterOffset; }
1711 }
1712 if (minOff == null)
1713 { minOff = 0; }
1714 else
1715 {
1716 for (var i = 0; i < members.length; i++)
1717 {
1718 if (members[i].col === col)
1719 { members[i].clusterOffset += 1; }
1720 }
1721 }
1722 if (this.drag && this.drag.blockId === manual.id)
1723 { this.drag = null; }
1724 manual.clusterId = cluster.id;
1725 manual.clusterOffset = minOff;
1726 manual.state = STATE_FLYING;
1727 manual.flame = manual.color === COLOR_GRAY;
1728 manual.settleTime = 0;
1729 manual.manualPhase = null;
1730 manual.manualTarget = null;
1731 manual.manualDecel = 0;
1732 manual.manualJumpHeight = 0;
1733 manual.pushedClusters = null;
1734 this.syncClusterMembers(cluster);
1735 this.cullOffTop();
1736 if (this.clusters[cluster.id])
1737 { this.checkChainRelaunch(cluster); }
1738 return true;
1739};
1740
1741/**
1742 * Turn a lone thrown block into a 1-block flying cluster so a later
1743 * shot can use the t1111 attach-at-bottom rule. A single throw stays
1744 * MANUAL (and is capped at the top of the field) until it collides.
1745 */
1746GameCore.prototype.promoteManualToCluster = function (manual)
1747{
1748 if (!manual)
1749 { return null; }
1750 if (manual.state === STATE_FLYING && manual.clusterId != null)
1751 {
1752 var existing = this.clusters[manual.clusterId];
1753 if (existing && existing.phase !== 'landed')
1754 { return existing; }
1755 }
1756 if (manual.state !== STATE_MANUAL)
1757 { return null; }
1758 var height = manual.manualJumpHeight != null ? manual.manualJumpHeight
1759 : Math.max(0.05, (manual.manualTarget || this.height - 1) - manual.y);
1760 var cluster = {
1761 id: this.nextClusterId++,
1762 jumpHeight: height,
1763 vy: manual.vy,
1764 decel: manual.manualDecel || 0,
1765 phase: (manual.manualPhase === 'down' || manual.vy <= 1e-10) ? 'down' : 'up',
1766 fromManual: true,
1767 anchorY: manual.y
1768 };
1769 this.clusters[cluster.id] = cluster;
1770 manual.state = STATE_FLYING;
1771 manual.clusterId = cluster.id;
1772 manual.clusterOffset = 0;
1773 manual.flame = manual.color === COLOR_GRAY;
1774 manual.settleTime = 0;
1775 manual.manualPhase = null;
1776 manual.manualTarget = null;
1777 manual.manualDecel = 0;
1778 manual.manualJumpHeight = 0;
1779 manual.pushedClusters = null;
1780 this.syncClusterMembers(cluster);
1781 return cluster;
1782};
1783
1784/** Kept for older tests / call sites: push a cluster column by one cell. */
1785GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1786{
1787 return this.resolveManualCollisions(manual, oldY, nextY);
1278};
1788};
1279
1789
1280
1790
@@ -1787,6 +2297,7 @@ GameCore.prototype.launchCluster = function (matchedBlocks, height)
1787 block.vy = cluster.vy;
2297 block.vy = cluster.vy;
1788 block.settleTime = 0;
2298 block.settleTime = 0;
1789 }
2299 }
2300 this.prepareClusterBody(cluster);
1790 return cluster;
2301 return cluster;
1791};
2302};
1792
2303
src/main.js +58 −0
@@ -61,6 +61,7 @@ var app = {
61 61 pointerType: null,
62 62 activeTouchId: null,
63 63 debugDrag: false,
64 debugPhysics: false,
64 65 gameOverShown: false,
65 66 selectTween: null,
66 67 online: false,
@@ -74,6 +75,7 @@ var app = {
74 75 window.onload = function ()
75 76 {
76 77 app.debugDrag = isDragDebugEnabled();
78 app.debugPhysics = isPhysicsDebugEnabled();
77 79 var play = document.getElementById('btn-play');
78 80 play.addEventListener('click', onPlay);
79 81 play.addEventListener('pointerup', function (e)
@@ -122,6 +124,8 @@ function startPlay (opts)
122 124 if (typeof miss !== 'number' || !isFinite(miss))
123 125 { miss = planetMod.missChance(app.planet); }
124 126 app.core = new GameCore(app.planet, Math.random, miss);
127 if (app.debugPhysics)
128 { app.core.debugInvariants = true; }
125 129 app.mode = 'playing';
126 130 app.online = !!opts.online;
127 131 app.gameOverShown = false;
@@ -494,6 +498,8 @@ function tickView (dt)
494 498 syncSprites();
495 499 refreshHud();
496 500 tickOnline(dt);
501 if (app.debugPhysics)
502 { logPhysicsDebug(); }
497 503
498 504 if (app.core.state === 'gameover' && !app.gameOverShown)
499 505 {
@@ -550,6 +556,22 @@ function isDragDebugEnabled ()
550 556 return false;
551 557 }
552 558
559 /** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */
560 function isPhysicsDebugEnabled ()
561 {
562 try
563 {
564 var q = window.location && window.location.search;
565 if (q && /(?:\?|&)debug(?:=|&|$)/.test(q))
566 { return true; }
567 if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1'
568 || window.localStorage.getItem('meteors-debug-drag') === '1'))
569 { return true; }
570 }
571 catch (err) { /* ignore */ }
572 return false;
573 }
574
553 575 function eventClient (e)
554 576 {
555 577 var t = null;
@@ -859,6 +881,42 @@ function onTouchCancel (e)
859 881 finishDrag({ noLaunch: true });
860 882 }
861 883
884 function logPhysicsDebug ()
885 {
886 if (!app.debugPhysics || !app.core)
887 { return; }
888 ensureDragDebugOverlay();
889 var el = document.getElementById('drag-debug');
890 if (!el)
891 { return; }
892 var issues = app.core.lastInvariantViolations || [];
893 var flying = 0;
894 var manuals = 0;
895 var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0;
896 for (var i = 0; i < app.core.blocks.length; i++)
897 {
898 if (app.core.blocks[i].state === 'flying')
899 { flying++; }
900 if (app.core.blocks[i].state === 'manual')
901 { manuals++; }
902 }
903 var status = issues.length
904 ? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n'))
905 : ('physics ok blocks=' + app.core.blocks.length
906 + ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters);
907 // Keep any live drag line above the physics status.
908 var dragLine = '';
909 if (app.debugDrag && lastDragDebug)
910 {
911 dragLine = el.textContent && el.textContent.indexOf('drag ') === 0
912 ? el.textContent.split('\n')[0] + '\n'
913 : '';
914 }
915 el.textContent = dragLine + status;
916 el.hidden = false;
917 el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8';
918 }
919
862 920 function ensureDragDebugOverlay ()
863 921 {
864 922 if (document.getElementById('drag-debug'))
@@ -61,6 +61,7 @@ var app = {
61 pointerType: null,
61 pointerType: null,
62 activeTouchId: null,
62 activeTouchId: null,
63 debugDrag: false,
63 debugDrag: false,
64 debugPhysics: false,
64 gameOverShown: false,
65 gameOverShown: false,
65 selectTween: null,
66 selectTween: null,
66 online: false,
67 online: false,
@@ -74,6 +75,7 @@ var app = {
74window.onload = function ()
75window.onload = function ()
75{
76{
76 app.debugDrag = isDragDebugEnabled();
77 app.debugDrag = isDragDebugEnabled();
78 app.debugPhysics = isPhysicsDebugEnabled();
77 var play = document.getElementById('btn-play');
79 var play = document.getElementById('btn-play');
78 play.addEventListener('click', onPlay);
80 play.addEventListener('click', onPlay);
79 play.addEventListener('pointerup', function (e)
81 play.addEventListener('pointerup', function (e)
@@ -122,6 +124,8 @@ function startPlay (opts)
122 if (typeof miss !== 'number' || !isFinite(miss))
124 if (typeof miss !== 'number' || !isFinite(miss))
123 { miss = planetMod.missChance(app.planet); }
125 { miss = planetMod.missChance(app.planet); }
124 app.core = new GameCore(app.planet, Math.random, miss);
126 app.core = new GameCore(app.planet, Math.random, miss);
127 if (app.debugPhysics)
128 { app.core.debugInvariants = true; }
125 app.mode = 'playing';
129 app.mode = 'playing';
126 app.online = !!opts.online;
130 app.online = !!opts.online;
127 app.gameOverShown = false;
131 app.gameOverShown = false;
@@ -494,6 +498,8 @@ function tickView (dt)
494 syncSprites();
498 syncSprites();
495 refreshHud();
499 refreshHud();
496 tickOnline(dt);
500 tickOnline(dt);
501 if (app.debugPhysics)
502 { logPhysicsDebug(); }
497
503
498 if (app.core.state === 'gameover' && !app.gameOverShown)
504 if (app.core.state === 'gameover' && !app.gameOverShown)
499 {
505 {
@@ -550,6 +556,22 @@ function isDragDebugEnabled ()
550 return false;
556 return false;
551}
557}
552
558
559/** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */
560function isPhysicsDebugEnabled ()
561{
562 try
563 {
564 var q = window.location && window.location.search;
565 if (q && /(?:\?|&)debug(?:=|&|$)/.test(q))
566 { return true; }
567 if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1'
568 || window.localStorage.getItem('meteors-debug-drag') === '1'))
569 { return true; }
570 }
571 catch (err) { /* ignore */ }
572 return false;
573}
574
553function eventClient (e)
575function eventClient (e)
554{
576{
555 var t = null;
577 var t = null;
@@ -859,6 +881,42 @@ function onTouchCancel (e)
859 finishDrag({ noLaunch: true });
881 finishDrag({ noLaunch: true });
860}
882}
861
883
884function logPhysicsDebug ()
885{
886 if (!app.debugPhysics || !app.core)
887 { return; }
888 ensureDragDebugOverlay();
889 var el = document.getElementById('drag-debug');
890 if (!el)
891 { return; }
892 var issues = app.core.lastInvariantViolations || [];
893 var flying = 0;
894 var manuals = 0;
895 var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0;
896 for (var i = 0; i < app.core.blocks.length; i++)
897 {
898 if (app.core.blocks[i].state === 'flying')
899 { flying++; }
900 if (app.core.blocks[i].state === 'manual')
901 { manuals++; }
902 }
903 var status = issues.length
904 ? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n'))
905 : ('physics ok blocks=' + app.core.blocks.length
906 + ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters);
907 // Keep any live drag line above the physics status.
908 var dragLine = '';
909 if (app.debugDrag && lastDragDebug)
910 {
911 dragLine = el.textContent && el.textContent.indexOf('drag ') === 0
912 ? el.textContent.split('\n')[0] + '\n'
913 : '';
914 }
915 el.textContent = dragLine + status;
916 el.hidden = false;
917 el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8';
918}
919
862function ensureDragDebugOverlay ()
920function ensureDragDebugOverlay ()
863{
921{
864 if (document.getElementById('drag-debug'))
922 if (document.getElementById('drag-debug'))