Convert StomachBehavior to a component/system + rejig body namespaces (#5249)
* Convert StomachBehavior to a component/system + rejig body namespaces * test * slightly more namespace changes * remove * Hello????? * fuck you github test runner * reviews * oobsy!
This commit is contained in:
102
Content.Server/Body/Components/BloodstreamComponent.cs
Normal file
102
Content.Server/Body/Components/BloodstreamComponent.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedBloodstreamComponent))]
|
||||
public class BloodstreamComponent : SharedBloodstreamComponent, IGasMixtureHolder
|
||||
{
|
||||
public override string Name => "Bloodstream";
|
||||
|
||||
/// <summary>
|
||||
/// Max volume of internal solution storage
|
||||
/// </summary>
|
||||
[DataField("maxVolume")] [ViewVariables]
|
||||
private FixedPoint2 _initialMaxVolume = FixedPoint2.New(250);
|
||||
|
||||
/// <summary>
|
||||
/// Internal solution for reagent storage
|
||||
/// </summary>
|
||||
[ViewVariables] private Solution? _internalSolution;
|
||||
|
||||
/// <summary>
|
||||
/// Empty volume of internal solution
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public FixedPoint2 EmptyVolume => _internalSolution?.AvailableVolume ?? FixedPoint2.Zero;
|
||||
|
||||
[ViewVariables]
|
||||
public GasMixture Air { get; set; } = new(6)
|
||||
{ Temperature = Atmospherics.NormalBodyTemperature };
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_internalSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner.Uid, DefaultSolutionName);
|
||||
if (_internalSolution != null)
|
||||
{
|
||||
_internalSolution.MaxVolume = _initialMaxVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to transfer provided solution to internal solution.
|
||||
/// Only supports complete transfers
|
||||
/// </summary>
|
||||
/// <param name="solution">Solution to be transferred</param>
|
||||
/// <returns>Whether or not transfer was a success</returns>
|
||||
public override bool TryTransferSolution(Solution solution)
|
||||
{
|
||||
// For now doesn't support partial transfers
|
||||
var current = _internalSolution?.CurrentVolume ?? FixedPoint2.Zero;
|
||||
var max = _internalSolution?.MaxVolume ?? FixedPoint2.Zero;
|
||||
if (solution.TotalVolume + current > max)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(Owner.Uid, _internalSolution, solution);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void PumpToxins(GasMixture to)
|
||||
{
|
||||
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
|
||||
|
||||
if (!Owner.TryGetComponent(out RespiratorComponent? metabolism))
|
||||
{
|
||||
atmosphereSystem.Merge(to, Air);
|
||||
Air.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var toxins = metabolism.Clean(this);
|
||||
var toOld = new float[to.Moles.Length];
|
||||
Array.Copy(to.Moles, toOld, toOld.Length);
|
||||
|
||||
atmosphereSystem.Merge(to, toxins);
|
||||
|
||||
for (var i = 0; i < toOld.Length; i++)
|
||||
{
|
||||
var newAmount = to.GetMoles(i);
|
||||
var oldAmount = toOld[i];
|
||||
var delta = newAmount - oldAmount;
|
||||
|
||||
toxins.AdjustMoles(i, -delta);
|
||||
}
|
||||
|
||||
atmosphereSystem.Merge(Air, toxins);
|
||||
}
|
||||
}
|
||||
}
|
||||
108
Content.Server/Body/Components/BodyComponent.cs
Normal file
108
Content.Server/Body/Components/BodyComponent.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using Content.Server.Ghost;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Body;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedBodyComponent))]
|
||||
[ComponentReference(typeof(IGhostOnMove))]
|
||||
public class BodyComponent : SharedBodyComponent, IGhostOnMove
|
||||
{
|
||||
private Container _partContainer = default!;
|
||||
|
||||
[DataField("gibSound")] private SoundSpecifier _gibSound = new SoundCollectionSpecifier("gib");
|
||||
|
||||
protected override bool CanAddPart(string slotId, SharedBodyPartComponent part)
|
||||
{
|
||||
return base.CanAddPart(slotId, part) &&
|
||||
_partContainer.CanInsert(part.Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddPart(BodyPartSlot slot, SharedBodyPartComponent part)
|
||||
{
|
||||
base.OnAddPart(slot, part);
|
||||
|
||||
_partContainer.Insert(part.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemovePart(BodyPartSlot slot, SharedBodyPartComponent part)
|
||||
{
|
||||
base.OnRemovePart(slot, part);
|
||||
|
||||
_partContainer.ForceRemove(part.Owner);
|
||||
part.Owner.RandomOffset(0.25f);
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_partContainer = Owner.EnsureContainer<Container>($"{Name}-{nameof(BodyComponent)}");
|
||||
var preset = Preset;
|
||||
|
||||
if (preset != null)
|
||||
{
|
||||
foreach (var slot in Slots)
|
||||
{
|
||||
// Using MapPosition instead of Coordinates here prevents
|
||||
// a crash within the character preview menu in the lobby
|
||||
var entity = Owner.EntityManager.SpawnEntity(preset.PartIDs[slot.Id], Owner.Transform.MapPosition);
|
||||
|
||||
if (!entity.TryGetComponent(out SharedBodyPartComponent? part))
|
||||
{
|
||||
Logger.Error($"Entity {slot.Id} does not have a {nameof(SharedBodyPartComponent)} component.");
|
||||
continue;
|
||||
}
|
||||
|
||||
SetPart(slot.Id, part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
// This is ran in Startup as entities spawned in Initialize
|
||||
// are not synced to the client since they are assumed to be
|
||||
// identical on it
|
||||
foreach (var (part, _) in Parts)
|
||||
{
|
||||
part.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Gib(bool gibParts = false)
|
||||
{
|
||||
base.Gib(gibParts);
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _gibSound.GetSound(), Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.025f));
|
||||
|
||||
if (Owner.TryGetComponent(out ContainerManagerComponent? container))
|
||||
{
|
||||
foreach (var cont in container.GetAllContainers())
|
||||
{
|
||||
foreach (var ent in cont.ContainedEntities)
|
||||
{
|
||||
cont.ForceRemove(ent);
|
||||
ent.Transform.Coordinates = Owner.Transform.Coordinates;
|
||||
ent.RandomOffset(0.25f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Owner.QueueDelete();
|
||||
}
|
||||
}
|
||||
}
|
||||
71
Content.Server/Body/Components/InternalsComponent.cs
Normal file
71
Content.Server/Body/Components/InternalsComponent.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Content.Server.Atmos.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class InternalsComponent : Component
|
||||
{
|
||||
public override string Name => "Internals";
|
||||
[ViewVariables] public IEntity? GasTankEntity { get; set; }
|
||||
[ViewVariables] public IEntity? BreathToolEntity { get; set; }
|
||||
|
||||
public void DisconnectBreathTool()
|
||||
{
|
||||
var old = BreathToolEntity;
|
||||
BreathToolEntity = null;
|
||||
|
||||
if (old != null && old.TryGetComponent(out BreathToolComponent? breathTool) )
|
||||
{
|
||||
breathTool.DisconnectInternals();
|
||||
DisconnectTank();
|
||||
}
|
||||
}
|
||||
|
||||
public void ConnectBreathTool(IEntity toolEntity)
|
||||
{
|
||||
if (BreathToolEntity != null && BreathToolEntity.TryGetComponent(out BreathToolComponent? tool))
|
||||
{
|
||||
tool.DisconnectInternals();
|
||||
}
|
||||
|
||||
BreathToolEntity = toolEntity;
|
||||
}
|
||||
|
||||
public void DisconnectTank()
|
||||
{
|
||||
if (GasTankEntity != null && GasTankEntity.TryGetComponent(out GasTankComponent? tank))
|
||||
{
|
||||
tank.DisconnectFromInternals(Owner);
|
||||
}
|
||||
|
||||
GasTankEntity = null;
|
||||
}
|
||||
|
||||
public bool TryConnectTank(IEntity tankEntity)
|
||||
{
|
||||
if (BreathToolEntity == null)
|
||||
return false;
|
||||
|
||||
if (GasTankEntity != null && GasTankEntity.TryGetComponent(out GasTankComponent? tank))
|
||||
{
|
||||
tank.DisconnectFromInternals(Owner);
|
||||
}
|
||||
|
||||
GasTankEntity = tankEntity;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AreInternalsWorking()
|
||||
{
|
||||
return BreathToolEntity != null &&
|
||||
GasTankEntity != null &&
|
||||
BreathToolEntity.TryGetComponent(out BreathToolComponent? breathTool) &&
|
||||
breathTool.IsFunctional &&
|
||||
GasTankEntity.TryGetComponent(out GasTankComponent? gasTank) &&
|
||||
gasTank.Air != null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
159
Content.Server/Body/Components/MechanismComponent.cs
Normal file
159
Content.Server/Body/Components/MechanismComponent.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Body.Surgery;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedMechanismComponent))]
|
||||
public class MechanismComponent : SharedMechanismComponent, IAfterInteract
|
||||
{
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUIMessage;
|
||||
}
|
||||
}
|
||||
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Target == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseAllSurgeryUIs();
|
||||
OptionsCache.Clear();
|
||||
PerformerCache = null;
|
||||
BodyCache = null;
|
||||
|
||||
if (eventArgs.Target.TryGetComponent(out SharedBodyComponent? body))
|
||||
{
|
||||
SendBodyPartListToUser(eventArgs, body);
|
||||
}
|
||||
else if (eventArgs.Target.TryGetComponent<SharedBodyPartComponent>(out var part))
|
||||
{
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
if (!part.TryAddMechanism(this))
|
||||
{
|
||||
eventArgs.Target.PopupMessage(eventArgs.User, Loc.GetString("mechanism-component-cannot-fit-message"));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SendBodyPartListToUser(AfterInteractEventArgs eventArgs, SharedBodyComponent body)
|
||||
{
|
||||
// Create dictionary to send to client (text to be shown : data sent back if selected)
|
||||
var toSend = new Dictionary<string, int>();
|
||||
|
||||
foreach (var (part, slot) in body.Parts)
|
||||
{
|
||||
// For each limb in the target, add it to our cache if it is a valid option.
|
||||
if (part.CanAddMechanism(this))
|
||||
{
|
||||
OptionsCache.Add(IdHash, slot);
|
||||
toSend.Add(part + ": " + part.Name, IdHash++);
|
||||
}
|
||||
}
|
||||
|
||||
if (OptionsCache.Count > 0 &&
|
||||
eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
||||
{
|
||||
OpenSurgeryUI(actor.PlayerSession);
|
||||
UpdateSurgeryUIBodyPartRequest(actor.PlayerSession, toSend);
|
||||
PerformerCache = eventArgs.User;
|
||||
BodyCache = body;
|
||||
}
|
||||
else // If surgery cannot be performed, show message saying so.
|
||||
{
|
||||
eventArgs.Target?.PopupMessage(eventArgs.User,
|
||||
Loc.GetString("mechanism-component-no-way-to-install-message", ("partName", Owner.Name)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after the client chooses from a list of possible BodyParts that can be operated on.
|
||||
/// </summary>
|
||||
private void HandleReceiveBodyPart(int key)
|
||||
{
|
||||
if (PerformerCache == null ||
|
||||
!PerformerCache.TryGetComponent(out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CloseSurgeryUI(actor.PlayerSession);
|
||||
|
||||
if (BodyCache == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: sanity checks to see whether user is in range, user is still able-bodied, target is still the same, etc etc
|
||||
if (!OptionsCache.TryGetValue(key, out var targetObject))
|
||||
{
|
||||
BodyCache.Owner.PopupMessage(PerformerCache,
|
||||
Loc.GetString("mechanism-component-no-useful-way-to-use-message",("partName", Owner.Name)));
|
||||
return;
|
||||
}
|
||||
|
||||
var target = (SharedBodyPartComponent) targetObject;
|
||||
var message = target.TryAddMechanism(this)
|
||||
? Loc.GetString("mechanism-component-jam-inside-message",("ownerName", Owner),("them", PerformerCache))
|
||||
: Loc.GetString("mechanism-component-cannot-fit-message");
|
||||
|
||||
BodyCache.Owner.PopupMessage(PerformerCache, message);
|
||||
|
||||
// TODO: {1:theName}
|
||||
}
|
||||
|
||||
private void OpenSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Open(session);
|
||||
}
|
||||
|
||||
private void UpdateSurgeryUIBodyPartRequest(IPlayerSession session, Dictionary<string, int> options)
|
||||
{
|
||||
UserInterface?.SendMessage(new RequestBodyPartSurgeryUIMessage(options), session);
|
||||
}
|
||||
|
||||
private void CloseSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Close(session);
|
||||
}
|
||||
|
||||
private void CloseAllSurgeryUIs()
|
||||
{
|
||||
UserInterface?.CloseAll();
|
||||
}
|
||||
|
||||
private void OnUIMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
switch (message.Message)
|
||||
{
|
||||
case ReceiveBodyPartSurgeryUIMessage msg:
|
||||
HandleReceiveBodyPart(msg.SelectedOptionId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
87
Content.Server/Body/Components/MetabolizerComponent.cs
Normal file
87
Content.Server/Body/Components/MetabolizerComponent.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Prototypes;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles metabolizing various reagents with given effects.
|
||||
/// </summary>
|
||||
[RegisterComponent, Friend(typeof(MetabolizerSystem))]
|
||||
public class MetabolizerComponent : Component
|
||||
{
|
||||
public override string Name => "Metabolizer";
|
||||
|
||||
public float AccumulatedFrametime = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// How often to metabolize reagents, in seconds.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DataField("updateFrequency")]
|
||||
public float UpdateFrequency = 1.0f;
|
||||
|
||||
/// <summary>
|
||||
/// From which solution will this metabolizer attempt to metabolize chemicals
|
||||
/// </summary>
|
||||
[DataField("solution")]
|
||||
public string SolutionName { get; set; } = SharedBloodstreamComponent.DefaultSolutionName;
|
||||
|
||||
/// <summary>
|
||||
/// Does this component use a solution on it's parent entity (the body) or itself
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Most things will use the parent entity (bloodstream).
|
||||
/// </remarks>
|
||||
[DataField("solutionOnBody")]
|
||||
public bool SolutionOnBody = true;
|
||||
|
||||
/// <summary>
|
||||
/// List of metabolizer types that this organ is. ex. Human, Slime, Felinid, w/e.
|
||||
/// </summary>
|
||||
[DataField("metabolizerTypes", customTypeSerializer:typeof(PrototypeIdHashSetSerializer<MetabolizerTypePrototype>))]
|
||||
public HashSet<string>? MetabolizerTypes = null;
|
||||
|
||||
/// <summary>
|
||||
/// Should this metabolizer remove chemicals that have no metabolisms defined?
|
||||
/// As a stop-gap, basically.
|
||||
/// </summary>
|
||||
[DataField("removeEmpty")]
|
||||
public bool RemoveEmpty = false;
|
||||
|
||||
/// <summary>
|
||||
/// How many reagents can this metabolizer process at once?
|
||||
/// Used to nerf 'stacked poisons' where having 5+ different poisons in a syringe, even at low
|
||||
/// quantity, would be muuuuch better than just one poison acting.
|
||||
/// </summary>
|
||||
[DataField("maxReagents")]
|
||||
public int MaxReagentsProcessable = 3;
|
||||
|
||||
/// <summary>
|
||||
/// A list of metabolism groups that this metabolizer will act on, in order of precedence.
|
||||
/// </summary>
|
||||
[DataField("groups")]
|
||||
public List<MetabolismGroupEntry>? MetabolismGroups = default!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Contains data about how a metabolizer will metabolize a single group.
|
||||
/// This allows metabolizers to remove certain groups much faster, or not at all.
|
||||
/// </summary>
|
||||
[DataDefinition]
|
||||
public class MetabolismGroupEntry
|
||||
{
|
||||
[DataField("id", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<MetabolismGroupPrototype>))]
|
||||
public string Id = default!;
|
||||
|
||||
[DataField("rateModifier")]
|
||||
public FixedPoint2 MetabolismRateModifier = 1.0;
|
||||
}
|
||||
}
|
||||
398
Content.Server/Body/Components/RespiratorComponent.cs
Normal file
398
Content.Server/Body/Components/RespiratorComponent.cs
Normal file
@@ -0,0 +1,398 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Alert;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Body.Behavior;
|
||||
using Content.Server.Temperature.Components;
|
||||
using Content.Server.Temperature.Systems;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class RespiratorComponent : Component
|
||||
{
|
||||
[ComponentDependency] private readonly SharedBodyComponent? _body = default!;
|
||||
|
||||
public override string Name => "Respirator";
|
||||
|
||||
private float _accumulatedFrameTime;
|
||||
|
||||
private bool _isShivering;
|
||||
private bool _isSweating;
|
||||
|
||||
[ViewVariables] [DataField("needsGases")] public Dictionary<Gas, float> NeedsGases { get; set; } = new();
|
||||
|
||||
[ViewVariables] [DataField("producesGases")] public Dictionary<Gas, float> ProducesGases { get; set; } = new();
|
||||
|
||||
[ViewVariables] [DataField("deficitGases")] public Dictionary<Gas, float> DeficitGases { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Heat generated due to metabolism. It's generated via metabolism
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("metabolismHeat")]
|
||||
public float MetabolismHeat { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Heat output via radiation.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("radiatedHeat")]
|
||||
public float RadiatedHeat { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum heat regulated via sweat
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("sweatHeatRegulation")]
|
||||
public float SweatHeatRegulation { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum heat regulated via shivering
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("shiveringHeatRegulation")]
|
||||
public float ShiveringHeatRegulation { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Amount of heat regulation that represents thermal regulation processes not
|
||||
/// explicitly coded.
|
||||
/// </summary>
|
||||
[DataField("implicitHeatRegulation")]
|
||||
public float ImplicitHeatRegulation { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Normal body temperature
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("normalBodyTemperature")]
|
||||
public float NormalBodyTemperature { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Deviation from normal temperature for body to start thermal regulation
|
||||
/// </summary>
|
||||
[DataField("thermalRegulationTemperatureThreshold")]
|
||||
public float ThermalRegulationTemperatureThreshold { get; private set; }
|
||||
|
||||
[ViewVariables] public bool Suffocating { get; private set; }
|
||||
|
||||
[DataField("damage", required: true)]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public DamageSpecifier Damage = default!;
|
||||
|
||||
[DataField("damageRecovery", required: true)]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public DamageSpecifier DamageRecovery = default!;
|
||||
|
||||
private Dictionary<Gas, float> NeedsAndDeficit(float frameTime)
|
||||
{
|
||||
var needs = new Dictionary<Gas, float>(NeedsGases);
|
||||
foreach (var (gas, amount) in DeficitGases)
|
||||
{
|
||||
var newAmount = (needs.GetValueOrDefault(gas) + amount) * frameTime;
|
||||
needs[gas] = newAmount;
|
||||
}
|
||||
|
||||
return needs;
|
||||
}
|
||||
|
||||
private void ClampDeficit()
|
||||
{
|
||||
var deficitGases = new Dictionary<Gas, float>(DeficitGases);
|
||||
|
||||
foreach (var (gas, deficit) in deficitGases)
|
||||
{
|
||||
if (!NeedsGases.TryGetValue(gas, out var need))
|
||||
{
|
||||
DeficitGases.Remove(gas);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (deficit > need)
|
||||
{
|
||||
DeficitGases[gas] = need;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private float SuffocatingPercentage()
|
||||
{
|
||||
var total = 0f;
|
||||
|
||||
foreach (var (gas, deficit) in DeficitGases)
|
||||
{
|
||||
var lack = 1f;
|
||||
if (NeedsGases.TryGetValue(gas, out var needed))
|
||||
{
|
||||
lack = deficit / needed;
|
||||
}
|
||||
|
||||
total += lack / Atmospherics.TotalNumberOfGases;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
private float GasProducedMultiplier(Gas gas, float usedAverage)
|
||||
{
|
||||
if (!ProducesGases.TryGetValue(gas, out var produces))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!NeedsGases.TryGetValue(gas, out var needs))
|
||||
{
|
||||
needs = 1;
|
||||
}
|
||||
|
||||
return needs * produces * usedAverage;
|
||||
}
|
||||
|
||||
private Dictionary<Gas, float> GasProduced(float usedAverage)
|
||||
{
|
||||
return ProducesGases.ToDictionary(pair => pair.Key, pair => GasProducedMultiplier(pair.Key, usedAverage));
|
||||
}
|
||||
|
||||
private void ProcessGases(float frameTime)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_body == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var lungs = _body.GetMechanismBehaviors<LungBehavior>().ToArray();
|
||||
|
||||
var needs = NeedsAndDeficit(frameTime);
|
||||
var used = 0f;
|
||||
foreach (var (gas, amountNeeded) in needs)
|
||||
{
|
||||
var bloodstreamAmount = bloodstream.Air.GetMoles(gas);
|
||||
var deficit = 0f;
|
||||
|
||||
if (bloodstreamAmount < amountNeeded)
|
||||
{
|
||||
if (!Owner.GetComponent<MobStateComponent>().IsCritical())
|
||||
{
|
||||
// Panic inhale
|
||||
foreach (var lung in lungs)
|
||||
{
|
||||
lung.Gasp();
|
||||
}
|
||||
}
|
||||
|
||||
bloodstreamAmount = bloodstream.Air.GetMoles(gas);
|
||||
|
||||
deficit = Math.Max(0, amountNeeded - bloodstreamAmount);
|
||||
|
||||
if (deficit > 0)
|
||||
{
|
||||
bloodstream.Air.SetMoles(gas, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
|
||||
}
|
||||
|
||||
DeficitGases[gas] = deficit;
|
||||
|
||||
|
||||
used += (amountNeeded - deficit) / amountNeeded;
|
||||
}
|
||||
|
||||
var produced = GasProduced(used / needs.Count);
|
||||
|
||||
foreach (var (gas, amountProduced) in produced)
|
||||
{
|
||||
bloodstream.Air.AdjustMoles(gas, amountProduced);
|
||||
}
|
||||
|
||||
ClampDeficit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process thermal regulation
|
||||
/// </summary>
|
||||
/// <param name="frameTime"></param>
|
||||
private void ProcessThermalRegulation(float frameTime)
|
||||
{
|
||||
var temperatureSystem = EntitySystem.Get<TemperatureSystem>();
|
||||
if (!Owner.TryGetComponent(out TemperatureComponent? temperatureComponent)) return;
|
||||
temperatureSystem.ReceiveHeat(Owner.Uid, MetabolismHeat, temperatureComponent);
|
||||
temperatureSystem.RemoveHeat(Owner.Uid, RadiatedHeat, temperatureComponent);
|
||||
|
||||
// implicit heat regulation
|
||||
var tempDiff = Math.Abs(temperatureComponent.CurrentTemperature - NormalBodyTemperature);
|
||||
var targetHeat = tempDiff * temperatureComponent.HeatCapacity;
|
||||
if (temperatureComponent.CurrentTemperature > NormalBodyTemperature)
|
||||
{
|
||||
temperatureSystem.RemoveHeat(Owner.Uid, Math.Min(targetHeat, ImplicitHeatRegulation), temperatureComponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
temperatureSystem.ReceiveHeat(Owner.Uid, Math.Min(targetHeat, ImplicitHeatRegulation), temperatureComponent);
|
||||
}
|
||||
|
||||
// recalc difference and target heat
|
||||
tempDiff = Math.Abs(temperatureComponent.CurrentTemperature - NormalBodyTemperature);
|
||||
targetHeat = tempDiff * temperatureComponent.HeatCapacity;
|
||||
|
||||
// if body temperature is not within comfortable, thermal regulation
|
||||
// processes starts
|
||||
if (tempDiff < ThermalRegulationTemperatureThreshold)
|
||||
{
|
||||
if (_isShivering || _isSweating)
|
||||
{
|
||||
Owner.PopupMessage(Loc.GetString("metabolism-component-is-comfortable"));
|
||||
}
|
||||
|
||||
_isShivering = false;
|
||||
_isSweating = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var actionBlocker = EntitySystem.Get<ActionBlockerSystem>();
|
||||
|
||||
if (temperatureComponent.CurrentTemperature > NormalBodyTemperature)
|
||||
{
|
||||
if (!actionBlocker.CanSweat(OwnerUid)) return;
|
||||
if (!_isSweating)
|
||||
{
|
||||
Owner.PopupMessage(Loc.GetString("metabolism-component-is-sweating"));
|
||||
_isSweating = true;
|
||||
}
|
||||
|
||||
// creadth: sweating does not help in airless environment
|
||||
if (EntitySystem.Get<AtmosphereSystem>().GetTileMixture(Owner.Transform.Coordinates) is not {})
|
||||
{
|
||||
temperatureSystem.RemoveHeat(OwnerUid, Math.Min(targetHeat, SweatHeatRegulation), temperatureComponent);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!actionBlocker.CanShiver(OwnerUid)) return;
|
||||
if (!_isShivering)
|
||||
{
|
||||
Owner.PopupMessage(Loc.GetString("metabolism-component-is-shivering"));
|
||||
_isShivering = true;
|
||||
}
|
||||
|
||||
temperatureSystem.ReceiveHeat(OwnerUid, Math.Min(targetHeat, ShiveringHeatRegulation), temperatureComponent);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes gases in the bloodstream.
|
||||
/// </summary>
|
||||
/// <param name="frameTime">
|
||||
/// The time since the last metabolism tick in seconds.
|
||||
/// </param>
|
||||
public void Update(float frameTime)
|
||||
{
|
||||
if (!Owner.TryGetComponent<MobStateComponent>(out var state) ||
|
||||
state.IsDead())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime += frameTime;
|
||||
|
||||
if (_accumulatedFrameTime < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ProcessGases(_accumulatedFrameTime);
|
||||
ProcessThermalRegulation(_accumulatedFrameTime);
|
||||
|
||||
_accumulatedFrameTime -= 1;
|
||||
|
||||
if (SuffocatingPercentage() > 0)
|
||||
{
|
||||
TakeSuffocationDamage();
|
||||
return;
|
||||
}
|
||||
|
||||
StopSuffocation();
|
||||
}
|
||||
|
||||
private void TakeSuffocationDamage()
|
||||
{
|
||||
Suffocating = true;
|
||||
|
||||
if (Owner.TryGetComponent(out ServerAlertsComponent? alertsComponent))
|
||||
{
|
||||
alertsComponent.ShowAlert(AlertType.LowOxygen);
|
||||
}
|
||||
|
||||
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner.Uid, Damage, true);
|
||||
}
|
||||
|
||||
private void StopSuffocation()
|
||||
{
|
||||
Suffocating = false;
|
||||
|
||||
if (Owner.TryGetComponent(out ServerAlertsComponent? alertsComponent))
|
||||
{
|
||||
alertsComponent.ClearAlert(AlertType.LowOxygen);
|
||||
}
|
||||
|
||||
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner.Uid, DamageRecovery, true);
|
||||
}
|
||||
|
||||
public GasMixture Clean(BloodstreamComponent bloodstream)
|
||||
{
|
||||
var gasMixture = new GasMixture(bloodstream.Air.Volume)
|
||||
{
|
||||
Temperature = bloodstream.Air.Temperature
|
||||
};
|
||||
|
||||
for (Gas gas = 0; gas < (Gas) Atmospherics.TotalNumberOfGases; gas++)
|
||||
{
|
||||
float amount;
|
||||
var molesInBlood = bloodstream.Air.GetMoles(gas);
|
||||
|
||||
if (!NeedsGases.TryGetValue(gas, out var needed))
|
||||
{
|
||||
amount = molesInBlood;
|
||||
}
|
||||
else
|
||||
{
|
||||
var overflowThreshold = needed * 5f;
|
||||
|
||||
amount = molesInBlood > overflowThreshold
|
||||
? molesInBlood - overflowThreshold
|
||||
: 0;
|
||||
}
|
||||
|
||||
gasMixture.AdjustMoles(gas, amount);
|
||||
bloodstream.Air.AdjustMoles(gas, -amount);
|
||||
}
|
||||
|
||||
return gasMixture;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Content.Server/Body/Components/RespiratorSystem.cs
Normal file
19
Content.Server/Body/Components/RespiratorSystem.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class RespiratorSystem : EntitySystem
|
||||
{
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var respirator in EntityManager.EntityQuery<RespiratorComponent>(false))
|
||||
{
|
||||
respirator.Update(frameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
69
Content.Server/Body/Components/StomachComponent.cs
Normal file
69
Content.Server/Body/Components/StomachComponent.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent, Friend(typeof(StomachSystem))]
|
||||
public class StomachComponent : Component
|
||||
{
|
||||
public override string Name => "Stomach";
|
||||
|
||||
public float AccumulatedFrameTime;
|
||||
|
||||
/// <summary>
|
||||
/// How fast should this component update, in seconds?
|
||||
/// </summary>
|
||||
[DataField("updateInterval")]
|
||||
public float UpdateInterval = 1.0f;
|
||||
|
||||
/// <summary>
|
||||
/// What solution should this stomach push reagents into, on the body?
|
||||
/// </summary>
|
||||
[DataField("bodySolutionName")]
|
||||
public string BodySolutionName = SharedBloodstreamComponent.DefaultSolutionName;
|
||||
|
||||
/// <summary>
|
||||
/// Initial internal solution storage volume
|
||||
/// </summary>
|
||||
[DataField("maxVolume")]
|
||||
public FixedPoint2 InitialMaxVolume { get; private set; } = FixedPoint2.New(100);
|
||||
|
||||
/// <summary>
|
||||
/// Time in seconds between reagents being ingested and them being
|
||||
/// transferred to <see cref="SharedBloodstreamComponent"/>
|
||||
/// </summary>
|
||||
[DataField("digestionDelay")]
|
||||
public float DigestionDelay = 20;
|
||||
|
||||
/// <summary>
|
||||
/// Used to track how long each reagent has been in the stomach
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public readonly List<ReagentDelta> ReagentDeltas = new();
|
||||
|
||||
/// <summary>
|
||||
/// Used to track quantity changes when ingesting & digesting reagents
|
||||
/// </summary>
|
||||
public class ReagentDelta
|
||||
{
|
||||
public readonly string ReagentId;
|
||||
public readonly FixedPoint2 Quantity;
|
||||
public float Lifetime { get; private set; }
|
||||
|
||||
public ReagentDelta(string reagentId, FixedPoint2 quantity)
|
||||
{
|
||||
ReagentId = reagentId;
|
||||
Quantity = quantity;
|
||||
Lifetime = 0.0f;
|
||||
}
|
||||
|
||||
public void Increment(float delta) => Lifetime += delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user