Salvage dungeons (#14520)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Content.Shared.Gravity;
|
||||
using Content.Shared.NPC;
|
||||
|
||||
namespace Content.Server.NPC.Pathfinding;
|
||||
@@ -45,7 +46,8 @@ public sealed partial class PathfindingSystem
|
||||
var modifier = 1f;
|
||||
|
||||
// TODO
|
||||
if ((end.Data.Flags & PathfindingBreadcrumbFlag.Space) != 0x0)
|
||||
if ((end.Data.Flags & PathfindingBreadcrumbFlag.Space) != 0x0 &&
|
||||
(!TryComp<GravityComponent>(end.GraphUid, out var gravity) || !gravity.Enabled))
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
@@ -16,12 +16,25 @@ public sealed partial class PathfindingSystem
|
||||
return dx + dy;
|
||||
}
|
||||
|
||||
public float ManhattanDistance(Vector2i start, Vector2i end)
|
||||
{
|
||||
var distance = end - start;
|
||||
return Math.Abs(distance.X) + Math.Abs(distance.Y);
|
||||
}
|
||||
|
||||
public float OctileDistance(PathPoly start, PathPoly end)
|
||||
{
|
||||
var (dx, dy) = GetDiff(start, end);
|
||||
return dx + dy + (1.41f - 2) * Math.Min(dx, dy);
|
||||
}
|
||||
|
||||
public float OctileDistance(Vector2i start, Vector2i end)
|
||||
{
|
||||
var diff = start - end;
|
||||
var ab = Vector2.Abs(diff);
|
||||
return ab.X + ab.Y + (1.41f - 2) * Math.Min(ab.X, ab.Y);
|
||||
}
|
||||
|
||||
private Vector2 GetDiff(PathPoly start, PathPoly end)
|
||||
{
|
||||
var startPos = start.Box.Center;
|
||||
|
||||
@@ -32,7 +32,7 @@ public sealed partial class PathfindingSystem
|
||||
/// If true, UpdateGrid() will not process grids.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Useful if something like a large explosion is in the process of shredding the grid, as it avoids uneccesary
|
||||
/// Useful if something like a large explosion is in the process of shredding the grid, as it avoids unneccesary
|
||||
/// updating.
|
||||
/// </remarks>
|
||||
public bool PauseUpdating = false;
|
||||
@@ -232,11 +232,6 @@ public sealed partial class PathfindingSystem
|
||||
|
||||
comp.DirtyChunks.Clear();
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
if (updateCount > 0)
|
||||
_sawmill.Debug($"Updated {updateCount} nav chunks in {_stopwatch.Elapsed.TotalMilliseconds:0.000}ms");
|
||||
#endif
|
||||
}
|
||||
|
||||
private bool IsBodyRelevant(PhysicsComponent body)
|
||||
|
||||
74
Content.Server/NPC/Pathfinding/PathfindingSystem.Helper.cs
Normal file
74
Content.Server/NPC/Pathfinding/PathfindingSystem.Helper.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
namespace Content.Server.NPC.Pathfinding;
|
||||
|
||||
public sealed partial class PathfindingSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds a generic path from start to end.
|
||||
/// </summary>
|
||||
public List<Vector2i> GetPath(Vector2i start, Vector2i end, bool diagonal = false)
|
||||
{
|
||||
if (start == end)
|
||||
{
|
||||
return new List<Vector2i>();
|
||||
}
|
||||
|
||||
var frontier = new PriorityQueue<Vector2i, float>();
|
||||
frontier.Enqueue(start, 0f);
|
||||
var cameFrom = new Dictionary<Vector2i, Vector2i>();
|
||||
var node = start;
|
||||
|
||||
while (frontier.TryDequeue(out node, out _))
|
||||
{
|
||||
if (node == end)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (diagonal)
|
||||
{
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
var direction = (DirectionFlag) i;
|
||||
var neighbor = node + direction.AsDir().ToIntVec();
|
||||
|
||||
if (!cameFrom.TryAdd(neighbor, node))
|
||||
continue;
|
||||
|
||||
var gScore = OctileDistance(neighbor, end);
|
||||
frontier.Enqueue(neighbor, gScore);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var direction = (DirectionFlag) Math.Pow(2, i);
|
||||
var neighbor = node + direction.AsDir().ToIntVec();
|
||||
|
||||
if (!cameFrom.TryAdd(neighbor, node))
|
||||
continue;
|
||||
|
||||
frontier.Enqueue(neighbor, ManhattanDistance(neighbor, end));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (node != end)
|
||||
{
|
||||
return new List<Vector2i>();
|
||||
}
|
||||
|
||||
var path = new List<Vector2i>();
|
||||
|
||||
do
|
||||
{
|
||||
path.Add(node);
|
||||
var before = cameFrom[node];
|
||||
node = before;
|
||||
} while (node != start);
|
||||
|
||||
path.Add(start);
|
||||
path.Reverse();
|
||||
return path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user