| 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 | import { listNpcShopSellableItems } from "/shared/shop/pricing.js"; |
| 13 | |
| 14 | const EQUIP_PRESETS = Object.freeze([ |
| 15 | { itemId: ItemId.clothShirt, slot: EQUIPMENT_SLOT.chest }, |
| 16 | { itemId: ItemId.stick, slot: EQUIPMENT_SLOT.weapon }, |
| 17 | { itemId: ItemId.bronzePickaxe, slot: EQUIPMENT_SLOT.weapon }, |
| 18 | { itemId: ItemId.bronzeAxe, slot: EQUIPMENT_SLOT.weapon }, |
| 19 | { itemId: ItemId.lantern, slot: EQUIPMENT_SLOT.offhand } |
| 20 | ]); |
| 21 | |
| 22 | export function initTileConfigModal ({ onSave, onRemove }) |
| 23 | { |
| 24 | const backdrop = document.getElementById("tile-config-backdrop"); |
| 25 | const modal = document.getElementById("tile-config-modal"); |
| 26 | const form = document.getElementById("tile-config-form"); |
| 27 | const title = document.getElementById("tile-config-title"); |
| 28 | const itemSearchBackdrop = document.getElementById("shop-item-search-backdrop"); |
| 29 | const itemSearchModal = document.getElementById("shop-item-search-modal"); |
| 30 | const itemSearchInput = document.getElementById("shop-item-search-input"); |
| 31 | const itemSearchResults = document.getElementById("shop-item-search-results"); |
| 32 | let open = false; |
| 33 | let context = null; |
| 34 | /** @type {{ itemId: number, quantity: number }[]} */ |
| 35 | let sellsDraft = []; |
| 36 | |
| 37 | function show (visible) |
| 38 | { |
| 39 | open = visible; |
| 40 | backdrop.hidden = !visible; |
| 41 | modal.hidden = !visible; |
| 42 | if (!visible) |
| 43 | { |
| 44 | context = null; |
| 45 | closeItemSearch(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function closeItemSearch () |
| 50 | { |
| 51 | if (itemSearchBackdrop) |
| 52 | { itemSearchBackdrop.hidden = true; } |
| 53 | if (itemSearchModal) |
| 54 | { itemSearchModal.hidden = true; } |
| 55 | } |
| 56 | |
| 57 | function openItemSearch () |
| 58 | { |
| 59 | if (!itemSearchBackdrop || !itemSearchModal || !itemSearchInput) |
| 60 | { return; } |
| 61 | itemSearchInput.value = ""; |
| 62 | renderItemSearchResults(""); |
| 63 | itemSearchBackdrop.hidden = false; |
| 64 | itemSearchModal.hidden = false; |
| 65 | itemSearchInput.focus(); |
| 66 | } |
| 67 | |
| 68 | function renderItemSearchResults (query) |
| 69 | { |
| 70 | if (!itemSearchResults) |
| 71 | { return; } |
| 72 | itemSearchResults.innerHTML = ""; |
| 73 | const q = String(query ?? "").trim().toLowerCase(); |
| 74 | const taken = new Set(sellsDraft.map((line) => line.itemId)); |
| 75 | const matches = listNpcShopSellableItems().filter((item) => |
| 76 | { |
| 77 | if (taken.has(item.itemId)) |
| 78 | { return false; } |
| 79 | if (!q) |
| 80 | { return true; } |
| 81 | return item.displayName.toLowerCase().includes(q) || item.key.toLowerCase().includes(q); |
| 82 | }).slice(0, 80); |
| 83 | if (!matches.length) |
| 84 | { |
| 85 | itemSearchResults.textContent = "No matching items."; |
| 86 | return; |
| 87 | } |
| 88 | for (const item of matches) |
| 89 | { |
| 90 | const row = document.createElement("button"); |
| 91 | row.type = "button"; |
| 92 | row.className = "shop-item-search-row"; |
| 93 | row.textContent = item.displayName; |
| 94 | row.addEventListener("click", () => |
| 95 | { |
| 96 | sellsDraft.push({ itemId: item.itemId, quantity: 0 }); |
| 97 | renderSellsList(); |
| 98 | closeItemSearch(); |
| 99 | }); |
| 100 | itemSearchResults.append(row); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function renderSellsList () |
| 105 | { |
| 106 | const list = form.querySelector("[data-sells-list]"); |
| 107 | if (!list) |
| 108 | { return; } |
| 109 | list.innerHTML = ""; |
| 110 | if (!sellsDraft.length) |
| 111 | { |
| 112 | const empty = document.createElement("p"); |
| 113 | empty.className = "hint"; |
| 114 | empty.textContent = "No items yet. Use + to add stock."; |
| 115 | list.append(empty); |
| 116 | return; |
| 117 | } |
| 118 | for (let index = 0; index < sellsDraft.length; index++) |
| 119 | { |
| 120 | const line = sellsDraft[index]; |
| 121 | const def = itemDefinition(line.itemId); |
| 122 | const row = document.createElement("div"); |
| 123 | row.className = "spawner-sells-row"; |
| 124 | const name = document.createElement("span"); |
| 125 | name.className = "spawner-sells-name"; |
| 126 | name.textContent = def?.displayName ?? `Item ${line.itemId}`; |
| 127 | const qty = document.createElement("input"); |
| 128 | qty.type = "number"; |
| 129 | qty.min = "0"; |
| 130 | qty.step = "1"; |
| 131 | qty.value = String(line.quantity | 0); |
| 132 | qty.title = "0 = infinite; otherwise restocks this many at 00:00 UTC"; |
| 133 | qty.addEventListener("input", () => |
| 134 | { |
| 135 | sellsDraft[index].quantity = Math.max(0, Number(qty.value) || 0) | 0; |
| 136 | }); |
| 137 | const remove = document.createElement("button"); |
| 138 | remove.type = "button"; |
| 139 | remove.textContent = "Remove"; |
| 140 | remove.addEventListener("click", () => |
| 141 | { |
| 142 | sellsDraft.splice(index, 1); |
| 143 | renderSellsList(); |
| 144 | }); |
| 145 | row.append(name, qty, remove); |
| 146 | list.append(row); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function renderHumanoidFields (config) |
| 151 | { |
| 152 | const wrap = document.createElement("div"); |
| 153 | wrap.className = "spawner-humanoid-fields"; |
| 154 | wrap.dataset.humanoid = "1"; |
| 155 | wrap.append( |
| 156 | field("Display name", textInput("displayName", config.displayName || "Shopkeeper")), |
| 157 | field("Sex", sexSelect("sex", config.sex)) |
| 158 | ); |
| 159 | for (const slot of EQUIPMENT_SLOTS) |
| 160 | { |
| 161 | wrap.append(field( |
| 162 | `Equip ${slot}`, |
| 163 | equipSelect(`equipment.${slot}`, slot, config.equipment?.[slot] ?? null) |
| 164 | )); |
| 165 | } |
| 166 | wrap.append(field("Hidden discount (0–1)", numberInput("hiddenDiscount", config.hiddenDiscount ?? 0.9, 0.01, 1, 0.01))); |
| 167 | |
| 168 | const sellsHeader = document.createElement("div"); |
| 169 | sellsHeader.className = "spawner-sells-header"; |
| 170 | const sellsTitle = document.createElement("strong"); |
| 171 | sellsTitle.textContent = "Sells"; |
| 172 | const addBtn = document.createElement("button"); |
| 173 | addBtn.type = "button"; |
| 174 | addBtn.textContent = "+"; |
| 175 | addBtn.title = "Add item"; |
| 176 | addBtn.addEventListener("click", () => openItemSearch()); |
| 177 | sellsHeader.append(sellsTitle, addBtn); |
| 178 | wrap.append(sellsHeader); |
| 179 | |
| 180 | const list = document.createElement("div"); |
| 181 | list.className = "spawner-sells-list"; |
| 182 | list.dataset.sellsList = "1"; |
| 183 | wrap.append(list); |
| 184 | |
| 185 | const buysLabel = document.createElement("label"); |
| 186 | buysLabel.className = "spawner-buys-row"; |
| 187 | const buys = document.createElement("input"); |
| 188 | buys.type = "checkbox"; |
| 189 | buys.name = "buysItems"; |
| 190 | buys.checked = !!config.buysItems; |
| 191 | buysLabel.append(buys, document.createTextNode(" Buys items")); |
| 192 | wrap.append(buysLabel); |
| 193 | |
| 194 | const hint = document.createElement("p"); |
| 195 | hint.className = "hint"; |
| 196 | hint.textContent = "Quantity 0 = infinite stock. Limited stock restocks daily at 00:00 UTC. Buys items: pays 80% market for transferable bag goods (not blank cheques); filled cheques sell as reserved goods with a 5% admin fee; extras resell at 120% until restock."; |
| 197 | wrap.append(hint); |
| 198 | |
| 199 | sellsDraft = (config.sells ?? []).map((line) => |
| 200 | { |
| 201 | if (line && typeof line === "object") |
| 202 | { return { itemId: line.itemId | 0, quantity: Math.max(0, line.quantity | 0 || 0) }; } |
| 203 | return { itemId: line | 0, quantity: 0 }; |
| 204 | }).filter((line) => line.itemId > 0); |
| 205 | |
| 206 | queueMicrotask(() => renderSellsList()); |
| 207 | return wrap; |
| 208 | } |
| 209 | |
| 210 | function syncHumanoidSection (config) |
| 211 | { |
| 212 | form.querySelector("[data-humanoid]")?.remove(); |
| 213 | const mobTypeId = form.querySelector('[name="mobTypeId"]')?.value; |
| 214 | if (!isHumanoidMob(mobTypeId)) |
| 215 | { |
| 216 | sellsDraft = []; |
| 217 | return; |
| 218 | } |
| 219 | form.append(renderHumanoidFields(config ?? normalizeSpawnerConfig({ |
| 220 | mobTypeId: MobId.humanoid |
| 221 | }, "spawner"))); |
| 222 | } |
| 223 | |
| 224 | function openSpawner (ctx) |
| 225 | { |
| 226 | context = { kind: "spawner", ...ctx }; |
| 227 | const config = normalizeSpawnerConfig(ctx.config, ctx.config.spawnerId); |
| 228 | title.textContent = `Spawner · ${ctx.tileX}, ${ctx.tileY}`; |
| 229 | form.innerHTML = ""; |
| 230 | const mobField = field("Mob type", mobSelect("mobTypeId", config.mobTypeId)); |
| 231 | form.append( |
| 232 | mobField, |
| 233 | field("Max alive", numberInput("maxAlive", config.maxAlive, 1, 20)), |
| 234 | field("Spawn range", numberInput("spawnRangeTiles", config.spawnRangeTiles, 1, 32)), |
| 235 | field("Max wander range", numberInput("maxWanderRangeTiles", config.maxWanderRangeTiles, 1, 64)), |
| 236 | field("Spawner id", textInput("spawnerId", config.spawnerId, true)) |
| 237 | ); |
| 238 | const spawnInput = form.querySelector('[name="spawnRangeTiles"]'); |
| 239 | const wanderInput = form.querySelector('[name="maxWanderRangeTiles"]'); |
| 240 | const syncWanderMin = () => |
| 241 | { |
| 242 | const spawn = Number(spawnInput.value) || 1; |
| 243 | wanderInput.min = String(spawn); |
| 244 | if (Number(wanderInput.value) < spawn) |
| 245 | { wanderInput.value = String(spawn); } |
| 246 | }; |
| 247 | spawnInput.addEventListener("input", syncWanderMin); |
| 248 | syncWanderMin(); |
| 249 | form.querySelector('[name="mobTypeId"]').addEventListener("change", () => |
| 250 | { |
| 251 | const nextType = form.querySelector('[name="mobTypeId"]').value; |
| 252 | const defaults = normalizeSpawnerConfig({ mobTypeId: nextType }, config.spawnerId); |
| 253 | form.querySelector('[name="maxAlive"]').value = String(defaults.maxAlive); |
| 254 | form.querySelector('[name="spawnRangeTiles"]').value = String(defaults.spawnRangeTiles); |
| 255 | form.querySelector('[name="maxWanderRangeTiles"]').value = String(defaults.maxWanderRangeTiles); |
| 256 | syncWanderMin(); |
| 257 | syncHumanoidSection(defaults); |
| 258 | }); |
| 259 | syncHumanoidSection(config); |
| 260 | show(true); |
| 261 | } |
| 262 | |
| 263 | document.getElementById("tile-config-close").addEventListener("click", () => show(false)); |
| 264 | document.getElementById("tile-config-cancel").addEventListener("click", () => show(false)); |
| 265 | backdrop.addEventListener("click", () => show(false)); |
| 266 | document.getElementById("shop-item-search-close")?.addEventListener("click", () => closeItemSearch()); |
| 267 | itemSearchBackdrop?.addEventListener("click", () => closeItemSearch()); |
| 268 | itemSearchInput?.addEventListener("input", () => renderItemSearchResults(itemSearchInput.value)); |
| 269 | |
| 270 | document.getElementById("tile-config-save").addEventListener("click", () => |
| 271 | { |
| 272 | if (!context) |
| 273 | { return; } |
| 274 | const data = new FormData(form); |
| 275 | const equipment = {}; |
| 276 | for (const slot of EQUIPMENT_SLOTS) |
| 277 | { |
| 278 | const raw = data.get(`equipment.${slot}`); |
| 279 | equipment[slot] = raw ? Number(raw) : null; |
| 280 | } |
| 281 | onSave?.(context, normalizeSpawnerConfig({ |
| 282 | mobTypeId: String(data.get("mobTypeId") || "deer"), |
| 283 | maxAlive: Number(data.get("maxAlive") || 3), |
| 284 | spawnRangeTiles: Number(data.get("spawnRangeTiles") || 4), |
| 285 | maxWanderRangeTiles: Number(data.get("maxWanderRangeTiles") || 4), |
| 286 | spawnerId: String(data.get("spawnerId") || context.config.spawnerId), |
| 287 | sex: String(data.get("sex") || "male"), |
| 288 | displayName: String(data.get("displayName") || ""), |
| 289 | equipment, |
| 290 | sells: sellsDraft, |
| 291 | buysItems: data.get("buysItems") === "on", |
| 292 | hiddenDiscount: Number(data.get("hiddenDiscount") || 0.9) |
| 293 | }, context.config.spawnerId)); |
| 294 | show(false); |
| 295 | }); |
| 296 | document.getElementById("tile-config-remove").addEventListener("click", () => |
| 297 | { |
| 298 | if (!context) |
| 299 | { return; } |
| 300 | onRemove?.(context); |
| 301 | show(false); |
| 302 | }); |
| 303 | |
| 304 | return { |
| 305 | isOpen: () => open, |
| 306 | openSpawner, |
| 307 | close: () => show(false) |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | function field (label, input) |
| 312 | { |
| 313 | const wrap = document.createElement("label"); |
| 314 | wrap.append(document.createTextNode(label), input); |
| 315 | return wrap; |
| 316 | } |
| 317 | |
| 318 | function numberInput (name, value, min, max, step = 1) |
| 319 | { |
| 320 | const input = document.createElement("input"); |
| 321 | input.type = "number"; |
| 322 | input.name = name; |
| 323 | input.min = String(min); |
| 324 | input.max = String(max); |
| 325 | input.step = String(step); |
| 326 | input.value = String(value); |
| 327 | return input; |
| 328 | } |
| 329 | |
| 330 | function textInput (name, value, readOnly = false) |
| 331 | { |
| 332 | const input = document.createElement("input"); |
| 333 | input.type = "text"; |
| 334 | input.name = name; |
| 335 | input.value = String(value ?? ""); |
| 336 | input.readOnly = readOnly; |
| 337 | return input; |
| 338 | } |
| 339 | |
| 340 | function sexSelect (name, value) |
| 341 | { |
| 342 | const select = document.createElement("select"); |
| 343 | select.name = name; |
| 344 | for (const sex of ["male", "female"]) |
| 345 | { |
| 346 | const option = document.createElement("option"); |
| 347 | option.value = sex; |
| 348 | option.textContent = sex; |
| 349 | if (sex === value) |
| 350 | { option.selected = true; } |
| 351 | select.append(option); |
| 352 | } |
| 353 | return select; |
| 354 | } |
| 355 | |
| 356 | function equipSelect (name, slot, value) |
| 357 | { |
| 358 | const select = document.createElement("select"); |
| 359 | select.name = name; |
| 360 | const none = document.createElement("option"); |
| 361 | none.value = ""; |
| 362 | none.textContent = "(none)"; |
| 363 | select.append(none); |
| 364 | for (const preset of EQUIP_PRESETS) |
| 365 | { |
| 366 | if (preset.slot !== slot) |
| 367 | { continue; } |
| 368 | const def = itemDefinition(preset.itemId); |
| 369 | const option = document.createElement("option"); |
| 370 | option.value = String(preset.itemId); |
| 371 | option.textContent = def?.displayName ?? String(preset.itemId); |
| 372 | if (preset.itemId === value) |
| 373 | { option.selected = true; } |
| 374 | select.append(option); |
| 375 | } |
| 376 | return select; |
| 377 | } |
| 378 | |
| 379 | function mobSelect (name, value) |
| 380 | { |
| 381 | const select = document.createElement("select"); |
| 382 | select.name = name; |
| 383 | for (const def of listSpawnableMobTypes()) |
| 384 | { |
| 385 | const option = document.createElement("option"); |
| 386 | option.value = def.mobTypeId; |
| 387 | option.textContent = def.displayName; |
| 388 | if (def.mobTypeId === value) |
| 389 | { option.selected = true; } |
| 390 | select.append(option); |
| 391 | } |
| 392 | return select; |
| 393 | } |