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);`.
This commit is contained in:
Kamron Batman 2021-08-07 14:33:35 -07:00 • committed by GitHub
parent b4de631f96
commit 9b554f69b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
219 changed files with 1897 additions and 2241 deletions

View file

@ -12,8 +12,8 @@ namespace Server.Spells.Bushido
9002
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, Timer> m_RegenTable = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_RegenTable = new();
public Confidence(Mobile caster, Item scroll) : base(caster, scroll, m_Info)
{
@ -53,82 +53,72 @@ namespace Server.Spells.Bushido
public static void BeginConfidence(Mobile m)
{
m_Table.TryGetValue(m, out var timer);
timer?.Stop();
m_Table[m] = timer = new InternalTimer(m);
StopConfidenceTimer(m);
timer.Start();
Timer.StartTimer(TimeSpan.FromSeconds(30.0),
() =>
{
EndConfidence(m);
m.SendLocalizedMessage(1063116); // Your confidence wanes.
},
out var timerToken
);
m_Table[m] = timerToken;
}
private static bool StopConfidenceTimer(Mobile m)
{
if (m_Table.Remove(m, out var timerToken))
{
timerToken.Cancel();
return true;
}
return false;
}
public static void EndConfidence(Mobile m)
{
if (m_Table.Remove(m, out var timer))
if (StopConfidenceTimer(m))
{
timer.Stop();
OnEffectEnd(m, typeof(Confidence));
}
OnEffectEnd(m, typeof(Confidence));
}
public static bool IsRegenerating(Mobile m) => m_RegenTable.ContainsKey(m);
// TODO: Move this to a central regeneration so it actually works properly
public static void BeginRegenerating(Mobile m)
{
m_RegenTable.TryGetValue(m, out var timer);
timer?.Stop();
StopRegenerating(m);
m_RegenTable[m] = timer = new RegenTimer(m);
// RunUO says this goes for 5 seconds, but UOGuide says 4 seconds during normal regeneration
var hits = (15 + m.Skills.Bushido.Fixed * m.Skills.Bushido.Fixed / 57600) / 4;
timer.Start();
TimerExecutionToken timerToken = default;
Timer.StartTimer(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), 4,
() =>
{
// ReSharper disable once AccessToModifiedClosure
if (timerToken.RemainingCount == 0)
{
StopRegenerating(m);
}
m.Hits += hits;
},
out timerToken
);
m_RegenTable[m] = timerToken;
}
public static void StopRegenerating(Mobile m)
{
if (m_RegenTable.Remove(m, out var timer))
{
timer.Stop();
}
}
private class InternalTimer : Timer
{
private readonly Mobile m_Mobile;
public InternalTimer(Mobile m) : base(TimeSpan.FromSeconds(15.0))
{
m_Mobile = m;
}
protected override void OnTick()
{
EndConfidence(m_Mobile);
m_Mobile.SendLocalizedMessage(1063116); // Your confidence wanes.
}
}
private class RegenTimer : Timer
{
private readonly int m_Hits;
private readonly Mobile m_Mobile;
private int m_Ticks;
public RegenTimer(Mobile m) : base(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0))
{
m_Mobile = m;
m_Hits = 15 + m.Skills.Bushido.Fixed * m.Skills.Bushido.Fixed / 57600;
}
protected override void OnTick()
{
++m_Ticks;
if (m_Ticks >= 5)
{
m_Mobile.Hits += m_Hits - m_Hits * 4 / 5;
StopRegenerating(m_Mobile);
}
m_Mobile.Hits += m_Hits / 5;
timer.Cancel();
}
}
}

View file

@ -13,7 +13,7 @@ namespace Server.Spells.Bushido
9002
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public CounterAttack(Mobile caster, Item scroll) : base(caster, scroll, m_Info)
{
@ -73,28 +73,39 @@ namespace Server.Spells.Bushido
public static bool IsCountering(Mobile m) => m_Table.ContainsKey(m);
private static bool StopCounterTimer(Mobile m)
{
if (m_Table.Remove(m, out var timerToken))
{
timerToken.Cancel();
return true;
}
return false;
}
public static void StartCountering(Mobile m)
{
m_Table.TryGetValue(m, out var timer);
timer?.Stop();
StopCounterTimer(m);
m_Table[m] = Timer.DelayCall(TimeSpan.FromSeconds(30.0), EndCountering, m);
Timer.StartTimer(TimeSpan.FromSeconds(30.0),
() =>
{
StopCountering(m);
m.SendLocalizedMessage(1063119); // You return to your normal stance.
},
out var timerToken
);
m_Table[m] = timerToken;
}
public static void StopCountering(Mobile m)
{
if (m_Table.Remove(m, out var timer))
if (StopCounterTimer(m))
{
timer.Stop();
OnEffectEnd(m, typeof(CounterAttack));
}
OnEffectEnd(m, typeof(CounterAttack));
}
private static void EndCountering(Mobile m)
{
StopCountering(m);
m.SendLocalizedMessage(1063119); // You return to your normal stance.
}
}
}

View file

@ -13,7 +13,7 @@ namespace Server.Spells.Bushido
9002
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public Evasion(Mobile caster, Item scroll)
: base(caster, scroll, m_Info)
@ -133,7 +133,7 @@ namespace Server.Spells.Bushido
BeginEvasion(Caster);
Caster.BeginAction<Evasion>();
Timer.DelayCall(TimeSpan.FromSeconds(20.0), Caster.EndAction<Evasion>);
Timer.StartTimer(TimeSpan.FromSeconds(20.0), Caster.EndAction<Evasion>);
}
FinishSequence();
@ -206,37 +206,36 @@ namespace Server.Spells.Bushido
public static void BeginEvasion(Mobile m)
{
m_Table.TryGetValue(m, out var timer);
timer?.Stop();
StopEvasionTimer(m);
m_Table[m] = timer = new InternalTimer(m, GetEvadeDuration(m));
timer.Start();
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 (m_Table.Remove(m, out var timer))
if (StopEvasionTimer(m))
{
timer.Stop();
}
OnEffectEnd(m, typeof(Evasion));
}
private class InternalTimer : Timer
{
private readonly Mobile m_Mobile;
public InternalTimer(Mobile m, TimeSpan delay)
: base(delay)
{
m_Mobile = m;
}
protected override void OnTick()
{
EndEvasion(m_Mobile);
m_Mobile.SendLocalizedMessage(1063121); // You no longer feel that you could deflect any attack.
OnEffectEnd(m, typeof(Evasion));
}
}
}

View file

@ -27,6 +27,8 @@ namespace Server.Spells.Bushido
ClearCurrentMove(attacker);
RemovePenalty(attacker);
HonorableExecutionTimer timer;
if (!defender.Alive)
{
attacker.FixedParticles(0x373A, 1, 17, 0x7E2, EffectLayer.Waist);
@ -38,7 +40,7 @@ namespace Server.Spells.Bushido
var swingBonus = Math.Max(1, (int)(bushido / 720.0));
m_Table[attacker] = new HonorableExecutionTimer(attacker, swingBonus);
timer = new HonorableExecutionTimer(attacker, swingBonus);
}
else
{
@ -58,9 +60,12 @@ namespace Server.Spells.Bushido
mods.Add(new DefaultSkillMod(SkillName.MagicResist, true, -resSpells));
}
m_Table[attacker] = new HonorableExecutionTimer(attacker, mods);
timer = new HonorableExecutionTimer(attacker, mods);
}
m_Table[attacker] = timer;
timer.Start();
attacker.Delta(MobileDelta.WeaponDamage);
CheckGain(attacker);
}

View file

@ -47,18 +47,18 @@ namespace Server.Spells.Bushido
{
var args = $"{RequiredSkill:0.#}\t{CastSkill.ToString()}\t ";
Caster.SendLocalizedMessage(
1063013,
1063013, // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability.
args
); // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability.
);
return false;
}
if (Caster.Mana < mana)
{
Caster.SendLocalizedMessage(
1060174,
1060174, // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
mana.ToString()
); // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
);
return false;
}
@ -72,18 +72,18 @@ namespace Server.Spells.Bushido
if (Caster.Skills[CastSkill].Value < RequiredSkill)
{
Caster.SendLocalizedMessage(
1070768,
1070768, // You need ~1_SKILL_REQUIREMENT~ Bushido skill to perform that attack!
RequiredSkill.ToString("F1")
); // You need ~1_SKILL_REQUIREMENT~ Bushido skill to perform that attack!
);
return false;
}
if (Caster.Mana < mana)
{
Caster.SendLocalizedMessage(
1060174,
1060174, // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
mana.ToString()
); // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
);
return false;
}
@ -107,20 +107,9 @@ namespace Server.Spells.Bushido
public virtual void OnCastSuccessful(Mobile caster)
{
if (Evasion.IsEvading(caster))
{
Evasion.EndEvasion(caster);
}
if (Confidence.IsConfident(caster))
{
Confidence.EndConfidence(caster);
}
if (CounterAttack.IsCountering(caster))
{
CounterAttack.StopCountering(caster);
}
Evasion.EndEvasion(caster);
Confidence.EndConfidence(caster);
CounterAttack.StopCountering(caster);
var spellID = SpellRegistry.GetRegistryNumber(this);

View file

@ -12,7 +12,7 @@ namespace Server.Spells.Chivalry
9002
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public DivineFurySpell(Mobile caster, Item scroll = null) : base(caster, scroll, m_Info)
{
@ -37,12 +37,17 @@ namespace Server.Spells.Chivalry
Caster.Stam = Caster.StamMax;
m_Table.TryGetValue(Caster, out var timer);
timer?.Stop();
RemoveTimer(Caster);
var delay = Math.Clamp(ComputePowerValue(10), 7, 24);
m_Table[Caster] = Timer.DelayCall(TimeSpan.FromSeconds(delay), Expire_Callback, Caster);
Timer.StartTimer(TimeSpan.FromSeconds(delay),
() => { StopDivineFury(Caster); },
out var timerToken
);
m_Table[Caster] = timerToken;
Caster.Delta(MobileDelta.WeaponDamage);
BuffInfo.AddBuff(
@ -54,14 +59,21 @@ namespace Server.Spells.Chivalry
FinishSequence();
}
public static bool UnderEffect(Mobile m) => m_Table.ContainsKey(m);
private static void Expire_Callback(Mobile m)
private static void RemoveTimer(Mobile m)
{
m_Table.Remove(m);
if (m_Table.Remove(m, out var timerToken))
{
timerToken.Cancel();
}
}
public static void StopDivineFury(Mobile m)
{
RemoveTimer(m);
m.Delta(MobileDelta.WeaponDamage);
m.PlaySound(0xF8);
}
public static bool UnderEffect(Mobile m) => m_Table.ContainsKey(m);
}
}

View file

@ -13,7 +13,7 @@ namespace Server.Spells.Chivalry
9002
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public EnemyOfOneSpell(Mobile caster, Item scroll = null) : base(caster, scroll, m_Info)
{
@ -36,12 +36,13 @@ namespace Server.Spells.Chivalry
Caster.FixedParticles(0x375A, 1, 30, 9966, 33, 2, EffectLayer.Head);
Caster.FixedParticles(0x37B9, 1, 30, 9502, 43, 3, EffectLayer.Head);
m_Table.TryGetValue(Caster, out var timer);
timer?.Stop();
RemoveTimer(Caster);
var delay = Math.Clamp(ComputePowerValue(1) / 60.0, 1.5, 3.5);
m_Table[Caster] = Timer.DelayCall(TimeSpan.FromMinutes(delay), Expire_Callback, Caster);
Timer.StartTimer(TimeSpan.FromMinutes(delay), () => Expire_Callback(Caster), out var timerToken);
m_Table[Caster] = timerToken;
if (Caster is PlayerMobile mobile)
{
@ -58,9 +59,17 @@ namespace Server.Spells.Chivalry
FinishSequence();
}
private static void RemoveTimer(Mobile m)
{
if (m_Table.Remove(m, out var timerToken))
{
timerToken.Cancel();
}
}
private static void Expire_Callback(Mobile m)
{
m_Table.Remove(m);
RemoveTimer(m);
m.PlaySound(0x1F8);

View file

@ -19,7 +19,7 @@ namespace Server.Spells.Fifth
Reagent.Nightshade
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public IncognitoSpell(Mobile caster, Item scroll = null) : base(caster, scroll, m_Info)
{
@ -111,7 +111,8 @@ namespace Server.Spells.Fifth
var timeVal = Math.Min(6 * Caster.Skills.Magery.Fixed / 50 + 1, 144);
var length = TimeSpan.FromSeconds(timeVal);
m_Table[Caster] = Timer.DelayCall(length, EndIncognito, Caster);
Timer.StartTimer(length, () => EndIncognito(Caster), out var timerToken);
m_Table[Caster] = timerToken;
BuffInfo.AddBuff(Caster, new BuffInfo(BuffIcon.Incognito, 1075819, length, Caster));
}
@ -126,16 +127,15 @@ namespace Server.Spells.Fifth
public static void StopTimer(Mobile m)
{
if (m_Table.Remove(m, out var t))
if (m_Table.Remove(m, out var timerToken))
{
t.Stop();
timerToken.Cancel();
}
BuffInfo.RemoveBuff(m, BuffIcon.Incognito);
}
private static void EndIncognito(Mobile m)
public static void EndIncognito(Mobile m)
{
if (m.CanBeginAction<IncognitoSpell>())
{
@ -148,9 +148,10 @@ namespace Server.Spells.Fifth
m.HueMod = -1;
m.NameMod = null;
m.EndAction<IncognitoSpell>();
BaseArmor.ValidateMobile(m);
BaseClothing.ValidateMobile(m);
StopTimer(m);
}
}
}

View file

@ -51,13 +51,10 @@ namespace Server.Spells.Fifth
var damage = Math.Min((int)((Caster.Skills.Magery.Value + Caster.Int) / 5), 60);
Timer.DelayCall(
TimeSpan.FromSeconds(1.0),
AosDelay_Callback,
Caster,
target,
m,
damage
Timer.StartTimer(TimeSpan.FromSeconds(1.0), () =>
{
AosDelay_Callback(Caster, target, m, damage);
}
);
}
}

View file

@ -51,7 +51,7 @@ namespace Server.Spells.Fourth
{
var duration = SpellHelper.GetDuration(Caster, m);
m_UnderEffect.Add(m);
Timer.DelayCall(duration, RemoveEffect, m);
Timer.StartTimer(duration, () => RemoveEffect(m));
m.UpdateResistances();
}

View file

@ -62,7 +62,7 @@ namespace Server.Spells.Fourth
m.Mana -= toDrain;
m_Table.Add(m);
Timer.DelayCall(TimeSpan.FromSeconds(5.0), AosDelay_Callback, m, toDrain);
Timer.StartTimer(TimeSpan.FromSeconds(5.0), () => AosDelay_Callback(m, toDrain));
}
}
else

View file

@ -46,7 +46,7 @@ namespace Server.Spells.Mysticism
Caster.MovingParticles(m, 0x407A, 7, 0, false, true, 0, 0, 0xBBE, 0xFA6, 0xFFFF, 0);
Caster.PlaySound(0x2EE);
Timer.DelayCall(TimeSpan.FromSeconds(1.0), Damage, m);
Timer.StartTimer(TimeSpan.FromSeconds(1.0), () => Damage(m));
}
FinishSequence();

View file

@ -191,7 +191,7 @@ namespace Server.Spells.Necromancy
0
);
Timer.DelayCall(
Timer.StartTimer(
TimeSpan.FromSeconds(2.0),
() => SummonDelay_Callback(Caster, c, p, map, group)
);
@ -279,10 +279,10 @@ namespace Server.Spells.Necromancy
if (list.Count > 3)
{
Timer.DelayCall(list[0].Kill);
Timer.StartTimer(list[0].Kill);
}
Timer.DelayCall(TimeSpan.FromSeconds(2.0), TimeSpan.FromSeconds(2.0), Summoned_Damage, summoned);
Timer.StartTimer(TimeSpan.FromSeconds(2.0), TimeSpan.FromSeconds(2.0), () => Summoned_Damage(summoned));
}
private static void Summoned_Damage(Mobile mob)

View file

@ -66,7 +66,7 @@ namespace Server.Spells.Necromancy
var duration = TimeSpan.FromSeconds(Caster.Skills.SpiritSpeak.Value / 12 + 1.0);
Timer.DelayCall(duration, mob => TryEndEffect(mob), m);
Timer.StartTimer(duration, () => TryEndEffect(m));
HarmfulSpell(m);

View file

@ -486,9 +486,9 @@ namespace Server.Spells.Ninjitsu
if (mana > m_Caster.Mana)
{
m_Caster.SendLocalizedMessage(
1060174,
1060174, // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
mana.ToString()
); // You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
);
}
else if (m_Caster is PlayerMobile mobile && mobile.MountBlockReason != BlockMountType.None)
{
@ -560,51 +560,50 @@ namespace Server.Spells.Ninjitsu
{
AnimalForm.RemoveContext(m_Mobile, true);
Stop();
return;
}
else
if (m_Body == 0x115) // Cu Sidhe
{
if (m_Body == 0x115) // Cu Sidhe
if (m_Counter++ >= 8)
{
if (m_Counter++ >= 8)
if (m_Mobile.Hits < m_Mobile.HitsMax && m_Mobile.Backpack != null)
{
if (m_Mobile.Hits < m_Mobile.HitsMax && m_Mobile.Backpack != null)
var b = m_Mobile.Backpack.FindItemByType<Bandage>();
if (b != null)
{
var b = m_Mobile.Backpack.FindItemByType<Bandage>();
if (b != null)
{
m_Mobile.Hits += Utility.RandomMinMax(20, 50);
b.Consume();
}
m_Mobile.Hits += Utility.RandomMinMax(20, 50);
b.Consume();
}
m_Counter = 0;
}
m_Counter = 0;
}
else if (m_Body == 0x114) // Reptalon
}
else if (m_Body == 0x114) // Reptalon
{
if (m_Mobile.Combatant != null && m_Mobile.Combatant != m_LastTarget)
{
if (m_Mobile.Combatant != null && m_Mobile.Combatant != m_LastTarget)
m_Counter = 1;
m_LastTarget = m_Mobile.Combatant;
}
if (m_Mobile.Warmode && m_LastTarget?.Alive == true && m_LastTarget?.Deleted != true &&
m_Counter-- <= 0)
{
if (m_Mobile.CanBeHarmful(m_LastTarget) && m_LastTarget.Map == m_Mobile.Map &&
m_LastTarget.InRange(m_Mobile.Location, BaseCreature.DefaultRangePerception) &&
m_Mobile.InLOS(m_LastTarget))
{
m_Counter = 1;
m_LastTarget = m_Mobile.Combatant;
m_Mobile.Direction = m_Mobile.GetDirectionTo(m_LastTarget);
m_Mobile.Freeze(TimeSpan.FromSeconds(1));
m_Mobile.PlaySound(0x16A);
StartTimer(TimeSpan.FromSeconds(1.3), () => BreathEffect_Callback(m_LastTarget));
}
if (m_Mobile.Warmode && m_LastTarget?.Alive == true && m_LastTarget?.Deleted != true &&
m_Counter-- <= 0)
{
if (m_Mobile.CanBeHarmful(m_LastTarget) && m_LastTarget.Map == m_Mobile.Map &&
m_LastTarget.InRange(m_Mobile.Location, BaseCreature.DefaultRangePerception) &&
m_Mobile.InLOS(m_LastTarget))
{
m_Mobile.Direction = m_Mobile.GetDirectionTo(m_LastTarget);
m_Mobile.Freeze(TimeSpan.FromSeconds(1));
m_Mobile.PlaySound(0x16A);
DelayCall(TimeSpan.FromSeconds(1.3), BreathEffect_Callback, m_LastTarget);
}
m_Counter = Math.Min((int)m_Mobile.GetDistanceToSqrt(m_LastTarget), 10);
}
m_Counter = Math.Min((int)m_Mobile.GetDistanceToSqrt(m_LastTarget), 10);
}
}
}
@ -617,7 +616,7 @@ namespace Server.Spells.Ninjitsu
m_Mobile.PlaySound(0x227);
Effects.SendMovingEffect(m_Mobile, target, 0x36D4, 5, 0);
DelayCall(TimeSpan.FromSeconds(1), BreathDamage_Callback, target);
StartTimer(TimeSpan.FromSeconds(1), () => BreathDamage_Callback(target));
}
}

View file

@ -38,7 +38,7 @@ namespace Server.Spells.Ninjitsu
if (valid)
{
attacker.BeginAction<Stealth>();
Timer.DelayCall(TimeSpan.FromSeconds(5.0), attacker.EndAction<Stealth>);
Timer.StartTimer(TimeSpan.FromSeconds(5.0), attacker.EndAction<Stealth>);
}
return valid;

View file

@ -34,7 +34,7 @@ namespace Server.Spells.Ninjitsu
if (valid)
{
attacker.BeginAction<Stealth>();
Timer.DelayCall(TimeSpan.FromSeconds(5.0), attacker.EndAction<Stealth>);
Timer.StartTimer(TimeSpan.FromSeconds(5.0), attacker.EndAction<Stealth>);
}
return valid;
@ -53,17 +53,14 @@ namespace Server.Spells.Ninjitsu
attacker.RevealingAction();
if (m_Table.Remove(defender, out var info))
{
info.m_Timer?.Stop();
}
StopTimer(defender);
var ninjitsu = attacker.Skills.Ninjitsu.Fixed;
var malus = ninjitsu / 60 + (int)Tracking.GetStalkingBonus(attacker, defender);
info = new SurpriseAttackInfo(defender, malus);
info.m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(8.0), EndSurprise, info);
var info = new SurpriseAttackInfo(defender, malus);
Timer.StartTimer(TimeSpan.FromSeconds(8.0), () => EndSurprise(info), out info._timerToken);
m_Table[defender] = info;
@ -90,19 +87,25 @@ namespace Server.Spells.Ninjitsu
return true;
}
private static void StopTimer(Mobile m)
{
if (m_Table.Remove(m, out var info))
{
info._timerToken.Cancel();
}
}
private static void EndSurprise(SurpriseAttackInfo info)
{
info.m_Timer?.Stop();
StopTimer(info.m_Target);
info.m_Target.SendLocalizedMessage(1063131); // Your defenses have returned to normal.
m_Table.Remove(info.m_Target);
}
private class SurpriseAttackInfo
{
public readonly int m_Malus;
public readonly Mobile m_Target;
public Timer m_Timer;
public TimerExecutionToken _timerToken;
public SurpriseAttackInfo(Mobile target, int effect)
{

View file

@ -19,7 +19,7 @@ namespace Server.Spells.Seventh
Reagent.MandrakeRoot
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
private readonly int m_NewBody;
@ -158,14 +158,7 @@ namespace Server.Spells.Seventh
caster.BodyMod = m_NewBody;
if (m_NewBody == 400 || m_NewBody == 401)
{
caster.HueMod = caster.Race.RandomSkinHue();
}
else
{
caster.HueMod = 0;
}
caster.HueMod = m_NewBody is 400 or 401 ? caster.Race.RandomSkinHue() : 0;
BaseArmor.ValidateMobile(caster);
BaseClothing.ValidateMobile(caster);
@ -176,7 +169,8 @@ namespace Server.Spells.Seventh
var duration = Math.Max((int)caster.Skills.Magery.Value, 120);
m_Table[caster] = Timer.DelayCall(TimeSpan.FromSeconds(duration), EndPolymorph, caster);
Timer.StartTimer(TimeSpan.FromSeconds(duration), () => EndPolymorph(caster), out var timerToken);
m_Table[caster] = timerToken;
}
}
}
@ -193,11 +187,11 @@ namespace Server.Spells.Seventh
{
if (m_Table.Remove(m, out var timer))
{
timer.Stop();
timer.Cancel();
}
}
private static void EndPolymorph(Mobile m)
public static void EndPolymorph(Mobile m)
{
if (m.CanBeginAction<PolymorphSpell>())
{
@ -206,10 +200,13 @@ namespace Server.Spells.Seventh
m.BodyMod = 0;
m.HueMod = -1;
m.NameMod = null;
m.EndAction<PolymorphSpell>();
BaseArmor.ValidateMobile(m);
BaseClothing.ValidateMobile(m);
StopTimer(m);
}
}
}

View file

@ -18,7 +18,7 @@ namespace Server.Spells.Sixth
Reagent.Nightshade
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public InvisibilitySpell(Mobile caster, Item scroll = null) : base(caster, scroll, m_Info)
{
@ -58,25 +58,28 @@ namespace Server.Spells.Sixth
m.Combatant = null;
m.Warmode = false;
RemoveTimer(m);
StopTimer(m);
var duration = TimeSpan.FromSeconds(1.2 * Caster.Skills.Magery.Fixed / 10);
BuffInfo.RemoveBuff(m, BuffIcon.HidingAndOrStealth);
BuffInfo.AddBuff(m, new BuffInfo(BuffIcon.Invisibility, 1075825, duration, m)); // Invisibility/Invisible
m_Table[m] = Timer.DelayCall(duration, EndInvisiblity, m);
Timer.StartTimer(duration,
() =>
{
m.RevealingAction();
StopTimer(m);
},
out var timerToken
);
m_Table[m] = timerToken;
}
FinishSequence();
}
private static void EndInvisiblity(Mobile m)
{
m.RevealingAction();
RemoveTimer(m);
}
public override bool CheckCast()
{
if (DuelContext.CheckSuddenDeath(Caster))
@ -95,11 +98,11 @@ namespace Server.Spells.Sixth
public static bool HasTimer(Mobile m) => m_Table.ContainsKey(m);
public static void RemoveTimer(Mobile m)
public static void StopTimer(Mobile m)
{
if (m_Table.Remove(m, out var t))
if (m_Table.Remove(m, out var timerToken))
{
t.Stop();
timerToken.Cancel();
}
}
}

View file

@ -131,7 +131,7 @@ namespace Server.Spells.Spellweaving
m_Table.Remove(m_Mobile);
DelayCall(TimeSpan.FromSeconds(120), m_Mobile.EndAction<AttuneWeaponSpell>);
StartTimer(TimeSpan.FromSeconds(120), m_Mobile.EndAction<AttuneWeaponSpell>);
BuffInfo.RemoveBuff(m_Mobile, BuffIcon.AttuneWeapon);
}
}

View file

@ -67,7 +67,7 @@ namespace Server.Spells.Spellweaving
var duration = TimeSpan.FromSeconds(12 + (int)(skill / 24) + FocusLevel * 2);
Timer.DelayCall(duration, RemoveEffect, Caster);
Timer.StartTimer(duration, () => RemoveEffect(Caster));
Caster.BeginAction(
typeof(EtherealVoyageSpell)
@ -82,7 +82,7 @@ namespace Server.Spells.Spellweaving
TransformationSpellHelper.RemoveContext(m, true);
Timer.DelayCall(TimeSpan.FromMinutes(5), m.EndAction<EtherealVoyageSpell>);
Timer.StartTimer(TimeSpan.FromMinutes(5), m.EndAction<EtherealVoyageSpell>);
BuffInfo.RemoveBuff(m, BuffIcon.EtherealVoyage);
}

View file

@ -95,7 +95,7 @@ namespace Server.Spells.Spellweaving
{
if (m_Table.ContainsKey(m))
{
Timer.DelayCall(TimeSpan.FromSeconds(Utility.RandomMinMax(2, 4)), HandleDeath_OnCallback, m);
Timer.StartTimer(TimeSpan.FromSeconds(Utility.RandomMinMax(2, 4)), () => HandleDeath_OnCallback(m));
}
}

View file

@ -91,7 +91,7 @@ namespace Server.Spells.Spellweaving
if (m_Table.Remove(m, out var timer))
{
timer.Stop();
Timer.DelayCall(TimeSpan.FromSeconds(60), timer.m_Caster.EndAction<GiftOfRenewalSpell>);
Timer.StartTimer(TimeSpan.FromSeconds(60), timer.m_Caster.EndAction<GiftOfRenewalSpell>);
return true;
}

View file

@ -75,7 +75,7 @@ namespace Server.Spells.Spellweaving
{
timer.Stop();
Timer.DelayCall(TimeSpan.FromSeconds(0.25), FinishEffect, target, timer);
Timer.StartTimer(TimeSpan.FromSeconds(0.25), () => FinishEffect(target, timer));
}
}

View file

@ -4,7 +4,7 @@ namespace Server.Items
{
public class TransientItem : Item
{
private Timer m_Timer;
private TimerExecutionToken _timerToken;
[Constructible]
public TransientItem(int itemID, TimeSpan lifeSpan)
@ -13,7 +13,7 @@ namespace Server.Items
CreationTime = Core.Now;
LifeSpan = lifeSpan;
m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckExpiry);
Timer.StartTimer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckExpiry, out _timerToken);
}
public TransientItem(Serial serial)
@ -60,8 +60,7 @@ namespace Server.Items
public override void OnDelete()
{
m_Timer?.Stop();
_timerToken.Cancel();
base.OnDelete();
}
@ -103,7 +102,7 @@ namespace Server.Items
LifeSpan = reader.ReadTimeSpan();
CreationTime = reader.ReadDateTime();
m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckExpiry);
Timer.StartTimer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckExpiry, out _timerToken);
}
}
}

View file

@ -55,7 +55,7 @@ namespace Server.Mobiles
public override void MoveToWorld(Point3D loc, Map map)
{
base.MoveToWorld(loc, map);
Timer.DelayCall(DoEffects);
Timer.StartTimer(DoEffects);
}
public void DoEffects()
@ -66,7 +66,7 @@ namespace Server.Mobiles
if (Alive && !Deleted)
{
Timer.DelayCall(TimeSpan.FromSeconds(7.0), DoEffects);
Timer.StartTimer(TimeSpan.FromSeconds(7.0), DoEffects);
}
}

View file

@ -11,7 +11,7 @@ namespace Server.Spells.Spellweaving
-1
);
private static readonly Dictionary<Mobile, Timer> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
public ThunderstormSpell(Mobile caster, Item scroll = null)
: base(caster, scroll, m_Info)
@ -63,7 +63,10 @@ namespace Server.Spells.Spellweaving
continue;
}
m_Table[m] = Timer.DelayCall(duration, DoExpire, m);
StopTimer(m);
Timer.StartTimer(duration, () => DoExpire(m), out var timerToken);
m_Table[m] = timerToken;
BuffInfo.AddBuff(
m,
@ -79,15 +82,17 @@ namespace Server.Spells.Spellweaving
public static int GetCastRecoveryMalus(Mobile m) => m_Table.ContainsKey(m) ? 6 : 0;
private static void StopTimer(Mobile m)
{
if (m_Table.Remove(m, out var timerToken))
{
timerToken.Cancel();
}
}
public static void DoExpire(Mobile m)
{
if (!m_Table.Remove(m, out var t))
{
return;
}
t.Stop();
StopTimer(m);
BuffInfo.RemoveBuff(m, BuffIcon.Thunderstorm);
}
}