fix: Fixes stabled, abilities targeting self, and unsummon memory leak (#1418)

## **MAJOR CHANGE**
* `Stabled` has been moved to `PlayerMobile`.
* New methods added, `PlayerMobile.AddStabled` and `PlayerMobile.RemoveStabled`.
* Added `PlayerMobile.AddFollower` and `PlayerMobile.RemoveFollower`.
* `Stabled`, `AutoStabled`, and `AllFollowers` are now `HashSet` and **_CAN BE NULL_**.

### Summary
* Fixes monster abilities causing harm to the monster through reflect
* Adds `CanTriggerAgainstSelf` to override this for healing or some other self-affecting ability
* Fixes a major memory leak where `UnsummonTimer` from animated dead spell lasts up-to 24hrs and therefore holds onto references of dead/deleted mobs.
* Fixes another minor leak where a mob is not unregistered from the animated dead spell list until the next spell cast.
This commit is contained in:
Kamron Batman 2023-07-03 09:45:22 -07:00 • committed by GitHub
parent d99e72db8f
commit 3b97e8d39c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 391 additions and 280 deletions

View file

@ -1,5 +1,6 @@
using ModernUO.Serialization;
using System.Collections.Generic;
using Server.Collections;
using Server.ContextMenus;
using Server.Gumps;
using Server.Items;
@ -41,11 +42,11 @@ namespace Server.Mobiles
public override void AddCustomContextEntries(Mobile from, List<ContextMenuEntry> list)
{
if (from.Alive)
if (from is PlayerMobile { Alive: true } pm)
{
list.Add(new StableEntry(this, from));
if (from.Stabled.Count > 0)
if (pm.Stabled?.Count > 0)
{
list.Add(new ClaimAllEntry(this, from));
}
@ -105,31 +106,38 @@ namespace Server.Mobiles
public void BeginClaimList(Mobile from)
{
if (Deleted || !from.CheckAlive())
if (Deleted || !from.CheckAlive() || from is not PlayerMobile pm)
{
return;
}
var list = new List<BaseCreature>();
for (var i = 0; i < from.Stabled.Count; ++i)
if (pm.Stabled?.Count > 0)
{
var pet = from.Stabled[i] as BaseCreature;
using var queue = PooledRefQueue<Mobile>.Create();
if (pet?.Deleted != false)
foreach (var m in pm.Stabled)
{
if (pet != null)
if (m is BaseCreature pet)
{
if (!pet.Deleted)
{
list.Add(pet);
break;
}
pet.IsStabled = false;
pet.StabledBy = null;
}
from.Stabled.RemoveAt(i);
--i;
continue;
queue.Enqueue(m);
}
list.Add(pet);
while (queue.Count > 0)
{
pm.RemoveStabled(queue.Dequeue());
}
}
if (list.Count > 0)
@ -144,7 +152,7 @@ namespace Server.Mobiles
public void EndClaimList(Mobile from, BaseCreature pet)
{
if (pet?.Deleted != false || from.Map != Map || !from.Stabled.Contains(pet) || !from.CheckAlive())
if (pet?.Deleted != false || from.Map != Map || from is not PlayerMobile pm || pm.Stabled?.Contains(pet) != true || !from.CheckAlive())
{
return;
}
@ -159,9 +167,8 @@ namespace Server.Mobiles
{
DoClaim(from, pet);
from.Stabled.Remove(pet);
(from as PlayerMobile)?.AutoStabled.Remove(pet);
pm.RemoveStabled(pet);
pm.AutoStabled?.Remove(pet);
}
else
{
@ -176,8 +183,6 @@ namespace Server.Mobiles
return;
}
Container bank = from.FindBankNoCreate();
if (!(from.Backpack?.GetAmount(typeof(Gold)) >= 30) &&
!(Banker.GetBalance(from) >= 30))
{
@ -197,7 +202,7 @@ namespace Server.Mobiles
public void EndStable(Mobile from, BaseCreature pet)
{
if (Deleted || !from.CheckAlive())
if (Deleted || !from.CheckAlive() || from is not PlayerMobile pm)
{
return;
}
@ -236,14 +241,12 @@ namespace Server.Mobiles
{
SayTo(from, 1042564); // I'm sorry. Your pet seems to be busy.
}
else if (from.Stabled.Count >= GetMaxStabled(from))
else if (pm.Stabled?.Count >= GetMaxStabled(from))
{
SayTo(from, 1042565); // You have too many pets in the stables!
}
else
{
Container bank = from.FindBankNoCreate();
if (from.Backpack?.ConsumeTotal(typeof(Gold), 30) == true || Banker.Withdraw(from, 30))
{
pet.ControlTarget = null;
@ -261,14 +264,10 @@ namespace Server.Mobiles
pet.Loyalty = MaxLoyalty; // Wonderfully happy
}
from.Stabled.Add(pet);
pm.AddStabled(pet);
SayTo(
from,
Core.AOS
? 1049677
: 502679
); // [AOS: Your pet has been stabled.] Very well, thy pet is stabled. Thou mayst recover it by saying 'claim' to me. In one real world week, I shall sell it off if it is not claimed!
// [AOS: Your pet has been stabled.] Very well, thy pet is stabled. Thou mayst recover it by saying 'claim' to me. In one real world week, I shall sell it off if it is not claimed!
SayTo(from, Core.AOS ? 1049677 : 502679);
}
else
{
@ -279,7 +278,7 @@ namespace Server.Mobiles
public void Claim(Mobile from, string petName = null)
{
if (Deleted || !from.CheckAlive())
if (Deleted || !from.CheckAlive() || from is not PlayerMobile pm)
{
return;
}
@ -289,44 +288,51 @@ namespace Server.Mobiles
var claimByName = petName != null;
for (var i = 0; i < from.Stabled.Count; ++i)
if (pm.Stabled?.Count > 0)
{
var pet = from.Stabled[i] as BaseCreature;
using var queue = PooledRefQueue<Mobile>.Create();
if (pet?.Deleted != false)
foreach (var m in pm.Stabled)
{
if (pet != null)
var pet = m as BaseCreature;
if (pet?.Deleted != false)
{
pet.IsStabled = false;
pet.StabledBy = null;
if (pet != null)
{
pet.IsStabled = false;
pet.StabledBy = null;
}
queue.Enqueue(pet);
continue;
}
++stabled;
if (claimByName && !pet.Name.InsensitiveEquals(petName))
{
continue;
}
if (CanClaim(from, pet))
{
DoClaim(from, pet);
queue.Enqueue(pet);
claimed = true;
pm.AutoStabled?.Remove(pet);
}
else
{
SayTo(from, 1049612, pet.Name); // ~1_NAME~ remained in the stables because you have too many followers.
}
from.Stabled.RemoveAt(i);
--i;
continue;
}
++stabled;
if (claimByName && !pet.Name.InsensitiveEquals(petName))
while (queue.Count > 0)
{
continue;
}
if (CanClaim(from, pet))
{
DoClaim(from, pet);
from.Stabled.RemoveAt(i);
(from as PlayerMobile)?.AutoStabled.Remove(pet);
--i;
claimed = true;
}
else
{
SayTo(from, 1049612, pet.Name); // ~1_NAME~ remained in the stables because you have too many followers.
pm.RemoveStabled(queue.Dequeue());
}
}