ModernUO/Projects/UOContent/Mobiles/AI/BaseAI/TransferItem.cs
Kamron Batman 05b6ca9d5d
refactor: one master reference behind ControlMaster and SummonMaster (#2670)
## Summary

`BaseCreature` stored the pet owner (`ControlMaster`) and the summoner (`SummonMaster`) in two fields. Every flow that manages pets kept them in step by hand: summon, transfer, stable, claim, ball of summoning and GM obey. They now share one `_master` field, and the two properties read it through the Controlled and Summoned flags:

```csharp
ControlMaster => Controlled ? master : null
SummonMaster  => Controlled && !Summoned ? null : master
```

Every existing reader keeps its meaning:
- `ControlMaster` is still "the owner".
- `SummonMaster` is still "the summoner, controlled or not".

Blade spirits, energy vortexes, clones, undead minions and enraged creatures (a summon master without `Summoned`) never become visible through `ControlMaster`, so the ungated readers (ML notoriety, aggressor entries and similar) behave as before.

- Setting `SummonMaster` on a plain pet does nothing, instead of quietly replacing the owner.
- `SetControlMaster` now keeps both masters in step, so the hand-written `SummonMaster` copies are removed from ball of summoning, GM obey, pet transfer, auto-stable and claim, the animal trainer's stable and claim, and jail stabling.

## Serialization

BaseCreature moves to v24 with one serialized master (slot 15); the old `SummonMaster` slot is dropped.
- `MigrateFrom(V23Content)` resolves absent fields through the same default-value methods the live reader uses.
- A save that carried both masters (a summon traded before ownership followed transfers) resolves to the owner. Legacy pre-codegen streams follow the same rule.

Requires `ModernUO.Serialization.Generator` 4.2.0 (#2671). `UOContent.Tests` was still on 4.1.0 and is bumped to match.

## Test plan

- [x] `MasterShapes_RoundTrip`: controlled pet, controlled summon, energy vortex, enraged creature
- [x] `SummonMaster_OnPlainPet_KeepsTheOwner`: owner and follower counts unchanged
- [x] `LegacyV22Stream_DifferingMasters_OwnerWins`
- [x] `V23Stream_MigratesToOneMaster_AndDefaultsAbsentFields`: both master slots -> owner; flags past bit 32; absent fields take class defaults
- [x] UOContent.Tests (1168 passed, 2 skipped) and Server.Tests (907) green
2026-09-24 21:37:28 -07:00

171 lines
5.9 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)
{
_creature.ClearPetFriends();
_creature.IssueOrder(OrderType.Follow, null, to);
_creature.BondingBegin = DateTime.MinValue;
_creature.OwnerAbandonTime = DateTime.MinValue;
_creature.IsBonded = false;
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.
}
}