The Battle for Wesnoth  1.19.0-dev
utility.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 <vector>
23 #include <deque>
24 
25 #include <functional>
26 
27 #include "typedefs.hpp"
28 
29 class unit;
30 class team;
31 
32 namespace wb {
33 
34 /** @return The current viewing team's index */
35 std::size_t viewer_team();
36 
37 /** @return The current viewing side's number (i.e. team index + 1) */
38 int viewer_side();
39 
40 /** @return The side_actions instance belonging to the current viewing team */
42 
43 /** @return The side_actions instance belonging to the current playing team */
45 
46 /**
47  * For a given leader on a keep, find another leader on another keep in the same castle.
48  * @retval nullptr if no such leader has been found
49  */
51 
52 /**
53  * @return a leader from the specified team who can recruit on the specified hex
54  * @retval nullptr if no such leader has been found
55  */
56 unit* find_recruiter(std::size_t team_index, const map_location&);
57 /**
58  * executes @a func for each unti of side of @a side_num that can recruit on @a loc.
59  * @a func takes the leader unit and can return true to 'break' the loop
60  */
61 bool any_recruiter(int side_num, const map_location& loc, std::function<bool(unit&)> func);
62 
63 /**
64  * Applies the future unit map and @return a pointer to the unit at hex
65  * @retval nullptr if none is visible to the specified viewer side
66  */
68 
69 /**
70  * Applies the future unit map and @return a pointer to the unit at hex
71  * @retval nullptr if none is visible to the specified viewer side
72  * @param on_side Only search for units of this side.
73  * @param hex
74  * @param viewer_side
75  */
77 
78 /** Computes the MP cost for u to travel path */
79 int path_cost(const std::vector<map_location>& path, const unit& u);
80 
84  unit* const unit_;
85 };
86 
87 /**
88  * Finalizer class to help with exception safety
89  * sets variable to value on destruction
90  */
91 template <typename T>
93 {
94 public:
95  variable_finalizer(T & variable, T value):
96  variable_(&variable),
97  value_(value)
98  {}
100  {
101  if(variable_ != nullptr) {
102  *variable_ = value_;
103  }
104  }
105  /** Stop tracking the variable, i.e. this object won't do anything on destruction. */
106  void clear()
107  {
108  variable_ = nullptr;
109  }
110 private:
113 };
114 
115 void ghost_owner_unit(unit* unit);
117 
118 /** Return whether the whiteboard has actions. */
119 bool has_actions();
120 
121 /**
122  * Callable object class to filter teams.
123  *
124  * The argument is the team to consider.
125  */
126 typedef std::function<bool(team&)> team_filter;
127 
128 /** Returns whether a given team's plan is visible. */
130 
131 /**
132  * Apply a function to all the actions of the whiteboard.
133  *
134  * The actions are processed chronologically.
135  * The second parameter is a @ref team_filter, it is called for each team, if it returns false, the actions of this team won't be processed.
136  *
137  * @param function the function to execute.
138  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
139  */
140 void for_each_action(std::function<void(action*)> function,
142 
143 /**
144  * Find the first action occurring on a given hex.
145  *
146  * The actions are processed chronologically.
147  * The second parameter is a @ref team_filter, it is called for each team, if it returns false, the actions of this team won't be considered.
148  *
149  * @param hex where to search for an action.
150  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
151  * @retval action_ptr() when no action verifying the team_filter are present on the given hex.
152  */
154 
155 /**
156  * Find the actions of an unit.
157  *
158  * @param target the unit owning the actions.
159  */
160 std::deque<action_ptr> find_actions_of(const unit& target);
161 
162 } //end namespace wb
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
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
Finalizer class to help with exception safety sets variable to value on destruction.
Definition: utility.hpp:93
void clear()
Stop tracking the variable, i.e.
Definition: utility.hpp:106
variable_finalizer(T &variable, T value)
Definition: utility.hpp:95
std::string path
Definition: filesystem.cpp:84
Definition: display.hpp:45
side_actions_ptr current_side_actions()
Definition: utility.cpp:55
void for_each_action(std::function< void(action *)> function, team_filter team_filter)
Apply a function to all the actions of the whiteboard.
Definition: utility.cpp:183
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:156
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:178
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:77
std::size_t viewer_team()
Definition: utility.cpp:38
int viewer_side()
Definition: utility.cpp:43
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
action_ptr find_action_at(map_location hex, team_filter team_filter)
Find the first action occurring on a given hex.
Definition: utility.cpp:200
bool any_recruiter(int team_num, const map_location &loc, std::function< bool(unit &)> func)
executes func for each unti of side of side_num that can recruit on loc.
Definition: utility.cpp:90
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:106
std::shared_ptr< side_actions > side_actions_ptr
Definition: typedefs.hpp:66
int path_cost(const std::vector< map_location > &path, const unit &u)
Computes the MP cost for u to travel path.
Definition: utility.cpp:127
unit_const_ptr find_backup_leader(const unit &leader)
For a given leader on a keep, find another leader on another keep in the same castle.
Definition: utility.cpp:62
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:126
std::deque< action_ptr > find_actions_of(const unit &target)
Find the actions of an unit.
Definition: utility.cpp:224
side_actions_ptr viewer_actions()
Definition: utility.cpp:48
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:168
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:162
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Encapsulates the map of the game.
Definition: location.hpp:38
temporary_unit_hider(unit &u)
Definition: utility.cpp:146
Contains typedefs for the whiteboard.