From 45b72d4852944947bc99e7257bab042dad1a92a1 Mon Sep 17 00:00:00 2001 From: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:07:09 -0500 Subject: [PATCH] nullable parts in body prototypes (#12935) Co-authored-by: DrSmugleaf --- Content.Server/Body/Systems/BodySystem.cs | 16 ++++++------- .../Body/Prototypes/BodyPrototype.cs | 8 ++++--- .../Prototypes/BodyPrototypeSerializer.cs | 23 ++++++++----------- .../Body/Systems/SharedBodySystem.Body.cs | 3 +++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Content.Server/Body/Systems/BodySystem.cs b/Content.Server/Body/Systems/BodySystem.cs index 6432d603b1..f1a1e8cd6a 100644 --- a/Content.Server/Body/Systems/BodySystem.cs +++ b/Content.Server/Body/Systems/BodySystem.cs @@ -5,13 +5,13 @@ using Content.Server.GameTicking; using Content.Server.Humanoid; using Content.Server.Kitchen.Components; using Content.Server.Mind.Components; +using Content.Server.MobState; using Content.Shared.Body.Components; using Content.Shared.Body.Part; using Content.Shared.Body.Prototypes; using Content.Shared.Body.Systems; using Content.Shared.Coordinates; using Content.Shared.Humanoid; -using Content.Shared.MobState.Components; using Content.Shared.Movement.Events; using Content.Shared.Random.Helpers; using Robust.Shared.Audio; @@ -25,8 +25,8 @@ public sealed class BodySystem : SharedBodySystem { [Dependency] private readonly GameTicker _ticker = default!; [Dependency] private readonly IGameTiming _gameTiming = default!; - [Dependency] private readonly HumanoidSystem _humanoidSystem = default!; + [Dependency] private readonly MobStateSystem _mobState = default!; [Dependency] private readonly SharedAudioSystem _audio = default!; public override void Initialize() @@ -40,8 +40,7 @@ public sealed class BodySystem : SharedBodySystem private void OnRelayMoveInput(EntityUid uid, BodyComponent component, ref MoveInputEvent args) { - if (EntityManager.TryGetComponent(uid, out var mobState) && - mobState.IsDead() && + if (_mobState.IsDead(uid) && EntityManager.TryGetComponent(uid, out var mind) && mind.HasMind) { @@ -119,14 +118,15 @@ public sealed class BodySystem : SharedBodySystem protected override void InitBody(BodyComponent body, BodyPrototype prototype) { var root = prototype.Slots[prototype.Root]; + Containers.EnsureContainer(body.Owner, BodyContainerId); + if (root.Part == null) + return; var bodyId = Spawn(root.Part, body.Owner.ToCoordinates()); var partComponent = Comp(bodyId); var slot = new BodyPartSlot(root.Part, body.Owner, partComponent.PartType); body.Root = slot; partComponent.Body = bodyId; - Containers.EnsureContainer(body.Owner, BodyContainerId); - AttachPart(bodyId, slot, partComponent); InitPart(partComponent, prototype, prototype.Root); } @@ -136,7 +136,7 @@ public sealed class BodySystem : SharedBodySystem if (bodyId == null || !Resolve(bodyId.Value, ref body, false)) return new HashSet(); - var gibs = base.GibBody(bodyId, gibOrgans, body); + var gibs = base.GibBody(bodyId, gibOrgans, body, deleteItems); var xform = Transform(bodyId.Value); var coordinates = xform.Coordinates; @@ -157,7 +157,7 @@ public sealed class BodySystem : SharedBodySystem } else { - cont.ForceRemove(ent); + cont.Remove(ent, EntityManager, force: true); Transform(ent).Coordinates = coordinates; ent.RandomOffset(0.25f); } diff --git a/Content.Shared/Body/Prototypes/BodyPrototype.cs b/Content.Shared/Body/Prototypes/BodyPrototype.cs index 3cffe46e6e..f3335f8fac 100644 --- a/Content.Shared/Body/Prototypes/BodyPrototype.cs +++ b/Content.Shared/Body/Prototypes/BodyPrototype.cs @@ -1,4 +1,5 @@ using Robust.Shared.Prototypes; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; namespace Content.Shared.Body.Prototypes; @@ -34,18 +35,19 @@ public sealed class BodyPrototype : IPrototype [DataRecord] public sealed record BodyPrototypeSlot { - [DataField("part", required: true)] public readonly string Part = default!; + [DataField("part", customTypeSerializer: typeof(PrototypeIdSerializer))] + public readonly string? Part; public readonly HashSet Connections = new(); public readonly Dictionary Organs = new(); - public BodyPrototypeSlot(string part, HashSet? connections, Dictionary? organs) + public BodyPrototypeSlot(string? part, HashSet? connections, Dictionary? organs) { Part = part; Connections = connections ?? new HashSet(); Organs = organs ?? new Dictionary(); } - public void Deconstruct(out string part, out HashSet connections, out Dictionary organs) + public void Deconstruct(out string? part, out HashSet connections, out Dictionary organs) { part = Part; connections = Connections; diff --git a/Content.Shared/Body/Prototypes/BodyPrototypeSerializer.cs b/Content.Shared/Body/Prototypes/BodyPrototypeSerializer.cs index 4bf7ffa714..e850b08db3 100644 --- a/Content.Shared/Body/Prototypes/BodyPrototypeSerializer.cs +++ b/Content.Shared/Body/Prototypes/BodyPrototypeSerializer.cs @@ -14,18 +14,10 @@ namespace Content.Shared.Body.Prototypes; [TypeSerializer] public sealed class BodyPrototypeSerializer : ITypeReader { - private (ValidationNode Node, List Connections) ValidateSlot(ISerializationManager serializationManager, MappingDataNode slot, string slotId, IDependencyCollection dependencies) + private (ValidationNode Node, List Connections) ValidateSlot(MappingDataNode slot, IDependencyCollection dependencies) { var nodes = new List(); var prototypes = dependencies.Resolve(); - if (!slot.TryGet("part", out ValueDataNode? part)) - { - nodes.Add(new ErrorNode(slot, $"No part value data node found in root slot {slotId}")); - } - else if (!prototypes.HasIndex(part.Value)) - { - nodes.Add(new ErrorNode(slot, $"No entity prototype found with id {part.Value} for root slot {slotId}")); - } var connections = new List(); if (slot.TryGet("connections", out SequenceDataNode? connectionsNode)) @@ -79,8 +71,6 @@ public sealed class BodyPrototypeSerializer : ITypeReader(); - if (!node.TryGet("id", out ValueDataNode? id)) - nodes.Add(new ErrorNode(node, "No id value data node found")); if (!node.TryGet("root", out ValueDataNode? root)) nodes.Add(new ErrorNode(node, $"No root value data node found")); @@ -111,7 +101,7 @@ public sealed class BodyPrototypeSerializer : ITypeReader("name").Value; var root = node.Get("root").Value; var slotNodes = node.Get("slots"); - var allConnections = new Dictionary? Connections, Dictionary? Organs)>(); + var allConnections = new Dictionary? Connections, Dictionary? Organs)>(); foreach (var (keyNode, valueNode) in slotNodes) { var slotId = ((ValueDataNode) keyNode).Value; var slot = ((MappingDataNode) valueNode); - var part = slot.Get("part").Value; + + string? part = null; + if (slot.TryGet("part", out var value)) + { + part = value.Value; + } HashSet? connections = null; if (slot.TryGet("connections", out SequenceDataNode? slotConnectionsNode)) diff --git a/Content.Shared/Body/Systems/SharedBodySystem.Body.cs b/Content.Shared/Body/Systems/SharedBodySystem.Body.cs index e9b195ee1e..237e63fe24 100644 --- a/Content.Shared/Body/Systems/SharedBodySystem.Body.cs +++ b/Content.Shared/Body/Systems/SharedBodySystem.Body.cs @@ -88,6 +88,9 @@ public partial class SharedBodySystem foreach (var connection in connections) { var childSlot = prototype.Slots[connection]; + if (childSlot.Part == null) + continue; + var childPart = Spawn(childSlot.Part, coordinates); var childPartComponent = Comp(childPart); var slot = CreatePartSlot(connection, parent.Owner, childPartComponent.PartType, parent);