ModernUO/Projects/UOContent/Engines/ML Quests/Objectives/EscortObjective.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

294 lines
8.4 KiB
C#

using System;
using Server.Engines.Virtues;
using Server.Gumps;
using Server.Misc;
using Server.Mobiles;
namespace Server.Engines.MLQuests.Objectives
{
public class EscortObjective : BaseObjective
{
public EscortObjective(QuestArea destination = null) => Destination = destination;
public QuestArea Destination { get; set; }
public override bool CanOffer(IQuestGiver quester, PlayerMobile pm, bool message)
{
if (quester is BaseCreature creature && creature.Controlled ||
quester is BaseEscortable escortable && escortable.IsBeingDeleted)
{
return false;
}
var context = MLQuestSystem.GetContext(pm);
if (context != null)
{
foreach (var instance in context.QuestInstances)
{
if (instance.Quest.IsEscort)
{
if (message)
{
MLQuestSystem.Tell(quester, pm, 500896); // I see you already have an escort.
}
return false;
}
}
}
var nextEscort = pm.LastEscortTime + BaseEscortable.EscortDelay;
if (nextEscort > Core.Now)
{
if (message)
{
var minutes = (int)Math.Ceiling((nextEscort - Core.Now).TotalMinutes);
if (minutes == 1)
{
MLQuestSystem.Tell(quester, pm, 1042722);
}
else
{
// You must rest ~1_minsleft~ minutes before we set out on this journey.
MLQuestSystem.Tell(quester, pm, 1071195, minutes.ToString());
}
}
return false;
}
return true;
}
public override void WriteToGump(ref DynamicGumpBuilder builder, ref int y)
{
builder.AddHtmlLocalized(98, y, 312, 16, 1072206, 0x5F90); // Escort to
if (Destination.Name.Number > 0)
{
builder.AddHtmlLocalized(173, y, 312, 20, Destination.Name.Number, 0x7FFF);
}
else if (Destination.Name.String != null)
{
builder.AddLabel(173, y, 0x481, Destination.Name.String);
}
y += 16;
}
public override BaseObjectiveInstance CreateInstance(MLQuestInstance instance)
{
if (instance == null || Destination == null)
{
return null;
}
return new EscortObjectiveInstance(this, instance);
}
}
public class EscortObjectiveInstance : BaseObjectiveInstance
{
private readonly BaseCreature m_Escort;
private readonly EscortObjective m_Objective;
private DateTime m_LastSeenEscorter;
private TimerExecutionToken _timerToken;
public EscortObjectiveInstance(EscortObjective objective, MLQuestInstance instance)
: base(instance, objective)
{
m_Objective = objective;
HasCompleted = false;
Timer.StartTimer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckDestination, out _timerToken);
m_LastSeenEscorter = Core.Now;
m_Escort = instance.Quester as BaseCreature;
if (MLQuestSystem.Debug && m_Escort == null && instance.Quester != null)
{
Console.WriteLine(
"Warning: EscortObjective is not supported for type '{0}'",
instance.Quester.GetType().Name
);
}
}
public bool HasCompleted { get; set; }
public override DataType ExtraDataType => DataType.EscortObjective;
public override bool IsCompleted() => HasCompleted;
private void CheckDestination()
{
if (m_Escort == null || HasCompleted) // Completed by deserialization
{
StopTimer();
return;
}
var instance = Instance;
var pm = instance.Player;
if (instance.Removed)
{
Abandon();
}
else if (m_Objective.Destination.Contains(m_Escort))
{
// We have arrived! I thank thee, ~1_PLAYER_NAME~! I have no further need of thy services. Here is thy pay.
m_Escort.Say(1042809, pm.Name);
if (pm.Young || m_Escort.Region.IsPartOf("Haven Island"))
{
Titles.AwardFame(pm, 10, true);
}
else
{
VirtueSystem.AwardVirtue(
pm,
VirtueName.Compassion,
(m_Escort as BaseEscortable)?.IsPrisoner == true ? 400 : 200
);
}
EndFollow(m_Escort);
StopTimer();
HasCompleted = true;
CheckComplete();
// Auto claim reward
MLQuestSystem.OnDoubleClick(m_Escort, pm);
}
else if (pm.Map != m_Escort.Map || !pm.InRange(m_Escort, 30)) // TODO: verify range
{
if (m_LastSeenEscorter + BaseEscortable.AbandonDelay <= Core.Now)
{
Abandon();
}
}
else
{
m_LastSeenEscorter = Core.Now;
}
}
private void StopTimer()
{
_timerToken.Cancel();
}
public static void BeginFollow(BaseCreature quester, PlayerMobile pm)
{
quester.ControlSlots = 0;
quester.SetControlMaster(pm);
quester.ControlOrder = OrderType.Follow;
quester.ControlTarget = pm;
quester.CantWalk = false;
quester.SetSpeed(0.1, 0.2, false);
}
public static void EndFollow(BaseCreature quester)
{
quester.ControlOrder = OrderType.None;
quester.ControlTarget = null;
quester.SetControlMaster(null);
quester.SetSpeed(0.1, 0.2);
(quester as BaseEscortable)?.BeginDelete();
}
public override void OnQuestAccepted()
{
var pm = Instance.Player;
pm.LastEscortTime = Core.Now;
if (m_Escort != null)
{
BeginFollow(m_Escort, pm);
}
}
public void Abandon()
{
StopTimer();
var instance = Instance;
var pm = instance.Player;
if (m_Escort?.Deleted == false)
{
if (!pm.Alive)
{
m_Escort.Say(500901); // Ack! My escort has come to haunt me!
}
else
{
m_Escort.Say(500902); // My escort seems to have abandoned me!
}
EndFollow(m_Escort);
}
// Note: this sound is sent twice on OSI (once here and once in Cancel())
// m_Player.SendSound(0x5B3); // private sound
pm.SendLocalizedMessage(1071194); // You have failed your escort quest...
if (!instance.Removed)
{
instance.Cancel();
}
}
public override void OnQuesterDeleted()
{
if (IsCompleted() || Instance.Removed)
{
return;
}
Abandon();
}
public override void OnPlayerDeath()
{
// Note: OSI also cancels it when the quest is already complete
if ( /*IsCompleted() ||*/ Instance.Removed)
{
return;
}
Instance.Cancel();
}
public override void OnExpire()
{
Abandon();
}
public override void WriteToGump(ref DynamicGumpBuilder builder, ref int y)
{
m_Objective.WriteToGump(ref builder, ref y);
base.WriteToGump(ref builder, ref y);
// No extra instance stuff printed for this objective
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(HasCompleted);
}
}
}