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
This commit is contained in:
@@ -435,16 +435,35 @@ public partial class DungeonSimulation
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void ProcessMobAbility(MobInstance mob, PlayerInstance target)
|
private void ProcessMobAbility(MobInstance mob, PlayerInstance target)
|
||||||
{
|
{
|
||||||
// Find the best currently usable ability.
|
const int maxConsecutiveAbilityRepeats = 3;
|
||||||
var ability = mob.Abilities
|
|
||||||
|
// Find currently usable abilities.
|
||||||
|
var usableAbilities = mob.Abilities
|
||||||
.Where(a => a.IsAvailable(mob.HpPercent))
|
.Where(a => a.IsAvailable(mob.HpPercent))
|
||||||
.Where(a => a.Phase == 0 || a.Phase == mob.CurrentPhase)
|
.Where(a => a.Phase == 0 || a.Phase == mob.CurrentPhase)
|
||||||
.Where(a => IsImplementedAbilityType(a.Definition.AbilityType))
|
.Where(a => IsImplementedAbilityType(a.Definition.AbilityType))
|
||||||
.Where(a => CanUseAbilityNow(mob, target, a.Definition))
|
.Where(a => CanUseAbilityNow(mob, target, a.Definition))
|
||||||
.OrderByDescending(a => a.Priority)
|
.OrderByDescending(a => a.Priority)
|
||||||
.FirstOrDefault();
|
.ToList();
|
||||||
|
|
||||||
if (ability == null) return;
|
if (usableAbilities.Count == 0) return;
|
||||||
|
|
||||||
|
var ability = usableAbilities[0];
|
||||||
|
|
||||||
|
// Anti-repeat: if a mob is stuck using one ability and other valid options exist,
|
||||||
|
// force variety after a small streak.
|
||||||
|
if (usableAbilities.Count > 1 &&
|
||||||
|
!string.IsNullOrWhiteSpace(mob.LastUsedAbilityKey) &&
|
||||||
|
string.Equals(ability.Definition.Key, mob.LastUsedAbilityKey, StringComparison.OrdinalIgnoreCase) &&
|
||||||
|
mob.ConsecutiveAbilityUses >= maxConsecutiveAbilityRepeats)
|
||||||
|
{
|
||||||
|
var alternate = usableAbilities
|
||||||
|
.FirstOrDefault(a => !string.Equals(a.Definition.Key, mob.LastUsedAbilityKey, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (alternate != null)
|
||||||
|
{
|
||||||
|
ability = alternate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int manaCost = Math.Max(0, ability.Definition.ManaCost);
|
int manaCost = Math.Max(0, ability.Definition.ManaCost);
|
||||||
if (manaCost > 0)
|
if (manaCost > 0)
|
||||||
@@ -455,6 +474,17 @@ public partial class DungeonSimulation
|
|||||||
// Put ability on cooldown
|
// Put ability on cooldown
|
||||||
ability.CooldownRemaining = ability.Definition.Cooldown;
|
ability.CooldownRemaining = ability.Definition.Cooldown;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(mob.LastUsedAbilityKey) &&
|
||||||
|
string.Equals(mob.LastUsedAbilityKey, ability.Definition.Key, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
mob.ConsecutiveAbilityUses++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mob.LastUsedAbilityKey = ability.Definition.Key;
|
||||||
|
mob.ConsecutiveAbilityUses = 1;
|
||||||
|
}
|
||||||
|
|
||||||
string animationKey = ResolveAbilityAnimationKey(mob, ability);
|
string animationKey = ResolveAbilityAnimationKey(mob, ability);
|
||||||
|
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
|
|||||||
@@ -107,6 +107,12 @@ public class MobInstance
|
|||||||
/// <summary>Abilities this mob can use</summary>
|
/// <summary>Abilities this mob can use</summary>
|
||||||
public List<MobAbilityState> Abilities { get; set; } = new();
|
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>
|
/// <summary>Currently casting ability (null if not casting)</summary>
|
||||||
public MobAbilityState? CastingAbility { get; set; }
|
public MobAbilityState? CastingAbility { get; set; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user