public void generateMoves(Unit myUnit) { // start with a blank nav list navList = null; // init the closed list closedList = new bool[13, 13]; // make a NavNode heap heap = new NavHeap(60, false); // get the move allowance from stats: defense/attack/move/HP moves = myUnit.getStats()[2]; // grab x/y for the center/start tile startX = myUnit.getX(); startY = myUnit.getY(); // make a NavNode for the start currentNode = new NavNode(); currentNode.x = startX; currentNode.y = startY; currentNode.totalMoveCost = 0; // calculate transform numbers for the closed list transX = startX - 6; transY = startY - 6; // loop to greedy search out paths do { // add viable neighbors to the current node to the heap // up addNeighbor(currentNode.x, (currentNode.y + 1)); // down addNeighbor(currentNode.x, (currentNode.y - 1)); // left addNeighbor((currentNode.x - 1), currentNode.y); // right addNeighbor((currentNode.x + 1), currentNode.y); // add current node to nav list currentNode.nextNode = navList; navList = currentNode; // add current node to the closed list //Debug.Log("Adding tile (" + (currentNode.x - transX - 6) + ","+ //(currentNode.y - transY - 6) + ") to closed list."); closedList[(currentNode.x - transX), (currentNode.y - transY)] = true; // get the next best node from the heap currentNode = heap.pop(); } while (currentNode != null); // deploy the list of navigatable nodes (except the last one (starting node)) while (navList.nextNode != null) { // deploy a move indicator to location at nav node's location tileStack.deployTile(navList.x, navList.y, myUnit); // get next node in the list navList = navList.nextNode; } }
private void pickMove() { // start with a blank nav list navList = null; // init the closed list closedList = new bool[13, 13]; // make the NavNode heap(s) movesHeap = new NavHeap(60, false); // get the move allowance from stats: defense/attack/move/HP moves = currentUnit.getStats()[2]; // grab x/y for the center/start tile startX = currentUnit.getX(); startY = currentUnit.getY(); // make a NavNode for the start currentNode = new NavNode(); currentNode.x = startX; currentNode.y = startY; currentNode.totalMoveCost = 0; bestNode = currentNode; // calculate transform numbers for the closed list transX = startX - 6; transY = startY - 6; // loop to greedy search out paths do { // add viable neighbors to the current node to the heap // up addNeighbor(currentNode.x, (currentNode.y + 1)); // down addNeighbor(currentNode.x, (currentNode.y - 1)); // left addNeighbor((currentNode.x - 1), currentNode.y); // right addNeighbor((currentNode.x + 1), currentNode.y); // add current node to nav list currentNode.nextNode = navList; navList = currentNode; // add current node to the closed list closedList[(currentNode.x - transX), (currentNode.y - transY)] = true; // get the next best node from the heap currentNode = movesHeap.pop(); } while (currentNode != null); // go through the navList and find the max f maxF = 0; while (navList != null) { if (navList.f > maxF) { maxF = navList.f; bestNode = navList; } navList = navList.nextNode; } // move the current unit to the best node currentUnit.move(bestNode.x, bestNode.y); }