### 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);`.
242 lines
7.1 KiB
C#
242 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Server.Items;
|
|
|
|
namespace Server.Spells.Bushido
|
|
{
|
|
public class Evasion : SamuraiSpell
|
|
{
|
|
private static readonly SpellInfo m_Info = new(
|
|
"Evasion",
|
|
null,
|
|
-1,
|
|
9002
|
|
);
|
|
|
|
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
|
|
|
|
public Evasion(Mobile caster, Item scroll)
|
|
: base(caster, scroll, m_Info)
|
|
{
|
|
}
|
|
|
|
public override TimeSpan CastDelayBase => TimeSpan.FromSeconds(0.25);
|
|
|
|
public override double RequiredSkill => 60.0;
|
|
public override int RequiredMana => 10;
|
|
|
|
public override bool CheckCast() => VerifyCast(Caster, true) && base.CheckCast();
|
|
|
|
public static bool VerifyCast(Mobile caster, bool messages)
|
|
{
|
|
if (caster == null) // Sanity
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!(caster.FindItemOnLayer(Layer.OneHanded) is BaseWeapon weap))
|
|
{
|
|
weap = caster.FindItemOnLayer(Layer.TwoHanded) as BaseWeapon;
|
|
}
|
|
|
|
if (weap != null)
|
|
{
|
|
if (Core.ML && caster.Skills[weap.Skill].Base < 50)
|
|
{
|
|
if (messages)
|
|
{
|
|
caster.SendLocalizedMessage(
|
|
1076206
|
|
); // Your skill with your equipped weapon must be 50 or higher to use Evasion.
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
else if (!(caster.FindItemOnLayer(Layer.TwoHanded) is BaseShield))
|
|
{
|
|
if (messages)
|
|
{
|
|
caster.SendLocalizedMessage(1062944); // You must have a weapon or a shield equipped to use this ability!
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!caster.CanBeginAction<Evasion>())
|
|
{
|
|
if (messages)
|
|
{
|
|
caster.SendLocalizedMessage(501789); // You must wait before trying again.
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool CheckSpellEvasion(Mobile defender)
|
|
{
|
|
if (!(defender.FindItemOnLayer(Layer.OneHanded) is BaseWeapon weap))
|
|
{
|
|
weap = defender.FindItemOnLayer(Layer.TwoHanded) as BaseWeapon;
|
|
}
|
|
|
|
if (Core.ML)
|
|
{
|
|
if (defender.Spell?.IsCasting == true)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (weap != null)
|
|
{
|
|
if (defender.Skills[weap.Skill].Base < 50)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else if (!(defender.FindItemOnLayer(Layer.TwoHanded) is BaseShield))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (IsEvading(defender) && BaseWeapon.CheckParry(defender))
|
|
{
|
|
defender.Emote("*evades*"); // Yes. Eew. Blame OSI.
|
|
defender.FixedEffect(0x37B9, 10, 16);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override void OnBeginCast()
|
|
{
|
|
base.OnBeginCast();
|
|
|
|
Caster.FixedEffect(0x37C4, 10, 7, 4, 3);
|
|
}
|
|
|
|
public override void OnCast()
|
|
{
|
|
if (CheckSequence())
|
|
{
|
|
Caster.SendLocalizedMessage(1063120); // You feel that you might be able to deflect any attack!
|
|
Caster.FixedParticles(0x376A, 1, 20, 0x7F5, 0x960, 3, EffectLayer.Waist);
|
|
Caster.PlaySound(0x51B);
|
|
|
|
OnCastSuccessful(Caster);
|
|
|
|
BeginEvasion(Caster);
|
|
|
|
Caster.BeginAction<Evasion>();
|
|
Timer.StartTimer(TimeSpan.FromSeconds(20.0), Caster.EndAction<Evasion>);
|
|
}
|
|
|
|
FinishSequence();
|
|
}
|
|
|
|
public static bool IsEvading(Mobile m) => m_Table.ContainsKey(m);
|
|
|
|
public static TimeSpan GetEvadeDuration(Mobile m)
|
|
{
|
|
/* Evasion duration now scales with Bushido skill
|
|
*
|
|
* If the player has higher than GM Bushido, and GM Tactics and Anatomy, they get a 1 second bonus
|
|
* Evasion duration range:
|
|
* o 3-6 seconds w/o tactics/anatomy
|
|
* o 6-7 seconds w/ GM+ Bushido and GM tactics/anatomy
|
|
*/
|
|
|
|
if (!Core.ML)
|
|
{
|
|
return TimeSpan.FromSeconds(8.0);
|
|
}
|
|
|
|
double seconds = 3;
|
|
|
|
if (m.Skills.Bushido.Value > 60)
|
|
{
|
|
seconds += (m.Skills.Bushido.Value - 60) / 20;
|
|
}
|
|
|
|
if (m.Skills.Anatomy.Value >= 100.0 && m.Skills.Tactics.Value >= 100.0 && m.Skills.Bushido.Value > 100.0
|
|
) // Bushido being HIGHER than 100 for bonus is intended
|
|
{
|
|
seconds++;
|
|
}
|
|
|
|
return TimeSpan.FromSeconds((int)seconds);
|
|
}
|
|
|
|
public static double GetParryScalar(Mobile m)
|
|
{
|
|
/* Evasion modifier to parry now scales with Bushido skill
|
|
*
|
|
* If the player has higher than GM Bushido, and at least GM Tactics and Anatomy, they get a bonus to their evasion modifier (10% bonus to the evasion modifier to parry NOT 10% to the final parry chance)
|
|
*
|
|
* Bonus modifier to parry range: (these are the ranges for the evasion modifier)
|
|
* o 16-40% bonus w/o tactics/anatomy
|
|
* o 42-50% bonus w/ GM+ bushido and GM tactics/anatomy
|
|
*/
|
|
|
|
if (!Core.ML)
|
|
{
|
|
return 1.5;
|
|
}
|
|
|
|
double bonus = 0;
|
|
|
|
if (m.Skills.Bushido.Value >= 60)
|
|
{
|
|
bonus += (m.Skills.Bushido.Value - 60) * .004 + 0.16;
|
|
}
|
|
|
|
if (m.Skills.Anatomy.Value >= 100 && m.Skills.Tactics.Value >= 100 && m.Skills.Bushido.Value > 100
|
|
) // Bushido being HIGHER than 100 for bonus is intended
|
|
{
|
|
bonus += 0.10;
|
|
}
|
|
|
|
return 1.0 + bonus;
|
|
}
|
|
|
|
public static void BeginEvasion(Mobile m)
|
|
{
|
|
StopEvasionTimer(m);
|
|
|
|
Timer.StartTimer(GetEvadeDuration(m),
|
|
() =>
|
|
{
|
|
EndEvasion(m);
|
|
m.SendLocalizedMessage(1063121); // You no longer feel that you could deflect any attack.
|
|
},
|
|
out var timerToken
|
|
);
|
|
|
|
m_Table[m] = timerToken;
|
|
}
|
|
|
|
private static bool StopEvasionTimer(Mobile m)
|
|
{
|
|
if (m_Table.Remove(m, out var timer))
|
|
{
|
|
timer.Cancel();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static void EndEvasion(Mobile m)
|
|
{
|
|
if (StopEvasionTimer(m))
|
|
{
|
|
OnEffectEnd(m, typeof(Evasion));
|
|
}
|
|
}
|
|
}
|
|
}
|