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; 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 targetHandle = world.ecs.handleOf(targetId); if (targetHandle == null) { follow.targetEntityId = null; follow.trail = []; continue; } const targetPos = world.ecs.get(targetHandle, Component.position); if (!targetPos) { follow.targetEntityId = null; follow.trail = []; continue; } const dist = chebyshev( entity.position.tileX, entity.position.tileY, targetPos.tileX, targetPos.tileY ); if (dist > FOLLOW_BREAK_TILES) { follow.targetEntityId = null; follow.trail = []; clearPath(world.ecs, entity.handle); continue; } const targetMovement = world.ecs.get(targetHandle, Component.movement); if (dist > FOLLOW_ALWAYS_RUN_TILES) { entity.movement.gait = GAIT.run; } else if (dist > 1) { entity.movement.gait = targetMovement?.gait === GAIT.run ? GAIT.run : GAIT.walk; } if (dist <= 1) { clearPath(world.ecs, entity.handle); continue; } if (!Array.isArray(follow.trail)) { follow.trail = []; } trimTrailBehindFollower(follow.trail, entity.position); const targetPath = world.ecs.get(targetHandle, Component.path)?.tiles ?? []; const reused = buildFollowPath( world, entity.entityId, entity.position, targetPos, follow.trail, targetPath ); let path = world.ecs.get(entity.handle, Component.path); if (!path) { path = world.ecs.add(entity.handle, Component.path, { tiles: [] }); } if (reused.length) { path.tiles = reused; entity.movement.walkCarry = 0; continue; } // No shared trail/path yet — catch up with a one-shot approach path. queueCatchUp(world, entity.entityId, entity.handle, entity.position, targetPos, path); } } function buildFollowPath (world, followerId, followerPos, targetPos, trail, targetPathTiles) { const combined = []; for (const tile of trail) { combined.push({ x: tile.x, y: tile.y }); } for (const tile of targetPathTiles) { combined.push({ x: tile.x, y: tile.y }); } const result = []; for (const tile of combined) { if (tile.x === targetPos.tileX && tile.y === targetPos.tileY) { break; } if (tile.x === followerPos.tileX && tile.y === followerPos.tileY) { continue; } if (!world.walkableFor(followerId, tile.x, tile.y)) { break; } const prev = result[result.length - 1]; if (prev && prev.x === tile.x && prev.y === tile.y) { continue; } result.push(tile); } return result; } 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 = bestAdjacentGoal(world, entityId, position, targetPos); if (!goal) { path.tiles = []; return; } if (path.tiles?.length) { const last = path.tiles[path.tiles.length - 1]; if (last.x === goal.x && last.y === goal.y) { return; } } const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); path.tiles = findPath( position.tileX, position.tileY, goal.x, goal.y, (x, y) => world.walkableFor(entityId, x, y), bounds.minX, bounds.minY, bounds.maxX, bounds.maxY ); const movement = world.ecs.get(handle, Component.movement); if (movement) { movement.walkCarry = 0; } } function clearPath (ecs, handle) { const path = ecs.get(handle, Component.path); if (path) { path.tiles = []; } } function bestAdjacentGoal (world, entityId, position, targetPos) { const dirs = [ { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } ]; let best = null; let bestDist = Infinity; for (const dir of dirs) { const x = targetPos.tileX + dir.x; const y = targetPos.tileY + dir.y; if (x === position.tileX && y === position.tileY) { return { x, y }; } if (!world.walkableFor(entityId, x, y)) { continue; } const dist = chebyshev(position.tileX, position.tileY, x, y); if (dist < bestDist) { bestDist = dist; best = { x, y }; } } return best; }