The Battle for Wesnoth  1.17.21+dev
utility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2023
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 <algorithm>
21 #include <iterator>
22 #include <limits>
23 
24 #include "whiteboard/utility.hpp"
25 
26 #include "whiteboard/manager.hpp"
28 
29 #include "actions/create.hpp"
30 #include "display.hpp"
31 #include "map/map.hpp"
32 #include "play_controller.hpp"
33 #include "resources.hpp"
34 #include "team.hpp"
35 #include "units/unit.hpp"
37 #include "utils/iterable_pair.hpp"
38 
39 namespace wb {
40 
41 std::size_t viewer_team()
42 {
44 }
45 
47 {
49 }
50 
52 {
55  return side_actions;
56 }
57 
59 {
62  return side_actions;
63 }
64 
66 {
67  assert(leader.can_recruit());
68  assert(resources::gameboard->map().is_keep(leader.get_location()));
70  {
71  if (unit->can_recruit() && unit->id() != leader.id())
72  {
73  if (dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(*unit, leader.get_location()))
74  return unit.get_shared_ptr();
75  }
76  }
77  return unit_const_ptr();
78 }
79 
80 unit* find_recruiter(std::size_t team_index, const map_location& hex)
81 {
82  if ( !resources::gameboard->map().is_castle(hex) )
83  return nullptr;
84 
85  for(unit& u : resources::gameboard->units())
86  if(u.can_recruit()
87  && u.side() == static_cast<int>(team_index+1)
88  && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, hex))
89  return &u;
90  return nullptr;
91 }
92 
93 bool any_recruiter(int team_num, const map_location& loc, std::function<bool(unit&)> func)
94 {
95  if ( !resources::gameboard->map().is_castle(loc) ) {
96  return false;
97  }
98 
99  for(unit& u : resources::gameboard->units()) {
100  if(u.can_recruit() && u.side() == team_num && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, loc)) {
101  if(func(u)) {
102  return true;
103  }
104  }
105  }
106  return false;
107 }
108 
110 {
111  future_map planned_unit_map;
112  if(!resources::whiteboard->has_planned_unit_map())
113  {
114  ERR_WB << "future_visible_unit cannot find unit, future unit map failed to build.";
115  return nullptr;
116  }
117  //use global method get_visible_unit
119 }
120 
122 {
124  if (unit && unit->side() == on_side)
125  return unit;
126  else
127  return nullptr;
128 }
129 
130 int path_cost(const std::vector<map_location>& path, const unit& u)
131 {
132  if(path.size() < 2)
133  return 0;
134 
135  const team& u_team = resources::gameboard->get_team(u.side());
136  const map_location& dest = path.back();
137  if ( (resources::gameboard->map().is_village(dest) && !u_team.owns_village(dest))
138  || pathfind::enemy_zoc(u_team, dest, u_team) )
139  return u.total_movement();
140 
141  int result = 0;
142  const gamemap& map = resources::gameboard->map();
143  for(const map_location& loc : std::pair(path.begin()+1,path.end())) {
144  result += u.movement_cost(map[loc]);
145  }
146  return result;
147 }
148 
150  : unit_(&u)
151  {unit_->set_hidden(true);}
153 {
154  try {
155  unit_->set_hidden(false);
156  } catch (...) {}
157 }
158 
160 {
163 }
164 
166 {
167  unit->anim_comp().set_standing(true);
169 }
170 
172 {
173  for (team& t : resources::gameboard->teams()) {
174  if (!t.get_side_actions()->empty())
175  return true;
176  }
177 
178  return false;
179 }
180 
182 {
183  return !t.get_side_actions()->hidden();
184 }
185 
186 void for_each_action(std::function<void(action*)> function, team_filter team_filter)
187 {
188  bool end = false;
189  for(std::size_t turn=0; !end; ++turn) {
190  end = true;
191  for(team &side : resources::gameboard->teams()) {
192  side_actions &actions = *side.get_side_actions();
193  if(turn < actions.num_turns() && team_filter(side)) {
194  for(auto iter = actions.turn_begin(turn); iter != actions.turn_end(turn); ++iter) {
195  function(iter->get());
196  }
197  end = false;
198  }
199  }
200  }
201 }
202 
204 {
205  action_ptr result;
206  std::size_t result_turn = std::numeric_limits<std::size_t>::max();
207 
208  for(team &side : resources::gameboard->teams()) {
209  side_actions &actions = *side.get_side_actions();
210  if(team_filter(side)) {
211  side_actions::iterator chall = actions.find_first_action_at(hex);
212  if(chall == actions.end()) {
213  continue;
214  }
215 
216  std::size_t chall_turn = actions.get_turn(chall);
217  if(chall_turn < result_turn) {
218  result = *chall;
219  result_turn = chall_turn;
220  }
221  }
222  }
223 
224  return result;
225 }
226 
227 std::deque<action_ptr> find_actions_of(const unit& target)
228 {
229  return resources::gameboard->get_team(target.side()).get_side_actions()->actions_of(target);
230 }
231 
232 } //end namespace wb
double t
Definition: astarsearch.cpp:65
int viewing_side() const
The 1-based equivalent of the 0-based viewing_team() function.
Definition: display.hpp:130
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:122
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3094
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:101
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:86
team & get_team(int i)
Definition: game_board.hpp:98
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:212
virtual const unit_map & units() const override
Definition: game_board.hpp:113
virtual const gamemap & map() const override
Definition: game_board.hpp:103
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:76
bool owns_village(const map_location &loc) const
Definition: team.hpp:173
std::shared_ptr< wb::side_actions > get_side_actions() const
get the whiteboard planned actions for this team
Definition: team.hpp:373
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:134
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
Various functions related to the creation of units (recruits, recalls, and placed units).
const unit * unit_
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
Definition: unit.cpp:2793
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:613
const std::string & id() const
Gets this unit's id.
Definition: unit.hpp:381
int side() const
The side this unit belongs to.
Definition: unit.hpp:344
unit_animation_component & anim_comp() const
Definition: unit.hpp:1545
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1358
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1441
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1267
std::string path
Definition: filesystem.cpp:86
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:21
play_controller * controller
Definition: resources.cpp:22
filter_context * filter_con
Definition: resources.cpp:24
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:34
Definition: display.hpp:49
side_actions_ptr current_side_actions()
Definition: utility.cpp:58
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:186
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:159
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:181
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:80
std::size_t viewer_team()
Definition: utility.cpp:41
int viewer_side()
Definition: utility.cpp:46
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:203
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:93
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:109
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:130
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:65
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:227
side_actions_ptr viewer_actions()
Definition: utility.cpp:51
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:171
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:165
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:149
#define ERR_WB
Definition: typedefs.hpp:25