private Stack<Node> GetPath(Node target, IBot bot) { //bot.Connection.Send(bot.Room.WorldKey, 0, target.x, target.y, 9); //System.Threading.Thread.Sleep(10); //room.DrawSquare(target.x, target.y, System.Drawing.Color.Black); Stack<Node> result = new Stack<Node>(); Node current = target; result.Push(current); while (current.Mother != null) { /*if (current.F >= wallCost) result.Clear();*/ result.Push(current.Mother); current = current.Mother; } return result; }
public Stack<Node> FindPath(int xs, int ys, int xt, int yt, ICollection<Zombie> zombies, IBot bot) { int maxSearch = 2560; int currentSearch = 0; bool isStartingSquare = true; isOpen = new bool[bot.Room.Width, bot.Room.Height]; isClosed = new bool[bot.Room.Width, bot.Room.Height]; gValues = new int[bot.Room.Width, bot.Room.Height]; debug = new Queue<Node>(); open = new PathHeap(); search = new LowHPathHeap(); //open = new SortedSet<Node>(); //closed = new HashSet<Node>(); Node start = new Node(xs, ys); start.H = Heuristic(xs, ys, xt, yt); Node target = new Node(xt, yt); isOpen[start.x, start.y] = true; open.Add(start); search.Add(start); while (!open.IsEmpty() && currentSearch < maxSearch) //while (open.Count > 0) { Node current = open.GetRemoveFirst(); //Node current = open.First(); //open.Remove(current); isOpen[current.x, current.y] = false; isClosed[current.x, current.y] = true; if (current.Equals(target)) return GetPath(current, bot); for (int i = 4; i < 8; i++) { int x = adjacentNodes[i].x + current.x; int y = adjacentNodes[i].y + current.y; //Console.WriteLine("X:" + x + " Y:" + y + " " + room.GetMapBlock(x, y)); int id = bot.Room.BlockMap.getForegroundBlockIdFast(x, y); if (x >= 0 && x <= bot.Room.Width - 1 && y >= 0 && y <= bot.Room.Height - 1 && (MasterDig.isDigable(id) || MasterDig.isDug(id)) && distanceFromCenter(x, y, bot) >= 20 && (isStartingSquare ? !isZombie(x, y, zombies) : true)) { int totalAddCost = adjacentNodes[i].cost;// + (room.GetMapBlock(x, y) != 0 ? wallCost : 0); Node baby = new Node(x, y); if (isClosed[baby.x, baby.y]) continue; if (!isOpen[baby.x, baby.y]) { int addg = totalAddCost; baby.G = current.G + addg; gValues[baby.x, baby.y] = baby.G; baby.H = Heuristic(baby.x, baby.y, target.x, target.y); baby.Mother = current; open.Add(baby); //bot.Connection.Send(bot.Room.WorldKey, 0, baby.x, baby.y, 9); //System.Threading.Thread.Sleep(10); isOpen[baby.x, baby.y] = true; //debug.Enqueue(baby); search.Add(baby); ++currentSearch; } else { int addg = totalAddCost; if (current.G + addg < gValues[baby.x, baby.y]) { //Path to that Node is better, switch parents! baby.Mother = current; } } } } isStartingSquare = false; } //Target was not found, use closest search node if (!search.IsEmpty()) return GetPath(search.GetRemoveFirst(), bot); return null; }