Skip to content

Commit

Permalink
Fix mime wall & turf/tile helper issues. (#17844)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Jul 6, 2023
1 parent 88f9d2b commit 126f5d6
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
16 changes: 11 additions & 5 deletions Content.Server/Abilities/Mime/MimePowersSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Robust.Shared.Physics.Components;
using Robust.Shared.Timing;
using Content.Server.Speech.Muting;
using Robust.Shared.Containers;
using Robust.Shared.Map;

namespace Content.Server.Abilities.Mime
{
Expand All @@ -19,7 +21,8 @@ public sealed class MimePowersSystem : EntitySystem
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly EntityLookupSystem _lookupSystem = default!;
[Dependency] private readonly TurfSystem _turf = default!;

[Dependency] private readonly IMapManager _mapMan = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly IGameTiming _timing = default!;

public override void Initialize()
Expand Down Expand Up @@ -62,11 +65,14 @@ private void OnInvisibleWall(EntityUid uid, MimePowersComponent component, Invis
if (!component.Enabled)
return;

if (_container.IsEntityOrParentInContainer(uid))
return;

var xform = Transform(uid);
// Get the tile in front of the mime
var offsetValue = xform.LocalRotation.ToWorldVec().Normalized;
var coords = xform.Coordinates.Offset(offsetValue).SnapToGrid(EntityManager);
var tile = coords.GetTileRef();
var offsetValue = xform.LocalRotation.ToWorldVec();
var coords = xform.Coordinates.Offset(offsetValue).SnapToGrid(EntityManager, _mapMan);
var tile = coords.GetTileRef(EntityManager, _mapMan);
if (tile == null)
return;

Expand All @@ -88,7 +94,7 @@ private void OnInvisibleWall(EntityUid uid, MimePowersComponent component, Invis
}
_popupSystem.PopupEntity(Loc.GetString("mime-invisible-wall-popup", ("mime", uid)), uid);
// Make sure we set the invisible wall to despawn properly
Spawn(component.WallPrototype, coords);
Spawn(component.WallPrototype, _turf.GetTileCenter(tile.Value));
// Handle args so cooldown works
args.Handled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public FixedPoint2 TileReact(TileRef tile, ReagentPrototype reagent, FixedPoint2
var xoffs = random.NextFloat(-RandomOffsetMax, RandomOffsetMax);
var yoffs = random.NextFloat(-RandomOffsetMax, RandomOffsetMax);

var pos = tile.GridPosition().Offset(new Vector2(0.5f + xoffs, 0.5f + yoffs));
var center = entMan.System<TurfSystem>().GetTileCenter(tile);
var pos = center.Offset(new Vector2(xoffs, yoffs));
entMan.SpawnEntity(Entity, pos);

return Usage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public sealed partial class ExplosionSystem : EntitySystem
HashSet<EntityUid> encounteredGrids = new();
Dictionary<EntityUid, HashSet<Vector2i>>? previousGridJump;

// variables for transforming between grid and space-coordiantes
// variables for transforming between grid and space-coordinates
var spaceMatrix = Matrix3.Identity;
var spaceAngle = Angle.Zero;
if (referenceGrid != null)
Expand Down
3 changes: 2 additions & 1 deletion Content.Server/Maps/TileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class TileSystem : EntitySystem
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly DecalSystem _decal = default!;
[Dependency] private readonly TurfSystem _turf = default!;

public bool PryTile(Vector2i indices, EntityUid gridId)
{
Expand Down Expand Up @@ -68,7 +69,7 @@ public bool ReplaceTile(TileRef tileref, ContentTileDefinition replacementTile,
return false;

var variant = _robustRandom.Pick(replacementTile.PlacementVariants);
var decals = _decal.GetDecalsInRange(tileref.GridUid, tileref.GridPosition().SnapToGrid(EntityManager, _mapManager).Position, 0.5f);
var decals = _decal.GetDecalsInRange(tileref.GridUid, _turf.GetTileCenter(tileref).Position, 0.5f);
foreach (var (id, _) in decals)
{
_decal.RemoveDecal(tileref.GridUid, id);
Expand Down
12 changes: 9 additions & 3 deletions Content.Server/Respawn/SpecialRespawnSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Content.Server.Station.Systems;
using Content.Shared.Database;
using Content.Shared.Maps;
using Content.Shared.Physics;
using Content.Shared.Respawn;
using Robust.Shared.Map;
using Robust.Shared.Random;
Expand All @@ -19,7 +20,7 @@ public sealed class SpecialRespawnSystem : SharedSpecialRespawnSystem
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly TurfSystem _turf = default!;
[Dependency] private readonly IChatManager _chat = default!;

public override void Initialize()
Expand Down Expand Up @@ -100,10 +101,15 @@ private void OnTermination(EntityUid uid, SpecialRespawnComponent component, ref

foreach (var tile in grid.GetTilesIntersecting(circle))
{
if (tile.IsSpace(_tileDefinitionManager) || tile.IsBlockedTurf(true) || !_atmosphere.IsTileMixtureProbablySafe(entityGridUid, entityMapUid.Value, grid.TileIndicesFor(mapPos)))
if (tile.IsSpace(_tileDefinitionManager)
|| _turf.IsTileBlocked(tile, CollisionGroup.MobMask)
|| !_atmosphere.IsTileMixtureProbablySafe(entityGridUid, entityMapUid.Value,
grid.TileIndicesFor(mapPos)))
{
continue;
}

pos = tile.GridPosition();
pos = _turf.GetTileCenter(tile);
found = true;

if (found)
Expand Down
14 changes: 4 additions & 10 deletions Content.Server/Tools/ToolSystem.TilePrying.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using System.Threading;
using Content.Server.Fluids.Components;
using Content.Server.Tools.Components;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Fluids.Components;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Tools.Components;
using Robust.Shared.Audio;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Utility;

namespace Content.Server.Tools;

Expand Down Expand Up @@ -41,20 +34,21 @@ private void OnTilePryComplete(EntityUid uid, TilePryingComponent component, Til
var gridUid = args.Coordinates.GetGridUid(EntityManager);
if (!_mapManager.TryGetGrid(gridUid, out var grid))
{
Logger.Error("Attempted to pry from a non-existent grid?");
Log.Error("Attempted to pry from a non-existent grid?");
return;
}

var tile = grid.GetTileRef(args.Coordinates);
var center = _turf.GetTileCenter(tile);
if (args.Used != null)
{
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.User):actor} used {ToPrettyString(args.Used.Value):tool} to pry {_tileDefinitionManager[tile.Tile.TypeId].Name} at {ToPrettyString(tile.GridUid):grid} {tile.GridPosition()}");
$"{ToPrettyString(args.User):actor} used {ToPrettyString(args.Used.Value):tool} to pry {_tileDefinitionManager[tile.Tile.TypeId].Name} at {center}");
}
else
{
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.User):actor} pried {_tileDefinitionManager[tile.Tile.TypeId].Name} at {ToPrettyString(tile.GridUid):grid} {tile.GridPosition()}");
$"{ToPrettyString(args.User):actor} pried {_tileDefinitionManager[tile.Tile.TypeId].Name} at {center}");
}

_tile.PryTile(tile);
Expand Down
2 changes: 2 additions & 0 deletions Content.Server/Tools/ToolSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Popups;
using Content.Server.Tools.Components;
using Content.Shared.Maps;
using Content.Shared.Tools;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
Expand All @@ -17,6 +18,7 @@ public sealed partial class ToolSystem : SharedToolSystem
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly TurfSystem _turf = default!;

public override void Initialize()
{
Expand Down
23 changes: 13 additions & 10 deletions Content.Shared/Coordinates/Helpers/SnapgridHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, I
{
IoCManager.Resolve(ref entMan, ref mapManager);

var gridIdOpt = coordinates.GetGridUid(entMan);
var gridId = coordinates.GetGridUid(entMan);

var tileSize = 1f;

if (gridIdOpt is EntityUid gridId && gridId.IsValid())
if (gridId == null)
{
var grid = mapManager.GetGrid(gridId);
tileSize = grid.TileSize;
var xformSys = entMan.System<SharedTransformSystem>();
var mapPos = coordinates.ToMap(entMan, xformSys);
var mapX = (int)Math.Floor(mapPos.X) + 0.5f;
var mapY = (int)Math.Floor(mapPos.Y) + 0.5f;
mapPos = new MapCoordinates(new Vector2(mapX, mapY), mapPos.MapId);
return EntityCoordinates.FromMap(coordinates.EntityId, mapPos, xformSys);
}

var localPos = coordinates.Position;

var grid = mapManager.GetGrid(gridId.Value);
var tileSize = grid.TileSize;
var localPos = coordinates.WithEntityId(gridId.Value).Position;
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;

return new EntityCoordinates(coordinates.EntityId, x, y);
var gridPos = new EntityCoordinates(gridId.Value, new Vector2(x, y));
return gridPos.WithEntityId(coordinates.EntityId);
}

public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, MapGridComponent grid)
Expand Down
7 changes: 0 additions & 7 deletions Content.Shared/Maps/TurfHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ public static bool IsBlockedTurf(this TileRef turf, bool filterMobs, EntityLooku
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<TurfSystem>().IsTileBlocked(turf, mask);
}

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>
Expand Down
11 changes: 11 additions & 0 deletions Content.Shared/Maps/TurfSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class TurfSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly IMapManager _mapMan = default!;

/// <summary>
/// Returns true if a given tile is blocked by physics-enabled entities.
Expand Down Expand Up @@ -84,4 +85,14 @@ public bool IsTileBlocked(EntityUid gridUid,

return false;
}

/// <summary>
/// Returns the location of the centre of the tile in grid coordinates.
/// </summary>
public EntityCoordinates GetTileCenter(TileRef turf)
{
var grid = _mapMan.GetGrid(turf.GridUid);
var center = (turf.GridIndices + new Vector2(0.5f, 0.5f)) * grid.TileSize;
return new EntityCoordinates(turf.GridUid, center);
}
}

0 comments on commit 126f5d6

Please sign in to comment.