]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Revert "Shuffle body container subs slightly (#21084)" (#22339)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Mon, 11 Dec 2023 12:06:56 +0000 (23:06 +1100)
committerGitHub <noreply@github.com>
Mon, 11 Dec 2023 12:06:56 +0000 (05:06 -0700)
This reverts commit eb49ad11ba9f263dde00e98ec842081a316fcaf7.

Content.Shared/Body/Systems/SharedBodySystem.Body.cs
Content.Shared/Body/Systems/SharedBodySystem.Organs.cs
Content.Shared/Body/Systems/SharedBodySystem.Parts.cs
Content.Shared/Body/Systems/SharedBodySystem.cs

index d61ba1b7eba7ea6587c5fc8bf2cfcee60c7317ba..74a202386ec45abaf771ec401c5caf662eefdfe6 100644 (file)
@@ -25,11 +25,63 @@ public partial class SharedBodySystem
 
     private void InitializeBody()
     {
+        // Body here to handle root body parts.
+        SubscribeLocalEvent<BodyComponent, EntInsertedIntoContainerMessage>(OnBodyInserted);
+        SubscribeLocalEvent<BodyComponent, EntRemovedFromContainerMessage>(OnBodyRemoved);
+
         SubscribeLocalEvent<BodyComponent, ComponentInit>(OnBodyInit);
         SubscribeLocalEvent<BodyComponent, MapInitEvent>(OnBodyMapInit);
         SubscribeLocalEvent<BodyComponent, CanDragEvent>(OnBodyCanDrag);
     }
 
+    private void OnBodyInserted(EntityUid uid, BodyComponent component, EntInsertedIntoContainerMessage args)
+    {
+        // Root body part?
+        var slotId = args.Container.ID;
+
+        if (slotId != BodyRootContainerId)
+            return;
+
+        var entity = args.Entity;
+
+        if (TryComp(entity, out BodyPartComponent? childPart))
+        {
+            AddPart(uid, entity, slotId, childPart);
+            RecursiveBodyUpdate(entity, uid, childPart);
+        }
+
+        if (TryComp(entity, out OrganComponent? organ))
+        {
+            AddOrgan(entity, uid, uid, organ);
+        }
+    }
+
+    private void OnBodyRemoved(EntityUid uid, BodyComponent component, EntRemovedFromContainerMessage args)
+    {
+        // TODO: lifestage shenanigans
+        if (TerminatingOrDeleted(uid))
+            return;
+
+        // Root body part?
+        var slotId = args.Container.ID;
+
+        if (slotId != BodyRootContainerId)
+            return;
+
+        var entity = args.Entity;
+
+        if (TryComp(entity, out BodyPartComponent? childPart))
+        {
+            RemovePart(uid, entity, slotId, childPart);
+            RecursiveBodyUpdate(entity, null, childPart);
+        }
+
+        if (TryComp(entity, out OrganComponent? organ))
+        {
+            RemoveOrgan(entity, uid, organ);
+        }
+    }
+
     private void OnBodyInit(EntityUid bodyId, BodyComponent body, ComponentInit args)
     {
         // Setup the initial container.
@@ -177,8 +229,6 @@ public partial class SharedBodySystem
             yield break;
         }
 
-        yield return (body.RootContainer.ContainedEntity.Value, rootPart);
-
         foreach (var child in GetBodyPartChildren(body.RootContainer.ContainedEntity.Value, rootPart))
         {
             yield return child;
index ee4485f32a417f8beb37b2a7eb673631d642d9b6..6e392b9892c25d7b3899735a2640249c3c7f019e 100644 (file)
@@ -9,50 +9,26 @@ namespace Content.Shared.Body.Systems;
 
 public partial class SharedBodySystem
 {
-    private void InitializeOrgans()
+    private void AddOrgan(EntityUid uid, EntityUid bodyUid, EntityUid parentPartUid, OrganComponent component)
     {
-        SubscribeLocalEvent<OrganComponent, EntGotInsertedIntoContainerMessage>(OnOrganInserted);
-        SubscribeLocalEvent<OrganComponent, EntGotRemovedFromContainerMessage>(OnOrganRemoved);
-    }
-
-    private void OnOrganInserted(EntityUid uid, OrganComponent component, EntGotInsertedIntoContainerMessage args)
-    {
-        // No recursive updates for these as you can't insert organs into organsTM.
-        var parentUid = args.Container.Owner;
+        component.Body = bodyUid;
+        RaiseLocalEvent(uid, new AddedToPartEvent(parentPartUid));
 
-        if (HasComp<BodyComponent>(parentUid))
-        {
-            component.Body = parentUid;
-            Dirty(uid, component);
-        }
+        if (component.Body != null)
+            RaiseLocalEvent(uid, new AddedToPartInBodyEvent(component.Body.Value, parentPartUid));
 
-        // Organ inserted into body part.
-        if (TryComp(parentUid, out BodyPartComponent? partComp))
-        {
-            RaiseLocalEvent(uid, new AddedToPartEvent(parentUid));
-
-            if (partComp.Body != null)
-            {
-                component.Body = partComp.Body;
-                RaiseLocalEvent(uid, new AddedToPartInBodyEvent(partComp.Body.Value, parentUid));
-                Dirty(uid, component);
-            }
-        }
+        Dirty(uid, component);
     }
 
-    private void OnOrganRemoved(EntityUid uid, OrganComponent component, EntGotRemovedFromContainerMessage args)
+    private void RemoveOrgan(EntityUid uid, EntityUid parentPartUid, OrganComponent component)
     {
-        // Lifestage shenanigans.
-        if (component.Body != null && TerminatingOrDeleted(component.Body.Value))
-            return;
-
-        if (HasComp<BodyPartComponent>(args.Container.Owner))
-            RaiseLocalEvent(uid, new RemovedFromPartEvent(args.Container.Owner));
+        RaiseLocalEvent(uid, new RemovedFromPartEvent(parentPartUid));
 
-        if (component.Body == null)
-            return;
+        if (component.Body != null)
+        {
+            RaiseLocalEvent(uid, new RemovedFromPartInBodyEvent(component.Body.Value, parentPartUid));
+        }
 
-        RaiseLocalEvent(uid, new RemovedFromPartInBodyEvent(component.Body.Value, args.Container.Owner));
         component.Body = null;
         Dirty(uid, component);
     }
index 75a52ddbc57af9cf34fee50cff5c6216c0b63050..463a4b7661a9253faed81ba6c3cd3a9e61adad9f 100644 (file)
@@ -19,99 +19,89 @@ public partial class SharedBodySystem
         // TODO: This doesn't handle comp removal on child ents.
 
         // If you modify this also see the Body partial for root parts.
-        SubscribeLocalEvent<BodyPartComponent, EntGotInsertedIntoContainerMessage>(OnBodyPartInserted);
-        SubscribeLocalEvent<BodyPartComponent, EntGotRemovedFromContainerMessage>(OnBodyPartRemoved);
+        SubscribeLocalEvent<BodyPartComponent, EntInsertedIntoContainerMessage>(OnBodyPartInserted);
+        SubscribeLocalEvent<BodyPartComponent, EntRemovedFromContainerMessage>(OnBodyPartRemoved);
     }
 
-    private void OnBodyPartInserted(EntityUid uid, BodyPartComponent component, EntGotInsertedIntoContainerMessage args)
+    private void OnBodyPartInserted(EntityUid uid, BodyPartComponent component, EntInsertedIntoContainerMessage args)
     {
-        var parentUid = args.Container.Owner;
-
-        if (HasComp<BodyComponent>(parentUid))
-        {
-            AddPart(parentUid, uid, GetPartSlotContainerId(args.Container.ID), component);
-            RecursiveBodyUpdate(uid, parentUid, component);
-        }
-
         // Body part inserted into another body part.
-        if (TryComp(parentUid, out BodyPartComponent? partComp))
+        var entity = args.Entity;
+        var slotId = args.Container.ID;
+
+        if (component.Body != null)
         {
-            if (partComp.Body == null)
-                return;
+            if (TryComp(entity, out BodyPartComponent? childPart))
+            {
+                AddPart(component.Body.Value, entity, slotId, childPart);
+                RecursiveBodyUpdate(entity, component.Body.Value, childPart);
+            }
 
-            AddPart(partComp.Body.Value, uid, GetPartSlotContainerId(args.Container.ID), component);
-            RecursiveBodyUpdate(uid, partComp.Body, component);
+            if (TryComp(entity, out OrganComponent? organ))
+            {
+                AddOrgan(entity, component.Body.Value, uid, organ);
+            }
         }
     }
 
-    private void OnBodyPartRemoved(EntityUid uid, BodyPartComponent component, EntGotRemovedFromContainerMessage args)
+    private void OnBodyPartRemoved(EntityUid uid, BodyPartComponent component, EntRemovedFromContainerMessage args)
     {
-        if (component.Body != null)
+        // TODO: lifestage shenanigans
+        if (TerminatingOrDeleted(uid))
+            return;
+
+        // Body part removed from another body part.
+        var entity = args.Entity;
+        var slotId = args.Container.ID;
+
+        if (TryComp(entity, out BodyPartComponent? childPart) && childPart.Body != null)
         {
-            // Lifestage shenanigans.
-            if (TerminatingOrDeleted(component.Body.Value))
-                return;
+            RemovePart(childPart.Body.Value, entity, slotId, childPart);
+            RecursiveBodyUpdate(entity, null, childPart);
+        }
 
-            RemovePart(component.Body.Value, uid, GetPartSlotContainerId(args.Container.ID), component);
-            RecursiveBodyUpdate(uid, null, component);
+        if (TryComp(entity, out OrganComponent? organ))
+        {
+            RemoveOrgan(entity, uid, organ);
         }
     }
 
-    /// <summary>
-    /// Updates body parts recursively for a particular entity, including itself.
-    /// </summary>
     private void RecursiveBodyUpdate(EntityUid uid, EntityUid? bodyUid, BodyPartComponent component)
     {
-        // Recursively iterate all child parts and organs and ensure their body refs are updated.
         foreach (var children in GetBodyPartChildren(uid, component))
         {
-            if (children.Component.Body == bodyUid)
-                continue;
-
-            // Raise the body part change event if relevant.
-            if (Containers.TryGetContainingContainer(children.Id, out var childContainer))
+            if (children.Component.Body != bodyUid)
             {
-                var slotId = GetPartSlotContainerId(childContainer.ID);
+                children.Component.Body = bodyUid;
+                Dirty(children.Id, children.Component);
 
-                if (bodyUid != null)
-                {
-                    AddPart(bodyUid.Value, children.Id, slotId, children.Component);
-                }
-                else if (component.Body != null)
+                foreach (var slotId in children.Component.Organs.Keys)
                 {
-                    RemovePart(component.Body.Value, children.Id, slotId, children.Component);
-                }
-            }
-
-            children.Component.Body = bodyUid;
-            Dirty(children.Id, children.Component);
+                    var organContainerId = GetOrganContainerId(slotId);
 
-            foreach (var slotId in children.Component.Organs.Keys)
-            {
-                var organContainerId = GetOrganContainerId(slotId);
-
-                if (!Containers.TryGetContainer(children.Id, organContainerId, out var container))
-                    continue;
+                    if (!Containers.TryGetContainer(children.Id, organContainerId, out var container))
+                        continue;
 
-                foreach (var organ in container.ContainedEntities)
-                {
-                    if (TryComp(organ, out OrganComponent? organComp))
+                    foreach (var organ in container.ContainedEntities)
                     {
-                        var oldBody = organComp.Body;
-                        organComp.Body = bodyUid;
-
-                        if (bodyUid != null)
-                        {
-                            var ev = new AddedToPartInBodyEvent(bodyUid.Value, children.Id);
-                            RaiseLocalEvent(organ, ev);
-                        }
-                        else if (oldBody != null)
+                        if (TryComp(organ, out OrganComponent? organComp))
                         {
-                            var ev = new RemovedFromPartInBodyEvent(oldBody.Value, children.Id);
-                            RaiseLocalEvent(organ, ev);
+                            var oldBody = organComp.Body;
+                            organComp.Body = bodyUid;
+
+                            if (bodyUid != null)
+                            {
+                                var ev = new AddedToPartInBodyEvent(bodyUid.Value, children.Id);
+                                RaiseLocalEvent(organ, ev);
+                            }
+                            else if (oldBody != null)
+                            {
+                                var ev = new RemovedFromPartInBodyEvent(oldBody.Value, children.Id);
+                                RaiseLocalEvent(organ, ev);
+                            }
+
+                            Dirty(organ, organComp);
                         }
-
-                        Dirty(organ, organComp);
                     }
                 }
             }
@@ -126,8 +116,9 @@ public partial class SharedBodySystem
         BodyComponent? bodyComp = null)
     {
         DebugTools.AssertOwner(partUid, component);
-        component.Body = bodyUid;
         Dirty(partUid, component);
+        component.Body = bodyUid;
+
         var ev = new BodyPartAddedEvent(slotId, component);
         RaiseLocalEvent(bodyUid, ref ev);
 
@@ -143,8 +134,8 @@ public partial class SharedBodySystem
     {
         DebugTools.AssertOwner(partUid, component);
         Resolve(bodyUid, ref bodyComp, false);
-        component.Body = null;
         Dirty(partUid, component);
+        component.Body = null;
 
         var ev = new BodyPartRemovedEvent(slotId, component);
         RaiseLocalEvent(bodyUid, ref ev);
@@ -556,13 +547,15 @@ public partial class SharedBodySystem
     }
 
     /// <summary>
-    /// Returns all body part components for this entity not including itself.
+    /// Returns all body part components for this entity including itself.
     /// </summary>
     public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyPartChildren(EntityUid partId, BodyPartComponent? part = null)
     {
         if (!Resolve(partId, ref part, false))
             yield break;
 
+        yield return (partId, part);
+
         foreach (var slotId in part.Children.Keys)
         {
             var containerSlotId = GetPartSlotContainerId(slotId);
@@ -574,8 +567,6 @@ public partial class SharedBodySystem
                     if (!TryComp(containedEnt, out BodyPartComponent? childPart))
                         continue;
 
-                    yield return (containedEnt, childPart);
-
                     foreach (var value in GetBodyPartChildren(containedEnt, childPart))
                     {
                         yield return value;
@@ -638,9 +629,6 @@ public partial class SharedBodySystem
             return false;
         }
 
-        if (childId == parentId)
-            return true;
-
         foreach (var (foundId, _) in GetBodyPartChildren(parentId, parent))
         {
             if (foundId == childId)
index c10b3e9fcaab9a979ed5e3eae8e4e28048422828..431503c96e2d263941219d668ae9ccaa6e12caae 100644 (file)
@@ -39,7 +39,6 @@ public abstract partial class SharedBodySystem : EntitySystem
         base.Initialize();
 
         InitializeBody();
-        InitializeOrgans();
         InitializeParts();
     }