-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
348 lines (310 loc) · 10.2 KB
/
util.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
#include "util.h"
#include "definitions.h"
#include <algorithm>
//Generate random rooms and also push new room struct to "rooms" vector
void genRandom(Map& map){
for (int y = 0; y < sizey; y++){
for (int x = 0; x < sizex; x++){
map.tiles[y][x] = None;
map.onTop[y][x] = None;
map.blocks[y][x] = 0;
map.word[y][x] = " ";
map.discovered[y][x] = 0.2;
if (y <= 7){ //Top left corner becomes grass
if (x < 5) map.tiles[y][x] = Grass;
if (x == 5) map.tiles[y][x] = None;
if (x <= 5 && y == 7) map.tiles[y][x] = None;
}
int a = rand()%1;
int w = rand()%7+5;
int h = rand()%7+5;
//w = 6; h =6; //Uncomment for very uniform rooms
if (a == 0 && y > h+2 && x > w+5 && x < sizex-2 && y < sizey-2){ //Do not create room too close to edge of area
bool c = true;
//Check the new room is not overlapping another room
for (int y2 = 0; y2 < h+1; y2++){
for (int x2 = 0; x2 < w+1; x2++){
if(map.tiles[y - y2][x - x2] == Floor ||
map.tiles[y - y2][x - x2 + 2] == Floor){
c = false;
}
}
}
//Create new room
if (c){
for (int y2 = 0; y2 < h; y2++){
for (int x2 = 0; x2 < w; x2++){
if(map.tiles[y - y2][x - x2] == None){
map.tiles[y - y2][x - x2] = Floor;
}
}
}
Room r; //Add info to the room struct to be able to find room later
r.lowerx = x; //Sets coordinates of bottom right corner of room
r.lowery = y;
r.room_w = w;
r.room_h = h;
auto treasure_locations = genTreasure(r);
auto trap_locations = genTrap(r, treasure_locations);
for(auto& t: treasure_locations)
{
map.onTop[t.y][t.x] = TreasureTile;
map.treasures.push_back(Treasure{t.x,t.y,1});
}
for(auto& t: trap_locations)
{
map.onTop[t.y][t.x] = TrapTile;
map.traps.push_back(Trap{t.x,t.y, false, std::vector<int>{}});
}
map.rooms.push_back(r);
}
}
}
}
}
bool isLocationValid(Location l, std::list<Location> locations)
{
for(Location& location: locations)
{
if(l.x == location.x &&
l.y == location.y
)
{
return false;
}
}
return true;
}
std::list<Location> genTreasure(Room& room)
{
int area = (room.room_w - 1) * (room.room_h - 1);
// generate [1,MAX_TREASURE], but accomodate for
// size of the room
int treasure_limit = std::min(std::max(1,area / 2), MAX_TREASURES_PER_ROOM);
int treasure_count = rand() % treasure_limit;
std::list<Location> treasure_locations;
for(int i = 0; i < treasure_count; i++)
{
Location l;
while(true)
{
l.x = room.lowerx - 1 - (rand() % (room.room_w - 2));
l.y = room.lowery - 1 - (rand() % (room.room_h - 2));
if(isLocationValid(l, treasure_locations))
break;
}
treasure_locations.push_back(l);
}
return treasure_locations;
}
std::list<Location> genTrap(Room& room, std::list<Location> invalid_locations)
{
int area = (room.room_w - 1) * (room.room_h - 1);
// generate [0,MAX_TRAP] amound of traps per room,
// with accomodation of room size
int trap_limit = std::min(area / 4, MAX_TRAPS_PER_ROOM);
int trap_count = rand() % trap_limit;
std::list<Location> trap_locations;
for(int i = 0; i < trap_count; i++)
{
Location l;
while(true)
{
l.x = room.lowerx - 1 - (rand() % (room.room_w - 2));
l.y = room.lowery - 1 - (rand() % (room.room_h - 2));
if(isLocationValid(l, trap_locations) && isLocationValid(l, invalid_locations))
break;
}
trap_locations.push_back(l);
}
return trap_locations;
}
//Essentially BFS algorithm to reach a tile equal to "find" and change all tiles along the shortest path between the points to "change"
//y and x are the starting coordinates
void Dij(Map& map, int y, int x, enum category find, enum category change){
map.extra[y][x] = 1;
std::vector<Tree> leaves;
Tree l;
l.L = y;
l.R = x;
l.parent = -1;
leaves.push_back(l);
for(int n = 0; n < 50; n++){
int a = 0;
int mm = -1;
for (auto elem : leaves){
mm++;
auto j = elem.L;
auto i = elem.R;
if (map.tiles[j][i] == find ) {
int point = mm;
point = leaves[point].parent; //Do not change the final destination tile
while(1){
int yy = leaves[point].L;
int xx = leaves[point].R;
map.tiles[yy][xx] = change;
map.word[yy][xx] = " ";
point = leaves[point].parent;
if (point == -1) break;
}
a = 1;
break;
}
if ( j < 1 || i < 1 || j > sizey-1 || i > sizex-1) continue;
if ( map.blocks[j][i+1] == 0 && map.extra[j][i+1] == 0 ){
Tree p;
p.L = j;
p.R = i+1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j][i-1] == 0 && map.extra[j][i-1] == 0 ){
Tree p;
p.L = j;
p.R = i-1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j+1][i] == 0 && map.extra[j+1][i] == 0 ){
Tree p;
p.L = j+1;
p.R = i;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j-1][i] == 0 && map.extra[j-1][i] == 0){
Tree p;
p.L = j-1;
p.R = i;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
//Diagonal tiles
if ( map.blocks[j+1][i+1] == 0 && map.extra[j+1][i+1] == 0 ){
Tree p;
p.L = j+1;
p.R = i+1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j-1][i-1] == 0 && map.extra[j-1][i-1] == 0 ){
Tree p;
p.L = j-1;
p.R = i-1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j+1][i-1] == 0 && map.extra[j+1][i-1] == 0 ){
Tree p;
p.L = j+1;
p.R = i-1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
if ( map.blocks[j-1][i+1] == 0 && map.extra[j-1][i+1] == 0){
Tree p;
p.L = j-1;
p.R = i+1;
p.parent = mm;
leaves.push_back(p);
map.extra[p.L][p.R] = 1;
}
}
if (a) break;
}
for (int j = 0; j < sizey; j++){
for (int i = 0; i < sizex; i++){
map.extra[j][i] = 0;
}
}
}
void removeDirection(std::vector<Direction>& vec, Direction d)
{
vec.erase(std::remove(vec.begin(), vec.end(), d), vec.end());
}
bool isTileOccupiable(int x, int y, Map& map)
{
if(x < 0 || x > sizex)
{
return false;
}
if(y < 0 || y > sizey)
{
return false;
}
if(map.tiles[y][x] == Grass ||
map.tiles[y][x] == Floor ||
map.tiles[y][x] == Hallway ||
map.tiles[y][x] == TrapTile ||
map.tiles[y][x] == TreasureTile ||
map.tiles[y][x] == Door)
{
return true;
}
else
{
return false;
}
}
vector<Direction> getMovementDirections(int x, int y, Map& map)
{
std::vector<Direction> possible_directions{
STAY
};
if(isTileOccupiable(x-1,y,map))
possible_directions.push_back(LEFT);
if(isTileOccupiable(x+1,y,map))
possible_directions.push_back(RIGHT);
if(isTileOccupiable(x,y-1,map))
possible_directions.push_back(UP);
if(isTileOccupiable(x,y+1,map))
possible_directions.push_back(DOWN);
if(isTileOccupiable(x-1,y-1,map))
possible_directions.push_back(UP_LEFT);
if(isTileOccupiable(x+1,y-1,map))
possible_directions.push_back(UP_RIGHT);
if(isTileOccupiable(x-1,y+1,map))
possible_directions.push_back(DOWN_LEFT);
if(isTileOccupiable(x+1,y+1,map))
possible_directions.push_back(DOWN_RIGHT);
return possible_directions;
}
std::vector<Agent> createAgents(int numAgents, int startX, int startY, Map& map)
{
std::vector<Agent> agents;
for(int i = 0; i < numAgents; i++)
{
agents.push_back(Agent(startX+rand()%3+2,startY+rand()%4+2, numAgents, map, i));
}
return agents;
}
int isAgentOnTile(int x, int y, std::vector<Agent>& agents, Map& map)
{
for(size_t i = 0; i < agents.size(); i++)
{
if(agents[i].m_x == x && agents[i].m_y == y)
{
return i;
}
}
return -1;
}
std::vector<int> getAgentsOnTile(int x, int y, std::vector<Agent>& agents, Map& map)
{
std::vector<int> agentsOnTile;
for(size_t i = 0; i < agents.size(); i++)
{
if(agents[i].m_x == x && agents[i].m_y == y && agents[i].m_health > 0)
{
agentsOnTile.push_back(i);
}
}
return agentsOnTile;
}