Files
OldThink/Content.Server/Atmos/Miasma/MiasmaSystem.cs

281 lines
10 KiB
C#
Raw Normal View History

using Content.Shared.MobState;
using Content.Shared.Damage;
using Content.Shared.Atmos;
using Content.Server.Atmos.EntitySystems;
2022-06-24 04:20:52 -04:00
using Content.Server.Temperature.Systems;
using Content.Server.Body.Components;
2022-06-19 01:51:55 -04:00
using Content.Shared.Examine;
Refactors the AtmosphereSystem public-facing API to allow for multiple atmos backends. (#8134) * Refactors the entirety of the AtmosphereSystem public-facing API to allow for multiple atmos backends. * actually compiles * Remove commented out code * funny bracket * Move archived moles, temperature from GasMixture to TileAtmosphere. * WIP customizable map default mixture still VERY buggy * broken mess aaaaaaaaaaaaa * Fix lattice, etc not being considered space * visualization for "IsSpace" * help * Update Content.Client/Atmos/Overlays/AtmosDebugOverlay.cs Co-authored-by: Moony <moonheart08@users.noreply.github.com> * Holy SHIT it compiles AGAIN * Fix AtmosDeviceSystem crash at shutdown * Fix immutable tiles on map blueprints not being fixed by fixgridatmos/revalidate. * Use space instead of gasmixture immutable for heat capacity calculations * Remove all LINDA-specific code from GasMixture, move it to TileAtmosphere/AtmosphereSystem instead. * Fix roundstart tiles not processing * Update Content.Server/Atmos/Commands/SetTemperatureCommand.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Update Content.Server/Atmos/EntitySystems/AtmosphereSystem.API.cs Changed Files tab is so large I can't commit both suggestions at once mfw Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Moony <moonheart08@users.noreply.github.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-07-04 16:51:34 +02:00
using Robust.Server.GameObjects;
2022-07-02 22:46:24 -04:00
using Content.Shared.Tag;
using Robust.Shared.Containers;
using Robust.Shared.Random;
namespace Content.Server.Atmos.Miasma
{
public sealed class MiasmaSystem : EntitySystem
{
Refactors the AtmosphereSystem public-facing API to allow for multiple atmos backends. (#8134) * Refactors the entirety of the AtmosphereSystem public-facing API to allow for multiple atmos backends. * actually compiles * Remove commented out code * funny bracket * Move archived moles, temperature from GasMixture to TileAtmosphere. * WIP customizable map default mixture still VERY buggy * broken mess aaaaaaaaaaaaa * Fix lattice, etc not being considered space * visualization for "IsSpace" * help * Update Content.Client/Atmos/Overlays/AtmosDebugOverlay.cs Co-authored-by: Moony <moonheart08@users.noreply.github.com> * Holy SHIT it compiles AGAIN * Fix AtmosDeviceSystem crash at shutdown * Fix immutable tiles on map blueprints not being fixed by fixgridatmos/revalidate. * Use space instead of gasmixture immutable for heat capacity calculations * Remove all LINDA-specific code from GasMixture, move it to TileAtmosphere/AtmosphereSystem instead. * Fix roundstart tiles not processing * Update Content.Server/Atmos/Commands/SetTemperatureCommand.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Update Content.Server/Atmos/EntitySystems/AtmosphereSystem.API.cs Changed Files tab is so large I can't commit both suggestions at once mfw Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Moony <moonheart08@users.noreply.github.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-07-04 16:51:34 +02:00
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// System Variables
/// Rotting
/// <summary>
/// How often the rotting ticks.
/// Feel free to weak this if there are perf concerns.
/// </summary>
private float _rotUpdateRate = 5f;
/// Miasma Disease Pool
/// Miasma outbreaks are not per-entity,
/// so this ensures that each entity in the same incident
/// receives the same disease.
public readonly IReadOnlyList<string> MiasmaDiseasePool = new[]
{
"VentCough",
"AMIV",
"SpaceCold",
"SpaceFlu",
"BirdFlew",
"VanAusdallsRobovirus",
"BleedersBite",
"Plague",
2022-07-27 00:46:24 -04:00
"TongueTwister",
"MemeticAmirmir"
};
/// <summary>
/// The current pool disease.
/// </summary>
private string _poolDisease = "";
/// <summary>
/// The list of diseases in the pool.
/// </summary>
/// <summary>
/// This ticks up to PoolRepickTime.
/// After that, it resets to 0.
/// Any infection will also reset it to 0.
/// </summary>
private float _poolAccumulator = 0f;
/// <summmary>
/// How long without an infection before we pick a new disease.
/// </summary>
private TimeSpan _poolRepickTime = TimeSpan.FromMinutes(5);
2022-06-19 01:51:55 -04:00
public override void Update(float frameTime)
{
base.Update(frameTime);
// Disease pool
_poolAccumulator += frameTime;
if (_poolAccumulator > _poolRepickTime.TotalSeconds)
{
_poolAccumulator = 0f;
_poolDisease = _random.Pick(MiasmaDiseasePool);
}
// Rotting
foreach (var (rotting, perishable) in EntityQuery<RottingComponent, PerishableComponent>())
{
if (!perishable.Progressing)
continue;
perishable.DeathAccumulator += frameTime;
if (perishable.DeathAccumulator < perishable.RotAfter.TotalSeconds)
continue;
perishable.RotAccumulator += frameTime;
if (perishable.RotAccumulator < _rotUpdateRate) // This is where it starts to get noticable on larger animals, no need to run every second
continue;
perishable.RotAccumulator -= _rotUpdateRate;
2022-06-24 04:20:52 -04:00
EnsureComp<FliesComponent>(perishable.Owner);
2022-07-26 20:35:34 -04:00
if (rotting.DealDamage)
{
DamageSpecifier damage = new();
damage.DamageDict.Add("Blunt", 0.3); // Slowly accumulate enough to gib after like half an hour
damage.DamageDict.Add("Cellular", 0.3); // Cloning rework might use this eventually
_damageableSystem.TryChangeDamage(perishable.Owner, damage, true, true);
}
if (!TryComp<PhysicsComponent>(perishable.Owner, out var physics))
continue;
// We need a way to get the mass of the mob alone without armor etc in the future
float molRate = perishable.MolsPerSecondPerUnitMass * _rotUpdateRate;
2022-06-24 04:20:52 -04:00
Refactors the AtmosphereSystem public-facing API to allow for multiple atmos backends. (#8134) * Refactors the entirety of the AtmosphereSystem public-facing API to allow for multiple atmos backends. * actually compiles * Remove commented out code * funny bracket * Move archived moles, temperature from GasMixture to TileAtmosphere. * WIP customizable map default mixture still VERY buggy * broken mess aaaaaaaaaaaaa * Fix lattice, etc not being considered space * visualization for "IsSpace" * help * Update Content.Client/Atmos/Overlays/AtmosDebugOverlay.cs Co-authored-by: Moony <moonheart08@users.noreply.github.com> * Holy SHIT it compiles AGAIN * Fix AtmosDeviceSystem crash at shutdown * Fix immutable tiles on map blueprints not being fixed by fixgridatmos/revalidate. * Use space instead of gasmixture immutable for heat capacity calculations * Remove all LINDA-specific code from GasMixture, move it to TileAtmosphere/AtmosphereSystem instead. * Fix roundstart tiles not processing * Update Content.Server/Atmos/Commands/SetTemperatureCommand.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Update Content.Server/Atmos/EntitySystems/AtmosphereSystem.API.cs Changed Files tab is so large I can't commit both suggestions at once mfw Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Moony <moonheart08@users.noreply.github.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-07-04 16:51:34 +02:00
var transform = Transform(perishable.Owner);
var indices = _transformSystem.GetGridOrMapTilePosition(perishable.Owner);
var tileMix = _atmosphereSystem.GetTileMixture(transform.GridUid, null, indices, true);
tileMix?.AdjustMoles(Gas.Miasma, molRate * physics.FixturesMass);
}
}
public override void Initialize()
{
base.Initialize();
2022-06-24 04:20:52 -04:00
// Core rotting stuff
SubscribeLocalEvent<RottingComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<RottingComponent, OnTemperatureChangeEvent>(OnTempChange);
SubscribeLocalEvent<PerishableComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<PerishableComponent, BeingGibbedEvent>(OnGibbed);
2022-06-19 01:51:55 -04:00
SubscribeLocalEvent<PerishableComponent, ExaminedEvent>(OnExamined);
2022-06-24 04:20:52 -04:00
// Containers
SubscribeLocalEvent<AntiRottingContainerComponent, EntInsertedIntoContainerMessage>(OnEntInserted);
SubscribeLocalEvent<AntiRottingContainerComponent, EntRemovedFromContainerMessage>(OnEntRemoved);
2022-06-24 04:20:52 -04:00
// Fly audiovisual stuff
SubscribeLocalEvent<FliesComponent, ComponentInit>(OnFliesInit);
SubscribeLocalEvent<FliesComponent, ComponentShutdown>(OnFliesShutdown);
// Init disease pool
_poolDisease = _random.Pick(MiasmaDiseasePool);
2022-06-24 04:20:52 -04:00
}
private void OnShutdown(EntityUid uid, RottingComponent component, ComponentShutdown args)
{
RemComp<FliesComponent>(uid);
if (TryComp<PerishableComponent>(uid, out var perishable))
{
perishable.DeathAccumulator = 0;
perishable.RotAccumulator = 0;
}
}
private void OnTempChange(EntityUid uid, RottingComponent component, OnTemperatureChangeEvent args)
{
2022-07-02 22:46:24 -04:00
if (HasComp<BodyPreservedComponent>(uid))
return;
2022-06-24 04:20:52 -04:00
bool decompose = (args.CurrentTemperature > 274f);
ToggleDecomposition(uid, decompose);
}
private void OnMobStateChanged(EntityUid uid, PerishableComponent component, MobStateChangedEvent args)
{
if (args.Component.IsDead())
EnsureComp<RottingComponent>(uid);
}
private void OnGibbed(EntityUid uid, PerishableComponent component, BeingGibbedEvent args)
{
if (!TryComp<PhysicsComponent>(uid, out var physics))
return;
2022-06-24 04:20:52 -04:00
if (!component.Rotting)
2022-06-19 01:51:55 -04:00
return;
var molsToDump = (component.MolsPerSecondPerUnitMass * physics.FixturesMass) * component.DeathAccumulator;
Refactors the AtmosphereSystem public-facing API to allow for multiple atmos backends. (#8134) * Refactors the entirety of the AtmosphereSystem public-facing API to allow for multiple atmos backends. * actually compiles * Remove commented out code * funny bracket * Move archived moles, temperature from GasMixture to TileAtmosphere. * WIP customizable map default mixture still VERY buggy * broken mess aaaaaaaaaaaaa * Fix lattice, etc not being considered space * visualization for "IsSpace" * help * Update Content.Client/Atmos/Overlays/AtmosDebugOverlay.cs Co-authored-by: Moony <moonheart08@users.noreply.github.com> * Holy SHIT it compiles AGAIN * Fix AtmosDeviceSystem crash at shutdown * Fix immutable tiles on map blueprints not being fixed by fixgridatmos/revalidate. * Use space instead of gasmixture immutable for heat capacity calculations * Remove all LINDA-specific code from GasMixture, move it to TileAtmosphere/AtmosphereSystem instead. * Fix roundstart tiles not processing * Update Content.Server/Atmos/Commands/SetTemperatureCommand.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Update Content.Server/Atmos/EntitySystems/AtmosphereSystem.API.cs Changed Files tab is so large I can't commit both suggestions at once mfw Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Moony <moonheart08@users.noreply.github.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-07-04 16:51:34 +02:00
var transform = Transform(uid);
var indices = _transformSystem.GetGridOrMapTilePosition(uid, transform);
var tileMix = _atmosphereSystem.GetTileMixture(transform.GridUid, null, indices, true);
tileMix?.AdjustMoles(Gas.Miasma, molsToDump);
2022-06-19 01:51:55 -04:00
2022-06-24 04:20:52 -04:00
// Waste of entities to let these through
2022-06-19 01:51:55 -04:00
foreach (var part in args.GibbedParts)
2022-06-24 04:20:52 -04:00
EntityManager.DeleteEntity(part);
2022-06-19 01:51:55 -04:00
}
private void OnExamined(EntityUid uid, PerishableComponent component, ExaminedEvent args)
{
2022-06-24 04:20:52 -04:00
if (component.Rotting)
2022-06-19 01:51:55 -04:00
args.PushMarkup(Loc.GetString("miasma-rotting"));
}
2022-06-24 04:20:52 -04:00
/// Containers
private void OnEntInserted(EntityUid uid, AntiRottingContainerComponent component, EntInsertedIntoContainerMessage args)
{
if (TryComp<PerishableComponent>(args.Entity, out var perishable))
2022-07-02 22:46:24 -04:00
{
ModifyPreservationSource(args.Entity, true);
2022-06-24 04:20:52 -04:00
ToggleDecomposition(args.Entity, false, perishable);
2022-07-02 22:46:24 -04:00
}
}
private void OnEntRemoved(EntityUid uid, AntiRottingContainerComponent component, EntRemovedFromContainerMessage args)
{
if (TryComp<PerishableComponent>(args.Entity, out var perishable))
2022-07-02 22:46:24 -04:00
{
ModifyPreservationSource(args.Entity, false);
2022-06-24 04:20:52 -04:00
ToggleDecomposition(args.Entity, true, perishable);
2022-07-02 22:46:24 -04:00
}
2022-06-24 04:20:52 -04:00
}
/// Fly stuff
private void OnFliesInit(EntityUid uid, FliesComponent component, ComponentInit args)
{
component.VirtFlies = EntityManager.SpawnEntity("AmbientSoundSourceFlies", Transform(uid).Coordinates);
Transform(component.VirtFlies).AttachParent(uid);
}
private void OnFliesShutdown(EntityUid uid, FliesComponent component, ComponentShutdown args)
{
EntityManager.DeleteEntity(component.VirtFlies);
}
/// Public functions
public void ToggleDecomposition(EntityUid uid, bool decompose, PerishableComponent? perishable = null)
{
if (!Resolve(uid, ref perishable))
return;
if (decompose == perishable.Progressing) // Saved a few cycles
return;
2022-07-02 22:46:24 -04:00
perishable.Progressing = decompose;
2022-06-24 04:20:52 -04:00
if (!perishable.Rotting)
return;
if (decompose)
{
EnsureComp<FliesComponent>(uid);
return;
}
RemComp<FliesComponent>(uid);
}
2022-07-02 22:46:24 -04:00
/// <summary>
/// Add or remove a preservation source.
/// Remove is just "add = false"
/// If we have 0 we remove the whole component.
/// </summary>
public void ModifyPreservationSource(EntityUid uid, bool add)
{
var component = EnsureComp<BodyPreservedComponent>(uid);
if (add)
{
component.PreservationSources++;
return;
}
component.PreservationSources--;
if (component.PreservationSources == 0)
RemCompDeferred(uid, component);
}
public string RequestPoolDisease()
{
// We reset the current time on this outbreak so people don't get unlucky at the transition time
_poolAccumulator = 0f;
return _poolDisease;
}
}
}