Crossfire Server, Trunk  R21466
square_spiral.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 
21 /* peterm@langmuir.eecs.berkeley.edu: this function generates a random
22 snake-type layout.
23 */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <global.h>
28 #include <time.h>
29 
30 #include <maze_gen.h>
31 #include <room_gen.h>
32 #include <random_map.h>
33 #include <sproto.h>
34 #include <rproto.h>
35 
36 char **map_gen_onion(int xsize, int ysize, int option, int layers);
37 
38 /* These are some helper functions which help with
39  manipulating a centered onion and turning it into
40  a square spiral */
41 
53 void find_top_left_corner(char **maze, int *cx, int *cy)
54 {
55  (*cy)--;
56  /* find the top wall. */
57  while (maze[*cx][*cy] == 0) {
58  (*cy)--;
59  }
60  /* proceed right until a corner is detected */
61  while (maze[*cx][*cy+1] == 0) {
62  (*cx)++;
63  }
64 
65  /* cx and cy should now be the top-right corner of the onion layer */
66 }
67 
78 char **make_square_spiral_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
79 {
80  int i, j;
81  int cx, cy;
82  int tx, ty;
83 
84  (void)_unused_option;
85  (void)_unused_layers;
86 
87  /* generate and allocate a doorless, centered onion */
88  char **maze = map_gen_onion(xsize, ysize, OPT_CENTERED|OPT_NO_DOORS, 0);
89 
90  /* find the layout center. */
91  cx = 0;
92  cy = 0;
93  for (i = 0; i < xsize; i++)
94  for (j = 0; j < ysize; j++) {
95  if (maze[i][j] == 'C') {
96  cx = i;
97  cy = j;
98  }
99  }
100  tx = cx;
101  ty = cy;
102  while (1) {
103  find_top_left_corner(maze, &tx, &ty);
104 
105  if (ty < 2 || tx < 2 || tx > xsize-2 || ty > ysize-2) {
106  break;
107  }
108  make_wall(maze, tx, ty-1, 1); /* make a vertical wall with a door */
109 
110  maze[tx][ty-1] = '#'; /* convert the door that make_wall puts here to a wall */
111  maze[tx-1][ty] = 'D';/* make a doorway out of this layer */
112 
113  /* walk left until we find the top-left corner */
114  while ((tx > 2) && maze[tx-1][ty]) {
115  tx--;
116  }
117 
118  make_wall(maze, tx-1, ty, 0); /* make a horizontal wall with a door */
119 
120  /* walk down until we find the bottom-left corner */
121  while (((ty+1) < ysize) && maze[tx][ty+1]) {
122  ty++;
123  }
124 
125  make_wall(maze, tx, ty+1, 1); /* make a vertical wall with a door */
126 
127  /* walk rightuntil we find the bottom-right corner */
128  while (((tx+1) < xsize) && maze[tx+1][ty]) {
129  tx++;
130  }
131 
132  make_wall(maze, tx+1, ty, 0); /* make a horizontal wall with a door */
133  tx++; /* set up for next layer. */
134  }
135 
136  /* place the exits. */
137  if (RANDOM()%2) {
138  maze[cx][cy] = '>';
139  maze[xsize-2][1] = '<';
140  } else {
141  maze[cx][cy] = '<';
142  maze[xsize-2][1] = '>';
143  }
144 
145  return maze;
146 }
void find_top_left_corner(char **maze, int *cx, int *cy)
Definition: square_spiral.c:53
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
Definition: square_spiral.c:78
#define OPT_CENTERED
Definition: random_map.h:122
#define RANDOM()
Definition: define.h:682
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
#define OPT_NO_DOORS
Definition: random_map.h:129
int make_wall(char **maze, int x, int y, int dir)
Definition: random_map.c:644