Unique gas serialization for atmos (#1629)

* Add unique gas mixture serialization for atmos

* Refactor doAfterEventArgs

* Adds atmos commands

* Roundstard now has correct ratio of gases

* Fixed hashcode for gasmixture, better grid atmos serialization

* Airlocks create gas based on adjacent tiles now.

* Enables barotrauma component damage
This commit is contained in:
Víctor Aguilera Puerto
2020-08-09 16:52:59 +02:00
committed by GitHub
parent 4fe1083bfb
commit 7293d985a5
9 changed files with 11060 additions and 49 deletions

View File

@@ -259,10 +259,64 @@ namespace Content.Server.Atmos
}
}
public class ClearAtmos : IClientCommand
{
public string Command => "clearatmos";
public string Description => "Clear a grid of all gases.";
public string Help => "clearatmos <GridId>";
public void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
{
if (args.Length < 1) return;
if (!int.TryParse(args[0], out var id))
{
shell.SendText(player, "Not enough arguments!");
}
var gridId = new GridId(id);
var mapMan = IoCManager.Resolve<IMapManager>();
if (!gridId.IsValid() || !mapMan.TryGetGrid(gridId, out var gridComp))
{
shell.SendText(player, "Invalid grid ID.");
return;
}
var entMan = IoCManager.Resolve<IEntityManager>();
if (!entMan.TryGetEntity(gridComp.GridEntityId, out var grid))
{
shell.SendText(player, "Failed to get grid entity.");
return;
}
if (!grid.HasComponent<GridAtmosphereComponent>())
{
shell.SendText(player, "Grid doesn't have an atmosphere.");
return;
}
var gam = grid.GetComponent<GridAtmosphereComponent>();
var tiles = 0;
var moles = 0f;
foreach (var tile in gam)
{
if (tile.Air.Immutable) continue;
tiles++;
moles += tile.Air.TotalMoles;
tile.Air.RemoveRatio(1f);
gam.Invalidate(tile.GridIndices);
}
shell.SendText(player, $"Removed {moles} moles from {tiles} tiles.");
}
}
public class SetTemperature : IClientCommand
{
public string Command => "settemp";
public string Description => "Sets a tile's temperature.";
public string Description => "Sets a tile's temperature (in kelvin).";
public string Help => "Usage: settemp <X> <Y> <GridId> <Temperature>";
public void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
{
@@ -322,4 +376,63 @@ namespace Content.Server.Atmos
gam.Invalidate(indices);
}
}
public class SetAtmosTemperature : IClientCommand
{
public string Command => "setatmostemp";
public string Description => "Sets a grid's temperature (in kelvin).";
public string Help => "Usage: setatmostemp <GridId> <Temperature>";
public void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
{
if (args.Length < 2) return;
if(!int.TryParse(args[0], out var id)
|| !float.TryParse(args[1], out var temperature)) return;
var gridId = new GridId(id);
var mapMan = IoCManager.Resolve<IMapManager>();
if (temperature < Atmospherics.TCMB)
{
shell.SendText(player, "Invalid temperature.");
return;
}
if (!gridId.IsValid() || !mapMan.TryGetGrid(gridId, out var gridComp))
{
shell.SendText(player, "Invalid grid ID.");
return;
}
var entMan = IoCManager.Resolve<IEntityManager>();
if (!entMan.TryGetEntity(gridComp.GridEntityId, out var grid))
{
shell.SendText(player, "Failed to get grid entity.");
return;
}
if (!grid.HasComponent<GridAtmosphereComponent>())
{
shell.SendText(player, "Grid doesn't have an atmosphere.");
return;
}
var gam = grid.GetComponent<GridAtmosphereComponent>();
var tiles = 0;
foreach (var tile in gam)
{
if (tile.Air == null)
continue;
tiles++;
tile.Air.Temperature = temperature;
gam.Invalidate(tile.GridIndices);
}
shell.SendText(player, $"Changed the temperature of {tiles} tiles.");
}
}
}

View File

@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos.Reactions;
using Content.Server.Interfaces;
@@ -520,9 +521,10 @@ namespace Content.Server.Atmos
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(_moles, other._moles)
&& Equals(_molesArchived, other._molesArchived)
return _moles.SequenceEqual(other._moles)
&& _molesArchived.SequenceEqual(other._molesArchived)
&& _temperature.Equals(other._temperature)
&& ReactionResults.SequenceEqual(other.ReactionResults)
&& Immutable == other.Immutable
&& LastShare.Equals(other.LastShare)
&& TemperatureArchived.Equals(other.TemperatureArchived)
@@ -531,12 +533,28 @@ namespace Content.Server.Atmos
public override int GetHashCode()
{
return HashCode.Combine(_moles, _molesArchived, _temperature, Immutable, LastShare, TemperatureArchived, Volume);
var hashCode = new HashCode();
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var moles = _moles[i];
var molesArchived = _molesArchived[i];
hashCode.Add(moles);
hashCode.Add(molesArchived);
}
hashCode.Add(_temperature);
hashCode.Add(TemperatureArchived);
hashCode.Add(Immutable);
hashCode.Add(LastShare);
hashCode.Add(Volume);
return hashCode.ToHashCode();
}
public object Clone()
{
return new GasMixture()
var newMixture = new GasMixture()
{
_moles = (float[])_moles.Clone(),
_molesArchived = (float[])_molesArchived.Clone(),
@@ -546,6 +564,7 @@ namespace Content.Server.Atmos
TemperatureArchived = TemperatureArchived,
Volume = Volume,
};
return newMixture;
}
}
}

View File

@@ -21,6 +21,7 @@ namespace Content.Server.GameObjects.Components.Atmos
public override string Name => "Airtight";
private bool _airBlocked = true;
private bool _fixVacuum = false;
[ViewVariables(VVAccess.ReadWrite)]
public bool AirBlocked
@@ -33,11 +34,15 @@ namespace Content.Server.GameObjects.Components.Atmos
}
}
[ViewVariables]
public bool FixVacuum => _fixVacuum;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _airBlocked, "airBlocked", true);
serializer.DataField(ref _fixVacuum, "fixVacuum", false);
}
public override void Initialize()

View File

@@ -4,6 +4,7 @@ using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces.GameObjects;
using Content.Shared.Atmos;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Mobs;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
@@ -50,8 +51,7 @@ namespace Content.Server.GameObjects.Components.Atmos
if(pressure > Atmospherics.WarningLowPressure)
goto default;
// TODO ATMOS Uncomment this when saltern is pressurized
//damageable.TakeDamage(DamageType.Brute, Atmospherics.LowPressureDamage, Owner, null);
damageable.TakeDamage(DamageType.Brute, Atmospherics.LowPressureDamage, Owner);
if (status == null) break;
@@ -73,8 +73,7 @@ namespace Content.Server.GameObjects.Components.Atmos
var damage = (int) MathF.Min((pressure / Atmospherics.HazardHighPressure) * Atmospherics.PressureDamageCoefficient, Atmospherics.MaxHighPressureDamage);
// TODO ATMOS Uncomment this when saltern is pressurized
//damageable.TakeDamage(DamageType.Brute, damage, Owner, null);
damageable.TakeDamage(DamageType.Brute, damage, Owner);
if (status == null) break;

View File

@@ -122,8 +122,6 @@ namespace Content.Server.GameObjects.Components.Atmos
public void RepopulateTiles()
{
_tiles.Clear();
foreach (var tile in _grid.GetAllTiles())
{
if(!_tiles.ContainsKey(tile.GridIndices))
@@ -160,13 +158,34 @@ namespace Content.Server.GameObjects.Components.Atmos
{
tile.Air = new GasMixture(GetVolumeForCells(1));
tile.Air.MarkImmutable();
} else if (IsAirBlocked(indices))
{
tile.Air = null;
}
else
{
tile.Air ??= new GasMixture(GetVolumeForCells(1));
var obs = GetObstructingComponent(indices);
if (obs != null)
{
if (tile.Air == null && obs.FixVacuum)
{
var adjacent = GetAdjacentTiles(indices);
tile.Air = new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C};
var ratio = 1f / adjacent.Count;
foreach (var (direction, adj) in adjacent)
{
var mix = adj.Air.RemoveRatio(ratio);
tile.Air.Merge(mix);
adj.Air.Merge(mix);
}
}
}
tile.Air ??= new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C};
}
tile.UpdateAdjacent();
@@ -290,7 +309,9 @@ namespace Content.Server.GameObjects.Components.Atmos
foreach (var dir in Cardinal())
{
var side = indices.Offset(dir);
sides[dir] = GetTile(side);
var tile = GetTile(side);
if(tile?.Air != null)
sides[dir] = tile;
}
return sides;
@@ -475,6 +496,9 @@ namespace Content.Server.GameObjects.Components.Atmos
if (!serializer.TryReadDataField("uniqueMixes", out List<GasMixture> uniqueMixes) ||
!serializer.TryReadDataField("tiles", out Dictionary<MapIndices, int> tiles))
return;
_tiles.Clear();
foreach (var (indices, mix) in tiles)
{
_tiles.Add(indices, new TileAtmosphere(this, gridId, indices, (GasMixture)uniqueMixes[mix].Clone()));
@@ -483,12 +507,22 @@ namespace Content.Server.GameObjects.Components.Atmos
} else if (serializer.Writing)
{
var uniqueMixes = new List<GasMixture>();
var uniqueMixHash = new Dictionary<GasMixture, int>();
var tiles = new Dictionary<MapIndices, int>();
foreach (var (indices, tile) in _tiles)
{
if (tile.Air == null) continue;
if (uniqueMixHash.TryGetValue(tile.Air, out var index))
{
tiles[indices] = index;
continue;
}
uniqueMixes.Add(tile.Air);
tiles[indices] = uniqueMixes.Count - 1;
var newIndex = uniqueMixes.Count - 1;
uniqueMixHash[tile.Air] = newIndex;
tiles[indices] = newIndex;
}
serializer.DataField(ref uniqueMixes, "uniqueMixes", new List<GasMixture>());

View File

@@ -63,6 +63,12 @@ namespace Content.Shared.Atmos
/// </summary>
public const float MolesCellStandard = (OneAtmosphere * CellVolume / (T20C * R));
public const float OxygenStandard = 0.21f;
public const float NitrogenStandard = 0.79f;
public const float OxygenMolesStandard = MolesCellStandard * OxygenStandard;
public const float NitrogenMolesStandard = MolesCellStandard * NitrogenStandard;
#endregion
/// <summary>

View File

@@ -176,7 +176,9 @@
- fillgas
- listgases
- removegas
- clearatmos
- settemp
- setatmostemp
CanViewVar: true
CanAdminPlace: true
CanScript: true

File diff suppressed because it is too large Load Diff

View File

@@ -52,6 +52,7 @@
- key: enum.WiresUiKey.Key
type: WiresBoundUserInterface
- type: Airtight
fixVacuum: true
adjacentAtmosphere: true
- type: Occluder
- type: SnapGrid