All open tiles have the same cost. I''m using the Manhatten distance formula to calculate cost.
This is the class I am using..
astar.h:
class AStar{ struct NODE { char x, y; long f; int Parent, ID; }; void ConstructPath(NODE n); bool IsInOPEN(NODE n); bool IsInCLOSED(NODE n); void RemoveFromOPEN(NODE n); void GenerateSuccs(NODE n); void GenerateSucc(int x,int y,int Parent); NODE GetBestNode(); NODE GetNodeFromCLOSED(int ID); int cID; NODE nStart,nGoal; vector<NODE> OPEN; vector<NODE> CLOSED;public: vector<NODE> PATH; bool FindPath(POINT StartPos,POINT EndPos);};
astar.cpp:
void AStar::ConstructPath(NODE n){ NODE nTemp=n; vector<NODE> AltPath; while (nTemp.ID != -1) { PATH.push_back(nTemp); nTemp=GetNodeFromCLOSED(nTemp.Parent); } PATH.push_back(nTemp); for (int i=(int)PATH.size()-1;i>=0;i--) { AltPath.push_back(PATH[i]); } PATH=AltPath;}bool AStar::IsInOPEN(NODE n){ for (UINT i=0;i<OPEN.size();i++) { if ((OPEN[i].x == n.x) && (OPEN[i].y == n.y)) return true; } return false;}bool AStar::IsInCLOSED(NODE n){ for(UINT i=0;i<CLOSED.size();i++) { if ((CLOSED[i].x == n.x) && (CLOSED[i].y == n.y)) return true; } return false;}void AStar::RemoveFromOPEN(NODE n){ for (UINT i=0;i<OPEN.size();i++) { if ((OPEN[i].x == n.x) && (OPEN[i].y == n.y)) { OPEN.erase(OPEN.begin()+i); return; } }}void AStar::GenerateSucc(int x,int y,int Parent){ if ((x<0) || (y<0)) return; if ((x>=MAP_X) ||(y>=MAP_Y)) return; if(Map.IsSolid(x,y)) return; NODE n; n.x=x; n.y=y; n.ID=cID++; n.Parent=Parent; n.f = abs(nGoal.x - n.x) + abs(nGoal.y - n.y); if ((!IsInOPEN(n)) && (!IsInCLOSED(n))) OPEN.push_back(n);}void AStar::GenerateSuccs(NODE n){ GenerateSucc(n.x ,n.y-1,n.ID); // Up GenerateSucc(n.x-1,n.y ,n.ID); // Left GenerateSucc(n.x+1,n.y ,n.ID); // Right GenerateSucc(n.x ,n.y+1,n.ID); // Down RemoveFromOPEN(n); CLOSED.push_back(n);}NODE AStar::GetNodeFromCLOSED(int ID){ for (UINT i=0;i<CLOSED.size();i++) if (CLOSED[i].ID == ID) return CLOSED[i]; return CLOSED[0]; // to silence the warning}NODE AStar::GetBestNode(){ long f=OPEN[0].f; UINT ArrPos=0; for (UINT i=1;i<OPEN.size();i++) { if(OPEN[i].f<f) { //if (OPEN.f<=f) {<br></font><br> f=OPEN[<font color=purple>i</font>].f;<br> ArrPos=i;<br> }<br> }<br> <font color=blue>return</font> OPEN[<font color=purple>ArrPos</font>];<br>}<br><font color=blue>bool</font> AStar::FindPath(POINT StartPos,POINT EndPos)<br>{<br> PATH.clear();<br> OPEN.clear();<br> CLOSED.clear();<br> cID=0;<br> <font color=gray>// – Search initialization —————<br></font><br> NODE n;<br> n.f=n.Parent=n.ID=-1;<br> n.x=(<font color=blue>char</font>)StartPos.x;<br> n.y=(<font color=blue>char</font>)StartPos.y;<br> nStart=n;<br> n.f=n.Parent=n.ID=0;<br> n.x= (<font color=blue>char</font>)EndPos.x;<br> n.y= (<font color=blue>char</font>)EndPos.y;<br> nGoal=n;<br><br> OPEN.push_back(nStart);<br> <font color=gray>// – Pathfinding ————————-<br></font><br> <font color=blue>while</font> (!OPEN.empty()) {<br> n=GetBestNode();<br> <font color=blue>if</font> ((n.x==nGoal.x) && (n.y==nGoal.y))<br> <font color=blue>break</font>;<br> GenerateSuccs(n);<br> }<br><br> <font color=blue>if</font> (OPEN.empty())<br> <font color=blue>return</font> <font color=blue>false</font>;<br> <font color=blue>else</font> {<br> ConstructPath(n);<br> PATH.erase(PATH.begin());<br> <font color=blue>return</font> <font color=blue>true</font>;<br> }<br>}<br></pre><!–ENDSCRIPT–><br><br>Thanks for everyone''s input so far.