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.3 by root, Sun Sep 10 16:06:37 2006 UTC vs.
Revision 1.9 by root, Tue Apr 15 03:00:24 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines