The Battle for Wesnoth  1.17.0-dev
entity_location.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * The structure that tracks WML event locations.
19  */
20 
22 
23 #include "game_board.hpp"
24 #include "resources.hpp"
25 #include "units/unit.hpp"
26 #include "units/filter.hpp"
27 #include "variable.hpp"
28 
29 
30 // This file is in the game_events namespace.
31 namespace game_events {
32 
34 
35 /**
36  * Constructor for when an event has a location but not necessarily a unit.
37  * Can also be used if the event has a unit and the caller already has the
38  * unit's location and underlying ID.
39  */
41  : map_location(loc), id_(id), filter_loc_(loc)
42 {}
43 
44 /**
45  * Constructor for when an event has a unit that needs to be filtered as if
46  * it was in a different location.
47  */
49  const map_location & filter_loc)
50  : map_location(loc), id_(id), filter_loc_(filter_loc)
51 {}
52 
53 /**
54  * Convenience constructor for when an event has a unit, saving the caller
55  * the need to explicitly get the location and underlying ID.
56  */
59  , id_(u.underlying_id())
60  , filter_loc_(*this)
61 {}
62 
63 /**
64  * Convenience constructor for when an event has a unit that needs to be
65  * filtered as if it was in a different location, and the caller does not
66  * want to explicitly get the unit's location and underlying ID.
67  */
70  , id_(u.underlying_id())
71  , filter_loc_(filter_loc)
72 {}
73 
74 
75 /**
76  * Determines if @a un_it matches (using underlying ID) the unit that was
77  * supplied when this was constructed.
78  * If no unit was supplied, then all units (including non-existent units)
79  * match.
80  */
82 {
83  return id_ == 0 || ( un_it.valid() && id_ == un_it->underlying_id() );
84 }
85 
86 
87 /**
88  * Determines if @a un_it matches @a filter. If the filter is not empty,
89  * the unit is required to additionally match the unit that was supplied
90  * when this was constructed.
91  */
93  const vconfig & filter) const
94 {
95  if ( !un_it.valid() )
96  return false;
97 
98  if ( filter.empty() )
99  // Skip the check for un_it matching *this.
100  return true;
101 
102  // Filter the unit at the filter location (should be the unit's
103  // location if no special filter location was specified).
104  return unit_filter(filter).matches(*un_it, filter_loc_) &&
105  matches_unit(un_it);
106 }
107 
109 {
110  if(!resources::gameboard) {
111  return nullptr;
112  }
113  if(id_ == 0) {
114  auto un_it = resources::gameboard->units().find(*this);
115  if(un_it.valid()) {
116  return un_it.get_shared_ptr();
117  }
118  return nullptr;
119  }
121 }
122 
123 } // end namespace game_events
std::size_t id_
The underlying ID of the unit associated with this.
bool matches_unit_filter(const unit_map::const_iterator &un_it, const vconfig &filter) const
Determines if un_it matches filter.
virtual const unit_map & units() const override
Definition: game_board.hpp:112
This class represents a single unit of a specific type.
Definition: unit.hpp:121
const map_location & filter_loc() const
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:132
bool empty() const
Definition: variable.hpp:100
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
unit_const_ptr get_unit() const
static const entity_location null_entity
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...
entity_location(const map_location &loc, std::size_t id=0)
Constructor for when an event has a location but not necessarily a unit.
game_board * gameboard
Definition: resources.cpp:21
Encapsulates the map of the game.
Definition: location.hpp:38
Domain specific events.
Definition: action_wml.cpp:87
unit_iterator find(std::size_t id)
Definition: map.cpp:310
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:218
static std::string get_location(const std::string &loc)
map_location filter_loc_
This map_location allows a unit to be filtered as if it were somewhere other than where it is...
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
static const map_location & null_location()
Definition: location.hpp:81
bool valid() const
Definition: map.hpp:274
Define locations as used by the game&#39;s events mechanism.