Ability fixes and cleanup

This commit is contained in:
dingo
2026-02-08 18:57:04 -05:00
parent ec48fce15a
commit 6e3be67f64
10 changed files with 248 additions and 16 deletions
+3 -3
View File
@@ -204,7 +204,7 @@ public sealed partial class DungeonHandler
}
}
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, _abilityDefinitions, _mobSpriteConfigs);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings);
var instance = new DungeonInstance
{
@@ -497,7 +497,7 @@ public sealed partial class DungeonHandler
try
{
var layout = DungeonGenerator.Generate(persisted.Seed, persisted.Tier);
var simulation = new DungeonSimulation(layout, persisted.Tier, _mobDefinitions, persisted.LevelBracketMin, _abilityDefinitions, _mobSpriteConfigs);
var simulation = new DungeonSimulation(layout, persisted.Tier, _mobDefinitions, persisted.LevelBracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings);
// NOTE: We intentionally do NOT restore mob state here.
// When a dungeon is restored (all players left), ALL mobs respawn fresh.
@@ -571,7 +571,7 @@ public sealed partial class DungeonHandler
// Using DungeonGenerator.Generate() would create a V1 layout which has different
// room geometry than V2/V3, causing mobs to spawn in wrong positions.
var layout = instance.Simulation.Layout;
var freshSimulation = new DungeonSimulation(layout, instance.Tier, _mobDefinitions, instance.LevelBracketMin, _abilityDefinitions, _mobSpriteConfigs);
var freshSimulation = new DungeonSimulation(layout, instance.Tier, _mobDefinitions, instance.LevelBracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings);
// Clear and rebuild mobs, keeping those with loot
instance.Simulation.Mobs.Clear();
+6 -1
View File
@@ -42,6 +42,10 @@ public sealed partial class DungeonHandler
_abilityDefinitions = abilities.ToDictionary(a => a.Id);
var abilityAnimationMappings = await db.Set<MobAbilityAnimationMapping>()
.ToListAsync();
_abilityAnimationMappings = abilityAnimationMappings.ToDictionary(m => m.AbilityAssignmentId);
// Build sprite configs from animation assignments
var spriteConfigBuilder = new MobSpriteConfigBuilder(db);
_mobSpriteConfigs = await spriteConfigBuilder.BuildSpriteConfigsAsync(mobs.Select(m => m.Id));
@@ -50,10 +54,11 @@ public sealed partial class DungeonHandler
var assignmentCount = await db.Set<MobAnimationAssignment>().CountAsync();
var abilityAssignmentsCount = mobs.Sum(m => m.AbilityAssignments?.Count ?? 0);
_log.LogInformation(
"Refreshed mob cache: {MobCount} mobs, {AbilityCount} abilities, {AbilityAssignments} ability assignments, {AssignmentCount} animation assignments",
"Refreshed mob cache: {MobCount} mobs, {AbilityCount} abilities, {AbilityAssignments} ability assignments, {AbilityAnimationMappings} ability animation mappings, {AssignmentCount} animation assignments",
mobs.Count,
_abilityDefinitions.Count,
abilityAssignmentsCount,
_abilityAnimationMappings.Count,
assignmentCount);
if (abilityAssignmentsCount > 0)
+15 -5
View File
@@ -41,6 +41,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Mob definitions cache
private Dictionary<int, DungeonMobDefinition> _mobDefinitions = new();
private Dictionary<int, MobAbilityDefinition> _abilityDefinitions = new();
private Dictionary<int, MobAbilityAnimationMapping> _abilityAnimationMappings = new();
private Dictionary<int, string?> _mobSpriteConfigs = new(); // MobDefinitionId -> SpriteConfig JSON
private bool _initialized = false;
@@ -678,7 +679,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
}
}
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, _abilityDefinitions, _mobSpriteConfigs);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings);
instance = new DungeonInstance
{
@@ -1531,7 +1532,6 @@ public sealed partial class DungeonHandler : IClientMessageHandler
{
_ = HandleDungeonClearedAsync(session);
}
continue;
}
foreach (var session in playersInDungeon)
@@ -2891,6 +2891,16 @@ public sealed partial class DungeonHandler : IClientMessageHandler
x = e.X,
y = e.Y,
reason = e.Reason
}),
abilities = tickResult.AbilityEvents.Select(e => new
{
sourceType = e.SourceType.ToString().ToLower(),
sourceId = e.SourceId,
abilityKey = e.AbilityKey,
animationKey = e.AnimationKey,
targetX = e.TargetX,
targetY = e.TargetY,
targetId = e.TargetId
})
},
@@ -2919,10 +2929,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
{
_log.LogInformation("Dungeon cleared! Player {Name} can now exit via portal", session.Character.Name);
// Send dungeon cleared notification (but don't force exit)
await SendAsync(session.Connection, DungeonOpCodes.DUNGEON_STATE, new
// Send dungeon cleared notification on dedicated opcode.
// Full world state still flows through regular DUNGEON_STATE packets.
await SendAsync(session.Connection, DungeonOpCodes.DUNGEON_CLEARED, new
{
phase = "cleared",
message = "Dungeon cleared! Use the portal to exit."
}, CancellationToken.None);
+1 -2
View File
@@ -134,8 +134,7 @@ public partial class DungeonHandler
else
spriteConfig = mobDef.SpriteConfigJson;
var maxId = dungeon.Simulation.Mobs.Keys.DefaultIfEmpty(0).Max();
var newMobId = maxId + 1;
var newMobId = dungeon.Simulation.AllocateRuntimeMobId();
var stats = mobDef.GetStatsForBracket(dungeon.LevelBracketMin);
var mob = new MobInstance
+62 -3
View File
@@ -47,9 +47,11 @@ public partial class DungeonSimulation
private int _nextDroppedItemId = 1;
private int _nextMobId = 10000; // Start summoned mobs at high ID to avoid conflicts
private readonly object _mobIdLock = new();
private readonly Dictionary<int, DungeonMobDefinition> _mobDefinitions;
private readonly Dictionary<int, MobAbilityDefinition>? _abilityDefinitions;
private readonly Dictionary<int, string?>? _spriteConfigs;
private readonly Dictionary<int, MobAbilityAnimationMapping>? _abilityAnimationMappings;
private readonly List<(int MobDefinitionId, float X, float Y, int Tier)> _pendingSummons = new();
public DungeonSimulation(
@@ -58,7 +60,8 @@ public partial class DungeonSimulation
Dictionary<int, DungeonMobDefinition> mobDefinitions,
int bracketMin = 1,
Dictionary<int, MobAbilityDefinition>? abilityDefinitions = null,
Dictionary<int, string?>? spriteConfigs = null)
Dictionary<int, string?>? spriteConfigs = null,
Dictionary<int, MobAbilityAnimationMapping>? abilityAnimationMappings = null)
{
Layout = layout;
Tier = tier;
@@ -66,6 +69,7 @@ public partial class DungeonSimulation
_mobDefinitions = mobDefinitions;
_abilityDefinitions = abilityDefinitions;
_spriteConfigs = spriteConfigs;
_abilityAnimationMappings = abilityAnimationMappings;
SpawnMobs();
InitializeChests();
@@ -272,6 +276,7 @@ public partial class DungeonSimulation
AnimationKey = "attack",
DamageTypeKey = stats.DamageTypeKey
},
ResolvedAnimationKey = "attack",
CooldownRemaining = 0,
Priority = 1
});
@@ -434,9 +439,18 @@ public partial class DungeonSimulation
float distance = Distance(mob.X, mob.Y, target.X, target.Y);
bool isSummonAbility = ability.Definition.AbilityType is MobAbilityType.SummonSingle or MobAbilityType.SummonBulk;
if (!isSummonAbility && ability.Definition.Range > 0 && distance > ability.Definition.Range) return;
int manaCost = Math.Max(0, ability.Definition.ManaCost);
if (manaCost > 0)
{
if (mob.Mana < manaCost) return;
mob.Mana -= manaCost;
}
// Put ability on cooldown
ability.CooldownRemaining = ability.Definition.Cooldown;
string animationKey = ResolveAbilityAnimationKey(mob, ability);
// Emit ability event for client animation
_pendingAbilityEvents.Add(new AbilityEvent
@@ -444,7 +458,7 @@ public partial class DungeonSimulation
SourceType = EntityType.Mob,
SourceId = mob.Id,
AbilityKey = ability.Definition.Key,
AnimationKey = ability.Definition.AnimationKey,
AnimationKey = animationKey,
TargetX = target.X,
TargetY = target.Y,
TargetId = (int)target.CharacterId
@@ -574,6 +588,7 @@ public partial class DungeonSimulation
{
AbilityId = abilityDef.Id,
Definition = abilityDef,
ResolvedAnimationKey = ResolveMappedAnimationKey(assignment.Id, abilityDef),
CooldownRemaining = 0,
Priority = assignment.PriorityOverride ?? abilityDef.Priority,
Phase = assignment.Phase,
@@ -582,6 +597,32 @@ public partial class DungeonSimulation
});
}
}
private string ResolveMappedAnimationKey(int abilityAssignmentId, MobAbilityDefinition abilityDef)
{
if (_abilityAnimationMappings != null &&
_abilityAnimationMappings.TryGetValue(abilityAssignmentId, out var mapping) &&
!string.IsNullOrWhiteSpace(mapping.AnimationKey))
{
return mapping.AnimationKey.Trim();
}
return string.IsNullOrWhiteSpace(abilityDef.AnimationKey) ? "attack" : abilityDef.AnimationKey;
}
private static string ResolveAbilityAnimationKey(MobInstance mob, MobAbilityState ability)
{
var key = string.IsNullOrWhiteSpace(ability.ResolvedAnimationKey)
? ability.Definition.AnimationKey
: ability.ResolvedAnimationKey;
if (string.IsNullOrWhiteSpace(key))
key = "attack";
key = key.Replace("[dir]", mob.FacingDirection, StringComparison.OrdinalIgnoreCase);
key = key.Replace("{dir}", mob.FacingDirection, StringComparison.OrdinalIgnoreCase);
return key;
}
private void ProcessMeleeAbility(MobInstance mob, MobAbilityDefinition ability, PlayerInstance target)
{
@@ -1332,6 +1373,24 @@ public partial class DungeonSimulation
_pendingSummons.Add((mobDefinitionId, x, y, tier));
}
public int AllocateRuntimeMobId()
{
lock (_mobIdLock)
{
if (_nextMobId < 10000)
{
_nextMobId = 10000;
}
while (Mobs.ContainsKey(_nextMobId))
{
_nextMobId++;
}
return _nextMobId++;
}
}
/// <summary>
/// Process all pending summon requests
/// </summary>
@@ -1374,7 +1433,7 @@ public partial class DungeonSimulation
var mob = new MobInstance
{
Id = _nextMobId++,
Id = AllocateRuntimeMobId(),
DefinitionId = mobDef.Id,
Name = mobDef.Name,
X = x,
+8
View File
@@ -544,6 +544,10 @@ public static class MobAI
{
mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.02f * deltaTime));
}
if (mob.Mana < mob.MaxMana)
{
mob.Mana = Math.Min(mob.MaxMana, mob.Mana + (int)(mob.MaxMana * 0.02f * deltaTime));
}
// State machine for wandering
// States: Idle (standing still) -> Moving to wander target -> Idle again
@@ -635,6 +639,10 @@ public static class MobAI
{
mob.Health = Math.Min(mob.MaxHealth, mob.Health + (int)(mob.MaxHealth * 0.05f * deltaTime));
}
if (mob.Mana < mob.MaxMana)
{
mob.Mana = Math.Min(mob.MaxMana, mob.Mana + (int)(mob.MaxMana * 0.05f * deltaTime));
}
}
}
+1
View File
@@ -27,6 +27,7 @@ public class MobAbilityState
{
public int AbilityId { get; set; }
public MobAbilityDefinition Definition { get; set; } = null!;
public string ResolvedAnimationKey { get; set; } = "attack";
public float CooldownRemaining { get; set; }
public int Priority { get; set; }
public int Phase { get; set; }