ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/random_maps/random_map.C
Revision: 1.62
Committed: Sat Jul 3 01:12:45 2010 UTC (16 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.61: +18 -21 lines
Log Message:
things might not be simpler than they appear

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.31 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.20 *
4 root 1.47 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.46 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 Frank Tore Johansen
7 pippijn 1.20 *
8 root 1.42 * 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.20 *
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.30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 pippijn 1.20 * GNU General Public License for more details.
17     *
18 root 1.42 * 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.30 *
22 root 1.31 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.20 */
24 elmex 1.1
25     #include <time.h>
26     #include <stdio.h>
27     #include <global.h>
28     #include <random_map.h>
29     #include <rproto.h>
30     #include <sproto.h>
31    
32 root 1.39 #define CEDE coroapi::cede_to_tick ()
33 root 1.22
34 root 1.55 noinline SV *
35     random_map_params::get_sv (const char *option) const
36 root 1.51 {
37     SV **he = hv_fetch (hv, option, strlen (option), 0);
38    
39 root 1.55 return he ? *he : 0;
40 root 1.51 }
41    
42 root 1.55 noinline const_utf8_string
43     random_map_params::get_str (const char *option, const_utf8_string fallback) const
44     {
45     SV *sv = get_sv (option);
46     return sv ? cfSvPVutf8_nolen (sv) : fallback;
47     }
48    
49     noinline IV
50 root 1.51 random_map_params::get_iv (const char *option, IV fallback) const
51     {
52 root 1.55 SV *sv = get_sv (option);
53     return sv ? SvIV (sv) : fallback;
54     }
55    
56     noinline UV
57     random_map_params::get_uv (const char *option, UV fallback) const
58     {
59     SV *sv = get_sv (option);
60     return sv ? SvUV (sv) : fallback;
61     }
62    
63     noinline NV
64     random_map_params::get_nv (const char *option, NV fallback) const
65     {
66     SV *sv = get_sv (option);
67     return sv ? SvNV (sv) : fallback;
68     }
69    
70     noinline void
71     random_map_params::set (const char *option, SV *value) const
72     {
73     int len = strlen (option);
74    
75     if (value)
76     hv_store (hv, option, len, value, 0);
77     else
78     hv_delete (hv, option, len, G_DISCARD);
79     }
80    
81     noinline void
82     random_map_params::set (const char *option, const_utf8_string value) const
83     {
84     set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0);
85     }
86    
87     void
88     random_map_params::set (const char *option, IV value) const
89     {
90     set (option, newSViv (value));
91     }
92    
93     void
94     random_map_params::set (const char *option, UV value) const
95     {
96     set (option, newSVuv (value));
97     }
98    
99     void
100     random_map_params::set (const char *option, NV value) const
101     {
102     set (option, newSVnv (value));
103     }
104    
105     void
106     random_map_params::hv_clone ()
107     {
108     HV *copy = newHV ();
109    
110     hv_iterinit (hv);
111    
112     // does not work for utf-8 keys
113     while (HE *he = hv_iternext (hv))
114     {
115     STRLEN klen; const char *key = HePV (he, klen);
116     hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he));
117     }
118    
119     SvREFCNT_dec (hv);
120     hv = copy;
121     }
122    
123     shstr_tmp
124     random_map_params::as_shstr () const
125     {
126     set ("xsize" , xsize);
127     set ("ysize" , ysize);
128     set ("layoutoptions1" , layoutoptions1);
129     set ("layoutoptions2" , layoutoptions2);
130     set ("layoutoptions3" , layoutoptions3);
131     set ("symmetry" , symmetry);
132     set ("dungeon_depth" , dungeon_depth);
133     set ("orientation" , orientation);
134     set ("random_seed" , (UV)random_seed);
135     set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
136     set ("difficulty_increase", difficulty_increase);
137     set ("dungeon_level" , dungeon_level);
138    
139     dynbuf_text buf;
140     hv_iterinit (hv);
141    
142     // does not work for utf-8 keys
143     while (HE *he = hv_iternext (hv))
144     {
145     STRLEN klen; const char *key = HePV (he, klen);
146     STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen);
147    
148     buf.fadd (key, klen);
149     buf << ' ';
150     buf.fadd (value, vlen);
151     buf << '\n';
152     }
153 root 1.51
154 root 1.55 return shstr (buf);
155 root 1.51 }
156    
157     random_map_params::~random_map_params ()
158     {
159     SvREFCNT_dec (hv);
160     }
161    
162 root 1.43 bool
163     maptile::generate_random_map (random_map_params *RP)
164     {
165     RP->Xsize = RP->xsize;
166     RP->Ysize = RP->ysize;
167    
168     /* pick a random seed, or use the one from the input file */
169     RP->random_seed = RP->random_seed
170     ? RP->random_seed + RP->dungeon_level
171     : time (0);
172    
173     // we run "single-threaded"
174     rmg_rndm.seed (RP->random_seed);
175    
176 root 1.55 shstr buf = RP->as_shstr ();
177 root 1.43
178     if (RP->difficulty == 0)
179     {
180     RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */
181    
182     if (RP->difficulty_increase > 0.001)
183     RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase);
184    
185     if (RP->difficulty < 1)
186     RP->difficulty = 1;
187     }
188     else
189     RP->difficulty_given = 1;
190    
191     if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
192     RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
193    
194     if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
195     RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
196    
197 root 1.58 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
198     min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
199    
200 root 1.43 if (RP->symmetry == SYMMETRY_RANDOM)
201     RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
202     else
203     RP->symmetry_used = RP->symmetry;
204    
205     if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
206     RP->Ysize = RP->Ysize / 2 + 1;
207    
208     if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
209     RP->Xsize = RP->Xsize / 2 + 1;
210    
211     if (RP->expand2x > 0)
212     {
213     RP->Xsize /= 2;
214     RP->Ysize /= 2;
215     }
216    
217 root 1.62 const char *layoutstyle = RP->get_str ("layoutstyle", "");
218    
219     if (strstr (layoutstyle, "onion"))
220 root 1.43 RP->map_layout_style = LAYOUT_ONION;
221 root 1.62 else if (strstr (layoutstyle, "maze"))
222 root 1.43 RP->map_layout_style = LAYOUT_MAZE;
223 root 1.62 else if (strstr (layoutstyle, "spiral"))
224 root 1.43 RP->map_layout_style = LAYOUT_SPIRAL;
225 root 1.62 else if (strstr (layoutstyle, "rogue"))
226 root 1.43 RP->map_layout_style = LAYOUT_ROGUELIKE;
227 root 1.62 else if (strstr (layoutstyle, "snake"))
228 root 1.43 RP->map_layout_style = LAYOUT_SNAKE;
229 root 1.62 else if (strstr (layoutstyle, "squarespiral"))
230 root 1.43 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
231 root 1.62 else if (strstr (layoutstyle, "cave"))
232 root 1.57 RP->map_layout_style = LAYOUT_CAVE;
233 root 1.43 else
234 root 1.59 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */
235 root 1.43
236 root 1.61 layout maze (RP->Xsize, RP->Ysize);
237     maze.generate (RP);
238 root 1.43
239     /* increment these for the current map */
240     ++RP->dungeon_level;
241    
242 root 1.61 // need to patch RP becasue following code doesn't use the layout object
243     RP->Xsize = maze.w;
244     RP->Ysize = maze.h;
245 root 1.43
246     /* allocate the map and set the floor */
247 root 1.61 make_map_floor (maze, RP->get_str ("floorstyle", ""), RP);
248 root 1.43
249     /* set region */
250     default_region = RP->region;
251    
252     CEDE;
253    
254 root 1.61 place_specials_in_map (this, maze, RP);
255 root 1.43
256     CEDE;
257    
258 root 1.62 const char *wallstyle = RP->get_str ("wallstyle", "");
259 root 1.51
260 root 1.43 /* create walls unless the wallstyle is "none" */
261 root 1.51 if (strcmp (wallstyle, "none"))
262 root 1.43 {
263 root 1.61 make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP);
264 root 1.43
265 root 1.62 const char *doorstyle = RP->get_str ("doorstyle", "");
266    
267 root 1.43 /* place doors unless doorstyle or wallstyle is "none" */
268 root 1.62 if (strcmp (doorstyle, "none"))
269     put_doors (this, maze, doorstyle, RP);
270 root 1.43 }
271    
272     CEDE;
273    
274 root 1.51 const char *exitstyle = RP->get_str ("exitstyle", "");
275    
276 root 1.43 /* create exits unless the exitstyle is "none" */
277 root 1.51 if (strcmp (exitstyle, "none"))
278 root 1.61 place_exits (this, maze, exitstyle, RP->orientation, RP);
279 root 1.43
280     CEDE;
281    
282 root 1.62 const char *monsterstyle = RP->get_str ("monsterstyle", "");
283    
284 root 1.43 /* create monsters unless the monsterstyle is "none" */
285 root 1.62 if (strcmp (monsterstyle, "none"))
286     place_monsters (this, monsterstyle, RP->difficulty, RP);
287 root 1.43
288     CEDE;
289    
290     /* treasures needs to have a proper difficulty set for the map. */
291     difficulty = estimate_difficulty ();
292    
293     CEDE;
294    
295 root 1.55 const char *treasurestyle = RP->get_str ("treasurestyle", "");
296    
297 root 1.43 /* create treasure unless the treasurestyle is "none" */
298 root 1.61 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP);
299 root 1.43
300     CEDE;
301    
302 root 1.55 const char *decorstyle = RP->get_str ("treasurestyle", "");
303    
304 root 1.43 /* create decor unless the decorstyle is "none" */
305 root 1.55 if (strcmp (decorstyle, "none"))
306 root 1.61 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
307 root 1.43
308     CEDE;
309    
310     /* generate treasures, etc. */
311     fix_auto_apply ();
312    
313     CEDE;
314    
315 root 1.61 unblock_exits (this, maze, RP);
316 root 1.43
317 root 1.51 msg = buf;
318 root 1.43 in_memory = MAP_ACTIVE;
319    
320     CEDE;
321    
322     return 1;
323     }
324