]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Pathfinding stuff (#23516)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Thu, 4 Jan 2024 12:33:56 +0000 (23:33 +1100)
committerGitHub <noreply@github.com>
Thu, 4 Jan 2024 12:33:56 +0000 (05:33 -0700)
Won't fix any crashes

Content.Server/NPC/Pathfinding/PathfindingSystem.Grid.cs
Content.Server/NPC/Pathfinding/PathfindingSystem.cs

index b3db87e8966c8f24a10a692878f3f9ae7b2abfa4..075011171fef7c93601063e50d2f75debf5997bd 100644 (file)
@@ -102,7 +102,7 @@ public sealed partial class PathfindingSystem
             // TODO: Dump all this shit and just do it live it's probably fast enough.
             if (comp.DirtyChunks.Count == 0 ||
                 curTime < comp.NextUpdate ||
-                !TryComp<MapGridComponent>(uid, out var mapGridComp))
+                !_gridQuery.TryGetComponent(uid, out var mapGridComp))
             {
                 continue;
             }
@@ -145,15 +145,7 @@ public sealed partial class PathfindingSystem
             // Without parallel this is roughly 3x slower on my desktop.
             Parallel.For(0, dirt.Length, options, i =>
             {
-                // Doing the queries per task seems faster.
-                var accessQuery = GetEntityQuery<AccessReaderComponent>();
-                var destructibleQuery = GetEntityQuery<DestructibleComponent>();
-                var doorQuery = GetEntityQuery<DoorComponent>();
-                var climbableQuery = GetEntityQuery<ClimbableComponent>();
-                var fixturesQuery = GetEntityQuery<FixturesComponent>();
-                var xformQuery = GetEntityQuery<TransformComponent>();
-                BuildBreadcrumbs(dirt[i], (uid, mapGridComp), accessQuery, destructibleQuery, doorQuery, climbableQuery,
-                    fixturesQuery, xformQuery);
+                BuildBreadcrumbs(dirt[i], (uid, mapGridComp));
             });
 
             const int Division = 4;
@@ -284,9 +276,9 @@ public sealed partial class PathfindingSystem
 
     private void OnMoveEvent(ref MoveEvent ev)
     {
-        if (!TryComp<FixturesComponent>(ev.Sender, out var fixtures) ||
+        if (!_fixturesQuery.TryGetComponent(ev.Sender, out var fixtures) ||
             !IsBodyRelevant(fixtures) ||
-            HasComp<MapGridComponent>(ev.Sender))
+            _gridQuery.HasComponent(ev.Sender))
         {
             return;
         }
@@ -408,19 +400,11 @@ public sealed partial class PathfindingSystem
 
     private Vector2i GetOrigin(EntityCoordinates coordinates, EntityUid gridUid)
     {
-        var gridXform = Transform(gridUid);
-        var localPos = gridXform.InvWorldMatrix.Transform(coordinates.ToMapPos(EntityManager));
+        var localPos = _transform.GetInvWorldMatrix(gridUid).Transform(coordinates.ToMapPos(EntityManager, _transform));
         return new Vector2i((int) Math.Floor(localPos.X / ChunkSize), (int) Math.Floor(localPos.Y / ChunkSize));
     }
 
-    private void BuildBreadcrumbs(GridPathfindingChunk chunk,
-        Entity<MapGridComponent> grid,
-        EntityQuery<AccessReaderComponent> accessQuery,
-        EntityQuery<DestructibleComponent> destructibleQuery,
-        EntityQuery<DoorComponent> doorQuery,
-        EntityQuery<ClimbableComponent> climbableQuery,
-        EntityQuery<FixturesComponent> fixturesQuery,
-        EntityQuery<TransformComponent> xformQuery)
+    private void BuildBreadcrumbs(GridPathfindingChunk chunk, Entity<MapGridComponent> grid)
     {
         var sw = new Stopwatch();
         sw.Start();
@@ -447,7 +431,7 @@ public sealed partial class PathfindingSystem
                 var tilePos = new Vector2i(x, y) + gridOrigin;
                 tilePolys.Clear();
 
-                var tile = grid.Comp.GetTileRef(tilePos);
+                var tile = _maps.GetTileRef(grid.Owner, grid.Comp, tilePos);
                 var flags = tile.Tile.IsEmpty ? PathfindingBreadcrumbFlag.Space : PathfindingBreadcrumbFlag.None;
                 // var isBorder = x < 0 || y < 0 || x == ChunkSize - 1 || y == ChunkSize - 1;
 
@@ -457,16 +441,16 @@ public sealed partial class PathfindingSystem
                 foreach (var ent in available)
                 {
                     // Irrelevant for pathfinding
-                    if (!fixturesQuery.TryGetComponent(ent, out var fixtures) ||
+                    if (!_fixturesQuery.TryGetComponent(ent, out var fixtures) ||
                         !IsBodyRelevant(fixtures))
                     {
                         continue;
                     }
 
-                    var xform = xformQuery.GetComponent(ent);
+                    var xform = _xformQuery.GetComponent(ent);
 
                     if (xform.ParentUid != grid.Owner ||
-                        grid.Comp.LocalToTile(xform.Coordinates) != tilePos)
+                        _maps.LocalToTile(grid.Owner, grid.Comp, xform.Coordinates) != tilePos)
                     {
                         continue;
                     }
@@ -489,7 +473,7 @@ public sealed partial class PathfindingSystem
 
                         foreach (var ent in tileEntities)
                         {
-                            if (!fixturesQuery.TryGetComponent(ent, out var fixtures))
+                            if (!_fixturesQuery.TryGetComponent(ent, out var fixtures))
                                 continue;
 
                             var colliding = false;
@@ -516,7 +500,7 @@ public sealed partial class PathfindingSystem
                                 }
 
                                 if (!intersects ||
-                                    !xformQuery.TryGetComponent(ent, out var xform))
+                                    !_xformQuery.TryGetComponent(ent, out var xform))
                                 {
                                     continue;
                                 }
@@ -535,22 +519,22 @@ public sealed partial class PathfindingSystem
                             if (!colliding)
                                 continue;
 
-                            if (accessQuery.HasComponent(ent))
+                            if (_accessQuery.HasComponent(ent))
                             {
                                 flags |= PathfindingBreadcrumbFlag.Access;
                             }
 
-                            if (doorQuery.HasComponent(ent))
+                            if (_doorQuery.HasComponent(ent))
                             {
                                 flags |= PathfindingBreadcrumbFlag.Door;
                             }
 
-                            if (climbableQuery.HasComponent(ent))
+                            if (_climbableQuery.HasComponent(ent))
                             {
                                 flags |= PathfindingBreadcrumbFlag.Climb;
                             }
 
-                            if (destructibleQuery.TryGetComponent(ent, out var damageable))
+                            if (_destructibleQuery.TryGetComponent(ent, out var damageable))
                             {
                                 damage += _destructible.DestroyedAt(ent, damageable).Float();
                             }
index 1830b35bb68673f34affa2100222d90c89353aa7..13ad963a0c37fe11c3056f9e5f33f857c70de03d 100644 (file)
@@ -6,11 +6,15 @@ using System.Threading.Tasks;
 using Content.Server.Administration.Managers;
 using Content.Server.Destructible;
 using Content.Server.NPC.Systems;
+using Content.Shared.Access.Components;
 using Content.Shared.Administration;
+using Content.Shared.Climbing.Components;
+using Content.Shared.Doors.Components;
 using Content.Shared.NPC;
 using Robust.Server.Player;
 using Robust.Shared.Enums;
 using Robust.Shared.Map;
+using Robust.Shared.Map.Components;
 using Robust.Shared.Physics;
 using Robust.Shared.Physics.Systems;
 using Robust.Shared.Player;
@@ -45,6 +49,7 @@ namespace Content.Server.NPC.Pathfinding
         [Dependency] private readonly EntityLookupSystem _lookup = default!;
         [Dependency] private readonly FixtureSystem _fixtures = default!;
         [Dependency] private readonly NPCSystem _npc = default!;
+        [Dependency] private readonly SharedMapSystem _maps = default!;
         [Dependency] private readonly SharedPhysicsSystem _physics = default!;
         [Dependency] private readonly SharedTransformSystem _transform = default!;
 
@@ -63,9 +68,26 @@ namespace Content.Server.NPC.Pathfinding
         private int _portalIndex;
         private readonly Dictionary<int, PathPortal> _portals = new();
 
+        private EntityQuery<AccessReaderComponent> _accessQuery;
+        private EntityQuery<DestructibleComponent> _destructibleQuery;
+        private EntityQuery<DoorComponent> _doorQuery;
+        private EntityQuery<ClimbableComponent> _climbableQuery;
+        private EntityQuery<FixturesComponent> _fixturesQuery;
+        private EntityQuery<MapGridComponent> _gridQuery;
+        private EntityQuery<TransformComponent> _xformQuery;
+
         public override void Initialize()
         {
             base.Initialize();
+
+            _accessQuery = GetEntityQuery<AccessReaderComponent>();
+            _destructibleQuery = GetEntityQuery<DestructibleComponent>();
+            _doorQuery = GetEntityQuery<DoorComponent>();
+            _climbableQuery = GetEntityQuery<ClimbableComponent>();
+            _fixturesQuery = GetEntityQuery<FixturesComponent>();
+            _gridQuery = GetEntityQuery<MapGridComponent>();
+            _xformQuery = GetEntityQuery<TransformComponent>();
+
             _playerManager.PlayerStatusChanged += OnPlayerChange;
             InitializeGrid();
             SubscribeNetworkEvent<RequestPathfindingDebugMessage>(OnBreadcrumbs);