-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinding.hpp
More file actions
100 lines (83 loc) · 2.51 KB
/
Copy pathPathfinding.hpp
File metadata and controls
100 lines (83 loc) · 2.51 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
#ifndef PATHFINDING_H_
#define PATHFINDING_H_
#include <list>
#include "TileMap.hpp"
#include "SFMLDebugDraw.h"
struct Node {
int x, y;
float g, h;
Node *parent;
Node() : x(-1), y(-1), g(0), h(0), parent(nullptr) {}
Node(int _x, int _y) : x(_x), y(_y), g(0), h(0), parent(nullptr) {}
void set(int _x, int _y) {
x = _x;
y = _y;
}
float f() const { return g + h; }
const bool operator==(const Node &other) {
return (x == other.x && y == other.y);
}
const bool operator>(const Node &other) { return f() > other.f(); }
const bool operator<(const Node &other) { return f() < other.f(); }
int distanceTo(const Node &other) const {
return std::sqrt((other.x - x) * (other.x - x) +
(other.y - y) * (other.y - y));
}
};
enum class EdgeType { WALK, JUMP };
struct Edge {
Edge()
: n1(nullptr), n2(nullptr), edgeType(EdgeType::WALK), jumpPower(0.0f) {}
Edge(Node *a, Node *b)
: n1(a), n2(b), edgeType(EdgeType::WALK), jumpPower(0.0f) {}
Node *n1;
Node *n2;
EdgeType edgeType;
float jumpPower;
const bool operator==(const Edge &other) const {
return (*other.n1 == *n1 && *other.n2 == *n2) ||
(*other.n1 == *n2 && *other.n2 == *n1);
}
};
class Pathfinder {
typedef std::list<Node *>::iterator NodeIter;
struct Point {
int x, y;
};
public:
Pathfinder() : m_tileMap(nullptr) { init(); }
Pathfinder(TileMap *tileMap) : m_tileMap(tileMap) { init(); }
void setMap(TileMap *tileMap) { m_tileMap = tileMap; }
std::vector<Node> getPath(Node start, Node goal);
private:
void init() {
m_mass = 0.476074f;
m_velocity = 5.0f;
m_gravity = 40.0f;
m_jump = 3.0f * m_gravity;
initSides();
}
int manhattanDistance(Node start, Node goal);
void setNodes(Node goal);
std::vector<Node *> identifySuccessors(const Node *current, Node *start,
Node *goal);
std::vector<Node *> nodeNeighbors(const Node *node);
Node *jump(int x, int y, int dX, int dY, Node *start, Node *goal);
Node *nextNode(const std::list<Node *> &nodeList);
Node *getPoolNode(int x, int y);
void initSides();
void preprocessGrid(Node goal);
bool canWalkBetween(Node ¤t, Node &other);
bool canJumpBetween(Node ¤t, Node &other);
float getJumpCost(Node ¤t, Node &other);
private:
const TileMap *m_tileMap;
std::vector<Node> m_nodes;
std::vector<Edge> m_edges;
std::vector<Point> sides;
float m_mass;
float m_velocity;
float m_gravity;
float m_jump;
};
#endif // PATHFINDING_H_