/** * Item Spawner — authored world-editor spawner that creates ground items or * openable wooden containers (chest / barrel / crate) with optional conditions. */ import { WOOD_TIER_ORDER, WoodTier, MATERIAL_DISPLAY_NAME } from "./materialTiers.js"; import { itemDefinition } from "./items.js"; import { normalizeConditionSet, emptyConditionSet } from "../quests/conditions.js"; import { spriteImageName } from "./tileSprites.js"; export const ItemSpawnerKind = "item"; export const ItemSpawnerAppearance = Object.freeze({ item: "item", chest: "chest", barrel: "barrel", crate: "crate" }); export const ITEM_SPAWNER_APPEARANCES = Object.freeze([ ItemSpawnerAppearance.item, ItemSpawnerAppearance.chest, ItemSpawnerAppearance.barrel, ItemSpawnerAppearance.crate ]); export const ITEM_SPAWNER_CONTAINER_APPEARANCES = Object.freeze([ ItemSpawnerAppearance.chest, ItemSpawnerAppearance.barrel, ItemSpawnerAppearance.crate ]); /** Fail toast when container ConditionSet fails (British English). */ export const ITEM_SPAWNER_LOCKED_MESSAGE = "The container is locked."; /** Partial-loot toast when inventory cannot hold everything. */ export const ITEM_SPAWNER_SPACE_TOAST = "Not enough space -- some items were dropped!"; export const ITEM_SPAWNER_OPEN_DURATION_MS = 2000; export const ITEM_SPAWNER_SHOVE_MS = 1000; /** Magenta container art folder under assets/blocks/. */ export const ITEM_SPAWNER_CONTAINER_ASSET_KEY = "containers"; export function isItemSpawnerConfig (config) { if (!config || typeof config !== "object") { return false; } if (config.spawnerKind === ItemSpawnerKind) { return true; } // Legacy / explicit sentinel without mobTypeId. if (config.kind === ItemSpawnerKind) { return true; } return false; } export function isItemSpawnerContainerAppearance (appearance) { return ITEM_SPAWNER_CONTAINER_APPEARANCES.includes(appearance); } /** British English display name for Open / context menu. */ export function itemSpawnerContainerDisplayName (appearance) { switch (appearance) { case ItemSpawnerAppearance.barrel: return "Barrel"; case ItemSpawnerAppearance.crate: return "Crate"; case ItemSpawnerAppearance.chest: default: return "Chest"; } } export function itemSpawnerOpenLabel (appearance) { return `Open ${itemSpawnerContainerDisplayName(appearance)}`; } /** * Catalog image name for a wood-tinted container (`block:containers:pine-chest`). */ export function itemSpawnerContainerSpriteName (appearance, material = WoodTier.pine) { const look = isItemSpawnerContainerAppearance(appearance) ? appearance : ItemSpawnerAppearance.chest; const wood = WOOD_TIER_ORDER.includes(material) ? material : WoodTier.pine; return spriteImageName("block", ITEM_SPAWNER_CONTAINER_ASSET_KEY, `${wood}-${look}`); } /** Editor palette / overlay preview — default pine chest. */ export function itemSpawnerPreviewSpriteName () { return itemSpawnerContainerSpriteName(ItemSpawnerAppearance.chest, WoodTier.pine); } function normalizeItemStacks (raw) { if (!Array.isArray(raw)) { return []; } const lines = []; for (const entry of raw) { if (!entry || typeof entry !== "object") { continue; } const itemId = Number(entry.itemId) | 0; if (!Number.isFinite(itemId) || itemId <= 0 || !itemDefinition(itemId)) { continue; } const quantity = Math.max(1, Math.min(10_000, Math.floor(Number(entry.quantity) || 1))); lines.push({ itemId, quantity }); } return lines; } /** Distinct item types/stacks count for Appearance=Item gating. */ export function itemSpawnerStackCount (items) { return Array.isArray(items) ? items.length : 0; } export function defaultItemSpawnerConfig (spawnerId = "item-spawner") { return { spawnerKind: ItemSpawnerKind, spawnerId: String(spawnerId || "item-spawner"), items: [], respawnDelaySeconds: 25, spawnRangeTiles: 0, appearance: ItemSpawnerAppearance.chest, material: WoodTier.pine, when: emptyConditionSet() }; } /** * Clamp / coerce item-spawner fields. Auto-switches Appearance away from Item * when more than one item stack is selected. */ export function normalizeItemSpawnerConfig (config, spawnerId = "item-spawner") { const base = defaultItemSpawnerConfig(spawnerId); const merged = { ...base, ...(config ?? {}) }; const id = String(merged.spawnerId || config?.spawnerId || spawnerId || "item-spawner"); const items = normalizeItemStacks(merged.items); let appearance = String(merged.appearance ?? base.appearance); if (!ITEM_SPAWNER_APPEARANCES.includes(appearance)) { appearance = ItemSpawnerAppearance.chest; } if (appearance === ItemSpawnerAppearance.item && items.length !== 1) { appearance = ItemSpawnerAppearance.chest; } const material = WOOD_TIER_ORDER.includes(merged.material) ? merged.material : WoodTier.pine; const respawnDelaySeconds = Math.max( 0, Math.min( 86_400, Math.floor( Number( merged.respawnDelaySeconds != null && merged.respawnDelaySeconds !== "" ? merged.respawnDelaySeconds : base.respawnDelaySeconds ) || 0 ) ) ); const rawRange = Number(merged.spawnRangeTiles); const spawnRangeTiles = Math.max( 0, Math.min(32, Number.isFinite(rawRange) ? (rawRange | 0) : base.spawnRangeTiles) ); const when = isItemSpawnerContainerAppearance(appearance) ? normalizeConditionSet(merged.when ?? merged.requirements ?? emptyConditionSet()) : emptyConditionSet(); return { spawnerKind: ItemSpawnerKind, spawnerId: id, items, respawnDelaySeconds, spawnRangeTiles, appearance, material, when }; } /** Wood material options for the editor Material dropdown. */ export function listItemSpawnerMaterials () { return WOOD_TIER_ORDER.map((id) => ({ id, displayName: MATERIAL_DISPLAY_NAME[id] ?? id })); }