bool Player::GetPath(vector<Point> & OutPath,Point Start, Point End, int MaxRange){ OutPath.clear(); if (Start == End){ OutPath.push_back(Start); return true; } Array2d<bool> HasPassed(false); struct Path{ Point P; Path * LastPath; Path(){ LastPath = NULL; } Path(Point InP, Path * InLastPath){ P = InP; LastPath = InLastPath; }; void PushPath(vector<Point> & OutPath){ if (LastPath != NULL) LastPath->PushPath(OutPath); OutPath.push_back(P); } }; vector<vector<Path>> Paths(MaxRange); //sets the original point in the vector Paths[0].push_back(Path(Start, NULL)); for (int r = 0; r < MaxRange - 1; r++){ vector<Path> & PrevPath = Paths[r]; vector<Path> & CurPath = Paths[r+1]; for (Path & PPath : PrevPath){ for (Point NewP:SquareIterate(PPath.P, 1)){ //not necessary, but possibly a more efficient alternative to the bool array method //bool IsFurthurFromDest = GetBoardDistance(NewP, Start) >= GetBoardDistance(PPath.P, Start); //bool IsCloserToEnd = GetBoardDistance(NewP, End) <= GetBoardDistance(PPath.P, End); if (!HasPassed[NewP] && BlankPoint(NewP)){// && (IsFurthurFromDest || IsCloserToEnd)){ HasPassed[NewP] = true; CurPath.push_back(Path(NewP, &PPath)); if (NewP == End){ Path NewPath = Path(NewP, &PPath); NewPath.PushPath(OutPath); return true; } } } } } return false; }
void PushPath(vector<Point> & OutPath){ if (LastPath != NULL) LastPath->PushPath(OutPath); OutPath.push_back(P); }