diff --git a/Content.Server/Polymorph/Components/PolymorphedEntityComponent.cs b/Content.Server/Polymorph/Components/PolymorphedEntityComponent.cs index 7620203a86..7e3437a908 100644 --- a/Content.Server/Polymorph/Components/PolymorphedEntityComponent.cs +++ b/Content.Server/Polymorph/Components/PolymorphedEntityComponent.cs @@ -18,7 +18,7 @@ public sealed partial class PolymorphedEntityComponent : Component /// The original entity that the player will revert back into /// [DataField(required: true)] - public EntityUid Parent; + public EntityUid? Parent; /// /// The amount of time that has passed since the entity was created diff --git a/Content.Server/Polymorph/Systems/PolymorphSystem.cs b/Content.Server/Polymorph/Systems/PolymorphSystem.cs index d0f8b4e8fc..aabc0366cd 100644 --- a/Content.Server/Polymorph/Systems/PolymorphSystem.cs +++ b/Content.Server/Polymorph/Systems/PolymorphSystem.cs @@ -55,6 +55,7 @@ public sealed partial class PolymorphSystem : EntitySystem SubscribeLocalEvent(OnBeforeFullySliced); SubscribeLocalEvent(OnDestruction); + SubscribeLocalEvent(OnPolymorphedTerminating); InitializeMap(); } @@ -147,6 +148,16 @@ public sealed partial class PolymorphSystem : EntitySystem } } + private void OnPolymorphedTerminating(Entity ent, ref EntityTerminatingEvent args) + { + if (ent.Comp.Configuration.RevertOnDelete) + Revert(ent.AsNullable()); + + // Remove our original entity too + // Note that Revert will set Parent to null, so reverted entities will not be deleted + QueueDel(ent.Comp.Parent); + } + /// /// Polymorphs the target entity into the specific polymorph prototype /// @@ -284,13 +295,21 @@ public sealed partial class PolymorphSystem : EntitySystem if (Deleted(uid)) return null; - var parent = component.Parent; + if (component.Parent is not { } parent) + return null; + + // Clear our reference to the original entity + component.Parent = null; if (Deleted(parent)) return null; var uidXform = Transform(uid); var parentXform = Transform(parent); + // Don't swap back onto a terminating grid + if (TerminatingOrDeleted(uidXform.ParentUid)) + return null; + if (component.Configuration.ExitPolymorphSound != null) _audio.PlayPvs(component.Configuration.ExitPolymorphSound, uidXform.Coordinates); diff --git a/Content.Shared/Polymorph/PolymorphPrototype.cs b/Content.Shared/Polymorph/PolymorphPrototype.cs index 0978d7119a..26637431f0 100644 --- a/Content.Shared/Polymorph/PolymorphPrototype.cs +++ b/Content.Shared/Polymorph/PolymorphPrototype.cs @@ -105,6 +105,12 @@ public sealed partial record PolymorphConfiguration [DataField(serverOnly: true)] public bool RevertOnDeath = true; + /// + /// Whether or not the polymorph reverts when the entity is deleted. + /// + [DataField(serverOnly: true)] + public bool RevertOnDelete = true; + /// /// Whether or not the polymorph reverts when the entity is eaten or fully sliced. ///