2019-08-18 15:14:35 +02:00
using System ;
using System.Collections.Generic ;
using Microsoft.Xna.Framework ;
namespace MLEM.Pathfinding {
2020-05-21 17:21:34 +02:00
/// <summary>
2022-10-09 22:18:17 +02:00
/// A 3-dimensional implementation of <see cref="AStar{T}"/> that uses <see cref="Vector3"/> for positions, and the manhattan distance as its heuristic.
2020-05-21 17:21:34 +02:00
/// </summary>
2019-08-18 15:14:35 +02:00
public class AStar3 : AStar < Vector3 > {
2022-10-09 22:18:17 +02:00
private readonly bool includeDiagonals ;
2019-08-18 15:14:35 +02:00
2020-05-21 17:21:34 +02:00
/// <inheritdoc />
2022-10-09 22:18:17 +02:00
public AStar3 ( GetCost defaultCostFunction , bool includeDiagonals , float defaultCost = 1 , int defaultMaxTries = 10000 , CollectAdditionalNeighbors defaultAdditionalNeighbors = null ) :
base ( defaultCostFunction , defaultCost , defaultMaxTries , defaultAdditionalNeighbors ) {
this . includeDiagonals = includeDiagonals ;
}
2019-08-18 15:14:35 +02:00
2020-05-21 17:21:34 +02:00
/// <inheritdoc />
2022-10-09 22:18:17 +02:00
protected override float GetHeuristicDistance ( Vector3 start , Vector3 position ) {
return Math . Abs ( position . X - start . X ) + Math . Abs ( position . Y - start . Y ) + Math . Abs ( position . Z - start . Z ) ;
2019-08-18 15:14:35 +02:00
}
2020-05-21 17:21:34 +02:00
/// <inheritdoc />
2022-10-09 22:18:17 +02:00
protected override void CollectNeighbors ( Vector3 position , ISet < Vector3 > neighbors ) {
if ( this . includeDiagonals ) {
for ( var x = - 1 ; x < = 1 ; x + + ) {
for ( var y = - 1 ; y < = 1 ; y + + ) {
for ( var z = - 1 ; z < = 1 ; z + + ) {
if ( x = = 0 & & y = = 0 & & z = = 0 )
continue ;
neighbors . Add ( position + new Vector3 ( x , y , z ) ) ;
}
}
}
} else {
neighbors . Add ( position + new Vector3 ( 1 , 0 , 0 ) ) ;
neighbors . Add ( position + new Vector3 ( - 1 , 0 , 0 ) ) ;
neighbors . Add ( position + new Vector3 ( 0 , 1 , 0 ) ) ;
neighbors . Add ( position + new Vector3 ( 0 , - 1 , 0 ) ) ;
neighbors . Add ( position + new Vector3 ( 0 , 0 , 1 ) ) ;
neighbors . Add ( position + new Vector3 ( 0 , 0 , - 1 ) ) ;
}
2019-08-18 15:14:35 +02:00
}
}
2022-06-17 18:23:47 +02:00
}