-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.cpp
464 lines (364 loc) · 13.1 KB
/
Map.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "Map.h"
#include "Utils.h"
#include<iostream>
#include<queue>
//Helper function for A*
/*
bool const locCompute(std::pair<Location*, float>, std::pair<Location*, float>);
float calcDist(Location*, Location*); //calculates the distance to target
//helper functions for vector search
bool listSearch(Location * location, std::list<Location*> & visited); //Just loops from the back to the front, more likely to find
*/
Map::Map()
{
init();
// Create array object and buffers. Remember to delete your buffers when the object is destroyed!
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO_vert);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object (VAO) first, then bind the associated buffers to it.
// Consider the VAO as a container for all your buffers.
glBindVertexArray(VAO);
// Now bind a VBO to it as a GL_ARRAY_BUFFER. The GL_ARRAY_BUFFER is an array containing relevant data to what
// you want to draw, such as vertices, normals, colors, etc.
glBindBuffer(GL_ARRAY_BUFFER, VBO_vert);
// glBufferData populates the most recently bound buffer with data starting at the 3rd argument and ending after
// the 2nd argument number of indices. How does OpenGL know how long an index spans? Go to glVertexAttribPointer.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
// Enable the usage of layout location 0 (check the vertex shader to see what this is)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader. In this case, it's 0. Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS.
3, // This second line tells us how any components there are per vertex. In this case, it's 3 (we have an x, y, and z component)
GL_FLOAT, // What type these components are
GL_FALSE, // GL_TRUE means the values should be normalized. GL_FALSE means they shouldn't
3 * sizeof(GLfloat), // Offset between consecutive indices. Since each of our vertices have 3 floats, they should have the size of 3 floats in between
(GLvoid*)0); // Offset of the first vertex's component. In our case it's 0 since we don't pad the vertices array with anything.
// We've sent the vertex data over to OpenGL, but there's still something missing.
// In what order should it draw those vertices? That's why we'll need a GL_ELEMENT_ARRAY_BUFFER for this.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
// Unbind the currently bound buffer so that we don't accidentally make unwanted changes to it.
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind the VAO now so we don't accidentally tamper with it.
// NOTE: You must NEVER unbind the element array buffer associated with a VAO!
glBindVertexArray(0);
//here we set the material for each object
glGetError();
}
Map::~Map()
{
/*
for (Ressource* res : Mountains) {
delete(res);
}
for (Ressource* res :Crystals) {
delete(res);
}
for (Ressource* res : Energies) {
delete(res);
}
*/
for (int i = 0; i < MAPSIZE; i++) {
for (int j = 0; j < MAPSIZE; j++) {
delete(map[i][j]);
}
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO_vert);
glDeleteBuffers(1, &EBO);
}
void Map::init()
{
std::cout << "Maps is innit" << std::endl;
for (int i = 0; i < mapSize; i++) {
for (int j = 0; j < mapSize; j++) {
//std::cout << i << " " << j << std::endl;
map[i][j] = new Location(i, j);
// std::cout << map[i][j]->getPos().x << " " << map[i][j]->getPos().y << std::endl;
}
}
}
Location * Map::getLoc(glm::vec2 pos)
{
return getLoc(int(pos.x),int(pos.y));
}
Location * Map::getLoc(int x, int y)
{
//needs to check if x and y are in bounds
if (x > MAPSIZE - 1) {
//std::cout << "invalid location: " << x << " " << y << std::endl;
x = MAPSIZE - 1;
}
else if (x < 0) {
//std::cout << "invalid location: " << x << " " << y << std::endl;
x = 0;
}
if (y > MAPSIZE - 1) {
//std::cout << "invalid location: " << x << " " << y << std::endl;
y = MAPSIZE - 1;
}
else if (y < 0) {
//std::cout << "invalid location: " << x << " " << y << std::endl;
y = 0;
}
return map[x][y]; //This will always be valid! not always free
}
/*
Unit * Map::getThrone(int ID)
{
return Thrones[ID];
}
Player * Map::getPlayer(int ID)
{
return Players[ID];
}
void Map::addPlayer(int ID, Player * player)
{
Players[ID] = player;
}
void Map::addThrone(int ID, Unit* throne)
{
Thrones[ID] = throne;
}
bool ** Map::getBox(glm::vec2 a, glm::vec2 b)
{
return getBox(a.x, a.y, b.x, b.y);
}
bool ** Map::getBox(int x, int y, int x2, int y2) //This function may be useless? We went with traversing the Locatio* graph.
{
int xsize = abs(x2 - x + 1);
int ysize = abs(y2 - y + 1);
bool ** lockMap = new bool * [xsize]; //allocate first then for loop it
return nullptr;
}
*/
bool Map::isAdjacent(Location * A, Location * B)
{
glm::vec2 Apos = A->getPos();
glm::vec2 Bpos = B->getPos();
//glm::vec2 dirs[4] = { glm::vec2(0.0f, 1.0f), glm::vec2(0.0f, -1.0f), glm::vec2(1.0f, 0.0f), glm::vec2(-1.0f, 0.0f) };
/*
for (glm::vec2 dir : dirs) {
if (Apos + dir == Bpos) {
return true;
}
}*/
if (glm::length(Apos - Bpos) <= 1.1f) { //Change to get isWithinDist
return true;
}
return false;
}
//May write another one to find closest between target and a locateabl?
//And another that finds the closest space occupied by an owner of a certain class type?
Location * Map::findClosest(Location * base) //Maybe a bit slow so don't use for huge bunches at once, cache for group spawn?
{
//Visited is a map which we may want to keep around for caching, eh probs not?
std::unordered_map<Location*, bool> visited;
std::list<Location*> stack; //the current stack to look through, should be a deque
stack.push_back(base);
while (!stack.empty()) {
Location* base = stack.front();
stack.pop_front();
Location * newVert;
if (base->state) {
return base;
}
else { //dfs search
int x = (int)base->getPos().x;
int y = (int)base->getPos().y;
int x2 = 0;
int y2 = 0;
visited[base] = base->state; //we have now visited this vertex
if (x > 0) { //there's a vertex on the left
x2 = x - 1;
y2 = y;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) { //Check if the vertex isn't going to be checked AND hasn't already been
stack.push_back(newVert); //If it wasn't then we add it to the visited list
}
}
if (x < MAPSIZE - 1) { //vertex to the right
x2 = x + 1;
y2 = y;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
if (y > 0) {
x2 = x;
y2 = y - 1;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
if (y < MAPSIZE - 1) {
x2 = x;
y2 = y + 1;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
}
//Now the vertices are added so back to the top of the while loop!
}
//Getting here means the stack becomes empty :(
return nullptr; //then the search has failed. we throw an exception, this should basically end the game. Maybe an easter egg?
//Basically this is a bit dangerous
}
Location * Map::findClosestTo(Location * start, Location * target) //closest point to start toward target
{
//Priority queue, look till free, go toward second location -> min distance
glm::vec2 dirs[4] = { glm::vec2(0.0f, 1.0f), glm::vec2(0.0f, -1.0f), glm::vec2(1.0f, 0.0f), glm::vec2(-1.0f, 0.0f) };
std::priority_queue<std::pair<Location*, float>, std::vector<std::pair<Location*, float>>, decltype(&Utils::locComp)> stack(Utils::locComp);
std::unordered_map<Location*, float> cost;
float newCost = 0;
glm::vec2 newPos;
Location* newLoc;
stack.push(std::pair<Location*, float>(start, 0.0f)); //add the start location
cost.emplace(start, 0.0f);
while (!stack.empty()) {
Location* current = stack.top().first;
stack.pop();
if (current->state) { //if we find a free spot, return
return current;
}
newCost = cost.at(current) + 3; //Everytime we move away from the original point, 3* as bad as getting closer to the target.
for (glm::vec2 dir : dirs) {
newPos = current->getPos() + dir; //Make sure we aren't running off the map
newLoc = getLoc(newPos);
bool cnd = cost.find(newLoc) == cost.end(); //check if it's already there
if (!cnd) {
cnd = newCost < cost.at(newLoc); //check if we found a better path to it,
}
if (cnd ) { //then newLoc is not in cost so we add it to all of them, since it must be taken
cost.emplace(newLoc, newCost);
stack.push(std::pair<Location*, float>(newLoc, newCost + Utils::calcDist(newLoc, target)));
}
}
}
return nullptr;
}
Location * Map::findClosest(Location * base, int bound)
{
//Visited is a map which we may want to keep around for caching, eh probs not?
std::unordered_map<Location*, bool> visited;
std::list<Location*> stack; //the current stack to look through, should be a deque
stack.push_back(base);
while (!stack.empty() && visited.size() < bound) {
Location* base = stack.front();
stack.pop_front();
Location * newVert;
if (base->state) {
return base;
}
else { //dfs search
int x = (int)base->getPos().x;
int y = (int)base->getPos().y;
int x2 = 0;
int y2 = 0;
visited[base] = base->state; //we have now visited this vertex
if (x > 0) { //there's a vertex on the left
x2 = x - 1;
y2 = y;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) { //Check if the vertex isn't going to be checked AND hasn't already been
stack.push_back(newVert); //If it wasn't then we add it to the visited list
}
}
if (x < MAPSIZE - 1) { //vertex to the right
x2 = x + 1;
y2 = y;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
if (y > 0) {
x2 = x;
y2 = y - 1;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
if (y < MAPSIZE - 1) {
x2 = x;
y2 = y + 1;
newVert = getLoc(x2, y2);
if (!Utils::listSearch(newVert, stack) && visited.find(newVert) == visited.end()) {
stack.push_back(newVert);
}
}
}
} //Now the vertices are added so back to the top of the while loop!
}
Location * Map::findClosestTo(Location * start, Location * target, int bound)
{
//Priority queue, look till free, go toward second location -> min distance
glm::vec2 dirs[4] = { glm::vec2(0.0f, 1.0f), glm::vec2(0.0f, -1.0f), glm::vec2(1.0f, 0.0f), glm::vec2(-1.0f, 0.0f) };
std::priority_queue<std::pair<Location*, float>, std::vector<std::pair<Location*, float>>, decltype(&Utils::locComp)> stack(Utils::locComp);
std::unordered_map<Location*, float> cost;
float newCost = 0;
glm::vec2 newPos;
Location* newLoc;
stack.push(std::pair<Location*, float>(start, 0.0f)); //add the start location
cost.emplace(start, 0.0f);
while (!stack.empty() && cost.size() <= bound) { //Enforces bound on visited verts
Location* current = stack.top().first;
stack.pop();
if (current->state) { //if we find a free spot, return
return current;
}
newCost = cost.at(current) + 3; //Everytime we move away from the original point, 3* as bad as getting closer to the target.
for (glm::vec2 dir : dirs) {
newPos = current->getPos() + dir; //Make sure we aren't running off the map
newLoc = getLoc(newPos);
bool cnd = cost.find(newLoc) == cost.end(); //check if it's already there
if (!cnd) {
cnd = newCost < cost.at(newLoc); //check if we found a better path to it,
}
if (cnd) { //then newLoc is not in cost so we add it to all of them, since it must be taken
cost.emplace(newLoc, newCost);
stack.push(std::pair<Location*, float>(newLoc, newCost + Utils::calcDist(newLoc, target)));
}
}
}
return nullptr;
}
void Map::draw()
{
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
/*
bool listSearch(Location * location, std::list<Location*>& list) //linear search from the back
{
std::list<Location *>::iterator it;
for (it = list.end(); it != list.begin();) {
if (*--it == location) {
return true;
}
}
return false;
}
bool const locCompute(std::pair<Location*, float> a, std::pair<Location*, float> b) //compare priority, if the same use x values
{
//Need to be careful, if reflexive false, then keys are equivalent -> prioritize x over y
if (a.second != b.second) {
return a.second > b.second;
}
else { //If they are equal pick greater x
float da = a.first->getPos().x - b.first->getPos().x;
return (da > 0.0);
}
}
float calcDist(Location* a, Location* b) { //Rounded Euclidian distance -> maybe make float
float x = a->getPos().x - b->getPos().x;
float y = a->getPos().y - b->getPos().y;
return (sqrt(x*x + y*y));
}
*/