Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DarkRift;
- namespace WorldOfTitansPlugin.Mapping {
- class PathFinding {
- public enum dir {
- start,
- none,
- up,
- down,
- left,
- right,
- upright,
- upleft,
- downright,
- downleft
- }
- public static List<Node> pathFind(Node[,] nodeMap, int enemyX, int enemyY, int playerX, int playerY) {
- int size = nodeMap.GetLength(0);
- Node start = nodeMap[enemyY, enemyX];
- Node end = nodeMap[playerY, playerX];
- List<Node> openSet = new List<Node>();
- List<Node> closedSet = new List<Node>();
- Node current = null;
- List<Node> Vertexes = new List<Node>();
- int count = 0;
- dir lastDir = dir.start;
- Node tempNode = null;
- openSet.Add(start);
- while (openSet.Count > 0) {
- //Keep going
- int winner = 0;
- for (int ndx = 0; ndx < openSet.Count; ndx++) {
- if (openSet[0].f < openSet[winner].f) {
- winner = ndx;
- }
- }
- //Error after this
- current = openSet[winner];
- if (current == end) {
- int tempC = 0;
- tempNode = current;
- while (tempNode.previous != null) {
- tempC++;
- dir currentDir = getDirection(tempNode, tempNode.previous);
- if (lastDir == dir.start || currentDir != lastDir) {
- Vertexes.Add(tempNode);
- lastDir = currentDir;
- }
- tempNode = tempNode.previous;
- count++;
- }
- Vertexes.Reverse();
- foreach (Node node in nodeMap) {
- node.f = 0;
- node.h = 0;
- node.g = 0;
- node.previous = null;
- }
- return Vertexes;
- }
- openSet.Remove(current);
- closedSet.Add(current);
- List<Node> neighbors = current.neighbors;
- foreach (Node neighbor in neighbors) {
- if (closedSet.Contains(neighbor) || neighbor == null) {
- continue;
- }
- float tempG = current.g + 1;
- if (!openSet.Contains(neighbor)) {
- openSet.Add(neighbor);
- }
- else if (tempG >= neighbor.g) {
- continue;
- }
- neighbor.g = tempG;
- neighbor.h = heuristic(neighbor, end);
- neighbor.f = neighbor.g + neighbor.h;
- neighbor.previous = current;
- }
- }
- //No soln
- tempNode = current;
- while (tempNode.previous != null) {
- dir currentDir = getDirection(tempNode, tempNode.previous);
- if (lastDir == dir.start || currentDir != lastDir) {
- Vertexes.Add(tempNode);
- lastDir = currentDir;
- }
- tempNode = tempNode.previous;
- count++;
- }
- foreach (Node node in nodeMap) {
- node.f = 0;
- node.h = 0;
- node.g = 0;
- node.previous = null;
- }
- Vertexes.Reverse();
- return null;
- }
- public static float heuristic(Node a, Node b) {
- float x1 = a.i;
- float y1 = a.j;
- float x2 = b.i;
- float y2 = b.j;
- float d = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
- return d;
- }
- public static dir getDirection(Node from, Node to) {
- switch (to.i - from.i) {
- case -1:
- //Left
- switch (to.j - from.j) {
- case -1:
- //Up-Left
- return dir.upleft;
- case 0:
- //Middle-Left
- return dir.left;
- case 1:
- //Down-Left
- return dir.downleft;
- }
- break;
- case 0:
- //Same
- switch (to.j - from.j) {
- case -1:
- //Up
- return dir.up;
- case 1:
- //Down
- return dir.down;
- }
- break;
- case 1:
- //Right
- switch (to.j - from.j) {
- case -1:
- //Up-Right
- return dir.upright;
- case 0:
- //Right
- return dir.left;
- case 1:
- //Down-Right
- return dir.downright;
- }
- break;
- }
- return dir.none;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement