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.60 by root, Fri Jul 2 03:40:14 2010 UTC vs.
Revision 1.77 by root, Sat Apr 23 04:56:53 2011 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 (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 8 * 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 9 * the terms of the Affero GNU General Public License as published by the
23 */ 23 */
24 24
25#include <time.h> 25#include <time.h>
26#include <stdio.h> 26#include <stdio.h>
27#include <global.h> 27#include <global.h>
28#include <random_map.h> 28#include <rmg.h>
29#include <rproto.h> 29#include <rproto.h>
30#include <sproto.h> 30#include <sproto.h>
31 31
32#define CEDE coroapi::cede_to_tick () 32#define CEDE coroapi::cede_to_tick ()
33 33
34random_map_params::random_map_params ()
35{
36 hv = newHV ();
37}
38
39random_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
57random_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
77random_map_params::~random_map_params ()
78{
79 SvREFCNT_dec (hv);
80}
81
82shstr_tmp
83random_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
34noinline SV * 113noinline SV *
35random_map_params::get_sv (const char *option) const 114random_map_params::opt_sv (const char *option) const
36{ 115{
37 SV **he = hv_fetch (hv, option, strlen (option), 0); 116 SV **he = hv_fetch (hv, option, strlen (option), 0);
38 117
39 return he ? *he : 0; 118 return he ? *he : 0;
40} 119}
41 120
42noinline const_utf8_string 121noinline const_utf8_string
43random_map_params::get_str (const char *option, const_utf8_string fallback) const 122random_map_params::get_str (const char *option, const_utf8_string fallback) const
44{ 123{
45 SV *sv = get_sv (option); 124 SV *sv = opt_sv (option);
46 return sv ? cfSvPVutf8_nolen (sv) : fallback; 125 return sv ? cfSvPVutf8_nolen (sv) : fallback;
47} 126}
48 127
49noinline IV 128noinline IV
50random_map_params::get_iv (const char *option, IV fallback) const 129random_map_params::get_iv (const char *option, IV fallback) const
51{ 130{
52 SV *sv = get_sv (option); 131 SV *sv = opt_sv (option);
53 return sv ? SvIV (sv) : fallback; 132 return sv ? SvIV (sv) : fallback;
54} 133}
55 134
56noinline UV 135noinline UV
57random_map_params::get_uv (const char *option, UV fallback) const 136random_map_params::get_uv (const char *option, UV fallback) const
58{ 137{
59 SV *sv = get_sv (option); 138 SV *sv = opt_sv (option);
60 return sv ? SvUV (sv) : fallback; 139 return sv ? SvUV (sv) : fallback;
61} 140}
62 141
63noinline NV 142noinline NV
64random_map_params::get_nv (const char *option, NV fallback) const 143random_map_params::get_nv (const char *option, NV fallback) const
65{ 144{
66 SV *sv = get_sv (option); 145 SV *sv = opt_sv (option);
67 return sv ? SvNV (sv) : fallback; 146 return sv ? SvNV (sv) : fallback;
68} 147}
69 148
70noinline void 149noinline void
71random_map_params::set (const char *option, SV *value) const 150random_map_params::set (const char *option, SV *value) const
100random_map_params::set (const char *option, NV value) const 179random_map_params::set (const char *option, NV value) const
101{ 180{
102 set (option, newSVnv (value)); 181 set (option, newSVnv (value));
103} 182}
104 183
105void
106random_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
123shstr_tmp
124random_map_params::as_shstr () const
125{
126 set ("xsize" , xsize);
127 set ("ysize" , ysize);
128 set ("monsterstyle" , monsterstyle);
129 set ("exit_on_final_map" , exit_on_final_map);
130 set ("layoutstyle" , layoutstyle);
131 set ("doorstyle" , doorstyle);
132 set ("final_map" , final_map);
133 set ("this_map" , this_map);
134 set ("expand2x" , expand2x);
135 set ("layoutoptions1" , layoutoptions1);
136 set ("layoutoptions2" , layoutoptions2);
137 set ("layoutoptions3" , layoutoptions3);
138 set ("symmetry" , symmetry);
139 set ("dungeon_depth" , dungeon_depth);
140 set ("orientation" , orientation);
141 set ("origin_x" , origin_x);
142 set ("origin_y" , origin_y);
143 set ("random_seed" , (UV)random_seed);
144 set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
145 set ("difficulty_increase", difficulty_increase);
146 set ("dungeon_level" , dungeon_level);
147
148 dynbuf_text buf;
149 hv_iterinit (hv);
150
151 // does not work for utf-8 keys
152 while (HE *he = hv_iternext (hv))
153 {
154 STRLEN klen; const char *key = HePV (he, klen);
155 STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen);
156
157 buf.fadd (key, klen);
158 buf << ' ';
159 buf.fadd (value, vlen);
160 buf << '\n';
161 }
162
163 return shstr (buf);
164}
165
166random_map_params::~random_map_params ()
167{
168 SvREFCNT_dec (hv);
169}
170
171bool 184bool
172maptile::generate_random_map (random_map_params *RP) 185maptile::generate_random_map (random_map_params *RP)
173{ 186{
174 RP->Xsize = RP->xsize; 187 RP->Xsize = RP->xsize;
175 RP->Ysize = RP->ysize; 188 RP->Ysize = RP->ysize;
176 189
190 max_it (RP->dungeon_level, 1);
191
192 IV expand2x = RP->get_iv ("expand2x");
193 UV random_seed = RP->get_uv ("random_seed");
194
177 /* pick a random seed, or use the one from the input file */ 195 /* pick a random seed, or use the one from the input file */
178 RP->random_seed = RP->random_seed 196 random_seed = random_seed
179 ? RP->random_seed + RP->dungeon_level 197 ? random_seed + RP->dungeon_level
180 : time (0); 198 : pticks;
181 199
182 // we run "single-threaded" 200 // we run "single-threaded"
183 rmg_rndm.seed (RP->random_seed); 201 rmg_rndm.seed (random_seed);
184 202
185 shstr buf = RP->as_shstr (); 203 shstr buf = RP->as_shstr ();
186 204
187 if (RP->difficulty == 0) 205 if (RP->difficulty == 0)
188 { 206 {
189 RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ 207 RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */
190 208
191 if (RP->difficulty_increase > 0.001) 209 if (RP->difficulty_increase > 0.001f)
192 RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); 210 RP->difficulty = RP->dungeon_level * RP->difficulty_increase;
193 211
194 if (RP->difficulty < 1) 212 if (RP->difficulty < 1)
195 RP->difficulty = 1; 213 RP->difficulty = 1;
196 } 214 }
197 else 215 else
204 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; 222 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
205 223
206 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); 224 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
207 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); 225 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
208 226
227 int symmetry = RP->get_iv ("symmetry", SYMMETRY_NONE);
228
209 if (RP->symmetry == SYMMETRY_RANDOM) 229 if (symmetry == SYMMETRY_RANDOM)
210 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; 230 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
211 else 231 else
212 RP->symmetry_used = RP->symmetry; 232 RP->symmetry_used = symmetry;
213 233
214 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) 234 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
215 RP->Ysize = RP->Ysize / 2 + 1; 235 RP->Ysize = RP->Ysize / 2 + 1;
216 236
217 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) 237 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
218 RP->Xsize = RP->Xsize / 2 + 1; 238 RP->Xsize = RP->Xsize / 2 + 1;
219 239
220 if (RP->expand2x > 0) 240 if (expand2x)
221 { 241 {
222 RP->Xsize /= 2; 242 RP->Xsize /= 2;
223 RP->Ysize /= 2; 243 RP->Ysize /= 2;
224 } 244 }
225 245
246 const char *layoutstyle = RP->get_str ("layoutstyle", "");
247
226 if (strstr (RP->layoutstyle, "onion")) 248 if (strstr (layoutstyle, "onion"))
227 RP->map_layout_style = LAYOUT_ONION; 249 RP->map_layout_style = LAYOUT_ONION;
228 else if (strstr (RP->layoutstyle, "maze")) 250 else if (strstr (layoutstyle, "maze"))
229 RP->map_layout_style = LAYOUT_MAZE; 251 RP->map_layout_style = LAYOUT_MAZE;
230 else if (strstr (RP->layoutstyle, "spiral")) 252 else if (strstr (layoutstyle, "spiral"))
231 RP->map_layout_style = LAYOUT_SPIRAL; 253 RP->map_layout_style = LAYOUT_SPIRAL;
232 else if (strstr (RP->layoutstyle, "rogue")) 254 else if (strstr (layoutstyle, "rogue"))
233 RP->map_layout_style = LAYOUT_ROGUELIKE; 255 RP->map_layout_style = LAYOUT_ROGUELIKE;
234 else if (strstr (RP->layoutstyle, "snake")) 256 else if (strstr (layoutstyle, "snake"))
235 RP->map_layout_style = LAYOUT_SNAKE; 257 RP->map_layout_style = LAYOUT_SNAKE;
236 else if (strstr (RP->layoutstyle, "squarespiral")) 258 else if (strstr (layoutstyle, "squarespiral"))
237 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; 259 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
238 else if (strstr (RP->layoutstyle, "cave")) 260 else if (strstr (layoutstyle, "cave"))
239 RP->map_layout_style = LAYOUT_CAVE; 261 RP->map_layout_style = LAYOUT_CAVE;
262 else if (strstr (layoutstyle, "castle"))
263 RP->map_layout_style = LAYOUT_CASTLE;
264 else if (strstr (layoutstyle, "multiple"))
265 RP->map_layout_style = LAYOUT_MULTIPLE;
240 else 266 else
241 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ 267 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */
242 268
243 Layout layout (RP->Xsize, RP->Ysize); 269 layout maze (RP->Xsize, RP->Ysize);
244 layout.generate (RP); 270 maze.generate (RP);
271
272 if (RP->get_iv ("rotate", 1))
273 maze.rotate (rmg_rndm (4));
274
275 maze.symmetrize (RP->symmetry_used);
276
277 if (expand2x)
278 maze.expand2x ();
279
280#if 0
281 maze.print ();//D
282#endif
245 283
246 /* increment these for the current map */ 284 /* increment these for the current map */
247 ++RP->dungeon_level; 285 ++RP->dungeon_level;
248 286
249 // need to patch RP becasue following code doesn't use the Layout object 287 // need to patch RP becasue following code doesn't use the layout object
250 RP->Xsize = layout.w; 288 RP->Xsize = maze.w;
251 RP->Ysize = layout.h; 289 RP->Ysize = maze.h;
252 290
253 /* allocate the map and set the floor */ 291 /* allocate the map and set the floor */
254 make_map_floor (layout, RP->get_str ("floorstyle", ""), RP); 292 make_map_floor (maze, RP->get_str ("floorstyle", ""), RP);
255 293
256 /* set region */ 294 /* set region */
257 default_region = RP->region; 295 default_region = region::find (RP->get_str ("region", 0));
258 296
259 CEDE; 297 CEDE;
260 298
261 place_specials_in_map (this, layout, RP); 299 place_specials_in_map (this, maze, RP);
262 300
263 CEDE; 301 CEDE;
264 302
265 const char *wallstyle = RP->get_str ("wallstyle", 0); 303 const char *wallstyle = RP->get_str ("wallstyle", "");
266 304
267 /* create walls unless the wallstyle is "none" */ 305 /* create walls unless the wallstyle is "none" */
268 if (strcmp (wallstyle, "none")) 306 if (strcmp (wallstyle, "none"))
269 { 307 {
270 make_map_walls (this, layout, wallstyle, RP->get_str ("miningstyle", ""), RP); 308 make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP);
309
310 const char *doorstyle = RP->get_str ("doorstyle", "");
271 311
272 /* place doors unless doorstyle or wallstyle is "none" */ 312 /* place doors unless doorstyle or wallstyle is "none" */
273 if (strcmp (RP->doorstyle, "none")) 313 if (strcmp (doorstyle, "none"))
274 put_doors (this, layout, RP->doorstyle, RP); 314 put_doors (this, maze, doorstyle, RP);
275 } 315 }
276 316
277 CEDE; 317 CEDE;
278 318
279 const char *exitstyle = RP->get_str ("exitstyle", ""); 319 const char *exitstyle = RP->get_str ("exitstyle", "");
280 320
281 /* create exits unless the exitstyle is "none" */ 321 /* create exits unless the exitstyle is "none" */
282 if (strcmp (exitstyle, "none")) 322 if (strcmp (exitstyle, "none"))
283 place_exits (this, layout, exitstyle, RP->orientation, RP); 323 place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP);
284 324
285 CEDE; 325 CEDE;
326
327 const char *monsterstyle = RP->get_str ("monsterstyle", "");
286 328
287 /* create monsters unless the monsterstyle is "none" */ 329 /* create monsters unless the monsterstyle is "none" */
288 if (strcmp (RP->monsterstyle, "none")) 330 if (strcmp (monsterstyle, "none"))
289 place_monsters (this, RP->monsterstyle, RP->difficulty, RP); 331 place_monsters (this, monsterstyle, RP->difficulty, RP);
290 332
291 CEDE; 333 CEDE;
292 334
293 /* treasures needs to have a proper difficulty set for the map. */ 335 /* treasures needs to have a proper difficulty set for the map. */
294 difficulty = estimate_difficulty (); 336 difficulty = estimate_difficulty ();
296 CEDE; 338 CEDE;
297 339
298 const char *treasurestyle = RP->get_str ("treasurestyle", ""); 340 const char *treasurestyle = RP->get_str ("treasurestyle", "");
299 341
300 /* create treasure unless the treasurestyle is "none" */ 342 /* create treasure unless the treasurestyle is "none" */
301 place_treasure (this, layout, treasurestyle, RP->get_iv ("treasureoptions"), RP); 343 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP);
302 344
303 CEDE; 345 CEDE;
304 346
305 const char *decorstyle = RP->get_str ("treasurestyle", ""); 347 const char *decorstyle = RP->get_str ("decorstyle", "");
306 348
307 /* create decor unless the decorstyle is "none" */ 349 /* create decor unless the decorstyle is "none" */
308 if (strcmp (decorstyle, "none")) 350 if (strcmp (decorstyle, "none"))
309 put_decor (this, layout, decorstyle, RP->get_iv ("decoroptions"), RP); 351 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
310 352
311 CEDE; 353 CEDE;
312 354
313 /* generate treasures, etc. */ 355 /* generate treasures, etc. */
314 fix_auto_apply (); 356 fix_auto_apply ();
315 357
316 CEDE; 358 CEDE;
317 359
318 unblock_exits (this, layout, RP); 360 unblock_exits (this, maze);
319 361
320 msg = buf; 362 msg = buf;
321 in_memory = MAP_ACTIVE; 363 in_memory = MAP_ACTIVE;
322 364
323 CEDE; 365 CEDE;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines