Files
OldThink/Content.Server/Fluids/EntitySystems/DrainSystem.cs

102 lines
4.2 KiB
C#
Raw Normal View History

2022-10-04 12:56:47 +11:00
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Fluids.Components;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.Audio;
2023-04-10 15:37:03 +10:00
using Content.Shared.Fluids.Components;
2022-10-04 12:56:47 +11:00
using Robust.Shared.Collections;
namespace Content.Server.Fluids.EntitySystems
{
public sealed class DrainSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly SolutionContainerSystem _solutionSystem = default!;
[Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
public override void Update(float frameTime)
{
base.Update(frameTime);
2022-10-04 12:56:47 +11:00
var managerQuery = GetEntityQuery<SolutionContainerManagerComponent>();
var xformQuery = GetEntityQuery<TransformComponent>();
var puddleQuery = GetEntityQuery<PuddleComponent>();
var puddles = new ValueList<(EntityUid Entity, string Solution)>();
foreach (var drain in EntityQuery<DrainComponent>())
{
drain.Accumulator += frameTime;
if (drain.Accumulator < drain.DrainFrequency)
{
continue;
}
drain.Accumulator -= drain.DrainFrequency;
2022-10-04 12:56:47 +11:00
if (!managerQuery.TryGetComponent(drain.Owner, out var manager))
continue;
// Best to do this one every second rather than once every tick...
_solutionSystem.TryGetSolution(drain.Owner, DrainComponent.SolutionName, out var drainSolution, manager);
if (drainSolution is null)
2022-10-04 12:56:47 +11:00
continue;
2022-10-04 12:56:47 +11:00
// Remove a bit from the buffer
_solutionSystem.SplitSolution(drain.Owner, drainSolution, (drain.UnitsDestroyedPerSecond * drain.DrainFrequency));
2022-10-04 12:56:47 +11:00
// This will ensure that UnitsPerSecond is per second...
var amount = drain.UnitsPerSecond * drain.DrainFrequency;
2022-10-04 12:56:47 +11:00
if (!xformQuery.TryGetComponent(drain.Owner, out var xform))
continue;
puddles.Clear();
foreach (var entity in _lookup.GetEntitiesInRange(xform.MapPosition, drain.Range))
{
2022-10-04 12:56:47 +11:00
// No InRangeUnobstructed because there's no collision group that fits right now
// and these are placed by mappers and not buildable/movable so shouldnt really be a problem...
if (puddleQuery.TryGetComponent(entity, out var puddle))
{
2022-10-04 12:56:47 +11:00
puddles.Add((entity, puddle.SolutionName));
}
}
if (puddles.Count == 0)
{
_ambientSoundSystem.SetAmbience(drain.Owner, false);
continue;
}
_ambientSoundSystem.SetAmbience(drain.Owner, true);
amount /= puddles.Count;
2022-10-04 12:56:47 +11:00
foreach (var (puddle, solution) in puddles)
{
2022-10-04 12:56:47 +11:00
// Queue the solution deletion if it's empty. EvaporationSystem might also do this
// but queuedelete should be pretty safe.
if (!_solutionSystem.TryGetSolution(puddle, solution, out var puddleSolution))
{
2022-10-04 12:56:47 +11:00
EntityManager.QueueDeleteEntity(puddle);
continue;
}
2022-10-04 12:56:47 +11:00
// Removes the lowest of:
// the drain component's units per second adjusted for # of puddles
// the puddle's remaining volume (making it cleanly zero)
// the drain's remaining volume in its buffer.
var transferSolution = _solutionSystem.SplitSolution(puddle, puddleSolution,
2023-01-12 16:41:40 +13:00
FixedPoint2.Min(FixedPoint2.New(amount), puddleSolution.Volume, drainSolution.AvailableVolume));
_solutionSystem.TryAddSolution(drain.Owner, drainSolution, transferSolution);
2023-01-12 16:41:40 +13:00
if (puddleSolution.Volume <= 0)
{
2022-10-04 12:56:47 +11:00
QueueDel(puddle);
}
}
}
}
}
}