The Battle for Wesnoth  1.19.0-dev
entity_location.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
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 {
93  if ( !un_it.valid() )
94  return false;
95 
96  if ( filter.empty() )
97  // Skip the check for un_it matching *this.
98  return true;
99 
100  // Filter the unit at the filter location (should be the unit's
101  // location if no special filter location was specified).
102  return filter.matches(*un_it, filter_loc_) &&
103  matches_unit(un_it);
104 }
105 
107 {
108  if(!resources::gameboard) {
109  return nullptr;
110  }
111  if(id_ == 0) {
112  auto un_it = resources::gameboard->units().find(*this);
113  if(un_it.valid()) {
114  return un_it.get_shared_ptr();
115  }
116  return nullptr;
117  }
119 }
120 
121 } // end namespace game_events
virtual const unit_map & units() const override
Definition: game_board.hpp:106
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:123
bool empty() const
Definition: filter.hpp:176
unit_iterator find(std::size_t id)
Definition: map.cpp:302
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Define locations as used by the game's events mechanism.
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:207
Domain specific events.
game_board * gameboard
Definition: resources.cpp:20
static std::string get_location(const std::string &loc)
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
entity_location(const map_location &loc, std::size_t id=0)
Constructor for when an event has a location but not necessarily a unit.
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...
static const entity_location null_entity
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 unit_filter &filter) const
Determines if un_it matches filter.
map_location filter_loc_
This map_location allows a unit to be filtered as if it were somewhere other than where it is.
unit_const_ptr get_unit() const
Encapsulates the map of the game.
Definition: location.hpp:38
static const map_location & null_location()
Definition: location.hpp:81
bool valid() const
Definition: map.hpp:273
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:217