Files
systemrevivalserver/Game/Dungeon/DungeonHandler.MobLoot.cs
T
dingo 5d3de08037 Keep mob corpses persistent and enforce death animation state
- Stop automatic corpse removal after loot is emptied; corpses now remain in world

- On all mob death paths, set dead state plus death animation (death_[dir]) and clear combat/cast targeting

- Include dead mobs in spatial state feed so corpses do not disappear from clients

- Add mob currentAnimation/hasLoot in payload and reject loot interaction when corpse has no loot
2026-02-09 09:24:14 -05:00

192 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Server.Networking;
using GameServer.Networking.DTOs;
using Server.Networking.Messages;
namespace GameServer.Dungeon;
public sealed partial class DungeonHandler
{
#region Mob Loot Handlers
private async Task HandleMobLootRequest(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
var request = System.Text.Json.JsonSerializer.Deserialize<MobLootRequest>(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;
}
// No interaction once corpse loot is exhausted
if (!mob.HasDeathLoot)
{
await SendErrorAsync(connection, "Mob has no loot", 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<object>();
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<byte> payload, CancellationToken ct)
{
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
var request = System.Text.Json.JsonSerializer.Deserialize<MobLootTakeItemRequest>(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<LootDrop> { lootItem }, 0);
if (addedItems.Count > 0)
{
// Remove from mob loot
mob.DeathLoot.RemoveAt(request.ItemIndex);
// Send updated inventory
await HandleInventoryRequest(connection, session, ct);
// Send updated mob loot state so client knows what's left
await HandleMobLootRequest(connection, session, System.Text.Encoding.UTF8.GetBytes(
System.Text.Json.JsonSerializer.Serialize(new { mobId = request.MobId })), 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<byte> payload, CancellationToken ct)
{
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
var request = System.Text.Json.JsonSerializer.Deserialize<MobLootTakeAllRequest>(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<LootDrop>(mob.DeathLoot) : new List<LootDrop>();
// 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
}