Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DarkRift;
  7.  
  8. namespace WorldOfTitansPlugin.Mapping {
  9.     class PathFinding {
  10.  
  11.         public enum dir {
  12.             start,
  13.             none,
  14.             up,
  15.             down,
  16.             left,
  17.             right,
  18.             upright,
  19.             upleft,
  20.             downright,
  21.             downleft
  22.         }
  23.  
  24.         public static List<Node> pathFind(Node[,] nodeMap, int enemyX, int enemyY, int playerX, int playerY) {
  25.  
  26.             int size = nodeMap.GetLength(0);
  27.  
  28.             Node start = nodeMap[enemyY, enemyX];
  29.             Node end = nodeMap[playerY, playerX];
  30.  
  31.             List<Node> openSet = new List<Node>();
  32.             List<Node> closedSet = new List<Node>();
  33.  
  34.             Node current = null;
  35.             List<Node> Vertexes = new List<Node>();
  36.             int count = 0;
  37.             dir lastDir = dir.start;
  38.             Node tempNode = null;
  39.  
  40.             openSet.Add(start);
  41.  
  42.             while (openSet.Count > 0) {
  43.                 //Keep going
  44.  
  45.                 int winner = 0;
  46.                 for (int ndx = 0; ndx < openSet.Count; ndx++) {
  47.                     if (openSet[0].f < openSet[winner].f) {
  48.                         winner = ndx;
  49.                     }
  50.                 }
  51.  
  52.                 //Error after this
  53.  
  54.                 current = openSet[winner];
  55.                
  56.                 if (current == end) {
  57.  
  58.                     int tempC = 0;
  59.  
  60.                     tempNode = current;
  61.                     while (tempNode.previous != null) {
  62.                         tempC++;
  63.                         dir currentDir = getDirection(tempNode, tempNode.previous);
  64.  
  65.                         if (lastDir == dir.start || currentDir != lastDir) {
  66.                             Vertexes.Add(tempNode);
  67.  
  68.                             lastDir = currentDir;
  69.                         }
  70.  
  71.                         tempNode = tempNode.previous;
  72.                         count++;
  73.                     }
  74.  
  75.                     Vertexes.Reverse();
  76.  
  77.                     foreach (Node node in nodeMap) {
  78.                         node.f = 0;
  79.                         node.h = 0;
  80.                         node.g = 0;
  81.                         node.previous = null;
  82.                     }
  83.  
  84.  
  85.  
  86.                     return Vertexes;
  87.                 }
  88.  
  89.                 openSet.Remove(current);
  90.                 closedSet.Add(current);
  91.  
  92.                 List<Node> neighbors = current.neighbors;
  93.  
  94.                 foreach (Node neighbor in neighbors) {
  95.  
  96.                     if (closedSet.Contains(neighbor) || neighbor == null) {
  97.                         continue;
  98.                     }
  99.  
  100.                     float tempG = current.g + 1;
  101.  
  102.                     if (!openSet.Contains(neighbor)) {
  103.                         openSet.Add(neighbor);
  104.                     }
  105.  
  106.                     else if (tempG >= neighbor.g) {
  107.                         continue;
  108.                     }
  109.  
  110.                     neighbor.g = tempG;
  111.                     neighbor.h = heuristic(neighbor, end);
  112.                     neighbor.f = neighbor.g + neighbor.h;
  113.  
  114.                     neighbor.previous = current;
  115.                 }
  116.             }
  117.  
  118.             //No soln
  119.  
  120.             tempNode = current;
  121.             while (tempNode.previous != null) {
  122.                 dir currentDir = getDirection(tempNode, tempNode.previous);
  123.  
  124.                 if (lastDir == dir.start || currentDir != lastDir) {
  125.                     Vertexes.Add(tempNode);
  126.  
  127.                     lastDir = currentDir;
  128.                 }
  129.  
  130.                 tempNode = tempNode.previous;
  131.                 count++;
  132.             }
  133.  
  134.             foreach (Node node in nodeMap) {
  135.                 node.f = 0;
  136.                 node.h = 0;
  137.                 node.g = 0;
  138.                 node.previous = null;
  139.             }
  140.  
  141.             Vertexes.Reverse();
  142.  
  143.             return null;
  144.         }
  145.  
  146.         public static float heuristic(Node a, Node b) {
  147.             float x1 = a.i;
  148.             float y1 = a.j;
  149.             float x2 = b.i;
  150.             float y2 = b.j;
  151.  
  152.             float d = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  153.  
  154.             return d;
  155.         }
  156.  
  157.         public static dir getDirection(Node from, Node to) {
  158.  
  159.             switch (to.i - from.i) {
  160.                 case -1:
  161.                     //Left
  162.                     switch (to.j - from.j) {
  163.                         case -1:
  164.                             //Up-Left
  165.                             return dir.upleft;
  166.                         case 0:
  167.                             //Middle-Left
  168.                             return dir.left;
  169.                         case 1:
  170.                             //Down-Left
  171.                             return dir.downleft;
  172.                     }
  173.  
  174.                     break;
  175.                 case 0:
  176.                     //Same
  177.                     switch (to.j - from.j) {
  178.                         case -1:
  179.                             //Up
  180.                             return dir.up;
  181.                         case 1:
  182.                             //Down
  183.                             return dir.down;
  184.                     }
  185.  
  186.                     break;
  187.                 case 1:
  188.                     //Right
  189.                     switch (to.j - from.j) {
  190.                         case -1:
  191.                             //Up-Right
  192.                             return dir.upright;
  193.                         case 0:
  194.                             //Right
  195.                             return dir.left;
  196.                         case 1:
  197.                             //Down-Right
  198.                             return dir.downright;
  199.                     }
  200.  
  201.                     break;
  202.             }
  203.  
  204.             return dir.none;
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement