diff --git a/data/lua/core/map.lua b/data/lua/core/map.lua index ed6d8e7114d1..fc7b56a5a6b4 100644 --- a/data/lua/core/map.lua +++ b/data/lua/core/map.lua @@ -123,4 +123,17 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then return wesnoth.map.find{gives_income = true, wml.tag["and"](cfg)} end) wesnoth.match_location = wesnoth.deprecate_api('wesnoth.match_location', 'wesnoth.map.matches', 1, nil, wesnoth.map.matches) -end \ No newline at end of file +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 diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 1047bec4f02e..41ce62fb6495 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 1e7876c38c0f..05671a26286b 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 97ff2fe73ec5..5482e7d644b6 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 a559af48c133..fe2093bd2f9b 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 5b03a0ca9146..a5eecfce0e74 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, map_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); }