The Battle for Wesnoth  1.19.0-dev
utility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 #include <limits>
21 
22 #include "whiteboard/utility.hpp"
23 
24 #include "whiteboard/manager.hpp"
26 
27 #include "display.hpp"
28 #include "map/map.hpp"
29 #include "play_controller.hpp"
30 #include "resources.hpp"
31 #include "team.hpp"
32 #include "units/unit.hpp"
34 #include "utils/iterable_pair.hpp"
35 
36 namespace wb {
37 
38 std::size_t viewer_team()
39 {
41 }
42 
44 {
46 }
47 
49 {
52  return side_actions;
53 }
54 
56 {
59  return side_actions;
60 }
61 
63 {
64  assert(leader.can_recruit());
65  assert(resources::gameboard->map().is_keep(leader.get_location()));
67  {
68  if (unit->can_recruit() && unit->id() != leader.id())
69  {
70  if (dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(*unit, leader.get_location()))
71  return unit.get_shared_ptr();
72  }
73  }
74  return unit_const_ptr();
75 }
76 
77 unit* find_recruiter(std::size_t team_index, const map_location& hex)
78 {
79  if ( !resources::gameboard->map().is_castle(hex) )
80  return nullptr;
81 
82  for(unit& u : resources::gameboard->units())
83  if(u.can_recruit()
84  && u.side() == static_cast<int>(team_index+1)
85  && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, hex))
86  return &u;
87  return nullptr;
88 }
89 
90 bool any_recruiter(int team_num, const map_location& loc, std::function<bool(unit&)> func)
91 {
92  if ( !resources::gameboard->map().is_castle(loc) ) {
93  return false;
94  }
95 
96  for(unit& u : resources::gameboard->units()) {
97  if(u.can_recruit() && u.side() == team_num && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, loc)) {
98  if(func(u)) {
99  return true;
100  }
101  }
102  }
103  return false;
104 }
105 
107 {
108  future_map planned_unit_map;
109  if(!resources::whiteboard->has_planned_unit_map())
110  {
111  ERR_WB << "future_visible_unit cannot find unit, future unit map failed to build.";
112  return nullptr;
113  }
114  //use global method get_visible_unit
116 }
117 
119 {
121  if (unit && unit->side() == on_side)
122  return unit;
123  else
124  return nullptr;
125 }
126 
127 int path_cost(const std::vector<map_location>& path, const unit& u)
128 {
129  if(path.size() < 2)
130  return 0;
131 
132  const team& u_team = resources::gameboard->get_team(u.side());
133  const map_location& dest = path.back();
134  if ( (resources::gameboard->map().is_village(dest) && !u_team.owns_village(dest))
135  || pathfind::enemy_zoc(u_team, dest, u_team) )
136  return u.total_movement();
137 
138  int result = 0;
139  const gamemap& map = resources::gameboard->map();
140  for(const map_location& loc : std::pair(path.begin()+1,path.end())) {
141  result += u.movement_cost(map[loc]);
142  }
143  return result;
144 }
145 
147  : unit_(&u)
148  {unit_->set_hidden(true);}
150 {
151  try {
152  unit_->set_hidden(false);
153  } catch (...) {}
154 }
155 
157 {
160 }
161 
163 {
164  unit->anim_comp().set_standing(true);
166 }
167 
169 {
170  for (team& t : resources::gameboard->teams()) {
171  if (!t.get_side_actions()->empty())
172  return true;
173  }
174 
175  return false;
176 }
177 
179 {
180  return !t.get_side_actions()->hidden();
181 }
182 
183 void for_each_action(std::function<void(action*)> function, team_filter team_filter)
184 {
185  bool end = false;
186  for(std::size_t turn=0; !end; ++turn) {
187  end = true;
188  for(team &side : resources::gameboard->teams()) {
189  side_actions &actions = *side.get_side_actions();
190  if(turn < actions.num_turns() && team_filter(side)) {
191  for(auto iter = actions.turn_begin(turn); iter != actions.turn_end(turn); ++iter) {
192  function(iter->get());
193  }
194  end = false;
195  }
196  }
197  }
198 }
199 
201 {
202  action_ptr result;
203  std::size_t result_turn = std::numeric_limits<std::size_t>::max();
204 
205  for(team &side : resources::gameboard->teams()) {
206  side_actions &actions = *side.get_side_actions();
207  if(team_filter(side)) {
208  side_actions::iterator chall = actions.find_first_action_at(hex);
209  if(chall == actions.end()) {
210  continue;
211  }
212 
213  std::size_t chall_turn = actions.get_turn(chall);
214  if(chall_turn < result_turn) {
215  result = *chall;
216  result_turn = chall_turn;
217  }
218  }
219  }
220 
221  return result;
222 }
223 
224 std::deque<action_ptr> find_actions_of(const unit& target)
225 {
226  return resources::gameboard->get_team(target.side()).get_side_actions()->actions_of(target);
227 }
228 
229 } //end namespace wb
double t
Definition: astarsearch.cpp:63
int viewing_side() const
The 1-based equivalent of the 0-based viewing_team() function.
Definition: display.hpp:131
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:123
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3145
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:102
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:79
team & get_team(int i)
Definition: game_board.hpp:91
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:213
virtual const unit_map & units() const override
Definition: game_board.hpp:106
virtual const gamemap & map() const override
Definition: game_board.hpp:96
bool can_recruit_on(const map_location &leader_loc, const map_location &recruit_loc, int side) const
Checks to see if a leader at leader_loc could recruit on recruit_loc.
Definition: game_state.cpp:322
Encapsulates the map of the game.
Definition: map.hpp:172
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
bool owns_village(const map_location &loc) const
Definition: team.hpp:171
std::shared_ptr< wb::side_actions > get_side_actions() const
get the whiteboard planned actions for this team
Definition: team.hpp:371
void set_standing(bool with_bars=true)
Sets the animation state to standing.
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
This internal whiteboard class holds the planned action queues for a team, and offers many utility me...
container::iterator iterator
const unit * unit_
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
Definition: unit.cpp:2778
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:612
const std::string & id() const
Gets this unit's id.
Definition: unit.hpp:380
int side() const
The side this unit belongs to.
Definition: unit.hpp:343
unit_animation_component & anim_comp() const
Definition: unit.hpp:1565
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1378
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1461
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1287
std::string path
Definition: filesystem.cpp:83
bool enemy_zoc(const team &current_team, const map_location &loc, const team &viewing_team, bool see_all)
Determines if a given location is in an enemy zone of control.
Definition: pathfind.cpp:134
game_board * gameboard
Definition: resources.cpp:20
play_controller * controller
Definition: resources.cpp:21
filter_context * filter_con
Definition: resources.cpp:23
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
Definition: display.hpp:45
side_actions_ptr current_side_actions()
Definition: utility.cpp:55
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:183
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:156
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:178
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:77
std::size_t viewer_team()
Definition: utility.cpp:38
int viewer_side()
Definition: utility.cpp:43
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
action_ptr find_action_at(map_location hex, team_filter team_filter)
Find the first action occurring on a given hex.
Definition: utility.cpp:200
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:90
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:106
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:127
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:62
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:126
std::deque< action_ptr > find_actions_of(const unit &target)
Find the actions of an unit.
Definition: utility.cpp:224
side_actions_ptr viewer_actions()
Definition: utility.cpp:48
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:168
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:162
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Encapsulates the map of the game.
Definition: location.hpp:38
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:253
temporary_unit_hider(unit &u)
Definition: utility.cpp:146
#define ERR_WB
Definition: typedefs.hpp:25