This commit is contained in:
alexei-zebra 2024-01-05 03:37:44 +03:00
parent b2735d8068
commit eb94127cb6
6 changed files with 201 additions and 73 deletions

View file

@ -71,7 +71,7 @@ HudRenderer::HudRenderer(Engine* engine,
uicamera->perspective = false;
uicamera->flipped = true;
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
Panel* panel = new Panel(vec2(350, 300), vec4(5.0f), 1.0f);
debugPanel = shared_ptr<UINode>(panel);
panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
@ -99,12 +99,18 @@ HudRenderer::HudRenderer(Engine* engine,
auto indices = this->level->content->indices;
auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states;
if (def) {
if (def)
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
}
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
})));
panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player;
auto indices = this->level->content->indices;
auto str_ = L"dir: "+std::to_wstring(player->selectedVoxel.getDir()) + L" signal: "+std::to_wstring(player->selectedVoxel.getSig());
return str_;
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"seed: "+std::to_wstring(this->level->world->seed);

View file

@ -231,7 +231,7 @@ void BlocksRenderer::blockCube(int x, int y, int z,
vec3 coord(x, y, z);
if (block->rotatable) {
auto& rotations = block->rotations;
auto& orient = rotations.variants[states & BLOCK_ROT_MASK];
auto& orient = rotations.variants[states & BLOCK_DIR_MASK];
X = orient.axisX;
Y = orient.axisY;
Z = orient.axisZ;

View file

@ -75,7 +75,11 @@ void BlocksController::updateBlock(int x, int y, int z) {
return;
}
if (def->rt.funcsset.update) {
int states = vox->states;
int id = vox->id;
scripting::update_block(def, x, y, z);
if (states != vox->states || id!= vox->id)
updateSides(x, y, z);
}
}

View file

@ -300,6 +300,7 @@ void PlayerController::updateInteraction(){
player->chosenBlock = chunks->get(x,y,z)->id;
}
} else {
player->selectedVoxel = voxel{0, 0};
selectedBlockId = -1;
selectedBlockStates = 0;
}

View file

@ -113,7 +113,6 @@ int l_is_replaceable_at(lua_State* L) {
}
int l_get_block_dir(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
@ -132,44 +131,6 @@ int l_set_block_dir(lua_State* L) {
vox->setDir(s);
return 0;
}
int l_get_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);
voxel* vox = scripting::level->chunks->get(x, y, z);
bool state = vox->getCustomState(i);
lua_pushboolean(L, state);
return 1;
}
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);
bool s = lua_toboolean(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomState(i, s);
return 0;
}
int l_get_block_custom_value(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);
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, 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),
@ -190,6 +151,131 @@ int l_set_block_sig(lua_State* L) {
return 0;
}
int l_get_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);
voxel* vox = scripting::level->chunks->get(x, y, z);
bool state = vox->getCustomState(i);
lua_pushboolean(L, state);
return 1;
}
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);
bool s = lua_toboolean(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomState(i, s);
return 0;
}
int l_get_block_custom_value(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);
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, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomValue(v);
return 0;
}
// for specific modes
int l_get_block_custom_bits(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
int offset = lua_tointeger(L, 4);
int bits = lua_tointeger(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr)
lua_pushinteger(L, 0);
else
lua_pushinteger(L, vox->getCustomBits(offset, bits));
return 1;
}
int l_set_block_custom_bits(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
int offset = lua_tointeger(L, 4);
int bits = lua_tointeger(L, 5);
int value = lua_tointeger(L, 6);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox != nullptr)
vox->setCustomBits(offset, bits, value);
return 0;
}
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
lua_pushinteger(L, x);
lua_pushinteger(L, y);
lua_pushinteger(L, z);
return 3;
}
int l_get_block_x(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua_pushivec3(L, 1, 0, 0);
}
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
if (!def->rotatable) {
return lua_pushivec3(L, 1, 0, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
return lua_pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
}
}
int l_get_block_y(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua_pushivec3(L, 0, 1, 0);
}
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
if (!def->rotatable) {
return lua_pushivec3(L, 0, 1, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
return lua_pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
}
}
int l_get_block_z(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua_pushivec3(L, 0, 0, 1);
}
const Block* def = scripting::level->content->indices->getBlockDef(vox->id);
if (!def->rotatable) {
return lua_pushivec3(L, 0, 0, 1);
} else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()];
return lua_pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
}
}
#define lua_addfunc(L, FUNC, NAME) (lua_pushcfunction(L, FUNC),\
lua_setglobal(L, NAME))
@ -206,6 +292,7 @@ void apilua::create_funcs(lua_State* L) {
lua_addfunc(L, l_set_player_pos, "set_player_pos");
lua_addfunc(L, l_get_player_rot, "get_player_rot");
lua_addfunc(L, l_set_player_rot, "set_player_rot");
lua_addfunc(L, l_get_block_states, "get_block_states"); // old function
lua_addfunc(L, l_get_block_dir, "get_block_dir");
@ -216,4 +303,7 @@ void apilua::create_funcs(lua_State* L) {
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");
lua_addfunc(L, l_get_block_x, "get_block_X");
lua_addfunc(L, l_get_block_y, "get_block_Y");
lua_addfunc(L, l_get_block_z, "get_block_Z");
}

View file

@ -3,51 +3,78 @@
#include "../typedefs.h"
const int BLOCK_DIR_NORTH = 0x0;
const int BLOCK_DIR_WEST = 0x1;
const int BLOCK_DIR_SOUTH = 0x2;
const int BLOCK_DIR_EAST = 0x3;
const int BLOCK_DIR_UP = 0x4;
const int BLOCK_DIR_DOWN = 0x5;
const u_int32 BLOCK_DIR_NORTH = 0x0;
const u_int32 BLOCK_DIR_WEST = 0x1;
const u_int32 BLOCK_DIR_SOUTH = 0x2;
const u_int32 BLOCK_DIR_EAST = 0x3;
const u_int32 BLOCK_DIR_UP = 0x4;
const u_int32 BLOCK_DIR_DOWN = 0x5;
// limited to 8 block orientations
const int BLOCK_DIR_MASK = 0x7;
const int BLOCK_SIG_MASK = 0x8;
const u_int32 BLOCK_DIR_MASK = 0b111;
const u_int32 BLOCK_DIR_OFFSET = 0x0;
// limited to 16 block variants
const int BLOCK_VARIANT_MASK = 0xF0;
const u_int32 BLOCK_SIG_MASK = 0b1;
const u_int32 BLOCK_SIG_OFFSET = 0x3;
//
// |* * * * * * * * * * * * * * * *|* * *|*|* * * *|* * * *|*'*'*'*|
// id dir/ \variant value state
// |sig|
//
const u_int32 BLOCK_VARIANT_MASK = 0b1111;
const u_int32 BLOCK_VARIANT_OFFSET = 0x4;
const u_int32 BLOCK_BITS_MASK = 0b11111111;
const u_int32 BLOCK_BITS_OFFSET = 0x8;
const u_int32 BLOCK_VALUE_MASK = 0b1111;
const u_int32 BLOCK_VALUE_OFFSET = 0x8;
const u_int32 BLOCK_STATE_MASK = 0b1111;
const u_int32 BLOCK_STATE_OFFSET = 0xC;
//*-----------------------------------------------------------------------|
//* 16 19 20 24 28 32 |
//* | | | / | | | |
//* |* * * * * * * * * * * * * * * *|* * *|*|* * * *|* * * *|*'*'*'*| |
//* id dir / \ variant value 4states |
//* |sig| |
//*-----------------------------------------------------------------------|
struct voxel {
blockid_t id;
u_short16 states;
inline u_int32 getId() const {
return id;
}
inline u_char8 getDir() const {
return states & BLOCK_DIR_MASK;
return (states >> BLOCK_DIR_OFFSET) & BLOCK_DIR_MASK;
}
inline void setDir(u_char8 dir) {
states &= ~BLOCK_DIR_MASK;
states |= dir;
states &= ~BLOCK_DIR_MASK << BLOCK_DIR_OFFSET;
states |= dir << BLOCK_DIR_OFFSET;
}
//todo: fix this
inline u_char8 getVariant() const {
return states & BLOCK_VARIANT_MASK;
return (states >> BLOCK_VALUE_OFFSET) & BLOCK_VARIANT_MASK;
}
//todo: fix this
inline void setVariant(u_char8 variant) {
states &= ~BLOCK_VARIANT_MASK;
states |= variant;
states &= ~BLOCK_VARIANT_MASK << BLOCK_VARIANT_OFFSET;
states |= variant << BLOCK_VARIANT_OFFSET;
}
inline u_char8 getCustomBits(u_char8 offset, u_char8 len) const {
return (states >> (BLOCK_BITS_OFFSET + offset)) & ((1 << len) - 1);
}
inline void setCustomBits(u_char8 offset, u_char8 len, u_char8 value) {
int len_musk = ((1 << len) - 1);
offset += BLOCK_BITS_OFFSET;
value &= len_musk;
states &= ~(len_musk << offset);
states |= value << offset;
}
// id of the state in range 0 to 3
inline bool getCustomState(u_char8 id) const {
return states & (1 << id+24);
@ -76,12 +103,12 @@ struct voxel {
}
inline bool getSig() const {
return states & BLOCK_SIG_MASK;
return (states >> BLOCK_SIG_OFFSET) & BLOCK_SIG_MASK;
}
inline void setSig(bool sig) {
states &= ~BLOCK_SIG_MASK;
states |= sig;
states &= ~(BLOCK_SIG_MASK << BLOCK_SIG_OFFSET);
states |= sig << BLOCK_SIG_OFFSET;
}
};