Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Schniz/robotikka. Lee Ve Tom Ve Ani Doin our Robottttt
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980#include "SearchNode.h"
#include <math.h>
SearchNode::SearchNode(int x, int y) : X(x), Y(y) {}
SearchNode::SearchNode() : SearchNode(0, 0) {}
// Heuristic distance between this and the goal nodedouble SearchNode::goalDistanceEstimate(SearchNode& goalNode) { return sqrt(pow(X - goalNode.X, 2) + pow(Y - goalNode.Y, 2));}
bool SearchNode::isGoal(SearchNode& goalNode) { return X == goalNode.X && Y == goalNode.Y;}
/** * Adds the cell as a successor if its not the previous one * and its free. */void addIfNotBackwards(Cell* cell, Cell* previousCell, AStarSearch<SearchNode>* search) { // Don't add it if it isn't there. if (cell == NULL) return;
int x = cell->getX(); int y = cell->getY();
if (previousCell == NULL || (!cell->equalsTo(previousCell) && cell->isFree())) { SearchNode newNode = SearchNode(x, y); search->AddSuccessor(newNode); }}
bool SearchNode::getSuccessors(AStarSearch<SearchNode>* search, Map* map, SearchNode* parentNode) { // Infinity! Cell* previousCell = NULL;
if (parentNode) { previousCell = map->getCell(parentNode->X, parentNode->Y); }
SearchNode newNode;
// push each possible move except allowing the search to go backwards
Cell* neighbors[] = { map->getCell(Y, X - 1), map->getCell(Y, X + 1), map->getCell(Y - 1, X), map->getCell(Y + 1, X) };
for (int cellIndex = 0; cellIndex < 4; cellIndex++) { addIfNotBackwards(neighbors[cellIndex], previousCell, search); }
return true;}
// given this node, what does it cost to move to successor. In the case// of our map the answer is the map terrain value at this node since that is// conceptually where we're movingdouble SearchNode::getCost(SearchNode& successor, Map* map) { return map->getCell(Y, X)->Cell_Cost;}
bool SearchNode::isSameState(SearchNode& rhs) { // same state in a maze search is simply when (x,y) are the same return X == rhs.X && Y == rhs.Y;}
ostream& operator<<(ostream& os, const SearchNode& node) { os << "[" << node.X << "," << node.Y << "]";
return os;}