Crossfire Server, Trunk
room_gen_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 #include "global.h"
22 
23 #include <stdlib.h>
24 
25 #include "random_map.h"
26 
31 #define RANDOM_OPTIONS 0
32 #define REGULAR_SPIRAL 1
33 #define FINE_SPIRAL 2
34 #define FIT_SPIRAL 4
35 #define MAX_SPIRAL_OPT 8
37 
38 #include <math.h>
39 
40 #ifndef MIN
41 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
42 #endif
43 #ifndef MAX
44 #define MAX(x, y) (((x) < (y)) ? (y) : (x))
45 #endif
46 
47 #define MAX_FINE .454545
48 
49 extern int surround_check(char **maze, int i, int j, int xsize, int ysize);
50 
62 char **map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
63 {
64  int i, j;
65  float parm = 0;
66  float x = 0, y = 0;
67  int ic, jc;
68  float SizeX, SizeY;
69  float xscale, yscale;
70 
71  (void)_unused_layers;
72 
73  /* allocate that array, set it up */
74  char **maze = (char **)calloc(sizeof(char *), xsize);
75 
76  for (i = 0; i < xsize; i++) {
77  maze[i] = (char *)calloc(sizeof(char), ysize);
78  }
79 
80  /* slightly easier to fill and then cut */
81  for (i = 0; i < xsize; i++)
82  for (j = 0; j < ysize; j++) {
83  maze[i][j] = '#';
84  }
85 
86  ic = xsize/2;
87  jc = ysize/2;
88  SizeX = xsize/2-2;
89  SizeY = ysize/2-2;
90 
91  /* select random options if necessary */
92  if (option == 0) {
94  }
95 
96  /* the order in which these are evaluated matters*/
97 
98  /* the following two are mutually exclusive.
99  pick one if they're both set. */
100  if ((option&REGULAR_SPIRAL) && (option&FIT_SPIRAL)) {
101  /* unset REGULAR_SPIRAL half the time */
102  if (RANDOM()%2 && (option&REGULAR_SPIRAL)) {
104  } else {
105  option -= FIT_SPIRAL;
106  }
107  }
108 
109  xscale = yscale = MAX_FINE; /* fine spiral */
110 
111  /* choose the spiral pitch */
112  if (!(option&FINE_SPIRAL)) {
113  float pitch = (RANDOM()%5)/10.+10./22.;
114 
115  xscale = yscale = pitch;
116  }
117 
118  if ((option&FIT_SPIRAL) && (xsize != ysize)) {
119  if (xsize > ysize) {
120  xscale *= (float)xsize/(float)ysize;
121  } else {
122  yscale *= (float)ysize/(float)xsize;
123  }
124  }
125 
126  if (option&REGULAR_SPIRAL) {
127  float scale = MIN(xscale, yscale);
128 
129  xscale = yscale = scale;
130  }
131 
132  /* cut out the spiral */
133  while ((fabsf(x) < SizeX) && (fabsf(y) < SizeY)) {
134  x = parm*cos(parm)*xscale;
135  y = parm*sin(parm)*yscale;
136  maze[(int)(ic+x)][(int)(jc+y)] = '\0';
137  parm += 0.01;
138  };
139 
140  maze[(int)(ic+x+0.5)][(int)(jc+y+0.5)] = '<';
141 
142  /* cut out the center in a 2x2 and place the center and downexit */
143  maze[ic][jc+1] = '>';
144  maze[ic][jc] = 'C';
145 
146  return maze;
147 }
148 
159 void connect_spirals(int xsize, int ysize, int sym, char **layout)
160 {
161  int i, j, ic = xsize/2, jc = ysize/2;
162 
163  if (sym == X_SYM) {
164  layout[ic][jc] = 0;
165  /* go left from map center */
166  for (i = ic-1, j = jc; i > 0 && layout[i][j] == '#'; i--) {
167  layout[i][j] = 0;
168  }
169  /* go right */
170  for (i = ic+1, j = jc; i < xsize-1 && layout[i][j] == '#'; i++) {
171  layout[i][j] = 0;
172  }
173  }
174 
175  if (sym == Y_SYM) {
176  layout[ic][jc] = 0;
177  /* go up */
178  for (i = ic, j = jc-1; j > 0 && layout[i][j] == '#'; j--) {
179  layout[i][j] = 0;
180  }
181  /* go down */
182  for (i = ic, j = jc+1; j < ysize-1 && layout[i][j] == '#'; j++) {
183  layout[i][j] = 0;
184  }
185  }
186 
187  if (sym == XY_SYM) {
188  /* go left from map center */
189  layout[ic][jc/2] = 0;
190  layout[ic/2][jc] = 0;
191  layout[ic][jc/2+jc] = 0;
192  layout[ic/2+ic][jc] = 0;
193  for (i = ic-1, j = jc/2; i > 0 && layout[i][j] == '#'; i--) {
194  layout[i][j+jc] = 0;
195  layout[i][j] = 0;
196  }
197  /* go right */
198  for (i = ic+1, j = jc/2; i < xsize-1 && layout[i][j] == '#'; i++) {
199  layout[i][j+jc] = 0;
200  layout[i][j] = 0;
201  }
202  /* go up */
203  for (i = ic/2, j = jc-1; j > 0 && layout[i][j] == '#'; j--) {
204  layout[i][j] = 0;
205  layout[i+ic][j] = 0;
206  }
207  /* go down */
208  for (i = ic/2, j = jc+1; j < ysize-1 && layout[i][j] == '#'; j++) {
209  layout[i][j] = 0;
210  layout[i+ic][j] = 0;
211  }
212  }
213 
214  /* get rid of bad doors. */
215  for (i = 0; i < xsize; i++)
216  for (j = 0; j < ysize; j++) {
217  if (layout[i][j] == 'D') { /* remove bad door. */
218  int si = surround_check(layout, i, j, xsize, ysize);
219  if (si != 3 && si != 12) {
220  layout[i][j] = 0;
221  /* back up and recheck any nearby doors */
222  i = 0;
223  j = 0;
224  }
225  }
226  }
227 }
global.h
random_map.h
layout
Definition: main.c:85
connect_spirals
void connect_spirals(int xsize, int ysize, int sym, char **layout)
Definition: room_gen_spiral.c:159
diamondslots.x
x
Definition: diamondslots.py:15
REGULAR_SPIRAL
#define REGULAR_SPIRAL
Definition: room_gen_spiral.c:32
map_gen_spiral
char ** map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
Definition: room_gen_spiral.c:62
MAX_SPIRAL_OPT
#define MAX_SPIRAL_OPT
Definition: room_gen_spiral.c:35
FINE_SPIRAL
#define FINE_SPIRAL
Definition: room_gen_spiral.c:33
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
MIN
#define MIN(x, y)
Definition: room_gen_spiral.c:41
X_SYM
#define X_SYM
Definition: random_map.h:147
RANDOM
#define RANDOM()
Definition: define.h:644
FIT_SPIRAL
#define FIT_SPIRAL
Definition: room_gen_spiral.c:34
MAX_FINE
#define MAX_FINE
Definition: room_gen_spiral.c:47
diamondslots.y
y
Definition: diamondslots.py:16
CFweardisguise.option
option
Definition: CFweardisguise.py:16
surround_check
int surround_check(char **maze, int i, int j, int xsize, int ysize)
Definition: rogue_layout.c:280
make_face_from_files.int
int
Definition: make_face_from_files.py:26
XY_SYM
#define XY_SYM
Definition: random_map.h:149
Y_SYM
#define Y_SYM
Definition: random_map.h:148