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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines