Mob changes
This commit is contained in:
+207
-31
@@ -368,6 +368,17 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
await HandleLootChestClose(connection, session, payload, ct);
|
await HandleLootChestClose(connection, session, payload, ct);
|
||||||
break;
|
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
|
// Dungeon browser
|
||||||
case DungeonOpCodes.DUNGEON_LIST_REQUEST:
|
case DungeonOpCodes.DUNGEON_LIST_REQUEST:
|
||||||
await HandleDungeonListRequest(connection, session, ct);
|
await HandleDungeonListRequest(connection, session, ct);
|
||||||
@@ -1665,7 +1676,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task ProcessMobDeathLootAsync(DungeonInstance dungeon, DeathEvent death)
|
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))
|
if (!dungeon.Simulation.Mobs.TryGetValue(death.EntityId, out var mob))
|
||||||
return;
|
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
|
// Generate loot from the mob's loot tables
|
||||||
var loot = _lootService.GenerateMobLoot(
|
var loot = _lootService.GenerateMobLoot(
|
||||||
mob.DefinitionId,
|
mob.DefinitionId,
|
||||||
@@ -1689,7 +1693,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
1.0f
|
1.0f
|
||||||
);
|
);
|
||||||
|
|
||||||
// Collect all items to give (normal loot + carried player loot)
|
// Collect all items (normal loot + carried player loot)
|
||||||
var allItems = new List<LootDrop>(loot);
|
var allItems = new List<LootDrop>(loot);
|
||||||
long totalGold = 0;
|
long totalGold = 0;
|
||||||
|
|
||||||
@@ -1728,28 +1732,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
mob.CarriedPlayerLoot.Clear();
|
mob.CarriedPlayerLoot.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allItems.Count == 0 && totalGold == 0)
|
// Store loot on the mob body for players to loot manually
|
||||||
return;
|
mob.DeathLoot = allItems;
|
||||||
|
mob.DeathLootGold = totalGold;
|
||||||
|
|
||||||
// Try to add loot to player's inventory, get overflow
|
_log.LogDebug("Mob {Mob} died with {Count} items and {Gold} gold loot",
|
||||||
var (addedItems, overflowItems, overflowGold) = await AddLootToInventoryWithOverflowAsync(
|
mob.Name, allItems.Count, totalGold);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -3632,7 +3620,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
isInCombat = p.IsInCombat
|
isInCombat = p.IsInCombat
|
||||||
}),
|
}),
|
||||||
|
|
||||||
mobs = sim.Mobs.Values.Where(m => m.IsAlive).Select(m => new
|
mobs = sim.Mobs.Values.Select(m => new
|
||||||
{
|
{
|
||||||
id = m.Id,
|
id = m.Id,
|
||||||
name = m.Name,
|
name = m.Name,
|
||||||
@@ -4615,6 +4603,172 @@ public partial class DungeonHandler
|
|||||||
await HandleInventoryRequest(connection, session, ct);
|
await HandleInventoryRequest(connection, session, ct);
|
||||||
await HandleStashRequest(connection, session, ct);
|
await HandleStashRequest(connection, session, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
_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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request DTOs for stash operations
|
// Request DTOs for stash operations
|
||||||
@@ -4645,4 +4799,26 @@ public class FireProjectileInput
|
|||||||
public float DirectionY { get; set; }
|
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
|
#endregion
|
||||||
|
|||||||
@@ -160,6 +160,13 @@ public class MobInstance
|
|||||||
/// <summary>Check if this mob is carrying loot from player kills</summary>
|
/// <summary>Check if this mob is carrying loot from player kills</summary>
|
||||||
public bool HasCarriedLoot => CarriedPlayerLoot.Count > 0;
|
public bool HasCarriedLoot => CarriedPlayerLoot.Count > 0;
|
||||||
|
|
||||||
|
// Death loot - generated when mob dies, lootable by players
|
||||||
|
public List<LootDrop>? DeathLoot { get; set; }
|
||||||
|
public long DeathLootGold { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Check if this mob has loot available</summary>
|
||||||
|
public bool HasDeathLoot => DeathLoot != null && (DeathLoot.Count > 0 || DeathLootGold > 0);
|
||||||
|
|
||||||
/// <summary>Current HP as percentage (0-1)</summary>
|
/// <summary>Current HP as percentage (0-1)</summary>
|
||||||
public float HpPercent => MaxHealth > 0 ? (float)Health / MaxHealth : 0f;
|
public float HpPercent => MaxHealth > 0 ? (float)Health / MaxHealth : 0f;
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,12 @@ public static class DungeonOpCodes
|
|||||||
// Player Death (280-289)
|
// Player Death (280-289)
|
||||||
public const int PLAYER_DEATH_LOOT_LOST = 280; // Notifies victim what they lost
|
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)
|
// Crafting (330-349)
|
||||||
public const int CRAFTING_SKILLS_REQUEST = 330;
|
public const int CRAFTING_SKILLS_REQUEST = 330;
|
||||||
public const int CRAFTING_SKILLS_STATE = 331;
|
public const int CRAFTING_SKILLS_STATE = 331;
|
||||||
|
|||||||
Reference in New Issue
Block a user