2022-02-10 17:53:10 +13:00
using System.Threading.Tasks ;
2020-09-12 22:52:50 +02:00
using Content.Server.Atmos ;
2022-02-10 17:53:10 +13:00
using Content.Server.Atmos.Components ;
using Content.Server.Atmos.EntitySystems ;
2021-11-11 16:10:57 -07:00
using Content.Server.Body.Components ;
2021-11-30 18:25:02 -07:00
using Content.Server.Body.Systems ;
2020-09-12 22:52:50 +02:00
using Content.Shared.Atmos ;
2021-06-09 22:19:39 +02:00
using Content.Shared.Body.Components ;
2020-09-12 22:52:50 +02:00
using NUnit.Framework ;
2021-02-11 01:13:03 -08:00
using Robust.Server.Maps ;
using Robust.Shared.GameObjects ;
2020-09-12 22:52:50 +02:00
using Robust.Shared.Map ;
using Robust.Shared.Maths ;
2020-10-10 15:25:13 +02:00
namespace Content.IntegrationTests.Tests.Body
2020-09-12 22:52:50 +02:00
{
[TestFixture]
2021-11-30 18:25:02 -07:00
[TestOf(typeof(LungSystem))]
2022-02-16 00:23:23 -07:00
public sealed class LungTest : ContentIntegrationTest
2020-09-12 22:52:50 +02:00
{
2021-02-09 22:04:47 +01:00
private const string Prototypes = @ "
2020-11-18 15:30:36 +01:00
- type : entity
2022-02-10 17:53:10 +13:00
name : HumanBodyDummy
id : HumanBodyDummy
2020-11-18 15:30:36 +01:00
components :
2021-09-06 15:49:44 +02:00
- type : SolutionContainerManager
2020-11-18 15:30:36 +01:00
- type : Body
template : HumanoidTemplate
preset : HumanPreset
centerSlot : torso
2022-02-10 17:53:10 +13:00
- type : MobState
thresholds :
0 : ! type : NormalMobState { }
2021-11-28 19:25:42 -07:00
- type : ThermalRegulator
2020-11-18 15:30:36 +01:00
metabolismHeat : 5000
radiatedHeat : 400
implicitHeatRegulation : 5000
sweatHeatRegulation : 5000
shiveringHeatRegulation : 5000
normalBodyTemperature : 310.15
thermalRegulationTemperatureThreshold : 25
2021-11-28 19:25:42 -07:00
- type : Respirator
2020-11-18 15:30:36 +01:00
";
2020-09-12 22:52:50 +02:00
[Test]
public async Task AirConsistencyTest ( )
{
2022-02-10 17:53:10 +13:00
// --- Setup
2021-02-09 22:04:47 +01:00
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes } ;
2021-11-06 11:49:59 +01:00
var server = StartServer ( options ) ;
2020-09-12 22:52:50 +02:00
2022-02-10 17:53:10 +13:00
await server . WaitIdleAsync ( ) ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
var mapLoader = server . ResolveDependency < IMapLoader > ( ) ;
var mapManager = server . ResolveDependency < IMapManager > ( ) ;
var entityManager = server . ResolveDependency < IEntityManager > ( ) ;
RespiratorSystem respSys = default ;
MetabolizerSystem metaSys = default ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
MapId mapId ;
IMapGrid grid = null ;
SharedBodyComponent body = default ;
EntityUid human = default ;
GridAtmosphereComponent relevantAtmos = default ;
float startingMoles = 0.0f ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
var testMapName = "Maps/Test/Breathing/3by3-20oxy-80nit.yml" ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
await server . WaitPost ( ( ) = >
{
mapId = mapManager . CreateMap ( ) ;
grid = mapLoader . LoadBlueprint ( mapId , testMapName ) ;
} ) ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
Assert . NotNull ( grid , $"Test blueprint {testMapName} not found." ) ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
float GetMapMoles ( )
{
var totalMapMoles = 0.0f ;
foreach ( var tile in relevantAtmos . Tiles . Values )
{
totalMapMoles + = tile . Air ? . TotalMoles ? ? 0.0f ;
}
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
return totalMapMoles ;
}
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
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 ) ) ;
} ) ;
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
// --- End setup
2022-02-07 20:13:14 +13:00
2022-02-10 17:53:10 +13:00
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 ) ) ;
} ) ;
}
2020-09-12 22:52:50 +02:00
await server . WaitIdleAsync ( ) ;
}
[Test]
public async Task NoSuffocationTest ( )
{
2021-02-09 22:04:47 +01:00
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes } ;
2021-11-06 11:49:59 +01:00
var server = StartServer ( options ) ;
2021-02-09 22:04:47 +01:00
2020-09-12 22:52:50 +02:00
await server . WaitIdleAsync ( ) ;
var mapLoader = server . ResolveDependency < IMapLoader > ( ) ;
var mapManager = server . ResolveDependency < IMapManager > ( ) ;
var entityManager = server . ResolveDependency < IEntityManager > ( ) ;
MapId mapId ;
IMapGrid grid = null ;
2021-07-31 04:50:32 -07:00
RespiratorComponent respirator = null ;
2021-12-05 18:09:01 +01:00
EntityUid human = default ;
2020-09-12 22:52:50 +02:00
var testMapName = "Maps/Test/Breathing/3by3-20oxy-80nit.yml" ;
await server . WaitPost ( ( ) = >
{
mapId = mapManager . CreateMap ( ) ;
grid = mapLoader . LoadBlueprint ( mapId , testMapName ) ;
} ) ;
Assert . NotNull ( grid , $"Test blueprint {testMapName} not found." ) ;
await server . WaitAssertion ( ( ) = >
{
var center = new Vector2 ( 0.5f , - 1.5f ) ;
var coordinates = new EntityCoordinates ( grid . GridEntityId , center ) ;
2022-02-10 17:53:10 +13:00
human = entityManager . SpawnEntity ( "HumanBodyDummy" , coordinates ) ;
2020-09-12 22:52:50 +02:00
2021-12-08 12:43:38 +01:00
Assert . True ( entityManager . HasComponent < SharedBodyComponent > ( human ) ) ;
Assert . True ( entityManager . TryGetComponent ( human , out respirator ) ) ;
2022-02-10 17:53:10 +13:00
Assert . False ( respirator . SuffocationCycles > respirator . SuffocationCycleThreshold ) ;
2020-09-12 22:52:50 +02:00
} ) ;
2020-10-14 11:36:34 +02:00
var increment = 10 ;
for ( var tick = 0 ; tick < 600 ; tick + = increment )
2020-09-12 22:52:50 +02:00
{
2020-10-14 11:36:34 +02:00
await server . WaitRunTicks ( increment ) ;
2021-12-03 10:25:07 +01:00
await server . WaitAssertion ( ( ) = >
{
2022-02-10 17:53:10 +13:00
Assert . False ( respirator . SuffocationCycles > respirator . SuffocationCycleThreshold , $"Entity {entityManager.GetComponent<MetaDataComponent>(human).EntityName} is suffocating on tick {tick}" ) ;
2021-12-03 10:25:07 +01:00
} ) ;
2020-09-12 22:52:50 +02:00
}
await server . WaitIdleAsync ( ) ;
}
}
}