public bool isMatch(Node n) { if (n!=null) return (x==n.x && y==n.y); else return false; }
public Node(Node parentNode, Node goalNode, int gCost, ushort x, ushort y, Conquer_Online_Server.Game.Map Map) { this.Map = Map; this.parentNode = parentNode; this._goalNode = goalNode; this.gCost = gCost; this.x=x; this.y=y; InitNode(); }
public Node(Node parentNode, Node goalNode, int gCost, ushort x, ushort y, PhoenixProject.Game.Map Map) { this.Map = Map; this.parentNode = parentNode; this._goalNode = goalNode; this.gCost = gCost; this.x=x; this.y=y; InitNode(); }
public int IndexOf(Node n) { for (int i =0; i< _list.Count ;i++) { Node nodeInTheList = (Node) _list[i]; if (nodeInTheList.isMatch (n)) return i; } return -1; }
public int push(Node n) { int k = _list.BinarySearch (n,_nodeComparer); if (k==-1) // no element _list.Insert (0,n); else if (k<0) // find location by complement { k=~k; _list.Insert (k,n); } else if (k>=0) _list.Insert (k,n); return k; }
public ArrayList GetSuccessors() { ArrayList successors = new ArrayList (); for (int xd = -1; xd <= 1; xd++) { for (int yd = -1; yd <= 1; yd++) { if (Map.Floor[x + xd, y + yd, Conquer_Online_Server.Game.MapObjectType.Monster, null]) { Node n = new Node(this, this._goalNode, 1,(ushort)(x + xd), (ushort)(y + yd), Map); if (!n.isMatch(this.parentNode) && !n.isMatch(this)) successors.Add(n); } } } return successors; }
public static List<Coordonates> FindWay(ushort myX, ushort myY, ushort toX, ushort toY, Conquer_Online_Server.Game.Map map) { List<Coordonates> SolutionPathList = new List<Coordonates>(); Node node_goal = new Node(null, null, 1, toX, toY, map); Node node_start = new Node(null, node_goal, 1, myX, myY, map); SortedCostNodeList OPEN = new SortedCostNodeList(); SortedCostNodeList CLOSED = new SortedCostNodeList(); OPEN.push(node_start); while (OPEN.Count > 0) { //if (count == 2000) // break; Node node_current = OPEN.pop(); if (node_current.isMatch(node_goal)) { node_goal.parentNode = node_current.parentNode; break; } ArrayList successors = node_current.GetSuccessors(); foreach (Node node_successor in successors) { int oFound = OPEN.IndexOf(node_successor); if (oFound > 0) { Node existing_node = OPEN.NodeAt(oFound); if (existing_node.CompareTo(node_current) <= 0) continue; } int cFound = CLOSED.IndexOf(node_successor); if (cFound > 0) { Node existing_node = CLOSED.NodeAt(cFound); if (existing_node.CompareTo(node_current) <= 0) continue; } if (oFound != -1) OPEN.RemoveAt(oFound); if (cFound != -1) CLOSED.RemoveAt(cFound); OPEN.push(node_successor); } CLOSED.push(node_current); } Node p = node_goal; while (p != null) { SolutionPathList.Add(new Coordonates() { X = p.x, Y = p.y }); p = p.parentNode; } return SolutionPathList; }