BlueGit

timfa questward

Share condition-set editor between quests and dialogues.

Move the full condition UI into a reusable module covering every ConditionKind, and use it for dialogue triggers and branches.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

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