Re-organize all projects (#4166)
This commit is contained in:
71
Content.Server/Body/Behavior/BrainBehavior.cs
Normal file
71
Content.Server/Body/Behavior/BrainBehavior.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
#nullable enable
|
||||
using Content.Server.Ghost;
|
||||
using Content.Server.Ghost.Components;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
public class BrainBehavior : MechanismBehavior
|
||||
{
|
||||
protected override void OnAddedToBody(IBody body)
|
||||
{
|
||||
base.OnAddedToBody(body);
|
||||
|
||||
HandleMind(body.Owner, Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddedToPart(IBodyPart part)
|
||||
{
|
||||
base.OnAddedToPart(part);
|
||||
|
||||
HandleMind(part.Owner, Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddedToPartInBody(IBody body, IBodyPart part)
|
||||
{
|
||||
base.OnAddedToPartInBody(body, part);
|
||||
|
||||
HandleMind(body.Owner, Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemovedFromBody(IBody old)
|
||||
{
|
||||
base.OnRemovedFromBody(old);
|
||||
|
||||
HandleMind(Part!.Owner, old.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemovedFromPart(IBodyPart old)
|
||||
{
|
||||
base.OnRemovedFromPart(old);
|
||||
|
||||
HandleMind(Owner, old.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart)
|
||||
{
|
||||
base.OnRemovedFromPartInBody(oldBody, oldPart);
|
||||
|
||||
HandleMind(oldBody.Owner, Owner);
|
||||
}
|
||||
|
||||
private void HandleMind(IEntity newEntity, IEntity oldEntity)
|
||||
{
|
||||
newEntity.EnsureComponent<MindComponent>();
|
||||
var oldMind = oldEntity.EnsureComponent<MindComponent>();
|
||||
|
||||
if (!newEntity.HasComponent<IGhostOnMove>())
|
||||
newEntity.AddComponent<GhostOnMoveComponent>();
|
||||
|
||||
// TODO: This is an awful solution.
|
||||
if (!newEntity.HasComponent<IMoverComponent>())
|
||||
newEntity.AddComponent<SharedDummyInputMoverComponent>();
|
||||
|
||||
oldMind.Mind?.TransferTo(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Content.Server/Body/Behavior/HeartBehavior.cs
Normal file
29
Content.Server/Body/Behavior/HeartBehavior.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Content.Shared.Body.Networks;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
public class HeartBehavior : MechanismBehavior
|
||||
{
|
||||
private float _accumulatedFrameTime;
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
// TODO BODY do between pre and metabolism
|
||||
if (Parent.Body == null ||
|
||||
!Parent.Body.Owner.HasComponent<SharedBloodstreamComponent>())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Update at most once per second
|
||||
_accumulatedFrameTime += frameTime;
|
||||
|
||||
// TODO: Move/accept/process bloodstream reagents only when the heart is pumping
|
||||
if (_accumulatedFrameTime >= 1)
|
||||
{
|
||||
// bloodstream.Update(_accumulatedFrameTime);
|
||||
_accumulatedFrameTime -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
109
Content.Server/Body/Behavior/LiverBehavior.cs
Normal file
109
Content.Server/Body/Behavior/LiverBehavior.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
#nullable enable
|
||||
using System.Linq;
|
||||
using Content.Server.Body.Circulatory;
|
||||
using Content.Shared.Body.Networks;
|
||||
using Content.Shared.Chemistry.Metabolizable;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Metabolizes reagents in <see cref="SharedBloodstreamComponent"/> after they are digested.
|
||||
/// </summary>
|
||||
public class LiverBehavior : MechanismBehavior
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
private float _accumulatedFrameTime;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the liver is functional.
|
||||
/// </summary>
|
||||
//[ViewVariables] private bool _liverFailing = false;
|
||||
|
||||
/// <summary>
|
||||
/// Modifier for alcohol damage.
|
||||
/// </summary>
|
||||
//[DataField("alcoholLethality")]
|
||||
//[ViewVariables] private float _alcoholLethality = 0.005f;
|
||||
|
||||
/// <summary>
|
||||
/// Modifier for alcohol damage.
|
||||
/// </summary>
|
||||
//[DataField("alcoholExponent")]
|
||||
//[ViewVariables] private float _alcoholExponent = 1.6f;
|
||||
|
||||
/// <summary>
|
||||
/// Toxin volume that can be purged without damage.
|
||||
/// </summary>
|
||||
//[DataField("toxinTolerance")]
|
||||
//[ViewVariables] private float _toxinTolerance = 3f;
|
||||
|
||||
/// <summary>
|
||||
/// Toxin damage modifier.
|
||||
/// </summary>
|
||||
//[DataField("toxinLethality")]
|
||||
//[ViewVariables] private float _toxinLethality = 0.01f;
|
||||
|
||||
/// <summary>
|
||||
/// Loops through each reagent in _internalSolution,
|
||||
/// and calls <see cref="IMetabolizable.Metabolize"/> for each of them.
|
||||
/// Also handles toxins and alcohol.
|
||||
/// </summary>
|
||||
/// <param name="frameTime">
|
||||
/// The time since the last update in seconds.
|
||||
/// </param>
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
if (Body == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime += frameTime;
|
||||
|
||||
// Update at most once per second
|
||||
if (_accumulatedFrameTime < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime -= 1;
|
||||
|
||||
if (!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (bloodstream.Solution.CurrentVolume <= ReagentUnit.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Run metabolism for each reagent, remove metabolized reagents
|
||||
// Using ToList here lets us edit reagents while iterating
|
||||
foreach (var reagent in bloodstream.Solution.ReagentList.ToList())
|
||||
{
|
||||
if (!_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype? prototype))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//TODO BODY Check if it's a Toxin. If volume < _toxinTolerance, just remove it. If greater, add damage = volume * _toxinLethality
|
||||
//TODO BODY Check if it has BoozePower > 0. Affect drunkenness, apply damage. Proposed formula (SS13-derived): damage = sqrt(volume) * BoozePower^_alcoholExponent * _alcoholLethality / 10
|
||||
//TODO BODY Liver failure.
|
||||
|
||||
//TODO Make sure reagent prototypes actually have the toxin and boozepower vars set.
|
||||
|
||||
// Run metabolism code for each reagent
|
||||
foreach (var metabolizable in prototype.Metabolism)
|
||||
{
|
||||
var reagentDelta = metabolizable.Metabolize(Body.Owner, reagent.ReagentId, frameTime);
|
||||
bloodstream.Solution.TryRemoveReagent(reagent.ReagentId, reagentDelta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
202
Content.Server/Body/Behavior/LungBehavior.cs
Normal file
202
Content.Server/Body/Behavior/LungBehavior.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Body.Circulatory;
|
||||
using Content.Server.Body.Respiratory;
|
||||
using Content.Server.GameObjects.Components.Atmos;
|
||||
using Content.Server.Notification;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.MobState;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
[DataDefinition]
|
||||
public class LungBehavior : MechanismBehavior
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
private float _accumulatedFrameTime;
|
||||
|
||||
[ViewVariables] private TimeSpan _lastGaspPopupTime;
|
||||
|
||||
[DataField("air")]
|
||||
[ViewVariables]
|
||||
public GasMixture Air { get; set; } = new()
|
||||
{
|
||||
Volume = 6,
|
||||
Temperature = Atmospherics.NormalBodyTemperature
|
||||
};
|
||||
|
||||
[DataField("gaspPopupCooldown")]
|
||||
[ViewVariables]
|
||||
public TimeSpan GaspPopupCooldown { get; private set; } = TimeSpan.FromSeconds(8);
|
||||
|
||||
[ViewVariables] public LungStatus Status { get; set; }
|
||||
|
||||
[DataField("cycleDelay")]
|
||||
[ViewVariables]
|
||||
public float CycleDelay { get; set; } = 2;
|
||||
|
||||
public LungBehavior()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
}
|
||||
|
||||
protected override void OnAddedToBody(IBody body)
|
||||
{
|
||||
base.OnAddedToBody(body);
|
||||
Inhale(CycleDelay);
|
||||
}
|
||||
|
||||
public void Gasp()
|
||||
{
|
||||
if (_gameTiming.CurTime >= _lastGaspPopupTime + GaspPopupCooldown)
|
||||
{
|
||||
_lastGaspPopupTime = _gameTiming.CurTime;
|
||||
Owner.PopupMessageEveryone(Loc.GetString("Gasp"));
|
||||
}
|
||||
|
||||
Inhale(CycleDelay);
|
||||
}
|
||||
|
||||
public void Transfer(GasMixture from, GasMixture to, float ratio)
|
||||
{
|
||||
to.Merge(from.RemoveRatio(ratio));
|
||||
}
|
||||
|
||||
public void ToBloodstream(GasMixture mixture)
|
||||
{
|
||||
if (Body == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var to = bloodstream.Air;
|
||||
|
||||
to.Merge(mixture);
|
||||
mixture.Clear();
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
if (Body != null && Body.Owner.TryGetComponent(out IMobStateComponent? mobState) && mobState.IsCritical())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Status == LungStatus.None)
|
||||
{
|
||||
Status = LungStatus.Inhaling;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime += Status switch
|
||||
{
|
||||
LungStatus.Inhaling => frameTime,
|
||||
LungStatus.Exhaling => -frameTime,
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
var absoluteTime = Math.Abs(_accumulatedFrameTime);
|
||||
var delay = CycleDelay;
|
||||
|
||||
if (absoluteTime < delay)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (Status)
|
||||
{
|
||||
case LungStatus.Inhaling:
|
||||
Inhale(absoluteTime);
|
||||
Status = LungStatus.Exhaling;
|
||||
break;
|
||||
case LungStatus.Exhaling:
|
||||
Exhale(absoluteTime);
|
||||
Status = LungStatus.Inhaling;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
_accumulatedFrameTime = absoluteTime - delay;
|
||||
}
|
||||
|
||||
public void Inhale(float frameTime)
|
||||
{
|
||||
if (Body != null &&
|
||||
Body.Owner.TryGetComponent(out InternalsComponent? internals) &&
|
||||
internals.BreathToolEntity != null &&
|
||||
internals.GasTankEntity != null &&
|
||||
internals.BreathToolEntity.TryGetComponent(out BreathToolComponent? breathTool) &&
|
||||
breathTool.IsFunctional &&
|
||||
internals.GasTankEntity.TryGetComponent(out GasTankComponent? gasTank) &&
|
||||
gasTank.Air != null)
|
||||
{
|
||||
Inhale(frameTime, gasTank.RemoveAirVolume(Atmospherics.BreathVolume));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Owner.Transform.Coordinates.TryGetTileAir(out var tileAir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Inhale(frameTime, tileAir);
|
||||
}
|
||||
|
||||
public void Inhale(float frameTime, GasMixture from)
|
||||
{
|
||||
var ratio = (Atmospherics.BreathVolume / from.Volume) * frameTime;
|
||||
|
||||
Transfer(from, Air, ratio);
|
||||
ToBloodstream(Air);
|
||||
}
|
||||
|
||||
public void Exhale(float frameTime)
|
||||
{
|
||||
if (!Owner.Transform.Coordinates.TryGetTileAir(out var tileAir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Exhale(frameTime, tileAir);
|
||||
}
|
||||
|
||||
public void Exhale(float frameTime, GasMixture to)
|
||||
{
|
||||
// TODO: Make the bloodstream separately pump toxins into the lungs, making the lungs' only job to empty.
|
||||
if (Body == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bloodstream.PumpToxins(Air);
|
||||
|
||||
var lungRemoved = Air.RemoveRatio(0.5f);
|
||||
to.Merge(lungRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
public enum LungStatus
|
||||
{
|
||||
None = 0,
|
||||
Inhaling,
|
||||
Exhaling
|
||||
}
|
||||
}
|
||||
109
Content.Server/Body/Behavior/MechanismBehavior.cs
Normal file
109
Content.Server/Body/Behavior/MechanismBehavior.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Body.Behavior;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Mechanism;
|
||||
using Content.Shared.Body.Part;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
public abstract class MechanismBehavior : IMechanismBehavior
|
||||
{
|
||||
public IBody? Body => Part?.Body;
|
||||
|
||||
public IBodyPart? Part => Parent.Part;
|
||||
|
||||
public IMechanism Parent { get; private set; } = default!;
|
||||
|
||||
public IEntity Owner => Parent.Owner;
|
||||
|
||||
public virtual void Initialize(IMechanism parent)
|
||||
{
|
||||
Parent = parent;
|
||||
}
|
||||
|
||||
public virtual void Startup()
|
||||
{
|
||||
if (Part == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Body == null)
|
||||
{
|
||||
AddedToPart(Part);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddedToPartInBody(Body, Part);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddedToBody(IBody body)
|
||||
{
|
||||
DebugTools.AssertNotNull(Body);
|
||||
DebugTools.AssertNotNull(body);
|
||||
|
||||
OnAddedToBody(body);
|
||||
}
|
||||
|
||||
public void AddedToPart(IBodyPart part)
|
||||
{
|
||||
DebugTools.AssertNotNull(Part);
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
OnAddedToPart(part);
|
||||
}
|
||||
|
||||
public void AddedToPartInBody(IBody body, IBodyPart part)
|
||||
{
|
||||
DebugTools.AssertNotNull(Body);
|
||||
DebugTools.AssertNotNull(body);
|
||||
DebugTools.AssertNotNull(Part);
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
OnAddedToPartInBody(body, part);
|
||||
}
|
||||
|
||||
public void RemovedFromBody(IBody old)
|
||||
{
|
||||
DebugTools.AssertNull(Body);
|
||||
DebugTools.AssertNotNull(old);
|
||||
|
||||
OnRemovedFromBody(old);
|
||||
}
|
||||
|
||||
public void RemovedFromPart(IBodyPart old)
|
||||
{
|
||||
DebugTools.AssertNull(Part);
|
||||
DebugTools.AssertNotNull(old);
|
||||
|
||||
OnRemovedFromPart(old);
|
||||
}
|
||||
|
||||
public void RemovedFromPartInBody(IBody oldBody, IBodyPart oldPart)
|
||||
{
|
||||
DebugTools.AssertNull(Body);
|
||||
DebugTools.AssertNull(Part);
|
||||
DebugTools.AssertNotNull(oldBody);
|
||||
DebugTools.AssertNotNull(oldPart);
|
||||
|
||||
OnRemovedFromPartInBody(oldBody, oldPart);
|
||||
}
|
||||
|
||||
protected virtual void OnAddedToBody(IBody body) { }
|
||||
|
||||
protected virtual void OnAddedToPart(IBodyPart part) { }
|
||||
|
||||
protected virtual void OnAddedToPartInBody(IBody body, IBodyPart part) { }
|
||||
|
||||
protected virtual void OnRemovedFromBody(IBody old) { }
|
||||
|
||||
protected virtual void OnRemovedFromPart(IBodyPart old) { }
|
||||
|
||||
protected virtual void OnRemovedFromPartInBody(IBody oldBody, IBodyPart oldPart) { }
|
||||
|
||||
public virtual void Update(float frameTime) { }
|
||||
}
|
||||
}
|
||||
74
Content.Server/Body/Behavior/MechanismExtensions.cs
Normal file
74
Content.Server/Body/Behavior/MechanismExtensions.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Behavior;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Part;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
public static class MechanismExtensions
|
||||
{
|
||||
public static bool HasMechanismBehavior<T>(this IBody body) where T : IMechanismBehavior
|
||||
{
|
||||
return body.Parts.Any(p => p.Key.HasMechanismBehavior<T>());
|
||||
}
|
||||
|
||||
public static bool HasMechanismBehavior<T>(this IBodyPart part) where T : IMechanismBehavior
|
||||
{
|
||||
return part.Mechanisms.Any(m => m.HasBehavior<T>());
|
||||
}
|
||||
|
||||
public static IEnumerable<IMechanismBehavior> GetMechanismBehaviors(this IBody body)
|
||||
{
|
||||
foreach (var (part, _) in body.Parts)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
foreach (var behavior in mechanism.Behaviors.Values)
|
||||
{
|
||||
yield return behavior;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetMechanismBehaviors(this IBody body,
|
||||
[NotNullWhen(true)] out List<IMechanismBehavior>? behaviors)
|
||||
{
|
||||
behaviors = body.GetMechanismBehaviors().ToList();
|
||||
|
||||
if (behaviors.Count == 0)
|
||||
{
|
||||
behaviors = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetMechanismBehaviors<T>(this IBody body) where T : class, IMechanismBehavior
|
||||
{
|
||||
foreach (var (part, _) in body.Parts)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
foreach (var behavior in mechanism.Behaviors.Values)
|
||||
{
|
||||
if (behavior is T tBehavior)
|
||||
{
|
||||
yield return tBehavior;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetMechanismBehaviors<T>(this IBody entity, [NotNullWhen(true)] out List<T>? behaviors)
|
||||
where T : class, IMechanismBehavior
|
||||
{
|
||||
behaviors = entity.GetMechanismBehaviors<T>().ToList();
|
||||
|
||||
if (behaviors.Count == 0)
|
||||
{
|
||||
behaviors = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
175
Content.Server/Body/Behavior/StomachBehavior.cs
Normal file
175
Content.Server/Body/Behavior/StomachBehavior.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Body.Circulatory;
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Shared.Body.Networks;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.Chemistry.Solution;
|
||||
using Content.Shared.Chemistry.Solution.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Behavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Where reagents go when ingested. Tracks ingested reagents over time, and
|
||||
/// eventually transfers them to <see cref="SharedBloodstreamComponent"/> once digested.
|
||||
/// </summary>
|
||||
public class StomachBehavior : MechanismBehavior
|
||||
{
|
||||
private float _accumulatedFrameTime;
|
||||
|
||||
/// <summary>
|
||||
/// Updates digestion status of ingested reagents.
|
||||
/// Once reagents surpass _digestionDelay they are moved to the
|
||||
/// bloodstream, where they are then metabolized.
|
||||
/// </summary>
|
||||
/// <param name="frameTime">
|
||||
/// The time since the last update in seconds.
|
||||
/// </param>
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
if (Body == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime += frameTime;
|
||||
|
||||
// Update at most once per second
|
||||
if (_accumulatedFrameTime < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulatedFrameTime -= 1;
|
||||
|
||||
if (!Body.Owner.TryGetComponent(out SolutionContainerComponent? solution) ||
|
||||
!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Add reagents ready for transfer to bloodstream to transferSolution
|
||||
var transferSolution = new Solution();
|
||||
|
||||
// Use ToList here to remove entries while iterating
|
||||
foreach (var delta in _reagentDeltas.ToList())
|
||||
{
|
||||
//Increment lifetime of reagents
|
||||
delta.Increment(1);
|
||||
if (delta.Lifetime > _digestionDelay)
|
||||
{
|
||||
solution.TryRemoveReagent(delta.ReagentId, delta.Quantity);
|
||||
transferSolution.AddReagent(delta.ReagentId, delta.Quantity);
|
||||
_reagentDeltas.Remove(delta);
|
||||
}
|
||||
}
|
||||
|
||||
// Transfer digested reagents to bloodstream
|
||||
bloodstream.TryTransferSolution(transferSolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Max volume of internal solution storage
|
||||
/// </summary>
|
||||
public ReagentUnit MaxVolume
|
||||
{
|
||||
get => Owner.TryGetComponent(out SharedSolutionContainerComponent? solution) ? solution.MaxVolume : ReagentUnit.Zero;
|
||||
set
|
||||
{
|
||||
if (Owner.TryGetComponent(out SharedSolutionContainerComponent? solution))
|
||||
{
|
||||
solution.MaxVolume = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initial internal solution storage volume
|
||||
/// </summary>
|
||||
[DataField("maxVolume")]
|
||||
[ViewVariables]
|
||||
protected ReagentUnit InitialMaxVolume { get; private set; } = ReagentUnit.New(100);
|
||||
|
||||
/// <summary>
|
||||
/// Time in seconds between reagents being ingested and them being
|
||||
/// transferred to <see cref="SharedBloodstreamComponent"/>
|
||||
/// </summary>
|
||||
[DataField("digestionDelay")] [ViewVariables]
|
||||
private float _digestionDelay = 20;
|
||||
|
||||
/// <summary>
|
||||
/// Used to track how long each reagent has been in the stomach
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
private readonly List<ReagentDelta> _reagentDeltas = new();
|
||||
|
||||
public override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
Owner.EnsureComponentWarn(out SolutionContainerComponent solution);
|
||||
|
||||
solution.MaxVolume = InitialMaxVolume;
|
||||
}
|
||||
|
||||
public bool CanTransferSolution(Solution solution)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SharedSolutionContainerComponent? solutionComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: For now no partial transfers. Potentially change by design
|
||||
if (!solutionComponent.CanAddSolution(solution))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryTransferSolution(Solution solution)
|
||||
{
|
||||
if (Body == null || !CanTransferSolution(solution))
|
||||
return false;
|
||||
|
||||
if (!Body.Owner.TryGetComponent(out SolutionContainerComponent? solutionComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add solution to _stomachContents
|
||||
solutionComponent.TryAddSolution(solution);
|
||||
// Add each reagent to _reagentDeltas. Used to track how long each reagent has been in the stomach
|
||||
foreach (var reagent in solution.Contents)
|
||||
{
|
||||
_reagentDeltas.Add(new ReagentDelta(reagent.ReagentId, reagent.Quantity));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to track quantity changes when ingesting & digesting reagents
|
||||
/// </summary>
|
||||
protected class ReagentDelta
|
||||
{
|
||||
public readonly string ReagentId;
|
||||
public readonly ReagentUnit Quantity;
|
||||
public float Lifetime { get; private set; }
|
||||
|
||||
public ReagentDelta(string reagentId, ReagentUnit quantity)
|
||||
{
|
||||
ReagentId = reagentId;
|
||||
Quantity = quantity;
|
||||
Lifetime = 0.0f;
|
||||
}
|
||||
|
||||
public void Increment(float delta) => Lifetime += delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user