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