diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index d7a004a..f14c141 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -367,6 +367,17 @@ public sealed partial class DungeonHandler : IClientMessageHandler case DungeonOpCodes.LOOT_CHEST_CLOSE: await HandleLootChestClose(connection, session, payload, ct); break; + + // Mob loot operations + case DungeonOpCodes.MOB_LOOT_REQUEST: + await HandleMobLootRequest(connection, session, payload, ct); + break; + case DungeonOpCodes.MOB_LOOT_TAKE_ITEM: + await HandleMobLootTakeItem(connection, session, payload, ct); + break; + case DungeonOpCodes.MOB_LOOT_TAKE_ALL: + await HandleMobLootTakeAll(connection, session, payload, ct); + break; // Dungeon browser case DungeonOpCodes.DUNGEON_LIST_REQUEST: @@ -1665,7 +1676,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler } /// - /// Process loot when a mob dies - includes normal loot roll plus any carried player loot + /// Process loot when a mob dies - stores loot on mob body for player interaction /// private async Task ProcessMobDeathLootAsync(DungeonInstance dungeon, DeathEvent death) { @@ -1675,13 +1686,6 @@ public sealed partial class DungeonHandler : IClientMessageHandler if (!dungeon.Simulation.Mobs.TryGetValue(death.EntityId, out var mob)) return; - // Find the player who killed it - var killerSession = _playerSessions.Values - .FirstOrDefault(s => s.CharacterId == (ulong)death.KillerId); - - if (killerSession == null) - return; - // Generate loot from the mob's loot tables var loot = _lootService.GenerateMobLoot( mob.DefinitionId, @@ -1689,7 +1693,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler 1.0f ); - // Collect all items to give (normal loot + carried player loot) + // Collect all items (normal loot + carried player loot) var allItems = new List(loot); long totalGold = 0; @@ -1728,28 +1732,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler mob.CarriedPlayerLoot.Clear(); } - if (allItems.Count == 0 && totalGold == 0) - return; + // Store loot on the mob body for players to loot manually + mob.DeathLoot = allItems; + mob.DeathLootGold = totalGold; - // Try to add loot to player's inventory, get overflow - var (addedItems, overflowItems, overflowGold) = await AddLootToInventoryWithOverflowAsync( - killerSession, allItems, totalGold); - - // Send loot popup for items that were added directly - if (addedItems.Count > 0) - { - await SendLootPopupAsync(killerSession, addedItems); - } - - // If there's overflow, spawn a personal loot chest - if (overflowItems.Count > 0 || overflowGold > 0) - { - await SpawnLootChestAsync(dungeon, killerSession, overflowItems, overflowGold, - mob.X, mob.Y, null, mob.DefinitionId); - } - - _log.LogDebug("Player {Player} received {Count} items and {Gold} gold from {Mob}", - killerSession.Character.Name, addedItems.Count, totalGold - overflowGold, mob.Name); + _log.LogDebug("Mob {Mob} died with {Count} items and {Gold} gold loot", + mob.Name, allItems.Count, totalGold); } catch (Exception ex) { @@ -3632,7 +3620,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler isInCombat = p.IsInCombat }), - mobs = sim.Mobs.Values.Where(m => m.IsAlive).Select(m => new + mobs = sim.Mobs.Values.Select(m => new { id = m.Id, name = m.Name, @@ -3642,7 +3630,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler health = m.Health, maxHealth = m.MaxHealth, state = m.State.ToString().ToLower(), - spriteConfig = string.IsNullOrEmpty(m.SpriteConfigJson) ? null : + spriteConfig = string.IsNullOrEmpty(m.SpriteConfigJson) ? null : System.Text.Json.JsonSerializer.Deserialize(m.SpriteConfigJson), tintColor = m.TintColor, scale = m.Scale @@ -4610,11 +4598,177 @@ public partial class DungeonHandler success = true, message = "Item withdrawn from stash." }, ct); - + // Send updated inventory and stash await HandleInventoryRequest(connection, session, ct); await HandleStashRequest(connection, session, ct); } + + #region Mob Loot Handlers + + private async Task HandleMobLootRequest(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory payload, CancellationToken ct) + { + var json = System.Text.Encoding.UTF8.GetString(payload.Span); + var request = System.Text.Json.JsonSerializer.Deserialize(json); + if (request == null) return; + + if (!session.CurrentDungeonId.HasValue || + !_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) + return; + + if (!dungeon.Simulation.Mobs.TryGetValue(request.MobId, out var mob)) + { + await SendErrorAsync(connection, "Mob not found", ct); + return; + } + + // Check if mob is dead + if (mob.IsAlive) + { + await SendErrorAsync(connection, "Mob is still alive", ct); + return; + } + + // Check distance + if (session.PlayerInstance != null) + { + float dist = MathF.Sqrt( + MathF.Pow(session.PlayerInstance.X - mob.X, 2) + + MathF.Pow(session.PlayerInstance.Y - mob.Y, 2)); + + if (dist > 96) // Interaction range + { + await SendErrorAsync(connection, "Too far from mob", ct); + return; + } + } + + // Send mob loot state + var lootItems = new List(); + + if (mob.DeathLoot != null) + { + foreach (var loot in mob.DeathLoot) + { + lootItems.Add(new + { + definitionId = loot.ItemDefinitionId, + name = loot.Item.Name, + description = loot.Item.Description, + type = loot.Item.Type.ToString(), + rarity = loot.Item.Rarity.ToString(), + quantity = loot.Quantity, + spritePath = loot.Item.SpritePath, + tintColor = loot.Item.TintColor + }); + } + } + + await SendAsync(connection, DungeonOpCodes.MOB_LOOT_STATE, new + { + mobId = mob.Id, + mobName = mob.Name, + gold = mob.DeathLootGold, + items = lootItems + }, ct); + } + + private async Task HandleMobLootTakeItem(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory payload, CancellationToken ct) + { + var json = System.Text.Encoding.UTF8.GetString(payload.Span); + var request = System.Text.Json.JsonSerializer.Deserialize(json); + if (request == null) return; + + if (!session.CurrentDungeonId.HasValue || + !_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) + return; + + if (!dungeon.Simulation.Mobs.TryGetValue(request.MobId, out var mob)) + return; + + if (!mob.IsAlive && mob.DeathLoot != null && request.ItemIndex >= 0 && request.ItemIndex < mob.DeathLoot.Count) + { + var lootItem = mob.DeathLoot[request.ItemIndex]; + + // Try to add to inventory + var (addedItems, overflowItems, _) = await AddLootToInventoryWithOverflowAsync( + session, new List { lootItem }, 0); + + if (addedItems.Count > 0) + { + // Remove from mob loot + mob.DeathLoot.RemoveAt(request.ItemIndex); + + // Send updated inventory + await HandleInventoryRequest(connection, session, ct); + + _log.LogDebug("Player {Player} looted {Item} x{Qty} from mob {Mob}", + session.Character.Name, lootItem.Item.Name, lootItem.Quantity, mob.Name); + } + else if (overflowItems.Count > 0) + { + await SendErrorAsync(connection, "Inventory full", ct); + } + } + } + + private async Task HandleMobLootTakeAll(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory payload, CancellationToken ct) + { + var json = System.Text.Encoding.UTF8.GetString(payload.Span); + var request = System.Text.Json.JsonSerializer.Deserialize(json); + if (request == null) return; + + if (!session.CurrentDungeonId.HasValue || + !_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) + return; + + if (!dungeon.Simulation.Mobs.TryGetValue(request.MobId, out var mob)) + return; + + if (!mob.IsAlive && mob.HasDeathLoot) + { + long goldToTake = mob.DeathLootGold; + var itemsToTake = mob.DeathLoot != null ? new List(mob.DeathLoot) : new List(); + + // Try to add all loot to inventory + var (addedItems, overflowItems, overflowGold) = await AddLootToInventoryWithOverflowAsync( + session, itemsToTake, goldToTake); + + // Remove successfully added items from mob loot + if (mob.DeathLoot != null) + { + foreach (var added in addedItems) + { + var toRemove = mob.DeathLoot.FirstOrDefault(l => + l.ItemDefinitionId == added.ItemDefinitionId && + l.Quantity == added.Quantity); + if (toRemove != null) + { + mob.DeathLoot.Remove(toRemove); + } + } + } + + // Remove taken gold + long goldTaken = goldToTake - overflowGold; + mob.DeathLootGold = overflowGold; + + // If there's overflow, spawn a loot chest + if (overflowItems.Count > 0 || overflowGold > 0) + { + await SpawnLootChestAsync(dungeon, session, overflowItems, overflowGold, + mob.X, mob.Y, null, mob.DefinitionId); + } + + // Send updated inventory + await HandleInventoryRequest(connection, session, ct); + + _log.LogDebug("Player {Player} looted all from mob {Mob}: {Items} items, {Gold} gold", + session.Character.Name, mob.Name, addedItems.Count, goldTaken); + } + } + + #endregion } // Request DTOs for stash operations @@ -4645,4 +4799,26 @@ public class FireProjectileInput public float DirectionY { get; set; } } +// Mob loot request DTOs +public class MobLootRequest +{ + [System.Text.Json.Serialization.JsonPropertyName("mobId")] + public int MobId { get; set; } +} + +public class MobLootTakeItemRequest +{ + [System.Text.Json.Serialization.JsonPropertyName("mobId")] + public int MobId { get; set; } + + [System.Text.Json.Serialization.JsonPropertyName("itemIndex")] + public int ItemIndex { get; set; } +} + +public class MobLootTakeAllRequest +{ + [System.Text.Json.Serialization.JsonPropertyName("mobId")] + public int MobId { get; set; } +} + #endregion diff --git a/Game/Dungeon/MobInstance.cs b/Game/Dungeon/MobInstance.cs index 91b196b..2e80e96 100644 --- a/Game/Dungeon/MobInstance.cs +++ b/Game/Dungeon/MobInstance.cs @@ -156,9 +156,16 @@ public class MobInstance // Carried loot from player kills public List CarriedPlayerLoot { get; set; } = new(); - + /// Check if this mob is carrying loot from player kills public bool HasCarriedLoot => CarriedPlayerLoot.Count > 0; + + // Death loot - generated when mob dies, lootable by players + public List? DeathLoot { get; set; } + public long DeathLootGold { get; set; } + + /// Check if this mob has loot available + public bool HasDeathLoot => DeathLoot != null && (DeathLoot.Count > 0 || DeathLootGold > 0); /// Current HP as percentage (0-1) public float HpPercent => MaxHealth > 0 ? (float)Health / MaxHealth : 0f; diff --git a/Networking/Messages/DungeonOpCodes.cs b/Networking/Messages/DungeonOpCodes.cs index 2af37e4..ff40153 100644 --- a/Networking/Messages/DungeonOpCodes.cs +++ b/Networking/Messages/DungeonOpCodes.cs @@ -77,7 +77,13 @@ public static class DungeonOpCodes // Player Death (280-289) public const int PLAYER_DEATH_LOOT_LOST = 280; // Notifies victim what they lost - + + // Mob Loot (290-299) + public const int MOB_LOOT_REQUEST = 290; // Request loot from a dead mob + public const int MOB_LOOT_STATE = 291; // Mob loot contents response + public const int MOB_LOOT_TAKE_ITEM = 292; // Take specific item from mob + public const int MOB_LOOT_TAKE_ALL = 293; // Take all items from mob + // Crafting (330-349) public const int CRAFTING_SKILLS_REQUEST = 330; public const int CRAFTING_SKILLS_STATE = 331;