Skip to content

Instantly share code, notes, and snippets.

@MarcinMM
Created July 25, 2011 03:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MarcinMM/1103535 to your computer and use it in GitHub Desktop.
Rawr
var path:Array = new Array();
var openList:Array = new Array();
var closedList:Array = new Array();
var closedListD:Dictionary = new Dictionary();
var neighborList:Vector.<Node>;
trace("path from: " + this.x + "-" + this.y + " to " + endNode.x + "-" + endNode.y);
var currentNode:Node = this;
openList.push(this);
var i:int = 0;
var thisOpenIndex:int;
if (type == "corridor") {
neighborList = currentNode.solidNeighbors;
} else if (type == "creature") {
neighborList = currentNode.walkingNeighbors;
}
// SAFTY OFF! [sic]
var openIndex:int;
var closedIndex:int;
var gCost2:int = 0;
while (i < 1000 && openList.length > 0 && currentNode != endNode && (!currentNode.sameLoc(endNode))) {
//i++;
openList.sortOn("fCost");
// perf hit here, needs to turn openList into a priorityQueue from http://www.polygonal.de/doc/ds/
currentNode = openList.shift();
closedList.push(currentNode);
closedListD[currentNode] = true;
//if (closedListD[currentNode] == true) trace ('blibli');
if (type == "corridor") {
neighborList = currentNode.solidNeighbors;
} else if (type == "creature") {
neighborList = currentNode.walkingNeighbors;
}
for each (var neighbor:Node in neighborList) {
// perf hit here, need to turn closedList into a hashtable from flash.utils.Dictionary
// so we can just do a check on (array[node] == true) rather than searching every time
// also
openIndex = openList.indexOf(neighbor);
closedIndex = closedList.indexOf(neighbor);
//currentNode.findG(neighbor);
gCost2 = currentNode.gCost + 10; // this needs an actual calculation
if (gCost2 < neighbor.gCost) {
if (openIndex !== -1) {
openList.splice(openIndex, 1);
openIndex = -1;
}
/*if (closedListD[neighbor] == true) {
closedListD[neighbor] = false;
}*/
if (closedIndex !== -1) {
trace(closedListD[neighbor]);
closedList.splice(closedIndex, 1);
closedIndex = -1;
}
}
//if (openIndex === -1 && closedListD[neighbor] === false) {
if (openIndex === -1 && closedIndex === -1) {
neighbor.gCost = gCost2;
neighbor.findH(endNode);
neighbor.updateF();
neighbor.setParent(currentNode);
openList.push(neighbor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment