Move scenario & map generation to functions. Reduces code duplication.

This commit is contained in:
Andrius Silinskas 2013-07-16 10:06:30 +01:00
parent 9a67ecf381
commit be398487ea

View file

@ -162,6 +162,38 @@ static void store_carryover(game_state& gamestate, playsingle_controller& playco
gamestate.carryover_sides_start = sides.to_config();
}
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;
}
void play_replay(display& disp, game_state& gamestate, const config& game_config,
CVideo& video)
@ -437,16 +469,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// If the entire scenario should be randomly generated
if((*scenario)["scenario_generation"] != "") {
LOG_G << "randomly generating scenario...\n";
const cursor::setter cursor_setter(cursor::WAIT);
static config scenario2;
scenario2 = random_generate_scenario((*scenario)["scenario_generation"], scenario->child("generator"));
//TODO comment or remove
//level_ = scenario;
//merge carryover information into the newly generated scenario
scenario = &scenario2;
generate_scenario(scenario);
}
std::string map_data = (*scenario)["map_data"];
if(map_data.empty() && (*scenario)["map"] != "") {
@ -455,18 +478,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// If the map should be randomly generated
if(map_data.empty() && (*scenario)["map_generation"] != "") {
const cursor::setter cursor_setter(cursor::WAIT);
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_level;
new_level = *scenario;
new_level["map_data"] = map_data;
scenario = &new_level;
LOG_G << "generated map\n";
generate_map(scenario);
}
sound::empty_playlist();
@ -624,15 +636,9 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// If the entire scenario should be randomly generated
if((*scenario)["scenario_generation"] != "") {
LOG_G << "randomly generating scenario...\n";
const cursor::setter cursor_setter(cursor::WAIT);
static config scenario2;
scenario2 = random_generate_scenario((*scenario)["scenario_generation"], scenario->child("generator"));
//TODO comment or remove
//level_ = scenario;
scenario = &scenario2;
generate_scenario(scenario);
}
std::string map_data = (*scenario)["map_data"];
if(map_data.empty() && (*scenario)["map"] != "") {
map_data = read_map((*scenario)["map"]);
@ -640,17 +646,7 @@ LEVEL_RESULT play_game(display& disp, game_state& gamestate, const config& game_
// If the map should be randomly generated
if(map_data.empty() && (*scenario)["map_generation"] != "") {
const cursor::setter cursor_setter(cursor::WAIT);
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_level;
new_level = *scenario;
new_level["map_data"] = map_data;
scenario = &new_level;
LOG_G << "generated map\n";
generate_map(scenario);
}
// Move the player information into the hosts gamestate.