-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinding.cpp
More file actions
152 lines (124 loc) · 3.48 KB
/
Copy pathPathfinding.cpp
File metadata and controls
152 lines (124 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Pathfinding.h"
/* Node */
Node::Node(Node* n) {
this->parent = n->parent;
this->child = n->child;
this->g = n->g;
this->f = n->f;
this->h = n->h;
this->x = n->x;
this->y = n->y;
this->z = n->z;
}
Node::Node(glm::vec3 pos, float g, float h, float f) :x(pos.x), y(pos.y), z(pos.z) {
}
float Node::calculate_g(Node& cur) {
return cur.g + 1;
}
float Node::calculate_h(Node& end) {
return glm::length(glm::vec3(end.x, end.y, end.z) - glm::vec3(x, y, z));
}
float Node::calculate_f(Node& end) {
return g + h;
}
/* Pathfinding */
void Pathfinding::search(PathmapWorld* world, const glm::vec3& start, const glm::vec3& end) {
// Initialize start node
WORLD_ = world;
start_node = new Node(start);
end_node = new Node(end);
open_nodes.push_back(*start_node);
// Start searching
while(!open_nodes.empty()) {
Node cur = open_nodes.front();
for(Node n : open_nodes) {
if(n.less_than(cur)) { cur = n; }
}
if(cur.equals(end_node)) {
*end_node = new Node(cur);
break;
}
for(unsigned i = 0; i < open_nodes.size(); i++) {
if(open_nodes[i].equals(cur)) {
open_nodes.erase(open_nodes.begin() + i);
}
}
closed_nodes.push_back(cur);
for(Node n : get_neighbours(cur)) {
bool skip = false;
for(Node m : closed_nodes) {
if(n.equals(m)) { skip = true; }
}
if(skip) {
continue; // Do not calculate the node or add it to open nodes etc
}
n.parent = new Node(&cur);
n.calculate_g(*n.parent);
n.calculate_h(*end_node);
n.calculate_f(*end_node);
skip = false;
for(Node l : open_nodes) {
if(l.equals(n) && l.less_than(n)) {
skip = true;
break;
}
}
if(skip) { continue; }
open_nodes.push_back(n);
}
}
Node cur = *end_node;
while(cur.parent != nullptr) {
if(cur.parent != nullptr) { cur.parent->child = new Node(&cur); }
cur = *cur.parent;
cur_node = new Node(&cur);
}
std::cout << ""; // Debugging without a set end point
}
bool Pathfinding::has_path() {
if(cur_node == nullptr) { return false; }
return true;
}
glm::vec3& Pathfinding::get_current_pos() {
glm::vec3 res = (cur_node != nullptr) ? WORLD_->translate(glm::vec3(cur_node->x, cur_node->y, cur_node->z)) : glm::vec3(0);
return res;
}
glm::vec3& Pathfinding::get_next_pos() {
cur_node = cur_node->child;
glm::vec3 res(glm::vec3(cur_node->x * WORLD_->GRIDSPACE_, cur_node->y * WORLD_->GRIDSPACE_, cur_node->z * WORLD_->GRIDSPACE_));
return res;
}
glm::vec3& Pathfinding::get_prev_pos() {
cur_node = cur_node->parent;
glm::vec3 res(glm::vec3(cur_node->x * WORLD_->GRIDSPACE_, cur_node->y * WORLD_->GRIDSPACE_, cur_node->z * WORLD_->GRIDSPACE_));
return res;
}
bool Pathfinding::has_next() {
return (cur_node->child != nullptr);
}
bool Pathfinding::has_prev() {
return (cur_node->parent != nullptr);
}
std::vector<Node> Pathfinding::get_neighbours(const Node& cur) {
std::vector<Node> nodes {};
glm::vec3 res(glm::vec3(cur.x, cur.y, cur.z));
std::vector<glm::vec3> neighbours(WORLD_->get_walkable_neighbours(res));
for(glm::vec3 pos : neighbours) {
nodes.push_back(Node(pos));
}
return nodes;
}
std::vector<glm::vec3> Pathfinding::get_path(PathmapWorld* world, const glm::vec3& start, const glm::vec3& end) {
search(world, start, end);
std::vector<glm::vec3> res {};
if(cur_node != nullptr) {
Node cur = *cur_node;
while(cur.child != nullptr) {
cur = *cur.child;
res.push_back(world->translate(glm::vec3(cur.x, cur.y, cur.z)));
}
} else {
return get_path(world, start, world->open.at(rand() % world->open.size()));
}
return res;
}