Yeah, this really isn't wanting to work. Gonna need to probably redo everything
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "Pather.h"
|
||||
#include <iostream>
|
||||
|
||||
node::node(sf::Vector2i XY, int h, int cF, int cL, node* p, Pather* pather_) {
|
||||
node::node(sf::Vector2i XY, double h, int cF, int cL, node* p, Pather* pather_) {
|
||||
xy = XY;
|
||||
hueristic = h;
|
||||
cameFrom = cF;
|
||||
@@ -22,7 +22,7 @@ void node::neighbors() {
|
||||
int y = pather->getEndNodePosition().y;
|
||||
|
||||
sf::Vector2i dest0XY(xy.x, xy.y - 1); // North
|
||||
if (!pather->map->isTileSolid(dest0XY) && pather->visitedMap[dest0XY.x][dest0XY.y] != 1) {
|
||||
if (!pather->map->isTileSolid(dest0XY) && pather->visitedMap(dest0XY.x, dest0XY.y) != 1) {
|
||||
// If so, find the distance between this node and the end node, the hueristic
|
||||
int tempx = (x - dest0XY.x);
|
||||
int tempy = (y - dest0XY.y);
|
||||
@@ -30,17 +30,17 @@ void node::neighbors() {
|
||||
// I think dv is the hueristic??
|
||||
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||||
|
||||
int v = static_cast<int>(dv);
|
||||
double v = dv;
|
||||
|
||||
// Take that value and create a new node
|
||||
pather->openList.emplace(new node(dest0XY, v, 0, 1, pather->active_node, pather), v);
|
||||
pather->openList.emplace(new node(dest0XY, v, 3, 1, pather->active_node, pather), v);
|
||||
|
||||
// Set that tile as visited so we don't get stuck in a loop
|
||||
pather->visitedMap[dest0XY.x][dest0XY.y] = 1;
|
||||
pather->visitedMap(dest0XY.x, dest0XY.y) = 1;
|
||||
}
|
||||
|
||||
sf::Vector2i dest1XY(xy.x + 1, xy.y); // East
|
||||
if (!pather->map->isTileSolid(dest1XY) && pather->visitedMap[dest1XY.x][dest1XY.y] != 1) {
|
||||
if (!pather->map->isTileSolid(dest1XY) && pather->visitedMap(dest1XY.x, dest1XY.y) != 1) {
|
||||
// If so, find the distance between this node and the end node, the hueristic
|
||||
int tempx = (x - dest1XY.x);
|
||||
int tempy = (y - dest1XY.y);
|
||||
@@ -48,17 +48,17 @@ void node::neighbors() {
|
||||
// I think dv is the hueristic??
|
||||
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||||
|
||||
int v = static_cast<int>(dv);
|
||||
double v = dv;
|
||||
|
||||
// Take that value and create a new node
|
||||
pather->openList.emplace(new node(dest1XY, v, 0, 1, pather->active_node, pather), v);
|
||||
pather->openList.emplace(new node(dest1XY, v, 4, 1, pather->active_node, pather), v);
|
||||
|
||||
// Set that tile as visited so we don't get stuck in a loop
|
||||
pather->visitedMap[dest1XY.x][dest1XY.y] = 1;
|
||||
pather->visitedMap(dest1XY.x, dest1XY.y) = 1;
|
||||
}
|
||||
|
||||
sf::Vector2i dest2XY(xy.x, xy.y + 1); // South
|
||||
if (!pather->map->isTileSolid(dest2XY) && pather->visitedMap[dest2XY.x][dest2XY.y] != 1) {
|
||||
if (!pather->map->isTileSolid(dest2XY) && pather->visitedMap(dest2XY.x, dest2XY.y) != 1) {
|
||||
// If so, find the distance between this node and the end node, the hueristic
|
||||
int tempx = (x - dest2XY.x);
|
||||
int tempy = (y - dest2XY.y);
|
||||
@@ -66,17 +66,17 @@ void node::neighbors() {
|
||||
// I think dv is the hueristic??
|
||||
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||||
|
||||
int v = static_cast<int>(dv);
|
||||
double v = dv;
|
||||
|
||||
// Take that value and create a new node
|
||||
pather->openList.emplace(new node(dest2XY, v, 0, 1, pather->active_node, pather), v);
|
||||
pather->openList.emplace(new node(dest2XY, v, 1, 1, pather->active_node, pather), v);
|
||||
|
||||
// Set that tile as visited so we don't get stuck in a loop
|
||||
pather->visitedMap[dest2XY.x][dest2XY.y] = 1;
|
||||
pather->visitedMap(dest2XY.x, dest2XY.y) = 1;
|
||||
}
|
||||
|
||||
sf::Vector2i dest3XY(xy.x - 1, xy.y); // West
|
||||
if (!pather->map->isTileSolid(dest3XY) && pather->visitedMap[dest3XY.x][dest3XY.y] != 1) {
|
||||
if (!pather->map->isTileSolid(dest3XY) && pather->visitedMap(dest3XY.x, dest3XY.y) != 1) {
|
||||
// If so, find the distance between this node and the end node, the hueristic
|
||||
int tempx = (x - dest3XY.x);
|
||||
int tempy = (y - dest3XY.y);
|
||||
@@ -84,13 +84,13 @@ void node::neighbors() {
|
||||
// I think dv is the hueristic??
|
||||
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||||
|
||||
int v = static_cast<int>(dv);
|
||||
double v = dv;
|
||||
|
||||
// Take that value and create a new node
|
||||
pather->openList.emplace(new node(dest3XY, v, 0, 1, pather->active_node, pather), v);
|
||||
pather->openList.emplace(new node(dest3XY, v, 2, 1, pather->active_node, pather), v);
|
||||
|
||||
// Set that tile as visited so we don't get stuck in a loop
|
||||
pather->visitedMap[dest3XY.x][dest3XY.y] = 1;
|
||||
pather->visitedMap(dest3XY.x, dest3XY.y) = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ void node::neighbors() {
|
||||
|
||||
Pather::Pather(Map* map_) {
|
||||
map = map_;
|
||||
//visitedMap = new MultiArray<int, App::WINDOW_HEIGHT, App::WINDOW_WIDTH>();
|
||||
}
|
||||
|
||||
Pather::~Pather() {
|
||||
@@ -109,15 +110,18 @@ sf::Vector2i Pather::getEndNodePosition() {
|
||||
|
||||
std::deque<int> Pather::pathTo(sf::Vector2i start, sf::Vector2i end) {
|
||||
|
||||
|
||||
// Clear the visited map for erroneous data
|
||||
for (int i = 0; i < Map::CELLS_WIDTH; i++) {
|
||||
for (int l = 0; l < Map::CELLS_HEIGHT; l++) {
|
||||
visitedMap[i][l] = 0;
|
||||
visitedMap(i, l) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << visitedMap(10, 163);
|
||||
|
||||
// Place the start and end nodes
|
||||
start_node = new node(start, 0, 0, 0, nullptr, this);
|
||||
start_node = new node(start, 7000, 0, 0, nullptr, this);
|
||||
end_node = new node(end, 0, 0, 0, nullptr, this);
|
||||
|
||||
// Set the entry point, clean up any stray data from last run
|
||||
@@ -126,7 +130,7 @@ std::deque<int> Pather::pathTo(sf::Vector2i start, sf::Vector2i end) {
|
||||
closedList.clear();
|
||||
|
||||
// Seed for the loop
|
||||
openList.emplace(start_node, 0);
|
||||
openList.emplace(start_node, start_node->hueristic);
|
||||
|
||||
early_exit = false;
|
||||
path_list = loop();
|
||||
@@ -136,26 +140,34 @@ std::deque<int> Pather::pathTo(sf::Vector2i start, sf::Vector2i end) {
|
||||
|
||||
|
||||
std::deque<int> Pather::loop() {
|
||||
while (!openList.empty() && !early_exit) {
|
||||
|
||||
// Damn thing keeps falling out of scope
|
||||
|
||||
|
||||
while (!openList.empty() && !early_exit) {
|
||||
// Early exit jankyness, need to change this
|
||||
if (closedList.size() > 3000) {
|
||||
no_path = true;
|
||||
early_exit = true;
|
||||
break;
|
||||
}
|
||||
else if (active_node->xy.x == end_node->xy.x && end_node->xy.y == end_node->xy.y) {
|
||||
//if (closedList.size() > 3000) {
|
||||
// no_path = true;
|
||||
// early_exit = true;
|
||||
// break;
|
||||
//}
|
||||
if (active_node->xy.x == end_node->xy.x && active_node->xy.y == end_node->xy.y) {
|
||||
early_exit = true;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// Find the pair with the lowest hueristic
|
||||
// 5/10
|
||||
std::pair<node*, int> bestMin;
|
||||
// 5/10
|
||||
std::pair<node*, double> bestMin(start_node, 10000);
|
||||
|
||||
for (auto testMin: openList) {
|
||||
if (bestMin.second < testMin.second)
|
||||
if (bestMin.second >= testMin.second)
|
||||
bestMin = testMin;
|
||||
}
|
||||
|
||||
// Set the new active node to the lowest hueristic that we found earlier
|
||||
active_node = bestMin.first;
|
||||
|
||||
// Find the neighbors for that node
|
||||
active_node->neighbors();
|
||||
|
||||
@@ -163,12 +175,11 @@ std::deque<int> Pather::loop() {
|
||||
openList.erase(active_node);
|
||||
|
||||
// Check to see if the node has already been added to the closed list, if not, add it
|
||||
if (closedList.count(active_node) > 0) {
|
||||
if (closedList.count(active_node) == 0) {
|
||||
closedList.emplace(active_node, active_node->hueristic);
|
||||
}
|
||||
|
||||
// Set the new active node to the lowest hueristic that we found earlier
|
||||
active_node = bestMin.first;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,10 +195,12 @@ std::deque<int> Pather::loop() {
|
||||
std::deque<int> Pather::returnPath() {
|
||||
std::deque<int> path;
|
||||
|
||||
while (active_node != nullptr) {
|
||||
while (active_node->parent != nullptr) {
|
||||
path.push_back(active_node->cameFrom);
|
||||
|
||||
node* parent = active_node->parent;
|
||||
delete active_node;
|
||||
active_node = active_node->parent;
|
||||
active_node = parent;
|
||||
}
|
||||
|
||||
return path;
|
||||
|
||||
Reference in New Issue
Block a user