From f4900dfdb08d99569f959993f2f1ecd084b52da6 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sat, 20 Feb 2021 00:42:17 -0500 Subject: [PATCH] Rename existing map functions in the map generation kernel --- data/lua/core.lua | 13 +++++++++++++ src/scripting/game_lua_kernel.cpp | 2 +- src/scripting/lua_terrainfilter.cpp | 9 +++++++-- src/scripting/lua_terrainmap.cpp | 9 +-------- src/scripting/lua_terrainmap.hpp | 2 +- src/scripting/mapgen_lua_kernel.cpp | 23 ++++++++++++++++++----- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/data/lua/core.lua b/data/lua/core.lua index 29142246dde66..9a503c0736959 100644 --- a/data/lua/core.lua +++ b/data/lua/core.lua @@ -762,6 +762,19 @@ else wml.shallow_parsed = wesnoth.deprecate_api('wml.shallow_parsed', '(no replacement)', 1, nil, wml.shallow_parsed, 'Since vconfigs are not supported outside of the game kernel, this function is redundant and will be removed from plugin and map generation contexts. It will continue to work in the game kernel.') end +if wesnoth.kernel_type() == "Mapgen Lua Kernel" then + -- More map module stuff + wesnoth.create_filter = wesnoth.deprecate_api('wesnoth.create_filter', 'wesnoth.map.filter', 1, nil, wesnoth.map.filter) + wesnoth.create_map = wesnoth.deprecate_api('wesnoth.create_map', 'wesnoth.map.create', 1, nil, wesnoth.map.create) + wesnoth.default_generate_height_map = wesnoth.deprecate_api('wesnoth.default_generate_height_map', 'wesnoth.map.generate_height_map', 1, nil, wesnoth.map.generate_height_map) + wesnoth.generate_default_map = wesnoth.deprecate_api('wesnoth.generate_default_map', 'wesnoth.map.generate', 1, nil, wesnoth.map.generate) + -- These were originally only on the map metatable, so the deprecated versions also need to be in the map module + wesnoth.map.get_locations = wesnoth.deprecate_api('map:get_locations', 'map:find', 1, nil, wesnoth.map.find) + wesnoth.map.get_tiles_radius = wesnoth.deprecate_api('map:get_tiles_radius', 'map:find_in_radius', 1, nil, function(map, locs, filter, radius) + return wesnoth.map.find_in_radius(map, locs, radius, filter) + end, 'The filter is now the last parameter, instead of the radius') +end + --[========[GUI2 Dialog Manipulations]========] --! Displays a WML message box with attributes from table @attr and options diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 1047bec4f02e0..41ce62fb6495d 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -4068,7 +4068,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports cmd_log_ << lua_unit_type::register_table(L); // Create the unit_types table - cmd_log_ << lua_terrainmap::register_metatables(L, false); + cmd_log_ << lua_terrainmap::register_metatables(L); // Create the ai elements table. cmd_log_ << "Adding ai elements table...\n"; diff --git a/src/scripting/lua_terrainfilter.cpp b/src/scripting/lua_terrainfilter.cpp index 1e7876c38c0f9..05671a26286b8 100644 --- a/src/scripting/lua_terrainfilter.cpp +++ b/src/scripting/lua_terrainfilter.cpp @@ -184,6 +184,11 @@ static int luaW_push_locationset(lua_State* L, const std::set& loc static std::set luaW_to_locationset(lua_State* L, int index) { std::set res; + map_location single; + if(luaW_tolocation(L, index, single)) { + res.insert(single); + return res; + } lua_pushvalue(L, index); size_t len = lua_rawlen(L, -1); for(size_t i = 0; i != len; ++i) { @@ -734,10 +739,10 @@ int intf_mg_get_locations(lua_State* L) int intf_mg_get_tiles_radius(lua_State* L) { gamemap_base& m = luaW_checkterrainmap(L, 1); - lua_mapgen::filter& f = luaW_check_mgfilter(L, 3); location_set s = luaW_to_locationset(L, 2); + int r = luaL_checkinteger(L, 3); + lua_mapgen::filter& f = luaW_check_mgfilter(L, 4); location_set res; - int r = luaL_checkinteger(L, 4); get_tiles_radius(std::move(s), r, res, [&](const map_location& l) { return m.on_board_with_border(l); diff --git a/src/scripting/lua_terrainmap.cpp b/src/scripting/lua_terrainmap.cpp index 782612e8386ce..6c0c10f7b5966 100644 --- a/src/scripting/lua_terrainmap.cpp +++ b/src/scripting/lua_terrainmap.cpp @@ -491,7 +491,7 @@ static int impl_replace_if_failed_tostring(lua_State* L) } namespace lua_terrainmap { - std::string register_metatables(lua_State* L, bool use_tf) + std::string register_metatables(lua_State* L) { std::ostringstream cmd_out; @@ -506,13 +506,6 @@ namespace lua_terrainmap { lua_setfield(L, -2, "__newindex"); lua_pushstring(L, terrainmapKey); lua_setfield(L, -2, "__metatable"); - // terrainmap methods - if(use_tf) { - lua_pushcfunction(L, intf_mg_get_locations); - lua_setfield(L, -2, "get_locations"); - lua_pushcfunction(L, intf_mg_get_tiles_radius); - lua_setfield(L, -2, "get_tiles_radius"); - } luaL_newmetatable(L, mapReplaceIfFailedKey); lua_pushcfunction(L, impl_replace_if_failed_tostring); diff --git a/src/scripting/lua_terrainmap.hpp b/src/scripting/lua_terrainmap.hpp index a559af48c1331..fe2093bd2f9bb 100644 --- a/src/scripting/lua_terrainmap.hpp +++ b/src/scripting/lua_terrainmap.hpp @@ -60,5 +60,5 @@ int intf_terrainmap_get(lua_State *L); int intf_replace_if_failed(lua_State* L); namespace lua_terrainmap { - std::string register_metatables(lua_State *L, bool use_tf); + std::string register_metatables(lua_State *L); } diff --git a/src/scripting/mapgen_lua_kernel.cpp b/src/scripting/mapgen_lua_kernel.cpp index 5b03a0ca91464..651d267324fe2 100644 --- a/src/scripting/mapgen_lua_kernel.cpp +++ b/src/scripting/mapgen_lua_kernel.cpp @@ -222,10 +222,6 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars) static luaL_Reg const callbacks[] { { "find_path", &intf_find_path }, { "random", &intf_random }, - { "create_filter", &intf_terrainfilter_create }, - { "create_map", &intf_terrainmap_create }, - { "default_generate_height_map", &intf_default_generate_height_map }, - { "generate_default_map", &intf_default_generate }, { "get_variable", &dispatch<&mapgen_lua_kernel::intf_get_variable> }, { nullptr, nullptr } }; @@ -236,8 +232,25 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars) lua_pop(L, 1); assert(lua_gettop(L) == 0); + static luaL_Reg const map_callbacks[] { + // Map methods + { "find", &intf_mg_get_locations }, + { "find_in_radius", &intf_mg_get_tiles_radius }, + // Static functions + { "filter", &intf_terrainfilter_create }, + { "create", &intf_terrainmap_create }, + { "generate_height_map", &intf_default_generate_height_map }, + { "generate", &intf_default_generate }, + { nullptr, nullptr } + }; + + luaW_getglobal(L, "wesnoth", "map"); + assert(lua_istable(L,-1)); + luaL_setfuncs(L, callbacks, 0); + lua_pop(L, 1); + assert(lua_gettop(L) == 0); - cmd_log_ << lua_terrainmap::register_metatables(L, true); + cmd_log_ << lua_terrainmap::register_metatables(L); cmd_log_ << lua_terrainfilter::register_metatables(L); }