Atrinik Server  4.0
square_spiral.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 
40 void find_top_left_corner(char **maze, int *cx, int *cy)
41 {
42  (*cy)--;
43 
44  /* Find the top wall. */
45  while (maze[*cx][*cy] == '\0') {
46  (*cy)--;
47  }
48 
49  /* Proceed right until a corner is detected */
50  while (maze[*cx][*cy + 1] == '\0') {
51  (*cx)++;
52  }
53 
54  /* cx and cy should now be the top-right corner of the onion layer */
55 }
56 
66 char **make_square_spiral_layout(int xsize, int ysize)
67 {
68  int i, j;
69  int cx = 0, cy = 0;
70  int tx, ty;
71 
72  /* Generate and allocate a doorless, centered onion */
73  char **maze = map_gen_onion(xsize, ysize, OPT_CENTERED | OPT_NO_DOORS, 0);
74 
75  /* Find the layout center. */
76  for (i = 0; i < xsize; i++) {
77  for (j = 0; j < ysize; j++) {
78  if (maze[i][j] == 'C') {
79  cx = i;
80  cy = j;
81  }
82  }
83  }
84 
85  tx = cx;
86  ty = cy;
87 
88  while (1) {
89  find_top_left_corner(maze, &tx, &ty);
90 
91  if (ty < 2 || tx < 2 || tx > xsize - 2 || ty > ysize - 2) {
92  break;
93  }
94 
95  /* make a vertical wall with a door */
96  make_wall(maze, tx, ty - 1, 1);
97 
98  /* convert the door that make_wall puts here to a wall */
99  maze[tx][ty - 1] = '#';
100 
101  /* make a doorway out of this layer */
102  maze[tx - 1][ty] = 'D';
103 
104  /* walk left until we find the top-left corner */
105  while (maze[tx - 1][ty]) {
106  tx--;
107  }
108 
109  /* make a horizontal wall with a door */
110  make_wall(maze, tx - 1, ty, 0);
111 
112  /* walk down until we find the bottom-left corner */
113  while (maze[tx][ty + 1]) {
114  ty++;
115  }
116 
117  /* make a vertical wall with a door */
118  make_wall(maze, tx, ty + 1, 1);
119 
120  /* walk rightuntil we find the bottom-right corner */
121  while (maze[tx + 1][ty]) {
122  tx++;
123  }
124 
125  /* make a horizontal wall with a door */
126  make_wall(maze, tx + 1, ty, 0);
127 
128  /* set up for next layer. */
129  tx++;
130  }
131 
132  /* place the exits. */
133  if (rndm_chance(2)) {
134  maze[cx][cy] = '>';
135  maze[xsize - 2][1] = '<';
136  } else {
137  maze[cx][cy] = '<';
138  maze[xsize - 2][1] = '>';
139  }
140 
141  return maze;
142 }
void find_top_left_corner(char **maze, int *cx, int *cy)
Definition: square_spiral.c:40
#define OPT_CENTERED
Definition: random_map.h:155
int make_wall(char **maze, int x, int y, int dir)
Definition: random_map.c:709
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
char ** make_square_spiral_layout(int xsize, int ysize)
Definition: square_spiral.c:66