Simplifying the code for multiplayer game descriptions.
This commit is contained in:
parent
19c1b0e26e
commit
750af58863
7 changed files with 66 additions and 100 deletions
|
@ -22,8 +22,10 @@
|
|||
|
||||
mp_game_settings::mp_game_settings() :
|
||||
name(),
|
||||
era(),
|
||||
password(),
|
||||
hash(),
|
||||
mp_era(),
|
||||
mp_scenario(),
|
||||
num_turns(0),
|
||||
village_gold(0),
|
||||
xp_modifier(0),
|
||||
|
@ -52,8 +54,10 @@ mp_game_settings::mp_game_settings(const config& cfg)
|
|||
mp_game_settings::mp_game_settings(const mp_game_settings& settings)
|
||||
{
|
||||
name = settings.name;
|
||||
era = settings.era;
|
||||
password = settings.password;
|
||||
hash = settings.hash;
|
||||
mp_era = settings.mp_era;
|
||||
mp_scenario = settings.mp_scenario;
|
||||
num_turns = settings.num_turns;
|
||||
village_gold = settings.village_gold;
|
||||
xp_modifier = settings.xp_modifier;
|
||||
|
@ -77,7 +81,9 @@ void mp_game_settings::set_from_config(const config& game_cfg)
|
|||
{
|
||||
const config& cfg = game_cfg.child("multiplayer");
|
||||
name = cfg["scenario"];
|
||||
era = cfg["era"];
|
||||
hash = cfg["hash"];
|
||||
mp_era = cfg["mp_era"];
|
||||
mp_scenario = cfg["mp_scenario"];
|
||||
xp_modifier = lexical_cast_default<int>(cfg["experience_modifier"]);
|
||||
num_turns = lexical_cast_default<int>(cfg["turns"]);
|
||||
use_map_settings = utils::string_bool(cfg["mp_use_map_settings"]);
|
||||
|
@ -90,13 +96,16 @@ void mp_game_settings::set_from_config(const config& game_cfg)
|
|||
mp_countdown_action_bonus = lexical_cast_default<int>(cfg["mp_countdown_action_bonus"]);
|
||||
village_gold = lexical_cast_default<int>(cfg["mp_village_gold"]);
|
||||
allow_observers = utils::string_bool(cfg["observer"]);
|
||||
saved_game = utils::string_bool(cfg["savegame"]);
|
||||
}
|
||||
|
||||
void mp_game_settings::reset()
|
||||
{
|
||||
name = "";
|
||||
era = "";
|
||||
password = "";
|
||||
hash = "";
|
||||
mp_era = "";
|
||||
mp_scenario = "";
|
||||
num_turns = 0;
|
||||
village_gold = 0;
|
||||
xp_modifier = 0;
|
||||
|
@ -115,7 +124,9 @@ config mp_game_settings::to_config() const
|
|||
config cfg;
|
||||
|
||||
cfg["scenario"] = name;
|
||||
cfg["era"] = era;
|
||||
cfg["hash"] = hash;
|
||||
cfg["mp_era"] = mp_era;
|
||||
cfg["mp_scenario"] = mp_scenario;
|
||||
cfg["turns"] = lexical_cast_default<std::string>(num_turns, "20");
|
||||
cfg["experience_modifier"] = lexical_cast<std::string>(xp_modifier);
|
||||
cfg["mp_countdown"] = mp_countdown ? "yes" : "no";
|
||||
|
@ -128,6 +139,7 @@ config mp_game_settings::to_config() const
|
|||
cfg["mp_shroud"] = shroud_game ? "yes" : "no";
|
||||
cfg["mp_use_map_settings"] = use_map_settings ? "yes" : "no";
|
||||
cfg["observer"] = allow_observers ? "yes" : "no";
|
||||
cfg["savegame"] = saved_game ? "yes" : "no";
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
|
|
@ -34,8 +34,10 @@ struct mp_game_settings : public savegame::savegame_config
|
|||
// The items returned while configuring the game
|
||||
|
||||
std::string name;
|
||||
std::string era;
|
||||
std::string password;
|
||||
std::string hash;
|
||||
std::string mp_era;
|
||||
std::string mp_scenario;
|
||||
|
||||
int num_turns;
|
||||
int village_gold;
|
||||
|
|
|
@ -1515,11 +1515,12 @@ void connect::load_game()
|
|||
}
|
||||
} else {
|
||||
level_.clear();
|
||||
level_["savegame"] = "no";
|
||||
params_.saved_game = false;
|
||||
params_.mp_scenario = params_.scenario_data["id"];
|
||||
level_.merge_with(params_.scenario_data);
|
||||
level_.add_child("multiplayer", params_.to_config());
|
||||
|
||||
level_["hash"] = level_.hash();
|
||||
params_.hash = level_.hash();
|
||||
level_["next_underlying_unit_id"] = lexical_cast<std::string>(0);
|
||||
n_unit::id_manager::instance().clear();
|
||||
|
||||
|
@ -1542,7 +1543,7 @@ void connect::load_game()
|
|||
append_to_title(" - " + level_["name"]);
|
||||
|
||||
|
||||
std::string era = params_.era;
|
||||
std::string era = params_.mp_era;
|
||||
if (params_.saved_game) {
|
||||
if (const config &c = level_.child("snapshot").child("era"))
|
||||
era = c["id"];
|
||||
|
|
|
@ -287,7 +287,7 @@ mp_game_settings& create::get_parameters()
|
|||
++era_list.first;
|
||||
}
|
||||
|
||||
parameters_.era = (*era_list.first)["id"];
|
||||
parameters_.mp_era = (*era_list.first)["id"];
|
||||
parameters_.num_turns = turns;
|
||||
// CHECK
|
||||
parameters_.mp_countdown_init_time = mp_countdown_init_time_val;
|
||||
|
|
|
@ -113,6 +113,7 @@ void level_to_gamestate(config& level, game_state& state)
|
|||
//adds the starting pos to the level
|
||||
if (!level.child("replay_start")) {
|
||||
level.add_child("replay_start", level);
|
||||
level.child("replay_start").remove_child("multiplayer", 1);
|
||||
}
|
||||
//this is important, if it does not happen, the starting position is missing and
|
||||
//will be drawn from the snapshot instead (which is not what we want since we have
|
||||
|
|
|
@ -546,42 +546,43 @@ void loadgame::load_multiplayer_game()
|
|||
}
|
||||
|
||||
void loadgame::fill_mplevel_config(config& level){
|
||||
level["savegame"] = "yes";
|
||||
// If we have a start of scenario MP campaign scenario the snapshot
|
||||
// is empty the starting position contains the wanted info.
|
||||
const config& start_data = !gamestate_.snapshot.empty() ? gamestate_.snapshot : gamestate_.starting_pos;
|
||||
level["map_data"] = start_data["map_data"];
|
||||
level["id"] = start_data["id"];
|
||||
level["name"] = start_data["name"];
|
||||
level["completion"] = start_data["completion"];
|
||||
level["next_underlying_unit_id"] = start_data["next_underlying_unit_id"];
|
||||
// Probably not needed.
|
||||
level["turn"] = start_data["turn_at"];
|
||||
level["turn_at"] = start_data["turn_at"];
|
||||
gamestate_.mp_settings().saved_game = true;
|
||||
|
||||
level.add_child("multiplayer", gamestate_.mp_settings().to_config());
|
||||
// If we have a start of scenario MP campaign scenario the snapshot
|
||||
// is empty the starting position contains the wanted info.
|
||||
const config& start_data = !gamestate_.snapshot.empty() ? gamestate_.snapshot : gamestate_.starting_pos;
|
||||
level["map_data"] = start_data["map_data"];
|
||||
level["id"] = start_data["id"];
|
||||
level["name"] = start_data["name"];
|
||||
level["completion"] = start_data["completion"];
|
||||
level["next_underlying_unit_id"] = start_data["next_underlying_unit_id"];
|
||||
// Probably not needed.
|
||||
level["turn"] = start_data["turn_at"];
|
||||
level["turn_at"] = start_data["turn_at"];
|
||||
|
||||
//Start-of-scenario save
|
||||
if(gamestate_.snapshot.empty()){
|
||||
//For a start-of-scenario-save, write the data to the starting_pos and not the snapshot, since
|
||||
//there should only be snapshots for midgame reloads
|
||||
if (config &c = level.child("replay_start")) {
|
||||
c.merge_with(start_data);
|
||||
} else {
|
||||
level.add_child("replay_start") = start_data;
|
||||
}
|
||||
level.add_child("snapshot") = config();
|
||||
level.add_child("multiplayer", gamestate_.mp_settings().to_config());
|
||||
|
||||
//Start-of-scenario save
|
||||
if(gamestate_.snapshot.empty()){
|
||||
//For a start-of-scenario-save, write the data to the starting_pos and not the snapshot, since
|
||||
//there should only be snapshots for midgame reloads
|
||||
if (config &c = level.child("replay_start")) {
|
||||
c.merge_with(start_data);
|
||||
} else {
|
||||
level.add_child("snapshot") = start_data;
|
||||
level.add_child("replay_start") = gamestate_.starting_pos;
|
||||
level.add_child("replay_start") = start_data;
|
||||
}
|
||||
level["random_seed"] = start_data["random_seed"];
|
||||
level["random_calls"] = start_data["random_calls"];
|
||||
level.add_child("snapshot") = config();
|
||||
} else {
|
||||
level.add_child("snapshot") = start_data;
|
||||
level.add_child("replay_start") = gamestate_.starting_pos;
|
||||
}
|
||||
level["random_seed"] = start_data["random_seed"];
|
||||
level["random_calls"] = start_data["random_calls"];
|
||||
|
||||
// Adds the replay data, and the replay start, to the level,
|
||||
// so clients can receive it.
|
||||
level.add_child("replay") = gamestate_.replay_data;
|
||||
level.add_child("statistics") = statistics::write_stats();
|
||||
// Adds the replay data, and the replay start, to the level,
|
||||
// so clients can receive it.
|
||||
level.add_child("replay") = gamestate_.replay_data;
|
||||
level.add_child("statistics") = statistics::write_stats();
|
||||
}
|
||||
|
||||
void loadgame::copy_era(config &cfg)
|
||||
|
|
|
@ -2009,8 +2009,10 @@ void server::process_data_game(const network::connection sock,
|
|||
assert(gamelist != NULL);
|
||||
simple_wml::node& desc = gamelist->add_child("game");
|
||||
g->level().root().copy_into(desc);
|
||||
if (g->level().root().child("multiplayer"))
|
||||
g->level().root().child("multiplayer")->copy_into(desc);
|
||||
if (data.root().child("multiplayer"))
|
||||
data.root().child("multiplayer")->copy_into(desc);
|
||||
else
|
||||
ERR_SERVER << "server.cpp, process_data_game: Expected child 'multiplayer' was not found in level WML.\n";
|
||||
g->set_description(&desc);
|
||||
desc.set_attr_dup("id", lexical_cast<std::string>(g->id()).c_str());
|
||||
} else {
|
||||
|
@ -2030,42 +2032,12 @@ void server::process_data_game(const network::connection sock,
|
|||
desc.set_attr_dup("map_data", data["map_data"]);
|
||||
}
|
||||
if(data.child("era")) {
|
||||
desc.set_attr_dup("mp_era", data.child("era")->attr("id"));
|
||||
if(!(utils::string_bool(data.child("era")->attr("require_era").to_string(),true))) {
|
||||
desc.set_attr("require_era", "no");
|
||||
}
|
||||
} else {
|
||||
desc.set_attr("mp_era", "");
|
||||
}
|
||||
|
||||
const simple_wml::node* mp = data.child("multiplayer");
|
||||
|
||||
// map id
|
||||
desc.set_attr_dup("mp_scenario", data["id"]);
|
||||
desc.set_attr_dup("savegame", data["savegame"]);
|
||||
desc.set_attr_dup("hash", data["hash"]);
|
||||
if (mp){
|
||||
desc.set_attr_dup("observer", (*mp)["observer"]);
|
||||
desc.set_attr_dup("mp_village_gold", (*mp)["mp_village_gold"]);
|
||||
desc.set_attr_dup("experience_modifier", (*mp)["experience_modifier"]);
|
||||
desc.set_attr_dup("mp_fog", (*mp)["mp_fog"]);
|
||||
desc.set_attr_dup("mp_shroud", (*mp)["mp_shroud"]);
|
||||
desc.set_attr_dup("mp_use_map_settings", (*mp)["mp_use_map_settings"]);
|
||||
desc.set_attr_dup("mp_countdown", (*mp)["mp_countdown"]);
|
||||
desc.set_attr_dup("mp_countdown_init_time", (*mp)["mp_countdown_init_time"]);
|
||||
desc.set_attr_dup("mp_countdown_turn_bonus", (*mp)["mp_countdown_turn_bonus"]);
|
||||
desc.set_attr_dup("mp_countdown_reservoir_time", (*mp)["mp_countdown_reservoir_time"]);
|
||||
desc.set_attr_dup("mp_countdown_action_bonus", (*mp)["mp_countdown_action_bonus"]);
|
||||
}
|
||||
//desc["map_name"] = data["name"];
|
||||
//desc["map_description"] = data["description"];
|
||||
//desc[""] = data["objectives"];
|
||||
//desc[""] = data["random_start_time"];
|
||||
//desc[""] = data["turns"];
|
||||
//desc.set_attr_dup("client_version", data["version"]);
|
||||
|
||||
// Record the full scenario in g->level()
|
||||
|
||||
g->level().swap(data);
|
||||
// The host already put himself in the scenario so we just need
|
||||
// to update_side_data().
|
||||
|
@ -2137,34 +2109,11 @@ void server::process_data_game(const network::connection sock,
|
|||
} else {
|
||||
desc.set_attr("map_data", "");
|
||||
}
|
||||
if (data.root().child("multiplayer"))
|
||||
data.root().child("multiplayer")->copy_into(desc);
|
||||
else
|
||||
ERR_SERVER << "server.cpp, process_data_game: Expected child 'multiplayer' was not found in level WML.\n";
|
||||
|
||||
if(s.child("era")) {
|
||||
desc.set_attr_dup("mp_era", s.child("era")->attr("id"));
|
||||
} else {
|
||||
DBG_SERVER << "using the era id of the previous scenario: " << era_id << std::endl;
|
||||
desc.set_attr_dup("mp_era", era_id.c_str());
|
||||
}
|
||||
|
||||
// map id
|
||||
desc.set_attr_dup("mp_scenario", s["id"]);
|
||||
desc.set_attr_dup("observer", s["observer"]);
|
||||
desc.set_attr_dup("mp_village_gold", s["mp_village_gold"]);
|
||||
desc.set_attr_dup("experience_modifier", s["experience_modifier"]);
|
||||
desc.set_attr_dup("mp_fog", s["mp_fog"]);
|
||||
desc.set_attr_dup("mp_shroud", s["mp_shroud"]);
|
||||
desc.set_attr_dup("mp_use_map_settings", s["mp_use_map_settings"]);
|
||||
desc.set_attr_dup("mp_countdown", s["mp_countdown"]);
|
||||
desc.set_attr_dup("mp_countdown_init_time", s["mp_countdown_init_time"]);
|
||||
desc.set_attr_dup("mp_countdown_turn_bonus", s["mp_countdown_turn_bonus"]);
|
||||
desc.set_attr_dup("mp_countdown_reservoir_time", s["mp_countdown_reservoir_time"]);
|
||||
desc.set_attr_dup("mp_countdown_action_bonus", s["mp_countdown_action_bonus"]);
|
||||
desc.set_attr_dup("hash", s["hash"]);
|
||||
//desc["map_name"] = s["name"];
|
||||
//desc["map_description"] = s["description"];
|
||||
//desc[""] = s["objectives"];
|
||||
//desc[""] = s["random_start_time"];
|
||||
//desc[""] = s["turns"];
|
||||
//desc["client_version"] = s["version"];
|
||||
// Send the update of the game description to the lobby.
|
||||
update_game_in_lobby(g);
|
||||
return;
|
||||
|
|
Loading…
Add table
Reference in a new issue