ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/maze_gen.C
(Generate patch)

Comparing deliantra/server/random_maps/maze_gen.C (file contents):
Revision 1.6 by root, Thu Jan 18 19:42:10 2007 UTC vs.
Revision 1.27 by root, Sat Apr 23 04:56:52 2011 UTC

1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 1994-2004 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
3blocked maze with the property that there is only one path from one spot 25blocked maze with the property that there is only one path from one spot
4to any other, and there is always a path from one spot to any other. 26to 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> 40#include <vector>
41
19#include <global.h> 42#include <global.h>
20 43
21/*#include <random_map.h>*/
22#include <maze_gen.h>
23#include <time.h> 44#include <rmg.h>
24 45#include "rproto.h"
25
26/* this include solely, and only, is needed for the definition of RANDOM */
27
28
29 46
30/* global variables that everyone needs: don't want to pass them in 47/* global variables that everyone needs: don't want to pass them in
31 as parameters every time. */ 48 as parameters every time. */
32int *wall_x_list = 0; 49static fixed_stack<point> seeds;
33int *wall_y_list = 0; 50static int xsize, ysize;
34int wall_free_size = 0; 51static char **maze;
35 52
36/* heuristically, we need to change wall_chance based on the size of 53static void
37 the maze. */ 54push (point p)
38
39int 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
43mazes have sizable rooms. option = 1, full, 0, sparse.*/
44
45char **
46maze_gen (int xsize, int ysize, int option)
47{ 55{
48 int i, j; 56 seeds.push (p);
49 57 maze [p.x][p.y] = '#';
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} 58}
92 59
93 60/* randomly returns one of the elements from the wall point list */
61static point
62pop_rand ()
63{
64 return seeds.remove (rmg_rndm (seeds.size));
65}
94 66
95/* the free wall points are those outer points which aren't corners or 67/* 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. */ 68 near corners, and don't have a maze wall growing out of them already. */
97 69static void
98void 70push_walls ()
99make_wall_free_list (int xsize, int ysize)
100{ 71{
101 int i, j, count;
102
103 count = 0; /* entries already placed in the free list */
104 /*allocate it */
105 if (wall_free_size < 0)
106 return;
107 wall_x_list = (int *) calloc (sizeof (int), wall_free_size);
108 wall_y_list = (int *) calloc (sizeof (int), wall_free_size);
109
110
111 /* top and bottom wall */ 72 /* top and bottom wall */
112 for (i = 2; i < xsize - 2; i++) 73 for (int x = 2; x < xsize - 2; x++)
113 { 74 {
114 wall_x_list[count] = i; 75 push (point (x, 0));
115 wall_y_list[count] = 0; 76 push (point (x, ysize - 1));
116 count++;
117 wall_x_list[count] = i;
118 wall_y_list[count] = ysize - 1;
119 count++;
120 } 77 }
121 78
122 /* left and right wall */ 79 /* left and right wall */
123 for (j = 2; j < ysize - 2; j++) 80 for (int y = 2; y < ysize - 2; y++)
124 {
125 wall_x_list[count] = 0;
126 wall_y_list[count] = j;
127 count++;
128 wall_x_list[count] = xsize - 1;
129 wall_y_list[count] = j;
130 count++;
131 } 81 {
82 push (point ( 0, y));
83 push (point (xsize - 1, y));
84 }
132} 85}
133
134
135
136/* randomly returns one of the elements from the wall point list */
137
138void
139pop_wall_point (int *x, int *y)
140{
141 int index = RANDOM () % wall_free_size;
142
143 *x = wall_x_list[index];
144 *y = wall_y_list[index];
145 /* write the last array point here */
146 wall_x_list[index] = wall_x_list[wall_free_size - 1];
147 wall_y_list[index] = wall_y_list[wall_free_size - 1];
148 wall_free_size--;
149}
150
151
152 86
153/* find free point: randomly look for a square adjacent to this one where 87/* find free point: randomly look for a square adjacent to this one where
154we can place a new block without closing a path. We may only look 88we can place a new block without closing a path. We may only look
155up, down, right, or left. */ 89up, down, right, or left. */
156 90static int
157int 91find_free_point (point &p, point pc)
158find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize)
159{ 92{
160
161/* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ 93 /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */
162 int dirlist[4]; 94 int dirlist[4];
163 int count = 0; /* # elements in dirlist */ 95 int count = 0; /* # elements in dirlist */
164 96
97 int xc = pc.x;
98 int yc = pc.y;
99
165 /* look up */ 100 /* look up */
166 if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */ 101 if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */
167 { 102 {
168 int cleartest = (int) maze[xc][yc + 1] + (int) maze[xc - 1][yc + 1] + (int) maze[xc + 1][yc + 1]; 103 int cleartest = maze[xc][yc + 1] + maze[xc - 1][yc + 1] + maze[xc + 1][yc + 1]
169
170 cleartest += (int) maze[xc][yc + 2] + (int) maze[xc - 1][yc + 2] + (int) maze[xc + 1][yc + 2]; 104 + maze[xc][yc + 2] + maze[xc - 1][yc + 2] + maze[xc + 1][yc + 2];
171 105
172 if (cleartest == 0) 106 if (cleartest == 0)
173 {
174 dirlist[count] = 1; 107 dirlist[count++] = 1;
175 count++;
176 }
177 } 108 }
178
179 109
180 /* look down */ 110 /* look down */
181 if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ 111 if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */
182 { 112 {
183 int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; 113 int cleartest = maze[xc][yc - 1] + maze[xc - 1][yc - 1] + maze[xc + 1][yc - 1]
184
185 cleartest += (int) maze[xc][yc - 2] + (int) maze[xc - 1][yc - 2] + (int) maze[xc + 1][yc - 2]; 114 + maze[xc][yc - 2] + maze[xc - 1][yc - 2] + maze[xc + 1][yc - 2];
186 115
187 if (cleartest == 0) 116 if (cleartest == 0)
188 {
189 dirlist[count] = 2; 117 dirlist[count++] = 2;
190 count++;
191 }
192 } 118 }
193
194 119
195 /* look right */ 120 /* look right */
196 if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ 121 if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */
197 { 122 {
198 int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; 123 int cleartest = maze[xc + 1][yc] + maze[xc + 1][yc - 1] + maze[xc + 1][yc + 1]
199
200 cleartest += (int) maze[xc + 2][yc] + (int) maze[xc + 2][yc - 1] + (int) maze[xc + 2][yc + 1]; 124 + maze[xc + 2][yc] + maze[xc + 2][yc - 1] + maze[xc + 2][yc + 1];
201 125
202 if (cleartest == 0) 126 if (cleartest == 0)
203 {
204 dirlist[count] = 3; 127 dirlist[count++] = 3;
205 count++;
206 }
207 } 128 }
208
209 129
210 /* look left */ 130 /* look left */
211 if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ 131 if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */
212 { 132 {
213 int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; 133 int cleartest = maze[xc - 1][yc] + maze[xc - 1][yc - 1] + maze[xc - 1][yc + 1]
214
215 cleartest += (int) maze[xc - 2][yc] + (int) maze[xc - 2][yc - 1] + (int) maze[xc - 2][yc + 1]; 134 + maze[xc - 2][yc] + maze[xc - 2][yc - 1] + maze[xc - 2][yc + 1];
216 135
217 if (cleartest == 0) 136 if (cleartest == 0)
218 {
219 dirlist[count] = 4; 137 dirlist[count++] = 4;
220 count++;
221 }
222 } 138 }
223 139
224 if (count == 0) 140 if (count == 0)
225 return -1; /* failed to find any clear points */ 141 return -1; /* failed to find any clear points */
226 142
227 /* choose a random direction */ 143 /* choose a random direction */
228 if (count > 1)
229 count = RANDOM () % count;
230 else
231 count = 0;
232 switch (dirlist[count]) 144 switch (dirlist [rmg_rndm (count)])
233 { 145 {
234 case 1: /* up */ 146 case 1: /* up */
147 p.x = xc;
148 p.y = yc + 1;
149 break;
150
151 case 2: /* down */
152 p.x = xc;
153 p.y = yc - 1;
154 break;
155
156 case 3: /* right */
157 p.x = xc + 1;
158 p.y = yc;
159 break;
160
161 case 4: /* left */
162 p.x = xc - 1;
163 p.y = yc;
164 break;
165 }
166
167 return 1;
168}
169
170/* the outsize interface routine: accepts sizes, returns a char
171** maze. option is a flag for either a sparse or a full maze. Sparse
172mazes have sizable rooms. option = 3=full, 2=braided, 1=sparse, 0=rooms.*/
173void
174maze_gen (layout &maze, int subtype)
175{
176 xsize = maze.w;
177 ysize = maze.h;
178 ::maze = maze;
179
180 maze.clear ();
181 maze.border ();
182
183 if (xsize < 4 || ysize < 4)
184 return;
185
186 seeds.reset (xsize * ysize);
187
188 if (subtype > 0)
189 push_walls ();
190
191 if (subtype == 0 || subtype == 2)
192 for (int i = (xsize + ysize) / 2; i; --i)
193 push (point (rmg_rndm (1, xsize - 2), rmg_rndm (1, ysize - 2)));
194
195 bool full = subtype == 3;
196
197 /* recursively generate the walls of the maze */
198 /* first pop a random starting point */
199 while (seeds.size)
200 {
201 point p = pop_rand ();
202
203 for (;;)
235 { 204 {
236 *y = yc + 1; 205 point pc;
237 *x = xc; 206
207 maze [p.x][p.y] = '#';
208
209 if (find_free_point (pc, p) < 0)
238 break; 210 break;
239 }; 211
240 case 2: /* down */ 212 if (full)
213 push (p);
214
215 if (!rmg_rndm (8))
241 { 216 {
242 *y = yc - 1; 217 if (!full)
243 *x = xc; 218 push (pc);
219
244 break; 220 break;
245 }; 221 }
246 case 3: /* right */ 222
247 {
248 *y = yc; 223 p = pc;
249 *x = xc + 1;
250 break;
251 } 224 }
252 case 4: /* left */
253 {
254 *x = xc - 1;
255 *y = yc;
256 break;
257 }
258 default: /* ??? */
259 {
260 return -1;
261 }
262 }
263 return 1;
264}
265
266/* recursive routine which will fill every available space in the maze
267 with walls*/
268
269void
270fill_maze_full (char **maze, int x, int y, int xsize, int ysize)
271{
272 int xc, yc;
273
274 /* write a wall here */
275 maze[x][y] = '#';
276
277 /* decide if we're going to pick from the wall_free_list */
278 if (rndm (4) && wall_free_size > 0)
279 { 225 }
280 pop_wall_point (&xc, &yc);
281 fill_maze_full (maze, xc, yc, xsize, ysize);
282 }
283 226
284 /* change the if to a while for a complete maze. */ 227 seeds.free ();
285 while (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1)
286 {
287 fill_maze_full (maze, xc, yc, xsize, ysize);
288 }
289} 228}
290 229
291
292/* recursive routine which will fill much of the maze, but will leave
293 some free spots (possibly large) toward the center.*/
294
295void
296fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize)
297{
298 int xc, yc;
299
300 /* write a wall here */
301 maze[x][y] = '#';
302
303 /* decide if we're going to pick from the wall_free_list */
304 if (rndm (4) && wall_free_size > 0)
305 {
306 pop_wall_point (&xc, &yc);
307 fill_maze_sparse (maze, xc, yc, xsize, ysize);
308 }
309
310 /* change the if to a while for a complete maze. */
311 if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1)
312 {
313 fill_maze_sparse (maze, xc, yc, xsize, ysize);
314 }
315}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines