The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
3  Part of the Battle for Wesnoth Project http://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 "utility.hpp"
24 
25 #include "manager.hpp"
26 #include "side_actions.hpp"
27 
28 #include "actions/create.hpp"
29 #include "game_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 
37 namespace wb {
38 
39 size_t viewer_team()
40 {
42 }
43 
45 {
47 }
48 
50 {
52  (*resources::teams)[resources::screen->viewing_team()].get_side_actions();
53  return side_actions;
54 }
55 
57 {
59  (*resources::teams)[resources::controller->current_side() - 1].get_side_actions();
60  return side_actions;
61 }
62 
64 {
65  assert(leader.can_recruit());
66  assert(resources::gameboard->map().is_keep(leader.get_location()));
68  {
69  if (unit->can_recruit() && unit->id() != leader.id())
70  {
71  if ( dynamic_cast<game_state*>(resources::filter_con)->can_recruit_on(*unit, leader.get_location()) )
72  return unit.get_shared_ptr();
73  }
74  }
75  return unit_const_ptr();
76 }
77 
78 unit* find_recruiter(size_t team_index, map_location const& hex)
79 {
80  if ( !resources::gameboard->map().is_castle(hex) )
81  return nullptr;
82 
83  for(unit& u : *resources::units)
84  if(u.can_recruit()
85  && u.side() == static_cast<int>(team_index+1)
86  && dynamic_cast<game_state*>(resources::filter_con)->can_recruit_on(u, hex))
87  return &u;
88  return nullptr;
89 }
90 
92 {
93  future_map planned_unit_map;
94  if(!resources::whiteboard->has_planned_unit_map())
95  {
96  ERR_WB << "future_visible_unit cannot find unit, future unit map failed to build." << std::endl;
97  return nullptr;
98  }
99  //use global method get_visible_unit
100  return resources::gameboard->get_visible_unit(hex, resources::teams->at(viewer_side - 1), false);
101 }
102 
104 {
105  unit* unit = future_visible_unit(hex, viewer_side);
106  if (unit && unit->side() == on_side)
107  return unit;
108  else
109  return nullptr;
110 }
111 
112 int path_cost(std::vector<map_location> const& path, unit const& u)
113 {
114  if(path.size() < 2)
115  return 0;
116 
117  team const& u_team = resources::teams->at(u.side()-1);
118  map_location const& dest = path.back();
119  if ( (resources::gameboard->map().is_village(dest) && !u_team.owns_village(dest))
120  || pathfind::enemy_zoc(u_team, dest, u_team) )
121  return u.total_movement();
122 
123  int result = 0;
124  gamemap const& map = resources::gameboard->map();
125  for(map_location const& loc : std::make_pair(path.begin()+1,path.end())) {
126  result += u.movement_cost(map[loc]);
127  }
128  return result;
129 }
130 
132  : unit_(&u)
133  {unit_->set_hidden(true);}
135 {
136  try {
137  unit_->set_hidden(false);
138  } catch (...) {}
139 }
140 
142 {
143  unit->anim_comp().set_disabled_ghosted(false);
145 }
146 
148 {
149  unit->anim_comp().set_standing(true);
151 }
152 
154 {
155  for (team& t : *resources::teams) {
156  if (!t.get_side_actions()->empty())
157  return true;
158  }
159 
160  return false;
161 }
162 
164 {
165  return !t.get_side_actions()->hidden();
166 }
167 
168 void for_each_action(std::function<void(action*)> function, team_filter team_filter)
169 {
170  bool end = false;
171  for(size_t turn=0; !end; ++turn) {
172  end = true;
173  for(team &side : *resources::teams) {
174  side_actions &actions = *side.get_side_actions();
175  if(turn < actions.num_turns() && team_filter(side)) {
176  for(auto iter = actions.turn_begin(turn); iter != actions.turn_end(turn); ++iter) {
177  function(iter->get());
178  }
179  end = false;
180  }
181  }
182  }
183 }
184 
186 {
188  size_t result_turn = std::numeric_limits<size_t>::max();
189 
190  for(team &side : *resources::teams) {
191  side_actions &actions = *side.get_side_actions();
192  if(team_filter(side)) {
193  side_actions::iterator chall = actions.find_first_action_at(hex);
194  if(chall == actions.end()) {
195  continue;
196  }
197 
198  size_t chall_turn = actions.get_turn(chall);
199  if(chall_turn < result_turn) {
200  result = *chall;
201  result_turn = chall_turn;
202  }
203  }
204  }
205 
206  return result;
207 }
208 
209 std::deque<action_ptr> find_actions_of(unit const &target)
210 {
211  return (*resources::teams)[target.side()-1].get_side_actions()->actions_of(target);
212 }
213 
214 } //end namespace wb
215 
container::iterator iterator
play_controller * controller
Definition: resources.cpp:21
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
int viewer_side()
Definition: utility.cpp:44
int total_movement() const
Definition: unit.hpp:218
bool team_has_visible_plan(team &t)
Returns whether a given team's plan is visible.
Definition: utility.cpp:163
const std::string & id() const
Definition: unit.hpp:148
boost::shared_ptr< wb::side_actions > get_side_actions() const
get the whiteboard planned actions for this team
Definition: team.hpp:382
iterator end()
Returns the iterator for the position after the last executed action within the actions queue...
unit_iterator end()
Definition: map.hpp:311
const map_location & get_location() const
Definition: unit.hpp:286
void set_hidden(bool state) const
Definition: unit.cpp:2460
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3536
Definition: unit.hpp:95
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
int movement_cost(const t_translation::t_terrain &terrain) const
Definition: unit.hpp:308
size_t viewer_team()
Definition: utility.cpp:39
bool owns_village(const map_location &loc) const
Definition: team.hpp:190
game_display * screen
Definition: resources.cpp:27
iterator turn_begin(size_t turn_num)
size_t get_turn(const_iterator it) const
Returns the turn of a given iterator planned execution.
unit * find_recruiter(size_t team_index, map_location const &hex)
Definition: utility.cpp:78
bool has_actions()
Return whether the whiteboard has actions.
Definition: utility.cpp:153
int path_cost(std::vector< map_location > const &path, unit const &u)
Computes the MP cost for u to travel path.
Definition: utility.cpp:112
int side() const
Definition: unit.hpp:201
const unit * unit_
-file sdl_utils.hpp
GLdouble GLdouble t
Definition: glew.h:1366
temporary_unit_hider(unit &u)
Definition: utility.cpp:131
void unghost_owner_unit(unit *unit)
Definition: utility.cpp:147
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 'side' (in game nomenclature).
Definition: team.hpp:50
static std::string at(const std::string &file, int line)
GLsizei const char ** path
Definition: glew.h:4654
GLuint GLuint end
Definition: glew.h:1221
GLuint64EXT * result
Definition: glew.h:10727
size_t num_turns() const
Returns the number of turns that have plans.
int current_side() const
Returns the number of the side whose turn it is.
std::vector< team > * teams
Definition: resources.cpp:29
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:192
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:249
filter_context * filter_con
Definition: resources.cpp:23
game_board * gameboard
Definition: resources.cpp:20
#define ERR_WB
Definition: typedefs.hpp:26
Encapsulates the map of the game.
Definition: map.hpp:37
bool can_recruit() const
Definition: unit.hpp:207
void ghost_owner_unit(unit *unit)
Definition: utility.cpp:141
unit_animation_component & anim_comp() const
Definition: unit.hpp:276
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:168
int viewing_side() const
Definition: display.hpp:103
Encapsulates the map of the game.
Definition: location.hpp:38
Various functions related to the creation of units (recruits, recalls, and placed units)...
std::deque< action_ptr > find_actions_of(unit const &target)
Find the actions of an unit.
Definition: utility.cpp:209
side_actions_ptr viewer_actions()
Definition: utility.cpp:49
iterator turn_end(size_t turn_num)
virtual const gamemap & map() const
Definition: game_board.hpp:98
std::function< bool(team &)> team_filter
Callable object class to filter teams.
Definition: utility.hpp:115
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:63
unit * future_visible_unit(map_location hex, int viewer_side)
Applies the future unit map and.
Definition: utility.cpp:91
boost::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:36
void set_standing(bool with_bars=true)
Sets the animation state to standing.
size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:102
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 occuring on a given hex.
Definition: utility.cpp:185
This internal whiteboard class holds the planned action queues for a team, and offers many utility me...
unit_map * units
Definition: resources.cpp:35
bool enemy_zoc(team const &current_team, map_location const &loc, team const &viewing_team, bool see_all)
Determines if a given location is in an enemy zone of control.
Definition: pathfind.cpp:138
Definition: display.hpp:47
GLenum target
Definition: glew.h:5190
side_actions_ptr current_side_actions()
Definition: utility.cpp:56