Files
tbd-station-14/Content.Server/GameObjects/Components/Nutrition/StomachComponent.cs
moneyl 3ab8036363 De-hardcode chemical metabolism of StomachComponent (#437)
* Move chemical reaction effects into Chemistry/ReactionEffects/ subfolder

* Replace hardcoded StomachComponent metabolism with IMetabolizable

The benefits of this approach are that reagent metabolism effects are not hardcoded into StomachComponent, and metabolism effects can be more easily chained together in yaml prototypes, and reagents can have different metabolism rates. One problem with this approach is that getting metabolism rates slower than 1u / second is impossible. Implementing #377 should resolve that problem.

* Fix DefaultFood and DefaultDrink so they remove reagent regardless of Hunger/ThirstComponent presence

Previously if neither of those were present the reagents wouldn't be removed from the stomach. This fixes that.

* Additional comment on function

* Make metabolizer interface implementations explicit

Also removed some unused using statements

* Make StomachComponent._reagentDeltas readonly

* Fix misleading variable names and docs for metabolizables

Changes one of the arguments for `IMetabolizable.Metabolize()` to be called `tickTime` instead of `frameTime` to more accurately reflect it's purpose. It's not really the frametime, but the time since the last metabolism tick. Also updated and expanded the docs to reflect this and to be more clear.
2019-11-21 23:24:19 +01:00

99 lines
3.8 KiB
C#

using System.Collections.Generic;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Nutrition;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Nutrition
{
[RegisterComponent]
public class StomachComponent : SharedStomachComponent
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
#pragma warning restore 649
[ViewVariables(VVAccess.ReadOnly)]
private SolutionComponent _stomachContents;
public int MaxVolume
{
get => _stomachContents.MaxVolume;
set => _stomachContents.MaxVolume = value;
}
private int _initialMaxVolume;
//Used to track changes to reagent amounts during metabolism
private readonly Dictionary<string, int> _reagentDeltas = new Dictionary<string, int>();
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _initialMaxVolume, "max_volume", 20);
}
public override void Initialize()
{
base.Initialize();
//Doesn't use Owner.AddComponent<>() to avoid cross-contamination (e.g. with blood or whatever they holds other solutions)
_stomachContents = new SolutionComponent();
_stomachContents.InitializeFromPrototype();
_stomachContents.MaxVolume = _initialMaxVolume;
_stomachContents.Owner = Owner; //Manually set owner to avoid crash when VV'ing this
}
public bool TryTransferSolution(Solution solution)
{
// TODO: For now no partial transfers. Potentially change by design
if (solution.TotalVolume + _stomachContents.CurrentVolume > _stomachContents.MaxVolume)
{
return false;
}
_stomachContents.TryAddSolution(solution, false, true);
return true;
}
/// <summary>
/// Loops through each reagent in _stomachContents, and calls the IMetabolizable for each of them./>
/// </summary>
/// <param name="tickTime">The time since the last metabolism tick in seconds.</param>
public void Metabolize(float tickTime)
{
if (_stomachContents.CurrentVolume == 0)
return;
//Run metabolism for each reagent, track quantity changes
_reagentDeltas.Clear();
foreach (var reagent in _stomachContents.ReagentList)
{
if(!_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype proto))
continue;
foreach (var metabolizable in proto.Metabolism)
{
_reagentDeltas[reagent.ReagentId] = metabolizable.Metabolize(Owner, reagent.ReagentId, tickTime);
}
}
//Apply changes to quantity afterwards. Can't change the reagent quantities while the iterating the
//list of reagents, because that would invalidate the iterator and throw an exception.
foreach (var reagentDelta in _reagentDeltas)
{
_stomachContents.TryRemoveReagent(reagentDelta.Key, reagentDelta.Value);
}
}
/// <summary>
/// Triggers metabolism of the reagents inside _stomachContents. Called by <see cref="StomachSystem"/>
/// </summary>
/// <param name="tickTime">The time since the last metabolism tick in seconds.</param>
public void OnUpdate(float tickTime)
{
Metabolize(tickTime);
}
}
}