AtmosphereSystem no longer creates a component manually. (#3839)

- Maps get SpaceGridAtmosphereComponent added automatically
This commit is contained in:
Vera Aguilera Puerto
2021-04-13 13:17:10 +02:00
committed by GitHub
parent 30d5b58319
commit c17426dfa7
21 changed files with 97 additions and 257 deletions

View File

@@ -3,26 +3,20 @@ using System.Diagnostics.CodeAnalysis;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.Atmos
{
public static class AtmosHelpers
{
public static TileAtmosphere? GetTileAtmosphere(this EntityCoordinates coordinates, IEntityManager? entityManager = null)
public static TileAtmosphere? GetTileAtmosphere(this EntityCoordinates coordinates)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
var gridAtmos = EntitySystem.Get<AtmosphereSystem>().GetGridAtmosphere(coordinates.GetGridId(entityManager));
return gridAtmos.GetTile(coordinates);
return EntitySystem.Get<AtmosphereSystem>().GetGridAtmosphere(coordinates).GetTile(coordinates);
}
public static GasMixture? GetTileAir(this EntityCoordinates coordinates, IEntityManager? entityManager = null)
{
return coordinates.GetTileAtmosphere(entityManager)?.Air;
return coordinates.GetTileAtmosphere()?.Air;
}
public static bool TryGetTileAtmosphere(this EntityCoordinates coordinates, [NotNullWhen(true)] out TileAtmosphere? atmosphere)
@@ -54,36 +48,6 @@ namespace Content.Server.Atmos
return true;
}
public static TileAtmosphere? GetTileAtmosphere(this Vector2i indices, GridId gridId)
{
var gridAtmos = EntitySystem.Get<AtmosphereSystem>().GetGridAtmosphere(gridId);
return gridAtmos.GetTile(indices);
}
public static GasMixture? GetTileAir(this Vector2i indices, GridId gridId)
{
return indices.GetTileAtmosphere(gridId)?.Air;
}
public static bool TryGetTileAtmosphere(this Vector2i indices, GridId gridId,
[NotNullWhen(true)] out TileAtmosphere? atmosphere)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
return !Equals(atmosphere = indices.GetTileAtmosphere(gridId), default);
}
public static bool TryGetTileAir(this Vector2i indices, GridId gridId, [NotNullWhen(true)] out GasMixture? air)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
return !Equals(air = indices.GetTileAir(gridId), default);
}
public static bool InvalidateTileAir(this ITransformComponent transform, AtmosphereSystem? atmosSystem = null)
{
return InvalidateTileAir(transform.Coordinates);
}
public static bool InvalidateTileAir(this EntityCoordinates coordinates)
{
if (!coordinates.TryGetTileAtmosphere(out var tileAtmos))

View File

@@ -157,6 +157,11 @@ namespace Content.Server.Atmos
void RepopulateTiles();
/// <summary>
/// Returns a dictionary of adjacent TileAtmospheres.
/// </summary>
Dictionary<AtmosDirection, TileAtmosphere> GetAdjacentTiles(EntityCoordinates coordinates, bool includeAirBlocked = false);
/// <summary>
/// Returns a dictionary of adjacent TileAtmospheres.
/// </summary>

View File

@@ -3,6 +3,7 @@ using Content.Server.Atmos;
using Content.Shared.Atmos;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Maps;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -18,7 +19,7 @@ namespace Content.Server.Chemistry.TileReactions
public ReagentUnit TileReact(TileRef tile, ReagentPrototype reagent, ReagentUnit reactVolume)
{
if (reactVolume <= ReagentUnit.Zero || tile.Tile.IsEmpty) return ReagentUnit.Zero;
var tileAtmos = tile.GridIndices.GetTileAtmosphere(tile.GridIndex);
var tileAtmos = tile.GridPosition().GetTileAtmosphere();
if (tileAtmos == null || !tileAtmos.Hotspot.Valid || tileAtmos.Air == null) return ReagentUnit.Zero;
tileAtmos.Air.Temperature =
MathF.Max(MathF.Min(tileAtmos.Air.Temperature - (_coolingTemperature * 1000f),

View File

@@ -2,6 +2,7 @@
using Content.Server.Atmos;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Maps;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -17,7 +18,7 @@ namespace Content.Server.Chemistry.TileReactions
public ReagentUnit TileReact(TileRef tile, ReagentPrototype reagent, ReagentUnit reactVolume)
{
if (reactVolume <= ReagentUnit.Zero || tile.Tile.IsEmpty) return ReagentUnit.Zero;
var tileAtmos = tile.GridIndices.GetTileAtmosphere(tile.GridIndex);
var tileAtmos = tile.GridPosition().GetTileAtmosphere();
if (tileAtmos?.Air == null || !tileAtmos.Hotspot.Valid) return ReagentUnit.Zero;
tileAtmos.Air.Temperature *= MathF.Max(_temperatureMultiplier * reactVolume.Float(), 1f);
tileAtmos.Air.React(tileAtmos);

View File

@@ -141,7 +141,9 @@ namespace Content.Server.GameObjects.Components.Atmos
UpdatePosition(_lastPosition.Item1, _lastPosition.Item2);
if (_fixVacuum)
_atmosphereSystem.GetGridAtmosphere(_lastPosition.Item1).FixVacuum(_lastPosition.Item2);
{
_atmosphereSystem.GetGridAtmosphere(_lastPosition.Item1)?.FixVacuum(_lastPosition.Item2);
}
}
private void OnTransformMove()
@@ -165,8 +167,8 @@ namespace Content.Server.GameObjects.Components.Atmos
{
var gridAtmos = _atmosphereSystem.GetGridAtmosphere(gridId);
gridAtmos.UpdateAdjacentBits(pos);
gridAtmos.Invalidate(pos);
gridAtmos?.UpdateAdjacentBits(pos);
gridAtmos?.Invalidate(pos);
}
}
}

View File

@@ -61,7 +61,7 @@ namespace Content.Server.GameObjects.Components.Atmos
{
return;
}
if (IsHoldingPressure())
{
Owner.PopupMessage(eventArgs.User, Loc.GetString("A gush of air blows in your face... Maybe you should reconsider."));
@@ -76,15 +76,12 @@ namespace Content.Server.GameObjects.Components.Atmos
{
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
if (!Owner.Transform.Coordinates.TryGetTileAtmosphere(out var tileAtmos))
return false;
var gridAtmosphere = atmosphereSystem.GetGridAtmosphere(Owner.Transform.GridID);
var gridAtmosphere = atmosphereSystem.GetGridAtmosphere(Owner.Transform.Coordinates);
var minMoles = float.MaxValue;
var maxMoles = 0f;
foreach (var (_, adjacent) in gridAtmosphere.GetAdjacentTiles(tileAtmos.GridIndices))
foreach (var (_, adjacent) in gridAtmosphere.GetAdjacentTiles(Owner.Transform.Coordinates))
{
// includeAirBlocked remains false, and therefore Air must be present
var moles = adjacent.Air!.TotalMoles;
@@ -107,7 +104,7 @@ namespace Content.Server.GameObjects.Components.Atmos
if (tileAtmos.Hotspot.Valid)
return true;
var gridAtmosphere = atmosphereSystem.GetGridAtmosphere(Owner.Transform.GridID);
var gridAtmosphere = atmosphereSystem.GetGridAtmosphere(Owner.Transform.Coordinates);
foreach (var (_, adjacent) in gridAtmosphere.GetAdjacentTiles(tileAtmos.GridIndices))
{

View File

@@ -125,7 +125,7 @@ namespace Content.Server.GameObjects.Components.Atmos
{
// Already get the pressure before Dirty(), because we can't get the EntitySystem in that thread or smth
var pressure = 0f;
var gam = EntitySystem.Get<AtmosphereSystem>().GetGridAtmosphere(Owner.Transform.GridID);
var gam = EntitySystem.Get<AtmosphereSystem>().GetGridAtmosphere(Owner.Transform.Coordinates);
var tile = gam?.GetTile(Owner.Transform.Coordinates)?.Air;
if (tile != null)
{
@@ -185,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Atmos
}
var atmosSystem = EntitySystem.Get<AtmosphereSystem>();
var gam = atmosSystem.GetGridAtmosphere(pos.GetGridId(Owner.EntityManager));
var gam = atmosSystem.GetGridAtmosphere(pos);
var tile = gam.GetTile(pos)?.Air;
if (tile == null)
{

View File

@@ -514,6 +514,11 @@ namespace Content.Server.GameObjects.Components.Atmos
return _mapGridComponent.Grid.GetTileRef(indices).IsSpace();
}
public Dictionary<AtmosDirection, TileAtmosphere> GetAdjacentTiles(EntityCoordinates coordinates, bool includeAirBlocked = false)
{
return GetAdjacentTiles(coordinates.ToVector2i(_serverEntityManager, _mapManager), includeAirBlocked);
}
public Dictionary<AtmosDirection, TileAtmosphere> GetAdjacentTiles(Vector2i indices, bool includeAirBlocked = false)
{
var sides = new Dictionary<AtmosDirection, TileAtmosphere>();

View File

@@ -38,7 +38,7 @@ namespace Content.Server.GameObjects.Components.Atmos.Piping
private void JoinGridAtmos()
{
var gridAtmos = EntitySystem.Get<AtmosphereSystem>()
.GetGridAtmosphere(Owner.Transform.GridID);
.GetGridAtmosphere(Owner.Transform.Coordinates);
JoinedGridAtmos = gridAtmos;
JoinedGridAtmos.AddPipeNetDevice(this);
}

View File

@@ -63,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Atmos.Piping.Scrubbers
if (!SiphonEnabled)
return;
var tileAtmos = Owner.Transform.Coordinates.GetTileAtmosphere(Owner.EntityManager);
var tileAtmos = Owner.Transform.Coordinates.GetTileAtmosphere();
if (_scrubberOutlet == null || tileAtmos == null || tileAtmos.Air == null)
return;

View File

@@ -63,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Atmos.Piping.Vents
if (!VentEnabled)
return;
var tileAtmos = Owner.Transform.Coordinates.GetTileAtmosphere(Owner.EntityManager);
var tileAtmos = Owner.Transform.Coordinates.GetTileAtmosphere();
if (_ventInlet == null || tileAtmos == null || tileAtmos.Air == null)
return;

View File

@@ -2,11 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.GameObjects.Components.Atmos
{
[RegisterComponent]
[ComponentReference(typeof(IGridAtmosphereComponent))]
public class SpaceGridAtmosphereComponent : UnsimulatedGridAtmosphereComponent
{
public override string Name => "SpaceGridAtmosphere";

View File

@@ -283,7 +283,7 @@ namespace Content.Server.GameObjects.Components.Disposal
var atmosSystem = EntitySystem.Get<AtmosphereSystem>();
atmosSystem
.GetGridAtmosphere(Owner.Transform.GridID)?
.GetGridAtmosphere(Owner.Transform.Coordinates)?
.Invalidate(tileAtmos.GridIndices);
}

View File

@@ -21,7 +21,7 @@ namespace Content.Server.GameObjects.EntitySystems
// creadth: everything exposable by atmos should be updated as well
foreach (var atmosExposedComponent in EntityManager.ComponentManager.EntityQuery<AtmosExposedComponent>(true))
{
var tile = atmosExposedComponent.Owner.Transform.Coordinates.GetTileAtmosphere(EntityManager);
var tile = atmosExposedComponent.Owner.Transform.Coordinates.GetTileAtmosphere();
if (tile == null) continue;
atmosExposedComponent.Update(tile, _lastUpdate);
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using Content.Server.Atmos;
using Content.Server.Atmos.Reactions;
using Content.Server.GameObjects.Components.Atmos;
using Content.Server.GameObjects.Components.NodeContainer.Nodes;
using Content.Shared;
using Content.Shared.Atmos;
using Content.Shared.GameObjects.EntitySystems.Atmos;
@@ -31,7 +32,6 @@ namespace Content.Server.GameObjects.EntitySystems
private GasReactionPrototype[] _gasReactions = Array.Empty<GasReactionPrototype>();
private SpaceGridAtmosphereComponent _spaceAtmos = default!;
private GridTileLookupSystem? _gridTileLookup = null;
/// <summary>
@@ -51,10 +51,7 @@ namespace Content.Server.GameObjects.EntitySystems
_gasReactions = _protoMan.EnumeratePrototypes<GasReactionPrototype>().ToArray();
Array.Sort(_gasReactions, (a, b) => b.Priority.CompareTo(a.Priority));
_spaceAtmos = new SpaceGridAtmosphereComponent();
_spaceAtmos.Initialize();
IoCManager.InjectDependencies(_spaceAtmos);
_mapManager.MapCreated += OnMapCreated;
_mapManager.TileChanged += OnTileChanged;
Array.Resize(ref _gasSpecificHeats, MathHelper.NextMultipleOf(Atmospherics.TotalNumberOfGases, 4));
@@ -116,6 +113,8 @@ namespace Content.Server.GameObjects.EntitySystems
{
base.Shutdown();
_mapManager.MapCreated -= OnMapCreated;
EntityManager.EventBus.UnsubscribeEvent<RotateEvent>(EventSource.Local, this);
}
@@ -127,18 +126,39 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
public IGridAtmosphereComponent GetGridAtmosphere(GridId gridId)
public IGridAtmosphereComponent? GetGridAtmosphere(GridId gridId)
{
if (!gridId.IsValid())
return null;
if (!_mapManager.TryGetGrid(gridId, out var grid))
return null;
return ComponentManager.TryGetComponent(grid.GridEntityId, out IGridAtmosphereComponent? gridAtmosphere)
? gridAtmosphere : null;
}
public IGridAtmosphereComponent GetGridAtmosphere(EntityCoordinates coordinates)
{
return GetGridAtmosphere(coordinates.ToMap(EntityManager));
}
public IGridAtmosphereComponent GetGridAtmosphere(MapCoordinates coordinates)
{
if (coordinates.MapId == MapId.Nullspace)
{
return _spaceAtmos;
throw new ArgumentException($"Coordinates cannot be in nullspace!", nameof(coordinates));
}
var grid = _mapManager.GetGrid(gridId);
if (_mapManager.TryFindGridAt(coordinates, out var grid))
{
if (ComponentManager.TryGetComponent(grid.GridEntityId, out IGridAtmosphereComponent? atmos))
{
return atmos;
}
}
if (!EntityManager.TryGetEntity(grid.GridEntityId, out var gridEnt)) return _spaceAtmos;
return gridEnt.TryGetComponent(out IGridAtmosphereComponent? atmos) ? atmos : _spaceAtmos;
return _mapManager.GetMapEntity(coordinates.MapId).GetComponent<IGridAtmosphereComponent>();
}
public override void Update(float frameTime)
@@ -164,7 +184,18 @@ namespace Content.Server.GameObjects.EntitySystems
return;
}
GetGridAtmosphere(eventArgs.NewTile.GridIndex)?.Invalidate(eventArgs.NewTile.GridIndices);
GetGridAtmosphere(eventArgs.NewTile.GridPosition(_mapManager))?.Invalidate(eventArgs.NewTile.GridIndices);
}
private void OnMapCreated(object? sender, MapEventArgs e)
{
if (e.Map == MapId.Nullspace)
return;
var map = _mapManager.GetMapEntity(e.Map);
if (!map.HasComponent<IGridAtmosphereComponent>())
map.AddComponent<SpaceGridAtmosphereComponent>();
}
}
}