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
|
#ifndef MAP_H_3AB00D12
#define MAP_H_3AB00D12
#include <vector>
#include <algorithm>
#include <array>
#include <list>
#include <map>
#include <random>
#include "consts.h"
#include "direction.h"
enum class Tile {
Floor,
Wall,
Dust,
Lamp
};
enum class Source {
None,
Dust,
Lamp,
Player
};
struct MapData {
bool lit = false;
bool wasLit = false;
size_t dustLife = 0;
Source lightType = Source::None;
int lightRadius = 0;
std::set<coord> litTiles;
int renderId = -1;
bool dirtyRender = true;
bool sign = false;
std::string text;
};
struct Chunk {
std::array<Tile, CHUNK_WIDTH*CHUNK_HEIGHT> tiles;
std::array<MapData, CHUNK_WIDTH*CHUNK_HEIGHT> data;
int litTiles = 0;
int x;
int y;
};
inline void toChunkPos(int x, int y, int& cx, int& cy) {
cx = std::floor(static_cast<double>(x) / CHUNK_WIDTH);
cy = std::floor(static_cast<double>(y) / CHUNK_HEIGHT);
}
class Map {
public:
inline int getLeft() const
{
return left_;
}
inline int getRight() const
{
return left_ + width_;
}
inline int getTop() const
{
return top_;
}
inline int getBottom() const
{
return top_ + height_;
}
inline int getWidth() const
{
return width_;
}
inline int getHeight() const
{
return height_;
}
inline int getTrueX(int x) const
{
return (x - left_);
}
inline int getTrueY(int y) const
{
return (y - top_);
}
inline bool inBounds(int x, int y) const
{
return (x >= left_) &&
(x < left_ + width_) &&
(y >= top_) &&
(y < top_ + height_);
}
inline const Tile& tile(int x, int y) const
{
return loadedTiles_[(x - left_) + width_ * (y - top_)];
}
inline Tile& tile(int x, int y)
{
return const_cast<Tile&>(static_cast<const Map&>(*this).tile(x, y));
}
inline const MapData& at(int x, int y) const
{
return loaded_[(x - left_) + width_ * (y - top_)];
}
inline MapData& at(int x, int y)
{
return const_cast<MapData&>(static_cast<const Map&>(*this).at(x, y));
}
inline const std::vector<Tile>& tiles() const
{
return loadedTiles_;
}
inline std::vector<Tile>& tiles()
{
return loadedTiles_;
}
inline const std::vector<MapData>& data() const
{
return loaded_;
}
inline std::vector<MapData>& data()
{
return loaded_;
}
inline int getUnloadedLitTiles() const { return unloadedLitTiles_; }
void load(int newLeftChunk, int newTopChunk, int newWidthChunks, int newHeightChunks, std::mt19937& rng) {
// Flush the currently loaded data as long as there is any (this isn't the first load).
if (!loaded_.empty()) {
std::vector<size_t> touchedChunks;
for (int chunkY = 0; chunkY < chunksVert_; chunkY++) {
for (int chunkX = 0; chunkX < chunksHoriz_; chunkX++) {
size_t chunkIndex = chunkByPos_.at(leftmostChunk_ + chunkX).at(topmostChunk_ + chunkY);
touchedChunks.push_back(chunkIndex);
Chunk& chunk = chunks_.at(chunkIndex);
chunk.litTiles = 0;
int startX = chunkX * CHUNK_WIDTH;
int startY = chunkY * CHUNK_HEIGHT;
for (int y=0; y<CHUNK_HEIGHT; y++) {
std::copy(
std::next(std::begin(loadedTiles_), (chunkY*CHUNK_HEIGHT + y)*width_ + chunkX*CHUNK_WIDTH),
std::next(std::begin(loadedTiles_), (chunkY*CHUNK_HEIGHT + y)*width_ + (chunkX+1)*CHUNK_WIDTH),
std::next(std::begin(chunk.tiles), y*CHUNK_WIDTH));
for (int x=0; x<CHUNK_WIDTH; x++) {
chunk.data[x+y*CHUNK_WIDTH] = loaded_[startX+x+(startY+y)*width_];
if (chunk.data[x+y*CHUNK_WIDTH].lit) {
chunk.litTiles++;
}
}
}
unloadedLitTiles_ += chunk.litTiles;
}
}
// Destroy any completely unlit chunks.
for (size_t chunkIndex : touchedChunks) {
if (chunks_[chunkIndex].litTiles == 0) {
chunkByPos_[chunks_[chunkIndex].x].erase(chunks_[chunkIndex].y);
freeList_.push_back(chunkIndex);
}
}
}
leftmostChunk_ = newLeftChunk;
topmostChunk_ = newTopChunk;
chunksHoriz_ = newWidthChunks;
chunksVert_ = newHeightChunks;
left_ = newLeftChunk * CHUNK_WIDTH;
top_ = newTopChunk * CHUNK_HEIGHT;
width_ = chunksHoriz_ * CHUNK_WIDTH;
height_ = chunksVert_ * CHUNK_HEIGHT;
loaded_ = std::vector<MapData>(width_ * height_);
loadedTiles_ = std::vector<Tile>(width_ * height_, Tile::Floor);
dirtyTiles_.clear();
dirtySet_ = std::vector<bool>(width_ * height_, false);
// Load in the requested chunks.
for (int chunkY = 0; chunkY < chunksVert_; chunkY++) {
for (int chunkX = 0; chunkX < chunksHoriz_; chunkX++) {
// Instantiate a new chunk if necessary.
if (!chunkByPos_[leftmostChunk_ + chunkX].count(topmostChunk_ + chunkY)) {
size_t chunkIndex;
if (!freeList_.empty()) {
chunkIndex = freeList_.front();
freeList_.pop_front();
} else {
chunkIndex = chunks_.size();
chunks_.emplace_back();
}
chunkByPos_[leftmostChunk_ + chunkX][topmostChunk_ + chunkY] = chunkIndex;
Chunk& chunk = chunks_[chunkIndex];
chunk.x = leftmostChunk_ + chunkX;
chunk.y = topmostChunk_ + chunkY;
chunk.litTiles = 0;
for (Tile& tile : chunk.tiles)
{
if (std::bernoulli_distribution(0.5)(rng))
{
tile = Tile::Wall;
} else {
tile = Tile::Floor;
}
}
for (MapData& md : chunk.data) {
md.dirtyRender = true;
}
}
size_t chunkIndex = chunkByPos_[leftmostChunk_ + chunkX][topmostChunk_ + chunkY];
Chunk& chunk = chunks_[chunkIndex];
for (int y = 0; y < CHUNK_HEIGHT; y++) {
std::copy(
std::next(std::begin(chunk.tiles), y*CHUNK_WIDTH),
std::next(std::begin(chunk.tiles), (y+1)*CHUNK_WIDTH),
std::next(std::begin(loadedTiles_), (chunkY*CHUNK_HEIGHT + y)*width_ + chunkX*CHUNK_WIDTH));
std::copy(
std::next(std::begin(chunk.data), y*CHUNK_WIDTH),
std::next(std::begin(chunk.data), (y+1)*CHUNK_WIDTH),
std::next(std::begin(loaded_), (chunkY*CHUNK_HEIGHT + y)*width_ + chunkX*CHUNK_WIDTH));
}
unloadedLitTiles_ -= chunk.litTiles;
}
}
}
void markDirtyAround(int x, int y) {
if (x > left_) {
if (y > top_) {
markDirty(x-1, y-1);
}
markDirty(x-1, y );
if (y < top_+height_-1) {
markDirty(x-1, y+1);
}
}
if (y > top_) {
markDirty(x , y-1);
}
markDirty(x , y );
if (y < top_+height_-1) {
markDirty(x , y+1);
}
if (x < left_+width_-1) {
if (y > top_) {
markDirty(x+1, y-1);
}
markDirty(x+1, y );
if (y < top_+height_-1) {
markDirty(x+1, y+1);
}
}
}
void markDirty(int x, int y) {
size_t index = (x-left_)+(y-top_)*width_;
if (!dirtySet_[index]) {
dirtySet_[index] = true;
dirtyTiles_.emplace_back(x,y);
}
}
const std::vector<coord> getDirty() const {
return dirtyTiles_;
}
void resetDirty() {
for (auto [x,y] : dirtyTiles_) {
dirtySet_[(x-left_)+(y-top_)*width_] = false;
}
dirtyTiles_.clear();
}
private:
int left_;
int top_;
int width_;
int height_;
int leftmostChunk_;
int topmostChunk_;
int chunksHoriz_;
int chunksVert_;
std::vector<Tile> loadedTiles_;
std::vector<MapData> loaded_;
std::vector<Chunk> chunks_;
std::list<size_t> freeList_;
std::map<int, std::map<int, size_t>> chunkByPos_; // chunkByPos_[X][Y]
int unloadedLitTiles_ = 0;
std::vector<coord> dirtyTiles_;
std::vector<bool> dirtySet_;
};
#endif /* end of include guard: MAP_H_3AB00D12 */
|