2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
|
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
namespace Content.Shared.NPC;
|
|
|
|
|
|
|
|
|
|
public abstract class SharedPathfindingSystem : EntitySystem
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is equivalent to agent radii for navmeshes. In our case it's preferable that things are cleanly
|
|
|
|
|
/// divisible per tile so we'll make sure it works as a discrete number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const byte SubStep = 4;
|
|
|
|
|
|
|
|
|
|
public const byte ChunkSize = 8;
|
2023-07-08 14:08:32 +10:00
|
|
|
public static readonly Vector2 ChunkSizeVec = new(ChunkSize, ChunkSize);
|
2022-09-30 14:39:48 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We won't do points on edges so we'll offset them slightly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected const float StepOffset = 1f / SubStep / 2f;
|
|
|
|
|
|
2023-07-08 14:08:32 +10:00
|
|
|
private static readonly Vector2 StepOffsetVec = new(StepOffset, StepOffset);
|
|
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
public Vector2 GetCoordinate(Vector2i chunk, Vector2i index)
|
|
|
|
|
{
|
2023-07-08 14:08:32 +10:00
|
|
|
return new Vector2(index.X, index.Y) / SubStep+ (chunk) * ChunkSizeVec + StepOffsetVec;
|
2022-09-30 14:39:48 +10:00
|
|
|
}
|
2023-06-27 19:17:42 +10:00
|
|
|
|
|
|
|
|
public static float ManhattanDistance(Vector2i start, Vector2i end)
|
|
|
|
|
{
|
|
|
|
|
var distance = end - start;
|
|
|
|
|
return Math.Abs(distance.X) + Math.Abs(distance.Y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static 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);
|
|
|
|
|
}
|
2022-09-30 14:39:48 +10:00
|
|
|
}
|