import type { BehaviorInput, ObservedCorpse, ObservedPlayer, PlayerRef, } from '../../sdk/behavior-plugin/index.js'; const MAX_EPOCH_SNAPSHOTS = 64; interface RememberedPlayer extends Omit {} interface RememberedCorpse extends Omit {} /** Serializable data passed to one Behavior callback, including Worker callbacks. */ export interface ClawclawBehaviorObservationSnapshot { readonly epochId: number; readonly revision: number; readonly nowMs: number; readonly players: readonly Readonly[]; readonly corpses: readonly Readonly[]; } function deepFreeze(value: T): T { if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value; Object.freeze(value); for (const child of Object.values(value as Record)) deepFreeze(child); return value; } function playerMatches(ref: Readonly, player: Readonly): boolean { return ref.seat === player.seat; } function playerRef(value: unknown): PlayerRef | undefined { if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined; const source = value as Record; const nested = source.player && typeof source.player === 'object' && !Array.isArray(source.player) ? source.player as Record : source; const seat = nested.seat; if (typeof seat !== 'number' || !Number.isInteger(seat) || seat < 1 || seat > 10) return undefined; const name = typeof nested.name === 'string' ? nested.name.trim() : ''; return { name: name || `${seat}号`, seat }; } function knownAlive( input: Readonly, ref: Readonly, ): boolean | undefined { const found = input.gameState.players?.find((player) => player.seat === ref.seat); if (!found) return undefined; const legacy = found as unknown as { readonly is_alive?: boolean }; return found.known_status?.is_alive ?? legacy.is_alive; } /** * Runtime-owned cross-tick observation memory shared by finite and continuous * Behavior hosts. Each capture is detached and remains stable after later ticks. */ export class ClawclawBehaviorObservationMemory { private readonly players = new Map(); private readonly corpses = new Map(); private readonly snapshots = new Map(); private scopeKey?: string; private revisionValue = 0; capture(input: Readonly): Readonly { const scopeKey = `${input.scope.sessionId}\u0000${input.scope.segmentId}`; if (this.scopeKey !== scopeKey) { this.clear(); this.scopeKey = scopeKey; } const existing = this.snapshots.get(input.epochId); if (existing) return existing; const observedAtMs = input.cycle?.stateReceivedAtMs ?? input.nowMs; for (const visible of input.gameState.visible_players ?? []) { const ref = playerRef(visible); if (!ref) continue; for (const [key, remembered] of this.players) { if (playerMatches(ref, remembered)) this.players.delete(key); } this.players.set(`seat:${ref.seat}`, { name: ref.name, seat: ref.seat, x: visible.x, y: visible.y, room: visible.room, ...(visible.distance === undefined ? {} : { distance: visible.distance }), ...(knownAlive(input, ref) === undefined ? {} : { isAlive: knownAlive(input, ref) }), observedAtMs, }); } for (const visible of input.gameState.visible_corpses ?? []) { const ref = playerRef(visible.player); this.corpses.set(visible.corpse_id, { id: visible.corpse_id, ...(ref === undefined ? {} : { name: ref.name, seat: ref.seat, }), x: visible.x, y: visible.y, room: visible.room, ...(visible.distance === undefined ? {} : { distance: visible.distance }), observedAtMs, }); } const snapshot = deepFreeze({ epochId: input.epochId, revision: ++this.revisionValue, nowMs: input.nowMs, players: [...this.players.values()].map((player) => ({ ...player, ageMs: Math.max(0, input.nowMs - player.observedAtMs), })), corpses: [...this.corpses.values()].map((corpse) => ({ ...corpse, ageMs: Math.max(0, input.nowMs - corpse.observedAtMs), })), } satisfies ClawclawBehaviorObservationSnapshot); this.snapshots.set(input.epochId, snapshot); while (this.snapshots.size > MAX_EPOCH_SNAPSHOTS) { const oldest = this.snapshots.keys().next().value as number | undefined; if (oldest === undefined) break; this.snapshots.delete(oldest); } return snapshot; } snapshot(epochId: number): Readonly | undefined { return this.snapshots.get(epochId); } clear(): void { this.players.clear(); this.corpses.clear(); this.snapshots.clear(); this.scopeKey = undefined; } }