The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
display_context.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
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 #include "display_context.hpp"
16 
17 #include "map/map.hpp"
18 #include "map/location.hpp"
19 #include "team.hpp"
20 #include "units/unit.hpp"
21 #include "units/map.hpp"
22 
23 std::vector<std::string>& display_context::hidden_label_categories_ref() {
24  return const_cast<std::vector<std::string>&>(this->hidden_label_categories());
25 }
26 
27 bool display_context::would_be_discovered(const map_location & loc, int side_num, bool see_all)
28 {
29  map_location adjs[6];
30  get_adjacent_tiles(loc,adjs);
31 
32  for (const map_location &u_loc : adjs)
33  {
34  unit_map::const_iterator u_it = units().find(u_loc);
35  if (!u_it.valid()) {
36  continue;
37  }
38  const unit & u = *u_it;
39  if (teams()[side_num-1].is_enemy(u.side()) && !u.incapacitated()) {
40  // Enemy spotted in adjacent tiles, check if we can see him.
41  // Watch out to call invisible with see_all=true to avoid infinite recursive calls!
42  if(see_all) {
43  return true;
44  } else if (!teams()[side_num-1].fogged(u_loc)
45  && !u.invisible(u_loc, true)) {
46  return true;
47  }
48  }
49  }
50  return false;
51 }
52 
53 const unit * display_context::get_visible_unit(const map_location & loc, const team &current_team, bool see_all) const
54 {
55  if (!map().on_board(loc)) return nullptr;
56  const unit_map::const_iterator u = units().find(loc);
57  if (!u.valid() || !u->is_visible_to_team(current_team, map(), see_all)) {
58  return nullptr;
59  }
60  return &*u;
61 }
62 
63 /**
64  * Will return true iff the unit @a u has any possible moves
65  * it can do (including attacking etc).
66  */
67 
69 {
70  if(!u.attacks_left() && u.movement_left()==0)
71  return false;
72 
73  // Units with goto commands that have already done their gotos this turn
74  // (i.e. don't have full movement left) should have red globes.
75  if(u.has_moved() && u.has_goto()) {
76  return false;
77  }
78 
79  const team &current_team = teams()[u.side() - 1];
80 
81  map_location locs[6];
83  for(int n = 0; n != 6; ++n) {
84  if (map().on_board(locs[n])) {
85  const unit_map::const_iterator i = units().find(locs[n]);
86  if (i.valid() && !i->incapacitated() &&
87  current_team.is_enemy(i->side())) {
88  return true;
89  }
90 
91  if (u.movement_cost(map()[locs[n]]) <= u.movement_left()) {
92  return true;
93  }
94  }
95  }
96 
97  return false;
98 }
99 
100 
101 /**
102  * Given the location of a village, will return the 0-based index
103  * of the team that currently owns it, and -1 if it is unowned.
104  */
106 {
107  const std::vector<team> & t = teams();
108  for(size_t i = 0; i != t.size(); ++i) {
109  if(t[i].owns_village(loc))
110  return i;
111  }
112  return -1;
113 }
114 
115 /**
116  * Determine if we are an observer, by checking if every team is not locally controlled
117  */
119 {
120  for (const team &t : teams()) {
121  if (t.is_local())
122  return false;
123  }
124 
125  return true;
126 }
127 
128 /// Static info getters previously declared at global scope in unit.?pp
129 
130 int display_context::side_units(int side) const
131 {
132  int res = 0;
133  for (const unit &u : units()) {
134  if (u.side() == side) ++res;
135  }
136  return res;
137 }
138 
140 {
141  int res = 0;
142  for (const unit &u : units()) {
143  if (u.side() == side) res += u.cost();
144  }
145  return res;
146 }
147 
148 int display_context::side_upkeep(int side) const
149 {
150  int res = 0;
151  for (const unit &u : units()) {
152  if (u.side() == side) res += u.upkeep();
153  }
154  return res;
155 }
156 
158 {
159  team_data res;
160  res.units = side_units(side);
161  res.upkeep = side_upkeep(side);
162  res.villages = tm.villages().size();
163  res.expenses = std::max<int>(0,res.upkeep - tm.support());
164  res.net_income = tm.total_income() - res.expenses;
165  res.gold = tm.gold();
166  res.teamname = tm.user_team_name();
167  return res;
168 }
169 
bool has_moved() const
Definition: unit.hpp:255
int side_units(int side_num) const
Returns the number of units of the side side_num.
const map_location & get_location() const
Definition: unit.hpp:286
bool has_goto() const
Definition: unit.hpp:256
Definition: unit.hpp:95
int movement_cost(const t_translation::t_terrain &terrain) const
Definition: unit.hpp:308
std::vector< std::string > & hidden_label_categories_ref()
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.hpp:274
bool is_enemy(int n) const
Definition: team.hpp:247
int village_owner(const map_location &loc) const
Given the location of a village, will return the 0-based index of the team that currently owns it...
int side() const
Definition: unit.hpp:201
-file sdl_utils.hpp
bool unit_can_move(const unit &u) const
Will return true iff the unit u has any possible moves it can do (including attacking etc)...
GLdouble GLdouble t
Definition: glew.h:1366
virtual const gamemap & map() const =0
int side_upkeep(int side_num) const
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
bool incapacitated() const
Definition: unit.hpp:215
const t_string & user_team_name() const
Definition: team.hpp:298
virtual const unit_map & units() const =0
int movement_left() const
Returns how far a unit can move this turn (zero if incapacitated).
Definition: unit.hpp:220
const std::set< map_location > & villages() const
Definition: team.hpp:189
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2291
Encapsulates the map of the game.
Definition: location.hpp:38
GLuint res
Definition: glew.h:9258
virtual const std::vector< team > & teams() const =0
int attacks_left() const
Definition: unit.hpp:230
size_t i
Definition: function.cpp:1057
int support() const
Calculate total support capacity, based on support_per_village.
Definition: team.hpp:208
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:467
int side_units_cost(int side_num) const
Returns the total cost of units of side side_num.
int gold() const
Definition: team.hpp:194
GLclampd n
Definition: glew.h:5903
bool is_observer() const
Check if we are an observer in this game.
int total_income() const
Definition: team.hpp:201
unit_iterator find(size_t id)
Definition: map.cpp:285
bool valid() const
Definition: map.hpp:229
virtual const std::vector< std::string > & hidden_label_categories() const =0
const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false) const
bool would_be_discovered(const map_location &loc, int side_num, bool see_all=true)
Given a location and a side number, indicates whether an invisible unit of that side at that location...
team_data calculate_team_data(const class team &tm, int side) const
std::string teamname