Allow dropping one log from a stack; keep remainder on fire/debark.
Bag context menu now offers Drop one/half/all/X for stacks, and ground log actions correctly leave the rest of a stack untouched with origin splits. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -10,6 +10,7 @@ import
10
10
EQUIPMENT_SLOT,
11
11
LETTER_MAX_BODY_CHARS
12
12
} from "/shared/definitions/items.js";
13
import { stackQuantity } from "/shared/items/inventory.js";
13
14
import { formatLetterSignature } from "/shared/courier/systemLetters.js";
14
15
import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";
15
16
import { spellDefinition } from "/shared/definitions/spells.js";
@@ -485,13 +486,47 @@ export function initInventory ({
485
486
});
486
487
}
487
488
}
488
items.push({
489
label: "Drop",
490
onSelect: () => socket.send({ type: MessageType.dropItem, slotIndex })
491
});
489
const dropQty = stackQuantity(slot.quantity);
490
if (dropQty <= 1)
491
{
492
items.push({
493
label: "Drop",
494
onSelect: () => dropSlot(slotIndex, 1)
495
});
496
}
497
else
498
{
499
const name = itemDisplayName(slot);
500
items.push(
501
{
502
label: `Drop one ${name}`,
503
onSelect: () => dropSlot(slotIndex, 1)
504
},
505
{
506
label: `Drop half ${name}`,
507
onSelect: () => dropSlot(slotIndex, Math.max(1, Math.floor(dropQty / 2)))
508
},
509
{
510
label: `Drop all ${name}`,
511
onSelect: () => dropSlot(slotIndex, null)
512
},
513
{
514
label: `Drop X ${name}`,
515
onSelect: () => openDrop(slotIndex, slot)
516
}
517
);
518
}
492
519
contextMenu?.show(x, y, items);
493
520
}
494
521
522
function dropSlot (slotIndex, quantity)
523
{
524
const payload = { type: MessageType.dropItem, slotIndex };
525
if (quantity != null)
526
{ payload.quantity = quantity; }
527
socket.send(payload);
528
}
529
495
530
function showBankTransferMenu (x, y, side, slotIndex, slot)
496
531
{
497
532
if (side === "inventory" && slot.itemId === ItemId.filledCheque)
@@ -570,7 +605,22 @@ export function initInventory ({
570
605
571
606
function openTransfer (side, slotIndex, slot)
572
607
{
573
transferState = { side, slotIndex, maxQty: slot.quantity | 0 };
608
transferState = { kind: "bank", side, slotIndex, maxQty: stackQuantity(slot.quantity) };
609
document.querySelector("#transfer-panel header h2").textContent = "Transfer";
610
document.getElementById("transfer-confirm").textContent = "Transfer";
611
document.getElementById("transfer-item-name").textContent = itemDisplayName(slot);
612
const input = document.getElementById("transfer-qty");
613
input.value = "1";
614
input.max = String(transferState.maxQty);
615
transferPanel.hidden = false;
616
transferBackdrop.hidden = false;
617
}
618
619
function openDrop (slotIndex, slot)
620
{
621
transferState = { kind: "drop", slotIndex, maxQty: stackQuantity(slot.quantity) };
622
document.querySelector("#transfer-panel header h2").textContent = "Drop";
623
document.getElementById("transfer-confirm").textContent = "Drop";
574
624
document.getElementById("transfer-item-name").textContent = itemDisplayName(slot);
575
625
const input = document.getElementById("transfer-qty");
576
626
input.value = "1";
@@ -594,7 +644,10 @@ export function initInventory ({
594
644
transferState.maxQty,
595
645
Math.max(1, Number(document.getElementById("transfer-qty").value) | 0)
596
646
);
597
transferSlot(transferState.side, transferState.slotIndex, quantity);
647
if (transferState.kind === "drop")
648
{ dropSlot(transferState.slotIndex, quantity); }
649
else
650
{ transferSlot(transferState.side, transferState.slotIndex, quantity); }
598
651
closeTransfer();
599
652
}
600
653
@@ -10,6 +10,7 @@ import10
EQUIPMENT_SLOT,10
EQUIPMENT_SLOT,11
LETTER_MAX_BODY_CHARS11
LETTER_MAX_BODY_CHARS12
} from "/shared/definitions/items.js";12
} from "/shared/definitions/items.js";13
import { stackQuantity } from "/shared/items/inventory.js";13
import { formatLetterSignature } from "/shared/courier/systemLetters.js";14
import { formatLetterSignature } from "/shared/courier/systemLetters.js";14
import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";15
import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";15
import { spellDefinition } from "/shared/definitions/spells.js";16
import { spellDefinition } from "/shared/definitions/spells.js";@@ -485,13 +486,47 @@ export function initInventory ({485
});486
});486
}487
}487
}488
}488
items.push({489
const dropQty = stackQuantity(slot.quantity);489
label: "Drop",490
if (dropQty <= 1)490
onSelect: () => socket.send({ type: MessageType.dropItem, slotIndex })491
{491
});492
items.push({493
label: "Drop",494
onSelect: () => dropSlot(slotIndex, 1)495
});496
}497
else498
{499
const name = itemDisplayName(slot);500
items.push(501
{502
label: `Drop one ${name}`,503
onSelect: () => dropSlot(slotIndex, 1)504
},505
{506
label: `Drop half ${name}`,507
onSelect: () => dropSlot(slotIndex, Math.max(1, Math.floor(dropQty / 2)))508
},509
{510
label: `Drop all ${name}`,511
onSelect: () => dropSlot(slotIndex, null)512
},513
{514
label: `Drop X ${name}`,515
onSelect: () => openDrop(slotIndex, slot)516
}517
);518
}492
contextMenu?.show(x, y, items);519
contextMenu?.show(x, y, items);493
}520
}494
521
522
function dropSlot (slotIndex, quantity)523
{524
const payload = { type: MessageType.dropItem, slotIndex };525
if (quantity != null)526
{ payload.quantity = quantity; }527
socket.send(payload);528
}529
495
function showBankTransferMenu (x, y, side, slotIndex, slot)530
function showBankTransferMenu (x, y, side, slotIndex, slot)496
{531
{497
if (side === "inventory" && slot.itemId === ItemId.filledCheque)532
if (side === "inventory" && slot.itemId === ItemId.filledCheque)@@ -570,7 +605,22 @@ export function initInventory ({570
605
571
function openTransfer (side, slotIndex, slot)606
function openTransfer (side, slotIndex, slot)572
{607
{573
transferState = { side, slotIndex, maxQty: slot.quantity | 0 };608
transferState = { kind: "bank", side, slotIndex, maxQty: stackQuantity(slot.quantity) };609
document.querySelector("#transfer-panel header h2").textContent = "Transfer";610
document.getElementById("transfer-confirm").textContent = "Transfer";611
document.getElementById("transfer-item-name").textContent = itemDisplayName(slot);612
const input = document.getElementById("transfer-qty");613
input.value = "1";614
input.max = String(transferState.maxQty);615
transferPanel.hidden = false;616
transferBackdrop.hidden = false;617
}618
619
function openDrop (slotIndex, slot)620
{621
transferState = { kind: "drop", slotIndex, maxQty: stackQuantity(slot.quantity) };622
document.querySelector("#transfer-panel header h2").textContent = "Drop";623
document.getElementById("transfer-confirm").textContent = "Drop";574
document.getElementById("transfer-item-name").textContent = itemDisplayName(slot);624
document.getElementById("transfer-item-name").textContent = itemDisplayName(slot);575
const input = document.getElementById("transfer-qty");625
const input = document.getElementById("transfer-qty");576
input.value = "1";626
input.value = "1";@@ -594,7 +644,10 @@ export function initInventory ({594
transferState.maxQty,644
transferState.maxQty,595
Math.max(1, Number(document.getElementById("transfer-qty").value) | 0)645
Math.max(1, Number(document.getElementById("transfer-qty").value) | 0)596
);646
);597
transferSlot(transferState.side, transferState.slotIndex, quantity);647
if (transferState.kind === "drop")648
{ dropSlot(transferState.slotIndex, quantity); }649
else650
{ transferSlot(transferState.side, transferState.slotIndex, quantity); }598
closeTransfer();651
closeTransfer();599
}652
}600
653
@@ -17,7 +17,7 @@ import { SkillId, skillLevel } from "../../../shared/definitions/skills.js";
17
17
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";
18
18
import { rollSkill } from "../../../shared/combat/rolls.js";
19
19
import { stackQuantity } from "../../../shared/items/inventory.js";
20
import { attachOrigins, ashOrigin } from "../../../shared/items/origin.js";
20
import { attachOrigins, ashOrigin, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";
21
21
import { addGroundItem, publicGroundItem, removeGroundItem, bagView } from "../items/bags.js";
22
22
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
23
23
import { tileToChunk, tileToLocal } from "../world/coordinates.js";
@@ -173,7 +173,10 @@ function consumeOneGroundLog (world, item, events)
173
173
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
174
174
return;
175
175
}
176
const split = splitOrigins(normalizeOrigins(item.stack), 1);
176
177
item.stack.quantity = qty - 1;
178
item.stack.origins = split.remain;
179
delete item.stack.origin;
177
180
events.push({
178
181
type: "ground-item-appear",
179
182
...publicGroundItem(item),
@@ -17,7 +17,7 @@ import { SkillId, skillLevel } from "../../../shared/definitions/skills.js";17
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";17
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";18
import { rollSkill } from "../../../shared/combat/rolls.js";18
import { rollSkill } from "../../../shared/combat/rolls.js";19
import { stackQuantity } from "../../../shared/items/inventory.js";19
import { stackQuantity } from "../../../shared/items/inventory.js";20
import { attachOrigins, ashOrigin } from "../../../shared/items/origin.js";20
import { attachOrigins, ashOrigin, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";21
import { addGroundItem, publicGroundItem, removeGroundItem, bagView } from "../items/bags.js";21
import { addGroundItem, publicGroundItem, removeGroundItem, bagView } from "../items/bags.js";22
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";22
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";23
import { tileToChunk, tileToLocal } from "../world/coordinates.js";23
import { tileToChunk, tileToLocal } from "../world/coordinates.js";@@ -173,7 +173,10 @@ function consumeOneGroundLog (world, item, events)173
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });173
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });174
return;174
return;175
}175
}176
const split = splitOrigins(normalizeOrigins(item.stack), 1);176
item.stack.quantity = qty - 1;177
item.stack.quantity = qty - 1;178
item.stack.origins = split.remain;179
delete item.stack.origin;177
events.push({180
events.push({178
type: "ground-item-appear",181
type: "ground-item-appear",179
...publicGroundItem(item),182
...publicGroundItem(item),@@ -33,7 +33,15 @@ import {
33
33
} from "../../../shared/definitions/dyes.js";
34
34
import { herbDefinition, isHerbId } from "../../../shared/definitions/herbs.js";
35
35
import { insertStack, stackQuantity } from "../../../shared/items/inventory.js";
36
import { attachOrigins, GatherOriginVerb, gatherOrigin, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js";
36
import {
37
attachOrigins,
38
GatherOriginVerb,
39
gatherOrigin,
40
craftOrigin,
41
CraftOriginVerb,
42
normalizeOrigins,
43
splitOrigins
44
} from "../../../shared/items/origin.js";
37
45
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";
38
46
import
39
47
{
@@ -826,11 +834,12 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
826
834
}
827
835
828
836
const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle));
837
const split = splitOrigins(normalizeOrigins(item.stack), 1);
829
838
const debarkedStack = attachOrigins({
830
839
itemId: item.stack.itemId,
831
840
quantity: 1,
832
841
instance: debarkedLogInstance(),
833
origins: item.stack.origins ?? []
842
origins: split.taken
834
843
});
835
844
const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;
836
845
@@ -842,8 +851,10 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
842
851
else
843
852
{
844
853
item.stack = {
845
...item.stack,
846
quantity: qty - 1
854
itemId: item.stack.itemId,
855
quantity: qty - 1,
856
instance: item.stack.instance ?? null,
857
origins: split.remain
847
858
};
848
859
events.push(publishGroundItem(item));
849
860
@@ -33,7 +33,15 @@ import {33
} from "../../../shared/definitions/dyes.js";33
} from "../../../shared/definitions/dyes.js";34
import { herbDefinition, isHerbId } from "../../../shared/definitions/herbs.js";34
import { herbDefinition, isHerbId } from "../../../shared/definitions/herbs.js";35
import { insertStack, stackQuantity } from "../../../shared/items/inventory.js";35
import { insertStack, stackQuantity } from "../../../shared/items/inventory.js";36
import { attachOrigins, GatherOriginVerb, gatherOrigin, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js";36
import {37
attachOrigins,38
GatherOriginVerb,39
gatherOrigin,40
craftOrigin,41
CraftOriginVerb,42
normalizeOrigins,43
splitOrigins44
} from "../../../shared/items/origin.js";37
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";45
import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js";38
import46
import39
{47
{@@ -826,11 +834,12 @@ function completeDebark (world, handle, entityId, character, gather, events, pri826
}834
}827
835
828
const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle));836
const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle));837
const split = splitOrigins(normalizeOrigins(item.stack), 1);829
const debarkedStack = attachOrigins({838
const debarkedStack = attachOrigins({830
itemId: item.stack.itemId,839
itemId: item.stack.itemId,831
quantity: 1,840
quantity: 1,832
instance: debarkedLogInstance(),841
instance: debarkedLogInstance(),833
origins: item.stack.origins ?? []842
origins: split.taken834
});843
});835
const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;844
const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;836
845
@@ -842,8 +851,10 @@ function completeDebark (world, handle, entityId, character, gather, events, pri842
else851
else843
{852
{844
item.stack = {853
item.stack = {845
...item.stack,854
itemId: item.stack.itemId,846
quantity: qty - 1855
quantity: qty - 1,856
instance: item.stack.instance ?? null,857
origins: split.remain847
};858
};848
events.push(publishGroundItem(item));859
events.push(publishGroundItem(item));849
860
@@ -10,7 +10,7 @@ import { rollSkill } from "../../../shared/combat/rolls.js";
10
10
import { firestartingDifficulty, campfireDurationMs } from "../../../shared/definitions/craftingStations.js";
11
11
import { MATERIAL_DISPLAY_NAME } from "../../../shared/definitions/materialTiers.js";
12
12
import { stackQuantity } from "../../../shared/items/inventory.js";
13
import { attachOrigins, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js";
13
import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";
14
14
import
15
15
{
16
16
addGroundItem,
@@ -98,7 +98,10 @@ function consumeOneGroundStack (world, item, events)
98
98
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
99
99
return;
100
100
}
101
const split = splitOrigins(normalizeOrigins(item.stack), 1);
101
102
item.stack.quantity = qty - 1;
103
item.stack.origins = split.remain;
104
delete item.stack.origin;
102
105
events.push({
103
106
type: "ground-item-appear",
104
107
...publicGroundItem(item),
@@ -10,7 +10,7 @@ import { rollSkill } from "../../../shared/combat/rolls.js";10
import { firestartingDifficulty, campfireDurationMs } from "../../../shared/definitions/craftingStations.js";10
import { firestartingDifficulty, campfireDurationMs } from "../../../shared/definitions/craftingStations.js";11
import { MATERIAL_DISPLAY_NAME } from "../../../shared/definitions/materialTiers.js";11
import { MATERIAL_DISPLAY_NAME } from "../../../shared/definitions/materialTiers.js";12
import { stackQuantity } from "../../../shared/items/inventory.js";12
import { stackQuantity } from "../../../shared/items/inventory.js";13
import { attachOrigins, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js";13
import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";14
import14
import15
{15
{16
addGroundItem,16
addGroundItem,@@ -98,7 +98,10 @@ function consumeOneGroundStack (world, item, events)98
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });98
events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });99
return;99
return;100
}100
}101
const split = splitOrigins(normalizeOrigins(item.stack), 1);101
item.stack.quantity = qty - 1;102
item.stack.quantity = qty - 1;103
item.stack.origins = split.remain;104
delete item.stack.origin;102
events.push({105
events.push({103
type: "ground-item-appear",106
type: "ground-item-appear",104
...publicGroundItem(item),107
...publicGroundItem(item),@@ -155,6 +155,49 @@ test("fish cook recipes use the species name as craft label", () =>
155
155
assert.equal(minnow.craftGroupLabel, "Minnow");
156
156
});
157
157
158
test("start-fire on a log stack consumes only one log", () =>
159
{
160
const harness = makeSimulation({ workerCount: 1 });
161
try
162
{
163
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
164
const playerId = harness.simulation.spawnCharacter(1, character);
165
const worker = workerOf(harness.simulation, playerId);
166
worker.world.rng = () => 0.999;
167
setSkillLevel(worker, playerId, SkillId.firestarting, 1);
168
giveInventory(harness, playerId, [
169
{ itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
170
]);
171
172
const groundItemId = 9010;
173
addGroundItem(worker.world, {
174
groundItemId,
175
tileX: 9,
176
tileY: 8,
177
stack: { itemId: WoodItemId.pineLog, quantity: 5 },
178
droppedAt: harness.now(),
179
expiresAt: harness.now() + 600_000,
180
source: "test"
181
});
182
183
harness.simulation.enqueue(1, {
184
type: "start-fire",
185
characterId: character.character_id,
186
groundItemId
187
});
188
advanceMs(harness, 4000);
189
190
const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];
191
assert.equal(block?.typeId, BlockType.campfire);
192
const remain = worker.world.groundItems.get(groundItemId);
193
assert.ok(remain);
194
assert.equal(remain.stack.itemId, WoodItemId.pineLog);
195
assert.equal(remain.stack.quantity, 4);
196
}
197
finally
198
{ harness.close(); }
199
});
200
158
201
test("start-fire places campfire and expiry drops ash", () =>
159
202
{
160
203
const harness = makeSimulation({ workerCount: 1 });
@@ -155,6 +155,49 @@ test("fish cook recipes use the species name as craft label", () =>155
assert.equal(minnow.craftGroupLabel, "Minnow");155
assert.equal(minnow.craftGroupLabel, "Minnow");156
});156
});157
157
158
test("start-fire on a log stack consumes only one log", () =>159
{160
const harness = makeSimulation({ workerCount: 1 });161
try162
{163
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });164
const playerId = harness.simulation.spawnCharacter(1, character);165
const worker = workerOf(harness.simulation, playerId);166
worker.world.rng = () => 0.999;167
setSkillLevel(worker, playerId, SkillId.firestarting, 1);168
giveInventory(harness, playerId, [169
{ itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }170
]);171
172
const groundItemId = 9010;173
addGroundItem(worker.world, {174
groundItemId,175
tileX: 9,176
tileY: 8,177
stack: { itemId: WoodItemId.pineLog, quantity: 5 },178
droppedAt: harness.now(),179
expiresAt: harness.now() + 600_000,180
source: "test"181
});182
183
harness.simulation.enqueue(1, {184
type: "start-fire",185
characterId: character.character_id,186
groundItemId187
});188
advanceMs(harness, 4000);189
190
const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];191
assert.equal(block?.typeId, BlockType.campfire);192
const remain = worker.world.groundItems.get(groundItemId);193
assert.ok(remain);194
assert.equal(remain.stack.itemId, WoodItemId.pineLog);195
assert.equal(remain.stack.quantity, 4);196
}197
finally198
{ harness.close(); }199
});200
158
test("start-fire places campfire and expiry drops ash", () =>201
test("start-fire places campfire and expiry drops ash", () =>159
{202
{160
const harness = makeSimulation({ workerCount: 1 });203
const harness = makeSimulation({ workerCount: 1 });@@ -10,6 +10,7 @@ import
10
10
} from "../../shared/items/inventory.js";
11
11
import { ItemOriginKind } from "../../shared/items/origin.js";
12
12
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
13
import { WoodItemId } from "../../shared/definitions/woodItems.js";
13
14
import { Component } from "../src/ecs/components.js";
14
15
import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
15
16
import
@@ -26,6 +27,25 @@ function workerOf (simulation, entityId)
26
27
return simulation.workers[record.workerId];
27
28
}
28
29
30
function giveInventory (harness, entityId, items)
31
{
32
const inventory = emptySlots();
33
items.forEach((stack, index) =>
34
{
35
inventory[index] = {
36
itemId: stack.itemId,
37
quantity: stack.quantity,
38
...(stack.instance ? { instance: structuredClone(stack.instance) } : {})
39
};
40
});
41
harness.simulation.applyBag(entityId, {
42
inventory,
43
equipment: emptyEquipment(),
44
vault: [],
45
vaultReserved: []
46
});
47
}
48
29
49
test("stackable items merge until max stack then need a new slot", () =>
30
50
{
31
51
const slots = emptySlots();
@@ -132,6 +152,35 @@ test("a player can drop and pick up an item on their tile", () =>
132
152
{ harness.close(); }
133
153
});
134
154
155
test("dropping one log from a stack leaves the rest in the bag", () =>
156
{
157
const harness = makeSimulation({ workerCount: 1 });
158
try
159
{
160
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
161
const entityId = harness.simulation.spawnCharacter(1, character);
162
giveInventory(harness, entityId, [
163
{ itemId: WoodItemId.pineLog, quantity: 7 }
164
]);
165
harness.simulation.enqueue(1, {
166
type: "drop-item",
167
characterId: character.character_id,
168
slotIndex: 0,
169
quantity: 1
170
});
171
const events = harness.simulation.step().events;
172
const appear = events.find((event) => event.type === "ground-item-appear");
173
assert.ok(appear);
174
assert.equal(appear.itemId, WoodItemId.pineLog);
175
assert.equal(appear.quantity, 1);
176
const after = harness.simulation.bagOf(entityId);
177
assert.equal(after.inventory[0]?.itemId, WoodItemId.pineLog);
178
assert.equal(after.inventory[0]?.quantity, 6);
179
}
180
finally
181
{ harness.close(); }
182
});
183
135
184
test("walkTilesPerSecond controls how many tiles move per simulated second", () =>
136
185
{
137
186
const harness = makeSimulation({ workerCount: 1 });
@@ -10,6 +10,7 @@ import10
} from "../../shared/items/inventory.js";10
} from "../../shared/items/inventory.js";11
import { ItemOriginKind } from "../../shared/items/origin.js";11
import { ItemOriginKind } from "../../shared/items/origin.js";12
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";12
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";13
import { WoodItemId } from "../../shared/definitions/woodItems.js";13
import { Component } from "../src/ecs/components.js";14
import { Component } from "../src/ecs/components.js";14
import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";15
import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";15
import16
import@@ -26,6 +27,25 @@ function workerOf (simulation, entityId)26
return simulation.workers[record.workerId];27
return simulation.workers[record.workerId];27
}28
}28
29
30
function giveInventory (harness, entityId, items)31
{32
const inventory = emptySlots();33
items.forEach((stack, index) =>34
{35
inventory[index] = {36
itemId: stack.itemId,37
quantity: stack.quantity,38
...(stack.instance ? { instance: structuredClone(stack.instance) } : {})39
};40
});41
harness.simulation.applyBag(entityId, {42
inventory,43
equipment: emptyEquipment(),44
vault: [],45
vaultReserved: []46
});47
}48
29
test("stackable items merge until max stack then need a new slot", () =>49
test("stackable items merge until max stack then need a new slot", () =>30
{50
{31
const slots = emptySlots();51
const slots = emptySlots();@@ -132,6 +152,35 @@ test("a player can drop and pick up an item on their tile", () =>132
{ harness.close(); }152
{ harness.close(); }133
});153
});134
154
155
test("dropping one log from a stack leaves the rest in the bag", () =>156
{157
const harness = makeSimulation({ workerCount: 1 });158
try159
{160
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });161
const entityId = harness.simulation.spawnCharacter(1, character);162
giveInventory(harness, entityId, [163
{ itemId: WoodItemId.pineLog, quantity: 7 }164
]);165
harness.simulation.enqueue(1, {166
type: "drop-item",167
characterId: character.character_id,168
slotIndex: 0,169
quantity: 1170
});171
const events = harness.simulation.step().events;172
const appear = events.find((event) => event.type === "ground-item-appear");173
assert.ok(appear);174
assert.equal(appear.itemId, WoodItemId.pineLog);175
assert.equal(appear.quantity, 1);176
const after = harness.simulation.bagOf(entityId);177
assert.equal(after.inventory[0]?.itemId, WoodItemId.pineLog);178
assert.equal(after.inventory[0]?.quantity, 6);179
}180
finally181
{ harness.close(); }182
});183
135
test("walkTilesPerSecond controls how many tiles move per simulated second", () =>184
test("walkTilesPerSecond controls how many tiles move per simulated second", () =>136
{185
{137
const harness = makeSimulation({ workerCount: 1 });186
const harness = makeSimulation({ workerCount: 1 });@@ -148,6 +148,71 @@ test("debark-log on ground oak log yields bark and debarked flag", () =>
148
148
{ harness.close(); }
149
149
});
150
150
151
test("debark-log on a ground log stack strips only one log", () =>
152
{
153
const harness = makeSimulation({ workerCount: 1 });
154
try
155
{
156
harness.simulation.reloadWorld();
157
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
158
setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
159
const inventory = emptySlots();
160
inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
161
harness.stateDb.saveBag(character.character_id, {
162
inventory,
163
equipment: emptyEquipment(),
164
vault: [],
165
vaultReserved: []
166
});
167
const entityId = harness.simulation.spawnCharacter(1, character);
168
const record = harness.simulation.entityRecord(entityId);
169
const worker = harness.simulation.workers[record.workerId];
170
171
const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
172
addGroundItem(worker.world, {
173
groundItemId,
174
tileX: 8,
175
tileY: 9,
176
stack: { itemId: WoodItemId.oakLog, quantity: 4 },
177
droppedAt: Date.now(),
178
expiresAt: Date.now() + 600_000,
179
source: "test"
180
});
181
182
harness.simulation.enqueue(1, {
183
type: "debark-log",
184
characterId: character.character_id,
185
groundItemId
186
});
187
harness.simulation.step();
188
189
for (let i = 0; i < 50; i++)
190
{
191
harness.advance(50);
192
harness.simulation.step();
193
}
194
195
const remain = worker.world.groundItems.get(groundItemId);
196
assert.ok(remain);
197
assert.equal(remain.stack.quantity, 3);
198
assert.equal(isLogDebarked(remain.stack), false);
199
200
const debarked = [...worker.world.groundItems.values()]
201
.find((row) => row.groundItemId !== groundItemId
202
&& row.stack?.itemId === WoodItemId.oakLog
203
&& isLogDebarked(row.stack));
204
assert.ok(debarked);
205
assert.equal(debarked.stack.quantity, 1);
206
207
const bark = [...worker.world.groundItems.values()]
208
.find((row) => row.stack?.itemId === WoodItemId.oakBark);
209
assert.ok(bark);
210
assert.equal(bark.stack.quantity, 1);
211
}
212
finally
213
{ harness.close(); }
214
});
215
151
216
test("animal fat and tanning brew alchemy recipes resolve", () =>
152
217
{
153
218
clearResolvedAlchemyPotionsCache();
@@ -148,6 +148,71 @@ test("debark-log on ground oak log yields bark and debarked flag", () =>148
{ harness.close(); }148
{ harness.close(); }149
});149
});150
150
151
test("debark-log on a ground log stack strips only one log", () =>152
{153
const harness = makeSimulation({ workerCount: 1 });154
try155
{156
harness.simulation.reloadWorld();157
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });158
setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);159
const inventory = emptySlots();160
inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };161
harness.stateDb.saveBag(character.character_id, {162
inventory,163
equipment: emptyEquipment(),164
vault: [],165
vaultReserved: []166
});167
const entityId = harness.simulation.spawnCharacter(1, character);168
const record = harness.simulation.entityRecord(entityId);169
const worker = harness.simulation.workers[record.workerId];170
171
const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);172
addGroundItem(worker.world, {173
groundItemId,174
tileX: 8,175
tileY: 9,176
stack: { itemId: WoodItemId.oakLog, quantity: 4 },177
droppedAt: Date.now(),178
expiresAt: Date.now() + 600_000,179
source: "test"180
});181
182
harness.simulation.enqueue(1, {183
type: "debark-log",184
characterId: character.character_id,185
groundItemId186
});187
harness.simulation.step();188
189
for (let i = 0; i < 50; i++)190
{191
harness.advance(50);192
harness.simulation.step();193
}194
195
const remain = worker.world.groundItems.get(groundItemId);196
assert.ok(remain);197
assert.equal(remain.stack.quantity, 3);198
assert.equal(isLogDebarked(remain.stack), false);199
200
const debarked = [...worker.world.groundItems.values()]201
.find((row) => row.groundItemId !== groundItemId202
&& row.stack?.itemId === WoodItemId.oakLog203
&& isLogDebarked(row.stack));204
assert.ok(debarked);205
assert.equal(debarked.stack.quantity, 1);206
207
const bark = [...worker.world.groundItems.values()]208
.find((row) => row.stack?.itemId === WoodItemId.oakBark);209
assert.ok(bark);210
assert.equal(bark.stack.quantity, 1);211
}212
finally213
{ harness.close(); }214
});215
151
test("animal fat and tanning brew alchemy recipes resolve", () =>216
test("animal fat and tanning brew alchemy recipes resolve", () =>152
{217
{153
clearResolvedAlchemyPotionsCache();218
clearResolvedAlchemyPotionsCache();