Solution refactor (#4407)

* Rename SolutionContainerCaps -> Capability

* Move IExamine event to Chemistry System.

* ECS the ISolutionChange into SolutionChangeEvent

* Unify SolutionContainer into a single shared component

* Replace ISolutionInteraction with SolutionContainerComponent

* Move all methods from SolutionContainer to ChemistrySystem

* Refactor EntitySystem calls to Dependencies

* Refactor SolutionContainer to SolutionManager

* Fix yamls

* Fix test fails

* Fix post merge issues

* Fix various issues with SolutionManager

* More fixes

* Fix more components

* Fix events not being directed

* Fixes for Hypospray

* Separate removal and iteration on Metabolism

* Fix creampie problems

* Address some of sloth's issues

* Refactors for Systems

* Refactored solution location

* Fix tests

* Address more sloth issues

* Fix dependency

* Fix merge conflicts

* Add xmldocs for Capabilities components

* Remove HasSolution/TryGetDefaultSolution and Add/Remove Drainable/Refillable

* Replace Grindable/Juiceable with Extractable

* Refactor field names

* Fix Drainable

* Fix some issues with spillable and injector

* Fix issues with Grinder

* Fix Beaker having duplicate solutions

* Fix foaming

* Address some MGS issues

* Fix Uid issues

* Fix errors in solution Tranfer

* Fixed some extra values constant values

* Cola is drinkable now
This commit is contained in:
Ygg01
2021-09-06 15:49:44 +02:00
committed by GitHub
parent b8911d58ac
commit c209e3f29b
166 changed files with 4268 additions and 3278 deletions

View File

@@ -1,11 +1,10 @@
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.Components;
using Content.Shared.Chemistry.EntitySystems;
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;
@@ -18,6 +17,7 @@ namespace Content.Server.Body.Behavior
/// </summary>
public class StomachBehavior : MechanismBehavior
{
private const string DefaultSolutionName = "stomach";
private float _accumulatedFrameTime;
/// <summary>
@@ -30,7 +30,6 @@ namespace Content.Server.Body.Behavior
/// </param>
public override void Update(float frameTime)
{
// Do not metabolise if the organ does not have a body.
if (Body == null)
{
@@ -49,8 +48,8 @@ namespace Content.Server.Body.Behavior
// Note that "Owner" should be the organ that has this behaviour/mechanism, and it should have a dedicated
// solution container. "Body.Owner" is something else, and may have more than one solution container.
if (!Owner.TryGetComponent(out SolutionContainerComponent? solution) ||
!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
if (!Body.Owner.TryGetComponent(out BloodstreamComponent? bloodstream)
|| !EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SharedBloodstreamComponent.DefaultSolutionName, out var solution))
{
return;
}
@@ -68,13 +67,15 @@ namespace Content.Server.Body.Behavior
// This reagent has been in the somach 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 (solution.Solution.ContainsReagent(delta.ReagentId, out ReagentUnit quantity)){
if (quantity > delta.Quantity) {
if (solution.ContainsReagent(delta.ReagentId, out ReagentUnit quantity))
{
if (quantity > delta.Quantity)
{
quantity = delta.Quantity;
}
solution.TryRemoveReagent(delta.ReagentId, quantity);
EntitySystem.Get<SolutionContainerSystem>()
.TryRemoveReagent(Owner.Uid, solution, delta.ReagentId, quantity);
transferSolution.AddReagent(delta.ReagentId, quantity);
}
@@ -86,17 +87,28 @@ namespace Content.Server.Body.Behavior
bloodstream.TryTransferSolution(transferSolution);
}
public Solution? StomachSolution
{
get
{
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, DefaultSolutionName, out var solution);
return solution;
}
}
/// <summary>
/// Max volume of internal solution storage
/// </summary>
public ReagentUnit MaxVolume
{
get => Owner.TryGetComponent(out SharedSolutionContainerComponent? solution) ? solution.MaxVolume : ReagentUnit.Zero;
get =>
StomachSolution?.MaxVolume ?? ReagentUnit.Zero;
set
{
if (Owner.TryGetComponent(out SharedSolutionContainerComponent? solution))
if (StomachSolution != null)
{
solution.MaxVolume = value;
StomachSolution.MaxVolume = value;
}
}
}
@@ -118,27 +130,25 @@ namespace Content.Server.Body.Behavior
/// <summary>
/// Used to track how long each reagent has been in the stomach
/// </summary>
[ViewVariables]
private readonly List<ReagentDelta> _reagentDeltas = new();
[ViewVariables] private readonly List<ReagentDelta> _reagentDeltas = new();
public override void Startup()
{
base.Startup();
Owner.EnsureComponentWarn(out SolutionContainerComponent solution);
var solution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner, DefaultSolutionName);
solution.MaxVolume = InitialMaxVolume;
}
public bool CanTransferSolution(Solution solution)
{
if (!Owner.TryGetComponent(out SharedSolutionContainerComponent? solutionComponent))
if (StomachSolution == null)
{
return false;
}
// TODO: For now no partial transfers. Potentially change by design
if (!solutionComponent.CanAddSolution(solution))
if (!StomachSolution.CanAddSolution(solution))
{
return false;
}
@@ -151,13 +161,13 @@ namespace Content.Server.Body.Behavior
if (Owner == null || !CanTransferSolution(solution))
return false;
if (!Owner.TryGetComponent(out SolutionContainerComponent? solutionComponent))
if (StomachSolution == null)
{
return false;
}
// Add solution to _stomachContents
solutionComponent.TryAddSolution(solution);
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)
{

View File

@@ -1,13 +1,12 @@
using System;
using System.Linq;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Respiratory;
using Content.Server.Chemistry.Components;
using Content.Shared.Atmos;
using Content.Shared.Body.Networks;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Chemistry.Solution;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -23,31 +22,33 @@ namespace Content.Server.Body.Circulatory
/// <summary>
/// Max volume of internal solution storage
/// </summary>
[DataField("maxVolume")]
[ViewVariables] private ReagentUnit _initialMaxVolume = ReagentUnit.New(250);
[DataField("maxVolume")] [ViewVariables]
private ReagentUnit _initialMaxVolume = ReagentUnit.New(250);
/// <summary>
/// Internal solution for reagent storage
/// </summary>
[ViewVariables] private SolutionContainerComponent _internalSolution = default!;
[ViewVariables] private Solution? _internalSolution;
/// <summary>
/// Empty volume of internal solution
/// </summary>
[ViewVariables] public ReagentUnit EmptyVolume => _internalSolution.EmptyVolume;
[ViewVariables]
public ReagentUnit EmptyVolume => _internalSolution?.AvailableVolume ?? ReagentUnit.Zero;
[ViewVariables]
public GasMixture Air { get; set; } = new(6)
{Temperature = Atmospherics.NormalBodyTemperature};
[ViewVariables] public SolutionContainerComponent Solution => _internalSolution;
{ Temperature = Atmospherics.NormalBodyTemperature };
protected override void Initialize()
{
base.Initialize();
_internalSolution = Owner.EnsureComponent<SolutionContainerComponent>();
_internalSolution.MaxVolume = _initialMaxVolume;
_internalSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner, DefaultSolutionName);
if (_internalSolution != null)
{
_internalSolution.MaxVolume = _initialMaxVolume;
}
}
/// <summary>
@@ -59,12 +60,14 @@ namespace Content.Server.Body.Circulatory
public override bool TryTransferSolution(Solution solution)
{
// For now doesn't support partial transfers
if (solution.TotalVolume + _internalSolution.CurrentVolume > _internalSolution.MaxVolume)
var current = _internalSolution?.CurrentVolume ?? ReagentUnit.Zero;
var max = _internalSolution?.MaxVolume ?? ReagentUnit.Zero;
if (solution.TotalVolume + current > max)
{
return false;
}
_internalSolution.TryAddSolution(solution);
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(Owner.Uid, _internalSolution, solution);
return true;
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Content.Shared.Body.Networks;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -25,11 +26,11 @@ namespace Content.Server.Body.Metabolism
public float UpdateFrequency = 1.0f;
/// <summary>
/// Whether this metabolizer should attempt to metabolize chemicals in its parent bodies' bloodstream,
/// From which solution will this metabolizer attempt to metabolize chemicals in its parent bodies' bloodstream,
/// as opposed to a solution container on the metabolizing entity itself.
/// </summary>
[DataField("takeFromBloodstream")]
public bool TakeFromBloodstream = true;
[DataField("solution")]
public string SolutionName { get; set; } = SharedBloodstreamComponent.DefaultSolutionName;
/// <summary>
/// A dictionary mapping reagent string IDs to a list of effects & associated metabolism rate.

View File

@@ -1,20 +1,36 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Body.Circulatory;
using Content.Server.Chemistry.Components;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Chemistry.Solution;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.Body.Metabolism
{
// TODO mirror in the future working on mechanisms move updating here to BodySystem so it can be ordered?
[UsedImplicitly]
public class MetabolizerSystem : EntitySystem
{
[Dependency]
private readonly SolutionContainerSystem _solutionContainerSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MetabolizerComponent, ComponentInit>(OnMetabolizerInit);
}
private void OnMetabolizerInit(EntityUid uid, MetabolizerComponent component, ComponentInit args)
{
_solutionContainerSystem.EnsureSolution(EntityManager.GetEntity(uid), component.SolutionName);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
@@ -35,37 +51,34 @@ namespace Content.Server.Body.Metabolism
private void TryMetabolize(MetabolizerComponent comp)
{
var owner = comp.Owner;
var reagentList = new List<Solution.ReagentQuantity>();
SolutionContainerComponent? solution = null;
IReadOnlyList<Solution.ReagentQuantity> reagentList = new List<Solution.ReagentQuantity>();
Solution? solution = null;
SharedBodyComponent? body = null;
var solutionsSys = Get<SolutionContainerSystem>();
// if this field is passed we should try and take from the bloodstream over anything else
if (comp.TakeFromBloodstream && owner.TryGetComponent<SharedMechanismComponent>(out var mech))
if (owner.TryGetComponent<SharedMechanismComponent>(out var mech))
{
body = mech.Body;
if (body != null)
{
if (body.Owner.TryGetComponent<BloodstreamComponent>(out var bloodstream)
&& bloodstream.Solution.CurrentVolume >= ReagentUnit.Zero)
if (body.Owner.HasComponent<BloodstreamComponent>()
&& solutionsSys.TryGetSolution(body.Owner, comp.SolutionName, out solution)
&& solution.CurrentVolume >= ReagentUnit.Zero)
{
solution = bloodstream.Solution;
reagentList = bloodstream.Solution.ReagentList.ToList();
reagentList = solution.Contents;
}
}
}
else if (owner.TryGetComponent<SolutionContainerComponent>(out var sol))
{
// if we have no mechanism/body but a solution container instead,
// we'll just use that to metabolize from
solution = sol;
reagentList = sol.ReagentList.ToList();
}
if (solution == null || reagentList.Count == 0)
{
// We're all outta ideas on where to metabolize from
return;
}
List<Solution.ReagentQuantity> removeReagents = new(5);
// Run metabolism for each reagent, remove metabolized reagents
foreach (var reagent in reagentList)
{
@@ -100,8 +113,10 @@ namespace Content.Server.Body.Metabolism
effect.Metabolize(ent, reagent);
}
solution.TryRemoveReagent(reagent.ReagentId, metabolism.MetabolismRate);
removeReagents.Add(new Solution.ReagentQuantity(reagent.ReagentId, metabolism.MetabolismRate));
}
solutionsSys.TryRemoveAllReagents(solution, removeReagents);
}
}
}