The Battle for Wesnoth  1.13.10+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 - 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 */
14 
15 #pragma once
16 
17 #include "commandline_options.hpp"
18 #include "config.hpp"
19 #include "flg_manager.hpp"
20 #include "saved_game.hpp"
21 #include <set>
22 
23 namespace randomness { class mt_rng; }
24 struct mp_campaign_info;
25 
26 namespace ng {
27 
28 enum controller {
34 };
35 
36 class connect_engine;
38 
39 typedef const std::unique_ptr<connect_engine> connect_engine_ptr;
40 typedef std::shared_ptr<side_engine> side_engine_ptr;
41 typedef std::pair<ng::controller, std::string> controller_option;
42 
44 {
45 public:
48 
50 
51  void import_user(const std::string& name, const bool observer,
52  int side_taken = -1);
53  void import_user(const config& data, const bool observer,
54  int side_taken = -1);
55 
56  // Returns true if there are still sides available for this game.
57  bool sides_available() const;
58 
59  // Import all sides into the level.
60  void update_level();
61  // Updates the level and sends a diff to the clients.
62  void update_and_send_diff(bool update_time_of_day = false);
63 
64  bool can_start_game() const;
65  void start_game();
66  void start_game_commandline(const commandline_options& cmdline_opts);
67 
68  void leave_game();
69 
70  // Return pair first element specifies whether to leave the game
71  // and second element whether to silently update UI.
72  std::pair<bool, bool> process_network_data(const config& data);
73 
74  // Returns the side which is taken by a given user,
75  // or -1 if none was found.
76  int find_user_side_index_by_id(const std::string& id) const;
77 
78 
79  /* Setters & Getters */
80 
81  const config& level() const { return level_; }
83  {
84  if(config& scenario = level_.child("scenario"))
85  return scenario;
86  else if(config& snapshot = level_.child("snapshot"))
87  return snapshot;
88  else
89  throw "No scenariodata found";
90  }
91  const std::set<std::string>& connected_users() const;
92  const std::vector<std::string>& user_team_names() const
93  { return user_team_names_; }
94  std::vector<side_engine_ptr>& side_engines() { return side_engines_; }
95  const mp_game_settings& params() const { return params_; }
96  bool first_scenario() const { return first_scenario_; }
97  bool force_lock_settings() const { return force_lock_settings_; }
98 
99  bool receive_from_server(config& dst) const;
100 
102  {
103  return campaign_info_;
104  }
105 
106 private:
107  connect_engine(const connect_engine&) = delete;
108  void operator=(const connect_engine&) = delete;
109 
110  void send_level_data() const;
111 
114 
116 
117  friend class side_engine;
118 
121 
123 
126  const bool first_scenario_;
127 
129 
130  std::vector<side_engine_ptr> side_engines_;
131  std::vector<const config*> era_factions_;
132  std::vector<std::string> team_names_;
133  std::vector<std::string> user_team_names_;
134  std::vector<std::string> player_teams_;
135 
136  std::set<std::string>& connected_users_rw();
137  void send_to_server(const config& cfg) const;
138 };
139 
141 {
142 public:
143  side_engine(const config& cfg, connect_engine& parent_engine,
144  const int index);
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  void resolve_random( randomness::mt_rng & rng, const std::vector<std::string> & avoid_faction_ids = std::vector<std::string>());
162 
163  // Resets this side to its default state.
164  void reset();
165 
166  // Place user into this side.
167  void place_user(const std::string& name);
168  void place_user(const config& data, bool contains_selection = false);
169 
172  bool controller_changed(const int selection);
174 
175  // Game set up from command line helpers.
176  void set_faction_commandline(const std::string& faction_name);
178 
179  /* Setters & Getters */
180 
182  { return (!cfg_["save_id"].empty()) ? cfg_["save_id"] : cfg_["id"]; }
183  // The id of the side of the previous scenario that should control this side.
185  { return (!cfg_["previous_save_id"].empty()) ? cfg_["previous_save_id"] : save_id(); }
186  const std::vector<controller_option>& controller_options() const
187  { return controller_options_; }
188  const config& cfg() const { return cfg_; }
190  unsigned current_controller_index() const
191  { return current_controller_index_; }
192  int index() const { return index_; }
193  void set_index(int index) { index_ = index; }
194  int team() const { return team_; }
195  void set_team(int team) { team_ = team; }
196  std::vector<std::string> get_children_to_swap();
197  std::multimap<std::string, config> get_side_children();
198  void set_side_children(std::multimap<std::string, config> children);
199  int color() const { return color_; }
201  int gold() const { return gold_; }
202  void set_gold(int gold) { gold_ = gold; }
203  int income() const { return income_; }
204  void set_income(int income) { income_ = income; }
205  const std::string& player_id() const { return player_id_; }
206  const std::string& reserved_for() const { return reserved_for_; }
209  const std::string& ai_algorithm() const { return ai_algorithm_; }
212  bool allow_player() const { return allow_player_; }
213  bool allow_changes() const { return allow_changes_; }
216  bool allow_shuffle() const { return !disallow_shuffle_;}
217  const std::vector<std::string>& player_teams() const
218  { return parent_.player_teams_; }
219  flg_manager& flg() { return flg_; }
220 
221  const std::string color_id() const { return color_id_; }
222  const std::vector<std::string>& color_options() const { return color_options_; }
223 
224  const std::string team_name() const
225  {
226  return parent_.team_names_[team_];
227  }
228 
230  {
232  }
233 
234 private:
235  side_engine(const side_engine& engine) = delete;
236  void operator=(const side_engine&) = delete;
237 
238  void add_controller_option(ng::controller controller,
239  const std::string& name, const std::string& controller_value);
240 
243 
246  std::vector<controller_option> controller_options_;
247 
248  const bool allow_player_;
249  const bool controller_lock_;
250 
251  int index_;
252  int team_;
253  int color_;
254  int gold_;
255  int income_;
256  // set during create_engines constructor never set after that.
257  // the name of the player who is preferred for this side,
258  // if controller_ == reserved only this player can take this side.
259  // can also be a number of a side if this side shoudl be controlled
260  // by the player who controlls that side
264 
268  const bool allow_changes_;
270 
271  std::vector<std::string> color_options_;
272 
274 };
275 
276 } // end namespace ng
bool can_start_game() const
flg_manager & flg()
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:400
int gold() const
std::vector< char_t > string
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)
void operator=(const connect_engine &)=delete
std::vector< side_engine_ptr > side_engines_
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_
const std::vector< controller_option > & controller_options() const
std::string player_id_
std::vector< std::string > user_team_names_
const config & level() const
const bool allow_changes_
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:28
std::vector< controller_option > controller_options_
Definitions for the interface to Wesnoth Markup Language (WML).
bool ready_for_start() const
const std::string color_id() const
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid_faction_ids=std::vector< std::string >())
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)
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
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)
const std::string user_team_name() const
std::string user_description() const
void start_game_commandline(const commandline_options &cmdline_opts)
std::vector< std::string > color_options_
void set_gold(int gold)
const bool first_scenario_
ng::controller controller() const
bool available_for_user(const std::string &name="") const
ng::controller controller_
std::vector< const config * > era_factions_
mp_campaign_info * campaign_info_
const ng::controller default_controller_
const std::vector< std::string > & user_team_names() const
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
std::string reserved_for_
int color() const
connect_engine(saved_game &state, const bool first_scenario, mp_campaign_info *campaign_info)
bool receive_from_server(config &dst) const
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:149
void update_current_controller_index()
unsigned current_controller_index_
connect_engine & parent_
std::string observer
std::shared_ptr< side_engine > side_engine_ptr
const std::vector< std::string > & color_options() const
void operator=(const side_engine &)=delete
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)
const bool controller_lock_
bool controller_changed(const int selection)
void set_side_children(std::multimap< std::string, config > children)
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
const mp_game_settings & params_
const std::unique_ptr< connect_engine > connect_engine_ptr
std::string color_id_
const mp_campaign_info * campaign_info() const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
const bool allow_player_
std::vector< std::string > team_names_
side_engine(const config &cfg, connect_engine &parent_engine, const int index)
const std::string team_name() const
std::multimap< std::string, config > get_side_children()
static std::string controller_name(const team &t)
Definition: game_stats.cpp:77
std::pair< bool, bool > process_network_data(const config &data)
const std::vector< std::string > & player_teams() const
void update_side_controller_options()