From 1c73c9399102266a1ffeef89db232d82fecd4475 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 22 Feb 2021 22:19:23 -0500 Subject: [PATCH] For consistency, mapgen should also expose a matches function --- src/scripting/lua_terrainfilter.cpp | 20 +++++++++++++++++++- src/scripting/lua_terrainfilter.hpp | 1 + src/scripting/mapgen_lua_kernel.cpp | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/scripting/lua_terrainfilter.cpp b/src/scripting/lua_terrainfilter.cpp index a39a93c241409..4aeaa136f2c6c 100644 --- a/src/scripting/lua_terrainfilter.cpp +++ b/src/scripting/lua_terrainfilter.cpp @@ -718,9 +718,9 @@ static int intf_mg_get_locations_part2(lua_State* L, gamemap_base& m, lua_mapgen return 1; } + int intf_mg_get_locations(lua_State* L) { - //todo: create filter form table if needed LOG_LMG << "map:get_locations\n"; gamemap_base& m = luaW_checkterrainmap(L, 1); if(luaW_is_mgfilter(L, 2)) { @@ -736,6 +736,24 @@ int intf_mg_get_locations(lua_State* L) } } +int intf_mg_match_location(lua_State* L) +{ + LOG_LMG << "map:match_location\n"; + gamemap_base& m = luaW_checkterrainmap(L, 1); + map_location loc = luaW_checklocation(L, 2); + if(luaW_is_mgfilter(L, 3)) { + lua_mapgen::filter& f = luaW_check_mgfilter(L, 3); + lua_pushboolean(L, f.matches(m, loc)); + } else if (lua_istable(L, 3)) { + lua_mapgen::filter f(L, 3, 0); + lua_pushboolean(L, f.matches(m, loc)); + } + else { + return luaW_type_error(L, 3, "terrainfilter"); + } + return 1; +} + int intf_mg_get_tiles_radius(lua_State* L) { gamemap_base& m = luaW_checkterrainmap(L, 1); diff --git a/src/scripting/lua_terrainfilter.hpp b/src/scripting/lua_terrainfilter.hpp index 4f0f38687afef..bfd5f8d735f76 100644 --- a/src/scripting/lua_terrainfilter.hpp +++ b/src/scripting/lua_terrainfilter.hpp @@ -66,6 +66,7 @@ void lua_mgfilter_setmetatable(lua_State *L); int intf_terrainfilter_create(lua_State *L); +int intf_mg_match_location(lua_State* L); int intf_mg_get_locations(lua_State* L); int intf_mg_get_tiles_radius(lua_State* L); diff --git a/src/scripting/mapgen_lua_kernel.cpp b/src/scripting/mapgen_lua_kernel.cpp index a5eecfce0e74c..51b947977c9eb 100644 --- a/src/scripting/mapgen_lua_kernel.cpp +++ b/src/scripting/mapgen_lua_kernel.cpp @@ -234,6 +234,7 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars) static luaL_Reg const map_callbacks[] { // Map methods + { "matches", &intf_mg_match_location }, { "find", &intf_mg_get_locations }, { "find_in_radius", &intf_mg_get_tiles_radius }, // Static functions