import { ORE_BEARING_MATERIALS, oreBoulderState, materialFromOreBoulderState, MATERIAL_DISPLAY_NAME } from "./materialTiers.js"; import { spriteImageName, resolveState } from "./tileSprites.js"; export const GroundType = Object.freeze({ grass: 1, wood: 2, water: 3, deepWater: 4 }); export const BlockType = Object.freeze({ stoneWall: 1, torch: 2, /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */ spawner: 3, bank: 4, churchAltar: 5, cauldron: 6, boulder: 7, furnace: 8, anvil: 9, woodworkingBench: 10, craftingBench: 11 }); export const GROUND_DEFINITIONS = Object.freeze({ [GroundType.grass]: Object.freeze({ id: GroundType.grass, key: "grass", displayName: "Grass", passable: true, wades: false, water: false, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [GroundType.wood]: Object.freeze({ id: GroundType.wood, key: "wood", displayName: "Wood", passable: true, wades: false, water: false, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [GroundType.water]: Object.freeze({ id: GroundType.water, key: "water", displayName: "Water", passable: true, wades: true, water: true, frames: 4, framesPerRow: 4, animationSpeed: 0.12 }), [GroundType.deepWater]: Object.freeze({ id: GroundType.deepWater, key: "deep-water", displayName: "Deep water", passable: false, wades: false, water: true, frames: 4, framesPerRow: 4, animationSpeed: 0.12 }) }); export const BLOCK_DEFINITIONS = Object.freeze({ [BlockType.stoneWall]: Object.freeze({ id: BlockType.stoneWall, key: "stone-wall", displayName: "Stone wall", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [BlockType.torch]: Object.freeze({ id: BlockType.torch, key: "torch", displayName: "Torch", passable: true, lightRangeTiles: 8, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [BlockType.spawner]: Object.freeze({ id: BlockType.spawner, key: "spawner", displayName: "Spawner", passable: true, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, /** Not shown in the placeable block palette; use the Spawners tab. */ editorHidden: true }), [BlockType.bank]: Object.freeze({ id: BlockType.bank, key: "bank", displayName: "Bank", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [BlockType.churchAltar]: Object.freeze({ id: BlockType.churchAltar, key: "church-altar", displayName: "Church altar", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [BlockType.cauldron]: Object.freeze({ id: BlockType.cauldron, key: "cauldron", displayName: "Cauldron", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0 }), [BlockType.boulder]: Object.freeze({ id: BlockType.boulder, key: "boulder", displayName: "Boulder", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */ gatherable: true }), [BlockType.furnace]: Object.freeze({ id: BlockType.furnace, key: "furnace", displayName: "Furnace", passable: false, lightRangeTiles: 4, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "furnace" }), [BlockType.anvil]: Object.freeze({ id: BlockType.anvil, key: "anvil", displayName: "Anvil", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "anvil" }), [BlockType.woodworkingBench]: Object.freeze({ id: BlockType.woodworkingBench, key: "woodworking-bench", displayName: "Woodworking bench", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "woodworking-bench" }), [BlockType.craftingBench]: Object.freeze({ id: BlockType.craftingBench, key: "crafting-bench", displayName: "Crafting bench", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "crafting-bench" }) }); /** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */ export function listPlaceableGrounds () { return Object.values(GROUND_DEFINITIONS); } export function listPlaceableBlocks () { return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden); } /** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */ export function oreBoulderDisplayName (state) { const material = materialFromOreBoulderState(state); if (!material) { return null; } const label = MATERIAL_DISPLAY_NAME[material] ?? material; return material === "coal" ? "Coal boulder" : `${label} ore boulder`; } /** * Hover / UI label for a gatherable resource node. * Ore boulders today; trees / fishing / herbs can extend this later. * Returns null when the block is not an active resource node. */ export function resourceNodeDisplayName (block) { if (!block) { return null; } if (block.typeId === BlockType.boulder) { return oreBoulderDisplayName(block.state); } return null; } /** * Editor palette cards: one entry per placeable, with boulder expanded into * plain rock + each ore/coal variant (shared BlockType, distinct state). */ export function listPlaceableBlockPaletteEntries (catalog = null) { const entries = []; for (const def of listPlaceableBlocks()) { if (def.id === BlockType.boulder) { entries.push({ typeId: def.id, state: "boulder", key: def.key, displayName: "Boulder", spriteName: spriteImageName("block", def.key, "boulder") }); for (const material of ORE_BEARING_MATERIALS) { const state = oreBoulderState(material); entries.push({ typeId: def.id, state, key: def.key, displayName: oreBoulderDisplayName(state) ?? state, spriteName: spriteImageName("block", def.key, state) }); } continue; } const states = catalog?.blocks?.[def.key]?.states ?? []; const state = resolveState(states, null); entries.push({ typeId: def.id, state, key: def.key, displayName: def.displayName, spriteName: state ? spriteImageName("block", def.key, state) : spriteImageName("block", def.key, def.key) }); } return entries; } export function groundDefinition (typeId) { return GROUND_DEFINITIONS[typeId] ?? null; } export function blockDefinition (typeId) { return BLOCK_DEFINITIONS[typeId] ?? null; } export function isGroundPassable (typeId) { const def = groundDefinition(typeId); if (!def) { return true; } return def.passable !== false; } export function isWaterGround (typeId) { return !!groundDefinition(typeId)?.water; } export function isWadeGround (typeId) { return !!groundDefinition(typeId)?.wades; } export function isBlockPassable (typeId) { if (typeId == null) { return true; } const def = blockDefinition(typeId); return def ? def.passable : false; } export function blockLightRange (typeId) { const def = blockDefinition(typeId); return def ? def.lightRangeTiles : 0; } export function normalizeGround (value, fallbackTypeId = GroundType.grass) { if (typeof value === "number") { return { typeId: value, state: null }; } if (value && Number.isInteger(value.typeId)) { return { typeId: value.typeId, state: value.state ?? null }; } return { typeId: fallbackTypeId, state: null }; } export function normalizeBlock (value) { if (!value || !Number.isInteger(value.typeId)) { return null; } return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null }; } export function setBlockVisualState (block, state) { if (!block) { return null; } block.state = state ?? null; return block; }