Abilities

This commit is contained in:
dingo
2026-01-25 19:08:00 -05:00
parent 5f545bc8f7
commit 21b09ed90a
3 changed files with 222 additions and 24 deletions
+12 -10
View File
@@ -1135,7 +1135,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
float statBonus = baseDamage * (statModifier / 100f);
int totalDamage = (int)(baseDamage + statBonus);
Console.WriteLine($"[HandleFireProjectile] Weapon: {weapon.Name} ({weapon.MinDamage}-{weapon.MaxDamage}), Ammo: {ammo.Name} ({ammo.MinDamage}-{ammo.MaxDamage}), Total: {totalDamage}");
// Debug logging disabled
// Console.WriteLine($"[HandleFireProjectile] Weapon: {weapon.Name} ({weapon.MinDamage}-{weapon.MaxDamage}), Ammo: {ammo.Name} ({ammo.MinDamage}-{ammo.MaxDamage}), Total: {totalDamage}");
// Create projectile
var projectile = new ProjectileInstance
@@ -1165,17 +1166,18 @@ public sealed partial class DungeonHandler : IClientMessageHandler
};
dungeon.Simulation.Projectiles.TryAdd(projectile.Id, projectile);
Console.WriteLine($"[HandleFireProjectile] Added projectile {projectile.Id} to simulation. Total projectiles: {dungeon.Simulation.Projectiles.Count}");
// Debug logging disabled
// Console.WriteLine($"[HandleFireProjectile] Added projectile {projectile.Id} to simulation. Total projectiles: {dungeon.Simulation.Projectiles.Count}");
// Broadcast projectile spawned
Console.WriteLine($"[HandleFireProjectile] Broadcasting projectile {projectile.Id} to {dungeon.Simulation.Players.Count} players");
// Console.WriteLine($"[HandleFireProjectile] Broadcasting projectile {projectile.Id} to {dungeon.Simulation.Players.Count} players");
foreach (var dungeonPlayer in dungeon.Simulation.Players.Values)
{
Console.WriteLine($"[HandleFireProjectile] Checking player CharId={dungeonPlayer.CharacterId}");
// Console.WriteLine($"[HandleFireProjectile] Checking player CharId={dungeonPlayer.CharacterId}");
var playerSession = _playerSessions.Values.FirstOrDefault(p => p.CharacterId == dungeonPlayer.CharacterId);
if (playerSession != null)
{
Console.WriteLine($"[HandleFireProjectile] Sending EVENT_PROJECTILE_SPAWNED (504) to player {playerSession.Character.Name} (CharId={playerSession.CharacterId})");
// Console.WriteLine($"[HandleFireProjectile] Sending EVENT_PROJECTILE_SPAWNED (504) to player {playerSession.Character.Name} (CharId={playerSession.CharacterId})");
try
{
await SendAsync(playerSession.Connection, DungeonOpCodes.EVENT_PROJECTILE_SPAWNED, new
@@ -1194,17 +1196,17 @@ public sealed partial class DungeonHandler : IClientMessageHandler
backX = projectile.BackX,
backY = projectile.BackY
}, ct);
Console.WriteLine($"[HandleFireProjectile] Successfully sent EVENT_PROJECTILE_SPAWNED to {playerSession.Character.Name}");
// Console.WriteLine($"[HandleFireProjectile] Successfully sent EVENT_PROJECTILE_SPAWNED to {playerSession.Character.Name}");
}
catch (Exception ex)
{
Console.WriteLine($"[HandleFireProjectile] ERROR sending EVENT_PROJECTILE_SPAWNED to {playerSession.Character.Name}: {ex.Message}");
}
}
else
{
Console.WriteLine($"[HandleFireProjectile] WARNING: No session found for player CharId={dungeonPlayer.CharacterId}");
}
// else
// {
// Console.WriteLine($"[HandleFireProjectile] WARNING: No session found for player CharId={dungeonPlayer.CharacterId}");
// }
}
_log.LogDebug("Player {Name} fired projectile with {Weapon} using {Ammo} for {Damage} damage",
+137 -6
View File
@@ -46,9 +46,11 @@ public partial class DungeonSimulation
private readonly List<ProjectileExpiredEvent> _pendingProjectileExpiredEvents = new();
private int _nextDroppedItemId = 1;
private int _nextMobId = 10000; // Start summoned mobs at high ID to avoid conflicts
private readonly Dictionary<int, DungeonMobDefinition> _mobDefinitions;
private readonly Dictionary<int, MobAbilityDefinition>? _abilityDefinitions;
private readonly Dictionary<int, string?>? _spriteConfigs;
private readonly List<(int MobDefinitionId, float X, float Y, int Tier)> _pendingSummons = new();
public DungeonSimulation(
DungeonLayout layout,
@@ -407,7 +409,10 @@ public partial class DungeonSimulation
Phase = DungeonPhase.Cleared;
SpawnExit();
}
// Process pending summons
ProcessPendingSummons();
return new TickResult
{
DamageEvents = new List<DamageEvent>(_pendingDamageEvents),
@@ -561,10 +566,11 @@ public partial class DungeonSimulation
private void UpdateProjectiles()
{
if (Projectiles.Count > 0)
{
Console.WriteLine($"[UpdateProjectiles] Updating {Projectiles.Count} projectiles. Mobs alive: {Mobs.Values.Count(m => m.IsAlive)}");
}
// Debug logging disabled
// if (Projectiles.Count > 0)
// {
// Console.WriteLine($"[UpdateProjectiles] Updating {Projectiles.Count} projectiles. Mobs alive: {Mobs.Values.Count(m => m.IsAlive)}");
// }
var toRemove = new List<int>();
@@ -1228,7 +1234,132 @@ public partial class DungeonSimulation
float dy = y2 - y1;
return MathF.Sqrt(dx * dx + dy * dy);
}
/// <summary>
/// Request a mob to be summoned (will be processed at end of tick)
/// </summary>
public void RequestMobSummon(int mobDefinitionId, float x, float y, int tier)
{
_pendingSummons.Add((mobDefinitionId, x, y, tier));
}
/// <summary>
/// Process all pending summon requests
/// </summary>
private void ProcessPendingSummons()
{
if (_pendingSummons.Count == 0) return;
foreach (var summon in _pendingSummons)
{
SpawnSummonedMob(summon.MobDefinitionId, summon.X, summon.Y, summon.Tier);
}
_pendingSummons.Clear();
}
/// <summary>
/// Spawn a summoned mob at runtime
/// </summary>
private void SpawnSummonedMob(int mobDefinitionId, float x, float y, int tier)
{
if (!_mobDefinitions.TryGetValue(mobDefinitionId, out var mobDef))
{
Console.WriteLine($"[DungeonSimulation] Cannot summon mob {mobDefinitionId} - definition not found");
return;
}
// Use bracket-aware stat scaling
var stats = mobDef.GetStatsForBracket(BracketMin);
// Get sprite config
string? spriteConfig = null;
if (_spriteConfigs != null && _spriteConfigs.TryGetValue(mobDef.Id, out var config) && !string.IsNullOrEmpty(config))
{
spriteConfig = config;
}
else
{
spriteConfig = mobDef.SpriteConfigJson;
}
var mob = new MobInstance
{
Id = _nextMobId++,
DefinitionId = mobDef.Id,
Name = mobDef.Name,
X = x,
Y = y,
SpawnX = x,
SpawnY = y,
Health = stats.MaxHealth,
MaxHealth = stats.MaxHealth,
Mana = mobDef.BaseMana,
MaxMana = mobDef.BaseMana,
IsAlive = true,
Type = mobDef.Type,
Behavior = mobDef.Behavior,
PreferredRange = mobDef.PreferredRange,
FleeThreshold = mobDef.FleeThreshold,
Stats = stats,
State = MobState.Idle,
StateTime = 0,
SpriteConfigJson = spriteConfig,
TintColor = mobDef.TintColor,
Scale = mobDef.Scale,
CurrentAnimation = "idle_down",
FacingDirection = "down",
Phases = mobDef.Phases
};
// Load abilities for this mob
if (_abilityDefinitions != null && mobDef.AbilityAssignments != null)
{
foreach (var assignment in mobDef.AbilityAssignments.Where(a => a.IsEnabled))
{
if (_abilityDefinitions.TryGetValue(assignment.AbilityDefinitionId, out var abilityDef))
{
mob.Abilities.Add(new MobAbilityState
{
AbilityId = abilityDef.Id,
Definition = abilityDef,
CooldownRemaining = 0,
Priority = assignment.PriorityOverride ?? abilityDef.Priority,
Phase = assignment.Phase,
MinHpPercent = assignment.MinHpPercent,
MaxHpPercent = assignment.MaxHpPercent
});
}
}
}
// If no abilities loaded, give a basic attack
if (mob.Abilities.Count == 0)
{
mob.Abilities.Add(new MobAbilityState
{
AbilityId = 0,
Definition = new MobAbilityDefinition
{
Id = 0,
Name = "Basic Attack",
Key = "basic_attack",
AbilityType = MobAbilityType.MeleeAttack,
BaseDamage = stats.PhysicalDamage,
Range = stats.AttackRange,
Cooldown = 1f / stats.AttackSpeed,
AnimationKey = "attack",
DamageTypeKey = stats.DamageTypeKey
},
CooldownRemaining = 0,
Priority = 1
});
}
Mobs[mob.Id] = mob;
Console.WriteLine($"[DungeonSimulation] Summoned {mob.Name} (ID {mob.Id}) at ({x}, {y})");
}
}
+73 -8
View File
@@ -475,19 +475,35 @@ public static class MobAI
private static void UpdateSummoner(MobInstance mob, DungeonSimulation sim, float deltaTime)
{
var target = FindNearestPlayer(mob, sim);
if (target == null)
{
UpdateIdleWander(mob, sim, deltaTime);
return;
}
float distance = GetDistance(mob.X, mob.Y, target.X, target.Y);
float preferredRange = mob.PreferredRange > 0 ? mob.PreferredRange : 200f;
mob.TargetId = target.CharacterId;
mob.UpdateFacingDirection(target.X, target.Y);
// Try to use summon ability
float hpPercent = (float)mob.Health / mob.MaxHealth;
var summonAbility = mob.Abilities
.Where(a => a.Definition.AbilityType == MobAbilityType.SummonBulk || a.Definition.AbilityType == MobAbilityType.SummonSingle)
.FirstOrDefault(a => a.IsAvailable(hpPercent) && mob.Mana >= a.Definition.ManaCost);
if (summonAbility != null)
{
// Start casting
mob.CastingAbility = summonAbility;
mob.CastTimeRemaining = summonAbility.Definition.CastTime;
mob.State = MobState.Summoning;
mob.CurrentAnimation = mob.GetDirectionalAnimation("cast");
return;
}
// Stay back
if (distance < preferredRange * 0.5f)
{
@@ -496,8 +512,8 @@ public static class MobAI
}
else
{
mob.State = MobState.Summoning;
mob.CurrentAnimation = mob.GetDirectionalAnimation("cast");
mob.State = MobState.Idle;
mob.CurrentAnimation = mob.GetDirectionalAnimation("idle");
}
}
@@ -746,14 +762,63 @@ public static class MobAI
#endregion
#region Ability System
private static void ExecuteAbility(MobInstance mob, MobAbilityState ability, DungeonSimulation sim)
{
ability.CooldownRemaining = ability.Definition.Cooldown;
mob.Mana -= ability.Definition.ManaCost;
mob.CurrentAnimation = mob.GetDirectionalAnimation(ability.Definition.AnimationKey);
// Execute ability based on type
switch (ability.Definition.AbilityType)
{
case MobAbilityType.SummonSingle:
case MobAbilityType.SummonBulk:
ExecuteSummon(mob, ability, sim);
break;
// TODO: Implement other ability types
default:
Console.WriteLine($"[MobAI] Ability type {ability.Definition.AbilityType} not yet implemented for {mob.Name}");
break;
}
}
private static void ExecuteSummon(MobInstance mob, MobAbilityState ability, DungeonSimulation sim)
{
if (!ability.Definition.SummonMobId.HasValue)
{
Console.WriteLine($"[MobAI] Summon ability {ability.Definition.Name} has no SummonMobId");
return;
}
int summonCount = ability.Definition.SummonCount > 0 ? ability.Definition.SummonCount : 1;
int summonMobId = ability.Definition.SummonMobId.Value;
float summonDistance = ability.Definition.DisplacementDistance > 0 ? ability.Definition.DisplacementDistance : 50f;
Console.WriteLine($"[MobAI] {mob.Name} summoning {summonCount}x mob {summonMobId} at distance {summonDistance}");
// Spawn mobs around the summoner
for (int i = 0; i < summonCount; i++)
{
// Calculate spawn position in a circle around summoner
float angle = (float)(Math.PI * 2 * i / summonCount);
float spawnX = mob.X + (float)Math.Cos(angle) * summonDistance;
float spawnY = mob.Y + (float)Math.Sin(angle) * summonDistance;
// Make sure spawn position is walkable
if (!sim.CanMoveTo(spawnX, spawnY))
{
// Try closer
spawnX = mob.X + (float)Math.Cos(angle) * (summonDistance * 0.5f);
spawnY = mob.Y + (float)Math.Sin(angle) * (summonDistance * 0.5f);
}
// Request mob spawn (this will be handled by DungeonSimulation)
sim.RequestMobSummon(summonMobId, spawnX, spawnY, sim.Tier);
}
}
#endregion
#region Helpers