-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
307 lines (260 loc) · 8.12 KB
/
Map.cpp
File metadata and controls
307 lines (260 loc) · 8.12 KB
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
#include "settings.h"
#include "precomp.h"
#include "Map.h"
#include "game.h"
namespace Tmpl8 {
const char* Map::ReadBetweenAsString(const char* start, const char* end) {
long int length = static_cast<long int>(end - start);
char* const string = (char*)MALLOC64(length + 1);
for (int i = 0; i < length; i++) {
string[i] = start[i];
}
string[length] = 0;
return string;
}
int Map::ReadBetweenAsInt(const char* start, const char* end) {
int i = 0;
int length = 0;
while (start + i != end) {
if (start[i] != '\n' && start[i] != '\t') {
length++;
}
i++;
}
i = 0;
char* snum = (char*)MALLOC64(length + 1);
ASSERT_VAR(snum, snum != 0, "Could not allocate memory for parsing int");
if (snum != 0) {
while (start + i != end) {
snum[i] = start[i];
i++;
}
snum[length] = 0;
int num = atoi(snum);
FREE64(snum);
return num;
}
return -1;
}
void Map::ReadBetweenAsIntArr(const char* start, const char* end, const char* separator, int length, IntArray& result) {
const char* cursor = start;
int i = 0;
for (; i < length - 1; i++) {
const char* nextvalue = strstr(cursor, separator);
int value = ReadBetweenAsInt(cursor, nextvalue) - 1; // Tiled shifts all indecies by one
result[i] = value;
cursor += (nextvalue - cursor) + 1;
}
result[i] = ReadBetweenAsInt(cursor, end) - 1;
}
const char* Map::ReadFromFile(const char* filename) const {
FILE* file;
file = fopen(filename, "rb");
if (file == nullptr) {
FatalError("File not found: %s", filename);
return false;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
ASSERT_VAR(size, size >= 0, "File must not be empty");
char* rawcontents = (char*)MALLOC64(size + 1);
if (rawcontents != 0) {
fread(rawcontents, size, 1, file);
rawcontents[size] = 0;
fclose(file);
}
else {
fclose(file);
ASSERT_VAR(rawcontents, rawcontents != 0, "Contents pointer must not be 0");
}
long toremove = 0;
for (long c = 0; c < size; c++) {
if (rawcontents[c] == '\n' || rawcontents[c] == '\r') {
toremove++;
}
}
char* contents = (char*)MALLOC64(size + 1 - toremove);
long writeindex = 0;
long readindex = 0;
for (; readindex < size; readindex++) {
if (rawcontents[readindex] == '\n' || rawcontents[readindex] == '\r') {
continue;
}
contents[writeindex++] = rawcontents[readindex];
}
FREE64(rawcontents);
return contents;
}
void Map::Init(Game* context) {
m_game = context;
m_layers.Reset(LAYERS_NUMBER);
IntArray(MAP_SIZE, EMPTY_TILE).MoveTo(m_layers[WALLS_LAYER_INDEX].m_tiles);
}
bool Map::ReadFromTMX(const char* filename) {
const char* contents = ReadFromFile(filename);
ASSERT(contents != nullptr, "Contents must not be nullptr");
int readLayerCount = 0;
const char* cursor = contents;
while (true) {
cursor = strstr(cursor, "<layer id=");
if (cursor == nullptr) {
break;
}
cursor += 9;
readLayerCount++;
}
cursor = contents;
for (int l = 0; l < readLayerCount; l++){
cursor = strstr(cursor, "<layer") + 6;
ASSERT(cursor != nullptr, "Cursor must not be nullptr");
//const char* hnamestart = strstr(cursor, "name=") + 5;
//const char* hnameend = strstr(hnamestart, "\"");
const char* hdatastart = strstr(cursor, "<data encoding=\"csv\">") + 21;
const char* hdataend = strstr(cursor, "</data>");
const int length = MAP_WIDTH * MAP_HEIGHT;
IntArray data(length);
ReadBetweenAsIntArr(hdatastart, hdataend, ",", length, data);
data.MoveTo(m_layers[l].m_tiles);
}
FREE64((void*)contents);
return true;
}
void Map::GenerateWalls() {
for (int y{ 1 }; y < MAP_HEIGHT - 1; y++) {
for (int x{ 1 }; x < MAP_WIDTH - 1; x++) {
if (x < SAFE_ZONE.x && y < SAFE_ZONE.y) continue; // leave an empty area at top-left
bool canPlace{ true };
if (m_layers[MAP_LAYER_INDEX].GetTileAtGridPosition(int2(x, y)) != EMPTY_TILE) {
continue;
}
for (int p{ 0 }; p < m_game->m_playerNum; p++) {
int2 playerPos = WorldToGrid(m_game->GetLevel()->m_players[p]->GetPosition());
if (playerPos.x == x && playerPos.y == y) {
canPlace = false;
break;
}
}
if (!canPlace) continue;
if (RandomFloat() <= WALLS_DENSITY) {
m_layers[WALLS_LAYER_INDEX].SetTileAtGridPosition(int2(x, y), BREAKABLE_WALL);
m_game->GetLevel()->m_softBlocksLeft++;
}
}
}
}
void Map::Clear() {
for (int y{ 0 }; y < MAP_HEIGHT; y++) {
for (int x{ 0 }; x < MAP_WIDTH; x++) {
m_layers[WALLS_LAYER_INDEX].SetTileAtGridPosition(int2(x, y), EMPTY_TILE);
}
}
}
void Map::Render() {
for (int layerIndex{ 0 }; layerIndex < m_layers.GetLength(); layerIndex++) {
for (int tileIndex{ 0 }; tileIndex < m_layers[layerIndex].GetLength(); tileIndex++) {
const int2 pos{ GetGridCoordinateByIndex(tileIndex) };
const int id{ m_layers[layerIndex].GetTileAtIndex(tileIndex) };
if (id == EMPTY_TILE) continue;
if (Game::IsIDOpaque(id)) {
// opaque tiles can be just copied over to screen
const Surface* const surface{ &m_game->m_sheet.GetSurfaceWithID(id) };
surface->CopyTo(m_game->logicscreen, pos.x, pos.y);
}
else {
// non fully opaque tiles must be drawn with respect to transparency
const Sprite* const sprite{ m_game->m_sheet.GetSpriteWithID(id) };
sprite->Draw(m_game->logicscreen, pos.x, pos.y);
}
}
}
}
int Map::GetTileIDAtGridPosition(int2 pos) const {
for (int l{ 0 }; l < m_layers.GetLength(); l++) {
const int id{ m_layers[l].GetTileAtGridPosition(pos) };
if (id != EMPTY_TILE)
return id;
}
return EMPTY_TILE;
}
int2 Map::GetGridCoordinateByIndex(int index) {
ASSERT_VAR(index, index >= 0 && index < MAP_WIDTH * MAP_HEIGHT, "Index must be within the map");
return int2(
(index % MAP_WIDTH) * TILE_WIDTH,
(index / MAP_WIDTH) * TILE_HEIGHT);
}
int Map::GetIndexByGridCoordinate(int2 position) {
ASSERT_VAR(position.x, position.x >= 0 && position.x < MAP_WIDTH, "Position must be in the grid.");
ASSERT_VAR(position.y, position.y >= 0 && position.y < MAP_HEIGHT, "Position must be in the grid.");
return position.x + position.y * MAP_WIDTH;
}
int2 Map::GridToWorld(int2 grid) {
return int2(grid.x * TILE_WIDTH + HALF_TILE_WIDTH, grid.y * TILE_HEIGHT + HALF_TILE_HEIGHT);
}
int2 Map::WorldToGrid(int2 world) {
return int2(world.x / TILE_WIDTH, world.y / TILE_HEIGHT);
}
bool Map::IsGridPosOnMap(int2 pos) {
return AABB::Contains(int2(0, 0), int2(MAP_WIDTH, MAP_HEIGHT), pos);
}
int2 Map::SnapToGrid(int2 pos) {
return GridToWorld(WorldToGrid(pos));
}
bool Map::IsUnbreakableWallAtGrid(int2 gridpos) const {
if (!Map::IsGridPosOnMap(gridpos)) {
return false;
}
const int id{ GetTileIDAtGridPosition(gridpos) };
return Game::IsIDUnbreakableWall(id);
}
bool Map::IsWallAtGrid(int2 gridpos) const {
if (!Map::IsGridPosOnMap(gridpos)) {
return false;
}
const int id{ GetTileIDAtGridPosition(gridpos) };
return Game::IsIDWall(id);
}
bool Map::IsFireAtGrid(int2 gridpos) const {
if (!Map::IsGridPosOnMap(gridpos)) {
return false;
}
const int id{ GetTileIDAtGridPosition(gridpos) };
return Game::IsIDExplosion(id);
}
bool Map::IsBombAtGrid(int2 gridpos) const {
if (!Map::IsGridPosOnMap(gridpos)) {
return false;
}
const int id{ GetTileIDAtGridPosition(gridpos) };
return Game::IsIDBomb(id);
}
int2 Map::GetRandomTileGrid() {
return int2(RandomInt(1, MAP_WIDTH - 1), RandomInt(1, MAP_HEIGHT - 1));
}
int2 Map::GetRandomEmptyTileGrid() {
int attempt{ 0 };
int2 result;
do {
result = GetRandomTileGrid();
++attempt;
if (attempt > 100) {
PRINT_CRITICAL("Too many attempts to find a random free tile on the grid.");
return int2(0, 0);
}
} while (GetTileIDAtGridPosition(result) != EMPTY_TILE);
return result;
}
int2 Map::GetRandomSpawnableTileGrid() {
int attempt{ 0 };
int2 result{ 0, 0 };
do {
result = GetRandomTileGrid();
++attempt;
if (attempt > 100) {
PRINT_CRITICAL("Too many attempts to find a random free tile on the grid.");
return int2(0, 0);
}
} while (GetTileIDAtGridPosition(result) != EMPTY_TILE || (result.x < SAFE_ZONE.x && result.y < SAFE_ZONE.y));
return result;
}
}