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.
This commit is contained in:
Kamron Batman 2026-10-18 17:41:27 -07:00 • committed by GitHub
parent 8cced36794
commit 4e57ec2373
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 182 additions and 61 deletions

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ModernUO.Serialization;
@ -1500,10 +1500,47 @@ namespace Server.Mobiles
public List<MLQuest> MLQuests =>
(m_MLQuests ??= StaticMLQuester ? MLQuestSystem.FindQuestList(GetType()) : ConstructQuestList()) ?? MLQuestSystem.EmptyList;
/// <summary>
/// The abilities this creature has. Must be a pure, stable query: it is called from lookups, damage
/// calculations and cleanup, so it must not spawn, mutate state, or change with the creature's state.
/// Gate abilities that should only work some of the time with <see cref="CanUseAbility" />.
/// </summary>
public virtual MonsterAbility[] GetMonsterAbilities() => null;
/// <summary>
/// Whether <paramref name="ability" /> may trigger or be looked up right now. Must be a pure read.
/// </summary>
public virtual bool CanUseAbility(MonsterAbility ability) => true;
private MonsterAbilityTrigger _activeTriggers;
// Deadlines in Core.TickCount, keyed by ability. Lives on the creature so it is collected with it.
private Dictionary<MonsterAbility, long> _abilityCooldowns;
public bool IsAbilityOnCooldown(MonsterAbility ability)
{
if (_abilityCooldowns?.TryGetValue(ability, out var nextTrigger) != true)
{
return false;
}
if (nextTrigger - Core.TickCount > 0)
{
return true;
}
_abilityCooldowns.Remove(ability);
return false;
}
public void SetAbilityCooldown(MonsterAbility ability, TimeSpan cooldown)
{
_abilityCooldowns ??= new Dictionary<MonsterAbility, long>();
_abilityCooldowns[ability] = Core.TickCount + (long)cooldown.TotalMilliseconds;
}
public void ClearAbilityCooldowns() => _abilityCooldowns = null;
public virtual MonsterAbility GetAbility(MonsterAbilityType type)
{
var abilities = GetMonsterAbilities();
@ -1516,6 +1553,11 @@ namespace Server.Mobiles
for (var i = 0; i < abilities.Length; i++)
{
var ability = abilities[i];
if (!CanUseAbility(ability))
{
continue;
}
if (ability is MonsterAbilityGroup group)
{
ability = group.GetAbilityWithType(type);
@ -1579,7 +1621,7 @@ namespace Server.Mobiles
for (var i = 0; i < abilities.Length; i++)
{
var ability = abilities[i];
if (ability.CanTrigger(this, trigger))
if (CanUseAbility(ability) && ability.CanTrigger(this, trigger))
{
ability.Trigger(trigger, this, defender);
triggered = true;
@ -1606,7 +1648,7 @@ namespace Server.Mobiles
for (var i = 0; i < abilities.Length; i++)
{
var ability = abilities[i];
if (ability.CanTrigger(this, trigger))
if (CanUseAbility(ability) && ability.CanTrigger(this, trigger))
{
ability.Move(this, d);
}
@ -1625,7 +1667,7 @@ namespace Server.Mobiles
for (var i = 0; i < abilities.Length; i++)
{
var ability = abilities[i];
if (ability.CanTrigger(this, trigger))
if (CanUseAbility(ability) && ability.CanTrigger(this, trigger))
{
if ((trigger & MonsterAbilityTrigger.GiveMeleeDamage) != 0)
{
@ -1666,7 +1708,7 @@ namespace Server.Mobiles
for (var i = 0; i < abilities.Length; i++)
{
var ability = abilities[i];
if (ability.CanTrigger(this, trigger))
if (CanUseAbility(ability) && ability.CanTrigger(this, trigger))
{
if ((trigger & MonsterAbilityTrigger.GiveSpellDamage) != 0)
{