ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/random_maps/random_map.C
Revision: 1.63
Committed: Sat Jul 3 01:49:18 2010 UTC (16 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.62: +70 -40 lines
Log Message:
isolation remover would try to tunnel along the border, but make_tunnel refused, leading to isolated areas

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.63 random_map_params::random_map_params ()
35     {
36     hv = newHV ();
37     }
38    
39     random_map_params::random_map_params (HV *hv)
40     {
41     this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv);
42    
43     assign (wall_name, get_str ("wall_name"));
44    
45     xsize = get_iv ("xsize");
46     ysize = get_iv ("ysize");
47     expand2x = get_iv ("expand2x");
48     layoutoptions1 = get_iv ("layoutoptions1");
49     layoutoptions2 = get_iv ("layoutoptions2");
50     layoutoptions3 = get_iv ("layoutoptions3");
51     symmetry = get_iv ("symmetry");
52     difficulty = get_iv ("difficulty");
53     difficulty_given = get_iv ("difficulty_given");
54     difficulty_increase = get_nv ("difficulty_increase");
55     dungeon_level = get_iv ("dungeon_level");
56     dungeon_depth = get_iv ("dungeon_depth");
57     orientation = get_iv ("orientation");
58     random_seed = get_uv ("random_seed");
59     total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy
60     map_layout_style = get_iv ("map_layout_style");
61     symmetry_used = get_iv ("symmetry_used");
62     }
63    
64     random_map_params::~random_map_params ()
65     {
66     SvREFCNT_dec (hv);
67     }
68    
69     shstr_tmp
70     random_map_params::as_shstr () const
71     {
72     set ("xsize" , xsize);
73     set ("ysize" , ysize);
74     set ("layoutoptions1" , layoutoptions1);
75     set ("layoutoptions2" , layoutoptions2);
76     set ("layoutoptions3" , layoutoptions3);
77     set ("symmetry" , symmetry);
78     set ("dungeon_depth" , dungeon_depth);
79     set ("orientation" , orientation);
80     set ("random_seed" , (UV)random_seed);
81     set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
82     set ("difficulty_increase", difficulty_increase);
83     set ("dungeon_level" , dungeon_level);
84    
85     dynbuf_text buf;
86     hv_iterinit (hv);
87    
88     // does not work for utf-8 keys
89     while (HE *he = hv_iternext (hv))
90     {
91     STRLEN klen; const char *key = HePV (he, klen);
92     STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen);
93    
94     buf.fadd (key, klen);
95     buf << ' ';
96     buf.fadd (value, vlen);
97     buf << '\n';
98     }
99    
100     return shstr (buf);
101     }
102    
103 root 1.55 noinline SV *
104     random_map_params::get_sv (const char *option) const
105 root 1.51 {
106     SV **he = hv_fetch (hv, option, strlen (option), 0);
107    
108 root 1.55 return he ? *he : 0;
109 root 1.51 }
110    
111 root 1.55 noinline const_utf8_string
112     random_map_params::get_str (const char *option, const_utf8_string fallback) const
113     {
114     SV *sv = get_sv (option);
115     return sv ? cfSvPVutf8_nolen (sv) : fallback;
116     }
117    
118     noinline IV
119 root 1.51 random_map_params::get_iv (const char *option, IV fallback) const
120     {
121 root 1.55 SV *sv = get_sv (option);
122     return sv ? SvIV (sv) : fallback;
123     }
124    
125     noinline UV
126     random_map_params::get_uv (const char *option, UV fallback) const
127     {
128     SV *sv = get_sv (option);
129     return sv ? SvUV (sv) : fallback;
130     }
131    
132     noinline NV
133     random_map_params::get_nv (const char *option, NV fallback) const
134     {
135     SV *sv = get_sv (option);
136     return sv ? SvNV (sv) : fallback;
137     }
138    
139     noinline void
140     random_map_params::set (const char *option, SV *value) const
141     {
142     int len = strlen (option);
143    
144     if (value)
145     hv_store (hv, option, len, value, 0);
146     else
147     hv_delete (hv, option, len, G_DISCARD);
148     }
149    
150     noinline void
151     random_map_params::set (const char *option, const_utf8_string value) const
152     {
153     set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0);
154     }
155    
156     void
157     random_map_params::set (const char *option, IV value) const
158     {
159     set (option, newSViv (value));
160     }
161    
162     void
163     random_map_params::set (const char *option, UV value) const
164     {
165     set (option, newSVuv (value));
166     }
167    
168     void
169     random_map_params::set (const char *option, NV value) const
170     {
171     set (option, newSVnv (value));
172     }
173    
174     void
175     random_map_params::hv_clone ()
176     {
177     HV *copy = newHV ();
178    
179     hv_iterinit (hv);
180    
181     // does not work for utf-8 keys
182     while (HE *he = hv_iternext (hv))
183     {
184     STRLEN klen; const char *key = HePV (he, klen);
185     hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he));
186     }
187    
188     SvREFCNT_dec (hv);
189     hv = copy;
190     }
191    
192 root 1.43 bool
193     maptile::generate_random_map (random_map_params *RP)
194     {
195     RP->Xsize = RP->xsize;
196     RP->Ysize = RP->ysize;
197    
198     /* pick a random seed, or use the one from the input file */
199     RP->random_seed = RP->random_seed
200     ? RP->random_seed + RP->dungeon_level
201     : time (0);
202    
203     // we run "single-threaded"
204     rmg_rndm.seed (RP->random_seed);
205    
206 root 1.55 shstr buf = RP->as_shstr ();
207 root 1.43
208     if (RP->difficulty == 0)
209     {
210     RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */
211    
212     if (RP->difficulty_increase > 0.001)
213     RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase);
214    
215     if (RP->difficulty < 1)
216     RP->difficulty = 1;
217     }
218     else
219     RP->difficulty_given = 1;
220    
221     if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
222     RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
223    
224     if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
225     RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
226    
227 root 1.58 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
228     min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
229    
230 root 1.43 if (RP->symmetry == SYMMETRY_RANDOM)
231     RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
232     else
233     RP->symmetry_used = RP->symmetry;
234    
235     if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
236     RP->Ysize = RP->Ysize / 2 + 1;
237    
238     if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
239     RP->Xsize = RP->Xsize / 2 + 1;
240    
241     if (RP->expand2x > 0)
242     {
243     RP->Xsize /= 2;
244     RP->Ysize /= 2;
245     }
246    
247 root 1.62 const char *layoutstyle = RP->get_str ("layoutstyle", "");
248    
249     if (strstr (layoutstyle, "onion"))
250 root 1.43 RP->map_layout_style = LAYOUT_ONION;
251 root 1.62 else if (strstr (layoutstyle, "maze"))
252 root 1.43 RP->map_layout_style = LAYOUT_MAZE;
253 root 1.62 else if (strstr (layoutstyle, "spiral"))
254 root 1.43 RP->map_layout_style = LAYOUT_SPIRAL;
255 root 1.62 else if (strstr (layoutstyle, "rogue"))
256 root 1.43 RP->map_layout_style = LAYOUT_ROGUELIKE;
257 root 1.62 else if (strstr (layoutstyle, "snake"))
258 root 1.43 RP->map_layout_style = LAYOUT_SNAKE;
259 root 1.62 else if (strstr (layoutstyle, "squarespiral"))
260 root 1.43 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
261 root 1.62 else if (strstr (layoutstyle, "cave"))
262 root 1.57 RP->map_layout_style = LAYOUT_CAVE;
263 root 1.43 else
264 root 1.59 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */
265 root 1.43
266 root 1.61 layout maze (RP->Xsize, RP->Ysize);
267     maze.generate (RP);
268 root 1.43
269     /* increment these for the current map */
270     ++RP->dungeon_level;
271    
272 root 1.61 // need to patch RP becasue following code doesn't use the layout object
273     RP->Xsize = maze.w;
274     RP->Ysize = maze.h;
275 root 1.43
276     /* allocate the map and set the floor */
277 root 1.61 make_map_floor (maze, RP->get_str ("floorstyle", ""), RP);
278 root 1.43
279     /* set region */
280 root 1.63 default_region = region::find (RP->get_str ("region", 0));
281 root 1.43
282     CEDE;
283    
284 root 1.61 place_specials_in_map (this, maze, RP);
285 root 1.43
286     CEDE;
287    
288 root 1.62 const char *wallstyle = RP->get_str ("wallstyle", "");
289 root 1.51
290 root 1.43 /* create walls unless the wallstyle is "none" */
291 root 1.51 if (strcmp (wallstyle, "none"))
292 root 1.43 {
293 root 1.61 make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP);
294 root 1.43
295 root 1.62 const char *doorstyle = RP->get_str ("doorstyle", "");
296    
297 root 1.43 /* place doors unless doorstyle or wallstyle is "none" */
298 root 1.62 if (strcmp (doorstyle, "none"))
299     put_doors (this, maze, doorstyle, RP);
300 root 1.43 }
301    
302     CEDE;
303    
304 root 1.51 const char *exitstyle = RP->get_str ("exitstyle", "");
305    
306 root 1.43 /* create exits unless the exitstyle is "none" */
307 root 1.51 if (strcmp (exitstyle, "none"))
308 root 1.61 place_exits (this, maze, exitstyle, RP->orientation, RP);
309 root 1.43
310     CEDE;
311    
312 root 1.62 const char *monsterstyle = RP->get_str ("monsterstyle", "");
313    
314 root 1.43 /* create monsters unless the monsterstyle is "none" */
315 root 1.62 if (strcmp (monsterstyle, "none"))
316     place_monsters (this, monsterstyle, RP->difficulty, RP);
317 root 1.43
318     CEDE;
319    
320     /* treasures needs to have a proper difficulty set for the map. */
321     difficulty = estimate_difficulty ();
322    
323     CEDE;
324    
325 root 1.55 const char *treasurestyle = RP->get_str ("treasurestyle", "");
326    
327 root 1.43 /* create treasure unless the treasurestyle is "none" */
328 root 1.61 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP);
329 root 1.43
330     CEDE;
331    
332 root 1.55 const char *decorstyle = RP->get_str ("treasurestyle", "");
333    
334 root 1.43 /* create decor unless the decorstyle is "none" */
335 root 1.55 if (strcmp (decorstyle, "none"))
336 root 1.61 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
337 root 1.43
338     CEDE;
339    
340     /* generate treasures, etc. */
341     fix_auto_apply ();
342    
343     CEDE;
344    
345 root 1.61 unblock_exits (this, maze, RP);
346 root 1.43
347 root 1.51 msg = buf;
348 root 1.43 in_memory = MAP_ACTIVE;
349    
350     CEDE;
351    
352     return 1;
353     }
354