-
Notifications
You must be signed in to change notification settings - Fork 6
/
caverngenerator.c
253 lines (219 loc) · 5.83 KB
/
caverngenerator.c
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
#include <stdlib.h>
#include "binaryutils.h"
#define uniform() (rand()/(RAND_MAX + 1.0))
#define coin() ((rand() <= RAND_MAX/2) / 0 : 1)
#define sign(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0)
static int connect(unsigned char *binary, int width, int height);
static void breakbackground8connections(unsigned char *binary, int width, int height);
static void filter5_4(unsigned char *binary, int width, int height, unsigned char *buff);
static void addborder(unsigned char *binary, int width, int height, unsigned char value);
static int mem_count(unsigned char *pixels, int N, int value);
static void get3x3(unsigned char *out, unsigned char *binary, int width, int height, int x, int y, unsigned char border);
unsigned char *makecaverns(int width, int height)
{
unsigned char *answer = 0;
unsigned char *buff = 0;
int x, y;
int i;
int Nsteps = 10;
answer = malloc(width * height);
if (!answer)
goto out_of_memory;
buff = malloc(width * height);
if (!buff)
goto out_of_memory;
addborder(answer, width, height, 0);
for (y = 1; y < height - 1; y++)
{
for (x = 1; x < width - 1; x++)
{
answer[y*width + x] = uniform() < 0.5 ? 1 : 0;
}
}
for (i = 0; i < Nsteps; i++)
filter5_4(answer, width, height, buff);
connect(answer, width, height);
breakbackground8connections(answer, width, height);
getbiggestobject(answer, width, height, 4);
free(buff);
return answer;
out_of_memory:
free(buff);
free(answer);
return 0;
}
static int connect(unsigned char *binary, int width, int height)
{
int N;
int *ids;
unsigned char *flags;
unsigned int *closex;
unsigned int *closey;
int i;
int x, y;
int id;
int cx, cy;
int d1, d2;
int dx, dy;
double p;
cx = width / 2;
cy = height / 2;
ids = labelconnected(binary, width, height, 4, &N);
flags = malloc(N);
closex = malloc((N+1) * sizeof(int));
closey = malloc((N+1) * sizeof(int));
for (i = 0; i < N; i++)
{
flags[i] = 0;
closex[i] = width + 100;
closey[i] = height + 100;
}
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
id = ids[y*width + x];
if (id > 0)
{
d1 = (closex[id] - cx)*(closex[id] - cx) + (closey[id] - cy)*(closey[id] - cy);
d2 = (x - cx) *(x - cx) + (y - cy)*(y - cy);
if (d2 < d1)
{
closex[id] = x;
closey[id] = y;
}
}
}
}
for (i = 1; i < N; i++)
{
if (closex[i] != cx || closey[i] != cy)
{
x = closex[i];
y = closey[i];
while (x != cx || y != cy)
{
dx = cx - x;
dy = cy - y;
p = ((double)abs(dx)) / (abs(dx) + abs(dy));
if (uniform() < p)
{
dx = sign(dx);
dy = 0;
}
else
{
dx = 0;
dy = sign(dy);
}
x += dx;
y += dy;
binary[y*width + x] = 1;
if (ids[y*width + x] != id)
break;
}
}
}
free(ids);
free(flags);
free(closex);
free(closey);
}
static void breakbackground8connections(unsigned char *binary, int width, int height)
{
int x, y;
unsigned char neighbours[9];
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
if (binary[y*width + x] == 0)
{
get3x3(neighbours, binary, width, height, x, y, 0);
if (neighbours[0] == 0 && neighbours[1] == 1 && neighbours[3] == 1)
binary[y*width + x] = 1;
if (neighbours[2] == 0 && neighbours[1] == 1 && neighbours[5] == 1)
binary[y*width + x] = 1;
if (neighbours[6] == 0 && neighbours[3] == 1 && neighbours[7] == 1)
binary[y*width + x] = 1;
if (neighbours[8] == 0 && neighbours[5] == 1 && neighbours[7] == 1)
binary[y*width + x] = 1;
}
}
}
}
static void filter5_4(unsigned char *binary, int width, int height, unsigned char *buff)
{
int x, y;
unsigned char neighbours[9];
int N;
for (y = 1; y < height - 1; y++)
{
for (x = 1; x < width - 1; x++)
{
get3x3(neighbours, binary, width, height, x, y, 1);
N = mem_count(neighbours, 9, 1);
if (N >= 5)
buff[y*width + x] = 1;
else
buff[y*width + x] = 0;
}
}
for (y = 1; y < height - 1; y++)
for (x = 1; x < width - 1; x++)
binary[y*width + x] = buff[y*width + x];
}
static void addborder(unsigned char *binary, int width, int height, unsigned char value)
{
int x, y;
for (x = 0; x < width; x++)
binary[x] = value;
for (x = 0; x < width; x++)
binary[(height - 1)*width + x] = value;
for (y = 0; y < height; y++)
binary[y*width] = value;
for (y = 0; y < height; y++)
binary[y*width + width - 1] = value;
}
/*
mem_count - count the number of bytes equal to value
Params: pixels - the memory
N - memory size
value - value to test
Returns: number of bytes in buffer equal to value.
*/
static int mem_count(unsigned char *pixels, int N, int value)
{
int answer = 0;
int i;
for (i = 0; i<N; i++)
if (pixels[i] == value)
answer++;
return answer;
}
/*
get 3x3 neighbourhood, padding for boundaries
Params: out - return pointer for neighbourhood
binary - the binary image
width - image width
height - image height
x, y - centre pixel x, y co-ordinates
border - value to pad borders with.
Notes: pattern returned is
0 1 2
3 4 5
6 7 8
where 4 is the pixel at x, y.
*/
static void get3x3(unsigned char *out, unsigned char *binary, int width, int height, int x, int y, unsigned char border)
{
if (y > 0 && x > 0) out[0] = binary[(y - 1)*width + x - 1]; else out[0] = border;
if (y > 0) out[1] = binary[(y - 1)*width + x]; else out[1] = border;
if (y > 0 && x < width - 1) out[2] = binary[(y - 1)*width + x + 1]; else out[2] = border;
if (x > 0) out[3] = binary[y*width + x - 1]; else out[3] = border;
out[4] = binary[y*width + x];
if (x < width - 1) out[5] = binary[y*width + x + 1]; else out[5] = border;
if (y < height - 1 && x > 0) out[6] = binary[(y + 1)*width + x - 1]; else out[6] = border;
if (y < height - 1) out[7] = binary[(y + 1)*width + x]; else out[7] = border;
if (y < height - 1 && x < width - 1) out[8] = binary[(y + 1)*width + x + 1]; else out[8] = border;
}