private static SearchNode FindPathReversed(Data.System start, Data.System end, int MaxJumpCount = 0) { SearchNode startNode = new SearchNode(start, 0, 0, null); MinHeap openList = new MinHeap(); openList.Add(startNode); bool[] brWorld = new bool[GlobalData.Systems.Count - 1]; brWorld[GlobalData.Systems.FindIndex(a => a == start)] = true; int Jumps = 0; while (openList.HasNext()) { if (MaxJumpCount > 0 && Jumps >= MaxJumpCount) { return(null); } SearchNode current = openList.ExtractFirst(); if ((current.System.Coordinates - end.Coordinates).Length <= 3) { return(new SearchNode(end, current.pathCost + 1, current.cost + 1, current)); } var surroundings = surrounding(current.System); for (int i = 0; i < surroundings.Length; i++) { Surr surr = surroundings[i]; int brWorldIdx = GlobalData.Systems.FindIndex(a => a == surr.System); if (brWorld[brWorldIdx] == false) { brWorld[brWorldIdx] = true; int pathCost = current.pathCost + surr.Cost; int cost = pathCost + (int)(surr.System.Coordinates - end.Coordinates).Length; SearchNode node = new SearchNode(surr.System, cost, pathCost, current); openList.Add(node); Jumps++; } } } return(null); //no path found }
/// <summary> /// Method that switfly finds the best path from start to end. Doesn't reverse outcome /// </summary> /// <returns>The end breadcrump where each .next is a step back)</returns> private static SearchNode FindPathReversed(World world, Point3D start, Point3D end) { SearchNode startNode = new SearchNode(start, 0, 0, null); MinHeap openList = new MinHeap(); openList.Add(startNode); int sx = world.Right; int sy = world.Top; int sz = world.Back; bool[] brWorld = new bool[sx * sy * sz]; brWorld[start.X + (start.Y + start.Z * sy) * sx] = true; while (openList.HasNext()) { SearchNode current = openList.ExtractFirst(); if (current.position.GetDistanceSquared(end) <= 3) { return(new SearchNode(end, current.pathCost + 1, current.cost + 1, current)); } for (int i = 0; i < surrounding.Length; i++) { Surr surr = surrounding[i]; Point3D tmp = new Point3D(current.position, surr.Point); int brWorldIdx = tmp.X + (tmp.Y + tmp.Z * sy) * sx; if (world.PositionIsFree(tmp) && brWorld[brWorldIdx] == false) { brWorld[brWorldIdx] = true; int pathCost = current.pathCost + surr.Cost; int cost = pathCost + tmp.GetDistanceSquared(end); SearchNode node = new SearchNode(tmp, cost, pathCost, current); openList.Add(node); } } } return(null); //no path found }