import type { Position } from '../../../sdk/types.js'; import { assessRoutes, assessRoutesFromEvidence, type RouteEvidence, } from './escape-planner.js'; function dist(ax: number, ay: number, bx: number, by: number): number { return Math.hypot(ax - bx, ay - by); } /** * 离线从烘焙地图算出的「离路点」:远离任务点/出生点、避开走廊与枢纽的低人流坐标 * (世界像素,与 GameState / 可行走网格同坐标系)。HIDE_SPOTS(藏身角落)是它的具体集合, * 共用下面的选点器。 */ export interface OffRoutePoint extends Position { /** 所在房间,仅供日志/调试。 */ room: string; } export interface NearestSafePointOptions { /** 点离任一威胁多近就硬排除。 */ threatExcludeRadius: number; /** 去该点的测地路径经过威胁这个距离内也排除(避免走向把守者)。 */ pathThreatRadius: number; /** 当前已选点:仍是合法候选就保持不变,杜绝等距摇摆。 */ stickyTo?: T | null; /** 被标记为不可达的移动目标:在其附近的点跳过。 */ blockedTarget?: Position | null; /** Current-frame parallel A* projection; invalid or incomplete evidence falls back safely. */ routeEvidence?: Readonly | null; } const BLOCKED_RADIUS = 12; function samePoint(a: Position, b: Position): boolean { return a.x === b.x && a.y === b.y; } /** 所有点都被威胁封死时的兜底:离威胁最远的点(仍好过原地等死)。 */ function farthestFromThreats( points: readonly T[], threatPoints: Position[], blocked?: Position | null, ): T | null { let best: T | null = null; let bestMin = -Infinity; for (const s of points) { if (blocked && dist(s.x, s.y, blocked.x, blocked.y) <= BLOCKED_RADIUS) continue; const minD = threatPoints.length === 0 ? 0 : Math.min(...threatPoints.map(p => dist(s.x, s.y, p.x, p.y))); if (minD > bestMin) { bestMin = minD; best = s; } } return best; } /** * 从离线点集里选「测地最近的安全点」:威胁旁的点硬排除、去路经过威胁附近的点排除,余者取测地最近 * (distance-field 一次扫描;测地不可达按欧氏垫底),带粘性(当前点仍合法就不换)。全部被威胁封死 * 时退回「离威胁最远」的点。nearestSafeHideSpot 复用此选点器。 */ export function nearestSafePoint( points: readonly T[], from: Position, threatPoints: Position[], opts: NearestSafePointOptions, ): T | null { const excludeR = opts.threatExcludeRadius; const blocked = opts.blockedTarget ?? null; const candidates = points.filter(s => (!blocked || dist(s.x, s.y, blocked.x, blocked.y) > BLOCKED_RADIUS) && !threatPoints.some(p => dist(s.x, s.y, p.x, p.y) <= excludeR)); if (candidates.length === 0) return farthestFromThreats(points, threatPoints, blocked); // 无威胁时粘性可零成本短路(不必为路径检查扫距离场)。 if (opts.stickyTo && threatPoints.length === 0) { const sticky = candidates.find(c => samePoint(c, opts.stickyTo!)); if (sticky) return sticky; } const targets = candidates.map(c => ({ x: c.x, y: c.y })); const routes = assessRoutesFromEvidence( opts.routeEvidence, from, targets, threatPoints, opts.pathThreatRadius, ) ?? assessRoutes(from, targets, threatPoints, opts.pathThreatRadius); const indexed = candidates.map((c, i) => ({ c, i })); const viable = routes == null ? indexed : indexed.filter(({ i }) => !routes[i].nearThreat); const pool = viable.length > 0 ? viable : indexed; if (opts.stickyTo) { const sticky = pool.find(({ c }) => samePoint(c, opts.stickyTo!)); if (sticky) return sticky.c; } // 测地最近(distance-field 距离;测地不可达按欧氏垫底)。 let best = pool[0].c; let bestScore = Infinity; for (const { c, i } of pool) { const euclid = dist(from.x, from.y, c.x, c.y); const geo = routes?.[i].distancePx; const score = geo == null ? euclid : geo !== Infinity ? geo : euclid + 1e6; if (score < bestScore) { bestScore = score; best = c; } } return best; }