From c3858e6151fd6c246cd8ffcf4eced623f969ce79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Aguilera=20Puerto?= Date: Sun, 6 Sep 2020 19:08:30 +0200 Subject: [PATCH] Optimize even further --- .../Atmos/GridAtmosphereComponent.cs | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/Content.Server/GameObjects/Components/Atmos/GridAtmosphereComponent.cs b/Content.Server/GameObjects/Components/Atmos/GridAtmosphereComponent.cs index 0d203f3330..11f2e91a78 100644 --- a/Content.Server/GameObjects/Components/Atmos/GridAtmosphereComponent.cs +++ b/Content.Server/GameObjects/Components/Atmos/GridAtmosphereComponent.cs @@ -54,6 +54,7 @@ namespace Content.Server.GameObjects.Components.Atmos private bool _paused = false; private float _timer = 0f; private Stopwatch _stopwatch = new Stopwatch(); + private GridId _gridId; [ViewVariables] public int UpdateCounter { get; private set; } = 0; @@ -157,11 +158,9 @@ namespace Content.Server.GameObjects.Components.Atmos /// public virtual void PryTile(MapIndices indices) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGridComponent)) return; if (IsSpace(indices) || IsAirBlocked(indices)) return; - var mapGrid = mapGridComponent.Grid; - indices.PryTile(mapGrid.Index, _mapManager, _tileDefinitionManager, _serverEntityManager); + indices.PryTile(_gridId, _mapManager, _tileDefinitionManager, _serverEntityManager); } public override void Initialize() @@ -173,7 +172,9 @@ namespace Content.Server.GameObjects.Components.Atmos public override void OnAdd() { base.OnAdd(); - RepopulateTiles(); + + if (Owner.TryGetComponent(out IMapGridComponent? mapGrid)) + _gridId = mapGrid.GridIndex; } public virtual void RepopulateTiles() @@ -201,15 +202,13 @@ namespace Content.Server.GameObjects.Components.Atmos protected virtual void Revalidate() { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; - foreach (var indices in _invalidatedCoords.ToArray()) { var tile = GetTile(indices); if (tile == null) { - tile = new TileAtmosphere(this, mapGrid.Grid.Index, indices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C}); + tile = new TileAtmosphere(this, _gridId, indices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C}); Tiles[indices] = tile; } @@ -263,9 +262,8 @@ namespace Content.Server.GameObjects.Components.Atmos /// public virtual void FixVacuum(MapIndices indices) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; var tile = GetTile(indices); - if (tile?.GridIndex != mapGrid.Grid.Index) return; + if (tile?.GridIndex != _gridId) return; var adjacent = GetAdjacentTiles(indices); tile.Air = new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C}; Tiles[indices] = tile; @@ -284,9 +282,7 @@ namespace Content.Server.GameObjects.Components.Atmos [MethodImpl(MethodImplOptions.AggressiveInlining)] public virtual void AddActiveTile(TileAtmosphere? tile) { - // TODO ATMOS Optimization: Cache the grid Id for faster superconduction. Do the same for all the other ones. - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; - if (tile?.GridIndex != mapGrid.Grid.Index) return; + if (tile?.GridIndex != _gridId) return; tile.Excited = true; _activeTiles.Add(tile); } @@ -305,8 +301,7 @@ namespace Content.Server.GameObjects.Components.Atmos [MethodImpl(MethodImplOptions.AggressiveInlining)] public virtual void AddHotspotTile(TileAtmosphere? tile) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; - if (tile?.GridIndex != mapGrid.Grid.Index || tile?.Air == null) return; + if (tile?.GridIndex != _gridId || tile?.Air == null) return; _hotspotTiles.Add(tile); } @@ -320,8 +315,7 @@ namespace Content.Server.GameObjects.Components.Atmos public virtual void AddSuperconductivityTile(TileAtmosphere? tile) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; - if (tile?.GridIndex != mapGrid.Grid.Index) return; + if (tile?.GridIndex != _gridId) return; _superconductivityTiles.Add(tile); } @@ -335,8 +329,7 @@ namespace Content.Server.GameObjects.Components.Atmos [MethodImpl(MethodImplOptions.AggressiveInlining)] public virtual void AddHighPressureDelta(TileAtmosphere? tile) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; - if (tile?.GridIndex != mapGrid.Grid.Index) return; + if (tile?.GridIndex != _gridId) return; _highPressureDelta.Add(tile); } @@ -390,14 +383,12 @@ namespace Content.Server.GameObjects.Components.Atmos /// public TileAtmosphere? GetTile(MapIndices indices, bool createSpace = true) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return null; - if (Tiles.TryGetValue(indices, out var tile)) return tile; // We don't have that tile! if (IsSpace(indices) && createSpace) { - return new TileAtmosphere(this, mapGrid.Grid.Index, indices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.TCMB}, true); + return new TileAtmosphere(this, _gridId, indices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.TCMB}, true); } return null; @@ -781,13 +772,11 @@ namespace Content.Server.GameObjects.Components.Atmos private IEnumerable GetObstructingComponents(MapIndices indices) { - if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return Enumerable.Empty(); - var gridLookup = EntitySystem.Get(); var list = new List(); - foreach (var v in gridLookup.GetEntitiesIntersecting(mapGrid.GridIndex, indices)) + foreach (var v in gridLookup.GetEntitiesIntersecting(_gridId, indices)) { if (v.TryGetComponent(out var ac)) list.Add(ac);