MP Game Utils: formatting and minor code cleanup

This commit is contained in:
Charles Dang 2017-11-24 23:08:40 +11:00
parent 21eb31b757
commit acd31b1a77

View file

@ -13,16 +13,13 @@
*/
#include "game_initialization/mp_game_utils.hpp"
#include "carryover.hpp"
#include "config.hpp"
#include "formula/string_utils.hpp"
#include "saved_game.hpp"
#include "game_config.hpp"
#include "game_config_manager.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "savegame.hpp"
#include "units/id.hpp"
#include "saved_game.hpp"
#include "wesnothd_connection_error.hpp"
static lg::log_domain log_engine("engine");
@ -38,8 +35,8 @@ static lg::log_domain log_network("network");
#define LOG_NW LOG_STREAM(info, log_network)
#define ERR_NW LOG_STREAM(err, log_network)
namespace mp {
namespace mp
{
// This is for the wesnothd server, it expects a more detailed summary in [multiplayer]
static void add_multiplayer_classification(config& multiplayer, saved_game& state)
{
@ -54,8 +51,9 @@ config initial_level_config(saved_game& state)
{
const mp_game_settings& params = state.mp_settings();
state.set_defaults();
//Also impliers state.expand_scenario()
//We need to call this before expand_mp_events/options oterwise they might be overwritten
// Also impliers state.expand_scenario()
// We need to call this before expand_mp_events/options otherwise they might be overwritten.
state.expand_random_scenario();
state.expand_mp_events();
state.expand_mp_options();
@ -65,56 +63,63 @@ config initial_level_config(saved_game& state)
}
config& scenario = state.get_starting_pos();
if(!state.mp_settings().saved_game)
{
if(!state.mp_settings().saved_game) {
state.set_random_seed();
}
if (scenario["objectives"].empty()) {
scenario["objectives"] = "<big>" + t_string(N_("Victory:"), "wesnoth") +
"</big>\n<span foreground=\"#00ff00\">&#8226; " +
t_string(N_("Defeat enemy leader(s)"), "wesnoth") + "</span>";
if(scenario["objectives"].empty()) {
// Generic victory objectives.
std::ostringstream ss;
ss << "<big>";
ss << t_string(N_("Victory:"), "wesnoth") << "</big>\n";
ss << "<span color='#00ff00'>" << font::unicode_bullet << " ";
ss << t_string(N_("Defeat enemy leader(s)"), "wesnoth") << "</span>";
scenario["objectives"] = ss.str();
}
config level = state.to_config();
add_multiplayer_classification(level.child_or_add("multiplayer"), state);
// [multiplayer] mp_era= should be persistent over saves.
std::string era = params.mp_era;
//[multiplayer] mp_era= should be persistent over saves.
//[era], [modification]s are toplevel tags here, they are not part of the saved_game and only used during mp_connect/mp_wait
// Initialize the list of sides available for the current era.
// We also need this no not get a segfault in mp_connect for ai configuration
const config &era_cfg =
game_config_manager::get()->game_config().find_child("era", "id", era);
if (!era_cfg) {
if (!params.saved_game)
{
utils::string_map i18n_symbols;
i18n_symbols["era"] = era;
throw config::error(vgettext("Cannot find era $era", i18n_symbols));
/**
* [era] and [modification]s are toplevel tags here.
* They are not part of the saved_game and are only used during mp_staging/mp_join_game.
*
* @todo: see if all the comments ai algorithms are still up-to-date and relevant.
*
* -- vultraz, 2017-11-24
*/
const config& game_config = game_config_manager::get()->game_config();
const config& era_cfg = game_config.find_child("era", "id", era);
if(!era_cfg) {
if(!params.saved_game) {
throw config::error(vgettext("Cannot find era $era", {{"era", era}}));
}
// FIXME: @todo We should tell user about missing era but still load game
// FIXME: @todo We should tell user about missing era but still load game...
WRN_CF << "Missing era in MP load game " << era << std::endl;
//Otherwise we get an error when qwhen we try to add ai algirithms in moultiplayer_connect
// Otherwise we get an error when when we try to add ai algorithms in mp_staging.
level.add_child("era");
}
else
{
/*config& cfg = */level.add_child("era", era_cfg);
} else {
level.add_child("era", era_cfg);
const config& custom_side = game_config_manager::get()->
game_config().find_child("multiplayer_side", "id", "Custom");
// Initialize the list of sides available for the current era.
// We also need this so not to get a segfault in mp_staging for ai configuration.
const config& custom_side = game_config.find_child("multiplayer_side", "id", "Custom");
level.child("era").add_child_at("multiplayer_side", custom_side, 0);
}
// Add modifications, needed for ai aglorithms which are applied in mp_connect
// Add modifications, needed for ai algorithms which are applied in mp_staging.
const std::vector<std::string>& mods = params.active_mods;
for (unsigned i = 0; i < mods.size(); i++) {
/*config& cfg = */level.add_child("modification",
game_config_manager::get()->
game_config().find_child("modification", "id", mods[i]));
for(unsigned i = 0; i < mods.size(); ++i) {
level.add_child("modification", game_config.find_child("modification", "id", mods[i]));
}
// This will force connecting clients to be using the same version number as us.
@ -133,14 +138,13 @@ void level_to_gamestate(const config& level, saved_game& state)
void check_response(bool res, const config& data)
{
if (!res) {
if(!res) {
throw wesnothd_error(_("Connection timed out"));
}
if (const config& err = data.child("error")) {
if(const config& err = data.child("error")) {
throw wesnothd_error(err["message"]);
}
}
} // end namespace mp