The Battle for Wesnoth  1.19.5+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 side's number (i.e. team index + 1) */
35 int viewer_side();
36 
37 /** @return The side_actions instance belonging to the current viewing team */
39 
40 /** @return The side_actions instance belonging to the current playing team */
42 
43 /**
44  * For a given leader on a keep, find another leader on another keep in the same castle.
45  * @retval nullptr if no such leader has been found
46  */
48 
49 /**
50  * @return a leader from the specified team who can recruit on the specified hex
51  * @retval nullptr if no such leader has been found
52  */
53 unit* find_recruiter(std::size_t team_index, const map_location&);
54 /**
55  * executes @a func for each unti of side of @a side_num that can recruit on @a loc.
56  * @a func takes the leader unit and can return true to 'break' the loop
57  */
58 bool any_recruiter(int side_num, const map_location& loc, std::function<bool(unit&)> func);
59 
60 /**
61  * Applies the future unit map and @return a pointer to the unit at hex
62  * @retval nullptr if none is visible to the specified viewer side
63  */
65 
66 /**
67  * Applies the future unit map and @return a pointer to the unit at hex
68  * @retval nullptr if none is visible to the specified viewer side
69  * @param on_side Only search for units of this side.
70  * @param hex
71  * @param viewer_side
72  */
74 
75 /** Computes the MP cost for u to travel path */
76 int path_cost(const std::vector<map_location>& path, const unit& u);
77 
81  unit* const unit_;
82 };
83 
84 /**
85  * Finalizer class to help with exception safety
86  * sets variable to value on destruction
87  */
88 template <typename T>
90 {
91 public:
92  variable_finalizer(T & variable, T value):
93  variable_(&variable),
94  value_(value)
95  {}
97  {
98  if(variable_ != nullptr) {
99  *variable_ = value_;
100  }
101  }
102  /** Stop tracking the variable, i.e. this object won't do anything on destruction. */
103  void clear()
104  {
105  variable_ = nullptr;
106  }
107 private:
110 };
111 
112 void ghost_owner_unit(unit* unit);
114 
115 /** Return whether the whiteboard has actions. */
116 bool has_actions();
117 
118 /**
119  * Callable object class to filter teams.
120  *
121  * The argument is the team to consider.
122  */
123 typedef std::function<bool(team&)> team_filter;
124 
125 /** Returns whether a given team's plan is visible. */
127 
128 /**
129  * Apply a function to all the actions of the whiteboard.
130  *
131  * The actions are processed chronologically.
132  * 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.
133  *
134  * @param function the function to execute.
135  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
136  */
137 void for_each_action(std::function<void(action*)> function,
139 
140 /**
141  * Find the first action occurring on a given hex.
142  *
143  * The actions are processed chronologically.
144  * 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.
145  *
146  * @param hex where to search for an action.
147  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
148  * @retval action_ptr() when no action verifying the team_filter are present on the given hex.
149  */
151 
152 /**
153  * Find the actions of an unit.
154  *
155  * @param target the unit owning the actions.
156  */
157 std::deque<action_ptr> find_actions_of(const unit& target);
158 
159 } //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:90
void clear()
Stop tracking the variable, i.e.
Definition: utility.hpp:103
variable_finalizer(T &variable, T value)
Definition: utility.hpp:92
std::string path
Definition: filesystem.cpp:90
Definition: display.hpp:45
side_actions_ptr current_side_actions()
Definition: utility.cpp:48
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:176
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:149
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:171
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:70
int viewer_side()
Definition: utility.cpp:37
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:193
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:83
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:99
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:120
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:55
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:123
std::deque< action_ptr > find_actions_of(const unit &target)
Find the actions of an unit.
Definition: utility.cpp:217
side_actions_ptr viewer_actions()
Definition: utility.cpp:42
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:161
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:155
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Encapsulates the map of the game.
Definition: location.hpp:45
temporary_unit_hider(unit &u)
Definition: utility.cpp:139
Contains typedefs for the whiteboard.