forked from Dzejrou/tdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.hpp
201 lines (174 loc) · 5.35 KB
/
Grid.hpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#include <Ogre.h>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
#include "EntitySystem.hpp"
#include "Components.hpp"
#include "Helpers.hpp"
#include "Enums.hpp"
#include "Util.hpp"
/**
* Class representing the pathfinding grid.
*/
class Grid
{
/**
* GameSerializer is a friend class so that it can easily access the grid realted data
* (like dimensions and node distance) when saving the game.
*/
friend class GameSerializer;
public:
/**
* Brief: Returns true if a given node is in the grid.
* Param: ID of the node.
*/
bool in_board(std::size_t) const;
/**
* Brief: Returns a constant reference to the list of
* freed nodes.
*/
const std::set<std::size_t>& get_freed() const;
/**
* Brief: Returns a constant reference to the list of
* unfreed nodes.
*/
const std::set<std::size_t>& get_unfreed() const;
/**
* Brief: Removes all nodes from the list of freed nodes.
*/
void clear_freed();
/**
* Brief: Removes all nodes from the list of unfreed nodes.
*/
void clear_unfreed();
/**
* Brief: Created a new node at the given position.
* Param: EntitySystem that contains the node.
* Param: 2D position of the node.
*/
std::size_t add_node(EntitySystem&, Ogre::Vector2);
/**
* Brief: Adds a given node to the list of the freed nodes.
* Param: ID of the node.
*/
void add_freed(std::size_t);
/**
* Brief: Adds a given node to the list of the unfreed nodes.
* Param: ID of the node.
*/
void add_unfreed(std::size_t);
/**
* Brief: Removes a given node from the node list.
* Param: ID of the node.
* TODO: Possibly implement "unlink"?
*/
void remove_node(std::size_t);
/**
* Brief: Returns the ID of a node at a given position in the grid.
* Param: Column number.
* Param: Row number.
*/
std::size_t get_node(std::size_t, std::size_t) const;
/**
* Brief: Returns the ID of a node that is closed to a given world coorinate.
* Param: X axis coordinate.
* Param: Z axis coordinate.
* Note: Adding the ability to specify in what direction the node must be might
* be beneficial for pathfinding.
*/
std::size_t get_node_from_position(Ogre::Real, Ogre::Real) const;
/**
* Brief: Generates a grid graph with the given parameters to be used for
* pathfinding.
* Param: EntitySystem that will contain the nodes.
* Param: Starting position (x,z axes) of the grid.
* Param: Width of the graph (in node count).
* Param: Height of the graph (in node count).
* Param: Distance between adjascent nodes.
*/
void create_graph(EntitySystem&, Ogre::Vector2, std::size_t, std::size_t, Ogre::Real);
/**
* Brief: Returns the distance between two nodes in the four non-diagonal
* directions.
*/
Ogre::Real get_distance() const;
/**
* Brief: Returns a reference to the static instance of this class.
* Note: Handles initialization and safe destruction by itself.
* TODO: Possibly unite this and Lpp::Script under one singleton interface?
*/
static Grid& instance();
/**
* Breif: Returns a random node within the graph.
*/
std::size_t get_random_free_node() const;
/**
* Brief: Returns the 2D position of the central node of the grid (or one of them
* if the grid has even dimensions).
* Param: EntitySystem that contains components of the nodes.
*/
Ogre::Vector2 get_center_position(EntitySystem&) const;
/**
* Since there should be only one grid at all times accesible from the Grid::instance
* method, all copy/move operations are disabled for this class.
*/
Grid(const Grid&) = delete;
Grid& operator=(const Grid&) = delete;
Grid(Grid&&) = delete;
Grid& operator=(Grid&&) = delete;
private:
/**
* Constructor.
* Kept private since there should be only one grid at all times.
*/
Grid() = default;
/**
* Destructor.
*/
~Grid() {}
/**
* Brief: Generates a neighbour list for a given node (thus linking it to the graph).
* Param: ID of the node.
* Param: Auxuliary vector containing component pointers for fast access.
* (This method will ever be called only in the GridSystem::create_graph method,
* which already has such a vector and so it's used here too.)
*/
void link_(std::size_t, std::vector<GridNodeComponent*>&);
/**
* Vector containing the IDs of the nodes in the grid, basically
* representing a 2D matrix stored in a 1D container.
*/
std::vector<std::size_t> nodes_;
/**
* Auxiliary vectors containing IDs of the nodes that have been
* freed/unfreed on last frame. Used for pathfinding correction
* and structure model changes.
*/
std::set<std::size_t> freed_, unfreed_;
/**
* Dimensions of the grid in node count.
* (Actual dimensions = dimensions * distance.)
*/
std::size_t width_, height_;
/**
* Distance between two adjascent nodes.
*/
Ogre::Real distance_;
/**
* ID of the first node, this allows for the node IDs to be outside the
* (0, width_ * height_) range,
*/
std::size_t starting_index_;
/**
* Coordinates of the starting node of the grid.
* (This 2D vector contains X and Z coordinates despite it's second member
* being names Y).
*/
Ogre::Vector2 start_;
/**
* Used for easier returning of a random free node.
*/
std::vector<std::size_t> free_nodes_;
};