| 1 |
elmex |
1.1 |
/* |
| 2 |
root |
1.31 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
pippijn |
1.20 |
* |
| 4 |
root |
1.47 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 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 |
pippijn |
1.20 |
* |
| 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 |
pippijn |
1.20 |
* |
| 13 |
|
|
* 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 |
|
|
* |
| 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.30 |
* |
| 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 |
|
|
#include <random_map.h> |
| 29 |
|
|
#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.55 |
noinline SV * |
| 35 |
|
|
random_map_params::get_sv (const char *option) const |
| 36 |
root |
1.51 |
{ |
| 37 |
|
|
SV **he = hv_fetch (hv, option, strlen (option), 0); |
| 38 |
|
|
|
| 39 |
root |
1.55 |
return he ? *he : 0; |
| 40 |
root |
1.51 |
} |
| 41 |
|
|
|
| 42 |
root |
1.55 |
noinline const_utf8_string |
| 43 |
|
|
random_map_params::get_str (const char *option, const_utf8_string fallback) const |
| 44 |
|
|
{ |
| 45 |
|
|
SV *sv = get_sv (option); |
| 46 |
|
|
return sv ? cfSvPVutf8_nolen (sv) : fallback; |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
noinline IV |
| 50 |
root |
1.51 |
random_map_params::get_iv (const char *option, IV fallback) const |
| 51 |
|
|
{ |
| 52 |
root |
1.55 |
SV *sv = get_sv (option); |
| 53 |
|
|
return sv ? SvIV (sv) : fallback; |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
noinline UV |
| 57 |
|
|
random_map_params::get_uv (const char *option, UV fallback) const |
| 58 |
|
|
{ |
| 59 |
|
|
SV *sv = get_sv (option); |
| 60 |
|
|
return sv ? SvUV (sv) : fallback; |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
|
noinline NV |
| 64 |
|
|
random_map_params::get_nv (const char *option, NV fallback) const |
| 65 |
|
|
{ |
| 66 |
|
|
SV *sv = get_sv (option); |
| 67 |
|
|
return sv ? SvNV (sv) : fallback; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
noinline void |
| 71 |
|
|
random_map_params::set (const char *option, SV *value) const |
| 72 |
|
|
{ |
| 73 |
|
|
int len = strlen (option); |
| 74 |
|
|
|
| 75 |
|
|
if (value) |
| 76 |
|
|
hv_store (hv, option, len, value, 0); |
| 77 |
|
|
else |
| 78 |
|
|
hv_delete (hv, option, len, G_DISCARD); |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
noinline void |
| 82 |
|
|
random_map_params::set (const char *option, const_utf8_string value) const |
| 83 |
|
|
{ |
| 84 |
|
|
set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
void |
| 88 |
|
|
random_map_params::set (const char *option, IV value) const |
| 89 |
|
|
{ |
| 90 |
|
|
set (option, newSViv (value)); |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
void |
| 94 |
|
|
random_map_params::set (const char *option, UV value) const |
| 95 |
|
|
{ |
| 96 |
|
|
set (option, newSVuv (value)); |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
void |
| 100 |
|
|
random_map_params::set (const char *option, NV value) const |
| 101 |
|
|
{ |
| 102 |
|
|
set (option, newSVnv (value)); |
| 103 |
|
|
} |
| 104 |
|
|
|
| 105 |
|
|
void |
| 106 |
|
|
random_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 |
|
|
|
| 123 |
|
|
shstr_tmp |
| 124 |
|
|
random_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 |
root |
1.51 |
|
| 163 |
root |
1.55 |
return shstr (buf); |
| 164 |
root |
1.51 |
} |
| 165 |
|
|
|
| 166 |
|
|
random_map_params::~random_map_params () |
| 167 |
|
|
{ |
| 168 |
|
|
SvREFCNT_dec (hv); |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
root |
1.43 |
bool |
| 172 |
|
|
maptile::generate_random_map (random_map_params *RP) |
| 173 |
|
|
{ |
| 174 |
|
|
RP->Xsize = RP->xsize; |
| 175 |
|
|
RP->Ysize = RP->ysize; |
| 176 |
|
|
|
| 177 |
|
|
/* pick a random seed, or use the one from the input file */ |
| 178 |
|
|
RP->random_seed = RP->random_seed |
| 179 |
|
|
? RP->random_seed + RP->dungeon_level |
| 180 |
|
|
: time (0); |
| 181 |
|
|
|
| 182 |
|
|
// we run "single-threaded" |
| 183 |
|
|
rmg_rndm.seed (RP->random_seed); |
| 184 |
|
|
|
| 185 |
root |
1.55 |
shstr buf = RP->as_shstr (); |
| 186 |
root |
1.43 |
|
| 187 |
|
|
if (RP->difficulty == 0) |
| 188 |
|
|
{ |
| 189 |
|
|
RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ |
| 190 |
|
|
|
| 191 |
|
|
if (RP->difficulty_increase > 0.001) |
| 192 |
|
|
RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); |
| 193 |
|
|
|
| 194 |
|
|
if (RP->difficulty < 1) |
| 195 |
|
|
RP->difficulty = 1; |
| 196 |
|
|
} |
| 197 |
|
|
else |
| 198 |
|
|
RP->difficulty_given = 1; |
| 199 |
|
|
|
| 200 |
|
|
if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
| 201 |
|
|
RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
| 202 |
|
|
|
| 203 |
|
|
if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
| 204 |
|
|
RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
| 205 |
|
|
|
| 206 |
root |
1.58 |
min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); |
| 207 |
|
|
min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); |
| 208 |
|
|
|
| 209 |
root |
1.43 |
if (RP->symmetry == SYMMETRY_RANDOM) |
| 210 |
|
|
RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; |
| 211 |
|
|
else |
| 212 |
|
|
RP->symmetry_used = RP->symmetry; |
| 213 |
|
|
|
| 214 |
|
|
if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) |
| 215 |
|
|
RP->Ysize = RP->Ysize / 2 + 1; |
| 216 |
|
|
|
| 217 |
|
|
if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
| 218 |
|
|
RP->Xsize = RP->Xsize / 2 + 1; |
| 219 |
|
|
|
| 220 |
|
|
if (RP->expand2x > 0) |
| 221 |
|
|
{ |
| 222 |
|
|
RP->Xsize /= 2; |
| 223 |
|
|
RP->Ysize /= 2; |
| 224 |
|
|
} |
| 225 |
|
|
|
| 226 |
|
|
if (strstr (RP->layoutstyle, "onion")) |
| 227 |
|
|
RP->map_layout_style = LAYOUT_ONION; |
| 228 |
|
|
else if (strstr (RP->layoutstyle, "maze")) |
| 229 |
|
|
RP->map_layout_style = LAYOUT_MAZE; |
| 230 |
|
|
else if (strstr (RP->layoutstyle, "spiral")) |
| 231 |
|
|
RP->map_layout_style = LAYOUT_SPIRAL; |
| 232 |
|
|
else if (strstr (RP->layoutstyle, "rogue")) |
| 233 |
|
|
RP->map_layout_style = LAYOUT_ROGUELIKE; |
| 234 |
|
|
else if (strstr (RP->layoutstyle, "snake")) |
| 235 |
|
|
RP->map_layout_style = LAYOUT_SNAKE; |
| 236 |
|
|
else if (strstr (RP->layoutstyle, "squarespiral")) |
| 237 |
|
|
RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; |
| 238 |
root |
1.57 |
else if (strstr (RP->layoutstyle, "cave")) |
| 239 |
|
|
RP->map_layout_style = LAYOUT_CAVE; |
| 240 |
root |
1.43 |
else |
| 241 |
root |
1.59 |
RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ |
| 242 |
root |
1.43 |
|
| 243 |
root |
1.61 |
layout maze (RP->Xsize, RP->Ysize); |
| 244 |
|
|
maze.generate (RP); |
| 245 |
root |
1.43 |
|
| 246 |
|
|
/* increment these for the current map */ |
| 247 |
|
|
++RP->dungeon_level; |
| 248 |
|
|
|
| 249 |
root |
1.61 |
// need to patch RP becasue following code doesn't use the layout object |
| 250 |
|
|
RP->Xsize = maze.w; |
| 251 |
|
|
RP->Ysize = maze.h; |
| 252 |
root |
1.43 |
|
| 253 |
|
|
/* allocate the map and set the floor */ |
| 254 |
root |
1.61 |
make_map_floor (maze, RP->get_str ("floorstyle", ""), RP); |
| 255 |
root |
1.43 |
|
| 256 |
|
|
/* set region */ |
| 257 |
|
|
default_region = RP->region; |
| 258 |
|
|
|
| 259 |
|
|
CEDE; |
| 260 |
|
|
|
| 261 |
root |
1.61 |
place_specials_in_map (this, maze, RP); |
| 262 |
root |
1.43 |
|
| 263 |
|
|
CEDE; |
| 264 |
|
|
|
| 265 |
root |
1.51 |
const char *wallstyle = RP->get_str ("wallstyle", 0); |
| 266 |
|
|
|
| 267 |
root |
1.43 |
/* create walls unless the wallstyle is "none" */ |
| 268 |
root |
1.51 |
if (strcmp (wallstyle, "none")) |
| 269 |
root |
1.43 |
{ |
| 270 |
root |
1.61 |
make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); |
| 271 |
root |
1.43 |
|
| 272 |
|
|
/* place doors unless doorstyle or wallstyle is "none" */ |
| 273 |
|
|
if (strcmp (RP->doorstyle, "none")) |
| 274 |
root |
1.61 |
put_doors (this, maze, RP->doorstyle, RP); |
| 275 |
root |
1.43 |
} |
| 276 |
|
|
|
| 277 |
|
|
CEDE; |
| 278 |
|
|
|
| 279 |
root |
1.51 |
const char *exitstyle = RP->get_str ("exitstyle", ""); |
| 280 |
|
|
|
| 281 |
root |
1.43 |
/* create exits unless the exitstyle is "none" */ |
| 282 |
root |
1.51 |
if (strcmp (exitstyle, "none")) |
| 283 |
root |
1.61 |
place_exits (this, maze, exitstyle, RP->orientation, RP); |
| 284 |
root |
1.43 |
|
| 285 |
|
|
CEDE; |
| 286 |
|
|
|
| 287 |
|
|
/* create monsters unless the monsterstyle is "none" */ |
| 288 |
|
|
if (strcmp (RP->monsterstyle, "none")) |
| 289 |
|
|
place_monsters (this, RP->monsterstyle, RP->difficulty, RP); |
| 290 |
|
|
|
| 291 |
|
|
CEDE; |
| 292 |
|
|
|
| 293 |
|
|
/* treasures needs to have a proper difficulty set for the map. */ |
| 294 |
|
|
difficulty = estimate_difficulty (); |
| 295 |
|
|
|
| 296 |
|
|
CEDE; |
| 297 |
|
|
|
| 298 |
root |
1.55 |
const char *treasurestyle = RP->get_str ("treasurestyle", ""); |
| 299 |
|
|
|
| 300 |
root |
1.43 |
/* create treasure unless the treasurestyle is "none" */ |
| 301 |
root |
1.61 |
place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); |
| 302 |
root |
1.43 |
|
| 303 |
|
|
CEDE; |
| 304 |
|
|
|
| 305 |
root |
1.55 |
const char *decorstyle = RP->get_str ("treasurestyle", ""); |
| 306 |
|
|
|
| 307 |
root |
1.43 |
/* create decor unless the decorstyle is "none" */ |
| 308 |
root |
1.55 |
if (strcmp (decorstyle, "none")) |
| 309 |
root |
1.61 |
put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); |
| 310 |
root |
1.43 |
|
| 311 |
|
|
CEDE; |
| 312 |
|
|
|
| 313 |
|
|
/* generate treasures, etc. */ |
| 314 |
|
|
fix_auto_apply (); |
| 315 |
|
|
|
| 316 |
|
|
CEDE; |
| 317 |
|
|
|
| 318 |
root |
1.61 |
unblock_exits (this, maze, RP); |
| 319 |
root |
1.43 |
|
| 320 |
root |
1.51 |
msg = buf; |
| 321 |
root |
1.43 |
in_memory = MAP_ACTIVE; |
| 322 |
|
|
|
| 323 |
|
|
CEDE; |
| 324 |
|
|
|
| 325 |
|
|
return 1; |
| 326 |
|
|
} |
| 327 |
|
|
|