epic Respiration Rework (#6022)

This commit is contained in:
mirrorcult
2022-02-06 21:48:15 -07:00
committed by GitHub
parent ee4c5559a3
commit 4ae1c4e332
32 changed files with 596 additions and 586 deletions

View File

@@ -1,7 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Content.Server.Atmos;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Components;
using Content.Server.Body.Systems;
using Content.Shared.Atmos;
@@ -9,7 +9,6 @@ using Content.Shared.Body.Components;
using NUnit.Framework;
using Robust.Server.Maps;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -21,16 +20,17 @@ namespace Content.IntegrationTests.Tests.Body
{
private const string Prototypes = @"
- type: entity
name: HumanBodyAndBloodstreamDummy
id: HumanBodyAndBloodstreamDummy
name: HumanBodyDummy
id: HumanBodyDummy
components:
- type: Bloodstream
max_volume: 100
- type: SolutionContainerManager
- type: Body
template: HumanoidTemplate
preset: HumanPreset
centerSlot: torso
- type: MobState
thresholds:
0: !type:NormalMobState {}
- type: ThermalRegulator
metabolismHeat: 5000
radiatedHeat: 400
@@ -40,101 +40,84 @@ namespace Content.IntegrationTests.Tests.Body
normalBodyTemperature: 310.15
thermalRegulationTemperatureThreshold: 25
- type: Respirator
needsGases:
Oxygen: 0.00060763888
producesGases:
Oxygen: 0.00045572916
CarbonDioxide: 0.00015190972
";
[Test]
public async Task AirConsistencyTest()
{
// --- Setup
var options = new ServerContentIntegrationOption{ExtraPrototypes = Prototypes};
var server = StartServer(options);
server.Assert(() =>
await server.WaitIdleAsync();
var mapLoader = server.ResolveDependency<IMapLoader>();
var mapManager = server.ResolveDependency<IMapManager>();
var entityManager = server.ResolveDependency<IEntityManager>();
RespiratorSystem respSys = default;
MetabolizerSystem metaSys = default;
MapId mapId;
IMapGrid grid = null;
SharedBodyComponent body = default;
EntityUid human = default;
GridAtmosphereComponent relevantAtmos = default;
float startingMoles = 0.0f;
var testMapName = "Maps/Test/Breathing/3by3-20oxy-80nit.yml";
await server.WaitPost(() =>
{
var mapManager = IoCManager.Resolve<IMapManager>();
var mapId = mapManager.CreateMap();
var entityManager = IoCManager.Resolve<IEntityManager>();
var human = entityManager.SpawnEntity("HumanBodyAndBloodstreamDummy", new MapCoordinates(Vector2.Zero, mapId));
var bodySys = EntitySystem.Get<BodySystem>();
var lungSys = EntitySystem.Get<LungSystem>();
Assert.That(entityManager.TryGetComponent(human, out SharedBodyComponent body));
var lungs = bodySys.GetComponentsOnMechanisms<LungComponent>(human, body).ToArray();
Assert.That(lungs.Count, Is.EqualTo(1));
Assert.That(entityManager.TryGetComponent(human, out BloodstreamComponent bloodstream));
var gas = new GasMixture(1);
var originalOxygen = 2;
var originalNitrogen = 8;
var breathedPercentage = Atmospherics.BreathVolume / gas.Volume;
gas.AdjustMoles(Gas.Oxygen, originalOxygen);
gas.AdjustMoles(Gas.Nitrogen, originalNitrogen);
var (lung, _) = lungs[0];
lungSys.TakeGasFrom(((IComponent) lung).Owner, 1, gas, lung);
var lungOxygen = originalOxygen * breathedPercentage;
var lungNitrogen = originalNitrogen * breathedPercentage;
Assert.That(bloodstream.Air.GetMoles(Gas.Oxygen), Is.EqualTo(lungOxygen));
Assert.That(bloodstream.Air.GetMoles(Gas.Nitrogen), Is.EqualTo(lungNitrogen));
var mixtureOxygen = originalOxygen - lungOxygen;
var mixtureNitrogen = originalNitrogen - lungNitrogen;
Assert.That(gas.GetMoles(Gas.Oxygen), Is.EqualTo(mixtureOxygen));
Assert.That(gas.GetMoles(Gas.Nitrogen), Is.EqualTo(mixtureNitrogen));
var lungOxygenBeforeExhale = lung.Air.GetMoles(Gas.Oxygen);
var lungNitrogenBeforeExhale = lung.Air.GetMoles(Gas.Nitrogen);
// Empty after it transfer to the bloodstream
Assert.Zero(lungOxygenBeforeExhale);
Assert.Zero(lungNitrogenBeforeExhale);
lungSys.PushGasTo(((IComponent) lung).Owner, gas, lung);
var lungOxygenAfterExhale = lung.Air.GetMoles(Gas.Oxygen);
var exhaledOxygen = Math.Abs(lungOxygenBeforeExhale - lungOxygenAfterExhale);
// Not completely empty
Assert.Positive(lung.Air.Moles.Sum());
// Retains needed gas
Assert.Positive(bloodstream.Air.GetMoles(Gas.Oxygen));
// Expels toxins
Assert.Zero(bloodstream.Air.GetMoles(Gas.Nitrogen));
mixtureOxygen += exhaledOxygen;
var finalTotalOxygen = gas.GetMoles(Gas.Oxygen) +
bloodstream.Air.GetMoles(Gas.Oxygen) +
lung.Air.GetMoles(Gas.Oxygen);
// No ticks were run, metabolism doesn't run and so no oxygen is used up
Assert.That(finalTotalOxygen, Is.EqualTo(originalOxygen));
Assert.That(gas.GetMoles(Gas.Oxygen), Is.EqualTo(mixtureOxygen).Within(0.000001f));
var finalTotalNitrogen = gas.GetMoles(Gas.Nitrogen) +
bloodstream.Air.GetMoles(Gas.Nitrogen) +
lung.Air.GetMoles(Gas.Nitrogen);
// Nitrogen stays constant
Assert.That(finalTotalNitrogen, Is.EqualTo(originalNitrogen).Within(0.000001f));
mapId = mapManager.CreateMap();
grid = mapLoader.LoadBlueprint(mapId, testMapName);
});
Assert.NotNull(grid, $"Test blueprint {testMapName} not found.");
float GetMapMoles()
{
var totalMapMoles = 0.0f;
foreach (var tile in relevantAtmos.Tiles.Values)
{
totalMapMoles += tile.Air?.TotalMoles ?? 0.0f;
}
return totalMapMoles;
}
await server.WaitAssertion(() =>
{
var coords = new Vector2(0.5f, -1f);
var coordinates = new EntityCoordinates(grid.GridEntityId, coords);
human = entityManager.SpawnEntity("HumanBodyDummy", coordinates);
respSys = EntitySystem.Get<RespiratorSystem>();
metaSys = EntitySystem.Get<MetabolizerSystem>();
relevantAtmos = entityManager.GetComponent<GridAtmosphereComponent>(grid.GridEntityId);
startingMoles = GetMapMoles();
Assert.True(entityManager.TryGetComponent(human, out body));
Assert.True(entityManager.HasComponent<RespiratorComponent>(human));
});
// --- End setup
var inhaleCycles = 100;
for (var i = 0; i < inhaleCycles; i++)
{
await server.WaitAssertion(() =>
{
// inhale
respSys.Update(2.0f);
Assert.That(GetMapMoles(), Is.LessThan(startingMoles));
// metabolize + exhale
metaSys.Update(1.0f);
metaSys.Update(1.0f);
respSys.Update(2.0f);
Assert.That(GetMapMoles(), Is.EqualTo(startingMoles).Within(0.0001));
});
}
await server.WaitIdleAsync();
}
@@ -169,22 +152,21 @@ namespace Content.IntegrationTests.Tests.Body
{
var center = new Vector2(0.5f, -1.5f);
var coordinates = new EntityCoordinates(grid.GridEntityId, center);
human = entityManager.SpawnEntity("HumanBodyAndBloodstreamDummy", coordinates);
human = entityManager.SpawnEntity("HumanBodyDummy", coordinates);
Assert.True(entityManager.HasComponent<SharedBodyComponent>(human));
Assert.True(entityManager.TryGetComponent(human, out respirator));
Assert.False(respirator.Suffocating);
Assert.False(respirator.SuffocationCycles > respirator.SuffocationCycleThreshold);
});
var increment = 10;
for (var tick = 0; tick < 600; tick += increment)
{
await server.WaitRunTicks(increment);
await server.WaitAssertion(() =>
{
Assert.False(respirator.Suffocating, $"Entity {entityManager.GetComponent<MetaDataComponent>(human).EntityName} is suffocating on tick {tick}");
Assert.False(respirator.SuffocationCycles > respirator.SuffocationCycleThreshold, $"Entity {entityManager.GetComponent<MetaDataComponent>(human).EntityName} is suffocating on tick {tick}");
});
}