The Battle for Wesnoth  1.19.4+dev
connect_engine.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2024
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 side_engine;
39 
40 typedef std::shared_ptr<side_engine> side_engine_ptr;
41 typedef std::pair<ng::controller, std::string> controller_option;
42 
44 {
45 public:
47  const bool first_scenario, mp_game_metadata* metadata);
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();
63 
64  bool can_start_game() const;
65  void start_game();
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(auto scenario = level_.optional_child("scenario"))
85  return *scenario;
86  else if(auto snapshot = level_.optional_child("snapshot"))
87  return *snapshot;
88  else
89  throw "No scenariodata found";
90  }
91  const std::set<std::string>& connected_users() const;
92 
94  {
95  std::string team_name;
96  std::string user_team_name; // TODO: use t_string?
97 
99  };
100 
101  const std::vector<team_data_pod>& team_data() const
102  {
103  return team_data_;
104  }
105 
106  std::vector<side_engine_ptr>& side_engines() { return side_engines_; }
107  const mp_game_settings& params() const { return params_; }
108  bool first_scenario() const { return first_scenario_; }
109  bool force_lock_settings() const { return force_lock_settings_; }
110 
111 private:
112  connect_engine(const connect_engine&) = delete;
114 
115  void send_level_data() const;
116 
119 
121 
122  friend class side_engine;
123 
126 
128 
131  const bool first_scenario_;
132 
134 
135  std::vector<side_engine_ptr> side_engines_;
136  std::vector<const config*> era_factions_;
137  std::vector<team_data_pod> team_data_;
138 
139  std::set<std::string>& connected_users_rw();
140 };
141 
143 {
144 public:
145  side_engine(const config& cfg, connect_engine& parent_engine,
146  const int index);
147 
148  // An untranslated user_description which is used by other clients
149  // An empty string means the other clients should generate the description on their own
150  // Used by new_config().
151  std::string user_description() const;
152 
153  // Creates a config representing this side.
154  config new_config() const;
155 
156  // Returns true, if the player has chosen his/her leader and this side
157  // is ready for the game to start.
158  bool ready_for_start() const;
159  // Returns true if this side is waiting for a network player and
160  // players are allowed.
161  bool available_for_user(const std::string& name = "") const;
162 
163  void resolve_random( randomness::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 
183  std::string save_id() const
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.
186  std::string previous_save_id() const
187  { return (!cfg_["previous_save_id"].empty()) ? cfg_["previous_save_id"] : save_id(); }
188  const std::vector<controller_option>& controller_options() const
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  unsigned team() const { return team_; }
197  void set_team(unsigned team) { team_ = team; }
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_; }
208  void set_reserved_for(const std::string& reserved_for)
210  const std::string& ai_algorithm() const { return ai_algorithm_; }
211  void set_ai_algorithm(const std::string& 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  flg_manager& flg() { return flg_; }
219 
220  const std::string color_id() const { return color_id_; }
221  const std::vector<std::string>& color_options() const { return color_options_; }
222 
223  const std::string team_name() const
224  {
225  return parent_.team_data_[team_].team_name;
226  }
227 
228  const std::string user_team_name() const
229  {
230  return t_string::from_serialized(parent_.team_data_[team_].user_team_name);
231  }
232 
233 private:
234  side_engine(const side_engine& engine) = delete;
235  side_engine& operator=(const side_engine&) = delete;
236 
237  friend class connect_engine;
238 
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  unsigned 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 should be controlled
261  // by the player who controls that side
262  std::string reserved_for_;
263  std::string player_id_;
264  std::string ai_algorithm_;
265 
269  const bool allow_changes_;
271 
272  std::vector<std::string> color_options_;
273 
274  std::string color_id_;
275 };
276 
277 } // end namespace ng
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:383
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const config & level() const
const std::vector< team_data_pod > & team_data() const
std::vector< const config * > era_factions_
std::vector< team_data_pod > team_data_
std::vector< side_engine_ptr > & side_engines()
bool first_scenario() const
const ng::controller default_controller_
std::set< std::string > & connected_users_rw()
std::pair< bool, bool > process_network_data(const config &data)
void import_user(const std::string &name, const bool observer, int side_taken=-1)
bool sides_available() const
std::vector< side_engine_ptr > side_engines_
const bool first_scenario_
bool force_lock_settings() const
void save_reserved_sides_information()
const std::set< std::string > & connected_users() const
const mp_game_settings & params() const
const mp_game_settings & params_
void send_level_data() const
connect_engine & operator=(const connect_engine &)=delete
mp_game_metadata * mp_metadata_
void update_side_controller_options()
connect_engine(saved_game &state, const bool first_scenario, mp_game_metadata *metadata)
void start_game_commandline(const commandline_options &cmdline_opts, const game_config_view &game_config)
connect_engine(const connect_engine &)=delete
int find_user_side_index_by_id(const std::string &id) const
bool can_start_game() const
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:30
const std::string team_name() const
const config & cfg() const
void set_gold(int gold)
void set_team(unsigned team)
unsigned current_controller_index_
unsigned team() const
void update_controller_options()
flg_manager & flg()
bool waiting_to_choose_faction() const
std::string player_id_
std::string previous_save_id() const
const bool controller_lock_
const bool allow_changes_
void set_controller_commandline(const std::string &controller_name)
void set_income(int income)
void set_faction_commandline(const std::string &faction_name)
int income() const
void place_user(const std::string &name)
void add_controller_option(ng::controller controller, const std::string &name, const std::string &controller_value)
const std::vector< std::string > & color_options() const
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid_faction_ids=std::vector< std::string >())
void update_current_controller_index()
void set_side_children(std::multimap< std::string, config > children)
side_engine(const config &cfg, connect_engine &parent_engine, const int index)
std::vector< controller_option > controller_options_
const std::string user_team_name() const
std::string save_id() const
connect_engine & parent_
side_engine(const side_engine &engine)=delete
std::string ai_algorithm_
side_engine & operator=(const side_engine &)=delete
void set_reserved_for(const std::string &reserved_for)
bool allow_player() const
std::string reserved_for_
void set_index(int index)
std::vector< std::string > color_options_
void set_controller(ng::controller controller)
void set_waiting_to_choose_status(bool status)
void set_color(int color)
bool controller_changed(const int selection)
std::string color_id_
std::string user_description() const
ng::controller controller_
const std::string & ai_algorithm() const
bool allow_shuffle() const
std::multimap< std::string, config > get_side_children()
const std::string color_id() const
bool ready_for_start() const
void set_ai_algorithm(const std::string &ai_algorithm)
config new_config() const
bool allow_changes() const
const std::vector< controller_option > & controller_options() const
const std::string & player_id() const
const std::string & reserved_for() const
ng::controller controller() const
bool available_for_user(const std::string &name="") const
unsigned current_controller_index() const
const bool allow_player_
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:153
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
Definitions for the interface to Wesnoth Markup Language (WML).
std::string observer
Game configuration data as global variables.
Definition: build_info.cpp:61
static std::string controller_name(const team &t)
Definition: game_stats.cpp:60
@ CNTR_RESERVED
@ CNTR_NETWORK
@ CNTR_COMPUTER
@ CNTR_EMPTY
@ CNTR_LOCAL
std::pair< ng::controller, std::string > controller_option
std::shared_ptr< side_engine > side_engine_ptr
std::string_view data
Definition: picture.cpp:178