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