Add dialogue item takes with soft bag removal and path warnings.
Steps can remove items after lines (taking whatever the player has). The editor highlights takes yellow when branch/trigger conditions do not guarantee those items on every path.
@@ -6,7 +6,8 @@ import { itemDefinition } from "/shared/definitions/items.js";
6
6
import { evaluateConditionSet } from "/shared/quests/conditions.js";
7
7
import {
8
8
formatDialogueRewardMessage,
9
formatDialogueQuestRewardMessage
9
formatDialogueQuestRewardMessage,
10
formatDialogueTakeMessage
10
11
} from "/shared/quests/dialogueRewards.js";
11
12
12
13
const ICON = 48;
@@ -101,6 +102,18 @@ export function initDialogueSimulate ({ getTree, getQuestTitle = null } = {})
101
102
function rewardMessagesForStep (step)
102
103
{
103
104
const messages = [];
105
const takeParts = [];
106
for (const take of step?.itemTakes ?? [])
107
{
108
const def = itemDefinition(take.itemId);
109
takeParts.push({
110
displayName: def?.displayName ?? `Item ${take.itemId}`,
111
quantity: Math.max(1, take.quantity | 0 || 1)
112
});
113
}
114
const takeMsg = formatDialogueTakeMessage(takeParts);
115
if (takeMsg)
116
{ messages.push(takeMsg); }
104
117
const bagParts = [];
105
118
for (const reward of step?.itemRewards ?? [])
106
119
{
@@ -6,7 +6,8 @@ import { itemDefinition } from "/shared/definitions/items.js";6
import { evaluateConditionSet } from "/shared/quests/conditions.js";6
import { evaluateConditionSet } from "/shared/quests/conditions.js";7
import {7
import {8
formatDialogueRewardMessage,8
formatDialogueRewardMessage,9
formatDialogueQuestRewardMessage9
formatDialogueQuestRewardMessage,10
formatDialogueTakeMessage10
} from "/shared/quests/dialogueRewards.js";11
} from "/shared/quests/dialogueRewards.js";11
12
12
const ICON = 48;13
const ICON = 48;@@ -101,6 +102,18 @@ export function initDialogueSimulate ({ getTree, getQuestTitle = null } = {})101
function rewardMessagesForStep (step)102
function rewardMessagesForStep (step)102
{103
{103
const messages = [];104
const messages = [];105
const takeParts = [];106
for (const take of step?.itemTakes ?? [])107
{108
const def = itemDefinition(take.itemId);109
takeParts.push({110
displayName: def?.displayName ?? `Item ${take.itemId}`,111
quantity: Math.max(1, take.quantity | 0 || 1)112
});113
}114
const takeMsg = formatDialogueTakeMessage(takeParts);115
if (takeMsg)116
{ messages.push(takeMsg); }104
const bagParts = [];117
const bagParts = [];105
for (const reward of step?.itemRewards ?? [])118
for (const reward of step?.itemRewards ?? [])106
{119
{@@ -1,5 +1,9 @@
1
1
import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js";
2
2
import { emptyConditionSet } from "/shared/quests/conditions.js";
3
import {
4
dialogueTakeIsUnguaranteed,
5
unguaranteedDialogueTakes
6
} from "/shared/quests/dialoguePathTakes.js";
3
7
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
4
8
import { renderConditionSet } from "./conditionSetEditor.js";
5
9
import { initDialogueSimulate } from "./dialogueSimulate.js";
@@ -51,7 +55,12 @@ export function initDialoguesModal ({
51
55
}
52
56
}
53
57
54
/** @type {null | { step: object, resultsEl: HTMLElement, inputEl: HTMLInputElement }} */
58
/** @type {null | {
59
* step: object,
60
* field: "itemRewards" | "itemTakes",
61
* resultsEl: HTMLElement,
62
* inputEl: HTMLInputElement
63
* }} */
55
64
let itemPickContext = null;
56
65
57
66
function closeInlineItemSearch ()
@@ -61,9 +70,9 @@ export function initDialoguesModal ({
61
70
itemPickContext = null;
62
71
}
63
72
64
function openInlineItemSearch (step, inputEl, resultsEl)
73
function openInlineItemSearch (step, field, inputEl, resultsEl)
65
74
{
66
itemPickContext = { step, inputEl, resultsEl };
75
itemPickContext = { step, field, inputEl, resultsEl };
67
76
inputEl.value = "";
68
77
renderInlineItemSearch("");
69
78
inputEl.focus();
@@ -73,10 +82,10 @@ export function initDialoguesModal ({
73
82
{
74
83
if (!itemPickContext)
75
84
{ return; }
76
const { step, resultsEl } = itemPickContext;
85
const { step, field, resultsEl } = itemPickContext;
77
86
resultsEl.replaceChildren();
78
87
const q = String(query ?? "").trim().toLowerCase();
79
const taken = new Set((step.itemRewards ?? []).map((row) => row.itemId));
88
const taken = new Set((step[field] ?? []).map((row) => row.itemId));
80
89
const matches = listAllItemPickerEntries().filter((item) =>
81
90
{
82
91
if (taken.has(item.itemId))
@@ -106,9 +115,9 @@ export function initDialoguesModal ({
106
115
row.append(name, key);
107
116
row.addEventListener("click", () =>
108
117
{
109
if (!Array.isArray(step.itemRewards))
110
{ step.itemRewards = []; }
111
step.itemRewards.push({ itemId: item.itemId, quantity: 1 });
118
if (!Array.isArray(step[field]))
119
{ step[field] = []; }
120
step[field].push({ itemId: item.itemId, quantity: 1 });
112
121
closeInlineItemSearch();
113
122
renderEditor();
114
123
});
@@ -156,6 +165,7 @@ export function initDialoguesModal ({
156
165
stepId: `step-${draft.steps.length + 1}`,
157
166
lines: [{ speaker: "npc", text: "…", effects: [] }],
158
167
itemRewards: [],
168
itemTakes: [],
159
169
questRewards: [],
160
170
exit: { kind: "end" }
161
171
});
@@ -208,6 +218,8 @@ export function initDialoguesModal ({
208
218
stepsTitle.append(stepsStrong);
209
219
editorEl.append(stepsTitle);
210
220
221
const takeWarnings = unguaranteedDialogueTakes(draft);
222
211
223
draft.steps.forEach((step, stepIndex) =>
212
224
{
213
225
const box = document.createElement("div");
@@ -221,6 +233,7 @@ export function initDialoguesModal ({
221
233
stepId: "greeting",
222
234
lines: [{ speaker: "npc", text: "…", effects: [] }],
223
235
itemRewards: [],
236
itemTakes: [],
224
237
questRewards: [],
225
238
exit: { kind: "end" }
226
239
});
@@ -284,7 +297,7 @@ export function initDialoguesModal ({
284
297
const rewardsHead = sectionHeader("Item rewards", () =>
285
298
{
286
299
searchWrap.hidden = false;
287
openInlineItemSearch(step, searchInput, searchResults);
300
openInlineItemSearch(step, "itemRewards", searchInput, searchResults);
288
301
});
289
302
box.append(rewardsHead);
290
303
const rewardsHint = document.createElement("p");
@@ -325,6 +338,69 @@ export function initDialoguesModal ({
325
338
});
326
339
box.append(searchWrap);
327
340
341
if (!Array.isArray(step.itemTakes))
342
{ step.itemTakes = []; }
343
const takeSearchWrap = document.createElement("div");
344
takeSearchWrap.className = "dialogue-reward-item-search";
345
takeSearchWrap.hidden = true;
346
const takeSearchInput = document.createElement("input");
347
takeSearchInput.type = "search";
348
takeSearchInput.placeholder = "Search items…";
349
takeSearchInput.autocomplete = "off";
350
takeSearchInput.addEventListener("input", () => renderInlineItemSearch(takeSearchInput.value));
351
const takeSearchResults = document.createElement("div");
352
takeSearchResults.className = "dialogue-reward-item-results";
353
takeSearchWrap.append(takeSearchInput, takeSearchResults);
354
const takesHead = sectionHeader("Item takes", () =>
355
{
356
takeSearchWrap.hidden = false;
357
openInlineItemSearch(step, "itemTakes", takeSearchInput, takeSearchResults);
358
});
359
box.append(takesHead);
360
const takesHint = document.createElement("p");
361
takesHint.className = "content-editor-hint";
362
takesHint.textContent = "Removed from the bag after all lines (soft: takes what the player has). Yellow = branch/trigger path does not guarantee the player has them.";
363
box.append(takesHint);
364
if (!step.itemTakes.length)
365
{
366
const empty = document.createElement("p");
367
empty.className = "content-editor-hint";
368
empty.textContent = "No item takes.";
369
box.append(empty);
370
}
371
step.itemTakes.forEach((take, takeIndex) =>
372
{
373
const def = itemDefinition(take.itemId);
374
const row = document.createElement("div");
375
row.className = "spawner-drops-row";
376
if (dialogueTakeIsUnguaranteed(takeWarnings, step.stepId, take.itemId))
377
{
378
row.classList.add("dialogue-item-take-unguaranteed");
379
row.title = "Not guaranteed by every branch/trigger path to this step.";
380
}
381
const name = document.createElement("span");
382
name.className = "spawner-sells-name";
383
name.textContent = def?.displayName ?? `Item ${take.itemId}`;
384
const qty = document.createElement("input");
385
qty.type = "number";
386
qty.min = "1";
387
qty.step = "1";
388
qty.value = String(Math.max(1, take.quantity | 0 || 1));
389
qty.title = "Quantity";
390
qty.addEventListener("input", () =>
391
{
392
step.itemTakes[takeIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0;
393
renderEditor();
394
});
395
row.append(name, qty, removeButton(() =>
396
{
397
step.itemTakes.splice(takeIndex, 1);
398
renderEditor();
399
}));
400
box.append(row);
401
});
402
box.append(takeSearchWrap);
403
328
404
if (!Array.isArray(step.questRewards))
329
405
{ step.questRewards = []; }
330
406
const questRewardsHead = sectionHeader("Quest rewards", () =>
@@ -1,5 +1,9 @@1
import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js";1
import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js";2
import { emptyConditionSet } from "/shared/quests/conditions.js";2
import { emptyConditionSet } from "/shared/quests/conditions.js";3
import {4
dialogueTakeIsUnguaranteed,5
unguaranteedDialogueTakes6
} from "/shared/quests/dialoguePathTakes.js";3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";7
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";4
import { renderConditionSet } from "./conditionSetEditor.js";8
import { renderConditionSet } from "./conditionSetEditor.js";5
import { initDialogueSimulate } from "./dialogueSimulate.js";9
import { initDialogueSimulate } from "./dialogueSimulate.js";@@ -51,7 +55,12 @@ export function initDialoguesModal ({51
}55
}52
}56
}53
57
54
/** @type {null | { step: object, resultsEl: HTMLElement, inputEl: HTMLInputElement }} */58
/** @type {null | {59
* step: object,60
* field: "itemRewards" | "itemTakes",61
* resultsEl: HTMLElement,62
* inputEl: HTMLInputElement63
* }} */55
let itemPickContext = null;64
let itemPickContext = null;56
65
57
function closeInlineItemSearch ()66
function closeInlineItemSearch ()@@ -61,9 +70,9 @@ export function initDialoguesModal ({61
itemPickContext = null;70
itemPickContext = null;62
}71
}63
72
64
function openInlineItemSearch (step, inputEl, resultsEl)73
function openInlineItemSearch (step, field, inputEl, resultsEl)65
{74
{66
itemPickContext = { step, inputEl, resultsEl };75
itemPickContext = { step, field, inputEl, resultsEl };67
inputEl.value = "";76
inputEl.value = "";68
renderInlineItemSearch("");77
renderInlineItemSearch("");69
inputEl.focus();78
inputEl.focus();@@ -73,10 +82,10 @@ export function initDialoguesModal ({73
{82
{74
if (!itemPickContext)83
if (!itemPickContext)75
{ return; }84
{ return; }76
const { step, resultsEl } = itemPickContext;85
const { step, field, resultsEl } = itemPickContext;77
resultsEl.replaceChildren();86
resultsEl.replaceChildren();78
const q = String(query ?? "").trim().toLowerCase();87
const q = String(query ?? "").trim().toLowerCase();79
const taken = new Set((step.itemRewards ?? []).map((row) => row.itemId));88
const taken = new Set((step[field] ?? []).map((row) => row.itemId));80
const matches = listAllItemPickerEntries().filter((item) =>89
const matches = listAllItemPickerEntries().filter((item) =>81
{90
{82
if (taken.has(item.itemId))91
if (taken.has(item.itemId))@@ -106,9 +115,9 @@ export function initDialoguesModal ({106
row.append(name, key);115
row.append(name, key);107
row.addEventListener("click", () =>116
row.addEventListener("click", () =>108
{117
{109
if (!Array.isArray(step.itemRewards))118
if (!Array.isArray(step[field]))110
{ step.itemRewards = []; }119
{ step[field] = []; }111
step.itemRewards.push({ itemId: item.itemId, quantity: 1 });120
step[field].push({ itemId: item.itemId, quantity: 1 });112
closeInlineItemSearch();121
closeInlineItemSearch();113
renderEditor();122
renderEditor();114
});123
});@@ -156,6 +165,7 @@ export function initDialoguesModal ({156
stepId: `step-${draft.steps.length + 1}`,165
stepId: `step-${draft.steps.length + 1}`,157
lines: [{ speaker: "npc", text: "…", effects: [] }],166
lines: [{ speaker: "npc", text: "…", effects: [] }],158
itemRewards: [],167
itemRewards: [],168
itemTakes: [],159
questRewards: [],169
questRewards: [],160
exit: { kind: "end" }170
exit: { kind: "end" }161
});171
});@@ -208,6 +218,8 @@ export function initDialoguesModal ({208
stepsTitle.append(stepsStrong);218
stepsTitle.append(stepsStrong);209
editorEl.append(stepsTitle);219
editorEl.append(stepsTitle);210
220
221
const takeWarnings = unguaranteedDialogueTakes(draft);222
211
draft.steps.forEach((step, stepIndex) =>223
draft.steps.forEach((step, stepIndex) =>212
{224
{213
const box = document.createElement("div");225
const box = document.createElement("div");@@ -221,6 +233,7 @@ export function initDialoguesModal ({221
stepId: "greeting",233
stepId: "greeting",222
lines: [{ speaker: "npc", text: "…", effects: [] }],234
lines: [{ speaker: "npc", text: "…", effects: [] }],223
itemRewards: [],235
itemRewards: [],236
itemTakes: [],224
questRewards: [],237
questRewards: [],225
exit: { kind: "end" }238
exit: { kind: "end" }226
});239
});@@ -284,7 +297,7 @@ export function initDialoguesModal ({284
const rewardsHead = sectionHeader("Item rewards", () =>297
const rewardsHead = sectionHeader("Item rewards", () =>285
{298
{286
searchWrap.hidden = false;299
searchWrap.hidden = false;287
openInlineItemSearch(step, searchInput, searchResults);300
openInlineItemSearch(step, "itemRewards", searchInput, searchResults);288
});301
});289
box.append(rewardsHead);302
box.append(rewardsHead);290
const rewardsHint = document.createElement("p");303
const rewardsHint = document.createElement("p");@@ -325,6 +338,69 @@ export function initDialoguesModal ({325
});338
});326
box.append(searchWrap);339
box.append(searchWrap);327
340
341
if (!Array.isArray(step.itemTakes))342
{ step.itemTakes = []; }343
const takeSearchWrap = document.createElement("div");344
takeSearchWrap.className = "dialogue-reward-item-search";345
takeSearchWrap.hidden = true;346
const takeSearchInput = document.createElement("input");347
takeSearchInput.type = "search";348
takeSearchInput.placeholder = "Search items…";349
takeSearchInput.autocomplete = "off";350
takeSearchInput.addEventListener("input", () => renderInlineItemSearch(takeSearchInput.value));351
const takeSearchResults = document.createElement("div");352
takeSearchResults.className = "dialogue-reward-item-results";353
takeSearchWrap.append(takeSearchInput, takeSearchResults);354
const takesHead = sectionHeader("Item takes", () =>355
{356
takeSearchWrap.hidden = false;357
openInlineItemSearch(step, "itemTakes", takeSearchInput, takeSearchResults);358
});359
box.append(takesHead);360
const takesHint = document.createElement("p");361
takesHint.className = "content-editor-hint";362
takesHint.textContent = "Removed from the bag after all lines (soft: takes what the player has). Yellow = branch/trigger path does not guarantee the player has them.";363
box.append(takesHint);364
if (!step.itemTakes.length)365
{366
const empty = document.createElement("p");367
empty.className = "content-editor-hint";368
empty.textContent = "No item takes.";369
box.append(empty);370
}371
step.itemTakes.forEach((take, takeIndex) =>372
{373
const def = itemDefinition(take.itemId);374
const row = document.createElement("div");375
row.className = "spawner-drops-row";376
if (dialogueTakeIsUnguaranteed(takeWarnings, step.stepId, take.itemId))377
{378
row.classList.add("dialogue-item-take-unguaranteed");379
row.title = "Not guaranteed by every branch/trigger path to this step.";380
}381
const name = document.createElement("span");382
name.className = "spawner-sells-name";383
name.textContent = def?.displayName ?? `Item ${take.itemId}`;384
const qty = document.createElement("input");385
qty.type = "number";386
qty.min = "1";387
qty.step = "1";388
qty.value = String(Math.max(1, take.quantity | 0 || 1));389
qty.title = "Quantity";390
qty.addEventListener("input", () =>391
{392
step.itemTakes[takeIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0;393
renderEditor();394
});395
row.append(name, qty, removeButton(() =>396
{397
step.itemTakes.splice(takeIndex, 1);398
renderEditor();399
}));400
box.append(row);401
});402
box.append(takeSearchWrap);403
328
if (!Array.isArray(step.questRewards))404
if (!Array.isArray(step.questRewards))329
{ step.questRewards = []; }405
{ step.questRewards = []; }330
const questRewardsHead = sectionHeader("Quest rewards", () =>406
const questRewardsHead = sectionHeader("Quest rewards", () =>@@ -1717,3 +1717,14 @@ label {
1717
1717
border-radius: 4px;
1718
1718
background: #12151c;
1719
1719
}
1720
1721
.dialogue-item-take-unguaranteed {
1722
background: rgba(224, 168, 74, 0.18);
1723
border: 1px solid #e0a84a;
1724
border-radius: 4px;
1725
padding: 0.2rem 0.35rem;
1726
}
1727
1728
.dialogue-item-take-unguaranteed .spawner-sells-name {
1729
color: #e8c84a;
1730
}
@@ -1717,3 +1717,14 @@ label {1717
border-radius: 4px;1717
border-radius: 4px;1718
background: #12151c;1718
background: #12151c;1719
}1719
}1720
1721
.dialogue-item-take-unguaranteed {1722
background: rgba(224, 168, 74, 0.18);1723
border: 1px solid #e0a84a;1724
border-radius: 4px;1725
padding: 0.2rem 0.35rem;1726
}1727
1728
.dialogue-item-take-unguaranteed .spawner-sells-name {1729
color: #e8c84a;1730
}@@ -4,12 +4,16 @@ import {
4
4
countableObjectiveProgress,
5
5
objectiveProgressFingerprint
6
6
} from "../../../shared/quests/objectiveProgress.js";
7
import { countItemInSlots, insertStack, takeMatchingFromSlots, insertStackGrowable } from "../../../shared/items/inventory.js";
7
import { countItemInSlots, insertStack, takeMatchingFromSlots, takeMatchingUpToFromSlots, insertStackGrowable } from "../../../shared/items/inventory.js";
8
8
import { attachOrigins, godsOrigin } from "../../../shared/items/origin.js";
9
9
import { itemDefinition, itemDisplayName } from "../../../shared/definitions/items.js";
10
10
import { applySkillXp, publicSkillsPayload } from "../../../shared/skills/state.js";
11
11
import { skillLevel, SKILL_DEFINITIONS } from "../../../shared/definitions/skills.js";
12
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../../shared/quests/dialogueRewards.js";
12
import {
13
formatDialogueRewardMessage,
14
formatDialogueQuestRewardMessage,
15
formatDialogueTakeMessage
16
} from "../../../shared/quests/dialogueRewards.js";
13
17
import {
14
18
formatQuestBankRewardLetterBody,
15
19
QUESTWARD_GIFT_AUTHOR,
@@ -328,6 +332,7 @@ export class QuestRuntime
328
332
{
329
333
const step = tree.steps.find((s) => s.stepId === session.stepId);
330
334
const rewardMessages = [
335
...this.#takeStepItems(characterId, step, events),
331
336
...this.#grantStepItemRewards(characterId, step, events),
332
337
...this.#grantStepQuestRewards(characterId, step, playerView, events)
333
338
];
@@ -408,6 +413,40 @@ export class QuestRuntime
408
413
return { ok: true, events };
409
414
}
410
415
416
/**
417
* Soft-take step.itemTakes from the bag (min(have, required)). Returns player-facing notices.
418
*/
419
#takeStepItems (characterId, step, events)
420
{
421
const takes = step?.itemTakes ?? [];
422
if (!takes.length)
423
{ return []; }
424
const parts = [];
425
for (const take of takes)
426
{
427
const bag = this.#loadBag(characterId);
428
if (!bag)
429
{ continue; }
430
const itemId = take.itemId | 0;
431
const want = Math.max(1, Number(take.quantity) || 1);
432
const def = itemDefinition(itemId);
433
const displayName = def?.displayName ?? `Item ${itemId}`;
434
const result = takeMatchingUpToFromSlots(bag.inventory, {
435
itemId,
436
quantity: want,
437
compact: false
438
});
439
if (!result.ok || result.takenQuantity <= 0)
440
{ continue; }
441
bag.inventory = result.slots;
442
this.#persistBag(characterId, bag);
443
events.push({ type: "inventoryDirty" });
444
parts.push({ displayName, quantity: result.takenQuantity });
445
}
446
const message = formatDialogueTakeMessage(parts);
447
return message ? [message] : [];
448
}
449
411
450
/**
412
451
* Grant step.itemRewards into bag (overflow → bank/vault). Returns player-facing notices.
413
452
*/
@@ -4,12 +4,16 @@ import {4
countableObjectiveProgress,
4
countableObjectiveProgress,
5
objectiveProgressFingerprint
5
objectiveProgressFingerprint
6
} from "../../../shared/quests/objectiveProgress.js";
6
} from "../../../shared/quests/objectiveProgress.js";
7
import { countItemInSlots, insertStack, takeMatchingFromSlots, insertStackGrowable } from "../../../shared/items/inventory.js";
7
import { countItemInSlots, insertStack, takeMatchingFromSlots, takeMatchingUpToFromSlots, insertStackGrowable } from "../../../shared/items/inventory.js";
8
import { attachOrigins, godsOrigin } from "../../../shared/items/origin.js";
8
import { attachOrigins, godsOrigin } from "../../../shared/items/origin.js";
9
import { itemDefinition, itemDisplayName } from "../../../shared/definitions/items.js";
9
import { itemDefinition, itemDisplayName } from "../../../shared/definitions/items.js";
10
import { applySkillXp, publicSkillsPayload } from "../../../shared/skills/state.js";
10
import { applySkillXp, publicSkillsPayload } from "../../../shared/skills/state.js";
11
import { skillLevel, SKILL_DEFINITIONS } from "../../../shared/definitions/skills.js";
11
import { skillLevel, SKILL_DEFINITIONS } from "../../../shared/definitions/skills.js";
12
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../../shared/quests/dialogueRewards.js";
12
import {
13
formatDialogueRewardMessage,
14
formatDialogueQuestRewardMessage,
15
formatDialogueTakeMessage
16
} from "../../../shared/quests/dialogueRewards.js";
13
import {
17
import {
14
formatQuestBankRewardLetterBody,
18
formatQuestBankRewardLetterBody,
15
QUESTWARD_GIFT_AUTHOR,
19
QUESTWARD_GIFT_AUTHOR,
@@ -328,6 +332,7 @@ export class QuestRuntime328
{
332
{
329
const step = tree.steps.find((s) => s.stepId === session.stepId);
333
const step = tree.steps.find((s) => s.stepId === session.stepId);
330
const rewardMessages = [
334
const rewardMessages = [
335
...this.#takeStepItems(characterId, step, events),
331
...this.#grantStepItemRewards(characterId, step, events),
336
...this.#grantStepItemRewards(characterId, step, events),
332
...this.#grantStepQuestRewards(characterId, step, playerView, events)
337
...this.#grantStepQuestRewards(characterId, step, playerView, events)
333
];
338
];
@@ -408,6 +413,40 @@ export class QuestRuntime408
return { ok: true, events };
413
return { ok: true, events };
409
}
414
}
410
415
416
/**
417
* Soft-take step.itemTakes from the bag (min(have, required)). Returns player-facing notices.
418
*/
419
#takeStepItems (characterId, step, events)
420
{
421
const takes = step?.itemTakes ?? [];
422
if (!takes.length)
423
{ return []; }
424
const parts = [];
425
for (const take of takes)
426
{
427
const bag = this.#loadBag(characterId);
428
if (!bag)
429
{ continue; }
430
const itemId = take.itemId | 0;
431
const want = Math.max(1, Number(take.quantity) || 1);
432
const def = itemDefinition(itemId);
433
const displayName = def?.displayName ?? `Item ${itemId}`;
434
const result = takeMatchingUpToFromSlots(bag.inventory, {
435
itemId,
436
quantity: want,
437
compact: false
438
});
439
if (!result.ok || result.takenQuantity <= 0)
440
{ continue; }
441
bag.inventory = result.slots;
442
this.#persistBag(characterId, bag);
443
events.push({ type: "inventoryDirty" });
444
parts.push({ displayName, quantity: result.takenQuantity });
445
}
446
const message = formatDialogueTakeMessage(parts);
447
return message ? [message] : [];
448
}
449
411
/**
450
/**
412
* Grant step.itemRewards into bag (overflow → bank/vault). Returns player-facing notices.
451
* Grant step.itemRewards into bag (overflow → bank/vault). Returns player-facing notices.
413
*/
452
*/
@@ -0,0 +1,168 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { normalizeDialogueTree } from "../../shared/quests/normalize.js";
4
import { ConditionKind } from "../../shared/quests/conditions.js";
5
import { formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";
6
import { unguaranteedDialogueTakes } from "../../shared/quests/dialoguePathTakes.js";
7
import {
8
countItemInSlots,
9
emptyEquipment,
10
emptySlots,
11
INVENTORY_SLOTS,
12
takeMatchingUpToFromSlots
13
} from "../../shared/items/inventory.js";
14
import { ItemId } from "../../shared/definitions/items.js";
15
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
16
17
test("normalizeDialogueTree keeps itemTakes", () =>
18
{
19
const tree = normalizeDialogueTree({
20
dialogueId: "Take Tin",
21
steps: [{
22
stepId: "ask",
23
lines: [{ speaker: "npc", text: "Pay up." }],
24
itemTakes: [{ itemId: ItemId.tinOre, quantity: 3 }, { itemId: ItemId.tinOre, quantity: 9 }]
25
}]
26
});
27
assert.deepEqual(tree.steps[0].itemTakes, [{ itemId: ItemId.tinOre, quantity: 3 }]);
28
});
29
30
test("formatDialogueTakeMessage lists removed bag items", () =>
31
{
32
assert.equal(
33
formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 1 }]),
34
"Tin ore has been removed from your bag."
35
);
36
assert.equal(
37
formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 3 }]),
38
"3 Tin ore have been removed from your bag."
39
);
40
});
41
42
test("takeMatchingUpToFromSlots soft-takes what is available", () =>
43
{
44
const slots = emptySlots(4);
45
slots[0] = { itemId: ItemId.bones, quantity: 2 };
46
const result = takeMatchingUpToFromSlots(slots, {
47
itemId: ItemId.bones,
48
quantity: 5,
49
compact: false
50
});
51
assert.equal(result.ok, true);
52
assert.equal(result.takenQuantity, 2);
53
assert.equal(countItemInSlots(result.slots, ItemId.bones), 0);
54
});
55
56
test("unguaranteedDialogueTakes warns unless every path guarantees hasItem", () =>
57
{
58
const unguaranteed = normalizeDialogueTree({
59
dialogueId: "unguarded-take",
60
entryStepId: "start",
61
steps: [
62
{
63
stepId: "start",
64
lines: [{ text: "Hi" }],
65
exit: {
66
kind: "branches",
67
branches: [{
68
label: "go",
69
when: { all: [] },
70
nextStepId: "pay"
71
}]
72
}
73
},
74
{
75
stepId: "pay",
76
lines: [{ text: "Hand it over." }],
77
itemTakes: [{ itemId: ItemId.bones, quantity: 2 }]
78
}
79
]
80
});
81
const warn = unguaranteedDialogueTakes(unguaranteed);
82
assert.equal(warn.get("pay")?.has(ItemId.bones), true);
83
84
const guaranteed = normalizeDialogueTree({
85
dialogueId: "guarded-take",
86
entryStepId: "start",
87
steps: [
88
{
89
stepId: "start",
90
lines: [{ text: "Hi" }],
91
exit: {
92
kind: "branches",
93
branches: [{
94
label: "go",
95
when: {
96
all: [{
97
condition: {
98
kind: ConditionKind.hasItem,
99
itemId: ItemId.bones,
100
quantity: 2
101
}
102
}]
103
},
104
nextStepId: "pay"
105
}]
106
}
107
},
108
{
109
stepId: "pay",
110
lines: [{ text: "Hand it over." }],
111
itemTakes: [{ itemId: ItemId.bones, quantity: 2 }]
112
}
113
]
114
});
115
const ok = unguaranteedDialogueTakes(guaranteed);
116
assert.equal(ok.has("pay"), false);
117
});
118
119
test("dialogue advance soft-takes itemTakes then reports a notice", () =>
120
{
121
const harness = makeSimulation({ workerCount: 1 });
122
try
123
{
124
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
125
const playerId = harness.simulation.spawnCharacter(1, character);
126
harness.simulation.step();
127
128
const tree = normalizeDialogueTree({
129
dialogueId: "collect-bones",
130
steps: [{
131
stepId: "greeting",
132
lines: [{ speaker: "npc", text: "Those bones look useful." }],
133
itemTakes: [{ itemId: ItemId.bones, quantity: 5 }]
134
}]
135
});
136
const revisionId = harness.worldDb.getActiveRevisionId();
137
harness.worldDb.saveDialogueTree(revisionId, tree);
138
harness.simulation.questContent.reload();
139
140
const inventory = emptySlots(INVENTORY_SLOTS);
141
inventory[0] = { itemId: ItemId.bones, quantity: 2 };
142
harness.simulation.applyBag(playerId, {
143
inventory,
144
equipment: emptyEquipment(),
145
vault: [],
146
vaultReserved: []
147
});
148
149
const npcId = 9001;
150
const opened = harness.simulation.quests.tryOpenDialogue(
151
character.character_id,
152
npcId,
153
"collect-bones"
154
);
155
assert.equal(opened.ok, true);
156
157
const advanced = harness.simulation.quests.advanceDialogue(character.character_id);
158
assert.equal(advanced.ok, true);
159
const notice = (advanced.events ?? []).find((e) => e.type === "dialogueItemRewards");
160
assert.ok(notice);
161
assert.match(notice.messages.join(" "), /2 Bones have been removed from your bag/);
162
163
const bag = harness.simulation.bagOf(playerId);
164
assert.equal(countItemInSlots(bag.inventory, ItemId.bones), 0);
165
}
166
finally
167
{ harness.close(); }
168
});
@@ -0,0 +1,168 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { normalizeDialogueTree } from "../../shared/quests/normalize.js";4
import { ConditionKind } from "../../shared/quests/conditions.js";5
import { formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";6
import { unguaranteedDialogueTakes } from "../../shared/quests/dialoguePathTakes.js";7
import {8
countItemInSlots,9
emptyEquipment,10
emptySlots,11
INVENTORY_SLOTS,12
takeMatchingUpToFromSlots13
} from "../../shared/items/inventory.js";14
import { ItemId } from "../../shared/definitions/items.js";15
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";16
17
test("normalizeDialogueTree keeps itemTakes", () =>18
{19
const tree = normalizeDialogueTree({20
dialogueId: "Take Tin",21
steps: [{22
stepId: "ask",23
lines: [{ speaker: "npc", text: "Pay up." }],24
itemTakes: [{ itemId: ItemId.tinOre, quantity: 3 }, { itemId: ItemId.tinOre, quantity: 9 }]25
}]26
});27
assert.deepEqual(tree.steps[0].itemTakes, [{ itemId: ItemId.tinOre, quantity: 3 }]);28
});29
30
test("formatDialogueTakeMessage lists removed bag items", () =>31
{32
assert.equal(33
formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 1 }]),34
"Tin ore has been removed from your bag."35
);36
assert.equal(37
formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 3 }]),38
"3 Tin ore have been removed from your bag."39
);40
});41
42
test("takeMatchingUpToFromSlots soft-takes what is available", () =>43
{44
const slots = emptySlots(4);45
slots[0] = { itemId: ItemId.bones, quantity: 2 };46
const result = takeMatchingUpToFromSlots(slots, {47
itemId: ItemId.bones,48
quantity: 5,49
compact: false50
});51
assert.equal(result.ok, true);52
assert.equal(result.takenQuantity, 2);53
assert.equal(countItemInSlots(result.slots, ItemId.bones), 0);54
});55
56
test("unguaranteedDialogueTakes warns unless every path guarantees hasItem", () =>57
{58
const unguaranteed = normalizeDialogueTree({59
dialogueId: "unguarded-take",60
entryStepId: "start",61
steps: [62
{63
stepId: "start",64
lines: [{ text: "Hi" }],65
exit: {66
kind: "branches",67
branches: [{68
label: "go",69
when: { all: [] },70
nextStepId: "pay"71
}]72
}73
},74
{75
stepId: "pay",76
lines: [{ text: "Hand it over." }],77
itemTakes: [{ itemId: ItemId.bones, quantity: 2 }]78
}79
]80
});81
const warn = unguaranteedDialogueTakes(unguaranteed);82
assert.equal(warn.get("pay")?.has(ItemId.bones), true);83
84
const guaranteed = normalizeDialogueTree({85
dialogueId: "guarded-take",86
entryStepId: "start",87
steps: [88
{89
stepId: "start",90
lines: [{ text: "Hi" }],91
exit: {92
kind: "branches",93
branches: [{94
label: "go",95
when: {96
all: [{97
condition: {98
kind: ConditionKind.hasItem,99
itemId: ItemId.bones,100
quantity: 2101
}102
}]103
},104
nextStepId: "pay"105
}]106
}107
},108
{109
stepId: "pay",110
lines: [{ text: "Hand it over." }],111
itemTakes: [{ itemId: ItemId.bones, quantity: 2 }]112
}113
]114
});115
const ok = unguaranteedDialogueTakes(guaranteed);116
assert.equal(ok.has("pay"), false);117
});118
119
test("dialogue advance soft-takes itemTakes then reports a notice", () =>120
{121
const harness = makeSimulation({ workerCount: 1 });122
try123
{124
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });125
const playerId = harness.simulation.spawnCharacter(1, character);126
harness.simulation.step();127
128
const tree = normalizeDialogueTree({129
dialogueId: "collect-bones",130
steps: [{131
stepId: "greeting",132
lines: [{ speaker: "npc", text: "Those bones look useful." }],133
itemTakes: [{ itemId: ItemId.bones, quantity: 5 }]134
}]135
});136
const revisionId = harness.worldDb.getActiveRevisionId();137
harness.worldDb.saveDialogueTree(revisionId, tree);138
harness.simulation.questContent.reload();139
140
const inventory = emptySlots(INVENTORY_SLOTS);141
inventory[0] = { itemId: ItemId.bones, quantity: 2 };142
harness.simulation.applyBag(playerId, {143
inventory,144
equipment: emptyEquipment(),145
vault: [],146
vaultReserved: []147
});148
149
const npcId = 9001;150
const opened = harness.simulation.quests.tryOpenDialogue(151
character.character_id,152
npcId,153
"collect-bones"154
);155
assert.equal(opened.ok, true);156
157
const advanced = harness.simulation.quests.advanceDialogue(character.character_id);158
assert.equal(advanced.ok, true);159
const notice = (advanced.events ?? []).find((e) => e.type === "dialogueItemRewards");160
assert.ok(notice);161
assert.match(notice.messages.join(" "), /2 Bones have been removed from your bag/);162
163
const bag = harness.simulation.bagOf(playerId);164
assert.equal(countItemInSlots(bag.inventory, ItemId.bones), 0);165
}166
finally167
{ harness.close(); }168
});@@ -10,7 +10,7 @@ import {
10
10
formatObjectiveDisplayText
11
11
} from "../../shared/quests/objectiveProgress.js";
12
12
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
13
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";
13
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage, formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";
14
14
15
15
test("ConditionSet AND + NOT", () =>
16
16
{
@@ -96,6 +96,10 @@ test("dialogue reward messages bag vs bank and lists", () =>
96
96
formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
97
97
"The Alpha and Beta quests have been added to your journal."
98
98
);
99
assert.equal(
100
formatDialogueTakeMessage([{ displayName: "Copper ore", quantity: 2 }]),
101
"2 Copper ore have been removed from your bag."
102
);
99
103
});
100
104
101
105
test("countable objective progress for hasItem killCount and skillLevelAtLeast", () =>
@@ -10,7 +10,7 @@ import {10
formatObjectiveDisplayText10
formatObjectiveDisplayText11
} from "../../shared/quests/objectiveProgress.js";11
} from "../../shared/quests/objectiveProgress.js";12
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";12
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";13
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";13
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage, formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";14
14
15
test("ConditionSet AND + NOT", () =>15
test("ConditionSet AND + NOT", () =>16
{16
{@@ -96,6 +96,10 @@ test("dialogue reward messages bag vs bank and lists", () =>96
formatDialogueQuestRewardMessage(["Alpha", "Beta"]),96
formatDialogueQuestRewardMessage(["Alpha", "Beta"]),97
"The Alpha and Beta quests have been added to your journal."97
"The Alpha and Beta quests have been added to your journal."98
);98
);99
assert.equal(100
formatDialogueTakeMessage([{ displayName: "Copper ore", quantity: 2 }]),101
"2 Copper ore have been removed from your bag."102
);99
});103
});100
104
101
test("countable objective progress for hasItem killCount and skillLevelAtLeast", () =>105
test("countable objective progress for hasItem killCount and skillLevelAtLeast", () =>