import { BlockType } from "../definitions/tiles.js"; /** Chebyshev distance between tile centres (0 = same tile). */ export function chebyshevTileDistance (ax, ay, bx, by) { return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); } /** Orthogonal or diagonal neighbour — not the same tile. Block interaction range. */ export function isAdjacentTile (ax, ay, bx, by) { return chebyshevTileDistance(ax, ay, bx, by) === 1; } export function isSameTile (ax, ay, bx, by) { return ax === bx && ay === by; } /** Furniture you can talk / shop across (NPC counter side). */ export function isNpcCounterBlockType (typeId) { return typeId === BlockType.table || typeId === BlockType.marketplace; } /** * Talk / shop reach: direct adjacency, or both sides of a table/counter. * @param {(tileX: number, tileY: number) => number|null|undefined} getBlockTypeAt */ export function canTalkOrShopNear (playerX, playerY, npcX, npcY, getBlockTypeAt) { if (isAdjacentTile(playerX, playerY, npcX, npcY)) { return true; } if (typeof getBlockTypeAt !== "function") { return false; } for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { if (dx === 0 && dy === 0) { continue; } const counterX = npcX + dx; const counterY = npcY + dy; if (!isNpcCounterBlockType(getBlockTypeAt(counterX, counterY))) { continue; } if (isAdjacentTile(playerX, playerY, counterX, counterY)) { return true; } } } return false; } /** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */ export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType) { for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { if (dx === 0 && dy === 0) { continue; } if (getBlockTypeAt(tileX + dx, tileY + dy) === blockType) { return true; } } } return false; }