Debian Codegraph @

random_maps/rogue_layout.c

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