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

Comparing deliantra/server/random_maps/rogue_layout.C (file contents):
Revision 1.6 by root, Sat Jan 27 02:19:37 2007 UTC vs.
Revision 1.11 by root, Sat Nov 7 18:30:05 2009 UTC

1
2/* generate a rogue/nethack-like layout */ 1/* generate a rogue/nethack-like layout */
3#include <global.h> 2#include <global.h>
4#include <random_map.h> 3#include <random_map.h>
5#include <math.h> 4#include <rproto.h>
6 5
7typedef struct 6typedef struct
8{ 7{
9 int x; 8 int x;
10 int y; /* coordinates of room centers */ 9 int y; /* coordinates of room centers */
14 int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */ 13 int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */
15 14
16 int rtype; /* circle or rectangular */ 15 int rtype; /* circle or rectangular */
17} Room; 16} Room;
18 17
19static int roguelike_place_room (Room * Rooms, int xsize, int ysize, int nrooms); 18static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms);
20static void roguelike_make_rooms (Room * Rooms, char **maze, int options); 19static void roguelike_make_rooms (Room *rooms, char **maze, int options);
21static void roguelike_link_rooms (Room * Rooms, char **maze, int xsize, int ysize); 20static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize);
22 21
23int 22int
24surround_check (char **layout, int i, int j, int Xsize, int Ysize) 23surround_check (char **layout, int i, int j, int Xsize, int Ysize)
25{ 24{
26 /* 1 = wall to left, 25 /* 1 = wall to left,
27 2 = wall to right, 26 2 = wall to right,
28 4 = wall above 27 4 = wall above
29 8 = wall below */ 28 8 = wall below */
30 int surround_index = 0; 29 int surround_index = 0;
31 30
32 if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) 31 if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) surround_index |= 1;
33 surround_index += 1;
34 if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) 32 if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) surround_index |= 2;
35 surround_index += 2;
36 if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) 33 if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) surround_index |= 4;
37 surround_index += 4;
38 if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) 34 if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) surround_index |= 8;
39 surround_index += 8; 35
40 return surround_index; 36 return surround_index;
41} 37}
42
43 38
44/* actually make the layout: we work by a reduction process: 39/* actually make the layout: we work by a reduction process:
45 * first we make everything a wall, then we remove areas to make rooms 40 * first we make everything a wall, then we remove areas to make rooms
46 */ 41 */
47 42void
48char ** 43roguelike_layout_gen (Layout maze, int options)
49roguelike_layout_gen (int xsize, int ysize, int options)
50{ 44{
51 int i, j; 45 int i, j;
52 Room *Rooms = 0;
53 Room *walk; 46 Room *walk;
54 int nrooms = 0; 47 int nrooms = 0;
55 int tries = 0; 48 int tries = 0;
56 49
57 /* allocate that array, write walls everywhere up */ 50 int xsize = maze->w;
58 char **maze = (char **) malloc (sizeof (char *) * xsize); 51 int ysize = maze->h;
59
60 for (i = 0; i < xsize; i++)
61 {
62 maze[i] = (char *) malloc (sizeof (char) * ysize);
63 for (j = 0; j < ysize; j++)
64 maze[i][j] = '#';
65 }
66 52
67 /* minimum room size is basically 5x5: if xsize/ysize is 53 /* minimum room size is basically 5x5: if xsize/ysize is
68 less than 3x that then hollow things out, stick in 54 less than 3x that then hollow things out, stick in
69 a stairsup and stairs down, and exit */ 55 a stairsup and stairs down, and exit */
70
71 if (xsize < 11 || ysize < 11) 56 if (xsize < 11 || ysize < 11)
72 { 57 {
73 for (i = 1; i < xsize - 1; i++) 58 maze->clear ();
74 for (j = 1; j < ysize - 1; j++) 59 maze->border ();
75 maze[i][j] = 0; 60
76 maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; 61 maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
77 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 62 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
63
78 return maze; 64 return;
79 } 65 }
66
67 maze->clear ('#');
80 68
81 /* decide on the number of rooms */ 69 /* decide on the number of rooms */
82 nrooms = rndm (10) + 6; 70 nrooms = rmg_rndm (10) + 6;
83 Rooms = (Room *) calloc (nrooms + 1, sizeof (Room)); 71 Room *rooms = salloc0<Room> (nrooms + 1);
84 72
85 /* actually place the rooms */ 73 /* actually place the rooms */
86 i = 0; 74 i = 0;
87 while (tries < 450 && i < nrooms) 75 while (tries < 450 && i < nrooms)
88 { 76 {
89 /* try to place the room */ 77 /* try to place the room */
90 if (!roguelike_place_room (Rooms, xsize, ysize, nrooms)) 78 if (!roguelike_place_room (rooms, xsize, ysize, nrooms))
91 tries++; 79 tries++;
92 else 80 else
93 i++; 81 i++;
94 } 82 }
95 83
96 if (i == 0) 84 if (i == 0) /* no can do! */
97 { /* no can do! */ 85 {
98 for (i = 1; i < xsize - 1; i++) 86 maze->clear ();
99 for (j = 1; j < ysize - 1; j++) 87 maze->border ();
100 maze[i][j] = 0; 88
101 maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; 89 maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>';
102 maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; 90 maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<';
103 free (Rooms); 91
92 sfree (rooms, nrooms + 1);
104 return maze; 93 return;
105 } 94 }
106
107 95
108 /* erase the areas occupied by the rooms */ 96 /* erase the areas occupied by the rooms */
109 roguelike_make_rooms (Rooms, maze, options); 97 roguelike_make_rooms (rooms, maze, options);
110 98
111 roguelike_link_rooms (Rooms, maze, xsize, ysize); 99 roguelike_link_rooms (rooms, maze, xsize, ysize);
112 100
113 /* put in the stairs */ 101 /* put in the stairs */
114 102
115 maze[Rooms->x][Rooms->y] = '<'; 103 maze[rooms->x][rooms->y] = '<';
104
116 /* get the last one */ 105 /* get the last one */
117 for (walk = Rooms; walk->x != 0; walk++); 106 for (walk = rooms; walk->x != 0; walk++)
107 ;
108
118 /* back up one */ 109 /* back up one */
119 walk--; 110 walk--;
111
120 if (walk == Rooms) 112 if (walk == rooms)
121 { 113 {
122 /* In this case, there is only a single room. We don't want to 114 /* In this case, there is only a single room. We don't want to
123 * clobber are up exit (above) with a down exit, so put the 115 * clobber are up exit (above) with a down exit, so put the
124 * other exit one space up/down, depending which is a space 116 * other exit one space up/down, depending which is a space
125 * and not a wall. 117 * and not a wall.
136 for (i = 0; i < xsize; i++) 128 for (i = 0; i < xsize; i++)
137 for (j = 0; j < ysize; j++) 129 for (j = 0; j < ysize; j++)
138 { 130 {
139 if (maze[i][j] == '.') 131 if (maze[i][j] == '.')
140 maze[i][j] = 0; 132 maze[i][j] = 0;
133
141 if (maze[i][j] == 'D') 134 if (maze[i][j] == 'D')
142 { /* remove bad door. */ 135 { /* remove bad door. */
143 int si = surround_check (maze, i, j, xsize, ysize); 136 int si = surround_check (maze, i, j, xsize, ysize);
144 137
145 if (si != 3 && si != 12) 138 if (si != 3 && si != 12)
150 j = 0; 143 j = 0;
151 } 144 }
152 } 145 }
153 } 146 }
154 147
155 free (Rooms); 148 sfree (rooms, nrooms + 1);
156 return maze;
157} 149}
158
159
160 150
161static int 151static int
162roguelike_place_room (Room * Rooms, int xsize, int ysize, int nrooms) 152roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms)
163{ 153{
164
165 int tx, ty; /* trial center locations */ 154 int tx, ty; /* trial center locations */
166 int sx, sy; /* trial sizes */ 155 int sx, sy; /* trial sizes */
167 int ax, ay; /* min coords of rect */ 156 int ax, ay; /* min coords of rect */
168 int zx, zy; /* max coords of rect */ 157 int zx, zy; /* max coords of rect */
169 int x_basesize; 158 int x_basesize;
170 int y_basesize; 159 int y_basesize;
171 Room *walk; 160 Room *walk;
172 161
173 /* decide on the base x and y sizes */ 162 /* decide on the base x and y sizes */
174
175 x_basesize = xsize / isqrt (nrooms); 163 x_basesize = xsize / isqrt (nrooms);
176 y_basesize = ysize / isqrt (nrooms); 164 y_basesize = ysize / isqrt (nrooms);
177 165
178 tx = rndm (xsize); 166 tx = rmg_rndm (xsize);
179 ty = rndm (ysize); 167 ty = rmg_rndm (ysize);
180 168
181 /* generate a distribution of sizes centered about basesize */ 169 /* generate a distribution of sizes centered about basesize */
182 sx = rndm (x_basesize) + rndm (x_basesize) + rndm (x_basesize); 170 sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize);
183 sy = rndm (y_basesize) + rndm (y_basesize) + rndm (y_basesize); 171 sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize);
184 sy = (int) (sy * .5); /* renormalize */ 172 sy = (int) (sy * .5); /* renormalize */
185 173
186 /* find the corners */ 174 /* find the corners */
187 ax = tx - sx / 2; 175 ax = tx - sx / 2;
188 zx = tx + sx / 2 + sx % 2; 176 zx = tx + sx / 2 + sx % 2;
189 177
190 ay = ty - sy / 2; 178 ay = ty - sy / 2;
191 zy = ty + sy / 2 + sy % 2; 179 zy = ty + sy / 2 + sy % 2;
192 180
193 /* check to see if it's in the map */ 181 /* check to see if it's in the map */
194 if (zx > xsize - 1 || ax < 1) 182 if (zx > xsize - 1 || ax < 1) return 0;
195 return 0;
196 if (zy > ysize - 1 || ay < 1) 183 if (zy > ysize - 1 || ay < 1) return 0;
197 return 0;
198 184
199 /* no small fish */ 185 /* no small fish */
200 if (sx < 3 || sy < 3) 186 if (sx < 3 || sy < 3)
201 return 0; 187 return 0;
202 188
203 /* check overlap with existing rooms */ 189 /* check overlap with existing rooms */
204 for (walk = Rooms; walk->x != 0; walk++) 190 for (walk = rooms; walk->x != 0; walk++)
205 { 191 {
206 int dx = abs (tx - walk->x); 192 int dx = abs (tx - walk->x);
207 int dy = abs (ty - walk->y); 193 int dy = abs (ty - walk->y);
208 194
209 if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2)) 195 if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2))
211 } 197 }
212 198
213 /* if we've got here, presumably the room is OK. */ 199 /* if we've got here, presumably the room is OK. */
214 200
215 /* get a pointer to the first free room */ 201 /* get a pointer to the first free room */
216 for (walk = Rooms; walk->x != 0; walk++); 202 for (walk = rooms; walk->x != 0; walk++)
203 ;
204
217 walk->x = tx; 205 walk->x = tx;
218 walk->y = ty; 206 walk->y = ty;
219 walk->sx = sx; 207 walk->sx = sx;
220 walk->sy = sy; 208 walk->sy = sy;
221 walk->ax = ax; 209 walk->ax = ax;
222 walk->ay = ay; 210 walk->ay = ay;
223 walk->zx = zx; 211 walk->zx = zx;
224 walk->zy = zy; 212 walk->zy = zy;
213
225 return 1; /* success */ 214 return 1; /* success */
226
227} 215}
228
229 216
230/* write all the rooms into the maze */ 217/* write all the rooms into the maze */
231static void 218static void
232roguelike_make_rooms (Room * Rooms, char **maze, int options) 219roguelike_make_rooms (Room *rooms, char **maze, int options)
233{ 220{
234 int making_circle = 0; 221 int making_circle = 0;
235 int i, j; 222 int i, j;
236 int R; 223 int R;
237 Room *walk; 224 Room *walk;
238 225
239 for (walk = Rooms; walk->x != 0; walk++) 226 for (walk = rooms; walk->x != 0; walk++)
240 { 227 {
241 /* first decide what shape to make */ 228 /* first decide what shape to make */
242 switch (options) 229 switch (options)
243 { 230 {
244 case 1: 231 case 1:
246 break; 233 break;
247 case 2: 234 case 2:
248 making_circle = 1; 235 making_circle = 1;
249 break; 236 break;
250 default: 237 default:
251 making_circle = ((rndm (3) == 0) ? 1 : 0); 238 making_circle = ((rmg_rndm (3) == 0) ? 1 : 0);
252 break; 239 break;
253 } 240 }
254 241
255 if (walk->sx < walk->sy) 242 if (walk->sx < walk->sy)
256 R = walk->sx / 2; 243 R = walk->sx / 2;
258 R = walk->sy / 2; 245 R = walk->sy / 2;
259 246
260 /* enscribe a rectangle */ 247 /* enscribe a rectangle */
261 for (i = walk->ax; i < walk->zx; i++) 248 for (i = walk->ax; i < walk->zx; i++)
262 for (j = walk->ay; j < walk->zy; j++) 249 for (j = walk->ay; j < walk->zy; j++)
263 {
264 if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) 250 if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R)
265 maze[i][j] = '.'; 251 maze[i][j] = '.';
266 }
267 } 252 }
268} 253}
269
270
271 254
272static void 255static void
273roguelike_link_rooms (Room * Rooms, char **maze, int xsize, int ysize) 256roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize)
274{ 257{
275 Room *walk; 258 Room *walk;
276 int i, j; 259 int i, j;
277 260
278 /* link each room to the previous room */ 261 /* link each room to the previous room */
279 if (Rooms[1].x == 0) 262 if (rooms[1].x == 0)
280 return; /* only 1 room */ 263 return; /* only 1 room */
281 264
282 for (walk = Rooms + 1; walk->x != 0; walk++) 265 for (walk = rooms + 1; walk->x != 0; walk++)
283 { 266 {
284 int x1 = walk->x; 267 int x1 = walk->x;
285 int y1 = walk->y; 268 int y1 = walk->y;
286 int x2 = (walk - 1)->x; 269 int x2 = (walk - 1)->x;
287 int y2 = (walk - 1)->y; 270 int y2 = (walk - 1)->y;
288 int in_wall = 0; 271 int in_wall = 0;
289 272
290 if (rndm (2)) 273 if (rmg_rndm (2))
291 { /* connect in x direction first */ 274 { /* connect in x direction first */
292 /* horizontal connect */ 275 /* horizontal connect */
293 /* swap (x1,y1) (x2,y2) if necessary */ 276 /* swap (x1,y1) (x2,y2) if necessary */
294 277
295 if (x2 < x1) 278 if (x2 < x1)
317 maze[i - 1][j] = 'D'; 300 maze[i - 1][j] = 'D';
318 } 301 }
319 else if (maze[i][j] != 'D' && maze[i][j] != '.') 302 else if (maze[i][j] != 'D' && maze[i][j] != '.')
320 maze[i][j] = 0; 303 maze[i][j] = 0;
321 } 304 }
305
322 j = MIN (y1, y2); 306 j = min (y1, y2);
307
323 if (maze[i][j] == '.') 308 if (maze[i][j] == '.')
324 in_wall = 0; 309 in_wall = 0;
310
325 if (maze[i][j] == 0 || maze[i][j] == '#') 311 if (maze[i][j] == 0 || maze[i][j] == '#')
326 in_wall = 1; 312 in_wall = 1;
313
327 for ( /* j set already */ ; j < MAX (y1, y2); j++) 314 for ( /* j set already */ ; j < max (y1, y2); j++)
328 { 315 {
329 if (in_wall == 0 && maze[i][j] == '#') 316 if (in_wall == 0 && maze[i][j] == '#')
330 { 317 {
331 in_wall = 1; 318 in_wall = 1;
332 maze[i][j] = 'D'; 319 maze[i][j] = 'D';
370 } 357 }
371 else if (maze[i][j] != 'D' && maze[i][j] != '.') 358 else if (maze[i][j] != 'D' && maze[i][j] != '.')
372 maze[i][j] = 0; 359 maze[i][j] = 0;
373 } 360 }
374 361
375 i = MIN (x1, x2); 362 i = min (x1, x2);
363
376 if (maze[i][j] == '.') 364 if (maze[i][j] == '.')
377 in_wall = 0; 365 in_wall = 0;
366
378 if (maze[i][j] == 0 || maze[i][j] == '#') 367 if (maze[i][j] == 0 || maze[i][j] == '#')
379 in_wall = 1; 368 in_wall = 1;
369
380 for ( /* i set already */ ; i < MAX (x1, x2); i++) 370 for ( /* i set already */ ; i < max (x1, x2); i++)
381 { 371 {
382 if (in_wall == 0 && maze[i][j] == '#') 372 if (in_wall == 0 && maze[i][j] == '#')
383 { 373 {
384 in_wall = 1; 374 in_wall = 1;
385 maze[i][j] = 'D'; 375 maze[i][j] = 'D';
396 386
397 } 387 }
398 388
399 } 389 }
400} 390}
391

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines