Repository navigation
Expand file tree
/
Copy pathWorld.cpp
More file actions
182 lines (154 loc) · 7.09 KB
/
Copy pathWorld.cpp
File metadata and controls
182 lines (154 loc) · 7.09 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
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include "World.hpp"
#include "FindAndUnion.hpp"
#include <iostream>
#include <iomanip>
World::World(unsigned int width, unsigned int height)
{
data_ = new Brick* [width];
for (unsigned int i = 0; i < width; ++i) {
data_[i] = new Brick[height];
std::memset(data_[i], 0, height * sizeof(Brick));
}
width_ = width;
height_ = height;
}
World::~World()
{
for (unsigned int i = 0; i < width_; ++i)
delete [] data_[i];
delete [] data_;
}
unsigned int World::countAdjacentWalls_(unsigned int x, unsigned int y)
{
if (x >= width_ || y >= height_)
throw IndexError();
unsigned int result = 0;
unsigned int x0, x1, y0, y1;
x0 = std::max(0, int(x) - 1);
x1 = std::min(width_ - 1, x + 1);
y0 = std::max(0, int(y) - 1);
y1 = std::min(height_ - 1, y + 1);
for (unsigned int xi = x0; xi <= x1; ++xi) {
for (unsigned int yi = y0; yi <= y1; ++yi) {
if (xi == x && yi == y)
continue;
result += (data_[xi][yi] == Brick::WALL ? 1 : 0);
}
}
return result;
}
void World::makeRandom(unsigned int seed) noexcept
{
std::srand(seed);
for (unsigned int xi = 0; xi < width_; ++xi) {
for (unsigned int yi = 0; yi < height_; ++yi) {
if (std::rand() % 4 == 0)
data_[xi][yi] = Brick::WALL;
else
data_[xi][yi] = Brick::FLOOR;
}
}
Brick **tmp;
tmp = new Brick* [width_];
for (unsigned int i = 0; i < width_; ++i) {
tmp[i] = new Brick[height_];
std::memset(tmp[i], 0, height_ * sizeof(Brick));
}
for (unsigned int generation = 0; generation < 20; ++generation) {
for (unsigned int xi = 0; xi < width_; ++xi) {
for (unsigned int yi = 0; yi < height_; ++yi) {
unsigned int adj_walls = countAdjacentWalls_(xi, yi);
if (data_[xi][yi] == Brick::WALL)
tmp[xi][yi] = ((adj_walls >= 1 && adj_walls <= 5) ? Brick::WALL : Brick::FLOOR);
else
tmp[xi][yi] = (adj_walls == 3 ? Brick::WALL : Brick::FLOOR);
}
}
std::swap(tmp, data_);
}
for (unsigned int i = 0; i < width_; ++i)
delete [] tmp[i];
delete [] tmp;
for (unsigned int xi = 0; xi < width_; ++xi)
data_[xi][0] = data_[xi][height_ - 1] = Brick::WALL;
for (unsigned int yi = 0; yi < height_; ++yi)
data_[0][yi] = data_[width_ - 1][yi] = Brick::WALL;
FindAndUnion parts;
unsigned int **colors = new unsigned int* [width_];
for (unsigned int i = 0; i < width_; ++i) {
colors[i] = new unsigned int [height_];
std::memset(colors[i], static_cast<unsigned char>(-1), height_ * sizeof(unsigned int));
}
std::function<void(unsigned int, unsigned int, unsigned int)> dfs = [&colors, &dfs, this](unsigned int x, unsigned int y, unsigned int color) {
colors[x][y] = color;
if (data_[x - 1][y] == Brick::FLOOR && colors[x - 1][y] != color)
dfs(x - 1, y, color);
if (data_[x + 1][y] == Brick::FLOOR && colors[x + 1][y] != color)
dfs(x + 1, y, color);
if (data_[x][y - 1] == Brick::FLOOR && colors[x][y - 1] != color)
dfs(x, y - 1, color);
if (data_[x][y + 1] == Brick::FLOOR && colors[x][y + 1] != color)
dfs(x, y + 1, color);
};
for (unsigned int xi = 1; xi < width_ - 1; ++xi) {
for (unsigned int yi = 1; yi < height_ - 1; ++yi) {
if (data_[xi][yi] == Brick::WALL || colors[xi][yi] != static_cast<unsigned int>(-1))
continue;
unsigned int color = parts.newSet();
dfs(xi, yi, color);
}
}
for (unsigned int xi = 1; xi < width_ - 1; ++xi) {
for (unsigned int yi = 1; yi < height_ - 1; ++yi) {
if (data_[xi][yi] != Brick::WALL)
continue;
unsigned int new_color = static_cast<unsigned int>(-1);
if (data_[xi - 1][yi] == Brick::FLOOR && data_[xi + 1][yi] == Brick::FLOOR && parts.representative(colors[xi - 1][yi]) != parts.representative(colors[xi + 1][yi]))
parts.merge(colors[xi - 1][yi], colors[xi + 1][yi]), new_color = parts.representative(colors[xi - 1][yi]);
if (data_[xi - 1][yi] == Brick::FLOOR && data_[xi][yi - 1] == Brick::FLOOR && parts.representative(colors[xi - 1][yi]) != parts.representative(colors[xi][yi - 1]))
parts.merge(colors[xi - 1][yi], colors[xi][yi - 1]), new_color = parts.representative(colors[xi - 1][yi]);
if (data_[xi - 1][yi] == Brick::FLOOR && data_[xi][yi + 1] == Brick::FLOOR && parts.representative(colors[xi - 1][yi]) != parts.representative(colors[xi][yi + 1]))
parts.merge(colors[xi - 1][yi], colors[xi][yi + 1]), new_color = parts.representative(colors[xi - 1][yi]);
if (data_[xi + 1][yi] == Brick::FLOOR && data_[xi][yi - 1] == Brick::FLOOR && parts.representative(colors[xi + 1][yi]) != parts.representative(colors[xi][yi - 1]))
parts.merge(colors[xi + 1][yi], colors[xi][yi - 1]), new_color = parts.representative(colors[xi + 1][yi]);
if (data_[xi + 1][yi] == Brick::FLOOR && data_[xi][yi + 1] == Brick::FLOOR && parts.representative(colors[xi + 1][yi]) != parts.representative(colors[xi][yi + 1]))
parts.merge(colors[xi + 1][yi], colors[xi][yi + 1]), new_color = parts.representative(colors[xi + 1][yi]);
if (data_[xi][yi - 1] == Brick::FLOOR && data_[xi][yi + 1] == Brick::FLOOR && parts.representative(colors[xi][yi - 1]) != parts.representative(colors[xi][yi + 1]))
parts.merge(colors[xi][yi - 1], colors[xi][yi + 1]), new_color = parts.representative(colors[xi][yi - 1]);
if (new_color != static_cast<unsigned int>(-1)) {
data_[xi][yi] = Brick::FLOOR;
colors[xi][yi] = new_color;
}
}
}
for (unsigned int xi = 0; xi < width_; ++xi) {
for (unsigned int yi = 0; yi < height_; ++yi) {
if (data_[xi][yi] == Brick::FLOOR && rand() % 8 == 0) {
data_[xi][yi] = Brick::DIAMOND;
++diamondCounter_;
}
}
}
}
void World::stepPlayer(float step_x, float step_y)
{
const float margin = 0.4;
unsigned int player_x = playerX_;
unsigned int player_y = playerY_;
if (get(player_x, player_y) == Brick::DIAMOND)
set(player_x, player_y, World::Brick::FLOOR);
playerX_ += step_x;
playerY_ += step_y;
if (get(player_x - 1, player_y) == Brick::WALL)
playerX_ = std::max(playerX_, player_x + margin);
if (get(player_x + 1, player_y) == Brick::WALL)
playerX_ = std::min(playerX_, player_x + 1.0f - margin);
if (get(player_x, player_y - 1) == Brick::WALL)
playerY_ = std::max(playerY_, player_y + margin);
if (get(player_x, player_y + 1) == Brick::WALL)
playerY_ = std::min(playerY_, player_y + 1.0f - margin);
}