public void ReloadPathfinding(int[,] map,Point start, Point end) { Map = new StarNode[map.GetLength(0), map.GetLength(1)]; for (int x = 0; x < map.GetLength(0); x++) for (int y = 0; y < map.GetLength(1); y++) { Map[x, y] = new StarNode(new Point(x, y)); Map[x, y].type = map[x, y]; } Start = start; curr = new StarNode(start); End = end; ValidPath = false; IsComplete = false; OpenSet = new List<StarNode>(); ClosedSet = new List<StarNode>(); CurNodeNeighbors = new List<StarNode>(); CompleatedPath = new List<StarNode>(); //StepFinding(); RunPathfinding(); }
private void RunPathfinding() { curr.G = 0; curr.H = HcostEstimate(curr.Location,End); OpenSet.Add(curr); while (OpenSet.Count != 0) { curr = GetLowestInList(OpenSet); if (curr.Location.X == End.X && curr.Location.Y == End.Y) { CompleatedPath = ConstructPath(curr); LargestGScore = MaxGscore(ClosedSet); IsComplete = true; ValidPath = true; break; } OpenSet.Remove(curr); ClosedSet.Add(curr); GetNeighborstonode(curr, CurNodeNeighbors); for (int i = 0; i < CurNodeNeighbors.Count; i++) { if (ClosedSet.Contains(CurNodeNeighbors[i])) continue; double tentative_g_score = curr.G + DistBetween(curr.Location, CurNodeNeighbors[i].Location); if (!OpenSet.Contains(CurNodeNeighbors[i])) { OpenSet.Add(CurNodeNeighbors[i]); } else if (tentative_g_score >= CurNodeNeighbors[i].G) { continue; } CurNodeNeighbors[i].Previous = curr; CurNodeNeighbors[i].H = HcostEstimate(CurNodeNeighbors[i].Location, End); CurNodeNeighbors[i].G = tentative_g_score; } CurNodeNeighbors.Clear(); } if (OpenSet.Count == 0) { IsComplete = true; ValidPath = false; } }
private void GetNeighborstonode(StarNode curr, List<StarNode> Nnodes) { // Retrieve 8 in a square for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) continue; if (curr.Location.X + x > -1 && curr.Location.X + x < Map.GetLength(0) && curr.Location.Y + y > -1 && curr.Location.Y + y < Map.GetLength(1)) if ((Map[curr.Location.X + x, curr.Location.Y + y].type != 1)) { if (/*!OpenSet.Contains(Map[curr.Location.X + x, curr.Location.Y + y]) &&*/ !ClosedSet.Contains(Map[curr.Location.X + x, curr.Location.Y + y])) { if (Map[curr.Location.X + x, curr.Location.Y].type != 1 && Map[curr.Location.X, curr.Location.Y + y].type != 1) { Nnodes.Add(Map[curr.Location.X + x, curr.Location.Y + y]); } } else continue; } } }
private List<StarNode> ConstructPath(StarNode curr) { List<StarNode> final = new List<StarNode>(); final.Add(curr); while (curr.Previous != null) { curr = curr.Previous; final.Add(curr); } return final; }
public void StepFinding(Point start, Point end, int[,]map) { if (Isrun == false) { Map = new StarNode[map.GetLength(0), map.GetLength(1)]; for (int x = 0; x < map.GetLength(0); x++) for (int y = 0; y < map.GetLength(1); y++) { Map[x, y] = new StarNode(new Point(x, y)); Map[x, y].type = map[x, y]; } Start = start; curr = new StarNode(start); End = end; OpenSet.Clear(); ClosedSet.Clear(); CurNodeNeighbors.Clear(); CompleatedPath.Clear(); curr.G = 0; curr.H = HcostEstimate(curr.Location, End); OpenSet.Add(curr); Isrun = true; } if (OpenSet.Count != 0) { curr = GetLowestInList(OpenSet); if (curr.Location.X == End.X && curr.Location.Y == End.Y) { CompleatedPath = ConstructPath(curr); //LargestGScore = MaxGscore(ClosedSet); IsComplete = true; ValidPath = true; Isrun = false; return; } OpenSet.Remove(curr); ClosedSet.Add(curr); GetNeighborstonode(curr, CurNodeNeighbors); for (int i = 0; i < CurNodeNeighbors.Count; i++) { if (ClosedSet.Contains(CurNodeNeighbors[i])) continue; double tentative_g_score = curr.G + DistBetween(curr.Location, CurNodeNeighbors[i].Location); if (!OpenSet.Contains(CurNodeNeighbors[i])) { OpenSet.Add(CurNodeNeighbors[i]); } else if (tentative_g_score >= CurNodeNeighbors[i].G) { continue; } CurNodeNeighbors[i].Previous = curr; CurNodeNeighbors[i].H = HcostEstimate(CurNodeNeighbors[i].Location, End); CurNodeNeighbors[i].G = tentative_g_score; } CurNodeNeighbors.Clear(); } if (OpenSet.Count == 0) { IsComplete = true; ValidPath = false; } }