ModernUO/Projects/UOContent/Spells/Spellweaving/GiftOfLife.cs
Kamron Batman 392c4e16d5
refactor: Changes BaseCreature to the SerializationGenerator (delta save requirement) (#2611)
The pure SerializationGenerator conversion of `BaseCreature`, split out of #2592 so it can serve as the reference for converting every other large hand-written class in the Delta Saves project (#7, phase 3). Two behaviour changes from #2592 are deliberately not here and follow in their own PRs on top of this one: `SpeedClass`, and the collapse of `ControlMaster`/`SummonMaster` into one reference (a creature can lose or keep either independently: Blade Spirits and Energy Vortexes are summoned but never controlled, EnragedCreature and talisman summons keep a summon master with neither flag set).

## What this is

- `BaseCreature` becomes `[SerializationGenerator(23, false)]` with a `[SerializableField]` per serialized slot and `[SaveFlag]` elision on nearly every field, so a stock creature serializes to its version plus flags. Field orders run 0..53 with no gaps (54 fields).
- The hand-written reader stays as `private void Deserialize(IGenericReader reader, int version)` for every pre-codegen version (0..22); post-codegen bumps use `MigrateFrom` from here on. `[AfterDeserialization]` carries the post-load fixups main did after reading (stat timers, AI type, followers, reacquire seeding).
- `ControlMaster` and `SummonMaster` stay two independent fields with main's semantics, each elided when null.
- `DamageMin`/`DamageMax`/`ActiveSpeed`/`PassiveSpeed` are no longer virtual (nothing in the tree overrode them); comments swept to the constraints that matter.
- Direct writes to serialized backing fields outside the generated setters (`SetDamage`, `SetResistance`, the move-speed helpers, the loot flag, feed loyalty, the delete timer) call `this.MarkDirty()`, matching the #2609 standard, so the class is ready for delta saves once `Mobile` is audited.
- Schema `Server.Mobiles.BaseCreature.v23.json` regenerated by the tool (a second run produces no diff).

## Deferred to follow-up PRs

SpeedClass: the serialized `_speedClass` field, `DefaultSpeedClass` replacing the type constant, `ApplySpeedClass`/`OnSpeedClassChange`, "None means custom", the four-speeds-as-one-block elision, `NPCSpeeds.FindEntry(SpeedLevel)`, the constructor fallback to Medium, and their tests.

Master references: serializing one `Master` with a `Controlled`/`Summoned` fan-out and the `SetControlMaster` lockstep.

## Tests

UOContent.Tests 776 / Server.Tests 855 green. `BaseCreatureSerializationTests` covers: a default creature elides to version + flags; a populated creature round-trips with exact byte consumption; back-to-back saves are byte-identical; an uncontrolled summon keeps its SummonMaster; byte-authentic v22 legacy streams (replicas of main's `Serialize`) load through the legacy reader for a wild tamable, a controlled pet, a controlled summon with an anchored `SummonEnd`, and a summon-master-only creature (the EnragedCreature shape); a running delete timer round-trips through `[DeserializeTimer]`; `Friends`, `CurrentWayPoint` and `HomeMap` round-trip; a `BaseVendor` stub round-trips the generated BaseVendor v2 → generated BaseCreature v23 chain.

## Behaviour notes for reviewers

- `ActiveMoveSpeed`/`PassiveMoveSpeed` getters return the raw override (0 = inherit); `CurrentMoveSpeed` is the resolved pace.
- Speeds elided as table defaults re-snap to the current `npc-speeds.json` on load, so table edits reach unmodified spawns on restart.
- `GetSpeeds` no longer throws on the save/load path when the table has no entry for the type: saves elide against the creature's own values and loads keep the stream. Construction still throws (`InvalidOperationException`, was `KeyNotFoundException`). An elided load with no table entry would otherwise resume at speed 0, so `[AfterDeserialization]` logs once and paces it at Medium.
- `virtual` removed from `ActiveSpeed`, `PassiveSpeed`, `DamageMin`, `DamageMax` (no overrides in the tree; forks may have some).
- `ControlMaster`, `SummonMaster`, `ControlOrder`, `Tamable`, `IsParagon` are `[SerializableProperty]` over hand-written setters because follower bookkeeping must run before the assignment, which a `fieldChanged` hook cannot express; the wire format is identical.

## Prerequisites for cherry-picking
#2609 (BaseVendor is already generated on top of BaseCreature) and SerializationGenerator 4.1.0.
2026-09-06 14:11:51 -07:00

162 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Engines.BuffIcons;
using Server.Gumps;
using Server.Mobiles;
using Server.Targeting;
namespace Server.Spells.Spellweaving
{
public class GiftOfLifeSpell : ArcanistSpell, ITargetingSpell<Mobile>
{
private static readonly SpellInfo _info = new(
"Gift of Life",
"Illorae",
-1
);
private static readonly Dictionary<Mobile, ExpireTimer> _table = new();
public GiftOfLifeSpell(Mobile caster, Item scroll = null) : base(caster, scroll, _info)
{
}
public override TimeSpan CastDelayBase => TimeSpan.FromSeconds(4.0);
public override double RequiredSkill => 38.0;
public override int RequiredMana => 70;
public double HitsScalar => (Caster.Skills.Spellweaving.Value / 2.4 + FocusLevel) / 100;
public void Target(Mobile m)
{
if (m.IsDeadBondedPet || !m.Alive)
{
// As per Osi: Nothing happens.
}
else if (m != Caster && !(m is BaseCreature { IsBonded: true } bc && bc.ControlMaster == Caster))
{
Caster.SendLocalizedMessage(1072077); // You may only cast this spell on yourself or a bonded pet.
}
else if (_table.ContainsKey(m))
{
Caster.SendLocalizedMessage(501775); // This spell is already in effect.
}
else if (CheckBSequence(m))
{
if (Caster == m)
{
Caster.SendLocalizedMessage(1074774); // You weave powerful magic, protecting yourself from death.
}
else
{
Caster.SendLocalizedMessage(1074775); // You weave powerful magic, protecting your pet from death.
SpellHelper.Turn(Caster, m);
}
m.PlaySound(0x244);
m.FixedParticles(0x3709, 1, 30, 0x26ED, 5, 2, EffectLayer.Waist);
m.FixedParticles(0x376A, 1, 30, 0x251E, 5, 3, EffectLayer.Waist);
var skill = Caster.Skills.Spellweaving.Value;
var duration = TimeSpan.FromMinutes((int)(skill / 24) * 2 + FocusLevel);
var t = new ExpireTimer(m, duration, this);
t.Start();
_table[m] = t;
(m as PlayerMobile)?.AddBuff(
new BuffInfo(BuffIcon.GiftOfLife, 1031615, 1075807, duration, retainThroughDeath: true)
);
}
}
public override void OnCast()
{
Caster.Target = new SpellTarget<Mobile>(this, TargetFlags.Beneficial);
}
[OnEvent(nameof(CreatureEvents.CreatureDeathEvent))]
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnDeathEvent(Mobile m)
{
if (_table.ContainsKey(m))
{
Timer.StartTimer(TimeSpan.FromSeconds(Utility.RandomMinMax(2, 4)), () => HandleDeath_OnCallback(m));
}
}
private static void HandleDeath_OnCallback(Mobile m)
{
if (!_table.TryGetValue(m, out var timer))
{
return;
}
var hitsScalar = timer.Spell.HitsScalar;
if (m is BaseCreature pet && pet.IsDeadBondedPet)
{
var master = pet.GetMaster();
if (master?.NetState != null && Utility.InUpdateRange(pet.Location, master.Location))
{
master.SendGump(new PetResurrectGump(master, pet, hitsScalar));
}
else
{
var friends = pet.Friends;
for (var i = 0; i < friends?.Count; i++)
{
var friend = friends[i];
if (friend.NetState != null && Utility.InUpdateRange(pet.Location, friend.Location))
{
friend.SendGump(new PetResurrectGump(friend, pet));
break;
}
}
}
}
else
{
m.SendGump(new ResurrectGump(m, hitsScalar));
}
// Per OSI, buff is removed when gump sent, irregardless of online status or acceptance
timer.DoExpire();
}
private class ExpireTimer : Timer
{
private Mobile _mobile;
public ExpireTimer(Mobile m, TimeSpan delay, GiftOfLifeSpell spell) : base(delay)
{
_mobile = m;
Spell = spell;
}
public GiftOfLifeSpell Spell { get; }
protected override void OnTick()
{
DoExpire();
}
public void DoExpire()
{
Stop();
_mobile.SendLocalizedMessage(1074776); // You are no longer protected with Gift of Life.
_table.Remove(_mobile);
(_mobile as PlayerMobile)?.RemoveBuff(BuffIcon.GiftOfLife);
}
}
}
}