From b2735d80685c3b1b84228a0ce120056e85affb98 Mon Sep 17 00:00:00 2001 From: alexei-zebra Date: Fri, 5 Jan 2024 00:25:20 +0300 Subject: [PATCH] add signal & fix --- src/logic/scripting/api_lua.cpp | 31 ++++++++++++++++++++++++++----- src/voxels/voxel.h | 32 +++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index 41873d65f..7e8eb7d56 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -146,8 +146,8 @@ int l_set_block_custom_state(lua_State* L) { int x = lua_tointeger(L, 1), y = lua_tointeger(L, 2), z = lua_tointeger(L, 3), - i = lua_tointeger(L, 4), - s = lua_tointeger(L, 5); + i = lua_tointeger(L, 4); + bool s = lua_toboolean(L, 5); voxel* vox = scripting::level->chunks->get(x, y, z); vox->setCustomState(i, s); return 0; @@ -157,20 +157,39 @@ int l_get_block_custom_value(lua_State* L) { y = lua_tointeger(L, 2), z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); - bool state = vox->getCustomValue(); - lua_pushboolean(L, state); + int state = vox->getCustomValue(); + lua_pushinteger(L, state); return 1; } int l_set_block_custom_value(lua_State* L) { int x = lua_tointeger(L, 1), y = lua_tointeger(L, 2), z = lua_tointeger(L, 3), - v = lua_tointeger(L, 5); + v = lua_tointeger(L, 4); voxel* vox = scripting::level->chunks->get(x, y, z); vox->setCustomValue(v); return 0; } +int l_get_block_sig(lua_State* L) { + int x = lua_tointeger(L, 1), + y = lua_tointeger(L, 2), + z = lua_tointeger(L, 3); + voxel* vox = scripting::level->chunks->get(x, y, z); + bool sig = vox->getSig(); + lua_pushboolean(L, sig); + return 1; +} +int l_set_block_sig(lua_State* L) { + int x = lua_tointeger(L, 1), + y = lua_tointeger(L, 2), + z = lua_tointeger(L, 3); + bool s = lua_toboolean(L, 4); + voxel* vox = scripting::level->chunks->get(x, y, z); + vox->setSig(s); + return 0; +} + #define lua_addfunc(L, FUNC, NAME) (lua_pushcfunction(L, FUNC),\ lua_setglobal(L, NAME)) @@ -195,4 +214,6 @@ void apilua::create_funcs(lua_State* L) { lua_addfunc(L, l_set_block_custom_state, "set_block_custom_state"); lua_addfunc(L, l_get_block_custom_value, "get_block_custom_value"); lua_addfunc(L, l_set_block_custom_value, "set_block_custom_value"); + lua_addfunc(L, l_get_block_sig, "get_block_sig"); + lua_addfunc(L, l_set_block_sig, "set_block_sig"); } diff --git a/src/voxels/voxel.h b/src/voxels/voxel.h index 19d967836..c333dcaca 100644 --- a/src/voxels/voxel.h +++ b/src/voxels/voxel.h @@ -10,28 +10,39 @@ const int BLOCK_DIR_EAST = 0x3; const int BLOCK_DIR_UP = 0x4; const int BLOCK_DIR_DOWN = 0x5; -// limited to 16 block orientations -const int BLOCK_ROT_MASK = 0xF; +// limited to 8 block orientations +const int BLOCK_DIR_MASK = 0x7; + +const int BLOCK_SIG_MASK = 0x8; + // limited to 16 block variants const int BLOCK_VARIANT_MASK = 0xF0; +// +// |* * * * * * * * * * * * * * * *|* * *|*|* * * *|* * * *|*'*'*'*| +// id dir/ \variant value state +// |sig| +// + struct voxel { blockid_t id; u_short16 states; inline u_char8 getDir() const { - return states & BLOCK_ROT_MASK; + return states & BLOCK_DIR_MASK; } inline void setDir(u_char8 dir) { - states &= ~BLOCK_ROT_MASK; + states &= ~BLOCK_DIR_MASK; states |= dir; } + //todo: fix this inline u_char8 getVariant() const { return states & BLOCK_VARIANT_MASK; } + //todo: fix this inline void setVariant(u_char8 variant) { states &= ~BLOCK_VARIANT_MASK; states |= variant; @@ -51,9 +62,7 @@ struct voxel { } } - /* if the block has a custom state you - can use value in range 0 to 15 else - you can use value in range 0 to 255 */ + // can use value in range 0 to 15 inline u_char8 getCustomValue() const { return states >> 20; } @@ -65,6 +74,15 @@ struct voxel { states &= ~(0xF << 20); states |= (value << 20); } + + inline bool getSig() const { + return states & BLOCK_SIG_MASK; + } + + inline void setSig(bool sig) { + states &= ~BLOCK_SIG_MASK; + states |= sig; + } }; #endif /* VOXELS_VOXEL_H_ */