The Battle for Wesnoth  1.19.13+dev
connect_engine.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2025
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 
49  void import_user(const std::string& name, const bool observer,
50  int side_taken = -1);
51  void import_user(const config& data, const bool observer,
52  int side_taken = -1);
53 
54  // Returns true if there are still sides available for this game.
55  bool sides_available() const;
56 
57  // Import all sides into the level.
58  void update_level();
59  // Updates the level and sends a diff to the clients.
60  void update_and_send_diff();
61 
62  bool can_start_game() const;
63  void start_game();
65 
66  void leave_game();
67 
68  // Returns true if a signal to leave the game was received
69  bool process_network_data(const config& data);
70 
71  // Returns the side which is taken by a given user,
72  // or -1 if none was found.
73  int find_user_side_index_by_id(const std::string& id) const;
74 
75 
76  /* Setters & Getters */
77 
78  const config& level() const { return level_; }
80  {
81  if(auto scenario = level_.optional_child("scenario"))
82  return *scenario;
83  else if(auto snapshot = level_.optional_child("snapshot"))
84  return *snapshot;
85  else
86  throw "No scenariodata found";
87  }
88  const std::set<std::string>& connected_users() const;
89 
91  {
92  std::string team_name;
93  std::string user_team_name; // TODO: use t_string?
94 
96  };
97 
98  const std::vector<team_data_pod>& team_data() const
99  {
100  return team_data_;
101  }
102 
103  const std::vector<side_engine_ptr>& side_engines() const { return side_engines_; }
104  const mp_game_settings& params() const { return params_; }
105  bool first_scenario() const { return first_scenario_; }
106  bool force_lock_settings() const { return force_lock_settings_; }
107 
108 private:
109  connect_engine(const connect_engine&) = delete;
111 
112  void send_level_data() const;
113 
116 
118 
119  friend class side_engine;
120 
123 
125 
128  const bool first_scenario_;
129 
131 
132  std::vector<side_engine_ptr> side_engines_;
133  std::vector<const config*> era_factions_;
134  std::vector<team_data_pod> team_data_;
135 
137 
138  std::set<std::string>& connected_users_rw();
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().
150  std::string user_description() const;
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);
178  void set_controller_commandline(const std::string& controller_name);
179 
180  /* Setters & Getters */
181 
182  std::string save_id() const
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.
185  std::string previous_save_id() const
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  unsigned team() const { return team_; }
196  void set_team(unsigned team) { team_ = team; }
197  std::multimap<std::string, config> get_side_children();
198  void set_side_children(const 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_; }
207  void set_reserved_for(const std::string& reserved_for)
209  const std::string& ai_algorithm() const { return ai_algorithm_; }
210  void set_ai_algorithm(const std::string& 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  flg_manager& flg() { return flg_; }
218 
219  const std::string color_id() const { return color_id_; }
220  const std::vector<std::string>& color_options() const { return color_options_; }
221 
222  const std::string team_name() const
223  {
224  return parent_.team_data_[team_].team_name;
225  }
226 
227  const std::string user_team_name() const
228  {
229  return t_string::from_serialized(parent_.team_data_[team_].user_team_name);
230  }
231 
232 private:
233  side_engine(const side_engine& engine) = delete;
234  side_engine& operator=(const side_engine&) = delete;
235 
236  friend class connect_engine;
237 
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  unsigned 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 should be controlled
260  // by the player who controls that side
261  std::string reserved_for_;
262  std::string player_id_;
263  std::string ai_algorithm_;
264 
268  const bool allow_changes_;
270 
271  std::vector<std::string> color_options_;
272 
273  std::string color_id_;
274 };
275 
276 } // end namespace ng
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
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:380
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_
bool first_scenario() const
const ng::controller default_controller_
std::set< std::string > & connected_users_rw()
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()
ng::era_metadata era_info_
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
const std::vector< side_engine_ptr > & side_engines() const
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 process_network_data(const config &data)
bool can_start_game() const
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:43
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 set_side_children(const std::multimap< std::string, config > &children)
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()
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:164
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:61
@ 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:188