Guest User

Untitled

a guest
Apr 12th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var aStarData:AStarData = aStarQueue.shift();
  2.            
  3.             var startPos:Position = aStarData.tank.pos;
  4.             var endPos:Position = aStarData.endPos;
  5.             //var startTime:int = new Date().getTime();
  6.            
  7.             var startNode:AStarNode = nodeMatrix[startPos.x][startPos.y];
  8.             startNode.value = 0;
  9.             startNode.isOpen = true;
  10.             startNode.prev = null;
  11.             nodeMatrix[endPos.x][endPos.y].isEndNode = true;
  12.            
  13.             // A* algorithm
  14.             var openList:Vector.<AStarNode> = new Vector.<AStarNode>();
  15.             openList[0] = startNode;
  16.             while (openList.length != 0) {
  17.                 var currNode:AStarNode = openList.shift();
  18.                 if (currNode.isEndNode) break;
  19.                 for each (var neighborNode:AStarNode in currNode.neighbors) {
  20.                     if (neighborNode.isClosed) continue;
  21.                     var tentativeValue:int = currNode.value + currNode.distToNeighbor(neighborNode);
  22.                     if (neighborNode.isOpen && tentativeValue >= neighborNode.value) continue;
  23.                     neighborNode.prev = currNode;
  24.                     neighborNode.value = tentativeValue;
  25.                     neighborNode.f = tentativeValue + h(neighborNode, endPos);
  26.                     if (neighborNode.isOpen) {
  27.                         var index:int = openList.indexOf(neighborNode)
  28.                         openList.splice(index--, 1);
  29.                         while (index >= 0 && openList[index].f > neighborNode.f) index--;
  30.                         openList.splice(index + 1, 0, neighborNode);
  31.                     }
  32.                     else if (openList.length == 0) {
  33.                         openList[0] = neighborNode;
  34.                         neighborNode.isOpen = true;
  35.                     }
  36.                     else {
  37.                         var index:int = openList.length - 1;
  38.                         while (index >= 0 && openList[index].f > neighborNode.f) index--;
  39.                         openList.splice(index + 1, 0, neighborNode);
  40.                         neighborNode.isOpen = true;
  41.                     }
  42.                 }
  43.                 currNode.isOpen = false;
  44.                 currNode.isClosed = true;
  45.             }
  46.            
  47.             nodeMatrix[endPos.x][endPos.y].isEndNode = false;
  48.            
  49.             if (nodeMatrix[endPos.x][endPos.y].prev == null) {
  50.                 reset();
  51.                 dispatchEvent(new AStarEvent(AStarEvent.FINISHED, aStarData.tank, null));
  52.                 return;
  53.             }
Add Comment
Please, Sign In to add comment