Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using AStar;
  6. using AStar.Exceptions;
  7.  
  8. namespace AStar
  9. {
  10.     public class AStarPathFinder
  11.     {
  12.         private int[,] walkableMap;//only 0 and 1 allowed
  13.         private Node[,] checkedNodes;
  14.         public List<Node> finalPath = new List<Node>();
  15.         public bool PathFound = false;
  16.  
  17.         private Node startPos = new Node();
  18.         private Node endPos = new Node();
  19.  
  20.         private int[,] checkOffset = new int[8,2];
  21.  
  22.         private int mapWidth = 0;
  23.         private int mapHeight = 0;
  24.  
  25.         private bool startSet = false;
  26.         private bool endSet = false;
  27.         private bool mapSet = false;
  28.  
  29.         public AStarPathFinder()
  30.         {
  31.             checkOffset[0, 0] = 1; //right
  32.             checkOffset[0, 1] = 0;
  33.  
  34.             checkOffset[1, 0] = 1; //down right
  35.             checkOffset[1, 1] = 1;
  36.  
  37.             checkOffset[2, 0] = 0; //down
  38.             checkOffset[2, 1] = 1;
  39.  
  40.             checkOffset[3, 0] = -1; //down left
  41.             checkOffset[3, 1] = 1;
  42.  
  43.             checkOffset[4, 0] = -1; //left
  44.             checkOffset[4, 1] = 0;
  45.  
  46.             checkOffset[5, 0] = -1; //up left
  47.             checkOffset[5, 1] = -1;
  48.  
  49.             checkOffset[6, 0] = 0; //up
  50.             checkOffset[6, 1] = -1;
  51.            
  52.             checkOffset[7, 0] = 1; //up right
  53.             checkOffset[7, 1] = -1;
  54.         }
  55.  
  56.         public void Add(List<List<int>> walkableMap)
  57.         {
  58.             int[,] map = new int[walkableMap.Count, walkableMap.ElementAt(0).Count];
  59.            
  60.             int i = 0;
  61.             int j = 0;
  62.  
  63.             foreach (List<int> row in walkableMap)
  64.             {
  65.                 foreach (int id in row)
  66.                 {
  67.                     if (id != 0 && id != 1)
  68.                     {
  69.                         throw (new IllegalMapException("Walkable map may only contain 0's and 1's"));
  70.                     }
  71.                     else
  72.                     {
  73.                         map[i, j] = id;
  74.                     }
  75.                     i++;
  76.                 }
  77.                 i = 0;
  78.                 j++;
  79.             }
  80.            
  81.             mapWidth = walkableMap.Count;
  82.             mapHeight = walkableMap.ElementAt(0).Count;
  83.            
  84.             checkedNodes = new Node[walkableMap.Count, walkableMap.ElementAt(0).Count];
  85.             mapSet = true;
  86.  
  87.             this.walkableMap = map;
  88.         }
  89.  
  90.         public void SetStartingPosition(Node node)
  91.         {
  92.             startPos = node;
  93.             startSet = true;
  94.         }
  95.         public void SetStartingPosition(int x, int y)
  96.         {
  97.             startPos = new Node(x, y);
  98.             startSet = true;
  99.         }
  100.  
  101.         public void SetEndingPosition(Node node)
  102.         {
  103.             endPos = node;
  104.             endSet = true;
  105.         }
  106.         public void SetEndingPosition(int x, int y)
  107.         {
  108.             endPos = new Node(x, y);
  109.             endSet = true;
  110.         }
  111.  
  112.         public void searchPath()
  113.         {
  114.             if (startSet && endSet && mapSet)
  115.             {
  116.                 int curX = startPos.x;
  117.                 int curY = startPos.y;
  118.  
  119.                 bool pathFound = false;
  120.                 while (!pathFound)
  121.                 {
  122.                     if (curX == endPos.x && curY == endPos.y)
  123.                     {
  124.                         pathFound = true;
  125.                         break;
  126.                     }
  127.  
  128.                     int closestX = 0;
  129.                     int closestY = 0;
  130.                     double closestDistance = 9999999;
  131.  
  132.                     int newX = 0;
  133.                     int newY = 0;
  134.  
  135.                     for (int i = 0; i < 7; i++)
  136.                     {
  137.                         int xOffset = checkOffset[i, 0];
  138.                         int yOffset = checkOffset[i, 1];
  139.  
  140.                         if (curX + xOffset >= 0 && curX + xOffset <= mapWidth && curY + yOffset >= 0 && curY + yOffset <= mapHeight)
  141.                         {
  142.                             if (checkedNodes[curX + xOffset, curY + yOffset] == null)
  143.                             {
  144.                                 Node n = new Node();
  145.                                 n.x = curX + xOffset;
  146.                                 n.y = curY + yOffset;
  147.  
  148.                                 int w = 0;//width length
  149.                                 int h = 0;//height length
  150.  
  151.                                 //make sure no negative values are used
  152.                                 if (endPos.x - n.x >= 0)
  153.                                 {
  154.                                     w = endPos.x - n.x;
  155.                                 }
  156.                                 else
  157.                                 {
  158.                                     w = n.x - endPos.x;
  159.                                 }
  160.  
  161.                                 if (endPos.y - n.y >= 0)
  162.                                 {
  163.                                     h = endPos.y - n.y;
  164.                                 }
  165.                                 else
  166.                                 {
  167.                                     h = n.y - endPos.y;
  168.                                 }
  169.                                 //calculate the scores
  170.  
  171.                                 n.h = w + h;
  172.                                 n.g = Math.Sqrt(w * w + h * h);
  173.                                 n.f = n.g + n.h;
  174.  
  175.                                 if (closestDistance > n.f)
  176.                                 {
  177.                                     closestDistance = n.f;
  178.                                     closestX = n.x;
  179.                                     closestY = n.y;
  180.                                    
  181.                                     newX = closestX;
  182.                                     newY = closestY;
  183.  
  184.                                     n.setDirectionForOffset(xOffset, yOffset);
  185.                                 }
  186.                                 checkedNodes[curX + xOffset, curY + yOffset] = n;
  187.                             }
  188.                         }
  189.                     }
  190.  
  191.                     curX = newX;
  192.                     curY = newY;
  193.  
  194.                 }
  195.             }
  196.             else
  197.             {
  198.                 throw (new IllegalArgumentException("Not enough data to search a path."));
  199.             }
  200.  
  201.             //get the actual path
  202.  
  203.             bool finalPathFound = false;
  204.             finalPath = new List<Node>();
  205.             //finalPath.Add(checkedNodes[endPos.x, endPos.y]);
  206.  
  207.             int myX = endPos.x;
  208.             int myY = endPos.y;
  209.  
  210.             Console.WriteLine("starting");
  211.  
  212.             while (!finalPathFound)
  213.             {
  214.                 if(myX == startPos.x && myY == startPos.y)
  215.                 {
  216.                     finalPathFound = true;
  217.                     break;
  218.                 }
  219.  
  220.                 int [] offset = checkedNodes[myX, myY].getOffsetForDirection();
  221.                 if (myX + offset[0] >= 0 && myX + offset[0] <= mapWidth && myY + offset[1] >= 0 && myY + offset[1] <= mapHeight)
  222.                 {
  223.                     myX += offset[0];
  224.                     myY += offset[1];
  225.  
  226.                     finalPath.Add(checkedNodes[myX + offset[0], myY + offset[1]]);
  227.                 }
  228.             }
  229.  
  230.             Console.WriteLine("done");
  231.  
  232.             PathFound = true;
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement