The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
entity_location.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[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 /**
16  * @file
17  * The structure that tracks WML event locations.
18  */
19 
20 #include "global.hpp"
21 #include "entity_location.hpp"
22 
23 #include "resources.hpp"
24 #include "units/unit.hpp"
25 #include "units/filter.hpp"
26 #include "variable.hpp"
27 
28 
29 // This file is in the game_events namespace.
30 namespace game_events {
31 
33 
34 /**
35  * Constructor for when an event has a location but not necessarily a unit.
36  * Can also be used if the event has a unit and the caller already has the
37  * unit's location and underlying ID.
38  */
40  : map_location(loc), id_(id), filter_loc_(loc)
41 {}
42 
43 /**
44  * Constructor for when an event has a unit that needs to be filtered as if
45  * it was in a different location.
46  */
48  const map_location & filter_loc)
49  : map_location(loc), id_(id), filter_loc_(filter_loc)
50 {}
51 
52 /**
53  * Convenience constructor for when an event has a unit, saving the caller
54  * the need to explicitly get the location and underlying ID.
55  */
58  , id_(u.underlying_id())
59  , filter_loc_(*this)
60 {}
61 
62 /**
63  * Convenience constructor for when an event has a unit that needs to be
64  * filtered as if it was in a different location, and the caller does not
65  * want to explicitly get the unit's location and underlying ID.
66  */
67 entity_location::entity_location(const unit &u, const map_location & filter_loc)
69  , id_(u.underlying_id())
70  , filter_loc_(filter_loc)
71 {}
72 
73 
74 /**
75  * Determines if @a un_it matches (using underlying ID) the unit that was
76  * supplied when this was constructed.
77  * If no unit was supplied, then all units (including non-existent units)
78  * match.
79  */
81 {
82  return id_ == 0 || ( un_it.valid() && id_ == un_it->underlying_id() );
83 }
84 
85 
86 /**
87  * Determines if @a un_it matches @a filter. If the filter is not empty,
88  * the unit is required to additionally match the unit that was supplied
89  * when this was constructed.
90  */
92  const vconfig & filter) const
93 {
94  if ( !un_it.valid() )
95  return false;
96 
97  if ( filter.empty() )
98  // Skip the check for un_it matching *this.
99  return true;
100 
101  // Filter the unit at the filter location (should be the unit's
102  // location if no special filter location was specified).
103  return unit_filter(filter, resources::filter_con).matches(*un_it, filter_loc_) &&
104  matches_unit(un_it);
105 }
106 
108 {
109  if(resources::units == nullptr) {
110  return nullptr;
111  }
112  if(id_ == 0) {
113  auto un_it = resources::units->find(*this);
114  if(un_it.valid()) {
115  return un_it.get_shared_ptr();
116  }
117  return nullptr;
118  }
120 }
121 
122 } // end namespace game_events
123 
unit_const_ptr get_unit() const
std::string id_
Definition: formula.cpp:636
Definition: unit.hpp:95
entity_location(const map_location &loc, size_t id=0)
Constructor for when an event has a location but not necessarily a unit.
static const entity_location null_entity
pointer get_shared_ptr() const
Definition: map.hpp:180
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:67
filter_context * filter_con
Definition: resources.cpp:23
bool matches_unit_filter(const unit_map::const_iterator &un_it, const vconfig &filter) const
Determines if un_it matches filter.
size_t id_
The underlying ID of the unit associated with this.
static const map_location & null_location()
Definition: location.hpp:195
Encapsulates the map of the game.
Definition: location.hpp:38
Domain specific events.
Definition: action_wml.cpp:93
static std::string get_location(const std::string &loc)
bool empty() const
Definition: variable.hpp:93
map_location filter_loc_
This map_location allows a unit to be filtered as if it were somewhere other than where it is...
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:3448
bool matches_unit(const unit_map::const_iterator &un_it) const
Determines if un_it matches (using underlying ID) the unit that was supplied when this was constructe...
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
unit_iterator find(size_t id)
Definition: map.cpp:285
bool valid() const
Definition: map.hpp:229
Define locations as used by the game's events mechanism.
unit_map * units
Definition: resources.cpp:35