diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index 6b43785..dfcb6b3 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -1762,6 +1762,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler mob.DeathLoot = allItems; mob.DeathLootGold = totalGold; + // If mob has no loot, mark it for corpse removal + if (!mob.HasDeathLoot && dungeon != null) + { + mob.LootEmptiedAtTick = dungeon.Simulation.CurrentTick; + } + _log.LogDebug("Mob {Mob} died with {Count} items and {Gold} gold loot", mob.Name, allItems.Count, totalGold); } @@ -1963,83 +1969,78 @@ public sealed partial class DungeonHandler : IClientMessageHandler foreach (var drop in loot) { - // Check if item is stackable and already exists in inventory - var existingStack = existingItems.FirstOrDefault(i => - i.ItemDefinitionId == drop.ItemDefinitionId && - drop.Item.Stackable); + int remainingToAdd = drop.Quantity; - if (existingStack != null && drop.Item.Stackable) + // If item is stackable, try to fill existing stacks first + if (drop.Item.Stackable) { - int spaceInStack = drop.Item.MaxStack - existingStack.Quantity; - int toAdd = Math.Min(drop.Quantity, spaceInStack); + // Find all existing stacks of this item that have room + var existingStacks = existingItems + .Where(i => i.ItemDefinitionId == drop.ItemDefinitionId && i.Quantity < drop.Item.MaxStack) + .OrderBy(i => i.Quantity) // Fill least-full stacks first + .ToList(); - if (toAdd > 0) + foreach (var stack in existingStacks) { - existingStack.Quantity += toAdd; - added.Add(new LootDrop - { - ItemDefinitionId = drop.ItemDefinitionId, - Item = drop.Item, - Quantity = toAdd - }); - } + if (remainingToAdd <= 0) break; - int remaining = drop.Quantity - toAdd; - if (remaining > 0) - { - // Try to create a new stack for remaining items - int nextSlot = FindNextSlot(); - if (nextSlot >= 0) + int spaceInStack = drop.Item.MaxStack - stack.Quantity; + int toAdd = Math.Min(remainingToAdd, spaceInStack); + + if (toAdd > 0) { - var newItem = new DungeonCharacterItem - { - CharacterId = session.CharacterId, - ItemDefinitionId = drop.ItemDefinitionId, - Quantity = remaining, - SlotIndex = nextSlot - }; - db.Set().Add(newItem); - existingItems.Add(newItem); + stack.Quantity += toAdd; + remainingToAdd -= toAdd; added.Add(new LootDrop { ItemDefinitionId = drop.ItemDefinitionId, Item = drop.Item, - Quantity = remaining - }); - } - else - { - // Inventory full - overflow - overflow.Add(new LootDrop - { - ItemDefinitionId = drop.ItemDefinitionId, - Item = drop.Item, - Quantity = remaining + Quantity = toAdd }); } } } - else + + // If there's still quantity remaining, create new stack(s) + while (remainingToAdd > 0) { int nextSlot = FindNextSlot(); if (nextSlot >= 0) { - // Create new inventory entry + // Determine how much to put in this new stack (up to max stack size) + int quantityForNewStack = drop.Item.Stackable + ? Math.Min(remainingToAdd, drop.Item.MaxStack) + : remainingToAdd; + var newItem = new DungeonCharacterItem { CharacterId = session.CharacterId, ItemDefinitionId = drop.ItemDefinitionId, - Quantity = drop.Quantity, + Quantity = quantityForNewStack, SlotIndex = nextSlot }; db.Set().Add(newItem); existingItems.Add(newItem); - added.Add(drop); + + added.Add(new LootDrop + { + ItemDefinitionId = drop.ItemDefinitionId, + Item = drop.Item, + Quantity = quantityForNewStack + }); + + remainingToAdd -= quantityForNewStack; } else { - // Inventory full - overflow - overflow.Add(drop); + // Inventory full - overflow the rest + overflow.Add(new LootDrop + { + ItemDefinitionId = drop.ItemDefinitionId, + Item = drop.Item, + Quantity = remainingToAdd + }); + remainingToAdd = 0; } } }