Pathfinding stuff (#23516)

Won't fix any crashes
This commit is contained in:
metalgearsloth
2024-01-04 23:33:56 +11:00
committed by GitHub
parent 0603115ccb
commit f84cd9c48a
2 changed files with 38 additions and 32 deletions

View 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. // TODO: Dump all this shit and just do it live it's probably fast enough.
if (comp.DirtyChunks.Count == 0 || if (comp.DirtyChunks.Count == 0 ||
curTime < comp.NextUpdate || curTime < comp.NextUpdate ||
!TryComp<MapGridComponent>(uid, out var mapGridComp)) !_gridQuery.TryGetComponent(uid, out var mapGridComp))
{ {
continue; continue;
} }
@@ -145,15 +145,7 @@ public sealed partial class PathfindingSystem
// Without parallel this is roughly 3x slower on my desktop. // Without parallel this is roughly 3x slower on my desktop.
Parallel.For(0, dirt.Length, options, i => Parallel.For(0, dirt.Length, options, i =>
{ {
// Doing the queries per task seems faster. BuildBreadcrumbs(dirt[i], (uid, mapGridComp));
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);
}); });
const int Division = 4; const int Division = 4;
@@ -284,9 +276,9 @@ public sealed partial class PathfindingSystem
private void OnMoveEvent(ref MoveEvent ev) private void OnMoveEvent(ref MoveEvent ev)
{ {
if (!TryComp<FixturesComponent>(ev.Sender, out var fixtures) || if (!_fixturesQuery.TryGetComponent(ev.Sender, out var fixtures) ||
!IsBodyRelevant(fixtures) || !IsBodyRelevant(fixtures) ||
HasComp<MapGridComponent>(ev.Sender)) _gridQuery.HasComponent(ev.Sender))
{ {
return; return;
} }
@@ -408,19 +400,11 @@ public sealed partial class PathfindingSystem
private Vector2i GetOrigin(EntityCoordinates coordinates, EntityUid gridUid) private Vector2i GetOrigin(EntityCoordinates coordinates, EntityUid gridUid)
{ {
var gridXform = Transform(gridUid); var localPos = _transform.GetInvWorldMatrix(gridUid).Transform(coordinates.ToMapPos(EntityManager, _transform));
var localPos = gridXform.InvWorldMatrix.Transform(coordinates.ToMapPos(EntityManager));
return new Vector2i((int) Math.Floor(localPos.X / ChunkSize), (int) Math.Floor(localPos.Y / ChunkSize)); return new Vector2i((int) Math.Floor(localPos.X / ChunkSize), (int) Math.Floor(localPos.Y / ChunkSize));
} }
private void BuildBreadcrumbs(GridPathfindingChunk chunk, private void BuildBreadcrumbs(GridPathfindingChunk chunk, Entity<MapGridComponent> grid)
Entity<MapGridComponent> grid,
EntityQuery<AccessReaderComponent> accessQuery,
EntityQuery<DestructibleComponent> destructibleQuery,
EntityQuery<DoorComponent> doorQuery,
EntityQuery<ClimbableComponent> climbableQuery,
EntityQuery<FixturesComponent> fixturesQuery,
EntityQuery<TransformComponent> xformQuery)
{ {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
@@ -447,7 +431,7 @@ public sealed partial class PathfindingSystem
var tilePos = new Vector2i(x, y) + gridOrigin; var tilePos = new Vector2i(x, y) + gridOrigin;
tilePolys.Clear(); 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 flags = tile.Tile.IsEmpty ? PathfindingBreadcrumbFlag.Space : PathfindingBreadcrumbFlag.None;
// var isBorder = x < 0 || y < 0 || x == ChunkSize - 1 || y == ChunkSize - 1; // 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) foreach (var ent in available)
{ {
// Irrelevant for pathfinding // Irrelevant for pathfinding
if (!fixturesQuery.TryGetComponent(ent, out var fixtures) || if (!_fixturesQuery.TryGetComponent(ent, out var fixtures) ||
!IsBodyRelevant(fixtures)) !IsBodyRelevant(fixtures))
{ {
continue; continue;
} }
var xform = xformQuery.GetComponent(ent); var xform = _xformQuery.GetComponent(ent);
if (xform.ParentUid != grid.Owner || if (xform.ParentUid != grid.Owner ||
grid.Comp.LocalToTile(xform.Coordinates) != tilePos) _maps.LocalToTile(grid.Owner, grid.Comp, xform.Coordinates) != tilePos)
{ {
continue; continue;
} }
@@ -489,7 +473,7 @@ public sealed partial class PathfindingSystem
foreach (var ent in tileEntities) foreach (var ent in tileEntities)
{ {
if (!fixturesQuery.TryGetComponent(ent, out var fixtures)) if (!_fixturesQuery.TryGetComponent(ent, out var fixtures))
continue; continue;
var colliding = false; var colliding = false;
@@ -516,7 +500,7 @@ public sealed partial class PathfindingSystem
} }
if (!intersects || if (!intersects ||
!xformQuery.TryGetComponent(ent, out var xform)) !_xformQuery.TryGetComponent(ent, out var xform))
{ {
continue; continue;
} }
@@ -535,22 +519,22 @@ public sealed partial class PathfindingSystem
if (!colliding) if (!colliding)
continue; continue;
if (accessQuery.HasComponent(ent)) if (_accessQuery.HasComponent(ent))
{ {
flags |= PathfindingBreadcrumbFlag.Access; flags |= PathfindingBreadcrumbFlag.Access;
} }
if (doorQuery.HasComponent(ent)) if (_doorQuery.HasComponent(ent))
{ {
flags |= PathfindingBreadcrumbFlag.Door; flags |= PathfindingBreadcrumbFlag.Door;
} }
if (climbableQuery.HasComponent(ent)) if (_climbableQuery.HasComponent(ent))
{ {
flags |= PathfindingBreadcrumbFlag.Climb; flags |= PathfindingBreadcrumbFlag.Climb;
} }
if (destructibleQuery.TryGetComponent(ent, out var damageable)) if (_destructibleQuery.TryGetComponent(ent, out var damageable))
{ {
damage += _destructible.DestroyedAt(ent, damageable).Float(); damage += _destructible.DestroyedAt(ent, damageable).Float();
} }

View File

@@ -6,11 +6,15 @@ using System.Threading.Tasks;
using Content.Server.Administration.Managers; using Content.Server.Administration.Managers;
using Content.Server.Destructible; using Content.Server.Destructible;
using Content.Server.NPC.Systems; using Content.Server.NPC.Systems;
using Content.Shared.Access.Components;
using Content.Shared.Administration; using Content.Shared.Administration;
using Content.Shared.Climbing.Components;
using Content.Shared.Doors.Components;
using Content.Shared.NPC; using Content.Shared.NPC;
using Robust.Server.Player; using Robust.Server.Player;
using Robust.Shared.Enums; using Robust.Shared.Enums;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Player; using Robust.Shared.Player;
@@ -45,6 +49,7 @@ namespace Content.Server.NPC.Pathfinding
[Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly FixtureSystem _fixtures = default!; [Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] private readonly NPCSystem _npc = default!; [Dependency] private readonly NPCSystem _npc = default!;
[Dependency] private readonly SharedMapSystem _maps = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly SharedTransformSystem _transform = default!;
@@ -63,9 +68,26 @@ namespace Content.Server.NPC.Pathfinding
private int _portalIndex; private int _portalIndex;
private readonly Dictionary<int, PathPortal> _portals = new(); 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() public override void Initialize()
{ {
base.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; _playerManager.PlayerStatusChanged += OnPlayerChange;
InitializeGrid(); InitializeGrid();
SubscribeNetworkEvent<RequestPathfindingDebugMessage>(OnBreadcrumbs); SubscribeNetworkEvent<RequestPathfindingDebugMessage>(OnBreadcrumbs);