Advertisement
Guest User

Untitled

a guest
Apr 15th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace AStar {
  7.     public class AStar {
  8.  
  9.         public AStarNode _nodeStart;
  10.         public AStarNode _nodeGoal;
  11.  
  12.         public List<AStarNode> openList;
  13.         public List<AStarNode> closedList;
  14.         public List<AStarNode> neighbors;
  15.         public List<AStarNode> finalPath;
  16.  
  17.         public AStar() {
  18.             openList = new List<AStarNode>();
  19.             closedList = new List<AStarNode>();
  20.             neighbors = new List<AStarNode>();
  21.             finalPath = new List<AStarNode>();
  22.         }
  23.  
  24.  
  25.         public void FindPath(AStarNode start, AStarNode goal) {
  26.             openList = new List<AStarNode>();
  27.             closedList = new List<AStarNode>();
  28.             neighbors = new List<AStarNode>();
  29.  
  30.             _nodeStart = start;
  31.             _nodeGoal = goal;
  32.    
  33.             openList.Add(start);
  34.             while(openList.Count > 0) {
  35.  
  36.                 openList.Sort(SortNodes);
  37.                 AStarNode currentNode = openList[0];
  38.                 openList.RemoveAt(0);
  39.  
  40.                 if (currentNode.IsEqualTo(goal)) {
  41.                     finalPath = new List<AStarNode>();
  42.                     while(currentNode.parent != null) {
  43.                         finalPath.Insert(0, currentNode);
  44.                         currentNode = currentNode.parent;
  45.                     }
  46.                     break;
  47.            
  48.                 } else {
  49.                     currentNode.GetNeighbors(neighbors);
  50.                     for(int iNB = 0; iNB < neighbors.Count; iNB++) {
  51.                         AStarNode neighbor = neighbors[iNB];
  52.  
  53.                         int existsInOpenList = -1;
  54.                         int existsInClosedList = -1;
  55.  
  56.                         for (int ioi = 0; ioi < openList.Count; ioi++) {
  57.                             if (openList[ioi].IsEqualTo(neighbor)) {
  58.                                 existsInOpenList = ioi;
  59.                                 break;
  60.                             }
  61.                         }
  62.                         for (int ici = 0; ici < closedList.Count; ici++) {
  63.                             if (closedList[ici].IsEqualTo(neighbor)) {
  64.                                 existsInClosedList = ici;
  65.                                 break;
  66.                             }
  67.                         }
  68.  
  69.                         if(existsInOpenList != -1) {
  70.                             if (openList[existsInOpenList].g > (neighbor.g + openList[existsInOpenList].nodeCost)) {
  71.                                 openList[existsInOpenList].g = (neighbor.g + openList[existsInOpenList].nodeCost);
  72.                                 continue;
  73.                             }
  74.                         } else if(existsInClosedList != -1) {
  75.                             if (closedList[existsInClosedList].g > (neighbor.g + closedList[existsInClosedList].nodeCost)) {
  76.                                 closedList[existsInClosedList].g = (neighbor.g + closedList[existsInClosedList].nodeCost);
  77.                                 openList.Add(closedList[existsInClosedList]);
  78.                                 closedList.RemoveAt(existsInClosedList);
  79.                                 continue;
  80.                             }
  81.                         } else {
  82.                             //AddToOpenList(openList, neighbor);
  83.                             openList.Add(neighbor);
  84.                         }
  85.                     }
  86.                 }
  87.                 closedList.Add(currentNode);
  88.             }
  89.         }
  90.  
  91.         public int SortNodes(AStarNode n1, AStarNode n2) {
  92.             if (n1 == null) {
  93.                 if (n2 == null) {
  94.                     return 0;
  95.                 } else {
  96.                     return -1;
  97.                 }
  98.             } else {
  99.                 if (n2 == null) {
  100.                     return 1;
  101.                 } else {
  102.                     if (n1.f == n2.f) return 0;
  103.                     else return ((n1.f > n2.f) ? 1 : -1);
  104.                 }
  105.             }
  106.         }
  107.  
  108.         private void AddToOpenList(List<AStarNode> openList, AStarNode node) {
  109.             int i = 0;
  110.             if (openList.Count < 1) {
  111.                 openList.Add(node);
  112.                 return;
  113.             }
  114.             while (i < openList.Count) {
  115.                 if (openList[i].f > node.f) {
  116.                     openList.Insert(i, node);
  117.                     break;
  118.                 }
  119.                 i++;
  120.             }
  121.         }
  122.     }
  123.  
  124.     public class AStarNode {
  125.  
  126.         public AStarNode parent;
  127.         public AStarNode goal;
  128.  
  129.         public int nodeCost;
  130.         public int g;
  131.         public double h;
  132.         public double f;
  133.  
  134.         public AStarNode(AStarNode parent, AStarNode goal, int parentG, int nodeG) {
  135.             this.parent = parent;
  136.             this.goal = goal;
  137.             this.g = parentG + nodeG;
  138.             this.nodeCost = nodeG;
  139.         }
  140.    
  141.         public virtual void GetNeighbors(List<AStarNode> listNeighbors) { return; }
  142.    
  143.         public virtual void Calculate() {}
  144.    
  145.         public virtual bool IsEqualTo(AStarNode node) { return false; }
  146.     }
  147.  
  148.     public class AStarNode2D : AStarNode{
  149.         public int x;
  150.         public int y;
  151.  
  152.         public AStarNode2D(AStarNode parent, AStarNode goal, int parentG, int nodeG, int x, int y) : base(parent, goal, parentG, nodeG) {
  153.             this.x = x;
  154.             this.y = y;
  155.             Calculate();
  156.         }
  157.  
  158.         public override void GetNeighbors(List<AStarNode> listNeighbors) {
  159.             listNeighbors.Clear();
  160.             AddNeighbor(listNeighbors, x-1, y  );
  161.             AddNeighbor(listNeighbors, x-1, y-1);
  162.             AddNeighbor(listNeighbors, x  , y-1);
  163.             AddNeighbor(listNeighbors, x+1, y-1);
  164.             AddNeighbor(listNeighbors, x+1, y  );
  165.             AddNeighbor(listNeighbors, x+1, y+1);
  166.             AddNeighbor(listNeighbors, x  , y+1);
  167.             AddNeighbor(listNeighbors, x-1, y+1);
  168.         }
  169.         private void AddNeighbor(List<AStarNode> listNeighbors, int x, int y) {
  170.             if(x >= 0 && x < WorldData.gridWidth && y >= 0 && y < WorldData.gridHeight) {  
  171.                 if(WorldData.grid[(y * WorldData.gridWidth) + x] != -1) {
  172.                     listNeighbors.Add(new AStarNode2D(this, goal, g, WorldData.grid[(y * WorldData.gridWidth) + x], x, y));
  173.                 }
  174.             }
  175.         }
  176.    
  177.         public override void Calculate() {
  178.             // Heuristics! :O
  179.             if (goal != null) {
  180.                 this.h = Math.Sqrt(Math.Pow((this.x - ((AStarNode2D)goal).x), 2) + Math.Pow((this.y - ((AStarNode2D)goal).y), 2));
  181.                 this.f = this.g + this.h;
  182.             }
  183.         }
  184.    
  185.         public override bool IsEqualTo(AStarNode node) {
  186.             return ((this.x == ((AStarNode2D)node).x) && (this.y == ((AStarNode2D)node).y));
  187.         }
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement