Files
tbd-station-14/Content.Server/Atmos/Piping/Trinary/EntitySystems/GasMixerSystem.cs
Vera Aguilera Puerto 5cd42c9ad6 Inline UID
2021-12-03 15:53:09 +01:00

156 lines
6.2 KiB
C#

using System;
using Content.Server.Atmos.Piping.Components;
using Content.Server.Atmos.Piping.Trinary.Components;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Trinary.Components;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Atmos.Piping.Trinary.EntitySystems
{
[UsedImplicitly]
public class GasMixerSystem : EntitySystem
{
[Dependency] private UserInterfaceSystem _userInterfaceSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GasMixerComponent, AtmosDeviceUpdateEvent>(OnMixerUpdated);
SubscribeLocalEvent<GasMixerComponent, InteractHandEvent>(OnMixerInteractHand);
// Bound UI subscriptions
SubscribeLocalEvent<GasMixerComponent, GasMixerChangeOutputPressureMessage>(OnOutputPressureChangeMessage);
SubscribeLocalEvent<GasMixerComponent, GasMixerChangeNodePercentageMessage>(OnChangeNodePercentageMessage);
SubscribeLocalEvent<GasMixerComponent, GasMixerToggleStatusMessage>(OnToggleStatusMessage);
}
private void OnMixerUpdated(EntityUid uid, GasMixerComponent mixer, AtmosDeviceUpdateEvent args)
{
// TODO ATMOS: Cache total moles since it's expensive.
if (!mixer.Enabled)
return;
if (!EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer))
return;
if (!nodeContainer.TryGetNode(mixer.InletOneName, out PipeNode? inletOne)
|| !nodeContainer.TryGetNode(mixer.InletTwoName, out PipeNode? inletTwo)
|| !nodeContainer.TryGetNode(mixer.OutletName, out PipeNode? outlet))
return;
var outputStartingPressure = outlet.Air.Pressure;
if (outputStartingPressure >= mixer.TargetPressure)
return; // Target reached, no need to mix.
var generalTransfer = (mixer.TargetPressure - outputStartingPressure) * outlet.Air.Volume / Atmospherics.R;
var transferMolesOne = inletOne.Air.Temperature > 0 ? mixer.InletOneConcentration * generalTransfer / inletOne.Air.Temperature : 0f;
var transferMolesTwo = inletTwo.Air.Temperature > 0 ? mixer.InletTwoConcentration * generalTransfer / inletTwo.Air.Temperature : 0f;
if (mixer.InletTwoConcentration <= 0f)
{
if (inletOne.Air.Temperature <= 0f)
return;
transferMolesOne = MathF.Min(transferMolesOne, inletOne.Air.TotalMoles);
transferMolesTwo = 0f;
}
else if (mixer.InletOneConcentration <= 0)
{
if (inletTwo.Air.Temperature <= 0f)
return;
transferMolesOne = 0f;
transferMolesTwo = MathF.Min(transferMolesTwo, inletTwo.Air.TotalMoles);
}
else
{
if (inletOne.Air.Temperature <= 0f || inletTwo.Air.Temperature <= 0f)
return;
if (transferMolesOne <= 0 || transferMolesTwo <= 0)
return;
if (inletOne.Air.TotalMoles < transferMolesOne || inletTwo.Air.TotalMoles < transferMolesTwo)
{
var ratio = MathF.Min(inletOne.Air.TotalMoles / transferMolesOne, inletTwo.Air.TotalMoles / transferMolesTwo);
transferMolesOne *= ratio;
transferMolesTwo *= ratio;
}
}
// Actually transfer the gas now.
if (transferMolesOne > 0f)
{
var removed = inletOne.Air.Remove(transferMolesOne);
outlet.AssumeAir(removed);
}
if (transferMolesTwo > 0f)
{
var removed = inletTwo.Air.Remove(transferMolesTwo);
outlet.AssumeAir(removed);
}
}
private void OnMixerInteractHand(EntityUid uid, GasMixerComponent component, InteractHandEvent args)
{
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent(args.User, out ActorComponent? actor))
return;
if (IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(component.Owner).Anchored)
{
_userInterfaceSystem.TryOpen(uid, GasMixerUiKey.Key, actor.PlayerSession);
DirtyUI(uid, component);
}
else
{
args.User.PopupMessageCursor(Loc.GetString("comp-gas-mixer-ui-needs-anchor"));
}
args.Handled = true;
}
private void DirtyUI(EntityUid uid, GasMixerComponent? mixer)
{
if (!Resolve(uid, ref mixer))
return;
_userInterfaceSystem.TrySetUiState(uid, GasMixerUiKey.Key,
new GasMixerBoundUserInterfaceState(IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(mixer.Owner).EntityName, mixer.TargetPressure, mixer.Enabled, mixer.InletOneConcentration));
}
private void OnToggleStatusMessage(EntityUid uid, GasMixerComponent mixer, GasMixerToggleStatusMessage args)
{
mixer.Enabled = args.Enabled;
DirtyUI(uid, mixer);
}
private void OnOutputPressureChangeMessage(EntityUid uid, GasMixerComponent mixer, GasMixerChangeOutputPressureMessage args)
{
mixer.TargetPressure = Math.Clamp(args.Pressure, 0f, Atmospherics.MaxOutputPressure);
DirtyUI(uid, mixer);
}
private void OnChangeNodePercentageMessage(EntityUid uid, GasMixerComponent mixer,
GasMixerChangeNodePercentageMessage args)
{
float nodeOne = Math.Clamp(args.NodeOne, 0f, 100.0f) / 100.0f;
mixer.InletOneConcentration = nodeOne;
mixer.InletTwoConcentration = 1.0f - mixer.InletOneConcentration;
DirtyUI(uid, mixer);
}
}
}