From 711b77b7e25c2d94c0dc1611801210b5179b696b Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sat, 14 Sep 2024 18:10:47 -0400 Subject: [PATCH] Lua API: Add __dir metamethod to wesnoth.terrain_types --- src/scripting/game_lua_kernel.cpp | 18 ++++++++++++++++++ src/scripting/game_lua_kernel.hpp | 1 + 2 files changed, 19 insertions(+) diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 99fdf45c0e4..5ceda504110 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -1124,6 +1124,22 @@ int game_lua_kernel::impl_get_terrain_info(lua_State *L) return 1; } +/** + * Gets a list of known terrain codes. + * - Ret 1: array of terrain codes + */ +int game_lua_kernel::impl_get_terrain_list(lua_State *L) +{ + auto codes = board().map().tdata()->list(); + std::vector terrains; + terrains.reserve(codes.size()); + for(auto code : codes) { + terrains.push_back(t_translation::write_terrain_code(code)); + } + lua_push(L, terrains); + return 1; +} + /** * Gets time of day information. * - Arg 1: schedule object, location, time area ID, or nil @@ -5267,6 +5283,8 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports lua_createtable(L, 0, 2); lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_get_terrain_info>); lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_get_terrain_list>); + lua_setfield(L, -2, "__dir"); lua_pushstring(L, "terrain types"); lua_setfield(L, -2, "__metatable"); lua_setmetatable(L, -2); diff --git a/src/scripting/game_lua_kernel.hpp b/src/scripting/game_lua_kernel.hpp index 5d8c3098483..b56d994ffe1 100644 --- a/src/scripting/game_lua_kernel.hpp +++ b/src/scripting/game_lua_kernel.hpp @@ -97,6 +97,7 @@ class game_lua_kernel : public lua_kernel_base int intf_view_locked(lua_State *L); int intf_lock_view(lua_State *L); int impl_get_terrain_info(lua_State *L); + int impl_get_terrain_list(lua_State *L); template int intf_get_time_of_day(lua_State *L); int impl_schedule_get(lua_State *L);