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