diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 68a95cb8..6a8f3dd2 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -30,6 +30,7 @@ extern const luaL_Reg jsonlib[]; extern const luaL_Reg mat4lib[]; extern const luaL_Reg packlib[]; extern const luaL_Reg playerlib[]; +extern const luaL_Reg generationlib[]; extern const luaL_Reg quatlib[]; // quat.cpp extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; diff --git a/src/logic/scripting/lua/libgeneration.cpp b/src/logic/scripting/lua/libgeneration.cpp new file mode 100644 index 00000000..c9b55e1d --- /dev/null +++ b/src/logic/scripting/lua/libgeneration.cpp @@ -0,0 +1,4 @@ +#include "api_lua.hpp" + +const luaL_Reg generationlib[] = { + {NULL, NULL}}; diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index 4b603aca..b9d6f5ef 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -29,4 +29,27 @@ namespace lua { static int createMetatable(lua::State*); inline static std::string TYPENAME = "bytearray"; }; + + class Heightmap : public Userdata { + std::vector buffer; + uint width, height; + public: + Heightmap(uint width, uint height); + virtual ~Heightmap(); + + uint getWidth() const { + return width; + } + + uint getHeight() const { + return height; + } + + const std::string& getTypeName() const override { + return TYPENAME; + } + + static int createMetatable(lua::State*); + inline static std::string TYPENAME = "heightmap"; + }; } diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 01df4ea9..8008517a 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -37,6 +37,7 @@ static void create_libs(lua::State* L) { openlib(L, "gui", guilib); openlib(L, "input", inputlib); openlib(L, "inventory", inventorylib); + openlib(L, "generation", generationlib); openlib(L, "item", itemlib); openlib(L, "json", jsonlib); openlib(L, "mat4", mat4lib); @@ -95,7 +96,8 @@ void lua::initialize() { initialize_libs_extends(L); - newusertype(L, "bytearray"); + newusertype(L, "Bytearray"); + newusertype(L, "Heightmap"); } void lua::finalize() { diff --git a/src/logic/scripting/lua/lua_custom_types.cpp b/src/logic/scripting/lua/lua_type_bytearray.cpp similarity index 81% rename from src/logic/scripting/lua/lua_custom_types.cpp rename to src/logic/scripting/lua/lua_type_bytearray.cpp index 8ee90d92..9c81c380 100644 --- a/src/logic/scripting/lua/lua_custom_types.cpp +++ b/src/logic/scripting/lua/lua_type_bytearray.cpp @@ -16,7 +16,7 @@ Bytearray::Bytearray(std::vector buffer) : buffer(std::move(buffer)) { Bytearray::~Bytearray() { } -static int l_bytearray_append(lua::State* L) { +static int l_append(lua::State* L) { if (auto buffer = touserdata(L, 1)) { auto value = tointeger(L, 2); buffer->data().push_back(static_cast(value)); @@ -24,7 +24,7 @@ static int l_bytearray_append(lua::State* L) { return 0; } -static int l_bytearray_insert(lua::State* L) { +static int l_insert(lua::State* L) { auto buffer = touserdata(L, 1); if (buffer == nullptr) { return 0; @@ -39,7 +39,7 @@ static int l_bytearray_insert(lua::State* L) { return 0; } -static int l_bytearray_remove(lua::State* L) { +static int l_remove(lua::State* L) { auto buffer = touserdata(L, 1); if (buffer == nullptr) { return 0; @@ -54,12 +54,12 @@ static int l_bytearray_remove(lua::State* L) { } static std::unordered_map bytearray_methods { - {"append", lua::wrap}, - {"insert", lua::wrap}, - {"remove", lua::wrap}, + {"append", lua::wrap}, + {"insert", lua::wrap}, + {"remove", lua::wrap}, }; -static int l_bytearray_meta_meta_call(lua::State* L) { +static int l_meta_meta_call(lua::State* L) { if (lua_istable(L, 2)) { size_t len = objlen(L, 2); std::vector buffer(len); @@ -78,7 +78,7 @@ static int l_bytearray_meta_meta_call(lua::State* L) { return newuserdata(L, static_cast(size)); } -static int l_bytearray_meta_index(lua::State* L) { +static int l_meta_index(lua::State* L) { auto buffer = touserdata(L, 1); if (buffer == nullptr) { return 0; @@ -97,7 +97,7 @@ static int l_bytearray_meta_index(lua::State* L) { return pushinteger(L, data[index]); } -static int l_bytearray_meta_newindex(lua::State* L) { +static int l_meta_newindex(lua::State* L) { auto buffer = touserdata(L, 1); if (buffer == nullptr) { return 0; @@ -115,14 +115,14 @@ static int l_bytearray_meta_newindex(lua::State* L) { return 0; } -static int l_bytearray_meta_len(lua::State* L) { +static int l_meta_len(lua::State* L) { if (auto buffer = touserdata(L, 1)) { return pushinteger(L, buffer->data().size()); } return 0; } -static int l_bytearray_meta_tostring(lua::State* L) { +static int l_meta_tostring(lua::State* L) { auto buffer = touserdata(L, 1); if (buffer == nullptr) { return 0; @@ -146,7 +146,7 @@ static int l_bytearray_meta_tostring(lua::State* L) { } } -static int l_bytearray_meta_add(lua::State* L) { +static int l_meta_add(lua::State* L) { auto bufferA = touserdata(L, 1); auto bufferB = touserdata(L, 2); if (bufferA == nullptr || bufferB == nullptr) { @@ -164,19 +164,19 @@ static int l_bytearray_meta_add(lua::State* L) { int Bytearray::createMetatable(lua::State* L) { createtable(L, 0, 6); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__index"); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__newindex"); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__len"); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__tostring"); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__add"); createtable(L, 0, 1); - pushcfunction(L, lua::wrap); + pushcfunction(L, lua::wrap); setfield(L, "__call"); setmetatable(L); return 1; diff --git a/src/logic/scripting/lua/lua_type_heightmap.cpp b/src/logic/scripting/lua/lua_type_heightmap.cpp new file mode 100644 index 00000000..253d56f7 --- /dev/null +++ b/src/logic/scripting/lua/lua_type_heightmap.cpp @@ -0,0 +1,73 @@ +#include "lua_custom_types.hpp" + +#include +#include +#include + +#include "lua_util.hpp" + +using namespace lua; + +Heightmap::Heightmap(uint width, uint height) : width(width), height(height) { + buffer.resize(width*height); +} + +Heightmap::~Heightmap() { +} + +static int l_meta_meta_call(lua::State* L) { + auto width = tointeger(L, 2); + auto height = tointeger(L, 3); + if (width <= 0 || height <= 0) { + throw std::runtime_error("width and height must be greather than 0"); + } + return newuserdata( + L, static_cast(width), static_cast(height) + ); +} + +static int l_meta_index(lua::State* L) { + auto map = touserdata(L, 1); + if (map == nullptr) { + return 0; + } + if (isstring(L, 2)) { + auto fieldname = tostring(L, 2); + if (!std::strcmp(fieldname, "width")) { + return pushinteger(L, map->getWidth()); + } else if (!std::strcmp(fieldname, "height")) { + return pushinteger(L, map->getHeight()); + } + } + return 0; +} + +static int l_meta_tostring(lua::State* L) { + auto map = touserdata(L, 1); + if (map == nullptr) { + return 0; + } + + std::stringstream stream; + stream << std::hex << reinterpret_cast(map); + auto ptrstr = stream.str(); + + return pushstring( + L, "Heightmap(" + std::to_string(map->getWidth()) + + "*" + std::to_string(map->getHeight()) + " at 0x" + ptrstr + ")" + ); +} + +int Heightmap::createMetatable(lua::State* L) { + createtable(L, 0, 2); + pushcfunction(L, lua::wrap); + setfield(L, "__tostring"); + pushcfunction(L, lua::wrap); + setfield(L, "__index"); + + createtable(L, 0, 1); + pushcfunction(L, lua::wrap); + setfield(L, "__call"); + setmetatable(L); + return 1; +}