ModernUO/Projects/UOContent/Mobiles/AI/BaseAI/TransferItem.cs
Kamron Batman ab738d90af
fix: pet release never finished, summon master follows the pet, horse breeder and notoriety guards (#2613)
Pet and summon bugs found by the master-reference audit for #2592 (comments there have the full inventory). Independent of delta saves.

## Releasing a pet never finished

The Release order ran two half-releases that never met:

- The order handler cleared the master. That set `Controlled` to false, so `Obey` never ran again and the think-side `DoOrderRelease` (re-home, three-day delete timer, backpack drop) was dead code: a released pet kept its pack and never despawned.
- The loyalty drain called `DoOrderRelease` directly, so a pet whose loyalty hit zero got the countdown and dropped its pack but kept its master and its owner's follower slots, the opposite of the code comment's intent.

`DoOrderRelease` is now the whole release (targets, bonding, `SetControlMaster(null)`, re-home, delete or countdown, pack drop) and runs once, synchronously, from the handler or the loyalty drain. Summons still die on release, as before. Two tests pin both entry points: master cleared, follower slots returned, countdown running, home anchored.

This is the one place the `PetOrders` / `PetOrderHandlers` split bit; the wider audit of that duplication is a separate task.

## Summon master follows the pet

Transfer, stable claim, GM "obey" and Ball of Summoning copied `SummonMaster` only when `Summoned`, so a talisman summon (`Summoned` is false, `SummonMaster` set) kept its original summoner after a transfer: two different masters on one creature, with the original summoner's area spells still exempting it. They now mirror the summon master whenever it is set. Jail stabling cleared only `ControlMaster`, so a jailed talisman summon kept charging the summoner's follower slots; it now clears both, like the stable master and auto-stable already do.

## Small ones

- The faction horse breeder set `Controlled`/`ControlMaster` directly; it now goes through `SetControlMaster` and tells the buyer why it refused (1049607) instead of silently deleting the horse.
- `Notoriety` dereferenced `SummonMaster` on a summoned creature without a null check.

## Tests

UOContent.Tests 778 green (two new).
2026-09-06 16:12:29 -07:00

177 lines
6.1 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TransferItem.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
using System;
using System.Runtime.CompilerServices;
using ModernUO.Serialization;
namespace Server.Mobiles;
[SerializationGenerator(0, false)]
internal sealed partial class TransferItem : Item
{
private readonly BaseCreature _creature;
public override string DefaultName => _creature.GetType().Name;
public TransferItem(BaseCreature creature) : base(ShrinkTable.Lookup(creature))
{
_creature = creature;
Movable = false;
Hue = creature.Hue & 0x0FFF;
}
public override bool SkipSerialization => true;
public static bool IsInCombat(BaseCreature creature) => creature?.Aggressors.Count > 0 || creature?.Aggressed.Count > 0;
public override void GetProperties(IPropertyList list)
{
base.GetProperties(list);
list.Add(1041603); // This item represents a pet currently in consideration for trade
list.Add(1041601, _creature.Name); // Pet Name: ~1_val~
if (_creature.ControlMaster != null)
{
list.Add(1041602, _creature.ControlMaster.Name); // Owner: ~1_val~
}
}
public override bool AllowSecureTrade(Mobile from, Mobile to, Mobile newOwner, bool accepted)
{
if (!base.AllowSecureTrade(from, to, newOwner, accepted) || IsInvalidTrade(from, to))
{
return false;
}
return !accepted || HandleAcceptedTrade(from, to);
}
private bool IsInvalidTrade(Mobile from, Mobile to) =>
Deleted
|| _creature?.Deleted != false
|| _creature.ControlMaster != from
|| !from.CheckAlive()
|| !to.CheckAlive()
|| from.Map != _creature.Map
|| !from.InRange(_creature, 14);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool HandleAcceptedTrade(Mobile from, Mobile to) =>
ValidateYoungStatus(from, to) && ValidateControlStatus(from, to) && ValidateFollowerLimit(to) &&
!IsInCombat(_creature);
private bool ValidateFollowerLimit(Mobile to)
{
if (to.Followers + _creature.ControlSlots > to.FollowersMax)
{
to.SendLocalizedMessage(1049607);
// You have too many followers to control that creature.
return false;
}
return true;
}
private static bool ValidateYoungStatus(Mobile from, Mobile to)
{
var youngFrom = from is PlayerMobile mobile && mobile.Young;
var youngTo = to is PlayerMobile playerMobile && playerMobile.Young;
if (youngFrom && !youngTo)
{
from.SendLocalizedMessage(502051);
// As a young player, you may not transfer pets to older players.
return false;
}
if (!youngFrom && youngTo)
{
from.SendLocalizedMessage(502052);
// As an older player, you may not transfer pets to young players.
return false;
}
return true;
}
private bool ValidateControlStatus(Mobile from, Mobile to)
{
if (!_creature.CanBeControlledBy(to))
{
SendTransferRefusalMessages(from, to, 1043248, 1043249);
// The pet refuses to be transferred because it will not obey ~1_NAME~.~3_BLANK~
// The pet will not accept you as a master because it does not trust you.~3_BLANK~
return false;
}
if (!_creature.CanBeControlledBy(from))
{
SendTransferRefusalMessages(from, to, 1043250, 1043251);
// The pet refuses to be transferred because it will not obey you sufficiently.~3_BLANK~
// The pet will not accept you as a master because it does not trust ~2_NAME~.~3_BLANK~
return false;
}
return true;
}
private static void SendTransferRefusalMessages(Mobile from, Mobile to, int fromMessage, int toMessage)
{
var args = $"{to.Name}\t{from.Name}\t ";
from.SendLocalizedMessage(fromMessage, args);
to.SendLocalizedMessage(toMessage, args);
}
public override void OnSecureTrade(Mobile from, Mobile to, Mobile newOwner, bool accepted)
{
if (Deleted || IsInvalidTrade(from, to))
{
Delete();
return;
}
Delete();
if (!accepted || !_creature.SetControlMaster(to))
{
return;
}
TransferPetOwnership(from, to);
}
private void TransferPetOwnership(Mobile from, Mobile to)
{
if (_creature.SummonMaster != null)
{
_creature.SummonMaster = to;
}
_creature.ControlTarget = to;
_creature.ControlOrder = OrderType.Follow;
_creature.BondingBegin = DateTime.MinValue;
_creature.OwnerAbandonTime = DateTime.MinValue;
_creature.IsBonded = false;
_creature.PlaySound(_creature.GetIdleSound());
var args = $"{from.Name}\t{_creature.Name}\t{to.Name}";
from.SendLocalizedMessage(1043253, args);
// You have transferred your pet to ~3_GETTER~.
to.SendLocalizedMessage(1043252, args);
// ~1_NAME~ has transferred the allegiance of ~2_PET_NAME~ to you.
}
}