The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 - 2025
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
29class unit;
30class team;
31
32namespace wb {
33
34/** @return The current viewing side's number (i.e. team index + 1) */
35int 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 */
53unit* 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 */
58bool any_recruiter(int side_num, const map_location& loc, const 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 */
73const unit* future_visible_unit(int on_side, map_location hex, int viewer_side = wb::viewer_side());
74
75/** Computes the MP cost for u to travel path */
76int path_cost(const std::vector<map_location>& path, const unit& u);
77
83
84/**
85 * Finalizer class to help with exception safety
86 * sets variable to value on destruction
87 */
88template <typename T>
90{
91public:
92 variable_finalizer(T & variable, T value):
93 variable_(&variable),
95 {}
97 {
98 if(variable_ != nullptr) {
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 }
107private:
110};
111
114
115/** Return whether the whiteboard has actions. */
116bool has_actions();
117
118/**
119 * Callable object class to filter teams.
120 *
121 * The argument is the team to consider.
122 */
123typedef 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 */
137void for_each_action(const 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 */
157std::deque<action_ptr> find_actions_of(const unit& target);
158
159} //end namespace wb
map_location loc
Definition move.cpp:172
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:39
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
Definition display.hpp:45
side_actions_ptr current_side_actions()
Definition utility.cpp:48
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, const team_filter &team_filter)
Find the first action occurring on a given hex.
Definition utility.cpp:193
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
bool any_recruiter(int team_num, const map_location &loc, const std::function< bool(unit &)> &func)
executes func for each unti of side of side_num that can recruit on loc.
Definition utility.cpp:83
void for_each_action(const std::function< void(action *)> &function, const team_filter &team_filter)
Apply a function to all the actions of the whiteboard.
Definition utility.cpp:176
void unghost_owner_unit(unit *unit)
Definition utility.cpp:155
const unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition utility.cpp:99
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
Encapsulates the map of the game.
Definition location.hpp:46
Contains typedefs for the whiteboard.