-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
utility.hpp
153 lines (124 loc) · 4.35 KB
/
utility.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright (C) 2010 - 2014 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
*/
#ifndef WB_UTILITY_HPP_
#define WB_UTILITY_HPP_
#include <vector>
#include <deque>
#include <boost/function.hpp>
#include "typedefs.hpp"
class unit;
class team;
namespace wb {
/// @return The current viewing team's index
size_t viewer_team();
/// @return The current viewing side's number (i.e. team index + 1)
int viewer_side();
/// @return The side_actions instance belonging to the current viewing team
side_actions_ptr viewer_actions();
/// @return The side_actions instance belonging to the current playing team
side_actions_ptr current_side_actions();
/**
* For a given leader on a keep, find another leader on another keep in the same castle.
* @retval NULL if no such leader has been found
*/
UnitConstPtr find_backup_leader(unit const& leader);
/**
* @return a leader from the specified team who can recruit on the specified hex
* @retval NULL if no such leader has been found
*/
unit* find_recruiter(size_t team_index, map_location const&);
/// Applies the future unit map and @return a pointer to the unit at hex
/// @retval NULL if none is visible to the specified viewer side
unit* future_visible_unit(map_location hex, int viewer_side = wb::viewer_side());
/// Applies the future unit map and @return a pointer to the unit at hex
/// @retval NULL if none is visible to the specified viewer side
/// @param on_side Only search for units of this side.
unit* future_visible_unit(int on_side, map_location hex, int viewer_side = wb::viewer_side());
/// Computes the MP cost for u to travel path
int path_cost(std::vector<map_location> const& path, unit const& u);
struct temporary_unit_hider {
temporary_unit_hider(unit& u);
~temporary_unit_hider();
unit* const unit_;
};
/**
* Finalizer class to help with exception safety
* sets variable to value on destruction
*/
template <typename T>
class variable_finalizer
{
public:
variable_finalizer(T & variable, T value):
variable_(&variable),
value_(value)
{}
~variable_finalizer()
{
if(variable_ != NULL) {
*variable_ = value_;
}
}
/** Stop tracking the variable, i.e. this object won't do anything on destruction. */
void clear()
{
variable_ = NULL;
}
private:
T * variable_;
T value_;
};
void ghost_owner_unit(unit* unit);
void unghost_owner_unit(unit* unit);
/** Return whether the whiteboard has actions. */
bool has_actions();
/**
* Callable object class to filter teams.
*
* The argument is the team to consider.
*/
typedef boost::function<bool(team&)> team_filter;
/** Returns whether a given team's plan is visible. */
bool team_has_visible_plan(team&);
/**
* Apply a function to all the actions of the whiteboard.
*
* The actions are processed chronologically.
* 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.
*
* @param function the function to execute.
* @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
*/
void for_each_action(boost::function<void(action_ptr)> function,
team_filter team_filter = team_has_visible_plan);
/**
* Find the first action occuring on a given hex.
*
* The actions are processed chronologically.
* 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.
*
* @param hex where to search for an action.
* @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
* @retval action_ptr() when no action verifying the team_filter are present on the given hex.
*/
action_ptr find_action_at(map_location hex, team_filter team_filter = team_has_visible_plan);
/**
* Find the actions of an unit.
*
* @param target the unit owning the actions.
*/
std::deque<action_ptr> find_actions_of(unit const &target);
} //end namespace wb
#endif /* WB_UTILITY_HPP_ */