| 1 | import { BlockType } from "../definitions/tiles.js"; |
| 2 | |
| 3 | /** Chebyshev distance between tile centres (0 = same tile). */ |
| 4 | export function chebyshevTileDistance (ax, ay, bx, by) |
| 5 | { |
| 6 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 7 | } |
| 8 | |
| 9 | /** Orthogonal or diagonal neighbour — not the same tile. Block interaction range. */ |
| 10 | export function isAdjacentTile (ax, ay, bx, by) |
| 11 | { |
| 12 | return chebyshevTileDistance(ax, ay, bx, by) === 1; |
| 13 | } |
| 14 | |
| 15 | export function isSameTile (ax, ay, bx, by) |
| 16 | { |
| 17 | return ax === bx && ay === by; |
| 18 | } |
| 19 | |
| 20 | /** Furniture you can talk / shop across (NPC counter side). */ |
| 21 | export function isNpcCounterBlockType (typeId) |
| 22 | { |
| 23 | return typeId === BlockType.table || typeId === BlockType.marketplace; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Talk / shop reach: direct adjacency, or both sides of a table/counter. |
| 28 | * @param {(tileX: number, tileY: number) => number|null|undefined} getBlockTypeAt |
| 29 | */ |
| 30 | export function canTalkOrShopNear (playerX, playerY, npcX, npcY, getBlockTypeAt) |
| 31 | { |
| 32 | if (isAdjacentTile(playerX, playerY, npcX, npcY)) |
| 33 | { return true; } |
| 34 | if (typeof getBlockTypeAt !== "function") |
| 35 | { return false; } |
| 36 | for (let dy = -1; dy <= 1; dy++) |
| 37 | { |
| 38 | for (let dx = -1; dx <= 1; dx++) |
| 39 | { |
| 40 | if (dx === 0 && dy === 0) |
| 41 | { continue; } |
| 42 | const counterX = npcX + dx; |
| 43 | const counterY = npcY + dy; |
| 44 | if (!isNpcCounterBlockType(getBlockTypeAt(counterX, counterY))) |
| 45 | { continue; } |
| 46 | if (isAdjacentTile(playerX, playerY, counterX, counterY)) |
| 47 | { return true; } |
| 48 | } |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** True when blockType exists on a tile adjacent to (tileX, tileY), not on that tile. */ |
| 54 | export function hasAdjacentBlockType (getBlockTypeAt, tileX, tileY, blockType) |
| 55 | { |
| 56 | for (let dy = -1; dy <= 1; dy++) |
| 57 | { |
| 58 | for (let dx = -1; dx <= 1; dx++) |
| 59 | { |
| 60 | if (dx === 0 && dy === 0) |
| 61 | { continue; } |
| 62 | if (getBlockTypeAt(tileX + dx, tileY + dy) === blockType) |
| 63 | { return true; } |
| 64 | } |
| 65 | } |
| 66 | return false; |
| 67 | } |