Saved Game: used a scoped enum for starting_point

This commit is contained in:
Charles Dang 2021-01-02 23:43:45 +11:00
parent 0ed489013f
commit 9d36056dc1
2 changed files with 33 additions and 34 deletions

View File

@ -123,7 +123,7 @@ saved_game::saved_game()
, replay_start_() , replay_start_()
, classification_() , classification_()
, mp_settings_() , mp_settings_()
, starting_point_type_(STARTING_POINT_NONE) , starting_point_type_(starting_point::NONE)
, starting_point_() , starting_point_()
, replay_data_() , replay_data_()
, skip_story_(false) , skip_story_(false)
@ -136,7 +136,7 @@ saved_game::saved_game(config cfg)
, replay_start_() , replay_start_()
, classification_(cfg) , classification_(cfg)
, mp_settings_() , mp_settings_()
, starting_point_type_(STARTING_POINT_NONE) , starting_point_type_(starting_point::NONE)
, starting_point_() , starting_point_()
, replay_data_() , replay_data_()
, skip_story_(false) , skip_story_(false)
@ -194,9 +194,9 @@ void saved_game::write_config(config_writer& out) const
void saved_game::write_starting_point(config_writer& out) const void saved_game::write_starting_point(config_writer& out) const
{ {
if(starting_point_type_ == STARTING_POINT_SNAPSHOT) { if(starting_point_type_ == starting_point::SNAPSHOT) {
out.write_child("snapshot", starting_point_); out.write_child("snapshot", starting_point_);
} else if(starting_point_type_ == STARTING_POINT_SCENARIO) { } else if(starting_point_type_ == starting_point::SCENARIO) {
out.write_child("scenario", starting_point_); out.write_child("scenario", starting_point_);
} }
} }
@ -215,7 +215,7 @@ void saved_game::write_general_info(config_writer& out) const
void saved_game::set_defaults() void saved_game::set_defaults()
{ {
const bool is_loaded_game = starting_point_type_ != STARTING_POINT_SCENARIO; const bool is_loaded_game = starting_point_type_ != starting_point::SCENARIO;
const bool is_multiplayer_tag = classification().get_tagname() == "multiplayer"; const bool is_multiplayer_tag = classification().get_tagname() == "multiplayer";
const game_config_view& game_config = game_config_manager::get()->game_config(); const game_config_view& game_config = game_config_manager::get()->game_config();
static const std::vector<std::string> team_defaults { static const std::vector<std::string> team_defaults {
@ -268,7 +268,7 @@ void saved_game::set_defaults()
void saved_game::expand_scenario() void saved_game::expand_scenario()
{ {
if(starting_point_type_ == STARTING_POINT_NONE && !has_carryover_expanded_) { if(starting_point_type_ == starting_point::NONE && !has_carryover_expanded_) {
game_config_manager::get()->load_game_config_for_game(classification(), carryover_["next_scenario"]); game_config_manager::get()->load_game_config_for_game(classification(), carryover_["next_scenario"]);
const game_config_view& game_config = game_config_manager::get()->game_config(); const game_config_view& game_config = game_config_manager::get()->game_config();
@ -276,7 +276,7 @@ void saved_game::expand_scenario()
game_config.find_child(classification().get_tagname(), "id", carryover_["next_scenario"]); game_config.find_child(classification().get_tagname(), "id", carryover_["next_scenario"]);
if(scenario) { if(scenario) {
starting_point_type_ = STARTING_POINT_SCENARIO; starting_point_type_ = starting_point::SCENARIO;
starting_point_ = scenario; starting_point_ = scenario;
// A hash has to be generated using an unmodified scenario data. // A hash has to be generated using an unmodified scenario data.
@ -288,7 +288,7 @@ void saved_game::expand_scenario()
set_defaults(); set_defaults();
} else { } else {
ERR_NG << "Couldn't find [" << classification().get_tagname() << "] with id=" << carryover_["next_scenario"] << std::endl; ERR_NG << "Couldn't find [" << classification().get_tagname() << "] with id=" << carryover_["next_scenario"] << std::endl;
starting_point_type_ = STARTING_POINT_INVALID; starting_point_type_ = starting_point::INVALID;
starting_point_.clear(); starting_point_.clear();
} }
} }
@ -371,7 +371,7 @@ void saved_game::expand_mp_events()
{ {
expand_scenario(); expand_scenario();
if(starting_point_type_ == STARTING_POINT_SCENARIO && !starting_point_["has_mod_events"].to_bool(false)) { if(starting_point_type_ == starting_point::SCENARIO && !starting_point_["has_mod_events"].to_bool(false)) {
std::vector<modevents_entry> mods; std::vector<modevents_entry> mods;
std::set<std::string> loaded_resources; std::set<std::string> loaded_resources;
@ -410,7 +410,7 @@ void saved_game::expand_mp_events()
void saved_game::expand_mp_options() void saved_game::expand_mp_options()
{ {
if(starting_point_type_ == STARTING_POINT_SCENARIO && !has_carryover_expanded_) { if(starting_point_type_ == starting_point::SCENARIO && !has_carryover_expanded_) {
std::vector<modevents_entry> mods; std::vector<modevents_entry> mods;
std::transform(classification_.active_mods.begin(), classification_.active_mods.end(), std::back_inserter(mods), std::transform(classification_.active_mods.begin(), classification_.active_mods.end(), std::back_inserter(mods),
@ -478,7 +478,7 @@ void saved_game::expand_random_scenario()
{ {
expand_scenario(); expand_scenario();
if(starting_point_type_ == STARTING_POINT_SCENARIO) { if(starting_point_type_ == starting_point::SCENARIO) {
// If the entire scenario should be randomly generated // If the entire scenario should be randomly generated
if(!starting_point_["scenario_generation"].empty()) { if(!starting_point_["scenario_generation"].empty()) {
LOG_NG << "randomly generating scenario...\n"; LOG_NG << "randomly generating scenario...\n";
@ -538,7 +538,7 @@ void saved_game::post_scenario_generation(const config& old_scenario, config& ge
void saved_game::expand_carryover() void saved_game::expand_carryover()
{ {
expand_scenario(); expand_scenario();
if(starting_point_type_ == STARTING_POINT_SCENARIO && !has_carryover_expanded_) { if(starting_point_type_ == starting_point::SCENARIO && !has_carryover_expanded_) {
carryover_info sides(carryover_); carryover_info sides(carryover_);
sides.transfer_to(get_starting_point()); sides.transfer_to(get_starting_point());
@ -553,12 +553,12 @@ void saved_game::expand_carryover()
bool saved_game::valid() const bool saved_game::valid() const
{ {
return starting_point_type_ != STARTING_POINT_INVALID; return starting_point_type_ != starting_point::INVALID;
} }
config& saved_game::set_snapshot(config snapshot) config& saved_game::set_snapshot(config snapshot)
{ {
starting_point_type_ = STARTING_POINT_SNAPSHOT; starting_point_type_ = starting_point::SNAPSHOT;
starting_point_.swap(snapshot); starting_point_.swap(snapshot);
return starting_point_; return starting_point_;
@ -566,7 +566,7 @@ config& saved_game::set_snapshot(config snapshot)
void saved_game::set_scenario(config scenario) void saved_game::set_scenario(config scenario)
{ {
starting_point_type_ = STARTING_POINT_SCENARIO; starting_point_type_ = starting_point::SCENARIO;
starting_point_.swap(scenario); starting_point_.swap(scenario);
has_carryover_expanded_ = false; has_carryover_expanded_ = false;
@ -576,7 +576,7 @@ void saved_game::set_scenario(config scenario)
void saved_game::remove_snapshot() void saved_game::remove_snapshot()
{ {
starting_point_type_ = STARTING_POINT_NONE; starting_point_type_ = starting_point::NONE;
starting_point_.clear(); starting_point_.clear();
} }
@ -597,7 +597,7 @@ const config& saved_game::get_replay_starting_point()
expand_carryover(); expand_carryover();
} }
if(starting_point_type_ == STARTING_POINT_SCENARIO) { if(starting_point_type_ == starting_point::SCENARIO) {
return starting_point_; return starting_point_;
} }
@ -606,7 +606,7 @@ const config& saved_game::get_replay_starting_point()
void saved_game::convert_to_start_save() void saved_game::convert_to_start_save()
{ {
assert(starting_point_type_ == STARTING_POINT_SNAPSHOT); assert(starting_point_type_ == starting_point::SNAPSHOT);
carryover_info sides(starting_point_, true); carryover_info sides(starting_point_, true);
@ -634,9 +634,9 @@ config saved_game::to_config() const
replay_data_.write(r.add_child("replay")); replay_data_.write(r.add_child("replay"));
if(starting_point_type_ == STARTING_POINT_SNAPSHOT) { if(starting_point_type_ == starting_point::SNAPSHOT) {
r.add_child("snapshot", starting_point_); r.add_child("snapshot", starting_point_);
} else if(starting_point_type_ == STARTING_POINT_SCENARIO) { } else if(starting_point_type_ == starting_point::SCENARIO) {
r.add_child("scenario", starting_point_); r.add_child("scenario", starting_point_);
} }
@ -650,7 +650,7 @@ std::string saved_game::get_scenario_id() const
{ {
std::string scenario_id; std::string scenario_id;
if(starting_point_type_ == STARTING_POINT_SNAPSHOT || starting_point_type_ == STARTING_POINT_SCENARIO) { if(starting_point_type_ == starting_point::SNAPSHOT || starting_point_type_ == starting_point::SCENARIO) {
scenario_id = starting_point_["id"].str(); scenario_id = starting_point_["id"].str();
} else if(!has_carryover_expanded_) { } else if(!has_carryover_expanded_) {
scenario_id = carryover_["next_scenario"].str(); scenario_id = carryover_["next_scenario"].str();
@ -767,13 +767,13 @@ void saved_game::set_data(config& cfg)
replay_data_.set_to_end(); replay_data_.set_to_end();
if(config& snapshot = cfg.child("snapshot")) { if(config& snapshot = cfg.child("snapshot")) {
starting_point_type_ = STARTING_POINT_SNAPSHOT; starting_point_type_ = starting_point::SNAPSHOT;
starting_point_.swap(snapshot); starting_point_.swap(snapshot);
} else if(config& scenario = cfg.child("scenario")) { } else if(config& scenario = cfg.child("scenario")) {
starting_point_type_ = STARTING_POINT_SCENARIO; starting_point_type_ = starting_point::SCENARIO;
starting_point_.swap(scenario); starting_point_.swap(scenario);
} else { } else {
starting_point_type_ = STARTING_POINT_NONE; starting_point_type_ = starting_point::NONE;
starting_point_.clear(); starting_point_.clear();
} }
@ -799,7 +799,7 @@ void saved_game::clear()
replay_data_.swap(replay_recorder_base()); replay_data_.swap(replay_recorder_base());
replay_start_.clear(); replay_start_.clear();
starting_point_.clear(); starting_point_.clear();
starting_point_type_ = STARTING_POINT_NONE; starting_point_type_ = starting_point::NONE;
} }
void swap(saved_game& lhs, saved_game& rhs) void swap(saved_game& lhs, saved_game& rhs)

View File

@ -20,20 +20,19 @@
class config_writer; class config_writer;
class saved_game class saved_game
{ {
enum STARTING_POINT_TYPE enum class starting_point {
{
/** There is no scenario stating pos data (start-of-scenario). */ /** There is no scenario stating pos data (start-of-scenario). */
STARTING_POINT_NONE, NONE,
/** We have a [snapshot] (mid-game-savefile). */ /** We have a [snapshot] (mid-game-savefile). */
STARTING_POINT_SNAPSHOT, SNAPSHOT,
/** We have a [scenario] (start-of-scenario) savefile. */ /** We have a [scenario] (start-of-scenario) savefile. */
STARTING_POINT_SCENARIO, SCENARIO,
/** We failed to get a starting pos in expand_scenario. */ /** We failed to get a starting pos in expand_scenario. */
STARTING_POINT_INVALID INVALID
}; };
public: public:
saved_game(); saved_game();
saved_game(const saved_game& state); saved_game(const saved_game& state);
@ -104,7 +103,7 @@ public:
bool is_mid_game_save() const bool is_mid_game_save() const
{ {
return starting_point_type_ == STARTING_POINT_SNAPSHOT; return starting_point_type_ == starting_point::SNAPSHOT;
} }
/** /**
* converts a normal savegame form the end of a scenaio to a start-of-scenario savefile for the next scenaio, * converts a normal savegame form the end of a scenaio to a start-of-scenario savefile for the next scenaio,
@ -153,7 +152,7 @@ private:
game_classification classification_; game_classification classification_;
mp_game_settings mp_settings_; mp_game_settings mp_settings_;
STARTING_POINT_TYPE starting_point_type_; starting_point starting_point_type_;
/** /**
The starting pos where the (non replay) game will be started from. The starting pos where the (non replay) game will be started from.
This can eigher be a [scenario] for a fresh game or a [snapshot] if this is a reloaded game This can eigher be a [scenario] for a fresh game or a [snapshot] if this is a reloaded game