From dc0c497ec1aea32e9cef242a143c63a4d7964b2d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 30 Nov 2023 19:11:58 +0300 Subject: [PATCH] fixes + Block.replaceable --- src/definitions.cpp | 4 ++++ src/logic/PlayerController.cpp | 9 +++++--- src/voxels/Block.h | 1 + src/voxels/Chunks.cpp | 38 +++++++++++++++++++++++++++++----- src/world/Level.cpp | 8 ++----- src/world/Level.h | 1 - 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/definitions.cpp b/src/definitions.cpp index fbdac15d..354c293e 100644 --- a/src/definitions.cpp +++ b/src/definitions.cpp @@ -13,6 +13,7 @@ using glm::vec3; // All in-game definitions (blocks, items, etc..) void setup_definitions(ContentBuilder* builder) { Block* block = new Block("core:air", "air"); + block->replaceable = true; block->drawGroup = 1; block->lightPassing = true; block->skyLightPassing = true; @@ -61,6 +62,7 @@ void setup_definitions(ContentBuilder* builder) { block->skyLightPassing = false; block->obstacle = false; block->selectable = false; + block->replaceable = true; builder->add(block); block = new Block("base:sand", "sand"); @@ -74,6 +76,7 @@ void setup_definitions(ContentBuilder* builder) { block->drawGroup = 5; block->lightPassing = true; block->obstacle = false; + block->replaceable = true; block->model = BlockModel::xsprite; block->hitbox.scale(vec3(0.7f), vec3(0.5f, 0.0f, 0.5f)); builder->add(block); @@ -82,6 +85,7 @@ void setup_definitions(ContentBuilder* builder) { block->drawGroup = 5; block->lightPassing = true; block->obstacle = false; + block->replaceable = true; block->model = BlockModel::xsprite; block->hitbox.scale(vec3(0.7f)); builder->add(block); diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index ae2ecf1a..726a8fe2 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -238,9 +238,12 @@ void PlayerController::updateInteraction(){ y = (int)(iend.y)+(int)(norm.y); z = (int)(iend.z)+(int)(norm.z); } - if (!level->physics->isBlockInside(x,y,z, player->hitbox)){ - chunks->set(x, y, z, player->choosenBlock, states); - lighting->onBlockSet(x,y,z, player->choosenBlock); + vox = chunks->get(x, y, z); + if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) { + if (!level->physics->isBlockInside(x,y,z, player->hitbox)){ + chunks->set(x, y, z, player->choosenBlock, states); + lighting->onBlockSet(x,y,z, player->choosenBlock); + } } } if (Events::jactive(BIND_PLAYER_PICK)){ diff --git a/src/voxels/Block.h b/src/voxels/Block.h index e77ddeef..fa5d4299 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -34,6 +34,7 @@ public: bool skyLightPassing = false; bool obstacle = true; bool selectable = true; + bool replaceable = false; bool breakable = true; bool rotatable = false; AABB hitbox; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index d7cb67d6..c98781c6 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -221,14 +221,21 @@ voxel* Chunks::rayCast(vec3 start, // TODO: replace this dumb solution with something better if (def && !def->rt.solid) { + const int gridSize = BLOCK_AABB_GRID * 2; const AABB& box = def->hitbox; - const int subs = BLOCK_AABB_GRID; + const int subs = gridSize; iend = vec3(ix, iy, iz); end -= iend; - for (int i = 0; i < subs; i++) { - end.x += dx / float(subs); - end.y += dy / float(subs); - end.z += dz / float(subs); + int six = end.x * gridSize; + int siy = end.y * gridSize; + int siz = end.z * gridSize; + float stxMax = (txDelta < infinity) ? txDelta * xdist : infinity; + float styMax = (tyDelta < infinity) ? tyDelta * ydist : infinity; + float stzMax = (tzDelta < infinity) ? tzDelta * zdist : infinity; + for (int i = 0; i < subs*2; i++) { + end.x = six / float(gridSize); + end.y = siy / float(gridSize); + end.z = siz / float(gridSize); if (box.inside(end)) { end += iend; norm.x = norm.y = norm.z = 0.0f; @@ -237,6 +244,27 @@ voxel* Chunks::rayCast(vec3 start, if (steppedIndex == 2) norm.z = -stepz; return voxel; } + if (stxMax < styMax) { + if (stxMax < stzMax) { + six += stepx; + stxMax += txDelta; + steppedIndex = 0; + } else { + siz += stepz; + stzMax += tzDelta; + steppedIndex = 2; + } + } else { + if (styMax < stzMax) { + siy += stepy; + styMax += tyDelta; + steppedIndex = 1; + } else { + siz += stepz; + stzMax += tzDelta; + steppedIndex = 2; + } + } } } else { iend.x = ix; diff --git a/src/world/Level.cpp b/src/world/Level.cpp index e1c4bddb..851f3b9a 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -13,7 +13,6 @@ Level::Level(World* world, const Content* content, Player* player, EngineSettings& settings) : world(world), content(content), - contentIds(content->indices), player(player), chunksStorage(new ChunksStorage(this)), events(new LevelEvents()) , @@ -22,11 +21,8 @@ Level::Level(World* world, const Content* content, Player* player, EngineSetting uint matrixSize = (settings.chunks.loadDistance+ settings.chunks.padding) * 2; - chunks = new Chunks(matrixSize, matrixSize, - 0, 0, - world->wfile, - events, - content); + chunks = new Chunks(matrixSize, matrixSize, 0, 0, + world->wfile, events, content); lighting = new Lighting(content, chunks); events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) { diff --git a/src/world/Level.h b/src/world/Level.h index 410a8ae2..b7cf33ce 100644 --- a/src/world/Level.h +++ b/src/world/Level.h @@ -17,7 +17,6 @@ class ChunksStorage; class PlayerController; class Level { - const ContentIndices* const contentIds; public: World* world; const Content* const content;