Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wesnoth.unit_types and wesnoth.terrain_types are now available in map generation scripts #5838

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 0 additions & 52 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -839,45 +839,6 @@ int game_lua_kernel::intf_lock_view(lua_State *L)
return 0;
}

/**
* Gets details about a terrain.
* - Arg 1: terrain code string.
* - Ret 1: table.
*/
int game_lua_kernel::impl_get_terrain_info(lua_State *L)
{
char const *m = luaL_checkstring(L, 2);
t_translation::terrain_code t = t_translation::read_terrain_code(m);
if (t == t_translation::NONE_TERRAIN) return 0;
const terrain_type& info = board().map().tdata()->get_terrain_info(t);

lua_newtable(L);
lua_pushstring(L, info.id().c_str());
lua_setfield(L, -2, "id");
luaW_pushtstring(L, info.name());
lua_setfield(L, -2, "name");
luaW_pushtstring(L, info.editor_name());
lua_setfield(L, -2, "editor_name");
luaW_pushtstring(L, info.description());
lua_setfield(L, -2, "description");
lua_push(L, info.icon_image());
lua_setfield(L, -2, "icon");
lua_push(L, info.editor_image());
lua_setfield(L, -2, "editor_image");
lua_pushinteger(L, info.light_bonus(0));
lua_setfield(L, -2, "light");
lua_pushboolean(L, info.is_village());
lua_setfield(L, -2, "village");
lua_pushboolean(L, info.is_castle());
lua_setfield(L, -2, "castle");
lua_pushboolean(L, info.is_keep());
lua_setfield(L, -2, "keep");
lua_pushinteger(L, info.gives_healing());
lua_setfield(L, -2, "healing");

return 1;
}

/**
* Gets time of day information.
* - Arg 1: optional turn number
Expand Down Expand Up @@ -4089,19 +4050,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
// Create the unit_types table
cmd_log_ << lua_terrainmap::register_metatables(L);

// Create the unit_types table
cmd_log_ << "Adding terrain_types table...\n";
lua_getglobal(L, "wesnoth");
lua_newuserdatauv(L, 0, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_get_terrain_info>);
lua_setfield(L, -2, "__index");
lua_pushstring(L, "terrain types");
lua_setfield(L, -2, "__metatable");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "terrain_types");
lua_pop(L, 1);

// Create the ai elements table.
cmd_log_ << "Adding ai elements table...\n";

Expand Down
1 change: 0 additions & 1 deletion src/scripting/game_lua_kernel.hpp
Expand Up @@ -92,7 +92,6 @@ class game_lua_kernel : public lua_kernel_base
int intf_unit_ability(lua_State *L);
int intf_view_locked(lua_State *L);
int intf_lock_view(lua_State *L);
int impl_get_terrain_info(lua_State *L);
int intf_get_time_of_day(lua_State *L);
int intf_get_max_liminal_bonus(lua_State *L);
int intf_get_village_owner(lua_State *L);
Expand Down
53 changes: 53 additions & 0 deletions src/scripting/lua_terrainmap.cpp
Expand Up @@ -25,6 +25,7 @@
#include "resources.hpp"
#include "game_board.hpp"
#include "play_controller.hpp"
#include "game_config_manager.hpp"

#include "lua/lauxlib.h"
#include "lua/lua.h"
Expand Down Expand Up @@ -570,6 +571,46 @@ static int impl_replace_if_failed_tostring(lua_State* L)
return 1;
}

/**
* Gets details about a terrain.
* - Arg 1: terrain code string.
* - Ret 1: table.
*/
int impl_get_terrain_info(lua_State *L)
{
char const *m = luaL_checkstring(L, 2);
t_translation::terrain_code t = t_translation::read_terrain_code(m);
if (t == t_translation::NONE_TERRAIN) return 0;
auto tdata = game_config_manager::get()->terrain_types();
const terrain_type& info = tdata->get_terrain_info(t);

lua_newtable(L);
lua_pushstring(L, info.id().c_str());
lua_setfield(L, -2, "id");
luaW_pushtstring(L, info.name());
lua_setfield(L, -2, "name");
luaW_pushtstring(L, info.editor_name());
lua_setfield(L, -2, "editor_name");
luaW_pushtstring(L, info.description());
lua_setfield(L, -2, "description");
lua_push(L, info.icon_image());
lua_setfield(L, -2, "icon");
lua_push(L, info.editor_image());
lua_setfield(L, -2, "editor_image");
lua_pushinteger(L, info.light_bonus(0));
lua_setfield(L, -2, "light");
lua_pushboolean(L, info.is_village());
lua_setfield(L, -2, "village");
lua_pushboolean(L, info.is_castle());
lua_setfield(L, -2, "castle");
lua_pushboolean(L, info.is_keep());
lua_setfield(L, -2, "keep");
lua_pushinteger(L, info.gives_healing());
lua_setfield(L, -2, "healing");

return 1;
}

namespace lua_terrainmap {
std::string register_metatables(lua_State* L)
{
Expand Down Expand Up @@ -605,6 +646,18 @@ namespace lua_terrainmap {
lua_pushstring(L, maplocationKey);
lua_setfield(L, -2, "__metatable");

cmd_out << "Adding terrain_types table...\n";
lua_getglobal(L, "wesnoth");
lua_newuserdatauv(L, 0, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, impl_get_terrain_info);
lua_setfield(L, -2, "__index");
lua_pushstring(L, "terrain types");
lua_setfield(L, -2, "__metatable");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "terrain_types");
lua_pop(L, 1);

return cmd_out.str();
}
}
2 changes: 2 additions & 0 deletions src/scripting/mapgen_lua_kernel.cpp
Expand Up @@ -22,6 +22,7 @@
#include "scripting/lua_pathfind_cost_calculator.hpp"
#include "scripting/lua_terrainfilter.hpp"
#include "scripting/lua_terrainmap.hpp"
#include "scripting/lua_unit_type.hpp"

#include <ostream>
#include <string>
Expand Down Expand Up @@ -263,6 +264,7 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars)

cmd_log_ << lua_terrainmap::register_metatables(L);
cmd_log_ << lua_terrainfilter::register_metatables(L);
cmd_log_ << lua_unit_type::register_table(L);
}

void mapgen_lua_kernel::run_generator(const char * prog, const config & generator)
Expand Down