import type { BehaviorInput, BehaviorReadApi, Position, } from '../behavior-plugin/index.js'; import { behaviorDistance, behaviorRooms, behaviorSelf, behaviorVisibleCorpses, type BehaviorCorpse, } from './model.js'; const SEARCH_ARRIVAL_RANGE = 110; export type CorpseSearchResult = | Readonly<{ readonly kind: 'found'; readonly corpse: BehaviorCorpse }> | Readonly<{ readonly kind: 'move'; readonly target: Position; readonly summary: string }> | Readonly<{ readonly kind: 'failed'; readonly reason: string; readonly summary: string }>; export class FiniteCorpseSearch { private selectedId?: string; private selected?: BehaviorCorpse; private routeIndex = 0; async step( input: Readonly, api: BehaviorReadApi, corpseSeat?: number, ): Promise { const visible = behaviorVisibleCorpses(input); const selectedVisible = corpseSeat !== undefined ? visible.find((corpse) => corpse.seat === corpseSeat) : this.selectedId ? visible.find((corpse) => corpse.id === this.selectedId) : [...visible].sort((left, right) => { const self = behaviorSelf(input) ?? { x: 0, y: 0 }; return behaviorDistance(self, left) - behaviorDistance(self, right); })[0]; const remembered = selectedVisible ?? (this.selectedId ? this.selected : undefined) ?? api.observation.lastSeenCorpse(corpseSeat); if (remembered) { const corpse: BehaviorCorpse = { id: remembered.id, x: remembered.x, y: remembered.y, ...('name' in remembered && remembered.name ? { name: remembered.name } : {}), ...('seat' in remembered && remembered.seat ? { seat: remembered.seat } : {}), ...('room' in remembered && remembered.room ? { room: remembered.room } : {}), ...('distance' in remembered && remembered.distance !== undefined ? { distance: remembered.distance } : {}), }; this.selectedId = corpse.id; this.selected = corpse; return { kind: 'found', corpse }; } const rooms = behaviorRooms(api); const self = behaviorSelf(input); if (!self || rooms.length === 0) { return { kind: 'failed', reason: 'search_route_unavailable', summary: '当前无法建立尸体搜索路线。' }; } while ( this.routeIndex < rooms.length && behaviorDistance(self, rooms[this.routeIndex]) <= SEARCH_ARRIVAL_RANGE ) this.routeIndex += 1; if (this.routeIndex >= rooms.length) { return { kind: 'failed', reason: 'corpse_not_found', summary: corpseSeat === undefined ? '有限搜索后仍未发现尸体。' : `有限搜索后仍未找到${corpseSeat}号玩家的尸体。`, }; } const room = rooms[this.routeIndex]; return { kind: 'move', target: room, summary: corpseSeat === undefined ? `正在有限搜索尸体,下一观察点是${room.name}。` : `正在有限搜索${corpseSeat}号玩家的尸体,下一观察点是${room.name}。`, }; } }