Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
992 lines (902 loc) · 31.4 KB
/
Copy pathgame.js
File metadata and controls
992 lines (902 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
"use strict";
(function () {
const {
CONFIG,
cloneBoard,
getBoardMode,
setBoardMode,
getBoardSide,
getBoardDimensions,
boardCellAt,
placementDistance,
rotationCount,
resetAllScores,
resetSavedGames,
randomShape,
getConfiguredShapes,
shapeById,
shapeDimensions,
rotateShape,
canPlace,
canPlaceAnywhere,
clearCells,
createEmptyBoard,
createMatch,
cloneMatch,
matchPlayerColor,
scorePlacement,
recordMatchPlacement,
endMatchTurn,
chooseAiMove,
fillShape,
findCompletedCells,
loadBestScore,
loadModifiedBestScore,
loadTimedBestScore,
loadGame,
saveBestScore,
saveModifiedBestScore,
saveTimedBestScore,
saveGame: writeGame,
animateClear,
animateAiMove,
buildBoard,
clearPreview,
elements,
hideModal: hideModalView,
makePiece,
renderGame,
shiftTray,
revealTrayItem,
renderPreview,
showModal,
initSettings,
openSettings,
loadPreferences,
isDefaultPreferences,
isDefaultTimedPreferences,
} = window.GenericBlockGame;
let activeTimed = loadPreferences().timeTrialEnabled;
let state = createInitialState(activeTimed);
let bestScore = loadBestScore();
let modifiedBestScore = loadModifiedBestScore();
let timedBestScore = loadTimedBestScore(false);
let timedModifiedBestScore = loadTimedBestScore(true);
let drag = null;
let timerInterval = null;
let lastTimerTick = 0;
let placementView = null;
let placementPending = false;
let boardChangeId = 0;
let aiTimeout = null;
let stopAiFlight = () => {};
let modalAction = startNewGame;
function hideModal() {
hideModalView();
modalAction = startNewGame;
scheduleAiMove();
}
function isAiTurn() {
return Boolean(state.aiOpponent && state.match?.player === 1 && !state.gameOver);
}
function aiCanMove() {
return isAiTurn() && !isTrayEmpty() && !placementPending && !document.hidden
&& !elements.settingsPanel.classList.contains("is-open")
&& !elements.modal.classList.contains("is-open")
&& !elements.changelogModal.classList.contains("is-open");
}
function scheduleAiMove() {
window.clearTimeout(aiTimeout);
aiTimeout = null;
if (!aiCanMove()) return;
const changeId = boardChangeId;
aiTimeout = window.setTimeout(() => {
aiTimeout = null;
if (changeId !== boardChangeId || !aiCanMove()) return;
const move = chooseAiMove(state, loadPreferences().aiDifficulty);
if (!move) { passTurn(true); return; }
const item = state.tray.find(piece => piece.uid === move.uid && !piece.used);
if (!item) return;
const shape = rotateShape(shapeById(item.shapeId), item.rotation || 0);
if (!canPlace(state.board, shape, move.q, move.r)) return;
// Pending blocks other input and AI scheduling while the piece is moving.
placementPending = true;
revealTrayItem(item.uid, false);
applyPreferences();
stopAiFlight = animateAiMove(item, shape, move.q, move.r, () => {
stopAiFlight = () => {};
if (changeId !== boardChangeId) return;
placementPending = false;
placePiece(item, shape, move.q, move.r);
});
}, 400);
}
function cancelAiFlight() {
stopAiFlight();
stopAiFlight = () => {};
}
// Creates all mutable values for a completely new game.
function createInitialState(timed = activeTimed) {
const preferences = loadPreferences();
const size = preferences.boardSide;
return {
board: createEmptyBoard(size),
score: 0,
streak: 0,
scoringVersion: 2,
scoreRun: window.GenericBlockGame.createScoreRun?.(preferences, getBoardMode(), timed),
tray: [],
history: [],
gameOver: false,
match: preferences.twoPlayerEnabled || preferences.aiEnabled ? createMatch() : null,
aiOpponent: preferences.aiEnabled,
timedMode: timed,
timerStarted: false,
timeRemainingMs: timed ? preferences.timeTrialSeconds * 1000 : null,
};
}
// Supplies renderGame with the current board-aware placement check.
function isShapePlaceable(shape) {
return canPlaceAnywhere(state.board, shape);
}
function getPreviewClearCells(shape, originQ, originR) {
const previewBoard = cloneBoard(state.board);
fillShape(previewBoard, shape, originQ, originR);
const completed = findCompletedCells(previewBoard);
return completed.zoneCount ? completed.cells : null;
}
function render() {
renderGame(placementView || state, activeBestScore(), activeBestScoreLabel(), isShapePlaceable, beginDrag);
applyPreferences();
renderTimer();
scheduleAiMove();
}
// A failed write must not stop play or hide the fact that progress is unsaved.
function saveGame(state) {
let bestSaved = true;
try { updateBestScore(); } catch { bestSaved = false; }
const gameSaved = writeGame(state);
elements.saveStatus.hidden = bestSaved && gameSaved;
return bestSaved && gameSaved;
}
function settingsAreModified() {
const preferences = loadPreferences();
return activeTimed
? !isDefaultTimedPreferences(preferences)
: !isDefaultPreferences(preferences);
}
function activeBestScore() {
if (activeTimed) {
return settingsAreModified() ? timedModifiedBestScore : timedBestScore;
}
return settingsAreModified() ? modifiedBestScore : bestScore;
}
function activeBestScoreLabel() {
if (activeTimed) {
return settingsAreModified() ? "Best timed score (modified)" : "Best timed score";
}
return settingsAreModified() ? "Best score (modified)" : "Best score";
}
// Settings affect controls immediately without resetting the current board.
function applyPreferences() {
const preferences = loadPreferences();
elements.undoButton.classList.toggle("setting-hidden", !preferences.undoEnabled);
CONFIG.drag.baseLiftPx = preferences.dragInitialOffset;
CONFIG.drag.maxExtraLiftPx = preferences.dragMovingOffset;
CONFIG.drag.maxHorizontalOffsetPx = preferences.dragHorizontalOffset;
CONFIG.drag.invalidMoveResistance = Math.max(0, preferences.snapOverlapMultiplier - 1);
const view = placementView || state;
document.documentElement.style.setProperty("--piece-hue",
view.match ? (matchPlayerColor(view, view.match.player) === "red" ? 0 : 212) : preferences.pieceHue);
const trayEmpty = isTrayEmpty();
elements.continueButton.hidden = !trayEmpty;
elements.continueButton.disabled = placementPending || Boolean(drag) || state.gameOver;
elements.passButton.hidden = !state.match || trayEmpty;
elements.passButton.disabled = placementPending || Boolean(drag) || state.gameOver || isAiTurn();
elements.undoButton.disabled = !canUndo();
const trayLocked = placementPending || Boolean(drag);
elements.trayPreviousButton.disabled = trayLocked || !(Number(elements.tray.dataset.offset) > 0);
elements.trayNextButton.disabled = trayLocked
|| !(Number(elements.tray.dataset.offset) < Number(elements.tray.dataset.limit));
}
function isTrayEmpty() {
return state.tray.length > 0 && state.tray.every(item => item.used);
}
function shouldWaitForContinue() {
return loadPreferences().slowModeEnabled && !isAiTurn();
}
function continueTray() {
if (!isTrayEmpty() || placementPending || drag || state.gameOver || expireTimeTrialIfNeeded()) return;
if (state.match) endMatchTurn(state.match);
deliverTray();
}
function applyAccessibilityPreferences() {
applyPreferences();
if (!shouldWaitForContinue()) continueTray();
}
function canUndo() {
return loadPreferences().undoEnabled && state.history.length > 0
&& !placementPending && !drag && !isAiTurn()
&& !(activeTimed && state.timerStarted && state.timeRemainingMs <= 0);
}
// Any setting change begins a clean run under the newly selected rules.
function restartForSettingsChange() {
const preferences = loadPreferences();
cleanupDrag();
cancelAiFlight();
boardChangeId += 1;
placementPending = false;
placementView = null;
stopTimer();
activeTimed = preferences.timeTrialEnabled;
state = createInitialState(activeTimed);
hideModal();
buildBoard(preferences.boardSide);
deliverTray();
}
function refreshBestScores() {
bestScore = loadBestScore();
modifiedBestScore = loadModifiedBestScore();
timedBestScore = loadTimedBestScore(false);
timedModifiedBestScore = loadTimedBestScore(true);
}
function switchBoardMode(mode) {
if (mode === getBoardMode()) return;
cleanupDrag();
cancelAiFlight();
stopTimer();
boardChangeId += 1;
placementView = null;
placementPending = false;
if (state.tray.length) saveGame(state);
setBoardMode(mode);
refreshBestScores();
const preferences = loadPreferences();
activeTimed = preferences.timeTrialEnabled;
state = createInitialState(activeTimed);
restoreCurrentGame(false);
}
// A new tray also becomes the earliest point that Undo may return to.
function prepareTray() {
const traySize = loadPreferences().traySize;
state.tray = Array.from({ length: traySize }, (_, index) => ({
uid: `${Date.now()}-${index}-${Math.random().toString(36).slice(2)}`,
shapeId: randomShape().id,
rotation: Math.floor(Math.random() * rotationCount()),
used: false,
}));
if (traySize > 1) ensureTrayHasPlaceablePiece();
state.history = [];
state.restoredTrayItemUid = null;
}
function showNewTray() {
elements.tray.classList.add("new-tray");
window.setTimeout(() => elements.tray.classList.remove("new-tray"), 360);
}
function deliverTray() {
prepareTray();
render();
showNewTray();
saveGame(state);
checkGameOver();
}
function getPlaceableTrayItem(index) {
const configuredShapes = getConfiguredShapes();
const enabledShapes = configuredShapes.filter(shape => shape.enabled && shape.weight > 0);
const pool = enabledShapes.length ? enabledShapes : configuredShapes;
for (const shape of pool) {
for (let rotation = 0; rotation < rotationCount(); rotation += 1) {
const rotatedShape = rotateShape(shape, rotation);
if (canPlaceAnywhere(state.board, rotatedShape)) {
return {
uid: `${Date.now()}-${index}-${Math.random().toString(36).slice(2)}`,
shapeId: shape.id,
rotation,
used: false,
};
}
}
}
return null;
}
function ensureTrayHasPlaceablePiece() {
const hasPlaceablePiece = state.tray.some(item => {
const shape = rotateShape(shapeById(item.shapeId), item.rotation || 0);
return canPlaceAnywhere(state.board, shape);
});
if (hasPlaceablePiece) return;
const placeableItem = getPlaceableTrayItem(0);
if (placeableItem) state.tray[0] = placeableItem;
}
// Stores the exact state before a move so Undo can restore it.
function snapshot() {
return {
board: cloneBoard(state.board),
score: state.score,
streak: state.streak,
tray: state.tray.map(item => ({ ...item })),
match: cloneMatch(state.match),
};
}
function placePiece(item, shape, originQ, originR) {
if (state.gameOver || placementPending || expireTimeTrialIfNeeded()) return;
if (item.used || !canPlace(state.board, shape, originQ, originR)) return;
placementPending = true;
const changeId = boardChangeId;
if (activeTimed && !state.timerStarted) {
state.timerStarted = true;
startTimer();
}
window.GenericBlockGame.trackScoreRun?.(state);
state.history.push(snapshot());
fillShape(state.board, shape, originQ, originR, state.match ? state.match.player + 1 : 1);
item.used = true;
const completed = findCompletedCells(state.board);
const matchPoints = state.match ? recordMatchPlacement(state.match, completed) : 0;
const scored = scorePlacement(state.streak, completed);
state.streak = scored.streak;
const gained = state.match ? matchPoints : scored.points;
state.score += gained;
const trayFinished = state.tray.every(piece => piece.used);
const trayAdvanced = trayFinished && !shouldWaitForContinue();
// Keep the old board and tray only for display. Saves contain the whole move.
if (completed.zoneCount || trayFinished) {
placementView = { ...state, board: cloneBoard(state.board),
tray: state.tray.map(piece => ({ ...piece })), match: cloneMatch(state.match) };
}
clearCells(state.board, completed.cells);
if (trayAdvanced) {
if (state.match) endMatchTurn(state.match);
prepareTray();
}
checkGameOver(false);
saveGame(state);
render();
if (completed.zoneCount) animateClear(completed.cells, gained);
const delay = (completed.zoneCount ? 280 : 0) + (trayFinished ? 80 : 0);
if (delay) {
window.setTimeout(() => { if (changeId === boardChangeId) finishPlacement(trayAdvanced); }, delay);
} else finishPlacement();
}
// Finish the effect, then apply any change to slow mode.
function finishPlacement(trayAdvanced = false) {
placementView = null;
placementPending = false;
if (isTrayEmpty() && !state.gameOver && !shouldWaitForContinue()) {
continueTray();
return;
}
render();
if (trayAdvanced) showNewTray();
if (state.gameOver) showCurrentResult();
}
// Undo history is discarded whenever a new tray is delivered.
function undo() {
if (expireTimeTrialIfNeeded() || !canUndo()) return;
const previous = state.history.pop();
if (!previous) return;
const remainingHistory = state.history;
const restoredItem = previous.tray.find((item, index) => {
const currentItem = state.tray[index];
return !item.used && currentItem?.used;
});
state.board = previous.board;
state.score = previous.score;
state.streak = previous.streak;
state.tray = previous.tray;
state.match = cloneMatch(previous.match);
state.history = remainingHistory;
state.restoredTrayItemUid = restoredItem?.uid || null;
state.gameOver = false;
hideModal();
render();
elements.undoButton.classList.add("rewinding");
window.setTimeout(() => {
elements.undoButton.classList.remove("rewinding");
}, 240);
saveGame(state);
startTimer();
}
function updateBestScore() {
if (state.match) return;
if (activeTimed && settingsAreModified()) {
if (state.score > timedModifiedBestScore) {
saveTimedBestScore(state.score, true);
timedModifiedBestScore = state.score;
}
} else if (activeTimed) {
if (state.score > timedBestScore) {
saveTimedBestScore(state.score, false);
timedBestScore = state.score;
}
} else if (settingsAreModified()) {
if (state.score > modifiedBestScore) {
saveModifiedBestScore(state.score);
modifiedBestScore = state.score;
}
} else if (state.score > bestScore) {
saveBestScore(state.score);
bestScore = state.score;
}
}
function checkGameOver(showResult = true) {
if (state.match) return;
const remaining = state.tray.filter(item => !item.used);
const noMovesRemain = remaining.length
&& remaining.every(item => {
const shape = rotateShape(shapeById(item.shapeId), item.rotation || 0);
return !isShapePlaceable(shape);
});
if (!noMovesRemain) return;
state.gameOver = true;
window.GenericBlockGame.finishScoreRun?.(state);
stopTimer();
if (showResult) showCurrentResult();
saveGame(state);
}
function showMatchResult() {
const [firstScore, secondScore] = state.match.scores;
const firstName = state.aiOpponent ? "You" : "Red";
const secondName = state.aiOpponent ? "AI" : "Blue";
const result = firstScore === secondScore ? "It's a draw" : firstScore > secondScore
? (state.aiOpponent ? "You win" : "Red wins") : `${secondName} wins`;
modalAction = startNewGame;
showModal(result, `${firstName}: ${firstScore.toLocaleString()} points. ${secondName}: ${secondScore.toLocaleString()} points.`,
"Play again", { gameOver: true, match: true });
}
function passTurn(fromAi = false) {
if (!state.match || isTrayEmpty() || state.gameOver || placementPending || drag) return;
if (isAiTurn() && !fromAi) return;
hideModal();
state.gameOver = endMatchTurn(state.match, true);
if (state.gameOver) window.GenericBlockGame.finishScoreRun?.(state);
state.history = [];
if (state.gameOver) {
render();
showMatchResult();
saveGame(state);
} else {
deliverTray();
}
}
function requestPass() {
if (!state.match || isTrayEmpty() || state.gameOver || placementPending || drag || isAiTurn()) return;
const canStillPlace = state.tray.some(item => !item.used
&& isShapePlaceable(rotateShape(shapeById(item.shapeId), item.rotation || 0)));
if (!canStillPlace) {
passTurn();
return;
}
const player = matchPlayerColor(state, state.match.player) === "red" ? "Red" : "Blue";
modalAction = passTurn;
const endsMatch = state.match.consecutivePasses === 1;
showModal(`${player}, pass?`,
`You can still place a piece. Discard the rest of this tray and end your turn?${endsMatch ? " This pass will end the game." : ""}`,
"Pass", { showCancel: true });
}
function formatTime(milliseconds) {
const totalSeconds = Math.max(0, Math.ceil(milliseconds / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${String(seconds).padStart(2, "0")}`;
}
function renderTimer() {
elements.timerDisplay.hidden = !activeTimed;
if (!activeTimed) return;
elements.timerDisplay.textContent = formatTime(state.timeRemainingMs || 0);
elements.timerDisplay.classList.toggle("ending", state.timeRemainingMs <= 30000);
}
function startTimer() {
stopTimer();
if (expireTimeTrialIfNeeded()) return;
if (!activeTimed || !state.timerStarted || state.gameOver) {
renderTimer();
return;
}
lastTimerTick = Date.now();
timerInterval = window.setInterval(() => {
updateRemainingTime();
renderTimer();
if (state.timeRemainingMs <= 0) {
finishTimeTrial();
} else if (Math.ceil(state.timeRemainingMs / 1000) % 5 === 0) {
saveGame(state);
}
}, 250);
}
function updateRemainingTime() {
if (timerInterval === null) return;
const now = Date.now();
state.timeRemainingMs = Math.max(0, state.timeRemainingMs - (now - lastTimerTick));
lastTimerTick = now;
}
function expireTimeTrialIfNeeded() {
updateRemainingTime();
if (!activeTimed || !state.timerStarted || state.timeRemainingMs > 0) return false;
if (!state.gameOver) finishTimeTrial();
return true;
}
function stopTimer() {
updateRemainingTime();
if (timerInterval !== null) {
window.clearInterval(timerInterval);
}
timerInterval = null;
renderTimer();
}
function finishTimeTrial() {
stopTimer();
boardChangeId += 1;
placementView = null;
placementPending = false;
cleanupDrag();
state.timeRemainingMs = 0;
state.gameOver = true;
window.GenericBlockGame.finishScoreRun?.(state);
saveGame(state);
render();
showModal(
"Time's up",
`${activeBestScoreLabel()}: ${activeBestScore().toLocaleString()}`,
"Play again",
{ gameOver: true, score: state.score, showUndo: false },
);
}
// Starts a drag from the entire large tray slot, not just the colored cells.
function beginDrag(event) {
if (state.gameOver || placementPending || isAiTurn() || expireTimeTrialIfNeeded()) return;
if (drag || event.isPrimary === false) return;
if (event.button !== undefined && event.button !== 0) return;
if (elements.modal.classList.contains("is-open")) return;
const slot = event.currentTarget;
const item = state.tray.find(piece => piece.uid === slot.dataset.uid);
if (!item || item.used || slot.classList.contains("disabled")) return;
event.preventDefault();
try {
slot.setPointerCapture(event.pointerId);
} catch {
// Window-level listeners below still keep the drag working if capture fails.
}
slot.classList.add("dragging");
const shape = rotateShape(shapeById(item.shapeId), item.rotation || 0);
const boardRect = elements.board.getBoundingClientRect();
const boardSize = getBoardDimensions(state.board);
const cellSize = boardRect.width / boardSize.width;
const piece = makePiece(shape, cellSize);
const { width, height, minX, minY } = shapeDimensions(shape);
const trayPiece = slot.querySelector(".piece");
const trayPieceRect = trayPiece.getBoundingClientRect();
const grabX = Math.max(
0,
Math.min(trayPieceRect.width, event.clientX - trayPieceRect.left),
);
const grabY = Math.max(
0,
Math.min(trayPieceRect.height, event.clientY - trayPieceRect.top),
);
elements.dragLayer.replaceChildren(...piece.childNodes);
elements.dragLayer.style.width = `${width * cellSize}px`;
elements.dragLayer.style.height = `${height * cellSize}px`;
elements.dragLayer.classList.add("active");
drag = {
pointerId: event.pointerId,
item,
shape,
slot,
cellSize,
anchorX: -minX * cellSize,
anchorY: -minY * cellSize,
startX: event.clientX,
startY: event.clientY,
grabRatioX: trayPieceRect.width ? grabX / trayPieceRect.width : 0.5,
grabRatioY: trayPieceRect.height ? grabY / trayPieceRect.height : 0.5,
trayPieceRect,
layerWidth: width * cellSize,
layerHeight: height * cellSize,
layerLeft: 0,
layerTop: 0,
originQ: null,
originR: null,
valid: false,
};
applyPreferences();
updateDrag(event);
window.addEventListener("pointermove", updateDrag, { passive: false });
window.addEventListener("pointerup", endDrag);
window.addEventListener("pointercancel", cancelDrag);
}
// Apply the same offsets toward the board and along the tray in either layout.
function dragOffsetsForPointer(pointerX, pointerY) {
const landscape = window.matchMedia("(orientation: landscape) and (max-height: 500px)").matches;
const towardBoard = landscape ? drag.startX - pointerX : drag.startY - pointerY;
const progress = Math.min(1, Math.max(0, towardBoard) / CONFIG.drag.extraLiftDistancePx);
const lift = CONFIG.drag.baseLiftPx + progress * CONFIG.drag.maxExtraLiftPx;
const acrossTray = landscape ? pointerY - drag.startY : pointerX - drag.startX;
const sideProgress = Math.min(1, Math.abs(acrossTray) / CONFIG.drag.extraLiftDistancePx);
const sideOffset = Math.sign(acrossTray) * CONFIG.drag.maxHorizontalOffsetPx * sideProgress;
return landscape ? { x: -lift, y: sideOffset } : { x: sideOffset, y: -lift };
}
function updateDrag(event) {
if (!drag || event.pointerId !== drag.pointerId) return;
event.preventDefault();
const { width, height } = shapeDimensions(drag.shape);
const offset = dragOffsetsForPointer(event.clientX, event.clientY);
const pieceWidth = width * drag.cellSize;
const pieceHeight = height * drag.cellSize;
const left = event.clientX + offset.x - pieceWidth * drag.grabRatioX;
const top = event.clientY + offset.y - pieceHeight * drag.grabRatioY;
drag.layerLeft = left;
drag.layerTop = top;
elements.dragLayer.style.transform = `translate3d(${left}px, ${top}px, 0)`;
// Snap only the board preview. The dragged piece keeps following the pointer.
const boardRect = elements.board.getBoundingClientRect();
const anchorX = left + drag.anchorX - boardRect.left - boardRect.width / 2;
const anchorY = top + drag.anchorY - boardRect.top - boardRect.height / 2;
const candidate = boardCellAt(state.board, anchorX, anchorY, drag.cellSize);
const candidateIsValid = canPlace(
state.board,
drag.shape,
candidate.q,
candidate.r,
);
// Hold the previous valid origin when the nearest snap point is invalid.
// Normal snapping changes at half a cell; resistance extends that boundary.
if (drag.valid && !candidateIsValid) {
const resistance = Math.max(0, CONFIG.drag.invalidMoveResistance);
const distance = placementDistance(state.board, candidate.rawQ - drag.originQ, candidate.rawR - drag.originR);
const releaseDistance = 0.5 + resistance;
if (distance < releaseDistance) {
drag.previewClearCells = getPreviewClearCells(drag.shape, drag.originQ, drag.originR);
elements.dragLayer.classList.remove("invalid");
renderPreview(drag);
return;
}
}
drag.originQ = candidate.q;
drag.originR = candidate.r;
drag.valid = candidateIsValid;
drag.previewClearCells = drag.valid
? getPreviewClearCells(drag.shape, drag.originQ, drag.originR)
: null;
elements.dragLayer.classList.toggle("invalid", !drag.valid);
renderPreview(drag);
}
function endDrag(event) {
if (!drag || event.pointerId !== drag.pointerId) return;
const completedDrag = drag;
if (completedDrag.valid) {
cleanupDrag();
placePiece(
completedDrag.item,
completedDrag.shape,
completedDrag.originQ,
completedDrag.originR,
);
} else {
returnDragToTray();
}
}
function cancelDrag(event) {
if (event?.pointerId !== undefined && drag && event.pointerId !== drag.pointerId) return;
returnDragToTray();
}
function detachDragListeners() {
if (!drag) return;
window.removeEventListener("pointermove", updateDrag);
window.removeEventListener("pointerup", endDrag);
window.removeEventListener("pointercancel", cancelDrag);
}
function cleanupDrag() {
if (!drag) return;
detachDragListeners();
drag.slot.classList.remove("dragging");
elements.dragLayer.classList.remove("active", "invalid");
elements.dragLayer.classList.remove("returning");
elements.dragLayer.style.transformOrigin = "";
elements.dragLayer.replaceChildren();
clearPreview();
drag = null;
applyPreferences();
}
function returnDragToTray() {
if (!drag) return;
const returningDrag = drag;
detachDragListeners();
clearPreview();
elements.dragLayer.classList.remove("invalid");
elements.dragLayer.classList.add("returning");
const targetLeft = returningDrag.trayPieceRect.left;
const targetTop = returningDrag.trayPieceRect.top;
const scaleX = returningDrag.trayPieceRect.width / returningDrag.layerWidth;
const scaleY = returningDrag.trayPieceRect.height / returningDrag.layerHeight;
elements.dragLayer.style.transformOrigin = "top left";
elements.dragLayer.style.transform = `translate3d(${targetLeft}px, ${targetTop}px, 0) scale(${scaleX}, ${scaleY})`;
window.setTimeout(() => {
if (drag !== returningDrag) return;
cleanupDrag();
}, 50);
}
function openResetModal() {
if (placementPending || drag) return;
modalAction = startNewGame;
showModal(
activeTimed ? "Restart time trial?" : "Restart game?",
activeTimed
? "Your timed board, score, and countdown will restart."
: state.match ? `Restart the board and both scores? ${state.aiOpponent ? "You" : "Red"} will start.`
: "Your current board and score will be replaced with a fresh game.",
"Restart",
{ showCancel: true },
);
}
function startNewGame() {
if (placementPending) return;
boardChangeId += 1;
window.clearTimeout(aiTimeout);
cleanupDrag();
stopTimer();
hideModal();
const cells = new Set(Object.keys(state.board.cells).filter(key => state.board.cells[key]));
const hasFilledCells = cells.size > 0;
if (hasFilledCells) {
placementPending = true;
const changeId = boardChangeId;
animateClear(cells, 0);
window.setTimeout(() => { if (changeId === boardChangeId) resetGameState(); }, 280);
} else {
resetGameState();
}
}
function resetGameState() {
cancelAiFlight();
boardChangeId += 1;
placementPending = false;
placementView = null;
state = createInitialState(activeTimed);
deliverTray();
}
// Fullscreen must be started from a direct button interaction in most browsers.
async function toggleFullscreen() {
try {
if (document.fullscreenElement) {
await document.exitFullscreen();
} else {
await document.documentElement.requestFullscreen();
}
} catch {
// Some embedded and mobile browsers expose the API but reject the request.
}
}
function updateFullscreenButton() {
const isFullscreen = Boolean(document.fullscreenElement);
elements.fullscreenButton.classList.toggle("is-fullscreen", isFullscreen);
elements.fullscreenButton.setAttribute(
"aria-label",
isFullscreen ? "Exit fullscreen" : "Enter fullscreen",
);
elements.fullscreenButton.title = isFullscreen ? "Exit fullscreen" : "Enter fullscreen";
}
elements.undoButton.addEventListener("click", undo);
elements.continueButton.addEventListener("click", continueTray);
for (const [button, steps] of [[elements.trayPreviousButton, -1], [elements.trayNextButton, 1]]) {
button.addEventListener("click", () => {
if (placementPending || drag) return;
shiftTray(steps);
applyPreferences();
});
}
elements.passButton.addEventListener("click", requestPass);
elements.resetButton.addEventListener("click", openResetModal);
elements.settingsButton.addEventListener("click", openSettings);
elements.modalButton.addEventListener("click", () => modalAction());
elements.modalCancelButton.addEventListener("click", () => {
hideModal();
elements.passButton.focus();
});
document.addEventListener("keydown", event => {
if (event.key === "Escape" && !elements.modalCancelButton.hidden) hideModal();
});
elements.modalUndoButton.addEventListener("click", undo);
if (document.documentElement.hasAttribute("data-native-fullscreen")) {
elements.fullscreenButton.hidden = true;
elements.fullscreenButton.tabIndex = -1;
} else {
elements.fullscreenButton.addEventListener("click", toggleFullscreen);
document.addEventListener("fullscreenchange", updateFullscreenButton);
if (!document.fullscreenEnabled || !document.documentElement.requestFullscreen) {
elements.fullscreenButton.classList.add("unsupported");
}
}
window.addEventListener("blur", cancelDrag);
window.addEventListener("resize", () => {
cleanupDrag();
if (!placementPending) render();
});
document.addEventListener("visibilitychange", () => {
if (document.hidden) cancelDrag();
scheduleAiMove();
});
// Prevent browser selection, native dragging, long-press menus, and right click.
document.addEventListener("contextmenu", event => event.preventDefault());
document.addEventListener("selectstart", event => event.preventDefault());
document.addEventListener("dragstart", event => event.preventDefault());
initSettings({
onChange: restartForSettingsChange,
onBoardChange: switchBoardMode,
onClose: () => {
if (state.gameOver) showCurrentResult();
else scheduleAiMove();
},
onResetSettings: () => {
resetSavedGames();
restartForSettingsChange();
},
onAccessibilityChange: applyAccessibilityPreferences,
onChangelogClose: scheduleAiMove,
onResetBest: () => {
resetAllScores();
refreshBestScores();
elements.bestScore.textContent = "0";
},
});
function showCurrentResult() {
if (state.match) {
showMatchResult();
return;
}
showModal(
activeTimed && state.timeRemainingMs <= 0 ? "Time's up" : "No more moves",
`${activeBestScoreLabel()}: ${activeBestScore().toLocaleString()}`,
"Play again",
{ gameOver: true, score: state.score, showUndo: canUndo() },
);
}
// Each board restores its own saved game when its tab becomes active.
function restoreCurrentGame(showWelcome) {
const preferences = loadPreferences();
const savedState = loadGame(preferences.aiEnabled ? "ai" : Boolean(preferences.twoPlayerEnabled));
if (savedState?.tray?.length && savedState.timedMode === activeTimed
&& getBoardSide(savedState.board) === preferences.boardSide) {
state = { ...createInitialState(activeTimed), ...savedState };
if (!savedState.scoreRun) state.scoreRun = null;
window.GenericBlockGame.trackScoreRun?.(state);
if (savedState.scoringVersion !== 2) {
state.streak = 0;
state.scoringVersion = 2;
saveGame(state);
}
buildBoard(getBoardSide(state.board));
render();
if (state.gameOver) {
if (showWelcome) showCurrentResult();
else hideModal();
} else {
hideModal();
if (!shouldWaitForContinue()) continueTray();
if (state.timerStarted) startTimer();
}
} else {
buildBoard(getBoardSide(state.board));
if (showWelcome) render();
else {
hideModal();
deliverTray();
}
}
}
window.GenericBlockGame.profile?.subscribe(event => {
if (event.type !== "profile-restored") return;
cleanupDrag();
cancelAiFlight();
stopTimer();
boardChangeId += 1;
placementPending = false;
placementView = null;
refreshBestScores();
activeTimed = loadPreferences().timeTrialEnabled;
state = createInitialState(activeTimed);
restoreCurrentGame(true);
window.GenericBlockGame.openSettings();
});
restoreCurrentGame(true);
}());