The Battle for Wesnoth  1.13.4+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 - 2016 by Andrius Silinskas <[email protected]>
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 */
14 #include "mp_game_utils.hpp"
15 
16 #include "carryover.hpp"
17 #include "config.hpp"
18 #include "dialogs.hpp"
19 #include "formula/string_utils.hpp"
20 #include "saved_game.hpp"
21 #include "game_config.hpp"
22 #include "game_config_manager.hpp"
23 #include "gettext.hpp"
24 #include "log.hpp"
25 #include "mp_options.hpp"
26 #include "savegame.hpp"
27 #include "units/id.hpp"
29 
30 static lg::log_domain log_engine("engine");
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 #define ERR_NG LOG_STREAM(err, log_engine)
33 
34 static lg::log_domain log_config("config");
35 #define LOG_CF LOG_STREAM(info, log_config)
36 #define WRN_CF LOG_STREAM(warn, log_config)
37 #define ERR_CF LOG_STREAM(err, log_config)
38 
39 static lg::log_domain log_network("network");
40 #define LOG_NW LOG_STREAM(info, log_network)
41 #define ERR_NW LOG_STREAM(err, log_network)
42 
43 namespace mp {
44 
45 // To remove radundant informaion in the clientside internal programmflow
46 // I want to remove these values from mp_settings so i need to readd them here
47 static void add_multiplayer_classification(config& multiplayer, saved_game& state)
48 {
49  multiplayer["mp_scenario"] = state.get_scenario_id();
50  multiplayer["mp_scenario_name"] = state.get_starting_pos()["name"];
51  multiplayer["difficulty_define"] = state.classification().difficulty;
52  multiplayer["mp_campaign"] = state.classification().campaign;
53 }
54 
56 {
57  const mp_game_settings& params = state.mp_settings();
58  state.set_defaults();
59  //Also impliers state.expand_scenario()
60  //We need to call this before expand_mp_events/options oterwise they might be overwritten
61  state.expand_random_scenario();
62  state.expand_mp_events();
63  state.expand_mp_options();
64 
65  config& scenario = state.get_starting_pos();
66  if(!state.mp_settings().saved_game)
67  {
68  state.set_random_seed();
69  }
70 
71  if (scenario["objectives"].empty()) {
72  scenario["objectives"] = "<big>" + t_string(N_("Victory:"), "wesnoth") +
73  "</big>\n<span foreground=\"#00ff00\">&#8226; " +
74  t_string(N_("Defeat enemy leader(s)"), "wesnoth") + "</span>";
75  }
76 
77  config level = state.to_config();
78  add_multiplayer_classification(level.child_or_add("multiplayer"), state);
79 
80  std::string era = params.mp_era;
81  //[multiplayer] mp_era= should be persistent over saves.
82 
83  //[era], [modification]s are toplevel tags here, they are not part of the saved_game and only used during mp_connect/mp_wait
84  // Initialize the list of sides available for the current era.
85  // We also need this no not get a segfault in mp_connect for ai configuration
86  const config &era_cfg =
87  game_config_manager::get()->game_config().find_child("era", "id", era);
88  if (!era_cfg) {
89  if (!params.saved_game)
90  {
91  utils::string_map i18n_symbols;
92  i18n_symbols["era"] = era;
93  throw config::error(vgettext("Cannot find era $era", i18n_symbols));
94  }
95  // FIXME: @todo We should tell user about missing era but still load game
96  WRN_CF << "Missing era in MP load game " << era << std::endl;
97  //Otherwise we get an error when qwhen we try to add ai algirithms in moultiplayer_connect
98  level.add_child("era");
99  }
100  else
101  {
102  /*config& cfg = */level.add_child("era", era_cfg);
103 
104  const config& custom_side = game_config_manager::get()->
105  game_config().find_child("multiplayer_side", "id", "Custom");
106  level.child("era").add_child_at("multiplayer_side", custom_side, 0);
107 
108  }
109  // Add modifications, needed for ai aglorithms which are applied in mp_connect
110 
111  const std::vector<std::string>& mods = params.active_mods;
112  for (unsigned i = 0; i < mods.size(); i++) {
113  /*config& cfg = */level.add_child("modification",
115  game_config().find_child("modification", "id", mods[i]));
116  }
117 
118  // This will force connecting clients to be using the same version number as us.
119  level["version"] = game_config::version;
120  return level;
121 }
122 
124 {
125  game_classification::CAMPAIGN_TYPE type = state.classification().campaign_type;
126  bool show_connect = state.mp_settings().show_connect;
127  state = saved_game(level);
128  state.classification().campaign_type = type;
129  state.mp_settings().show_connect = show_connect;
130 }
131 
132 void check_response(bool res, const config& data)
133 {
134  if (!res) {
135  throw wesnothd_error(_("Connection timed out"));
136  }
137 
138  if (const config& err = data.child("error")) {
139  throw wesnothd_error(err["message"]);
140  }
141 }
142 
143 } // end namespace mp
144 
An error occured during when trying to coommunicate with the wesnothd server.
std::string era()
GLint level
Definition: glew.h:1220
config to_config() const
Definition: saved_game.cpp:510
static lg::log_domain log_config("config")
void set_defaults()
does some post loading stuff must be used before passing the data to connect_engine ...
Definition: saved_game.cpp:167
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
config & child_or_add(const std::string &key)
Returns a reference to the first child with the given key.
Definition: config.cpp:734
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:387
const GLfloat * params
Definition: glew.h:1499
void level_to_gamestate(const config &level, saved_game &state)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Definitions for the interface to Wesnoth Markup Language (WML).
#define WRN_CF
This module controls the multiplayer lobby.
std::vector< std::string > active_mods
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
std::map< std::string, t_string > string_map
void set_random_seed()
sets the random seed if that didn't already happen.
Definition: saved_game.cpp:119
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:356
config & add_child(const std::string &key)
Definition: config.cpp:743
void check_response(bool res, const config &data)
std::string campaign
the campaign being played
config & get_starting_pos()
Definition: saved_game.cpp:472
GLuint res
Definition: glew.h:9258
logger & err()
Definition: log.cpp:79
Game configuration data as global variables.
Definition: build_info.cpp:38
const config & game_config() const
std::string get_scenario_id()
Definition: saved_game.cpp:533
size_t i
Definition: function.cpp:1057
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:321
#define N_(String)
Definition: gettext.hpp:90
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:54
config & child(const std::string &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:658
config & find_child(const std::string &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:1010
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:58
static lg::log_domain log_network("network")
const std::string version
Definition: game_config.cpp:48
GLsizei const GLcharARB ** string
Definition: glew.h:4503
static lg::log_domain log_engine("engine")
config & add_child_at(const std::string &key, const config &val, unsigned index)
Definition: config.cpp:773
static void add_multiplayer_classification(config &multiplayer, saved_game &state)