| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js"; |
| 3 | import { chunkBoundsAround, findPath } from "../navigation/astar.js"; |
| 4 | import { chebyshev } from "../combat/service.js"; |
| 5 | |
| 6 | const FOLLOW_BREAK_TILES = CHUNK_SIZE; |
| 7 | const FOLLOW_ALWAYS_RUN_TILES = 3; |
| 8 | const FOLLOW_TRAIL_MAX = CHUNK_SIZE * 2; |
| 9 | |
| 10 | export function clearFollow (ecs, entityId) |
| 11 | { |
| 12 | const handle = ecs.handleOf(entityId); |
| 13 | const follow = handle != null ? ecs.get(handle, Component.follow) : null; |
| 14 | if (follow) |
| 15 | { |
| 16 | follow.targetEntityId = null; |
| 17 | follow.trail = []; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | export function setFollow (ecs, followerId, targetId) |
| 22 | { |
| 23 | const handle = ecs.handleOf(followerId); |
| 24 | if (handle == null) |
| 25 | { return false; } |
| 26 | let follow = ecs.get(handle, Component.follow); |
| 27 | if (!follow) |
| 28 | { follow = ecs.add(handle, Component.follow, { targetEntityId: null, trail: [] }); } |
| 29 | follow.targetEntityId = targetId; |
| 30 | follow.trail = []; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | /** Record a tile the target just vacated so followers can reuse that path. */ |
| 35 | export function recordFollowTrail (ecs, targetEntityId, fromTileX, fromTileY) |
| 36 | { |
| 37 | for (const entity of ecs.query(Component.follow)) |
| 38 | { |
| 39 | const follow = entity.follow; |
| 40 | if (follow.targetEntityId !== targetEntityId) |
| 41 | { continue; } |
| 42 | if (!Array.isArray(follow.trail)) |
| 43 | { follow.trail = []; } |
| 44 | const last = follow.trail[follow.trail.length - 1]; |
| 45 | if (last && last.x === fromTileX && last.y === fromTileY) |
| 46 | { continue; } |
| 47 | follow.trail.push({ x: fromTileX, y: fromTileY }); |
| 48 | if (follow.trail.length > FOLLOW_TRAIL_MAX) |
| 49 | { follow.trail.splice(0, follow.trail.length - FOLLOW_TRAIL_MAX); } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** Drop trail tiles the follower has already reached. */ |
| 54 | export function consumeFollowTrail (ecs, followerId, tileX, tileY) |
| 55 | { |
| 56 | const handle = ecs.handleOf(followerId); |
| 57 | const follow = handle != null ? ecs.get(handle, Component.follow) : null; |
| 58 | if (!follow?.trail?.length) |
| 59 | { return; } |
| 60 | while (follow.trail.length && follow.trail[0].x === tileX && follow.trail[0].y === tileY) |
| 61 | { follow.trail.shift(); } |
| 62 | } |
| 63 | |
| 64 | export function stepFollow (world) |
| 65 | { |
| 66 | for (const entity of world.ecs.query(Component.follow, Component.position, Component.movement)) |
| 67 | { |
| 68 | const follow = entity.follow; |
| 69 | const targetId = follow.targetEntityId; |
| 70 | if (targetId == null) |
| 71 | { continue; } |
| 72 | if (world.frozen?.has?.(entity.entityId)) |
| 73 | { continue; } |
| 74 | |
| 75 | const targetHandle = world.ecs.handleOf(targetId); |
| 76 | if (targetHandle == null) |
| 77 | { |
| 78 | follow.targetEntityId = null; |
| 79 | follow.trail = []; |
| 80 | continue; |
| 81 | } |
| 82 | const targetPos = world.ecs.get(targetHandle, Component.position); |
| 83 | if (!targetPos) |
| 84 | { |
| 85 | follow.targetEntityId = null; |
| 86 | follow.trail = []; |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | const dist = chebyshev( |
| 91 | entity.position.tileX, |
| 92 | entity.position.tileY, |
| 93 | targetPos.tileX, |
| 94 | targetPos.tileY |
| 95 | ); |
| 96 | if (dist > FOLLOW_BREAK_TILES) |
| 97 | { |
| 98 | follow.targetEntityId = null; |
| 99 | follow.trail = []; |
| 100 | clearPath(world.ecs, entity.handle); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | const targetMovement = world.ecs.get(targetHandle, Component.movement); |
| 105 | if (dist > FOLLOW_ALWAYS_RUN_TILES) |
| 106 | { entity.movement.gait = GAIT.run; } |
| 107 | else if (dist > 1) |
| 108 | { entity.movement.gait = targetMovement?.gait === GAIT.run ? GAIT.run : GAIT.walk; } |
| 109 | |
| 110 | if (dist <= 1) |
| 111 | { |
| 112 | clearPath(world.ecs, entity.handle); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (!Array.isArray(follow.trail)) |
| 117 | { follow.trail = []; } |
| 118 | trimTrailBehindFollower(follow.trail, entity.position); |
| 119 | |
| 120 | const targetPath = world.ecs.get(targetHandle, Component.path)?.tiles ?? []; |
| 121 | const reused = buildFollowPath( |
| 122 | world, |
| 123 | entity.entityId, |
| 124 | entity.position, |
| 125 | targetPos, |
| 126 | follow.trail, |
| 127 | targetPath |
| 128 | ); |
| 129 | |
| 130 | let path = world.ecs.get(entity.handle, Component.path); |
| 131 | if (!path) |
| 132 | { path = world.ecs.add(entity.handle, Component.path, { tiles: [] }); } |
| 133 | |
| 134 | if (reused.length) |
| 135 | { |
| 136 | path.tiles = reused; |
| 137 | entity.movement.walkCarry = 0; |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | // No shared trail/path yet — catch up with a one-shot approach path. |
| 142 | queueCatchUp(world, entity.entityId, entity.handle, entity.position, targetPos, path); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | function buildFollowPath (world, followerId, followerPos, targetPos, trail, targetPathTiles) |
| 147 | { |
| 148 | const combined = []; |
| 149 | for (const tile of trail) |
| 150 | { combined.push({ x: tile.x, y: tile.y }); } |
| 151 | for (const tile of targetPathTiles) |
| 152 | { combined.push({ x: tile.x, y: tile.y }); } |
| 153 | |
| 154 | const result = []; |
| 155 | for (const tile of combined) |
| 156 | { |
| 157 | if (tile.x === targetPos.tileX && tile.y === targetPos.tileY) |
| 158 | { break; } |
| 159 | if (tile.x === followerPos.tileX && tile.y === followerPos.tileY) |
| 160 | { continue; } |
| 161 | if (!world.walkableFor(followerId, tile.x, tile.y)) |
| 162 | { break; } |
| 163 | const prev = result[result.length - 1]; |
| 164 | if (prev && prev.x === tile.x && prev.y === tile.y) |
| 165 | { continue; } |
| 166 | result.push(tile); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | function trimTrailBehindFollower (trail, position) |
| 172 | { |
| 173 | while (trail.length) |
| 174 | { |
| 175 | const next = trail[0]; |
| 176 | if (next.x === position.tileX && next.y === position.tileY) |
| 177 | { |
| 178 | trail.shift(); |
| 179 | continue; |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | function queueCatchUp (world, entityId, handle, position, targetPos, path) |
| 186 | { |
| 187 | const goal = bestAdjacentGoal(world, entityId, position, targetPos); |
| 188 | if (!goal) |
| 189 | { |
| 190 | path.tiles = []; |
| 191 | return; |
| 192 | } |
| 193 | if (path.tiles?.length) |
| 194 | { |
| 195 | const last = path.tiles[path.tiles.length - 1]; |
| 196 | if (last.x === goal.x && last.y === goal.y) |
| 197 | { return; } |
| 198 | } |
| 199 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 200 | path.tiles = findPath( |
| 201 | position.tileX, |
| 202 | position.tileY, |
| 203 | goal.x, |
| 204 | goal.y, |
| 205 | (x, y) => world.walkableFor(entityId, x, y), |
| 206 | bounds.minX, |
| 207 | bounds.minY, |
| 208 | bounds.maxX, |
| 209 | bounds.maxY |
| 210 | ); |
| 211 | const movement = world.ecs.get(handle, Component.movement); |
| 212 | if (movement) |
| 213 | { movement.walkCarry = 0; } |
| 214 | } |
| 215 | |
| 216 | function clearPath (ecs, handle) |
| 217 | { |
| 218 | const path = ecs.get(handle, Component.path); |
| 219 | if (path) |
| 220 | { path.tiles = []; } |
| 221 | } |
| 222 | |
| 223 | function bestAdjacentGoal (world, entityId, position, targetPos) |
| 224 | { |
| 225 | const dirs = [ |
| 226 | { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, |
| 227 | { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } |
| 228 | ]; |
| 229 | let best = null; |
| 230 | let bestDist = Infinity; |
| 231 | for (const dir of dirs) |
| 232 | { |
| 233 | const x = targetPos.tileX + dir.x; |
| 234 | const y = targetPos.tileY + dir.y; |
| 235 | if (x === position.tileX && y === position.tileY) |
| 236 | { return { x, y }; } |
| 237 | if (!world.walkableFor(entityId, x, y)) |
| 238 | { continue; } |
| 239 | const dist = chebyshev(position.tileX, position.tileY, x, y); |
| 240 | if (dist < bestDist) |
| 241 | { |
| 242 | bestDist = dist; |
| 243 | best = { x, y }; |
| 244 | } |
| 245 | } |
| 246 | return best; |
| 247 | } |