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.9 by root, Mon Apr 14 22:41:17 2008 UTC vs.
Revision 1.31 by root, Sat Nov 17 23:40:02 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines