Metabolism 3.0 (#5157)

* basic system + convert all plantmetabolism

* stragglers

* convert all old metabolisms over

* fix YAML errors + dumb serialization issue

* remove unused thingy

* reimplement

* add organ type condition

* organtype condition but real

* cleanups + test fix

* metabolismtype -> metabolizertype

* solution resilience

* fixes

* serializer + use entityuid + hashset

* this is apparently an entirely different thing

* turns out it just works

* oops
This commit is contained in:
mirrorcult
2021-11-08 15:33:45 -07:00
committed by GitHub
parent f5b11d6af8
commit 31d622f941
53 changed files with 969 additions and 912 deletions

View File

@@ -1,18 +1,23 @@
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.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
{
/// <summary>
/// Handles metabolizing various reagents with given effects.
/// </summary>
[RegisterComponent]
[RegisterComponent, Friend(typeof(MetabolizerSystem))]
public class MetabolizerComponent : Component
{
public override string Name => "Metabolizer";
@@ -27,33 +32,44 @@ namespace Content.Server.Body.Metabolism
public float UpdateFrequency = 1.0f;
/// <summary>
/// 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.
/// From which solution will this metabolizer attempt to metabolize chemicals
/// </summary>
[DataField("solution")]
public string SolutionName { get; set; } = SharedBloodstreamComponent.DefaultSolutionName;
/// <summary>
/// A dictionary mapping reagent string IDs to a list of effects & associated metabolism rate.
/// Does this component use a solution on it's parent entity (the body) or itself
/// </summary>
/// <returns></returns>
[DataField("metabolisms", required: true, customTypeSerializer:typeof(PrototypeIdDictionarySerializer<ReagentEffectsEntry, ReagentPrototype>))]
public Dictionary<string, ReagentEffectsEntry> Metabolisms = default!;
/// <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>
/// A list of metabolism groups that this metabolizer will act on, in order of precedence.
/// </summary>
[DataField("groups", required: true)]
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 ReagentEffectsEntry
public class MetabolismGroupEntry
{
/// <summary>
/// Amount of reagent to metabolize, per metabolism cycle.
/// </summary>
[DataField("metabolismRate")]
public FixedPoint2 MetabolismRate = FixedPoint2.New(1.0f);
[DataField("id", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<MetabolismGroupPrototype>))]
public string Id = default!;
/// <summary>
/// A list of effects to apply when these reagents are metabolized.
/// </summary>
[DataField("effects", required: true)]
public ReagentEffect[] Effects = default!;
[DataField("rateModifier")]
public FixedPoint2 MetabolismRateModifier = 1.0;
}
}

View File

@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Body.Circulatory;
using Content.Server.Body.Mechanism;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Body.Components;
using Content.Shared.Body.Mechanism;
@@ -9,6 +12,7 @@ using Content.Shared.FixedPoint;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.Body.Metabolism
{
@@ -16,9 +20,8 @@ namespace Content.Server.Body.Metabolism
[UsedImplicitly]
public class MetabolizerSystem : EntitySystem
{
[Dependency]
private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
@@ -29,7 +32,20 @@ namespace Content.Server.Body.Metabolism
private void OnMetabolizerInit(EntityUid uid, MetabolizerComponent component, ComponentInit args)
{
_solutionContainerSystem.EnsureSolution(uid, component.SolutionName);
if (!component.SolutionOnBody)
{
_solutionContainerSystem.EnsureSolution(uid, component.SolutionName);
}
else
{
if (EntityManager.TryGetComponent<MechanismComponent>(uid, out var mech))
{
if (mech.Body != null)
{
_solutionContainerSystem.EnsureSolution(mech.Body.OwnerUid, component.SolutionName);
}
}
}
}
public override void Update(float frameTime)
@@ -43,80 +59,96 @@ namespace Content.Server.Body.Metabolism
// Only update as frequently as it should
if (metab.AccumulatedFrametime >= metab.UpdateFrequency)
{
metab.AccumulatedFrametime = 0.0f;
TryMetabolize(metab);
metab.AccumulatedFrametime -= metab.UpdateFrequency;
TryMetabolize(metab.OwnerUid, metab);
}
}
}
private void TryMetabolize(MetabolizerComponent comp)
private void TryMetabolize(EntityUid uid, MetabolizerComponent? meta=null, MechanismComponent? mech=null)
{
var owner = comp.Owner;
IReadOnlyList<Solution.ReagentQuantity> reagentList = new List<Solution.ReagentQuantity>();
Solution? solution = null;
SharedBodyComponent? body = null;
if (!Resolve(uid, ref meta))
return;
// if this field is passed we should try and take from the bloodstream over anything else
if (owner.TryGetComponent<SharedMechanismComponent>(out var mech))
Resolve(uid, ref mech, false);
// First step is get the solution we actually care about
Solution? solution = null;
EntityUid? solutionEntityUid = null;
SolutionContainerManagerComponent? manager = null;
if (meta.SolutionOnBody)
{
body = mech.Body;
if (body != null)
if (mech != null)
{
if (body.Owner.HasComponent<BloodstreamComponent>()
&& _solutionContainerSystem.TryGetSolution(body.OwnerUid, comp.SolutionName, out solution)
&& solution.CurrentVolume >= FixedPoint2.Zero)
var body = mech.Body;
if (body != null)
{
reagentList = solution.Contents;
if (!Resolve(body.OwnerUid, ref manager, false))
return;
_solutionContainerSystem.TryGetSolution(body.OwnerUid, meta.SolutionName, out solution, manager);
solutionEntityUid = body.OwnerUid;
}
}
}
if (solution == null || reagentList.Count == 0)
else
{
// We're all outta ideas on where to metabolize from
return;
if (!Resolve(uid, ref manager, false))
return;
_solutionContainerSystem.TryGetSolution(uid, meta.SolutionName, out solution, manager);
solutionEntityUid = uid;
}
List<Solution.ReagentQuantity> removeReagents = new(5);
var ent = body?.Owner ?? owner;
// Run metabolism for each reagent, remove metabolized reagents
foreach (var reagent in reagentList)
if (solutionEntityUid == null || solution == null)
return;
// we found our guy
foreach (var reagent in solution.Contents.ToArray())
{
if (!comp.Metabolisms.ContainsKey(reagent.ReagentId))
if (!_prototypeManager.TryIndex<ReagentPrototype>(reagent.ReagentId, out var proto))
continue;
var metabolism = comp.Metabolisms[reagent.ReagentId];
// Run metabolism code for each reagent
foreach (var effect in metabolism.Effects)
{
var conditionsMet = true;
if (effect.Conditions != null)
{
// yes this is 3 nested for loops, but all of these lists are
// basically guaranteed to be small or empty
foreach (var condition in effect.Conditions)
{
if (!condition.Condition(ent, reagent))
{
conditionsMet = false;
break;
}
}
}
if (proto.Metabolisms == null)
continue;
if (!conditionsMet)
// loop over all our groups and see which ones apply
FixedPoint2 mostToRemove = FixedPoint2.Zero;
foreach (var group in meta.MetabolismGroups)
{
if (!proto.Metabolisms.Keys.Contains(group.Id))
continue;
// If we're part of a body, pass that entity to Metabolize
// Otherwise, just pass our owner entity, maybe we're a plant or something
effect.Metabolize(ent, reagent);
var entry = proto.Metabolisms[group.Id];
// we don't remove reagent for every group, just whichever had the biggest rate
if (entry.MetabolismRate > mostToRemove)
mostToRemove = entry.MetabolismRate;
// do all effects, if conditions apply
foreach (var effect in entry.Effects)
{
bool failed = false;
var quant = new Solution.ReagentQuantity(reagent.ReagentId, reagent.Quantity);
if (effect.Conditions != null)
{
foreach (var cond in effect.Conditions)
{
if (!cond.Condition(solutionEntityUid.Value, meta.OwnerUid, quant, EntityManager))
failed = true;
}
if (failed)
continue;
}
effect.Metabolize(solutionEntityUid.Value, meta.OwnerUid, quant, EntityManager);
}
}
removeReagents.Add(new Solution.ReagentQuantity(reagent.ReagentId, metabolism.MetabolismRate));
// remove a certain amount of reagent
if (mostToRemove > FixedPoint2.Zero)
_solutionContainerSystem.TryRemoveReagent(solutionEntityUid.Value, solution, reagent.ReagentId, mostToRemove);
}
_solutionContainerSystem.TryRemoveAllReagents(ent.Uid, solution, removeReagents);
}
}
}