ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/random_map.C
Revision: 1.83
Committed: Wed Nov 16 23:42:02 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.82: +1 -1 lines
Log Message:
copyright update 2016

File Contents

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