From e4892b7e76ebfdf7708c31d671b9bddf0525bc6c Mon Sep 17 00:00:00 2001 From: ThereDrD <88589686+ThereDrD0@users.noreply.github.com> Date: Tue, 13 Jan 2026 17:43:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D0=B9=20=D0=9D=D0=9F=D0=A1=20(#3708)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Content.Server/_Sunrise/NPC/NpcSleepSystem.cs | 107 +++++++++++++++++ .../_Sunrise/NPCSleep/NPCSleepSystem.cs | 111 ------------------ Content.Shared/Mobs/Systems/MobStateSystem.cs | 3 - .../_Sunrise/SunriseCCVars/SunriseCCVars.cs | 6 +- .../ConfigPresets/Build/development.toml | 5 + 5 files changed, 116 insertions(+), 116 deletions(-) create mode 100644 Content.Server/_Sunrise/NPC/NpcSleepSystem.cs delete mode 100644 Content.Server/_Sunrise/NPCSleep/NPCSleepSystem.cs diff --git a/Content.Server/_Sunrise/NPC/NpcSleepSystem.cs b/Content.Server/_Sunrise/NPC/NpcSleepSystem.cs new file mode 100644 index 0000000000..8f883e42f2 --- /dev/null +++ b/Content.Server/_Sunrise/NPC/NpcSleepSystem.cs @@ -0,0 +1,107 @@ +using Content.Server.NPC.HTN; +using Content.Server.NPC.Systems; +using Content.Shared._Sunrise.SunriseCCVars; +using Content.Shared.CCVar; +using Content.Shared.Ghost; +using Content.Shared.Mobs.Systems; +using Content.Shared.NPC; +using Robust.Server.GameObjects; +using Robust.Shared.Configuration; +using Robust.Shared.Player; +using Robust.Shared.Timing; + +namespace Content.Server._Sunrise.NPC; + +public sealed partial class NpcSleepSystem : EntitySystem +{ + [Dependency] private readonly IConfigurationManager _configuration = default!; + [Dependency] private readonly IGameTiming _timing = default!; + [Dependency] private readonly MobStateSystem _mobState = default!; + [Dependency] private readonly NPCSystem _npc = default!; + [Dependency] private readonly TransformSystem _transform = default!; + [Dependency] private readonly EntityLookupSystem _lookup = default!; + + public bool Enabled = true; + public bool DisableWithoutPlayers = true; + + public float DisableDistance = 20f; + + private TimeSpan _nextCheckTime = TimeSpan.Zero; + private static readonly TimeSpan CheckCooldown = TimeSpan.FromSeconds(5); + + private EntityQuery _actorQuery; + private EntityQuery _activeQuery; + private EntityQuery _ghostQuery; + + private readonly HashSet> _players = []; + + public override void Initialize() + { + base.Initialize(); + + Subs.CVar(_configuration, CCVars.NPCEnabled, value => Enabled = value, true); + Subs.CVar(_configuration, SunriseCCVars.NpcDisableWithoutPlayers, obj => DisableWithoutPlayers = obj, true); + Subs.CVar(_configuration, SunriseCCVars.NpcDisableDistance, obj => DisableDistance = obj, true); + + _actorQuery = GetEntityQuery(); + _activeQuery = GetEntityQuery(); + _ghostQuery = GetEntityQuery(); + } + + public override void Update(float frameTime) + { + base.Update(frameTime); + + if (!Enabled || !DisableWithoutPlayers) + return; + + if (_timing.CurTime < _nextCheckTime) + return; + + _nextCheckTime = _timing.CurTime + CheckCooldown; + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var htn, out var xform)) + { + if (!_mobState.IsAlive(uid)) + continue; + + if (_actorQuery.HasComp(uid)) + continue; + + var isActive = _activeQuery.HasComponent(uid); + if (AllowNpc(uid, xform)) + { + if (!isActive) + _npc.WakeNPC(uid, htn); + } + else + { + if (isActive) + _npc.SleepNPC(uid, htn); + } + } + } + + private bool AllowNpc(EntityUid uid, TransformComponent xform) + { + var npcCoords = _transform.GetMapCoordinates(uid, xform); + + _players.Clear(); + _lookup.GetEntitiesInRange(npcCoords, + DisableDistance, + _players, + LookupFlags.Dynamic | LookupFlags.Approximate); + + foreach (var ent in _players) + { + if (_ghostQuery.HasComp(ent)) + continue; + + // Достаточно одного подходящего игрока + return true; + } + + return false; + } +} diff --git a/Content.Server/_Sunrise/NPCSleep/NPCSleepSystem.cs b/Content.Server/_Sunrise/NPCSleep/NPCSleepSystem.cs deleted file mode 100644 index ea11a53134..0000000000 --- a/Content.Server/_Sunrise/NPCSleep/NPCSleepSystem.cs +++ /dev/null @@ -1,111 +0,0 @@ -using Content.Server.NPC.HTN; -using Content.Server.NPC.Systems; -using Content.Shared._Sunrise.SunriseCCVars; -using Content.Shared.CCVar; -using Content.Shared.Ghost; -using Content.Shared.Mobs.Systems; -using Content.Shared.NPC; -using Robust.Server.Player; -using Robust.Shared.Configuration; -using Robust.Shared.Player; -using Robust.Shared.Timing; - -namespace Content.Server._Sunrise.NPCSleep; - -public sealed partial class NPCSleepSystem : EntitySystem -{ - [Dependency] private readonly IConfigurationManager _configurationManager = default!; - [Dependency] private readonly IPlayerManager _playerManager = default!; - [Dependency] private readonly IGameTiming _timing = default!; - [Dependency] private readonly MobStateSystem _mobStateSystem = default!; - [Dependency] private readonly NPCSystem _npcSystem = default!; - [Dependency] private readonly SharedTransformSystem _transform = default!; - - public bool Enabled { get; set; } = true; - - public bool DisableWithoutPlayers { get; set; } = true; - - public float DisableDistance { get; set; } = 20f; - - public TimeSpan NextTick = TimeSpan.Zero; - public TimeSpan RefreshCooldown = TimeSpan.FromSeconds(5); - - public override void Initialize() - { - base.Initialize(); - - Subs.CVar(_configurationManager, CCVars.NPCEnabled, value => Enabled = value, true); - Subs.CVar(_configurationManager, SunriseCCVars.NPCDisableWithoutPlayers, obj => DisableWithoutPlayers = obj, true); - Subs.CVar(_configurationManager, SunriseCCVars.NPCDisableDistance, obj => DisableDistance = obj, true); - } - - public override void Update(float frameTime) - { - base.Update(frameTime); - - if (!Enabled || !DisableWithoutPlayers) - return; - - if (NextTick > _timing.CurTime) - return; - - NextTick += RefreshCooldown; - - var query = EntityQueryEnumerator(); - - while(query.MoveNext(out var uid, out var htn)) - { - if (!_mobStateSystem.IsAlive(uid)) - continue; - - if (HasComp(uid)) - continue; - - if (AllowNpc(uid)) - { - if (!HasComp(uid)) - { - _npcSystem.WakeNPC(uid, htn); - } - } - else - { - if (HasComp(uid)) - { - _npcSystem.SleepNPC(uid, htn); - } - } - } - } - - private bool AllowNpc(EntityUid uid) - { - var xform = Transform(uid); - var npcCoords = xform.Coordinates.ToMap(EntityManager, _transform); - var npcMapId = xform.MapID; - - foreach (var playerSession in _playerManager.SessionsDict) - { - if (playerSession.Value.AttachedEntity == null) - continue; - - if (HasComp(playerSession.Value.AttachedEntity)) - continue; - - var xformPlayer = Transform(playerSession.Value.AttachedEntity.Value); - var playerMapId = xformPlayer.MapID; - - if (npcMapId != playerMapId) - continue; - - var playerCoords = xformPlayer.Coordinates.ToMap(EntityManager, _transform); - - var distance = (npcCoords.Position - playerCoords.Position).Length(); - - if (distance < DisableDistance) - return true; - } - - return false; - } -} diff --git a/Content.Shared/Mobs/Systems/MobStateSystem.cs b/Content.Shared/Mobs/Systems/MobStateSystem.cs index c6de0729d5..0497f3cce0 100644 --- a/Content.Shared/Mobs/Systems/MobStateSystem.cs +++ b/Content.Shared/Mobs/Systems/MobStateSystem.cs @@ -2,7 +2,6 @@ using Content.Shared.ActionBlocker; using Content.Shared.Administration.Logs; using Content.Shared.Damage.Systems; using Content.Shared.Mobs.Components; -using Content.Shared.Mech.Components; using Content.Shared.Standing; using Robust.Shared.Timing; @@ -40,8 +39,6 @@ public partial class MobStateSystem : EntitySystem /// If the entity is alive public bool IsAlive(EntityUid target, MobStateComponent? component = null) { - if (TryComp(target, out var mech)) - return !mech.Broken; if (!_mobStateQuery.Resolve(target, ref component, false)) return false; return component.CurrentState == MobState.Alive; diff --git a/Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs b/Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs index beddcf1c00..dbd61c1be3 100644 --- a/Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs +++ b/Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs @@ -292,9 +292,11 @@ public sealed partial class SunriseCCVars : CVars * NPCs */ - public static readonly CVarDef NPCDisableWithoutPlayers = CVarDef.Create("npc.disable_without_players", true); + public static readonly CVarDef NpcDisableWithoutPlayers = + CVarDef.Create("npc.disable_without_players", true, CVar.SERVERONLY | CVar.ARCHIVE); - public static readonly CVarDef NPCDisableDistance = CVarDef.Create("npc.disable_distance", 20f); + public static readonly CVarDef NpcDisableDistance = + CVarDef.Create("npc.disable_distance", 20f, CVar.SERVERONLY | CVar.ARCHIVE); /* * Vote diff --git a/Resources/ConfigPresets/Build/development.toml b/Resources/ConfigPresets/Build/development.toml index 284d7b480c..dd67ea4629 100644 --- a/Resources/ConfigPresets/Build/development.toml +++ b/Resources/ConfigPresets/Build/development.toml @@ -47,3 +47,8 @@ ssd_sleep_time = 3600 [transithub] arrivals_round_start_spawn = false + +# Sunrise-Edit +[npc] +disable_without_players = false +# Sunrise-Edit \ No newline at end of file