From 4db3b30dc137e9c9c0b906730e205a7ddd8365b3 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 19 Oct 2022 10:07:44 +1100 Subject: [PATCH] Sleep NPCs on attach (#12014) --- .../Mind/Commands/MakeSentientCommand.cs | 2 -- Content.Server/NPC/Systems/NPCSystem.cs | 23 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Content.Server/Mind/Commands/MakeSentientCommand.cs b/Content.Server/Mind/Commands/MakeSentientCommand.cs index 472d2f9499..0df1b5d597 100644 --- a/Content.Server/Mind/Commands/MakeSentientCommand.cs +++ b/Content.Server/Mind/Commands/MakeSentientCommand.cs @@ -44,8 +44,6 @@ namespace Content.Server.Mind.Commands public static void MakeSentient(EntityUid uid, IEntityManager entityManager, bool allowMovement = true, bool allowSpeech = true) { - entityManager.RemoveComponent(uid); - entityManager.EnsureComponent(uid); if (allowMovement) { diff --git a/Content.Server/NPC/Systems/NPCSystem.cs b/Content.Server/NPC/Systems/NPCSystem.cs index fd6e545cb6..d58a73381b 100644 --- a/Content.Server/NPC/Systems/NPCSystem.cs +++ b/Content.Server/NPC/Systems/NPCSystem.cs @@ -1,11 +1,11 @@ +using Content.Server.MobState; using Content.Server.NPC.Components; using Content.Server.NPC.HTN; using Content.Shared.CCVar; using Content.Shared.MobState; using JetBrains.Annotations; +using Robust.Server.GameObjects; using Robust.Shared.Configuration; -using Robust.Shared.Prototypes; -using Robust.Shared.Reflection; namespace Content.Server.NPC.Systems { @@ -17,6 +17,7 @@ namespace Content.Server.NPC.Systems { [Dependency] private readonly IConfigurationManager _configurationManager = default!; [Dependency] private readonly HTNSystem _htn = default!; + [Dependency] private readonly MobStateSystem _mobState = default!; private ISawmill _sawmill = default!; @@ -43,10 +44,25 @@ namespace Content.Server.NPC.Systems SubscribeLocalEvent(OnMobStateChange); SubscribeLocalEvent(OnNPCMapInit); SubscribeLocalEvent(OnNPCShutdown); + SubscribeLocalEvent(OnPlayerNPCAttach); + SubscribeLocalEvent(OnPlayerNPCDetach); _configurationManager.OnValueChanged(CCVars.NPCEnabled, SetEnabled, true); _configurationManager.OnValueChanged(CCVars.NPCMaxUpdates, SetMaxUpdates, true); } + private void OnPlayerNPCAttach(EntityUid uid, NPCComponent component, PlayerAttachedEvent args) + { + SleepNPC(uid, component); + } + + private void OnPlayerNPCDetach(EntityUid uid, NPCComponent component, PlayerDetachedEvent args) + { + if (_mobState.IsIncapacitated(uid)) + return; + + WakeNPC(uid, component); + } + private void SetMaxUpdates(int obj) => _maxUpdates = obj; private void SetEnabled(bool value) => Enabled = value; @@ -116,6 +132,9 @@ namespace Content.Server.NPC.Systems private void OnMobStateChange(EntityUid uid, NPCComponent component, MobStateChangedEvent args) { + if (HasComp(uid)) + return; + switch (args.CurrentMobState) { case DamageState.Alive: