From 7dbd8f8b7a978168af179b5fdc623e38d8a69d64 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sat, 26 Aug 2023 13:45:29 +1000 Subject: [PATCH] Add TryGetNpc (#19553) --- .../NPC/Pathfinding/PathfindingSystem.Grid.cs | 4 ++-- .../NPC/Pathfinding/PathfindingSystem.cs | 7 +++---- Content.Server/NPC/Systems/NPCSystem.cs | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Content.Server/NPC/Pathfinding/PathfindingSystem.Grid.cs b/Content.Server/NPC/Pathfinding/PathfindingSystem.Grid.cs index f2d4620a1e..ca970815b6 100644 --- a/Content.Server/NPC/Pathfinding/PathfindingSystem.Grid.cs +++ b/Content.Server/NPC/Pathfinding/PathfindingSystem.Grid.cs @@ -641,7 +641,7 @@ public sealed partial class PathfindingSystem } } - // _sawmill.Debug($"Built breadcrumbs in {sw.Elapsed.TotalMilliseconds}ms"); + // Log.Debug($"Built breadcrumbs in {sw.Elapsed.TotalMilliseconds}ms"); SendBreadcrumbs(chunk, grid.Owner); } @@ -827,7 +827,7 @@ public sealed partial class PathfindingSystem } } - // _sawmill.Debug($"Built navmesh in {sw.Elapsed.TotalMilliseconds}ms"); + // Log.Debug($"Built navmesh in {sw.Elapsed.TotalMilliseconds}ms"); SendPolys(chunk, component.Owner, chunkPolys); } diff --git a/Content.Server/NPC/Pathfinding/PathfindingSystem.cs b/Content.Server/NPC/Pathfinding/PathfindingSystem.cs index 71272d2299..06ff9faa69 100644 --- a/Content.Server/NPC/Pathfinding/PathfindingSystem.cs +++ b/Content.Server/NPC/Pathfinding/PathfindingSystem.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Content.Server.Administration.Managers; using Content.Server.Destructible; using Content.Server.NPC.HTN; +using Content.Server.NPC.Systems; using Content.Shared.Administration; using Content.Shared.NPC; using Robust.Server.Player; @@ -44,10 +45,9 @@ namespace Content.Server.NPC.Pathfinding [Dependency] private readonly DestructibleSystem _destructible = default!; [Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly FixtureSystem _fixtures = default!; + [Dependency] private readonly NPCSystem _npc = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!; - private ISawmill _sawmill = default!; - private readonly Dictionary _subscribedSessions = new(); [ViewVariables] @@ -66,7 +66,6 @@ namespace Content.Server.NPC.Pathfinding public override void Initialize() { base.Initialize(); - _sawmill = Logger.GetSawmill("nav"); _playerManager.PlayerStatusChanged += OnPlayerChange; InitializeGrid(); SubscribeNetworkEvent(OnBreadcrumbs); @@ -418,7 +417,7 @@ namespace Content.Server.NPC.Pathfinding public PathFlags GetFlags(EntityUid uid) { - if (!TryComp(uid, out var npc)) + if (!_npc.TryGetNpc(uid, out var npc)) { return PathFlags.None; } diff --git a/Content.Server/NPC/Systems/NPCSystem.cs b/Content.Server/NPC/Systems/NPCSystem.cs index 7c5930afc7..40b8e26897 100644 --- a/Content.Server/NPC/Systems/NPCSystem.cs +++ b/Content.Server/NPC/Systems/NPCSystem.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Content.Server.NPC.Components; using Content.Server.NPC.HTN; using Content.Shared.CCVar; @@ -77,6 +78,20 @@ namespace Content.Server.NPC.Systems return Resolve(uid, ref active, false); } + public bool TryGetNpc(EntityUid uid, [NotNullWhen(true)] out NPCComponent? component) + { + // If you add your own NPC components then add them here. + + if (TryComp(uid, out var htn)) + { + component = htn; + return true; + } + + component = null; + return false; + } + /// /// Allows the NPC to actively be updated. ///