Skip to content

Commit 186392e

Browse files
ShadowCommanderElectroJrmetalgearslothPJB3005
authored
Refactor grid or map methods (space-wizards#5124)
* Rename TryGetMapOrGridCoordinates to make it clearer it gets grid first * Add terminating or deleted checks to TryGetGridOrMapCoordinates * Add comment to check if TerminatingOrDeleted check is necessary * Reorganize AttachToGridOrMap to match TryGetGridOrMapCoordinates * Move validation to method * Replace internals with TryGetGridOrMapCoordinates * Explicitly set coordinates type * Format * Change name back for now * Don't duplicate `TerminatingOrDeleted()` check * Don't call `GetInvWorldMatrix` for the map * Don't check `TerminatingOrDeleted(uid)` in `TryGetMapOrGridCoordinates()` * Fix parenting to terminating grid * Fix matrix error --------- Co-authored-by: ElectroJr <leonsfriedrich@gmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
1 parent ebe4538 commit 186392e

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

Robust.Shared/GameObjects/Systems/SharedGridTraversalSystem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void CheckTraversal(EntityUid entity, TransformComponent xform, EntityUid
100100
if (_mapManager.TryFindGridAt(map, mapPos, out var gridUid, out _))
101101
{
102102
// Some minor duplication here with AttachParent but only happens when going on/off grid so not a big deal ATM.
103-
if (gridUid != xform.GridUid)
103+
if (gridUid != xform.GridUid && !TerminatingOrDeleted(gridUid))
104104
_transform.SetParent(entity, xform, gridUid);
105105
return;
106106
}
@@ -110,4 +110,3 @@ public void CheckTraversal(EntityUid entity, TransformComponent xform, EntityUid
110110
_transform.SetParent(entity, xform, map);
111111
}
112112
}
113-

Robust.Shared/GameObjects/Systems/SharedTransformSystem.Component.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,27 +1413,21 @@ public Matrix3x2 GetInvWorldMatrix(TransformComponent component, EntityQuery<Tra
14131413
/// </summary>
14141414
public void AttachToGridOrMap(EntityUid uid, TransformComponent? xform = null)
14151415
{
1416+
// TODO make this log an error?
1417+
// SetCoordinates already does this when trying to move entities mid-deletion.
14161418
if (TerminatingOrDeleted(uid))
14171419
return;
14181420

1419-
if (!XformQuery.Resolve(uid, ref xform))
1421+
if (!XformQuery.Resolve(uid, ref xform, false))
14201422
return;
14211423

1422-
if (!xform.ParentUid.IsValid() || xform.ParentUid == xform.GridUid)
1424+
if (!xform.ParentUid.IsValid())
14231425
return;
14241426

1425-
EntityUid newParent;
1426-
var oldPos = GetWorldPosition(xform);
1427-
if (_mapManager.TryFindGridAt(xform.MapID, oldPos, out var gridUid, out _)
1428-
&& !TerminatingOrDeleted(gridUid))
1429-
{
1430-
newParent = gridUid;
1431-
}
1432-
else if (_map.TryGetMap(xform.MapID, out var mapEnt) && !TerminatingOrDeleted(mapEnt))
1433-
{
1434-
newParent = mapEnt.Value;
1435-
}
1436-
else
1427+
if (xform.ParentUid == xform.GridUid)
1428+
return;
1429+
1430+
if (!TryGetMapOrGridCoordinates(uid, out var coordinates, xform))
14371431
{
14381432
if (!_mapQuery.HasComp(uid))
14391433
Log.Warning($"Failed to attach entity to map or grid. Entity: ({ToPrettyString(uid)}). Trace: {Environment.StackTrace}");
@@ -1442,32 +1436,50 @@ public void AttachToGridOrMap(EntityUid uid, TransformComponent? xform = null)
14421436
return;
14431437
}
14441438

1445-
if (newParent == xform.ParentUid || newParent == uid)
1439+
if (coordinates.Value.EntityId == xform.ParentUid || coordinates.Value.EntityId == uid)
14461440
return;
14471441

1448-
var newPos = Vector2.Transform(oldPos, GetInvWorldMatrix(newParent));
1449-
SetCoordinates(uid, xform, new(newParent, newPos));
1442+
SetCoordinates(uid, xform, coordinates.Value);
14501443
}
14511444

1452-
public bool TryGetMapOrGridCoordinates(EntityUid uid, [NotNullWhen(true)] out EntityCoordinates? coordinates, TransformComponent? xform = null)
1445+
/// <summary>
1446+
/// Attempts to get the current coordinates of the provided entity, relative to either a grid currently at that location, or the map.
1447+
/// </summary>
1448+
/// <remarks>
1449+
/// This will not return coordinates if the map or grid are currently being deleted.
1450+
/// </remarks>
1451+
/// <param name="uid">The entity to get the coordinates of.</param>
1452+
/// <param name="coordinates">The returned coordinates on the grid or map.</param>
1453+
/// <param name="xform">The transform component of the <paramref name="uid"/>.</param>
1454+
/// <returns>Whether the grid or map coordinates were found and <paramref name="coordinates"/> was set.</returns>
1455+
public bool TryGetMapOrGridCoordinates(
1456+
EntityUid uid,
1457+
[NotNullWhen(true)] out EntityCoordinates? coordinates,
1458+
TransformComponent? xform = null)
14531459
{
14541460
coordinates = null;
14551461

1456-
if (!XformQuery.Resolve(uid, ref xform))
1462+
if (!XformQuery.Resolve(uid, ref xform, false))
14571463
return false;
14581464

14591465
if (!xform.ParentUid.IsValid())
14601466
return false;
14611467

1462-
if (xform.MapUid is not { } map)
1468+
if (xform.MapUid is not { } map || TerminatingOrDeleted(map))
14631469
return false;
14641470

1465-
var newParent = map;
14661471
var oldPos = GetWorldPosition(xform);
1467-
if (_mapManager.TryFindGridAt(map, oldPos, out var gridUid, out _))
1468-
newParent = gridUid;
1472+
if (_mapManager.TryFindGridAt(map, oldPos, out var gridUid, out _) && !TerminatingOrDeleted(gridUid))
1473+
{
1474+
coordinates = gridUid == xform.ParentUid
1475+
? new EntityCoordinates(gridUid, xform.LocalPosition)
1476+
: new EntityCoordinates(gridUid, Vector2.Transform(oldPos, GetInvWorldMatrix(gridUid)));
1477+
}
1478+
else
1479+
{
1480+
coordinates = new EntityCoordinates(map, oldPos);
1481+
}
14691482

1470-
coordinates = new(newParent, Vector2.Transform(oldPos, GetInvWorldMatrix(newParent)));
14711483
return true;
14721484
}
14731485
#endregion

0 commit comments

Comments
 (0)