add VoxelStructure lua usertype
This commit is contained in:
@@ -158,7 +158,7 @@ static int l_file_write_bytes(lua::State* L) {
|
||||
|
||||
fs::path path = resolve_path(lua::require_string(L, pathIndex));
|
||||
|
||||
if (auto bytearray = lua::touserdata<lua::Bytearray>(L, -1)) {
|
||||
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1)) {
|
||||
auto& bytes = bytearray->data();
|
||||
return lua::pushboolean(
|
||||
L, files::write_bytes(path, bytes.data(), bytes.size())
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "coders/binary_json.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/generator/VoxelStructure.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -25,6 +27,21 @@ static int l_save_structure(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_load_structure(lua::State* L) {
|
||||
auto paths = engine->getPaths();
|
||||
auto filename = lua::require_string(L, 1);
|
||||
auto path = paths->resolve(filename);
|
||||
if (!std::filesystem::exists(path)) {
|
||||
throw std::runtime_error("file "+path.u8string()+" does not exist");
|
||||
}
|
||||
auto map = files::read_binary_json(path);
|
||||
|
||||
auto structure = std::make_shared<VoxelStructure>();
|
||||
structure->deserialize(map);
|
||||
return lua::newuserdata<lua::LuaVoxelStructure>(L, std::move(structure));
|
||||
}
|
||||
|
||||
const luaL_Reg generationlib[] = {
|
||||
{"save_structure", lua::wrap<l_save_structure>},
|
||||
{"load_structure", lua::wrap<l_load_structure>},
|
||||
{NULL, NULL}};
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
#include <vector>
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
#include "maths/Heightmap.hpp"
|
||||
|
||||
struct fnl_state;
|
||||
class Heightmap;
|
||||
class VoxelStructure;
|
||||
|
||||
namespace lua {
|
||||
class Userdata {
|
||||
@@ -15,12 +16,12 @@ namespace lua {
|
||||
virtual const std::string& getTypeName() const = 0;
|
||||
};
|
||||
|
||||
class Bytearray : public Userdata {
|
||||
class LuaBytearray : public Userdata {
|
||||
std::vector<ubyte> buffer;
|
||||
public:
|
||||
Bytearray(size_t capacity);
|
||||
Bytearray(std::vector<ubyte> buffer);
|
||||
virtual ~Bytearray();
|
||||
LuaBytearray(size_t capacity);
|
||||
LuaBytearray(std::vector<ubyte> buffer);
|
||||
virtual ~LuaBytearray();
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
@@ -32,6 +33,7 @@ namespace lua {
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Bytearray";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaBytearray>());
|
||||
|
||||
class LuaHeightmap : public Userdata {
|
||||
std::shared_ptr<Heightmap> map;
|
||||
@@ -41,21 +43,13 @@ namespace lua {
|
||||
|
||||
virtual ~LuaHeightmap();
|
||||
|
||||
uint getWidth() const {
|
||||
return map->getWidth();
|
||||
}
|
||||
uint getWidth() const;
|
||||
|
||||
uint getHeight() const {
|
||||
return map->getHeight();
|
||||
}
|
||||
uint getHeight() const;
|
||||
|
||||
float* getValues() {
|
||||
return map->getValues();
|
||||
}
|
||||
float* getValues();
|
||||
|
||||
const float* getValues() const {
|
||||
return map->getValues();
|
||||
}
|
||||
const float* getValues() const;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
@@ -74,4 +68,25 @@ namespace lua {
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Heightmap";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaHeightmap>());
|
||||
|
||||
class LuaVoxelStructure : public Userdata {
|
||||
std::shared_ptr<VoxelStructure> structure;
|
||||
public:
|
||||
LuaVoxelStructure(std::shared_ptr<VoxelStructure> structure);
|
||||
|
||||
virtual ~LuaVoxelStructure();
|
||||
|
||||
std::shared_ptr<VoxelStructure> getStructure() const {
|
||||
return structure;
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "VoxelStructure";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaVoxelStructure>());
|
||||
}
|
||||
|
||||
@@ -96,8 +96,9 @@ void lua::initialize() {
|
||||
|
||||
initialize_libs_extends(L);
|
||||
|
||||
newusertype<Bytearray, Bytearray::createMetatable>(L, "Bytearray");
|
||||
newusertype<LuaHeightmap, LuaHeightmap::createMetatable>(L, "Heightmap");
|
||||
newusertype<LuaBytearray>(L);
|
||||
newusertype<LuaHeightmap>(L);
|
||||
newusertype<LuaVoxelStructure>(L);
|
||||
}
|
||||
|
||||
void lua::finalize() {
|
||||
|
||||
@@ -248,10 +248,12 @@ namespace lua {
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <class T, lua_CFunction func>
|
||||
inline void newusertype(lua::State* L, const std::string& name) {
|
||||
template <class T>
|
||||
inline std::enable_if_t<std::is_base_of_v<Userdata, T>>
|
||||
newusertype(lua::State* L) {
|
||||
const std::string& name = T::TYPENAME;
|
||||
usertypeNames[typeid(T)] = name;
|
||||
func(L);
|
||||
T::createMetatable(L);
|
||||
|
||||
pushcfunction(L, userdata_destructor);
|
||||
setfield(L, "__gc");
|
||||
|
||||
+18
-18
@@ -1,23 +1,23 @@
|
||||
#include "lua_custom_types.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "lua_util.hpp"
|
||||
#include "../lua_util.hpp"
|
||||
|
||||
using namespace lua;
|
||||
|
||||
Bytearray::Bytearray(size_t capacity) : buffer(capacity) {
|
||||
LuaBytearray::LuaBytearray(size_t capacity) : buffer(capacity) {
|
||||
buffer.resize(capacity);
|
||||
}
|
||||
|
||||
Bytearray::Bytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
|
||||
LuaBytearray::LuaBytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
|
||||
}
|
||||
|
||||
Bytearray::~Bytearray() {
|
||||
LuaBytearray::~LuaBytearray() {
|
||||
}
|
||||
|
||||
static int l_append(lua::State* L) {
|
||||
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
||||
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
|
||||
auto value = tointeger(L, 2);
|
||||
buffer->data().push_back(static_cast<ubyte>(value));
|
||||
}
|
||||
@@ -25,7 +25,7 @@ static int l_append(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_insert(lua::State* L) {
|
||||
auto buffer = touserdata<Bytearray>(L, 1);
|
||||
auto buffer = touserdata<LuaBytearray>(L, 1);
|
||||
if (buffer == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ static int l_insert(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_remove(lua::State* L) {
|
||||
auto buffer = touserdata<Bytearray>(L, 1);
|
||||
auto buffer = touserdata<LuaBytearray>(L, 1);
|
||||
if (buffer == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -69,17 +69,17 @@ static int l_meta_meta_call(lua::State* L) {
|
||||
buffer[i] = static_cast<ubyte>(tointeger(L, -1));
|
||||
pop(L);
|
||||
}
|
||||
return newuserdata<Bytearray>(L, std::move(buffer));
|
||||
return newuserdata<LuaBytearray>(L, std::move(buffer));
|
||||
}
|
||||
auto size = tointeger(L, 2);
|
||||
if (size < 0) {
|
||||
throw std::runtime_error("size can not be less than 0");
|
||||
}
|
||||
return newuserdata<Bytearray>(L, static_cast<size_t>(size));
|
||||
return newuserdata<LuaBytearray>(L, static_cast<size_t>(size));
|
||||
}
|
||||
|
||||
static int l_meta_index(lua::State* L) {
|
||||
auto buffer = touserdata<Bytearray>(L, 1);
|
||||
auto buffer = touserdata<LuaBytearray>(L, 1);
|
||||
if (buffer == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ static int l_meta_index(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_meta_newindex(lua::State* L) {
|
||||
auto buffer = touserdata<Bytearray>(L, 1);
|
||||
auto buffer = touserdata<LuaBytearray>(L, 1);
|
||||
if (buffer == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -116,14 +116,14 @@ static int l_meta_newindex(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_meta_len(lua::State* L) {
|
||||
if (auto buffer = touserdata<Bytearray>(L, 1)) {
|
||||
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
|
||||
return pushinteger(L, buffer->data().size());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_meta_tostring(lua::State* L) {
|
||||
auto buffer = touserdata<Bytearray>(L, 1);
|
||||
auto buffer = touserdata<LuaBytearray>(L, 1);
|
||||
if (buffer == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -147,8 +147,8 @@ static int l_meta_tostring(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_meta_add(lua::State* L) {
|
||||
auto bufferA = touserdata<Bytearray>(L, 1);
|
||||
auto bufferB = touserdata<Bytearray>(L, 2);
|
||||
auto bufferA = touserdata<LuaBytearray>(L, 1);
|
||||
auto bufferB = touserdata<LuaBytearray>(L, 2);
|
||||
if (bufferA == nullptr || bufferB == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
@@ -159,10 +159,10 @@ static int l_meta_add(lua::State* L) {
|
||||
ab.reserve(dataA.size() + dataB.size());
|
||||
ab.insert(ab.end(), dataA.begin(), dataA.end());
|
||||
ab.insert(ab.end(), dataB.begin(), dataB.end());
|
||||
return newuserdata<Bytearray>(L, std::move(ab));
|
||||
return newuserdata<LuaBytearray>(L, std::move(ab));
|
||||
}
|
||||
|
||||
int Bytearray::createMetatable(lua::State* L) {
|
||||
int LuaBytearray::createMetatable(lua::State* L) {
|
||||
createtable(L, 0, 6);
|
||||
pushcfunction(L, lua::wrap<l_meta_index>);
|
||||
setfield(L, "__index");
|
||||
+19
-2
@@ -1,4 +1,4 @@
|
||||
#include "lua_custom_types.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
@@ -11,7 +11,8 @@
|
||||
#include "coders/png.hpp"
|
||||
#include "files/util.hpp"
|
||||
#include "graphics/core/ImageData.hpp"
|
||||
#include "lua_util.hpp"
|
||||
#include "maths/Heightmap.hpp"
|
||||
#include "../lua_util.hpp"
|
||||
|
||||
using namespace lua;
|
||||
|
||||
@@ -27,6 +28,22 @@ void LuaHeightmap::setSeed(int64_t seed) {
|
||||
noise->seed = seed;
|
||||
}
|
||||
|
||||
uint LuaHeightmap::getWidth() const {
|
||||
return map->getWidth();
|
||||
}
|
||||
|
||||
uint LuaHeightmap::getHeight() const {
|
||||
return map->getHeight();
|
||||
}
|
||||
|
||||
float* LuaHeightmap::getValues() {
|
||||
return map->getValues();
|
||||
}
|
||||
|
||||
const float* LuaHeightmap::getValues() const {
|
||||
return map->getValues();
|
||||
}
|
||||
|
||||
static int l_dump(lua::State* L) {
|
||||
if (auto heightmap = touserdata<LuaHeightmap>(L, 1)) {
|
||||
auto filename = require_string(L, 2);
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
#include "../lua_util.hpp"
|
||||
|
||||
#include "world/generator/VoxelStructure.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
using namespace lua;
|
||||
|
||||
LuaVoxelStructure::LuaVoxelStructure(std::shared_ptr<VoxelStructure> structure)
|
||||
: structure(std::move(structure)) {}
|
||||
|
||||
LuaVoxelStructure::~LuaVoxelStructure() {
|
||||
}
|
||||
|
||||
static int l_meta_tostring(lua::State* L) {
|
||||
return pushstring(L, "VoxelStructure(0x" + util::tohex(
|
||||
reinterpret_cast<uint64_t>(topointer(L, 1)))+")");
|
||||
}
|
||||
|
||||
int LuaVoxelStructure::createMetatable(lua::State* L) {
|
||||
createtable(L, 0, 1);
|
||||
pushcfunction(L, lua::wrap<l_meta_tostring>);
|
||||
setfield(L, "__tostring");
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user