You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
111 lines
3.1 KiB
9 years ago
|
#include "Pather.h"
|
||
9 years ago
|
|
||
9 years ago
|
node::node(sf::Vector2i XY, double h, int cF, node* p, Pather* pather_) {
|
||
|
position = XY;
|
||
9 years ago
|
hueristic = h;
|
||
9 years ago
|
came_from = cF;
|
||
9 years ago
|
parent = p;
|
||
|
pather = pather_;
|
||
|
}
|
||
|
node::node() {
|
||
|
|
||
|
}
|
||
|
node::~node() {
|
||
|
|
||
|
}
|
||
|
|
||
9 years ago
|
void node::getNewNeighbors() {
|
||
9 years ago
|
|
||
9 years ago
|
// Get the end node position for calculating distance from end(hueristic)
|
||
9 years ago
|
int x = pather->getEndNodePosition().x;
|
||
|
int y = pather->getEndNodePosition().y;
|
||
|
|
||
9 years ago
|
sf::Vector2i dest0XY(position.x, position.y - 1); // North
|
||
|
if (!pather->map->isTileSolid(dest0XY) && pather->visited_map(dest0XY.x, dest0XY.y) != 1) {
|
||
9 years ago
|
// If so, find the distance between this node and the end node, the hueristic
|
||
|
int tempx = (x - dest0XY.x);
|
||
|
int tempy = (y - dest0XY.y);
|
||
|
|
||
|
// I think dv is the hueristic??
|
||
|
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||
|
|
||
9 years ago
|
double v = dv;
|
||
9 years ago
|
|
||
|
// Take that value and create a new node
|
||
9 years ago
|
pather->open_list.emplace(new node(dest0XY, v, 0, pather->getActiveNode(), pather), v);
|
||
9 years ago
|
|
||
|
// Set that tile as visited so we don't get stuck in a loop
|
||
9 years ago
|
pather->visited_map(dest0XY.x, dest0XY.y) = 1;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
sf::Vector2i dest1XY(position.x + 1, position.y); // East
|
||
|
if (!pather->map->isTileSolid(dest1XY) && pather->visited_map(dest1XY.x, dest1XY.y) != 1) {
|
||
9 years ago
|
// If so, find the distance between this node and the end node, the hueristic
|
||
|
int tempx = (x - dest1XY.x);
|
||
|
int tempy = (y - dest1XY.y);
|
||
|
|
||
|
// I think dv is the hueristic??
|
||
|
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||
|
|
||
9 years ago
|
double v = dv;
|
||
9 years ago
|
|
||
|
// Take that value and create a new node
|
||
9 years ago
|
pather->open_list.emplace(new node(dest1XY, v, 1, pather->getActiveNode(), pather), v);
|
||
9 years ago
|
|
||
|
// Set that tile as visited so we don't get stuck in a loop
|
||
9 years ago
|
pather->visited_map(dest1XY.x, dest1XY.y) = 1;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
sf::Vector2i dest2XY(position.x, position.y + 1); // South
|
||
|
if (!pather->map->isTileSolid(dest2XY) && pather->visited_map(dest2XY.x, dest2XY.y) != 1) {
|
||
9 years ago
|
// If so, find the distance between this node and the end node, the hueristic
|
||
|
int tempx = (x - dest2XY.x);
|
||
|
int tempy = (y - dest2XY.y);
|
||
|
|
||
|
// I think dv is the hueristic??
|
||
|
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||
|
|
||
9 years ago
|
double v = dv;
|
||
9 years ago
|
|
||
|
// Take that value and create a new node
|
||
9 years ago
|
pather->open_list.emplace(new node(dest2XY, v, 2, pather->getActiveNode(), pather), v);
|
||
9 years ago
|
|
||
|
// Set that tile as visited so we don't get stuck in a loop
|
||
9 years ago
|
pather->visited_map(dest2XY.x, dest2XY.y) = 1;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
sf::Vector2i dest3XY(position.x - 1, position.y); // West
|
||
|
if (!pather->map->isTileSolid(dest3XY) && pather->visited_map(dest3XY.x, dest3XY.y) != 1) {
|
||
9 years ago
|
// If so, find the distance between this node and the end node, the hueristic
|
||
|
int tempx = (x - dest3XY.x);
|
||
|
int tempy = (y - dest3XY.y);
|
||
|
|
||
|
// I think dv is the hueristic??
|
||
|
double dv = sqrt((tempx * tempx) + (tempy * tempy));
|
||
|
|
||
9 years ago
|
double v = dv;
|
||
9 years ago
|
|
||
|
// Take that value and create a new node
|
||
9 years ago
|
pather->open_list.emplace(new node(dest3XY, v, 3, pather->getActiveNode(), pather), v);
|
||
9 years ago
|
|
||
|
// Set that tile as visited so we don't get stuck in a loop
|
||
9 years ago
|
pather->visited_map(dest3XY.x, dest3XY.y) = 1;
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
sf::Vector2i node::getPosition() {
|
||
|
return position;
|
||
|
}
|
||
|
|
||
|
node* node::getParent() {
|
||
|
return parent;
|
||
|
}
|
||
|
|
||
|
double node::getHueristic() {
|
||
|
return hueristic;
|
||
|
}
|
||
|
|
||
|
int node::getCameFrom() {
|
||
|
return came_from;
|
||
|
}
|