ModernUO/Projects/UOContent/Engines/ML Quests/Gumps/RaceChangeGump.cs
Kamron Batman b90ac0d481
perf: Migrate Quest gumps to DynamicGump (#2416)
## Summary

Migrates the Quest system gumps from legacy `Gump` to `DynamicGump` / `StaticGump<T>`.

**Renames** `Engines/ML Quests/Gumps/BaseQuestGump` to **`BaseMLQuestGump`** to
disambiguate from the Core quest abstract (`Engines/Quests/Core/QuestSystem.cs`).
Both abstracts now extend `DynamicGump`.

**Abstract bases**:
- `Server.Engines.Quests.BaseQuestGump` (Core) - now `abstract DynamicGump`. Holds constants and a static `AddHtmlObject(ref DynamicGumpBuilder, ...)` helper. Concrete subclasses each provide their own `BuildLayout`.
- `Server.Engines.MLQuests.Gumps.BaseMLQuestGump` (ML, renamed) - now `abstract DynamicGump` with a `protected abstract BuildContent(ref DynamicGumpBuilder)` hook. The base's `BuildLayout` draws shared chrome (background art, frame, label header) and then invokes `BuildContent`; subclasses use `BuildPage`, `SetTitle`, `RegisterButton`, `SetPageCount`, and content helpers (`AddDescription`, `AddObjectives`, `AddObjectivesProgress`, `AddRewardsPage`, `AddRewards`, `AddConversation`).

**Concrete Core gumps** migrated to `DynamicGump`:
- `QuestCancelGump`, `QuestOfferGump` (Core), `QuestObjectivesGump`, `QuestConversationsGump`, `QuestLogUpdatedGump`, `QuestItemInfoGump`, `SheetMusicOfferGump` (Impresario), `PaintedImageGump` (renamed from `PaintedImage.InternalGump`).

**Concrete ML gumps** migrated to `DynamicGump` (extending `BaseMLQuestGump` or directly):
- `InfoNPCGump`, `QuestConversationGump`, `QuestLogDetailedGump`, `QuestLogGump`, `QuestOfferGump` (ML), `QuestReportBackGump`, `QuestRewardGump`, `QuestCancelConfirmGump`, `RaceChangeConfirmGump`.

**`StaticGump<T>` migrations**:
- `ScrollOfAbraxusGump` (Dark Tides). Its layout is a single hard-coded cliloc (1060116) with no per-instance dynamic content - safe to cache.

**Cliloc rule**: Every other quest dialog bakes per-instance cliloc IDs into its layout (quest titles, NPC names, race-specific prompts, era-conditional progress messages, escort destinations). Per the cliloc rule, baking different cliloc numbers into a cached layout would defeat `StaticGump<T>` caching - so all of these are `DynamicGump`.

**Signature changes** to support the migration:
- `QuestObjective.RenderMessage`/`RenderProgress` now take `ref DynamicGumpBuilder builder` (15 overrides updated across Collector, Solen Matriarch, Ambitious Solen Queen, Study of the Solen Hive, Terrible Hatchlings, The Summoning, Uzeraan Turmoil, Witch Apprentice, Emino's Undertaking).
- ML `BaseObjective.WriteToGump` / `BaseObjectiveInstance.WriteToGump` / `BaseReward.WriteToGump` now take `ref DynamicGumpBuilder` (KillObjective, GainSkillObjective, EscortObjective, CollectObjective, DeliverObjective, BaseReward).

**Empty-gump and `Singleton` rules**: All concrete gumps now have private constructors with static `DisplayTo` entry points that null-check the player NetState before allocation. All gumps that shouldn't stack are `Singleton => true`.

**External callers updated**: `MLQuest.SendOffer`/`OnRefuse`, `MLQuestEntry.SendProgressGump`/`SendRewardGump`/`SendReportBackGump`, `MLQuestSystem.QuestGumpRequest` and `ViewQuestsCommand`, `BoonCollector` (Darius/Nedrick), `SirHelper.OnDoubleClick` (no longer caches a single shared `InfoNPCGump` instance - `DisplayTo` constructs one per click and the gump's `Singleton => true` handles deduplication), `RaceChangeDeed.OnDoubleClick`, `PaintedImage.OnDoubleClick`, `ScrollOfAbraxus.OnDoubleClick`, `Impresario.OnTalk`, and `PlayerMobile`'s `BaseQuestGump` alias is now `BaseMLQuestGump`.

**Concrete subclasses found beyond the listed entry points**: `SheetMusicOfferGump` (in Impresario.cs), `PaintedImageGump` (was `PaintedImage.InternalGump`), `QuestObjectivesGump`, `QuestConversationsGump`, `QuestLogUpdatedGump`, `QuestItemInfoGump` (the Core base has these embedded across `QuestSystem.cs`/`QuestObjective.cs`/`QuestConversation.cs`/`QuestItemInfo.cs`).

**Code-standards cleanup**: Renamed legacy `m_X` private fields to `_x` in rewritten files; braces on all control flow.
2026-04-25 20:23:13 -07:00

348 lines
10 KiB
C#

using System;
using System.Buffers;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
using Server.Network;
using Server.Spells.Fifth;
using Server.Spells.Ninjitsu;
using Server.Spells.Seventh;
namespace Server.Engines.MLQuests.Gumps
{
public interface IRaceChanger
{
bool CheckComplete(PlayerMobile from);
void ConsumeNeeded(PlayerMobile from);
void OnCancel(PlayerMobile from);
}
public class RaceChangeConfirmGump : DynamicGump
{
private static Dictionary<NetState, RaceChangeState> m_Pending;
private readonly IRaceChanger _owner;
private readonly Race _race;
public override bool Singleton => true;
private RaceChangeConfirmGump(IRaceChanger owner, Race targetRace) : base(50, 50)
{
_owner = owner;
_race = targetRace;
}
public static void DisplayTo(PlayerMobile from, IRaceChanger owner, Race targetRace)
{
if (from?.NetState == null || targetRace == null)
{
return;
}
from.SendGump(new RaceChangeConfirmGump(owner, targetRace));
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.AddPage();
builder.AddBackground(0, 0, 240, 135, 0x2422);
if (_race == Race.Human)
{
builder.AddHtmlLocalized(15, 15, 210, 75, 1073643, 0); // Are you sure you wish to embrace your humanity?
}
else if (_race == Race.Elf)
{
builder.AddHtmlLocalized(15, 15, 210, 75, 1073642, 0); // Are you sure you want to follow the elven ways?
}
else
{
builder.AddHtml(15, 15, 210, 75, $"Are you sure you want to change your race to {_race.Name}?");
}
builder.AddButton(160, 95, 0xF7, 0xF8, 1);
builder.AddButton(90, 95, 0xF2, 0xF1, 0);
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
var from = sender.Mobile;
switch (info.ButtonID)
{
case 0: // Cancel
{
_owner?.OnCancel((PlayerMobile)from);
break;
}
case 1: // Okay
{
if (_owner?.CheckComplete((PlayerMobile)from) != false)
{
Offer(_owner, (PlayerMobile)from, _race);
}
break;
}
}
}
public static unsafe void Initialize()
{
m_Pending = new Dictionary<NetState, RaceChangeState>();
IncomingExtendedCommandPackets.RegisterExtended(0x2A, true, &RaceChangeReply);
}
public static bool IsPending(NetState state) => state != null && m_Pending.ContainsKey(state);
private static void Offer(IRaceChanger owner, PlayerMobile from, Race targetRace)
{
var ns = from.NetState;
if (ns == null || !CanChange(from, targetRace))
{
return;
}
CloseCurrent(ns);
m_Pending[ns] = new RaceChangeState(owner, ns, targetRace);
ns.SendRaceChanger(from.Female, targetRace);
}
private static void CloseCurrent(NetState ns)
{
if (m_Pending.TryGetValue(ns, out var state))
{
state._timeoutToken.Cancel();
m_Pending.Remove(ns);
}
ns.SendCloseRaceChanger();
}
private static void Timeout(NetState ns)
{
if (IsPending(ns))
{
m_Pending.Remove(ns);
ns.SendCloseRaceChanger();
}
}
public static bool IsWearingEquipment(Mobile from)
{
foreach (var item in from.Items)
{
switch (item.Layer)
{
case Layer.Hair:
case Layer.FacialHair:
case Layer.Backpack:
case Layer.Mount:
case Layer.Bank:
{
continue; // ignore
}
default:
{
return true;
}
}
}
return false;
}
private static bool CanChange(PlayerMobile from, Race targetRace)
{
if (from.Deleted)
{
return false;
}
if (from.Race == targetRace)
{
from.SendLocalizedMessage(1111918); // You are already that race.
}
else if (!MondainsLegacy.CheckML(from, false))
{
from.SendLocalizedMessage(1073651); // You must have Mondain's Legacy before proceeding...
}
else if (!from.Alive)
{
from.SendLocalizedMessage(1073646); // Only the living may proceed...
}
else if (from.Mounted)
{
from.SendLocalizedMessage(1073647); // You may not continue while mounted...
}
// TODO: Does this cover everything?
else if (!from.CanBeginAction<PolymorphSpell>() || DisguisePersistence.IsDisguised(from) ||
AnimalForm.UnderTransformation(from) || !from.CanBeginAction<IncognitoSpell>() ||
from.IsBodyMod)
{
from.SendLocalizedMessage(1073648); // You may only proceed while in your original state...
}
else if (from.Spell?.IsCasting == true)
{
from.SendLocalizedMessage(1073649); // One may not proceed while embracing magic...
}
else if (from.Poisoned)
{
from.SendLocalizedMessage(1073652); // You must be healthy to proceed...
}
else if (IsWearingEquipment(from))
{
from.SendLocalizedMessage(1073650); // To proceed you must be unburdened by equipment...
}
else
{
return true;
}
return false;
}
private static void RaceChangeReply(NetState state, SpanReader reader)
{
if (!m_Pending.TryGetValue(state, out var raceChangeState))
{
return;
}
CloseCurrent(state);
if (state.Mobile is not PlayerMobile pm)
{
return;
}
var owner = raceChangeState.m_Owner;
var targetRace = raceChangeState.m_TargetRace;
if (reader.Length == 5)
{
owner?.OnCancel(pm);
return;
}
if (!CanChange(pm, targetRace) || owner?.CheckComplete(pm) == false)
{
return;
}
int hue = reader.ReadUInt16();
int hairItemId = reader.ReadUInt16();
int hairHue = reader.ReadUInt16();
int facialHairItemId = reader.ReadUInt16();
int facialHairHue = reader.ReadUInt16();
pm.Race = targetRace;
pm.Hue = targetRace.ClipSkinHue(hue) | 0x8000;
if (targetRace.ValidateHair(pm, hairItemId))
{
pm.HairItemID = hairItemId;
pm.HairHue = targetRace.ClipHairHue(hairHue);
}
else
{
pm.HairItemID = 0;
}
if (targetRace.ValidateFacialHair(pm, facialHairItemId))
{
pm.FacialHairItemID = facialHairItemId;
pm.FacialHairHue = targetRace.ClipHairHue(facialHairHue);
}
else
{
pm.FacialHairItemID = 0;
}
if (targetRace == Race.Human)
{
pm.SendLocalizedMessage(1073654); // You are now fully human.
}
else if (targetRace == Race.Elf)
{
pm.SendLocalizedMessage(1073653); // You are now fully initiated into the Elven culture.
}
else
{
pm.SendMessage($"You have fully changed your race to {targetRace.Name}.");
}
owner?.ConsumeNeeded(pm);
}
private class RaceChangeState
{
private static readonly TimeSpan m_TimeoutDelay = TimeSpan.FromMinutes(1);
public readonly IRaceChanger m_Owner;
public readonly Race m_TargetRace;
public TimerExecutionToken _timeoutToken;
public RaceChangeState(IRaceChanger owner, NetState ns, Race targetRace)
{
m_Owner = owner;
m_TargetRace = targetRace;
Timer.StartTimer(m_TimeoutDelay, () => Timeout(ns), out _timeoutToken);
}
}
}
[SerializationGenerator(0, false)]
public partial class RaceChangeDeed : Item, IRaceChanger
{
[Constructible]
public RaceChangeDeed() : base(0x14F0) => LootType = LootType.Blessed;
public override string DefaultName => "a race change deed";
public bool CheckComplete(PlayerMobile pm)
{
if (Deleted)
{
return false;
}
if (!IsChildOf(pm.Backpack))
{
pm.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
return false;
}
return true;
}
public void ConsumeNeeded(PlayerMobile pm)
{
Consume();
}
public void OnCancel(PlayerMobile pm)
{
}
public override void OnDoubleClick(Mobile from)
{
if (from is not PlayerMobile pm)
{
return;
}
if (CheckComplete(pm))
{
RaceChangeConfirmGump.DisplayTo(pm, this, pm.Race == Race.Human ? Race.Elf : Race.Human);
}
}
}
}