ModernUO/Projects/UOContent/Engines/ML Quests/MLQuestEntry.cs
Kamron Batman 9b554f69b0
feat(timers): Adds timer pooling, fixes timer related bugs, and changes timer api (#667)
### Changes/Fixes:
* Adds timer pooling.
* Allows pool to be configurable in ModernUO.json
* Pool replenishes itself asynchronously if depleted.
* Fixes an issue with barkeeps and town criers
* Fixes an issue with incognito buff icons not being removed
* Fixes an issue with polymorph name mod not being removed
* Fixes several places where timers go on forever even after an object is deleted, keeping a reference (memory leak)
* Eliminates the timer for MiningCart altogether.
* Deletes `AcidSlime` since it is a duplicate of `PoolOfAcid`
* Fixes HonorableExecution and standardizes the code for other Bushido moves.

## Changes to the Timer API:
```cs
public class Timer
{
  // Creates a timer that will be returned to the pool once execution stops.
  public static void StartTimer(Action callback);
  public static void StartTimer(TimeSpan delay, Action callback);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback);
  public static void StartTimer(TimeSpan interval, int count, Action callback);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback);

  // Creates a timer and returns a token for more control. Requires manual cancellation in order for the timer to be returned to the pool.
  // If the token is dereferenced, the timer will be dereferenced too. While not returning a timer to the pool is not considered hazardous, it does defeat the purpose of pooled timers.
  public static void StartTimer(Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token);

  // If you aren't sure how to use the API above, or you don't care about performance, then you can use the old RunUO Timer.DelayCall
  public static DelayCallTimer DelayCall(Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback);
}

public struct TimerExecutionToken
{
  public bool Running { get; }
  public int Index { get; }
  public int RemainingCount { get; }
  public DateTime Next { get; }
}
```

## When to use `TimerExecutionToken`?
Use tokens when you want to gain the performance benefit of using a pooled timer, but you need one of the following:
* Access to the next time the timer will tick:`token.Next`
* Access to which interval, how many intervals there are, or how many are remaining: `token.Index`, `token.Count`, and `token.RemainingCount`
* Stop a timer manually.
* Determine if the timer is running: `timer.Running`
* See notes below about requirements for using tokens!

## Notes about using the TimerExecutionToken:
When you opt-in to receive a token, you must call `Cancel()` to return the timer. This can be done inside of the callback, or outside of the callback at any time.
If this is not called and your timer is an infinite interval, then you will create a potential memory leak, or null pointer exception in your callback.
If the timer ends and is stopped, but cancel is not called, then the timer will never return to the pool and stay referenced until the token is deleted or cancel is called. (Memory leak)

## Is this thread safe?
No. The ModernUO timer system is not thread safe at all. If you require a thread safe timer system, contact me and I'll help adapt this system. Keep in mind that there is a massive performance hit to make this thread safe when there are literally no use cases for it.

If you need to synchronize execution, meaning you want to execute code from another thread on the core thread. Let's say you have a discord bot that is pushing commands to the game server. Then use `EventLoopContext.Post(SendOrPostCallback callback, object state);`.
2021-08-07 14:33:35 -07:00

569 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using Server.Engines.MLQuests.Gumps;
using Server.Engines.MLQuests.Objectives;
using Server.Mobiles;
namespace Server.Engines.MLQuests
{
[Flags]
public enum MLQuestInstanceFlags : byte
{
None = 0x00,
ClaimReward = 0x01,
Removed = 0x02,
Failed = 0x04
}
public class MLQuestInstance
{
private MLQuestInstanceFlags m_Flags;
private IQuestGiver m_Quester;
private TimerExecutionToken _timerToken;
public MLQuestInstance(MLQuest quest, IQuestGiver quester, PlayerMobile player)
{
Quest = quest;
m_Quester = quester;
QuesterType = quester?.GetType();
Player = player;
Accepted = Core.Now;
m_Flags = MLQuestInstanceFlags.None;
Objectives = new BaseObjectiveInstance[quest.Objectives.Count];
BaseObjectiveInstance obj;
var timed = false;
for (var i = 0; i < quest.Objectives.Count; ++i)
{
Objectives[i] = obj = quest.Objectives[i].CreateInstance(this);
if (obj.IsTimed)
{
timed = true;
}
}
Register();
if (timed)
{
Timer.StartTimer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), Slice, out _timerToken);
}
}
public MLQuest Quest { get; set; }
public IQuestGiver Quester
{
get => m_Quester;
set
{
m_Quester = value;
QuesterType = value?.GetType();
}
}
public Type QuesterType { get; private set; }
public PlayerMobile Player { get; set; }
public MLQuestContext PlayerContext => MLQuestSystem.GetOrCreateContext(Player);
public DateTime Accepted { get; set; }
public bool ClaimReward
{
get => GetFlag(MLQuestInstanceFlags.ClaimReward);
set => SetFlag(MLQuestInstanceFlags.ClaimReward, value);
}
public bool Removed
{
get => GetFlag(MLQuestInstanceFlags.Removed);
set => SetFlag(MLQuestInstanceFlags.Removed, value);
}
public bool Failed
{
get => GetFlag(MLQuestInstanceFlags.Failed);
set => SetFlag(MLQuestInstanceFlags.Failed, value);
}
public BaseObjectiveInstance[] Objectives { get; set; }
public bool SkipReportBack => TextDefinition.IsNullOrEmpty(Quest.CompletionMessage);
private void Register()
{
Quest?.Instances?.Add(this);
if (Player != null)
{
PlayerContext.QuestInstances.Add(this);
}
}
private void Unregister()
{
Quest?.Instances?.Remove(this);
if (Player != null)
{
PlayerContext.QuestInstances.Remove(this);
}
Removed = true;
}
public bool AllowsQuestItem(Item item, Type type)
{
foreach (var objective in Objectives)
{
if (!objective.Expired && objective.AllowsQuestItem(item, type))
{
return true;
}
}
return false;
}
public bool IsCompleted()
{
var requiresAll = Quest.ObjectiveType == ObjectiveType.All;
foreach (var obj in Objectives)
{
var complete = obj.IsCompleted();
if (complete && !requiresAll)
{
return true;
}
if (!complete && requiresAll)
{
return false;
}
}
return requiresAll;
}
public void CheckComplete()
{
if (IsCompleted())
{
Player.PlaySound(0x5B5); // public sound
foreach (var obj in Objectives)
{
obj.OnQuestCompleted();
}
TextDefinition.SendMessageTo(Player, Quest.CompletionNotice, 0x23);
/*
* Advance to the ClaimReward=true stage if this quest has no
* completion message to show anyway. This suppresses further
* triggers of CheckComplete.
*
* For quests that require collections, this is done later when
* the player double clicks the quester.
*/
if (!Removed && SkipReportBack && !Quest.RequiresCollection
) // An OnQuestCompleted can potentially have removed this instance already
{
ContinueReportBack(false);
}
}
}
public void Fail()
{
Failed = true;
}
private void Slice()
{
if (ClaimReward || Removed)
{
StopTimer();
return;
}
var hasAnyFails = false;
var hasAnyLeft = false;
foreach (var obj in Objectives)
{
if (!obj.Expired)
{
if (obj.IsTimed && obj.EndTime <= Core.Now)
{
Player.SendLocalizedMessage(1072258); // You failed to complete an objective in time!
obj.Expired = true;
obj.OnExpire();
hasAnyFails = true;
}
else
{
hasAnyLeft = true;
}
}
}
if (Quest.ObjectiveType == ObjectiveType.All && hasAnyFails || !hasAnyLeft)
{
Fail();
}
if (!hasAnyLeft)
{
StopTimer();
}
}
public void SendProgressGump()
{
Player.SendGump(new QuestConversationGump(Quest, Player, Quest.InProgressMessage));
}
public void SendRewardOffer()
{
Quest.GetRewards(this);
}
// TODO: Split next quest stuff from SendRewardGump stuff?
public void SendRewardGump()
{
var nextQuestType = Quest.NextQuest;
if (nextQuestType != null)
{
ClaimRewards(); // skip reward gump
if (Removed) // rewards were claimed successfully
{
var nextQuest = MLQuestSystem.FindQuest(nextQuestType);
nextQuest?.SendOffer(m_Quester, Player);
}
}
else
{
Player.SendGump(new QuestRewardGump(this));
}
}
public void SendReportBackGump()
{
if (SkipReportBack)
{
ContinueReportBack(true); // skip ahead
}
else
{
Player.SendGump(new QuestReportBackGump(this));
}
}
public void ContinueReportBack(bool sendRewardGump)
{
// There is a backpack check here on OSI for the rewards as well (even though it's not needed...)
if (Quest.ObjectiveType == ObjectiveType.All)
{
// TODO: 1115877 - You no longer have the required items to complete this quest.
foreach (var objective in Objectives)
{
if (!objective.IsCompleted())
{
return;
}
}
foreach (var objective in Objectives)
{
if (!objective.OnBeforeClaimReward())
{
return;
}
}
foreach (var objective in Objectives)
{
objective.OnClaimReward();
}
}
else
{
/* The following behavior is unverified, as OSI (currently) has no collect quest requiring
* only one objective to be completed. It is assumed that only one objective is claimed
* (the first completed one), even when multiple are complete.
*/
var complete = false;
foreach (var objective in Objectives)
{
if (objective.IsCompleted())
{
if (objective.OnBeforeClaimReward())
{
complete = true;
objective.OnClaimReward();
}
break;
}
}
if (!complete)
{
return;
}
}
ClaimReward = true;
if (Quest.HasRestartDelay)
{
PlayerContext.SetDoneQuest(Quest, Core.Now + Quest.GetRestartDelay());
}
// This is correct for ObjectiveType.Any as well
foreach (var objective in Objectives)
{
objective.OnAfterClaimReward();
}
if (sendRewardGump)
{
SendRewardOffer();
}
}
public void ClaimRewards()
{
if (Quest == null || Player?.Deleted != false || !ClaimReward || Removed)
{
return;
}
var rewards = new List<Item>();
foreach (var reward in Quest.Rewards)
{
reward.AddRewardItems(Player, rewards);
}
if (rewards.Count != 0)
{
// On OSI a more naive method of checking is used.
// For containers, only the actual container item counts.
var canFit = true;
foreach (var rewardItem in rewards)
{
if (!Player.AddToBackpack(rewardItem))
{
canFit = false;
break;
}
}
if (!canFit)
{
foreach (var rewardItem in rewards)
{
rewardItem.Delete();
}
Player.SendLocalizedMessage(
1078524
); // Your backpack is full. You cannot complete the quest and receive your reward.
return;
}
foreach (var rewardItem in rewards)
{
var rewardName = rewardItem.Name ?? $"#{rewardItem.LabelNumber}";
if (rewardItem.Stackable)
{
Player.SendLocalizedMessage(
1115917,
$"{rewardItem.Amount}\t{rewardName}"
); // You receive a reward: ~1_QUANTITY~ ~2_ITEM~
}
else
{
Player.SendLocalizedMessage(1074360, rewardName); // You receive a reward: ~1_REWARD~
}
}
}
foreach (var objective in Objectives)
{
objective.OnRewardClaimed();
}
Quest.OnRewardClaimed(this);
var context = PlayerContext;
if (Quest.RecordCompletion && !Quest.HasRestartDelay) // Quests with restart delays are logged earlier as per OSI
{
context.SetDoneQuest(Quest);
}
if (Quest.IsChainTriggered)
{
context.ChainOffers.Remove(Quest);
}
var nextQuestType = Quest.NextQuest;
if (nextQuestType != null)
{
var nextQuest = MLQuestSystem.FindQuest(nextQuestType);
if (nextQuest != null && !context.ChainOffers.Contains(nextQuest))
{
context.ChainOffers.Add(nextQuest);
}
}
Remove();
}
public void Cancel()
{
Cancel(false);
}
public void Cancel(bool removeChain)
{
Remove();
Player.SendSound(0x5B3); // private sound
foreach (var obj in Objectives)
{
obj.OnQuestCancelled();
}
Quest.OnCancel(this);
if (removeChain)
{
PlayerContext.ChainOffers.Remove(Quest);
}
}
public void Remove()
{
Unregister();
StopTimer();
}
private void StopTimer()
{
_timerToken.Cancel();
}
public void OnQuesterDeleted()
{
foreach (var obj in Objectives)
{
obj.OnQuesterDeleted();
}
Quest.OnQuesterDeleted(this);
}
public void OnPlayerDeath()
{
foreach (var obj in Objectives)
{
obj.OnPlayerDeath();
}
Quest.OnPlayerDeath(this);
}
private bool GetFlag(MLQuestInstanceFlags flag) => (m_Flags & flag) != 0;
private void SetFlag(MLQuestInstanceFlags flag, bool value)
{
if (value)
{
m_Flags |= flag;
}
else
{
m_Flags &= ~flag;
}
}
public void Serialize(IGenericWriter writer)
{
// Version info is written in MLQuestPersistence.Serialize
MLQuestSystem.WriteQuestRef(writer, Quest);
writer.Write(m_Quester?.Deleted != false ? Serial.MinusOne : m_Quester.Serial);
writer.Write(ClaimReward);
writer.Write(Objectives.Length);
foreach (var objInstance in Objectives)
{
objInstance.Serialize(writer);
}
}
public static MLQuestInstance Deserialize(IGenericReader reader, int version, PlayerMobile pm)
{
var quest = MLQuestSystem.ReadQuestRef(reader);
// TODO: Serialize quester TYPE too, the quest giver reference then becomes optional (only for escorts)
var quester = World.FindEntity(reader.ReadUInt()) as IQuestGiver;
var claimReward = reader.ReadBool();
var objectives = reader.ReadInt();
MLQuestInstance instance;
if (quest != null && quester != null && pm != null)
{
instance = quest.CreateInstance(quester, pm);
instance.ClaimReward = claimReward;
}
else
{
instance = null;
}
for (var i = 0; i < objectives; ++i)
{
BaseObjectiveInstance.Deserialize(
reader,
version,
i < instance?.Objectives.Length ? instance.Objectives[i] : null
);
}
instance?.Slice();
return instance;
}
}
}