Merge branch 'feature/ground-item-stack-merge'
@@ -19,7 +19,7 @@ import {
19
19
FLOWER_MAX_STACK,
20
20
mixFlowersForDye
21
21
} from "../../../shared/definitions/dyes.js";
22
import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js";
22
import { bagView, placeOrMergeGroundStack } from "../items/bags.js";
23
23
import {
24
24
countItemInSlots,
25
25
emptySlots,
@@ -389,27 +389,15 @@ function grantEmptyGlassBottles (world, handle, characterId, quantity, events, n
389
389
if (remainder <= 0)
390
390
{ return; }
391
391
const position = world.ecs.get(handle, Component.position);
392
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
393
const item = {
394
groundItemId,
392
const placed = placeOrMergeGroundStack(world, {
395
393
tileX: position.tileX,
396
394
tileY: position.tileY,
397
395
stack: { itemId: ItemId.glassBottle, quantity: remainder },
398
droppedAt: now,
399
expiresAt: now + (world.settings.groundItemDespawnDurationMs ?? 600_000),
396
now,
400
397
source: "alchemy-overflow",
401
398
droppedByCharacterId: characterId
402
};
403
addGroundItem(world, item);
404
events.push({
405
type: "ground-item-appear",
406
...publicGroundItem(item),
407
stack: item.stack,
408
droppedAt: item.droppedAt,
409
source: item.source,
410
droppedByCharacterId: item.droppedByCharacterId,
411
expiresAt: item.expiresAt
412
399
});
400
events.push(...placed.events);
413
401
events.push({
414
402
type: "toast",
415
403
sessionId: null,
@@ -19,7 +19,7 @@ import {19
FLOWER_MAX_STACK,19
FLOWER_MAX_STACK,20
mixFlowersForDye20
mixFlowersForDye21
} from "../../../shared/definitions/dyes.js";21
} from "../../../shared/definitions/dyes.js";22
import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js";22
import { bagView, placeOrMergeGroundStack } from "../items/bags.js";23
import {23
import {24
countItemInSlots,24
countItemInSlots,25
emptySlots,25
emptySlots,@@ -389,27 +389,15 @@ function grantEmptyGlassBottles (world, handle, characterId, quantity, events, n389
if (remainder <= 0)389
if (remainder <= 0)390
{ return; }390
{ return; }391
const position = world.ecs.get(handle, Component.position);391
const position = world.ecs.get(handle, Component.position);392
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);392
const placed = placeOrMergeGroundStack(world, {393
const item = {394
groundItemId,395
tileX: position.tileX,393
tileX: position.tileX,396
tileY: position.tileY,394
tileY: position.tileY,397
stack: { itemId: ItemId.glassBottle, quantity: remainder },395
stack: { itemId: ItemId.glassBottle, quantity: remainder },398
droppedAt: now,396
now,399
expiresAt: now + (world.settings.groundItemDespawnDurationMs ?? 600_000),400
source: "alchemy-overflow",397
source: "alchemy-overflow",401
droppedByCharacterId: characterId398
droppedByCharacterId: characterId402
};403
addGroundItem(world, item);404
events.push({405
type: "ground-item-appear",406
...publicGroundItem(item),407
stack: item.stack,408
droppedAt: item.droppedAt,409
source: item.source,410
droppedByCharacterId: item.droppedByCharacterId,411
expiresAt: item.expiresAt412
});399
});400
events.push(...placed.events);413
events.push({401
events.push({414
type: "toast",402
type: "toast",415
sessionId: null,403
sessionId: null,@@ -67,7 +67,7 @@ import { tileToChunk } from "../world/coordinates.js";
67
67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";
68
68
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
69
69
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";
70
import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";
70
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";
71
71
import { resolveItemDualTint } from "../../../shared/render/tint.js";
72
72
import { essenceItemIdForElement } from "../../../shared/definitions/essenceItems.js";
73
73
import { castMobAttackSpell } from "../spells/mobCast.js";
@@ -659,25 +659,14 @@ function spawnMobDrops (world, targetHandle, tileX, tileY, now, attackerId = nul
659
659
});
660
660
if (tryAutoLootToKiller(world, attackerId, stack, events))
661
661
{ continue; }
662
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
663
const item = {
664
groundItemId,
662
const placed = placeOrMergeGroundStack(world, {
665
663
tileX,
666
664
tileY,
667
665
stack,
668
droppedAt: now,
669
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),
666
now,
670
667
source: "mob-drop"
671
};
672
addGroundItem(world, item);
673
events.push({
674
type: "ground-item-appear",
675
...publicGroundItem(item),
676
stack: item.stack,
677
droppedAt: item.droppedAt,
678
source: item.source,
679
expiresAt: item.expiresAt
680
668
});
669
events.push(...placed.events);
681
670
}
682
671
return events;
683
672
}
@@ -67,7 +67,7 @@ import { tileToChunk } from "../world/coordinates.js";67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";68
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";68
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";69
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";69
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";70
import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";70
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";71
import { resolveItemDualTint } from "../../../shared/render/tint.js";71
import { resolveItemDualTint } from "../../../shared/render/tint.js";72
import { essenceItemIdForElement } from "../../../shared/definitions/essenceItems.js";72
import { essenceItemIdForElement } from "../../../shared/definitions/essenceItems.js";73
import { castMobAttackSpell } from "../spells/mobCast.js";73
import { castMobAttackSpell } from "../spells/mobCast.js";@@ -659,25 +659,14 @@ function spawnMobDrops (world, targetHandle, tileX, tileY, now, attackerId = nul659
});659
});660
if (tryAutoLootToKiller(world, attackerId, stack, events))660
if (tryAutoLootToKiller(world, attackerId, stack, events))661
{ continue; }661
{ continue; }662
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);662
const placed = placeOrMergeGroundStack(world, {663
const item = {664
groundItemId,665
tileX,663
tileX,666
tileY,664
tileY,667
stack,665
stack,668
droppedAt: now,666
now,669
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),670
source: "mob-drop"667
source: "mob-drop"671
};672
addGroundItem(world, item);673
events.push({674
type: "ground-item-appear",675
...publicGroundItem(item),676
stack: item.stack,677
droppedAt: item.droppedAt,678
source: item.source,679
expiresAt: item.expiresAt680
});668
});669
events.push(...placed.events);681
}670
}682
return events;671
return events;683
}672
}@@ -18,7 +18,7 @@ import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../share
18
18
import { rollSkill } from "../../../shared/combat/rolls.js";
19
19
import { stackQuantity } from "../../../shared/items/inventory.js";
20
20
import { attachOrigins, ashOrigin, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";
21
import { addGroundItem, publicGroundItem, removeGroundItem, bagView } from "../items/bags.js";
21
import { placeOrMergeGroundStack, 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";
24
24
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
@@ -193,7 +193,6 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)
193
193
if (!ashItemId)
194
194
{ return; }
195
195
const woodName = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;
196
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
197
196
const stack = attachOrigins({
198
197
itemId: ashItemId,
199
198
quantity: 1,
@@ -202,24 +201,14 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)
202
201
quantity: 1
203
202
}]
204
203
});
205
const item = {
206
groundItemId,
204
const placed = placeOrMergeGroundStack(world, {
207
205
tileX,
208
206
tileY,
209
207
stack,
210
droppedAt: now,
211
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),
208
now,
212
209
source: "campfire-ash"
213
};
214
addGroundItem(world, item);
215
events.push({
216
type: "ground-item-appear",
217
...publicGroundItem(item),
218
stack: item.stack,
219
droppedAt: item.droppedAt,
220
source: item.source,
221
expiresAt: item.expiresAt
222
210
});
211
events.push(...placed.events);
223
212
}
224
213
225
214
/**
@@ -18,7 +18,7 @@ import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../share18
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, normalizeOrigins, splitOrigins } 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 { placeOrMergeGroundStack, 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";24
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";24
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";@@ -193,7 +193,6 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)193
if (!ashItemId)193
if (!ashItemId)194
{ return; }194
{ return; }195
const woodName = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;195
const woodName = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;196
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);197
const stack = attachOrigins({196
const stack = attachOrigins({198
itemId: ashItemId,197
itemId: ashItemId,199
quantity: 1,198
quantity: 1,@@ -202,24 +201,14 @@ function spawnAsh (world, tileX, tileY, woodTier, litByName, now, events)202
quantity: 1201
quantity: 1203
}]202
}]204
});203
});205
const item = {204
const placed = placeOrMergeGroundStack(world, {206
groundItemId,207
tileX,205
tileX,208
tileY,206
tileY,209
stack,207
stack,210
droppedAt: now,208
now,211
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),212
source: "campfire-ash"209
source: "campfire-ash"213
};214
addGroundItem(world, item);215
events.push({216
type: "ground-item-appear",217
...publicGroundItem(item),218
stack: item.stack,219
droppedAt: item.droppedAt,220
source: item.source,221
expiresAt: item.expiresAt222
});210
});211
events.push(...placed.events);223
}212
}224
213
225
/**214
/**@@ -40,7 +40,7 @@ import {
40
40
takeFromSlot
41
41
} from "../../../shared/items/inventory.js";
42
42
import { attachOrigins, gatherOrigin, GatherOriginVerb } from "../../../shared/items/origin.js";
43
import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";
43
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";
44
44
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
45
45
import { tileToChunk, tileToLocal } from "../world/coordinates.js";
46
46
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
@@ -491,30 +491,19 @@ export function stepFishing (world, now)
491
491
492
492
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break")
493
493
{
494
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
495
494
const stack = attachOrigins({
496
495
itemId,
497
496
quantity: 1,
498
497
origins: []
499
498
});
500
const item = {
501
groundItemId,
499
const placed = placeOrMergeGroundStack(world, {
502
500
tileX,
503
501
tileY,
504
502
stack,
505
droppedAt: now,
506
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),
503
now,
507
504
source
508
};
509
addGroundItem(world, item);
510
events.push({
511
type: "ground-item-appear",
512
...publicGroundItem(item),
513
stack: item.stack,
514
droppedAt: item.droppedAt,
515
source: item.source,
516
expiresAt: item.expiresAt
517
505
});
506
events.push(...placed.events);
518
507
}
519
508
520
509
/**
@@ -40,7 +40,7 @@ import {40
takeFromSlot40
takeFromSlot41
} from "../../../shared/items/inventory.js";41
} from "../../../shared/items/inventory.js";42
import { attachOrigins, gatherOrigin, GatherOriginVerb } from "../../../shared/items/origin.js";42
import { attachOrigins, gatherOrigin, GatherOriginVerb } from "../../../shared/items/origin.js";43
import { addGroundItem, publicGroundItem, bagView } from "../items/bags.js";43
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";44
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";44
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";45
import { tileToChunk, tileToLocal } from "../world/coordinates.js";45
import { tileToChunk, tileToLocal } from "../world/coordinates.js";46
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";46
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";@@ -491,30 +491,19 @@ export function stepFishing (world, now)491
491
492
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break")492
function spawnGroundStack (world, tileX, tileY, itemId, now, events, source = "fishing-line-break")493
{493
{494
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);495
const stack = attachOrigins({494
const stack = attachOrigins({496
itemId,495
itemId,497
quantity: 1,496
quantity: 1,498
origins: []497
origins: []499
});498
});500
const item = {499
const placed = placeOrMergeGroundStack(world, {501
groundItemId,502
tileX,500
tileX,503
tileY,501
tileY,504
stack,502
stack,505
droppedAt: now,503
now,506
expiresAt: now + (world.settings?.groundItemDespawnDurationMs ?? 600_000),507
source504
source508
};509
addGroundItem(world, item);510
events.push({511
type: "ground-item-appear",512
...publicGroundItem(item),513
stack: item.stack,514
droppedAt: item.droppedAt,515
source: item.source,516
expiresAt: item.expiresAt517
});505
});506
events.push(...placed.events);518
}507
}519
508
520
/**509
/**@@ -55,7 +55,7 @@ import {
55
55
SeasonId,
56
56
scaleRegrowDurationMs
57
57
} from "../../../shared/world/season.js";
58
import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js";
58
import { bagView, placeOrMergeGroundStack, publicGroundItem } from "../items/bags.js";
59
59
import { clearEngage } from "../combat/service.js";
60
60
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";
61
61
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";
@@ -842,7 +842,6 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
842
842
instance: debarkedLogInstance(),
843
843
origins: split.taken
844
844
});
845
const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;
846
845
847
846
if (qty === 1)
848
847
{
@@ -859,22 +858,18 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
859
858
};
860
859
events.push(publishGroundItem(item));
861
860
862
const debarkedItem = {
863
groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),
861
const placedDebarked = placeOrMergeGroundStack(world, {
864
862
tileX: item.tileX,
865
863
tileY: item.tileY,
866
864
stack: debarkedStack,
867
droppedAt: now,
868
expiresAt: now + despawnMs,
865
now,
869
866
source: "debark",
870
867
droppedByCharacterId: character.characterId
871
};
872
addGroundItem(world, debarkedItem);
873
events.push(publishGroundItem(debarkedItem));
868
});
869
events.push(...placedDebarked.events);
874
870
}
875
871
876
const barkItem = {
877
groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),
872
const placedBark = placeOrMergeGroundStack(world, {
878
873
tileX: item.tileX,
879
874
tileY: item.tileY,
880
875
stack: attachOrigins({
@@ -882,13 +877,11 @@ function completeDebark (world, handle, entityId, character, gather, events, pri
882
877
quantity: 1,
883
878
origins: [{ origin, quantity: 1 }]
884
879
}),
885
droppedAt: now,
886
expiresAt: now + despawnMs,
880
now,
887
881
source: "debark",
888
882
droppedByCharacterId: character.characterId
889
};
890
addGroundItem(world, barkItem);
891
events.push(publishGroundItem(barkItem));
883
});
884
events.push(...placedBark.events);
892
885
893
886
const skills = world.ecs.get(handle, Component.skills);
894
887
let skillsChanged = false;
@@ -55,7 +55,7 @@ import {55
SeasonId,55
SeasonId,56
scaleRegrowDurationMs56
scaleRegrowDurationMs57
} from "../../../shared/world/season.js";57
} from "../../../shared/world/season.js";58
import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js";58
import { bagView, placeOrMergeGroundStack, publicGroundItem } from "../items/bags.js";59
import { clearEngage } from "../combat/service.js";59
import { clearEngage } from "../combat/service.js";60
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";60
import { isAdjacentTile } from "../../../shared/navigation/adjacency.js";61
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";61
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";@@ -842,7 +842,6 @@ function completeDebark (world, handle, entityId, character, gather, events, pri842
instance: debarkedLogInstance(),842
instance: debarkedLogInstance(),843
origins: split.taken843
origins: split.taken844
});844
});845
const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000;846
845
847
if (qty === 1)846
if (qty === 1)848
{847
{@@ -859,22 +858,18 @@ function completeDebark (world, handle, entityId, character, gather, events, pri859
};858
};860
events.push(publishGroundItem(item));859
events.push(publishGroundItem(item));861
860
862
const debarkedItem = {861
const placedDebarked = placeOrMergeGroundStack(world, {863
groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),864
tileX: item.tileX,862
tileX: item.tileX,865
tileY: item.tileY,863
tileY: item.tileY,866
stack: debarkedStack,864
stack: debarkedStack,867
droppedAt: now,865
now,868
expiresAt: now + despawnMs,869
source: "debark",866
source: "debark",870
droppedByCharacterId: character.characterId867
droppedByCharacterId: character.characterId871
};868
});872
addGroundItem(world, debarkedItem);869
events.push(...placedDebarked.events);873
events.push(publishGroundItem(debarkedItem));874
}870
}875
871
876
const barkItem = {872
const placedBark = placeOrMergeGroundStack(world, {877
groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1),878
tileX: item.tileX,873
tileX: item.tileX,879
tileY: item.tileY,874
tileY: item.tileY,880
stack: attachOrigins({875
stack: attachOrigins({@@ -882,13 +877,11 @@ function completeDebark (world, handle, entityId, character, gather, events, pri882
quantity: 1,877
quantity: 1,883
origins: [{ origin, quantity: 1 }]878
origins: [{ origin, quantity: 1 }]884
}),879
}),885
droppedAt: now,880
now,886
expiresAt: now + despawnMs,887
source: "debark",881
source: "debark",888
droppedByCharacterId: character.characterId882
droppedByCharacterId: character.characterId889
};883
});890
addGroundItem(world, barkItem);884
events.push(...placedBark.events);891
events.push(publishGroundItem(barkItem));892
885
893
const skills = world.ecs.get(handle, Component.skills);886
const skills = world.ecs.get(handle, Component.skills);894
let skillsChanged = false;887
let skillsChanged = false;@@ -22,7 +22,8 @@ import
22
22
singleItemFromStack,
23
23
stackQuantity,
24
24
consumeFillCharge,
25
moveInventorySlot
25
moveInventorySlot,
26
stacksMatch
26
27
} from "../../../shared/items/inventory.js";
27
28
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";
28
29
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
@@ -42,7 +43,8 @@ import
42
43
writtenOrigin,
43
44
bankIssuedOrigin,
44
45
normalizeOrigins,
45
mergeOriginLists
46
mergeOriginLists,
47
splitOrigins
46
48
} from "../../../shared/items/origin.js";
47
49
import { BANK_LETTER_AUTHOR } from "../../../shared/courier/systemLetters.js";
48
50
import { canCharacterPickupGroundItem } from "../../../shared/items/letters.js";
@@ -257,6 +259,120 @@ export function setGroundItemsForRegion (world, regionX, regionY, items, tileToR
257
259
{ addGroundItem(world, item); }
258
260
}
259
261
262
export function groundItemAppearEvent (item)
263
{
264
return {
265
type: "ground-item-appear",
266
...publicGroundItem(item),
267
stack: item.stack,
268
droppedAt: item.droppedAt,
269
source: item.source,
270
droppedByCharacterId: item.droppedByCharacterId,
271
expiresAt: item.expiresAt
272
};
273
}
274
275
function allocateGroundItemId (world, preferredId)
276
{
277
if (preferredId != null)
278
{ return preferredId | 0; }
279
return (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
280
}
281
282
/**
283
* Place a stack on the ground, merging into matching piles on the same tile
284
* using bag stacking rules. Successful merges reset the despawn timer.
285
*
286
* @returns {{ events: object[] }}
287
*/
288
export function placeOrMergeGroundStack (world, {
289
tileX,
290
tileY,
291
stack,
292
now,
293
source = "world",
294
droppedByCharacterId = null,
295
groundItemId = null,
296
despawnDurationMs = null
297
})
298
{
299
const events = [];
300
const quantity = stackQuantity(stack?.quantity);
301
if (!stack || quantity <= 0)
302
{ return { events }; }
303
304
const def = itemDefinition(stack.itemId);
305
const rules = stackRulesFor(def);
306
const duration = despawnDurationMs
307
?? world.settings?.groundItemDespawnDurationMs
308
?? 600_000;
309
let remaining = quantity;
310
let pendingOrigins = normalizeOrigins(stack);
311
let preferredId = groundItemId;
312
313
if (rules.stackable && !rules.uniqueInstance)
314
{
315
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY)) ?? [])];
316
for (const id of ids)
317
{
318
const existing = world.groundItems.get(id);
319
if (!existing?.stack || !stacksMatch(existing.stack, stack, def))
320
{ continue; }
321
const space = rules.maxStackSize - stackQuantity(existing.stack.quantity);
322
if (space <= 0)
323
{ continue; }
324
const add = Math.min(space, remaining);
325
const split = splitOrigins(pendingOrigins, add);
326
existing.stack.origins = mergeOriginLists(normalizeOrigins(existing.stack), split.taken);
327
existing.stack.quantity = stackQuantity(existing.stack.quantity) + add;
328
if (stack.instance != null && existing.stack.instance == null)
329
{ existing.stack.instance = cloneStack(stack.instance); }
330
delete existing.stack.origin;
331
existing.droppedAt = now;
332
existing.expiresAt = now + duration;
333
pendingOrigins = split.remain;
334
remaining -= add;
335
events.push(groundItemAppearEvent(existing));
336
if (remaining === 0)
337
{ return { events }; }
338
}
339
}
340
341
while (remaining > 0)
342
{
343
const cap = rules.maxStackSize === Number.POSITIVE_INFINITY
344
? remaining
345
: rules.maxStackSize;
346
const amount = rules.stackable && !rules.uniqueInstance
347
? Math.min(remaining, cap)
348
: 1;
349
const split = splitOrigins(pendingOrigins, amount);
350
const item = {
351
groundItemId: allocateGroundItemId(world, preferredId),
352
tileX,
353
tileY,
354
stack: {
355
itemId: stack.itemId,
356
quantity: amount,
357
instance: stack.instance ? cloneStack(stack.instance) : null,
358
origins: split.taken
359
},
360
droppedAt: now,
361
expiresAt: now + duration,
362
source,
363
droppedByCharacterId
364
};
365
preferredId = null;
366
addGroundItem(world, item);
367
events.push(groundItemAppearEvent(item));
368
pendingOrigins = split.remain;
369
remaining -= amount;
370
if (!rules.stackable || rules.uniqueInstance)
371
{ break; }
372
}
373
return { events };
374
}
375
260
376
function dropItem (world, sessionId, command, now)
261
377
{
262
378
const { handle } = world.requireOwnedEntity(sessionId, command.characterId);
@@ -266,19 +382,17 @@ function dropItem (world, sessionId, command, now)
266
382
{ throw userError("That inventory slot is empty."); }
267
383
inventory.slots = taken.slots;
268
384
const position = world.ecs.get(handle, Component.position);
269
const item = {
270
groundItemId: command.groundItemId,
385
const placed = placeOrMergeGroundStack(world, {
271
386
tileX: position.tileX,
272
387
tileY: position.tileY,
273
388
stack: taken.taken,
274
droppedAt: now,
275
expiresAt: now + (world.settings.groundItemDespawnDurationMs ?? 600_000),
389
now,
276
390
source: "player-drop",
277
droppedByCharacterId: command.characterId
278
};
279
addGroundItem(world, item);
391
droppedByCharacterId: command.characterId,
392
groundItemId: command.groundItemId
393
});
280
394
return {
281
events: [{ type: "ground-item-appear", ...publicGroundItem(item), stack: item.stack, droppedAt: item.droppedAt, source: item.source, droppedByCharacterId: item.droppedByCharacterId, expiresAt: item.expiresAt }],
395
events: placed.events,
282
396
private: bagView(world.ecs, handle)
283
397
};
284
398
}
@@ -22,7 +22,8 @@ import22
singleItemFromStack,22
singleItemFromStack,23
stackQuantity,23
stackQuantity,24
consumeFillCharge,24
consumeFillCharge,25
moveInventorySlot25
moveInventorySlot,26
stacksMatch26
} from "../../../shared/items/inventory.js";27
} from "../../../shared/items/inventory.js";27
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";28
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";28
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";29
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";@@ -42,7 +43,8 @@ import42
writtenOrigin,43
writtenOrigin,43
bankIssuedOrigin,44
bankIssuedOrigin,44
normalizeOrigins,45
normalizeOrigins,45
mergeOriginLists46
mergeOriginLists,47
splitOrigins46
} from "../../../shared/items/origin.js";48
} from "../../../shared/items/origin.js";47
import { BANK_LETTER_AUTHOR } from "../../../shared/courier/systemLetters.js";49
import { BANK_LETTER_AUTHOR } from "../../../shared/courier/systemLetters.js";48
import { canCharacterPickupGroundItem } from "../../../shared/items/letters.js";50
import { canCharacterPickupGroundItem } from "../../../shared/items/letters.js";@@ -257,6 +259,120 @@ export function setGroundItemsForRegion (world, regionX, regionY, items, tileToR257
{ addGroundItem(world, item); }259
{ addGroundItem(world, item); }258
}260
}259
261
262
export function groundItemAppearEvent (item)263
{264
return {265
type: "ground-item-appear",266
...publicGroundItem(item),267
stack: item.stack,268
droppedAt: item.droppedAt,269
source: item.source,270
droppedByCharacterId: item.droppedByCharacterId,271
expiresAt: item.expiresAt272
};273
}274
275
function allocateGroundItemId (world, preferredId)276
{277
if (preferredId != null)278
{ return preferredId | 0; }279
return (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);280
}281
282
/**283
* Place a stack on the ground, merging into matching piles on the same tile284
* using bag stacking rules. Successful merges reset the despawn timer.285
*286
* @returns {{ events: object[] }}287
*/288
export function placeOrMergeGroundStack (world, {289
tileX,290
tileY,291
stack,292
now,293
source = "world",294
droppedByCharacterId = null,295
groundItemId = null,296
despawnDurationMs = null297
})298
{299
const events = [];300
const quantity = stackQuantity(stack?.quantity);301
if (!stack || quantity <= 0)302
{ return { events }; }303
304
const def = itemDefinition(stack.itemId);305
const rules = stackRulesFor(def);306
const duration = despawnDurationMs307
?? world.settings?.groundItemDespawnDurationMs308
?? 600_000;309
let remaining = quantity;310
let pendingOrigins = normalizeOrigins(stack);311
let preferredId = groundItemId;312
313
if (rules.stackable && !rules.uniqueInstance)314
{315
const ids = [...(world.groundByTile.get(tileKey(tileX, tileY)) ?? [])];316
for (const id of ids)317
{318
const existing = world.groundItems.get(id);319
if (!existing?.stack || !stacksMatch(existing.stack, stack, def))320
{ continue; }321
const space = rules.maxStackSize - stackQuantity(existing.stack.quantity);322
if (space <= 0)323
{ continue; }324
const add = Math.min(space, remaining);325
const split = splitOrigins(pendingOrigins, add);326
existing.stack.origins = mergeOriginLists(normalizeOrigins(existing.stack), split.taken);327
existing.stack.quantity = stackQuantity(existing.stack.quantity) + add;328
if (stack.instance != null && existing.stack.instance == null)329
{ existing.stack.instance = cloneStack(stack.instance); }330
delete existing.stack.origin;331
existing.droppedAt = now;332
existing.expiresAt = now + duration;333
pendingOrigins = split.remain;334
remaining -= add;335
events.push(groundItemAppearEvent(existing));336
if (remaining === 0)337
{ return { events }; }338
}339
}340
341
while (remaining > 0)342
{343
const cap = rules.maxStackSize === Number.POSITIVE_INFINITY344
? remaining345
: rules.maxStackSize;346
const amount = rules.stackable && !rules.uniqueInstance347
? Math.min(remaining, cap)348
: 1;349
const split = splitOrigins(pendingOrigins, amount);350
const item = {351
groundItemId: allocateGroundItemId(world, preferredId),352
tileX,353
tileY,354
stack: {355
itemId: stack.itemId,356
quantity: amount,357
instance: stack.instance ? cloneStack(stack.instance) : null,358
origins: split.taken359
},360
droppedAt: now,361
expiresAt: now + duration,362
source,363
droppedByCharacterId364
};365
preferredId = null;366
addGroundItem(world, item);367
events.push(groundItemAppearEvent(item));368
pendingOrigins = split.remain;369
remaining -= amount;370
if (!rules.stackable || rules.uniqueInstance)371
{ break; }372
}373
return { events };374
}375
260
function dropItem (world, sessionId, command, now)376
function dropItem (world, sessionId, command, now)261
{377
{262
const { handle } = world.requireOwnedEntity(sessionId, command.characterId);378
const { handle } = world.requireOwnedEntity(sessionId, command.characterId);@@ -266,19 +382,17 @@ function dropItem (world, sessionId, command, now)266
{ throw userError("That inventory slot is empty."); }382
{ throw userError("That inventory slot is empty."); }267
inventory.slots = taken.slots;383
inventory.slots = taken.slots;268
const position = world.ecs.get(handle, Component.position);384
const position = world.ecs.get(handle, Component.position);269
const item = {385
const placed = placeOrMergeGroundStack(world, {270
groundItemId: command.groundItemId,271
tileX: position.tileX,386
tileX: position.tileX,272
tileY: position.tileY,387
tileY: position.tileY,273
stack: taken.taken,388
stack: taken.taken,274
droppedAt: now,389
now,275
expiresAt: now + (world.settings.groundItemDespawnDurationMs ?? 600_000),276
source: "player-drop",390
source: "player-drop",277
droppedByCharacterId: command.characterId391
droppedByCharacterId: command.characterId,278
};392
groundItemId: command.groundItemId279
addGroundItem(world, item);393
});280
return {394
return {281
events: [{ type: "ground-item-appear", ...publicGroundItem(item), stack: item.stack, droppedAt: item.droppedAt, source: item.source, droppedByCharacterId: item.droppedByCharacterId, expiresAt: item.expiresAt }],395
events: placed.events,282
private: bagView(world.ecs, handle)396
private: bagView(world.ecs, handle)283
};397
};284
}398
}@@ -24,6 +24,7 @@ import {
24
24
bagView,
25
25
applyBagView,
26
26
addGroundItem,
27
placeOrMergeGroundStack,
27
28
expireGroundItems,
28
29
groundItemsInChunk,
29
30
groundItemsInRegion,
@@ -604,26 +605,16 @@ export class WorkerWorld
604
605
for (const drop of drops)
605
606
{
606
607
const stack = drop.stack ?? { itemId: drop.itemId, quantity: 1 };
607
const item = {
608
groundItemId: drop.groundItemId,
608
const placed = placeOrMergeGroundStack(this, {
609
609
tileX: message.tileX,
610
610
tileY: message.tileY,
611
611
stack,
612
droppedAt: message.now,
613
expiresAt: message.now + (this.settings.groundItemDespawnDurationMs ?? 600_000),
612
now: message.now,
614
613
source: "player-death",
615
droppedByCharacterId: message.characterId
616
};
617
addGroundItem(this, item);
618
events.push({
619
type: "ground-item-appear",
620
...publicGroundItem(item),
621
stack: item.stack,
622
droppedAt: item.droppedAt,
623
source: item.source,
624
droppedByCharacterId: item.droppedByCharacterId,
625
expiresAt: item.expiresAt
614
droppedByCharacterId: message.characterId,
615
groundItemId: drop.groundItemId
626
616
});
617
events.push(...placed.events);
627
618
}
628
619
return { events, private: privateBag };
629
620
}
@@ -24,6 +24,7 @@ import {24
bagView,24
bagView,25
applyBagView,25
applyBagView,26
addGroundItem,26
addGroundItem,27
placeOrMergeGroundStack,27
expireGroundItems,28
expireGroundItems,28
groundItemsInChunk,29
groundItemsInChunk,29
groundItemsInRegion,30
groundItemsInRegion,@@ -604,26 +605,16 @@ export class WorkerWorld604
for (const drop of drops)605
for (const drop of drops)605
{606
{606
const stack = drop.stack ?? { itemId: drop.itemId, quantity: 1 };607
const stack = drop.stack ?? { itemId: drop.itemId, quantity: 1 };607
const item = {608
const placed = placeOrMergeGroundStack(this, {608
groundItemId: drop.groundItemId,609
tileX: message.tileX,609
tileX: message.tileX,610
tileY: message.tileY,610
tileY: message.tileY,611
stack,611
stack,612
droppedAt: message.now,612
now: message.now,613
expiresAt: message.now + (this.settings.groundItemDespawnDurationMs ?? 600_000),614
source: "player-death",613
source: "player-death",615
droppedByCharacterId: message.characterId614
droppedByCharacterId: message.characterId,616
};615
groundItemId: drop.groundItemId617
addGroundItem(this, item);618
events.push({619
type: "ground-item-appear",620
...publicGroundItem(item),621
stack: item.stack,622
droppedAt: item.droppedAt,623
source: item.source,624
droppedByCharacterId: item.droppedByCharacterId,625
expiresAt: item.expiresAt626
});616
});617
events.push(...placed.events);627
}618
}628
return { events, private: privateBag };619
return { events, private: privateBag };629
}620
}@@ -13,7 +13,7 @@ import { stackQuantity } from "../../../shared/items/inventory.js";
13
13
import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";
14
14
import
15
15
{
16
addGroundItem,
16
placeOrMergeGroundStack,
17
17
publicGroundItem,
18
18
removeGroundItem
19
19
} from "../items/bags.js";
@@ -133,25 +133,15 @@ function cookedItemIdForRaw (rawDef)
133
133
134
134
function spawnGroundStack (world, tileX, tileY, stack, now, events)
135
135
{
136
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
137
const item = {
138
groundItemId,
136
const placed = placeOrMergeGroundStack(world, {
139
137
tileX,
140
138
tileY,
141
139
stack,
142
droppedAt: now,
140
now,
143
141
source: "tinders",
144
expiresAt: now + 1000 * 60 * 30
145
};
146
addGroundItem(world, item);
147
events.push({
148
type: "ground-item-appear",
149
...publicGroundItem(item),
150
stack: item.stack,
151
droppedAt: item.droppedAt,
152
source: item.source,
153
expiresAt: item.expiresAt
142
despawnDurationMs: 1000 * 60 * 30
154
143
});
144
events.push(...placed.events);
155
145
}
156
146
157
147
/**
@@ -13,7 +13,7 @@ import { stackQuantity } from "../../../shared/items/inventory.js";13
import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";13
import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js";14
import14
import15
{15
{16
addGroundItem,16
placeOrMergeGroundStack,17
publicGroundItem,17
publicGroundItem,18
removeGroundItem18
removeGroundItem19
} from "../items/bags.js";19
} from "../items/bags.js";@@ -133,25 +133,15 @@ function cookedItemIdForRaw (rawDef)133
133
134
function spawnGroundStack (world, tileX, tileY, stack, now, events)134
function spawnGroundStack (world, tileX, tileY, stack, now, events)135
{135
{136
const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);136
const placed = placeOrMergeGroundStack(world, {137
const item = {138
groundItemId,139
tileX,137
tileX,140
tileY,138
tileY,141
stack,139
stack,142
droppedAt: now,140
now,143
source: "tinders",141
source: "tinders",144
expiresAt: now + 1000 * 60 * 30142
despawnDurationMs: 1000 * 60 * 30145
};146
addGroundItem(world, item);147
events.push({148
type: "ground-item-appear",149
...publicGroundItem(item),150
stack: item.stack,151
droppedAt: item.droppedAt,152
source: item.source,153
expiresAt: item.expiresAt154
});143
});144
events.push(...placed.events);155
}145
}156
146
157
/**147
/**@@ -0,0 +1,192 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
4
import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
5
import
6
{
7
addGroundItem,
8
placeOrMergeGroundStack
9
} from "../src/items/bags.js";
10
import
11
{
12
makeCharacter,
13
makeSimulation
14
} from "./helpers/simulationHarness.js";
15
16
function emptyWorld (settings = {})
17
{
18
return {
19
groundItems: new Map(),
20
groundByTile: new Map(),
21
nextGroundItemId: 1,
22
settings: {
23
groundItemDespawnDurationMs: 600_000,
24
...settings
25
}
26
};
27
}
28
29
function giveInventory (harness, entityId, items)
30
{
31
const inventory = emptySlots();
32
items.forEach((stack, index) =>
33
{
34
inventory[index] = {
35
itemId: stack.itemId,
36
quantity: stack.quantity,
37
...(stack.instance ? { instance: structuredClone(stack.instance) } : {})
38
};
39
});
40
harness.simulation.applyBag(entityId, {
41
inventory,
42
equipment: emptyEquipment(),
43
vault: [],
44
vaultReserved: []
45
});
46
}
47
48
test("placeOrMergeGroundStack merges matching piles and resets despawn", () =>
49
{
50
const world = emptyWorld();
51
const now = 10_000;
52
addGroundItem(world, {
53
groundItemId: 1,
54
tileX: 4,
55
tileY: 5,
56
stack: { itemId: ItemId.coins, quantity: 25 },
57
droppedAt: 1_000,
58
expiresAt: 5_000,
59
source: "mob-drop"
60
});
61
62
const placed = placeOrMergeGroundStack(world, {
63
tileX: 4,
64
tileY: 5,
65
stack: { itemId: ItemId.coins, quantity: 10 },
66
now,
67
source: "player-drop",
68
droppedByCharacterId: 7,
69
groundItemId: 99
70
});
71
72
assert.equal(world.groundItems.size, 1);
73
const pile = world.groundItems.get(1);
74
assert.equal(pile.stack.quantity, 35);
75
assert.equal(pile.expiresAt, now + 600_000);
76
assert.equal(pile.droppedAt, now);
77
assert.equal(pile.source, "mob-drop");
78
assert.equal(placed.events.length, 1);
79
assert.equal(placed.events[0].groundItemId, 1);
80
assert.equal(placed.events[0].quantity, 35);
81
assert.equal(placed.events[0].expiresAt, now + 600_000);
82
assert.equal(world.groundItems.has(99), false);
83
});
84
85
test("placeOrMergeGroundStack overflows into a new pile at max stack", () =>
86
{
87
const world = emptyWorld();
88
const max = itemDefinition(ItemId.apple).maxStackSize ?? 99;
89
addGroundItem(world, {
90
groundItemId: 1,
91
tileX: 1,
92
tileY: 1,
93
stack: { itemId: ItemId.apple, quantity: max },
94
droppedAt: 1_000,
95
expiresAt: 2_000,
96
source: "player-drop"
97
});
98
99
const placed = placeOrMergeGroundStack(world, {
100
tileX: 1,
101
tileY: 1,
102
stack: { itemId: ItemId.apple, quantity: 5 },
103
now: 50_000,
104
source: "player-drop",
105
groundItemId: 2
106
});
107
108
assert.equal(world.groundItems.size, 2);
109
assert.equal(world.groundItems.get(1).stack.quantity, max);
110
assert.equal(world.groundItems.get(1).expiresAt, 2_000);
111
assert.equal(world.groundItems.get(2).stack.quantity, 5);
112
assert.equal(world.groundItems.get(2).expiresAt, 50_000 + 600_000);
113
assert.equal(placed.events.length, 1);
114
assert.equal(placed.events[0].groundItemId, 2);
115
});
116
117
test("placeOrMergeGroundStack does not merge non-stackable equipment", () =>
118
{
119
const world = emptyWorld();
120
const equipmentId = Object.values(ItemId).find((id) =>
121
{
122
const def = itemDefinition(id);
123
return def?.equipment && def.equipment.kind !== "arrowAmmo";
124
});
125
assert.ok(equipmentId);
126
addGroundItem(world, {
127
groundItemId: 1,
128
tileX: 2,
129
tileY: 2,
130
stack: { itemId: equipmentId, quantity: 1 },
131
droppedAt: 1,
132
expiresAt: 100,
133
source: "mob-drop"
134
});
135
placeOrMergeGroundStack(world, {
136
tileX: 2,
137
tileY: 2,
138
stack: { itemId: equipmentId, quantity: 1 },
139
now: 200,
140
source: "mob-drop",
141
groundItemId: 2
142
});
143
assert.equal(world.groundItems.size, 2);
144
});
145
146
test("dropping coins onto an existing ground coin pile merges and refreshes despawn", () =>
147
{
148
const harness = makeSimulation({ workerCount: 1 });
149
try
150
{
151
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
152
const entityId = harness.simulation.spawnCharacter(1, character);
153
giveInventory(harness, entityId, [
154
{ itemId: ItemId.coins, quantity: 40 }
155
]);
156
const bag = harness.simulation.bagOf(entityId);
157
const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins);
158
assert.ok(coinSlot >= 0);
159
160
harness.simulation.enqueue(1, {
161
type: "drop-item",
162
characterId: character.character_id,
163
slotIndex: coinSlot,
164
quantity: 15
165
});
166
const first = harness.simulation.step().events.find((event) => event.type === "ground-item-appear");
167
assert.ok(first);
168
assert.equal(first.quantity, 15);
169
const firstExpires = first.expiresAt;
170
const groundId = first.groundItemId;
171
172
harness.advance(5_000);
173
harness.simulation.enqueue(1, {
174
type: "drop-item",
175
characterId: character.character_id,
176
slotIndex: coinSlot,
177
quantity: 10
178
});
179
const secondEvents = harness.simulation.step().events.filter((event) => event.type === "ground-item-appear");
180
assert.equal(secondEvents.length, 1);
181
assert.equal(secondEvents[0].groundItemId, groundId);
182
assert.equal(secondEvents[0].quantity, 25);
183
assert.ok(secondEvents[0].expiresAt > firstExpires);
184
185
const snapshot = harness.simulation.snapshotChunk(0, 0);
186
const piles = (snapshot.groundItems ?? []).filter((item) => item.itemId === ItemId.coins);
187
assert.equal(piles.length, 1);
188
assert.equal(piles[0].quantity, 25);
189
}
190
finally
191
{ harness.close(); }
192
});
@@ -0,0 +1,192 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";4
import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";5
import6
{7
addGroundItem,8
placeOrMergeGroundStack9
} from "../src/items/bags.js";10
import11
{12
makeCharacter,13
makeSimulation14
} from "./helpers/simulationHarness.js";15
16
function emptyWorld (settings = {})17
{18
return {19
groundItems: new Map(),20
groundByTile: new Map(),21
nextGroundItemId: 1,22
settings: {23
groundItemDespawnDurationMs: 600_000,24
...settings25
}26
};27
}28
29
function giveInventory (harness, entityId, items)30
{31
const inventory = emptySlots();32
items.forEach((stack, index) =>33
{34
inventory[index] = {35
itemId: stack.itemId,36
quantity: stack.quantity,37
...(stack.instance ? { instance: structuredClone(stack.instance) } : {})38
};39
});40
harness.simulation.applyBag(entityId, {41
inventory,42
equipment: emptyEquipment(),43
vault: [],44
vaultReserved: []45
});46
}47
48
test("placeOrMergeGroundStack merges matching piles and resets despawn", () =>49
{50
const world = emptyWorld();51
const now = 10_000;52
addGroundItem(world, {53
groundItemId: 1,54
tileX: 4,55
tileY: 5,56
stack: { itemId: ItemId.coins, quantity: 25 },57
droppedAt: 1_000,58
expiresAt: 5_000,59
source: "mob-drop"60
});61
62
const placed = placeOrMergeGroundStack(world, {63
tileX: 4,64
tileY: 5,65
stack: { itemId: ItemId.coins, quantity: 10 },66
now,67
source: "player-drop",68
droppedByCharacterId: 7,69
groundItemId: 9970
});71
72
assert.equal(world.groundItems.size, 1);73
const pile = world.groundItems.get(1);74
assert.equal(pile.stack.quantity, 35);75
assert.equal(pile.expiresAt, now + 600_000);76
assert.equal(pile.droppedAt, now);77
assert.equal(pile.source, "mob-drop");78
assert.equal(placed.events.length, 1);79
assert.equal(placed.events[0].groundItemId, 1);80
assert.equal(placed.events[0].quantity, 35);81
assert.equal(placed.events[0].expiresAt, now + 600_000);82
assert.equal(world.groundItems.has(99), false);83
});84
85
test("placeOrMergeGroundStack overflows into a new pile at max stack", () =>86
{87
const world = emptyWorld();88
const max = itemDefinition(ItemId.apple).maxStackSize ?? 99;89
addGroundItem(world, {90
groundItemId: 1,91
tileX: 1,92
tileY: 1,93
stack: { itemId: ItemId.apple, quantity: max },94
droppedAt: 1_000,95
expiresAt: 2_000,96
source: "player-drop"97
});98
99
const placed = placeOrMergeGroundStack(world, {100
tileX: 1,101
tileY: 1,102
stack: { itemId: ItemId.apple, quantity: 5 },103
now: 50_000,104
source: "player-drop",105
groundItemId: 2106
});107
108
assert.equal(world.groundItems.size, 2);109
assert.equal(world.groundItems.get(1).stack.quantity, max);110
assert.equal(world.groundItems.get(1).expiresAt, 2_000);111
assert.equal(world.groundItems.get(2).stack.quantity, 5);112
assert.equal(world.groundItems.get(2).expiresAt, 50_000 + 600_000);113
assert.equal(placed.events.length, 1);114
assert.equal(placed.events[0].groundItemId, 2);115
});116
117
test("placeOrMergeGroundStack does not merge non-stackable equipment", () =>118
{119
const world = emptyWorld();120
const equipmentId = Object.values(ItemId).find((id) =>121
{122
const def = itemDefinition(id);123
return def?.equipment && def.equipment.kind !== "arrowAmmo";124
});125
assert.ok(equipmentId);126
addGroundItem(world, {127
groundItemId: 1,128
tileX: 2,129
tileY: 2,130
stack: { itemId: equipmentId, quantity: 1 },131
droppedAt: 1,132
expiresAt: 100,133
source: "mob-drop"134
});135
placeOrMergeGroundStack(world, {136
tileX: 2,137
tileY: 2,138
stack: { itemId: equipmentId, quantity: 1 },139
now: 200,140
source: "mob-drop",141
groundItemId: 2142
});143
assert.equal(world.groundItems.size, 2);144
});145
146
test("dropping coins onto an existing ground coin pile merges and refreshes despawn", () =>147
{148
const harness = makeSimulation({ workerCount: 1 });149
try150
{151
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });152
const entityId = harness.simulation.spawnCharacter(1, character);153
giveInventory(harness, entityId, [154
{ itemId: ItemId.coins, quantity: 40 }155
]);156
const bag = harness.simulation.bagOf(entityId);157
const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins);158
assert.ok(coinSlot >= 0);159
160
harness.simulation.enqueue(1, {161
type: "drop-item",162
characterId: character.character_id,163
slotIndex: coinSlot,164
quantity: 15165
});166
const first = harness.simulation.step().events.find((event) => event.type === "ground-item-appear");167
assert.ok(first);168
assert.equal(first.quantity, 15);169
const firstExpires = first.expiresAt;170
const groundId = first.groundItemId;171
172
harness.advance(5_000);173
harness.simulation.enqueue(1, {174
type: "drop-item",175
characterId: character.character_id,176
slotIndex: coinSlot,177
quantity: 10178
});179
const secondEvents = harness.simulation.step().events.filter((event) => event.type === "ground-item-appear");180
assert.equal(secondEvents.length, 1);181
assert.equal(secondEvents[0].groundItemId, groundId);182
assert.equal(secondEvents[0].quantity, 25);183
assert.ok(secondEvents[0].expiresAt > firstExpires);184
185
const snapshot = harness.simulation.snapshotChunk(0, 0);186
const piles = (snapshot.groundItems ?? []).filter((item) => item.itemId === ItemId.coins);187
assert.equal(piles.length, 1);188
assert.equal(piles[0].quantity, 25);189
}190
finally191
{ harness.close(); }192
});