ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_spiral.C
Revision: 1.20
Committed: Wed Jun 30 20:51:02 2010 UTC (13 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +1 -1 lines
Log Message:
implement isolation remover, moved layout functions to their own file

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.19 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.18 * Copyright (©) 1994 Mark Wedel
6     * Copyright (©) 1992 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 root 1.17 #include <rproto.h>
34 elmex 1.1
35 root 1.2 #define RANDOM_OPTIONS 0 /* Pick random options below */
36     #define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly */
37     #define FINE_SPIRAL 2 /* uses the min. separation: most coiling */
38     #define FIT_SPIRAL 4 /* scale to a rectangular region, not square */
39     #define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */
40 elmex 1.1
41     #define MINDIST 3
42    
43     #define MAX_FINE .454545
44    
45 root 1.2 extern int surround_check (char **maze, int i, int j, int xsize, int ysize);
46    
47 root 1.13 void
48 root 1.14 map_gen_spiral (Layout maze, int option)
49 root 1.2 {
50     int i, j;
51     float parm = 0;
52     float x = 0, y = 0;
53     int ic, jc;
54     float SizeX, SizeY;
55     float xscale, yscale;
56 elmex 1.1
57 root 1.13 /* slightly easier to fill and then cut */
58 root 1.20 maze->fill ('#');
59 elmex 1.1
60 root 1.13 int xsize = maze->w;
61     int ysize = maze->h;
62 root 1.2
63     ic = xsize / 2;
64     jc = ysize / 2;
65     SizeX = xsize / 2 - 2;
66     SizeY = ysize / 2 - 2;
67 elmex 1.1
68     /* select random options if necessary */
69 root 1.2 if (option == 0)
70 root 1.15 option = rmg_rndm (MAX_SPIRAL_OPT);
71 elmex 1.1
72 root 1.2 /* the order in which these are evaluated matters */
73 elmex 1.1
74     /* the following two are mutually exclusive.
75     pick one if they're both set. */
76 root 1.2 if ((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL))
77 elmex 1.1 {
78     /* unset REGULAR_SPIRAL half the time */
79 root 1.15 if (rmg_rndm (2) && (option & REGULAR_SPIRAL))
80 elmex 1.1 option -= REGULAR_SPIRAL;
81     else
82     option -= FIT_SPIRAL;
83     }
84    
85 root 1.2 xscale = yscale = MAX_FINE; /* fine spiral */
86    
87 elmex 1.1 /* choose the spiral pitch */
88 root 1.2 if (!(option & FINE_SPIRAL))
89     {
90 root 1.15 float pitch = (rmg_rndm (5)) / 10. + 10. / 22.;
91 root 1.2
92     xscale = yscale = pitch;
93     }
94    
95     if ((option & FIT_SPIRAL) && (xsize != ysize))
96     {
97     if (xsize > ysize)
98     xscale *= (float) xsize / (float) ysize;
99     else
100     yscale *= (float) ysize / (float) xsize;
101     }
102    
103     if (option & REGULAR_SPIRAL)
104     {
105 root 1.17 float scale = min (xscale, yscale);
106 root 1.2
107     xscale = yscale = scale;
108     }
109 elmex 1.1
110     /* cut out the spiral */
111 root 1.2 while ((fabs (x) < SizeX) && (fabs (y) < SizeY))
112 elmex 1.1 {
113 root 1.2 x = parm * cos (parm) * xscale;
114     y = parm * sin (parm) * yscale;
115 root 1.12 maze[int (ic + x)][int (jc + y)] = '\0';
116 root 1.2 parm += 0.01;
117 root 1.13 }
118 root 1.2
119 root 1.12 maze[int (ic + x + 0.5)][int (jc + y + 0.5)] = '<';
120 elmex 1.1
121     /* cut out the center in a 2x2 and place the center and downexit */
122 root 1.2 maze[ic][jc + 1] = '>';
123     maze[ic][jc] = 'C';
124 elmex 1.1 }
125    
126     /* the following function connects disjoint spirals which may
127     result from the symmetrization process. */
128 root 1.2 void
129     connect_spirals (int xsize, int ysize, int sym, char **layout)
130     {
131     int i, j, ic = xsize / 2, jc = ysize / 2;
132 elmex 1.1
133 root 1.4 if (sym == SYMMETRY_X)
134 root 1.2 {
135     layout[ic][jc] = 0;
136 root 1.14
137 root 1.2 /* go left from map center */
138     for (i = ic - 1, j = jc; i > 0 && layout[i][j] == '#'; i--)
139     layout[i][j] = 0;
140 root 1.14
141 root 1.2 /* go right */
142     for (i = ic + 1, j = jc; i < xsize - 1 && layout[i][j] == '#'; i++)
143     layout[i][j] = 0;
144 elmex 1.1 }
145    
146 root 1.4 if (sym == SYMMETRY_Y)
147 root 1.2 {
148 root 1.14 layout[ic][jc] = 0;
149 root 1.2
150     /* go up */
151     for (i = ic, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
152     layout[i][j] = 0;
153 root 1.14
154 root 1.2 /* go down */
155     for (i = ic, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
156     layout[i][j] = 0;
157     }
158    
159 root 1.4 if (sym == SYMMETRY_XY)
160 root 1.2 {
161     /* go left from map center */
162     layout[ic][jc / 2] = 0;
163     layout[ic / 2][jc] = 0;
164     layout[ic][jc / 2 + jc] = 0;
165     layout[ic / 2 + ic][jc] = 0;
166 root 1.14
167 root 1.2 for (i = ic - 1, j = jc / 2; i > 0 && layout[i][j] == '#'; i--)
168     {
169     layout[i][j + jc] = 0;
170     layout[i][j] = 0;
171     }
172 root 1.14
173 root 1.2 /* go right */
174     for (i = ic + 1, j = jc / 2; i < xsize - 1 && 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 up */
181     for (i = ic / 2, j = jc - 1; j > 0 && layout[i][j] == '#'; j--)
182     {
183     layout[i][j] = 0;
184     layout[i + ic][j] = 0;
185     }
186 root 1.14
187 root 1.2 /* go down */
188     for (i = ic / 2, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++)
189     {
190     layout[i][j] = 0;
191     layout[i + ic][j] = 0;
192     }
193    
194     }
195 elmex 1.1
196     /* get rid of bad doors. */
197 root 1.2 for (i = 0; i < xsize; i++)
198     for (j = 0; j < ysize; j++)
199     {
200     if (layout[i][j] == 'D')
201     { /* remove bad door. */
202     int si = surround_check (layout, i, j, xsize, ysize);
203    
204     if (si != 3 && si != 12)
205     {
206     layout[i][j] = 0;
207     /* back up and recheck any nearby doors */
208     i = 0;
209     j = 0;
210     }
211     }
212 elmex 1.1 }
213     }
214 root 1.14