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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines