ModernUO/Projects/UOContent/Engines/Quests/Core/QuestSystem.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

742 lines
22 KiB
C#

using System;
using System.Collections.Generic;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.Quests.Ambitious;
using Server.Engines.Quests.Collector;
using Server.Engines.Quests.Doom;
using Server.Engines.Quests.Hag;
using Server.Engines.Quests.Haven;
using Server.Engines.Quests.Matriarch;
using Server.Engines.Quests.Naturalist;
using Server.Engines.Quests.Necro;
using Server.Engines.Quests.Ninja;
using Server.Engines.Quests.Samurai;
using Server.Engines.Quests.Zento;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
using Server.Network;
namespace Server.Engines.Quests
{
public delegate void QuestCallback();
public abstract class QuestSystem
{
public static readonly Type[] QuestTypes =
{
typeof(TheSummoningQuest),
typeof(DarkTidesQuest),
typeof(UzeraanTurmoilQuest),
typeof(CollectorQuest),
typeof(WitchApprenticeQuest),
typeof(StudyOfSolenQuest),
typeof(SolenMatriarchQuest),
typeof(AmbitiousQueenQuest),
typeof(EminosUndertakingQuest),
typeof(HaochisTrialsQuest),
typeof(TerribleHatchlingsQuest)
};
private TimerExecutionToken _timerToken;
public QuestSystem(PlayerMobile from)
{
From = from;
Objectives = new List<QuestObjective>();
Conversations = new List<QuestConversation>();
}
public QuestSystem()
{
}
public abstract object Name { get; }
public abstract object OfferMessage { get; }
public abstract int Picture { get; }
public abstract bool IsTutorial { get; }
public abstract TimeSpan RestartDelay { get; }
public abstract Type[] TypeReferenceTable { get; }
public PlayerMobile From { get; set; }
public List<QuestObjective> Objectives { get; set; }
public List<QuestConversation> Conversations { get; set; }
public virtual void StartTimer()
{
if (_timerToken.Running)
{
return;
}
// TODO: Find out if this can go on forever. We should not allow timers to leak if this is the case.
Timer.StartTimer(TimeSpan.FromSeconds(0.5), TimeSpan.FromSeconds(0.5), Slice, out _timerToken);
}
public virtual void StopTimer()
{
_timerToken.Cancel();
}
public virtual void Slice()
{
for (var i = Objectives.Count - 1; i >= 0; --i)
{
var obj = Objectives[i];
if (obj.GetTimerEvent())
{
obj.CheckProgress();
}
}
}
public virtual void OnKill(BaseCreature creature, Container corpse)
{
for (var i = Objectives.Count - 1; i >= 0; --i)
{
var obj = Objectives[i];
if (obj.GetKillEvent(creature, corpse))
{
obj.OnKill(creature, corpse);
}
}
}
public virtual bool IgnoreYoungProtection(Mobile from)
{
for (var i = Objectives.Count - 1; i >= 0; --i)
{
var obj = Objectives[i];
if (obj.IgnoreYoungProtection(from))
{
return true;
}
}
return false;
}
public virtual void BaseDeserialize(IGenericReader reader)
{
var referenceTable = TypeReferenceTable;
var version = reader.ReadEncodedInt();
switch (version)
{
case 0:
{
var count = reader.ReadEncodedInt();
Objectives = new List<QuestObjective>(count);
for (var i = 0; i < count; ++i)
{
var obj = QuestSerializer.DeserializeObjective(referenceTable, reader);
if (obj != null)
{
obj.System = this;
Objectives.Add(obj);
}
}
count = reader.ReadEncodedInt();
Conversations = new List<QuestConversation>(count);
for (var i = 0; i < count; ++i)
{
var conv = QuestSerializer.DeserializeConversation(referenceTable, reader);
if (conv != null)
{
conv.System = this;
Conversations.Add(conv);
}
}
break;
}
}
ChildDeserialize(reader);
}
public virtual void ChildDeserialize(IGenericReader reader)
{
var version = reader.ReadEncodedInt();
}
public virtual void BaseSerialize(IGenericWriter writer)
{
var referenceTable = TypeReferenceTable;
writer.WriteEncodedInt(0); // version
writer.WriteEncodedInt(Objectives.Count);
for (var i = 0; i < Objectives.Count; ++i)
{
QuestSerializer.Serialize(referenceTable, Objectives[i], writer);
}
writer.WriteEncodedInt(Conversations.Count);
for (var i = 0; i < Conversations.Count; ++i)
{
QuestSerializer.Serialize(referenceTable, Conversations[i], writer);
}
ChildSerialize(writer);
}
public virtual void ChildSerialize(IGenericWriter writer)
{
writer.WriteEncodedInt(0); // version
}
public bool IsObjectiveInProgress(Type type)
{
var obj = FindObjective(type);
return obj?.Completed == false;
}
public T FindObjective<T>() where T : QuestObjective
{
for (var i = Objectives.Count - 1; i >= 0; --i)
{
var obj = Objectives[i];
if (obj is T t)
{
return t;
}
}
return null;
}
public QuestObjective FindObjective(Type type)
{
for (var i = Objectives.Count - 1; i >= 0; --i)
{
var obj = Objectives[i];
if (obj.GetType() == type)
{
return obj;
}
}
return null;
}
public virtual void SendOffer()
{
QuestOfferGump.DisplayTo(From, this);
}
public virtual void GetContextMenuEntries(ref PooledRefList<ContextMenuEntry> list)
{
if (Objectives.Count > 0)
{
list.Add(new QuestCallbackEntry(6154, ShowQuestLog)); // View Quest Log
}
if (Conversations.Count > 0)
{
list.Add(new QuestCallbackEntry(6156, ShowQuestConversation)); // Quest Conversation
}
list.Add(new QuestCallbackEntry(6155, BeginCancelQuest)); // Cancel Quest
}
public virtual void ShowQuestLogUpdated()
{
QuestLogUpdatedGump.DisplayTo(From, this);
}
public virtual void ShowQuestLog()
{
if (Objectives.Count > 0)
{
var gumps = From.GetGumps();
gumps.Close<QuestItemInfoGump>();
gumps.Close<QuestLogUpdatedGump>();
gumps.Close<QuestConversationsGump>();
gumps.Close<QuestObjectivesGump>();
QuestObjectivesGump.DisplayTo(From, Objectives);
var last = Objectives[^1];
if (last.Info != null)
{
QuestItemInfoGump.DisplayTo(From, last.Info);
}
}
}
public virtual void ShowQuestConversation()
{
if (Conversations.Count > 0)
{
var gumps = From.GetGumps();
gumps.Close<QuestItemInfoGump>();
gumps.Close<QuestObjectivesGump>();
gumps.Close<QuestConversationsGump>();
QuestConversationsGump.DisplayTo(From, Conversations);
var last = Conversations[^1];
if (last.Info != null)
{
QuestItemInfoGump.DisplayTo(From, last.Info);
}
}
}
public virtual void BeginCancelQuest()
{
QuestCancelGump.DisplayTo(From, this);
}
public virtual void EndCancelQuest(bool shouldCancel)
{
if (From.Quest != this)
{
return;
}
if (shouldCancel)
{
From.SendLocalizedMessage(1049015); // You have canceled your quest.
Cancel();
}
else
{
From.SendLocalizedMessage(1049014); // You have chosen not to cancel your quest.
}
}
public virtual void Cancel()
{
ClearQuest(false);
}
public virtual void Complete()
{
ClearQuest(true);
}
public virtual void ClearQuest(bool completed)
{
StopTimer();
if (From.Quest != this)
{
return;
}
From.Quest = null;
var restartDelay = RestartDelay;
if (completed && restartDelay > TimeSpan.Zero || !completed && restartDelay == TimeSpan.MaxValue)
{
From.DoneQuests ??= new List<QuestRestartInfo>();
var found = false;
var ourQuestType = GetType();
for (var i = 0; i < From.DoneQuests.Count; ++i)
{
var restartInfo = From.DoneQuests[i];
if (restartInfo.QuestType == ourQuestType)
{
restartInfo.Reset(restartDelay);
found = true;
break;
}
}
if (!found)
{
From.DoneQuests.Add(new QuestRestartInfo(ourQuestType, restartDelay));
}
}
}
public virtual void AddConversation(QuestConversation conv)
{
conv.System = this;
if (conv.Logged)
{
Conversations.Add(conv);
}
var gumps = From.GetGumps();
gumps.Close<QuestItemInfoGump>();
gumps.Close<QuestObjectivesGump>();
if (conv.Logged)
{
QuestConversationsGump.DisplayTo(From, Conversations);
}
else
{
QuestConversationsGump.DisplayTo(From, conv);
}
if (conv.Info != null)
{
QuestItemInfoGump.DisplayTo(From, conv.Info);
}
}
public virtual void AddObjective(QuestObjective obj)
{
obj.System = this;
Objectives.Add(obj);
ShowQuestLogUpdated();
}
public virtual void Accept()
{
if (From.Quest != null)
{
return;
}
From.Quest = this;
From.SendLocalizedMessage(1049019); // You have accepted the Quest.
StartTimer();
}
public virtual void Decline()
{
From.SendLocalizedMessage(1049018); // You have declined the Quest.
}
public static bool CanOfferQuest(Mobile check, Type questType) => CanOfferQuest(check, questType, out _);
public static bool CanOfferQuest(Mobile check, Type questType, out bool inRestartPeriod)
{
inRestartPeriod = false;
if (check is not PlayerMobile pm)
{
return false;
}
if (pm.HasGump<QuestOfferGump>())
{
return false;
}
if (questType == typeof(DarkTidesQuest) && pm.Profession != 4) // necromancer
{
return false;
}
// warrior / magician / paladin
if (questType == typeof(UzeraanTurmoilQuest) && pm.Profession != 1 && pm.Profession != 2 && pm.Profession != 5)
{
return false;
}
if (questType == typeof(HaochisTrialsQuest) && pm.Profession != 6) // samurai
{
return false;
}
if (questType == typeof(EminosUndertakingQuest) && pm.Profession != 7) // ninja
{
return false;
}
var doneQuests = pm.DoneQuests;
if (doneQuests != null)
{
for (var i = 0; i < doneQuests.Count; ++i)
{
var restartInfo = doneQuests[i];
if (restartInfo.QuestType == questType)
{
var endTime = restartInfo.RestartTime;
if (Core.Now < endTime)
{
inRestartPeriod = true;
return false;
}
doneQuests.RemoveAt(i);
return true;
}
}
}
return true;
}
public static void FocusTo(Mobile who, Mobile to)
{
if (Utility.RandomBool())
{
who.Animate(17, 7, 1, true, false, 0);
}
else
{
who.Animate(32 + Utility.Random(3), 7, 1, true, false, 0);
}
who.Direction = who.GetDirectionTo(to);
}
public static int RandomBrightHue()
{
if (Utility.RandomDouble() < 0.1)
{
return Utility.RandomList(0x62, 0x71);
}
return Utility.RandomList(0x03, 0x0D, 0x13, 0x1C, 0x21, 0x30, 0x37, 0x3A, 0x44, 0x59);
}
}
public class QuestCancelGump : BaseQuestGump
{
private readonly QuestSystem _system;
public override bool Singleton => true;
private QuestCancelGump(QuestSystem system) : base(120, 50) => _system = system;
public static void DisplayTo(Mobile from, QuestSystem system)
{
if (from?.NetState == null || system == null)
{
return;
}
from.SendGump(new QuestCancelGump(system));
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.SetNoClose();
builder.AddPage();
builder.AddImageTiled(0, 0, 348, 262, 2702);
builder.AddAlphaRegion(0, 0, 348, 262);
builder.AddImage(0, 15, 10152);
builder.AddImageTiled(0, 30, 17, 200, 10151);
builder.AddImage(0, 230, 10154);
builder.AddImage(15, 0, 10252);
builder.AddImageTiled(30, 0, 300, 17, 10250);
builder.AddImage(315, 0, 10254);
builder.AddImage(15, 244, 10252);
builder.AddImageTiled(30, 244, 300, 17, 10250);
builder.AddImage(315, 244, 10254);
builder.AddImage(330, 15, 10152);
builder.AddImageTiled(330, 30, 17, 200, 10151);
builder.AddImage(330, 230, 10154);
builder.AddImage(333, 2, 10006);
builder.AddImage(333, 248, 10006);
builder.AddImage(2, 248, 10006);
builder.AddImage(2, 2, 10006);
builder.AddHtmlLocalized(25, 22, 200, 20, 1049000, 32000); // Confirm Quest Cancellation
builder.AddImage(25, 40, 3007);
if (_system.IsTutorial)
{
builder.AddHtmlLocalized(
25,
55,
300,
120,
1060836,
BaseQuestGump.White
); // This quest will give you valuable information, skills and equipment that will help you advance in the game at a quicker pace.<BR><BR>Are you certain you wish to cancel at this time?
}
else
{
builder.AddHtmlLocalized(25, 60, 300, 20, 1049001, BaseQuestGump.White); // You have chosen to abort your quest:
builder.AddImage(25, 81, 0x25E7);
BaseQuestGump.AddHtmlObject(ref builder, 48, 80, 280, 20, _system.Name, BaseQuestGump.DarkGreen, false, false);
builder.AddHtmlLocalized(25, 120, 280, 20, 1049002, BaseQuestGump.White); // Can this quest be restarted after quitting?
builder.AddImage(25, 141, 0x25E7);
builder.AddHtmlLocalized(
48,
140,
280,
20,
_system.RestartDelay < TimeSpan.MaxValue ? 1049016 : 1049017,
BaseQuestGump.DarkGreen
); // Yes/No
}
builder.AddRadio(25, 175, 9720, 9723, true, 1);
builder.AddHtmlLocalized(60, 180, 280, 20, 1049005, BaseQuestGump.White); // Yes, I really want to quit!
builder.AddRadio(25, 210, 9720, 9723, false, 0);
builder.AddHtmlLocalized(60, 215, 280, 20, 1049006, BaseQuestGump.White); // No, I don't want to quit.
builder.AddButton(265, 220, 247, 248, 1);
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (info.ButtonID == 1)
{
_system.EndCancelQuest(info.IsSwitched(1));
}
}
}
public class QuestOfferGump : BaseQuestGump
{
private readonly QuestSystem _system;
public override bool Singleton => true;
private QuestOfferGump(QuestSystem system) : base(75, 25) => _system = system;
public static void DisplayTo(Mobile from, QuestSystem system)
{
if (from?.NetState == null || system == null)
{
return;
}
from.SendGump(new QuestOfferGump(system));
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.SetNoClose();
builder.AddPage();
builder.AddImageTiled(50, 20, 400, 400, 2624);
builder.AddAlphaRegion(50, 20, 400, 400);
builder.AddImage(90, 33, 9005);
builder.AddHtmlLocalized(130, 45, 270, 20, 1049010, BaseQuestGump.White); // Quest Offer
builder.AddImageTiled(130, 65, 175, 1, 9101);
builder.AddImage(140, 110, 1209);
BaseQuestGump.AddHtmlObject(ref builder, 160, 108, 250, 20, _system.Name, BaseQuestGump.DarkGreen, false, false);
BaseQuestGump.AddHtmlObject(ref builder, 98, 140, 312, 200, _system.OfferMessage, BaseQuestGump.LightGreen, false, true);
builder.AddRadio(85, 350, 9720, 9723, true, 1);
builder.AddHtmlLocalized(120, 356, 280, 20, 1049011, BaseQuestGump.White); // I accept!
builder.AddRadio(85, 385, 9720, 9723, false, 0);
builder.AddHtmlLocalized(120, 391, 280, 20, 1049012, BaseQuestGump.White); // No thanks, I decline.
builder.AddButton(340, 390, 247, 248, 1);
builder.AddImageTiled(50, 29, 30, 390, 10460);
builder.AddImageTiled(34, 140, 17, 279, 9263);
builder.AddImage(48, 135, 10411);
builder.AddImage(-16, 285, 10402);
builder.AddImage(0, 10, 10421);
builder.AddImage(25, 0, 10420);
builder.AddImageTiled(83, 15, 350, 15, 10250);
builder.AddImage(34, 419, 10306);
builder.AddImage(442, 419, 10304);
builder.AddImageTiled(51, 419, 392, 17, 10101);
builder.AddImageTiled(415, 29, 44, 390, 2605);
builder.AddImageTiled(415, 29, 30, 390, 10460);
builder.AddImage(425, 0, 10441);
builder.AddImage(370, 50, 1417);
builder.AddImage(379, 60, _system.Picture);
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (info.ButtonID == 1)
{
if (info.IsSwitched(1))
{
_system.Accept();
}
else
{
_system.Decline();
}
}
}
}
public abstract class BaseQuestGump : DynamicGump
{
public const int Black = 0x0000;
public const int White = 0x7FFF;
public const int DarkGreen = 10000;
public const int LightGreen = 90000;
public const int Blue = 19777215;
protected BaseQuestGump(int x, int y) : base(x, y)
{
}
public static void AddHtmlObject(
ref DynamicGumpBuilder builder,
int x,
int y,
int width,
int height,
object message,
int color,
bool back,
bool scroll
)
{
if (message is int html)
{
builder.AddHtmlLocalized(x, y, width, height, html, color.C16216(), back, scroll);
}
else
{
builder.AddHtml(x, y, width, height, Html.Color($"{message}", color.C16216()), background: back, scrollbar: scroll);
}
}
}
}