Crossfire Server, Trunk
square_spiral.cpp
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 
80 char **make_square_spiral_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
81 {
82  int i, j;
83  int cx, cy;
84  int tx, ty;
85 
86  (void)_unused_option;
87  (void)_unused_layers;
88 
89  /* generate and allocate a doorless, centered onion */
90  char **maze = map_gen_onion(xsize, ysize, OPT_CENTERED|OPT_NO_DOORS, 0);
91 
92  /* find the layout center. */
93  cx = 0;
94  cy = 0;
95  for (i = 0; i < xsize; i++)
96  for (j = 0; j < ysize; j++) {
97  if (maze[i][j] == 'C') {
98  cx = i;
99  cy = j;
100  }
101  }
102  tx = cx;
103  ty = cy;
104  while (1) {
105  find_top_left_corner(maze, &tx, &ty);
106 
107  if (ty < 2 || tx < 2 || tx > xsize-2 || ty > ysize-2) {
108  break;
109  }
110  make_wall(maze, tx, ty-1, 1); /* make a vertical wall with a door */
111 
112  maze[tx][ty-1] = '#'; /* convert the door that make_wall puts here to a wall */
113  maze[tx-1][ty] = 'D';/* make a doorway out of this layer */
114 
115  /* walk left until we find the top-left corner */
116  while ((tx > 2) && maze[tx-1][ty]) {
117  tx--;
118  }
119 
120  make_wall(maze, tx-1, ty, 0); /* make a horizontal wall with a door */
121 
122  /* walk down until we find the bottom-left corner */
123  while (((ty+1) < ysize) && maze[tx][ty+1]) {
124  ty++;
125  }
126 
127  make_wall(maze, tx, ty+1, 1); /* make a vertical wall with a door */
128 
129  /* walk rightuntil we find the bottom-right corner */
130  while (((tx+1) < xsize) && maze[tx+1][ty]) {
131  tx++;
132  }
133 
134  make_wall(maze, tx+1, ty, 0); /* make a horizontal wall with a door */
135  tx++; /* set up for next layer. */
136  }
137 
138  /* place the exits. */
139  if (RANDOM()%2) {
140  maze[cx][cy] = '>';
141  maze[xsize-2][1] = '<';
142  } else {
143  maze[cx][cy] = '<';
144  maze[xsize-2][1] = '>';
145  }
146 
147  return maze;
148 }
global.h
random_map.h
room_gen.h
map_gen_onion
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
Definition: room_gen_onion.cpp:70
maze_gen.h
make_square_spiral_layout
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
Definition: square_spiral.cpp:80
find_top_left_corner
void find_top_left_corner(char **maze, int *cx, int *cy)
Definition: square_spiral.cpp:53
OPT_NO_DOORS
#define OPT_NO_DOORS
Definition: random_map.h:137
make_wall
int make_wall(char **maze, int x, int y, int dir)
Definition: random_map.cpp:658
rproto.h
sproto.h
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
RANDOM
#define RANDOM()
Definition: define.h:644
CFweardisguise.option
option
Definition: CFweardisguise.py:16
OPT_CENTERED
#define OPT_CENTERED
Definition: random_map.h:130