| 1 | /** |
| 2 | * Item Spawner — authored world-editor spawner that creates ground items or |
| 3 | * openable wooden containers (chest / barrel / crate) with optional conditions. |
| 4 | */ |
| 5 | import { WOOD_TIER_ORDER, WoodTier, MATERIAL_DISPLAY_NAME } from "./materialTiers.js"; |
| 6 | import { itemDefinition } from "./items.js"; |
| 7 | import { normalizeConditionSet, emptyConditionSet } from "../quests/conditions.js"; |
| 8 | import { spriteImageName } from "./tileSprites.js"; |
| 9 | |
| 10 | export const ItemSpawnerKind = "item"; |
| 11 | |
| 12 | export const ItemSpawnerAppearance = Object.freeze({ |
| 13 | item: "item", |
| 14 | chest: "chest", |
| 15 | barrel: "barrel", |
| 16 | crate: "crate" |
| 17 | }); |
| 18 | |
| 19 | export const ITEM_SPAWNER_APPEARANCES = Object.freeze([ |
| 20 | ItemSpawnerAppearance.item, |
| 21 | ItemSpawnerAppearance.chest, |
| 22 | ItemSpawnerAppearance.barrel, |
| 23 | ItemSpawnerAppearance.crate |
| 24 | ]); |
| 25 | |
| 26 | export const ITEM_SPAWNER_CONTAINER_APPEARANCES = Object.freeze([ |
| 27 | ItemSpawnerAppearance.chest, |
| 28 | ItemSpawnerAppearance.barrel, |
| 29 | ItemSpawnerAppearance.crate |
| 30 | ]); |
| 31 | |
| 32 | /** Fail toast when container ConditionSet fails (British English). */ |
| 33 | export const ITEM_SPAWNER_LOCKED_MESSAGE = "The container is locked."; |
| 34 | |
| 35 | /** Partial-loot toast when inventory cannot hold everything. */ |
| 36 | export const ITEM_SPAWNER_SPACE_TOAST = "Not enough space -- some items were dropped!"; |
| 37 | |
| 38 | export const ITEM_SPAWNER_OPEN_DURATION_MS = 2000; |
| 39 | export const ITEM_SPAWNER_SHOVE_MS = 1000; |
| 40 | |
| 41 | /** Magenta container art folder under assets/blocks/. */ |
| 42 | export const ITEM_SPAWNER_CONTAINER_ASSET_KEY = "containers"; |
| 43 | |
| 44 | export function isItemSpawnerConfig (config) |
| 45 | { |
| 46 | if (!config || typeof config !== "object") |
| 47 | { return false; } |
| 48 | if (config.spawnerKind === ItemSpawnerKind) |
| 49 | { return true; } |
| 50 | // Legacy / explicit sentinel without mobTypeId. |
| 51 | if (config.kind === ItemSpawnerKind) |
| 52 | { return true; } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | export function isItemSpawnerContainerAppearance (appearance) |
| 57 | { |
| 58 | return ITEM_SPAWNER_CONTAINER_APPEARANCES.includes(appearance); |
| 59 | } |
| 60 | |
| 61 | /** British English display name for Open / context menu. */ |
| 62 | export function itemSpawnerContainerDisplayName (appearance) |
| 63 | { |
| 64 | switch (appearance) |
| 65 | { |
| 66 | case ItemSpawnerAppearance.barrel: |
| 67 | return "Barrel"; |
| 68 | case ItemSpawnerAppearance.crate: |
| 69 | return "Crate"; |
| 70 | case ItemSpawnerAppearance.chest: |
| 71 | default: |
| 72 | return "Chest"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export function itemSpawnerOpenLabel (appearance) |
| 77 | { |
| 78 | return `Open ${itemSpawnerContainerDisplayName(appearance)}`; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Catalog image name for a wood-tinted container (`block:containers:pine-chest`). |
| 83 | */ |
| 84 | export function itemSpawnerContainerSpriteName (appearance, material = WoodTier.pine) |
| 85 | { |
| 86 | const look = isItemSpawnerContainerAppearance(appearance) |
| 87 | ? appearance |
| 88 | : ItemSpawnerAppearance.chest; |
| 89 | const wood = WOOD_TIER_ORDER.includes(material) ? material : WoodTier.pine; |
| 90 | return spriteImageName("block", ITEM_SPAWNER_CONTAINER_ASSET_KEY, `${wood}-${look}`); |
| 91 | } |
| 92 | |
| 93 | /** Editor palette / overlay preview — default pine chest. */ |
| 94 | export function itemSpawnerPreviewSpriteName () |
| 95 | { |
| 96 | return itemSpawnerContainerSpriteName(ItemSpawnerAppearance.chest, WoodTier.pine); |
| 97 | } |
| 98 | |
| 99 | function normalizeItemStacks (raw) |
| 100 | { |
| 101 | if (!Array.isArray(raw)) |
| 102 | { return []; } |
| 103 | const lines = []; |
| 104 | for (const entry of raw) |
| 105 | { |
| 106 | if (!entry || typeof entry !== "object") |
| 107 | { continue; } |
| 108 | const itemId = Number(entry.itemId) | 0; |
| 109 | if (!Number.isFinite(itemId) || itemId <= 0 || !itemDefinition(itemId)) |
| 110 | { continue; } |
| 111 | const quantity = Math.max(1, Math.min(10_000, Math.floor(Number(entry.quantity) || 1))); |
| 112 | lines.push({ itemId, quantity }); |
| 113 | } |
| 114 | return lines; |
| 115 | } |
| 116 | |
| 117 | /** Distinct item types/stacks count for Appearance=Item gating. */ |
| 118 | export function itemSpawnerStackCount (items) |
| 119 | { |
| 120 | return Array.isArray(items) ? items.length : 0; |
| 121 | } |
| 122 | |
| 123 | export function defaultItemSpawnerConfig (spawnerId = "item-spawner") |
| 124 | { |
| 125 | return { |
| 126 | spawnerKind: ItemSpawnerKind, |
| 127 | spawnerId: String(spawnerId || "item-spawner"), |
| 128 | items: [], |
| 129 | respawnDelaySeconds: 25, |
| 130 | spawnRangeTiles: 0, |
| 131 | appearance: ItemSpawnerAppearance.chest, |
| 132 | material: WoodTier.pine, |
| 133 | when: emptyConditionSet() |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Clamp / coerce item-spawner fields. Auto-switches Appearance away from Item |
| 139 | * when more than one item stack is selected. |
| 140 | */ |
| 141 | export function normalizeItemSpawnerConfig (config, spawnerId = "item-spawner") |
| 142 | { |
| 143 | const base = defaultItemSpawnerConfig(spawnerId); |
| 144 | const merged = { ...base, ...(config ?? {}) }; |
| 145 | const id = String(merged.spawnerId || config?.spawnerId || spawnerId || "item-spawner"); |
| 146 | const items = normalizeItemStacks(merged.items); |
| 147 | let appearance = String(merged.appearance ?? base.appearance); |
| 148 | if (!ITEM_SPAWNER_APPEARANCES.includes(appearance)) |
| 149 | { appearance = ItemSpawnerAppearance.chest; } |
| 150 | if (appearance === ItemSpawnerAppearance.item && items.length !== 1) |
| 151 | { appearance = ItemSpawnerAppearance.chest; } |
| 152 | const material = WOOD_TIER_ORDER.includes(merged.material) |
| 153 | ? merged.material |
| 154 | : WoodTier.pine; |
| 155 | const respawnDelaySeconds = Math.max( |
| 156 | 0, |
| 157 | Math.min( |
| 158 | 86_400, |
| 159 | Math.floor( |
| 160 | Number( |
| 161 | merged.respawnDelaySeconds != null && merged.respawnDelaySeconds !== "" |
| 162 | ? merged.respawnDelaySeconds |
| 163 | : base.respawnDelaySeconds |
| 164 | ) || 0 |
| 165 | ) |
| 166 | ) |
| 167 | ); |
| 168 | const rawRange = Number(merged.spawnRangeTiles); |
| 169 | const spawnRangeTiles = Math.max( |
| 170 | 0, |
| 171 | Math.min(32, Number.isFinite(rawRange) ? (rawRange | 0) : base.spawnRangeTiles) |
| 172 | ); |
| 173 | const when = isItemSpawnerContainerAppearance(appearance) |
| 174 | ? normalizeConditionSet(merged.when ?? merged.requirements ?? emptyConditionSet()) |
| 175 | : emptyConditionSet(); |
| 176 | return { |
| 177 | spawnerKind: ItemSpawnerKind, |
| 178 | spawnerId: id, |
| 179 | items, |
| 180 | respawnDelaySeconds, |
| 181 | spawnRangeTiles, |
| 182 | appearance, |
| 183 | material, |
| 184 | when |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | /** Wood material options for the editor Material dropdown. */ |
| 189 | export function listItemSpawnerMaterials () |
| 190 | { |
| 191 | return WOOD_TIER_ORDER.map((id) => ({ |
| 192 | id, |
| 193 | displayName: MATERIAL_DISPLAY_NAME[id] ?? id |
| 194 | })); |
| 195 | } |