The Battle for Wesnoth  1.17.0-dev
connect_engine.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2021
3  by Andrius Silinskas <silinskas.andrius@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "commandline_options.hpp"
19 #include "config.hpp"
20 #include "flg_manager.hpp"
21 #include "saved_game.hpp"
22 #include <set>
23 
24 namespace randomness { class mt_rng; }
25 struct mp_game_metadata;
26 class game_config_view;
27 
28 namespace ng {
29 
30 enum controller {
36 };
37 
38 class connect_engine;
40 
41 typedef std::shared_ptr<side_engine> side_engine_ptr;
42 typedef std::pair<ng::controller, std::string> controller_option;
43 
45 {
46 public:
48  const bool first_scenario, mp_game_metadata* metadata);
49 
50  config* current_config();
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, const game_config_view& game_config);
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 
95  {
96  std::string team_name;
97  std::string user_team_name; // TODO: use t_string?
98 
100  };
101 
102  const std::vector<team_data_pod>& team_data() const
103  {
104  return team_data_;
105  }
106 
107  std::vector<side_engine_ptr>& side_engines() { return side_engines_; }
108  const mp_game_settings& params() const { return params_; }
109  bool first_scenario() const { return first_scenario_; }
110  bool force_lock_settings() const { return force_lock_settings_; }
111 
112 private:
113  connect_engine(const connect_engine&) = delete;
114  connect_engine& operator=(const connect_engine&) = delete;
115 
116  void send_level_data() const;
117 
118  void save_reserved_sides_information();
119  void load_previous_sides_users();
120 
121  void update_side_controller_options();
122 
123  friend class side_engine;
124 
127 
129 
132  const bool first_scenario_;
133 
135 
136  std::vector<side_engine_ptr> side_engines_;
137  std::vector<const config*> era_factions_;
138  std::vector<team_data_pod> team_data_;
139 
140  std::set<std::string>& connected_users_rw();
141  void send_to_server(const config& cfg) const;
142 };
143 
145 {
146 public:
147  side_engine(const config& cfg, connect_engine& parent_engine,
148  const int index);
149 
150  // An untranslated user_description which is used by other clients
151  // An empty string means the other clients should generate the description on their own
152  // Used by new_config().
153  std::string user_description() const;
154 
155  // Creates a config representing this side.
156  config new_config() const;
157 
158  // Returns true, if the player has chosen his/her leader and this side
159  // is ready for the game to start.
160  bool ready_for_start() const;
161  // Returns true if this side is waiting for a network player and
162  // players are allowed.
163  bool available_for_user(const std::string& name = "") const;
164 
165  void resolve_random( randomness::mt_rng & rng, const std::vector<std::string> & avoid_faction_ids = std::vector<std::string>());
166 
167  // Resets this side to its default state.
168  void reset();
169 
170  // Place user into this side.
171  void place_user(const std::string& name);
172  void place_user(const config& data, bool contains_selection = false);
173 
174  void update_controller_options();
175  void update_current_controller_index();
176  bool controller_changed(const int selection);
177  void set_controller(ng::controller controller);
178 
179  // Game set up from command line helpers.
180  void set_faction_commandline(const std::string& faction_name);
181  void set_controller_commandline(const std::string& controller_name);
182 
183  /* Setters & Getters */
184 
185  std::string save_id() const
186  { return (!cfg_["save_id"].empty()) ? cfg_["save_id"] : cfg_["id"]; }
187  // The id of the side of the previous scenario that should control this side.
188  std::string previous_save_id() const
189  { return (!cfg_["previous_save_id"].empty()) ? cfg_["previous_save_id"] : save_id(); }
190  const std::vector<controller_option>& controller_options() const
191  { return controller_options_; }
192  const config& cfg() const { return cfg_; }
193  ng::controller controller() const { return controller_; }
194  unsigned current_controller_index() const
195  { return current_controller_index_; }
196  int index() const { return index_; }
197  void set_index(int index) { index_ = index; }
198  unsigned team() const { return team_; }
199  void set_team(unsigned team) { team_ = team; }
200  std::multimap<std::string, config> get_side_children();
201  void set_side_children(std::multimap<std::string, config> children);
202  int color() const { return color_; }
203  void set_color(int color) { color_ = color; color_id_ = color_options_[color]; }
204  int gold() const { return gold_; }
205  void set_gold(int gold) { gold_ = gold; }
206  int income() const { return income_; }
207  void set_income(int income) { income_ = income; }
208  const std::string& player_id() const { return player_id_; }
209  const std::string& reserved_for() const { return reserved_for_; }
210  void set_reserved_for(const std::string& reserved_for)
211  { reserved_for_ = reserved_for; }
212  const std::string& ai_algorithm() const { return ai_algorithm_; }
213  void set_ai_algorithm(const std::string& ai_algorithm)
214  { ai_algorithm_ = ai_algorithm; }
215  bool allow_player() const { return allow_player_; }
216  bool allow_changes() const { return allow_changes_; }
217  bool waiting_to_choose_faction() const { return waiting_to_choose_faction_; }
218  void set_waiting_to_choose_status(bool status) { waiting_to_choose_faction_ = status;}
219  bool allow_shuffle() const { return !disallow_shuffle_;}
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_data_[team_].team_name;
228  }
229 
230  const std::string user_team_name() const
231  {
232  return t_string::from_serialized(parent_.team_data_[team_].user_team_name);
233  }
234 
235 private:
236  side_engine(const side_engine& engine) = delete;
237  side_engine& operator=(const side_engine&) = delete;
238 
239  friend class connect_engine;
240 
241  void add_controller_option(ng::controller controller,
242  const std::string& name, const std::string& controller_value);
243 
246 
249  std::vector<controller_option> controller_options_;
250 
251  const bool allow_player_;
252  const bool controller_lock_;
253 
254  int index_;
255  unsigned team_;
256  int color_;
257  int gold_;
258  int income_;
259  // set during create_engines constructor never set after that.
260  // the name of the player who is preferred for this side,
261  // if controller_ == reserved only this player can take this side.
262  // can also be a number of a side if this side should be controlled
263  // by the player who controls that side
264  std::string reserved_for_;
265  std::string player_id_;
266  std::string ai_algorithm_;
267 
271  const bool allow_changes_;
273 
274  std::vector<std::string> color_options_;
275 
276  std::string color_id_;
277 };
278 
279 } // end namespace ng
flg_manager & flg()
int income() const
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:402
void set_waiting_to_choose_status(bool status)
void set_reserved_for(const std::string &reserved_for)
std::vector< side_engine_ptr > side_engines_
std::string save_id() const
void set_income(int income)
bool force_lock_settings() const
std::string ai_algorithm_
std::string player_id_
bool allow_changes() const
std::vector< team_data_pod > team_data_
const bool allow_changes_
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:29
const std::vector< team_data_pod > & team_data() const
std::vector< controller_option > controller_options_
Definitions for the interface to Wesnoth Markup Language (WML).
void set_color(int color)
mp_game_metadata * mp_metadata_
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
std::vector< side_engine_ptr > & side_engines()
std::string ready_for_start
const std::string & reserved_for() const
const std::string & player_id() const
const std::string team_name() const
const std::string & ai_algorithm() const
const mp_game_settings & params() const
std::vector< std::string > color_options_
void set_gold(int gold)
const bool first_scenario_
ng::controller controller_
const std::string color_id() const
std::vector< const config * > era_factions_
const ng::controller default_controller_
Game configuration data as global variables.
Definition: build_info.cpp:59
bool allow_shuffle() const
void set_ai_algorithm(const std::string &ai_algorithm)
std::string reserved_for_
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:154
bool allow_player() const
const config & level() const
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
unsigned current_controller_index_
connect_engine & parent_
std::string observer
std::shared_ptr< side_engine > side_engine_ptr
const std::string user_team_name() const
std::pair< ng::controller, std::string > controller_option
void set_index(int index)
std::string previous_save_id() const
const bool controller_lock_
const config & cfg() const
const mp_game_settings & params_
bool first_scenario() const
std::string color_id_
const std::vector< controller_option > & controller_options() const
ng::controller controller() const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
const bool allow_player_
bool waiting_to_choose_faction() const
unsigned current_controller_index() const
static std::string controller_name(const team &t)
Definition: game_stats.cpp:68
void set_team(unsigned team)
const std::vector< std::string > & color_options() const
unsigned team() const