Files
OldThink/Content.Shared/Maps/TurfHelpers.cs

206 lines
7.7 KiB
C#
Raw Normal View History

2020-08-11 03:45:40 +02:00
#nullable enable
using System.Collections.Generic;
2020-08-19 12:23:42 +02:00
using System.Linq;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
2020-08-11 03:45:40 +02:00
using Content.Shared.Physics;
2020-09-02 18:55:51 +02:00
using Content.Shared.Utility;
2020-08-11 03:45:40 +02:00
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Shared.Maps
{
public static class TurfHelpers
{
2020-08-13 14:18:26 +02:00
/// <summary>
/// Returns the content tile definition for a tile.
/// </summary>
public static ContentTileDefinition GetContentTileDefinition(this Tile tile)
{
var tileDefinitionManager = IoCManager.Resolve<ITileDefinitionManager>();
return (ContentTileDefinition)tileDefinitionManager[tile.TypeId];
}
/// <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, GridId gridId, IMapManager? mapManager = null)
2020-08-13 14:18:26 +02:00
{
if (!gridId.IsValid())
return default;
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
if (!mapManager.TryGetGrid(coordinates.GetGridId(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)
{
return (turf = coordinates.GetTileRef()) != null;
}
public static bool PryTile(this EntityCoordinates coordinates, IEntityManager? entityManager = null,
IMapManager? mapManager = null)
2020-09-02 18:55:51 +02:00
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
2020-09-02 18:55:51 +02:00
mapManager ??= IoCManager.Resolve<IMapManager>();
return coordinates.ToVector2i(entityManager, mapManager).PryTile(coordinates.GetGridId(entityManager));
2020-09-02 18:55:51 +02:00
}
public static bool PryTile(this Vector2i indices, GridId gridId,
2020-09-02 18:55:51 +02:00
IMapManager? mapManager = null, ITileDefinitionManager? tileDefinitionManager = null, IEntityManager? entityManager = null)
{
mapManager ??= IoCManager.Resolve<IMapManager>();
var grid = mapManager.GetGrid(gridId);
var tileRef = grid.GetTileRef(indices);
return tileRef.PryTile(mapManager, tileDefinitionManager, entityManager);
}
public static bool PryTile(this TileRef tileRef,
IMapManager? mapManager = null, ITileDefinitionManager? tileDefinitionManager = null, IEntityManager? entityManager = null)
{
var tile = tileRef.Tile;
var indices = tileRef.GridIndices;
// If the arguments are null, resolve the needed dependencies.
mapManager ??= IoCManager.Resolve<IMapManager>();
tileDefinitionManager ??= IoCManager.Resolve<ITileDefinitionManager>();
entityManager ??= IoCManager.Resolve<IEntityManager>();
if (tile.IsEmpty) return false;
var tileDef = (ContentTileDefinition) tileDefinitionManager[tile.TypeId];
if (!tileDef.CanCrowbar) return false;
var mapGrid = mapManager.GetGrid(tileRef.GridIndex);
var plating = tileDefinitionManager[tileDef.BaseTurfs[^1]];
mapGrid.SetTile(tileRef.GridIndices, new Tile(plating.TileId));
2020-09-02 20:47:19 +02:00
var half = mapGrid.TileSize / 2f;
2020-09-02 18:55:51 +02:00
//Actually spawn the relevant tile item at the right position and give it some random offset.
var tileItem = entityManager.SpawnEntity(tileDef.ItemDropPrototypeName, indices.ToEntityCoordinates(tileRef.GridIndex, mapManager).Offset(new Vector2(half, half)));
2020-09-02 20:47:19 +02:00
tileItem.RandomOffset(0.25f);
2020-09-02 18:55:51 +02:00
return true;
}
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)]
2020-09-12 20:10:56 +02:00
public static IEnumerable<IEntity> GetEntitiesInTile(this TileRef turf, bool approximate = false, IEntityManager? entityManager = null)
2020-08-11 03:45:40 +02:00
{
2020-09-12 20:10:56 +02:00
entityManager ??= IoCManager.Resolve<IEntityManager>();
2020-08-11 03:45:40 +02:00
2020-09-06 17:15:16 +02:00
return entityManager.GetEntitiesIntersecting(turf.MapIndex, GetWorldTileBox(turf), approximate);
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>
2020-09-12 20:10:56 +02:00
public static IEnumerable<IEntity> GetEntitiesInTile(this EntityCoordinates coordinates, bool approximate = false, IEntityManager? entityManager = null)
2020-08-19 12:23:42 +02:00
{
var turf = coordinates.GetTileRef();
if (turf == null)
return Enumerable.Empty<IEntity>();
2020-09-12 20:10:56 +02:00
return GetEntitiesInTile(turf.Value, approximate, entityManager);
2020-08-19 12:23:42 +02:00
}
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
public static IEnumerable<IEntity> GetEntitiesInTile(this Vector2i indices, GridId gridId, bool approximate = false, IEntityManager? entityManager = null)
2020-08-19 12:23:42 +02:00
{
return GetEntitiesInTile(indices.GetTileRef(gridId), approximate, entityManager);
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)
{
var physics = IoCManager.Resolve<IPhysicsManager>();
var worldBox = GetWorldTileBox(turf);
var query = physics.GetCollidingEntities(turf.MapIndex, in worldBox);
foreach (var body in query)
{
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.GridIndex, mapManager);
}
/// <summary>
/// Creates a box the size of a tile, at the same position in the world as the tile.
/// </summary>
private static Box2 GetWorldTileBox(TileRef turf)
{
var map = IoCManager.Resolve<IMapManager>();
// This is scaled to 90 % so it doesn't encompass walls on other tiles.
var tileBox = Box2.UnitCentered.Scale(0.9f);
if (map.TryGetGrid(turf.GridIndex, out var tileGrid))
{
tileBox = tileBox.Scale(tileGrid.TileSize);
return tileBox.Translated(tileGrid.GridTileToWorldPos(turf.GridIndices));
}
return tileBox;
}
}
}