The Battle for Wesnoth  1.17.0-dev
manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 */
71  whiteboard_lock get_activation_state_lock() { return activation_state_lock_; }
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);
92  void on_deselect_hex(){ erase_temp_move();}
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(action_ptr action);
99  /** Handles various cleanup right after removing an action from the queue */
100  void post_delete_action(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 */
113  void validate_viewer_actions();
114 
115  /** Whether the planned unit map is currently applied */
116  bool has_planned_unit_map() const { return planned_unit_map_active_; }
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. */
144  unit_map::iterator get_temp_move_unit() const;
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 */
168  void contextual_bump_up_action();
169  /** Moves the action determined by the UI toward the beginning of the queue */
170  void contextual_bump_down_action();
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 
198  void validate_actions_if_needed();
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 wb::viewer_team() 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  */
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
bool self_activate_once_
Definition: manager.hpp:206
whiteboard_lock unit_map_lock_
Definition: manager.hpp:288
bool initial_planned_unit_map_
Definition: manager.hpp:256
This class represents a single unit of a specific type.
Definition: unit.hpp:121
bool initial_planned_unit_map_
Definition: manager.hpp:287
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
std::shared_ptr< highlighter > highlighter_
Definition: manager.hpp:228
std::vector< arrow_ptr > move_arrows_
Definition: manager.hpp:232
whiteboard_lock activation_state_lock_
Reference counted "lock" to allow preventing whiteboard activation state changes. ...
Definition: manager.hpp:222
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:171
std::shared_ptr< bool > whiteboard_lock
Definition: typedefs.hpp:56
const std::unique_ptr< CKey > key_poller_
Definition: manager.hpp:236
bool executing_all_actions_
Track whether we&#39;re in the process of executing all actions.
Definition: manager.hpp:215
std::vector< fake_unit_ptr > fake_units_
Definition: manager.hpp:233
std::vector< map_location > hidden_unit_hexes_
Definition: manager.hpp:238
bool is_executing_actions() const
Is the whiteboard in the process of executing an action?
Definition: manager.hpp:74
bool planned_unit_map_active_
Definition: manager.hpp:211
std::weak_ptr< highlighter > get_highlighter()
Get the highlight visitor instance in use by the manager.
Definition: manager.hpp:173
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
whiteboard_lock unit_map_lock_
Reference counted "lock" to prevent the building of the unit map at certain times.
Definition: manager.hpp:224
Applies the planned unit map for the duration of the struct&#39;s life.
Definition: manager.hpp:252
This class is the frontend of the whiteboard framework for the rest of the Wesnoth code...
Definition: manager.hpp:43
bool gamestate_mutated_
Track whether the gamestate changed and we need to validate actions.
Definition: manager.hpp:219
void on_deselect_hex()
Definition: manager.hpp:92
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
std::unique_ptr< pathfind::marked_route > route_
Definition: manager.hpp:230
const std::unique_ptr< future_map > future_map_
Definition: manager.hpp:261
bool preparing_to_end_turn_
true if we&#39;re in the process of executing all action and should end turn once finished.
Definition: manager.hpp:217
bool active_
Tracks whether the whiteboard is active.
Definition: manager.hpp:204
bool has_planned_unit_map() const
Whether the planned unit map is currently applied.
Definition: manager.hpp:116
Encapsulates the map of the game.
Definition: location.hpp:38
bool wait_for_side_init_
Definition: manager.hpp:210
std::vector< config > net_buffer_
net_buffer_[i] = whiteboard network data to be sent "from" teams[i].
Definition: manager.hpp:241
ONLY IF whiteboard is currently active, applies the planned unit map for the duration of the struct&#39;s...
Definition: manager.hpp:273
bool is_active() const
Determine whether the whiteboard is activated.
Definition: manager.hpp:65
std::size_t temp_move_unit_underlying_id_
Definition: manager.hpp:234
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
double t
Definition: astarsearch.cpp:65
boost::dynamic_bitset team_plans_hidden_
team_plans_hidden_[i] = whether or not to hide actions from teams[i].
Definition: manager.hpp:244
static int cond(LexState *ls)
Definition: lparser.cpp:1394
bool has_temp_move() const
Informs whether an arrow is being displayed for move creation purposes.
Definition: manager.hpp:137
Ensures that the real unit map is active for the duration of the struct&#39;s life.
Definition: manager.hpp:283
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:61
bool executing_actions_
Track whenever we&#39;re modifying actions, to avoid dual execution etc.
Definition: manager.hpp:213
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28
std::unique_ptr< mapbuilder > mapbuilder_
Definition: manager.hpp:227
future_map_if(bool cond)
Definition: manager.hpp:266
bool inverted_behavior_
Definition: manager.hpp:205
Definition: display.hpp:49
const std::vector< map_location > & route_
Definition: move.cpp:295