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.2 by root, Tue Aug 29 08:01:36 2006 UTC vs.
Revision 1.11 by root, Sat Nov 7 18:30:05 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines