Advertisement
Guest User

Untitled

a guest
Aug 10th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import PathNode;
  3.     import flash.utils.Dictionary;
  4.     public class PathSearcher {
  5.         private var lvlData:Array;
  6.         private var beginPts:Array;
  7.         private var endPts:Array;
  8.         private var openList:Dictionary = new Dictionary;
  9.         private var closedList:Dictionary = new Dictionary;
  10.         private var resultsArr:Array = new Array;
  11.  
  12.         public function PathSearcher(levelData:Array, beginPoints:Array, endPoints:Array) {
  13.             // Allow the whole class to access it
  14.             lvlData = levelData;
  15.             beginPts = beginPoints;
  16.             endPts = endPoints;
  17.             var myNode:PathNode = new PathNode(beginPoints,myNode);
  18.             openList[beginPoints[0] + " " + beginPoints[1]] = myNode;
  19.             myNode.distS = 0;
  20.             FindPath(myNode);
  21.         }
  22.  
  23.         public function FindPath(node:PathNode):void {
  24.             //Is this the end node?
  25.             if (node.xpos == endPts[0] && node.ypos == endPts[1]) {
  26.                 retracePath();
  27.                 return;
  28.             }
  29.            
  30.             //Adds current node to closed list and removes it from open list.
  31.             delete openList[node.xpos + " " + node.ypos];
  32.             closedList[node.xpos + " " + node.ypos] = node;
  33.  
  34.             //Finds neighbouring nodes
  35.             for (var i = -1; i < 2; i++) {
  36.                 for (var j = -1; j < 2; j++) {
  37.                     if (i == 0 && j == 0) {
  38.                         //Prevent it from calculating for the current node
  39.                     } else {
  40.                         if (node.xpos + i >= 0 && node.ypos + j >= 0 && node.xpos + i < lvlData.length && node.ypos + j < lvlData[0].length) { //is it within boundaries?
  41.                             if (openList[(node.xpos + i) + " " + (node.ypos + j)] == undefined) { //not on open list
  42.                                 if (closedList[(node.xpos + i) + " " + (node.ypos + j)] == undefined) { //not on closed list either
  43.                                     if (lvlData[node.xpos + i][node.ypos + j] == 0) { //makes sure that it is walkable terrain
  44.                                         //Adds it to openlist, also creates new pathnode.
  45.                                         openList[(node.xpos + i) + " " + (node.ypos + j)] = new PathNode([(node.xpos + i),(node.ypos + j)], node);
  46.                                         var theNode:PathNode = openList[(node.xpos + i) + " " + (node.ypos + j)];
  47.                                         //Calculates distance to end node
  48.                                         theNode.distE = (Math.abs(theNode.xpos - endPts[0]) + Math.abs(theNode.ypos - endPts[1])) * 10;
  49.                                         //Calculates distance to start node by adding 14 or 10 to the parent's distS score
  50.                                         var cost = isDiag([theNode.parentNode.xpos,theNode.parentNode.ypos],[theNode.xpos,theNode.ypos]);
  51.                                         theNode.distS = theNode.parentNode.distS + cost;
  52.                                     }
  53.                                 }
  54.                             } else { //what to do if it is already on the open list
  55.                                 //this checks to see if it would be better to go to theNode through the currentNode instead of theNode's current parent.
  56.                                 var theNode:PathNode = openList[(node.xpos + i) + " " + (node.ypos + j)];
  57.                                 var cost = isDiag([node.xpos,node.ypos],[theNode.xpos,theNode.ypos]);
  58.                                 if (node.distS + cost < theNode.distS) {
  59.                                     //more efficient to make theNode's parents the current node.
  60.                                     theNode.parentNode = node;
  61.                                 }
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             var lowestTScore:int = 10000;
  68.             var pointerTo:PathNode;
  69.             for each (var Cur:PathNode in openList) {
  70.                 //loops through each node in openlist and finds the one with lowest total score.
  71.                 if (Cur.distS + Cur.distE < lowestTScore) {
  72.                     lowestTScore = Cur.distS + Cur.distE;
  73.                     pointerTo = Cur;
  74.                 }
  75.             }
  76.             //is openlist empty?
  77.             if (lowestTScore == 10000) {
  78.                 return;
  79.             } else {
  80.                 //do this again.
  81.                 FindPath(pointerTo);
  82.             }
  83.         }
  84.         private function isDiag(par:Array, cur:Array):int {
  85.             //returns 14 or 10
  86.             var returnV:uint = 0;
  87.             if (par[0] != cur[0]) {
  88.                 returnV++;
  89.             }
  90.             if (par[1] != cur[1]) {
  91.                 returnV++;
  92.             }
  93.             if (returnV == 2) {
  94.                 return 14;
  95.             } else {
  96.                 return 10;
  97.             }
  98.         }
  99.         private function retracePath():void{
  100.             //retraces path from end to start.
  101.             var endNode:PathNode = openList[endPts[0] + " " + endPts[1]];
  102.             while(endNode.xpos != beginPts[0] && endNode.ypos != beginPts[1]){
  103.                 resultsArr.push([endNode.ypos, endNode.xpos, endNode.distS]); // 'ypos' first because I seem to have mixed up x and y so for the results I flip them.
  104.                 endNode = endNode.parentNode;
  105.             }
  106.             trace(resultsArr);
  107.         }
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement