## Summary After #2670, `BaseCreature` stores one master. Callers still reached it through three differently gated names plus hand-rolled combinations of the `Controlled`/`Summoned` flags. Each gate is now one named view: | Member | Definition | Meaning | |---|---|---| | `Master` | the reference; its setter does the follower bookkeeping; serialized as before | whoever the creature answers to | | `ControlMaster` | `Controlled ? Master : null` | the owner | | `SummonMaster` | `Summoned ? Master : null` | the summoner | | `GetMaster()` | `ControlMaster ?? SummonMaster` | who answers for the creature | `ControlMaster` and `SummonMaster` stay settable so custom code keeps compiling: both setters assign `Master`. There is no guard, so a write through either one can read back as null until the matching flag is set. An enraged creature is the one shape with a master and neither flag. It keeps its meer through `Master`, while `GetMaster()` stays null, so the meer never answers for it (notoriety, kill credit). Readers move onto the view they mean: - "Owner or summoner" pairs and `ControlMaster ?? SummonMaster` collapse into `GetMaster()`: pack instinct, familiars, golem, house access, mounts, pack horse, Solen friendship, spell target and guild checks, and the `[` pet command. - Checks such as `Controlled && ControlMaster == from` drop the flag the view already applies. Checks that don't reduce keep it: `Controlled && ControlMaster != from`, a possibly-null `from`, and `MageAI.CanDispel`'s `Summoned &&`. **No behavior change, save format unchanged** (the schema generator produces no diff). Edge cases for custom code and shard operators: - `SummonMaster = x` on a plain pet now replaces its owner (#2670 made that a no-op), and `ControlMaster = null` on an uncontrolled summon now clears its caster (it did nothing before). - `SpellHelper.GetGuildFor` no longer falls back to an enraged creature's meer. Meer mages have no guild. ## Docs - `dev-docs/content-patterns.md` § Masters: which view answers which question, how to set a master, and which flag checks are redundant. - `dev-docs/runuo-migration-docs/09-items-mobiles-creatures.md` § Masters: RunUO's two-field reads mapped onto the views, plus the gotcha for creatures that set `SummonMaster` without `Summoned` (the write compiles, the read now returns null; use `Master`). - The `modernuo-content-patterns` and `migrate-items-mobiles` skills point at both. ## Test plan - [x] `MasterViewsTests`: all four views across wild, pet, controlled summon, uncontrolled summon and enraged shapes; reassigning `Master` moves follower slots; `SetControlMaster(null)` keeps an uncontrolled summon's caster; deleting a summon returns the caster's slots - [x] Serialization tests updated to the new views (round-trip, legacy v22, v23 migration) - [x] UOContent.Tests (1178 passed, 2 skipped) and Server.Tests (907) green
226 lines
6.8 KiB
C#
226 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Server;
|
|
using Server.Mobiles;
|
|
using Xunit;
|
|
|
|
namespace UOContent.Tests.Mobiles.AI;
|
|
|
|
// Publish 51 (26 March 2008): a pet told to follow, come, stay or stop "will not attack
|
|
// anything, even if it is attacked". Guard and attack are unaffected.
|
|
[Collection("Sequential UOContent Tests")]
|
|
public class PetRetaliationTests : IDisposable
|
|
{
|
|
private readonly List<Mobile> _created = new();
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var m in _created)
|
|
{
|
|
m?.Delete();
|
|
}
|
|
|
|
_created.Clear();
|
|
}
|
|
|
|
private sealed class StandDownPet : PetTestStub
|
|
{
|
|
public override bool StandsDownOnCommand => true;
|
|
}
|
|
|
|
private sealed class FightBackPet : PetTestStub
|
|
{
|
|
public override bool StandsDownOnCommand => false;
|
|
}
|
|
|
|
private (PlayerMobile master, T pet) Spawn<T>() where T : BaseCreature, new()
|
|
{
|
|
var master = new PlayerMobile(World.NewMobile);
|
|
master.DefaultMobileInit();
|
|
master.MoveToWorld(new Point3D(1000, 1000, 0), Map.Felucca);
|
|
_created.Add(master);
|
|
|
|
var pet = new T();
|
|
pet.MoveToWorld(new Point3D(1001, 1000, 0), Map.Felucca);
|
|
pet.SetControlMaster(master);
|
|
pet.AIObject.AITimer?.Stop();
|
|
_created.Add(pet);
|
|
|
|
return (master, pet);
|
|
}
|
|
|
|
private void Order(BaseCreature pet, Mobile master, OrderType order)
|
|
{
|
|
if (order == OrderType.None) // Publish 51's "stop": stops, may wander, will not attack
|
|
{
|
|
pet.ControlTarget = master;
|
|
pet.ControlOrder = OrderType.Follow;
|
|
pet.ControlOrder = OrderType.Stop;
|
|
return;
|
|
}
|
|
|
|
pet.ControlTarget = master;
|
|
pet.ControlOrder = order;
|
|
}
|
|
|
|
private BaseCreature Attack(BaseCreature pet)
|
|
{
|
|
var attacker = new PetTestStub();
|
|
attacker.MoveToWorld(new Point3D(1002, 1000, 0), pet.Map);
|
|
_created.Add(attacker);
|
|
pet.AIObject.AITimer?.Stop();
|
|
|
|
attacker.Combatant = pet; // a mob starts attacking the pet
|
|
return attacker;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(OrderType.Follow)]
|
|
[InlineData(OrderType.Come)]
|
|
[InlineData(OrderType.Stay)]
|
|
[InlineData(OrderType.None)] // stopped
|
|
public void StandDownOrder_IgnoresTheAttacker(OrderType order)
|
|
{
|
|
var (master, pet) = Spawn<StandDownPet>();
|
|
Order(pet, master, order);
|
|
var resting = pet.ControlOrder;
|
|
|
|
Attack(pet);
|
|
|
|
Assert.Equal(resting, pet.ControlOrder); // never converts to Attack
|
|
Assert.Null(pet.Combatant);
|
|
Assert.False(pet.Warmode);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(OrderType.Follow)]
|
|
[InlineData(OrderType.Come)]
|
|
[InlineData(OrderType.Stay)]
|
|
[InlineData(OrderType.None)]
|
|
public void WithoutStandDown_TheSameOrdersRetaliate(OrderType order)
|
|
{
|
|
var (master, pet) = Spawn<FightBackPet>();
|
|
Order(pet, master, order);
|
|
|
|
var attacker = Attack(pet);
|
|
|
|
Assert.Equal(OrderType.Attack, pet.ControlOrder);
|
|
Assert.Same(attacker, pet.Combatant);
|
|
}
|
|
|
|
// No damage callback may put a stand-down pet back into combat behind the policy's back.
|
|
[Theory]
|
|
[InlineData(Expansion.AOS, false)]
|
|
[InlineData(Expansion.AOS, true)]
|
|
[InlineData(Expansion.ML, false)]
|
|
[InlineData(Expansion.ML, true)]
|
|
public void StandDownPet_StaysDown_ThroughRepeatedDamage(Expansion era, bool spellDamage)
|
|
{
|
|
var previous = Core.Expansion;
|
|
|
|
try
|
|
{
|
|
Core.Expansion = era;
|
|
var (master, pet) = Spawn<StandDownPet>();
|
|
Order(pet, master, OrderType.Follow);
|
|
var attacker = Attack(pet);
|
|
|
|
Assert.Equal(OrderType.Follow, pet.ControlOrder); // the initial aggression stood down
|
|
|
|
for (var i = 0; i < 500 && pet.ControlOrder == OrderType.Follow; i++)
|
|
{
|
|
if (spellDamage)
|
|
{
|
|
pet.OnDamagedBySpell(attacker, 1);
|
|
}
|
|
else
|
|
{
|
|
pet.OnDamage(1, attacker, false);
|
|
}
|
|
}
|
|
|
|
Assert.Equal(OrderType.Follow, pet.ControlOrder);
|
|
Assert.Null(pet.Combatant);
|
|
}
|
|
finally
|
|
{
|
|
Core.Expansion = previous;
|
|
}
|
|
}
|
|
|
|
// "Guard: the pet should guard as it does currently."
|
|
[Fact]
|
|
public void GuardingPet_StillFights_UnderStandDown()
|
|
{
|
|
var (master, pet) = Spawn<StandDownPet>();
|
|
Order(pet, master, OrderType.Guard);
|
|
|
|
var attacker = Attack(pet);
|
|
|
|
Assert.Equal(OrderType.Guard, pet.ControlOrder);
|
|
Assert.Same(attacker, pet.Combatant);
|
|
Assert.True(pet.Warmode);
|
|
}
|
|
|
|
private sealed class UncommandablePet : PetTestStub
|
|
{
|
|
public override bool StandsDownOnCommand => true;
|
|
public override bool Commandable => false;
|
|
}
|
|
|
|
// The publish speaks of commanded pets. A creature nobody can give an order to was never
|
|
// told anything, yet it rests on None (wild, uncontrolled summon) or a system-issued Follow
|
|
// (familiar, escortee) - the same orders a pet stands down on. It must still fight back.
|
|
[Fact]
|
|
public void WildCreature_Retaliates_UnderStandDown()
|
|
{
|
|
var creature = new StandDownPet();
|
|
creature.MoveToWorld(new Point3D(1001, 1000, 0), Map.Felucca);
|
|
creature.AIObject.AITimer?.Stop();
|
|
_created.Add(creature);
|
|
|
|
Assert.Equal(OrderType.None, creature.ControlOrder);
|
|
|
|
var attacker = Attack(creature);
|
|
|
|
Assert.Same(attacker, creature.Combatant);
|
|
Assert.True(creature.Warmode);
|
|
}
|
|
|
|
[Fact] // energy vortex, blade spirits: Summoned with a SummonMaster, never Controlled
|
|
public void UncontrolledSummon_Retaliates_UnderStandDown()
|
|
{
|
|
var caster = new PlayerMobile(World.NewMobile);
|
|
caster.DefaultMobileInit();
|
|
caster.MoveToWorld(new Point3D(1000, 1000, 0), Map.Felucca);
|
|
_created.Add(caster);
|
|
|
|
var summon = new StandDownPet();
|
|
summon.Summoned = true;
|
|
summon.Master = caster;
|
|
summon.MoveToWorld(new Point3D(1001, 1000, 0), Map.Felucca);
|
|
summon.AIObject.AITimer?.Stop();
|
|
_created.Add(summon);
|
|
|
|
Assert.False(summon.Controlled);
|
|
Assert.Equal(OrderType.None, summon.ControlOrder);
|
|
|
|
var attacker = Attack(summon);
|
|
|
|
Assert.Same(attacker, summon.Combatant);
|
|
Assert.True(summon.Warmode);
|
|
}
|
|
|
|
[Fact] // familiar, escortee: Controlled with a master, but not Commandable
|
|
public void UncommandableCreature_Retaliates_UnderStandDown()
|
|
{
|
|
var (master, familiar) = Spawn<UncommandablePet>();
|
|
familiar.ControlTarget = master;
|
|
familiar.ControlOrder = OrderType.Follow; // system-issued, not a command
|
|
|
|
var attacker = Attack(familiar);
|
|
|
|
Assert.Same(attacker, familiar.Combatant);
|
|
Assert.True(familiar.Warmode);
|
|
}
|
|
}
|