ModernUO/Projects/UOContent/Items/Misc/Bola.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

232 lines
7.4 KiB
C#

using System;
using Server.Mobiles;
using Server.Network;
using Server.Spells.Ninjitsu;
using Server.Targeting;
namespace Server.Items
{
public class Bola : Item
{
[Constructible]
public Bola(int amount = 1) : base(0x26AC)
{
Weight = 4.0;
Stackable = true;
Amount = amount;
}
public Bola(Serial serial) : base(serial)
{
}
public override void OnDoubleClick(Mobile from)
{
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1040019); // The bola must be in your pack to use it.
}
else if (!from.CanBeginAction<Bola>())
{
from.SendLocalizedMessage(1049624); // You have to wait a few moments before you can use another bola!
}
else if (from.Target is BolaTarget)
{
from.SendLocalizedMessage(1049631); // This bola is already being used.
}
else if (!HasFreeHands(from))
{
from.SendLocalizedMessage(1040015); // Your hands must be free to use this
}
else if (from.Mounted)
{
from.SendLocalizedMessage(1040016); // You cannot use this while riding a mount
}
else if (AnimalForm.UnderTransformation(from))
{
from.SendLocalizedMessage(1070902); // You can't use this while in an animal form!
}
else
{
EtherealMount.StopMounting(from);
from.Target = new BolaTarget(this);
from.LocalOverheadMessage(MessageType.Emote, 0x3B2, 1049632); // * You begin to swing the bola...*
from.NonlocalOverheadMessage(
MessageType.Emote,
0x3B2,
1049633,
from.Name
); // ~1_NAME~ begins to menacingly swing a bola...
}
}
private static void FinishThrow(Mobile from, Mobile to)
{
if (Core.AOS)
{
new Bola().MoveToWorld(to.Location, to.Map);
}
if (to is ChaosDragoon || to is ChaosDragoonElite)
{
from.SendLocalizedMessage(1042047); // You fail to knock the rider from its mount.
}
var mt = to.Mount;
if (mt != null && !(to is ChaosDragoon || to is ChaosDragoonElite))
{
mt.Rider = null;
}
if (to is PlayerMobile mobile)
{
if (AnimalForm.UnderTransformation(mobile))
{
mobile.SendLocalizedMessage(1114066, from.Name); // ~1_NAME~ knocked you out of animal form!
}
else if (mobile.Mounted)
{
mobile.SendLocalizedMessage(1040023); // You have been knocked off of your mount!
}
mobile.SetMountBlock(BlockMountType.Dazed, TimeSpan.FromSeconds(Core.ML ? 10 : 3), true);
}
if (Core.AOS) /* only failsafe, attacker should already be dismounted */
{
(from as PlayerMobile)?.SetMountBlock(
BlockMountType.BolaRecovery,
TimeSpan.FromSeconds(Core.ML ? 10 : 3),
true
);
}
to.Damage(1);
Timer.StartTimer(TimeSpan.FromSeconds(2.0), from.EndAction<Bola>);
}
private static bool HasFreeHands(Mobile from)
{
var one = from.FindItemOnLayer(Layer.OneHanded);
var two = from.FindItemOnLayer(Layer.TwoHanded);
if (Core.SE)
{
var pack = from.Backpack;
if (pack != null)
{
if (one?.Movable == true)
{
pack.DropItem(one);
one = null;
}
if (two?.Movable == true)
{
pack.DropItem(two);
two = null;
}
}
}
else if (Core.AOS)
{
if (one?.Movable == true)
{
from.AddToBackpack(one);
one = null;
}
if (two?.Movable == true)
{
from.AddToBackpack(two);
two = null;
}
}
return one == null && two == null;
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(0);
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
}
public class BolaTarget : Target
{
private readonly Bola m_Bola;
public BolaTarget(Bola bola) : base(8, false, TargetFlags.Harmful) => m_Bola = bola;
protected override void OnTarget(Mobile from, object obj)
{
if (m_Bola.Deleted)
{
return;
}
if (obj is Mobile to)
{
if (!m_Bola.IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1040019); // The bola must be in your pack to use it.
}
else if (!HasFreeHands(from))
{
from.SendLocalizedMessage(1040015); // Your hands must be free to use this
}
else if (from.Mounted)
{
from.SendLocalizedMessage(1040016); // You cannot use this while riding a mount
}
else if (AnimalForm.UnderTransformation(from))
{
from.SendLocalizedMessage(1070902); // You can't use this while in an animal form!
}
else if (!to.Mounted && !AnimalForm.UnderTransformation(to))
{
from.SendLocalizedMessage(1049628); // You have no reason to throw a bola at that.
}
else if (!from.CanBeHarmful(to))
{
}
else if (from.BeginAction<Bola>())
{
EtherealMount.StopMounting(from);
from.DoHarmful(to);
m_Bola.Consume();
from.Direction = from.GetDirectionTo(to);
from.Animate(11, 5, 1, true, false, 0);
from.MovingEffect(to, 0x26AC, 10, 0, false, false);
Timer.StartTimer(TimeSpan.FromSeconds(0.5), () => FinishThrow(from, to));
}
else
{
from.SendLocalizedMessage(
1049624
); // You have to wait a few moments before you can use another bola!
}
}
else
{
from.SendLocalizedMessage(1049629); // You cannot throw a bola at that.
}
}
}
}
}