The Battle for Wesnoth  1.17.21+dev
lua_terrainfilter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 - 2023
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 <cstddef>
18 #include <string>
19 #include <memory>
20 #include <map>
21 #include <set>
22 #include "map/map.hpp"
23 
24 #include "scripting/lua_common.hpp"
25 struct lua_State;
26 struct map_location;
27 class mapgen_gamemap;
28 class filter_impl;
29 namespace lua_mapgen
30 {
31  class filter
32  {
33  public:
34  /**
35  * @a a lua table with the following attributes
36  * [1]: the filter table,
37  * [2]: attributeslocation_sets
38  */
39  explicit filter(lua_State* L, int data_index, int res_index = 0);
40 
41  //impl_ may contain pointers to known_sets_, copycontructing this will result in segfaults.
42  filter(const filter&) = delete;
43  filter& operator=(const filter&) = delete;
44  //moving is ok though.
45  filter(filter&&) = default;
46  filter& operator=(filter&&) = default;
47 
48  ~filter();
49 
50  bool matches(const gamemap_base& m, map_location l);
51  //todo: add a clear cache function.
52  private:
53  std::map<std::string, std::set<map_location>> known_sets_;
54  std::unique_ptr<filter_impl> impl_;
55  };
56 
57  std::string register_filter_metatables(lua_State *L);
58 }
59 
60 bool luaW_is_mgfilter(lua_State* L, int index);
61 
62 lua_mapgen::filter* luaW_to_mgfilter(lua_State *L, int index);
63 
64 lua_mapgen::filter& luaW_check_mgfilter(lua_State *L, int index);
65 
66 void lua_mgfilter_setmetatable(lua_State *L);
67 
68 int intf_terrainfilter_create(lua_State *L);
69 
70 int intf_mg_get_locations(lua_State* L);
71 int intf_mg_get_tiles_radius(lua_State* L);
72 
73 namespace lua_terrainfilter {
74  std::string register_metatables(lua_State *L);
75 }
filter(const filter &)=delete
filter(lua_State *L, int data_index, int res_index=0)
a lua table with the following attributes [1]: the filter table,
filter(filter &&)=default
filter & operator=(const filter &)=delete
filter & operator=(filter &&)=default
std::unique_ptr< filter_impl > impl_
std::map< std::string, std::set< map_location > > known_sets_
bool matches(const gamemap_base &m, map_location l)
lua_mapgen::filter * luaW_to_mgfilter(lua_State *L, int index)
lua_mapgen::filter & luaW_check_mgfilter(lua_State *L, int index)
int intf_mg_get_tiles_radius(lua_State *L)
int intf_terrainfilter_create(lua_State *L)
Create a filter.
int intf_mg_get_locations(lua_State *L)
void lua_mgfilter_setmetatable(lua_State *L)
bool luaW_is_mgfilter(lua_State *L, int index)
std::string register_filter_metatables(lua_State *L)
std::string register_metatables(lua_State *L)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
Encapsulates the map of the game.
Definition: location.hpp:38