### 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);`.
456 lines
11 KiB
C#
456 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Server.Items;
|
|
using Server.Utilities;
|
|
|
|
namespace Server.Engines.Doom
|
|
{
|
|
public enum GauntletSpawnerState
|
|
{
|
|
InSequence,
|
|
InProgress,
|
|
Completed
|
|
}
|
|
|
|
public class GauntletSpawner : Item
|
|
{
|
|
public const int PlayersPerSpawn = 5;
|
|
|
|
public const int InSequenceItemHue = 0x000;
|
|
public const int InProgressItemHue = 0x676;
|
|
public const int CompletedItemHue = 0x455;
|
|
|
|
private GauntletSpawnerState m_State;
|
|
|
|
private TimerExecutionToken _timerToken;
|
|
|
|
[Constructible]
|
|
public GauntletSpawner(string typeName = null) : base(0x36FE)
|
|
{
|
|
Visible = false;
|
|
Movable = false;
|
|
|
|
TypeName = typeName;
|
|
Creatures = new List<Mobile>();
|
|
Traps = new List<BaseTrap>();
|
|
}
|
|
|
|
public GauntletSpawner(Serial serial) : base(serial)
|
|
{
|
|
}
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public string TypeName { get; set; }
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public BaseDoor Door { get; set; }
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public BaseAddon Addon { get; set; }
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public GauntletSpawner Sequence { get; set; }
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public bool HasCompleted
|
|
{
|
|
get
|
|
{
|
|
if (Creatures.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (var i = 0; i < Creatures.Count; ++i)
|
|
{
|
|
var mob = Creatures[i];
|
|
|
|
if (!mob.Deleted)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public Rectangle2D RegionBounds { get; set; }
|
|
|
|
[CommandProperty(AccessLevel.GameMaster)]
|
|
public GauntletSpawnerState State
|
|
{
|
|
get => m_State;
|
|
set
|
|
{
|
|
if (m_State == value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_State = value;
|
|
|
|
var hue = 0;
|
|
var lockDoors = m_State == GauntletSpawnerState.InProgress;
|
|
|
|
hue = m_State switch
|
|
{
|
|
GauntletSpawnerState.InSequence => InSequenceItemHue,
|
|
GauntletSpawnerState.InProgress => InProgressItemHue,
|
|
GauntletSpawnerState.Completed => CompletedItemHue,
|
|
_ => hue
|
|
};
|
|
|
|
if (Door != null)
|
|
{
|
|
Door.Hue = hue;
|
|
Door.Locked = lockDoors;
|
|
|
|
if (lockDoors)
|
|
{
|
|
Door.KeyValue = Key.RandomValue();
|
|
Door.Open = false;
|
|
}
|
|
|
|
if (Door.Link != null)
|
|
{
|
|
Door.Link.Hue = hue;
|
|
Door.Link.Locked = lockDoors;
|
|
|
|
if (lockDoors)
|
|
{
|
|
Door.Link.KeyValue = Key.RandomValue();
|
|
Door.Open = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Addon != null)
|
|
{
|
|
Addon.Hue = hue;
|
|
}
|
|
|
|
if (m_State == GauntletSpawnerState.InProgress)
|
|
{
|
|
CreateRegion();
|
|
FullSpawn();
|
|
|
|
Timer.StartTimer(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), Slice, out _timerToken);
|
|
}
|
|
else
|
|
{
|
|
Stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<Mobile> Creatures { get; set; }
|
|
|
|
public List<BaseTrap> Traps { get; set; }
|
|
|
|
public Region Region { get; set; }
|
|
|
|
public override string DefaultName => "doom spawner";
|
|
|
|
public virtual void CreateRegion()
|
|
{
|
|
if (Region != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var map = Map;
|
|
|
|
if (map == null || map == Map.Internal)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Region = new GauntletRegion(this, map);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
ClearCreatures();
|
|
ClearTraps();
|
|
DestroyRegion();
|
|
|
|
_timerToken.Cancel();
|
|
}
|
|
|
|
public override void OnDelete()
|
|
{
|
|
base.OnDelete();
|
|
Stop();
|
|
|
|
Door?.Link?.Delete();
|
|
Door?.Delete();
|
|
Addon?.Delete();
|
|
}
|
|
|
|
public virtual void DestroyRegion()
|
|
{
|
|
Region?.Unregister();
|
|
|
|
Region = null;
|
|
}
|
|
|
|
public virtual int ComputeTrapCount()
|
|
{
|
|
var area = RegionBounds.Width * RegionBounds.Height;
|
|
|
|
return area / 100;
|
|
}
|
|
|
|
public virtual void ClearTraps()
|
|
{
|
|
for (var i = 0; i < Traps.Count; ++i)
|
|
{
|
|
Traps[i].Delete();
|
|
}
|
|
|
|
Traps.Clear();
|
|
}
|
|
|
|
public virtual void SpawnTrap()
|
|
{
|
|
var map = Map;
|
|
|
|
if (map == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
BaseTrap trap;
|
|
|
|
var random = Utility.Random(100);
|
|
|
|
trap = random switch
|
|
{
|
|
< 22 => new SawTrap(Utility.RandomBool() ? SawTrapType.WestFloor : SawTrapType.NorthFloor),
|
|
< 44 => new SpikeTrap(Utility.RandomBool() ? SpikeTrapType.WestFloor : SpikeTrapType.NorthFloor),
|
|
< 66 => new GasTrap(Utility.RandomBool() ? GasTrapType.NorthWall : GasTrapType.WestWall),
|
|
< 88 => new FireColumnTrap(),
|
|
_ => new MushroomTrap()
|
|
};
|
|
|
|
if (trap is FireColumnTrap || trap is MushroomTrap)
|
|
{
|
|
trap.Hue = 0x451;
|
|
}
|
|
|
|
// try 10 times to find a valid location
|
|
for (var i = 0; i < 10; ++i)
|
|
{
|
|
var x = Utility.Random(RegionBounds.X, RegionBounds.Width);
|
|
var y = Utility.Random(RegionBounds.Y, RegionBounds.Height);
|
|
var z = Z;
|
|
|
|
if (!map.CanFit(x, y, z, 16, false, false))
|
|
{
|
|
z = map.GetAverageZ(x, y);
|
|
}
|
|
|
|
if (!map.CanFit(x, y, z, 16, false, false))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
trap.MoveToWorld(new Point3D(x, y, z), map);
|
|
Traps.Add(trap);
|
|
|
|
return;
|
|
}
|
|
|
|
trap.Delete();
|
|
}
|
|
|
|
public virtual int ComputeSpawnCount()
|
|
{
|
|
var playerCount = 0;
|
|
|
|
var map = Map;
|
|
|
|
if (map != null)
|
|
{
|
|
var loc = GetWorldLocation();
|
|
|
|
var reg = Region.Find(loc, map).GetRegion("Doom Gauntlet");
|
|
|
|
if (reg != null)
|
|
{
|
|
playerCount = reg.GetPlayerCount();
|
|
}
|
|
}
|
|
|
|
if (playerCount == 0 && Region != null)
|
|
{
|
|
playerCount = Region.GetPlayerCount();
|
|
}
|
|
|
|
return Math.Max((playerCount + PlayersPerSpawn - 1) / PlayersPerSpawn, 1);
|
|
}
|
|
|
|
public virtual void ClearCreatures()
|
|
{
|
|
for (var i = 0; i < Creatures.Count; ++i)
|
|
{
|
|
Creatures[i].Delete();
|
|
}
|
|
|
|
Creatures.Clear();
|
|
}
|
|
|
|
public virtual void FullSpawn()
|
|
{
|
|
ClearCreatures();
|
|
|
|
var count = ComputeSpawnCount();
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
{
|
|
Spawn();
|
|
}
|
|
|
|
ClearTraps();
|
|
|
|
count = ComputeTrapCount();
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
{
|
|
SpawnTrap();
|
|
}
|
|
}
|
|
|
|
public virtual void Spawn()
|
|
{
|
|
try
|
|
{
|
|
if (TypeName == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var type = AssemblyHandler.FindTypeByName(TypeName);
|
|
|
|
if (type == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var mob = type.CreateEntityInstance<Mobile>();
|
|
|
|
if (mob != null)
|
|
{
|
|
mob.MoveToWorld(GetWorldLocation(), Map);
|
|
Creatures.Add(mob);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
public virtual void RecurseReset()
|
|
{
|
|
if (m_State != GauntletSpawnerState.InSequence)
|
|
{
|
|
State = GauntletSpawnerState.InSequence;
|
|
|
|
if (Sequence?.Deleted == false)
|
|
{
|
|
Sequence.RecurseReset();
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void Slice()
|
|
{
|
|
if (m_State != GauntletSpawnerState.InProgress)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var count = ComputeSpawnCount();
|
|
|
|
for (var i = Creatures.Count; i < count; ++i)
|
|
{
|
|
Spawn();
|
|
}
|
|
|
|
if (HasCompleted)
|
|
{
|
|
State = GauntletSpawnerState.Completed;
|
|
|
|
if (Sequence?.Deleted == false)
|
|
{
|
|
if (Sequence.State == GauntletSpawnerState.Completed)
|
|
{
|
|
RecurseReset();
|
|
}
|
|
|
|
Sequence.State = GauntletSpawnerState.InProgress;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Serialize(IGenericWriter writer)
|
|
{
|
|
base.Serialize(writer);
|
|
|
|
writer.Write(1); // version
|
|
|
|
writer.Write(RegionBounds);
|
|
|
|
writer.Write(Traps);
|
|
|
|
writer.Write(Creatures);
|
|
|
|
writer.Write(TypeName);
|
|
writer.Write(Door);
|
|
writer.Write(Addon);
|
|
writer.Write(Sequence);
|
|
|
|
writer.Write((int)m_State);
|
|
}
|
|
|
|
public override void Deserialize(IGenericReader reader)
|
|
{
|
|
base.Deserialize(reader);
|
|
|
|
var version = reader.ReadInt();
|
|
|
|
switch (version)
|
|
{
|
|
case 1:
|
|
{
|
|
RegionBounds = reader.ReadRect2D();
|
|
Traps = reader.ReadEntityList<BaseTrap>();
|
|
|
|
goto case 0;
|
|
}
|
|
case 0:
|
|
{
|
|
if (version < 1)
|
|
{
|
|
Traps = new List<BaseTrap>();
|
|
RegionBounds = new Rectangle2D(X - 40, Y - 40, 80, 80);
|
|
}
|
|
|
|
Creatures = reader.ReadEntityList<Mobile>();
|
|
|
|
TypeName = reader.ReadString();
|
|
Door = reader.ReadEntity<BaseDoor>();
|
|
Addon = reader.ReadEntity<BaseAddon>();
|
|
Sequence = reader.ReadEntity<GauntletSpawner>();
|
|
|
|
State = (GauntletSpawnerState)reader.ReadInt();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|