-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCPathfinder.h
155 lines (125 loc) · 3.98 KB
/
CPathfinder.h
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
153
154
155
#ifndef CPATHFINDER_H
#define CPATHFINDER_H
#include <map>
#include <vector>
#include <iostream>
#include "headers/HEngine.h"
#include "headers/Defines.h"
#include "ARegistrar.h"
#include "AAStar.h"
#include "CThreatMap.h"
// how far a unit can advance on its path?
#define MOVE_BUFFER 2
// number of frames to give a group for regrouping
#define FRAMES_TO_REGROUP 30
// cache version (stored in file header)
#define CACHE_VERSION "CACHEv04"
class CGroup;
class AIClasses;
class CPathfinder: public AAStar, public ARegistrar {
public:
CPathfinder(AIClasses *ai);
~CPathfinder();
class Node: public ANode {
public:
Node(unsigned short int id, unsigned char x, unsigned char z, float w) : ANode(id, w) {
this->x = x;
this->z = z;
}
std::map<int, std::vector<unsigned short int> > neighbours;
unsigned char x, z;
float3 toFloat3() const {
float fx = x * PATH2REAL;
float fy = 0.0f;
float fz = z * PATH2REAL;
return float3(fx, fy, fz);
}
void serialize(std::ostream& os);
static Node* unserialize(std::istream& is) {
unsigned char x, z;
unsigned short int m, id;
char N, M, K;
is.read((char*)&id, sizeof(unsigned short int));
is.read((char*)&x, sizeof(unsigned char));
is.read((char*)&z, sizeof(unsigned char));
Node *n = new Node(id, x, z, 1.0f);
is.read((char*)&N, sizeof(char));
for (unsigned int i = 0; i < N; i++) {
is.read((char*)&K, sizeof(char));
std::vector<unsigned short int> V;
n->neighbours[(int)K] = V;
is.read((char*)&M, sizeof(char));
for (unsigned int j = 0; j < M; j++) {
is.read((char*)&m, sizeof(unsigned short int));
n->neighbours[(int)K].push_back(m);
}
}
return n;
}
};
/* Get estimated path lenfth a group need to travel */
float getPathLength(CGroup&, float3&);
/* Get estimated time of arrival */
float getETA(CGroup&, float3&);
/* Update groups following paths */
void updateFollowers();
/* update the path itself NOTE: should always be called after updateFollowers() */
void updatePaths();
/* Add a group to the pathfinder */
bool addGroup(CGroup &group);
/* Overload */
void remove(ARegistrar &obj);
/* Get closest Node id to real world vector f, return NULL on failure */
Node* getClosestNode(const float3& f, float radius = PATH2REAL, CGroup* group = NULL);
/* Returns ERRORVECTOR on failure */
float3 getClosestPos(const float3& f, float radius = PATH2REAL, CGroup* group = NULL);
bool isBlocked(float x, float z, int movetype);
bool isBlocked(int x, int z, int movetype);
bool pathExists(CGroup &group, const float3 &s, const float3 &g);
bool pathAssigned(CGroup &group);
bool switchDebugMode(bool graph);
float REAL;
protected:
AIClasses *ai;
private:
/* Node Graph */
static std::vector<Node*> graph;
/* Group to receive repathing event next updatePaths() call */
int repathGroup;
/* Active map (graph[activeMap]), CRUCIAL to A* */
int activeMap;
/* Controls which path may be updated (round robin-ish) */
unsigned int update;
/* The paths <group_id, path> */
std::map<int, std::vector<float3> > paths;
/* The groups <group_id, CGroup*> */
std::map<int, CGroup*> groups;
/* Regrouping <group_id, frame_number> */
std::map<int, int> regrouping;
int X,Z,XX,ZZ;
unsigned int graphSize;
bool drawPaths;
static int drawPathGraph;
const float *sm;
const float *hm;
/* overload */
void successors(ANode *an, std::queue<ANode*> &succ);
/* overload */
float heuristic(ANode *an1, ANode *an2);
/* Add a path to a unit or group */
bool addPath(CGroup&, float3 &start, float3 &goal);
/* Reset weights of all nodes */
void resetWeights(CGroup&);
/* Adjust weights of all nodes based on threatmap layer */
void applyThreatMap(ThreatMapType tm_type);
/* Calculate the nodes */
void calcNodes();
/* Determine the nodes their neighbours */
void calcGraph();
/* Start pathfinding ("radius" not implemented yet) */
bool getPath(float3 &s, float3 &g, std::vector<float3> &path, CGroup&);
/* Draw the map */
void drawGraph(int map);
void drawNode(Node *n);
};
#endif