|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
|
|
6 | * |
|
|
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
|
|
8 | * the terms of the Affero GNU General Public License as published by the |
|
|
9 | * Free Software Foundation, either version 3 of the License, or (at your |
|
|
10 | * option) any later version. |
|
|
11 | * |
|
|
12 | * This program is distributed in the hope that it will be useful, |
|
|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 | * GNU General Public License for more details. |
|
|
16 | * |
|
|
17 | * You should have received a copy of the Affero GNU General Public License |
|
|
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
22 | */ |
1 | |
23 | |
2 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
24 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
3 | blocked maze with the property that there is only one path from one spot |
25 | blocked maze with the property that there is only one path from one spot |
4 | to any other, and there is always a path from one spot to any other. |
26 | to any other, and there is always a path from one spot to any other. |
5 | |
27 | |
… | |
… | |
13 | |
35 | |
14 | /* we need to maintain a list of wall points to generate |
36 | /* we need to maintain a list of wall points to generate |
15 | reasonable mazes: a straightforward recursive random walk maze |
37 | reasonable mazes: a straightforward recursive random walk maze |
16 | generator would generate a map with a trivial circle-the-outer-wall solution */ |
38 | generator would generate a map with a trivial circle-the-outer-wall solution */ |
17 | |
39 | |
18 | #include <stdio.h> |
|
|
19 | #include <global.h> |
40 | #include <global.h> |
20 | |
41 | |
21 | /*#include <random_map.h>*/ |
42 | #include "random_map.h" |
22 | #include <maze_gen.h> |
43 | #include "rproto.h" |
23 | #include <time.h> |
|
|
24 | |
|
|
25 | |
|
|
26 | /* this include solely, and only, is needed for the definition of RANDOM */ |
|
|
27 | |
|
|
28 | |
|
|
29 | |
44 | |
30 | /* global variables that everyone needs: don't want to pass them in |
45 | /* global variables that everyone needs: don't want to pass them in |
31 | as parameters every time. */ |
46 | as parameters every time. */ |
32 | int *wall_x_list = 0; |
47 | static int *wall_x_list = 0; |
33 | int *wall_y_list = 0; |
48 | static int *wall_y_list = 0; |
34 | int wall_free_size = 0; |
49 | static int wall_free_size = 0; |
35 | |
|
|
36 | /* heuristically, we need to change wall_chance based on the size of |
|
|
37 | the maze. */ |
|
|
38 | |
|
|
39 | int wall_chance; |
|
|
40 | |
|
|
41 | /* the outsize interface routine: accepts sizes, returns a char |
|
|
42 | ** maze. option is a flag for either a sparse or a full maze. Sparse |
|
|
43 | mazes have sizable rooms. option = 1, full, 0, sparse.*/ |
|
|
44 | |
|
|
45 | char ** |
|
|
46 | maze_gen (int xsize, int ysize, int option) |
|
|
47 | { |
|
|
48 | int i, j; |
|
|
49 | |
|
|
50 | /* allocate that array, set it up */ |
|
|
51 | char **maze = (char **) calloc (sizeof (char *), xsize); |
|
|
52 | |
|
|
53 | for (i = 0; i < xsize; i++) |
|
|
54 | { |
|
|
55 | maze[i] = (char *) calloc (sizeof (char), ysize); |
|
|
56 | } |
|
|
57 | |
|
|
58 | /* write the outer walls */ |
|
|
59 | for (i = 0; i < xsize; i++) |
|
|
60 | maze[i][0] = maze[i][ysize - 1] = '#'; |
|
|
61 | for (j = 0; j < ysize; j++) |
|
|
62 | maze[0][j] = maze[xsize - 1][j] = '#'; |
|
|
63 | |
|
|
64 | |
|
|
65 | /* find how many free wall spots there are */ |
|
|
66 | wall_free_size = 2 * (xsize - 4) + 2 * (ysize - 4); |
|
|
67 | |
|
|
68 | make_wall_free_list (xsize, ysize); |
|
|
69 | |
|
|
70 | /* return the empty maze */ |
|
|
71 | if (wall_free_size <= 0) |
|
|
72 | return maze; |
|
|
73 | |
|
|
74 | /* recursively generate the walls of the maze */ |
|
|
75 | /* first pop a random starting point */ |
|
|
76 | while (wall_free_size > 0) |
|
|
77 | { |
|
|
78 | pop_wall_point (&i, &j); |
|
|
79 | if (option) |
|
|
80 | fill_maze_full (maze, i, j, xsize, ysize); |
|
|
81 | else |
|
|
82 | fill_maze_sparse (maze, i, j, xsize, ysize); |
|
|
83 | } |
|
|
84 | |
|
|
85 | /* clean up our intermediate data structures. */ |
|
|
86 | |
|
|
87 | free (wall_x_list); |
|
|
88 | free (wall_y_list); |
|
|
89 | |
|
|
90 | return maze; |
|
|
91 | } |
|
|
92 | |
|
|
93 | |
|
|
94 | |
50 | |
95 | /* the free wall points are those outer points which aren't corners or |
51 | /* the free wall points are those outer points which aren't corners or |
96 | near corners, and don't have a maze wall growing out of them already. */ |
52 | near corners, and don't have a maze wall growing out of them already. */ |
97 | |
53 | static void |
98 | void |
|
|
99 | make_wall_free_list (int xsize, int ysize) |
54 | make_wall_free_list (int xsize, int ysize) |
100 | { |
55 | { |
101 | int i, j, count; |
56 | int i, j, count; |
102 | |
57 | |
103 | count = 0; /* entries already placed in the free list */ |
58 | count = 0; /* entries already placed in the free list */ |
104 | /*allocate it */ |
59 | /*allocate it */ |
105 | if (wall_free_size < 0) |
60 | if (wall_free_size < 0) |
106 | return; |
61 | return; |
|
|
62 | |
107 | wall_x_list = (int *) calloc (sizeof (int), wall_free_size); |
63 | wall_x_list = (int *)calloc (sizeof (int), wall_free_size); |
108 | wall_y_list = (int *) calloc (sizeof (int), wall_free_size); |
64 | wall_y_list = (int *)calloc (sizeof (int), wall_free_size); |
109 | |
|
|
110 | |
65 | |
111 | /* top and bottom wall */ |
66 | /* top and bottom wall */ |
112 | for (i = 2; i < xsize - 2; i++) |
67 | for (i = 2; i < xsize - 2; i++) |
113 | { |
68 | { |
114 | wall_x_list[count] = i; |
69 | wall_x_list[count] = i; |
… | |
… | |
129 | wall_y_list[count] = j; |
84 | wall_y_list[count] = j; |
130 | count++; |
85 | count++; |
131 | } |
86 | } |
132 | } |
87 | } |
133 | |
88 | |
134 | |
|
|
135 | |
|
|
136 | /* randomly returns one of the elements from the wall point list */ |
89 | /* randomly returns one of the elements from the wall point list */ |
137 | |
90 | static void |
138 | void |
|
|
139 | pop_wall_point (int *x, int *y) |
91 | pop_wall_point (int *x, int *y) |
140 | { |
92 | { |
141 | int index = RANDOM () % wall_free_size; |
93 | int index = rmg_rndm (wall_free_size); |
142 | |
94 | |
143 | *x = wall_x_list[index]; |
95 | *x = wall_x_list[index]; |
144 | *y = wall_y_list[index]; |
96 | *y = wall_y_list[index]; |
145 | /* write the last array point here */ |
97 | /* write the last array point here */ |
146 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
98 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
147 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
99 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
148 | wall_free_size--; |
100 | wall_free_size--; |
149 | } |
101 | } |
150 | |
102 | |
151 | |
|
|
152 | |
|
|
153 | /* find free point: randomly look for a square adjacent to this one where |
103 | /* find free point: randomly look for a square adjacent to this one where |
154 | we can place a new block without closing a path. We may only look |
104 | we can place a new block without closing a path. We may only look |
155 | up, down, right, or left. */ |
105 | up, down, right, or left. */ |
156 | |
106 | static int |
157 | int |
|
|
158 | find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) |
107 | find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) |
159 | { |
108 | { |
160 | |
|
|
161 | /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ |
109 | /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ |
162 | int dirlist[4]; |
110 | int dirlist[4]; |
163 | int count = 0; /* # elements in dirlist */ |
111 | int count = 0; /* # elements in dirlist */ |
164 | |
112 | |
165 | /* look up */ |
113 | /* look up */ |
166 | if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */ |
114 | if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */ |
… | |
… | |
168 | int cleartest = (int) maze[xc][yc + 1] + (int) maze[xc - 1][yc + 1] + (int) maze[xc + 1][yc + 1]; |
116 | int cleartest = (int) maze[xc][yc + 1] + (int) maze[xc - 1][yc + 1] + (int) maze[xc + 1][yc + 1]; |
169 | |
117 | |
170 | cleartest += (int) maze[xc][yc + 2] + (int) maze[xc - 1][yc + 2] + (int) maze[xc + 1][yc + 2]; |
118 | cleartest += (int) maze[xc][yc + 2] + (int) maze[xc - 1][yc + 2] + (int) maze[xc + 1][yc + 2]; |
171 | |
119 | |
172 | if (cleartest == 0) |
120 | if (cleartest == 0) |
173 | { |
|
|
174 | dirlist[count] = 1; |
121 | dirlist[count++] = 1; |
175 | count++; |
|
|
176 | } |
|
|
177 | } |
122 | } |
178 | |
|
|
179 | |
123 | |
180 | /* look down */ |
124 | /* look down */ |
181 | if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ |
125 | if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ |
182 | { |
126 | { |
183 | int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; |
127 | int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; |
184 | |
128 | |
185 | cleartest += (int) maze[xc][yc - 2] + (int) maze[xc - 1][yc - 2] + (int) maze[xc + 1][yc - 2]; |
129 | cleartest += (int) maze[xc][yc - 2] + (int) maze[xc - 1][yc - 2] + (int) maze[xc + 1][yc - 2]; |
186 | |
130 | |
187 | if (cleartest == 0) |
131 | if (cleartest == 0) |
188 | { |
|
|
189 | dirlist[count] = 2; |
132 | dirlist[count++] = 2; |
190 | count++; |
|
|
191 | } |
|
|
192 | } |
133 | } |
193 | |
|
|
194 | |
134 | |
195 | /* look right */ |
135 | /* look right */ |
196 | if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ |
136 | if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ |
197 | { |
137 | { |
198 | int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; |
138 | int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; |
199 | |
139 | |
200 | cleartest += (int) maze[xc + 2][yc] + (int) maze[xc + 2][yc - 1] + (int) maze[xc + 2][yc + 1]; |
140 | cleartest += (int) maze[xc + 2][yc] + (int) maze[xc + 2][yc - 1] + (int) maze[xc + 2][yc + 1]; |
201 | |
141 | |
202 | if (cleartest == 0) |
142 | if (cleartest == 0) |
203 | { |
|
|
204 | dirlist[count] = 3; |
143 | dirlist[count++] = 3; |
205 | count++; |
|
|
206 | } |
|
|
207 | } |
144 | } |
208 | |
|
|
209 | |
145 | |
210 | /* look left */ |
146 | /* look left */ |
211 | if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ |
147 | if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ |
212 | { |
148 | { |
213 | int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; |
149 | int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; |
214 | |
150 | |
215 | cleartest += (int) maze[xc - 2][yc] + (int) maze[xc - 2][yc - 1] + (int) maze[xc - 2][yc + 1]; |
151 | cleartest += (int) maze[xc - 2][yc] + (int) maze[xc - 2][yc - 1] + (int) maze[xc - 2][yc + 1]; |
216 | |
152 | |
217 | if (cleartest == 0) |
153 | if (cleartest == 0) |
218 | { |
|
|
219 | dirlist[count] = 4; |
154 | dirlist[count++] = 4; |
220 | count++; |
|
|
221 | } |
|
|
222 | } |
155 | } |
223 | |
156 | |
224 | if (count == 0) |
157 | if (count == 0) |
225 | return -1; /* failed to find any clear points */ |
158 | return -1; /* failed to find any clear points */ |
226 | |
159 | |
227 | /* choose a random direction */ |
160 | /* choose a random direction */ |
228 | if (count > 1) |
|
|
229 | count = RANDOM () % count; |
|
|
230 | else |
|
|
231 | count = 0; |
|
|
232 | switch (dirlist[count]) |
161 | switch (dirlist [rmg_rndm (count)]) |
233 | { |
162 | { |
234 | case 1: /* up */ |
163 | case 1: /* up */ |
235 | { |
|
|
236 | *y = yc + 1; |
164 | *y = yc + 1; |
237 | *x = xc; |
165 | *x = xc; |
238 | break; |
166 | break; |
239 | }; |
167 | |
240 | case 2: /* down */ |
168 | case 2: /* down */ |
241 | { |
|
|
242 | *y = yc - 1; |
169 | *y = yc - 1; |
243 | *x = xc; |
170 | *x = xc; |
244 | break; |
171 | break; |
245 | }; |
172 | |
246 | case 3: /* right */ |
173 | case 3: /* right */ |
247 | { |
|
|
248 | *y = yc; |
174 | *y = yc; |
249 | *x = xc + 1; |
175 | *x = xc + 1; |
250 | break; |
176 | break; |
251 | } |
177 | |
252 | case 4: /* left */ |
178 | case 4: /* left */ |
253 | { |
|
|
254 | *x = xc - 1; |
179 | *x = xc - 1; |
255 | *y = yc; |
180 | *y = yc; |
256 | break; |
181 | break; |
257 | } |
182 | |
258 | default: /* ??? */ |
183 | default: /* ??? */ |
259 | { |
|
|
260 | return -1; |
184 | return -1; |
261 | } |
185 | |
262 | } |
186 | } |
|
|
187 | |
263 | return 1; |
188 | return 1; |
264 | } |
189 | } |
265 | |
190 | |
266 | /* recursive routine which will fill every available space in the maze |
191 | /* recursive routine which will fill every available space in the maze |
267 | with walls*/ |
192 | with walls*/ |
268 | |
193 | static void |
269 | void |
|
|
270 | fill_maze_full (char **maze, int x, int y, int xsize, int ysize) |
194 | fill_maze_full (char **maze, int x, int y, int xsize, int ysize) |
271 | { |
195 | { |
272 | int xc, yc; |
196 | int xc, yc; |
273 | |
197 | |
274 | /* write a wall here */ |
198 | /* write a wall here */ |
275 | maze[x][y] = '#'; |
199 | maze[x][y] = '#'; |
276 | |
200 | |
277 | /* decide if we're going to pick from the wall_free_list */ |
201 | /* decide if we're going to pick from the wall_free_list */ |
278 | if (RANDOM () % 4 && wall_free_size > 0) |
202 | if (rmg_rndm (4) && wall_free_size > 0) |
279 | { |
203 | { |
280 | pop_wall_point (&xc, &yc); |
204 | pop_wall_point (&xc, &yc); |
281 | fill_maze_full (maze, xc, yc, xsize, ysize); |
205 | fill_maze_full (maze, xc, yc, xsize, ysize); |
282 | } |
206 | } |
283 | |
207 | |
284 | /* change the if to a while for a complete maze. */ |
208 | /* change the if to a while for a complete maze. */ |
285 | while (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
209 | while (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
286 | { |
|
|
287 | fill_maze_full (maze, xc, yc, xsize, ysize); |
210 | fill_maze_full (maze, xc, yc, xsize, ysize); |
288 | } |
|
|
289 | } |
211 | } |
290 | |
|
|
291 | |
212 | |
292 | /* recursive routine which will fill much of the maze, but will leave |
213 | /* recursive routine which will fill much of the maze, but will leave |
293 | some free spots (possibly large) toward the center.*/ |
214 | some free spots (possibly large) toward the center.*/ |
294 | |
215 | static void |
295 | void |
|
|
296 | fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize) |
216 | fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize) |
297 | { |
217 | { |
298 | int xc, yc; |
218 | int xc, yc; |
299 | |
219 | |
300 | /* write a wall here */ |
220 | /* write a wall here */ |
301 | maze[x][y] = '#'; |
221 | maze[x][y] = '#'; |
302 | |
222 | |
303 | /* decide if we're going to pick from the wall_free_list */ |
223 | /* decide if we're going to pick from the wall_free_list */ |
304 | if (RANDOM () % 4 && wall_free_size > 0) |
224 | if (rmg_rndm (4) && wall_free_size > 0) |
305 | { |
225 | { |
306 | pop_wall_point (&xc, &yc); |
226 | pop_wall_point (&xc, &yc); |
307 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
227 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
308 | } |
228 | } |
309 | |
229 | |
310 | /* change the if to a while for a complete maze. */ |
230 | /* change the if to a while for a complete maze. */ |
311 | if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
231 | if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
312 | { |
|
|
313 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
232 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
|
|
233 | } |
|
|
234 | |
|
|
235 | /* the outsize interface routine: accepts sizes, returns a char |
|
|
236 | ** maze. option is a flag for either a sparse or a full maze. Sparse |
|
|
237 | mazes have sizable rooms. option = 1, full, 0, sparse.*/ |
|
|
238 | void |
|
|
239 | maze_gen (Layout maze, int option) |
|
|
240 | { |
|
|
241 | maze->clear (); |
|
|
242 | maze->border (); |
|
|
243 | |
|
|
244 | /* find how many free wall spots there are */ |
|
|
245 | wall_free_size = 2 * (maze->w - 4) + 2 * (maze->h - 4); |
|
|
246 | |
|
|
247 | make_wall_free_list (maze->w, maze->h); |
|
|
248 | |
|
|
249 | /* return the empty maze */ |
|
|
250 | if (wall_free_size <= 0) |
|
|
251 | return; |
|
|
252 | |
|
|
253 | /* recursively generate the walls of the maze */ |
|
|
254 | /* first pop a random starting point */ |
|
|
255 | while (wall_free_size > 0) |
314 | } |
256 | { |
|
|
257 | int i, j; |
|
|
258 | |
|
|
259 | pop_wall_point (&i, &j); |
|
|
260 | |
|
|
261 | if (option) |
|
|
262 | fill_maze_full (maze, i, j, maze->w, maze->h); |
|
|
263 | else |
|
|
264 | fill_maze_sparse (maze, i, j, maze->w, maze->h); |
|
|
265 | } |
|
|
266 | |
|
|
267 | /* clean up our intermediate data structures. */ |
|
|
268 | |
|
|
269 | free (wall_x_list); |
|
|
270 | free (wall_y_list); |
315 | } |
271 | } |
|
|
272 | |