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.
391 lines
12 KiB
C#
391 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using ModernUO.CodeGeneratedEvents;
|
|
using Server.Logging;
|
|
using Server.Mobiles;
|
|
|
|
namespace Server.Engines.Virtues;
|
|
|
|
public enum VirtueLevel
|
|
{
|
|
None,
|
|
Seeker,
|
|
Follower,
|
|
Knight
|
|
}
|
|
|
|
[Flags]
|
|
public enum VirtueName
|
|
{
|
|
Humility,
|
|
Sacrifice,
|
|
Compassion,
|
|
Spirituality,
|
|
Valor,
|
|
Honor,
|
|
Justice,
|
|
Honesty
|
|
}
|
|
|
|
public class VirtueSystem : GenericPersistence
|
|
{
|
|
private static readonly ILogger logger = LogFactory.GetLogger(typeof(VirtueSystem));
|
|
|
|
private static readonly Dictionary<PlayerMobile, VirtueContext> _playerVirtues = new();
|
|
|
|
private static VirtueSystem _virtueSystemPersistence;
|
|
|
|
public static void Configure()
|
|
{
|
|
_virtueSystemPersistence = new VirtueSystem();
|
|
}
|
|
|
|
public VirtueSystem() : base("Virtues", 10)
|
|
{
|
|
}
|
|
|
|
private static void FixVirtue(Mobile m, int[] virtueValues)
|
|
{
|
|
if (m is not PlayerMobile pm)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var virtues = pm.Virtues;
|
|
for (var i = 0; i < virtueValues.Length; i++)
|
|
{
|
|
var val = virtueValues[i];
|
|
if (val > 0)
|
|
{
|
|
virtues.SetValue(i, val);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Initialize()
|
|
{
|
|
var migrations = Mobile.VirtueMigrations;
|
|
if (migrations?.Count > 0)
|
|
{
|
|
foreach (var (m, values) in migrations)
|
|
{
|
|
FixVirtue(m, values);
|
|
}
|
|
}
|
|
}
|
|
|
|
[OnEvent(nameof(PlayerMobile.PlayerDeletedEvent))]
|
|
public static void OnPlayerDeleted(PlayerMobile pm) => _playerVirtues.Remove(pm);
|
|
|
|
public override void Serialize(IGenericWriter writer)
|
|
{
|
|
writer.WriteEncodedInt(0); // version
|
|
|
|
writer.WriteEncodedInt(_playerVirtues.Count);
|
|
foreach (var (pm, virtues) in _playerVirtues)
|
|
{
|
|
writer.Write(pm);
|
|
virtues.Serialize(writer);
|
|
}
|
|
}
|
|
|
|
public override void Deserialize(IGenericReader reader)
|
|
{
|
|
reader.ReadEncodedInt(); // version
|
|
|
|
var contextCount = reader.ReadEncodedInt();
|
|
for (var i = 0; i < contextCount; i++)
|
|
{
|
|
var player = reader.ReadEntity<PlayerMobile>();
|
|
var virtues = new VirtueContext(player);
|
|
virtues.Deserialize(reader);
|
|
|
|
if (player != null && virtues.IsUsed())
|
|
{
|
|
_playerVirtues.Add(player, virtues);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static VirtueContext GetVirtues(PlayerMobile from) =>
|
|
from != null && _playerVirtues.TryGetValue(from, out var context) ? context : null;
|
|
|
|
public static VirtueContext GetOrCreateVirtues(PlayerMobile from)
|
|
{
|
|
if (from == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
ref var context = ref CollectionsMarshal.GetValueRefOrAddDefault(_playerVirtues, from, out var exists);
|
|
if (!exists)
|
|
{
|
|
context = new VirtueContext(from);
|
|
}
|
|
|
|
return context;
|
|
}
|
|
|
|
public static bool IsHighestPath(PlayerMobile from, VirtueName virtue) =>
|
|
GetVirtues(from)?.GetValue((int)virtue) >= GetMaxAmount(virtue);
|
|
|
|
public static VirtueLevel GetLevel(Mobile from, VirtueName virtue)
|
|
{
|
|
var v = GetVirtues(from as PlayerMobile)?.GetValue((int)virtue) ?? 0;
|
|
int vl;
|
|
|
|
if (v < 4000)
|
|
{
|
|
vl = 0;
|
|
}
|
|
else if (v >= GetMaxAmount(virtue))
|
|
{
|
|
vl = 3;
|
|
}
|
|
else
|
|
{
|
|
vl = (v + 9999) / 10000;
|
|
}
|
|
|
|
return (VirtueLevel)vl;
|
|
}
|
|
|
|
public static string GetName(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Humility => "Humility",
|
|
VirtueName.Sacrifice => "Sacrifice",
|
|
VirtueName.Compassion => "Compassion",
|
|
VirtueName.Spirituality => "Spirituality",
|
|
VirtueName.Valor => "Valor",
|
|
VirtueName.Honor => "Honor",
|
|
VirtueName.Justice => "Justice",
|
|
VirtueName.Honesty => "Honesty",
|
|
_ => ""
|
|
};
|
|
|
|
public static string GetLowerCaseName(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Humility => "humility",
|
|
VirtueName.Sacrifice => "sacrifice",
|
|
VirtueName.Compassion => "compassion",
|
|
VirtueName.Spirituality => "spirituality",
|
|
VirtueName.Valor => "valor",
|
|
VirtueName.Honor => "honor",
|
|
VirtueName.Justice => "justice",
|
|
VirtueName.Honesty => "honesty",
|
|
_ => ""
|
|
};
|
|
|
|
public static int GetMaxAmount(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Honor => 20000,
|
|
VirtueName.Sacrifice => 22000,
|
|
_ => 21000
|
|
};
|
|
|
|
public static int GetGainedLocalizedMessage(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Sacrifice => 1054160, // You have gained in sacrifice.
|
|
VirtueName.Compassion => 1053002, // You have gained in compassion.
|
|
VirtueName.Spirituality => 1155832, // You have gained in Spirituality.
|
|
VirtueName.Valor => 1054030, // You have gained in Valor!
|
|
VirtueName.Honor => 1063225, // You have gained in Honor.
|
|
VirtueName.Justice => 1049363, // You have gained in Justice.
|
|
VirtueName.Humility => 1052070, // You have gained in Humility.
|
|
_ => 0
|
|
};
|
|
|
|
public static int GetGainedAPathLocalizedMessage(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Sacrifice => 1052008, // You have gained a path in Sacrifice!
|
|
VirtueName.Spirituality => 1155833, // "You have gained a path in Spirituality!" (Why are there quotes?)
|
|
VirtueName.Valor => 1054032, // You have gained a path in Valor!
|
|
VirtueName.Honor => 1063226, // You have gained a path in Honor!
|
|
VirtueName.Justice => 1049367, // You have gained a path in Justice!
|
|
VirtueName.Humility => 1155811, // You have gained a path in Humility!
|
|
_ => 0
|
|
};
|
|
|
|
public static int GetHightestPathLocalizedMessage(VirtueName virtue) =>
|
|
virtue switch
|
|
{
|
|
VirtueName.Compassion => 1053003, // You have achieved the highest path of compassion and can no longer gain any further.
|
|
VirtueName.Spirituality => 1155831, // You cannot gain more Spirituality.
|
|
VirtueName.Valor => 1054031, // You have achieved the highest path in Valor and can no longer gain any further.
|
|
VirtueName.Honor => 1063228, // You cannot gain more Honor.
|
|
VirtueName.Justice => 1049534, // You cannot gain more Justice.
|
|
VirtueName.Humility => 1155808, // You cannot gain more Humility.
|
|
VirtueName.Honesty => 1153771, // You have achieved the highest path in Honesty and can no longer gain any further.
|
|
_ => 1052050, // You have achieved the highest path in this virtue.
|
|
};
|
|
|
|
public static bool Award(PlayerMobile from, VirtueName virtue, int amount, ref bool gainedPath)
|
|
{
|
|
var virtues = from.Virtues;
|
|
|
|
var current = virtues.GetValue((int)virtue);
|
|
|
|
var maxAmount = GetMaxAmount(virtue);
|
|
|
|
if (current >= maxAmount)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (current + amount >= maxAmount)
|
|
{
|
|
amount = maxAmount - current;
|
|
}
|
|
|
|
var oldLevel = GetLevel(from, virtue);
|
|
|
|
virtues.SetValue((int)virtue, current + amount);
|
|
|
|
gainedPath = GetLevel(from, virtue) != oldLevel;
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool Atrophy(PlayerMobile from, VirtueName virtue, int amount = 1)
|
|
{
|
|
var virtues = GetVirtues(from);
|
|
if (virtues == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var current = virtues.GetValue((int)virtue);
|
|
|
|
if (current - amount >= 0)
|
|
{
|
|
virtues.SetValue((int)virtue, current - amount);
|
|
}
|
|
else
|
|
{
|
|
virtues.SetValue((int)virtue, 0);
|
|
}
|
|
|
|
return current > 0;
|
|
}
|
|
|
|
public static bool IsSeeker(PlayerMobile from, VirtueName virtue) => GetLevel(from, virtue) >= VirtueLevel.Seeker;
|
|
|
|
public static bool IsFollower(PlayerMobile from, VirtueName virtue) => GetLevel(from, virtue) >= VirtueLevel.Follower;
|
|
|
|
public static bool IsKnight(PlayerMobile from, VirtueName virtue) => GetLevel(from, virtue) >= VirtueLevel.Knight;
|
|
|
|
public static void AwardVirtue(PlayerMobile pm, VirtueName virtue, int amount)
|
|
{
|
|
var virtues = GetOrCreateVirtues(pm);
|
|
if (virtue == VirtueName.Compassion)
|
|
{
|
|
if (virtues.CompassionGains > 0 && Core.Now > virtues.NextCompassionDay)
|
|
{
|
|
virtues.NextCompassionDay = DateTime.MinValue;
|
|
virtues.CompassionGains = 0;
|
|
}
|
|
|
|
if (virtues.CompassionGains >= 5)
|
|
{
|
|
pm.SendLocalizedMessage(1053004); // You must wait about a day before you can gain in compassion again.
|
|
return;
|
|
}
|
|
}
|
|
|
|
var gainedPath = false;
|
|
var virtueName = GetName(virtue);
|
|
|
|
if (Award(pm, virtue, amount, ref gainedPath))
|
|
{
|
|
if (gainedPath)
|
|
{
|
|
var gainedPathMessage = GetGainedAPathLocalizedMessage(virtue);
|
|
if (gainedPathMessage != 0)
|
|
{
|
|
pm.SendLocalizedMessage(gainedPathMessage);
|
|
}
|
|
else
|
|
{
|
|
pm.SendMessage($"You have gained a path in {virtueName}!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var gainMessage = GetGainedLocalizedMessage(virtue);
|
|
if (gainMessage != 0)
|
|
{
|
|
pm.SendLocalizedMessage(gainMessage);
|
|
}
|
|
else
|
|
{
|
|
pm.SendMessage($"You have gained in {virtueName}.");
|
|
}
|
|
}
|
|
|
|
if (virtue == VirtueName.Compassion)
|
|
{
|
|
virtues.NextCompassionDay = Core.Now + TimeSpan.FromDays(1.0);
|
|
|
|
if (++virtues.CompassionGains >= 5)
|
|
{
|
|
// You must wait about a day before you can gain in compassion again.
|
|
pm.SendLocalizedMessage(1053004);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
pm.SendLocalizedMessage(GetHightestPathLocalizedMessage(virtue));
|
|
}
|
|
}
|
|
|
|
public static void CheckAtrophies(PlayerMobile pm)
|
|
{
|
|
SacrificeVirtue.CheckAtrophy(pm);
|
|
JusticeVirtue.CheckAtrophy(pm);
|
|
CompassionVirtue.CheckAtrophy(pm);
|
|
ValorVirtue.CheckAtrophy(pm);
|
|
}
|
|
|
|
private class VirtueTimer : Timer
|
|
{
|
|
public VirtueTimer() : base(TimeSpan.FromMinutes(5.0), TimeSpan.FromMinutes(5.0))
|
|
{
|
|
}
|
|
|
|
public static void Initialize()
|
|
{
|
|
new VirtueTimer().Start();
|
|
}
|
|
|
|
protected override void OnTick()
|
|
{
|
|
if (_playerVirtues.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// This is not particularly efficient. If it gets too slow, then use a different architecture.
|
|
foreach (var (player, virtues) in _playerVirtues)
|
|
{
|
|
CheckAtrophies(player);
|
|
|
|
if (!virtues.IsUsed())
|
|
{
|
|
_playerVirtues.Remove(player);
|
|
}
|
|
}
|
|
}
|
|
|
|
~VirtueTimer()
|
|
{
|
|
VirtueSystem.logger.Error($"{nameof(VirtueTimer)} is no longer running!");
|
|
}
|
|
}
|
|
}
|