add block.get_field(...), block.set_field(...)
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "voxels/voxel.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "maths/voxmaths.hpp"
|
||||
#include "data/StructLayout.hpp"
|
||||
#include "api_lua.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
@@ -436,6 +438,107 @@ static int l_decompose_state(lua::State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_get_field(lua::State* L) {
|
||||
auto x = lua::tointeger(L, 1);
|
||||
auto y = lua::tointeger(L, 2);
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto name = lua::require_string(L, 4);
|
||||
size_t index = 0;
|
||||
if (lua::gettop(L) >= 5) {
|
||||
index = lua::tointeger(L, 5);
|
||||
}
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
auto cx = floordiv(x, CHUNK_W);
|
||||
auto cz = floordiv(z, CHUNK_D);
|
||||
auto chunk = level->chunks->getChunk(cx, cz);
|
||||
auto lx = x - cx * CHUNK_W;
|
||||
auto lz = z - cz * CHUNK_W;
|
||||
size_t voxelIndex = vox_index(lx, y, lz);
|
||||
|
||||
const auto& def = content->getIndices()->blocks.require(vox->id);
|
||||
if (def.dataStruct == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const auto& dataStruct = *def.dataStruct;
|
||||
const auto field = dataStruct.getField(name);
|
||||
if (field == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const ubyte* src = chunk->blocksMetadata.find(voxelIndex);
|
||||
if (src == nullptr) {
|
||||
throw std::runtime_error("block data is not allocated");
|
||||
}
|
||||
switch (field->type) {
|
||||
case data::FieldType::I8:
|
||||
case data::FieldType::I16:
|
||||
case data::FieldType::I32:
|
||||
case data::FieldType::I64:
|
||||
return lua::pushinteger(L, dataStruct.getInteger(src, *field, index));
|
||||
case data::FieldType::F32:
|
||||
case data::FieldType::F64:
|
||||
return lua::pushnumber(L, dataStruct.getNumber(src, *field, index));
|
||||
case data::FieldType::CHAR:
|
||||
return lua::pushstring(L,
|
||||
std::string(dataStruct.getChars(src, *field)).c_str());
|
||||
case data::FieldType::COUNT:
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_field(lua::State* L) {
|
||||
auto x = lua::tointeger(L, 1);
|
||||
auto y = lua::tointeger(L, 2);
|
||||
auto z = lua::tointeger(L, 3);
|
||||
auto name = lua::require_string(L, 4);
|
||||
auto value = lua::tovalue(L, 5);
|
||||
size_t index = 0;
|
||||
if (lua::gettop(L) >= 6) {
|
||||
index = lua::tointeger(L, 6);
|
||||
}
|
||||
auto vox = level->chunks->get(x, y, z);
|
||||
auto cx = floordiv(x, CHUNK_W);
|
||||
auto cz = floordiv(z, CHUNK_D);
|
||||
auto chunk = level->chunks->getChunk(cx, cz);
|
||||
auto lx = x - cx * CHUNK_W;
|
||||
auto lz = z - cz * CHUNK_W;
|
||||
size_t voxelIndex = vox_index(lx, y, lz);
|
||||
|
||||
const auto& def = content->getIndices()->blocks.require(vox->id);
|
||||
if (def.dataStruct == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const auto& dataStruct = *def.dataStruct;
|
||||
const auto field = dataStruct.getField(name);
|
||||
if (field == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
ubyte* dst = chunk->blocksMetadata.find(voxelIndex);
|
||||
if (dst == nullptr) {
|
||||
throw std::runtime_error("block data is not allocated");
|
||||
}
|
||||
switch (field->type) {
|
||||
case data::FieldType::CHAR:
|
||||
if (value.isString()) {
|
||||
return lua::pushinteger(L,
|
||||
dataStruct.setUnicode(dst, value.asString(), *field));
|
||||
}
|
||||
case data::FieldType::I8:
|
||||
case data::FieldType::I16:
|
||||
case data::FieldType::I32:
|
||||
case data::FieldType::I64:
|
||||
dataStruct.setInteger(dst, value.asInteger(), *field, index);
|
||||
break;
|
||||
case data::FieldType::F32:
|
||||
case data::FieldType::F64:
|
||||
dataStruct.setNumber(dst, value.asNumber(), *field, index);
|
||||
break;
|
||||
case data::FieldType::COUNT:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg blocklib[] = {
|
||||
{"index", lua::wrap<l_index>},
|
||||
{"name", lua::wrap<l_get_def>},
|
||||
@@ -469,5 +572,7 @@ const luaL_Reg blocklib[] = {
|
||||
{"raycast", lua::wrap<l_raycast>},
|
||||
{"compose_state", lua::wrap<l_compose_state>},
|
||||
{"decompose_state", lua::wrap<l_decompose_state>},
|
||||
{"get_field", lua::wrap<l_get_field>},
|
||||
{"set_field", lua::wrap<l_set_field>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user