The Battle for Wesnoth  1.19.5+dev
filter.hpp
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 #pragma once
17 
18 #include "pathutils.hpp"
19 #include "terrain/translation.hpp"
20 #include "variable.hpp"
21 
22 class config;
23 class filter_context;
24 class unit;
25 class unit_filter;
26 
27 //terrain_filter: a class that implements the Standard Location Filter
28 class terrain_filter : public xy_pred {
29 public:
30 
31  terrain_filter(const vconfig& cfg, const filter_context * fc, const bool flat_tod);
32  terrain_filter(const vconfig& cfg, const terrain_filter& original);
33  /** Default implementation, but defined out-of-line for efficiency reasons. */
34  ~terrain_filter();
35 
36  terrain_filter(const terrain_filter &other);
38 
39  /**
40  * @param loc The location to test
41  * @returns true if and only if the given location matches this filter
42  */
43  bool match(const map_location& loc) const {
44  return match_impl(loc, nullptr);
45  }
46 
47  /**
48  * @param loc The location to test
49  * @param ref_unit A reference unit for the $teleport_unit auto-stored variable
50  * @returns true if and only if the given location matches this filter
51  */
52  bool match(const map_location& loc, const unit& ref_unit) const {
53  return match_impl(loc, &ref_unit);
54  }
55 
56  virtual bool operator()(const map_location& loc) const { return this->match(loc); }
57 
58  /**
59  * gets all locations on the map that match this filter
60  * @param[out] locs set to store the results in
61  * @param[in] with_border whether to include the borders
62  */
63  void get_locations(std::set<map_location>& locs, bool with_border=false) const {
64  return get_locs_impl(locs, nullptr, with_border);
65  }
66 
67  /**
68  * gets all locations on the map that match this filter
69  * @param[out] locs set to store the results in
70  * @param[in] with_border whether to include the borders
71  * @param[in] ref_unit A reference unit for the $teleport_unit auto-stored variable
72  */
73  void get_locations(std::set<map_location>& locs, const unit& ref_unit, bool with_border=false) const {
74  return get_locs_impl(locs, &ref_unit, with_border);
75  }
76 
77  //flatten: use base time of day -- ignore illumination ability
78  void flatten(const bool flat_tod=true) { flat_ = flat_tod; }
79 
80  config to_config() const;
81  friend class terrain_filterimpl;
82 private:
83  bool match_impl(const map_location& loc, const unit* ref_unit) const;
84  void get_locs_impl(std::set<map_location>& locs, const unit* ref_unit, bool with_border) const;
85  bool match_internal(const map_location& loc, const unit* ref_unit, const bool ignore_xy) const;
86 
87  const vconfig cfg_; //config contains WML for a Standard Location Filter
89 
92 
93  //parsed_terrain: optimizes handling of terrain="..."
94  std::unique_ptr<t_translation::ter_match> parsed_terrain;
95 
96  //adjacent_matches: optimize handling of [filter_adjacent_location] for get_locations()
97  std::unique_ptr<std::vector<std::set<map_location>>> adjacent_matches;
98 
99  //adjacent_match_cache: optimize handling of [filter_adjacent_location] for match()
100  std::vector< std::pair<terrain_filter, std::map<map_location,bool>>> adjacent_match_cache;
101 
102  std::unique_ptr<unit_filter> ufilter_;
103  };
104 
106  const std::size_t max_loop_;
107  bool flat_;
108 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
bool match_internal(const map_location &loc, const unit *ref_unit, const bool ignore_xy) const
Definition: filter.cpp:96
bool match(const map_location &loc) const
Definition: filter.hpp:43
void get_locations(std::set< map_location > &locs, const unit &ref_unit, bool with_border=false) const
gets all locations on the map that match this filter
Definition: filter.hpp:73
bool match_impl(const map_location &loc, const unit *ref_unit) const
Definition: filter.cpp:372
terrain_filter(const vconfig &cfg, const filter_context *fc, const bool flat_tod)
Definition: filter.cpp:50
terrain_filter & operator=(const terrain_filter &other)
Definition: filter.cpp:78
terrain_filter_cache cache_
Definition: filter.hpp:105
bool match(const map_location &loc, const unit &ref_unit) const
Definition: filter.hpp:52
const vconfig cfg_
Definition: filter.hpp:87
const std::size_t max_loop_
Definition: filter.hpp:106
virtual bool operator()(const map_location &loc) const
Definition: filter.hpp:56
void flatten(const bool flat_tod=true)
Definition: filter.hpp:78
const filter_context * fc_
Definition: filter.hpp:88
void get_locations(std::set< map_location > &locs, bool with_border=false) const
gets all locations on the map that match this filter
Definition: filter.hpp:63
void get_locs_impl(std::set< map_location > &locs, const unit *ref_unit, bool with_border) const
Definition: filter.cpp:513
This class represents a single unit of a specific type.
Definition: unit.hpp:133
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
Encapsulates the map of the game.
Definition: location.hpp:45
std::unique_ptr< unit_filter > ufilter_
Definition: filter.hpp:102
std::vector< std::pair< terrain_filter, std::map< map_location, bool > > > adjacent_match_cache
Definition: filter.hpp:100
std::unique_ptr< t_translation::ter_match > parsed_terrain
Definition: filter.hpp:94
std::unique_ptr< std::vector< std::set< map_location > > > adjacent_matches
Definition: filter.hpp:97