diff --git a/client/src/main.ts b/client/src/main.ts index 0f21e03..08e2d3e 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -27,7 +27,7 @@ let lastNotice = ''; let noticeTimer: ReturnType; let inputElapsed = 0; let walkTo: { x: number; z: number } | undefined; -let pendingAction: { op: string; target: string } | undefined; +let pendingAction: { op: string; target: string; stopSequence?: number } | undefined; let actionElapsed = 0; let riskyArmed = "", gatherSent = 0; // A gather target can stay legitimately, persistently unreachable for a long time (a @@ -296,8 +296,20 @@ async function boot() { const target = actionTarget; const range = pendingAction ? pendingAction.op === 'attack' ? (snapshot.self.attack?.range??config.attackRange) + (target?.id === 'boss' ? 3 : target?.kind==='structure'?Math.max(target.width??0,target.depth??0)/2-.5:-0.5) : config.gatherRange - 0.5 : 0.8; const reach = pendingAction?.op === 'attack' && target ? Math.hypot(distance, target.position.y-p.y) : distance; - if (reach <= range) { + if (pendingAction?.stopSequence && snapshot.self.ack < pendingAction.stopSequence) { + // Do not reverse course from a pre-stop snapshot either. Wait for the + // authoritative stopped position, sending zero movement below. + } else if (reach <= range) { if (!pendingAction) { walkTo = undefined; } + // A delayed snapshot can show us in reach while the server is still + // moving. Gather/loot only after a snapshot acknowledges our stop; + // its position then determines whether we need to approach again. + else if (pendingAction.op !== 'attack' && !pendingAction.stopSequence) { + send('move'); + pendingAction.stopSequence = sequence; + actionElapsed = 0; + lastProgress = undefined; + } // A single fire-and-forget attempt can land a hair outside the authoritative // server position (which may lag a stale/throttled client render) and get // rejected with no second chance, so retry at a conservative cadence (well @@ -321,7 +333,11 @@ async function boot() { } else if(pendingAction.op!=='gather'||!gatherSent) { send(pendingAction.op, pendingAction.target);if(pendingAction.op==='gather')gatherSent=sequence; } } } - } else if (distance > .01) { x = dx / distance; z = dz / distance; actionElapsed = 0.3; } + } else if (distance > .01) { + if (pendingAction) delete pendingAction.stopSequence; + lastProgress = undefined; + x = dx / distance; z = dz / distance; actionElapsed = 0.3; + } } view.moving = !!(x || z); if (inputElapsed >= 0.05) { send('move', '', x, z); inputElapsed = 0; } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e965197..d6499e2 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -83,6 +83,13 @@ interpolation step. An open WebSocket shows "Synchronizing world" until a valid world snapshot has been processed; reconnect clears the previous input snapshot. No rigid-body physics is claimed: this is heightfield movement in open terrain. +Gather/loot approach sends a zero-movement intent and waits for a snapshot that +acknowledges it before attempting the action. That stopped server position is +checked again for range; stale pre-stop snapshots cannot trigger the action or +reverse the approach. Out-of-range confirmation resumes movement. This leaves +server range, cooldown, depletion, and loot ownership checks unchanged. The desktop +E2E check covers this with delayed snapshots and sparse input ticks. + Milestone 3 adds authoritative vertical jump velocity and gravity over that heightfield. Generator 2 increases terrain depth and chooses weighted biomes; generator 1 remains supported with unchanged heights/resource identities. diff --git a/tests/game.spec.ts b/tests/game.spec.ts index 6d56a67..6e1a5da 100644 --- a/tests/game.spec.ts +++ b/tests/game.spec.ts @@ -34,6 +34,17 @@ test('desktop renders models, moves authoritatively, gathers, and reconnects', a const errors: string[] = []; page.on('pageerror', error => errors.push(error.message)); await page.setViewportSize({ width: 1440, height: 900 }); + // Regression: sparse input plus stale snapshots used to overshoot the tree, + // send gathering intents from outside server range, then abandon the action. + // Keep the real simulation and input handlers; delay only delivery/cadence. + await page.routeWebSocket('**/ws', socket => { + const server = socket.connectToServer(); + server.onMessage(message => { setTimeout(() => socket.send(message), 1500); }); + }); + await page.addInitScript(() => { + const interval = window.setInterval.bind(window); + window.setInterval = ((handler, delay, ...args) => interval(handler, delay === 50 ? 1500 : delay, ...args)) as typeof window.setInterval; + }); await enter(page); // Gather from the spawn clearing before testing keyboard movement. A delayed // key release can otherwise change the nearest resource (including its risk @@ -61,7 +72,7 @@ test('desktop renders models, moves authoritatively, gathers, and reconnects', a }; }); return (gatherDiagnostics as { gainedXP: boolean }).gainedXP; - }, { timeout: 15000, message: 'Gather must award profession XP' }).toBe(true); + }, { timeout: 30000, message: 'Gather must award profession XP despite delayed stop acknowledgement' }).toBe(true); } catch (error) { // toMatchObject omits unmatched properties from its diff. Put the complete // last sample in the error so CI's text log actually includes diagnostics.