| 1 | import { |
| 2 | ConditionKind, |
| 3 | ConditionSetMode, |
| 4 | CONDITION_SET_MODE_LABELS, |
| 5 | emptyConditionSet, |
| 6 | normalizeConditionSet |
| 7 | } from "/shared/quests/conditions.js"; |
| 8 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 9 | import { listSpawnableMobTypes } from "/shared/definitions/mobs.js"; |
| 10 | import { |
| 11 | isMetaSkillId, |
| 12 | listSkillRequirementOptions, |
| 13 | MetaSkillId |
| 14 | } from "/shared/definitions/skills.js"; |
| 15 | import { createDimensionCombobox } from "./dimensionCombobox.js"; |
| 16 | import { OVERWORLD_ID } from "/shared/world/dimensions.js"; |
| 17 | |
| 18 | /** All condition kinds available in the shared editor UI. questStepAtLeast is load-only. */ |
| 19 | export const CONDITION_EDITOR_KINDS = Object.freeze( |
| 20 | Object.values(ConditionKind).filter((kind) => kind !== ConditionKind.questStepAtLeast) |
| 21 | ); |
| 22 | |
| 23 | export { |
| 24 | emptyConditionSet, |
| 25 | normalizeConditionSet, |
| 26 | ConditionKind, |
| 27 | ConditionSetMode, |
| 28 | CONDITION_SET_MODE_LABELS |
| 29 | }; |
| 30 | |
| 31 | export function renderConditionSet (when, onChange, ctx = {}) |
| 32 | { |
| 33 | const wrap = document.createElement("div"); |
| 34 | const questList = Array.isArray(ctx.quests) ? ctx.quests : []; |
| 35 | const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : []; |
| 36 | const set = normalizeConditionSet(when, { quests: questList }); |
| 37 | const bump = (remount = true) => onChange(set, { remount }); |
| 38 | const header = document.createElement("div"); |
| 39 | header.className = "content-editor-condition-set-header"; |
| 40 | const title = document.createElement("strong"); |
| 41 | title.textContent = "When"; |
| 42 | const mode = document.createElement("select"); |
| 43 | mode.className = "content-editor-condition-set-mode"; |
| 44 | mode.title = "How clauses in this set combine"; |
| 45 | for (const value of Object.values(ConditionSetMode)) |
| 46 | { |
| 47 | const opt = document.createElement("option"); |
| 48 | opt.value = value; |
| 49 | opt.textContent = CONDITION_SET_MODE_LABELS[value] ?? value; |
| 50 | if (set.mode === value) |
| 51 | { opt.selected = true; } |
| 52 | mode.append(opt); |
| 53 | } |
| 54 | mode.addEventListener("change", () => |
| 55 | { |
| 56 | set.mode = mode.value; |
| 57 | bump(false); |
| 58 | }); |
| 59 | header.append(title, mode); |
| 60 | wrap.append(header); |
| 61 | |
| 62 | set.all.forEach((clause, index) => |
| 63 | { |
| 64 | const row = document.createElement("div"); |
| 65 | row.className = "content-editor-clause"; |
| 66 | const notLabel = document.createElement("label"); |
| 67 | const notBox = document.createElement("input"); |
| 68 | notBox.type = "checkbox"; |
| 69 | notBox.checked = !!clause.not; |
| 70 | notBox.addEventListener("change", () => |
| 71 | { |
| 72 | set.all[index].not = notBox.checked; |
| 73 | bump(false); |
| 74 | }); |
| 75 | notLabel.append(notBox, document.createTextNode(" NOT")); |
| 76 | row.append(notLabel); |
| 77 | |
| 78 | const kind = document.createElement("select"); |
| 79 | for (const k of CONDITION_EDITOR_KINDS) |
| 80 | { |
| 81 | const opt = document.createElement("option"); |
| 82 | opt.value = k; |
| 83 | opt.textContent = k; |
| 84 | if ((clause.condition?.kind ?? "") === k) |
| 85 | { opt.selected = true; } |
| 86 | kind.append(opt); |
| 87 | } |
| 88 | const otherOpt = document.createElement("option"); |
| 89 | otherOpt.value = "__other__"; |
| 90 | otherOpt.textContent = "(other / JSON)"; |
| 91 | if (!CONDITION_EDITOR_KINDS.includes(clause.condition?.kind)) |
| 92 | { otherOpt.selected = true; } |
| 93 | kind.append(otherOpt); |
| 94 | kind.addEventListener("change", () => |
| 95 | { |
| 96 | if (kind.value === "__other__") |
| 97 | { return; } |
| 98 | set.all[index].condition = defaultCondition(kind.value); |
| 99 | bump(true); |
| 100 | }); |
| 101 | row.append(kind); |
| 102 | |
| 103 | const cond = clause.condition ?? { kind: ConditionKind.hasItem }; |
| 104 | if (cond.kind === ConditionKind.hasItem) |
| 105 | { |
| 106 | row.append(itemConditionPicker(cond, () => |
| 107 | { |
| 108 | set.all[index].condition = cond; |
| 109 | bump(false); |
| 110 | })); |
| 111 | row.append(miniField("qty", cond.quantity ?? 1, (v) => |
| 112 | { |
| 113 | cond.quantity = Number(v) || 1; |
| 114 | set.all[index].condition = cond; |
| 115 | bump(false); |
| 116 | })); |
| 117 | } |
| 118 | else if (cond.kind === ConditionKind.questState) |
| 119 | { |
| 120 | row.append(questStateConditionFields(cond, questList, () => |
| 121 | { |
| 122 | set.all[index].condition = cond; |
| 123 | bump(false); |
| 124 | })); |
| 125 | } |
| 126 | else if (cond.kind === ConditionKind.dialogueHeard) |
| 127 | { |
| 128 | row.append(dialogueHeardConditionFields(cond, dialogueList, () => |
| 129 | { |
| 130 | set.all[index].condition = cond; |
| 131 | bump(false); |
| 132 | })); |
| 133 | } |
| 134 | else if (cond.kind === ConditionKind.killCount) |
| 135 | { |
| 136 | row.append(killCountConditionFields(cond, () => |
| 137 | { |
| 138 | set.all[index].condition = cond; |
| 139 | bump(false); |
| 140 | })); |
| 141 | } |
| 142 | else if (cond.kind === ConditionKind.killMobFromSpawner) |
| 143 | { |
| 144 | row.append(killMobFromSpawnerConditionFields(cond, ctx, () => |
| 145 | { |
| 146 | set.all[index].condition = cond; |
| 147 | bump(false); |
| 148 | })); |
| 149 | } |
| 150 | else if (cond.kind === ConditionKind.atTile) |
| 151 | { |
| 152 | row.append(atTileConditionFields(cond, ctx, () => |
| 153 | { |
| 154 | set.all[index].condition = cond; |
| 155 | bump(false); |
| 156 | })); |
| 157 | } |
| 158 | else if ( |
| 159 | cond.kind === ConditionKind.inDimension |
| 160 | || cond.kind === ConditionKind.hasVisitedDimension |
| 161 | ) |
| 162 | { |
| 163 | row.append(dimensionConditionField(cond, ctx, () => |
| 164 | { |
| 165 | set.all[index].condition = cond; |
| 166 | bump(false); |
| 167 | })); |
| 168 | } |
| 169 | else if (cond.kind === ConditionKind.nearTile) |
| 170 | { |
| 171 | row.append(atTileConditionFields(cond, ctx, () => |
| 172 | { |
| 173 | set.all[index].condition = cond; |
| 174 | bump(false); |
| 175 | })); |
| 176 | row.append(miniField("range", cond.range ?? 1, (v) => |
| 177 | { |
| 178 | cond.range = Math.max(1, Number(v) || 1); |
| 179 | set.all[index].condition = cond; |
| 180 | bump(false); |
| 181 | })); |
| 182 | } |
| 183 | else if (cond.kind === ConditionKind.inRect) |
| 184 | { |
| 185 | row.append(dimensionConditionField(cond, ctx, () => |
| 186 | { |
| 187 | set.all[index].condition = cond; |
| 188 | bump(false); |
| 189 | })); |
| 190 | for (const key of ["minTileX", "minTileY", "maxTileX", "maxTileY"]) |
| 191 | { |
| 192 | row.append(miniField(key, cond[key] ?? 0, (v) => |
| 193 | { |
| 194 | cond[key] = Number(v) | 0; |
| 195 | set.all[index].condition = cond; |
| 196 | bump(false); |
| 197 | })); |
| 198 | } |
| 199 | } |
| 200 | else if (cond.kind === ConditionKind.atChunk) |
| 201 | { |
| 202 | row.append(dimensionConditionField(cond, ctx, () => |
| 203 | { |
| 204 | set.all[index].condition = cond; |
| 205 | bump(false); |
| 206 | })); |
| 207 | row.append(miniField("chunkX", cond.chunkX ?? 0, (v) => |
| 208 | { |
| 209 | cond.chunkX = Number(v) | 0; |
| 210 | set.all[index].condition = cond; |
| 211 | bump(false); |
| 212 | })); |
| 213 | row.append(miniField("chunkY", cond.chunkY ?? 0, (v) => |
| 214 | { |
| 215 | cond.chunkY = Number(v) | 0; |
| 216 | set.all[index].condition = cond; |
| 217 | bump(false); |
| 218 | })); |
| 219 | } |
| 220 | else if (cond.kind === ConditionKind.hasCompletedStep) |
| 221 | { |
| 222 | row.append(hasCompletedStepConditionFields(cond, questList, () => |
| 223 | { |
| 224 | set.all[index].condition = cond; |
| 225 | bump(false); |
| 226 | })); |
| 227 | } |
| 228 | else if (cond.kind === ConditionKind.dialogueTrigger) |
| 229 | { |
| 230 | row.append(miniField("dialogueId", cond.dialogueId ?? "", (v) => |
| 231 | { |
| 232 | cond.dialogueId = v; |
| 233 | set.all[index].condition = cond; |
| 234 | bump(false); |
| 235 | })); |
| 236 | row.append(miniField("trigger", cond.trigger ?? "", (v) => |
| 237 | { |
| 238 | cond.trigger = v; |
| 239 | set.all[index].condition = cond; |
| 240 | bump(false); |
| 241 | })); |
| 242 | } |
| 243 | else if ( |
| 244 | cond.kind === ConditionKind.hpAtLeast |
| 245 | || cond.kind === ConditionKind.staminaAtLeast |
| 246 | ) |
| 247 | { |
| 248 | row.append(miniField("amount", cond.amount ?? 1, (v) => |
| 249 | { |
| 250 | cond.amount = Math.max(0, Number(v) || 0); |
| 251 | set.all[index].condition = cond; |
| 252 | bump(false); |
| 253 | })); |
| 254 | } |
| 255 | else if ( |
| 256 | cond.kind === ConditionKind.hpPercentAtLeast |
| 257 | || cond.kind === ConditionKind.staminaPercentAtLeast |
| 258 | ) |
| 259 | { |
| 260 | row.append(miniField("percent", cond.percent ?? 50, (v) => |
| 261 | { |
| 262 | cond.percent = Math.min(100, Math.max(0, Number(v) || 0)); |
| 263 | set.all[index].condition = cond; |
| 264 | bump(false); |
| 265 | })); |
| 266 | } |
| 267 | else if (cond.kind === ConditionKind.skillLevelAtLeast) |
| 268 | { |
| 269 | row.append(skillRequirementFields(cond, () => |
| 270 | { |
| 271 | set.all[index].condition = cond; |
| 272 | bump(false); |
| 273 | })); |
| 274 | } |
| 275 | else if (cond.kind === ConditionKind.interactBlock) |
| 276 | { |
| 277 | row.append(atTileConditionFields(cond, ctx, () => |
| 278 | { |
| 279 | set.all[index].condition = cond; |
| 280 | bump(false); |
| 281 | })); |
| 282 | row.append(miniField("blockTypeId", cond.blockTypeId ?? "", (v) => |
| 283 | { |
| 284 | cond.blockTypeId = v === "" ? undefined : (Number(v) || v); |
| 285 | set.all[index].condition = cond; |
| 286 | bump(false); |
| 287 | })); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | const json = document.createElement("textarea"); |
| 292 | json.rows = 3; |
| 293 | json.value = JSON.stringify(cond, null, 2); |
| 294 | json.addEventListener("change", () => |
| 295 | { |
| 296 | try |
| 297 | { |
| 298 | set.all[index].condition = JSON.parse(json.value); |
| 299 | bump(false); |
| 300 | } |
| 301 | catch |
| 302 | { /* keep typing */ } |
| 303 | }); |
| 304 | row.append(json); |
| 305 | } |
| 306 | |
| 307 | const remove = document.createElement("button"); |
| 308 | remove.type = "button"; |
| 309 | remove.textContent = "×"; |
| 310 | remove.addEventListener("click", () => |
| 311 | { |
| 312 | set.all.splice(index, 1); |
| 313 | bump(true); |
| 314 | }); |
| 315 | row.append(remove); |
| 316 | wrap.append(row); |
| 317 | }); |
| 318 | |
| 319 | const add = document.createElement("button"); |
| 320 | add.type = "button"; |
| 321 | add.textContent = "Add clause"; |
| 322 | add.addEventListener("click", () => |
| 323 | { |
| 324 | set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) }); |
| 325 | bump(true); |
| 326 | }); |
| 327 | wrap.append(add); |
| 328 | |
| 329 | const jsonFallback = document.createElement("details"); |
| 330 | const summary = document.createElement("summary"); |
| 331 | summary.textContent = "Edit condition set as JSON"; |
| 332 | const area = document.createElement("textarea"); |
| 333 | area.rows = 5; |
| 334 | area.value = JSON.stringify(set, null, 2); |
| 335 | area.addEventListener("change", () => |
| 336 | { |
| 337 | try |
| 338 | { |
| 339 | onChange(normalizeConditionSet(JSON.parse(area.value)), { remount: true }); |
| 340 | } |
| 341 | catch |
| 342 | { /* ignore */ } |
| 343 | }); |
| 344 | jsonFallback.append(summary, area); |
| 345 | wrap.append(jsonFallback); |
| 346 | return wrap; |
| 347 | } |
| 348 | |
| 349 | /** X/Y inputs, tile summary, and center-editor control for atTile. */ |
| 350 | function atTileConditionFields (cond, ctx, onChanged) |
| 351 | { |
| 352 | const wrap = document.createElement("div"); |
| 353 | wrap.className = "quest-condition-attile"; |
| 354 | const dimField = dimensionConditionField(cond, ctx, () => |
| 355 | { |
| 356 | refreshInfo(); |
| 357 | onChanged(); |
| 358 | }); |
| 359 | const xInput = document.createElement("input"); |
| 360 | xInput.type = "number"; |
| 361 | xInput.step = "1"; |
| 362 | xInput.style.width = "5rem"; |
| 363 | xInput.value = String(cond.tileX ?? 0); |
| 364 | xInput.title = "Tile X"; |
| 365 | const yInput = document.createElement("input"); |
| 366 | yInput.type = "number"; |
| 367 | yInput.step = "1"; |
| 368 | yInput.style.width = "5rem"; |
| 369 | yInput.value = String(cond.tileY ?? 0); |
| 370 | yInput.title = "Tile Y"; |
| 371 | const xyRow = document.createElement("div"); |
| 372 | xyRow.className = "quest-condition-pair"; |
| 373 | const xLabel = document.createElement("label"); |
| 374 | xLabel.append(document.createTextNode("X "), xInput); |
| 375 | const yLabel = document.createElement("label"); |
| 376 | yLabel.append(document.createTextNode("Y "), yInput); |
| 377 | const centerBtn = document.createElement("button"); |
| 378 | centerBtn.type = "button"; |
| 379 | centerBtn.textContent = "Center editor"; |
| 380 | centerBtn.title = "Pan the world editor to this tile (keeps this UI open)"; |
| 381 | const info = document.createElement("pre"); |
| 382 | info.className = "quest-condition-tile-info"; |
| 383 | |
| 384 | const refreshInfo = () => |
| 385 | { |
| 386 | if (typeof ctx.describeWorldTile === "function") |
| 387 | { |
| 388 | info.textContent = ctx.describeWorldTile( |
| 389 | cond.tileX | 0, |
| 390 | cond.tileY | 0, |
| 391 | cond.dimensionId |
| 392 | ); |
| 393 | } |
| 394 | else |
| 395 | { info.textContent = "Tile inspection unavailable."; } |
| 396 | }; |
| 397 | |
| 398 | xInput.addEventListener("input", () => |
| 399 | { |
| 400 | cond.tileX = Number(xInput.value) | 0; |
| 401 | refreshInfo(); |
| 402 | onChanged(); |
| 403 | }); |
| 404 | yInput.addEventListener("input", () => |
| 405 | { |
| 406 | cond.tileY = Number(yInput.value) | 0; |
| 407 | refreshInfo(); |
| 408 | onChanged(); |
| 409 | }); |
| 410 | centerBtn.addEventListener("click", () => |
| 411 | { |
| 412 | ctx.centerEditorOnTile?.(cond.tileX | 0, cond.tileY | 0, cond.dimensionId); |
| 413 | refreshInfo(); |
| 414 | }); |
| 415 | |
| 416 | xyRow.append(xLabel, yLabel, centerBtn); |
| 417 | wrap.append(dimField, xyRow, info); |
| 418 | refreshInfo(); |
| 419 | return wrap; |
| 420 | } |
| 421 | |
| 422 | /** Searchable dimension combobox bound to condition.dimensionId. */ |
| 423 | function dimensionConditionField (cond, ctx, onChanged) |
| 424 | { |
| 425 | const root = document.createElement("div"); |
| 426 | root.className = "quest-condition-dim"; |
| 427 | const combo = createDimensionCombobox({ |
| 428 | root, |
| 429 | allowCreate: false, |
| 430 | placeholder: "Dimension…", |
| 431 | onSelect (id) |
| 432 | { |
| 433 | cond.dimensionId = id || OVERWORLD_ID; |
| 434 | onChanged(); |
| 435 | } |
| 436 | }); |
| 437 | const options = typeof ctx.getDimensionOptions === "function" |
| 438 | ? ctx.getDimensionOptions() |
| 439 | : [{ id: OVERWORLD_ID, label: "Overworld" }]; |
| 440 | combo.setOptions(options); |
| 441 | combo.setValue(cond.dimensionId || OVERWORLD_ID); |
| 442 | return root; |
| 443 | } |
| 444 | |
| 445 | /** Mob selector + kill quantity for killCount conditions. */ |
| 446 | function killCountConditionFields (cond, onChanged) |
| 447 | { |
| 448 | const wrap = document.createElement("div"); |
| 449 | wrap.className = "quest-condition-pair"; |
| 450 | const mobSelect = document.createElement("select"); |
| 451 | for (const def of listSpawnableMobTypes()) |
| 452 | { |
| 453 | const opt = document.createElement("option"); |
| 454 | opt.value = def.mobTypeId; |
| 455 | opt.textContent = def.displayName ?? def.mobTypeId; |
| 456 | if (def.mobTypeId === cond.mobTypeId) |
| 457 | { opt.selected = true; } |
| 458 | mobSelect.append(opt); |
| 459 | } |
| 460 | if (cond.mobTypeId && ![...mobSelect.options].some((o) => o.value === cond.mobTypeId)) |
| 461 | { |
| 462 | const missing = document.createElement("option"); |
| 463 | missing.value = cond.mobTypeId; |
| 464 | missing.textContent = `${cond.mobTypeId} (missing)`; |
| 465 | missing.selected = true; |
| 466 | mobSelect.append(missing); |
| 467 | } |
| 468 | if (!cond.mobTypeId && mobSelect.options.length) |
| 469 | { |
| 470 | cond.mobTypeId = mobSelect.options[0].value; |
| 471 | mobSelect.options[0].selected = true; |
| 472 | } |
| 473 | const qty = document.createElement("input"); |
| 474 | qty.type = "number"; |
| 475 | qty.min = "1"; |
| 476 | qty.step = "1"; |
| 477 | qty.style.width = "5rem"; |
| 478 | qty.value = String(Math.max(1, Number(cond.count) || 1)); |
| 479 | qty.title = "Kill count"; |
| 480 | mobSelect.addEventListener("change", () => |
| 481 | { |
| 482 | cond.mobTypeId = mobSelect.value || undefined; |
| 483 | onChanged(); |
| 484 | }); |
| 485 | qty.addEventListener("input", () => |
| 486 | { |
| 487 | cond.count = Math.max(1, Number(qty.value) || 1); |
| 488 | onChanged(); |
| 489 | }); |
| 490 | wrap.append(mobSelect, qty); |
| 491 | return wrap; |
| 492 | } |
| 493 | |
| 494 | /** Mob + spawner combobox + count for killMobFromSpawner conditions. */ |
| 495 | function killMobFromSpawnerConditionFields (cond, ctx, onChanged) |
| 496 | { |
| 497 | const wrap = document.createElement("div"); |
| 498 | wrap.className = "quest-condition-pair"; |
| 499 | const mobSelect = document.createElement("select"); |
| 500 | for (const def of listSpawnableMobTypes()) |
| 501 | { |
| 502 | const opt = document.createElement("option"); |
| 503 | opt.value = def.mobTypeId; |
| 504 | opt.textContent = def.displayName ?? def.mobTypeId; |
| 505 | if (def.mobTypeId === cond.mobTypeId) |
| 506 | { opt.selected = true; } |
| 507 | mobSelect.append(opt); |
| 508 | } |
| 509 | if (cond.mobTypeId && ![...mobSelect.options].some((o) => o.value === cond.mobTypeId)) |
| 510 | { |
| 511 | const missing = document.createElement("option"); |
| 512 | missing.value = cond.mobTypeId; |
| 513 | missing.textContent = `${cond.mobTypeId} (missing)`; |
| 514 | missing.selected = true; |
| 515 | mobSelect.append(missing); |
| 516 | } |
| 517 | if (!cond.mobTypeId && mobSelect.options.length) |
| 518 | { |
| 519 | cond.mobTypeId = mobSelect.options[0].value; |
| 520 | mobSelect.options[0].selected = true; |
| 521 | } |
| 522 | |
| 523 | const spawnerRoot = document.createElement("div"); |
| 524 | spawnerRoot.style.minWidth = "10rem"; |
| 525 | const combo = createDimensionCombobox({ |
| 526 | root: spawnerRoot, |
| 527 | allowCreate: false, |
| 528 | allowUnknown: true, |
| 529 | placeholder: "Spawner id…", |
| 530 | emptyLabel: "No spawners for this mob", |
| 531 | onSelect (id) |
| 532 | { |
| 533 | cond.spawnerId = id || ""; |
| 534 | onChanged(); |
| 535 | } |
| 536 | }); |
| 537 | |
| 538 | function refreshSpawnerOptions () |
| 539 | { |
| 540 | const rows = typeof ctx.getSpawnerOptions === "function" |
| 541 | ? (ctx.getSpawnerOptions(cond.mobTypeId) ?? []) |
| 542 | : []; |
| 543 | combo.setOptions(rows.map((row) => ({ |
| 544 | id: row.spawnerId, |
| 545 | label: row.spawnerId, |
| 546 | hint: row.mobTypeId |
| 547 | ? `${row.mobTypeId} @ ${row.tileX},${row.tileY}` |
| 548 | : `${row.tileX},${row.tileY}` |
| 549 | }))); |
| 550 | combo.setValue(cond.spawnerId || null); |
| 551 | } |
| 552 | |
| 553 | const qty = document.createElement("input"); |
| 554 | qty.type = "number"; |
| 555 | qty.min = "1"; |
| 556 | qty.step = "1"; |
| 557 | qty.style.width = "5rem"; |
| 558 | qty.value = String(Math.max(1, Number(cond.count) || 1)); |
| 559 | qty.title = "Kill count"; |
| 560 | |
| 561 | mobSelect.addEventListener("change", () => |
| 562 | { |
| 563 | cond.mobTypeId = mobSelect.value || undefined; |
| 564 | refreshSpawnerOptions(); |
| 565 | onChanged(); |
| 566 | }); |
| 567 | qty.addEventListener("input", () => |
| 568 | { |
| 569 | cond.count = Math.max(1, Number(qty.value) || 1); |
| 570 | onChanged(); |
| 571 | }); |
| 572 | |
| 573 | refreshSpawnerOptions(); |
| 574 | wrap.append(mobSelect, spawnerRoot, qty); |
| 575 | return wrap; |
| 576 | } |
| 577 | |
| 578 | /** Dialogue picker + step-id dropdown from that tree. */ |
| 579 | function dialogueHeardConditionFields (cond, dialogueList, onChanged) |
| 580 | { |
| 581 | const wrap = document.createElement("div"); |
| 582 | wrap.className = "quest-condition-pair"; |
| 583 | const dialogueSelect = document.createElement("select"); |
| 584 | const blank = document.createElement("option"); |
| 585 | blank.value = ""; |
| 586 | blank.textContent = "Select dialogue…"; |
| 587 | dialogueSelect.append(blank); |
| 588 | for (const tree of dialogueList) |
| 589 | { |
| 590 | const opt = document.createElement("option"); |
| 591 | opt.value = tree.dialogueId; |
| 592 | opt.textContent = tree.label ? `${tree.label} (${tree.dialogueId})` : tree.dialogueId; |
| 593 | if (tree.dialogueId === cond.dialogueId) |
| 594 | { opt.selected = true; } |
| 595 | dialogueSelect.append(opt); |
| 596 | } |
| 597 | if (cond.dialogueId && !dialogueList.some((d) => d.dialogueId === cond.dialogueId)) |
| 598 | { |
| 599 | const missing = document.createElement("option"); |
| 600 | missing.value = cond.dialogueId; |
| 601 | missing.textContent = `${cond.dialogueId} (missing)`; |
| 602 | missing.selected = true; |
| 603 | dialogueSelect.append(missing); |
| 604 | } |
| 605 | |
| 606 | const stepSelect = document.createElement("select"); |
| 607 | const fillSteps = () => |
| 608 | { |
| 609 | const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId) ?? null; |
| 610 | stepSelect.replaceChildren(); |
| 611 | const steps = tree?.steps ?? []; |
| 612 | if (!steps.length) |
| 613 | { |
| 614 | const empty = document.createElement("option"); |
| 615 | empty.value = cond.stepId ?? ""; |
| 616 | empty.textContent = cond.stepId ? String(cond.stepId) : "No steps"; |
| 617 | empty.selected = true; |
| 618 | stepSelect.append(empty); |
| 619 | return; |
| 620 | } |
| 621 | let matched = false; |
| 622 | for (const step of steps) |
| 623 | { |
| 624 | const opt = document.createElement("option"); |
| 625 | opt.value = step.stepId; |
| 626 | opt.textContent = step.label |
| 627 | ? `${step.label} (${step.stepId})` |
| 628 | : step.stepId; |
| 629 | if (step.stepId === cond.stepId) |
| 630 | { |
| 631 | opt.selected = true; |
| 632 | matched = true; |
| 633 | } |
| 634 | stepSelect.append(opt); |
| 635 | } |
| 636 | if (!matched && cond.stepId) |
| 637 | { |
| 638 | const fallback = document.createElement("option"); |
| 639 | fallback.value = cond.stepId; |
| 640 | fallback.textContent = `${cond.stepId} (missing)`; |
| 641 | fallback.selected = true; |
| 642 | stepSelect.append(fallback); |
| 643 | } |
| 644 | else if (!matched) |
| 645 | { |
| 646 | cond.stepId = steps[0].stepId; |
| 647 | stepSelect.options[0].selected = true; |
| 648 | } |
| 649 | }; |
| 650 | fillSteps(); |
| 651 | |
| 652 | dialogueSelect.addEventListener("change", () => |
| 653 | { |
| 654 | cond.dialogueId = dialogueSelect.value; |
| 655 | const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId); |
| 656 | cond.stepId = tree?.steps?.[0]?.stepId ?? ""; |
| 657 | fillSteps(); |
| 658 | onChanged(); |
| 659 | }); |
| 660 | stepSelect.addEventListener("change", () => |
| 661 | { |
| 662 | cond.stepId = stepSelect.value; |
| 663 | onChanged(); |
| 664 | }); |
| 665 | wrap.append(dialogueSelect, stepSelect); |
| 666 | return wrap; |
| 667 | } |
| 668 | |
| 669 | /** Quest picker + state dropdown from that quest's steps. */ |
| 670 | function questStateConditionFields (cond, questList, onChanged) |
| 671 | { |
| 672 | const wrap = document.createElement("div"); |
| 673 | wrap.className = "quest-condition-pair"; |
| 674 | const questSelect = document.createElement("select"); |
| 675 | const blank = document.createElement("option"); |
| 676 | blank.value = ""; |
| 677 | blank.textContent = "Select quest…"; |
| 678 | questSelect.append(blank); |
| 679 | for (const quest of questList) |
| 680 | { |
| 681 | const opt = document.createElement("option"); |
| 682 | opt.value = quest.questId; |
| 683 | opt.textContent = quest.title ? `${quest.title} (${quest.questId})` : quest.questId; |
| 684 | if (quest.questId === cond.questId) |
| 685 | { opt.selected = true; } |
| 686 | questSelect.append(opt); |
| 687 | } |
| 688 | if (cond.questId && !questList.some((q) => q.questId === cond.questId)) |
| 689 | { |
| 690 | const missing = document.createElement("option"); |
| 691 | missing.value = cond.questId; |
| 692 | missing.textContent = `${cond.questId} (missing)`; |
| 693 | missing.selected = true; |
| 694 | questSelect.append(missing); |
| 695 | } |
| 696 | |
| 697 | const stateSelect = document.createElement("select"); |
| 698 | const fillStates = () => |
| 699 | { |
| 700 | const quest = questList.find((q) => q.questId === cond.questId) ?? null; |
| 701 | stateSelect.replaceChildren(); |
| 702 | const options = [ |
| 703 | { value: "not-started", label: "not-started" }, |
| 704 | { value: "started", label: "started" }, |
| 705 | ...((quest?.steps ?? []).map((step) => ({ |
| 706 | value: step.stepId, |
| 707 | label: questStepMenuLabel(step) |
| 708 | }))), |
| 709 | { value: "completed", label: "completed" } |
| 710 | ]; |
| 711 | let matched = false; |
| 712 | const current = cond.state ?? "not-started"; |
| 713 | let currentKey = current === "not-started" || current === "completed" || current === "started" |
| 714 | ? current |
| 715 | : String(current); |
| 716 | const asIndex = Number(currentKey); |
| 717 | if (/^\d+$/.test(currentKey) && quest?.steps?.[asIndex - 1]) |
| 718 | { currentKey = quest.steps[asIndex - 1].stepId; } |
| 719 | for (const entry of options) |
| 720 | { |
| 721 | const opt = document.createElement("option"); |
| 722 | opt.value = entry.value; |
| 723 | opt.textContent = entry.label; |
| 724 | if (entry.value === currentKey) |
| 725 | { |
| 726 | opt.selected = true; |
| 727 | matched = true; |
| 728 | } |
| 729 | stateSelect.append(opt); |
| 730 | } |
| 731 | if (!matched) |
| 732 | { |
| 733 | const fallback = document.createElement("option"); |
| 734 | fallback.value = String(cond.state ?? "not-started"); |
| 735 | fallback.textContent = String(cond.state ?? "not-started"); |
| 736 | fallback.selected = true; |
| 737 | stateSelect.append(fallback); |
| 738 | } |
| 739 | }; |
| 740 | fillStates(); |
| 741 | |
| 742 | questSelect.addEventListener("change", () => |
| 743 | { |
| 744 | cond.questId = questSelect.value; |
| 745 | cond.state = "not-started"; |
| 746 | fillStates(); |
| 747 | onChanged(); |
| 748 | }); |
| 749 | stateSelect.addEventListener("change", () => |
| 750 | { |
| 751 | cond.state = stateSelect.value; |
| 752 | onChanged(); |
| 753 | }); |
| 754 | wrap.append(questSelect, stateSelect); |
| 755 | return wrap; |
| 756 | } |
| 757 | |
| 758 | function questStepMenuLabel (step) |
| 759 | { |
| 760 | const id = String(step?.stepId ?? ""); |
| 761 | const title = String(step?.title || step?.label || "").trim(); |
| 762 | if (title && title !== id) |
| 763 | { return `${title} (${id})`; } |
| 764 | return id || "step"; |
| 765 | } |
| 766 | |
| 767 | /** Quest picker + step id for hasCompletedStep. */ |
| 768 | function hasCompletedStepConditionFields (cond, questList, onChanged) |
| 769 | { |
| 770 | const wrap = document.createElement("div"); |
| 771 | wrap.className = "quest-condition-pair"; |
| 772 | const questSelect = document.createElement("select"); |
| 773 | const blank = document.createElement("option"); |
| 774 | blank.value = ""; |
| 775 | blank.textContent = "Select quest…"; |
| 776 | questSelect.append(blank); |
| 777 | for (const quest of questList) |
| 778 | { |
| 779 | const opt = document.createElement("option"); |
| 780 | opt.value = quest.questId; |
| 781 | opt.textContent = quest.title ? `${quest.title} (${quest.questId})` : quest.questId; |
| 782 | if (quest.questId === cond.questId) |
| 783 | { opt.selected = true; } |
| 784 | questSelect.append(opt); |
| 785 | } |
| 786 | if (cond.questId && !questList.some((q) => q.questId === cond.questId)) |
| 787 | { |
| 788 | const missing = document.createElement("option"); |
| 789 | missing.value = cond.questId; |
| 790 | missing.textContent = `${cond.questId} (missing)`; |
| 791 | missing.selected = true; |
| 792 | questSelect.append(missing); |
| 793 | } |
| 794 | const stepSelect = document.createElement("select"); |
| 795 | const fillSteps = () => |
| 796 | { |
| 797 | const quest = questList.find((q) => q.questId === cond.questId) ?? null; |
| 798 | stepSelect.replaceChildren(); |
| 799 | const steps = quest?.steps ?? []; |
| 800 | if (!steps.length) |
| 801 | { |
| 802 | const empty = document.createElement("option"); |
| 803 | empty.value = cond.stepId ?? ""; |
| 804 | empty.textContent = cond.stepId || "No steps"; |
| 805 | empty.selected = true; |
| 806 | stepSelect.append(empty); |
| 807 | return; |
| 808 | } |
| 809 | let matched = false; |
| 810 | for (const step of steps) |
| 811 | { |
| 812 | const opt = document.createElement("option"); |
| 813 | opt.value = step.stepId; |
| 814 | opt.textContent = questStepMenuLabel(step); |
| 815 | if (step.stepId === cond.stepId) |
| 816 | { |
| 817 | opt.selected = true; |
| 818 | matched = true; |
| 819 | } |
| 820 | stepSelect.append(opt); |
| 821 | } |
| 822 | if (!matched) |
| 823 | { |
| 824 | if (cond.stepId) |
| 825 | { |
| 826 | const fallback = document.createElement("option"); |
| 827 | fallback.value = cond.stepId; |
| 828 | fallback.textContent = cond.stepId; |
| 829 | fallback.selected = true; |
| 830 | stepSelect.append(fallback); |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | cond.stepId = steps[0].stepId; |
| 835 | stepSelect.options[0].selected = true; |
| 836 | } |
| 837 | } |
| 838 | }; |
| 839 | fillSteps(); |
| 840 | questSelect.addEventListener("change", () => |
| 841 | { |
| 842 | cond.questId = questSelect.value; |
| 843 | const quest = questList.find((q) => q.questId === cond.questId); |
| 844 | cond.stepId = quest?.steps?.[0]?.stepId ?? ""; |
| 845 | fillSteps(); |
| 846 | onChanged(); |
| 847 | }); |
| 848 | stepSelect.addEventListener("change", () => |
| 849 | { |
| 850 | cond.stepId = stepSelect.value; |
| 851 | onChanged(); |
| 852 | }); |
| 853 | wrap.append(questSelect, stepSelect); |
| 854 | return wrap; |
| 855 | } |
| 856 | |
| 857 | /** Compact searchable item picker for hasItem conditions. */ |
| 858 | function itemConditionPicker (cond, onPicked) |
| 859 | { |
| 860 | const wrap = document.createElement("div"); |
| 861 | wrap.className = "quest-condition-item-picker"; |
| 862 | const pickBtn = document.createElement("button"); |
| 863 | pickBtn.type = "button"; |
| 864 | const refreshLabel = () => |
| 865 | { |
| 866 | const def = itemDefinition(cond.itemId); |
| 867 | pickBtn.textContent = def?.displayName |
| 868 | ?? (cond.itemId ? `Item #${cond.itemId}` : "Pick item…"); |
| 869 | pickBtn.title = cond.itemId |
| 870 | ? `${def?.key ?? "item"} · #${cond.itemId}` |
| 871 | : "Choose an item"; |
| 872 | }; |
| 873 | refreshLabel(); |
| 874 | const searchWrap = document.createElement("div"); |
| 875 | searchWrap.className = "dialogue-reward-item-search"; |
| 876 | searchWrap.hidden = true; |
| 877 | const searchInput = document.createElement("input"); |
| 878 | searchInput.type = "search"; |
| 879 | searchInput.placeholder = "Search items…"; |
| 880 | searchInput.autocomplete = "off"; |
| 881 | const searchResults = document.createElement("div"); |
| 882 | searchResults.className = "dialogue-reward-item-results"; |
| 883 | |
| 884 | const renderMatches = (query) => |
| 885 | { |
| 886 | searchResults.replaceChildren(); |
| 887 | const q = String(query ?? "").trim().toLowerCase(); |
| 888 | const matches = listAllItemPickerEntries().filter((item) => |
| 889 | { |
| 890 | if (!q) |
| 891 | { return true; } |
| 892 | return item.displayName.toLowerCase().includes(q) |
| 893 | || item.key.toLowerCase().includes(q) |
| 894 | || String(item.itemId).includes(q); |
| 895 | }).slice(0, 40); |
| 896 | if (!matches.length) |
| 897 | { |
| 898 | searchResults.textContent = "No matching items."; |
| 899 | return; |
| 900 | } |
| 901 | for (const item of matches) |
| 902 | { |
| 903 | const row = document.createElement("button"); |
| 904 | row.type = "button"; |
| 905 | row.className = "shop-item-search-row"; |
| 906 | const name = document.createElement("span"); |
| 907 | name.className = "shop-item-search-row-name"; |
| 908 | name.textContent = item.displayName; |
| 909 | const key = document.createElement("span"); |
| 910 | key.className = "shop-item-search-row-key"; |
| 911 | key.textContent = `${item.key} · #${item.itemId}`; |
| 912 | row.append(name, key); |
| 913 | row.addEventListener("click", () => |
| 914 | { |
| 915 | cond.itemId = item.itemId; |
| 916 | refreshLabel(); |
| 917 | searchWrap.hidden = true; |
| 918 | onPicked(); |
| 919 | }); |
| 920 | searchResults.append(row); |
| 921 | } |
| 922 | }; |
| 923 | |
| 924 | pickBtn.addEventListener("click", () => |
| 925 | { |
| 926 | searchWrap.hidden = !searchWrap.hidden; |
| 927 | if (!searchWrap.hidden) |
| 928 | { |
| 929 | searchInput.value = ""; |
| 930 | renderMatches(""); |
| 931 | searchInput.focus(); |
| 932 | } |
| 933 | }); |
| 934 | searchInput.addEventListener("input", () => renderMatches(searchInput.value)); |
| 935 | searchWrap.append(searchInput, searchResults); |
| 936 | wrap.append(pickBtn, searchWrap); |
| 937 | return wrap; |
| 938 | } |
| 939 | |
| 940 | function miniField (label, value, onChange) |
| 941 | { |
| 942 | const wrap = document.createElement("label"); |
| 943 | wrap.textContent = `${label} `; |
| 944 | const input = document.createElement("input"); |
| 945 | input.type = "text"; |
| 946 | input.value = String(value ?? ""); |
| 947 | input.style.width = "7rem"; |
| 948 | input.addEventListener("input", () => onChange(input.value)); |
| 949 | wrap.append(input); |
| 950 | return wrap; |
| 951 | } |
| 952 | |
| 953 | function defaultCondition (kind) |
| 954 | { |
| 955 | switch (kind) |
| 956 | { |
| 957 | case ConditionKind.hasItem: |
| 958 | return { kind, itemId: 1, quantity: 1 }; |
| 959 | case ConditionKind.questState: |
| 960 | return { kind, questId: "", state: "not-started" }; |
| 961 | case ConditionKind.dialogueHeard: |
| 962 | return { kind, dialogueId: "", stepId: "" }; |
| 963 | case ConditionKind.dialogueTrigger: |
| 964 | return { kind, dialogueId: "", trigger: "" }; |
| 965 | case ConditionKind.killCount: |
| 966 | return { kind, mobTypeId: "deer", count: 1 }; |
| 967 | case ConditionKind.killMobFromSpawner: |
| 968 | return { kind, mobTypeId: "deer", spawnerId: "", count: 1 }; |
| 969 | case ConditionKind.atTile: |
| 970 | case ConditionKind.nearTile: |
| 971 | return { kind, dimensionId: OVERWORLD_ID, tileX: 0, tileY: 0, ...(kind === ConditionKind.nearTile ? { range: 1 } : {}) }; |
| 972 | case ConditionKind.inRect: |
| 973 | return { kind, dimensionId: OVERWORLD_ID, minTileX: 0, minTileY: 0, maxTileX: 0, maxTileY: 0 }; |
| 974 | case ConditionKind.atChunk: |
| 975 | return { kind, dimensionId: OVERWORLD_ID, chunkX: 0, chunkY: 0 }; |
| 976 | case ConditionKind.inDimension: |
| 977 | case ConditionKind.hasVisitedDimension: |
| 978 | return { kind, dimensionId: OVERWORLD_ID }; |
| 979 | case ConditionKind.hasCompletedStep: |
| 980 | return { kind, questId: "", stepId: "" }; |
| 981 | case ConditionKind.hpAtLeast: |
| 982 | case ConditionKind.staminaAtLeast: |
| 983 | return { kind, amount: 1 }; |
| 984 | case ConditionKind.hpPercentAtLeast: |
| 985 | case ConditionKind.staminaPercentAtLeast: |
| 986 | return { kind, percent: 50 }; |
| 987 | case ConditionKind.skillLevelAtLeast: |
| 988 | return { kind, skillId: MetaSkillId.combatLevel, level: 1 }; |
| 989 | case ConditionKind.interactBlock: |
| 990 | return { kind, dimensionId: OVERWORLD_ID, tileX: 0, tileY: 0 }; |
| 991 | default: |
| 992 | return { kind }; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | function skillRequirementFields (cond, onChange) |
| 997 | { |
| 998 | const wrap = document.createElement("span"); |
| 999 | wrap.style.display = "inline-flex"; |
| 1000 | wrap.style.gap = "0.35rem"; |
| 1001 | wrap.style.alignItems = "center"; |
| 1002 | wrap.style.flexWrap = "wrap"; |
| 1003 | |
| 1004 | const skillLabel = document.createElement("label"); |
| 1005 | skillLabel.textContent = "skill "; |
| 1006 | const select = document.createElement("select"); |
| 1007 | const options = listSkillRequirementOptions(); |
| 1008 | const current = String(cond.skillId ?? MetaSkillId.combatLevel); |
| 1009 | let found = false; |
| 1010 | for (const entry of options) |
| 1011 | { |
| 1012 | const opt = document.createElement("option"); |
| 1013 | opt.value = entry.skillId; |
| 1014 | opt.textContent = entry.displayName; |
| 1015 | if (entry.skillId === current) |
| 1016 | { |
| 1017 | opt.selected = true; |
| 1018 | found = true; |
| 1019 | } |
| 1020 | select.append(opt); |
| 1021 | } |
| 1022 | if (!found && current) |
| 1023 | { |
| 1024 | const opt = document.createElement("option"); |
| 1025 | opt.value = current; |
| 1026 | opt.textContent = current; |
| 1027 | opt.selected = true; |
| 1028 | select.append(opt); |
| 1029 | } |
| 1030 | select.addEventListener("change", () => |
| 1031 | { |
| 1032 | cond.skillId = select.value; |
| 1033 | const max = isMetaSkillId(cond.skillId) ? 99_999 : 99; |
| 1034 | cond.level = Math.max(1, Math.min(max, Number(cond.level) || 1)); |
| 1035 | levelInput.max = String(max); |
| 1036 | levelInput.value = String(cond.level); |
| 1037 | onChange(); |
| 1038 | }); |
| 1039 | skillLabel.append(select); |
| 1040 | |
| 1041 | const levelLabel = document.createElement("label"); |
| 1042 | levelLabel.textContent = "level "; |
| 1043 | const levelInput = document.createElement("input"); |
| 1044 | levelInput.type = "number"; |
| 1045 | levelInput.min = "1"; |
| 1046 | const maxLevel = isMetaSkillId(cond.skillId) ? 99_999 : 99; |
| 1047 | levelInput.max = String(maxLevel); |
| 1048 | levelInput.value = String(cond.level ?? 1); |
| 1049 | levelInput.style.width = "5rem"; |
| 1050 | levelInput.addEventListener("input", () => |
| 1051 | { |
| 1052 | const max = isMetaSkillId(cond.skillId) ? 99_999 : 99; |
| 1053 | cond.level = Math.max(1, Math.min(max, Number(levelInput.value) || 1)); |
| 1054 | onChange(); |
| 1055 | }); |
| 1056 | levelLabel.append(levelInput); |
| 1057 | |
| 1058 | wrap.append(skillLabel, levelLabel); |
| 1059 | return wrap; |
| 1060 | } |