| 1 | /** |
| 2 | * Right-click config modal for editor tiles that have configurable data (spawners). |
| 3 | */ |
| 4 | import { |
| 5 | listSpawnableMobTypes, |
| 6 | normalizeSpawnerConfig, |
| 7 | MobId, |
| 8 | isHumanoidMob |
| 9 | } from "/shared/definitions/mobs.js"; |
| 10 | import { ItemId, itemDefinition, EQUIPMENT_SLOT } from "/shared/definitions/items.js"; |
| 11 | import { EQUIPMENT_SLOTS } from "/shared/items/inventory.js"; |
| 12 | |
| 13 | const SHOP_STOCK_PRESETS = Object.freeze([ |
| 14 | { itemId: ItemId.copperOre, label: "Copper ore" }, |
| 15 | { itemId: ItemId.tinOre, label: "Tin ore" }, |
| 16 | { itemId: ItemId.coal, label: "Coal" }, |
| 17 | { itemId: ItemId.ironOre, label: "Iron ore" }, |
| 18 | { itemId: ItemId.pineLog, label: "Pine log" }, |
| 19 | { itemId: ItemId.birchLog, label: "Birch log" }, |
| 20 | { itemId: ItemId.oakLog, label: "Oak log" } |
| 21 | ]); |
| 22 | |
| 23 | const EQUIP_PRESETS = Object.freeze([ |
| 24 | { itemId: ItemId.clothShirt, slot: EQUIPMENT_SLOT.chest }, |
| 25 | { itemId: ItemId.stick, slot: EQUIPMENT_SLOT.weapon }, |
| 26 | { itemId: ItemId.bronzePickaxe, slot: EQUIPMENT_SLOT.weapon }, |
| 27 | { itemId: ItemId.bronzeAxe, slot: EQUIPMENT_SLOT.weapon }, |
| 28 | { itemId: ItemId.lantern, slot: EQUIPMENT_SLOT.offhand } |
| 29 | ]); |
| 30 | |
| 31 | export function initTileConfigModal ({ onSave, onRemove }) |
| 32 | { |
| 33 | const backdrop = document.getElementById("tile-config-backdrop"); |
| 34 | const modal = document.getElementById("tile-config-modal"); |
| 35 | const form = document.getElementById("tile-config-form"); |
| 36 | const title = document.getElementById("tile-config-title"); |
| 37 | let open = false; |
| 38 | let context = null; |
| 39 | |
| 40 | function show (visible) |
| 41 | { |
| 42 | open = visible; |
| 43 | backdrop.hidden = !visible; |
| 44 | modal.hidden = !visible; |
| 45 | if (!visible) |
| 46 | { context = null; } |
| 47 | } |
| 48 | |
| 49 | function renderHumanoidFields (config) |
| 50 | { |
| 51 | const wrap = document.createElement("div"); |
| 52 | wrap.className = "spawner-humanoid-fields"; |
| 53 | wrap.dataset.humanoid = "1"; |
| 54 | wrap.append( |
| 55 | field("Display name", textInput("displayName", config.displayName || "Shopkeeper")), |
| 56 | field("Sex", sexSelect("sex", config.sex)) |
| 57 | ); |
| 58 | for (const slot of EQUIPMENT_SLOTS) |
| 59 | { |
| 60 | wrap.append(field( |
| 61 | `Equip ${slot}`, |
| 62 | equipSelect(`equipment.${slot}`, slot, config.equipment?.[slot] ?? null) |
| 63 | )); |
| 64 | } |
| 65 | wrap.append(field("Hidden discount (0–1)", numberInput("hiddenDiscount", config.hiddenDiscount ?? 0.9, 0.01, 1, 0.01))); |
| 66 | const stockLabel = document.createElement("div"); |
| 67 | stockLabel.textContent = "Sells"; |
| 68 | stockLabel.style.marginTop = "0.5rem"; |
| 69 | wrap.append(stockLabel); |
| 70 | for (const preset of SHOP_STOCK_PRESETS) |
| 71 | { |
| 72 | const row = document.createElement("label"); |
| 73 | row.style.display = "flex"; |
| 74 | row.style.gap = "0.4rem"; |
| 75 | row.style.alignItems = "center"; |
| 76 | const box = document.createElement("input"); |
| 77 | box.type = "checkbox"; |
| 78 | box.name = "sells"; |
| 79 | box.value = String(preset.itemId); |
| 80 | box.checked = (config.sells ?? []).includes(preset.itemId); |
| 81 | row.append(box, document.createTextNode(preset.label)); |
| 82 | wrap.append(row); |
| 83 | } |
| 84 | return wrap; |
| 85 | } |
| 86 | |
| 87 | function syncHumanoidSection (config) |
| 88 | { |
| 89 | form.querySelector("[data-humanoid]")?.remove(); |
| 90 | const mobTypeId = form.querySelector('[name="mobTypeId"]')?.value; |
| 91 | if (!isHumanoidMob(mobTypeId)) |
| 92 | { return; } |
| 93 | form.append(renderHumanoidFields(config ?? normalizeSpawnerConfig({ |
| 94 | mobTypeId: MobId.humanoid |
| 95 | }, "spawner"))); |
| 96 | } |
| 97 | |
| 98 | function openSpawner (ctx) |
| 99 | { |
| 100 | context = { kind: "spawner", ...ctx }; |
| 101 | const config = normalizeSpawnerConfig(ctx.config, ctx.config.spawnerId); |
| 102 | title.textContent = `Spawner · ${ctx.tileX}, ${ctx.tileY}`; |
| 103 | form.innerHTML = ""; |
| 104 | const mobField = field("Mob type", mobSelect("mobTypeId", config.mobTypeId)); |
| 105 | form.append( |
| 106 | mobField, |
| 107 | field("Max alive", numberInput("maxAlive", config.maxAlive, 1, 20)), |
| 108 | field("Spawn range", numberInput("spawnRangeTiles", config.spawnRangeTiles, 1, 32)), |
| 109 | field("Max wander range", numberInput("maxWanderRangeTiles", config.maxWanderRangeTiles, 1, 64)), |
| 110 | field("Spawner id", textInput("spawnerId", config.spawnerId, true)) |
| 111 | ); |
| 112 | const spawnInput = form.querySelector('[name="spawnRangeTiles"]'); |
| 113 | const wanderInput = form.querySelector('[name="maxWanderRangeTiles"]'); |
| 114 | const syncWanderMin = () => |
| 115 | { |
| 116 | const spawn = Number(spawnInput.value) || 1; |
| 117 | wanderInput.min = String(spawn); |
| 118 | if (Number(wanderInput.value) < spawn) |
| 119 | { wanderInput.value = String(spawn); } |
| 120 | }; |
| 121 | spawnInput.addEventListener("input", syncWanderMin); |
| 122 | syncWanderMin(); |
| 123 | form.querySelector('[name="mobTypeId"]').addEventListener("change", () => |
| 124 | { |
| 125 | const nextType = form.querySelector('[name="mobTypeId"]').value; |
| 126 | const defaults = normalizeSpawnerConfig({ mobTypeId: nextType }, config.spawnerId); |
| 127 | form.querySelector('[name="maxAlive"]').value = String(defaults.maxAlive); |
| 128 | form.querySelector('[name="spawnRangeTiles"]').value = String(defaults.spawnRangeTiles); |
| 129 | form.querySelector('[name="maxWanderRangeTiles"]').value = String(defaults.maxWanderRangeTiles); |
| 130 | syncWanderMin(); |
| 131 | syncHumanoidSection(defaults); |
| 132 | }); |
| 133 | syncHumanoidSection(config); |
| 134 | show(true); |
| 135 | } |
| 136 | |
| 137 | document.getElementById("tile-config-close").addEventListener("click", () => show(false)); |
| 138 | document.getElementById("tile-config-cancel").addEventListener("click", () => show(false)); |
| 139 | backdrop.addEventListener("click", () => show(false)); |
| 140 | document.getElementById("tile-config-save").addEventListener("click", () => |
| 141 | { |
| 142 | if (!context) |
| 143 | { return; } |
| 144 | const data = new FormData(form); |
| 145 | const sells = data.getAll("sells").map((value) => Number(value)).filter((id) => id > 0); |
| 146 | const equipment = {}; |
| 147 | for (const slot of EQUIPMENT_SLOTS) |
| 148 | { |
| 149 | const raw = data.get(`equipment.${slot}`); |
| 150 | equipment[slot] = raw ? Number(raw) : null; |
| 151 | } |
| 152 | onSave?.(context, normalizeSpawnerConfig({ |
| 153 | mobTypeId: String(data.get("mobTypeId") || "deer"), |
| 154 | maxAlive: Number(data.get("maxAlive") || 3), |
| 155 | spawnRangeTiles: Number(data.get("spawnRangeTiles") || 4), |
| 156 | maxWanderRangeTiles: Number(data.get("maxWanderRangeTiles") || 4), |
| 157 | spawnerId: String(data.get("spawnerId") || context.config.spawnerId), |
| 158 | sex: String(data.get("sex") || "male"), |
| 159 | displayName: String(data.get("displayName") || ""), |
| 160 | equipment, |
| 161 | sells, |
| 162 | hiddenDiscount: Number(data.get("hiddenDiscount") || 0.9) |
| 163 | }, context.config.spawnerId)); |
| 164 | show(false); |
| 165 | }); |
| 166 | document.getElementById("tile-config-remove").addEventListener("click", () => |
| 167 | { |
| 168 | if (!context) |
| 169 | { return; } |
| 170 | onRemove?.(context); |
| 171 | show(false); |
| 172 | }); |
| 173 | |
| 174 | return { |
| 175 | isOpen: () => open, |
| 176 | openSpawner, |
| 177 | close: () => show(false) |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | function field (label, input) |
| 182 | { |
| 183 | const wrap = document.createElement("label"); |
| 184 | wrap.append(document.createTextNode(label), input); |
| 185 | return wrap; |
| 186 | } |
| 187 | |
| 188 | function numberInput (name, value, min, max, step = 1) |
| 189 | { |
| 190 | const input = document.createElement("input"); |
| 191 | input.type = "number"; |
| 192 | input.name = name; |
| 193 | input.min = String(min); |
| 194 | input.max = String(max); |
| 195 | input.step = String(step); |
| 196 | input.value = String(value); |
| 197 | return input; |
| 198 | } |
| 199 | |
| 200 | function textInput (name, value, readOnly = false) |
| 201 | { |
| 202 | const input = document.createElement("input"); |
| 203 | input.type = "text"; |
| 204 | input.name = name; |
| 205 | input.value = String(value ?? ""); |
| 206 | input.readOnly = readOnly; |
| 207 | return input; |
| 208 | } |
| 209 | |
| 210 | function sexSelect (name, value) |
| 211 | { |
| 212 | const select = document.createElement("select"); |
| 213 | select.name = name; |
| 214 | for (const sex of ["male", "female"]) |
| 215 | { |
| 216 | const option = document.createElement("option"); |
| 217 | option.value = sex; |
| 218 | option.textContent = sex; |
| 219 | if (sex === value) |
| 220 | { option.selected = true; } |
| 221 | select.append(option); |
| 222 | } |
| 223 | return select; |
| 224 | } |
| 225 | |
| 226 | function equipSelect (name, slot, value) |
| 227 | { |
| 228 | const select = document.createElement("select"); |
| 229 | select.name = name; |
| 230 | const none = document.createElement("option"); |
| 231 | none.value = ""; |
| 232 | none.textContent = "(none)"; |
| 233 | select.append(none); |
| 234 | for (const preset of EQUIP_PRESETS) |
| 235 | { |
| 236 | if (preset.slot !== slot) |
| 237 | { continue; } |
| 238 | const def = itemDefinition(preset.itemId); |
| 239 | const option = document.createElement("option"); |
| 240 | option.value = String(preset.itemId); |
| 241 | option.textContent = def?.displayName ?? String(preset.itemId); |
| 242 | if (preset.itemId === value) |
| 243 | { option.selected = true; } |
| 244 | select.append(option); |
| 245 | } |
| 246 | return select; |
| 247 | } |
| 248 | |
| 249 | function mobSelect (name, value) |
| 250 | { |
| 251 | const select = document.createElement("select"); |
| 252 | select.name = name; |
| 253 | for (const def of listSpawnableMobTypes()) |
| 254 | { |
| 255 | const option = document.createElement("option"); |
| 256 | option.value = def.mobTypeId; |
| 257 | option.textContent = def.displayName; |
| 258 | if (def.mobTypeId === value) |
| 259 | { option.selected = true; } |
| 260 | select.append(option); |
| 261 | } |
| 262 | return select; |
| 263 | } |