void astar_PathFound(Path path, double MillisecondsNeeded) { if (MillisecondsNeeded >= 0) { pathSearching = false; this.path = path; pathfound = true; pathPosition = 0; //DebugManager.AddItem("Found Path", myNumber.ToString() + " Enemy, Type: " + eType, new System.Diagnostics.StackTrace()); } else { DebugManager.AddItem("Invalid Path, Error:" +MillisecondsNeeded, myNumber.ToString() + " Enemy, Type: " + eType, new System.Diagnostics.StackTrace(), System.Drawing.Color.Yellow); pathSearching = false; } timeFromLastSearch = 0; }
public AStar() { openLinkedList = new LinkedList<PathNode>(); path = new Path(); //openList = new List<PathNode>(); newOpenList = new List<PathNode>(); myThread = new Thread(new ThreadStart(FindWay)); myThread.Priority = ThreadPriority.Lowest; threadNumber++; myThread.Name = "Enemy Thread #" + threadNumber.ToString(); PathFound += new PathFoundEventHandler(AStar_PathFound); searchMap = new PathNode[AStarMap.Mapnodes.GetLength(0), AStarMap.Mapnodes.GetLength(1)]; for (int i = 0; i < AStarMap.Mapnodes.GetLength(0); i++) { for (int k = 0; k < AStarMap.Mapnodes.GetLength(1); k++) { Node node = AStarMap.Mapnodes[i,k]; searchMap[i, k] = new PathNode(node.MapXPosition, node.MapYPosition, node.Walkable); } } }
private void FindWay() { while (!stop) { if (aim.Walkable == Walkable.Walkable && start.Walkable == Walkable.Walkable) { PathNode aimNode; PathNode currentNode; searching = true; //searchMap = MapToArray(); starttime = DateTime.Now; List<PathNode> similarNodes = new List<PathNode>(); MapToArray(ref searchMap); //openList.Add(start.ToPathNode()); openLinkedList.AddFirst(start.ToPathNode()); PathNodeComparer comparer = new PathNodeComparer(); aimNode = searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition]; while (openLinkedList.Count > 0 && searching && !abort) { if (openLinkedList.First.Value == aimNode) searching = false; if (searching) { currentNode = openLinkedList.First.Value; try { // openList.Remove(openList[0]); // openList.Sort(comparer); openLinkedList.RemoveFirst(); } catch (Exception) { abort = true; //throw; } try { while (openLinkedList.First.Value.FCost == currentNode.FCost) { similarNodes.Add(openLinkedList.First.Value); openLinkedList.RemoveFirst(); } } catch (Exception) { } if (currentNode.State != NodeState.Closed && searching) //ExploreNode(openList[0], searchMap); ExploreNode(currentNode, searchMap); for (int i = 0; i < similarNodes.Count; i++) { ExploreNode(similarNodes[i], searchMap); } similarNodes.Clear(); //openList.AddRange(newOpenList); //if (openList.Contains(aimNode)) // if (aimNode.State == NodeState.Closed) // searching = false; if (openLinkedList.Contains(aimNode)) if (aimNode.State == NodeState.Closed) searching = false; //myThread.Suspend(); } } if (!searching) { PathNode lastNode; LinkedList<PathNode> pathnodes = new LinkedList<PathNode>(); pathnodes.AddLast(searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition]); lastNode = searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition]; while (lastNode.RootNode != null) { pathnodes.AddBefore(pathnodes.First, lastNode.RootNode); lastNode = lastNode.RootNode; } path = new Path(pathnodes.ToList()); PathFound(path, (DateTime.Now - starttime).TotalMilliseconds); } else PathFound(path, -2); } else PathFound(path, -1); if (!stop) myThread.Suspend(); } }
void AStar_PathFound(Path path,double ml) { //throw new NotImplementedException(); }