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
This commit is contained in:
parent
77bd0d9a64
commit
05b6ca9d5d
10 changed files with 723 additions and 133 deletions
|
|
@ -182,22 +182,50 @@ public class BaseCreatureSerializationTests : IDisposable
|
|||
Assert.Equal(master, copy.LastOwner);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UncontrolledSummon_KeepsItsSummonMaster()
|
||||
// One serialized master; the Controlled and Summoned flags decide which views expose it.
|
||||
[Theory]
|
||||
[InlineData(true, false)] // controlled pet
|
||||
[InlineData(true, true)] // controlled summon
|
||||
[InlineData(false, true)] // energy vortex: summoned, never controlled
|
||||
[InlineData(false, false)] // EnragedCreature: a summon master without Summoned
|
||||
public void MasterShapes_RoundTrip(bool controlled, bool summoned)
|
||||
{
|
||||
var bc = NewCreature();
|
||||
var master = NewMaster();
|
||||
|
||||
// Energy vortex-style: summoned with a master, never controlled.
|
||||
bc.Summoned = true;
|
||||
bc.SummonMaster = master;
|
||||
bc.Summoned = summoned;
|
||||
|
||||
if (controlled)
|
||||
{
|
||||
bc.SetControlMaster(master);
|
||||
}
|
||||
else
|
||||
{
|
||||
bc.SummonMaster = master;
|
||||
}
|
||||
|
||||
var copy = Load(Snapshot(bc));
|
||||
|
||||
Assert.True(copy.Summoned);
|
||||
Assert.False(copy.Controlled);
|
||||
Assert.Equal(master, copy.SummonMaster);
|
||||
Assert.Null(copy.ControlMaster);
|
||||
Assert.Equal(controlled, copy.Controlled);
|
||||
Assert.Equal(summoned, copy.Summoned);
|
||||
Assert.Equal(controlled ? master : null, copy.ControlMaster);
|
||||
Assert.Equal(controlled && !summoned ? null : master, copy.SummonMaster);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SummonMaster_OnPlainPet_KeepsTheOwner()
|
||||
{
|
||||
var bc = NewCreature();
|
||||
var owner = NewMaster();
|
||||
var other = NewMaster();
|
||||
|
||||
bc.SetControlMaster(owner);
|
||||
bc.SummonMaster = other;
|
||||
|
||||
Assert.Equal(owner, bc.ControlMaster);
|
||||
Assert.Null(bc.SummonMaster);
|
||||
Assert.Equal(bc.ControlSlots, owner.Followers);
|
||||
Assert.Equal(0, other.Followers);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -418,8 +446,8 @@ public class BaseCreatureSerializationTests : IDisposable
|
|||
Assert.Equal(0.6, copy.CurrentMoveSpeed); // passive mode, inheriting
|
||||
}
|
||||
|
||||
// ControlMaster and SummonMaster are independent references: a summon master can
|
||||
// exist without Summoned (EnragedCreature), and a controlled summon carries both.
|
||||
// A summon master can exist without Summoned (EnragedCreature), and a controlled summon
|
||||
// reports the same mobile as both.
|
||||
[Theory]
|
||||
[InlineData(true, true, false, false)] // controlled pet
|
||||
[InlineData(true, true, true, true)] // controlled summon, SummonEnd on the wire
|
||||
|
|
@ -452,4 +480,79 @@ public class BaseCreatureSerializationTests : IDisposable
|
|||
Assert.InRange(copy.SummonEndValue, summonEnd - TimeSpan.FromSeconds(1), summonEnd + TimeSpan.FromSeconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
// Before summon ownership followed transfers, a traded summon kept its caster as summon master.
|
||||
[Fact]
|
||||
public void LegacyV22Stream_DifferingMasters_OwnerWins()
|
||||
{
|
||||
var owner = NewMaster();
|
||||
var caster = NewMaster();
|
||||
|
||||
var copy = LoadLegacyV22(true, owner, true, caster, Core.Now + TimeSpan.FromMinutes(5));
|
||||
|
||||
Assert.Equal(owner, copy.ControlMaster);
|
||||
Assert.Equal(owner, copy.SummonMaster);
|
||||
}
|
||||
|
||||
// v23 bit positions: every save-flagged field in schema order (DefaultAI is not flagged).
|
||||
private const int V23Controlled = 13;
|
||||
private const int V23ControlMaster = 14;
|
||||
private const int V23Summoned = 20;
|
||||
private const int V23SummonEnd = 21;
|
||||
private const int V23SummonMaster = 22;
|
||||
private const int V23RemoveStep = 50;
|
||||
private const int V23CorpseNameOverride = 52;
|
||||
|
||||
// v23 wrote the owner and the summoner to separate slots; v24 keeps one master.
|
||||
[Fact]
|
||||
public void V23Stream_MigratesToOneMaster_AndDefaultsAbsentFields()
|
||||
{
|
||||
var owner = NewMaster();
|
||||
var caster = NewMaster();
|
||||
var summonEnd = Core.Now + TimeSpan.FromMinutes(5);
|
||||
|
||||
var donor = new MobileStub();
|
||||
donor.DefaultMobileInit();
|
||||
_created.Add(donor);
|
||||
|
||||
var writer = new BufferWriter(true);
|
||||
donor.Serialize(writer);
|
||||
|
||||
writer.Write(23); // version
|
||||
writer.Write(
|
||||
1UL << V23Controlled | 1UL << V23ControlMaster | 1UL << V23Summoned | 1UL << V23SummonEnd |
|
||||
1UL << V23SummonMaster | 1UL << V23RemoveStep | 1UL << V23CorpseNameOverride
|
||||
);
|
||||
writer.WriteEncodedInt((int)AIType.AI_Melee); // DefaultAI
|
||||
writer.Write(owner); // ControlMaster
|
||||
writer.WriteAnchoredTime(summonEnd); // SummonEnd
|
||||
writer.Write(caster); // SummonMaster
|
||||
writer.WriteEncodedInt(3); // RemoveStep
|
||||
writer.Write("a migrated corpse"); // CorpseNameOverride
|
||||
|
||||
var buffer = new byte[writer.Position];
|
||||
writer.Buffer.AsSpan(0, (int)writer.Position).CopyTo(buffer);
|
||||
|
||||
var copy = Load(buffer);
|
||||
|
||||
Assert.True(copy.Controlled);
|
||||
Assert.True(copy.Summoned);
|
||||
Assert.Equal(owner, copy.ControlMaster);
|
||||
Assert.Equal(owner, copy.SummonMaster);
|
||||
Assert.InRange(copy.SummonEndValue, summonEnd - TimeSpan.FromSeconds(1), summonEnd + TimeSpan.FromSeconds(1));
|
||||
Assert.Equal(3, copy.RemoveStep);
|
||||
Assert.Equal("a migrated corpse", copy.CorpseNameOverride);
|
||||
|
||||
// Absent fields take the class defaults, not default(T).
|
||||
Assert.Equal(AIType.AI_Melee, copy.AI);
|
||||
Assert.Equal(-1, copy.HitsMaxSeed);
|
||||
Assert.Equal(1, copy.ControlSlots);
|
||||
Assert.Equal(BaseCreature.MaxLoyalty, copy.Loyalty);
|
||||
Assert.Equal(100, copy.PhysicalDamage);
|
||||
Assert.Equal(10, copy.RangeHome);
|
||||
Assert.Equal(FightMode.Closest, copy.FightMode);
|
||||
Assert.Equal(0.3, copy.ActiveSpeed);
|
||||
Assert.Equal(0.6, copy.PassiveSpeed);
|
||||
Assert.NotNull(copy.Owners);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue