Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
Rane
2022-04-23 21:04:49 -04:00
committed by GitHub
parent 9730e8f193
commit 18220b6488
9 changed files with 182 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
namespace Content.Server.Fluids.Components
{
[RegisterComponent]
public sealed class DrainComponent : Component
{
public const string SolutionName = "drainBuffer";
[DataField("accumulator")]
public float Accumulator = 0f;
/// <summary>
/// How many units per second the drain can absorb from the surrounding puddles.
/// Divided by puddles, so if there are 5 puddles this will take 1/5 from each puddle.
/// This will stay fixed to 1 second no matter what DrainFrequency is.
/// </summary>
[DataField("unitsPerSecond")]
public float UnitsPerSecond = 6f;
/// <summary>
/// How many units are ejected from the buffer per second.
/// </summary>
[DataField("unitsDestroyedPerSecond")]
public float UnitsDestroyedPerSecond = 1f;
/// <summary>
/// How many (unobstructed) tiles away the drain will
/// drain puddles from.
/// </summary>
[DataField("range")]
public float Range = 2f;
/// <summary>
/// How often in seconds the drain checks for puddles around it.
/// If the EntityQuery seems a bit unperformant this can be increased.
/// <summary>
[DataField("drainFrequency")]
public float DrainFrequency = 1f;
}
}

View File

@@ -1,9 +1,5 @@
using Content.Server.Fluids.EntitySystems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Fluids.Components
{

View File

@@ -0,0 +1,89 @@
using Content.Server.Fluids.Components;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Audio;
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);
foreach (var drain in EntityQuery<DrainComponent>())
{
drain.Accumulator += frameTime;
if (drain.Accumulator < drain.DrainFrequency)
{
continue;
}
drain.Accumulator -= drain.DrainFrequency;
/// Best to do this one every second rather than once every tick...
_solutionSystem.TryGetSolution(drain.Owner, DrainComponent.SolutionName, out var drainSolution);
if (drainSolution is null)
return;
/// Remove a bit from the buffer
_solutionSystem.SplitSolution(drain.Owner, drainSolution, (drain.UnitsDestroyedPerSecond * drain.DrainFrequency));
/// This will ensure that UnitsPerSecond is per second...
var amount = drain.UnitsPerSecond * drain.DrainFrequency;
var xform = Transform(drain.Owner);
List<PuddleComponent> puddles = new();
foreach (var entity in _lookup.GetEntitiesInRange(xform.MapPosition, drain.Range))
{
/// 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 (TryComp<PuddleComponent>(entity, out var puddleComp))
{
puddles.Add(puddleComp);
}
}
if (puddles.Count == 0)
{
_ambientSoundSystem.SetAmbience(drain.Owner, false);
continue;
}
_ambientSoundSystem.SetAmbience(drain.Owner, true);
amount /= puddles.Count;
foreach (var puddle in puddles)
{
/// Queue the solution deletion if it's empty. EvaporationSystem might also do this
/// but queuedelete should be pretty safe.
if (!_solutionSystem.TryGetSolution(puddle.Owner, puddle.SolutionName, out var puddleSolution))
{
EntityManager.QueueDeleteEntity(puddle.Owner);
continue;
}
/// 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.Owner, puddleSolution,
FixedPoint2.Min(FixedPoint2.New(amount), puddleSolution.CurrentVolume, drainSolution.AvailableVolume));
_solutionSystem.TryAddSolution(drain.Owner, drainSolution, transferSolution);
if (puddleSolution.CurrentVolume <= 0)
{
EntityManager.QueueDeleteEntity(puddle.Owner);
}
}
}
}
}
}