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:
mirrorcult
2021-11-11 16:10:57 -07:00
committed by GitHub
parent 509e1ba6e7
commit 457e8c64ee
77 changed files with 326 additions and 307 deletions

View File

@@ -2,8 +2,7 @@ using System;
using Content.Server.Atmos;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Circulatory;
using Content.Server.Body.Respiratory;
using Content.Server.Body.Components;
using Content.Server.Popups;
using Content.Shared.Atmos;
using Content.Shared.Body.Components;

View File

@@ -1,6 +1,5 @@
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;

View File

@@ -1,198 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Body.Circulatory;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Body.Networks;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
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 const string DefaultSolutionName = "stomach";
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)
{
// Do not metabolise if the organ does not have a body.
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) ||
StomachSolution == null) // Something has gone wrong here.
{
return;
}
// Reagents ready to transfer into the bloodstream are added to this solution
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)
{
// This reagent has been in the stomach long enough, TRY to transfer it.
// But first, check if the reagent still exists, and how much is left.
// Some poor spessman may have washed down a potassium snack with some water.
if (StomachSolution.ContainsReagent(delta.ReagentId, out FixedPoint2 quantity))
{
if (quantity > delta.Quantity)
{
quantity = delta.Quantity;
}
EntitySystem.Get<SolutionContainerSystem>()
.TryRemoveReagent(Owner.Uid, StomachSolution, delta.ReagentId, quantity);
transferSolution.AddReagent(delta.ReagentId, quantity);
}
_reagentDeltas.Remove(delta);
}
}
// Transfer digested reagents to bloodstream
bloodstream.TryTransferSolution(transferSolution);
}
public Solution? StomachSolution
{
get
{
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner.Uid, DefaultSolutionName, out var solution);
return solution;
}
}
/// <summary>
/// Max volume of internal solution storage
/// </summary>
public FixedPoint2 MaxVolume
{
get =>
StomachSolution?.MaxVolume ?? FixedPoint2.Zero;
set
{
if (StomachSolution != null)
{
StomachSolution.MaxVolume = value;
}
}
}
/// <summary>
/// Initial internal solution storage volume
/// </summary>
[DataField("maxVolume")]
[ViewVariables]
protected 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")] [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();
var solution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner.Uid, DefaultSolutionName);
solution.MaxVolume = InitialMaxVolume;
}
public bool CanTransferSolution(Solution solution)
{
if (StomachSolution == null)
{
return false;
}
// TODO: For now no partial transfers. Potentially change by design
if (!StomachSolution.CanAddSolution(solution))
{
return false;
}
return true;
}
public bool TryTransferSolution(Solution solution)
{
if (Owner == null || !CanTransferSolution(solution))
return false;
if (StomachSolution == null)
{
return false;
}
// Add solution to _stomachContents
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(Owner.Uid, StomachSolution, 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 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;
}
}
}

View File

@@ -1,18 +1,16 @@
using System;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Respiratory;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Body.Networks;
using Content.Shared.Body.Components;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Body.Circulatory
namespace Content.Server.Body.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedBloodstreamComponent))]

View File

@@ -1,8 +1,8 @@
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.Body.Slot;
using Content.Shared.Random.Helpers;
using Content.Shared.Sound;
using Robust.Shared.Audio;
@@ -12,7 +12,7 @@ using Robust.Shared.Log;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Body
namespace Content.Server.Body.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedBodyComponent))]

View File

@@ -2,7 +2,7 @@
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;
namespace Content.Server.Body.Respiratory
namespace Content.Server.Body.Components
{
[RegisterComponent]
public class InternalsComponent : Component

View File

@@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.UserInterface;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Body.Part;
using Content.Shared.Body.Surgery;
using Content.Shared.Interaction;
@@ -14,7 +13,7 @@ using Robust.Shared.Localization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.Body.Mechanism
namespace Content.Server.Body.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedMechanismComponent))]

View File

@@ -1,18 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Content.Shared.Body.Metabolism;
using Content.Shared.Body.Networks;
using Content.Shared.Chemistry.Reagent;
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.Dictionary;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
namespace Content.Server.Body.Metabolism
namespace Content.Server.Body.Components
{
/// <summary>
/// Handles metabolizing various reagents with given effects.

View File

@@ -5,7 +5,6 @@ using Content.Server.Alert;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Behavior;
using Content.Server.Body.Circulatory;
using Content.Server.Temperature.Components;
using Content.Server.Temperature.Systems;
using Content.Shared.ActionBlocker;
@@ -20,7 +19,7 @@ using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Body.Respiratory
namespace Content.Server.Body.Components
{
[RegisterComponent]
public class RespiratorComponent : Component

View File

@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Server.Body.Respiratory
namespace Content.Server.Body.Components
{
[UsedImplicitly]
public class RespiratorSystem : EntitySystem

View 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;
}
}
}

View File

@@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.UserInterface;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Body.Part;
using Content.Shared.Body.Surgery;
using Content.Shared.Interaction;

View File

@@ -1,6 +1,5 @@
using Content.Server.UserInterface;
using Content.Shared.Body.Components;
using Content.Shared.Body.Scanner;
using Content.Shared.Interaction;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;

View File

@@ -4,7 +4,6 @@ using System.Text;
using System.Threading.Tasks;
using Content.Server.DoAfter;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Body.Part;
using Content.Shared.Body.Surgery;
using Content.Shared.Popups;

View File

@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.Body.Mechanism;
using Content.Server.Body.Components;
using Content.Server.Body.Surgery.Messages;
using Content.Server.UserInterface;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Body.Part;
using Content.Shared.Body.Surgery;
using Content.Shared.Interaction;

View File

@@ -1,12 +1,17 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Body.Components;
using Content.Server.GameTicking;
using Content.Server.Mind.Components;
using Content.Shared.Body.Components;
using Content.Shared.MobState.Components;
using Content.Shared.Movement.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
namespace Content.Server.Body
namespace Content.Server.Body.Systems
{
public sealed class BodySystem : EntitySystem
{
@@ -34,5 +39,40 @@ namespace Content.Server.Body
_ticker.OnGhostAttempt(mind.Mind!, true);
}
}
public IEnumerable<T> GetComponentsOnMechanisms<T>(EntityUid uid,
SharedBodyComponent? body) where T : Component
{
if (!Resolve(uid, ref body))
yield break;
foreach (var (part, _) in body.Parts)
foreach (var mechanism in part.Mechanisms)
{
if (EntityManager.TryGetComponent<T>(mechanism.OwnerUid, out var comp))
yield return comp;
}
}
public bool TryGetComponentsOnMechanisms<T>(EntityUid uid,
[NotNullWhen(true)] out IEnumerable<T>? comps,
SharedBodyComponent? body) where T: Component
{
if (!Resolve(uid, ref body))
{
comps = null;
return false;
}
comps = GetComponentsOnMechanisms<T>(uid, body).ToArray();
if (!comps.Any())
{
comps = null;
return false;
}
return true;
}
}
}

View File

@@ -1,11 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Body.Circulatory;
using Content.Server.Body.Mechanism;
using Content.Server.Body.Components;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
@@ -16,7 +12,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Body.Metabolism
namespace Content.Server.Body.Systems
{
// TODO mirror in the future working on mechanisms move updating here to BodySystem so it can be ordered?
[UsedImplicitly]

View File

@@ -0,0 +1,121 @@
using Content.Server.Body.Components;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Utility;
namespace Content.Server.Body.Systems
{
public class StomachSystem : EntitySystem
{
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
public const string DefaultSolutionName = "stomach";
public override void Initialize()
{
SubscribeLocalEvent<StomachComponent, ComponentInit>(OnComponentInit);
}
public override void Update(float frameTime)
{
foreach (var (stomach, mech, sol)
in EntityManager.EntityQuery<StomachComponent, MechanismComponent, SolutionContainerManagerComponent>(false))
{
if (mech.Body == null)
continue;
stomach.AccumulatedFrameTime += frameTime;
if (stomach.AccumulatedFrameTime < stomach.UpdateInterval)
continue;
stomach.AccumulatedFrameTime -= stomach.UpdateInterval;
// Get our solutions
if (!_solutionContainerSystem.TryGetSolution(stomach.OwnerUid, DefaultSolutionName,
out var stomachSolution, sol))
continue;
if (!_solutionContainerSystem.TryGetSolution(mech.Body.OwnerUid, stomach.BodySolutionName,
out var bodySolution))
continue;
var transferSolution = new Solution();
var queue = new RemQueue<StomachComponent.ReagentDelta>();
foreach (var delta in stomach.ReagentDeltas)
{
delta.Increment(stomach.UpdateInterval);
if (delta.Lifetime > stomach.DigestionDelay)
{
if (stomachSolution.ContainsReagent(delta.ReagentId, out var quant))
{
if (quant > delta.Quantity)
quant = delta.Quantity;
_solutionContainerSystem.TryRemoveReagent(stomach.OwnerUid, stomachSolution,
delta.ReagentId, quant);
transferSolution.AddReagent(delta.ReagentId, quant);
}
queue.Add(delta);
}
}
foreach (var item in queue)
{
stomach.ReagentDeltas.Remove(item);
}
// Transfer everything to the body solution!
_solutionContainerSystem.TryAddSolution(mech.Body.OwnerUid, bodySolution, transferSolution);
}
}
private void OnComponentInit(EntityUid uid, StomachComponent component, ComponentInit args)
{
var solution = _solutionContainerSystem.EnsureSolution(uid, DefaultSolutionName);
solution.MaxVolume = component.InitialMaxVolume;
}
public bool CanTransferSolution(EntityUid uid, Solution solution,
SolutionContainerManagerComponent? solutions=null)
{
if (!Resolve(uid, ref solutions, false))
return false;
if (!_solutionContainerSystem.TryGetSolution(uid, DefaultSolutionName, out var stomachSolution, solutions))
return false;
// TODO: For now no partial transfers. Potentially change by design
if (!stomachSolution.CanAddSolution(solution))
return false;
return true;
}
public bool TryTransferSolution(EntityUid uid, Solution solution,
StomachComponent? stomach=null,
SolutionContainerManagerComponent? solutions=null)
{
if (!Resolve(uid, ref stomach, ref solutions, false))
return false;
if (!_solutionContainerSystem.TryGetSolution(uid, DefaultSolutionName, out var stomachSolution, solutions)
|| !CanTransferSolution(uid, solution, solutions))
return false;
_solutionContainerSystem.TryAddSolution(uid, stomachSolution, solution);
// Add each reagent to ReagentDeltas. Used to track how long each reagent has been in the stomach
foreach (var reagent in solution.Contents)
{
stomach.ReagentDeltas.Add(new StomachComponent.ReagentDelta(reagent.ReagentId, reagent.Quantity));
}
return true;
}
}
}