ModernUO/Projects/UOContent/Spells/Fifth/PoisonField.cs
Kamron Batman 2935eafe24
feat: convert all delta-time serialization to anchored time (#2589)
## Summary

Phase 3 of the anchored-time work: **every actively-written delta-time value in the engine now stores an anchored timestamp** — absolute on the wire, shifted forward by the downtime at load. Remaining time survives restarts (as delta did), and unlike delta, the bytes do not change on every save, so an idle world serializes identically save after save.

The answer to "is it possible everywhere": **yes** — including the one case that looked impossible.

## The GenericPersistence problem, solved

`GenericPersistence` bins (`Virtues.bin`, `StealableArtifacts.bin`, …) are raw payloads with no idx header, so they have no anchor of their own — anchored reads there would silently apply zero shift. But the anchor is a property of the **save**, not the file: every file in one save shares one `World.SaveStartTime`, and `Persistence.Load` reads **all** entity indexes (phase 1) before **any** persistence payload (phase 2). So the idx v5 header stamps a save-wide `World.LoadTimeShift`, and generic persistence readers inherit it. No file-format change, no per-bin header, old bins unaffected.

## Converted

- **Item v10 → v11**: `LastMoved` — previously whole-minute delta, rewritten every save for every item, the single largest source of idle-save churn — and `DecayResetTime` (retiring the TODO from #2583). **Mobile v37 → v38**: the three stat-gain stamps. **BaseCreature v20 → v21**: `SummonEnd`.
- **17 code-generated classes** (`[DeltaDateTime]` → `[AnchoredDateTime]`, version bump + `MigrateFrom` each): the five field spells, TransientItem, VirtueContext (×7 fields), PuzzleChestSolutionAndTime, BaseCamp, BaseBoat, RentedVendor, PlayerVendor, Ethics Player, Sheep, StarRoomGate, ChampionSpawn (×3), Corpse (`TimeOfDeath`, v19). The `MigrateFrom` bodies were generated from each class's current migration schema and are compiler-verified; VirtueContext's save-flagged nullables fall back to the same defaults the old deserialize left in place. Corpse's six migrations moved to a new `Corpse.Migrations.cs`.
- **Hand-written sites**: StealableArtifacts (v2), VendorInventory (v1), ML quest objectives (persistence v3) — each gated on its own version.

**Not converted, deliberately**: the ~25 read-only `ReadDeltaTime` sites in legacy version fallbacks and migration replays — they decode existing old bytes and must never change. `[DeltaDateTime]`/`WriteDeltaTime` remain available for them.

## Verification

- Build 0 errors / 0 warnings; **837 + 708 tests green**.
- Schema regeneration produced exactly the 17 expected new `vN.json` files (all `AnchoredTime` rule args), nothing else touched.
- **New acceptance tests** pin the point of the whole effort: serializing the same item at two save times **5 hours apart produces byte-identical output**, and `LastMoved`/`DecayResetTime` round-trip **exactly** at sub-minute precision (the old minutes encoding destroyed both properties).

## Notes for review

- `LastMoved` grows from a 1–3 byte encoded minutes value to 8-byte ticks per item — the price of byte-stability; it repays itself in incremental-save behavior since unchanged items now produce unchanged bytes.
- BaseEscortable-style semantics are unchanged: anchored shift preserves *remaining* time exactly, the same contract delta provided, so no gameplay-visible behavior changes — deadlines simply stop being consumed by downtime that delta already protected against, now with stable bytes.

## Enforcement

`WriteDeltaTime` is now `[Obsolete]` (interface + implementation). With the repo's warnings-as-errors, any new delta-time write — hand-written or emitted by a still-unconverted `[DeltaDateTime]` field — fails the build, with the migration instructions in the message. That the full solution still builds with **zero warnings** is itself the proof no active delta writer survived the conversion. `ReadDeltaTime` deliberately stays un-attributed: its remaining callers decode existing old bytes and are correct forever; its XML docs now state the legacy-decode-only contract.
2026-08-22 19:34:43 -07:00

257 lines
6.8 KiB
C#

using System;
using ModernUO.Serialization;
using Server.Collections;
using Server.Items;
using Server.Misc;
using Server.Mobiles;
namespace Server.Spells.Fifth;
public class PoisonFieldSpell : MagerySpell, ITargetingSpell<IPoint3D>
{
private static readonly SpellInfo _info = new(
"Poison Field",
"In Nox Grav",
230,
9052,
false,
Reagent.BlackPearl,
Reagent.Nightshade,
Reagent.SpidersSilk
);
public PoisonFieldSpell(Mobile caster, Item scroll = null) : base(caster, scroll, _info)
{
}
public override SpellCircle Circle => SpellCircle.Fifth;
public int TargetRange => Core.T2A ? 15 : 18;
public void Target(IPoint3D p)
{
if (SpellHelper.CheckTown(p, Caster) && CheckSequence())
{
SpellHelper.Turn(Caster, p);
SpellHelper.GetSurfaceTop(ref p);
var loc = new Point3D(p);
var eastToWest = SpellHelper.GetEastToWest(Caster.Location, loc);
Effects.PlaySound(loc, Caster.Map, 0x20B);
var itemID = eastToWest ? 0x3915 : 0x3922;
var duration = Core.Expansion switch
{
Expansion.None => TimeSpan.FromSeconds(20),
< Expansion.LBR => TimeSpan.FromSeconds(15 + Caster.Skills.Magery.Value * 0.4),
_ => TimeSpan.FromSeconds(3 + Caster.Skills.Magery.Value * 0.4)
};
for (var i = -2; i <= 2; ++i)
{
var targetLoc = new Point3D(eastToWest ? loc.X + i : loc.X, eastToWest ? loc.Y : loc.Y + i, loc.Z);
new PoisonField(itemID, targetLoc, Caster, Caster.Map, duration, i);
}
}
}
public override void OnCast()
{
Caster.Target = new SpellTarget<IPoint3D>(this, allowGround: true);
}
}
[DispellableField]
[SerializationGenerator(1, false)]
public partial class PoisonField : Item
{
private void MigrateFrom(V0Content content)
{
_caster = content.Caster;
_end = content.End;
}
[SerializableField(0)]
private Mobile _caster;
[AnchoredDateTime]
[SerializableField(1)]
private DateTime _end;
private Timer _timer;
public PoisonField(int itemID, Point3D loc, Mobile caster, Map map, TimeSpan duration, int val) : base(itemID)
{
var canFit = SpellHelper.AdjustField(ref loc, map, 12, false);
Visible = false;
Movable = false;
Light = LightType.Circle300;
MoveToWorld(loc, map);
_caster = caster;
_end = Core.Now + duration;
_timer = new InternalTimer(this, TimeSpan.FromSeconds(val.Abs() * 0.2), caster.InLOS(this), canFit);
_timer.Start();
}
public override bool BlocksFit => true;
public override void OnAfterDelete()
{
base.OnAfterDelete();
_timer?.Stop();
}
[AfterDeserialization]
private void AfterDeserialization()
{
_timer = new InternalTimer(this, TimeSpan.Zero, true, true);
_timer.Start();
}
public void ApplyPoisonTo(Mobile m)
{
if (_caster == null)
{
return;
}
int level;
if (Core.AOS)
{
level = (_caster.Skills.Magery.Value + _caster.Skills.Poisoning.Value) switch
{
> 199.8 => 3,
> 170.2 => 2,
> 130.2 => 1,
_ => 0
};
}
else
{
level = 1;
}
if (m.ApplyPoison(_caster, Poison.GetPoison(level)) is ApplyPoisonResult.Poisoned or ApplyPoisonResult.HigherPoisonActive)
{
if (SpellHelper.CanRevealCaster(m))
{
_caster.RevealingAction();
}
}
(m as BaseCreature)?.OnHarmfulSpell(_caster);
}
public override bool OnMoveOver(Mobile m)
{
if (Visible && _caster != null && (!Core.AOS || m != _caster) &&
SpellHelper.ValidIndirectTarget(_caster, m) && _caster.CanBeHarmful(m, false))
{
_caster.DoHarmful(m);
ApplyPoisonTo(m);
m.PlaySound(0x474);
}
return true;
}
private class InternalTimer : Timer
{
private readonly bool m_CanFit;
private readonly bool m_InLOS;
private readonly PoisonField m_Item;
public InternalTimer(PoisonField item, TimeSpan delay, bool inLOS, bool canFit) : base(
delay,
TimeSpan.FromSeconds(1.5)
)
{
m_Item = item;
m_InLOS = inLOS;
m_CanFit = canFit;
}
protected override void OnTick()
{
if (m_Item.Deleted)
{
return;
}
if (!m_Item.Visible)
{
if (m_InLOS && m_CanFit)
{
m_Item.Visible = true;
}
else
{
m_Item.Delete();
}
if (!m_Item.Deleted)
{
m_Item.ProcessDelta();
Effects.SendLocationParticles(
EffectItem.Create(m_Item.Location, m_Item.Map, EffectItem.DefaultDuration),
0x376A,
9,
10,
5040
);
}
}
else if (Core.Now > m_Item._end)
{
m_Item.Delete();
Stop();
}
else
{
var map = m_Item.Map;
var caster = m_Item._caster;
if (map != null && caster != null)
{
var eastToWest = m_Item.ItemID == 0x3915;
var bounds = new Rectangle2D(
m_Item.X - (eastToWest ? 0 : 1),
m_Item.Y - (eastToWest ? 1 : 0),
eastToWest ? 1 : 2,
eastToWest ? 2 : 1
);
using var queue = PooledRefQueue<Mobile>.Create();
foreach (var m in map.GetMobilesInBounds(bounds))
{
if (m.Z + 16 > m_Item.Z && m_Item.Z + 12 > m.Z && (!Core.AOS || m != caster) &&
SpellHelper.ValidIndirectTarget(caster, m) && caster.CanBeHarmful(m, false))
{
queue.Enqueue(m);
}
}
while (queue.Count > 0)
{
var m = queue.Dequeue();
caster.DoHarmful(m);
m_Item.ApplyPoisonTo(m);
m.PlaySound(0x474);
}
}
}
}
}
}