The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mp_game_utils.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2017 by Andrius Silinskas <silinskas.andrius@gmail.com>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
15 
16 #include "carryover.hpp"
17 #include "config.hpp"
18 #include "formula/string_utils.hpp"
19 #include "saved_game.hpp"
20 #include "game_config.hpp"
21 #include "game_config_manager.hpp"
22 #include "gettext.hpp"
23 #include "log.hpp"
24 #include "savegame.hpp"
25 #include "units/id.hpp"
27 
28 static lg::log_domain log_engine("engine");
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 #define ERR_NG LOG_STREAM(err, log_engine)
31 
32 static lg::log_domain log_config("config");
33 #define LOG_CF LOG_STREAM(info, log_config)
34 #define WRN_CF LOG_STREAM(warn, log_config)
35 #define ERR_CF LOG_STREAM(err, log_config)
36 
37 static lg::log_domain log_network("network");
38 #define LOG_NW LOG_STREAM(info, log_network)
39 #define ERR_NW LOG_STREAM(err, log_network)
40 
41 namespace mp {
42 
43 // This is for the wesnothd server, it expects a more detailed summary in [multiplayer]
44 static void add_multiplayer_classification(config& multiplayer, saved_game& state)
45 {
46  multiplayer["mp_scenario"] = state.get_scenario_id();
47  multiplayer["mp_scenario_name"] = state.get_starting_pos()["name"];
48  multiplayer["difficulty_define"] = state.classification().difficulty;
49  multiplayer["mp_campaign"] = state.classification().campaign;
50 }
51 
53 {
54  const mp_game_settings& params = state.mp_settings();
55  state.set_defaults();
56  //Also impliers state.expand_scenario()
57  //We need to call this before expand_mp_events/options oterwise they might be overwritten
58  state.expand_random_scenario();
59  state.expand_mp_events();
60  state.expand_mp_options();
61 
62  if(!state.valid()) {
63  throw config::error("Failed to load the scenario");
64  }
65 
66  config& scenario = state.get_starting_pos();
67  if(!state.mp_settings().saved_game)
68  {
69  state.set_random_seed();
70  }
71 
72  if (scenario["objectives"].empty()) {
73  scenario["objectives"] = "<big>" + t_string(N_("Victory:"), "wesnoth") +
74  "</big>\n<span foreground=\"#00ff00\">&#8226; " +
75  t_string(N_("Defeat enemy leader(s)"), "wesnoth") + "</span>";
76  }
77 
78  config level = state.to_config();
79  add_multiplayer_classification(level.child_or_add("multiplayer"), state);
80 
81  std::string era = params.mp_era;
82  //[multiplayer] mp_era= should be persistent over saves.
83 
84  //[era], [modification]s are toplevel tags here, they are not part of the saved_game and only used during mp_connect/mp_wait
85  // Initialize the list of sides available for the current era.
86  // We also need this no not get a segfault in mp_connect for ai configuration
87  const config &era_cfg =
88  game_config_manager::get()->game_config().find_child("era", "id", era);
89  if (!era_cfg) {
90  if (!params.saved_game)
91  {
92  utils::string_map i18n_symbols;
93  i18n_symbols["era"] = era;
94  throw config::error(vgettext("Cannot find era $era", i18n_symbols));
95  }
96  // FIXME: @todo We should tell user about missing era but still load game
97  WRN_CF << "Missing era in MP load game " << era << std::endl;
98  //Otherwise we get an error when qwhen we try to add ai algirithms in moultiplayer_connect
99  level.add_child("era");
100  }
101  else
102  {
103  /*config& cfg = */level.add_child("era", era_cfg);
104 
105  const config& custom_side = game_config_manager::get()->
106  game_config().find_child("multiplayer_side", "id", "Custom");
107  level.child("era").add_child_at("multiplayer_side", custom_side, 0);
108 
109  }
110  // Add modifications, needed for ai aglorithms which are applied in mp_connect
111 
112  const std::vector<std::string>& mods = params.active_mods;
113  for (unsigned i = 0; i < mods.size(); i++) {
114  /*config& cfg = */level.add_child("modification",
116  game_config().find_child("modification", "id", mods[i]));
117  }
118 
119  // This will force connecting clients to be using the same version number as us.
120  level["version"] = game_config::version;
121  return level;
122 }
123 
125 {
126  game_classification::CAMPAIGN_TYPE type = state.classification().campaign_type;
127  bool show_connect = state.mp_settings().show_connect;
128  state = saved_game(level);
129  state.classification().campaign_type = type;
130  state.mp_settings().show_connect = show_connect;
131 }
132 
133 void check_response(bool res, const config& data)
134 {
135  if (!res) {
136  throw wesnothd_error(_("Connection timed out"));
137  }
138 
139  if (const config& err = data.child("error")) {
140  throw wesnothd_error(err["message"]);
141  }
142 }
143 
144 } // end namespace mp
145 
An error occured during when trying to coommunicate with the wesnothd server.
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:352
std::vector< char_t > string
std::map< std::string, t_string > string_map
std::string era()
Definition: game.cpp:700
config to_config() const
Definition: saved_game.cpp:531
static lg::log_domain log_config("config")
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:715
void set_defaults()
does some post loading stuff must be used before passing the data to connect_engine ...
Definition: saved_game.cpp:177
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
config initial_level_config(saved_game &state)
void expand_random_scenario()
takes care of generate_map=, generate_scenario=, map= attributes This should be called before expandi...
Definition: saved_game.cpp:401
config & child_or_add(config_key_type key)
Definition: config.cpp:398
void level_to_gamestate(const config &level, saved_game &state)
Definitions for the interface to Wesnoth Markup Language (WML).
#define WRN_CF
Pubic entry points for the MP workflow.
Definition: lobby_data.cpp:48
std::vector< std::string > active_mods
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
void set_random_seed()
sets the random seed if that didn't already happen.
Definition: saved_game.cpp:130
void expand_mp_options()
adds values of [option]s into [carryover_sides_start][variables] so that they are applied in the next...
Definition: saved_game.cpp:360
config & add_child_at(config_key_type key, const config &val, unsigned index)
Definition: config.cpp:440
void check_response(bool res, const config &data)
std::string campaign
the campaign being played
config & get_starting_pos()
Definition: saved_game.cpp:488
logger & err()
Definition: log.cpp:79
Game configuration data as global variables.
Definition: build_info.cpp:53
const config & game_config() const
std::string get_scenario_id()
Definition: saved_game.cpp:554
#define i
void expand_mp_events()
adds [event]s from [era] and [modification] into this scenario does NOT expand [option]s because vari...
Definition: saved_game.cpp:316
#define N_(String)
Definition: gettext.hpp:97
bool valid() const
Definition: saved_game.cpp:459
config & add_child(config_key_type key)
Definition: config.cpp:408
std::string difficulty
The difficulty level the game is being played on.
std::string vgettext(const char *msgid, const utils::string_map &symbols)
game_classification & classification()
Definition: saved_game.hpp:55
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:59
static lg::log_domain log_network("network")
const std::string version
Definition: game_config.cpp:39
static lg::log_domain log_engine("engine")
static void add_multiplayer_classification(config &multiplayer, saved_game &state)