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 {
35 };
36 
37 class connect_engine;
39 
40 typedef const std::unique_ptr<connect_engine> connect_engine_ptr;
41 typedef std::shared_ptr<side_engine> side_engine_ptr;
42 typedef std::pair<ng::controller, std::string> controller_option;
43 
45 {
46 public:
49 
51 
52  void import_user(const std::string& name, const bool observer,
53  int side_taken = -1);
54  void import_user(const config& data, const bool observer,
55  int side_taken = -1);
56 
57  // Returns true if there are still sides available for this game.
58  bool sides_available() const;
59 
60  // Import all sides into the level.
61  void update_level();
62  // Updates the level and sends a diff to the clients.
63  void update_and_send_diff(bool update_time_of_day = false);
64 
65  bool can_start_game() const;
66  void start_game();
67  void start_game_commandline(const commandline_options& cmdline_opts);
68 
69  void leave_game();
70 
71  // Return pair first element specifies whether to leave the game
72  // and second element whether to silently update UI.
73  std::pair<bool, bool> process_network_data(const config& data);
74 
75  // Returns the side which is taken by a given user,
76  // or -1 if none was found.
77  int find_user_side_index_by_id(const std::string& id) const;
78 
79 
80  /* Setters & Getters */
81 
82  const config& level() const { return level_; }
84  {
85  if(config& scenario = level_.child("scenario"))
86  return scenario;
87  else if(config& snapshot = level_.child("snapshot"))
88  return snapshot;
89  else
90  throw "No scenariodata found";
91  }
92  const std::set<std::string>& connected_users() const;
93  const std::vector<std::string>& user_team_names() const
94  { return user_team_names_; }
95  std::vector<side_engine_ptr>& side_engines() { return side_engines_; }
96  const mp_game_settings& params() const { return params_; }
97  bool first_scenario() const { return first_scenario_; }
98  bool force_lock_settings() const { return force_lock_settings_; }
99 
100  bool receive_from_server(config& dst) const;
101 
103  {
104  return campaign_info_;
105  }
106 
107 private:
108  connect_engine(const connect_engine&) = delete;
109  void operator=(const connect_engine&) = delete;
110 
111  void send_level_data() const;
112 
115 
117 
118  friend class side_engine;
119 
122 
124 
127  const bool first_scenario_;
128 
130 
131  std::vector<side_engine_ptr> side_engines_;
132  std::vector<const config*> era_factions_;
133  std::vector<std::string> team_names_;
134  std::vector<std::string> user_team_names_;
135  std::vector<std::string> player_teams_;
136 
137  std::set<std::string>& connected_users_rw();
138  void send_to_server(const config& cfg) const;
139 };
140 
142 {
143 public:
144  side_engine(const config& cfg, connect_engine& parent_engine,
145  const int index);
146 
147  // An untranslated user_description which is used by other clients
148  // An empty string means the other clients should generate the description on their own
149  // Used by new_config().
151 
152  // Creates a config representing this side.
153  config new_config() const;
154 
155  // Returns true, if the player has chosen his/her leader and this side
156  // is ready for the game to start.
157  bool ready_for_start() const;
158  // Returns true if this side is waiting for a network player and
159  // players are allowed.
160  bool available_for_user(const std::string& name = "") const;
161 
162  void resolve_random( randomness::mt_rng & rng, const std::vector<std::string> & avoid_faction_ids = std::vector<std::string>());
163 
164  // Resets this side to its default state.
165  void reset();
166 
167  // Place user into this side.
168  void place_user(const std::string& name);
169  void place_user(const config& data, bool contains_selection = false);
170 
173  bool controller_changed(const int selection);
175 
176  // Game set up from command line helpers.
177  void set_faction_commandline(const std::string& faction_name);
179 
180  /* Setters & Getters */
181 
183  { return (!cfg_["save_id"].empty()) ? cfg_["save_id"] : cfg_["id"]; }
184  // The id of the side of the previous scenario that should control this side.
186  { return (!cfg_["previous_save_id"].empty()) ? cfg_["previous_save_id"] : save_id(); }
187  const std::vector<controller_option>& controller_options() const
188  { return controller_options_; }
189  const config& cfg() const { return cfg_; }
191  unsigned current_controller_index() const
192  { return current_controller_index_; }
193  int index() const { return index_; }
194  void set_index(int index) { index_ = index; }
195  int team() const { return team_; }
196  void set_team(int team) { team_ = team; }
197  std::vector<std::string> get_children_to_swap();
198  std::multimap<std::string, config> get_side_children();
199  void set_side_children(std::multimap<std::string, config> children);
200  int color() const { return color_; }
202  int gold() const { return gold_; }
203  void set_gold(int gold) { gold_ = gold; }
204  int income() const { return income_; }
205  void set_income(int income) { income_ = income; }
206  const std::string& player_id() const { return player_id_; }
207  const std::string& reserved_for() const { return reserved_for_; }
210  const std::string& ai_algorithm() const { return ai_algorithm_; }
213  bool allow_player() const { return allow_player_; }
214  bool allow_changes() const { return allow_changes_; }
217  bool allow_shuffle() const { return !disallow_shuffle_;}
218  const std::vector<std::string>& player_teams() const
219  { return parent_.player_teams_; }
220  flg_manager& flg() { return flg_; }
221 
222  const std::string color_id() const { return color_id_; }
223  const std::vector<std::string>& color_options() const { return color_options_; }
224 
225  const std::string team_name() const
226  {
227  return parent_.team_names_[team_];
228  }
229 
231  {
233  }
234 
235 private:
236  side_engine(const side_engine& engine) = delete;
237  void operator=(const side_engine&) = delete;
238 
239  void add_controller_option(ng::controller controller,
240  const std::string& name, const std::string& controller_value);
241 
244 
247  std::vector<controller_option> controller_options_;
248 
249  const bool allow_player_;
250  const bool controller_lock_;
251 
252  int index_;
253  int team_;
254  int color_;
255  int gold_;
256  int income_;
257  // set during create_engines constructor never set after that.
258  // the name of the player who is preferred for this side,
259  // if controller_ == reserved only this player can take this side.
260  // can also be a number of a side if this side shoudl be controlled
261  // by the player who controlls that side
265 
269  const bool allow_changes_;
273 
274  std::vector<std::string> color_options_;
275 };
276 
277 } // 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:352
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)
std::string custom_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()