The Battle for Wesnoth  1.17.14+dev
utility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2022
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
118  return resources::gameboard->get_visible_unit(hex, resources::gameboard->get_team(viewer_side), false);
119 }
120 
122 {
123  unit* unit = future_visible_unit(hex, viewer_side);
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 {
161  unit->anim_comp().set_disabled_ghosted(false);
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
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
container::iterator iterator
play_controller * controller
Definition: resources.cpp:22
std::shared_ptr< wb::side_actions > get_side_actions() const
get the whiteboard planned actions for this team
Definition: team.hpp:373
int viewer_side()
Definition: utility.cpp:46
bool team_has_visible_plan(team &t)
Returns whether a given team&#39;s plan is visible.
Definition: utility.cpp:181
iterator end()
Returns the iterator for the position after the last executed action within the actions queue...
unit_iterator end()
Definition: map.hpp:429
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:98
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:86
std::deque< action_ptr > find_actions_of(const unit &target)
Find the actions of an unit.
Definition: utility.cpp:227
virtual const unit_map & units() const override
Definition: game_board.hpp:113
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3122
This class represents a single unit of a specific type.
Definition: unit.hpp:133
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit&#39;s movement cost on a particular terrain.
Definition: unit.hpp:1443
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
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:327
std::shared_ptr< side_actions > side_actions_ptr
Definition: typedefs.hpp:66
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:80
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:171
virtual const gamemap & map() const override
Definition: game_board.hpp:103
int viewing_side() const
Definition: display.hpp:113
const unit * unit_
temporary_unit_hider(unit &u)
Definition: utility.cpp:149
std::size_t get_turn(const_iterator it) const
Returns the turn of a given iterator planned execution.
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:165
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
iterator find_first_action_at(map_location hex)
Find the first action occurring at a given hex.
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:75
team & get_team(int i)
Definition: game_board.hpp:98
const std::string & id() const
Gets this unit&#39;s id.
Definition: unit.hpp:383
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:212
Applies the planned unit map for the duration of the struct&#39;s life.
Definition: manager.hpp:252
filter_context * filter_con
Definition: resources.cpp:24
game_board * gameboard
Definition: resources.cpp:21
#define ERR_WB
Definition: typedefs.hpp:25
Encapsulates the map of the game.
Definition: map.hpp:171
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
std::string path
Definition: game_config.cpp:39
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
Definition: unit.cpp:2711
map_display and display: classes which take care of displaying the map and game-data on the screen...
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:159
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
iterator turn_begin(std::size_t turn_num)
Encapsulates the map of the game.
Definition: location.hpp:38
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:34
Various functions related to the creation of units (recruits, recalls, and placed units)...
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_animation_component & anim_comp() const
Definition: unit.hpp:1547
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:615
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
side_actions_ptr viewer_actions()
Definition: utility.cpp:51
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
double t
Definition: astarsearch.cpp:65
iterator turn_end(std::size_t turn_num)
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:126
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1360
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:112
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1269
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:109
int side() const
The side this unit belongs to.
Definition: unit.hpp:346
void set_standing(bool with_bars=true)
Sets the animation state to standing.
std::size_t num_turns() const
Returns the number of turns that have plans.
bool owns_village(const map_location &loc) const
Definition: team.hpp:173
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
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
This internal whiteboard class holds the planned action queues for a team, and offers many utility me...
Definition: display.hpp:49
std::size_t viewer_team()
Definition: utility.cpp:41
side_actions_ptr current_side_actions()
Definition: utility.cpp:58