ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/random_map.C
(Generate patch)

Comparing deliantra/server/random_maps/random_map.C (file contents):
Revision 1.63 by root, Sat Jul 3 01:49:18 2010 UTC vs.
Revision 1.85 by root, Sat Nov 17 23:40:02 2018 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 9 * 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 * 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 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the Affero GNU General Public License 19 * 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 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 21 * <http://www.gnu.org/licenses/>.
21 * 22 *
22 * The authors can be reached via e-mail to <support@deliantra.net> 23 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 24 */
24 25
25#include <time.h> 26#include <time.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <global.h> 28#include <global.h>
28#include <random_map.h> 29#include <rmg.h>
29#include <rproto.h> 30#include <rproto.h>
30#include <sproto.h> 31#include <sproto.h>
31 32
32#define CEDE coroapi::cede_to_tick () 33#define CEDE coroapi::cede_to_tick ()
33 34
34random_map_params::random_map_params () 35random_map_params::random_map_params ()
35{ 36{
36 hv = newHV (); 37 hv = newHV ();
37} 38}
38 39
40random_map_params::random_map_params (random_map_params *RP)
41{
42 *this = *RP;
43
44 HV *copy = newHV ();
45
46 hv_iterinit (hv);
47
48 // does not work for utf-8 keys
49 while (HE *he = hv_iternext (hv))
50 {
51 STRLEN klen; const char *key = HePV (he, klen);
52 hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he));
53 }
54
55 hv = copy;
56}
57
39random_map_params::random_map_params (HV *hv) 58random_map_params::random_map_params (HV *hv)
40{ 59{
41 this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv); 60 this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv);
42 61
43 assign (wall_name, get_str ("wall_name")); 62 assign (wall_name, get_str ("wall_name"));
44 63
45 xsize = get_iv ("xsize"); 64 xsize = get_iv ("xsize");
46 ysize = get_iv ("ysize"); 65 ysize = get_iv ("ysize");
47 expand2x = get_iv ("expand2x");
48 layoutoptions1 = get_iv ("layoutoptions1"); 66 layoutoptions1 = get_iv ("layoutoptions1");
49 layoutoptions2 = get_iv ("layoutoptions2"); 67 layoutoptions2 = get_iv ("layoutoptions2");
50 layoutoptions3 = get_iv ("layoutoptions3"); 68 layoutoptions3 = get_iv ("layoutoptions3");
51 symmetry = get_iv ("symmetry");
52 difficulty = get_iv ("difficulty"); 69 difficulty = get_iv ("difficulty");
53 difficulty_given = get_iv ("difficulty_given"); 70 difficulty_given = get_iv ("difficulty_given");
54 difficulty_increase = get_nv ("difficulty_increase"); 71 difficulty_increase = get_nv ("difficulty_increase");
55 dungeon_level = get_iv ("dungeon_level"); 72 dungeon_level = get_iv ("dungeon_level");
56 dungeon_depth = get_iv ("dungeon_depth"); 73 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 74 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"); 75 symmetry_used = get_iv ("symmetry_used");
62} 76}
63 77
64random_map_params::~random_map_params () 78random_map_params::~random_map_params ()
65{ 79{
72 set ("xsize" , xsize); 86 set ("xsize" , xsize);
73 set ("ysize" , ysize); 87 set ("ysize" , ysize);
74 set ("layoutoptions1" , layoutoptions1); 88 set ("layoutoptions1" , layoutoptions1);
75 set ("layoutoptions2" , layoutoptions2); 89 set ("layoutoptions2" , layoutoptions2);
76 set ("layoutoptions3" , layoutoptions3); 90 set ("layoutoptions3" , layoutoptions3);
77 set ("symmetry" , symmetry);
78 set ("dungeon_depth" , dungeon_depth); 91 set ("dungeon_depth" , dungeon_depth);
79 set ("orientation" , orientation);
80 set ("random_seed" , (UV)random_seed);
81 set ("difficulty" , difficulty && difficulty_given ? difficulty : 0); 92 set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
82 set ("difficulty_increase", difficulty_increase); 93 set ("difficulty_increase", difficulty_increase);
83 set ("dungeon_level" , dungeon_level); 94 set ("dungeon_level" , dungeon_level);
84 95
85 dynbuf_text buf; 96 dynbuf_text buf;
98 } 109 }
99 110
100 return shstr (buf); 111 return shstr (buf);
101} 112}
102 113
103noinline SV * 114ecb_noinline SV *
104random_map_params::get_sv (const char *option) const 115random_map_params::opt_sv (const char *option) const
105{ 116{
106 SV **he = hv_fetch (hv, option, strlen (option), 0); 117 SV **he = hv_fetch (hv, option, strlen (option), 0);
107 118
108 return he ? *he : 0; 119 return he ? *he : 0;
109} 120}
110 121
111noinline const_utf8_string 122ecb_noinline const_utf8_string
112random_map_params::get_str (const char *option, const_utf8_string fallback) const 123random_map_params::get_str (const char *option, const_utf8_string fallback) const
113{ 124{
114 SV *sv = get_sv (option); 125 SV *sv = opt_sv (option);
115 return sv ? cfSvPVutf8_nolen (sv) : fallback; 126 return sv ? cfSvPVutf8_nolen (sv) : fallback;
116} 127}
117 128
118noinline IV 129ecb_noinline IV
119random_map_params::get_iv (const char *option, IV fallback) const 130random_map_params::get_iv (const char *option, IV fallback) const
120{ 131{
121 SV *sv = get_sv (option); 132 SV *sv = opt_sv (option);
122 return sv ? SvIV (sv) : fallback; 133 return sv ? SvIV (sv) : fallback;
123} 134}
124 135
125noinline UV 136ecb_noinline UV
126random_map_params::get_uv (const char *option, UV fallback) const 137random_map_params::get_uv (const char *option, UV fallback) const
127{ 138{
128 SV *sv = get_sv (option); 139 SV *sv = opt_sv (option);
129 return sv ? SvUV (sv) : fallback; 140 return sv ? SvUV (sv) : fallback;
130} 141}
131 142
132noinline NV 143ecb_noinline NV
133random_map_params::get_nv (const char *option, NV fallback) const 144random_map_params::get_nv (const char *option, NV fallback) const
134{ 145{
135 SV *sv = get_sv (option); 146 SV *sv = opt_sv (option);
136 return sv ? SvNV (sv) : fallback; 147 return sv ? SvNV (sv) : fallback;
137} 148}
138 149
139noinline void 150ecb_noinline void
140random_map_params::set (const char *option, SV *value) const 151random_map_params::set (const char *option, SV *value) const
141{ 152{
142 int len = strlen (option); 153 int len = strlen (option);
143 154
144 if (value) 155 if (value)
145 hv_store (hv, option, len, value, 0); 156 hv_store (hv, option, len, value, 0);
146 else 157 else
147 hv_delete (hv, option, len, G_DISCARD); 158 hv_delete (hv, option, len, G_DISCARD);
148} 159}
149 160
150noinline void 161ecb_noinline void
151random_map_params::set (const char *option, const_utf8_string value) const 162random_map_params::set (const char *option, const_utf8_string value) const
152{ 163{
153 set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); 164 set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0);
154} 165}
155 166
169random_map_params::set (const char *option, NV value) const 180random_map_params::set (const char *option, NV value) const
170{ 181{
171 set (option, newSVnv (value)); 182 set (option, newSVnv (value));
172} 183}
173 184
174void
175random_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
192bool 185bool
193maptile::generate_random_map (random_map_params *RP) 186maptile::generate_random_map (random_map_params *RP)
194{ 187{
195 RP->Xsize = RP->xsize; 188 RP->Xsize = RP->xsize;
196 RP->Ysize = RP->ysize; 189 RP->Ysize = RP->ysize;
197 190
191 max_it (RP->dungeon_level, 1);
192
193 IV expand2x = RP->get_iv ("expand2x");
194 UV random_seed = RP->get_uv ("random_seed");
195
198 /* pick a random seed, or use the one from the input file */ 196 /* pick a random seed, or use the one from the input file */
199 RP->random_seed = RP->random_seed 197 random_seed = random_seed
200 ? RP->random_seed + RP->dungeon_level 198 ? random_seed + RP->dungeon_level
201 : time (0); 199 : server_tick;
202 200
203 // we run "single-threaded" 201 // we run "single-threaded"
204 rmg_rndm.seed (RP->random_seed); 202 rmg_rndm.seed (random_seed);
205 203
206 shstr buf = RP->as_shstr (); 204 shstr buf = RP->as_shstr ();
207 205
208 if (RP->difficulty == 0) 206 if (RP->difficulty == 0)
209 { 207 {
210 RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ 208 RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */
211 209
212 if (RP->difficulty_increase > 0.001) 210 if (RP->difficulty_increase > 0.001f)
213 RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); 211 RP->difficulty = RP->dungeon_level * RP->difficulty_increase;
214 212
215 if (RP->difficulty < 1) 213 if (RP->difficulty < 1)
216 RP->difficulty = 1; 214 RP->difficulty = 1;
217 } 215 }
218 else 216 else
225 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; 223 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
226 224
227 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); 225 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
228 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); 226 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
229 227
228 int symmetry = RP->get_iv ("symmetry", SYMMETRY_NONE);
229
230 if (RP->symmetry == SYMMETRY_RANDOM) 230 if (symmetry == SYMMETRY_RANDOM)
231 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; 231 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
232 else 232 else
233 RP->symmetry_used = RP->symmetry; 233 RP->symmetry_used = symmetry;
234 234
235 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) 235 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
236 RP->Ysize = RP->Ysize / 2 + 1; 236 RP->Ysize = RP->Ysize / 2 + 1;
237 237
238 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) 238 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
239 RP->Xsize = RP->Xsize / 2 + 1; 239 RP->Xsize = RP->Xsize / 2 + 1;
240 240
241 if (RP->expand2x > 0) 241 if (expand2x)
242 { 242 {
243 RP->Xsize /= 2; 243 RP->Xsize /= 2;
244 RP->Ysize /= 2; 244 RP->Ysize /= 2;
245 } 245 }
246 246
258 RP->map_layout_style = LAYOUT_SNAKE; 258 RP->map_layout_style = LAYOUT_SNAKE;
259 else if (strstr (layoutstyle, "squarespiral")) 259 else if (strstr (layoutstyle, "squarespiral"))
260 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; 260 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
261 else if (strstr (layoutstyle, "cave")) 261 else if (strstr (layoutstyle, "cave"))
262 RP->map_layout_style = LAYOUT_CAVE; 262 RP->map_layout_style = LAYOUT_CAVE;
263 else if (strstr (layoutstyle, "castle"))
264 RP->map_layout_style = LAYOUT_CASTLE;
265 else if (strstr (layoutstyle, "multiple"))
266 RP->map_layout_style = LAYOUT_MULTIPLE;
263 else 267 else
264 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ 268 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */
265 269
266 layout maze (RP->Xsize, RP->Ysize); 270 layout maze (RP->Xsize, RP->Ysize);
267 maze.generate (RP); 271 maze.generate (RP);
272
273 if (RP->get_iv ("rotate", 1))
274 maze.rotate (rmg_rndm (4));
275
276 maze.symmetrize (RP->symmetry_used);
277
278 if (expand2x)
279 maze.expand2x ();
280
281#if 0
282 maze.print ();//D
283#endif
268 284
269 /* increment these for the current map */ 285 /* increment these for the current map */
270 ++RP->dungeon_level; 286 ++RP->dungeon_level;
271 287
272 // need to patch RP becasue following code doesn't use the layout object 288 // need to patch RP becasue following code doesn't use the layout object
303 319
304 const char *exitstyle = RP->get_str ("exitstyle", ""); 320 const char *exitstyle = RP->get_str ("exitstyle", "");
305 321
306 /* create exits unless the exitstyle is "none" */ 322 /* create exits unless the exitstyle is "none" */
307 if (strcmp (exitstyle, "none")) 323 if (strcmp (exitstyle, "none"))
308 place_exits (this, maze, exitstyle, RP->orientation, RP); 324 place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP);
309 325
310 CEDE; 326 CEDE;
311 327
312 const char *monsterstyle = RP->get_str ("monsterstyle", ""); 328 const char *monsterstyle = RP->get_str ("monsterstyle", "");
313 329
327 /* create treasure unless the treasurestyle is "none" */ 343 /* create treasure unless the treasurestyle is "none" */
328 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); 344 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP);
329 345
330 CEDE; 346 CEDE;
331 347
332 const char *decorstyle = RP->get_str ("treasurestyle", ""); 348 const char *decorstyle = RP->get_str ("decorstyle", "");
333 349
334 /* create decor unless the decorstyle is "none" */ 350 /* create decor unless the decorstyle is "none" */
335 if (strcmp (decorstyle, "none")) 351 if (strcmp (decorstyle, "none"))
336 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); 352 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
337 353
340 /* generate treasures, etc. */ 356 /* generate treasures, etc. */
341 fix_auto_apply (); 357 fix_auto_apply ();
342 358
343 CEDE; 359 CEDE;
344 360
345 unblock_exits (this, maze, RP); 361 unblock_exits (this, maze);
346 362
347 msg = buf; 363 msg = buf;
348 in_memory = MAP_ACTIVE; 364 state = MAP_INACTIVE; // this is probably a lie, but currently works with ext/map-random.ext
349 365
350 CEDE; 366 CEDE;
351 367
352 return 1; 368 return 1;
353} 369}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines