-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
display_context.cpp
99 lines (81 loc) · 2.44 KB
/
display_context.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "display_context.hpp"
#include "map.hpp"
#include "map_location.hpp"
#include "team.hpp"
#include "unit.hpp"
#include "unit_map.hpp"
#include <boost/foreach.hpp>
const unit * display_context::get_visible_unit(const map_location & loc, const team ¤t_team, bool see_all) const
{
if (!map().on_board(loc)) return NULL;
const unit_map::const_iterator u = units().find(loc);
if (!u.valid() || !u->is_visible_to_team(current_team, map(), see_all)) {
return NULL;
}
return &*u;
}
/**
* Will return true iff the unit @a u has any possible moves
* it can do (including attacking etc).
*/
bool display_context::unit_can_move(const unit &u) const
{
if(!u.attacks_left() && u.movement_left()==0)
return false;
// Units with goto commands that have already done their gotos this turn
// (i.e. don't have full movement left) should have red globes.
if(u.has_moved() && u.has_goto()) {
return false;
}
const team ¤t_team = teams()[u.side() - 1];
map_location locs[6];
get_adjacent_tiles(u.get_location(), locs);
for(int n = 0; n != 6; ++n) {
if (map().on_board(locs[n])) {
const unit_map::const_iterator i = units().find(locs[n]);
if (i.valid() && !i->incapacitated() &&
current_team.is_enemy(i->side())) {
return true;
}
if (u.movement_cost(map()[locs[n]]) <= u.movement_left()) {
return true;
}
}
}
return false;
}
/**
* Given the location of a village, will return the 0-based index
* of the team that currently owns it, and -1 if it is unowned.
*/
int display_context::village_owner(const map_location& loc) const
{
const std::vector<team> & t = teams();
for(size_t i = 0; i != t.size(); ++i) {
if(t[i].owns_village(loc))
return i;
}
return -1;
}
/**
* Determine if we are an observer, by checking if every team is not locally controlled
*/
bool display_context::is_observer() const
{
BOOST_FOREACH(const team &t, teams()) {
if (t.is_local())
return false;
}
return true;
}