From 91f1edfc811fdebd4d1af9507a1bce84ea334f08 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Sat, 14 Jan 2023 13:50:32 +1300 Subject: [PATCH] Cache MapUid (#3687) --- .../Transform/TransformComponent.cs | 18 +++++++------- .../SharedTransformSystem.Component.cs | 24 ++++--------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs index d2c6cd026d..293064212f 100644 --- a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs +++ b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs @@ -99,14 +99,7 @@ namespace Robust.Shared.GameObjects /// /// The EntityUid of the map which this object is on, if any. /// - public EntityUid? MapUid - { - get - { - var id = _mapManager.GetMapEntityId(MapID); - return id.IsValid() ? id : null; - } - } + public EntityUid? MapUid { get; internal set; } /// /// The EntityUid of the grid which this object is on, if any. @@ -485,6 +478,8 @@ namespace Robust.Shared.GameObjects if (newMapId == MapID) return; + EntityUid? newUid = newMapId == MapId.Nullspace ? null : _mapManager.GetMapEntityId(newMapId); + //Set Paused state var mapPaused = _mapManager.IsMapPaused(newMapId); var metaEnts = _entMan.GetEntityQuery(); @@ -492,12 +487,14 @@ namespace Robust.Shared.GameObjects var metaSystem = _entMan.EntitySysManager.GetEntitySystem(); metaSystem.SetEntityPaused(Owner, mapPaused, metaData); + MapUid = newUid; MapID = newMapId; - UpdateChildMapIdsRecursive(MapID, mapPaused, xformQuery, metaEnts, metaSystem); + UpdateChildMapIdsRecursive(MapID, newUid, mapPaused, xformQuery, metaEnts, metaSystem); } internal void UpdateChildMapIdsRecursive( MapId newMapId, + EntityUid? newUid, bool mapPaused, EntityQuery xformQuery, EntityQuery metaQuery, @@ -513,11 +510,12 @@ namespace Robust.Shared.GameObjects var concrete = xformQuery.GetComponent(child.Value); + concrete.MapUid = newUid; concrete.MapID = newMapId; if (concrete.ChildCount != 0) { - concrete.UpdateChildMapIdsRecursive(newMapId, mapPaused, xformQuery, metaQuery, system); + concrete.UpdateChildMapIdsRecursive(newMapId, newUid, mapPaused, xformQuery, metaQuery, system); } } } diff --git a/Robust.Shared/GameObjects/Systems/SharedTransformSystem.Component.cs b/Robust.Shared/GameObjects/Systems/SharedTransformSystem.Component.cs index e786362f0b..a687393a75 100644 --- a/Robust.Shared/GameObjects/Systems/SharedTransformSystem.Component.cs +++ b/Robust.Shared/GameObjects/Systems/SharedTransformSystem.Component.cs @@ -182,7 +182,7 @@ public abstract partial class SharedTransformSystem // Children MAY be initialized here before their parents are. // We do this whole dance to handle this recursively, // setting _mapIdInitialized along the way to avoid going to the MapComponent every iteration. - static MapId FindMapIdAndSet(TransformComponent xform, IEntityManager entMan, EntityQuery xformQuery) + static MapId FindMapIdAndSet(TransformComponent xform, IEntityManager entMan, EntityQuery xformQuery, IMapManager mapManager) { if (xform._mapIdInitialized) return xform.MapID; @@ -191,7 +191,7 @@ public abstract partial class SharedTransformSystem if (xform.ParentUid.IsValid()) { - value = FindMapIdAndSet(xformQuery.GetComponent(xform.ParentUid), entMan, xformQuery); + value = FindMapIdAndSet(xformQuery.GetComponent(xform.ParentUid), entMan, xformQuery, mapManager); } else { @@ -207,6 +207,7 @@ public abstract partial class SharedTransformSystem } } + xform.MapUid = value == MapId.Nullspace ? null : mapManager.GetMapEntityId(value); xform.MapID = value; xform._mapIdInitialized = true; return value; @@ -216,7 +217,7 @@ public abstract partial class SharedTransformSystem if (!component._mapIdInitialized) { - FindMapIdAndSet(component, EntityManager, xformQuery); + FindMapIdAndSet(component, EntityManager, xformQuery, _mapManager); component._mapIdInitialized = true; } @@ -1124,23 +1125,6 @@ public abstract partial class SharedTransformSystem #endregion #region State Handling - private void ChangeMapId(TransformComponent xform, MapId newMapId, EntityQuery xformQuery, EntityQuery metaQuery) - { - if (newMapId == xform.MapID) - return; - - //Set Paused state - var mapPaused = _mapManager.IsMapPaused(newMapId); - var meta = metaQuery.GetComponent(xform.Owner); - _metaSys.SetEntityPaused(xform.Owner, mapPaused, meta); - - // Map entities retain their map Uids - if (xform.Owner != xform.MapUid) - xform.MapID = newMapId; - - xform.UpdateChildMapIdsRecursive(newMapId, mapPaused, xformQuery, metaQuery, _metaSys); - } - public void DetachParentToNull(TransformComponent xform) { if (xform._parent.IsValid())