ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_spiral.C
Revision: 1.11
Committed: Thu Nov 8 19:43:25 2007 UTC (16 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_4, rel-2_32, rel-2_43, rel-2_42, rel-2_41
Changes since 1.10: +4 -4 lines
Log Message:
update copyrights and other minor stuff to deliantra

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.11 * Copyright (©) 2005,2006,2007 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.11 * Deliantra is free software: you can redistribute it and/or modify
9 pippijn 1.7 * it under the terms of the GNU General Public License as published by
10 root 1.10 * the Free Software Foundation, either version 3 of the License, or
11 pippijn 1.7 * (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 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     * You should have received a copy of the GNU General Public License
19 root 1.10 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20     *
21 root 1.11 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.7 */
23 elmex 1.1
24     /* The onion room generator:
25     Onion rooms are like this:
26    
27     char **map_gen_spiral(int xsize, int ysize, int option);
28    
29    
30     */
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     char **
55     map_gen_spiral (int xsize, int ysize, int option)
56     {
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     /* allocate that array, set it up */
65 root 1.2 char **maze = (char **) calloc (sizeof (char *), xsize);
66    
67     for (i = 0; i < xsize; i++)
68     {
69     maze[i] = (char *) calloc (sizeof (char), ysize);
70     }
71 elmex 1.1
72     /* slightly easier to fill and then cut */
73 root 1.2 for (i = 0; i < xsize; i++)
74     for (j = 0; j < ysize; j++)
75     maze[i][j] = '#';
76    
77     ic = xsize / 2;
78     jc = ysize / 2;
79     SizeX = xsize / 2 - 2;
80     SizeY = ysize / 2 - 2;
81 elmex 1.1
82     /* select random options if necessary */
83 root 1.2 if (option == 0)
84     {
85 root 1.9 option = rndm (MAX_SPIRAL_OPT);
86 root 1.2 }
87 elmex 1.1
88 root 1.2 /* the order in which these are evaluated matters */
89 elmex 1.1
90     /* the following two are mutually exclusive.
91     pick one if they're both set. */
92 root 1.2 if ((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL))
93 elmex 1.1 {
94     /* unset REGULAR_SPIRAL half the time */
95 root 1.8 if (rndm (2) && (option & REGULAR_SPIRAL))
96 elmex 1.1 option -= REGULAR_SPIRAL;
97     else
98     option -= FIT_SPIRAL;
99     }
100    
101 root 1.2 xscale = yscale = MAX_FINE; /* fine spiral */
102    
103 elmex 1.1 /* choose the spiral pitch */
104 root 1.2 if (!(option & FINE_SPIRAL))
105     {
106 root 1.8 float pitch = (rndm (5)) / 10. + 10. / 22.;
107 root 1.2
108     xscale = yscale = pitch;
109     }
110    
111     if ((option & FIT_SPIRAL) && (xsize != ysize))
112     {
113     if (xsize > ysize)
114     xscale *= (float) xsize / (float) ysize;
115     else
116     yscale *= (float) ysize / (float) xsize;
117     }
118    
119     if (option & REGULAR_SPIRAL)
120     {
121     float scale = MIN (xscale, yscale);
122    
123     xscale = yscale = scale;
124     }
125 elmex 1.1
126     /* cut out the spiral */
127 root 1.2 while ((fabs (x) < SizeX) && (fabs (y) < SizeY))
128 elmex 1.1 {
129 root 1.2 x = parm * cos (parm) * xscale;
130     y = parm * sin (parm) * yscale;
131     maze[(int) (ic + x)][(int) (jc + y)] = '\0';
132     parm += 0.01;
133 elmex 1.1 };
134 root 1.2
135     maze[(int) (ic + x + 0.5)][(int) (jc + y + 0.5)] = '<';
136 elmex 1.1
137    
138     /* cut out the center in a 2x2 and place the center and downexit */
139 root 1.2 maze[ic][jc + 1] = '>';
140     maze[ic][jc] = 'C';
141 elmex 1.1
142    
143     return maze;
144     }
145    
146     /* the following function connects disjoint spirals which may
147     result from the symmetrization process. */
148 root 1.2 void
149     connect_spirals (int xsize, int ysize, int sym, char **layout)
150     {
151 elmex 1.1
152 root 1.2 int i, j, ic = xsize / 2, jc = ysize / 2;
153 elmex 1.1
154 root 1.4 if (sym == SYMMETRY_X)
155 root 1.2 {
156     layout[ic][jc] = 0;
157     /* go left from map center */
158     for (i = ic - 1, j = jc; i > 0 && layout[i][j] == '#'; i--)
159     layout[i][j] = 0;
160     /* go right */
161     for (i = ic + 1, j = jc; i < xsize - 1 && layout[i][j] == '#'; i++)
162     layout[i][j] = 0;
163 elmex 1.1 }
164    
165 root 1.4 if (sym == SYMMETRY_Y)
166 root 1.2 {
167    
168     layout[ic][jc] = 0;
169     /* go up */
170     for (i = ic, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
171     layout[i][j] = 0;
172     /* go down */
173     for (i = ic, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
174     layout[i][j] = 0;
175     }
176    
177 root 1.4 if (sym == SYMMETRY_XY)
178 root 1.2 {
179     /* go left from map center */
180     layout[ic][jc / 2] = 0;
181     layout[ic / 2][jc] = 0;
182     layout[ic][jc / 2 + jc] = 0;
183     layout[ic / 2 + ic][jc] = 0;
184     for (i = ic - 1, j = jc / 2; i > 0 && layout[i][j] == '#'; i--)
185     {
186     layout[i][j + jc] = 0;
187     layout[i][j] = 0;
188     }
189     /* go right */
190     for (i = ic + 1, j = jc / 2; i < xsize - 1 && layout[i][j] == '#'; i++)
191     {
192     layout[i][j + jc] = 0;
193     layout[i][j] = 0;
194     }
195     /* go up */
196     for (i = ic / 2, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
197     {
198     layout[i][j] = 0;
199     layout[i + ic][j] = 0;
200     }
201     /* go down */
202     for (i = ic / 2, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
203     {
204     layout[i][j] = 0;
205     layout[i + ic][j] = 0;
206     }
207    
208     }
209 elmex 1.1
210     /* get rid of bad doors. */
211 root 1.2 for (i = 0; i < xsize; i++)
212     for (j = 0; j < ysize; j++)
213     {
214     if (layout[i][j] == 'D')
215     { /* remove bad door. */
216     int si = surround_check (layout, i, j, xsize, ysize);
217    
218     if (si != 3 && si != 12)
219     {
220     layout[i][j] = 0;
221     /* back up and recheck any nearby doors */
222     i = 0;
223     j = 0;
224     }
225     }
226 elmex 1.1 }
227 root 1.2
228 elmex 1.1
229    
230     }