Kills TurfHelpers (#37939)
* Create TurfSystem equivalent for and obsolete TurfHelpers.GetTileRef * Fix EntitySystem uses of TurfHelpers.GetTileRef * Fix EntitySystem uses of TurfHelpers.TryGetTileRef * Fix construction condition uses of TurfHelpers.GetTileRef * Fix last use of TurfHelpers.IsBlockedTurf * Create TurfSystem equivalent to and obsolete TurfHelpers.GetContentTileDefinition * Fix uses of TurfHelpers.GetContentTileDefinition(TileRef) * Fix uses of TurfHelpers.GetContentTileDefinition(Tile) * Create TurfSystem equivalent to and obsolete TurfHelpers.IsSpace * Fix EntitySystem uses of TurfHelpers.IsSpace(Tile) * Fix EntitySystem uses of TurfHelpers.IsSpace(TileRef) * Fix remaining uses of TurfHelpers.IsSpace * Fix uses of TurfHelpers.GetEntitiesInTile * Delete TurfHelpers.cs * Add GetEntitiesInTile lookup methods * Convert some GetEntitiesInTile methods to LookupSystem extension methods * Use new GetEntitiesInTile methods * Recycle spiderweb hashset * Recycle floor tile hashset
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Toolshed.Commands.Values;
|
||||
|
||||
namespace Content.Shared.Maps;
|
||||
|
||||
@@ -11,8 +14,43 @@ namespace Content.Shared.Maps;
|
||||
/// </summary>
|
||||
public sealed class TurfSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
[Dependency] private readonly SharedMapSystem _mapSystem = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitions = default!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to get the turf at or under some given coordinates or null if no such turf exists.
|
||||
/// </summary>
|
||||
/// <param name="coordinates">The coordinates to search for a turf.</param>
|
||||
/// <returns>A <see cref="TileRef"/> for the turf found at the given coordinates or null if no such turf exists.</returns>
|
||||
public TileRef? GetTileRef(EntityCoordinates coordinates)
|
||||
{
|
||||
if (!coordinates.IsValid(EntityManager))
|
||||
return null;
|
||||
|
||||
var pos = _transform.ToMapCoordinates(coordinates);
|
||||
if (!_mapManager.TryFindGridAt(pos, out var gridUid, out var gridComp))
|
||||
return null;
|
||||
|
||||
if (!_mapSystem.TryGetTileRef(gridUid, gridComp, coordinates, out var tile))
|
||||
return null;
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to get the turf at or under some given coordinates.
|
||||
/// </summary>
|
||||
/// <param name="coordinates">The coordinates to search for a turf.</param>
|
||||
/// <param name="tile">Returns the turf found at the given coordinates if any.</param>
|
||||
/// <returns>True if a turf was found at the given coordinates, false otherwise.</returns>
|
||||
public bool TryGetTileRef(EntityCoordinates coordinates, [NotNullWhen(true)] out TileRef? tile)
|
||||
{
|
||||
return (tile = GetTileRef(coordinates)) is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a given tile is blocked by physics-enabled entities.
|
||||
@@ -63,14 +101,14 @@ public sealed class TurfSystem : EntitySystem
|
||||
rot -= gridRot;
|
||||
pos = (-gridRot).RotateVec(pos - gridPos);
|
||||
|
||||
var xform = new Transform(pos, (float) rot.Theta);
|
||||
var xform = new Transform(pos, (float)rot.Theta);
|
||||
|
||||
foreach (var fixture in fixtures.Fixtures.Values)
|
||||
{
|
||||
if (!fixture.Hard)
|
||||
continue;
|
||||
|
||||
if ((fixture.CollisionLayer & (int) mask) == 0)
|
||||
if ((fixture.CollisionLayer & (int)mask) == 0)
|
||||
continue;
|
||||
|
||||
for (var i = 0; i < fixture.Shape.ChildCount; i++)
|
||||
@@ -86,6 +124,26 @@ public sealed class TurfSystem : EntitySystem
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether a tile is considered to be space or directly exposed to space.
|
||||
/// </summary>
|
||||
/// <param name="tile">The tile in question.</param>
|
||||
/// <returns>True if the tile is considered to be space, false otherwise.</returns>
|
||||
public bool IsSpace(Tile tile)
|
||||
{
|
||||
return GetContentTileDefinition(tile).MapAtmosphere;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether a tile is considered to be space or directly exposed to space.
|
||||
/// </summary>
|
||||
/// <param name="tile">The tile in question.</param>
|
||||
/// <returns>True if the tile is considered to be space, false otherwise.</returns>
|
||||
public bool IsSpace(TileRef tile)
|
||||
{
|
||||
return IsSpace(tile.Tile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the location of the centre of the tile in grid coordinates.
|
||||
/// </summary>
|
||||
@@ -95,4 +153,78 @@ public sealed class TurfSystem : EntitySystem
|
||||
var center = (turf.GridIndices + new Vector2(0.5f, 0.5f)) * grid.TileSize;
|
||||
return new EntityCoordinates(turf.GridUid, center);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the content tile definition for a tile.
|
||||
/// </summary>
|
||||
public ContentTileDefinition GetContentTileDefinition(Tile tile)
|
||||
{
|
||||
return (ContentTileDefinition)_tileDefinitions[tile.TypeId];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the content tile definition for a tile ref.
|
||||
/// </summary>
|
||||
public ContentTileDefinition GetContentTileDefinition(TileRef tile)
|
||||
{
|
||||
return GetContentTileDefinition(tile.Tile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collects all of the entities intersecting with the turf at a given position into a provided <see cref="HashSet{EntityUid}"/>
|
||||
/// </summary>
|
||||
/// <param name="coords">The position of the turf to search for entities.</param>
|
||||
/// <param name="intersecting">The hashset used to collect the relevant entities.</param>
|
||||
/// <param name="flags">A set of lookup categories to search for relevant entities.</param>
|
||||
public void GetEntitiesInTile(EntityCoordinates coords, HashSet<EntityUid> intersecting, LookupFlags flags = LookupFlags.Static)
|
||||
{
|
||||
if (!TryGetTileRef(coords, out var tileRef))
|
||||
return;
|
||||
|
||||
_entityLookup.GetEntitiesInTile(tileRef.Value, intersecting, flags);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection containing all of the entities overlapping with the turf at a given position.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="GetEntitiesInTile(EntityCoordinates, HashSet{EntityUid}, LookupFlags)"/>
|
||||
/// <returns>A hashset containing all of the entities overlapping with the turf in question.</returns>
|
||||
public HashSet<EntityUid> GetEntitiesInTile(EntityCoordinates coords, LookupFlags flags = LookupFlags.Static)
|
||||
{
|
||||
if (!TryGetTileRef(coords, out var tileRef))
|
||||
return [];
|
||||
|
||||
return _entityLookup.GetEntitiesInTile(tileRef.Value, flags);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for looking up entities with respect to given turfs.
|
||||
/// </summary>
|
||||
public static partial class TurfLookupExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Collects all of the entities overlapping with a given turf into a provided <see cref="HashSet{EntityUid}"/>.
|
||||
/// </summary>
|
||||
/// <param name="turf">The turf in question.</param>
|
||||
/// <param name="intersecting">The hashset used to collect the relevant entities.</param>
|
||||
/// <param name="flags">A set of lookup categories to search for relevant entities.</param>
|
||||
public static void GetEntitiesInTile(this EntityLookupSystem lookupSystem, TileRef turf, HashSet<EntityUid> intersecting, LookupFlags flags = LookupFlags.Static)
|
||||
{
|
||||
var bounds = lookupSystem.GetWorldBounds(turf);
|
||||
bounds.Box = bounds.Box.Scale(0.9f); // Otherwise the box can clip into neighboring tiles.
|
||||
lookupSystem.GetEntitiesIntersecting(turf.GridUid, bounds, intersecting, flags);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection containing all of the entities overlapping with a given turf.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="GetEntitiesInTile(EntityLookupSystem, TileRef, HashSet{EntityUid}, LookupFlags)"/>
|
||||
/// <returns>A hashset containing all of the entities overlapping with the turf in question.</returns>
|
||||
public static HashSet<EntityUid> GetEntitiesInTile(this EntityLookupSystem lookupSystem, TileRef turf, LookupFlags flags = LookupFlags.Static)
|
||||
{
|
||||
var intersecting = new HashSet<EntityUid>();
|
||||
lookupSystem.GetEntitiesInTile(turf, intersecting, flags);
|
||||
return intersecting;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user