diff --git a/src/logic/scripting/lua/libs/libblock.cpp b/src/logic/scripting/lua/libs/libblock.cpp index 7926dbd5..bb527c9b 100644 --- a/src/logic/scripting/lua/libs/libblock.cpp +++ b/src/logic/scripting/lua/libs/libblock.cpp @@ -245,8 +245,25 @@ static int l_get_user_bits(lua::State* L) { } } uint mask = ((1 << bits) - 1) << offset; - uint data = (blockstate2int(vox->state) & mask) >> offset; - return lua::pushinteger(L, data); + return lua::pushinteger(L, (blockstate2int(vox->state) & mask) >> offset); +} + +static int l_get_variant(lua::State* L) { + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + + auto vox = blocks_agent::get(*level->chunks, x, y, z); + if (vox == nullptr) { + return lua::pushinteger(L, 0); + } + const auto& def = content->getIndices()->blocks.require(vox->id); + if (def.variants == nullptr) { + return lua::pushinteger(L, 0); + } + return lua::pushinteger( + L, (vox->state.userbits >> def.variants->offset) & def.variants->mask + ); } static int l_set_user_bits(lua::State* L) { @@ -282,6 +299,43 @@ static int l_set_user_bits(lua::State* L) { return 0; } +static int l_set_variant(lua::State* L) { + auto& chunks = *level->chunks; + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + + int cx = floordiv(x); + int cz = floordiv(z); + auto chunk = blocks_agent::get_chunk(chunks, cx, cz); + if (chunk == nullptr || y < 0 || y >= CHUNK_H) { + return 0; + } + int lx = x - cx * CHUNK_W; + int lz = z - cz * CHUNK_D; + auto vox = &chunk->voxels[vox_index(lx, y, lz)]; + const auto& def = content->getIndices()->blocks.require(vox->id); + + if (def.variants == nullptr) { + return 0; + } + + auto offset = def.variants->offset; + auto mask = def.variants->mask; + auto value = (lua::tointeger(L, 4) << offset) & mask; + + if (def.rt.extended) { + auto origin = blocks_agent::seek_origin(chunks, {x, y, z}, def, vox->state); + vox = blocks_agent::get(chunks, origin.x, origin.y, origin.z); + if (vox == nullptr) { + return 0; + } + } + vox->state.userbits = (vox->state.userbits & (~mask)) | value; + chunk->setModifiedAndUnsaved(); + return 0; +} + static int l_is_replaceable_at(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); @@ -653,6 +707,8 @@ const luaL_Reg blocklib[] = { {"set_rotation", lua::wrap}, {"get_user_bits", lua::wrap}, {"set_user_bits", lua::wrap}, + {"get_variant", lua::wrap}, + {"set_variant", lua::wrap}, {"is_extended", lua::wrap}, {"get_size", lua::wrap}, {"is_segment", lua::wrap},