diff --git a/src/components/frontend/minigames/bomperator/BomperatorGame.tsx b/src/components/frontend/minigames/bomperator/BomperatorGame.tsx
index c99c6ae6..6d9d7f84 100644
--- a/src/components/frontend/minigames/bomperator/BomperatorGame.tsx
+++ b/src/components/frontend/minigames/bomperator/BomperatorGame.tsx
@@ -5,7 +5,7 @@ import { Eye, Flag, Play, Radio, Users } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import type { BomperatorView, Dir, PlayerView } from "@/lib/minigames/bomperator/types";
-import { GRID_H, GRID_W, MAX_SEATS, MIN_TO_START, TICKS_PER_SECOND } from "@/lib/minigames/bomperator/constants";
+import { GRID_H, GRID_W, MAX_SEATS, MIN_TO_START, TICK_MS, TICKS_PER_SECOND } from "@/lib/minigames/bomperator/constants";
import { sendBomperatorInput } from "@/app/(frontend)/minigames/bomperator/actions";
import { BomperatorHud, seatColor } from "./BomperatorHud";
import { useBomperatorSounds } from "./sounds";
@@ -40,6 +40,147 @@ interface RenderPos {
y: number;
}
+/* ----- client-side prediction: replicate the engine's grid-locked movement ----- */
+
+const PRED_DIR_VEC: Record
= {
+ up: [0, -1],
+ down: [0, 1],
+ left: [-1, 0],
+ right: [1, 0],
+};
+const PRED_EPS = 1e-6;
+
+interface PredictBomb {
+ id: string;
+ x: number;
+ y: number;
+}
+
+function predCellCentered(x: number, y: number): boolean {
+ return Math.abs(x - Math.round(x)) < PRED_EPS && Math.abs(y - Math.round(y)) < PRED_EPS;
+}
+
+function predApproach(current: number, target: number, step: number): number {
+ const delta = target - current;
+ if (Math.abs(delta) <= step) return target;
+ return current + Math.sign(delta) * step;
+}
+
+function predBombAt(bombs: readonly PredictBomb[], x: number, y: number): PredictBomb | undefined {
+ return bombs.find((b) => b.x === x && b.y === y);
+}
+
+function predOpen(grid: readonly number[], x: number, y: number): boolean {
+ return x >= 0 && y >= 0 && x < GRID_W && y < GRID_H && grid[y * GRID_W + x] === 0;
+}
+
+function predCanStand(grid: readonly number[], bombs: readonly PredictBomb[], x: number, y: number): boolean {
+ return predOpen(grid, x, y) && !predBombAt(bombs, x, y);
+}
+
+/**
+ * One small advance step mirroring the engine's advancePlayer exactly: finish
+ * aligning the perpendicular axis first, then move toward the adjacent cell
+ * center; a charge blocks unless kick-capable (then it slides one cell forward,
+ * recorded into the mutable bombs list).
+ */
+function predStep(
+ grid: readonly number[],
+ bombs: PredictBomb[],
+ x0: number,
+ y0: number,
+ dir: Dir,
+ kick: boolean,
+ step: number,
+): RenderPos {
+ const [dx, dy] = PRED_DIR_VEC[dir];
+ let x = x0;
+ let y = y0;
+ if (dx !== 0) {
+ if (Math.abs(y - Math.round(y)) > PRED_EPS) {
+ const ty = Math.round(y);
+ y += Math.sign(ty - y) * Math.min(Math.abs(ty - y), step);
+ if (Math.abs(ty - y) < PRED_EPS) y = ty;
+ return { x, y };
+ }
+ const cy = Math.round(y);
+ const entering = dx > 0 ? Math.floor(x) + 1 : Math.ceil(x) - 1;
+ const home = dx > 0 ? Math.floor(x + PRED_EPS) : Math.ceil(x - PRED_EPS);
+ const bomb = predBombAt(bombs, entering, cy);
+ if (bomb) {
+ if (kick) {
+ const bx = entering + dx;
+ if (predOpen(grid, bx, cy) && !predBombAt(bombs, bx, cy)) {
+ bomb.x = bx;
+ bomb.y = cy;
+ return { x: predApproach(x, entering, step), y };
+ }
+ }
+ return { x: home, y };
+ }
+ if (!predCanStand(grid, bombs, entering, cy)) return { x: home, y };
+ return { x: predApproach(x, entering, step), y };
+ }
+ if (Math.abs(x - Math.round(x)) > PRED_EPS) {
+ const tx = Math.round(x);
+ x += Math.sign(tx - x) * Math.min(Math.abs(tx - x), step);
+ if (Math.abs(tx - x) < PRED_EPS) x = tx;
+ return { x, y };
+ }
+ const cx = Math.round(x);
+ const entering = dy > 0 ? Math.floor(y) + 1 : Math.ceil(y) - 1;
+ const home = dy > 0 ? Math.floor(y + PRED_EPS) : Math.ceil(y - PRED_EPS);
+ const bomb = predBombAt(bombs, cx, entering);
+ if (bomb) {
+ if (kick) {
+ const by = entering + dy;
+ if (predOpen(grid, cx, by) && !predBombAt(bombs, cx, by)) {
+ bomb.x = cx;
+ bomb.y = by;
+ return { x, y: predApproach(y, entering, step) };
+ }
+ }
+ return { x, y: home };
+ }
+ if (!predCanStand(grid, bombs, cx, entering)) return { x, y: home };
+ return { x, y: predApproach(y, entering, step) };
+}
+
+/**
+ * Advance over `durationMs` in small slices, honoring the engine's slip-to-rest:
+ * with no active direction the technician keeps traveling in `facing` until it
+ * lands exactly on a cell center; a blocked cell settles it back to the one it
+ * was leaving, where it then stays.
+ */
+function predAdvance(
+ grid: readonly number[],
+ bombs: PredictBomb[],
+ start: RenderPos,
+ dir: Dir | null,
+ facing: Dir,
+ speed: number,
+ kick: boolean,
+ durationMs: number,
+): RenderPos {
+ let { x, y } = start;
+ let remaining = Math.min(durationMs, 1500);
+ let guard = 0;
+ while (remaining > 0.5 && guard < 120) {
+ guard += 1;
+ const slice = Math.min(remaining, 16);
+ const step = (speed * slice) / 1000;
+ const effDir = dir ?? (predCellCentered(x, y) ? null : facing);
+ if (!effDir) break;
+ const res = predStep(grid, bombs, x, y, effDir, kick, step);
+ const moved = Math.abs(res.x - x) > 1e-9 || Math.abs(res.y - y) > 1e-9;
+ x = res.x;
+ y = res.y;
+ if (!moved) break; // settled against a blocked cell
+ remaining -= slice;
+ }
+ return { x, y };
+}
+
/* ------------------------------------------------------------- draw helpers */
function drawFloor(ctx: CanvasRenderingContext2D) {
@@ -218,6 +359,16 @@ export function BomperatorGame({
const renderPosRef = useRef