The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
play_controller.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Joerg Hinrichs <[email protected]>
3  wesnoth playlevel Copyright (C) 2003 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 #ifndef PLAY_CONTROLLER_H_INCLUDED
17 #define PLAY_CONTROLLER_H_INCLUDED
18 
19 #include "controller_base.hpp"
20 #include "floating_label.hpp"
21 #include "game_end_exceptions.hpp"
22 #include "help/help.hpp"
24 #include "menu_events.hpp"
25 #include "mouse_events.hpp"
26 #include "persist_manager.hpp"
27 #include "terrain/type_data.hpp"
28 #include "tod_manager.hpp"
29 #include "game_state.hpp"
30 
31 #include <boost/scoped_ptr.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <set>
34 
35 class game_display;
36 class game_data;
37 class team;
38 class unit;
39 class wmi_pager;
40 class replay;
41 class saved_game;
42 struct mp_game_settings;
45 
46 namespace actions {
47  class undo_list;
48 }
49 
50 namespace game_events {
51  class t_pump;
52  class manager;
53  class wml_menu_item;
54 } // namespace game_events
55 
56 namespace soundsource {
57  class manager;
58 } // namespace soundsource
59 
60 namespace statistics {
61  struct scenario_context;
62 } // namespace statistics
63 
64 namespace pathfind {
65  class manager;
66 }
67 
68 namespace tooltips {
69  struct manager;
70 } // namespace tooltips
71 
72 namespace wb {
73  class manager; // whiteboard manager
74 } // namespace wb
75 
76 // Holds gamestate related objects
77 class game_state;
78 
80 {
81 public:
82  play_controller(const config& level, saved_game& state_of_game,
83  const config& game_config,
84  const tdata_cache& tdata,
85  CVideo& video, bool skip_replay);
86  virtual ~play_controller();
87 
88  //event handler, overridden from observer
89  //there is nothing to handle in this class actually but that might change in the future
90  virtual void handle_generic_event(const std::string& /*name*/) {}
91 
92  bool can_undo() const;
93  bool can_redo() const;
94 
95  void undo();
96  void redo();
97 
98  void load_game();
99 
100  void save_game();
101  void save_game_auto(const std::string& filename);
102  void save_replay();
103  void save_replay_auto(const std::string& filename);
104  void save_map();
105 
106  void init_side_begin();
107 
108  /**
109  * Called by turn_info::process_network_data() or init_side() to call do_init_side() if necessary.
110  */
111  void maybe_do_init_side();
112 
113  /**
114  * Called by replay handler or init_side() to do actual work for turn change.
115  */
116  void do_init_side();
117 
118  void init_side_end();
119 
120  virtual void force_end_turn() = 0;
121  virtual void check_objectives() = 0;
122 
123  virtual void on_not_observer() = 0;
124 
125  /**
126  * Asks the user whether to continue on an OOS error.
127  *
128  * @throw quit_game_exception If the user wants to abort.
129  */
130  virtual void process_oos(const std::string& msg) const;
131 
134  }
136  gamestate().end_level_data_ = boost::none;
137  }
138  bool is_regular_game_end() const {
139  return gamestate().end_level_data_.get_ptr() != nullptr;
140  }
142  return *gamestate().end_level_data_;
143  }
144  const std::vector<team>& get_teams_const() const {
145  return gamestate().board_.teams_;
146  }
147 
148  const unit_map& get_units_const() const {
149  return gamestate().board_.units();
150  }
151 
152  const gamemap& get_map_const() const{
153  return gamestate().board_.map();
154  }
156  return gamestate().tod_manager_;
157  }
158 
159  bool is_observer() const {
160  return gamestate().board_.is_observer();
161  }
162 
164  return *gamestate_;
165  }
166  const game_state& gamestate() const {
167  return *gamestate_;
168  }
169 
170  /**
171  * Checks to see if a side has won.
172  *
173  * This will also remove control of villages from sides with dead leaders.
174  */
175  void check_victory();
176 
177  size_t turn() const {return gamestate().tod_manager_.turn();}
178 
179  /**
180  * Returns the number of the side whose turn it is.
181  *
182  * Numbering starts at one.
183  */
184  int current_side() const { return gamestate_->player_number_; }
185 
186  /**
187  * Builds the snapshot config from members and their respective configs.
188  */
189  config to_config() const;
190 
191  bool is_skipping_replay() const { return skip_replay_; }
193  bool is_linger_mode() const { return linger_; }
194  void do_autosave();
195 
196  void do_consolesave(const std::string& filename);
197 
200 
206 
208 
209  int get_ticks();
210 
214 
216 
217  bool is_browsing() const override;
218  bool is_lingering() const { return linger_; }
219 
220  class hotkey_handler;
221 
222  virtual bool is_replay() { return false; }
223 
225  {
226  return level_["name"].t_str();
227  }
228 
230  {
231  return level_["disallow_recall"].to_bool();
232  }
233 
235  {
236  return level_["theme"].str();
237  }
238 
240  {
241  return is_regular_game_end();
242  }
243 
245  {
247  throw return_to_play_side_exception();
248  }
249  }
250 
251  virtual void play_side_impl() {}
252 
253  void play_side();
254 
255  team& current_team();
256  const team& current_team() const;
257 
258  bool can_use_synced_wml_menu() const;
259  std::set<std::string> all_players() const;
260  int ticks() const { return ticks_; }
262 
263  void update_savegame_snapshot() const;
264  /**
265  * Changes the UI for this client to the passed side index.
266  */
267  void update_gui_to_player(const int team_index, const bool observe = false);
268 
269  virtual bool is_networked_mp() const { return false; }
270  virtual void send_to_wesnothd(const config&, const std::string& = "unknown") const { }
271  virtual bool recieve_from_wesnothd(config&) const { return false; }
272 protected:
274  {
278  };
280  void play_slice_catch();
281  bool have_keyboard_focus();
282  void process_focus_keydown_event(const SDL_Event& event);
283  void process_keydown_event(const SDL_Event& event);
284  void process_keyup_event(const SDL_Event& event);
285 
286  void init_managers();
287  ///preload events cannot be synced
288  void fire_preload();
289  void fire_prestart();
290  void fire_start();
291  void start_game();
292  virtual void init_gui();
293  void finish_side_turn();
294  void finish_turn(); //this should not throw an end turn or end level exception
295  bool enemies_visible() const;
296 
297  void enter_textbox();
298  void tab();
299 
300 
301  bool is_team_visible(int team_num, bool observer) const;
302  /// returns 0 if no such team was found.
303  int find_last_visible_team() const;
304 
305 private:
306  const int ticks_;
307 
308 protected:
309  //gamestate
311  boost::scoped_ptr<game_state> gamestate_;
314 
315  //managers
316  boost::scoped_ptr<tooltips::manager> tooltips_manager_;
317 
318  //whiteboard manager
320 
321  //plugins context
322  boost::scoped_ptr<plugins_context> plugins_context_;
323 
324  //more managers
329  boost::scoped_ptr<hotkey_handler> hotkey_handler_;
330  boost::scoped_ptr<soundsource::manager> soundsources_manager_;
332 
333  //other objects
334  boost::scoped_ptr<game_display> gui_;
335  boost::scoped_ptr<unit_experience_accelerator> xp_mod_;
336  boost::scoped_ptr<const statistics::scenario_context> statistics_context_;
338  const actions::undo_list& undo_stack() const { return *gamestate().undo_stack_; };
339  boost::scoped_ptr<replay> replay_;
340 
342  bool linger_;
343  /**
344  * Whether we did init sides in this session
345  * (false = we did init sides before we reloaded the game).
346  */
348  const std::string& select_victory_music() const;
349  const std::string& select_defeat_music() const;
350  void set_victory_music_list(const std::string& list);
351  void set_defeat_music_list(const std::string& list);
352 
353  void reset_gamestate(const config& level, int replay_pos);
354 
355 private:
356 
357  void init(CVideo& video, const config& level);
358 
361  std::vector<std::string> victory_music_;
362  std::vector<std::string> defeat_music_;
363 
365 
366 protected:
367  mutable bool ignore_replay_errors_;
369  virtual void sync_end_turn() {};
370  virtual void check_time_over();
371  virtual void update_viewing_player() = 0;
372  void play_turn();
373 };
374 
375 
376 #endif
play_controller(const config &level, saved_game &state_of_game, const config &game_config, const tdata_cache &tdata, CVideo &video, bool skip_replay)
const unit_map & get_units_const() const
events::mouse_handler & get_mouse_handler_base()
Get a reference to a mouse handler member a derived class uses.
virtual void send_to_wesnothd(const config &, const std::string &="unknown") const
help::help_manager help_manager_
bool is_team_visible(int team_num, bool observer) const
virtual const unit_map & units() const
Definition: game_board.hpp:99
int ticks() const
font::floating_label_context labels_manager_
const end_level_data & get_end_level_data_const() const
void save_replay_auto(const std::string &filename)
Definition: unit.hpp:95
bool victory_when_enemies_defeated_
GLint level
Definition: glew.h:1220
bool can_redo() const
const mp_game_settings & get_mp_settings()
boost::scoped_ptr< plugins_context > plugins_context_
events::mouse_handler mouse_handler_
virtual void process_oos(const std::string &msg) const
Asks the user whether to continue on an OOS error.
Definition: video.hpp:58
virtual void handle_generic_event(const std::string &)
int server_request_number_
Definition: game_state.hpp:66
virtual void update_viewing_player()=0
bool can_use_synced_wml_menu() const
virtual bool recieve_from_wesnothd(config &) const
void process_focus_keydown_event(const SDL_Event &event)
Process keydown (only when the general map display does not have focus).
boost::scoped_ptr< hotkey_handler > hotkey_handler_
void check_victory()
Checks to see if a side has won.
boost::scoped_ptr< soundsource::manager > soundsources_manager_
bool is_skipping_replay() const
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
controller_base framework: controller_base is roughly analogous to a "dialog" class in a GUI toolkit ...
void init(CVideo &video, const config &level)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
persist_manager persist_
const std::string & select_victory_music() const
const tdata_cache & tdata_
void process_keydown_event(const SDL_Event &event)
Process keydown (always).
boost::scoped_ptr< unit_experience_accelerator > xp_mod_
events::menu_handler menu_handler_
const gamemap & get_map_const() const
void process_keyup_event(const SDL_Event &event)
Process keyup (always).
bool enemies_visible() const
void do_consolesave(const std::string &filename)
saved_game & saved_game_
std::vector< std::string > defeat_music_
game_events::t_pump & pump()
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
scoped_savegame_snapshot(const play_controller &controller)
void save_game_auto(const std::string &filename)
int current_side() const
Returns the number of the side whose turn it is.
int get_server_request_number() const
std::vector< team > teams_
Definition: game_board.hpp:58
bool is_regular_game_end() const
void update_savegame_snapshot() const
virtual void check_time_over()
Implements a quit confirmation dialog.
size_t turn() const
t_string get_scenario_name()
virtual bool should_return_to_play_side()
Encapsulates the map of the game.
Definition: map.hpp:37
virtual void init_gui()
void maybe_do_init_side()
Called by turn_info::process_network_data() or init_side() to call do_init_side() if necessary...
tod_manager tod_manager_
Definition: game_state.hpp:50
boost::scoped_ptr< actions::undo_list > undo_stack_
undo_stack_ is never nullptr.
Definition: game_state.hpp:58
bool is_lingering() const
bool is_observer() const
std::set< std::string > all_players() const
const game_state & gamestate() const
bool is_browsing() const override
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
hotkey::command_executor * get_hotkey_command_executor()
Get (optionally) a command executor to handle context menu events.
void update_gui_to_player(const int team_index, const bool observe=false)
Changes the UI for this client to the passed side index.
virtual void force_end_turn()=0
void do_init_side()
Called by replay handler or init_side() to do actual work for turn change.
Domain specific events.
Definition: action_wml.cpp:93
virtual ~play_controller()
actions::undo_list & get_undo_stack()
void maybe_throw_return_to_play_side()
void set_end_level_data(const end_level_data &data)
tooltips.
Game configuration data as global variables.
Definition: build_info.cpp:38
t_possible_end_level_data end_level_data_
Definition: game_state.hpp:62
hotkey::scope_changer scope_
void reset_gamestate(const config &level, int replay_pos)
structure which will hide all current floating labels, and cause floating labels instantiated after i...
int turn() const
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
const std::vector< team > & get_teams_const() const
boost::scoped_ptr< tooltips::manager > tooltips_manager_
Additional information on the game outcome which can be provided by WML.
actions::undo_list & undo_stack()
void set_defeat_music_list(const std::string &list)
const tod_manager & get_tod_manager_const() const
std::string observer
Definition: game_config.cpp:84
virtual const gamemap & map() const
Definition: game_board.hpp:98
const game_classification & get_classification()
boost::scoped_ptr< game_state > gamestate_
Class to store the actions that a player can undo and redo.
Definition: undo.hpp:38
boost::shared_ptr< wb::manager > get_whiteboard()
game_state & gamestate()
cl_event event
Definition: glew.h:3070
bool have_keyboard_focus()
Derived classes should override this to return false when arrow keys should not scroll the map...
events::menu_handler & get_menu_handler()
game_board board_
Definition: game_state.hpp:49
boost::shared_ptr< wb::manager > whiteboard_manager_
config to_config() const
Builds the snapshot config from members and their respective configs.
virtual void on_not_observer()=0
virtual bool is_replay()
game_display & get_display()
Get a reference to a display member a derived class uses.
bool is_observer() const
Check if we are an observer in this game.
Container associating units to locations.
Definition: map.hpp:90
const actions::undo_list & undo_stack() const
void fire_preload()
preload events cannot be synced
void set_victory_music_list(const std::string &list)
boost::scoped_ptr< game_display > gui_
std::vector< std::string > victory_music_
const std::string & select_defeat_music() const
virtual bool is_networked_mp() const
virtual soundsource::manager * get_soundsource_man()
Get (optionally) a soundsources manager a derived class uses.
boost::scoped_ptr< const statistics::scenario_context > statistics_context_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
std::string theme()
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool is_linger_mode() const
virtual void check_objectives()=0
bool can_undo() const
Definition: display.hpp:47
int find_last_visible_team() const
returns 0 if no such team was found.
virtual void play_side_impl()
boost::scoped_ptr< replay > replay_
bool init_side_done_now_
Whether we did init sides in this session (false = we did init sides before we reloaded the game)...
bool remove_from_carryover_on_defeat_
void increase_server_request_number()
virtual void sync_end_turn()