diff --git a/client/src/main.ts b/client/src/main.ts index 8b95adb..9703beb 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -19,6 +19,12 @@ let inputElapsed = 0; let walkTo: { x: number; z: number } | undefined; let pendingAction: { op: string; target: string } | undefined; let actionElapsed = 0; +let actionAttempts = 0; +// A gather target can stay legitimately, persistently unreachable for a long time (a +// depleted resource waits out a real respawn timer measured in minutes, not a stray +// frame) -- retrying it forever would starve everything else. Give up on a target after +// a few tries and steer the next action() call to a fresh one instead. +const avoidUntil = new Map(); let requestedClose = false; const keys = new Set(); let view: WorldView; @@ -43,16 +49,18 @@ function selection(entity?: Entity) { } function action(op: string) { if (!snapshot) return; + const now = Date.now(); const kinds = op === 'attack' ? ['boss', 'sentinel'] : ['tree', 'ore', 'herb', 'corpse']; let target = view.selected; - if (!target || !kinds.includes(target.kind)) { + if (!target || !kinds.includes(target.kind) || (avoidUntil.get(target.id) ?? 0) > now) { const p = snapshot.self.entity.position; - target = snapshot.entities.filter(e => kinds.includes(e.kind)).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0]; + target = snapshot.entities.filter(e => kinds.includes(e.kind) && (avoidUntil.get(e.id) ?? 0) <= now).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0]; } if (!target) { notice('No target nearby.'); return; } selection(target); pendingAction = { op: target.kind === 'corpse' ? 'loot' : op, target: target.id }; walkTo = target.position; + actionAttempts = 0; } function connect() { requestedClose = false; sequence = 0; @@ -162,11 +170,25 @@ async function boot() { if (!pendingAction) { walkTo = 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. Keep retrying at a conservative cadence - // (well under the intent rate limit) instead of giving up forever; the - // server's own action cooldown makes repeat attempts harmless. - else { actionElapsed += dt; if (actionElapsed >= 0.3) { send(pendingAction.op, pendingAction.target); actionElapsed = 0; } } - } else { x = dx / distance; z = dz / distance; actionElapsed = 0.3; } + // rejected with no second chance, so retry at a conservative cadence (well + // under the intent rate limit) instead of giving up forever -- the server's + // own action cooldown makes repeat attempts harmless. But a target can also be + // legitimately, persistently rejected (a depleted resource waits out a real + // respawn timer), so give up on THIS target after a few tries and let the next + // action() pick a fresh one rather than retrying a lost cause indefinitely. + else { + actionElapsed += dt; + if (actionElapsed >= 0.3) { + actionElapsed = 0; + if (++actionAttempts > 8) { + avoidUntil.set(pendingAction.target, Date.now() + 8000); + const retryOp = pendingAction.op === 'loot' ? 'gather' : pendingAction.op; + walkTo = undefined; pendingAction = undefined; + action(retryOp); + } else { send(pendingAction.op, pendingAction.target); } + } + } + } else { x = dx / distance; z = dz / distance; actionElapsed = 0.3; actionAttempts = 0; } } view.moving = !!(x || z); if (inputElapsed >= 0.05) { send('move', '', x, z); inputElapsed = 0; }