-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcave.cpp
More file actions
189 lines (180 loc) · 7.28 KB
/
cave.cpp
File metadata and controls
189 lines (180 loc) · 7.28 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
#include "Utilities.h"
#include "particles.cpp"
class CaveGenerator{
public:
PARTICLETYPES * map;
int fillPercent;
PARTICLETYPES wallType;
CaveGenerator(int _width, int _height, int _fillPercent, PARTICLETYPES _wallType){
width = _width;
height = _height;
fillPercent = _fillPercent;
wallType = _wallType;
}
void init(){
map = (PARTICLETYPES *)malloc(width * height * sizeof(PARTICLETYPES));
for (int i = 0; i < width * height; ++i){
if (FastRand() % 100 <= fillPercent){
map[i] = wallType;
}
else{
map[i] = NOTHING;
}
}
}
PARTICLETYPES * getMap(){
return map;
}
void step(int steps){
for (int i = 0; i < steps; i++){
int sum;
PARTICLETYPES * newMap = (PARTICLETYPES *)malloc(width * height * sizeof(PARTICLETYPES));
for (int x = 0; x < width ; ++x){
for (int y = 0; y < height; ++y){
if (x <= 1) map[x + y * width] = wallType;
if (y <= 1) map[x + y * width] = wallType;
if (x >= width - 2) map[x + y * width] = wallType;
if (y >= height - 2) map[x + y * width] = wallType;
sum = getNeighbors(x , y);
if (map[x + y * width] == wallType && sum < 4){
newMap[x + y * width] = NOTHING;
}
else if (map[x + y * width] == NOTHING && sum >= 5){
newMap[x + y * width] = wallType;
}
else{
newMap[x + y * width] = map[x + y * width];
}
if (x <= 1) map[x + y * width] = wallType;
if (y <= 1) map[x + y * width] = wallType;
if (x >= width - 2) map[x + y * width] = wallType;
if (y >= height - 2) map[x + y * width] = wallType;
//map[x + y * width] = newMap[x + y * width];
}
}
for (int i2 = 0; i2< width * height; i2 ++){
map[i2] = newMap[i2];
}
delete [] newMap;
}
}
void stepJagged(int steps){
for (int i = 0; i < steps; i++){
int sum;
PARTICLETYPES * newMap = (PARTICLETYPES *)malloc(width * height * sizeof(PARTICLETYPES));
for (int x = 0; x < width ; ++x){
for (int y = 0; y < height; ++y){
if (x <= 1) map[x + y * width] = wallType;
if (y <= 1) map[x + y * width] = wallType;
if (x >= width - 2) map[x + y * width] = wallType;
if (y >= height - 2) map[x + y * width] = wallType;
sum = getNeighbors(x , y);
if (map[x + y * width] == wallType && sum <= 4){
newMap[x + y * width] = NOTHING;
}
else if (map[x + y * width] == NOTHING && sum > 4){
newMap[x + y * width] = wallType;
}
else{
newMap[x + y * width] = map[x + y * width];
}
if (x <= 1) map[x + y * width] = wallType;
if (y <= 1) map[x + y * width] = wallType;
if (x >= width - 2) map[x + y * width] = wallType;
if (y >= height - 2) map[x + y * width] = wallType;
//map[x + y * width] = newMap[x + y * width];
}
}
for (int i2 = 0; i2< width * height; i2 ++){
map[i2] = newMap[i2];
}
delete [] newMap;
}
}
std::vector<std::pair<int,int>> getRegionTiles(int startX, int startY){
std::vector<std::pair<int,int>> tiles;
std::map<int,int> mapFlags;
std::vector<std::pair<int,int>> queue;
queue.push_back(std::pair<int,int>(startX, startY));
mapFlags[startX + startY * width] = 1;
while (queue.size() > 0){
std::pair<int,int> tile = queue.back();
tiles.push_back(tile);
queue.pop_back();
for (int x = tile.first - 1; x <= tile.first + 1; ++x){
for (int y = tile.second - 1; y <= tile.second + 1; ++y){
if (x >= 0 && x < width && y >= 0 && y < height && (y == tile.second || x == tile.first)){
if (mapFlags[x + y * width] == 0 && map[x + y * width] == wallType){
mapFlags[x + y * width] = 1;
queue.push_back(std::pair<int,int>(x, y));
}
}
}
}
}
return tiles;
}
std::vector<std::vector<std::pair<int,int>>> getRegions(){
std::vector<std::vector<std::pair<int,int>>> regions;
std::map<int,int> mapFlags;
for (int x = 0; x < width; ++x){
for (int y = 0; y < height; ++y){
if (mapFlags[x + y * width] == 0 && map[x + y * width] == wallType){
std::vector<std::pair<int,int>> newRegion = getRegionTiles(x, y);
regions.push_back(newRegion);
for (std::pair<int,int> tile : newRegion){
mapFlags[tile.first, tile.second] = 1;
}
}
}
}
return regions;
}
//WARNING: VERY SLOW
void processMap(){
//Need to increase speed
std::vector<std::vector<std::pair<int,int>>> wallRegions = getRegions();
int wallThresholdSize = 50;
for (std::vector<std::pair<int,int>> wallRegion : wallRegions){
if (wallRegion.size() < wallThresholdSize){
for (std::pair<int,int> tile : wallRegion){
map[tile.first + tile.second * width] = NOTHING;
}
}
}
}
void deleteMap(){
delete [] map;
}
private:
int width;
int height;
int getNeighbors(int x, int y) {
int neighbors = 0;
if((x - 1 >= 0 && y - 1 >= 0 && map[x - 1 + (y - 1) * width] == wallType) || (x - 1 < 0 && y - 1 < 0)) {
neighbors += 1;
}
if((y - 1 >= 0 && map[x + (y - 1) * width] == wallType) || (y - 1 < 0)) {
neighbors += 1;
}
if((x + 1 < width && y - 1 >= 0 && map[x + 1 + (y - 1 ) * width] == wallType) || (x + 1 >= width && y - 1 < 0)) {
neighbors += 1;
}
if((x - 1 >= 0 && map[x - 1 + y* width] == wallType) || (x - 1 < 0)) {
neighbors += 1;
}
if((x + 1 < width && map[x + 1 + y * width] == wallType) || (x + 1 >= width)) {
neighbors += 1;
}
if((x - 1 >= 0 && y + 1 < height && map[x - 1 + ( y + 1 ) * width] == wallType) || (x - 1 < 0 && y + 1 >= height)) {
neighbors += 1;
}
if((y + 1 < height && map[x + (y + 1) * width] == wallType) || (y + 1 >= height)) {
neighbors += 1;
}
if((x + 1 < width && y + 1 < height && map[x + 1 + (y + 1) * width] == wallType) || (x + 1 >= width && y + 1 >= height)) {
neighbors += 1;
}
return neighbors;
}
};