diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-03-08 22:44:17 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-03-08 22:44:17 +0100 |
commit | a4d017a3f2b841788f7e47669c0d0cac9c3f31c8 (patch) | |
tree | ad14b1f27ee9dff35aaed29e67cb3a912bd5bb46 | |
parent | Some parsing optimizations (diff) | |
download | AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar.gz AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar.bz2 AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar.lz AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar.xz AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.tar.zst AltCraft-a4d017a3f2b841788f7e47669c0d0cac9c3f31c8.zip |
-rw-r--r-- | src/RendererSectionData.hpp | 1 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 36 | ||||
-rw-r--r-- | src/World.cpp | 16 |
3 files changed, 46 insertions, 7 deletions
diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index ca652f4..42e1a06 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -15,6 +15,7 @@ struct RendererSectionData { std::vector<glm::vec2> lights; size_t hash = 0; Vector sectionPos; + bool forced = false; }; RendererSectionData ParseSection(World *world, Vector sectionPosition);
\ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 3630a7a..7354c7c 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -17,17 +17,19 @@ void RendererWorld::WorkerFunction(size_t workerId) { EventListener tasksListener; tasksListener.RegisterHandler("RendererWorkerTask", [&](const Event& eventData) { - auto data = eventData.get<std::tuple<size_t, Vector>>(); + auto data = eventData.get<std::tuple<size_t, Vector, bool>>(); if (std::get<0>(data) != workerId) return; Vector vec = std::get<1>(data); + auto forced = std::get<2>(data); sectionsMutex.lock(); auto result = sections.find(vec); if (result != sections.end()) { - if (result->second.GetHash() != gs->world.GetSection(result->first).GetHash()) { + if (result->second.GetHash() != gs->world.GetSection(result->first).GetHash() || forced) { sectionsMutex.unlock(); auto data = std::make_unique<RendererSectionData>(ParseSection(&gs->world, vec)); + data->forced = true; renderDataMutex.lock(); renderData.push(std::move(data)); renderDataMutex.unlock(); @@ -43,6 +45,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { else { sectionsMutex.unlock(); auto data = std::make_unique<RendererSectionData>(ParseSection(&gs->world, vec)); + data->forced = true; renderDataMutex.lock(); renderData.push(std::move(data)); renderDataMutex.unlock(); @@ -133,10 +136,9 @@ RendererWorld::RendererWorld(GameState* ptr) { sectionsMutex.lock(); if (sections.find(data->sectionPos) != sections.end()) { - if (sections.find(data->sectionPos)->second.GetHash() == data->hash) { + if (sections.find(data->sectionPos)->second.GetHash() == data->hash && !data->forced) { LOG(INFO) << "Generated not necesarry RendererData"; sectionsMutex.unlock(); - renderData.pop(); continue; } sections.erase(sections.find(data->sectionPos)); @@ -176,11 +178,35 @@ RendererWorld::RendererWorld(GameState* ptr) { isParsing[vec] = true; isParsingMutex.unlock(); - PUSH_EVENT("RendererWorkerTask", std::make_tuple(currentWorker++, vec)); + PUSH_EVENT("RendererWorkerTask", std::make_tuple(currentWorker++, vec, false)); if (currentWorker >= numOfWorkers) currentWorker = 0; }); + listener->RegisterHandler("ChunkChangedForce", [this](const Event& eventData) { + auto vec = eventData.get<Vector>(); + Vector playerChunk(std::floor(gs->player->pos.x / 16), 0, std::floor(gs->player->pos.z / 16)); + + double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); + if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { + return; + } + + isParsingMutex.lock(); + if (isParsing.find(vec) == isParsing.end()) + isParsing[vec] = false; + if (isParsing[vec] == true) { + isParsingMutex.unlock(); + return; + } + isParsing[vec] = true; + isParsingMutex.unlock(); + + PUSH_EVENT("RendererWorkerTask", std::make_tuple(currentWorker++, vec, true)); + if (currentWorker >= numOfWorkers) + currentWorker = 0; + }); + listener->RegisterHandler("UpdateSectionsRender", [this](const Event&) { UpdateAllSections(gs->player->pos); }); diff --git a/src/World.cpp b/src/World.cpp index 59399f5..67abc02 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -350,10 +350,22 @@ void World::SetBlockId(Vector pos, BlockId block) { Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); - + Vector blockPos = pos - (sectionPos * 16); Section* section = GetSectionPtr(sectionPos); - section->SetBlockId(pos - (sectionPos * 16), block); + section->SetBlockId(blockPos, block); PUSH_EVENT("ChunkChanged",sectionPos); + if (blockPos.x == 0) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(-1, 0, 0)); + if (blockPos.x == 15) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(1, 0, 0)); + if (blockPos.y == 0) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(0, -1, 0)); + if (blockPos.y == 15) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(0, 1, 0)); + if (blockPos.z == 0) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(0, 0, -1)); + if (blockPos.z == 15) + PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(0, 0, 1)); } void World::SetBlockLight(Vector pos, unsigned char light) { |