The Battle for Wesnoth  1.19.5+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 {
38 {
40 }
41 
43 {
45  return side_actions;
46 }
47 
49 {
52  return side_actions;
53 }
54 
56 {
57  assert(leader.can_recruit());
58  assert(resources::gameboard->map().is_keep(leader.get_location()));
60  {
61  if (unit->can_recruit() && unit->id() != leader.id())
62  {
63  if (dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(*unit, leader.get_location()))
64  return unit.get_shared_ptr();
65  }
66  }
67  return unit_const_ptr();
68 }
69 
70 unit* find_recruiter(std::size_t team_index, const map_location& hex)
71 {
72  if ( !resources::gameboard->map().is_castle(hex) )
73  return nullptr;
74 
75  for(unit& u : resources::gameboard->units())
76  if(u.can_recruit()
77  && u.side() == static_cast<int>(team_index+1)
78  && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, hex))
79  return &u;
80  return nullptr;
81 }
82 
83 bool any_recruiter(int team_num, const map_location& loc, std::function<bool(unit&)> func)
84 {
85  if ( !resources::gameboard->map().is_castle(loc) ) {
86  return false;
87  }
88 
89  for(unit& u : resources::gameboard->units()) {
90  if(u.can_recruit() && u.side() == team_num && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, loc)) {
91  if(func(u)) {
92  return true;
93  }
94  }
95  }
96  return false;
97 }
98 
100 {
101  future_map planned_unit_map;
102  if(!resources::whiteboard->has_planned_unit_map())
103  {
104  ERR_WB << "future_visible_unit cannot find unit, future unit map failed to build.";
105  return nullptr;
106  }
107  //use global method get_visible_unit
109 }
110 
112 {
114  if (unit && unit->side() == on_side)
115  return unit;
116  else
117  return nullptr;
118 }
119 
120 int path_cost(const std::vector<map_location>& path, const unit& u)
121 {
122  if(path.size() < 2)
123  return 0;
124 
125  const team& u_team = resources::gameboard->get_team(u.side());
126  const map_location& dest = path.back();
127  if ( (resources::gameboard->map().is_village(dest) && !u_team.owns_village(dest))
128  || pathfind::enemy_zoc(u_team, dest, u_team) )
129  return u.total_movement();
130 
131  int result = 0;
132  const gamemap& map = resources::gameboard->map();
133  for(const map_location& loc : std::pair(path.begin()+1,path.end())) {
134  result += u.movement_cost(map[loc]);
135  }
136  return result;
137 }
138 
140  : unit_(&u)
141  {unit_->set_hidden(true);}
143 {
144  try {
145  unit_->set_hidden(false);
146  } catch (...) {}
147 }
148 
150 {
153 }
154 
156 {
157  unit->anim_comp().set_standing(true);
159 }
160 
162 {
163  for (team& t : resources::gameboard->teams()) {
164  if (!t.get_side_actions()->empty())
165  return true;
166  }
167 
168  return false;
169 }
170 
172 {
173  return !t.get_side_actions()->hidden();
174 }
175 
176 void for_each_action(std::function<void(action*)> function, team_filter team_filter)
177 {
178  bool end = false;
179  for(std::size_t turn=0; !end; ++turn) {
180  end = true;
181  for(team &side : resources::gameboard->teams()) {
182  side_actions &actions = *side.get_side_actions();
183  if(turn < actions.num_turns() && team_filter(side)) {
184  for(auto iter = actions.turn_begin(turn); iter != actions.turn_end(turn); ++iter) {
185  function(iter->get());
186  }
187  end = false;
188  }
189  }
190  }
191 }
192 
194 {
195  action_ptr result;
196  std::size_t result_turn = std::numeric_limits<std::size_t>::max();
197 
198  for(team &side : resources::gameboard->teams()) {
199  side_actions &actions = *side.get_side_actions();
200  if(team_filter(side)) {
201  side_actions::iterator chall = actions.find_first_action_at(hex);
202  if(chall == actions.end()) {
203  continue;
204  }
205 
206  std::size_t chall_turn = actions.get_turn(chall);
207  if(chall_turn < result_turn) {
208  result = *chall;
209  result_turn = chall_turn;
210  }
211  }
212  }
213 
214  return result;
215 }
216 
217 std::deque<action_ptr> find_actions_of(const unit& target)
218 {
219  return resources::gameboard->get_team(target.side()).get_side_actions()->actions_of(target);
220 }
221 
222 } //end namespace wb
double t
Definition: astarsearch.cpp:63
const team & viewing_team() const
Definition: display.cpp:342
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3087
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:111
team & get_team(int i)
Definition: game_board.hpp:92
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:107
virtual const gamemap & map() const override
Definition: game_board.hpp:97
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:320
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:75
int side() const
Definition: team.hpp:175
bool owns_village(const map_location &loc) const
Definition: team.hpp:172
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
map_display and display: classes which take care of displaying the map and game-data on the screen.
const unit * unit_
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
Definition: unit.cpp:2776
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:1614
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1404
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1487
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1313
std::string path
Definition: filesystem.cpp:90
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:48
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:176
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, team_filter team_filter)
Find the first action occurring on a given hex.
Definition: utility.cpp:193
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:83
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:99
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
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:155
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Encapsulates the map of the game.
Definition: location.hpp:45
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:139
#define ERR_WB
Definition: typedefs.hpp:25