This repository has been archived on 2024-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
TrueCraft/TrueCraft.Core/AI/AStarPathFinder.cs
Drew DeVault 362c852f51 Many improvements to server stability+performance
Sorry for the vague commit message. There were a lot of changes. Here's
a list of most of them:

- Lighting updates are timeboxed each frame
- The next environment frame is queued sooner if the current one took
  longer (as soon as immediately)
- Issues with the physics engines and mobs using it were (mostly) fixed,
  mobs no longer freak out and get stuck on physics objects
- Mob AI/pathfinding is done more intelligently
- The player can no longer spawn in an ocean or a desert biome
- Some deadlocks in Region were fixed (more remain to be fixed)

The current performance bottlenecks are lighting (still) and propegating
initial chunk loads to blocks that need to schedule things (such as
grass blocks). I think the main culprit in the latter case is grass
blocks and water blocks. The former can be improved by adding a block
cache to World, but that'll take some custom work. This step is just
gonna be slow no matter what, we might have to split it across several
frames but it's never going to be great.

There still seems to be a deadlock somewhere in all of this mess, in the
world code. I'll find it later.
2017-05-22 19:51:23 -04:00

110 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using TrueCraft.API;
using TrueCraft.API.World;
using System.Diagnostics;
using TrueCraft.API.Logic;
namespace TrueCraft.Core.AI
{
public class AStarPathFinder
{
private readonly Coordinates3D[] Neighbors =
{
Coordinates3D.North,
Coordinates3D.East,
Coordinates3D.South,
Coordinates3D.West
};
private readonly Coordinates3D[][] DiagonalNeighbors =
{
new[] { Coordinates3D.North, Coordinates3D.East },
new[] { Coordinates3D.North, Coordinates3D.West },
new[] { Coordinates3D.South, Coordinates3D.East },
new[] { Coordinates3D.South, Coordinates3D.West },
};
private PathResult TracePath(Coordinates3D start, Coordinates3D goal, Dictionary<Coordinates3D, Coordinates3D> parents)
{
var list = new List<Coordinates3D>();
var current = goal;
while (current != start)
{
current = parents[current];
list.Insert(0, current);
}
list.Add(goal);
return new PathResult { Waypoints = list };
}
private bool CanOccupyVoxel(IWorld world, BoundingBox box, Coordinates3D voxel)
{
var id = world.GetBlockID(voxel);
if (world.BlockRepository == null)
return id == 0;
var provider = world.BlockRepository.GetBlockProvider(id);
if (provider == null)
return true;
return provider.BoundingBox == null;
}
private IEnumerable<Coordinates3D> GetNeighbors(IWorld world, BoundingBox subject, Coordinates3D current)
{
for (int i = 0; i < Neighbors.Length; i++)
{
var next = Neighbors[i] + current;
if (CanOccupyVoxel(world, subject, next))
yield return next;
}
for (int i = 0; i < DiagonalNeighbors.Length; i++)
{
var pair = DiagonalNeighbors[i];
var next = pair[0] + pair[1] + current;
if (CanOccupyVoxel(world, subject, next)
&& CanOccupyVoxel(world, subject, pair[0] + current)
&& CanOccupyVoxel(world, subject, pair[1] + current))
yield return next;
}
}
public PathResult FindPath(IWorld world, BoundingBox subject, Coordinates3D start, Coordinates3D goal)
{
// Thanks to www.redblobgames.com/pathfinding/a-star/implementation.html
var parents = new Dictionary<Coordinates3D, Coordinates3D>();
var costs = new Dictionary<Coordinates3D, double>();
var openset = new PriorityQueue<Coordinates3D>();
var closedset = new HashSet<Coordinates3D>();
openset.Enqueue(start, 0);
parents[start] = start;
costs[start] = start.DistanceTo(goal);
while (openset.Count > 0)
{
var current = openset.Dequeue();
if (current == goal)
return TracePath(start, goal, parents);
closedset.Add(current);
foreach (var next in GetNeighbors(world, subject, current))
{
if (closedset.Contains(next))
continue;
var cost = (int)(costs[current] + current.DistanceTo(next));
if (!costs.ContainsKey(next) || cost < costs[next])
{
costs[next] = cost;
var priority = cost + next.DistanceTo(goal);
openset.Enqueue(next, priority);
parents[next] = current;
}
}
}
return null;
}
}
}