The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
connect_engine.hpp
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 #ifndef MULTIPLAYER_CONNECT_ENGINE_HPP_INCLUDED
15 #define MULTIPLAYER_CONNECT_ENGINE_HPP_INCLUDED
16 
17 #include "commandline_options.hpp"
18 #include "config.hpp"
19 #include "flg_manager.hpp"
20 #include "saved_game.hpp"
21 #include "multiplayer_ui.hpp"
22 #include "saved_game.hpp"
23 #include <boost/scoped_ptr.hpp>
24 #include <set>
25 
26 namespace rand_rng { class mt_rng; }
27 struct mp_campaign_info;
28 
29 namespace ng {
30 
31 enum controller {
38 };
39 
40 class connect_engine;
42 
43 typedef boost::scoped_ptr<connect_engine> connect_engine_ptr;
45 typedef std::pair<ng::controller, std::string> controller_option;
46 
48 {
49 public:
50  /// @param players the player which are already connected to the current game.
51  /// This is always empty unless we advance form a previous scenario.
52  connect_engine(saved_game& state,
53  const bool first_scenario, mp_campaign_info* campaign_info);
55 
57 
58  void import_user(const std::string& name, const bool observer,
59  int side_taken = -1);
60  void import_user(const config& data, const bool observer,
61  int side_taken = -1);
62 
63  // Returns true if there are still sides available for this game.
64  bool sides_available() const;
65 
66  // Import all sides into the level.
67  void update_level();
68  // Updates the level and sends a diff to the clients.
69  void update_and_send_diff(bool update_time_of_day = false);
70 
71  bool can_start_game() const;
72  void start_game();
73  void start_game_commandline(const commandline_options& cmdline_opts);
74 
75  // Return pair first element specifies whether to leave the game
76  // and second element whether to silently update UI.
77  std::pair<bool, bool> process_network_data(const config& data);
78 
79  // Returns the side which is taken by a given user,
80  // or -1 if none was found.
81  int find_user_side_index_by_id(const std::string& id) const;
82 
83 
84  /* Setters & Getters */
85 
86  const config& level() const { return level_; }
88  {
89  if(config& scenario = level_.child("scenario"))
90  return scenario;
91  else if(config& snapshot = level_.child("snapshot"))
92  return snapshot;
93  else
94  throw "No scenariodata found";
95  }
96  const std::set<std::string>& connected_users() const;
97  const std::vector<std::string>& user_team_names()
98  { return user_team_names_; }
99  std::vector<side_engine_ptr>& side_engines() { return side_engines_; }
100  const mp_game_settings& params() const { return params_; }
101  bool first_scenario() const { return first_scenario_; }
102  bool force_lock_settings() const { return force_lock_settings_; }
103 
104 private:
106  void operator=(const connect_engine&);
107 
108  void send_level_data() const;
109 
112 
114 
115  friend class side_engine;
116 
119 
121 
124  const bool first_scenario_;
125 
127 
128  std::vector<side_engine_ptr> side_engines_;
129  std::vector<const config*> era_factions_;
130  std::vector<std::string> team_names_;
131  std::vector<std::string> user_team_names_;
132  std::vector<std::string> player_teams_;
133 
134  std::set<std::string>& connected_users_rw();
135  void send_to_server(const config& cfg) const;
136  bool receive_from_server(config& dst) const;
137 };
138 
140 {
141 public:
142  side_engine(const config& cfg, connect_engine& parent_engine,
143  const int index);
144  ~side_engine();
145 
146  // An untranslated user_description which is used by other clients
147  // An empty string means the other clients should generate the description on their own
148  // Used by new_config().
150 
151  // Creates a config representing this side.
152  config new_config() const;
153 
154  // Returns true, if the player has chosen his/her leader and this side
155  // is ready for the game to start.
156  bool ready_for_start() const;
157  // Returns true if this side is waiting for a network player and
158  // players are allowed.
159  bool available_for_user(const std::string& name = "") const;
160 
161  bool swap_sides_on_drop_target(const unsigned drop_target);
162 
163  void resolve_random( rand_rng::mt_rng & rng, const std::vector<std::string> & avoid_faction_ids = std::vector<std::string>());
164 
165  // Resets this side to its default state.
166  void reset();
167 
168  // Place user into this side.
169  void place_user(const std::string& name);
170  void place_user(const config& data, bool contains_selection = false);
171 
174  bool controller_changed(const int selection);
176 
177  // Game set up from command line helpers.
178  void set_faction_commandline(const std::string& faction_name);
179  void set_controller_commandline(const std::string& controller_name);
180 
181  /* Setters & Getters */
182 
184  { return (!cfg_["save_id"].empty()) ? cfg_["save_id"] : cfg_["id"]; }
185  // The id of the side of the previous scenario that should control this side.
187  { return (!cfg_["previous_save_id"].empty()) ? cfg_["previous_save_id"] : save_id(); }
188  const std::vector<controller_option>& controller_options()
189  { return controller_options_; }
190  const config& cfg() const { return cfg_; }
192  unsigned current_controller_index() const
193  { return current_controller_index_; }
194  int index() const { return index_; }
195  void set_index(int index) { index_ = index; }
196  int team() const { return team_; }
197  void set_team(int team) { team_ = team; }
198  std::vector<std::string> get_children_to_swap();
199  std::multimap<std::string, config> get_side_children();
200  void set_side_children(std::multimap<std::string, config> children);
201  int color() const { return color_; }
202  void set_color(int color) { color_ = color; }
203  int gold() const { return gold_; }
204  void set_gold(int gold) { gold_ = gold; }
205  int income() const { return income_; }
206  void set_income(int income) { income_ = income; }
207  const std::string& player_id() const { return player_id_; }
208  const std::string& reserved_for() const { return reserved_for_; }
211  const std::string& ai_algorithm() const { return ai_algorithm_; }
214  bool allow_player() const { return allow_player_; }
215  bool allow_changes() const { return allow_changes_; }
218  bool allow_shuffle() const { return !disallow_shuffle_;}
219  const std::vector<std::string>& player_teams() const
220  { return parent_.player_teams_; }
221  flg_manager& flg() { return flg_; }
222 
223  std::vector<std::string> get_colors() const;
224  std::string get_color(int index = -1) const;
225  int num_colors() const;
226 private:
227  side_engine(const side_engine& engine);
228  void operator=(const side_engine&);
229 
230  void add_controller_option(ng::controller controller,
231  const std::string& name, const std::string& controller_value);
232 
235 
238  std::vector<controller_option> controller_options_;
239 
240  const bool allow_player_;
241  const bool controller_lock_;
242 
243  int index_;
244  int team_;
245  int color_;
246  int gold_;
247  int income_;
248  // set during create_engines constructor never set after that.
249  // the name of the player who is preferred for this side,
250  // if controller_ == reserved only this player can take this side.
251  // can also be a number of a side if this side shoudl be controlled
252  // by the player who controlls that side
256 
260  const bool allow_changes_;
263 };
264 
265 } // end namespace ng
266 
267 #endif
bool can_start_game() const
flg_manager & flg()
int gold() const
const std::string & ai_algorithm() const
void save_reserved_sides_information()
void set_waiting_to_choose_status(bool status)
void set_reserved_for(const std::string &reserved_for)
bool allow_changes() const
std::vector< std::string > player_teams_
std::string save_id() const
void import_user(const std::string &name, const bool observer, int side_taken=-1)
std::vector< side_engine_ptr > side_engines_
bool swap_sides_on_drop_target(const unsigned drop_target)
bool allow_shuffle() const
void add_controller_option(ng::controller controller, const std::string &name, const std::string &controller_value)
config new_config() const
void set_income(int income)
void place_user(const std::string &name)
std::string previous_save_id() const
void set_faction_commandline(const std::string &faction_name)
int index() const
void set_controller_commandline(const std::string &controller_name)
std::string ai_algorithm_
std::string player_id_
std::vector< std::string > user_team_names_
const config & level() const
const bool allow_changes_
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:32
std::vector< controller_option > controller_options_
Definitions for the interface to Wesnoth Markup Language (WML).
bool ready_for_start() const
const std::string & reserved_for() const
void update_and_send_diff(bool update_time_of_day=false)
const std::set< std::string > & connected_users() const
const config & cfg() const
bool sides_available() const
void set_color(int color)
std::string custom_color_
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
void operator=(const side_engine &)
const mp_game_settings & params() const
std::vector< side_engine_ptr > & side_engines()
bool waiting_to_choose_faction() const
bool first_scenario() const
void send_to_server(const config &cfg) const
const std::string & player_id() const
void set_controller(ng::controller controller)
void set_team(int team)
GLenum GLenum dst
Definition: glew.h:2392
boost::shared_ptr< side_engine > side_engine_ptr
std::string user_description() const
void start_game_commandline(const commandline_options &cmdline_opts)
std::vector< std::string > get_colors() const
void set_gold(int gold)
const bool first_scenario_
const std::vector< std::string > & user_team_names()
ng::controller controller() const
bool available_for_user(const std::string &name="") const
ng::controller controller_
std::vector< const config * > era_factions_
GLuint color
Definition: glew.h:5801
mp_campaign_info * campaign_info_
const ng::controller default_controller_
void operator=(const connect_engine &)
bool force_lock_settings() const
std::vector< std::string > get_children_to_swap()
void send_level_data() const
int team() const
int income() const
void update_controller_options()
unsigned current_controller_index() const
void set_ai_algorithm(const std::string &ai_algorithm)
bool allow_player() const
GLuint index
Definition: glew.h:1782
std::string reserved_for_
int color() const
const std::vector< controller_option > & controller_options()
connect_engine(saved_game &state, const bool first_scenario, mp_campaign_info *campaign_info)
bool receive_from_server(config &dst) const
int num_colors() const
void update_current_controller_index()
unsigned current_controller_index_
connect_engine & parent_
std::string observer
Definition: game_config.cpp:84
GLuint const GLchar * name
Definition: glew.h:1782
std::pair< ng::controller, std::string > controller_option
int find_user_side_index_by_id(const std::string &id) const
std::set< std::string > & connected_users_rw()
void set_index(int index)
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
const bool controller_lock_
bool controller_changed(const int selection)
void set_side_children(std::multimap< std::string, config > children)
const mp_game_settings & params_
std::string get_color(int index=-1) const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
boost::scoped_ptr< connect_engine > connect_engine_ptr
const bool allow_player_
void resolve_random(rand_rng::mt_rng &rng, const std::vector< std::string > &avoid_faction_ids=std::vector< std::string >())
std::vector< std::string > team_names_
side_engine(const config &cfg, connect_engine &parent_engine, const int index)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::multimap< std::string, config > get_side_children()
std::pair< bool, bool > process_network_data(const config &data)
const std::vector< std::string > & player_teams() const
void update_side_controller_options()