import { readFileSync } from 'fs'; import { inflateSync } from 'node:zlib'; import type { VisualPosition } from './contracts.js'; import { createSpatialApi, type SpatialFacts } from '../../spatial-api.js'; export interface MapRoomArea { name: string; polygon: Array<[number, number]>; bbox: { left: number; top: number; right: number; bottom: number }; } export interface SpatialTransition { a: string; b: string; } interface RawSpatialFacts { schema_version: 2; coordinates: { x_positive: 'right'; y_positive: 'down'; }; player_body: { body_length_px: number; }; room_lookup: { encoding: 'row-major-zlib-uint8-v1'; width_tiles: number; height_tiles: number; tile_width_px: number; tile_height_px: number; room_names: string[]; data_base64: string; }; rooms: MapRoomArea[]; room_transitions: SpatialTransition[]; } function loadSpatialFacts(): RawSpatialFacts { const parsed = JSON.parse( readFileSync(new URL('../../../../assets/clawclaw-spatial-facts.json', import.meta.url), 'utf8'), ) as RawSpatialFacts; if ( parsed.schema_version !== 2 || parsed.coordinates?.x_positive !== 'right' || parsed.coordinates?.y_positive !== 'down' || parsed.room_lookup?.encoding !== 'row-major-zlib-uint8-v1' || !Number.isInteger(parsed.room_lookup.width_tiles) || parsed.room_lookup.width_tiles <= 0 || !Number.isInteger(parsed.room_lookup.height_tiles) || parsed.room_lookup.height_tiles <= 0 || !Number.isFinite(parsed.room_lookup.tile_width_px) || parsed.room_lookup.tile_width_px <= 0 || !Number.isFinite(parsed.room_lookup.tile_height_px) || parsed.room_lookup.tile_height_px <= 0 || !Array.isArray(parsed.room_lookup.room_names) || parsed.room_lookup.room_names.length === 0 || typeof parsed.room_lookup.data_base64 !== 'string' || parsed.room_lookup.data_base64.length === 0 ) { throw new Error('Unsupported ClawClaw spatial facts'); } return parsed; } export const CLAWCLAW_SPATIAL_FACTS = loadSpatialFacts(); export const PLAYER_BODY_LENGTH_PX = CLAWCLAW_SPATIAL_FACTS.player_body.body_length_px; const RAW_ROOM_LOOKUP = CLAWCLAW_SPATIAL_FACTS.room_lookup; const ROOM_LOOKUP_CELL_COUNT = RAW_ROOM_LOOKUP.width_tiles * RAW_ROOM_LOOKUP.height_tiles; const ROOM_LOOKUP_BUFFER = inflateSync(Buffer.from(RAW_ROOM_LOOKUP.data_base64, 'base64')); if (ROOM_LOOKUP_BUFFER.byteLength !== ROOM_LOOKUP_CELL_COUNT) { throw new Error('Invalid ClawClaw room lookup size'); } const ROOM_LOOKUP = new Uint8Array( ROOM_LOOKUP_BUFFER.buffer, ROOM_LOOKUP_BUFFER.byteOffset, ROOM_LOOKUP_BUFFER.byteLength, ); if (ROOM_LOOKUP.some((roomIndex) => roomIndex >= RAW_ROOM_LOOKUP.room_names.length)) { throw new Error('Invalid ClawClaw room lookup index'); } const ROOM_BY_NAME = new Map( CLAWCLAW_SPATIAL_FACTS.rooms.map((room) => [room.name, room] as const), ); export function pointInPolygon(x: number, y: number, polygon: Array<[number, number]>): boolean { let inside = false; for (let index = 0, previous = polygon.length - 1; index < polygon.length; previous = index, index += 1) { const [xi, yi] = polygon[index]; const [xj, yj] = polygon[previous]; const intersects = ((yi > y) !== (yj > y)) && x < ((xj - xi) * (y - yi)) / (yj - yi || Number.EPSILON) + xi; if (intersects) inside = !inside; } return inside; } export function spatialRoom(name: string | null | undefined): MapRoomArea | null { return name ? ROOM_BY_NAME.get(name) ?? null : null; } export function roomAt(x: number, y: number): MapRoomArea | null { return CLAWCLAW_SPATIAL_FACTS.rooms.find((room) => pointInPolygon(x, y, room.polygon)) ?? null; } export function resolveRoom(position: Pick): string | null { if (!Number.isFinite(position.x) || !Number.isFinite(position.y)) return null; const tileX = Math.max(0, Math.min( RAW_ROOM_LOOKUP.width_tiles - 1, Math.floor(position.x / RAW_ROOM_LOOKUP.tile_width_px), )); const tileY = Math.max(0, Math.min( RAW_ROOM_LOOKUP.height_tiles - 1, Math.floor(position.y / RAW_ROOM_LOOKUP.tile_height_px), )); const roomIndex = ROOM_LOOKUP[tileY * RAW_ROOM_LOOKUP.width_tiles + tileX]; return RAW_ROOM_LOOKUP.room_names[roomIndex] ?? null; } export function sideInRoom(position: VisualPosition): string | null { const room = spatialRoom(position.room); if (!room || !pointInPolygon(position.x, position.y, room.polygon)) return null; const width = room.bbox.right - room.bbox.left; const height = room.bbox.bottom - room.bbox.top; if (width <= 0 || height <= 0) return null; const rx = (position.x - room.bbox.left) / width; const ry = (position.y - room.bbox.top) / height; const horizontal = rx < 0.33 ? '左' : rx > 0.67 ? '右' : ''; const vertical = ry < 0.33 ? '上' : ry > 0.67 ? '下' : ''; return horizontal || vertical ? `${horizontal}${vertical}侧` : '中部'; } export function placeLabel(position: VisualPosition): string { if (!position.room) return '未命名区域'; const side = sideInRoom(position); return side ? `${position.room}${side}` : position.room; } export function transitionIsMapped(from: string | null, to: string | null): boolean { if (!from || !to || from === to) return false; return CLAWCLAW_SPATIAL_FACTS.room_transitions.some((transition) => ( (transition.a === from && transition.b === to) || (transition.a === to && transition.b === from) )); } export const CLAWCLAW_PERCEPTION_SPATIAL_FACTS: SpatialFacts = Object.freeze({ coordinates: Object.freeze({ xPositive: 'right', yPositive: 'down' }), playerBodyLengthPx: PLAYER_BODY_LENGTH_PX, rooms: CLAWCLAW_SPATIAL_FACTS.rooms, roomTransitions: CLAWCLAW_SPATIAL_FACTS.room_transitions.map(({ a, b }) => ({ a, b })), }); /** Read-only map capability exposed to perception module code. */ export const CLAWCLAW_PERCEPTION_SPATIAL_API = createSpatialApi( CLAWCLAW_PERCEPTION_SPATIAL_FACTS, );