- Home /
This post has been wikified, any user with enough reputation can edit it.
A* endless loop
My A* path finding loops through a list of nodes that are neighbors of your starting node, however the game doesn't find a path when you are not on the same column as the goal.
public class Node
{
public Node()
{
}
public Vector3 pos;
public override bool Equals (object obj)
{
Node test = (Node)obj;
bool value = false;
if (pos.x == test.pos.x &&pos.y == test.pos.y &&pos.z == test.pos.z)
{
value = true;
}
return value;
}
}
my A* class
public class AstarHolder{
//List<Node> emptySet;
List<Node> openSet = new List<Node>();
List<Node> closedSet = new List<Node>();
Dictionary<Node,Node> cameFrom = new Dictionary<Node, Node>(); //new List<Node>();
Dictionary<Node,float> gScore = new Dictionary<Node, float>();
Dictionary<Node,float> fScore = new Dictionary<Node, float>();
public Dictionary<Node,List<Node>> neighborNodes = new Dictionary<Node, List<Node>>();
public AstarHolder()
{
}
public bool Holds(List<Node> list,Node searchNode)
{
foreach(Node node in list)
{
if (node.Equals(searchNode))
return true;
}
return false;
}
public bool Holds(Dictionary<Node,Node> list,Node searchNode)
{
foreach(Node node in list.Values)
//foreach()
{
if (node.Equals(searchNode))
return true;
}
return false;
}
public float Holds(Dictionary<Node,float> list,Node searchNode)
{
foreach(Node node in list.Keys)
{
if (node.Equals(searchNode))
return list[node];
}
return 1000;
}
float heurCost(Node start, Node goal)
{
return Vector3.Distance(start.pos,goal.pos)/4;
}
public List<Node> astar(Node start,Node goal)
{
//(list)
closedSet.Clear(); //the set of nodes already evauluated.
openSet.Add(start); //the set of tentative nodes to be checked
cameFrom.Clear(); // the map of navigated nodes
gScore[start] = 0;
fScore[start] = gScore[start] + heurCost(start,goal);
int it = -1;
while (openSet.Count > 0 && it < 10000)
{
it++;
//current = //the node in the openset having the lowest fscore[] value
Node current = new Node();
foreach (Node newCurrent in cameFrom.Values)
{
current = newCurrent;
}
float lowestScore =1000;
foreach (float fcheck in fScore.Values)
{
if (fcheck < lowestScore)
{
//lowestNode = fScore.;
lowestScore = fcheck;
}
}
foreach (Node testNode in fScore.Keys)
{
if (lowestScore == fScore[testNode] && !Holds(closedSet,testNode))
current = testNode;
}
if (current == goal || Holds(cameFrom,goal) /*|| cameFrom.Count >= 2*/)
{
return redoPath(cameFrom,goal);
}
openSet.Remove(current);
if (!Holds(closedSet,current))
closedSet.Add(current);
else
{
//breakpoint
}
List<Node> tempList = new List<Node>();// = neighborNodes[current];
foreach(Node finderNode in neighborNodes.Keys)
{
if (finderNode.Equals(current))
{
tempList = neighborNodes[finderNode];
}
}
if (current.pos.x == 0 && current.pos.y == 0)
{
it = 10000;
//breakpoint
}
else
{
//breakpoint
}
foreach(Node neighbor in tempList)
{
if (Holds(closedSet,neighbor))// closedSet.Contains(neighbor))
{
continue;
}
//float ngScore = gScore[current] + (Vector3.Distance(current.pos,neighbor.pos)/4);//distBetween(current,neighbor);
float ngScore = Holds(gScore,current) + (Vector3.Distance(current.pos,neighbor.pos)/4);//distBetween(current,neighbor);
if (!Holds(openSet,neighbor) || ngScore < gScore[neighbor])
{
cameFrom[neighbor] = current;
gScore[neighbor] = ngScore;
fScore[neighbor] = gScore[neighbor] + heurCost(neighbor,goal);
//if (!openSet.Contains(neighbor))
if (!Holds(openSet,neighbor))
openSet.Add(neighbor);
}
}
}
return null;
}
Can anyone help me make this actually get the proper path please?
Comment