using System;
using System.Collections;
namespace KillerInstinct.AI{
/// A node class for doing pathfinding on a 2-dimensional map
public class AStarNode2D : AStarNode
{
private int FX;
private int FY;
// the X-coordinate of the node
public int X
{
get
{ return FX; }
}
// the Y-coordinate of the node
public int Y
{
get
{ return FY; }
}
// Constructor for a node in a 2-dimensional map
public AStarNode2D(AStarNode parent, AStarNode goalNode, double ACost, int AX, int AY)
: base(parent, goalNode, ACost)
{
FX = AX;
FY = AY;
}
// Adds a successor to a list if it is not impassible or the parent node
private void AddSuccessor(ArrayList ASuccessors,int AX,int AY)
{
int CurrentCost = MapClass.GetMap(AX,AY);
if(CurrentCost == -1)
{
return;
}
AStarNode2D NewNode = new AStarNode2D(this,GoalNode,Cost + CurrentCost,AX,AY);
if(NewNode.IsSameState(Parent))
{
return;
}
ASuccessors.Add(NewNode);
}
#region Overidden Methods
// Determines whether the current node is the same state as the on passed.
// Returns true if they are the same state
public override bool IsSameState(AStarNode ANode)
{
if(ANode == null)
return false;
return ((((AStarNode2D)ANode).X == FX) &&
(((AStarNode2D)ANode).Y == FY));
}
// Calculates the estimated cost for the remaining trip to the goal.
public override void Calculate()
{
if(GoalNode != null)
{
double xd = FX - ((AStarNode2D)GoalNode).X;
double yd = FY - ((AStarNode2D)GoalNode).Y;
// "Euclidean distance" - Used when search can move at any angle.
//GoalEstimate = Math.Sqrt((xd*xd) + (yd*yd));
// "Manhattan Distance" - Used when search can only move vertically and
// horizontally.
//GoalEstimate = Math.Abs(xd) + Math.Abs(yd);
// "Diagonal Distance" - Used when the search can move in 8 directions.
GoalEstimate = Math.Max(Math.Abs(xd),Math.Abs(yd));
}
else
{
GoalEstimate = 0;
}
}
// Gets all successors nodes from the current node and adds them to the passed successor list
public override void GetSuccessors(ArrayList ASuccessors)
{
ASuccessors.Clear();
AddSuccessor(ASuccessors,FX-1,FY );
AddSuccessor(ASuccessors,FX-1,FY-1);
AddSuccessor(ASuccessors,FX ,FY-1);
AddSuccessor(ASuccessors,FX+1,FY-1);
AddSuccessor(ASuccessors,FX+1,FY );
AddSuccessor(ASuccessors,FX+1,FY+1);
AddSuccessor(ASuccessors,FX ,FY+1);
AddSuccessor(ASuccessors,FX-1,FY+1);
}
// Prints information about the current node
public override void PrintNodeInfo()
{
Console.WriteLine("X:\t{0}\tY:\t{1}\tCost:\t{2}\tEst:\t{3}\tTotal:\t{4}",FX,FY,Cost,GoalEstimate,TotalCost);
}
#endregion
}
}
|