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:
78
Content.Server/Body/Systems/BodySystem.cs
Normal file
78
Content.Server/Body/Systems/BodySystem.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
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.Systems
|
||||
{
|
||||
public sealed class BodySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly GameTicker _ticker = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<BodyComponent, RelayMoveInputEvent>(OnRelayMoveInput);
|
||||
}
|
||||
|
||||
private void OnRelayMoveInput(EntityUid uid, BodyComponent component, RelayMoveInputEvent args)
|
||||
{
|
||||
if (EntityManager.TryGetComponent<MobStateComponent>(uid, out var mobState) &&
|
||||
mobState.IsDead() &&
|
||||
EntityManager.TryGetComponent<MindComponent>(uid, out var mind) &&
|
||||
mind.HasMind)
|
||||
{
|
||||
if (!mind.Mind!.TimeOfDeath.HasValue)
|
||||
{
|
||||
mind.Mind.TimeOfDeath = _gameTiming.RealTime;
|
||||
}
|
||||
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
182
Content.Server/Body/Systems/MetabolizerSystem.cs
Normal file
182
Content.Server/Body/Systems/MetabolizerSystem.cs
Normal file
@@ -0,0 +1,182 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.MobState.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Body.Systems
|
||||
{
|
||||
// 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!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<MetabolizerComponent, ComponentInit>(OnMetabolizerInit);
|
||||
}
|
||||
|
||||
private void OnMetabolizerInit(EntityUid uid, MetabolizerComponent component, ComponentInit args)
|
||||
{
|
||||
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)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var metab in EntityManager.EntityQuery<MetabolizerComponent>(false))
|
||||
{
|
||||
metab.AccumulatedFrametime += frameTime;
|
||||
|
||||
// Only update as frequently as it should
|
||||
if (metab.AccumulatedFrametime >= metab.UpdateFrequency)
|
||||
{
|
||||
metab.AccumulatedFrametime -= metab.UpdateFrequency;
|
||||
TryMetabolize(metab.OwnerUid, metab);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TryMetabolize(EntityUid uid, MetabolizerComponent? meta=null, MechanismComponent? mech=null)
|
||||
{
|
||||
if (!Resolve(uid, ref meta))
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
if (mech != null)
|
||||
{
|
||||
var body = mech.Body;
|
||||
|
||||
if (body != null)
|
||||
{
|
||||
if (!Resolve(body.OwnerUid, ref manager, false))
|
||||
return;
|
||||
_solutionContainerSystem.TryGetSolution(body.OwnerUid, meta.SolutionName, out solution, manager);
|
||||
solutionEntityUid = body.OwnerUid;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Resolve(uid, ref manager, false))
|
||||
return;
|
||||
_solutionContainerSystem.TryGetSolution(uid, meta.SolutionName, out solution, manager);
|
||||
solutionEntityUid = uid;
|
||||
}
|
||||
|
||||
if (solutionEntityUid == null || solution == null)
|
||||
return;
|
||||
|
||||
// randomize the reagent list so we don't have any weird quirks
|
||||
// like alphabetical order or insertion order mattering for processing
|
||||
var list = solution.Contents.ToArray();
|
||||
_random.Shuffle(list);
|
||||
|
||||
int reagents = 0;
|
||||
foreach (var reagent in list)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex<ReagentPrototype>(reagent.ReagentId, out var proto))
|
||||
continue;
|
||||
|
||||
FixedPoint2 mostToRemove = FixedPoint2.Zero;
|
||||
if (proto.Metabolisms == null)
|
||||
{
|
||||
if (meta.RemoveEmpty)
|
||||
_solutionContainerSystem.TryRemoveReagent(solutionEntityUid.Value, solution, reagent.ReagentId, FixedPoint2.New(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
// we're done here entirely if this is true
|
||||
if (reagents >= meta.MaxReagentsProcessable)
|
||||
return;
|
||||
reagents += 1;
|
||||
|
||||
// loop over all our groups and see which ones apply
|
||||
if (meta.MetabolismGroups == null)
|
||||
continue;
|
||||
|
||||
foreach (var group in meta.MetabolismGroups)
|
||||
{
|
||||
if (!proto.Metabolisms.Keys.Contains(group.Id))
|
||||
continue;
|
||||
|
||||
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;
|
||||
|
||||
// if it's possible for them to be dead, and they are,
|
||||
// then we shouldn't process any effects, but should probably
|
||||
// still remove reagents
|
||||
if (EntityManager.TryGetComponent<MobStateComponent>(solutionEntityUid.Value, out var state))
|
||||
{
|
||||
if (state.IsDead())
|
||||
continue;
|
||||
}
|
||||
|
||||
var args = new ReagentEffectArgs(solutionEntityUid.Value, meta.OwnerUid, solution, proto, entry.MetabolismRate,
|
||||
EntityManager, null);
|
||||
|
||||
// do all effects, if conditions apply
|
||||
foreach (var effect in entry.Effects)
|
||||
{
|
||||
bool failed = false;
|
||||
if (effect.Conditions != null)
|
||||
{
|
||||
foreach (var cond in effect.Conditions)
|
||||
{
|
||||
if (!cond.Condition(args))
|
||||
failed = true;
|
||||
}
|
||||
|
||||
if (failed)
|
||||
continue;
|
||||
}
|
||||
|
||||
effect.Metabolize(args);
|
||||
}
|
||||
}
|
||||
|
||||
// remove a certain amount of reagent
|
||||
if (mostToRemove > FixedPoint2.Zero)
|
||||
_solutionContainerSystem.TryRemoveReagent(solutionEntityUid.Value, solution, reagent.ReagentId, mostToRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
121
Content.Server/Body/Systems/StomachSystem.cs
Normal file
121
Content.Server/Body/Systems/StomachSystem.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user