import { Component } from "../ecs/components.js"; import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js"; import { chunkBoundsAround, findPath } from "../navigation/astar.js"; import { chebyshev } from "../combat/service.js"; const FOLLOW_BREAK_TILES = CHUNK_SIZE; const FOLLOW_ALWAYS_RUN_TILES = 3; const FOLLOW_TRAIL_MAX = CHUNK_SIZE * 2; function canEntityRun (ecs, handle) { const stamina = ecs.get(handle, Component.stamina); if (!stamina) { return true; } return Number(stamina.current) > 0; } export function clearFollow (ecs, entityId) { const handle = ecs.handleOf(entityId); const follow = handle != null ? ecs.get(handle, Component.follow) : null; if (follow) { follow.targetEntityId = null; follow.trail = []; } } export function setFollow (ecs, followerId, targetId) { const handle = ecs.handleOf(followerId); if (handle == null) { return false; } let follow = ecs.get(handle, Component.follow); if (!follow) { follow = ecs.add(handle, Component.follow, { targetEntityId: null, trail: [] }); } follow.targetEntityId = targetId; follow.trail = []; return true; } /** Record a tile the target just vacated so followers can reuse that path. */ export function recordFollowTrail (ecs, targetEntityId, fromTileX, fromTileY) { for (const entity of ecs.query(Component.follow)) { const follow = entity.follow; if (follow.targetEntityId !== targetEntityId) { continue; } if (!Array.isArray(follow.trail)) { follow.trail = []; } const last = follow.trail[follow.trail.length - 1]; if (last && last.x === fromTileX && last.y === fromTileY) { continue; } follow.trail.push({ x: fromTileX, y: fromTileY }); if (follow.trail.length > FOLLOW_TRAIL_MAX) { follow.trail.splice(0, follow.trail.length - FOLLOW_TRAIL_MAX); } } } /** Drop trail tiles the follower has already reached. */ export function consumeFollowTrail (ecs, followerId, tileX, tileY) { const handle = ecs.handleOf(followerId); const follow = handle != null ? ecs.get(handle, Component.follow) : null; if (!follow?.trail?.length) { return; } while (follow.trail.length && follow.trail[0].x === tileX && follow.trail[0].y === tileY) { follow.trail.shift(); } } export function stepFollow (world) { for (const entity of world.ecs.query(Component.follow, Component.position, Component.movement)) { const follow = entity.follow; const targetId = follow.targetEntityId; if (targetId == null) { continue; } if (world.frozen?.has?.(entity.entityId)) { continue; } const target = resolveFollowTarget(world, targetId); if (!target) { follow.targetEntityId = null; follow.trail = []; continue; } const dist = chebyshev( entity.position.tileX, entity.position.tileY, target.pos.tileX, target.pos.tileY ); if (dist > FOLLOW_BREAK_TILES) { follow.targetEntityId = null; follow.trail = []; clearPath(world.ecs, entity.handle); continue; } const canRun = canEntityRun(world.ecs, entity.handle); if (dist > FOLLOW_ALWAYS_RUN_TILES) { entity.movement.gait = canRun ? GAIT.run : GAIT.walk; } else if (dist > 1) { entity.movement.gait = canRun && target.gait === GAIT.run ? GAIT.run : GAIT.walk; } if (!Array.isArray(follow.trail)) { follow.trail = []; } trimTrailBehindFollower(follow.trail, entity.position); const targetMoving = (target.pathTiles?.length ?? 0) > 0 || follow.trail.length > 0; // Stay put only when already adjacent and the target is idle — otherwise keep // chasing footsteps (including waiting on the target's current tile). if (dist <= 1 && !targetMoving) { clearPath(world.ecs, entity.handle); continue; } const reused = buildFollowPath( world, entity.entityId, entity.position, target.pos, follow.trail, target.pathTiles ); let path = world.ecs.get(entity.handle, Component.path); if (!path) { path = world.ecs.add(entity.handle, Component.path, { tiles: [] }); } if (reused.waiting) { // Next trail tile is occupied — wait for vacancy instead of detouring. assignFollowPath(entity.movement, path, reused.tiles); continue; } if (reused.tiles.length) { assignFollowPath(entity.movement, path, reused.tiles); continue; } // No shared trail/path yet — catch up with a one-shot approach path. queueCatchUp(world, entity.entityId, entity.handle, entity.position, target.pos, path); } } function resolveFollowTarget (world, targetId) { const targetHandle = world.ecs.handleOf(targetId); if (targetHandle != null) { const pos = world.ecs.get(targetHandle, Component.position); if (!pos) { return null; } const movement = world.ecs.get(targetHandle, Component.movement); const path = world.ecs.get(targetHandle, Component.path); return { pos, gait: movement?.gait, pathTiles: path?.tiles ?? [] }; } const ghost = world.ghosts?.get?.(targetId); if (!ghost) { return null; } return { pos: { tileX: ghost.tileX, tileY: ghost.tileY }, gait: ghost.gait, pathTiles: [] }; } function buildFollowPath (world, followerId, followerPos, targetPos, trail, targetPathTiles) { const combined = []; for (const tile of trail) { combined.push({ x: tile.x, y: tile.y }); } const lastTrail = combined[combined.length - 1]; if (!lastTrail || lastTrail.x !== targetPos.tileX || lastTrail.y !== targetPos.tileY) { combined.push({ x: targetPos.tileX, y: targetPos.tileY }); } for (const tile of targetPathTiles) { const prev = combined[combined.length - 1]; if (prev && prev.x === tile.x && prev.y === tile.y) { continue; } combined.push({ x: tile.x, y: tile.y }); } const result = []; let cursorX = followerPos.tileX; let cursorY = followerPos.tileY; for (const tile of combined) { if (tile.x === cursorX && tile.y === cursorY) { continue; } const stepDist = chebyshev(cursorX, cursorY, tile.x, tile.y); if (stepDist > 1) { // Gap between follower and trail — fall through to catch-up. if (!result.length) { return { tiles: [], waiting: false }; } break; } if (!world.walkableFor(followerId, tile.x, tile.y)) { // Occupied (usually by the follow target) — wait on that footstep. if (!result.length) { return { tiles: [{ x: tile.x, y: tile.y }], waiting: true }; } break; } const prev = result[result.length - 1]; if (prev && prev.x === tile.x && prev.y === tile.y) { continue; } result.push({ x: tile.x, y: tile.y }); cursorX = tile.x; cursorY = tile.y; } return { tiles: result, waiting: false }; } function assignFollowPath (movement, path, tiles) { const prevNext = path.tiles?.[0]; const next = tiles[0]; const nextChanged = !next || !prevNext || prevNext.x !== next.x || prevNext.y !== next.y; path.tiles = tiles; // Only reset walk cadence when the immediate step changes; refreshing the // remaining trail every systems tick must not stall walking followers. if (nextChanged) { movement.walkCarry = 0; } } function trimTrailBehindFollower (trail, position) { while (trail.length) { const next = trail[0]; if (next.x === position.tileX && next.y === position.tileY) { trail.shift(); continue; } break; } } function queueCatchUp (world, entityId, handle, position, targetPos, path) { const goal = { x: targetPos.tileX, y: targetPos.tileY }; if (position.tileX === goal.x && position.tileY === goal.y) { path.tiles = []; return; } if (path.tiles?.length) { const last = path.tiles[path.tiles.length - 1]; const first = path.tiles[0]; if (last.x === goal.x && last.y === goal.y && chebyshev(position.tileX, position.tileY, first.x, first.y) <= 1) { return; } } const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); const nextTiles = findPath( position.tileX, position.tileY, goal.x, goal.y, (x, y) => { // Plan onto the target tile; movement still waits while they occupy it. if (x === goal.x && y === goal.y) { return world.isEnterable?.(x, y) ?? world.walkableFor(entityId, x, y); } return world.walkableFor(entityId, x, y); }, bounds.minX, bounds.minY, bounds.maxX, bounds.maxY, { preferVertical: true } ); const movement = world.ecs.get(handle, Component.movement); if (movement) { assignFollowPath(movement, path, nextTiles); } else { path.tiles = nextTiles; } } function clearPath (ecs, handle) { const path = ecs.get(handle, Component.path); if (path) { path.tiles = []; } }