The Battle for Wesnoth  1.13.10+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 - 2017 by David White <dave@whitevine.net>
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 
21 
22 #include "game_board.hpp"
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::gameboard) {
110  return nullptr;
111  }
112  if(id_ == 0) {
113  auto un_it = resources::gameboard->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 
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
virtual const unit_map & units() const
Definition: game_board.hpp:97
unit_const_ptr get_unit() const
This class represents a single unit of a specific type.
Definition: unit.hpp:100
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
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:220
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:140
filter_context * filter_con
Definition: resources.cpp:23
game_board * gameboard
Definition: resources.cpp:20
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:220
Encapsulates the map of the game.
Definition: location.hpp:40
Domain specific events.
Definition: action_wml.cpp:88
static std::string get_location(const std::string &loc)
bool empty() const
Definition: variable.hpp:100
map_location filter_loc_
This map_location allows a unit to be filtered as if it were somewhere other than where it is...
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:42
unit_iterator find(size_t id)
Definition: map.cpp:311
bool valid() const
Definition: map.hpp:276
Define locations as used by the game's events mechanism.