forked from doxman/chamber-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloor.cc
255 lines (251 loc) · 5.88 KB
/
floor.cc
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
#include "floor.h"
#include <fstream>
#include <cstdlib>
using namespace std;
// PRIVATE METHODS
void Floor::flood(char floodChar, char ***gridptr, int r, int c, int chamberNum)
{
char **grid = *gridptr;
posn temp = {r, c};
chambers[chamberNum].tiles.push_back(temp);
grid[r][c] = floodChar;
if (r > 0 && grid[r-1][c]==FLOOR)
flood(floodChar, &grid, r-1, c, chamberNum);
if (r < (HEIGHT - 1) && grid[r+1][c]==FLOOR)
flood(floodChar, &grid, r+1, c, chamberNum);
if (c > 0 && grid[r][c-1]==FLOOR)
flood(floodChar, &grid, r, c-1, chamberNum);
if (c < (WIDTH - 1) && grid[r][c+1]==FLOOR)
flood(floodChar, &grid, r, c+1, chamberNum);
}
void Floor::floodGrid (char **grid)
{
char floodChar = 'a';
int chamberNum = floodChar - 'a';
for (int r = 0; r < HEIGHT; r++)
{
for (int c = 0; c < WIDTH; c++)
{
if (grid[r][c] == FLOOR)
{
flood(floodChar, &grid, r,c, chamberNum);
floodChar++;
chamberNum++;
}
}
}
}
void Floor::unfloodGrid(char **grid)
{
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
if (grid[i][j] >= 'a' && grid[i][j] <= 'e') // If the tile was previously flooded
grid[i][j] = FLOOR;
}
}
}
void Floor::spawnObject(char c)
{
int chamberNumber = -1;
// Makes sure player doesn't spawn in the same room as the stairs
if (c == STAIRS)
{
chamberNumber = rand() % (numChambers - 1);
if (chamberNumber >= playerChamber)
chamberNumber++;
}
else
chamberNumber = rand() % numChambers;
// Stores player's chamber value for reference later
if (c == PLAYER)
playerChamber = chamberNumber;
int numTiles = chambers[chamberNumber].tiles.size();
int tileNumber = rand() % numTiles;
posn temp = chambers[chamberNumber].tiles[tileNumber];
if (c == PLAYER)
{
player.initLoc(temp);
d->setChar(temp.row, temp.col, c);
}
else if (c == STAIRS)
{
stairs.initLoc(temp);
d->setChar(temp.row, temp.col, c);
}
else if (c == POTION)
{
potions[numPotions].initLoc(temp);
d->setChar(temp.row, temp.col, c);
numPotions++;
}
else if (c == GOLD)
{
golds[numGolds].initLoc(temp);
d->setChar(temp.row, temp.col, c);
numGolds++;
}
else if (c == ENEMY)
{
enemies[numEnemies].initLoc(temp);
d->setChar(temp.row, temp.col, c);
numEnemies++;
}
chambers[chamberNumber].tiles.erase(chambers[chamberNumber].tiles.begin() + tileNumber);
}
void Floor::sortEnemies()
{
Enemy sorted[numEnemies];
int numSorted = 0;
int currentMin; // Value of smallest remaining element: (79 * row) + col
int minIndex; // Location of smallest remaining element
int threshold = -1; // Lowest value we check; prevents counting the same element twice
int temp; // Temporarily holds value of each element
while (numSorted < numEnemies)
{
currentMin = 79 * 25;
minIndex = -1;
for (int i = 0; i < numEnemies; i++)
{
temp = 79 * enemies[i].getLoc().row + enemies[i].getLoc().col;
if (temp < currentMin && temp > threshold)
{
currentMin = temp;
minIndex = i;
}
}
sorted[numSorted] = enemies[minIndex];
threshold = currentMin;
numSorted++;
}
// Sorted; now set established array to be sorted
for (int i = 0; i < numEnemies; i++)
enemies[i] = sorted[i];
}
void Floor::moveEnemy(Enemy *e)
{
posn loc = e->getLoc();
bool open[numDirections]; // Need to initialize these to false
for (int i = 0; i < numDirections; i++)
open[i] = false;
int availableMoves = 0;
if (*d->getChar(loc.row - 1, loc.col) == FLOOR)
{
open[NORTH] = true;
availableMoves++;
}
if (*d->getChar(loc.row - 1, loc.col - 1) == FLOOR)
{
open[NORTHWEST] = true;
availableMoves++;
}
if (*d->getChar(loc.row - 1, loc.col + 1) == FLOOR)
{
open[NORTHEAST] = true;
availableMoves++;
}
if (*d->getChar(loc.row, loc.col - 1) == FLOOR)
{
open[WEST] = true;
availableMoves++;
}
if (*d->getChar(loc.row, loc.col + 1) == FLOOR)
{
open[EAST] = true;
availableMoves++;
}
if (*d->getChar(loc.row + 1, loc.col - 1) == FLOOR)
{
open[SOUTHWEST] = true;
availableMoves++;
}
if (*d->getChar(loc.row + 1, loc.col + 1) == FLOOR)
{
open[SOUTHEAST] = true;
availableMoves++;
}
if (*d->getChar(loc.row + 1, loc.col) == FLOOR)
{
open[SOUTH] = true;
availableMoves++;
}
if (availableMoves == 0) // All adjacent tiles are blocked
return;
d->setChar(loc.row, loc.col, FLOOR); // Clears the tile the enemy is leaving
int move = rand() % availableMoves;
int counter = 0;
for (int i = NORTH; i < numDirections; i++)
{
if (open[i] && counter == move)
{
e->move(i);
break;
}
if (open[i])
counter++;
}
loc = e->getLoc(); // New position
d->setChar(loc.row, loc.col, e->getObjectChar()); // Draws the enemy's new position
}
// PUBLIC METHODS
Floor::Floor()
{
d = new Display();
playerChamber = -1;
numPotions = 0;
numGolds = 0;
numEnemies = 0;
}
Floor::~Floor()
{
delete d;
}
void Floor::init()
{
ifstream f("test.ccmap");
f >> *d;
char **temp;
temp = d->getDisplay();
floodGrid(temp);
unfloodGrid(temp);
srand(time(NULL)); // Sets up rand()
spawnObject(PLAYER);
spawnObject(STAIRS);
while(numPotions < potionsSpawned)
spawnObject(POTION);
while(numGolds < goldsSpawned)
spawnObject(GOLD);
while(numEnemies < enemiesSpawned)
spawnObject(ENEMY);
sortEnemies();
}
void Floor::round()
{
sortEnemies();
for (int i = 0; i < numEnemies; i++)
moveEnemy(&enemies[i]);
}
void Floor::print()
{
cout << *d << endl;
// Print list of posns in each chamber
/* BLOCK SAVED FOR TESTING
for (int i = 0; i < numChambers; i++)
{
cout << "Chamber: " << i << endl;
int numTiles = chambers[i].tiles.size();
for (int j = 0; j < numTiles; j++)
cout << "(" << chambers[i].tiles[j].row << ", "
<< chambers[i].tiles[j].col << ")" << endl;
}
// BLOCK SAVED FOR TESTING*/
// Print coordinates of each enemy, in sorted order
/* BLOCK SAVED FOR TESTING
for (int i = 0; i < numEnemies; i++)
{
cout << "Enemy " << i << " position: " << enemies[i].getLoc().row
<< ", " << enemies[i].getLoc().col << endl;
}
// BLOCK SAVED FOR TESTING*/
}