Fix mechanism events not being called properly, add test (#2279)

* Add mechanism events when added/removed to/from body/parts

* Change old usages

* Add TODO

* Remove BodyExtensions and IHasBody

* Remove unnecessary extensions and fix wrong event call in mechanism behavior component

* Complete test and fix event calls
This commit is contained in:
DrSmugleaf
2020-10-17 12:26:39 +02:00
committed by GitHub
parent 05a78f117d
commit 101fa9e466
25 changed files with 365 additions and 154 deletions

View File

@@ -169,7 +169,7 @@ namespace Content.Server.GameObjects.Components.Body
return;
}
if (!player.AttachedEntity.TryGetBody(out var body))
if (!player.AttachedEntity.TryGetComponent(out IBody? body))
{
var random = IoCManager.Resolve<IRobustRandom>();
var text = $"You have no body{(random.Prob(0.2f) ? " and you must scream." : ".")}";
@@ -216,7 +216,7 @@ namespace Content.Server.GameObjects.Components.Body
return;
}
if (!player.AttachedEntity.TryGetBody(out var body))
if (!player.AttachedEntity.TryGetComponent(out IBody? body))
{
var random = IoCManager.Resolve<IRobustRandom>();
var text = $"You have no body{(random.Prob(0.2f) ? " and you must scream." : ".")}";

View File

@@ -47,7 +47,7 @@ namespace Content.Server.GameObjects.Components.Body
PerformerCache = null;
BodyCache = null;
if (eventArgs.Target.TryGetBody(out var body))
if (eventArgs.Target.TryGetComponent(out IBody? body))
{
SendBodyPartListToUser(eventArgs, body);
}

View File

@@ -104,7 +104,7 @@ namespace Content.Server.GameObjects.Components.Body.Part
_surgeonCache = null;
_owningBodyCache = null;
if (eventArgs.Target.TryGetBody(out var body))
if (eventArgs.Target.TryGetComponent(out IBody? body))
{
SendSlots(eventArgs, body);
}

View File

@@ -68,7 +68,7 @@ namespace Content.Server.GameObjects.Components.Body
_callbackCache = null;
// Attempt surgery on a body by sending a list of operable parts for the client to choose from
if (eventArgs.Target.TryGetBody(out var body))
if (eventArgs.Target.TryGetComponent(out IBody? body))
{
// Create dictionary to send to client (text to be shown : data sent back if selected)
var toSend = new Dictionary<string, int>();

View File

@@ -3,6 +3,7 @@ using Content.Server.GameObjects.Components.Body.Behavior;
using Content.Server.GameObjects.Components.Nutrition;
using Content.Server.GameObjects.Components.Utensil;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
@@ -82,7 +83,8 @@ namespace Content.Server.GameObjects.Components.Chemistry
var trueTarget = target ?? user;
if (!trueTarget.TryGetMechanismBehaviors<StomachBehaviorComponent>(out var stomachs))
if (!trueTarget.TryGetComponent(out IBody body) ||
!body.TryGetMechanismBehaviors<StomachBehaviorComponent>(out var stomachs))
{
return false;
}

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Atmos;
@@ -8,12 +9,14 @@ using Content.Server.GameObjects.Components.Temperature;
using Content.Shared.Atmos;
using Content.Shared.Chemistry;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -29,6 +32,8 @@ namespace Content.Server.GameObjects.Components.Metabolism
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[ComponentDependency] private readonly IBody? _body = default!;
public override string Name => "Metabolism";
private float _accumulatedFrameTime;
@@ -38,11 +43,11 @@ namespace Content.Server.GameObjects.Components.Metabolism
[ViewVariables(VVAccess.ReadWrite)] private int _suffocationDamage;
[ViewVariables] public Dictionary<Gas, float> NeedsGases { get; set; }
[ViewVariables] public Dictionary<Gas, float> NeedsGases { get; set; } = new Dictionary<Gas, float>();
[ViewVariables] public Dictionary<Gas, float> ProducesGases { get; set; }
[ViewVariables] public Dictionary<Gas, float> ProducesGases { get; set; } = new Dictionary<Gas, float>();
[ViewVariables] public Dictionary<Gas, float> DeficitGases { get; set; }
[ViewVariables] public Dictionary<Gas, float> DeficitGases { get; set; } = new Dictionary<Gas, float>();
/// <summary>
/// Heat generated due to metabolism. It's generated via metabolism
@@ -176,11 +181,18 @@ namespace Content.Server.GameObjects.Components.Metabolism
private void ProcessGases(float frameTime)
{
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
if (!Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
{
return;
}
if (_body == null)
{
return;
}
var lungs = _body.GetMechanismBehaviors<LungBehaviorComponent>().ToArray();
var needs = NeedsAndDeficit(frameTime);
var used = 0f;
foreach (var (gas, amountNeeded) in needs)
@@ -191,16 +203,13 @@ namespace Content.Server.GameObjects.Components.Metabolism
if (bloodstreamAmount < amountNeeded)
{
// Panic inhale
if (Owner.TryGetMechanismBehaviors(out List<LungBehaviorComponent> lungs))
foreach (var lung in lungs)
{
foreach (var lung in lungs)
{
lung.Gasp();
}
bloodstreamAmount = bloodstream.Air.GetMoles(gas);
lung.Gasp();
}
bloodstreamAmount = bloodstream.Air.GetMoles(gas);
deficit = Math.Max(0, amountNeeded - bloodstreamAmount);
if (deficit > 0)
@@ -238,7 +247,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
/// <param name="frameTime"></param>
private void ProcessThermalRegulation(float frameTime)
{
if (!Owner.TryGetComponent(out TemperatureComponent temperatureComponent)) return;
if (!Owner.TryGetComponent(out TemperatureComponent? temperatureComponent)) return;
temperatureComponent.ReceiveHeat(MetabolismHeat);
temperatureComponent.RemoveHeat(RadiatedHeat);
@@ -308,7 +317,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
/// <param name="frameTime">The time since the last metabolism tick in seconds.</param>
private void ProcessNutrients(float frameTime)
{
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
if (!Owner.TryGetComponent(out BloodstreamComponent? bloodstream))
{
return;
}
@@ -377,7 +386,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
{
Suffocating = true;
if (!Owner.TryGetComponent(out IDamageableComponent damageable))
if (!Owner.TryGetComponent(out IDamageableComponent? damageable))
{
return;
}

View File

@@ -16,7 +16,7 @@ namespace Content.Server.GameObjects.Components.Mobs
{
base.Appearance = value;
if (Owner.TryGetBody(out var body))
if (Owner.TryGetComponent(out IBody body))
{
foreach (var part in body.Parts.Values)
{
@@ -35,7 +35,7 @@ namespace Content.Server.GameObjects.Components.Mobs
{
base.Startup();
if (Appearance != null && Owner.TryGetBody(out var body))
if (Appearance != null && Owner.TryGetComponent(out IBody body))
{
foreach (var part in body.Parts.Values)
{

View File

@@ -5,6 +5,7 @@ using Content.Server.GameObjects.Components.Fluids;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using Content.Shared.GameObjects.Components.Nutrition;
using Content.Shared.GameObjects.EntitySystems;
@@ -151,7 +152,8 @@ namespace Content.Server.GameObjects.Components.Nutrition
return false;
}
if (!target.TryGetMechanismBehaviors<StomachBehaviorComponent>(out var stomachs))
if (!target.TryGetComponent(out IBody body) ||
!body.TryGetMechanismBehaviors<StomachBehaviorComponent>(out var stomachs))
{
return false;
}

View File

@@ -7,6 +7,7 @@ using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Utensil;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Body.Behavior;
using Content.Shared.GameObjects.Components.Body.Mechanism;
using Content.Shared.GameObjects.Components.Utensil;
@@ -131,7 +132,8 @@ namespace Content.Server.GameObjects.Components.Nutrition
var trueTarget = target ?? user;
if (!trueTarget.TryGetMechanismBehaviors<SharedStomachBehaviorComponent>(out var stomachs))
if (!trueTarget.TryGetComponent(out IBody? body) ||
!body.TryGetMechanismBehaviors<SharedStomachBehaviorComponent>(out var stomachs))
{
return false;
}