Skip to content

Commit

Permalink
fix random map generation
Browse files Browse the repository at this point in the history
this was previously broken because we passed a temporary pointer to
generate_map. While fixing this i moved it to saved_game.cpp

i also added a call to expand_random_scenario in mp_game_utils so that
in a mp campaign players advance to the next scenario which is randomly
generated. they have all the same scenario.
  • Loading branch information
gfgtdf committed Jun 15, 2014
1 parent 8362414 commit 976d990
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 68 deletions.
2 changes: 2 additions & 0 deletions src/mp_game_utils.cpp
Expand Up @@ -59,6 +59,8 @@ config initial_level_config(saved_game& state)
{
const mp_game_settings& params = state.mp_settings();
//Also impliers state.expand_scenario()
//We need to call this before expand_mp_events/options oterwise they might be overwritten
state.expand_random_scenario();
state.expand_mp_events();
state.expand_mp_options();

Expand Down
69 changes: 1 addition & 68 deletions src/playcampaign.cpp
Expand Up @@ -123,39 +123,6 @@ static void store_carryover(saved_game& gamestate, playsingle_controller& playco
}
}

static void generate_scenario(config const*& scenario)
{
LOG_G << "randomly generating scenario...\n";
const cursor::setter cursor_setter(cursor::WAIT);

static config new_scenario;
new_scenario = random_generate_scenario((*scenario)["scenario_generation"],
scenario->child("generator"));

//TODO comment or remove
//level_ = scenario;
//merge carryover information into the newly generated scenario

scenario = &new_scenario;
}

static void generate_map(config const*& scenario)
{
LOG_G << "randomly generating map...\n";
const cursor::setter cursor_setter(cursor::WAIT);

const std::string map_data = random_generate_map(
(*scenario)["map_generation"], scenario->child("generator"));

// Since we've had to generate the map,
// make sure that when we save the game,
// it will not ask for the map to be generated again on reload
static config new_scenario;
new_scenario = *scenario;
new_scenario["map_data"] = map_data;
scenario = &new_scenario;
}

LEVEL_RESULT play_replay(display& disp, saved_game& gamestate, const config& game_config,
CVideo& video, bool is_unit_test)
{
Expand Down Expand Up @@ -334,22 +301,7 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
}
}

// If the entire scenario should be randomly generated
if(starting_pos["scenario_generation"] != "") {
const config * tmp = &starting_pos;
generate_scenario(tmp);
}
std::string map_data = starting_pos["map_data"];
if(map_data.empty() && starting_pos["map"] != "") {
map_data = read_map(starting_pos["map"]);
}

// If the map should be randomly generated
if(map_data.empty() && starting_pos["map_generation"] != "") {
const config * tmp = &starting_pos;
generate_map(tmp);
}

gamestate.expand_random_scenario();
//In case this an mp scenario reloaded by sp this was not already done yet.
//We don't need to expand [option]s because [variables] are persitent
gamestate.expand_mp_events();
Expand Down Expand Up @@ -490,25 +442,6 @@ LEVEL_RESULT play_game(game_display& disp, saved_game& gamestate,
connect_engine->
start_game(mp::connect_engine::FORCE_IMPORT_USERS);
}

// TODO: move this code to mp::connect_engine
// in order to send generated data to the network
// before starting the game.
//
// If the entire scenario should be randomly generated
/*if((*scenario)["scenario_generation"] != "") {
generate_scenario(scenario);
}
std::string map_data = (*scenario)["map_data"];
if(map_data.empty() && (*scenario)["map"] != "") {
map_data = read_map((*scenario)["map"]);
}
// If the map should be randomly generated
if(map_data.empty() && (*scenario)["map_generation"] != "") {
generate_map(scenario);
}*/
}
else if (io_type == IO_NONE && gamestate.valid())
{
Expand Down
32 changes: 32 additions & 0 deletions src/saved_game.cpp
Expand Up @@ -37,9 +37,11 @@
#include "saved_game.hpp"
#include "gamestatus.hpp"
#include "carryover.hpp"
#include "cursor.hpp"
#include "log.hpp"
#include "resources.hpp"
#include "game_config_manager.hpp"
#include "generators/map_create.hpp"
#include "statistics.hpp"
#include "serialization/binary_or_text.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -305,6 +307,36 @@ void saved_game::expand_mp_options()
}
}

void saved_game::expand_random_scenario()
{
expand_scenario();
if(this->starting_pos_type_ == STARTINGPOS_SCENARIO)
{
// If the entire scenario should be randomly generated
if(starting_pos_["scenario_generation"] != "")
{
LOG_NG << "randomly generating scenario...\n";
const cursor::setter cursor_setter(cursor::WAIT);

starting_pos_ = random_generate_scenario(starting_pos_["scenario_generation"],
starting_pos_.child("generator"));
}
//it looks like we support a map= where map=filename equals more or less map_data={filename}
if(starting_pos_["map_data"].empty() && starting_pos_["map"] != "") {
starting_pos_["map_data"] = read_map(starting_pos_["map"]);
}
// If the map should be randomly generated
// We dont want that we accidently to this twice so we check for starting_pos_["map_data"].empty()
if(starting_pos_["map_data"].empty() && starting_pos_["map_generation"] != "") {
LOG_NG << "randomly generating map...\n";
const cursor::setter cursor_setter(cursor::WAIT);

starting_pos_["map_data"] = random_generate_map(
starting_pos_["map_generation"], starting_pos_.child("generator"));
}
}
}

void saved_game::expand_carryover()
{
expand_scenario();
Expand Down
3 changes: 3 additions & 0 deletions src/saved_game.hpp
Expand Up @@ -51,6 +51,9 @@ class saved_game
/// Note that since [variabels] are persistent we only use this once at the beginning
/// of a campaign but callings it multiple times is no harm eigher
void expand_mp_options();
/// takes care of generate_map=, generate_scenario=, map= attributes
/// This should be called before expanding carryover or mp_events because this might completely replace starting_pos_.
void expand_random_scenario();
bool valid();
void set_snapshot(const config& snapshot);
void set_scenario(const config& scenario);
Expand Down

0 comments on commit 976d990

Please sign in to comment.