The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utility.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
3  Part of the Battle for Wesnoth Project http://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 #ifndef WB_UTILITY_HPP_
20 #define WB_UTILITY_HPP_
21 
22 #include <vector>
23 #include <deque>
24 
25 #include "utils/functional.hpp"
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 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(size_t team_index, map_location const&);
57 
58 /// Applies the future unit map and @return a pointer to the unit at hex
59 /// @retval nullptr if none is visible to the specified viewer side
61 
62 /// Applies the future unit map and @return a pointer to the unit at hex
63 /// @retval nullptr if none is visible to the specified viewer side
64 /// @param on_side Only search for units of this side.
66 
67 /// Computes the MP cost for u to travel path
68 int path_cost(std::vector<map_location> const& path, unit const& u);
69 
73  unit* const unit_;
74 };
75 
76 /**
77  * Finalizer class to help with exception safety
78  * sets variable to value on destruction
79  */
80 template <typename T>
82 {
83 public:
85  variable_(&variable),
86  value_(value)
87  {}
89  {
90  if(variable_ != nullptr) {
91  *variable_ = value_;
92  }
93  }
94  /** Stop tracking the variable, i.e. this object won't do anything on destruction. */
95  void clear()
96  {
97  variable_ = nullptr;
98  }
99 private:
102 };
103 
104 void ghost_owner_unit(unit* unit);
106 
107 /** Return whether the whiteboard has actions. */
108 bool has_actions();
109 
110 /**
111  * Callable object class to filter teams.
112  *
113  * The argument is the team to consider.
114  */
115 typedef std::function<bool(team&)> team_filter;
116 
117 /** Returns whether a given team's plan is visible. */
119 
120 /**
121  * Apply a function to all the actions of the whiteboard.
122  *
123  * The actions are processed chronologically.
124  * 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.
125  *
126  * @param function the function to execute.
127  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
128  */
129 void for_each_action(std::function<void(action*)> function,
130  team_filter team_filter = team_has_visible_plan);
131 
132 /**
133  * Find the first action occuring on a given hex.
134  *
135  * The actions are processed chronologically.
136  * 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.
137  *
138  * @param hex where to search for an action.
139  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
140  * @retval action_ptr() when no action verifying the team_filter are present on the given hex.
141  */
142 action_ptr find_action_at(map_location hex, team_filter team_filter = team_has_visible_plan);
143 
144 /**
145  * Find the actions of an unit.
146  *
147  * @param target the unit owning the actions.
148  */
149 std::deque<action_ptr> find_actions_of(unit const &target);
150 
151 } //end namespace wb
152 
153 #endif /* WB_UTILITY_HPP_ */
int viewer_side()
Definition: utility.cpp:44
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:163
Definition: unit.hpp:95
size_t viewer_team()
Definition: utility.cpp:39
unit * find_recruiter(size_t team_index, map_location const &hex)
Definition: utility.cpp:78
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:153
int path_cost(std::vector< map_location > const &path, unit const &u)
Computes the MP cost for u to travel path.
Definition: utility.cpp:112
temporary_unit_hider(unit &u)
Definition: utility.cpp:131
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:147
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
void clear()
Stop tracking the variable, i.e.
Definition: utility.hpp:95
GLsizei const char ** path
Definition: glew.h:4654
GLsizei const GLfloat * value
Definition: glew.h:1817
Contains typedefs for the whiteboard.
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:141
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:168
Encapsulates the map of the game.
Definition: location.hpp:38
GLenum GLenum variable
Definition: glew.h:10668
std::deque< action_ptr > find_actions_of(unit const &target)
Find the actions of an unit.
Definition: utility.cpp:209
side_actions_ptr viewer_actions()
Definition: utility.cpp:49
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:115
Finalizer class to help with exception safety sets variable to value on destruction.
Definition: utility.hpp:81
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:63
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:91
variable_finalizer(T &variable, T value)
Definition: utility.hpp:84
boost::shared_ptr< side_actions > side_actions_ptr
Definition: typedefs.hpp:74
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
action_ptr find_action_at(map_location hex, team_filter team_filter)
Find the first action occuring on a given hex.
Definition: utility.cpp:185
Definition: display.hpp:47
GLenum target
Definition: glew.h:5190
side_actions_ptr current_side_actions()
Definition: utility.cpp:56