| 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 | isCentralExchangeSpawner, |
| 10 | spawnerEquipmentSlot |
| 11 | } from "/shared/definitions/mobs.js"; |
| 12 | import { SPELL_DEFINITIONS } from "/shared/definitions/spells.js"; |
| 13 | import { centralExchangeStaffCount } from "/shared/market/exchange.js"; |
| 14 | import { ITEM_DEFINITIONS, itemDefinition, isDyeableClothing } from "/shared/definitions/items.js"; |
| 15 | import { EQUIPMENT_SLOTS } from "/shared/items/inventory.js"; |
| 16 | import { listNpcShopSellableItems } from "/shared/shop/pricing.js"; |
| 17 | import { normalizeRgb } from "/shared/render/tint.js"; |
| 18 | |
| 19 | /** Items that can be worn in a given equipment slot. */ |
| 20 | export function listEquippableItemsForSlot (slot) |
| 21 | { |
| 22 | const out = []; |
| 23 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 24 | { |
| 25 | if (!def?.equipment || def.equipment.slot !== slot) |
| 26 | { continue; } |
| 27 | out.push({ |
| 28 | itemId: def.itemId, |
| 29 | key: def.key ?? String(def.itemId), |
| 30 | displayName: def.displayName ?? `Item ${def.itemId}` |
| 31 | }); |
| 32 | } |
| 33 | out.sort((a, b) => a.displayName.localeCompare(b.displayName)); |
| 34 | return out; |
| 35 | } |
| 36 | |
| 37 | function emptyEquipDraft () |
| 38 | { |
| 39 | return { itemId: null, dyeColor: null }; |
| 40 | } |
| 41 | |
| 42 | function equipDraftFromConfig (raw) |
| 43 | { |
| 44 | const { itemId, dyeColor } = spawnerEquipmentSlot(raw); |
| 45 | return { |
| 46 | itemId: itemId > 0 ? itemId : null, |
| 47 | dyeColor: dyeColor ? normalizeRgb(dyeColor) : null |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | function equipConfigFromDraft (draft) |
| 52 | { |
| 53 | const itemId = draft?.itemId; |
| 54 | if (itemId == null || itemId === "") |
| 55 | { return null; } |
| 56 | const id = Number(itemId) || null; |
| 57 | if (!id) |
| 58 | { return null; } |
| 59 | if (draft.dyeColor && isDyeableClothing(id)) |
| 60 | { return { itemId: id, dyeColor: normalizeRgb(draft.dyeColor) }; } |
| 61 | return id; |
| 62 | } |
| 63 | |
| 64 | function rgbToCss (color) |
| 65 | { |
| 66 | const c = normalizeRgb(color, { r: 0.78, g: 0.31, b: 0.47 }); |
| 67 | return `rgb(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)})`; |
| 68 | } |
| 69 | |
| 70 | export function initTileConfigModal ({ onSave, onRemove }) |
| 71 | { |
| 72 | const backdrop = document.getElementById("tile-config-backdrop"); |
| 73 | const modal = document.getElementById("tile-config-modal"); |
| 74 | const form = document.getElementById("tile-config-form"); |
| 75 | const title = document.getElementById("tile-config-title"); |
| 76 | const itemSearchBackdrop = document.getElementById("shop-item-search-backdrop"); |
| 77 | const itemSearchModal = document.getElementById("shop-item-search-modal"); |
| 78 | const itemSearchTitle = itemSearchModal?.querySelector("h2") ?? null; |
| 79 | const itemSearchInput = document.getElementById("shop-item-search-input"); |
| 80 | const itemSearchResults = document.getElementById("shop-item-search-results"); |
| 81 | const dyeBackdrop = document.getElementById("equip-dye-backdrop"); |
| 82 | const dyeModal = document.getElementById("equip-dye-modal"); |
| 83 | const dyeTitle = document.getElementById("equip-dye-title"); |
| 84 | const dyePreview = document.getElementById("equip-dye-preview"); |
| 85 | const dyeR = document.getElementById("equip-dye-r"); |
| 86 | const dyeG = document.getElementById("equip-dye-g"); |
| 87 | const dyeB = document.getElementById("equip-dye-b"); |
| 88 | const dyeRNum = document.getElementById("equip-dye-r-num"); |
| 89 | const dyeGNum = document.getElementById("equip-dye-g-num"); |
| 90 | const dyeBNum = document.getElementById("equip-dye-b-num"); |
| 91 | let open = false; |
| 92 | let context = null; |
| 93 | /** @type {{ itemId: number, quantity: number }[]} */ |
| 94 | let sellsDraft = []; |
| 95 | /** @type {{ spellId: string, chance: number }[]} */ |
| 96 | let spellsDraft = []; |
| 97 | /** @type {Record<string, { itemId: number|null, dyeColor: {r:number,g:number,b:number}|null }>} */ |
| 98 | let equipmentDraft = {}; |
| 99 | /** @type {{ kind: "sells" } | { kind: "equip", slot: string } | { kind: "spells" } | null} */ |
| 100 | let itemSearchMode = null; |
| 101 | /** @type {string|null} */ |
| 102 | let dyeEditSlot = null; |
| 103 | |
| 104 | function show (visible) |
| 105 | { |
| 106 | open = visible; |
| 107 | backdrop.hidden = !visible; |
| 108 | modal.hidden = !visible; |
| 109 | if (!visible) |
| 110 | { |
| 111 | context = null; |
| 112 | closeItemSearch(); |
| 113 | closeDyePicker(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | function closeItemSearch () |
| 118 | { |
| 119 | itemSearchMode = null; |
| 120 | if (itemSearchBackdrop) |
| 121 | { itemSearchBackdrop.hidden = true; } |
| 122 | if (itemSearchModal) |
| 123 | { itemSearchModal.hidden = true; } |
| 124 | } |
| 125 | |
| 126 | function openSellItemSearch () |
| 127 | { |
| 128 | itemSearchMode = { kind: "sells" }; |
| 129 | openItemSearch("Add shop stock"); |
| 130 | } |
| 131 | |
| 132 | function openEquipItemSearch (slot) |
| 133 | { |
| 134 | itemSearchMode = { kind: "equip", slot }; |
| 135 | openItemSearch(`Equip ${slot}`); |
| 136 | } |
| 137 | |
| 138 | function openItemSearch (heading) |
| 139 | { |
| 140 | if (!itemSearchBackdrop || !itemSearchModal || !itemSearchInput) |
| 141 | { return; } |
| 142 | if (itemSearchTitle) |
| 143 | { itemSearchTitle.textContent = heading ?? "Choose item"; } |
| 144 | itemSearchInput.value = ""; |
| 145 | itemSearchInput.placeholder = itemSearchMode?.kind === "equip" |
| 146 | ? "Search equipment…" |
| 147 | : "Search items…"; |
| 148 | renderItemSearchResults(""); |
| 149 | itemSearchBackdrop.hidden = false; |
| 150 | itemSearchModal.hidden = false; |
| 151 | itemSearchInput.focus(); |
| 152 | } |
| 153 | |
| 154 | function candidateItems () |
| 155 | { |
| 156 | if (itemSearchMode?.kind === "equip") |
| 157 | { return listEquippableItemsForSlot(itemSearchMode.slot); } |
| 158 | return listNpcShopSellableItems(); |
| 159 | } |
| 160 | |
| 161 | function renderItemSearchResults (query) |
| 162 | { |
| 163 | if (!itemSearchResults) |
| 164 | { return; } |
| 165 | itemSearchResults.innerHTML = ""; |
| 166 | const q = String(query ?? "").trim().toLowerCase(); |
| 167 | const taken = itemSearchMode?.kind === "sells" |
| 168 | ? new Set(sellsDraft.map((line) => line.itemId)) |
| 169 | : null; |
| 170 | const matches = candidateItems().filter((item) => |
| 171 | { |
| 172 | if (taken?.has(item.itemId)) |
| 173 | { return false; } |
| 174 | if (!q) |
| 175 | { return true; } |
| 176 | return item.displayName.toLowerCase().includes(q) || item.key.toLowerCase().includes(q); |
| 177 | }).slice(0, 120); |
| 178 | if (!matches.length) |
| 179 | { |
| 180 | itemSearchResults.textContent = "No matching items."; |
| 181 | return; |
| 182 | } |
| 183 | for (const item of matches) |
| 184 | { |
| 185 | const row = document.createElement("button"); |
| 186 | row.type = "button"; |
| 187 | row.className = "shop-item-search-row"; |
| 188 | row.textContent = item.displayName; |
| 189 | row.addEventListener("click", () => |
| 190 | { |
| 191 | if (itemSearchMode?.kind === "equip") |
| 192 | { |
| 193 | equipmentDraft[itemSearchMode.slot] = { |
| 194 | itemId: item.itemId, |
| 195 | dyeColor: null |
| 196 | }; |
| 197 | renderEquipmentRows(); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | sellsDraft.push({ itemId: item.itemId, quantity: 0 }); |
| 202 | renderSellsList(); |
| 203 | } |
| 204 | closeItemSearch(); |
| 205 | }); |
| 206 | itemSearchResults.append(row); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | function closeDyePicker () |
| 211 | { |
| 212 | dyeEditSlot = null; |
| 213 | if (dyeBackdrop) |
| 214 | { dyeBackdrop.hidden = true; } |
| 215 | if (dyeModal) |
| 216 | { dyeModal.hidden = true; } |
| 217 | } |
| 218 | |
| 219 | function readDyeSliders () |
| 220 | { |
| 221 | return { |
| 222 | r: Math.min(255, Math.max(0, Number(dyeR?.value) || 0)) / 255, |
| 223 | g: Math.min(255, Math.max(0, Number(dyeG?.value) || 0)) / 255, |
| 224 | b: Math.min(255, Math.max(0, Number(dyeB?.value) || 0)) / 255 |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | function syncDyePreview () |
| 229 | { |
| 230 | const color = readDyeSliders(); |
| 231 | if (dyePreview) |
| 232 | { dyePreview.style.background = rgbToCss(color); } |
| 233 | if (dyeRNum) |
| 234 | { dyeRNum.value = String(Math.round(color.r * 255)); } |
| 235 | if (dyeGNum) |
| 236 | { dyeGNum.value = String(Math.round(color.g * 255)); } |
| 237 | if (dyeBNum) |
| 238 | { dyeBNum.value = String(Math.round(color.b * 255)); } |
| 239 | } |
| 240 | |
| 241 | function setDyeSliders (color) |
| 242 | { |
| 243 | const c = normalizeRgb(color, { r: 200 / 255, g: 80 / 255, b: 120 / 255 }); |
| 244 | const r = Math.round(c.r * 255); |
| 245 | const g = Math.round(c.g * 255); |
| 246 | const b = Math.round(c.b * 255); |
| 247 | if (dyeR) |
| 248 | { dyeR.value = String(r); } |
| 249 | if (dyeG) |
| 250 | { dyeG.value = String(g); } |
| 251 | if (dyeB) |
| 252 | { dyeB.value = String(b); } |
| 253 | syncDyePreview(); |
| 254 | } |
| 255 | |
| 256 | function openDyePicker (slot) |
| 257 | { |
| 258 | const draft = equipmentDraft[slot]; |
| 259 | if (!draft?.itemId || !isDyeableClothing(draft.itemId) || !dyeModal || !dyeBackdrop) |
| 260 | { return; } |
| 261 | dyeEditSlot = slot; |
| 262 | const def = itemDefinition(draft.itemId); |
| 263 | if (dyeTitle) |
| 264 | { dyeTitle.textContent = `Dye — ${def?.displayName ?? slot}`; } |
| 265 | setDyeSliders(draft.dyeColor ?? { r: 200 / 255, g: 80 / 255, b: 120 / 255 }); |
| 266 | dyeBackdrop.hidden = false; |
| 267 | dyeModal.hidden = false; |
| 268 | } |
| 269 | |
| 270 | function bindDyeChannel (range, num) |
| 271 | { |
| 272 | range?.addEventListener("input", () => |
| 273 | { |
| 274 | if (num) |
| 275 | { num.value = range.value; } |
| 276 | syncDyePreview(); |
| 277 | }); |
| 278 | num?.addEventListener("input", () => |
| 279 | { |
| 280 | const v = Math.min(255, Math.max(0, Number(num.value) || 0)); |
| 281 | num.value = String(v); |
| 282 | if (range) |
| 283 | { range.value = String(v); } |
| 284 | syncDyePreview(); |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | bindDyeChannel(dyeR, dyeRNum); |
| 289 | bindDyeChannel(dyeG, dyeGNum); |
| 290 | bindDyeChannel(dyeB, dyeBNum); |
| 291 | |
| 292 | function renderSellsList () |
| 293 | { |
| 294 | const list = form.querySelector("[data-sells-list]"); |
| 295 | if (!list) |
| 296 | { return; } |
| 297 | list.innerHTML = ""; |
| 298 | if (!sellsDraft.length) |
| 299 | { |
| 300 | const empty = document.createElement("p"); |
| 301 | empty.className = "hint"; |
| 302 | empty.textContent = "No items yet. Use + to add stock."; |
| 303 | list.append(empty); |
| 304 | return; |
| 305 | } |
| 306 | for (let index = 0; index < sellsDraft.length; index++) |
| 307 | { |
| 308 | const line = sellsDraft[index]; |
| 309 | const def = itemDefinition(line.itemId); |
| 310 | const row = document.createElement("div"); |
| 311 | row.className = "spawner-sells-row"; |
| 312 | const name = document.createElement("span"); |
| 313 | name.className = "spawner-sells-name"; |
| 314 | name.textContent = def?.displayName ?? `Item ${line.itemId}`; |
| 315 | const qty = document.createElement("input"); |
| 316 | qty.type = "number"; |
| 317 | qty.min = "0"; |
| 318 | qty.step = "1"; |
| 319 | qty.value = String(line.quantity | 0); |
| 320 | qty.title = "0 = infinite; otherwise restocks this many at 00:00 server time"; |
| 321 | qty.addEventListener("input", () => |
| 322 | { |
| 323 | sellsDraft[index].quantity = Math.max(0, Number(qty.value) || 0) | 0; |
| 324 | }); |
| 325 | const remove = document.createElement("button"); |
| 326 | remove.type = "button"; |
| 327 | remove.textContent = "Remove"; |
| 328 | remove.addEventListener("click", () => |
| 329 | { |
| 330 | sellsDraft.splice(index, 1); |
| 331 | renderSellsList(); |
| 332 | }); |
| 333 | row.append(name, qty, remove); |
| 334 | list.append(row); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | function renderEquipmentRows () |
| 339 | { |
| 340 | const list = form.querySelector("[data-equipment-list]"); |
| 341 | if (!list) |
| 342 | { return; } |
| 343 | list.innerHTML = ""; |
| 344 | for (const slot of EQUIPMENT_SLOTS) |
| 345 | { |
| 346 | const draft = equipmentDraft[slot] ?? emptyEquipDraft(); |
| 347 | const itemId = draft.itemId ?? null; |
| 348 | const def = itemId ? itemDefinition(itemId) : null; |
| 349 | const dyeable = itemId != null && isDyeableClothing(itemId); |
| 350 | const row = document.createElement("div"); |
| 351 | row.className = "spawner-equip-row"; |
| 352 | const label = document.createElement("span"); |
| 353 | label.className = "spawner-equip-slot"; |
| 354 | label.textContent = slot; |
| 355 | const name = document.createElement("span"); |
| 356 | name.className = "spawner-equip-name"; |
| 357 | if (def && draft.dyeColor) |
| 358 | { |
| 359 | const swatch = document.createElement("span"); |
| 360 | swatch.className = "spawner-equip-dye-swatch"; |
| 361 | swatch.style.background = rgbToCss(draft.dyeColor); |
| 362 | name.append(swatch, document.createTextNode(def.displayName)); |
| 363 | } |
| 364 | else |
| 365 | { name.textContent = def?.displayName ?? "(none)"; } |
| 366 | const pick = document.createElement("button"); |
| 367 | pick.type = "button"; |
| 368 | pick.textContent = "Choose…"; |
| 369 | pick.addEventListener("click", () => openEquipItemSearch(slot)); |
| 370 | const dyeBtn = document.createElement("button"); |
| 371 | dyeBtn.type = "button"; |
| 372 | dyeBtn.textContent = "Dye…"; |
| 373 | dyeBtn.disabled = !dyeable; |
| 374 | dyeBtn.title = dyeable |
| 375 | ? "Set clothing dye colour" |
| 376 | : "Only tailor clothing can be dyed"; |
| 377 | dyeBtn.addEventListener("click", () => openDyePicker(slot)); |
| 378 | const clear = document.createElement("button"); |
| 379 | clear.type = "button"; |
| 380 | clear.textContent = "Clear"; |
| 381 | clear.disabled = itemId == null; |
| 382 | clear.addEventListener("click", () => |
| 383 | { |
| 384 | equipmentDraft[slot] = emptyEquipDraft(); |
| 385 | renderEquipmentRows(); |
| 386 | }); |
| 387 | row.append(label, name, pick, dyeBtn, clear); |
| 388 | list.append(row); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | function renderHumanoidFields (config) |
| 393 | { |
| 394 | const wrap = document.createElement("div"); |
| 395 | wrap.className = "spawner-humanoid-fields"; |
| 396 | wrap.dataset.humanoid = "1"; |
| 397 | const exchange = isCentralExchangeSpawner(config); |
| 398 | if (exchange) |
| 399 | { |
| 400 | const note = document.createElement("p"); |
| 401 | note.className = "hint"; |
| 402 | note.textContent = `Spawns all ${centralExchangeStaffCount()} Central Exchange clerks (names & sex fixed). Equipment below is shared. No shop interaction.`; |
| 403 | wrap.append(note); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | wrap.append( |
| 408 | field("Display name", textInput("displayName", config.displayName || "Shopkeeper")), |
| 409 | field("Sex", sexSelect("sex", config.sex)) |
| 410 | ); |
| 411 | } |
| 412 | |
| 413 | const equipHeader = document.createElement("div"); |
| 414 | equipHeader.className = "spawner-sells-header"; |
| 415 | const equipTitle = document.createElement("strong"); |
| 416 | equipTitle.textContent = "Equipment"; |
| 417 | equipHeader.append(equipTitle); |
| 418 | wrap.append(equipHeader); |
| 419 | |
| 420 | const equipList = document.createElement("div"); |
| 421 | equipList.className = "spawner-equip-list"; |
| 422 | equipList.dataset.equipmentList = "1"; |
| 423 | wrap.append(equipList); |
| 424 | |
| 425 | equipmentDraft = {}; |
| 426 | for (const slot of EQUIPMENT_SLOTS) |
| 427 | { equipmentDraft[slot] = equipDraftFromConfig(config.equipment?.[slot]); } |
| 428 | queueMicrotask(() => renderEquipmentRows()); |
| 429 | |
| 430 | if (!exchange) |
| 431 | { |
| 432 | wrap.append(field("Hidden discount (0–1)", numberInput("hiddenDiscount", config.hiddenDiscount ?? 0.9, 0.01, 1, 0.01))); |
| 433 | |
| 434 | const sellsHeader = document.createElement("div"); |
| 435 | sellsHeader.className = "spawner-sells-header"; |
| 436 | const sellsTitle = document.createElement("strong"); |
| 437 | sellsTitle.textContent = "Sells"; |
| 438 | const addBtn = document.createElement("button"); |
| 439 | addBtn.type = "button"; |
| 440 | addBtn.textContent = "+"; |
| 441 | addBtn.title = "Add item"; |
| 442 | addBtn.addEventListener("click", () => openSellItemSearch()); |
| 443 | sellsHeader.append(sellsTitle, addBtn); |
| 444 | wrap.append(sellsHeader); |
| 445 | |
| 446 | const list = document.createElement("div"); |
| 447 | list.className = "spawner-sells-list"; |
| 448 | list.dataset.sellsList = "1"; |
| 449 | wrap.append(list); |
| 450 | |
| 451 | const buysLabel = document.createElement("label"); |
| 452 | buysLabel.className = "spawner-buys-row"; |
| 453 | const buys = document.createElement("input"); |
| 454 | buys.type = "checkbox"; |
| 455 | buys.name = "buysItems"; |
| 456 | buys.checked = !!config.buysItems; |
| 457 | buysLabel.append(buys, document.createTextNode(" Buys items")); |
| 458 | wrap.append(buysLabel); |
| 459 | |
| 460 | const hint = document.createElement("p"); |
| 461 | hint.className = "hint"; |
| 462 | hint.textContent = "Quantity 0 = infinite stock. Limited stock restocks daily at 00:00 server time. 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."; |
| 463 | wrap.append(hint); |
| 464 | |
| 465 | sellsDraft = (config.sells ?? []).map((line) => |
| 466 | { |
| 467 | if (line && typeof line === "object") |
| 468 | { return { itemId: line.itemId | 0, quantity: Math.max(0, line.quantity | 0 || 0) }; } |
| 469 | return { itemId: line | 0, quantity: 0 }; |
| 470 | }).filter((line) => line.itemId > 0); |
| 471 | |
| 472 | queueMicrotask(() => renderSellsList()); |
| 473 | } |
| 474 | else |
| 475 | { sellsDraft = []; } |
| 476 | return wrap; |
| 477 | } |
| 478 | |
| 479 | function syncHumanoidSection (config) |
| 480 | { |
| 481 | form.querySelector("[data-humanoid]")?.remove(); |
| 482 | const mobTypeId = form.querySelector('[name="mobTypeId"]')?.value; |
| 483 | if (!isHumanoidMob(mobTypeId)) |
| 484 | { |
| 485 | sellsDraft = []; |
| 486 | equipmentDraft = {}; |
| 487 | return; |
| 488 | } |
| 489 | form.append(renderHumanoidFields(config ?? normalizeSpawnerConfig({ |
| 490 | mobTypeId: mobTypeId || MobId.humanoid |
| 491 | }, "spawner"))); |
| 492 | const maxAlive = form.querySelector('[name="maxAlive"]'); |
| 493 | if (maxAlive && isCentralExchangeSpawner({ mobTypeId })) |
| 494 | { |
| 495 | maxAlive.value = String(centralExchangeStaffCount()); |
| 496 | maxAlive.disabled = true; |
| 497 | } |
| 498 | else if (maxAlive) |
| 499 | { maxAlive.disabled = false; } |
| 500 | } |
| 501 | |
| 502 | function syncSpellsSection (config) |
| 503 | { |
| 504 | form.querySelector("[data-spells]")?.remove(); |
| 505 | form.append(renderSpellsFields(config)); |
| 506 | } |
| 507 | |
| 508 | function renderSpellsFields (config) |
| 509 | { |
| 510 | const wrap = document.createElement("div"); |
| 511 | wrap.className = "spawner-spells-fields"; |
| 512 | wrap.dataset.spells = "1"; |
| 513 | |
| 514 | const header = document.createElement("div"); |
| 515 | header.className = "spawner-sells-header"; |
| 516 | const titleEl = document.createElement("strong"); |
| 517 | titleEl.textContent = "Spells"; |
| 518 | const addBtn = document.createElement("button"); |
| 519 | addBtn.type = "button"; |
| 520 | addBtn.textContent = "+"; |
| 521 | addBtn.title = "Add spell"; |
| 522 | addBtn.addEventListener("click", () => |
| 523 | { |
| 524 | const available = Object.values(SPELL_DEFINITIONS).find((def) => |
| 525 | !spellsDraft.some((line) => line.spellId === def.spellId)); |
| 526 | if (!available) |
| 527 | { return; } |
| 528 | spellsDraft.push({ spellId: available.spellId, chance: 100 }); |
| 529 | renderSpellsList(); |
| 530 | }); |
| 531 | header.append(titleEl, addBtn); |
| 532 | wrap.append(header); |
| 533 | |
| 534 | const list = document.createElement("div"); |
| 535 | list.className = "spawner-sells-list"; |
| 536 | list.dataset.spellsList = "1"; |
| 537 | wrap.append(list); |
| 538 | |
| 539 | const hint = document.createElement("p"); |
| 540 | hint.className = "hint"; |
| 541 | hint.textContent = "Chance is the % roll when the mob picks that spell to cast. Only Attack (entity) spells are used in combat. Mobs only learn spells their Level can cast."; |
| 542 | wrap.append(hint); |
| 543 | |
| 544 | if (!Array.isArray(spellsDraft) || !spellsDraft.length) |
| 545 | { |
| 546 | spellsDraft = (config?.spells ?? []).map((line) => ({ |
| 547 | spellId: String(line.spellId ?? ""), |
| 548 | chance: Math.max(0, Math.min(100, Number(line.chance) || 0)) | 0 |
| 549 | })).filter((line) => line.spellId); |
| 550 | } |
| 551 | queueMicrotask(() => renderSpellsList()); |
| 552 | return wrap; |
| 553 | } |
| 554 | |
| 555 | function renderSpellsList () |
| 556 | { |
| 557 | const list = form.querySelector("[data-spells-list]"); |
| 558 | if (!list) |
| 559 | { return; } |
| 560 | list.replaceChildren(); |
| 561 | if (!spellsDraft.length) |
| 562 | { |
| 563 | const empty = document.createElement("p"); |
| 564 | empty.className = "hint"; |
| 565 | empty.textContent = "No spells."; |
| 566 | list.append(empty); |
| 567 | return; |
| 568 | } |
| 569 | const level = Math.max(1, Number(form.querySelector('[name="level"]')?.value) || 1) | 0; |
| 570 | for (let index = 0; index < spellsDraft.length; index++) |
| 571 | { |
| 572 | const line = spellsDraft[index]; |
| 573 | const def = SPELL_DEFINITIONS[line.spellId]; |
| 574 | const row = document.createElement("div"); |
| 575 | row.className = "spawner-sell-row"; |
| 576 | |
| 577 | const select = document.createElement("select"); |
| 578 | for (const spell of Object.values(SPELL_DEFINITIONS)) |
| 579 | { |
| 580 | const opt = document.createElement("option"); |
| 581 | opt.value = spell.spellId; |
| 582 | const minLv = spell.minMagicLevel | 0 || 1; |
| 583 | opt.textContent = `${spell.displayName} (Magic ${minLv}+)`; |
| 584 | if (spell.spellId === line.spellId) |
| 585 | { opt.selected = true; } |
| 586 | select.append(opt); |
| 587 | } |
| 588 | select.addEventListener("change", () => |
| 589 | { |
| 590 | spellsDraft[index].spellId = select.value; |
| 591 | renderSpellsList(); |
| 592 | }); |
| 593 | |
| 594 | const chance = document.createElement("input"); |
| 595 | chance.type = "number"; |
| 596 | chance.min = "0"; |
| 597 | chance.max = "100"; |
| 598 | chance.value = String(line.chance); |
| 599 | chance.title = "Cast chance %"; |
| 600 | chance.addEventListener("change", () => |
| 601 | { |
| 602 | spellsDraft[index].chance = Math.max(0, Math.min(100, Number(chance.value) || 0)) | 0; |
| 603 | }); |
| 604 | |
| 605 | const minLv = def?.minMagicLevel | 0 || 1; |
| 606 | const status = document.createElement("span"); |
| 607 | status.className = "hint"; |
| 608 | status.textContent = level < minLv ? `needs Lv.${minLv}` : `${line.chance}%`; |
| 609 | |
| 610 | const remove = document.createElement("button"); |
| 611 | remove.type = "button"; |
| 612 | remove.textContent = "×"; |
| 613 | remove.addEventListener("click", () => |
| 614 | { |
| 615 | spellsDraft.splice(index, 1); |
| 616 | renderSpellsList(); |
| 617 | }); |
| 618 | |
| 619 | row.append(select, chance, status, remove); |
| 620 | list.append(row); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | function openSpawner (ctx) |
| 625 | { |
| 626 | context = { kind: "spawner", ...ctx }; |
| 627 | const config = normalizeSpawnerConfig(ctx.config, ctx.config.spawnerId); |
| 628 | title.textContent = `Spawner · ${ctx.tileX}, ${ctx.tileY}`; |
| 629 | form.innerHTML = ""; |
| 630 | const mobField = field("Mob type", mobSelect("mobTypeId", config.mobTypeId)); |
| 631 | form.append( |
| 632 | mobField, |
| 633 | field("Level", numberInput("level", config.level ?? 1, 1, 99)), |
| 634 | field("Bounty", numberInput("bounty", config.bounty ?? 0, 0, 1_000_000)), |
| 635 | field("Max alive", numberInput("maxAlive", config.maxAlive, 1, 20)), |
| 636 | field("Spawn range", numberInput("spawnRangeTiles", config.spawnRangeTiles, 1, 32)), |
| 637 | field("Max wander range", numberInput("maxWanderRangeTiles", config.maxWanderRangeTiles, 1, 64)), |
| 638 | field("Spawner id", textInput("spawnerId", config.spawnerId, true)) |
| 639 | ); |
| 640 | const spawnInput = form.querySelector('[name="spawnRangeTiles"]'); |
| 641 | const wanderInput = form.querySelector('[name="maxWanderRangeTiles"]'); |
| 642 | const syncWanderMin = () => |
| 643 | { |
| 644 | const spawn = Number(spawnInput.value) || 1; |
| 645 | wanderInput.min = String(spawn); |
| 646 | if (Number(wanderInput.value) < spawn) |
| 647 | { wanderInput.value = String(spawn); } |
| 648 | }; |
| 649 | spawnInput.addEventListener("input", syncWanderMin); |
| 650 | syncWanderMin(); |
| 651 | form.querySelector('[name="level"]')?.addEventListener("input", () => renderSpellsList()); |
| 652 | form.querySelector('[name="mobTypeId"]').addEventListener("change", () => |
| 653 | { |
| 654 | const nextType = form.querySelector('[name="mobTypeId"]').value; |
| 655 | const defaults = normalizeSpawnerConfig({ mobTypeId: nextType }, config.spawnerId); |
| 656 | form.querySelector('[name="maxAlive"]').value = String(defaults.maxAlive); |
| 657 | form.querySelector('[name="spawnRangeTiles"]').value = String(defaults.spawnRangeTiles); |
| 658 | form.querySelector('[name="maxWanderRangeTiles"]').value = String(defaults.maxWanderRangeTiles); |
| 659 | form.querySelector('[name="level"]').value = String(defaults.level ?? 1); |
| 660 | form.querySelector('[name="bounty"]').value = String(defaults.bounty ?? 0); |
| 661 | syncWanderMin(); |
| 662 | syncHumanoidSection(defaults); |
| 663 | syncSpellsSection(defaults); |
| 664 | }); |
| 665 | spellsDraft = (config.spells ?? []).map((line) => ({ |
| 666 | spellId: String(line.spellId ?? ""), |
| 667 | chance: Math.max(0, Math.min(100, Number(line.chance) || 0)) | 0 |
| 668 | })).filter((line) => line.spellId); |
| 669 | syncHumanoidSection(config); |
| 670 | syncSpellsSection(config); |
| 671 | show(true); |
| 672 | } |
| 673 | |
| 674 | document.getElementById("tile-config-close").addEventListener("click", () => show(false)); |
| 675 | document.getElementById("tile-config-cancel").addEventListener("click", () => show(false)); |
| 676 | backdrop.addEventListener("click", () => show(false)); |
| 677 | document.getElementById("shop-item-search-close")?.addEventListener("click", () => closeItemSearch()); |
| 678 | itemSearchBackdrop?.addEventListener("click", () => closeItemSearch()); |
| 679 | itemSearchInput?.addEventListener("input", () => renderItemSearchResults(itemSearchInput.value)); |
| 680 | document.getElementById("equip-dye-close")?.addEventListener("click", () => closeDyePicker()); |
| 681 | dyeBackdrop?.addEventListener("click", () => closeDyePicker()); |
| 682 | document.getElementById("equip-dye-apply")?.addEventListener("click", () => |
| 683 | { |
| 684 | if (!dyeEditSlot || !equipmentDraft[dyeEditSlot]) |
| 685 | { return; } |
| 686 | equipmentDraft[dyeEditSlot].dyeColor = readDyeSliders(); |
| 687 | closeDyePicker(); |
| 688 | renderEquipmentRows(); |
| 689 | }); |
| 690 | document.getElementById("equip-dye-clear")?.addEventListener("click", () => |
| 691 | { |
| 692 | if (!dyeEditSlot || !equipmentDraft[dyeEditSlot]) |
| 693 | { return; } |
| 694 | equipmentDraft[dyeEditSlot].dyeColor = null; |
| 695 | closeDyePicker(); |
| 696 | renderEquipmentRows(); |
| 697 | }); |
| 698 | |
| 699 | document.getElementById("tile-config-save").addEventListener("click", () => |
| 700 | { |
| 701 | if (!context) |
| 702 | { return; } |
| 703 | const data = new FormData(form); |
| 704 | const equipment = {}; |
| 705 | for (const slot of EQUIPMENT_SLOTS) |
| 706 | { equipment[slot] = equipConfigFromDraft(equipmentDraft[slot]); } |
| 707 | onSave?.(context, normalizeSpawnerConfig({ |
| 708 | mobTypeId: String(data.get("mobTypeId") || "deer"), |
| 709 | maxAlive: Number(data.get("maxAlive") || 3), |
| 710 | spawnRangeTiles: Number(data.get("spawnRangeTiles") || 4), |
| 711 | maxWanderRangeTiles: Number(data.get("maxWanderRangeTiles") || 4), |
| 712 | level: Number(data.get("level") || 1), |
| 713 | bounty: Number(data.get("bounty") || 0), |
| 714 | spawnerId: String(data.get("spawnerId") || context.config.spawnerId), |
| 715 | sex: String(data.get("sex") || "male"), |
| 716 | displayName: String(data.get("displayName") || ""), |
| 717 | equipment, |
| 718 | sells: sellsDraft, |
| 719 | spells: spellsDraft, |
| 720 | buysItems: data.get("buysItems") === "on", |
| 721 | hiddenDiscount: Number(data.get("hiddenDiscount") || 0.9), |
| 722 | centralExchange: isCentralExchangeSpawner(String(data.get("mobTypeId") || "")) |
| 723 | }, context.config.spawnerId)); |
| 724 | show(false); |
| 725 | }); |
| 726 | document.getElementById("tile-config-remove").addEventListener("click", () => |
| 727 | { |
| 728 | if (!context) |
| 729 | { return; } |
| 730 | onRemove?.(context); |
| 731 | show(false); |
| 732 | }); |
| 733 | |
| 734 | return { |
| 735 | isOpen: () => open, |
| 736 | openSpawner, |
| 737 | close: () => show(false) |
| 738 | }; |
| 739 | } |
| 740 | |
| 741 | function field (label, input) |
| 742 | { |
| 743 | const wrap = document.createElement("label"); |
| 744 | wrap.append(document.createTextNode(label), input); |
| 745 | return wrap; |
| 746 | } |
| 747 | |
| 748 | function numberInput (name, value, min, max, step = 1) |
| 749 | { |
| 750 | const input = document.createElement("input"); |
| 751 | input.type = "number"; |
| 752 | input.name = name; |
| 753 | input.min = String(min); |
| 754 | input.max = String(max); |
| 755 | input.step = String(step); |
| 756 | input.value = String(value); |
| 757 | return input; |
| 758 | } |
| 759 | |
| 760 | function textInput (name, value, readOnly = false) |
| 761 | { |
| 762 | const input = document.createElement("input"); |
| 763 | input.type = "text"; |
| 764 | input.name = name; |
| 765 | input.value = String(value ?? ""); |
| 766 | input.readOnly = readOnly; |
| 767 | return input; |
| 768 | } |
| 769 | |
| 770 | function sexSelect (name, value) |
| 771 | { |
| 772 | const select = document.createElement("select"); |
| 773 | select.name = name; |
| 774 | for (const sex of ["male", "female"]) |
| 775 | { |
| 776 | const option = document.createElement("option"); |
| 777 | option.value = sex; |
| 778 | option.textContent = sex; |
| 779 | if (sex === value) |
| 780 | { option.selected = true; } |
| 781 | select.append(option); |
| 782 | } |
| 783 | return select; |
| 784 | } |
| 785 | |
| 786 | function mobSelect (name, value) |
| 787 | { |
| 788 | const select = document.createElement("select"); |
| 789 | select.name = name; |
| 790 | for (const def of listSpawnableMobTypes()) |
| 791 | { |
| 792 | const option = document.createElement("option"); |
| 793 | option.value = def.mobTypeId; |
| 794 | option.textContent = def.displayName; |
| 795 | if (def.mobTypeId === value) |
| 796 | { option.selected = true; } |
| 797 | select.append(option); |
| 798 | } |
| 799 | return select; |
| 800 | } |