| 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 target = resolveFollowTarget(world, targetId); |
| 76 | if (!target) |
| 77 | { |
| 78 | follow.targetEntityId = null; |
| 79 | follow.trail = []; |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | const dist = chebyshev( |
| 84 | entity.position.tileX, |
| 85 | entity.position.tileY, |
| 86 | target.pos.tileX, |
| 87 | target.pos.tileY |
| 88 | ); |
| 89 | if (dist > FOLLOW_BREAK_TILES) |
| 90 | { |
| 91 | follow.targetEntityId = null; |
| 92 | follow.trail = []; |
| 93 | clearPath(world.ecs, entity.handle); |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | if (dist > FOLLOW_ALWAYS_RUN_TILES) |
| 98 | { entity.movement.gait = GAIT.run; } |
| 99 | else if (dist > 1) |
| 100 | { entity.movement.gait = target.gait === GAIT.run ? GAIT.run : GAIT.walk; } |
| 101 | |
| 102 | if (!Array.isArray(follow.trail)) |
| 103 | { follow.trail = []; } |
| 104 | trimTrailBehindFollower(follow.trail, entity.position); |
| 105 | |
| 106 | const targetMoving = (target.pathTiles?.length ?? 0) > 0 || follow.trail.length > 0; |
| 107 | // Stay put only when already adjacent and the target is idle — otherwise keep |
| 108 | // chasing footsteps (including waiting on the target's current tile). |
| 109 | if (dist <= 1 && !targetMoving) |
| 110 | { |
| 111 | clearPath(world.ecs, entity.handle); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | const reused = buildFollowPath( |
| 116 | world, |
| 117 | entity.entityId, |
| 118 | entity.position, |
| 119 | target.pos, |
| 120 | follow.trail, |
| 121 | target.pathTiles |
| 122 | ); |
| 123 | |
| 124 | let path = world.ecs.get(entity.handle, Component.path); |
| 125 | if (!path) |
| 126 | { path = world.ecs.add(entity.handle, Component.path, { tiles: [] }); } |
| 127 | |
| 128 | if (reused.waiting) |
| 129 | { |
| 130 | // Next trail tile is occupied — wait for vacancy instead of detouring. |
| 131 | assignFollowPath(entity.movement, path, reused.tiles); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (reused.tiles.length) |
| 136 | { |
| 137 | assignFollowPath(entity.movement, path, reused.tiles); |
| 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, target.pos, path); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | function resolveFollowTarget (world, targetId) |
| 147 | { |
| 148 | const targetHandle = world.ecs.handleOf(targetId); |
| 149 | if (targetHandle != null) |
| 150 | { |
| 151 | const pos = world.ecs.get(targetHandle, Component.position); |
| 152 | if (!pos) |
| 153 | { return null; } |
| 154 | const movement = world.ecs.get(targetHandle, Component.movement); |
| 155 | const path = world.ecs.get(targetHandle, Component.path); |
| 156 | return { |
| 157 | pos, |
| 158 | gait: movement?.gait, |
| 159 | pathTiles: path?.tiles ?? [] |
| 160 | }; |
| 161 | } |
| 162 | const ghost = world.ghosts?.get?.(targetId); |
| 163 | if (!ghost) |
| 164 | { return null; } |
| 165 | return { |
| 166 | pos: { tileX: ghost.tileX, tileY: ghost.tileY }, |
| 167 | gait: ghost.gait, |
| 168 | pathTiles: [] |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | function buildFollowPath (world, followerId, followerPos, targetPos, trail, targetPathTiles) |
| 173 | { |
| 174 | const combined = []; |
| 175 | for (const tile of trail) |
| 176 | { combined.push({ x: tile.x, y: tile.y }); } |
| 177 | const lastTrail = combined[combined.length - 1]; |
| 178 | if (!lastTrail || lastTrail.x !== targetPos.tileX || lastTrail.y !== targetPos.tileY) |
| 179 | { combined.push({ x: targetPos.tileX, y: targetPos.tileY }); } |
| 180 | for (const tile of targetPathTiles) |
| 181 | { |
| 182 | const prev = combined[combined.length - 1]; |
| 183 | if (prev && prev.x === tile.x && prev.y === tile.y) |
| 184 | { continue; } |
| 185 | combined.push({ x: tile.x, y: tile.y }); |
| 186 | } |
| 187 | |
| 188 | const result = []; |
| 189 | let cursorX = followerPos.tileX; |
| 190 | let cursorY = followerPos.tileY; |
| 191 | for (const tile of combined) |
| 192 | { |
| 193 | if (tile.x === cursorX && tile.y === cursorY) |
| 194 | { continue; } |
| 195 | const stepDist = chebyshev(cursorX, cursorY, tile.x, tile.y); |
| 196 | if (stepDist > 1) |
| 197 | { |
| 198 | // Gap between follower and trail — fall through to catch-up. |
| 199 | if (!result.length) |
| 200 | { return { tiles: [], waiting: false }; } |
| 201 | break; |
| 202 | } |
| 203 | if (!world.walkableFor(followerId, tile.x, tile.y)) |
| 204 | { |
| 205 | // Occupied (usually by the follow target) — wait on that footstep. |
| 206 | if (!result.length) |
| 207 | { return { tiles: [{ x: tile.x, y: tile.y }], waiting: true }; } |
| 208 | break; |
| 209 | } |
| 210 | const prev = result[result.length - 1]; |
| 211 | if (prev && prev.x === tile.x && prev.y === tile.y) |
| 212 | { continue; } |
| 213 | result.push({ x: tile.x, y: tile.y }); |
| 214 | cursorX = tile.x; |
| 215 | cursorY = tile.y; |
| 216 | } |
| 217 | return { tiles: result, waiting: false }; |
| 218 | } |
| 219 | |
| 220 | function assignFollowPath (movement, path, tiles) |
| 221 | { |
| 222 | const prevNext = path.tiles?.[0]; |
| 223 | const next = tiles[0]; |
| 224 | const nextChanged = !next |
| 225 | || !prevNext |
| 226 | || prevNext.x !== next.x |
| 227 | || prevNext.y !== next.y; |
| 228 | path.tiles = tiles; |
| 229 | // Only reset walk cadence when the immediate step changes; refreshing the |
| 230 | // remaining trail every systems tick must not stall walking followers. |
| 231 | if (nextChanged) |
| 232 | { movement.walkCarry = 0; } |
| 233 | } |
| 234 | |
| 235 | function trimTrailBehindFollower (trail, position) |
| 236 | { |
| 237 | while (trail.length) |
| 238 | { |
| 239 | const next = trail[0]; |
| 240 | if (next.x === position.tileX && next.y === position.tileY) |
| 241 | { |
| 242 | trail.shift(); |
| 243 | continue; |
| 244 | } |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | function queueCatchUp (world, entityId, handle, position, targetPos, path) |
| 250 | { |
| 251 | const goal = { x: targetPos.tileX, y: targetPos.tileY }; |
| 252 | if (position.tileX === goal.x && position.tileY === goal.y) |
| 253 | { |
| 254 | path.tiles = []; |
| 255 | return; |
| 256 | } |
| 257 | if (path.tiles?.length) |
| 258 | { |
| 259 | const last = path.tiles[path.tiles.length - 1]; |
| 260 | const first = path.tiles[0]; |
| 261 | if (last.x === goal.x && last.y === goal.y |
| 262 | && chebyshev(position.tileX, position.tileY, first.x, first.y) <= 1) |
| 263 | { return; } |
| 264 | } |
| 265 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 266 | const nextTiles = findPath( |
| 267 | position.tileX, |
| 268 | position.tileY, |
| 269 | goal.x, |
| 270 | goal.y, |
| 271 | (x, y) => |
| 272 | { |
| 273 | // Plan onto the target tile; movement still waits while they occupy it. |
| 274 | if (x === goal.x && y === goal.y) |
| 275 | { return world.isEnterable?.(x, y) ?? world.walkableFor(entityId, x, y); } |
| 276 | return world.walkableFor(entityId, x, y); |
| 277 | }, |
| 278 | bounds.minX, |
| 279 | bounds.minY, |
| 280 | bounds.maxX, |
| 281 | bounds.maxY |
| 282 | ); |
| 283 | const movement = world.ecs.get(handle, Component.movement); |
| 284 | if (movement) |
| 285 | { assignFollowPath(movement, path, nextTiles); } |
| 286 | else |
| 287 | { path.tiles = nextTiles; } |
| 288 | } |
| 289 | |
| 290 | function clearPath (ecs, handle) |
| 291 | { |
| 292 | const path = ecs.get(handle, Component.path); |
| 293 | if (path) |
| 294 | { path.tiles = []; } |
| 295 | } |