The Battle for Wesnoth  1.15.2+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=false);
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  /// @param loc The location to test
39  /// @returns true if and only if the given location matches this filter
40  bool match(const map_location& loc) const {
41  return match_impl(loc, nullptr);
42  }
43 
44  /// @param loc The location to test
45  /// @param ref_unit A reference unit for the $teleport_unit auto-stored variable
46  /// @returns true if and only if the given location matches this filter
47  bool match(const map_location& loc, const unit& ref_unit) const {
48  return match_impl(loc, &ref_unit);
49  }
50 
51  virtual bool operator()(const map_location& loc) const { return this->match(loc); }
52 
53  /// gets all locations on the map that match this filter
54  /// @param[out] locs set to store the results in
55  /// @param[in] with_border whether to include the borders
56  void get_locations(std::set<map_location>& locs, bool with_border=false) const {
57  return get_locs_impl(locs, nullptr, with_border);
58  }
59 
60  /// gets all locations on the map that match this filter
61  /// @param[out] locs set to store the results in
62  /// @param[in] with_border whether to include the borders
63  /// @param[in] ref_unit A reference unit for the $teleport_unit auto-stored variable
64  void get_locations(std::set<map_location>& locs, const unit& ref_unit, bool with_border=false) const {
65  return get_locs_impl(locs, &ref_unit, with_border);
66  }
67 
68  //flatten: use base time of day -- ignore illumination ability
69  void flatten(const bool flat_tod=true) { flat_ = flat_tod; }
70 
71  config to_config() const;
72  friend class terrain_filterimpl;
73 private:
74  bool match_impl(const map_location& loc, const unit* ref_unit) const;
75  void get_locs_impl(std::set<map_location>& locs, const unit* ref_unit, bool with_border) const;
76  bool match_internal(const map_location& loc, const unit* ref_unit, const bool ignore_xy) const;
77 
78  const vconfig cfg_; //config contains WML for a Standard Location Filter
80 
83 
84  //parsed_terrain: optimizes handling of terrain="..."
85  std::unique_ptr<t_translation::ter_match> parsed_terrain;
86 
87  //adjacent_matches: optimize handling of [filter_adjacent_location] for get_locations()
88  std::unique_ptr<std::vector<std::set<map_location>>> adjacent_matches;
89 
90  //adjacent_match_cache: optimize handling of [filter_adjacent_location] for match()
91  std::vector< std::pair<terrain_filter, std::map<map_location,bool>> > adjacent_match_cache;
92 
93  std::unique_ptr<unit_filter> ufilter_;
94  };
95 
97  const std::size_t max_loop_;
98  bool flat_;
99 };
This class represents a single unit of a specific type.
Definition: unit.hpp:99
virtual bool operator()(const map_location &loc) const
Definition: filter.hpp:51
std::unique_ptr< t_translation::ter_match > parsed_terrain
Definition: filter.hpp:85
terrain_filter_cache cache_
Definition: filter.hpp:96
const vconfig cfg_
Definition: filter.hpp:78
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:56
bool match_internal(const map_location &loc, const unit *ref_unit, const bool ignore_xy) const
Definition: filter.cpp:100
void get_locs_impl(std::set< map_location > &locs, const unit *ref_unit, bool with_border) const
Definition: filter.cpp:522
const filter_context * fc_
Definition: filter.hpp:79
config to_config() const
Definition: filter.cpp:680
bool match(const map_location &loc, const unit &ref_unit) const
Definition: filter.hpp:47
void flatten(const bool flat_tod=true)
Definition: filter.hpp:69
terrain_filter(const vconfig &cfg, const filter_context *fc, const bool flat_tod=false)
std::unique_ptr< unit_filter > ufilter_
Definition: filter.hpp:93
Encapsulates the map of the game.
Definition: location.hpp:42
terrain_filter & operator=(const terrain_filter &other)
Definition: filter.cpp:75
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:64
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:425
std::vector< std::pair< terrain_filter, std::map< map_location, bool > > > adjacent_match_cache
Definition: filter.hpp:91
std::unique_ptr< std::vector< std::set< map_location > > > adjacent_matches
Definition: filter.hpp:88
bool match_impl(const map_location &loc, const unit *ref_unit) const
Definition: filter.cpp:372
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
const std::size_t max_loop_
Definition: filter.hpp:97
bool match(const map_location &loc) const
Definition: filter.hpp:40