Crossfire Server, Trunk  R20513
rogue_layout.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
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 {
29  int x, y;
30 
32  int sx, sy;
33 
35  int ax, ay, zx, zy;
36 
37  /* Room type (circular or rectangular) */
38  int rtype;
39 } Room;
40 
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 
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 
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 
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 
332 char **roguelike_layout_gen(int xsize, int ysize, int options) {
333  int i, j;
334  Room *Rooms = NULL;
335  Room *walk;
336  int nrooms = 0;
337  int tries = 0;
338 
339  /* allocate that array, write walls everywhere up */
340  char **maze = (char **)malloc(sizeof(char *)*xsize);
341  for (i = 0; i < xsize; i++) {
342  maze[i] = (char *)malloc(sizeof(char)*ysize);
343  for (j = 0; j < ysize; j++) {
344  maze[i][j] = '#';
345  }
346  }
347 
348  /* minimum room size is basically 5x5: if xsize/ysize is
349  less than 3x that then hollow things out, stick in
350  a stairsup and stairs down, and exit */
351 
352  if (xsize < 11 || ysize < 11) {
353  for (i = 1; i < xsize-1; i++)
354  for (j = 1; j < ysize-1; j++) {
355  maze[i][j] = 0;
356  }
357  maze[(xsize-1)/2][(ysize-1)/2] = '>';
358  maze[(xsize-1)/2][(ysize-1)/2+1] = '<';
359  return maze;
360  }
361 
362  /* decide on the number of rooms */
363  nrooms = RANDOM()%10+6;
364  Rooms = (Room *)calloc(nrooms+1, sizeof(Room));
365 
366  /* actually place the rooms */
367  i = 0;
368  while (tries < 450 && i < nrooms) {
369  /* try to place the room */
370  if (!roguelike_place_room(Rooms, xsize, ysize, nrooms)) {
371  tries++;
372  } else {
373  i++;
374  }
375  }
376 
377  if (i == 0) { /* no can do! */
378  for (i = 1; i < xsize-1; i++)
379  for (j = 1; j < ysize-1; j++) {
380  maze[i][j] = 0;
381  }
382  maze[(xsize-1)/2][(ysize-1)/2] = '>';
383  maze[(xsize-1)/2][(ysize-1)/2+1] = '<';
384  free(Rooms);
385  return maze;
386  }
387 
388  /* erase the areas occupied by the rooms */
389  roguelike_make_rooms(Rooms, maze, options);
390 
391  roguelike_link_rooms(Rooms, maze, xsize, ysize);
392 
393  /* put in the stairs */
394 
395  maze[Rooms->x][Rooms->y] = '<';
396  /* get the last one */
397  for (walk = Rooms; walk->x != 0; walk++)
398  ;
399  /* back up one */
400  walk--;
401  if (walk == Rooms) {
402  /* In this case, there is only a single room. We don't want to
403  * clobber are up exit (above) with a down exit, so put the
404  * other exit one space up/down, depending which is a space
405  * and not a wall.
406  */
407  if (maze[walk->x][walk->y+1] == '.') {
408  maze[walk->x][walk->y+1] = '>';
409  } else {
410  maze[walk->x][walk->y-1] = '>';
411  }
412  } else {
413  maze[walk->x][walk->y] = '>';
414  }
415 
416  /* convert all the '.' to 0, we're through with the '.' */
417  for (i = 0; i < xsize; i++)
418  for (j = 0; j < ysize; j++) {
419  if (maze[i][j] == '.') {
420  maze[i][j] = 0;
421  }
422  if (maze[i][j] == 'D') { /* remove bad door. */
423  int si = surround_check(maze, i, j, xsize, ysize);
424 
425  if (si != 3 && si != 12) {
426  maze[i][j] = 0;
427  /* back up and recheck any nearby doors */
428  i = 0;
429  j = 0;
430  }
431  }
432  }
433 
434  free(Rooms);
435  return maze;
436 }
char ** roguelike_layout_gen(int xsize, int ysize, int options)
Actually make the rogue layout.
Definition: rogue_layout.c:332
int sy
Definition: rogue_layout.c:32
static void roguelike_link_rooms(Room *Rooms, char **maze, int xsize, int ysize)
Link generated rooms with corridors.
Definition: rogue_layout.c:180
static struct Command_Line_Options options[]
Actual valid command line options.
Definition: init.c:284
int rtype
Definition: rogue_layout.c:38
static int roguelike_place_room(Room *Rooms, int xsize, int ysize, int nrooms)
Place a room in the layout.
Definition: rogue_layout.c:53
Random map related variables.
int isqrt(int n)
Compute the square root.
Definition: utils.c:585
int ax
Coordinates of the extrema of the rectangle.
Definition: rogue_layout.c:35
#define MAX(x, y)
Definition: compat.h:20
Global type definitions and header inclusions.
int sx
Room size.
Definition: rogue_layout.c:32
#define MIN(x, y)
Definition: compat.h:17
static void roguelike_make_rooms(Room *Rooms, char **maze, int options)
Write all the rooms into the maze.
Definition: rogue_layout.c:132
int ay
Definition: rogue_layout.c:35
int zy
Definition: rogue_layout.c:35
int zx
Definition: rogue_layout.c:35
#define RANDOM()
Definition: define.h:679
int x
Coordinates of room centers.
Definition: rogue_layout.c:29
int y
Definition: rogue_layout.c:29
int surround_check(char **layout, int i, int j, int Xsize, int Ysize)
Checks free spots around a spot.
Definition: rogue_layout.c:303
Definition: main.c:88