ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_spiral.C
Revision: 1.6
Committed: Sat Jan 6 14:42:30 2007 UTC (17 years, 5 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -0 lines
Log Message:
added some copyrights

File Contents

# Content
1
2 /*
3 CrossFire, A Multiplayer game for X-windows
4
5 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
6 Copyright (C) 1994 Mark Wedel
7 Copyright (C) 1992 Frank Tore Johansen
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 The author can be reached via e-mail to mark@pyramid.com
24 */
25
26
27 /* The onion room generator:
28 Onion rooms are like this:
29
30 char **map_gen_spiral(int xsize, int ysize, int option);
31
32
33 */
34 #include <global.h>
35 #include <random_map.h>
36
37 #define RANDOM_OPTIONS 0 /* Pick random options below */
38 #define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly */
39 #define FINE_SPIRAL 2 /* uses the min. separation: most coiling */
40 #define FIT_SPIRAL 4 /* scale to a rectangular region, not square */
41 #define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */
42 #include <math.h>
43
44 #ifndef MIN
45 # define MIN(x,y) (((x)<(y))? (x):(y))
46 #endif
47 #ifndef MAX
48 # define MAX(x,y) (((x)<(y))? (y):(x))
49 #endif
50
51 #define MINDIST 3
52
53 #define MAX_FINE .454545
54
55 extern int surround_check (char **maze, int i, int j, int xsize, int ysize);
56
57 char **
58 map_gen_spiral (int xsize, int ysize, int option)
59 {
60 int i, j;
61 float parm = 0;
62 float x = 0, y = 0;
63 int ic, jc;
64 float SizeX, SizeY;
65 float xscale, yscale;
66
67 /* allocate that array, set it up */
68 char **maze = (char **) calloc (sizeof (char *), xsize);
69
70 for (i = 0; i < xsize; i++)
71 {
72 maze[i] = (char *) calloc (sizeof (char), ysize);
73 }
74
75 /* slightly easier to fill and then cut */
76 for (i = 0; i < xsize; i++)
77 for (j = 0; j < ysize; j++)
78 maze[i][j] = '#';
79
80 ic = xsize / 2;
81 jc = ysize / 2;
82 SizeX = xsize / 2 - 2;
83 SizeY = ysize / 2 - 2;
84
85 /* select random options if necessary */
86 if (option == 0)
87 {
88 option = RANDOM () % MAX_SPIRAL_OPT;
89 }
90
91 /* the order in which these are evaluated matters */
92
93 /* the following two are mutually exclusive.
94 pick one if they're both set. */
95 if ((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL))
96 {
97 /* unset REGULAR_SPIRAL half the time */
98 if (RANDOM () % 2 && (option & REGULAR_SPIRAL))
99 option -= REGULAR_SPIRAL;
100 else
101 option -= FIT_SPIRAL;
102 }
103
104 xscale = yscale = MAX_FINE; /* fine spiral */
105
106 /* choose the spiral pitch */
107 if (!(option & FINE_SPIRAL))
108 {
109 float pitch = (RANDOM () % 5) / 10. + 10. / 22.;
110
111 xscale = yscale = pitch;
112 }
113
114 if ((option & FIT_SPIRAL) && (xsize != ysize))
115 {
116 if (xsize > ysize)
117 xscale *= (float) xsize / (float) ysize;
118 else
119 yscale *= (float) ysize / (float) xsize;
120 }
121
122 if (option & REGULAR_SPIRAL)
123 {
124 float scale = MIN (xscale, yscale);
125
126 xscale = yscale = scale;
127 }
128
129 /* cut out the spiral */
130 while ((fabs (x) < SizeX) && (fabs (y) < SizeY))
131 {
132 x = parm * cos (parm) * xscale;
133 y = parm * sin (parm) * yscale;
134 maze[(int) (ic + x)][(int) (jc + y)] = '\0';
135 parm += 0.01;
136 };
137
138 maze[(int) (ic + x + 0.5)][(int) (jc + y + 0.5)] = '<';
139
140
141 /* cut out the center in a 2x2 and place the center and downexit */
142 maze[ic][jc + 1] = '>';
143 maze[ic][jc] = 'C';
144
145
146 return maze;
147 }
148
149 /* the following function connects disjoint spirals which may
150 result from the symmetrization process. */
151 void
152 connect_spirals (int xsize, int ysize, int sym, char **layout)
153 {
154
155 int i, j, ic = xsize / 2, jc = ysize / 2;
156
157 if (sym == SYMMETRY_X)
158 {
159 layout[ic][jc] = 0;
160 /* go left from map center */
161 for (i = ic - 1, j = jc; i > 0 && layout[i][j] == '#'; i--)
162 layout[i][j] = 0;
163 /* go right */
164 for (i = ic + 1, j = jc; i < xsize - 1 && layout[i][j] == '#'; i++)
165 layout[i][j] = 0;
166 }
167
168 if (sym == SYMMETRY_Y)
169 {
170
171 layout[ic][jc] = 0;
172 /* go up */
173 for (i = ic, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
174 layout[i][j] = 0;
175 /* go down */
176 for (i = ic, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
177 layout[i][j] = 0;
178 }
179
180 if (sym == SYMMETRY_XY)
181 {
182 /* go left from map center */
183 layout[ic][jc / 2] = 0;
184 layout[ic / 2][jc] = 0;
185 layout[ic][jc / 2 + jc] = 0;
186 layout[ic / 2 + ic][jc] = 0;
187 for (i = ic - 1, j = jc / 2; i > 0 && layout[i][j] == '#'; i--)
188 {
189 layout[i][j + jc] = 0;
190 layout[i][j] = 0;
191 }
192 /* go right */
193 for (i = ic + 1, j = jc / 2; i < xsize - 1 && layout[i][j] == '#'; i++)
194 {
195 layout[i][j + jc] = 0;
196 layout[i][j] = 0;
197 }
198 /* go up */
199 for (i = ic / 2, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
200 {
201 layout[i][j] = 0;
202 layout[i + ic][j] = 0;
203 }
204 /* go down */
205 for (i = ic / 2, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
206 {
207 layout[i][j] = 0;
208 layout[i + ic][j] = 0;
209 }
210
211 }
212
213 /* get rid of bad doors. */
214 for (i = 0; i < xsize; i++)
215 for (j = 0; j < ysize; j++)
216 {
217 if (layout[i][j] == 'D')
218 { /* remove bad door. */
219 int si = surround_check (layout, i, j, xsize, ysize);
220
221 if (si != 3 && si != 12)
222 {
223 layout[i][j] = 0;
224 /* back up and recheck any nearby doors */
225 i = 0;
226 j = 0;
227 }
228 }
229 }
230
231
232
233 }