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