From 41091a147ce52e843198b261d929840197a78227 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 7 Oct 2024 18:12:18 +0200 Subject: [PATCH] Handle VOXELFLAG_NO_DATA when in VManip get_data() --- src/script/lua_api/l_vmanip.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index 72ecc55b6..ca7ad7971 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -71,8 +71,7 @@ int LuaVoxelManip::l_get_data(lua_State *L) bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; - - u32 volume = vm->m_area.getVolume(); + const u32 volume = vm->m_area.getVolume(); if (use_buffer) lua_pushvalue(L, 2); @@ -80,7 +79,8 @@ int LuaVoxelManip::l_get_data(lua_State *L) lua_createtable(L, volume, 0); for (u32 i = 0; i != volume; i++) { - lua_Integer cid = vm->m_data[i].getContent(); + // Do not push unintialized data to Lua + lua_Integer cid = (vm->m_flags[i] & VOXELFLAG_NO_DATA) ? CONTENT_IGNORE : vm->m_data[i].getContent(); lua_pushinteger(L, cid); lua_rawseti(L, -2, i + 1); } @@ -108,6 +108,12 @@ int LuaVoxelManip::l_set_data(lua_State *L) lua_pop(L, 1); } + // FIXME: in theory we should clear VOXELFLAG_NO_DATA here + // However there is no way to tell which values Lua code has intended to set + // (if they were VOXELFLAG_NO_DATA before), and which were just not touched. + // In practice this doesn't cause problems because read_from_map() will cause + // all covered blocks to be loaded anyway. + return 0; } @@ -231,8 +237,7 @@ int LuaVoxelManip::l_get_light_data(lua_State *L) bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; - - u32 volume = vm->m_area.getVolume(); + const u32 volume = vm->m_area.getVolume(); if (use_buffer) lua_pushvalue(L, 2); @@ -240,7 +245,8 @@ int LuaVoxelManip::l_get_light_data(lua_State *L) lua_createtable(L, volume, 0); for (u32 i = 0; i != volume; i++) { - lua_Integer light = vm->m_data[i].param1; + // Do not push unintialized data to Lua + lua_Integer light = (vm->m_flags[i] & VOXELFLAG_NO_DATA) ? 0 : vm->m_data[i].getParam1(); lua_pushinteger(L, light); lua_rawseti(L, -2, i + 1); } @@ -280,8 +286,7 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L) bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; - - u32 volume = vm->m_area.getVolume(); + const u32 volume = vm->m_area.getVolume(); if (use_buffer) lua_pushvalue(L, 2); @@ -289,7 +294,8 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L) lua_createtable(L, volume, 0); for (u32 i = 0; i != volume; i++) { - lua_Integer param2 = vm->m_data[i].param2; + // Do not push unintialized data to Lua + lua_Integer param2 = (vm->m_flags[i] & VOXELFLAG_NO_DATA) ? 0 : vm->m_data[i].getParam2(); lua_pushinteger(L, param2); lua_rawseti(L, -2, i + 1); }