std::vector<Point>* PathGenerator::generatePath(Point start, Point end) { this->resetTileNodes(); _start = start; _end = end; vector<Point>* path = new vector<Point>(); TileNode* startNode = _nodes[int(start.x)][int(start.y)]; startNode->setParent(NULL); _opened_nodes.push_back(startNode); TileNode* pathNode = NULL; while (true) { pathNode = getMinFNode(); addAdjacentOpenNodes(pathNode); _passed_nodes.push_back(pathNode); deletePassedNode(pathNode); if (pathNode->getX() == int(end.x) && pathNode->getY() == int(end.y)) { break; } } while (pathNode) { path->push_back(convertCoordinate2Pixel(pathNode->getX(), pathNode->getY(), _map_height)); pathNode = pathNode->getParent(); } std::reverse(path->begin(), path->end()); return path; }
void PathGenerator::addToOpenNode(int x, int y, adjType adjtype, TileNode* parent) { if (x < 0 || y < 0) { return; } if (!canPass(x, y)) { return; } TileNode* node = _nodes[x][y]; if (inPassedNodes(node)) { return; } else if (!inOpenNodes(node)) { _opened_nodes.push_back(node); int incf; if (adjtype == Straight) incf = 10; else incf = 14; node->setG(parent->getG() + incf); node->setH(descarteDistance(Point(node->getX(), node->getY()), _end) * 10); node->setF(node->getG() + node->getH()); node->setParent(parent); } }