Оптимизации оптимизаций НПС (#3708)

This commit is contained in:
ThereDrD 2026-01-13 17:43:23 +03:00 committed by GitHub
parent 19c034e949
commit e4892b7e76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 116 additions and 116 deletions

View file

@ -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<ActorComponent> _actorQuery;
private EntityQuery<ActiveNPCComponent> _activeQuery;
private EntityQuery<GhostComponent> _ghostQuery;
private readonly HashSet<Entity<ActorComponent>> _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<ActorComponent>();
_activeQuery = GetEntityQuery<ActiveNPCComponent>();
_ghostQuery = GetEntityQuery<GhostComponent>();
}
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<HTNComponent, TransformComponent>();
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;
}
}

View file

@ -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<HTNComponent>();
while(query.MoveNext(out var uid, out var htn))
{
if (!_mobStateSystem.IsAlive(uid))
continue;
if (HasComp<ActorComponent>(uid))
continue;
if (AllowNpc(uid))
{
if (!HasComp<ActiveNPCComponent>(uid))
{
_npcSystem.WakeNPC(uid, htn);
}
}
else
{
if (HasComp<ActiveNPCComponent>(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<GhostComponent>(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;
}
}

View file

@ -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
/// <returns>If the entity is alive</returns>
public bool IsAlive(EntityUid target, MobStateComponent? component = null)
{
if (TryComp<MechComponent>(target, out var mech))
return !mech.Broken;
if (!_mobStateQuery.Resolve(target, ref component, false))
return false;
return component.CurrentState == MobState.Alive;

View file

@ -292,9 +292,11 @@ public sealed partial class SunriseCCVars : CVars
* NPCs
*/
public static readonly CVarDef<bool> NPCDisableWithoutPlayers = CVarDef.Create("npc.disable_without_players", true);
public static readonly CVarDef<bool> NpcDisableWithoutPlayers =
CVarDef.Create("npc.disable_without_players", true, CVar.SERVERONLY | CVar.ARCHIVE);
public static readonly CVarDef<float> NPCDisableDistance = CVarDef.Create("npc.disable_distance", 20f);
public static readonly CVarDef<float> NpcDisableDistance =
CVarDef.Create("npc.disable_distance", 20f, CVar.SERVERONLY | CVar.ARCHIVE);
/*
* Vote

View file

@ -47,3 +47,8 @@ ssd_sleep_time = 3600
[transithub]
arrivals_round_start_spawn = false
# Sunrise-Edit
[npc]
disable_without_players = false
# Sunrise-Edit