Files
systemrevivalserver/Game/Dungeon/MobInstance.cs
T
dingo e5ee966451 Add anti-repeat mob ability selection when multiple options are valid
- Track last used ability key and consecutive-use count on MobInstance

- In ProcessMobAbility, avoid repeating the same ability more than 3 times in a row if alternatives are currently usable

- Keep existing priority-based selection as primary behavior
2026-02-11 09:19:32 -05:00

241 lines
8.1 KiB
C#

using DataServer.Data.Entities;
using DataServer.Data.Enums;
namespace GameServer.Dungeon;
/// <summary>
/// Loot carried by a mob from killing a player
/// </summary>
public class CarriedLoot
{
public ulong VictimCharacterId { get; set; }
public string VictimName { get; set; } = "";
public List<CarriedLootItem> Items { get; set; } = new();
public long Gold { get; set; }
}
public class CarriedLootItem
{
public int ItemDefinitionId { get; set; }
public int Quantity { get; set; }
}
/// <summary>
/// Runtime state for a mob ability
/// </summary>
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; }
public float? MinHpPercent { get; set; }
public float? MaxHpPercent { get; set; }
/// <summary>
/// Check if this ability is available (off cooldown and HP in range)
/// </summary>
public bool IsAvailable(float currentHpPercent)
{
if (CooldownRemaining > 0) return false;
if (MinHpPercent.HasValue && currentHpPercent < MinHpPercent.Value) return false;
if (MaxHpPercent.HasValue && currentHpPercent > MaxHpPercent.Value) return false;
return true;
}
}
/// <summary>
/// Active status effect on a mob
/// </summary>
public class MobStatusEffect
{
public string EffectType { get; set; } = ""; // poison, bleed, burn, slow, stun, etc.
public float Duration { get; set; }
public float TickInterval { get; set; }
public float NextTickIn { get; set; }
public int DamagePerTick { get; set; }
public float Modifier { get; set; } = 1.0f; // For slow, etc.
public ulong SourcePlayerId { get; set; }
}
/// <summary>
/// Runtime instance of a mob in a dungeon
/// </summary>
public class MobInstance
{
public int Id { get; set; }
public int DefinitionId { get; set; }
public string Name { get; set; } = "";
public float X { get; set; }
public float Y { get; set; }
public float Rotation { get; set; }
public float SpawnX { get; set; }
public float SpawnY { get; set; }
public int Health { get; set; }
public int MaxHealth { get; set; }
public int Mana { get; set; }
public int MaxMana { get; set; }
public bool IsAlive { get; set; } = true;
public DungeonMobCombatStats Stats { get; set; } = new();
public MobState State { get; set; } = MobState.Idle;
public MobType Type { get; set; }
public MobBehavior Behavior { get; set; } = MobBehavior.Aggressive;
/// <summary>Current phase for multi-phase mobs (0 = first phase)</summary>
public int CurrentPhase { get; set; } = 0;
/// <summary>Phase configurations loaded from definition</summary>
public List<MobPhaseConfig>? Phases { get; set; }
public ulong? TargetId { get; set; }
public long LastAttackTick { get; set; }
/// <summary>Time spent in current state (for behavior decisions)</summary>
public float StateTime { get; set; }
/// <summary>Last position for stuck detection</summary>
public float LastX { get; set; }
public float LastY { get; set; }
public float StuckTime { get; set; }
// === Abilities ===
/// <summary>Abilities this mob can use</summary>
public List<MobAbilityState> Abilities { get; set; } = new();
/// <summary>Last ability key used by this mob (for anti-repeat selection).</summary>
public string? LastUsedAbilityKey { get; set; }
/// <summary>How many times in a row the same ability has been used.</summary>
public int ConsecutiveAbilityUses { get; set; }
/// <summary>Currently casting ability (null if not casting)</summary>
public MobAbilityState? CastingAbility { get; set; }
/// <summary>Remaining cast time</summary>
public float CastTimeRemaining { get; set; }
/// <summary>Active status effects on this mob</summary>
public List<MobStatusEffect> StatusEffects { get; set; } = new();
/// <summary>Current animation being played</summary>
public string CurrentAnimation { get; set; } = "idle";
/// <summary>Direction mob is facing (down, up, left, right)</summary>
public string FacingDirection { get; set; } = "down";
// === Behavior-specific ===
/// <summary>Preferred combat distance (for ranged/skirmisher)</summary>
public float PreferredRange { get; set; } = 40f;
/// <summary>Flee threshold HP percentage</summary>
public float FleeThreshold { get; set; } = 0f;
/// <summary>Has already called for help this combat</summary>
public bool HasCalledForHelp { get; set; }
/// <summary>Wander target X when idle</summary>
public float WanderTargetX { get; set; }
/// <summary>Wander target Y when idle</summary>
public float WanderTargetY { get; set; }
/// <summary>Patrol waypoints (for Patrol behavior)</summary>
public List<(float X, float Y)>? PatrolPath { get; set; }
/// <summary>Current patrol waypoint index</summary>
public int PatrolIndex { get; set; }
/// <summary>Enrage stacks (for Enrage behavior)</summary>
public int EnrageStacks { get; set; }
/// <summary>Time since combat started (for time-based mechanics)</summary>
public float CombatTime { get; set; }
// Visual - sprite configuration JSON
public string? SpriteConfigJson { get; set; }
public string TintColor { get; set; } = "#ffffff";
public float Scale { get; set; } = 1.0f;
// Carried loot from player kills
public List<CarriedLoot> CarriedPlayerLoot { get; set; } = new();
/// <summary>Check if this mob is carrying loot from player kills</summary>
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; }
// Corpse cleanup tracking
public long? LootEmptiedAtTick { get; set; } // Tick when loot became empty (for corpse removal timer)
/// <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>
public float HpPercent => MaxHealth > 0 ? (float)Health / MaxHealth : 0f;
/// <summary>Check if mob is stunned</summary>
public bool IsStunned => StatusEffects.Any(e => e.EffectType == "stun" && e.Duration > 0);
/// <summary>Check if mob is rooted</summary>
public bool IsRooted => StatusEffects.Any(e => e.EffectType == "root" && e.Duration > 0);
/// <summary>Check if mob is silenced</summary>
public bool IsSilenced => StatusEffects.Any(e => e.EffectType == "silence" && e.Duration > 0);
/// <summary>Get current speed modifier from status effects</summary>
public float GetSpeedModifier()
{
float modifier = 1.0f;
foreach (var effect in StatusEffects.Where(e => e.EffectType == "slow"))
{
modifier *= effect.Modifier;
}
return modifier;
}
/// <summary>Update facing direction based on movement</summary>
public void UpdateFacingDirection(float targetX, float targetY)
{
float dx = targetX - X;
float dy = targetY - Y;
if (Math.Abs(dx) > Math.Abs(dy))
{
FacingDirection = dx > 0 ? "right" : "left";
}
else
{
FacingDirection = dy > 0 ? "down" : "up";
}
}
/// <summary>Get the animation key with direction</summary>
public string GetDirectionalAnimation(string baseAnim)
{
return $"{baseAnim}_{FacingDirection}";
}
}
public enum MobState
{
Idle,
Chasing,
Attacking,
Casting,
Returning,
Fleeing,
Kiting, // Moving away while attacking (skirmisher)
Summoning, // In summoning animation
Healing, // Healing self or ally
Dead,
Walking,
Damaged
}