fix: Cleans up LINQ calls. (#965)

- [X] Removes several `ToList()` uses with `PooledRefQueue`
- [X] Adds a `PeekRandom` to PooledRefQueue
- [X] Updates EV/BS so they dispel each other in a more efficient manner.
- [X] Fixes Firebomb so it works like a normal firefield.
- [X] Fixes field spells so they aren't unnecessarily using a Point3D ref more than necessary.
- [X] Removes extra allocation in campfire by using reverse loop.
- [X] Removes other LINQ calls that aren't needed.
This commit is contained in:
Kamron Batman 2022-03-20 19:20:54 -07:00 • committed by GitHub
parent daf89686d1
commit 23532db603
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 585 additions and 447 deletions

View file

@ -180,6 +180,22 @@ namespace Server.Collections
return _array[_head];
}
public T PeekRandom()
{
if (_size == 0)
{
ThrowForEmptyQueue();
}
var index = _head + Utility.Random(_size);
if (index >= _array.Length)
{
index -= _array.Length;
}
return _array[index];
}
public bool TryPeek([MaybeNullWhen(false)] out T result)
{
if (_size == 0)

View file

@ -17,10 +17,14 @@ namespace Server.Random
{
public static class RandomSources
{
private static IRandomSource m_Source;
private static IRandomSource m_SecureSource;
private static IRandomSource _source;
private static IRandomSource _secureSource;
public static IRandomSource Source => m_Source ??= new Xoshiro256PlusPlus();
public static IRandomSource SecureSource => m_SecureSource ??= new SecureRandom();
public static IRandomSource Source => _source ??= new Xoshiro256PlusPlus();
public static IRandomSource SecureSource => _secureSource ??= new SecureRandom();
public static void SetRng(IRandomSource newSource) => _source = newSource;
public static void SetSecureRng(IRandomSource newSource) => _secureSource = newSource;
}
}

View file

@ -920,6 +920,7 @@ namespace Server
return total + bonus;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Shuffle<T>(this IList<T> list)
{
var count = list.Count;
@ -930,6 +931,7 @@ namespace Server
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Shuffle<T>(this Span<T> list)
{
var count = list.Length;
@ -1094,6 +1096,15 @@ namespace Server
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double RandomDouble() => RandomSources.Source.NextDouble();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point3D RandomPointIn(Rectangle2D rect, Map map)
{
var x = Random(rect.X, rect.Width);
var y = Random(rect.Y, rect.Height);
return new Point3D(x, y, map.GetAverageZ(x, y));
}
/// <summary>
/// Random pink, blue, green, orange, red or yellow hue
/// </summary>