BlueGit

timfa questward

Files

questward / client / src / editor / conditionSetEditor.js

conditionSetEditor.js

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