Skip to content

Commit

Permalink
Rename existing map functions in the map generation kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Feb 20, 2021
1 parent 908479c commit f4900df
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
13 changes: 13 additions & 0 deletions data/lua/core.lua
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -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";
Expand Down
9 changes: 7 additions & 2 deletions src/scripting/lua_terrainfilter.cpp
Expand Up @@ -184,6 +184,11 @@ static int luaW_push_locationset(lua_State* L, const std::set<map_location>& loc
static std::set<map_location> luaW_to_locationset(lua_State* L, int index)
{
std::set<map_location> 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) {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 1 addition & 8 deletions src/scripting/lua_terrainmap.cpp
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_terrainmap.hpp
Expand Up @@ -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);
}
23 changes: 18 additions & 5 deletions src/scripting/mapgen_lua_kernel.cpp
Expand Up @@ -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 }
};
Expand All @@ -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);
}

Expand Down

0 comments on commit f4900df

Please sign in to comment.