public Node(Vector2 position, Node parent, Vector2 target) { this.position = position; this.parent = parent; if (parent == null) this.g = 0; else this.g = parent.g + 1; Vector2 difference = target - position; this.h = (int)(Math.Abs(difference.X) + Math.Abs(difference.Y)); this.f = this.g + this.h; }
public static List<Vector2> createPath(List<Tile> map, Vector2 start, Vector2 end) { List<Node> openList, closedList; openList = new List<Node>(); closedList = new List<Node>(); Node startNode = new Node(start, null, end); foreach (Tile tile in map) { if (!tile.walkable && tile.position == end) return new List<Vector2>(); if (tile.walkable && Vector2.Distance(tile.position, start) == 1) openList.Add(new Node(tile.position, startNode, end)); } closedList.Add(startNode); while (listContains(closedList, end) == null) { Node selected = lowestF(openList, closedList); if (openList.Count == 0) break; foreach (Tile tile in map) { //Adjacent node if (tile.walkable && Vector2.Distance(tile.position, selected.position) == 1) { Node adjacentInOpenList; if ((adjacentInOpenList = listContains(openList, tile.position)) != null) if (adjacentInOpenList.g < selected.g) { openList.Remove(adjacentInOpenList); adjacentInOpenList = new Node(adjacentInOpenList.position, selected, end); openList.Add(adjacentInOpenList); } //Not in the closed or open list, so add it if (listContains(openList, tile.position) == null && listContains(closedList, tile.position) == null) openList.Add(new Node(tile.position, selected, end)); } } } List<Vector2> path = new List<Vector2>(); if (openList.Count == 0) return path; Node tempNode = listContains(closedList, end); path.Add(tempNode.position); while (tempNode != null) //The startNode's parent is null { path.Add(tempNode.position); tempNode = tempNode.parent; } path.Reverse(); return path; }
static Node lowestF(List<Node> openList, List<Node> closedList) { Node ret = new Node(Vector2.Zero, null, Vector2.Zero); ret.f = int.MaxValue; foreach (Node node in openList) if (node.f < ret.f) ret = node; if (ret.f == int.MaxValue) ret = null; else { openList.Remove(ret); closedList.Add(ret); } return ret; }
private void makeMatrix() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Node node = new Node(); //Neu node o phia tren nho hon hoac bang 0 thi gan la true,nguoc lai la false if (i - 1 >= 0) { if (input[i - 1, j] <= 0) { node.setAdjacency(0, 9*(i-1)+j); } else { node.setAdjacency(0, -1); } } //Neu node o phia trai nho hon hoac bang 0 thi gan la true,nguoc lai la false if (j - 1 >= 0) { if (input[i, j - 1] <= 0) { node.setAdjacency(0, 9*i+j-1); } else { node.setAdjacency(0, -1); } } //Neu node o phia duoi nho hon hoac bang 0 thi gan la true,nguoc lai la false if (i + 1 < 9) { if (input[i + 1, j] <= 0) { node.setAdjacency(0, 9*(i+1)+j); } else { node.setAdjacency(0, -1); } } //Neu node o phia phai nho hon hoac bang 0 thi gan la true,nguoc lai la false if (j + 1 < 9) { if (input[i, j + 1] <= 0) { node.setAdjacency(0, 9*i+j+1); } else { node.setAdjacency(0, -1); } } matrix[i, j] = node; } } }