ModernUO/Projects/UOContent/Mobiles/Abilities/MonsterAbility.cs
Kamron Batman 4e57ec2373
fix: monster ability queries no longer have side effects (#2716)
## Problem
`GetMonsterAbilities()` answers three questions through one virtual: which abilities a creature has (`GetAbility`, `AOS.cs` reflect/barrier, bard fire-breath difficulty), which abilities are enabled right now, and where cooldown state must be cleaned up (`InvalidateNextAbilityTriggers` on death and delete). Every caller assumes it is a pure, stable list. About 70 creatures return a static array; only LordOaks computed it:

```
Mobile.Delete()
 → BaseCreature.OnDelete() → CreatureDeletedEvent
 → MonsterAbility.InvalidateNextAbilityTriggers()
 → LordOaks.GetMonsterAbilities() → CheckQueen() → new Silvani().MoveToWorld(...)
```

- Deleting a Lord Oaks nobody had fought (champion reset, `DeleteCreatures`, GM delete) spawned an orphaned Silvani where he stood.
- The spell damage scalar (`AlterDamageScalarFrom`) spawned her too.
- With the queen dead his list was empty, so cleanup skipped his cooldown entries. The static `Dictionary<BaseCreature, long>` on the ability singleton kept a reference to the deleted creature. `MonsterAbilityGroup` children's entries were never cleaned either.
- Neira threw unholy bones from `AlterDamageScalarFrom`, so any caller asking for a damage scalar (a preview gump, for example) could fire the effect.

## Changes
- **Per-creature cooldowns:** a lazy `Dictionary<MonsterAbility, long>` on `BaseCreature` (`IsAbilityOnCooldown`, `SetAbilityCooldown`, `ClearAbilityCooldowns`). The static dictionaries and the query-driven cleanup are gone, and death still resets cooldowns. Tick comparisons are wraparound-safe.
- **Pure contract:** `GetMonsterAbilities()` is documented as pure. The new `virtual bool CanUseAbility(MonsterAbility)` gates abilities that only work some of the time. Every `TriggerAbility*` loop and `GetAbility` check it.
- **LordOaks:** always lists his abilities; `CanUseAbility` and the 10% spell shield read "queen alive". The queen spawns when he is first aggressed or first given a combatant, never on the internal map. Aggression lands before the damage it causes, so the first spell is still shielded, as before.
- **Neira:** the unholy bone throw moves to `OnDamagedBySpell`.

A follow-up PR makes minions such as the queen, pixies and Barracoon's rats belong to their champion spawn, so a spawn reset cleans them up.

## Testing
- `LordOaksTests` covers three cases:
  - querying or deleting an unengaged Lord Oaks spawns nothing;
  - aggression spawns the queen exactly once, and the shield and abilities follow her life;
  - an Oaks on the internal map never spawns her.
- UOContent.Tests: 1234 passed, 2 skipped.
- A local constructor sweep (every Mobile built and deleted repeatedly) no longer reports LordOaks.
2026-10-18 17:41:27 -07:00

89 lines
2.8 KiB
C#

using System;
using ModernUO.CodeGeneratedEvents;
namespace Server.Mobiles;
/// <summary>
/// Abstract class used to build singletons for managing a specific monster ability.
/// </summary>
public abstract class MonsterAbility
{
public virtual MonsterAbilityType AbilityType => MonsterAbilityType.Generic;
public virtual MonsterAbilityTrigger AbilityTrigger => MonsterAbilityTrigger.None;
public virtual double ChanceToTrigger => 1.0;
public virtual TimeSpan MinTriggerCooldown => TimeSpan.Zero;
public virtual TimeSpan MaxTriggerCooldown => TimeSpan.Zero;
/// <summary>
/// Returns true if ability is not on cooldown, and the chance to trigger succeeds.
/// </summary>
/// <returns>Boolean indicating the ability can trigger.</returns>
public virtual bool CanTrigger(BaseCreature source, MonsterAbilityTrigger trigger)
{
if ((AbilityTrigger & trigger) == 0 || source is not { Alive: true, Deleted: false })
{
return false;
}
if (source.IsAbilityOnCooldown(this))
{
return false;
}
var c = ChanceToTrigger;
return c >= 1 || c > 0 && c > Utility.RandomDouble();
}
/// <summary>
/// Triggers the monster's ability. Override this and call `base.Trigger(source);` to make sure
/// the cooldown is tracked.
/// Note: This can fire after a monster is killed/deleted (map is null)
/// </summary>
public virtual void Trigger(MonsterAbilityTrigger trigger, BaseCreature source, Mobile target)
{
if (MinTriggerCooldown <= TimeSpan.Zero && MaxTriggerCooldown <= TimeSpan.Zero)
{
return;
}
var randomAmount = MinTriggerCooldown == MaxTriggerCooldown
? MinTriggerCooldown
: Utility.RandomMinMax(MinTriggerCooldown, MaxTriggerCooldown);
source.SetAbilityCooldown(this, randomAmount);
}
public virtual void AlterMeleeDamageFrom(BaseCreature source, Mobile target, ref int damage)
{
}
public virtual void AlterMeleeDamageTo(BaseCreature source, Mobile target, ref int damage)
{
}
public virtual void AlterSpellDamageScalarFrom(BaseCreature source, Mobile target, ref double scalar)
{
}
public virtual void AlterSpellDamageScalarTo(BaseCreature source, Mobile target, ref double scalar)
{
}
public virtual void AlterSpellDamageFrom(BaseCreature source, Mobile target, ref int damage)
{
}
public virtual void AlterSpellDamageTo(BaseCreature source, Mobile target, ref int damage)
{
}
public virtual void Move(BaseCreature source, Direction d)
{
}
[OnEvent(nameof(CreatureEvents.CreatureDeathEvent))]
public static void ResetCooldownsOnDeath(BaseCreature source) => source.ClearAbilityCooldowns();
}