2022-07-06 17:58:14 +10:00
using System.Threading.Tasks ;
2022-02-10 17:53:10 +13:00
using Content.Server.Atmos.Components ;
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 ;
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-06-19 20:22:28 -07:00
public sealed class LungTest
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 :
2022-07-06 17:58:14 +10:00
0 : Alive
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
2022-04-03 02:01:22 +02:00
damage :
types :
Asphyxiation : 1.5
damageRecovery :
types :
Asphyxiation : - 1.5
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
2022-06-19 20:22:28 -07:00
await using var pairTracker = await PoolManager . GetServerClient ( new PoolSettings { NoClient = true , ExtraPrototypes = Prototypes } ) ;
var server = pairTracker . Pair . Server ;
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 ;
2022-06-11 18:54:41 -07:00
EntityUid ? grid = null ;
2022-02-10 17:53:10 +13:00
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 ( ) ;
2022-04-15 16:35:58 -05:00
grid = mapLoader . LoadBlueprint ( mapId , testMapName ) . gridId ;
2022-02-10 17:53:10 +13:00
} ) ;
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 ) ;
2022-06-11 18:54:41 -07:00
var coordinates = new EntityCoordinates ( grid . Value , coords ) ;
2022-02-10 17:53:10 +13:00
human = entityManager . SpawnEntity ( "HumanBodyDummy" , coordinates ) ;
respSys = EntitySystem . Get < RespiratorSystem > ( ) ;
metaSys = EntitySystem . Get < MetabolizerSystem > ( ) ;
2022-06-11 18:54:41 -07:00
relevantAtmos = entityManager . GetComponent < GridAtmosphereComponent > ( grid . Value ) ;
2022-02-10 17:53:10 +13:00
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
2022-06-19 20:22:28 -07:00
await pairTracker . CleanReturnAsync ( ) ;
2020-09-12 22:52:50 +02:00
}
[Test]
public async Task NoSuffocationTest ( )
{
2022-06-19 20:22:28 -07:00
await using var pairTracker = await PoolManager . GetServerClient ( new PoolSettings { NoClient = true , ExtraPrototypes = Prototypes } ) ;
var server = pairTracker . Pair . Server ;
2020-09-12 22:52:50 +02:00
var mapLoader = server . ResolveDependency < IMapLoader > ( ) ;
var mapManager = server . ResolveDependency < IMapManager > ( ) ;
var entityManager = server . ResolveDependency < IEntityManager > ( ) ;
MapId mapId ;
2022-06-11 18:54:41 -07:00
EntityUid ? 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 ( ) ;
2022-04-15 16:35:58 -05:00
grid = mapLoader . LoadBlueprint ( mapId , testMapName ) . gridId ;
2020-09-12 22:52:50 +02:00
} ) ;
Assert . NotNull ( grid , $"Test blueprint {testMapName} not found." ) ;
await server . WaitAssertion ( ( ) = >
{
var center = new Vector2 ( 0.5f , - 1.5f ) ;
2022-06-11 18:54:41 -07:00
var coordinates = new EntityCoordinates ( grid . Value , 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
}
2022-06-19 20:22:28 -07:00
await pairTracker . CleanReturnAsync ( ) ;
2020-09-12 22:52:50 +02:00
}
}
}