* Update RobustToolbox * Transition direct type usages * More updates * Fix invalid use of to map * Update RobustToolbox * Fix dropping items * Rename name usages of "GridCoordinates" to "EntityCoordinates" * Revert "Update RobustToolbox" This reverts commit 9f334a17c5908ded0043a63158bb671e4aa3f346. * Revert "Update RobustToolbox" This reverts commit 3a9c8cfa3606fa501aa84407796d2ad920853a09. # Conflicts: # RobustToolbox * Fix cursed IMapGrid method usage. * GridTileLookupTest now uses EntityCoordinates Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es>
87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using Content.Shared.Maps;
|
|
using Robust.Shared.Interfaces.Random;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Maths;
|
|
|
|
namespace Content.Shared.Utility
|
|
{
|
|
public static class SharedDirectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Gets random directions until none are left
|
|
/// </summary>
|
|
/// <returns>An enumerable of the directions.</returns>
|
|
public static IEnumerable<Direction> RandomDirections()
|
|
{
|
|
var directions = new[]
|
|
{
|
|
Direction.East,
|
|
Direction.SouthEast,
|
|
Direction.South,
|
|
Direction.SouthWest,
|
|
Direction.West,
|
|
Direction.NorthWest,
|
|
Direction.North,
|
|
Direction.NorthEast,
|
|
};
|
|
|
|
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
|
var n = directions.Length;
|
|
|
|
while (n > 1)
|
|
{
|
|
n--;
|
|
var k = robustRandom.Next(n + 1);
|
|
var value = directions[k];
|
|
directions[k] = directions[n];
|
|
directions[n] = value;
|
|
}
|
|
|
|
foreach (var direction in directions)
|
|
{
|
|
yield return direction;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets tiles in random directions from the given one.
|
|
/// </summary>
|
|
/// <returns>An enumerable of the adjacent tiles.</returns>
|
|
public static IEnumerable<TileRef> AdjacentTilesRandom(this TileRef tile, bool ignoreSpace = false)
|
|
{
|
|
return tile.GridPosition().AdjacentTilesRandom(ignoreSpace);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets tiles in random directions from the given one.
|
|
/// </summary>
|
|
/// <returns>An enumerable of the adjacent tiles.</returns>
|
|
public static IEnumerable<TileRef> AdjacentTilesRandom(this EntityCoordinates coordinates, bool ignoreSpace = false)
|
|
{
|
|
foreach (var direction in RandomDirections())
|
|
{
|
|
var adjacent = coordinates.Offset(direction).GetTileRef();
|
|
|
|
if (adjacent == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (ignoreSpace && adjacent.Value.Tile.IsEmpty)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
yield return adjacent.Value;
|
|
}
|
|
}
|
|
|
|
public static EntityCoordinates Offset(this EntityCoordinates coordinates, Direction direction)
|
|
{
|
|
return coordinates.Offset(direction.ToVec());
|
|
}
|
|
}
|
|
}
|