The Battle for Wesnoth  1.15.2+dev
utility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13  */
14 
15 /**
16  * @file
17  */
18 
19 #include <algorithm>
20 #include <iterator>
21 #include <limits>
22 
23 #include "whiteboard/utility.hpp"
24 
25 #include "whiteboard/manager.hpp"
27 
28 #include "actions/create.hpp"
29 #include "display.hpp"
30 #include "map/map.hpp"
31 #include "play_controller.hpp"
32 #include "resources.hpp"
33 #include "team.hpp"
34 #include "units/unit.hpp"
36 #include "utils/iterable_pair.hpp"
37 
38 namespace wb {
39 
40 std::size_t viewer_team()
41 {
43 }
44 
46 {
48 }
49 
51 {
54  return side_actions;
55 }
56 
58 {
61  return side_actions;
62 }
63 
65 {
66  assert(leader.can_recruit());
67  assert(resources::gameboard->map().is_keep(leader.get_location()));
69  {
70  if (unit->can_recruit() && unit->id() != leader.id())
71  {
72  if (dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(*unit, leader.get_location()))
73  return unit.get_shared_ptr();
74  }
75  }
76  return unit_const_ptr();
77 }
78 
79 unit* find_recruiter(std::size_t team_index, const map_location& hex)
80 {
81  if ( !resources::gameboard->map().is_castle(hex) )
82  return nullptr;
83 
84  for(unit& u : resources::gameboard->units())
85  if(u.can_recruit()
86  && u.side() == static_cast<int>(team_index+1)
87  && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, hex))
88  return &u;
89  return nullptr;
90 }
91 
92 bool any_recruiter(int team_num, const map_location& loc, std::function<bool(unit&)> func)
93 {
94  if ( !resources::gameboard->map().is_castle(loc) ) {
95  return false;
96  }
97 
98  for(unit& u : resources::gameboard->units()) {
99  if(u.can_recruit() && u.side() == team_num && dynamic_cast<game_state&>(*resources::filter_con).can_recruit_on(u, loc)) {
100  if(func(u)) {
101  return true;
102  }
103  }
104  }
105  return false;
106 }
107 
109 {
110  future_map planned_unit_map;
111  if(!resources::whiteboard->has_planned_unit_map())
112  {
113  ERR_WB << "future_visible_unit cannot find unit, future unit map failed to build." << std::endl;
114  return nullptr;
115  }
116  //use global method get_visible_unit
117  return resources::gameboard->get_visible_unit(hex, resources::gameboard->get_team(viewer_side), false);
118 }
119 
121 {
122  unit* unit = future_visible_unit(hex, viewer_side);
123  if (unit && unit->side() == on_side)
124  return unit;
125  else
126  return nullptr;
127 }
128 
129 int path_cost(const std::vector<map_location>& path, const unit& u)
130 {
131  if(path.size() < 2)
132  return 0;
133 
134  const team& u_team = resources::gameboard->get_team(u.side());
135  const map_location& dest = path.back();
136  if ( (resources::gameboard->map().is_village(dest) && !u_team.owns_village(dest))
137  || pathfind::enemy_zoc(u_team, dest, u_team) )
138  return u.total_movement();
139 
140  int result = 0;
141  const gamemap& map = resources::gameboard->map();
142  for(const map_location& loc : std::make_pair(path.begin()+1,path.end())) {
143  result += u.movement_cost(map[loc]);
144  }
145  return result;
146 }
147 
149  : unit_(&u)
150  {unit_->set_hidden(true);}
152 {
153  try {
154  unit_->set_hidden(false);
155  } catch (...) {}
156 }
157 
159 {
160  unit->anim_comp().set_disabled_ghosted(false);
162 }
163 
165 {
166  unit->anim_comp().set_standing(true);
168 }
169 
171 {
172  for (team& t : resources::gameboard->teams()) {
173  if (!t.get_side_actions()->empty())
174  return true;
175  }
176 
177  return false;
178 }
179 
181 {
182  return !t.get_side_actions()->hidden();
183 }
184 
185 void for_each_action(std::function<void(action*)> function, team_filter team_filter)
186 {
187  bool end = false;
188  for(std::size_t turn=0; !end; ++turn) {
189  end = true;
190  for(team &side : resources::gameboard->teams()) {
191  side_actions &actions = *side.get_side_actions();
192  if(turn < actions.num_turns() && team_filter(side)) {
193  for(auto iter = actions.turn_begin(turn); iter != actions.turn_end(turn); ++iter) {
194  function(iter->get());
195  }
196  end = false;
197  }
198  }
199  }
200 }
201 
203 {
204  action_ptr result;
205  std::size_t result_turn = std::numeric_limits<std::size_t>::max();
206 
207  for(team &side : resources::gameboard->teams()) {
208  side_actions &actions = *side.get_side_actions();
209  if(team_filter(side)) {
210  side_actions::iterator chall = actions.find_first_action_at(hex);
211  if(chall == actions.end()) {
212  continue;
213  }
214 
215  std::size_t chall_turn = actions.get_turn(chall);
216  if(chall_turn < result_turn) {
217  result = *chall;
218  result_turn = chall_turn;
219  }
220  }
221  }
222 
223  return result;
224 }
225 
226 std::deque<action_ptr> find_actions_of(const unit& target)
227 {
228  return resources::gameboard->get_team(target.side()).get_side_actions()->actions_of(target);
229 }
230 
231 } //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:136
container::iterator iterator
play_controller * controller
Definition: resources.cpp:21
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
std::shared_ptr< wb::side_actions > get_side_actions() const
get the whiteboard planned actions for this team
Definition: team.hpp:385
int viewer_side()
Definition: utility.cpp:45
bool team_has_visible_plan(team &t)
Returns whether a given team&#39;s plan is visible.
Definition: utility.cpp:180
iterator end()
Returns the iterator for the position after the last executed action within the actions queue...
unit_iterator end()
Definition: map.hpp:415
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:88
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:92
std::deque< action_ptr > find_actions_of(const unit &target)
Find the actions of an unit.
Definition: utility.cpp:226
virtual const unit_map & units() const override
Definition: game_board.hpp:114
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2998
This class represents a single unit of a specific type.
Definition: unit.hpp:99
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit&#39;s movement cost on a particular terrain.
Definition: unit.hpp:1384
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:340
std::shared_ptr< side_actions > side_actions_ptr
Definition: typedefs.hpp:65
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:79
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:170
virtual const gamemap & map() const override
Definition: game_board.hpp:109
int viewing_side() const
Definition: display.hpp:103
const unit * unit_
-file sdl_utils.hpp
temporary_unit_hider(unit &u)
Definition: utility.cpp:148
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:164
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:44
team & get_team(int i)
Definition: game_board.hpp:104
const std::string & id() const
Gets this unit&#39;s id.
Definition: unit.hpp:344
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:196
Applies the planned unit map for the duration of the struct&#39;s life.
Definition: manager.hpp:251
filter_context * filter_con
Definition: resources.cpp:23
game_board * gameboard
Definition: resources.cpp:20
#define ERR_WB
Definition: typedefs.hpp:24
Encapsulates the map of the game.
Definition: map.hpp:36
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:61
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:2635
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:158
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:185
iterator turn_begin(std::size_t turn_num)
Encapsulates the map of the game.
Definition: location.hpp:42
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
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:129
unit_animation_component & anim_comp() const
Definition: unit.hpp:1488
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:577
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:64
side_actions_ptr viewer_actions()
Definition: utility.cpp:50
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:92
double t
Definition: astarsearch.cpp:64
iterator turn_end(std::size_t turn_num)
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:119
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1301
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:102
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1210
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:108
int side() const
The side this unit belongs to.
Definition: unit.hpp:304
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:185
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:32
action_ptr find_action_at(map_location hex, team_filter team_filter)
Find the first action occurring on a given hex.
Definition: utility.cpp:202
This internal whiteboard class holds the planned action queues for a team, and offers many utility me...
Definition: display.hpp:48
std::size_t viewer_team()
Definition: utility.cpp:40
side_actions_ptr current_side_actions()
Definition: utility.cpp:57