feat(minigames): tighten Ordnance Disposal feel, ghost charges and flight-safe correction
Two client-side follow-ups from the first prod playtest of prediction v2: Reconciliation no longer position-corrects while the latest inputs are still in flight. In that window the server legitimately trails the local sim (it is still applying the previous direction), and the previous always-on 20% pull dragged the sprite backwards on every turn and key release; that drag is what made movement feel heavy even after prediction landed. Soft correction now only runs once every sent input is acknowledged and the gap exceeds a 0.4 cell deadzone, pulling the excess; the teleport hard-snap is unchanged. Dropping a charge now renders an optimistic ghost at the technician's cell the same frame, mirroring every server rejection rule against the freshest state. The ghost blocks movement like a real charge (but cannot be kicked), burns its fuse on the local clock, and is retired per snapshot by a bomb at its cell, by a fresh bomb owned by our seat (possibly one cell away when the server trailed the displayed position), or by a 1.2s expiry covering server rejections.
This commit is contained in:
parent
3bf9de4848
commit
99e610e955
1 changed files with 93 additions and 7 deletions
|
|
@ -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<Array<{ seq: number; dir: Dir | null; sentAt: number }>>([]);
|
||||
const pushOverlayRef = useRef<Map<string, { x: number; y: number; at: number }>>(new Map());
|
||||
const ghostBombsRef = useRef<Array<{ x: number; y: number; placedAt: number }>>([]);
|
||||
const knownBombIdsRef = useRef<Set<string>>(new Set());
|
||||
const lastAckRef = useRef(0);
|
||||
const lastAckedDirRef = useRef<Dir | null>(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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue