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.71 by root, Sun Jul 4 22:12:26 2010 UTC

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 expand2x = get_iv ("expand2x");
66 layoutoptions1 = get_iv ("layoutoptions1");
67 layoutoptions2 = get_iv ("layoutoptions2");
68 layoutoptions3 = get_iv ("layoutoptions3");
69 difficulty = get_iv ("difficulty");
70 difficulty_given = get_iv ("difficulty_given");
71 difficulty_increase = get_nv ("difficulty_increase");
72 dungeon_level = get_iv ("dungeon_level");
73 dungeon_depth = get_iv ("dungeon_depth");
74 random_seed = get_uv ("random_seed");
75 total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy
76 symmetry_used = get_iv ("symmetry_used");
77}
78
79random_map_params::~random_map_params ()
80{
81 SvREFCNT_dec (hv);
82}
83
84shstr_tmp
85random_map_params::as_shstr () const
86{
87 set ("xsize" , xsize);
88 set ("ysize" , ysize);
89 set ("layoutoptions1" , layoutoptions1);
90 set ("layoutoptions2" , layoutoptions2);
91 set ("layoutoptions3" , layoutoptions3);
92 set ("dungeon_depth" , dungeon_depth);
93 set ("random_seed" , (UV)random_seed);
94 set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
95 set ("difficulty_increase", difficulty_increase);
96 set ("dungeon_level" , dungeon_level);
97
98 dynbuf_text buf;
99 hv_iterinit (hv);
100
101 // does not work for utf-8 keys
102 while (HE *he = hv_iternext (hv))
103 {
104 STRLEN klen; const char *key = HePV (he, klen);
105 STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen);
106
107 buf.fadd (key, klen);
108 buf << ' ';
109 buf.fadd (value, vlen);
110 buf << '\n';
111 }
112
113 return shstr (buf);
114}
115
34noinline SV * 116noinline SV *
35random_map_params::get_sv (const char *option) const 117random_map_params::get_sv (const char *option) const
36{ 118{
37 SV **he = hv_fetch (hv, option, strlen (option), 0); 119 SV **he = hv_fetch (hv, option, strlen (option), 0);
38 120
100random_map_params::set (const char *option, NV value) const 182random_map_params::set (const char *option, NV value) const
101{ 183{
102 set (option, newSVnv (value)); 184 set (option, newSVnv (value));
103} 185}
104 186
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 187bool
172maptile::generate_random_map (random_map_params *RP) 188maptile::generate_random_map (random_map_params *RP)
173{ 189{
174 RP->Xsize = RP->xsize; 190 RP->Xsize = RP->xsize;
175 RP->Ysize = RP->ysize; 191 RP->Ysize = RP->ysize;
192
193 max_it (RP->dungeon_level, 1);
176 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 RP->random_seed = RP->random_seed
179 ? RP->random_seed + RP->dungeon_level 197 ? RP->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 (RP->random_seed);
202 rmg_rndm.seed (RP->random_seed + pticks);//D
184 203
185 shstr buf = RP->as_shstr (); 204 shstr buf = RP->as_shstr ();
186 205
187 if (RP->difficulty == 0) 206 if (RP->difficulty == 0)
188 { 207 {
189 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 */
190 209
191 if (RP->difficulty_increase > 0.001) 210 if (RP->difficulty_increase > 0.001f)
192 RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); 211 RP->difficulty = RP->dungeon_level * RP->difficulty_increase;
193 212
194 if (RP->difficulty < 1) 213 if (RP->difficulty < 1)
195 RP->difficulty = 1; 214 RP->difficulty = 1;
196 } 215 }
197 else 216 else
204 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; 223 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
205 224
206 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); 225 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
207 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); 226 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
208 227
228 int symmetry = RP->get_iv ("symmetry", SYMMETRY_NONE);
229
209 if (RP->symmetry == SYMMETRY_RANDOM) 230 if (symmetry == SYMMETRY_RANDOM)
210 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; 231 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
211 else 232 else
212 RP->symmetry_used = RP->symmetry; 233 RP->symmetry_used = symmetry;
213 234
214 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) 235 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
215 RP->Ysize = RP->Ysize / 2 + 1; 236 RP->Ysize = RP->Ysize / 2 + 1;
216 237
217 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) 238 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
221 { 242 {
222 RP->Xsize /= 2; 243 RP->Xsize /= 2;
223 RP->Ysize /= 2; 244 RP->Ysize /= 2;
224 } 245 }
225 246
247 const char *layoutstyle = RP->get_str ("layoutstyle", "");
248
226 if (strstr (RP->layoutstyle, "onion")) 249 if (strstr (layoutstyle, "onion"))
227 RP->map_layout_style = LAYOUT_ONION; 250 RP->map_layout_style = LAYOUT_ONION;
228 else if (strstr (RP->layoutstyle, "maze")) 251 else if (strstr (layoutstyle, "maze"))
229 RP->map_layout_style = LAYOUT_MAZE; 252 RP->map_layout_style = LAYOUT_MAZE;
230 else if (strstr (RP->layoutstyle, "spiral")) 253 else if (strstr (layoutstyle, "spiral"))
231 RP->map_layout_style = LAYOUT_SPIRAL; 254 RP->map_layout_style = LAYOUT_SPIRAL;
232 else if (strstr (RP->layoutstyle, "rogue")) 255 else if (strstr (layoutstyle, "rogue"))
233 RP->map_layout_style = LAYOUT_ROGUELIKE; 256 RP->map_layout_style = LAYOUT_ROGUELIKE;
234 else if (strstr (RP->layoutstyle, "snake")) 257 else if (strstr (layoutstyle, "snake"))
235 RP->map_layout_style = LAYOUT_SNAKE; 258 RP->map_layout_style = LAYOUT_SNAKE;
236 else if (strstr (RP->layoutstyle, "squarespiral")) 259 else if (strstr (layoutstyle, "squarespiral"))
237 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; 260 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
238 else if (strstr (RP->layoutstyle, "cave")) 261 else if (strstr (layoutstyle, "cave"))
239 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;
240 else 267 else
241 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 */
242 269
243 Layout layout (RP->Xsize, RP->Ysize); 270 layout maze (RP->Xsize, RP->Ysize);
244 layout.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 (RP->expand2x)
279 maze.expand2x ();
280
281#if 0
282 maze.print ();//D
283#endif
245 284
246 /* increment these for the current map */ 285 /* increment these for the current map */
247 ++RP->dungeon_level; 286 ++RP->dungeon_level;
248 287
249 // 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
250 RP->Xsize = layout.w; 289 RP->Xsize = maze.w;
251 RP->Ysize = layout.h; 290 RP->Ysize = maze.h;
252 291
253 /* allocate the map and set the floor */ 292 /* allocate the map and set the floor */
254 make_map_floor (layout, RP->get_str ("floorstyle", ""), RP); 293 make_map_floor (maze, RP->get_str ("floorstyle", ""), RP);
255 294
256 /* set region */ 295 /* set region */
257 default_region = RP->region; 296 default_region = region::find (RP->get_str ("region", 0));
258 297
259 CEDE; 298 CEDE;
260 299
261 place_specials_in_map (this, layout, RP); 300 place_specials_in_map (this, maze, RP);
262 301
263 CEDE; 302 CEDE;
264 303
265 const char *wallstyle = RP->get_str ("wallstyle", 0); 304 const char *wallstyle = RP->get_str ("wallstyle", "");
266 305
267 /* create walls unless the wallstyle is "none" */ 306 /* create walls unless the wallstyle is "none" */
268 if (strcmp (wallstyle, "none")) 307 if (strcmp (wallstyle, "none"))
269 { 308 {
270 make_map_walls (this, layout, wallstyle, RP->get_str ("miningstyle", ""), RP); 309 make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP);
310
311 const char *doorstyle = RP->get_str ("doorstyle", "");
271 312
272 /* place doors unless doorstyle or wallstyle is "none" */ 313 /* place doors unless doorstyle or wallstyle is "none" */
273 if (strcmp (RP->doorstyle, "none")) 314 if (strcmp (doorstyle, "none"))
274 put_doors (this, layout, RP->doorstyle, RP); 315 put_doors (this, maze, doorstyle, RP);
275 } 316 }
276 317
277 CEDE; 318 CEDE;
278 319
279 const char *exitstyle = RP->get_str ("exitstyle", ""); 320 const char *exitstyle = RP->get_str ("exitstyle", "");
280 321
281 /* create exits unless the exitstyle is "none" */ 322 /* create exits unless the exitstyle is "none" */
282 if (strcmp (exitstyle, "none")) 323 if (strcmp (exitstyle, "none"))
283 place_exits (this, layout, exitstyle, RP->orientation, RP); 324 place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP);
284 325
285 CEDE; 326 CEDE;
327
328 const char *monsterstyle = RP->get_str ("monsterstyle", "");
286 329
287 /* create monsters unless the monsterstyle is "none" */ 330 /* create monsters unless the monsterstyle is "none" */
288 if (strcmp (RP->monsterstyle, "none")) 331 if (strcmp (monsterstyle, "none"))
289 place_monsters (this, RP->monsterstyle, RP->difficulty, RP); 332 place_monsters (this, monsterstyle, RP->difficulty, RP);
290 333
291 CEDE; 334 CEDE;
292 335
293 /* treasures needs to have a proper difficulty set for the map. */ 336 /* treasures needs to have a proper difficulty set for the map. */
294 difficulty = estimate_difficulty (); 337 difficulty = estimate_difficulty ();
296 CEDE; 339 CEDE;
297 340
298 const char *treasurestyle = RP->get_str ("treasurestyle", ""); 341 const char *treasurestyle = RP->get_str ("treasurestyle", "");
299 342
300 /* create treasure unless the treasurestyle is "none" */ 343 /* create treasure unless the treasurestyle is "none" */
301 place_treasure (this, layout, treasurestyle, RP->get_iv ("treasureoptions"), RP); 344 place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP);
302 345
303 CEDE; 346 CEDE;
304 347
305 const char *decorstyle = RP->get_str ("treasurestyle", ""); 348 const char *decorstyle = RP->get_str ("treasurestyle", "");
306 349
307 /* create decor unless the decorstyle is "none" */ 350 /* create decor unless the decorstyle is "none" */
308 if (strcmp (decorstyle, "none")) 351 if (strcmp (decorstyle, "none"))
309 put_decor (this, layout, decorstyle, RP->get_iv ("decoroptions"), RP); 352 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
310 353
311 CEDE; 354 CEDE;
312 355
313 /* generate treasures, etc. */ 356 /* generate treasures, etc. */
314 fix_auto_apply (); 357 fix_auto_apply ();
315 358
316 CEDE; 359 CEDE;
317 360
318 unblock_exits (this, layout, RP); 361 unblock_exits (this, maze);
319 362
320 msg = buf; 363 msg = buf;
321 in_memory = MAP_ACTIVE; 364 in_memory = MAP_ACTIVE;
322 365
323 CEDE; 366 CEDE;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines