import { Component } from "../ecs/components.js"; import { tileToChunk, tileToLocal } from "../world/coordinates.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; /** Find a neighbouring (or same) tile with the given block type. Prefer non-self when possible. */ export function findAdjacentBlockTile (world, tileX, tileY, blockType, dimensionId = OVERWORLD_ID) { const dim = normalizeDimensionId(dimensionId); let fallback = null; for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { const x = tileX + dx; const y = tileY + dy; const data = world.chunkData(tileToChunk(x), tileToChunk(y), dim); const index = tileToLocal(y) * CHUNK_SIZE + tileToLocal(x); if (data.blocks[index]?.typeId !== blockType) { continue; } if (dx !== 0 || dy !== 0) { return { tileX: x, tileY: y }; } fallback = { tileX: x, tileY: y }; } } return fallback; } /** Shove the actor toward a world tile (bank, furnace, mine, craft, …). */ export function tileShoveEvent (entityId, fromTileX, fromTileY, tileX, tileY, dimensionId = OVERWORLD_ID) { return { type: "tile-shove", attackerId: entityId, fromTileX, fromTileY, tileX, tileY, dimensionId: normalizeDimensionId(dimensionId) }; } export function tileShoveTowardBlock (world, handle, entityId, blockType) { const position = world.ecs.get(handle, Component.position); if (!position) { return null; } const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); const target = findAdjacentBlockTile(world, position.tileX, position.tileY, blockType, dim); if (!target) { return null; } return tileShoveEvent(entityId, position.tileX, position.tileY, target.tileX, target.tileY, dim); }