First up a screenshot to make the post look pretty.
Demo is HERE. Just click on a square for the character to find the fastest route there, uncovering the fog as he moves.
Press Alt+F4 to exit.
Give it a test, although should be fine.
Here's the source for the path finding.
(BTW, CVector<> is just a little wrapper for std::vector<> that throws one of my game exceptions on a subscript out of range. Don't want anyone to think I'm writing my own vector class [smile]).
bool CPath::Target(int Ox,int Oy,int Tx,int Ty){ CVector Vc,Vn; Steps.clear(); Pos=0; if(Ox==Tx && Oy==Ty) return true; if(!Open(Tx,Ty)) return false; Grid.Fill(INT_MAX); Grid(Tx,Ty)=0; Vc.push_back(CPoint(Tx,Ty)); int Value=0; bool Found=false; while(Vc.size()) { for(std::vector::iterator I=Vc.begin();I!=Vc.end();++I) { int X=I->X; int Y=I->Y; if(Grid(X,Y)==Value) { if(Y>0 && Open(X,Y-1) && Grid(X,Y-1)==INT_MAX){ Grid(X,Y-1)=Value+1; Vn.push_back(CPoint(X,Y-1)); } if(X1
if(Y1 && Open(X,Y+1) && Grid(X,Y+1)==INT_MAX){ Grid(X,Y+1)=Value+1; Vn.push_back(CPoint(X,Y+1)); }
if(X>0 && Open(X-1,Y) && Grid(X-1,Y)==INT_MAX){ Grid(X-1,Y)=Value+1; Vn.push_back(CPoint(X-1,Y)); }
}
}
Vc.swap(Vn);
Vn.clear();
++Value;
if(Grid(Ox,Oy)!=INT_MAX){ Vc.clear(); Found=true; }
}
if(!Found) return false;
int X=Ox,Y=Oy;
bool Finished=false;
while(!Finished)
{
int Min=INT_MAX;
if(Grid(X-1,Y-1)<=Min && Open(X-1,Y) && Open(X,Y-1)) Min=Grid(X-1,Y-1);
if(Grid(X+1,Y-1)<=Min && Open(X+1,Y) && Open(X,Y-1)) Min=Grid(X+1,Y-1);
if(Grid(X-1,Y+1)<=Min && Open(X-1,Y) && Open(X,Y+1)) Min=Grid(X-1,Y+1);
if(Grid(X+1,Y+1)<=Min && Open(X+1,Y) && Open(X,Y+1)) Min=Grid(X+1,Y+1);
if(Grid(X,Y-1)<=Min) Min=Grid(X,Y-1);
if(Grid(X+1,Y)<=Min) Min=Grid(X+1,Y);
if(Grid(X,Y+1)<=Min) Min=Grid(X,Y+1);
if(Grid(X-1,Y)<=Min) Min=Grid(X-1,Y);
CVector Choices;
if(Grid(X-1,Y-1)==Min) Choices.push_back(CPoint(-1,-1));
if(Grid(X+1,Y-1)==Min) Choices.push_back(CPoint(1,-1));
if(Grid(X-1,Y+1)==Min) Choices.push_back(CPoint(-1,1));
if(Grid(X+1,Y+1)==Min) Choices.push_back(CPoint(1,1));
if(Grid(X,Y-1)==Min) Choices.push_back(CPoint(0,-1));
if(Grid(X+1,Y)==Min) Choices.push_back(CPoint(1,0));
if(Grid(X,Y+1)==Min) Choices.push_back(CPoint(0,1));
if(Grid(X-1,Y)==Min) Choices.push_back(CPoint(-1,0));
CPoint Pt=Choices[std::rand()%Choices.size()];
Steps.push_back(Pt);
X+=Pt.X;
Y+=Pt.Y;
if(X==Tx && Y==Ty) Finished=true;
}
return true;
}
Any comments on the demo appreciated.