ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/random_maps/random_map.C
Revision: 1.69
Committed: Sat Jul 3 13:14:35 2010 UTC (16 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.68: +2 -0 lines
Log Message:
*** empty log message ***

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