1 | |
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 | */ |
2 | |
23 | |
3 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
24 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
4 | 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 |
5 | 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. |
6 | |
27 | |
… | |
… | |
14 | |
35 | |
15 | /* we need to maintain a list of wall points to generate |
36 | /* we need to maintain a list of wall points to generate |
16 | reasonable mazes: a straightforward recursive random walk maze |
37 | reasonable mazes: a straightforward recursive random walk maze |
17 | 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 */ |
18 | |
39 | |
19 | #include <stdio.h> |
|
|
20 | #include <global.h> |
40 | #include <global.h> |
21 | |
41 | |
22 | /*#include <random_map.h>*/ |
42 | #include "random_map.h" |
23 | #include <maze_gen.h> |
43 | #include "rproto.h" |
24 | #include <time.h> |
|
|
25 | |
|
|
26 | |
|
|
27 | /* this include solely, and only, is needed for the definition of RANDOM */ |
|
|
28 | |
|
|
29 | |
|
|
30 | |
44 | |
31 | /* 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 |
32 | as parameters every time. */ |
46 | as parameters every time. */ |
33 | int *wall_x_list = 0; |
47 | static int *wall_x_list = 0; |
34 | int *wall_y_list = 0; |
48 | static int *wall_y_list = 0; |
35 | int wall_free_size = 0; |
49 | static int wall_free_size = 0; |
36 | |
|
|
37 | /* heuristically, we need to change wall_chance based on the size of |
|
|
38 | the maze. */ |
|
|
39 | |
|
|
40 | int wall_chance; |
|
|
41 | |
|
|
42 | /* the outsize interface routine: accepts sizes, returns a char |
|
|
43 | ** maze. option is a flag for either a sparse or a full maze. Sparse |
|
|
44 | mazes have sizable rooms. option = 1, full, 0, sparse.*/ |
|
|
45 | |
|
|
46 | char ** |
|
|
47 | maze_gen (int xsize, int ysize, int option) |
|
|
48 | { |
|
|
49 | int i, j; |
|
|
50 | |
|
|
51 | /* allocate that array, set it up */ |
|
|
52 | char **maze = (char **) calloc (sizeof (char *), xsize); |
|
|
53 | |
|
|
54 | for (i = 0; i < xsize; i++) |
|
|
55 | { |
|
|
56 | maze[i] = (char *) calloc (sizeof (char), ysize); |
|
|
57 | } |
|
|
58 | |
|
|
59 | /* write the outer walls */ |
|
|
60 | for (i = 0; i < xsize; i++) |
|
|
61 | maze[i][0] = maze[i][ysize - 1] = '#'; |
|
|
62 | for (j = 0; j < ysize; j++) |
|
|
63 | maze[0][j] = maze[xsize - 1][j] = '#'; |
|
|
64 | |
|
|
65 | |
|
|
66 | /* find how many free wall spots there are */ |
|
|
67 | wall_free_size = 2 * (xsize - 4) + 2 * (ysize - 4); |
|
|
68 | |
|
|
69 | make_wall_free_list (xsize, ysize); |
|
|
70 | |
|
|
71 | /* return the empty maze */ |
|
|
72 | if (wall_free_size <= 0) |
|
|
73 | return maze; |
|
|
74 | |
|
|
75 | /* recursively generate the walls of the maze */ |
|
|
76 | /* first pop a random starting point */ |
|
|
77 | while (wall_free_size > 0) |
|
|
78 | { |
|
|
79 | pop_wall_point (&i, &j); |
|
|
80 | if (option) |
|
|
81 | fill_maze_full (maze, i, j, xsize, ysize); |
|
|
82 | else |
|
|
83 | fill_maze_sparse (maze, i, j, xsize, ysize); |
|
|
84 | } |
|
|
85 | |
|
|
86 | /* clean up our intermediate data structures. */ |
|
|
87 | |
|
|
88 | free (wall_x_list); |
|
|
89 | free (wall_y_list); |
|
|
90 | |
|
|
91 | return maze; |
|
|
92 | } |
|
|
93 | |
|
|
94 | |
|
|
95 | |
50 | |
96 | /* 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 |
97 | 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. */ |
98 | |
53 | static void |
99 | void |
|
|
100 | make_wall_free_list (int xsize, int ysize) |
54 | make_wall_free_list (int xsize, int ysize) |
101 | { |
55 | { |
102 | int i, j, count; |
56 | int i, j, count; |
103 | |
57 | |
104 | count = 0; /* entries already placed in the free list */ |
58 | count = 0; /* entries already placed in the free list */ |
105 | /*allocate it */ |
59 | /*allocate it */ |
106 | if (wall_free_size < 0) |
60 | if (wall_free_size < 0) |
107 | return; |
61 | return; |
|
|
62 | |
108 | wall_x_list = (int *) calloc (sizeof (int), wall_free_size); |
63 | wall_x_list = (int *)calloc (sizeof (int), wall_free_size); |
109 | wall_y_list = (int *) calloc (sizeof (int), wall_free_size); |
64 | wall_y_list = (int *)calloc (sizeof (int), wall_free_size); |
110 | |
|
|
111 | |
65 | |
112 | /* top and bottom wall */ |
66 | /* top and bottom wall */ |
113 | for (i = 2; i < xsize - 2; i++) |
67 | for (i = 2; i < xsize - 2; i++) |
114 | { |
68 | { |
115 | wall_x_list[count] = i; |
69 | wall_x_list[count] = i; |
… | |
… | |
130 | wall_y_list[count] = j; |
84 | wall_y_list[count] = j; |
131 | count++; |
85 | count++; |
132 | } |
86 | } |
133 | } |
87 | } |
134 | |
88 | |
135 | |
|
|
136 | |
|
|
137 | /* randomly returns one of the elements from the wall point list */ |
89 | /* randomly returns one of the elements from the wall point list */ |
138 | |
90 | static void |
139 | void |
|
|
140 | pop_wall_point (int *x, int *y) |
91 | pop_wall_point (int *x, int *y) |
141 | { |
92 | { |
142 | int index = RANDOM () % wall_free_size; |
93 | int index = rmg_rndm (wall_free_size); |
143 | |
94 | |
144 | *x = wall_x_list[index]; |
95 | *x = wall_x_list[index]; |
145 | *y = wall_y_list[index]; |
96 | *y = wall_y_list[index]; |
146 | /* write the last array point here */ |
97 | /* write the last array point here */ |
147 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
98 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
148 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
99 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
149 | wall_free_size--; |
100 | wall_free_size--; |
150 | } |
101 | } |
151 | |
102 | |
152 | |
|
|
153 | |
|
|
154 | /* 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 |
155 | 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 |
156 | up, down, right, or left. */ |
105 | up, down, right, or left. */ |
157 | |
106 | static int |
158 | int |
|
|
159 | 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) |
160 | { |
108 | { |
161 | |
|
|
162 | /* 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 */ |
163 | int dirlist[4]; |
110 | int dirlist[4]; |
164 | int count = 0; /* # elements in dirlist */ |
111 | int count = 0; /* # elements in dirlist */ |
165 | |
112 | |
166 | /* look up */ |
113 | /* look up */ |
167 | 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 */ |
… | |
… | |
169 | 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]; |
170 | |
117 | |
171 | 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]; |
172 | |
119 | |
173 | if (cleartest == 0) |
120 | if (cleartest == 0) |
174 | { |
|
|
175 | dirlist[count] = 1; |
121 | dirlist[count++] = 1; |
176 | count++; |
|
|
177 | } |
|
|
178 | } |
122 | } |
179 | |
|
|
180 | |
123 | |
181 | /* look down */ |
124 | /* look down */ |
182 | 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 */ |
183 | { |
126 | { |
184 | 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]; |
185 | |
128 | |
186 | 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]; |
187 | |
130 | |
188 | if (cleartest == 0) |
131 | if (cleartest == 0) |
189 | { |
|
|
190 | dirlist[count] = 2; |
132 | dirlist[count++] = 2; |
191 | count++; |
|
|
192 | } |
|
|
193 | } |
133 | } |
194 | |
|
|
195 | |
134 | |
196 | /* look right */ |
135 | /* look right */ |
197 | 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 */ |
198 | { |
137 | { |
199 | 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]; |
200 | |
139 | |
201 | 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]; |
202 | |
141 | |
203 | if (cleartest == 0) |
142 | if (cleartest == 0) |
204 | { |
|
|
205 | dirlist[count] = 3; |
143 | dirlist[count++] = 3; |
206 | count++; |
|
|
207 | } |
|
|
208 | } |
144 | } |
209 | |
|
|
210 | |
145 | |
211 | /* look left */ |
146 | /* look left */ |
212 | 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 */ |
213 | { |
148 | { |
214 | 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]; |
215 | |
150 | |
216 | 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]; |
217 | |
152 | |
218 | if (cleartest == 0) |
153 | if (cleartest == 0) |
219 | { |
|
|
220 | dirlist[count] = 4; |
154 | dirlist[count++] = 4; |
221 | count++; |
|
|
222 | } |
|
|
223 | } |
155 | } |
224 | |
156 | |
225 | if (count == 0) |
157 | if (count == 0) |
226 | return -1; /* failed to find any clear points */ |
158 | return -1; /* failed to find any clear points */ |
227 | |
159 | |
228 | /* choose a random direction */ |
160 | /* choose a random direction */ |
229 | if (count > 1) |
|
|
230 | count = RANDOM () % count; |
|
|
231 | else |
|
|
232 | count = 0; |
|
|
233 | switch (dirlist[count]) |
161 | switch (dirlist [rmg_rndm (count)]) |
234 | { |
162 | { |
235 | case 1: /* up */ |
163 | case 1: /* up */ |
236 | { |
|
|
237 | *y = yc + 1; |
164 | *y = yc + 1; |
238 | *x = xc; |
165 | *x = xc; |
239 | break; |
166 | break; |
240 | }; |
167 | |
241 | case 2: /* down */ |
168 | case 2: /* down */ |
242 | { |
|
|
243 | *y = yc - 1; |
169 | *y = yc - 1; |
244 | *x = xc; |
170 | *x = xc; |
245 | break; |
171 | break; |
246 | }; |
172 | |
247 | case 3: /* right */ |
173 | case 3: /* right */ |
248 | { |
|
|
249 | *y = yc; |
174 | *y = yc; |
250 | *x = xc + 1; |
175 | *x = xc + 1; |
251 | break; |
176 | break; |
252 | } |
177 | |
253 | case 4: /* left */ |
178 | case 4: /* left */ |
254 | { |
|
|
255 | *x = xc - 1; |
179 | *x = xc - 1; |
256 | *y = yc; |
180 | *y = yc; |
257 | break; |
181 | break; |
258 | } |
182 | |
259 | default: /* ??? */ |
183 | default: /* ??? */ |
260 | { |
|
|
261 | return -1; |
184 | return -1; |
262 | } |
185 | |
263 | } |
186 | } |
|
|
187 | |
264 | return 1; |
188 | return 1; |
265 | } |
189 | } |
266 | |
190 | |
267 | /* recursive routine which will fill every available space in the maze |
191 | /* recursive routine which will fill every available space in the maze |
268 | with walls*/ |
192 | with walls*/ |
269 | |
193 | static void |
270 | void |
|
|
271 | 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) |
272 | { |
195 | { |
273 | int xc, yc; |
196 | int xc, yc; |
274 | |
197 | |
275 | /* write a wall here */ |
198 | /* write a wall here */ |
276 | maze[x][y] = '#'; |
199 | maze[x][y] = '#'; |
277 | |
200 | |
278 | /* 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 */ |
279 | if (RANDOM () % 4 && wall_free_size > 0) |
202 | if (rmg_rndm (4) && wall_free_size > 0) |
280 | { |
203 | { |
281 | pop_wall_point (&xc, &yc); |
204 | pop_wall_point (&xc, &yc); |
282 | fill_maze_full (maze, xc, yc, xsize, ysize); |
205 | fill_maze_full (maze, xc, yc, xsize, ysize); |
283 | } |
206 | } |
284 | |
207 | |
285 | /* change the if to a while for a complete maze. */ |
208 | /* change the if to a while for a complete maze. */ |
286 | 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) |
287 | { |
|
|
288 | fill_maze_full (maze, xc, yc, xsize, ysize); |
210 | fill_maze_full (maze, xc, yc, xsize, ysize); |
289 | } |
|
|
290 | } |
211 | } |
291 | |
|
|
292 | |
212 | |
293 | /* 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 |
294 | some free spots (possibly large) toward the center.*/ |
214 | some free spots (possibly large) toward the center.*/ |
295 | |
215 | static void |
296 | void |
|
|
297 | 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) |
298 | { |
217 | { |
299 | int xc, yc; |
218 | int xc, yc; |
300 | |
219 | |
301 | /* write a wall here */ |
220 | /* write a wall here */ |
302 | maze[x][y] = '#'; |
221 | maze[x][y] = '#'; |
303 | |
222 | |
304 | /* 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 */ |
305 | if (RANDOM () % 4 && wall_free_size > 0) |
224 | if (rmg_rndm (4) && wall_free_size > 0) |
306 | { |
225 | { |
307 | pop_wall_point (&xc, &yc); |
226 | pop_wall_point (&xc, &yc); |
308 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
227 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
309 | } |
228 | } |
310 | |
229 | |
311 | /* change the if to a while for a complete maze. */ |
230 | /* change the if to a while for a complete maze. */ |
312 | 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) |
313 | { |
|
|
314 | 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) |
315 | } |
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); |
316 | } |
271 | } |
|
|
272 | |