Files
crystall-punk-14/Content.Shared/Maps/TurfHelpers.cs

196 lines
7.5 KiB
C#
Raw Normal View History

2020-08-19 12:23:42 +02:00
using System.Linq;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
2023-01-07 04:24:50 -06:00
using Content.Shared.Decals;
2020-08-11 03:45:40 +02:00
using Content.Shared.Physics;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Random;
namespace Content.Shared.Maps
{
public static class TurfHelpers
{
2020-08-13 14:18:26 +02:00
/// <summary>
/// Attempts to get the turf at map indices with grid id or null if no such turf is found.
/// </summary>
public static TileRef GetTileRef(this Vector2i vector2i, EntityUid gridId, IMapManager? mapManager = null)
2020-08-13 14:18:26 +02:00
{
mapManager ??= IoCManager.Resolve<IMapManager>();
2020-08-13 14:18:26 +02:00
if (!mapManager.TryGetGrid(gridId, out var grid))
return default;
2020-08-13 14:18:26 +02:00
if (!grid.TryGetTileRef(vector2i, out var tile))
return default;
2020-08-13 14:18:26 +02:00
return tile;
}
2020-08-11 03:45:40 +02:00
/// <summary>
/// Attempts to get the turf at a certain coordinates or null if no such turf is found.
/// </summary>
public static TileRef? GetTileRef(this EntityCoordinates coordinates, IEntityManager? entityManager = null, IMapManager? mapManager = null)
2020-08-11 03:45:40 +02:00
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
if (!coordinates.IsValid(entityManager))
2020-08-11 03:45:40 +02:00
return null;
(Smaller) Construction PR - (IC Construction) (#2575) * Disable Pulling When Buckling an entity * Projectile Improvements If you shoot at a person that is critted now it will only hit if you aim at that person otherwise go "above" him and hit other targets. - Dead people are still unhitable * Update Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> * Firelock In Progress * Revert "Projectile Improvements" This reverts commit 5821afc798e49e530d4086d7a9ddbe097805fdc4. * Firelock Graph * Revert "Merge branch 'master' into test2" This reverts commit c69661cc7d9dcdc6d8c0dd45770f9eb94b231463, reversing changes made to 5f1de8b8d24cd52190addb3df5617cb1012fd52c. * Bunch of stuff - Metal Rods - Reinforced Glass - SetStackCount Condition - Tables - Lattice * Output2 to FloorTileItemComponent * Plating, Underplating and Tiles (+FloorTile Improvements) * Turf Fixes + APC Electronics * Reinforced Glass In-hand textures * All the fixes * Final Changes * (Hopefully) Last commit * Update Resources/Prototypes/Entities/Constructible/Doors/firelock_frame.yml Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> * Update Content.Server/GameObjects/Components/Atmos/FirelockComponent.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * A Few more things * Edit FirelockComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2020-11-20 16:58:06 +02:00
mapManager ??= IoCManager.Resolve<IMapManager>();
2020-08-11 03:45:40 +02:00
2022-06-20 12:14:35 +12:00
if (!mapManager.TryGetGrid(coordinates.GetGridUid(entityManager), out var grid))
2020-08-11 03:45:40 +02:00
return null;
(Smaller) Construction PR - (IC Construction) (#2575) * Disable Pulling When Buckling an entity * Projectile Improvements If you shoot at a person that is critted now it will only hit if you aim at that person otherwise go "above" him and hit other targets. - Dead people are still unhitable * Update Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> * Firelock In Progress * Revert "Projectile Improvements" This reverts commit 5821afc798e49e530d4086d7a9ddbe097805fdc4. * Firelock Graph * Revert "Merge branch 'master' into test2" This reverts commit c69661cc7d9dcdc6d8c0dd45770f9eb94b231463, reversing changes made to 5f1de8b8d24cd52190addb3df5617cb1012fd52c. * Bunch of stuff - Metal Rods - Reinforced Glass - SetStackCount Condition - Tables - Lattice * Output2 to FloorTileItemComponent * Plating, Underplating and Tiles (+FloorTile Improvements) * Turf Fixes + APC Electronics * Reinforced Glass In-hand textures * All the fixes * Final Changes * (Hopefully) Last commit * Update Resources/Prototypes/Entities/Constructible/Doors/firelock_frame.yml Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> * Update Content.Server/GameObjects/Components/Atmos/FirelockComponent.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * A Few more things * Edit FirelockComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2020-11-20 16:58:06 +02:00
2020-09-06 17:05:01 +02:00
if (!grid.TryGetTileRef(coordinates, out var tile))
2020-08-11 03:45:40 +02:00
return null;
return tile;
}
public static bool TryGetTileRef(this EntityCoordinates coordinates, [NotNullWhen(true)] out TileRef? turf, IEntityManager? entityManager = null, IMapManager? mapManager = null)
{
return (turf = coordinates.GetTileRef(entityManager, mapManager)) != null;
}
/// <summary>
/// Returns the content tile definition for a tile.
/// </summary>
public static ContentTileDefinition GetContentTileDefinition(this Tile tile, ITileDefinitionManager? tileDefinitionManager = null)
{
tileDefinitionManager ??= IoCManager.Resolve<ITileDefinitionManager>();
return (ContentTileDefinition)tileDefinitionManager[tile.TypeId];
}
/// <summary>
/// Returns whether a tile is considered space.
/// </summary>
public static bool IsSpace(this Tile tile, ITileDefinitionManager? tileDefinitionManager = null)
{
return tile.GetContentTileDefinition(tileDefinitionManager).IsSpace;
}
/// <summary>
/// Returns the content tile definition for a tile ref.
/// </summary>
public static ContentTileDefinition GetContentTileDefinition(this TileRef tile, ITileDefinitionManager? tileDefinitionManager = null)
{
return tile.Tile.GetContentTileDefinition(tileDefinitionManager);
}
/// <summary>
/// Returns whether a tile ref is considered space.
/// </summary>
public static bool IsSpace(this TileRef tile, ITileDefinitionManager? tileDefinitionManager = null)
{
return tile.Tile.IsSpace(tileDefinitionManager);
}
2020-08-11 03:45:40 +02:00
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
2020-08-19 12:23:42 +02:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<EntityUid> GetEntitiesInTile(this TileRef turf, LookupFlags flags = LookupFlags.Static, EntityLookupSystem? lookupSystem = null)
2020-08-11 03:45:40 +02:00
{
lookupSystem ??= EntitySystem.Get<EntityLookupSystem>();
2020-08-11 03:45:40 +02:00
if (!GetWorldTileBox(turf, out var worldBox))
2021-12-04 12:47:09 +01:00
return Enumerable.Empty<EntityUid>();
return lookupSystem.GetEntitiesIntersecting(turf.GridUid, worldBox, flags);
2020-08-11 03:45:40 +02:00
}
2020-08-19 12:23:42 +02:00
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
public static IEnumerable<EntityUid> GetEntitiesInTile(this EntityCoordinates coordinates, LookupFlags flags = LookupFlags.Static, EntityLookupSystem? lookupSystem = null)
2020-08-19 12:23:42 +02:00
{
var turf = coordinates.GetTileRef();
if (turf == null)
2021-12-04 12:47:09 +01:00
return Enumerable.Empty<EntityUid>();
2020-08-19 12:23:42 +02:00
return GetEntitiesInTile(turf.Value, flags, lookupSystem);
2020-08-19 12:23:42 +02:00
}
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
public static IEnumerable<EntityUid> GetEntitiesInTile(this Vector2i indices, EntityUid gridId, LookupFlags flags = LookupFlags.Static, EntityLookupSystem? lookupSystem = null)
2020-08-19 12:23:42 +02:00
{
return GetEntitiesInTile(indices.GetTileRef(gridId), flags, lookupSystem);
2020-08-19 12:23:42 +02:00
}
/// <summary>
/// Checks if a turf has something dense on it.
/// </summary>
public static bool IsBlockedTurf(this TileRef turf, bool filterMobs, EntityLookupSystem? physics = null, IEntitySystemManager? entSysMan = null)
{
// TODO: Deprecate this with entitylookup.
if (physics == null)
{
IoCManager.Resolve(ref entSysMan);
physics = entSysMan.GetEntitySystem<EntityLookupSystem>();
}
if (!GetWorldTileBox(turf, out var worldBox))
return false;
var entManager = IoCManager.Resolve<IEntityManager>();
var query = physics.GetEntitiesIntersecting(turf.GridUid, worldBox);
foreach (var ent in query)
{
// Yes, this can fail. Welp!
if (!entManager.TryGetComponent(ent, out PhysicsComponent? body))
continue;
if (body.CanCollide && body.Hard && (body.CollisionLayer & (int) CollisionGroup.Impassable) != 0)
return true;
if (filterMobs && (body.CollisionLayer & (int) CollisionGroup.MobMask) != 0)
return true;
}
return false;
}
public static EntityCoordinates GridPosition(this TileRef turf, IMapManager? mapManager = null)
{
mapManager ??= IoCManager.Resolve<IMapManager>();
return turf.GridIndices.ToEntityCoordinates(turf.GridUid, mapManager);
}
/// <summary>
/// Creates a box the size of a tile, at the same position in the world as the tile.
/// </summary>
[Obsolete]
private static bool GetWorldTileBox(TileRef turf, out Box2Rotated res)
{
var entManager = IoCManager.Resolve<IEntityManager>();
var map = IoCManager.Resolve<IMapManager>();
if (map.TryGetGrid(turf.GridUid, out var tileGrid))
{
var gridRot = entManager.GetComponent<TransformComponent>(tileGrid.Owner).WorldRotation;
// This is scaled to 90 % so it doesn't encompass walls on other tiles.
var tileBox = Box2.UnitCentered.Scale(0.9f);
tileBox = tileBox.Scale(tileGrid.TileSize);
var worldPos = tileGrid.GridTileToWorldPos(turf.GridIndices);
tileBox = tileBox.Translated(worldPos);
// Now tileBox needs to be rotated to match grid rotation
res = new Box2Rotated(tileBox, gridRot, worldPos);
return true;
}
// Have to "return something"
res = Box2Rotated.UnitCentered;
return false;
}
}
}