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.1 by elmex, Sun Aug 13 17:16:03 2006 UTC vs.
Revision 1.9 by root, Tue Apr 15 03:00:24 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines