diff --git a/Content.IntegrationTests/Tests/GameObjects/Components/ActionBlocking/CuffUnitTest.cs b/Content.IntegrationTests/Tests/GameObjects/Components/ActionBlocking/CuffUnitTest.cs index ea8d5ea324..682f01752a 100644 --- a/Content.IntegrationTests/Tests/GameObjects/Components/ActionBlocking/CuffUnitTest.cs +++ b/Content.IntegrationTests/Tests/GameObjects/Components/ActionBlocking/CuffUnitTest.cs @@ -87,7 +87,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.ActionBlocking var slot = part.GetHashCode().ToString(); body.Template.Slots.Add(slot, BodyPartType.Hand); - body.InstallBodyPart(part, slot); + body.TryAddPart(slot, part, true); } } } diff --git a/Content.Server/Body/BodyCommands.cs b/Content.Server/Body/BodyCommands.cs index da8c9e4ca1..899a793f6d 100644 --- a/Content.Server/Body/BodyCommands.cs +++ b/Content.Server/Body/BodyCommands.cs @@ -48,7 +48,7 @@ namespace Content.Server.Body var slot = part.GetHashCode().ToString(); body.Template.Slots.Add(slot, BodyPartType.Hand); - body.InstallBodyPart(part, slot); + body.TryAddPart(slot, part, true); } } diff --git a/Content.Server/Body/BodyPart.cs b/Content.Server/Body/BodyPart.cs index 32121f056a..acd984af53 100644 --- a/Content.Server/Body/BodyPart.cs +++ b/Content.Server/Body/BodyPart.cs @@ -14,13 +14,11 @@ using Content.Shared.Damage.DamageContainer; using Content.Shared.Damage.ResistanceSet; using Content.Shared.GameObjects.Components.Body; using Content.Shared.GameObjects.Components.Damage; -using Robust.Server.GameObjects; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Reflection; using Robust.Shared.Interfaces.Serialization; using Robust.Shared.IoC; using Robust.Shared.Log; -using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -34,19 +32,10 @@ namespace Content.Server.Body /// which coordinates functions between BodyParts, or a /// . /// - public class BodyPart + public class BodyPart : IBodyPart { - /// - /// The body that this body part is in, if any. - /// - private BodyManagerComponent? _body; + private IBodyManagerComponent? _body; - /// - /// Set of all currently inside this - /// . - /// To add and remove from this list see and - /// - /// private readonly HashSet _mechanisms = new HashSet(); public BodyPart(BodyPartPrototype data) @@ -64,11 +53,8 @@ namespace Content.Server.Body LoadFromPrototype(data); } - /// - /// The body that this body part is in, if any. - /// [ViewVariables] - public BodyManagerComponent? Body + public IBodyManagerComponent? Body { get => _body; set @@ -111,91 +97,48 @@ namespace Content.Server.Body [ViewVariables] private HashSet Properties { get; } - /// - /// The name of this , often displayed to the user. - /// For example, it could be named "advanced robotic arm". - /// - [ViewVariables] - public string Name { get; private set; } + [ViewVariables] public string Name { get; private set; } - /// - /// Plural version of this name. - /// - [ViewVariables] - public string Plural { get; private set; } + [ViewVariables] public string Plural { get; private set; } - /// - /// Path to the RSI that represents this . - /// - [ViewVariables] - public string RSIPath { get; private set; } + [ViewVariables] public string RSIPath { get; private set; } - /// - /// RSI state that represents this . - /// - [ViewVariables] - public string RSIState { get; private set; } + [ViewVariables] public string RSIState { get; private set; } - /// - /// RSI map keys that this body part changes on the sprite. - /// - [ViewVariables] - public Enum? RSIMap { get; set; } + [ViewVariables] public Enum? RSIMap { get; set; } - /// - /// RSI color of this body part. - /// // TODO: SpriteComponent rework - public Color? RSIColor { get; set; } + [ViewVariables] public Color? RSIColor { get; set; } - /// - /// that this is considered - /// to be. - /// For example, . - /// - [ViewVariables] - public BodyPartType PartType { get; private set; } + [ViewVariables] public BodyPartType PartType { get; private set; } - /// - /// Determines many things: how many mechanisms can be fit inside this - /// , whether a body can fit through tiny crevices, etc. - /// - [ViewVariables] - private int Size { get; set; } + [ViewVariables] public int Size { get; private set; } - /// - /// Max HP of this . - /// - [ViewVariables] - public int MaxDurability { get; private set; } + [ViewVariables] public int MaxDurability { get; private set; } - /// - /// Current HP of this based on sum of all damage types. - /// - [ViewVariables] - public int CurrentDurability => MaxDurability - Damage.TotalDamage; + [ViewVariables] public int CurrentDurability => MaxDurability - Damage.TotalDamage; // TODO: Individual body part damage /// - /// Current damage dealt to this . + /// Current damage dealt to this . /// [ViewVariables] public DamageContainer Damage { get; private set; } /// - /// Armor of this against damages. + /// Armor of this against damages. /// [ViewVariables] public ResistanceSet Resistances { get; private set; } /// - /// At what HP this destroyed. + /// At what HP this destroyed. /// [ViewVariables] public int DestroyThreshold { get; private set; } /// - /// What types of BodyParts this can easily attach to. + /// What types of BodyParts this can easily attach to. /// For the most part, most limbs aren't universal and require extra work to /// attach between types. /// @@ -204,14 +147,15 @@ namespace Content.Server.Body /// /// Set of all currently inside this - /// . + /// . /// [ViewVariables] public IReadOnlyCollection Mechanisms => _mechanisms; /// - /// This method is called by - /// before is called. + /// This method is called by + /// before + /// is called. /// public void PreMetabolism(float frameTime) { @@ -222,8 +166,9 @@ namespace Content.Server.Body } /// - /// This method is called by - /// after is called. + /// This method is called by + /// after + /// is called. /// public void PostMetabolism(float frameTime) { @@ -258,9 +203,10 @@ namespace Content.Server.Body /// The property if found, null otherwise. /// The type of the property to find. /// True if successful, false otherwise. - public bool TryGetProperty(out T property) + public bool TryGetProperty([NotNullWhen(true)] out T? property) where T : BodyPartProperty { - property = (T) Properties.First(x => x.GetType() == typeof(T)); + property = (T?) Properties.FirstOrDefault(x => x.GetType() == typeof(T)); + return property != null; } @@ -269,20 +215,21 @@ namespace Content.Server.Body /// The resulting will be null if unsuccessful. /// /// True if successful, false otherwise. - public bool TryGetProperty(Type propertyType, out BodyPartProperty property) + public bool TryGetProperty(Type propertyType, [NotNullWhen(true)] out BodyPartProperty? property) { - property = (BodyPartProperty) Properties.First(x => x.GetType() == propertyType); + property = (BodyPartProperty?) Properties.First(x => x.GetType() == propertyType); + return property != null; } /// - /// Checks if the given type is on this . + /// Checks if the given type is on this . /// /// /// The subtype of to look for. /// /// - /// True if this has a property of type + /// True if this has a property of type /// , false otherwise. /// public bool HasProperty() where T : BodyPartProperty @@ -292,13 +239,13 @@ namespace Content.Server.Body /// /// Checks if a subtype of is on this - /// . + /// . /// /// /// The subtype of to look for. /// /// - /// True if this has a property of type + /// True if this has a property of type /// , false otherwise. /// public bool HasProperty(Type propertyType) @@ -306,21 +253,11 @@ namespace Content.Server.Body return Properties.Count(x => x.GetType() == propertyType) > 0; } - /// - /// Checks if another can be connected to this one. - /// - /// The part to connect. - /// True if it can be connected, false otherwise. - public bool CanAttachBodyPart(BodyPart toBeConnected) + public bool CanAttachPart(IBodyPart part) { - return SurgeryData.CanAttachBodyPart(toBeConnected); + return SurgeryData.CanAttachBodyPart(part); } - /// - /// Checks if a can be installed on this - /// . - /// - /// True if it can be installed, false otherwise. public bool CanInstallMechanism(Mechanism mechanism) { return SizeUsed + mechanism.Size <= Size && @@ -336,7 +273,7 @@ namespace Content.Server.Body /// The mechanism to try to install. /// /// True if successful, false if there was an error - /// (e.g. not enough room in ). + /// (e.g. not enough room in ). /// private bool TryInstallMechanism(Mechanism mechanism) { @@ -352,7 +289,7 @@ namespace Content.Server.Body /// /// Tries to install a into this - /// , potentially deleting the dropped + /// , potentially deleting the dropped /// . /// /// The mechanism to install. @@ -364,21 +301,13 @@ namespace Content.Server.Body { if (!TryInstallMechanism(droppedMechanism.ContainedMechanism)) { - return false; //Installing the mechanism failed for some reason. + return false; // Installing the mechanism failed for some reason. } droppedMechanism.Owner.Delete(); return true; } - /// - /// Tries to remove the given reference from - /// this . - /// - /// - /// The newly spawned , or null - /// if there was an error in spawning the entity or removing the mechanism. - /// public bool TryDropMechanism(IEntity dropLocation, Mechanism mechanismTarget, [NotNullWhen(true)] out DroppedMechanismComponent dropped) { @@ -403,11 +332,11 @@ namespace Content.Server.Body /// /// Tries to destroy the given in this - /// . Does NOT spawn a dropped entity. + /// . Does NOT spawn a dropped entity. /// /// /// Tries to destroy the given in this - /// . + /// . /// /// The mechanism to destroy. /// True if successful, false otherwise. @@ -421,18 +350,13 @@ namespace Content.Server.Body return true; } - /// - /// Checks if the given can be used on - /// the current state of this . - /// - /// True if it can be used, false otherwise. - public bool SurgeryCheck(SurgeryType toolType) + public bool SurgeryCheck(SurgeryType surgery) { - return SurgeryData.CheckSurgery(toolType); + return SurgeryData.CheckSurgery(surgery); } /// - /// Attempts to perform surgery on this with the given + /// Attempts to perform surgery on this with the given /// tool. /// /// True if successful, false if there was an error. @@ -474,7 +398,7 @@ namespace Content.Server.Body /// /// Tries to remove the given from this - /// . + /// . /// /// The mechanism to remove. /// True if it was removed, false otherwise. @@ -515,7 +439,7 @@ namespace Content.Server.Body /// /// Loads the given . - /// Current data on this will be overwritten! + /// Current data on this will be overwritten! /// protected virtual void LoadFromPrototype(BodyPartPrototype data) { diff --git a/Content.Server/Body/BodyPreset.cs b/Content.Server/Body/BodyPreset.cs index 12e67c53e9..2afc863beb 100644 --- a/Content.Server/Body/BodyPreset.cs +++ b/Content.Server/Body/BodyPreset.cs @@ -21,7 +21,7 @@ namespace Content.Server.Body [ViewVariables] public string Name { get; private set; } /// - /// Maps a template slot to the ID of the that should + /// Maps a template slot to the ID of the that should /// fill it. E.g. "right arm" : "BodyPart.arm.basic_human". /// [ViewVariables] diff --git a/Content.Server/Body/IBodyPart.cs b/Content.Server/Body/IBodyPart.cs new file mode 100644 index 0000000000..12859fcdf7 --- /dev/null +++ b/Content.Server/Body/IBodyPart.cs @@ -0,0 +1,131 @@ +#nullable enable +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using Content.Server.Body.Mechanisms; +using Content.Server.GameObjects.Components.Body; +using Content.Shared.Body.Part.Properties; +using Content.Shared.GameObjects.Components.Body; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Maths; + +namespace Content.Server.Body +{ + public interface IBodyPart + { + /// + /// The body that this body part is currently in, if any. + /// + IBodyManagerComponent? Body { get; set; } + + /// + /// that this is considered + /// to be. + /// For example, . + /// + BodyPartType PartType { get; } + + /// + /// The name of this , often displayed to the user. + /// For example, it could be named "advanced robotic arm". + /// + public string Name { get; } + + /// + /// Plural version of this name. + /// + public string Plural { get; } + + /// + /// Determines many things: how many mechanisms can be fit inside this + /// , whether a body can fit through tiny crevices, + /// etc. + /// + int Size { get; } + + /// + /// Max HP of this . + /// + int MaxDurability { get; } + + /// + /// Current HP of this based on sum of all damage types. + /// + int CurrentDurability { get; } + + /// + /// Collection of all s currently inside this + /// . + /// To add and remove from this list see and + /// + /// + IReadOnlyCollection Mechanisms { get; } + + /// + /// Path to the RSI that represents this . + /// + public string RSIPath { get; } + + /// + /// RSI state that represents this . + /// + public string RSIState { get; } + + /// + /// RSI map keys that this body part changes on the sprite. + /// + public Enum? RSIMap { get; set; } + + /// + /// RSI color of this body part. + /// + // TODO: SpriteComponent rework + public Color? RSIColor { get; set; } + + bool HasProperty() where T : BodyPartProperty; + + bool HasProperty(Type type); + + bool TryGetProperty([NotNullWhen(true)] out T? property) where T : BodyPartProperty; + + void PreMetabolism(float frameTime); + + void PostMetabolism(float frameTime); + + bool SpawnDropped([NotNullWhen(true)] out IEntity? dropped); + + /// + /// Checks if the given can be used on + /// the current state of this . + /// + /// True if it can be used, false otherwise. + bool SurgeryCheck(SurgeryType surgery); + + /// + /// Checks if another can be connected to this one. + /// + /// The part to connect. + /// True if it can be connected, false otherwise. + bool CanAttachPart(IBodyPart part); + + /// + /// Checks if a can be installed on this + /// . + /// + /// True if it can be installed, false otherwise. + bool CanInstallMechanism(Mechanism mechanism); + + /// + /// Tries to remove the given reference from + /// this . + /// + /// + /// The newly spawned , or null + /// if there was an error in spawning the entity or removing the mechanism. + /// + bool TryDropMechanism(IEntity dropLocation, Mechanism mechanismTarget, + [NotNullWhen(true)] out DroppedMechanismComponent dropped); + + bool DestroyMechanism(Mechanism mechanism); + } +} diff --git a/Content.Server/Body/IBodyPartContainer.cs b/Content.Server/Body/IBodyPartContainer.cs index fdc4c402e9..f47d9b4657 100644 --- a/Content.Server/Body/IBodyPartContainer.cs +++ b/Content.Server/Body/IBodyPartContainer.cs @@ -7,11 +7,11 @@ namespace Content.Server.Body /// Making a class inherit from this interface allows you to do many things with /// it in the class. /// This includes passing it as an argument to a - /// delegate, as to later typecast it back - /// to the original class type. - /// Every BodyPart also needs an to be its parent - /// (i.e. the holds many , - /// each of which have an upward reference to it). + /// delegate, as to later typecast + /// it back to the original class type. + /// Every BodyPart also needs an to be + /// its parent (i.e. the holds many + /// , each of which have an upward reference to it). /// public interface IBodyPartContainer { diff --git a/Content.Server/Body/Mechanisms/Behaviors/MechanismBehavior.cs b/Content.Server/Body/Mechanisms/Behaviors/MechanismBehavior.cs index 77c3d5e982..54f1e97384 100644 --- a/Content.Server/Body/Mechanisms/Behaviors/MechanismBehavior.cs +++ b/Content.Server/Body/Mechanisms/Behaviors/MechanismBehavior.cs @@ -70,7 +70,7 @@ namespace Content.Server.Body.Mechanisms.Behaviors } /// - /// Called when the containing is attached to a + /// Called when the containing is attached to a /// . /// For instance, attaching a head to a body will call this on the brain inside. /// @@ -82,7 +82,7 @@ namespace Content.Server.Body.Mechanisms.Behaviors /// /// Called when the parent is - /// installed into a . + /// installed into a . /// For instance, putting a brain into an empty head. /// public void InstalledIntoPart() @@ -92,22 +92,22 @@ namespace Content.Server.Body.Mechanisms.Behaviors } /// - /// Called when the containing is removed from a + /// Called when the containing is removed from a /// . /// For instance, cutting off ones head will call this on the brain inside. /// - public void RemovedFromBody(BodyManagerComponent old) + public void RemovedFromBody(IBodyManagerComponent old) { OnRemovedFromBody(old); TryRemoveNetwork(old); } /// - /// Called when the parent is removed from a - /// . + /// Called when the parent is + /// removed from a . /// For instance, taking a brain out of ones head. /// - public void RemovedFromPart(BodyPart old) + public void RemovedFromPart(IBodyPart old) { OnRemovedFromPart(old); TryRemoveNetwork(old.Body); @@ -121,7 +121,7 @@ namespace Content.Server.Body.Mechanisms.Behaviors } } - private void TryRemoveNetwork(BodyManagerComponent? body) + private void TryRemoveNetwork(IBodyManagerComponent? body) { if (Network != null) { @@ -137,7 +137,7 @@ namespace Content.Server.Body.Mechanisms.Behaviors protected virtual void OnRemove() { } /// - /// Called when the containing is attached to a + /// Called when the containing is attached to a /// . /// For instance, attaching a head to a body will call this on the brain inside. /// @@ -145,24 +145,24 @@ namespace Content.Server.Body.Mechanisms.Behaviors /// /// Called when the parent is - /// installed into a . + /// installed into a . /// For instance, putting a brain into an empty head. /// protected virtual void OnInstalledIntoPart() { } /// - /// Called when the containing is removed from a + /// Called when the containing is removed from a /// . /// For instance, cutting off ones head will call this on the brain inside. /// - protected virtual void OnRemovedFromBody(BodyManagerComponent old) { } + protected virtual void OnRemovedFromBody(IBodyManagerComponent old) { } /// - /// Called when the parent is removed from a - /// . + /// Called when the parent is + /// removed from a . /// For instance, taking a brain out of ones head. /// - protected virtual void OnRemovedFromPart(BodyPart old) { } + protected virtual void OnRemovedFromPart(IBodyPart old) { } /// /// Called every update when this behavior is connected to a diff --git a/Content.Server/Body/Mechanisms/Mechanism.cs b/Content.Server/Body/Mechanisms/Mechanism.cs index 9c88b0425b..1dfaf0ee67 100644 --- a/Content.Server/Body/Mechanisms/Mechanism.cs +++ b/Content.Server/Body/Mechanisms/Mechanism.cs @@ -12,13 +12,13 @@ using Robust.Shared.ViewVariables; namespace Content.Server.Body.Mechanisms { /// - /// Data class representing a persistent item inside a . + /// Data class representing a persistent item inside a . /// This includes livers, eyes, cameras, brains, explosive implants, /// binary communicators, and other things. /// public class Mechanism { - private BodyPart? _part; + private IBodyPart? _part; public Mechanism(MechanismPrototype data) { @@ -91,13 +91,13 @@ namespace Content.Server.Body.Mechanisms /// /// Determines a handful of things - mostly whether this - /// can fit into a . + /// can fit into a . /// [ViewVariables] public int Size { get; set; } /// - /// What kind of this can be + /// What kind of this can be /// easily installed into. /// [ViewVariables] @@ -109,9 +109,9 @@ namespace Content.Server.Body.Mechanisms [ViewVariables] private List Behaviors { get; } - public BodyManagerComponent? Body => Part?.Body; + public IBodyManagerComponent? Body => Part?.Body; - public BodyPart? Part + public IBodyPart? Part { get => _part; set @@ -202,7 +202,7 @@ namespace Content.Server.Body.Mechanisms } } - public void RemovedFromBody(BodyManagerComponent old) + public void RemovedFromBody(IBodyManagerComponent old) { foreach (var behavior in Behaviors) { @@ -211,7 +211,7 @@ namespace Content.Server.Body.Mechanisms } /// - /// This method is called by before + /// This method is called by before /// is called. /// public void PreMetabolism(float frameTime) @@ -223,7 +223,7 @@ namespace Content.Server.Body.Mechanisms } /// - /// This method is called by after + /// This method is called by after /// is called. /// public void PostMetabolism(float frameTime) diff --git a/Content.Server/Body/Surgery/BiologicalSurgeryData.cs b/Content.Server/Body/Surgery/BiologicalSurgeryData.cs index 37fbb57b29..f1bff3a145 100644 --- a/Content.Server/Body/Surgery/BiologicalSurgeryData.cs +++ b/Content.Server/Body/Surgery/BiologicalSurgeryData.cs @@ -23,7 +23,7 @@ namespace Content.Server.Body.Surgery private bool _skinRetracted; private bool _vesselsClamped; - public BiologicalSurgeryData(BodyPart parent) : base(parent) { } + public BiologicalSurgeryData(IBodyPart parent) : base(parent) { } protected override SurgeryAction? GetSurgeryStep(SurgeryType toolType) { @@ -123,7 +123,7 @@ namespace Content.Server.Body.Surgery return _skinOpened && _vesselsClamped && _skinRetracted; } - public override bool CanAttachBodyPart(BodyPart part) + public override bool CanAttachBodyPart(IBodyPart part) { return true; // TODO: if a bodypart is disconnected, you should have to do some surgery to allow another bodypart to be attached. @@ -216,8 +216,7 @@ namespace Content.Server.Body.Surgery } } - private void RemoveOrganSurgeryCallback(Mechanism target, IBodyPartContainer container, - ISurgeon surgeon, + private void RemoveOrganSurgeryCallback(Mechanism target, IBodyPartContainer container, ISurgeon surgeon, IEntity performer) { if (target == null || !Parent.Mechanisms.Contains(target)) diff --git a/Content.Server/Body/Surgery/SurgeryData.cs b/Content.Server/Body/Surgery/SurgeryData.cs index a4274d0042..48b7d265da 100644 --- a/Content.Server/Body/Surgery/SurgeryData.cs +++ b/Content.Server/Body/Surgery/SurgeryData.cs @@ -6,7 +6,7 @@ using Robust.Shared.Interfaces.GameObjects; namespace Content.Server.Body.Surgery { /// - /// This data class represents the state of a in regards to everything surgery related - + /// This data class represents the state of a in regards to everything surgery related - /// whether there's an incision on it, whether the bone is broken, etc. /// public abstract class SurgeryData @@ -14,40 +14,40 @@ namespace Content.Server.Body.Surgery protected delegate void SurgeryAction(IBodyPartContainer container, ISurgeon surgeon, IEntity performer); /// - /// The this surgeryData is attached to. + /// The this surgeryData is attached to. /// The class should not exist without a - /// that it represents, and will throw errors if it + /// that it represents, and will throw errors if it /// is null. /// - protected readonly BodyPart Parent; + protected readonly IBodyPart Parent; - protected SurgeryData(BodyPart parent) + protected SurgeryData(IBodyPart parent) { Parent = parent; } /// - /// The of the parent . + /// The of the parent . /// protected BodyPartType ParentType => Parent.PartType; /// - /// Returns the description of this current to be shown + /// Returns the description of this current to be shown /// upon observing the given entity. /// public abstract string GetDescription(IEntity target); /// /// Returns whether a can be installed into the - /// this represents. + /// this represents. /// public abstract bool CanInstallMechanism(Mechanism mechanism); /// - /// Returns whether the given can be connected to the - /// this represents. + /// Returns whether the given can be connected to the + /// this represents. /// - public abstract bool CanAttachBodyPart(BodyPart part); + public abstract bool CanAttachBodyPart(IBodyPart part); /// /// Gets the delegate corresponding to the surgery step using the given diff --git a/Content.Server/GameObjects/Components/Body/BodyManagerComponent.cs b/Content.Server/GameObjects/Components/Body/BodyManagerComponent.cs index e5154a9d93..c7e658e2b9 100644 --- a/Content.Server/GameObjects/Components/Body/BodyManagerComponent.cs +++ b/Content.Server/GameObjects/Components/Body/BodyManagerComponent.cs @@ -8,7 +8,6 @@ using Content.Server.Body.Network; using Content.Server.GameObjects.Components.Metabolism; using Content.Server.GameObjects.EntitySystems; using Content.Server.Interfaces.GameObjects.Components.Interaction; -using Content.Server.Mobs; using Content.Server.Observer; using Content.Shared.Body.Part; using Content.Shared.Body.Part.Properties.Movement; @@ -34,13 +33,14 @@ using Robust.Shared.ViewVariables; namespace Content.Server.GameObjects.Components.Body { /// - /// Component representing a collection of + /// Component representing a collection of /// attached to each other. /// [RegisterComponent] [ComponentReference(typeof(IDamageableComponent))] [ComponentReference(typeof(ISharedBodyManagerComponent))] - public class BodyManagerComponent : SharedBodyManagerComponent, IBodyPartContainer, IRelayMoveInput + [ComponentReference(typeof(IBodyManagerComponent))] + public class BodyManagerComponent : SharedBodyManagerComponent, IBodyPartContainer, IRelayMoveInput, IBodyManagerComponent { [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IBodyNetworkFactory _bodyNetworkFactory = default!; @@ -48,38 +48,28 @@ namespace Content.Server.GameObjects.Components.Body [ViewVariables] private string _presetName = default!; - private readonly Dictionary _parts = new Dictionary(); + private readonly Dictionary _parts = new Dictionary(); [ViewVariables] private readonly Dictionary _networks = new Dictionary(); /// - /// All with + /// All with /// that are currently affecting move speed, mapped to how big that leg /// they're on is. /// [ViewVariables] - private readonly Dictionary _activeLegs = new Dictionary(); + private readonly Dictionary _activeLegs = new Dictionary(); + + [ViewVariables] public BodyTemplate Template { get; private set; } = default!; + + [ViewVariables] public BodyPreset Preset { get; private set; } = default!; /// - /// The that this - /// is adhering to. - /// - [ViewVariables] - public BodyTemplate Template { get; private set; } = default!; - - /// - /// The that this - /// is adhering to. - /// - [ViewVariables] - public BodyPreset Preset { get; private set; } = default!; - - /// - /// Maps slot name to the + /// Maps slot name to the /// object filling it (if there is one). /// [ViewVariables] - public IReadOnlyDictionary Parts => _parts; + public IReadOnlyDictionary Parts => _parts; /// /// List of all slots in this body, taken from the keys of @@ -171,7 +161,7 @@ namespace Content.Server.GameObjects.Components.Body // Add a new BodyPart with the BodyPartPrototype as a baseline to our // BodyComponent. var addedPart = new BodyPart(newPartData); - AddBodyPart(addedPart, slotName); + TryAddPart(slotName, addedPart); } OnBodyChanged(); // TODO: Duplicate code @@ -180,8 +170,8 @@ namespace Content.Server.GameObjects.Components.Body /// /// Changes the current to the given /// . - /// Attempts to keep previous if there is a slot for - /// them in both . + /// Attempts to keep previous if there is a + /// slot for them in both . /// public void ChangeBodyTemplate(BodyTemplatePrototype newTemplate) { @@ -280,10 +270,10 @@ namespace Content.Server.GameObjects.Components.Body foreach (var (key, value) in _activeLegs) { - if (key.TryGetProperty(out LegProperty legProperty)) + if (key.TryGetProperty(out LegProperty? leg)) { // Speed of a leg = base speed * (1+log1024(leg length)) - speedSum += legProperty.Speed * (1 + (float) Math.Log(value, 1024.0)); + speedSum += leg.Speed * (1 + (float) Math.Log(value, 1024.0)); } } @@ -321,11 +311,11 @@ namespace Content.Server.GameObjects.Components.Body /// /// Recursively searches for if is connected to /// the center. Not efficient (O(n^2)), but most bodies don't have a ton - /// of s. + /// of s. /// /// The body part to find the center for. /// True if it is connected to the center, false otherwise. - private bool ConnectedToCenterPart(BodyPart target) + private bool ConnectedToCenterPart(IBodyPart target) { var searchedSlots = new List(); @@ -335,9 +325,7 @@ namespace Content.Server.GameObjects.Components.Body private bool ConnectedToCenterPartRecursion(ICollection searchedSlots, string slotName) { - TryGetBodyPart(slotName, out var part); - - if (part == null) + if (!TryGetBodyPart(slotName, out var part)) { return false; } @@ -367,14 +355,14 @@ namespace Content.Server.GameObjects.Components.Body } /// - /// Finds the central , if any, of this body based on + /// Finds the central , if any, of this body based on /// the . For humans, this is the torso. /// /// The if one exists, null otherwise. - private BodyPart? GetCenterBodyPart() + private IBodyPart? GetCenterBodyPart() { Parts.TryGetValue(Template.CenterSlot, out var center); - return center!; + return center; } /// @@ -387,29 +375,31 @@ namespace Content.Server.GameObjects.Components.Body } /// - /// Finds the in the given if + /// Finds the in the given if /// one exists. /// /// The slot to search in. /// The body part in that slot, if any. /// True if found, false otherwise. - private bool TryGetBodyPart(string slotName, [NotNullWhen(true)] out BodyPart result) + private bool TryGetBodyPart(string slotName, [NotNullWhen(true)] out IBodyPart? result) { return Parts.TryGetValue(slotName, out result!); } /// - /// Finds the slotName that the given resides in. + /// Finds the slotName that the given resides in. /// - /// The to find the slot for. + /// The to find the slot for. /// The slot found, if any. /// True if a slot was found, false otherwise - private bool TryGetSlotName(BodyPart part, [NotNullWhen(true)] out string result) + private bool TryGetSlotName(IBodyPart part, [NotNullWhen(true)] out string result) { // We enforce that there is only one of each value in the dictionary, // so we can iterate through the dictionary values to get the key from there. - result = Parts.FirstOrDefault(x => x.Value == part).Key; - return result != null; + var pair = Parts.FirstOrDefault(x => x.Value == part); + result = pair.Key; + + return !pair.Equals(default); } /// @@ -448,7 +438,7 @@ namespace Content.Server.GameObjects.Components.Body /// True if successful, false if there was an error or no connected /// s were found. /// - public bool TryGetBodyPartConnections(string slotName, [NotNullWhen(true)] out List result) + public bool TryGetBodyPartConnections(string slotName, [NotNullWhen(true)] out List result) { result = null!; @@ -457,7 +447,7 @@ namespace Content.Server.GameObjects.Components.Body return false; } - var toReturn = new List(); + var toReturn = new List(); foreach (var connection in connections) { if (TryGetBodyPart(connection, out var bodyPartResult)) @@ -481,9 +471,9 @@ namespace Content.Server.GameObjects.Components.Body /// /// /// True if successful, false if there was an error or no connected - /// s were found. + /// s were found. /// - private bool TryGetBodyPartConnections(BodyPart part, [NotNullWhen(true)] out List result) + private bool TryGetBodyPartConnections(IBodyPart part, [NotNullWhen(true)] out List result) { result = null!; @@ -492,11 +482,11 @@ namespace Content.Server.GameObjects.Components.Body } /// - /// Grabs all of the given type in this body. + /// Grabs all of the given type in this body. /// - public List GetBodyPartsOfType(BodyPartType type) + public List GetBodyPartsOfType(BodyPartType type) { - var toReturn = new List(); + var toReturn = new List(); foreach (var part in Parts.Values) { @@ -509,32 +499,6 @@ namespace Content.Server.GameObjects.Components.Body return toReturn; } - /// - /// Installs the given into the given slot. - /// - /// True if successful, false otherwise. - public bool InstallBodyPart(BodyPart part, string slotName) - { - DebugTools.AssertNotNull(part); - - // Make sure the given slot exists - if (!SlotExists(slotName)) - { - return false; - } - - // And that nothing is in it - if (TryGetBodyPart(slotName, out _)) - { - return false; - } - - AddBodyPart(part, slotName); // TODO: Sort this duplicate out - OnBodyChanged(); - - return true; - } - /// /// Installs the given into the /// given slot, deleting the afterwards. @@ -544,7 +508,7 @@ namespace Content.Server.GameObjects.Components.Body { DebugTools.AssertNotNull(part); - if (!InstallBodyPart(part.ContainedBodyPart, slotName)) + if (!TryAddPart(slotName, part.ContainedBodyPart)) { return false; } @@ -554,15 +518,15 @@ namespace Content.Server.GameObjects.Components.Body } /// - /// Disconnects the given reference, potentially - /// dropping other BodyParts if they were hanging + /// Disconnects the given reference, potentially + /// dropping other BodyParts if they were hanging /// off of it. /// /// /// The representing the dropped - /// , or null if none was dropped. + /// , or null if none was dropped. /// - public IEntity? DropPart(BodyPart part) + public IEntity? DropPart(IBodyPart part) { DebugTools.AssertNotNull(part); @@ -596,11 +560,11 @@ namespace Content.Server.GameObjects.Components.Body } /// - /// Disconnects the given reference, potentially - /// dropping other BodyParts if they were hanging + /// Disconnects the given reference, potentially + /// dropping other BodyParts if they were hanging /// off of it. /// - public void DisconnectBodyPart(BodyPart part, bool dropEntity) + public void DisconnectBodyPart(IBodyPart part, bool dropEntity) { DebugTools.AssertNotNull(part); @@ -640,12 +604,7 @@ namespace Content.Server.GameObjects.Components.Body { DebugTools.AssertNotNull(slotName); - if (!TryGetBodyPart(slotName, out var part)) - { - return; - } - - if (part == null) + if (!HasPart(slotName)) { return; } @@ -666,27 +625,47 @@ namespace Content.Server.GameObjects.Components.Body OnBodyChanged(); } - private void AddBodyPart(BodyPart part, string slotName) + public bool TryAddPart(string slot, IBodyPart part, bool force = false) { DebugTools.AssertNotNull(part); - DebugTools.AssertNotNull(slotName); + DebugTools.AssertNotNull(slot); - _parts.Add(slotName, part); + // Make sure the given slot exists + if (!force) + { + if (!SlotExists(slot)) + { + return false; + } + + // And that nothing is in it + if (!_parts.TryAdd(slot, part)) + { + return false; + } + } + else + { + _parts[slot] = part; + } part.Body = this; - var argsAdded = new BodyPartAddedEventArgs(part, slotName); + var argsAdded = new BodyPartAddedEventArgs(part, slot); foreach (var component in Owner.GetAllComponents().ToArray()) { component.BodyPartAdded(argsAdded); } - if (!Template.Layers.TryGetValue(slotName, out var partMap) || + // TODO: Sort this duplicate out + OnBodyChanged(); + + if (!Template.Layers.TryGetValue(slot, out var partMap) || !_reflectionManager.TryParseEnumReference(partMap, out var partEnum)) { Logger.Warning($"Template {Template.Name} has an invalid RSI map key {partMap} for body part {part.Name}."); - return; + return false; } part.RSIMap = partEnum; @@ -712,6 +691,13 @@ namespace Content.Server.GameObjects.Components.Body SendNetworkMessage(mechanismMessage); } + + return true; + } + + public bool HasPart(string slot) + { + return _parts.ContainsKey(slot); } /// @@ -720,7 +706,7 @@ namespace Content.Server.GameObjects.Components.Body /// /// The slot to remove it from. /// - /// Whether or not to drop the removed . + /// Whether or not to drop the removed . /// /// private bool RemoveBodyPart(string slotName, bool drop) @@ -780,17 +766,20 @@ namespace Content.Server.GameObjects.Components.Body /// The part to remove from this body. /// The slot that the part was in, if any. /// True if was removed, false otherwise. - private bool RemoveBodyPart(BodyPart part, [NotNullWhen(true)] out string slotName) + private bool RemoveBodyPart(IBodyPart part, [NotNullWhen(true)] out string? slotName) { DebugTools.AssertNotNull(part); - slotName = _parts.FirstOrDefault(pair => pair.Value == part).Key; + var pair = _parts.FirstOrDefault(kvPair => kvPair.Value == part); - if (slotName == null) + if (pair.Equals(default)) { + slotName = null; return false; } + slotName = pair.Key; + return RemoveBodyPart(slotName, false); } @@ -804,13 +793,13 @@ namespace Content.Server.GameObjects.Components.Body if (_networks.ContainsKey(network.GetType())) { - return false; + return true; } _networks.Add(network.GetType(), network); network.OnAdd(Owner); - return true; + return false; } /// @@ -844,62 +833,21 @@ namespace Content.Server.GameObjects.Components.Body return EnsureNetwork(typeof(T)); } - /// - /// Attempts to add a of the given name to - /// this body. - /// - /// - /// True if successful, false if there was an error - /// (such as passing in an invalid type or a network of that type already - /// existing). - /// - private bool EnsureNetwork(string networkName) + public void RemoveNetwork(Type networkType) { - DebugTools.AssertNotNull(networkName); + DebugTools.AssertNotNull(networkType); - var network = _bodyNetworkFactory.GetNetwork(networkName); - return EnsureNetwork(network); - } - - /// - /// Removes the of the given type in this body, - /// if there is one. - /// - /// The type of the network to remove. - public void RemoveNetwork(Type type) - { - DebugTools.AssertNotNull(type); - - if (_networks.Remove(type, out var network)) + if (_networks.Remove(networkType, out var network)) { network.OnRemove(); } } - /// - /// Removes the of the given type in this body, - /// if one exists. - /// - /// The type of the network to remove. public void RemoveNetwork() where T : BodyNetwork { RemoveNetwork(typeof(T)); } - /// - /// Removes the with the given name in this body, - /// if there is one. - /// - private void RemoveNetwork(string networkName) - { - var type = _bodyNetworkFactory.GetNetwork(networkName).GetType(); - - if (_networks.Remove(type, out var network)) - { - network.OnRemove(); - } - } - /// /// Attempts to get the of the given type in this body. /// @@ -924,7 +872,7 @@ namespace Content.Server.GameObjects.Components.Body /// a foot node from the given node. It can /// only search through BodyParts with . /// - private static float DistanceToNearestFoot(BodyManagerComponent body, BodyPart source) + private static float DistanceToNearestFoot(BodyManagerComponent body, IBodyPart source) { if (source.HasProperty() && source.TryGetProperty(out var property)) { @@ -934,7 +882,8 @@ namespace Content.Server.GameObjects.Components.Body return LookForFootRecursion(body, source, new List()); } - private static float LookForFootRecursion(BodyManagerComponent body, BodyPart current, + // TODO: Make this not static and not keep me up at night + private static float LookForFootRecursion(BodyManagerComponent body, IBodyPart current, ICollection searchedParts) { if (!current.TryGetProperty(out var extProperty)) diff --git a/Content.Server/GameObjects/Components/Body/BodyScannerComponent.cs b/Content.Server/GameObjects/Components/Body/BodyScannerComponent.cs index dfc1cb5198..343d4cff5e 100644 --- a/Content.Server/GameObjects/Components/Body/BodyScannerComponent.cs +++ b/Content.Server/GameObjects/Components/Body/BodyScannerComponent.cs @@ -56,7 +56,7 @@ namespace Content.Server.GameObjects.Components.Body /// /// Copy BodyTemplate and BodyPart data into a common data class that the client can read. /// - private BodyScannerInterfaceState InterfaceState(BodyTemplate template, IReadOnlyDictionary bodyParts) + private BodyScannerInterfaceState InterfaceState(BodyTemplate template, IReadOnlyDictionary bodyParts) { var partsData = new Dictionary(); diff --git a/Content.Server/GameObjects/Components/Body/DroppedBodyPartComponent.cs b/Content.Server/GameObjects/Components/Body/DroppedBodyPartComponent.cs index f7fb29c6e7..d917459671 100644 --- a/Content.Server/GameObjects/Components/Body/DroppedBodyPartComponent.cs +++ b/Content.Server/GameObjects/Components/Body/DroppedBodyPartComponent.cs @@ -101,7 +101,7 @@ namespace Content.Server.GameObjects.Components.Body foreach (var connectedPart in parts) { - if (!connectedPart.CanAttachBodyPart(ContainedBodyPart)) + if (!connectedPart.CanAttachPart(ContainedBodyPart)) { continue; } diff --git a/Content.Server/GameObjects/Components/Body/IBodyManagerComponent.cs b/Content.Server/GameObjects/Components/Body/IBodyManagerComponent.cs new file mode 100644 index 0000000000..26414eefa4 --- /dev/null +++ b/Content.Server/GameObjects/Components/Body/IBodyManagerComponent.cs @@ -0,0 +1,67 @@ +using System; +using Content.Server.Body; +using Content.Server.Body.Network; +using Content.Shared.GameObjects.Components.Body; + +namespace Content.Server.GameObjects.Components.Body +{ + // TODO: Merge with ISharedBodyManagerComponent + public interface IBodyManagerComponent : ISharedBodyManagerComponent + { + /// + /// The that this + /// is adhering to. + /// + public BodyTemplate Template { get; } + + /// + /// The that this + /// is adhering to. + /// + public BodyPreset Preset { get; } + + /// + /// Installs the given into the given slot. + /// + /// True if successful, false otherwise. + bool TryAddPart(string slot, IBodyPart part, bool force = false); + + bool HasPart(string slot); + + /// + /// Ensures that this body has the specified network. + /// + /// The type of the network to ensure. + /// + /// True if the network already existed, false if it had to be created. + /// + bool EnsureNetwork() where T : BodyNetwork; + + /// + /// Ensures that this body has the specified network. + /// + /// The type of the network to ensure. + /// + /// True if the network already existed, false if it had to be created. + /// + bool EnsureNetwork(Type networkType); + + /// + /// Removes the of the given type in this body, + /// if one exists. + /// + /// The type of the network to remove. + void RemoveNetwork() where T : BodyNetwork; + + /// + /// Removes the of the given type in this body, + /// if there is one. + /// + /// The type of the network to remove. + void RemoveNetwork(Type networkType); + + void PreMetabolism(float frameTime); + + void PostMetabolism(float frameTime); + } +} diff --git a/Content.Server/GameObjects/Components/Medical/HealingComponent.cs b/Content.Server/GameObjects/Components/Medical/HealingComponent.cs index 0b751e24c4..8768acc071 100644 --- a/Content.Server/GameObjects/Components/Medical/HealingComponent.cs +++ b/Content.Server/GameObjects/Components/Medical/HealingComponent.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Content.Server.GameObjects.Components.Body; using Content.Server.GameObjects.Components.Stack; using Content.Shared.Damage; using Content.Shared.GameObjects.Components.Body; diff --git a/Content.Server/Interfaces/GameObjects/Components/Interaction/IBodyPartAdded.cs b/Content.Server/Interfaces/GameObjects/Components/Interaction/IBodyPartAdded.cs index f261d6edc5..d18fe596ee 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Interaction/IBodyPartAdded.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Interaction/IBodyPartAdded.cs @@ -14,13 +14,13 @@ namespace Content.Server.Interfaces.GameObjects.Components.Interaction public class BodyPartAddedEventArgs : EventArgs { - public BodyPartAddedEventArgs(BodyPart part, string slotName) + public BodyPartAddedEventArgs(IBodyPart part, string slotName) { Part = part; SlotName = slotName; } - public BodyPart Part { get; } + public IBodyPart Part { get; } public string SlotName { get; } } @@ -36,13 +36,13 @@ namespace Content.Server.Interfaces.GameObjects.Components.Interaction public class BodyPartRemovedEventArgs : EventArgs { - public BodyPartRemovedEventArgs(BodyPart part, string slotName) + public BodyPartRemovedEventArgs(IBodyPart part, string slotName) { Part = part; SlotName = slotName; } - public BodyPart Part { get; } + public IBodyPart Part { get; } public string SlotName { get; } }