import { ConditionKind, ConditionSetMode, CONDITION_SET_MODE_LABELS, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js"; import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; import { listSpawnableMobTypes } from "/shared/definitions/mobs.js"; import { isMetaSkillId, listSkillRequirementOptions, MetaSkillId } from "/shared/definitions/skills.js"; import { createDimensionCombobox } from "./dimensionCombobox.js"; import { OVERWORLD_ID } from "/shared/world/dimensions.js"; /** All condition kinds available in the shared editor UI. */ export const CONDITION_EDITOR_KINDS = Object.freeze(Object.values(ConditionKind)); export { emptyConditionSet, normalizeConditionSet, ConditionKind, ConditionSetMode, CONDITION_SET_MODE_LABELS }; export function renderConditionSet (when, onChange, ctx = {}) { const wrap = document.createElement("div"); const set = normalizeConditionSet(when); const bump = (remount = true) => onChange(set, { remount }); const questList = Array.isArray(ctx.quests) ? ctx.quests : []; const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : []; const header = document.createElement("div"); header.className = "content-editor-condition-set-header"; const title = document.createElement("strong"); title.textContent = "When"; const mode = document.createElement("select"); mode.className = "content-editor-condition-set-mode"; mode.title = "How clauses in this set combine"; for (const value of Object.values(ConditionSetMode)) { const opt = document.createElement("option"); opt.value = value; opt.textContent = CONDITION_SET_MODE_LABELS[value] ?? value; if (set.mode === value) { opt.selected = true; } mode.append(opt); } mode.addEventListener("change", () => { set.mode = mode.value; bump(false); }); header.append(title, mode); wrap.append(header); set.all.forEach((clause, index) => { const row = document.createElement("div"); row.className = "content-editor-clause"; const notLabel = document.createElement("label"); const notBox = document.createElement("input"); notBox.type = "checkbox"; notBox.checked = !!clause.not; notBox.addEventListener("change", () => { set.all[index].not = notBox.checked; bump(false); }); notLabel.append(notBox, document.createTextNode(" NOT")); row.append(notLabel); const kind = document.createElement("select"); for (const k of CONDITION_EDITOR_KINDS) { const opt = document.createElement("option"); opt.value = k; opt.textContent = k; if ((clause.condition?.kind ?? "") === k) { opt.selected = true; } kind.append(opt); } const otherOpt = document.createElement("option"); otherOpt.value = "__other__"; otherOpt.textContent = "(other / JSON)"; if (!CONDITION_EDITOR_KINDS.includes(clause.condition?.kind)) { otherOpt.selected = true; } kind.append(otherOpt); kind.addEventListener("change", () => { if (kind.value === "__other__") { return; } set.all[index].condition = defaultCondition(kind.value); bump(true); }); row.append(kind); const cond = clause.condition ?? { kind: ConditionKind.hasItem }; if (cond.kind === ConditionKind.hasItem) { row.append(itemConditionPicker(cond, () => { set.all[index].condition = cond; bump(false); })); row.append(miniField("qty", cond.quantity ?? 1, (v) => { cond.quantity = Number(v) || 1; set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.questState) { row.append(questStateConditionFields(cond, questList, () => { set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.dialogueHeard) { row.append(dialogueHeardConditionFields(cond, dialogueList, () => { set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.killCount) { row.append(killCountConditionFields(cond, () => { set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.atTile) { row.append(atTileConditionFields(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); } else if ( cond.kind === ConditionKind.inDimension || cond.kind === ConditionKind.hasVisitedDimension ) { row.append(dimensionConditionField(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.nearTile) { row.append(atTileConditionFields(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); row.append(miniField("range", cond.range ?? 1, (v) => { cond.range = Math.max(1, Number(v) || 1); set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.inRect) { row.append(dimensionConditionField(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); for (const key of ["minTileX", "minTileY", "maxTileX", "maxTileY"]) { row.append(miniField(key, cond[key] ?? 0, (v) => { cond[key] = Number(v) | 0; set.all[index].condition = cond; bump(false); })); } } else if (cond.kind === ConditionKind.atChunk) { row.append(dimensionConditionField(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); row.append(miniField("chunkX", cond.chunkX ?? 0, (v) => { cond.chunkX = Number(v) | 0; set.all[index].condition = cond; bump(false); })); row.append(miniField("chunkY", cond.chunkY ?? 0, (v) => { cond.chunkY = Number(v) | 0; set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.questStepAtLeast) { row.append(miniField("questId", cond.questId ?? "", (v) => { cond.questId = v; set.all[index].condition = cond; bump(false); })); row.append(miniField("step", cond.step ?? 1, (v) => { cond.step = Math.max(1, Number(v) || 1); set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.dialogueTrigger) { row.append(miniField("dialogueId", cond.dialogueId ?? "", (v) => { cond.dialogueId = v; set.all[index].condition = cond; bump(false); })); row.append(miniField("trigger", cond.trigger ?? "", (v) => { cond.trigger = v; set.all[index].condition = cond; bump(false); })); } else if ( cond.kind === ConditionKind.hpAtLeast || cond.kind === ConditionKind.staminaAtLeast ) { row.append(miniField("amount", cond.amount ?? 1, (v) => { cond.amount = Math.max(0, Number(v) || 0); set.all[index].condition = cond; bump(false); })); } else if ( cond.kind === ConditionKind.hpPercentAtLeast || cond.kind === ConditionKind.staminaPercentAtLeast ) { row.append(miniField("percent", cond.percent ?? 50, (v) => { cond.percent = Math.min(100, Math.max(0, Number(v) || 0)); set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.skillLevelAtLeast) { row.append(skillRequirementFields(cond, () => { set.all[index].condition = cond; bump(false); })); } else if (cond.kind === ConditionKind.interactBlock) { row.append(atTileConditionFields(cond, ctx, () => { set.all[index].condition = cond; bump(false); })); row.append(miniField("blockTypeId", cond.blockTypeId ?? "", (v) => { cond.blockTypeId = v === "" ? undefined : (Number(v) || v); set.all[index].condition = cond; bump(false); })); } else { const json = document.createElement("textarea"); json.rows = 3; json.value = JSON.stringify(cond, null, 2); json.addEventListener("change", () => { try { set.all[index].condition = JSON.parse(json.value); bump(false); } catch { /* keep typing */ } }); row.append(json); } const remove = document.createElement("button"); remove.type = "button"; remove.textContent = "×"; remove.addEventListener("click", () => { set.all.splice(index, 1); bump(true); }); row.append(remove); wrap.append(row); }); const add = document.createElement("button"); add.type = "button"; add.textContent = "Add clause"; add.addEventListener("click", () => { set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) }); bump(true); }); wrap.append(add); const jsonFallback = document.createElement("details"); const summary = document.createElement("summary"); summary.textContent = "Edit condition set as JSON"; const area = document.createElement("textarea"); area.rows = 5; area.value = JSON.stringify(set, null, 2); area.addEventListener("change", () => { try { onChange(normalizeConditionSet(JSON.parse(area.value)), { remount: true }); } catch { /* ignore */ } }); jsonFallback.append(summary, area); wrap.append(jsonFallback); return wrap; } /** X/Y inputs, tile summary, and center-editor control for atTile. */ function atTileConditionFields (cond, ctx, onChanged) { const wrap = document.createElement("div"); wrap.className = "quest-condition-attile"; const dimField = dimensionConditionField(cond, ctx, () => { refreshInfo(); onChanged(); }); const xInput = document.createElement("input"); xInput.type = "number"; xInput.step = "1"; xInput.style.width = "5rem"; xInput.value = String(cond.tileX ?? 0); xInput.title = "Tile X"; const yInput = document.createElement("input"); yInput.type = "number"; yInput.step = "1"; yInput.style.width = "5rem"; yInput.value = String(cond.tileY ?? 0); yInput.title = "Tile Y"; const xyRow = document.createElement("div"); xyRow.className = "quest-condition-pair"; const xLabel = document.createElement("label"); xLabel.append(document.createTextNode("X "), xInput); const yLabel = document.createElement("label"); yLabel.append(document.createTextNode("Y "), yInput); const centerBtn = document.createElement("button"); centerBtn.type = "button"; centerBtn.textContent = "Center editor"; centerBtn.title = "Pan the world editor to this tile (keeps this UI open)"; const info = document.createElement("pre"); info.className = "quest-condition-tile-info"; const refreshInfo = () => { if (typeof ctx.describeWorldTile === "function") { info.textContent = ctx.describeWorldTile( cond.tileX | 0, cond.tileY | 0, cond.dimensionId ); } else { info.textContent = "Tile inspection unavailable."; } }; xInput.addEventListener("input", () => { cond.tileX = Number(xInput.value) | 0; refreshInfo(); onChanged(); }); yInput.addEventListener("input", () => { cond.tileY = Number(yInput.value) | 0; refreshInfo(); onChanged(); }); centerBtn.addEventListener("click", () => { ctx.centerEditorOnTile?.(cond.tileX | 0, cond.tileY | 0, cond.dimensionId); refreshInfo(); }); xyRow.append(xLabel, yLabel, centerBtn); wrap.append(dimField, xyRow, info); refreshInfo(); return wrap; } /** Searchable dimension combobox bound to condition.dimensionId. */ function dimensionConditionField (cond, ctx, onChanged) { const root = document.createElement("div"); root.className = "quest-condition-dim"; const combo = createDimensionCombobox({ root, allowCreate: false, placeholder: "Dimension…", onSelect (id) { cond.dimensionId = id || OVERWORLD_ID; onChanged(); } }); const options = typeof ctx.getDimensionOptions === "function" ? ctx.getDimensionOptions() : [{ id: OVERWORLD_ID, label: "Overworld" }]; combo.setOptions(options); combo.setValue(cond.dimensionId || OVERWORLD_ID); return root; } /** Mob selector + kill quantity for killCount conditions. */ function killCountConditionFields (cond, onChanged) { const wrap = document.createElement("div"); wrap.className = "quest-condition-pair"; const mobSelect = document.createElement("select"); for (const def of listSpawnableMobTypes()) { const opt = document.createElement("option"); opt.value = def.mobTypeId; opt.textContent = def.displayName ?? def.mobTypeId; if (def.mobTypeId === cond.mobTypeId) { opt.selected = true; } mobSelect.append(opt); } if (cond.mobTypeId && ![...mobSelect.options].some((o) => o.value === cond.mobTypeId)) { const missing = document.createElement("option"); missing.value = cond.mobTypeId; missing.textContent = `${cond.mobTypeId} (missing)`; missing.selected = true; mobSelect.append(missing); } if (!cond.mobTypeId && mobSelect.options.length) { cond.mobTypeId = mobSelect.options[0].value; mobSelect.options[0].selected = true; } const qty = document.createElement("input"); qty.type = "number"; qty.min = "1"; qty.step = "1"; qty.style.width = "5rem"; qty.value = String(Math.max(1, Number(cond.count) || 1)); qty.title = "Kill count"; mobSelect.addEventListener("change", () => { cond.mobTypeId = mobSelect.value || undefined; onChanged(); }); qty.addEventListener("input", () => { cond.count = Math.max(1, Number(qty.value) || 1); onChanged(); }); wrap.append(mobSelect, qty); return wrap; } /** Dialogue picker + step-id dropdown from that tree. */ function dialogueHeardConditionFields (cond, dialogueList, onChanged) { const wrap = document.createElement("div"); wrap.className = "quest-condition-pair"; const dialogueSelect = document.createElement("select"); const blank = document.createElement("option"); blank.value = ""; blank.textContent = "Select dialogue…"; dialogueSelect.append(blank); for (const tree of dialogueList) { const opt = document.createElement("option"); opt.value = tree.dialogueId; opt.textContent = tree.label ? `${tree.label} (${tree.dialogueId})` : tree.dialogueId; if (tree.dialogueId === cond.dialogueId) { opt.selected = true; } dialogueSelect.append(opt); } if (cond.dialogueId && !dialogueList.some((d) => d.dialogueId === cond.dialogueId)) { const missing = document.createElement("option"); missing.value = cond.dialogueId; missing.textContent = `${cond.dialogueId} (missing)`; missing.selected = true; dialogueSelect.append(missing); } const stepSelect = document.createElement("select"); const fillSteps = () => { const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId) ?? null; stepSelect.replaceChildren(); const steps = tree?.steps ?? []; if (!steps.length) { const empty = document.createElement("option"); empty.value = cond.stepId ?? ""; empty.textContent = cond.stepId ? String(cond.stepId) : "No steps"; empty.selected = true; stepSelect.append(empty); return; } let matched = false; for (const step of steps) { const opt = document.createElement("option"); opt.value = step.stepId; opt.textContent = step.label ? `${step.label} (${step.stepId})` : step.stepId; if (step.stepId === cond.stepId) { opt.selected = true; matched = true; } stepSelect.append(opt); } if (!matched && cond.stepId) { const fallback = document.createElement("option"); fallback.value = cond.stepId; fallback.textContent = `${cond.stepId} (missing)`; fallback.selected = true; stepSelect.append(fallback); } else if (!matched) { cond.stepId = steps[0].stepId; stepSelect.options[0].selected = true; } }; fillSteps(); dialogueSelect.addEventListener("change", () => { cond.dialogueId = dialogueSelect.value; const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId); cond.stepId = tree?.steps?.[0]?.stepId ?? ""; fillSteps(); onChanged(); }); stepSelect.addEventListener("change", () => { cond.stepId = stepSelect.value; onChanged(); }); wrap.append(dialogueSelect, stepSelect); return wrap; } /** Quest picker + state dropdown from that quest's steps. */ function questStateConditionFields (cond, questList, onChanged) { const wrap = document.createElement("div"); wrap.className = "quest-condition-pair"; const questSelect = document.createElement("select"); const blank = document.createElement("option"); blank.value = ""; blank.textContent = "Select quest…"; questSelect.append(blank); for (const quest of questList) { const opt = document.createElement("option"); opt.value = quest.questId; opt.textContent = quest.title ? `${quest.title} (${quest.questId})` : quest.questId; if (quest.questId === cond.questId) { opt.selected = true; } questSelect.append(opt); } if (cond.questId && !questList.some((q) => q.questId === cond.questId)) { const missing = document.createElement("option"); missing.value = cond.questId; missing.textContent = `${cond.questId} (missing)`; missing.selected = true; questSelect.append(missing); } const stateSelect = document.createElement("select"); const fillStates = () => { const quest = questList.find((q) => q.questId === cond.questId) ?? null; stateSelect.replaceChildren(); const options = [ { value: "not-started", label: "not-started" }, ...((quest?.steps ?? []).map((step, index) => ({ value: String(index + 1), label: `Step ${index + 1}: ${step.title || step.stepId}` }))), { value: "completed", label: "completed" } ]; let matched = false; for (const entry of options) { const opt = document.createElement("option"); opt.value = entry.value; opt.textContent = entry.label; const current = cond.state ?? "not-started"; const currentKey = current === "not-started" || current === "completed" ? current : String(Number(current) || current); if (entry.value === currentKey) { opt.selected = true; matched = true; } stateSelect.append(opt); } if (!matched) { const fallback = document.createElement("option"); fallback.value = String(cond.state ?? "not-started"); fallback.textContent = String(cond.state ?? "not-started"); fallback.selected = true; stateSelect.append(fallback); } }; fillStates(); questSelect.addEventListener("change", () => { cond.questId = questSelect.value; cond.state = "not-started"; fillStates(); onChanged(); }); stateSelect.addEventListener("change", () => { const raw = stateSelect.value; if (raw === "not-started" || raw === "completed") { cond.state = raw; } else { cond.state = Math.max(1, Number(raw) | 0); } onChanged(); }); wrap.append(questSelect, stateSelect); return wrap; } /** Compact searchable item picker for hasItem conditions. */ function itemConditionPicker (cond, onPicked) { const wrap = document.createElement("div"); wrap.className = "quest-condition-item-picker"; const pickBtn = document.createElement("button"); pickBtn.type = "button"; const refreshLabel = () => { const def = itemDefinition(cond.itemId); pickBtn.textContent = def?.displayName ?? (cond.itemId ? `Item #${cond.itemId}` : "Pick item…"); pickBtn.title = cond.itemId ? `${def?.key ?? "item"} · #${cond.itemId}` : "Choose an item"; }; refreshLabel(); const searchWrap = document.createElement("div"); searchWrap.className = "dialogue-reward-item-search"; searchWrap.hidden = true; const searchInput = document.createElement("input"); searchInput.type = "search"; searchInput.placeholder = "Search items…"; searchInput.autocomplete = "off"; const searchResults = document.createElement("div"); searchResults.className = "dialogue-reward-item-results"; const renderMatches = (query) => { searchResults.replaceChildren(); const q = String(query ?? "").trim().toLowerCase(); const matches = listAllItemPickerEntries().filter((item) => { if (!q) { return true; } return item.displayName.toLowerCase().includes(q) || item.key.toLowerCase().includes(q) || String(item.itemId).includes(q); }).slice(0, 40); if (!matches.length) { searchResults.textContent = "No matching items."; return; } for (const item of matches) { const row = document.createElement("button"); row.type = "button"; row.className = "shop-item-search-row"; const name = document.createElement("span"); name.className = "shop-item-search-row-name"; name.textContent = item.displayName; const key = document.createElement("span"); key.className = "shop-item-search-row-key"; key.textContent = `${item.key} · #${item.itemId}`; row.append(name, key); row.addEventListener("click", () => { cond.itemId = item.itemId; refreshLabel(); searchWrap.hidden = true; onPicked(); }); searchResults.append(row); } }; pickBtn.addEventListener("click", () => { searchWrap.hidden = !searchWrap.hidden; if (!searchWrap.hidden) { searchInput.value = ""; renderMatches(""); searchInput.focus(); } }); searchInput.addEventListener("input", () => renderMatches(searchInput.value)); searchWrap.append(searchInput, searchResults); wrap.append(pickBtn, searchWrap); return wrap; } function miniField (label, value, onChange) { const wrap = document.createElement("label"); wrap.textContent = `${label} `; const input = document.createElement("input"); input.type = "text"; input.value = String(value ?? ""); input.style.width = "7rem"; input.addEventListener("input", () => onChange(input.value)); wrap.append(input); return wrap; } function defaultCondition (kind) { switch (kind) { case ConditionKind.hasItem: return { kind, itemId: 1, quantity: 1 }; case ConditionKind.questState: return { kind, questId: "", state: "not-started" }; case ConditionKind.dialogueHeard: return { kind, dialogueId: "", stepId: "" }; case ConditionKind.dialogueTrigger: return { kind, dialogueId: "", trigger: "" }; case ConditionKind.killCount: return { kind, mobTypeId: "deer", count: 1 }; case ConditionKind.atTile: case ConditionKind.nearTile: return { kind, dimensionId: OVERWORLD_ID, tileX: 0, tileY: 0, ...(kind === ConditionKind.nearTile ? { range: 1 } : {}) }; case ConditionKind.inRect: return { kind, dimensionId: OVERWORLD_ID, minTileX: 0, minTileY: 0, maxTileX: 0, maxTileY: 0 }; case ConditionKind.atChunk: return { kind, dimensionId: OVERWORLD_ID, chunkX: 0, chunkY: 0 }; case ConditionKind.inDimension: case ConditionKind.hasVisitedDimension: return { kind, dimensionId: OVERWORLD_ID }; case ConditionKind.questStepAtLeast: return { kind, questId: "", step: 1 }; case ConditionKind.hpAtLeast: case ConditionKind.staminaAtLeast: return { kind, amount: 1 }; case ConditionKind.hpPercentAtLeast: case ConditionKind.staminaPercentAtLeast: return { kind, percent: 50 }; case ConditionKind.skillLevelAtLeast: return { kind, skillId: MetaSkillId.combatLevel, level: 1 }; case ConditionKind.interactBlock: return { kind, dimensionId: OVERWORLD_ID, tileX: 0, tileY: 0 }; default: return { kind }; } } function skillRequirementFields (cond, onChange) { const wrap = document.createElement("span"); wrap.style.display = "inline-flex"; wrap.style.gap = "0.35rem"; wrap.style.alignItems = "center"; wrap.style.flexWrap = "wrap"; const skillLabel = document.createElement("label"); skillLabel.textContent = "skill "; const select = document.createElement("select"); const options = listSkillRequirementOptions(); const current = String(cond.skillId ?? MetaSkillId.combatLevel); let found = false; for (const entry of options) { const opt = document.createElement("option"); opt.value = entry.skillId; opt.textContent = entry.displayName; if (entry.skillId === current) { opt.selected = true; found = true; } select.append(opt); } if (!found && current) { const opt = document.createElement("option"); opt.value = current; opt.textContent = current; opt.selected = true; select.append(opt); } select.addEventListener("change", () => { cond.skillId = select.value; const max = isMetaSkillId(cond.skillId) ? 99_999 : 99; cond.level = Math.max(1, Math.min(max, Number(cond.level) || 1)); levelInput.max = String(max); levelInput.value = String(cond.level); onChange(); }); skillLabel.append(select); const levelLabel = document.createElement("label"); levelLabel.textContent = "level "; const levelInput = document.createElement("input"); levelInput.type = "number"; levelInput.min = "1"; const maxLevel = isMetaSkillId(cond.skillId) ? 99_999 : 99; levelInput.max = String(maxLevel); levelInput.value = String(cond.level ?? 1); levelInput.style.width = "5rem"; levelInput.addEventListener("input", () => { const max = isMetaSkillId(cond.skillId) ? 99_999 : 99; cond.level = Math.max(1, Math.min(max, Number(levelInput.value) || 1)); onChange(); }); levelLabel.append(levelInput); wrap.append(skillLabel, levelLabel); return wrap; }