Move the initialization of the new level config to a different file.

This is initial step to unify the level initialization between
multiplayer_connect.cpp and playcampaign.cpp for multiplayer games.
This commit is contained in:
Andrius Silinskas 2013-07-15 19:03:46 +01:00
parent f58a8190b4
commit c00e94de18
6 changed files with 196 additions and 94 deletions

View File

@ -18938,6 +18938,14 @@
RelativePath="..\..\src\mp_game_settings.hpp"
>
</File>
<File
RelativePath="..\..\src\mp_game_utils.cpp"
>
</File>
<File
RelativePath="..\..\src\mp_game_utils.hpp"
>
</File>
<File
RelativePath="..\..\src\multiplayer.cpp"
>

View File

@ -768,6 +768,7 @@ set(wesnoth-main_SRC
movetype.cpp
mp_depcheck.cpp
mp_game_settings.cpp
mp_game_utils.cpp
multiplayer.cpp
multiplayer_configure.cpp
multiplayer_connect.cpp

View File

@ -436,6 +436,7 @@ wesnoth_sources = Split("""
movetype.cpp
mp_depcheck.cpp
mp_game_settings.cpp
mp_game_utils.cpp
multiplayer.cpp
multiplayer_configure.cpp
multiplayer_connect.cpp

143
src/mp_game_utils.cpp Normal file
View File

@ -0,0 +1,143 @@
/*
Copyright (C) 2013 by Andrius Silinskas <silinskas.andrius@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "mp_game_utils.hpp"
#include "config.hpp"
#include "formula_string_utils.hpp"
#include "gamestatus.hpp"
#include "game_config_manager.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "savegame.hpp"
#include "tod_manager.hpp"
#include "unit_id.hpp"
#include <boost/foreach.hpp>
static lg::log_domain log_config("config");
#define LOG_CF LOG_STREAM(info, log_config)
#define WRN_CF LOG_STREAM(warn, log_config)
#define ERR_CF LOG_STREAM(err, log_config)
namespace mp {
config initial_level_config(game_display& disp, mp_game_settings& params,
game_state& state)
{
config level;
if(params.saved_game) {
try{
savegame::loadgame load(disp,
resources::config_manager->game_config(), state);
load.load_multiplayer_game();
load.fill_mplevel_config(level);
}
catch (load_game_cancelled_exception){
return config();
}
} else {
level.clear();
params.mp_scenario = params.scenario_data["id"].str();
level.merge_with(params.scenario_data);
level["turns"] = params.num_turns;
level.add_child("multiplayer", params.to_config());
// Convert options to events
//FIXME
//level.add_child_at("event", mp::options::to_event(
// params.options.find_child("multiplayer", "id",
// params.mp_scenario)), 0);
params.hash = level.hash();
level["next_underlying_unit_id"] = 0;
n_unit::id_manager::instance().clear();
if (params.random_start_time)
{
if (!tod_manager::is_start_ToD(level["random_start_time"]))
{
level["random_start_time"] = true;
}
}
else
{
level["random_start_time"] = false;
}
level["experience_modifier"] = params.xp_modifier;
level["random_seed"] = state.carryover_sides_start["random_seed"];
}
std::string era = params.mp_era;
if (params.saved_game) {
if (const config &c = level.child("snapshot").child("era"))
era = c["id"].str();
}
// Initialize the list of sides available for the current era.
const config &era_cfg =
resources::config_manager->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));
}
// FIXME: @todo We should tell user about missing era but still load game
WRN_CF << "Missing era in MP load game " << era << "\n";
}
else
{
/*config& cfg =*/
level.add_child("era", era_cfg);
// Convert options to event
//FIXME
//cfg.add_child_at("event", mp::options::to_event(
// params.options.find_child("era", "id", era)), 0);
}
// Add modifications
const std::vector<std::string>& mods = params.active_mods;
for (unsigned i = 0; i < mods.size(); i++) {
/*config& cfg = */
level.add_child("modification",
resources::config_manager->
game_config().find_child("modification", "id", mods[i]));
// Convert options to event
//FIXME
//cfg.add_child_at("event", mp::options::to_event(
// params.options.find_child("modification", "id", mods[i])), 0);
}
// This will force connecting clients to be using the same version number as us.
level["version"] = game_config::version;
level["observer"] = params.allow_observers;
level["shuffle_sides"] = params.shuffle_sides;
if(level["objectives"].empty()) {
level["objectives"] = "<big>" + t_string(N_("Victory:"), "wesnoth") +
"</big>\n<span foreground=\"#00ff00\">&#8226; " +
t_string(N_("Defeat enemy leader(s)"), "wesnoth") + "</span>";
}
return level;
}
} // end namespace mp

31
src/mp_game_utils.hpp Normal file
View File

@ -0,0 +1,31 @@
/*
Copyright (C) 2013 by Andrius Silinskas <silinskas.andrius@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef MP_GAME_UTILS_HPP_INCLUDED
#define MP_GAME_UTILS_HPP_INCLUDED
#include "game_display.hpp"
#include "mp_game_settings.hpp"
class config;
class game_state;
namespace mp {
config initial_level_config(game_display& disp, mp_game_settings& params,
game_state& state);
} // end namespace mp
#endif

View File

@ -27,6 +27,7 @@
#include "gettext.hpp"
#include "log.hpp"
#include "map.hpp"
#include "mp_game_utils.hpp"
#include "multiplayer_connect.hpp"
#include "savegame.hpp"
#include "statistics.hpp"
@ -1716,111 +1717,28 @@ void connect::load_game()
{
DBG_MP << "loading game parameters" << std::endl;
if(params_.saved_game) {
try{
savegame::loadgame load(disp(), game_config(), state_);
load.load_multiplayer_game();
load.fill_mplevel_config(level_);
}
catch (load_game_cancelled_exception){
set_result(CREATE);
return;
}
} else {
level_.clear();
params_.saved_game = false;
params_.mp_scenario = params_.scenario_data["id"].str();
level_.merge_with(params_.scenario_data);
level_["turns"] = params_.num_turns;
level_.add_child("multiplayer", params_.to_config());
level_ = initial_level_config(disp(), params_, state_);
// Convert options to events
//FIXME
// level_.add_child_at("event", mp::options::to_event(params_.options
// .find_child("multiplayer", "id", params_.mp_scenario)), 0);
// Load game exception occured.
if (level_.empty()) {
set_result(CREATE);
return;
}
params_.hash = level_.hash();
level_["next_underlying_unit_id"] = 0;
n_unit::id_manager::instance().clear();
if (!params_.saved_game) {
era_sides_.clear();
BOOST_FOREACH(const config &e,
level_.child("era").child_range("multiplayer_side")) {
if (params_.random_start_time)
{
if (!tod_manager::is_start_ToD(level_["random_start_time"]))
{
level_["random_start_time"] = true;
}
era_sides_.push_back(&e);
}
else
{
level_["random_start_time"] = false;
}
level_["experience_modifier"] = params_.xp_modifier;
level_["random_seed"] = state_.carryover_sides_start["random_seed"];
}
// Add the map name to the title.
append_to_title("" + level_["name"].t_str());
std::string era = params_.mp_era;
if (params_.saved_game) {
if (const config &c = level_.child("snapshot").child("era"))
era = c["id"].str();
}
// Initialize the list of sides available for the current era.
const config &era_cfg = 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));
}
// FIXME: @todo We should tell user about missing era but still load game
WRN_CF << "Missing era in MP load game " << era << "\n";
}
else
{
era_sides_.clear();
BOOST_FOREACH(const config &e, era_cfg.child_range("multiplayer_side")) {
era_sides_.push_back(&e);
}
/*config& cfg =*/ level_.add_child("era", era_cfg);
// Convert options to event
//FIXME
// cfg.add_child_at("event", mp::options::to_event
// (params_.options.find_child("era", "id", era)), 0);
}
// Add modifications
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().find_child("modification", "id", mods[i]));
// Convert options to event
//FIXME
// cfg.add_child_at("event", mp::options::to_event
// (params_.options.find_child("modification", "id", mods[i])), 0);
}
gold_title_label_.hide(params_.saved_game);
income_title_label_.hide(params_.saved_game);
// This will force connecting clients to be using the same version number as us.
level_["version"] = game_config::version;
level_["observer"] = params_.allow_observers;
level_["shuffle_sides"] = params_.shuffle_sides;
if(level_["objectives"].empty()) {
level_["objectives"] = "<big>" + t_string(N_("Victory:"), "wesnoth") +
"</big>\n<span foreground=\"#00ff00\">&#8226; " +
t_string(N_("Defeat enemy leader(s)"), "wesnoth") + "</span>";
}
}
config* connect::current_config(){