ModernUO/Projects/UOContent.Tests/Tests/Mobiles/LordOaksTests.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

98 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Server;
using Server.Mobiles;
using Xunit;
namespace UOContent.Tests.Mobiles;
// Lord Oaks' abilities and spell shield depend on his queen, so the queen must spawn from combat,
// never from a query such as GetMonsterAbilities or a damage calculation.
[Collection("Sequential UOContent Tests")]
public class LordOaksTests : IDisposable
{
private readonly List<Mobile> _created = [];
public void Dispose()
{
foreach (var m in World.Mobiles.Values.OfType<Silvani>().ToList())
{
m.Delete();
}
for (var i = 0; i < _created.Count; i++)
{
_created[i].Delete();
}
}
private LordOaks NewOaks(Map map)
{
var oaks = new LordOaks();
_created.Add(oaks);
oaks.MoveToWorld(new Point3D(1000, 1000, 0), map);
return oaks;
}
private Mobile NewAttacker()
{
var attacker = new Mongbat();
_created.Add(attacker);
return attacker;
}
private static int SilvaniCount() => World.Mobiles.Values.Count(m => m is Silvani);
[Fact]
public void QueryingOrDeletingAnUnengagedOaks_SpawnsNoQueen()
{
var oaks = NewOaks(Map.Felucca);
var scalar = 1.0;
oaks.AlterDamageScalarFrom(null, ref scalar);
oaks.GetMonsterAbilities();
oaks.Delete();
Assert.Equal(1.0, scalar);
Assert.Equal(0, SilvaniCount());
}
[Fact]
public void Aggression_SpawnsTheQueenOnce_AndShieldsOaksWhileSheLives()
{
var oaks = NewOaks(Map.Felucca);
var attacker = NewAttacker();
oaks.AggressiveAction(attacker, false);
oaks.AggressiveAction(attacker, false);
var queen = World.Mobiles.Values.OfType<Silvani>().Single();
var shielded = 1.0;
oaks.AlterDamageScalarFrom(attacker, ref shielded);
Assert.Equal(0.1, shielded, 6);
Assert.True(oaks.CanUseAbility(MonsterAbilities.SummonPixiesCounter));
queen.Delete();
var exposed = 1.0;
oaks.AlterDamageScalarFrom(attacker, ref exposed);
oaks.AggressiveAction(attacker, false);
Assert.Equal(1.0, exposed);
Assert.False(oaks.CanUseAbility(MonsterAbilities.SummonPixiesCounter));
Assert.Equal(0, SilvaniCount());
}
[Fact]
public void AnOaksOnTheInternalMap_NeverSpawnsTheQueen()
{
var oaks = new LordOaks();
_created.Add(oaks);
oaks.AggressiveAction(NewAttacker(), false);
Assert.Equal(0, SilvaniCount());
}
}