The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) 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 /**
16  * @file
17  */
18 
19 #ifndef WB_MANAGER_HPP_
20 #define WB_MANAGER_HPP_
21 
22 #include "side_actions.hpp"
23 
24 #include "units/map.hpp"
25 
26 #include <boost/noncopyable.hpp>
27 
28 class CKey;
29 class team;
30 
31 namespace pathfind {
32  struct marked_route;
33 }
34 
35 namespace wb {
36 
37 class mapbuilder;
38 class highlighter;
39 
40 /**
41  * This class is the frontend of the whiteboard framework for the rest of the Wesnoth code.
42  */
43 class manager : private boost::noncopyable
44 {
45  friend struct future_map;
46  friend struct future_map_if_active;
47  friend struct real_map;
48 
49 public:
50 
51  manager();
52  ~manager();
53 
54  void print_help_once();
55 
56  /** Determine whether the game is initialized and the current side has control of the game
57  * i.e. the whiteboard can take over
58  */
59  bool can_modify_game_state() const;
60  /** Determine whether the whiteboard can be activated safely */
61  bool can_activate() const;
62  /** Determine whether the whiteboard is activated. */
63  bool is_active() const { return active_; }
64  /** Activates/Deactivates the whiteboard*/
65  void set_active(bool active);
66  /** Called by the key that temporarily toggles the activated state when held */
67  void set_invert_behavior(bool invert);
68  /** Prevents the whiteboard from changing its activation state, as long as the returned reference is held */
70 
71  /** Is the whiteboard in the process of executing an action? */
72  bool is_executing_actions() const { return executing_actions_; }
73 
74  /** Used to ask the whiteboard if its action execution hotkeys should be available to the user */
75  bool can_enable_execution_hotkeys() const;
76  /** Used to ask the whiteboard if hotkeys affecting the action queue should be available to the user */
77  bool can_enable_modifier_hotkeys() const;
78  /** Used to ask the whiteboard if its action reordering hotkeys should be available to the user */
79  bool can_enable_reorder_hotkeys() const;
80  /** Used to ask permission to the wb to move a leader, to avoid invalidating planned recruits */
81  bool allow_leader_to_move(unit const& leader) const;
82  /** @ return true if the whiteboard is ready to end turn. Triggers the execution of remaining planned actions. */
83  bool allow_end_turn();
84  /**
85  * The on_* methods below inform the whiteboard of specific events
86  */
87  void on_init_side();
88  void on_finish_side_turn(int side);
89  void on_mouseover_change(const map_location& hex);
91  void on_gamestate_change();
92  void on_viewer_change(size_t team_index);
93  void on_change_controller(int side, const team& t);
94  /** Handles various cleanup right before removing an action from the queue */
96  /** Handles various cleanup right after removing an action from the queue */
98 
99  /** Called by replay_network_sender to add whiteboard data to the outgoing network packets */
100  void send_network_data();
101  /** Called by turn_info::process_network_data() when network data needs to be processed */
102  void process_network_data(config const&);
103  /** Adds a side_actions::net_cmd to net_buffer_[team_index], whereupon it will (later) be sent to all allies */
104  void queue_net_cmd(size_t team_index, side_actions::net_cmd const&);
105 
106  /** Whether the current side has actions in the first turn of its planned actions queue */
107  static bool current_side_has_actions();
108 
109  /** Validates all actions of the current viewing side */
111 
112  /** Whether the planned unit map is currently applied */
114 
115 
116  /**
117  * Called from the display before drawing.
118  */
119  void pre_draw();
120  /**
121  * Called from the display after drawing.
122  */
123  void post_draw();
124  /**
125  * Called from the display when drawing hexes, to allow the whiteboard to
126  * add visual elements. Some visual elements such as arrows and fake units
127  * are not handled through this function, but separately registered with the display.
128  */
129  void draw_hex(const map_location& hex);
130 
131  /** Creates a temporary visual arrow, that follows the cursor, for move creation purposes */
132  void create_temp_move();
133  /** Informs whether an arrow is being displayed for move creation purposes */
134  bool has_temp_move() const { return route_ && !fake_units_.empty() && !move_arrows_.empty(); }
135  /** Erase the temporary arrow */
136  void erase_temp_move();
137  /** Creates a move action for the current side, and erases the temp move.
138  * The move action is inserted at the end of the queue, to be executed last. */
139  void save_temp_move();
140  /** @return an iterator to the unit that owns the temp move, resources::units->end() if there's none. */
142 
143  /** Creates an attack or attack-move action for the current side */
144  void save_temp_attack(const map_location& attacker_loc, const map_location& defender_loc, int weapon_choice);
145 
146  /** Creates a recruit action for the current side
147  * @return true if manager has saved a planned recruit */
148  bool save_recruit(const std::string& name, int side_num, const map_location& recruit_hex);
149 
150  /** Creates a recall action for the current side
151  * @return true if manager has saved a planned recall */
152  bool save_recall(const unit& unit, int side_num, const map_location& recall_hex);
153 
154  /** Creates a suppose-dead action for the current side */
155  void save_suppose_dead(unit& curr_unit, map_location const& loc);
156 
157  /** Executes first action in the queue for current side */
158  void contextual_execute();
159  /** Executes all actions for the current turn in sequence
160  * @return true if the there are no more actions left for this turn when the method returns */
161  bool execute_all_actions();
162  /** Deletes last action in the queue for current side */
163  void contextual_delete();
164  /** Moves the action determined by the UI toward the beginning of the queue */
166  /** Moves the action determined by the UI toward the beginning of the queue */
168 
169  /** Get the highlight visitor instance in use by the manager */
170  boost::weak_ptr<highlighter> get_highlighter() { return highlighter_; }
171 
172  /** Checks whether the whiteboard has any planned action on any team */
173  bool has_actions() const;
174  /** Checks whether the specified unit has at least one planned action */
175  bool unit_has_actions(unit const* unit) const;
176 
177  /** Used to track gold spending per-side when building the planned unit map
178  * Is referenced by the top bar gold display */
179  int get_spent_gold_for(int side);
180 
181  /** Determines whether or not the undo_stack should be cleared.
182  * @todo Only when there are networked allies and we have set a preferences option */
183  bool should_clear_undo() const {return true;}
184 
185  /** Displays the whiteboard options dialog. */
186  void options_dlg();
187 
188 private:
189  /** Transforms the unit map so that it now reflects the future state of things,
190  * i.e. when all planned actions will have been executed */
191  void set_planned_unit_map();
192  /** Restore the regular unit map */
193  void set_real_unit_map();
194 
196  /** Called by all of the save_***() methods after they have added their action to the queue */
197  void update_plan_hiding(size_t viewing_team);
198  void update_plan_hiding(); //same as above, but uses wb::viewer_team() as default argument
199 
200  /** Tracks whether the whiteboard is active. */
201  bool active_;
204 #if 0
205  bool print_help_once_;
206 #endif
209  /** Track whenever we're modifying actions, to avoid dual execution etc. */
211  /** Track whether we're in the process of executing all actions */
213  /** true if we're in the process of executing all action and should end turn once finished. */
215  /** Track whether the gamestate changed and we need to validate actions. */
217 
218  /** Reference counted "lock" to allow preventing whiteboard activation state changes */
220  /** Reference counted "lock" to prevent the building of the unit map at certain times */
222 
223 
224  boost::scoped_ptr<mapbuilder> mapbuilder_;
226 
227  boost::scoped_ptr<pathfind::marked_route> route_;
228 
229  std::vector<arrow_ptr> move_arrows_;
230  std::vector<fake_unit_ptr> fake_units_;
232 
233  boost::scoped_ptr<CKey> key_poller_;
234 
235  std::vector<map_location> hidden_unit_hexes_;
236 
237  ///net_buffer_[i] = whiteboard network data to be sent "from" teams[i].
238  std::vector<config> net_buffer_;
239 
240  ///team_plans_hidden_[i] = whether or not to hide actions from teams[i].
241  std::vector<bool> team_plans_hidden_;
242 
243  ///used to keep track of units owning planned moves for visual ghosting/unghosting
244  std::set<size_t> units_owning_moves_;
245 };
246 
247 /** Applies the planned unit map for the duration of the struct's life.
248  * Reverts to real unit map on destruction, unless planned unit map was already applied when the struct was created. */
250 {
251  future_map();
252  ~future_map();
254 };
255 
257 {
258  /** @param cond: If true, applies the planned unit map for the duration of the struct's life and reverts to real unit map on destruction.
259  No effect if cond == false.
260  */
261  boost::scoped_ptr<future_map> future_map_;
263  : future_map_(cond ? new future_map() : nullptr)
264  {}
265 };
266 
267 /** ONLY IF whiteboard is currently active, applies the planned unit map for the duration of the struct's life.
268  * Reverts to real unit map on destruction, unless planned unit map was already applied when the struct was created. */
270 {
275 };
276 
277 /** Ensures that the real unit map is active for the duration of the struct's life.
278  * On destruction reverts to planned unit map if it was active when the struct was created. */
279 struct real_map
280 {
281  real_map();
282  ~real_map();
285 };
286 
287 } // end namespace wb
288 
289 #endif /* WB_MANAGER_HPP_ */
bool self_activate_once_
Definition: manager.hpp:203
whiteboard_lock unit_map_lock_
Definition: manager.hpp:284
bool initial_planned_unit_map_
Definition: manager.hpp:253
bool can_activate() const
Determine whether the whiteboard can be activated safely.
Definition: manager.cpp:159
void update_plan_hiding()
Definition: manager.cpp:373
bool can_enable_modifier_hotkeys() const
Used to ask the whiteboard if hotkeys affecting the action queue should be available to the user...
Definition: manager.cpp:249
Definition: unit.hpp:95
void on_viewer_change(size_t team_index)
Definition: manager.cpp:376
void process_network_data(config const &)
Called by turn_info::process_network_data() when network data needs to be processed.
Definition: manager.cpp:622
bool can_enable_reorder_hotkeys() const
Used to ask the whiteboard if its action reordering hotkeys should be available to the user...
Definition: manager.cpp:254
bool initial_planned_unit_map_
Definition: manager.hpp:283
std::vector< arrow_ptr > move_arrows_
Definition: manager.hpp:229
void set_invert_behavior(bool invert)
Called by the key that temporarily toggles the activated state when held.
Definition: manager.cpp:193
void contextual_delete()
Deletes last action in the queue for current side.
Definition: manager.cpp:999
whiteboard_lock activation_state_lock_
Reference counted "lock" to allow preventing whiteboard activation state changes. ...
Definition: manager.hpp:219
void validate_actions_if_needed()
Definition: manager.cpp:1175
void post_delete_action(action_ptr action)
Handles various cleanup right after removing an action from the queue.
Definition: manager.cpp:324
boost::scoped_ptr< mapbuilder > mapbuilder_
Definition: manager.hpp:224
boost::scoped_ptr< future_map > future_map_
Definition: manager.hpp:261
void save_suppose_dead(unit &curr_unit, map_location const &loc)
Creates a suppose-dead action for the current side.
Definition: manager.cpp:902
boost::shared_ptr< highlighter > highlighter_
Definition: manager.hpp:225
bool is_active() const
Determine whether the whiteboard is activated.
Definition: manager.hpp:63
size_t temp_move_unit_underlying_id_
Definition: manager.hpp:231
bool executing_all_actions_
Track whether we're in the process of executing all actions.
Definition: manager.hpp:212
void pre_draw()
Called from the display before drawing.
Definition: manager.cpp:513
GLdouble GLdouble t
Definition: glew.h:1366
void contextual_bump_down_action()
Moves the action determined by the UI toward the beginning of the queue.
Definition: manager.cpp:1038
void save_temp_move()
Creates a move action for the current side, and erases the temp move.
Definition: manager.cpp:756
void set_active(bool active)
Activates/Deactivates the whiteboard.
Definition: manager.cpp:168
std::vector< fake_unit_ptr > fake_units_
Definition: manager.hpp:230
std::vector< map_location > hidden_unit_hexes_
Definition: manager.hpp:235
bool can_enable_execution_hotkeys() const
Used to ask the whiteboard if its action execution hotkeys should be available to the user...
Definition: manager.cpp:243
void save_temp_attack(const map_location &attacker_loc, const map_location &defender_loc, int weapon_choice)
Creates an attack or attack-move action for the current side.
Definition: manager.cpp:798
bool planned_unit_map_active_
Definition: manager.hpp:208
void on_gamestate_change()
Definition: manager.cpp:586
void draw_hex(const map_location &hex)
Called from the display when drawing hexes, to allow the whiteboard to add visual elements...
Definition: manager.cpp:540
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
boost::scoped_ptr< pathfind::marked_route > route_
Definition: manager.hpp:227
whiteboard_lock unit_map_lock_
Reference counted "lock" to prevent the building of the unit map at certain times.
Definition: manager.hpp:221
void queue_net_cmd(size_t team_index, side_actions::net_cmd const &)
Adds a side_actions::net_cmd to net_buffer_[team_index], whereupon it will (later) be sent to all all...
Definition: manager.cpp:635
void set_real_unit_map()
Restore the regular unit map.
Definition: manager.cpp:1156
bool has_actions() const
Checks whether the whiteboard has any planned action on any team.
Definition: manager.cpp:1050
void options_dlg()
Displays the whiteboard options dialog.
Definition: manager.cpp:1071
unit_map::iterator get_temp_move_unit() const
Definition: manager.cpp:793
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:249
void contextual_execute()
Executes first action in the queue for current side.
Definition: manager.cpp:912
This class is the frontend of the whiteboard framework for the rest of the Wesnoth code...
Definition: manager.hpp:43
boost::scoped_ptr< CKey > key_poller_
Definition: manager.hpp:233
bool gamestate_mutated_
Track whether the gamestate changed and we need to validate actions.
Definition: manager.hpp:216
bool allow_end_turn()
@ return true if the whiteboard is ready to end turn.
Definition: manager.cpp:945
void on_deselect_hex()
Definition: manager.hpp:90
static bool current_side_has_actions()
Whether the current side has actions in the first turn of its planned actions queue.
Definition: manager.cpp:411
std::set< size_t > units_owning_moves_
used to keep track of units owning planned moves for visual ghosting/unghosting
Definition: manager.hpp:244
bool preparing_to_end_turn_
true if we're in the process of executing all action and should end turn once finished.
Definition: manager.hpp:214
bool active_
Tracks whether the whiteboard is active.
Definition: manager.hpp:201
void on_change_controller(int side, const team &t)
Definition: manager.cpp:382
void on_finish_side_turn(int side)
Definition: manager.cpp:308
std::vector< bool > team_plans_hidden_
team_plans_hidden_[i] = whether or not to hide actions from teams[i].
Definition: manager.hpp:241
Encapsulates the map of the game.
Definition: location.hpp:38
bool save_recruit(const std::string &name, int side_num, const map_location &recruit_hex)
Creates a recruit action for the current side.
Definition: manager.cpp:848
bool wait_for_side_init_
Definition: manager.hpp:207
void post_draw()
Called from the display after drawing.
Definition: manager.cpp:528
int get_spent_gold_for(int side)
Used to track gold spending per-side when building the planned unit map Is referenced by the top bar ...
Definition: manager.cpp:1063
void erase_temp_move()
Erase the temporary arrow.
Definition: manager.cpp:743
bool can_modify_game_state() const
Determine whether the game is initialized and the current side has control of the game i...
Definition: manager.cpp:143
std::vector< config > net_buffer_
net_buffer_[i] = whiteboard network data to be sent "from" teams[i].
Definition: manager.hpp:238
GLboolean invert
Definition: glew.h:1390
ONLY IF whiteboard is currently active, applies the planned unit map for the duration of the struct's...
Definition: manager.hpp:269
void create_temp_move()
Creates a temporary visual arrow, that follows the cursor, for move creation purposes.
Definition: manager.cpp:641
bool allow_leader_to_move(unit const &leader) const
Used to ask permission to the wb to move a leader, to avoid invalidating planned recruits.
Definition: manager.cpp:259
void contextual_bump_up_action()
Moves the action determined by the UI toward the beginning of the queue.
Definition: manager.cpp:1026
GLuint const GLchar * name
Definition: glew.h:1782
void on_init_side()
The on_* methods below inform the whiteboard of specific events.
Definition: manager.cpp:292
bool save_recall(const unit &unit, int side_num, const map_location &recall_hex)
Creates a recall action for the current side.
Definition: manager.cpp:876
whiteboard_lock get_activation_state_lock()
Prevents the whiteboard from changing its activation state, as long as the returned reference is held...
Definition: manager.hpp:69
bool is_executing_actions() const
Is the whiteboard in the process of executing an action?
Definition: manager.hpp:72
void print_help_once()
Definition: manager.cpp:103
void send_network_data()
Called by replay_network_sender to add whiteboard data to the outgoing network packets.
Definition: manager.cpp:598
static int cond(LexState *ls)
Definition: lparser.cpp:1168
boost::weak_ptr< highlighter > get_highlighter()
Get the highlight visitor instance in use by the manager.
Definition: manager.hpp:170
bool should_clear_undo() const
Determines whether or not the undo_stack should be cleared.
Definition: manager.hpp:183
bool has_temp_move() const
Informs whether an arrow is being displayed for move creation purposes.
Definition: manager.hpp:134
Ensures that the real unit map is active for the duration of the struct's life.
Definition: manager.hpp:279
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool executing_actions_
Track whenever we're modifying actions, to avoid dual execution etc.
Definition: manager.hpp:210
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27
future_map_if(bool cond)
Definition: manager.hpp:262
bool inverted_behavior_
Definition: manager.hpp:202
bool execute_all_actions()
Executes all actions for the current turn in sequence.
Definition: manager.cpp:951
void on_mouseover_change(const map_location &hex)
Definition: manager.cpp:566
void validate_viewer_actions()
Validates all actions of the current viewing side.
Definition: manager.cpp:421
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Definition: display.hpp:47
bool unit_has_actions(unit const *unit) const
Checks whether the specified unit has at least one planned action.
Definition: manager.cpp:1056
bool has_planned_unit_map() const
Whether the planned unit map is currently applied.
Definition: manager.hpp:113
void set_planned_unit_map()
Transforms the unit map so that it now reflects the future state of things, i.e.
Definition: manager.cpp:1133
void pre_delete_action(action_ptr action)
Handles various cleanup right before removing an action from the queue.
Definition: manager.cpp:320