The Battle for Wesnoth  1.15.1+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 "utils/functional.hpp"
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 /// 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
65 
66 /// Applies the future unit map and @return a pointer to the unit at hex
67 /// @retval nullptr if none is visible to the specified viewer side
68 /// @param on_side Only search for units of this side.
70 
71 /// Computes the MP cost for u to travel path
72 int path_cost(const std::vector<map_location>& path, const unit& u);
73 
77  unit* const unit_;
78 };
79 
80 /**
81  * Finalizer class to help with exception safety
82  * sets variable to value on destruction
83  */
84 template <typename T>
86 {
87 public:
88  variable_finalizer(T & variable, T value):
89  variable_(&variable),
90  value_(value)
91  {}
93  {
94  if(variable_ != nullptr) {
95  *variable_ = value_;
96  }
97  }
98  /** Stop tracking the variable, i.e. this object won't do anything on destruction. */
99  void clear()
100  {
101  variable_ = nullptr;
102  }
103 private:
106 };
107 
108 void ghost_owner_unit(unit* unit);
110 
111 /** Return whether the whiteboard has actions. */
112 bool has_actions();
113 
114 /**
115  * Callable object class to filter teams.
116  *
117  * The argument is the team to consider.
118  */
119 typedef std::function<bool(team&)> team_filter;
120 
121 /** Returns whether a given team's plan is visible. */
123 
124 /**
125  * Apply a function to all the actions of the whiteboard.
126  *
127  * The actions are processed chronologically.
128  * 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.
129  *
130  * @param function the function to execute.
131  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
132  */
133 void for_each_action(std::function<void(action*)> function,
134  team_filter team_filter = team_has_visible_plan);
135 
136 /**
137  * Find the first action occurring on a given hex.
138  *
139  * The actions are processed chronologically.
140  * 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.
141  *
142  * @param hex where to search for an action.
143  * @param team_filter select whether a team is visited (default to @ref team_has_visible_plan).
144  * @retval action_ptr() when no action verifying the team_filter are present on the given hex.
145  */
146 action_ptr find_action_at(map_location hex, team_filter team_filter = team_has_visible_plan);
147 
148 /**
149  * Find the actions of an unit.
150  *
151  * @param target the unit owning the actions.
152  */
153 std::deque<action_ptr> find_actions_of(const unit& target);
154 
155 } //end namespace wb
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
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:99
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
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:99
Contains typedefs for the whiteboard.
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:61
std::string path
Definition: game_config.cpp:39
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:42
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:119
Finalizer class to help with exception safety sets variable to value on destruction.
Definition: utility.hpp:85
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:88
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