ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_spiral.C
Revision: 1.16
Committed: Mon Oct 12 14:00:58 2009 UTC (14 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_90
Changes since 1.15: +7 -6 lines
Log Message:
clarify license

File Contents

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