import { ORE_BEARING_MATERIALS, oreBoulderState, materialFromOreBoulderState, MATERIAL_DISPLAY_NAME, WoodTier, WOOD_TIER_ORDER } from "./materialTiers.js"; import { spriteImageName, resolveState } from "./tileSprites.js"; import { isSeasonalStumpState, SeasonId } from "../world/season.js"; import { composeNeighborConnectState, definitionUsesMaterialNeighborConnect } from "../world/neighborConnect.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, pineTree: 12, marketplace: 13, table: 14 }); 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, /** Outdoor grounds should expose summer/fall/winter (legacy `grass` ok for summer). */ outdoor: true }), [GroundType.wood]: Object.freeze({ id: GroundType.wood, key: "wood", displayName: "Wood", passable: true, wades: false, water: false, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false }), [GroundType.water]: Object.freeze({ id: GroundType.water, key: "water", displayName: "Water", passable: true, wades: true, water: true, frames: 4, framesPerRow: 4, animationSpeed: 0.12, outdoor: true }), [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, outdoor: true }) }); 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, /** Building walls count as indoor (no seasonal art required). */ outdoor: false }), [BlockType.torch]: Object.freeze({ id: BlockType.torch, key: "torch", displayName: "Torch", passable: true, lightRangeTiles: 8, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false }), [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, outdoor: false }), [BlockType.bank]: Object.freeze({ id: BlockType.bank, key: "bank", displayName: "Bank", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false }), [BlockType.churchAltar]: Object.freeze({ id: BlockType.churchAltar, key: "church-altar", displayName: "Church altar", passable: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false }), [BlockType.cauldron]: Object.freeze({ id: BlockType.cauldron, key: "cauldron", displayName: "Cauldron", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false }), [BlockType.boulder]: Object.freeze({ id: BlockType.boulder, key: "boulder", displayName: "Boulder", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */ gatherable: true, outdoor: true }), [BlockType.furnace]: Object.freeze({ id: BlockType.furnace, key: "furnace", displayName: "Furnace", passable: false, blocksLight: false, lightRangeTiles: 4, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "furnace", outdoor: false }), [BlockType.anvil]: Object.freeze({ id: BlockType.anvil, key: "anvil", displayName: "Anvil", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "anvil", outdoor: false }), [BlockType.woodworkingBench]: Object.freeze({ id: BlockType.woodworkingBench, key: "woodworking-bench", displayName: "Woodworking bench", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "woodworking-bench", outdoor: false }), [BlockType.craftingBench]: Object.freeze({ id: BlockType.craftingBench, key: "crafting-bench", displayName: "Crafting bench", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, craftingStationId: "crafting-bench", outdoor: false }), [BlockType.pineTree]: Object.freeze({ id: BlockType.pineTree, key: "pine-tree", displayName: "Pine tree", passable: false, /** Canopy: light passes but each tree tile adds +1 falloff distance. */ blocksLight: "partial", lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: true, gatherable: true, /** Wood tier yielded when chopped (axe). */ woodTier: WoodTier.pine }), [BlockType.marketplace]: Object.freeze({ id: BlockType.marketplace, key: "marketplace", displayName: "Central Exchange counter", passable: false, /** Furniture: blocks movement but not baked/dynamic light rays. */ blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false, /** Autotile via N/E/S/W bitmask states `"0"`…`"15"`. */ neighborConnect: true }), [BlockType.table]: Object.freeze({ id: BlockType.table, key: "table", displayName: "Table", passable: false, blocksLight: false, lightRangeTiles: 0, frames: 1, framesPerRow: 1, animationSpeed: 0, outdoor: false, /** Magenta base `0`–`15` (strip-union line tables); catalog expands `${wood}-${mask}`. * Filled-rectangle corners can later add diagonal-aware sprites without replacing these. */ neighborConnect: true, neighborConnectByMaterial: true, defaultNeighborMaterial: WoodTier.pine }) }); /** 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 and chopable 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); } if (isChopableTreeBlock(block)) { const def = BLOCK_DEFINITIONS[block.typeId]; return def?.displayName ?? "Tree"; } return null; } /** Wood tier for a tree block type, or null. */ export function woodTierForTreeBlock (typeId) { const def = BLOCK_DEFINITIONS[typeId]; return def?.woodTier ?? null; } /** True when this block is a standing (non-stump) gatherable tree. */ export function isChopableTreeBlock (block) { if (!block || woodTierForTreeBlock(block.typeId) == null) { return false; } return !isSeasonalStumpState(block.state); } /** * 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; } if (def.neighborConnect) { if (definitionUsesMaterialNeighborConnect(def)) { for (const material of WOOD_TIER_ORDER) { const state = composeNeighborConnectState(def, material, 0); const label = MATERIAL_DISPLAY_NAME[material] ?? material; entries.push({ typeId: def.id, state, key: def.key, displayName: `${label} table`, spriteName: spriteImageName("block", def.key, state) }); } continue; } entries.push({ typeId: def.id, state: "0", key: def.key, displayName: def.displayName, spriteName: spriteImageName("block", def.key, "0") }); 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; } /** * Liquid water for foam / wade (shallow water freezes in winter and is solid ice). * Deep water stays liquid year-round. */ export function isLiquidWaterGround (typeId, seasonId = null) { if (!isWaterGround(typeId)) { return false; } if (typeId === GroundType.water && seasonId === SeasonId.winter) { return false; } return true; } /** Wade clip/foam only on liquid shallow water (not winter ice). */ export function isWadeGroundInSeason (typeId, seasonId = null) { return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId); } export function isBlockPassable (typeId) { if (typeId == null) { return true; } const def = blockDefinition(typeId); return def ? def.passable : false; } /** * Light ray occlusion for a block tile. * - `clear` (0): no effect (default for passable; set `blocksLight: false` for furniture) * - `partial` (1): ray continues; each such tile adds +1 to falloff distance (2× cost) * - `solid` (2): ray stops (default for impassable; set `blocksLight: true` explicitly) */ export const LightOcclusion = Object.freeze({ clear: 0, partial: 1, solid: 2 }); /** * Resolve light occlusion for a block tile (type + visual state). * Stumps are a runtime/editor state override: no occlusion even on tree types. */ export function blockLightOcclusionForBlock (block) { if (!block) { return LightOcclusion.clear; } if (isSeasonalStumpState(block.state)) { return LightOcclusion.clear; } return blockLightOcclusion(block.typeId ?? null); } /** * Resolve light occlusion for a block type id. * `blocksLight`: `true` | `false` | `"partial"`; omitted → solid iff `!passable`. * Prefer {@link blockLightOcclusionForBlock} when block state is known. */ export function blockLightOcclusion (typeId) { if (typeId == null) { return LightOcclusion.clear; } const def = blockDefinition(typeId); if (!def) { return LightOcclusion.solid; } if (def.blocksLight === "partial") { return LightOcclusion.partial; } if (def.blocksLight === false) { return LightOcclusion.clear; } if (def.blocksLight === true) { return LightOcclusion.solid; } return def.passable === false ? LightOcclusion.solid : LightOcclusion.clear; } /** True when a block fully stops light rays (not partial canopy). */ export function isBlockOccludingLight (typeId) { return blockLightOcclusion(typeId) === LightOcclusion.solid; } 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; }