ModernUO/Projects/UOContent/Engines/Virtues/Honor.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

453 lines
13 KiB
C#

using System;
using Server.Gumps;
using Server.Mobiles;
using Server.Regions;
using Server.Targeting;
namespace Server
{
public static class HonorVirtue
{
private static readonly TimeSpan UseDelay = TimeSpan.FromMinutes(5.0);
public static void Initialize()
{
VirtueGump.Register(107, OnVirtueUsed);
}
private static void OnVirtueUsed(Mobile from)
{
if (from.Alive)
{
from.SendLocalizedMessage(1063160); // Target what you wish to honor.
from.Target = new InternalTarget();
}
}
private static int GetHonorDuration(Mobile from)
{
return VirtueHelper.GetLevel(from, VirtueName.Honor) switch
{
VirtueLevel.Seeker => 30,
VirtueLevel.Follower => 90,
VirtueLevel.Knight => 300,
_ => 0
};
}
private static void EmbraceHonor(PlayerMobile pm)
{
if (pm.HonorActive)
{
pm.SendLocalizedMessage(1063230); // You must wait awhile before you can embrace honor again.
return;
}
if (GetHonorDuration(pm) == 0)
{
pm.SendLocalizedMessage(1063234); // You do not have enough honor to do that
return;
}
var waitTime = Core.Now - pm.LastHonorUse;
if (waitTime < UseDelay)
{
var remainingTime = UseDelay - waitTime;
var remainingMinutes = (int)Math.Ceiling(remainingTime.TotalMinutes);
pm.SendLocalizedMessage(
1063240, // You must wait ~1_HONOR_WAIT~ minutes before embracing honor again
remainingMinutes.ToString()
);
return;
}
pm.SendGump(new HonorSelf(pm));
}
public static void ActivateEmbrace(PlayerMobile pm)
{
var duration = GetHonorDuration(pm);
int usedPoints;
if (pm.Virtues.Honor < 4399)
{
usedPoints = 400;
}
else if (pm.Virtues.Honor < 10599)
{
usedPoints = 600;
}
else
{
usedPoints = 1000;
}
VirtueHelper.Atrophy(pm, VirtueName.Honor, usedPoints);
pm.HonorActive = true;
pm.SendLocalizedMessage(1063235); // You embrace your honor
Timer.StartTimer(
TimeSpan.FromSeconds(duration),
() =>
{
pm.HonorActive = false;
pm.LastHonorUse = Core.Now;
pm.SendLocalizedMessage(1063236); // You no longer embrace your honor
}
);
}
private static void Honor(PlayerMobile source, Mobile target)
{
var honorTarget = target as IHonorTarget;
var reg = source.Region.GetRegion<GuardedRegion>();
var map = source.Map;
if (honorTarget == null)
{
return;
}
if (honorTarget.ReceivedHonorContext != null)
{
if (honorTarget.ReceivedHonorContext.Source == source)
{
return;
}
if (honorTarget.ReceivedHonorContext.CheckDistance())
{
source.SendLocalizedMessage(1063233); // Somebody else is honoring this opponent
return;
}
}
if (target.Hits < target.HitsMax)
{
source.SendLocalizedMessage(1063166); // You cannot honor this monster because it is too damaged.
return;
}
if (target.Body.IsHuman && (target is not BaseCreature cret || !cret.AlwaysAttackable && !cret.AlwaysMurderer))
{
if (reg?.IsDisabled() != true)
{
// Allow honor on blue if Out of guardzone
}
else if ((map?.Rules & MapRules.HarmfulRestrictions) == 0)
{
// Allow honor on blue if in Fel
}
else
{
source.SendLocalizedMessage(1001018); // You cannot perform negative acts
return; // cannot honor in trammel town on blue
}
}
if (Core.ML && target is PlayerMobile)
{
source.SendLocalizedMessage(1075614); // You cannot honor other players.
return;
}
source.SentHonorContext?.Cancel();
_ = new HonorContext(source, target);
source.Direction = source.GetDirectionTo(target);
if (!source.Mounted)
{
source.Animate(32, 5, 1, true, true, 0);
}
}
private class InternalTarget : Target
{
public InternalTarget() : base(12, false, TargetFlags.None) => CheckLOS = true;
protected override void OnTarget(Mobile from, object targeted)
{
if (from is not PlayerMobile pm)
{
return;
}
if (targeted == pm)
{
EmbraceHonor(pm);
}
else if (targeted is Mobile mobile)
{
Honor(pm, mobile);
}
}
protected override void OnTargetOutOfRange(Mobile from, object targeted)
{
from.SendLocalizedMessage(1063232); // You are too far away to honor your opponent
}
}
}
public interface IHonorTarget
{
HonorContext ReceivedHonorContext { get; set; }
}
public class HonorContext
{
private readonly Point3D m_InitialLocation;
private readonly Map m_InitialMap;
private readonly InternalTimer m_Timer;
private FirstHit m_FirstHit;
private double m_HonorDamage;
private bool m_Poisoned;
private int m_TotalDamage;
public HonorContext(PlayerMobile source, Mobile target)
{
Source = source;
Target = target;
m_FirstHit = FirstHit.NotDelivered;
m_Poisoned = false;
m_InitialLocation = source.Location;
m_InitialMap = source.Map;
source.SentHonorContext = this;
((IHonorTarget)target).ReceivedHonorContext = this;
m_Timer = new InternalTimer(this);
m_Timer.Start();
source.m_hontime = Core.Now + TimeSpan.FromMinutes(40);
Timer.StartTimer(
TimeSpan.FromMinutes(40),
() =>
{
if (source.m_hontime < Core.Now && source.SentHonorContext != null)
{
Cancel();
}
}
);
}
public PlayerMobile Source { get; }
public Mobile Target { get; }
public int PerfectionDamageBonus { get; private set; }
public int PerfectionLuckBonus => PerfectionDamageBonus * PerfectionDamageBonus / 10;
public void OnSourceDamaged(Mobile from, int amount)
{
if (from != Target)
{
return;
}
if (m_FirstHit == FirstHit.NotDelivered)
{
m_FirstHit = FirstHit.Granted;
}
}
public void OnTargetPoisoned()
{
m_Poisoned = true; // Set this flag for OnTargetDamaged which will be called next
}
public void OnTargetDamaged(Mobile from, int amount)
{
if (m_FirstHit == FirstHit.NotDelivered)
{
m_FirstHit = FirstHit.Delivered;
}
if (m_Poisoned)
{
m_HonorDamage += amount * 0.8;
m_Poisoned = false; // Reset the flag
return;
}
m_TotalDamage += amount;
if (from == Source)
{
if (Target.CanSee(Source) && Target.InLOS(Source) && (Source.InRange(Target, 1)
|| Source.Location == m_InitialLocation &&
Source.Map == m_InitialMap))
{
m_HonorDamage += amount;
}
else
{
m_HonorDamage += amount * 0.8;
}
}
else if (from is BaseCreature creature && creature.GetMaster() == Source)
{
m_HonorDamage += amount * 0.8;
}
}
public void OnTargetHit(Mobile from)
{
if (from != Source || PerfectionDamageBonus == 100)
{
return;
}
var bushido = (int)from.Skills.Bushido.Value;
if (bushido < 50)
{
return;
}
PerfectionDamageBonus += bushido / 10;
if (PerfectionDamageBonus >= 100)
{
PerfectionDamageBonus = 100;
Source.SendLocalizedMessage(1063254); // You have Achieved Perfection in inflicting damage to this opponent!
}
else
{
Source.SendLocalizedMessage(1063255); // You gain in Perfection as you precisely strike your opponent.
}
}
public void OnTargetMissed(Mobile from)
{
if (from != Source || PerfectionDamageBonus == 0)
{
return;
}
PerfectionDamageBonus -= 25;
if (PerfectionDamageBonus <= 0)
{
PerfectionDamageBonus = 0;
Source.SendLocalizedMessage(1063256); // You have lost all Perfection in fighting this opponent.
}
else
{
Source.SendLocalizedMessage(1063257); // You have lost some Perfection in fighting this opponent.
}
}
public void OnSourceBeneficialAction(Mobile to)
{
if (to != Target)
{
return;
}
if (PerfectionDamageBonus >= 0)
{
PerfectionDamageBonus = 0;
Source.SendLocalizedMessage(1063256); // You have lost all Perfection in fighting this opponent.
}
}
public void OnSourceKilled()
{
}
public void OnTargetKilled()
{
Cancel();
var targetFame = Target.Fame;
if (PerfectionDamageBonus > 0)
{
var restore = Math.Min(PerfectionDamageBonus * (targetFame + 5000) / 25000, 10);
Source.Hits += restore;
Source.Stam += restore;
Source.Mana += restore;
}
if (Source.Virtues.Honor > targetFame)
{
return;
}
var dGain =
targetFame / 100.0 * (m_HonorDamage / m_TotalDamage); // Initial honor gain is 100th of the monsters honor
if (m_HonorDamage == m_TotalDamage && m_FirstHit == FirstHit.Granted)
{
dGain *= 1.5; // honor gain is increased alot more if the combat was fully honorable
}
else
{
dGain *= 0.9;
}
// Minimum gain of 1 honor when the honor is under the monsters fame
var gain = Math.Clamp((int)dGain, 1, 200);
if (VirtueHelper.IsHighestPath(Source, VirtueName.Honor))
{
Source.SendLocalizedMessage(1063228); // You cannot gain more Honor.
return;
}
var gainedPath = false;
if (VirtueHelper.Award(Source, VirtueName.Honor, gain, ref gainedPath))
{
if (gainedPath)
{
Source.SendLocalizedMessage(1063226); // You have gained a path in Honor!
}
else
{
Source.SendLocalizedMessage(1063225); // You have gained in Honor.
}
}
}
public bool CheckDistance() => true;
public void Cancel()
{
Source.SentHonorContext = null;
((IHonorTarget)Target).ReceivedHonorContext = null;
m_Timer.Stop();
}
private enum FirstHit
{
NotDelivered,
Delivered,
Granted
}
private class InternalTimer : Timer
{
private readonly HonorContext m_Context;
public InternalTimer(HonorContext context) : base(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0)) =>
m_Context = context;
protected override void OnTick()
{
m_Context.CheckDistance();
}
}
}
}