diff --git a/src/components/frontend/minigames/bomperator/BomperatorGame.tsx b/src/components/frontend/minigames/bomperator/BomperatorGame.tsx index 6d9d7f84..25efa843 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, TICK_MS, TICKS_PER_SECOND } from "@/lib/minigames/bomperator/constants"; +import { BOMB_FUSE_TICKS, 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"; @@ -54,8 +54,18 @@ interface PredictBomb { id: string; x: number; y: number; + /** Optimistic local charge the server has not confirmed yet. Blocks movement + * like a real one but cannot be kick-pushed (the server has not placed it). */ + ghost?: boolean; } +/** + * Correction deadzone in cells. Gaps explainable by input flight time or + * snapshot-projection bias live inside this band; pulling on them is what made + * movement feel heavy, so they are left alone. + */ +const PRED_DEADZONE = 0.4; + function predCellCentered(x: number, y: number): boolean { return Math.abs(x - Math.round(x)) < PRED_EPS && Math.abs(y - Math.round(y)) < PRED_EPS; } @@ -108,7 +118,7 @@ function predStep( const home = dx > 0 ? Math.floor(x + PRED_EPS) : Math.ceil(x - PRED_EPS); const bomb = predBombAt(bombs, entering, cy); if (bomb) { - if (kick) { + if (kick && !bomb.ghost) { const bx = entering + dx; if (predOpen(grid, bx, cy) && !predBombAt(bombs, bx, cy)) { bomb.x = bx; @@ -132,7 +142,7 @@ function predStep( const home = dy > 0 ? Math.floor(y + PRED_EPS) : Math.ceil(y - PRED_EPS); const bomb = predBombAt(bombs, cx, entering); if (bomb) { - if (kick) { + if (kick && !bomb.ghost) { const by = entering + dy; if (predOpen(grid, cx, by) && !predBombAt(bombs, cx, by)) { bomb.x = cx; @@ -365,6 +375,8 @@ export function BomperatorGame({ const inputSeqRef = useRef(0); const pendingRef = useRef>([]); const pushOverlayRef = useRef>(new Map()); + const ghostBombsRef = useRef>([]); + const knownBombIdsRef = useRef>(new Set()); const lastAckRef = useRef(0); const lastAckedDirRef = useRef(null); const lastVersionSeenRef = useRef(-1); @@ -386,6 +398,35 @@ export function BomperatorGame({ const sendDrop = useCallback(async () => { playPlace(); + // Optimistic placement: the ghost charge appears at our cell the same instant + // space goes down. The server places the real one about one round trip later + // at its own (slightly trailing) position; the snapshot that shows our fresh + // bomb retires the ghost. Every server rejection rule is mirrored here + // against our freshest state, so a ghost almost never expires unfulfilled. + const view = matchRef.current; + const seat = view.yourSeat; + const me = seat !== null ? (view.players[seat] ?? null) : null; + if (view.phase === "battle" && me && me.alive) { + const at = predRef.current ?? { x: me.x, y: me.y }; + const cx = Math.round(at.x); + const cy = Math.round(at.y); + const ghosts = ghostBombsRef.current; + const overlay = pushOverlayRef.current; + const cellTaken = + cx < 0 || + cy < 0 || + cx >= GRID_W || + cy >= GRID_H || + view.grid[cy * GRID_W + cx] !== 0 || + view.bombs.some((b) => { + const to = overlay.get(b.id); + return (b.x === cx && b.y === cy) || (to !== undefined && to.x === cx && to.y === cy); + }) || + ghosts.some((g) => g.x === cx && g.y === cy); + if (!cellTaken && me.bombsActive + ghosts.length < me.bombsMax && ghosts.length < 4) { + ghosts.push({ x: cx, y: cy, placedAt: performance.now() }); + } + } try { await sendBomperatorInput(match.matchId, { drop: true }); } catch { @@ -497,6 +538,9 @@ export function BomperatorGame({ const to = overlay.get(b.id); return { id: b.id, x: to?.x ?? b.x, y: to?.y ?? b.y }; }); + for (const g of ghostBombsRef.current) { + bombsWorking.push({ id: `ghost:${g.placedAt}`, x: g.x, y: g.y, ghost: true }); + } /* -- local prediction: our own technician answers the keyboard immediately -- */ if (view.phase === "battle" && me && me.alive) { @@ -524,6 +568,7 @@ export function BomperatorGame({ predRef.current = null; pendingRef.current = []; overlay.clear(); + ghostBombsRef.current = []; } /* -- reconcile against the server once per fresh snapshot -- */ @@ -589,14 +634,49 @@ export function BomperatorGame({ // Teleport/respawn: snap instead of sliding across the map. predRef.current = est; facingRef.current = facing; - } else if (gap > 1e-4) { - // Soft correction: prediction error decays smoothly, no snap-back. - pred.x += (est.x - pred.x) * 0.2; - pred.y += (est.y - pred.y) * 0.2; + } else if (pending.length === 0 && gap > PRED_DEADZONE) { + // Correct only once EVERY sent input is acknowledged. While + // inputs are in flight the server legitimately trails the local + // sim (it has not seen the latest keys yet), so pulling toward + // it then drags the sprite backwards on every turn and key + // release; that drag is what made movement feel heavy. With + // everything acked, a gap beyond the deadzone is a genuine + // disagreement (a charge or wall the local sim missed), so + // close it firmly, leaving the deadzone band itself alone. + const k = Math.min(gap - PRED_DEADZONE, gap * 0.5) / gap; + pred.x += (est.x - pred.x) * k; + pred.y += (est.y - pred.y) * k; } } } lastAckRef.current = ack; + + // Reconcile optimistic ghost charges. The server placed our drop when + // a bomb we have not seen before shows up owned by our seat (it may + // sit a cell away from the ghost: the server trails our displayed + // position), or when anything occupies the ghost's cell. A ghost + // still unmatched past the round-trip budget expired because the + // server rejected the placement; retire it silently. + const ghosts = ghostBombsRef.current; + if (ghosts.length > 0) { + let freshOwn = 0; + for (const b of view.bombs) { + if (b.ownerSeat === me.seat && !knownBombIdsRef.current.has(b.id)) freshOwn += 1; + } + for (let i = ghosts.length - 1; i >= 0; i -= 1) { + const g = ghosts[i]; + const cellTaken = view.bombs.some((b) => b.x === g.x && b.y === g.y); + if (cellTaken) { + ghosts.splice(i, 1); + } else if (freshOwn > 0) { + freshOwn -= 1; + ghosts.splice(i, 1); + } else if (now - g.placedAt > 1200) { + ghosts.splice(i, 1); + } + } + } + knownBombIdsRef.current = new Set(view.bombs.map((b) => b.id)); } } @@ -634,6 +714,12 @@ export function BomperatorGame({ const w = bombsWorking.find((wb) => wb.id === bomb.id); drawBomb(ctx, w?.x ?? bomb.x, w?.y ?? bomb.y, bomb.fuseTicks, now); } + // Ghost charges: optimistic placements awaiting server confirmation. The + // fuse burns on the local clock so the blink cadence matches real ones. + for (const g of ghostBombsRef.current) { + const fuse = Math.max(0, BOMB_FUSE_TICKS - (now - g.placedAt) / TICK_MS); + drawBomb(ctx, g.x, g.y, fuse, now); + } for (const flame of view.flames) drawFlame(ctx, flame.x, flame.y, flame.ticksLeft, now); for (const player of view.players) { const isMe = mySeat !== null && player.seat === mySeat;