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