From 405f8b3d14298449e52d216e2d57996342553290 Mon Sep 17 00:00:00 2001 From: alexei-zebra Date: Wed, 3 Jan 2024 23:57:50 +0300 Subject: [PATCH] fix --- src/graphics/BlocksRenderer.cpp | 2 +- src/logic/BlocksController.cpp | 2 +- src/logic/PlayerController.cpp | 25 +++++++++++++------------ src/logic/scripting/api_lua.cpp | 9 +++++---- src/voxels/Chunk.cpp | 4 ++-- src/voxels/Chunks.cpp | 14 +++++++------- src/voxels/Chunks.h | 2 +- src/voxels/voxel.h | 7 ++++++- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index 5cba694bb..ce5a2992c 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -348,7 +348,7 @@ void BlocksRenderer::render(const voxel* voxels) { break; } case BlockModel::aabb: { - blockAABB(ivec3(x,y,z), texfaces, &def, vox.rotation(), !def.rt.emissive); + blockAABB(ivec3(x,y,z), texfaces, &def, vox.getDir(), !def.rt.emissive); break; } default: diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index fb9e82e70..f2349c598 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -57,7 +57,7 @@ void BlocksController::updateSides(int x, int y, int z) { } void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) { - chunks->set(x,y,z, 0, 0); + chunks->set(x,y,z, voxel{0,0}); lighting->onBlockSet(x,y,z, 0); if (def->rt.funcsset.onbroken) { scripting::on_block_broken(player, def, x, y, z); diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 21d9aee2e..994972a62 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -236,27 +236,27 @@ void PlayerController::updateInteraction(){ int x = iend.x; int y = iend.y; int z = iend.z; - uint8_t states = 0; + voxel voxel; Block* def = contentIds->getBlockDef(player->chosenBlock); if (def->rotatable){ const std::string& name = def->rotations.name; if (name == "pipe") { - if (norm.x < 0.0f) states = BLOCK_DIR_WEST; - else if (norm.x > 0.0f) states = BLOCK_DIR_EAST; - else if (norm.y > 0.0f) states = BLOCK_DIR_UP; - else if (norm.y < 0.0f) states = BLOCK_DIR_DOWN; - else if (norm.z > 0.0f) states = BLOCK_DIR_NORTH; - else if (norm.z < 0.0f) states = BLOCK_DIR_SOUTH; + if (norm.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST); + else if (norm.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST); + else if (norm.y > 0.0f) voxel.setDir(BLOCK_DIR_UP); + else if (norm.y < 0.0f) voxel.setDir(BLOCK_DIR_DOWN); + else if (norm.z > 0.0f) voxel.setDir(BLOCK_DIR_NORTH); + else if (norm.z < 0.0f) voxel.setDir(BLOCK_DIR_SOUTH); } else if (name == "pane") { glm::vec3 vec = camera->dir; if (abs(vec.x) > abs(vec.z)){ - if (vec.x > 0.0f) states = BLOCK_DIR_EAST; - if (vec.x < 0.0f) states = BLOCK_DIR_WEST; + if (vec.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST); + if (vec.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST); } if (abs(vec.x) < abs(vec.z)){ - if (vec.z > 0.0f) states = BLOCK_DIR_SOUTH; - if (vec.z < 0.0f) states = BLOCK_DIR_NORTH; + if (vec.z > 0.0f) voxel.setDir(BLOCK_DIR_SOUTH); + if (vec.z < 0.0f) voxel.setDir(BLOCK_DIR_NORTH); } } } @@ -285,7 +285,8 @@ void PlayerController::updateInteraction(){ chosenBlock = 0; } if (chosenBlock != vox->id) { - chunks->set(x, y, z, chosenBlock, states); + voxel.id = chosenBlock; + chunks->set(x, y, z, voxel); lighting->onBlockSet(x,y,z, chosenBlock); if (def->rt.funcsset.onplaced) { scripting::on_block_placed(player, def, x, y, z); diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index e6fe52ce3..b5f7469b5 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -39,13 +39,14 @@ int l_block_index(lua_State* L) { } int l_set_block(lua_State* L) { + voxel voxel; int x = lua_tointeger(L, 1); int y = lua_tointeger(L, 2); int z = lua_tointeger(L, 3); - int id = lua_tointeger(L, 4); - int states = lua_tointeger(L, 5); - scripting::level->chunks->set(x, y, z, id, states); - scripting::level->lighting->onBlockSet(x,y,z, id); + voxel.id = lua_tointeger(L, 4); + voxel.states = lua_tointeger(L, 5); + scripting::level->chunks->set(x, y, z, voxel); + scripting::level->lighting->onBlockSet(x,y,z, voxel.id); return 0; } diff --git a/src/voxels/Chunk.cpp b/src/voxels/Chunk.cpp index b234eccd9..611108c6e 100644 --- a/src/voxels/Chunk.cpp +++ b/src/voxels/Chunk.cpp @@ -84,7 +84,7 @@ bool Chunk::decode(u_char8* data) { void Chunk::convert(u_char8* data, const ContentLUT* lut) { for (size_t i = 0; i < CHUNK_VOL; i++) { - blockid_t id = data[i]; - data[i] = lut->getBlockId(id); + blockid_t id = ((u_short16*)data)[i]; + ((u_short16*)data)[i] = lut->getBlockId(id); } } diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index a73339377..def75ccbe 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -77,7 +77,7 @@ const AABB* Chunks::isObstacle(float x, float y, float z){ const Block* def = contentIds->getBlockDef(v->id); if (def->obstacle) { const AABB& hitbox = def->rotatable - ? def->rt.hitboxes[v->rotation()] + ? def->rt.hitboxes[v->getDir()] : def->hitbox; if (def->rt.solid) { return &hitbox; @@ -158,7 +158,7 @@ Chunk* Chunks::getChunk(int x, int z){ return chunks[z * w + x].get(); } -void Chunks::set(int x, int y, int z, int id, uint8_t states){ +void Chunks::set(int x, int y, int z, voxel voxel) { if (y < 0 || y >= CHUNK_H) return; x -= ox * CHUNK_W; @@ -172,14 +172,14 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){ return; int lx = x - cx * CHUNK_W; int lz = z - cz * CHUNK_D; - chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = id; - chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = states; + chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = voxel.id; + chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = voxel.states; chunk->setUnsaved(true); chunk->setModified(true); if (y < chunk->bottom) chunk->bottom = y; else if (y + 1 > chunk->top) chunk->top = y + 1; - else if (id == 0) chunk->updateHeights(); + else if (voxel.id == 0) chunk->updateHeights(); if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz))) chunk->setModified(true); @@ -246,7 +246,7 @@ voxel* Chunks::rayCast(vec3 start, if (!def->rt.solid) { const AABB& box = def->rotatable - ? def->rt.hitboxes[voxel->rotation()] + ? def->rt.hitboxes[voxel->getDir()] : def->hitbox; scalar_t distance; Ray ray(start, dir); @@ -344,7 +344,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { if (def->obstacle) { if (!def->rt.solid) { const AABB& box = def->rotatable - ? def->rt.hitboxes[voxel->rotation()] + ? def->rt.hitboxes[voxel->getDir()] : def->hitbox; scalar_t distance; ivec3 norm; diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 015c02216..58299ef55 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -42,7 +42,7 @@ public: voxel* get(int x, int y, int z); light_t getLight(int x, int y, int z); u_char8 getLight(int x, int y, int z, int channel); - void set(int x, int y, int z, int id, uint8_t states); + void set(int x, int y, int z, voxel voxel); voxel* rayCast(glm::vec3 start, glm::vec3 dir, diff --git a/src/voxels/voxel.h b/src/voxels/voxel.h index 457eebd89..a2ace6a93 100644 --- a/src/voxels/voxel.h +++ b/src/voxels/voxel.h @@ -19,10 +19,15 @@ struct voxel { blockid_t id; u_short16 states; - inline u_char8 rotation() const { + inline u_char8 getDir() const { return states & BLOCK_ROT_MASK; } + inline void setDir(u_char8 dir) { + states &= ~BLOCK_ROT_MASK; + states |= dir; + } + inline u_char8 variant() const { return (states & BLOCK_VARIANT_MASK) >> 4; }