-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.cpp
394 lines (350 loc) · 10.2 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
#include "map.h"
#include <QDebug>
#include <QDir>
#include <QJsonArray>
#include <QSettings>
Map::Map(QObject *parent) : QObject(parent),
m_width(0),
m_height(0),
m_pelletsLeft(0),
m_totalPellets(0)
{
QSettings settings;
if (!loadMap(settings.value("map", ":/maps/default.txt").toString())) {
loadMap(":/maps/default.txt");
}
}
bool Map::loadMap(const QString &filepath)
{
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Map: Unable to open map file:" << filepath;
m_parseError = "Map: Unable to open map file: " + filepath;
return false;
}
QVector<TileType> tiles;
int width = -1;
int height = 0;
QVector<QPoint> startingPositions;
QPoint monsterSpawn;
int totalPellets = 0;
while (!file.atEnd()) {
QByteArray line = file.readLine().trimmed();
if (line.isEmpty()) {
continue;
}
if (width == -1) {
width = line.length();
}
if (line.length() > 0 && line.length() != width) {
qWarning() << "Invalid map line at line" << height << "width:" << line.length() << line;
m_parseError = "Invalid map line at line number " + QString::number(height) + " width: " + QString::number(line.length()) + "\nContents: " + line;
return false;
}
int x = 0;
for (const char tile : line) {
switch(tile) {
case '_':
tiles.append(FloorTile);
break;
case '|':
tiles.append(WallTile);
break;
case '-':
tiles.append(DoorTile);
break;
case '.':
tiles.append(PelletTile);
totalPellets++;
break;
case 'o':
tiles.append(SuperPelletTile);
break;
case '#':
tiles.append(FloorTile);
startingPositions.append(QPoint(x, height));
break;
case '@':
tiles.append(FloorTile);
monsterSpawn = QPoint(x, height);
break;
default:
qWarning() << "Invalid tile: '" << tile << "'";
m_parseError = QStringLiteral("Invalid tile '") + tile + "' at line number " + QString::number(height) + ":\n " + line;
return false;
}
x++;
}
height++;
}
// Map is properly parsed, now we can modify ourselves
m_name = filepath;
m_tiles.clear();
m_startingPositions = startingPositions;
m_totalPellets = totalPellets;
m_monsterSpawn = monsterSpawn;
m_width = width;
m_height = height;
m_tiles = tiles;
emit mapChanged();
emit totalPelletsChanged();
m_powerups.clear();
m_pelletsLeft = 0;
for (int y=0; y<m_height; y++) {
for (int x=0; x<m_width; x++) {
switch(tileAt(x, y)) {
case PelletTile:
m_powerups.append(NormalPellet);
m_pelletsLeft++;
break;
case SuperPelletTile:
m_powerups.append(SuperPellet);
break;
default:
m_powerups.append(NoPowerup);
}
}
}
QSettings settings;
settings.setValue("map", filepath);
return true;
}
QStringList Map::availableMaps()
{
QDir dir(":/maps/");
QStringList ret;
for (const QFileInfo &file : dir.entryInfoList(QDir::Files)) {
ret.append(file.absoluteFilePath());
}
return ret;
}
void Map::resetPowerups()
{
if (m_pelletsLeft == m_totalPellets) {
return;
}
qDebug() << "Resetting powerups";
if (m_powerups.size() != m_tiles.size()) {
m_powerups.resize(m_tiles.size());
}
m_pelletsLeft = 0;
for (int y=0; y<m_height; y++) {
for (int x=0; x<m_width; x++) {
const int pos = y * m_width + x;
Powerup type;
switch(m_tiles[pos]) {
case PelletTile:
type = NormalPellet;
m_pelletsLeft++;
break;
case SuperPelletTile:
type = SuperPellet;
break;
default:
continue;
}
if (type == m_powerups[pos]) {
continue;
}
m_powerups[pos] = type;
emit powerupVisibleChanged(x, y, true);
}
}
emit pelletsLeftChanged();
}
bool Map::isValid() const
{
return (!m_tiles.isEmpty() && m_width * m_height == m_tiles.size());
}
bool Map::isValidPosition(int x, int y) const
{
if (!isWithinBounds(x, y)) {
return false;
}
TileType tile = tileAt(x, y);
return (tile != WallTile && tile != InvalidTile);
}
bool Map::isWithinBounds(int x, int y) const
{
if (x < 0 || x >= m_width)
return false;
if (y < 0 || y >= m_height)
return false;
return true;
}
QString Map::name() const
{
return m_name;
}
Map::Powerup Map::powerupAt(int x, int y) const
{
if (!isWithinBounds(x, y)) {
return NoPowerup;
}
const quint32 pos = y * m_width + x;
if (pos >= m_powerups.size()) {
return NoPowerup;
}
return m_powerups[pos];
}
Map::Powerup Map::takePowerup(int x, int y)
{
if (!isWithinBounds(x, y)) {
return NoPowerup;
}
const int pos = y * m_width + x;
if (pos >= m_powerups.size()) {
return NoPowerup;
}
Powerup powerup = m_powerups[pos];
if (powerup == NoPowerup) {
return powerup;
}
if (powerup == NormalPellet) {
m_pelletsLeft--;
emit pelletsLeftChanged();
}
m_powerups[pos] = NoPowerup;
emit powerupVisibleChanged(x, y, false);
return powerup;
}
Map::TileType Map::tileAt(int x, int y) const
{
if (!isWithinBounds(x, y)) {
return InvalidTile;
}
const quint32 pos = y * m_width + x;
if (pos >= m_tiles.length()) {
return InvalidTile;
}
return m_tiles[pos];
}
QString Map::tileSprite(int x, int y, TileCorner corner) const
{
TileType tile = tileAt(x,y);
if (tile == DoorTile) {
if (tileAt(x + 1, y) == WallTile || tileAt(x - 1, y) == WallTile) {
return "door-horizontal";
} else {
return "door-vertical";
}
} else if (tile != WallTile) {
return "floor";
}
const bool wallNorth = (tileAt(x, y - 1) == WallTile);
const bool wallSouth = (tileAt(x, y + 1) == WallTile);
const bool wallWest = (tileAt(x - 1, y) == WallTile);
const bool wallEast = (tileAt(x + 1, y) == WallTile);
const bool wallSouthEast = (tileAt(x + 1, y + 1) == WallTile);
const bool wallSouthWest = (tileAt(x - 1, y + 1) == WallTile);
const bool wallNorthEast = (tileAt(x + 1, y - 1) == WallTile);
const bool wallNorthWest = (tileAt(x - 1, y - 1) == WallTile);
switch (corner) {
case UpperLeft:
if (!wallNorth && !wallWest && !wallNorthWest) {
return "wall-corner";
} else if (!wallNorth && wallWest) {
return "wall-horizontal";
} else if (wallNorth && !wallWest) {
return "wall-vertical";
} else if (wallWest && wallNorth && !wallNorthWest) {
return "wall-corner-inverted";
} else if (wallNorth && wallWest && wallNorthWest) {
return "floor";
}
break;
case UpperRight:
if (!wallNorth && !wallEast && !wallNorthEast) {
return "wall-corner";
} else if (!wallNorth && wallEast) {
return "wall-horizontal";
} else if (wallNorth && !wallEast) {
return "wall-vertical";
} else if (wallEast && wallNorth && !wallNorthEast) {
return "wall-corner-inverted";
} else if (wallNorth && wallEast && wallNorthEast) {
return "floor";
}
break;
case BottomLeft:
if (!wallSouth && !wallWest && !wallSouthWest) {
return "wall-corner";
} else if (!wallSouth && wallWest) {
return "wall-horizontal";
} else if (wallSouth && !wallWest) {
return "wall-vertical";
} else if (wallWest && wallSouth && !wallSouthWest) {
return "wall-corner-inverted";
} else if (wallSouth && wallWest && wallSouthWest) {
return "floor";
}
break;
case BottomRight:
if (!wallSouth && !wallEast && !wallSouthEast) {
return "wall-corner";
} else if (!wallSouth && wallEast) {
return "wall-horizontal";
} else if (wallSouth && !wallEast) {
return "wall-vertical";
} else if (wallEast && wallSouth && !wallSouthEast) {
return "wall-corner-inverted";
} else if (wallSouth && wallEast && wallSouthEast) {
return "floor";
}
break;
}
return "invalid";
}
QString Map::powerupSprite(int x, int y) const
{
switch(tileAt(x, y)) {
case PelletTile:
return "pellet";
case SuperPelletTile:
return "superpellet";
default:
return QString();
}
}
QJsonObject Map::getSerialized() const
{
QJsonObject ret;
ret["width"] = width();
ret["height"] = height();
ret["pelletsleft"] = pelletsLeft();
QJsonArray mapData;
const TileType *tiles = m_tiles.data();
const Powerup *powerups = m_powerups.data();
for (int y=0; y<m_height; y++) {
char *row = new char[m_width + 1];
for (int x=0; x<m_width; x++) {
const quint32 index = y * m_width + x;
switch (powerups[index]) {
case NormalPellet:
row[x] = '.';
continue;
case SuperPellet:
row[x] = 'o';
continue;
default:
break;
}
switch(tiles[index]) {
case WallTile:
row[x] = '|';
break;
case DoorTile:
row[x] = '-';
break;
default:
row[x] = '_';
break;
}
}
row[m_width] = '\0';
mapData.append(row);
delete [] row;
}
ret["content"] = mapData;
return ret;
}