/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2001 Mark Wedel & Crossfire Development Team * Copyright (©) 1992 Frank Tore Johansen * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the Affero GNU General Public License * and the GNU General Public License along with this program. If not, see * . * * The authors can be reached via e-mail to */ #include #include #include #include #include #include #define CEDE coroapi::cede_to_tick () random_map_params::random_map_params () { hv = newHV (); } random_map_params::random_map_params (random_map_params *RP) { *this = *RP; HV *copy = newHV (); hv_iterinit (hv); // does not work for utf-8 keys while (HE *he = hv_iternext (hv)) { STRLEN klen; const char *key = HePV (he, klen); hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he)); } hv = copy; } random_map_params::random_map_params (HV *hv) { this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv); assign (wall_name, get_str ("wall_name")); xsize = get_iv ("xsize"); ysize = get_iv ("ysize"); layoutoptions1 = get_iv ("layoutoptions1"); layoutoptions2 = get_iv ("layoutoptions2"); layoutoptions3 = get_iv ("layoutoptions3"); difficulty = get_iv ("difficulty"); difficulty_given = get_iv ("difficulty_given"); difficulty_increase = get_nv ("difficulty_increase"); dungeon_level = get_iv ("dungeon_level"); dungeon_depth = get_iv ("dungeon_depth"); total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy symmetry_used = get_iv ("symmetry_used"); } random_map_params::~random_map_params () { SvREFCNT_dec (hv); } shstr_tmp random_map_params::as_shstr () const { set ("xsize" , xsize); set ("ysize" , ysize); set ("layoutoptions1" , layoutoptions1); set ("layoutoptions2" , layoutoptions2); set ("layoutoptions3" , layoutoptions3); set ("dungeon_depth" , dungeon_depth); set ("difficulty" , difficulty && difficulty_given ? difficulty : 0); set ("difficulty_increase", difficulty_increase); set ("dungeon_level" , dungeon_level); dynbuf_text buf; hv_iterinit (hv); // does not work for utf-8 keys while (HE *he = hv_iternext (hv)) { STRLEN klen; const char *key = HePV (he, klen); STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen); buf.fadd (key, klen); buf << ' '; buf.fadd (value, vlen); buf << '\n'; } return shstr (buf); } noinline SV * random_map_params::opt_sv (const char *option) const { SV **he = hv_fetch (hv, option, strlen (option), 0); return he ? *he : 0; } noinline const_utf8_string random_map_params::get_str (const char *option, const_utf8_string fallback) const { SV *sv = opt_sv (option); return sv ? cfSvPVutf8_nolen (sv) : fallback; } noinline IV random_map_params::get_iv (const char *option, IV fallback) const { SV *sv = opt_sv (option); return sv ? SvIV (sv) : fallback; } noinline UV random_map_params::get_uv (const char *option, UV fallback) const { SV *sv = opt_sv (option); return sv ? SvUV (sv) : fallback; } noinline NV random_map_params::get_nv (const char *option, NV fallback) const { SV *sv = opt_sv (option); return sv ? SvNV (sv) : fallback; } noinline void random_map_params::set (const char *option, SV *value) const { int len = strlen (option); if (value) hv_store (hv, option, len, value, 0); else hv_delete (hv, option, len, G_DISCARD); } noinline void random_map_params::set (const char *option, const_utf8_string value) const { set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); } void random_map_params::set (const char *option, IV value) const { set (option, newSViv (value)); } void random_map_params::set (const char *option, UV value) const { set (option, newSVuv (value)); } void random_map_params::set (const char *option, NV value) const { set (option, newSVnv (value)); } bool maptile::generate_random_map (random_map_params *RP) { RP->Xsize = RP->xsize; RP->Ysize = RP->ysize; max_it (RP->dungeon_level, 1); IV expand2x = RP->get_iv ("expand2x"); UV random_seed = RP->get_uv ("random_seed"); /* pick a random seed, or use the one from the input file */ random_seed = random_seed ? random_seed + RP->dungeon_level : pticks; // we run "single-threaded" rmg_rndm.seed (random_seed); shstr buf = RP->as_shstr (); if (RP->difficulty == 0) { RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ if (RP->difficulty_increase > 0.001f) RP->difficulty = RP->dungeon_level * RP->difficulty_increase; if (RP->difficulty < 1) RP->difficulty = 1; } else RP->difficulty_given = 1; if (RP->Xsize < MIN_RANDOM_MAP_SIZE) RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; if (RP->Ysize < MIN_RANDOM_MAP_SIZE) RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); int symmetry = RP->get_iv ("symmetry", SYMMETRY_NONE); if (symmetry == SYMMETRY_RANDOM) RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; else RP->symmetry_used = symmetry; if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) RP->Ysize = RP->Ysize / 2 + 1; if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) RP->Xsize = RP->Xsize / 2 + 1; if (expand2x) { RP->Xsize /= 2; RP->Ysize /= 2; } const char *layoutstyle = RP->get_str ("layoutstyle", ""); if (strstr (layoutstyle, "onion")) RP->map_layout_style = LAYOUT_ONION; else if (strstr (layoutstyle, "maze")) RP->map_layout_style = LAYOUT_MAZE; else if (strstr (layoutstyle, "spiral")) RP->map_layout_style = LAYOUT_SPIRAL; else if (strstr (layoutstyle, "rogue")) RP->map_layout_style = LAYOUT_ROGUELIKE; else if (strstr (layoutstyle, "snake")) RP->map_layout_style = LAYOUT_SNAKE; else if (strstr (layoutstyle, "squarespiral")) RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; else if (strstr (layoutstyle, "cave")) RP->map_layout_style = LAYOUT_CAVE; else if (strstr (layoutstyle, "castle")) RP->map_layout_style = LAYOUT_CASTLE; else if (strstr (layoutstyle, "multiple")) RP->map_layout_style = LAYOUT_MULTIPLE; else RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ layout maze (RP->Xsize, RP->Ysize); maze.generate (RP); if (RP->get_iv ("rotate", 1)) maze.rotate (rmg_rndm (4)); maze.symmetrize (RP->symmetry_used); if (expand2x) maze.expand2x (); #if 0 maze.print ();//D #endif /* increment these for the current map */ ++RP->dungeon_level; // need to patch RP becasue following code doesn't use the layout object RP->Xsize = maze.w; RP->Ysize = maze.h; /* allocate the map and set the floor */ make_map_floor (maze, RP->get_str ("floorstyle", ""), RP); /* set region */ default_region = region::find (RP->get_str ("region", 0)); CEDE; place_specials_in_map (this, maze, RP); CEDE; const char *wallstyle = RP->get_str ("wallstyle", ""); /* create walls unless the wallstyle is "none" */ if (strcmp (wallstyle, "none")) { make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); const char *doorstyle = RP->get_str ("doorstyle", ""); /* place doors unless doorstyle or wallstyle is "none" */ if (strcmp (doorstyle, "none")) put_doors (this, maze, doorstyle, RP); } CEDE; const char *exitstyle = RP->get_str ("exitstyle", ""); /* create exits unless the exitstyle is "none" */ if (strcmp (exitstyle, "none")) place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP); CEDE; const char *monsterstyle = RP->get_str ("monsterstyle", ""); /* create monsters unless the monsterstyle is "none" */ if (strcmp (monsterstyle, "none")) place_monsters (this, monsterstyle, RP->difficulty, RP); CEDE; /* treasures needs to have a proper difficulty set for the map. */ difficulty = estimate_difficulty (); CEDE; const char *treasurestyle = RP->get_str ("treasurestyle", ""); /* create treasure unless the treasurestyle is "none" */ place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); CEDE; const char *decorstyle = RP->get_str ("decorstyle", ""); /* create decor unless the decorstyle is "none" */ if (strcmp (decorstyle, "none")) put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); CEDE; /* generate treasures, etc. */ fix_auto_apply (); CEDE; unblock_exits (this, maze); msg = buf; in_memory = MAP_INACTIVE; // this is probably a lie, but currently works with ext/map-random.ext CEDE; return 1; }