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.61 by root, Fri Jul 2 15:03:57 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
35random_map_params::random_map_params ()
36{
37 hv = newHV ();
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
58random_map_params::random_map_params (HV *hv)
59{
60 this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv);
61
62 assign (wall_name, get_str ("wall_name"));
63
64 xsize = get_iv ("xsize");
65 ysize = get_iv ("ysize");
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 total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy
75 symmetry_used = get_iv ("symmetry_used");
76}
77
78random_map_params::~random_map_params ()
79{
80 SvREFCNT_dec (hv);
81}
82
83shstr_tmp
84random_map_params::as_shstr () const
85{
86 set ("xsize" , xsize);
87 set ("ysize" , ysize);
88 set ("layoutoptions1" , layoutoptions1);
89 set ("layoutoptions2" , layoutoptions2);
90 set ("layoutoptions3" , layoutoptions3);
91 set ("dungeon_depth" , dungeon_depth);
92 set ("difficulty" , difficulty && difficulty_given ? difficulty : 0);
93 set ("difficulty_increase", difficulty_increase);
94 set ("dungeon_level" , dungeon_level);
95
96 dynbuf_text buf;
97 hv_iterinit (hv);
98
99 // does not work for utf-8 keys
100 while (HE *he = hv_iternext (hv))
101 {
102 STRLEN klen; const char *key = HePV (he, klen);
103 STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen);
104
105 buf.fadd (key, klen);
106 buf << ' ';
107 buf.fadd (value, vlen);
108 buf << '\n';
109 }
110
111 return shstr (buf);
112}
113
34noinline SV * 114ecb_noinline SV *
35random_map_params::get_sv (const char *option) const 115random_map_params::opt_sv (const char *option) const
36{ 116{
37 SV **he = hv_fetch (hv, option, strlen (option), 0); 117 SV **he = hv_fetch (hv, option, strlen (option), 0);
38 118
39 return he ? *he : 0; 119 return he ? *he : 0;
40} 120}
41 121
42noinline const_utf8_string 122ecb_noinline const_utf8_string
43random_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
44{ 124{
45 SV *sv = get_sv (option); 125 SV *sv = opt_sv (option);
46 return sv ? cfSvPVutf8_nolen (sv) : fallback; 126 return sv ? cfSvPVutf8_nolen (sv) : fallback;
47} 127}
48 128
49noinline IV 129ecb_noinline IV
50random_map_params::get_iv (const char *option, IV fallback) const 130random_map_params::get_iv (const char *option, IV fallback) const
51{ 131{
52 SV *sv = get_sv (option); 132 SV *sv = opt_sv (option);
53 return sv ? SvIV (sv) : fallback; 133 return sv ? SvIV (sv) : fallback;
54} 134}
55 135
56noinline UV 136ecb_noinline UV
57random_map_params::get_uv (const char *option, UV fallback) const 137random_map_params::get_uv (const char *option, UV fallback) const
58{ 138{
59 SV *sv = get_sv (option); 139 SV *sv = opt_sv (option);
60 return sv ? SvUV (sv) : fallback; 140 return sv ? SvUV (sv) : fallback;
61} 141}
62 142
63noinline NV 143ecb_noinline NV
64random_map_params::get_nv (const char *option, NV fallback) const 144random_map_params::get_nv (const char *option, NV fallback) const
65{ 145{
66 SV *sv = get_sv (option); 146 SV *sv = opt_sv (option);
67 return sv ? SvNV (sv) : fallback; 147 return sv ? SvNV (sv) : fallback;
68} 148}
69 149
70noinline void 150ecb_noinline void
71random_map_params::set (const char *option, SV *value) const 151random_map_params::set (const char *option, SV *value) const
72{ 152{
73 int len = strlen (option); 153 int len = strlen (option);
74 154
75 if (value) 155 if (value)
76 hv_store (hv, option, len, value, 0); 156 hv_store (hv, option, len, value, 0);
77 else 157 else
78 hv_delete (hv, option, len, G_DISCARD); 158 hv_delete (hv, option, len, G_DISCARD);
79} 159}
80 160
81noinline void 161ecb_noinline void
82random_map_params::set (const char *option, const_utf8_string value) const 162random_map_params::set (const char *option, const_utf8_string value) const
83{ 163{
84 set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); 164 set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0);
85} 165}
86 166
100random_map_params::set (const char *option, NV value) const 180random_map_params::set (const char *option, NV value) const
101{ 181{
102 set (option, newSVnv (value)); 182 set (option, newSVnv (value));
103} 183}
104 184
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 185bool
172maptile::generate_random_map (random_map_params *RP) 186maptile::generate_random_map (random_map_params *RP)
173{ 187{
174 RP->Xsize = RP->xsize; 188 RP->Xsize = RP->xsize;
175 RP->Ysize = RP->ysize; 189 RP->Ysize = RP->ysize;
176 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
177 /* 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 */
178 RP->random_seed = RP->random_seed 197 random_seed = random_seed
179 ? RP->random_seed + RP->dungeon_level 198 ? random_seed + RP->dungeon_level
180 : time (0); 199 : server_tick;
181 200
182 // we run "single-threaded" 201 // we run "single-threaded"
183 rmg_rndm.seed (RP->random_seed); 202 rmg_rndm.seed (random_seed);
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)
218 RP->Xsize = RP->Xsize / 2 + 1; 239 RP->Xsize = RP->Xsize / 2 + 1;
219 240
220 if (RP->expand2x > 0) 241 if (expand2x)
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 maze (RP->Xsize, RP->Ysize); 270 layout maze (RP->Xsize, RP->Ysize);
244 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
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
252 291
253 /* allocate the map and set the floor */ 292 /* allocate the map and set the floor */
254 make_map_floor (maze, 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, maze, 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, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); 309 make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP);
271 310
311 const char *doorstyle = RP->get_str ("doorstyle", "");
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, maze, 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, maze, 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 ();
300 /* create treasure unless the treasurestyle is "none" */ 343 /* create treasure unless the treasurestyle is "none" */
301 place_treasure (this, maze, 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 ("decorstyle", "");
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, maze, decorstyle, RP->get_iv ("decoroptions"), RP); 352 put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP);
310 353
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, maze, RP); 361 unblock_exits (this, maze);
319 362
320 msg = buf; 363 msg = buf;
321 in_memory = MAP_ACTIVE; 364 state = MAP_INACTIVE; // this is probably a lie, but currently works with ext/map-random.ext
322 365
323 CEDE; 366 CEDE;
324 367
325 return 1; 368 return 1;
326} 369}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines