diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2019-01-27 04:10:36 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2019-01-27 04:10:36 +0100 |
commit | c3472b8abd8185f392e6c2afb68a7411232396d6 (patch) | |
tree | 7b3544cdb5eccfcce2a1a7c8efaa5c09ae7455df /src | |
parent | Refactored GameState (diff) | |
download | AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar.gz AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar.bz2 AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar.lz AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar.xz AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.tar.zst AltCraft-c3472b8abd8185f392e6c2afb68a7411232396d6.zip |
Diffstat (limited to '')
-rw-r--r-- | src/GameState.cpp | 17 | ||||
-rw-r--r-- | src/GameState.hpp | 12 | ||||
-rw-r--r-- | src/Render.cpp | 3 | ||||
-rw-r--r-- | src/RendererEntity.cpp | 8 | ||||
-rw-r--r-- | src/RendererEntity.hpp | 4 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 22 | ||||
-rw-r--r-- | src/World.cpp | 36 | ||||
-rw-r--r-- | src/World.hpp | 11 |
8 files changed, 55 insertions, 58 deletions
diff --git a/src/GameState.cpp b/src/GameState.cpp index ace2488..df227b3 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -10,6 +10,8 @@ void GameState::Update(float deltaTime) { if (!gameStatus.isGameStarted) return; + std::lock_guard<std::mutex> guard(accessMutex); + std::chrono::steady_clock clock; static auto timeOfPreviousSendedPacket(clock.now()); auto delta = clock.now() - timeOfPreviousSendedPacket; @@ -67,6 +69,7 @@ void GameState::Update(float deltaTime) { } void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) { + std::lock_guard<std::mutex> guard(accessMutex); switch ((PacketNamePlayCB)ptr->GetPacketId()) { case SpawnObject: { auto packet = std::static_pointer_cast<PacketSpawnObject>(ptr); @@ -487,6 +490,8 @@ void GameState::HandleMovement(GameState::MoveType direction, float deltaTime) { if (!gameStatus.isGameStarted) return; + std::lock_guard<std::mutex> guard(accessMutex); + const double playerSpeed = 43; float velocity = playerSpeed * deltaTime; @@ -543,6 +548,8 @@ void GameState::HandleRotation(double yaw, double pitch) { if (!gameStatus.isGameStarted) return; + std::lock_guard<std::mutex> guard(accessMutex); + double playerYaw = Entity::DecodeYaw(player->yaw); double playerPitch = Entity::DecodePitch(player->pitch); playerYaw += yaw; @@ -556,6 +563,8 @@ void GameState::HandleRotation(double yaw, double pitch) { } glm::mat4 GameState::GetViewMatrix() { + std::lock_guard<std::mutex> guard(accessMutex); + double playerYaw = Entity::DecodeYaw(player->yaw); double playerPitch = Entity::DecodePitch(player->pitch); glm::vec3 front, right, worldUp, up; @@ -580,6 +589,8 @@ void GameState::StartDigging() { if (!selectionStatus.isBlockSelected) return; + std::lock_guard<std::mutex> guard(accessMutex); + auto packetStart = std::make_shared<PacketPlayerDigging>(0, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast<Packet>(packetStart); PUSH_EVENT("SendPacket", packet); @@ -588,6 +599,8 @@ void GameState::StartDigging() { } void GameState::FinishDigging() { + std::lock_guard<std::mutex> guard(accessMutex); + auto packetFinish = std::make_shared<PacketPlayerDigging>(2, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast<Packet>(packetFinish); PUSH_EVENT("SendPacket", packet); @@ -599,6 +612,8 @@ void GameState::FinishDigging() { // send_packet(packet_type=start_digging_packet) // remove_delayed_action(finish_digging) void GameState::CancelDigging() { + std::lock_guard<std::mutex> guard(accessMutex); + auto packetCancel = std::make_shared<PacketPlayerDigging>(1, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast<Packet>(packetCancel); PUSH_EVENT("SendPacket", packet); @@ -637,6 +652,8 @@ void GameState::PlaceBlock() { if (!selectionStatus.isBlockSelected) return; + std::lock_guard<std::mutex> guard(accessMutex); + BlockFacing face = detectHitFace(selectionStatus.raycastHit, selectionStatus.selectedBlock); auto packetPlace = std::make_shared<PacketPlayerBlockPlacement>( selectionStatus.selectedBlock, (unsigned char)face, 0, 0, 0, 0); diff --git a/src/GameState.hpp b/src/GameState.hpp index cd39a48..dbea2c3 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -3,6 +3,7 @@ #include <memory> #include <string> #include <vector> +#include <mutex> #include <glm/mat4x4.hpp> @@ -67,6 +68,8 @@ class GameState { Window playerInventory; std::vector<Window> openedWindows; + + std::mutex accessMutex; public: void Update(float deltaTime); @@ -92,30 +95,37 @@ public: glm::mat4 GetViewMatrix(); inline Entity *GetPlayer() { + std::lock_guard<std::mutex> guard(accessMutex); return player; } - inline World &GetWorld() { + inline World GetWorld() { + std::lock_guard<std::mutex> guard(accessMutex); return world; } inline TimeStatus GetTimeStatus() { + std::lock_guard<std::mutex> guard(accessMutex); return timeStatus; } inline GameStatus GetGameStatus() { + std::lock_guard<std::mutex> guard(accessMutex); return gameStatus; } inline PlayerStatus GetPlayerStatus() { + std::lock_guard<std::mutex> guard(accessMutex); return playerStatus; } inline SelectionStatus GetSelectionStatus() { + std::lock_guard<std::mutex> guard(accessMutex); return selectionStatus; } inline Window &GetInventory() { + std::lock_guard<std::mutex> guard(accessMutex); return playerInventory; } }; diff --git a/src/Render.cpp b/src/Render.cpp index b5dea63..5432979 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -384,7 +384,8 @@ void Render::RenderGui() { if (world) { Entity *playerPtr = world->GameStatePtr()->GetPlayer(); SelectionStatus selectionStatus = world->GameStatePtr()->GetSelectionStatus(); - World *worldPtr = &world->GameStatePtr()->GetWorld(); + World worldObj = world->GameStatePtr()->GetWorld(); + World *worldPtr = &worldObj; ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index a1c9566..fef7dbd 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -4,9 +4,10 @@ #include <glm/gtc/type_ptr.hpp> #include "Entity.hpp" -#include "World.hpp" +#include "GameState.hpp" #include "Renderer.hpp" #include "AssetManager.hpp" +#include "GlobalState.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, @@ -114,9 +115,8 @@ GLuint RendererEntity::GetVao(){ return Vao; } -RendererEntity::RendererEntity(World *ptr, unsigned int id) +RendererEntity::RendererEntity(unsigned int id) { - world = ptr; entityId = id; } @@ -125,7 +125,7 @@ RendererEntity::~RendererEntity() { void RendererEntity::Render(RenderState & renderState) { glm::mat4 model = glm::mat4(1.0); - Entity& entity = world->GetEntity(entityId); + Entity entity = GlobalState::GetGameState()->GetWorld().GetEntity(entityId); model = glm::translate(model, entity.pos.glm()); model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0)); model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index e2e8bf1..961ef45 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -2,14 +2,12 @@ #include <GL/glew.h> -class World; class RenderState; class RendererEntity { unsigned int entityId; - World *world; public: - RendererEntity(World *ptr, unsigned int id); + RendererEntity(unsigned int id); ~RendererEntity(); void Render(RenderState& renderState); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 78519f3..d798343 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -36,6 +36,8 @@ void RendererWorld::WorkerFunction(size_t workerId) { } void RendererWorld::ParseQueueUpdate() { + World world = gs->GetWorld(); + while (!parseQueue.empty()) { size_t id = 0; for (; id < RendererWorld::parsingBufferSize && parsing[id].parsing; ++id) {} @@ -52,13 +54,13 @@ void RendererWorld::ParseQueueUpdate() { vec.y -= 4500; } - parsing[id].data.section = gs->GetWorld().GetSection(vec); - parsing[id].data.north = gs->GetWorld().GetSection(vec + Vector(0, 0, 1)); - parsing[id].data.south = gs->GetWorld().GetSection(vec + Vector(0, 0, -1)); - parsing[id].data.west = gs->GetWorld().GetSection(vec + Vector(1, 0, 0)); - parsing[id].data.east = gs->GetWorld().GetSection(vec + Vector(-1, 0, 0)); - parsing[id].data.bottom = gs->GetWorld().GetSection(vec + Vector(0, -1, 0)); - parsing[id].data.top = gs->GetWorld().GetSection(vec + Vector(0, 1, 0)); + parsing[id].data.section = world.GetSection(vec); + parsing[id].data.north = world.GetSection(vec + Vector(0, 0, 1)); + parsing[id].data.south = world.GetSection(vec + Vector(0, 0, -1)); + parsing[id].data.west = world.GetSection(vec + Vector(1, 0, 0)); + parsing[id].data.east = world.GetSection(vec + Vector(-1, 0, 0)); + parsing[id].data.bottom = world.GetSection(vec + Vector(0, -1, 0)); + parsing[id].data.top = world.GetSection(vec + Vector(0, 1, 0)); parsing[id].parsing = true; @@ -74,6 +76,8 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { elements.clear(); elements.reserve(size); + World world = gs->GetWorld(); + for (size_t i = 0; i < size; i++) { Vector vec = parseQueue.front(); parseQueue.pop(); @@ -86,7 +90,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { if (std::find(elements.begin(), elements.end(), vec) != elements.end()) continue; - const Section& section = gs->GetWorld().GetSection(vec); + const Section& section = world.GetSection(vec); bool skip = false; @@ -186,7 +190,7 @@ RendererWorld::RendererWorld(GameState* ptr) { auto data = eventData.get<unsigned int>(); for (unsigned int entityId : gs->GetWorld().GetEntitiesList()) { if (entityId == data) { - entities.push_back(RendererEntity(&gs->GetWorld(), entityId)); + entities.push_back(RendererEntity(entityId)); } } }); diff --git a/src/World.cpp b/src/World.cpp index 9a4bfd1..ffb657f 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -3,7 +3,6 @@ #include <bitset> #include <glm/glm.hpp> -#include "Section.hpp" #include "Event.hpp" #include "DebugInfo.hpp" #include "Packet.hpp" @@ -18,13 +17,13 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { Section section = ParseSection(&chunkData, chunkPosition); if (packet->GroundUpContinuous) { - if (!sections.insert(std::make_pair(chunkPosition, std::make_unique<Section>(section))).second) { + if (!sections.insert(std::make_pair(chunkPosition, section)).second) { LOG(ERROR) << "New chunk not created " << chunkPosition << " potential memory leak"; } UpdateSectionsList(); } else { - std::swap(*sections.at(chunkPosition).get(), section); + std::swap(sections.at(chunkPosition), section); } PUSH_EVENT("ChunkChanged", chunkPosition); @@ -58,12 +57,6 @@ Section World::ParseSection(StreamInput *data, Vector position) { std::move(blockLight), std::move(skyLight)); } -World::~World() { -} - -World::World() { -} - bool World::isPlayerCollides(double X, double Y, double Z) { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); if (sections.find(PlayerChunk) == sections.end() || @@ -119,9 +112,7 @@ bool World::isPlayerCollides(double X, double Y, double Z) { } std::vector<Vector> World::GetSectionsList() { - sectionsListMutex.lock(); auto vec = sectionsList; - sectionsListMutex.unlock(); return vec; } @@ -134,7 +125,7 @@ const Section &World::GetSection(Vector sectionPos) { return fallbackSection; } else { - return *result->second.get(); + return result->second; } } @@ -199,7 +190,6 @@ void World::UpdatePhysics(float delta) { return { false }; }; - entitiesMutex.lock(); for (auto& it : entities) { if (it.isFlying) { VectorF newPos = it.pos + VectorF(it.vel.x, it.vel.y, it.vel.z) * delta; @@ -247,49 +237,39 @@ void World::UpdatePhysics(float delta) { it.vel = it.vel + resistForce; } } - entitiesMutex.unlock(); DebugInfo::totalSections = sections.size(); } Entity& World::GetEntity(unsigned int EntityId){ - entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == EntityId) { - entitiesMutex.unlock(); return it; } } - entitiesMutex.unlock(); static Entity fallback; return fallback; } std::vector<unsigned int> World::GetEntitiesList() { - entitiesMutex.lock(); std::vector<unsigned int> ret; for (auto& it : entities) { ret.push_back(it.entityId); } - entitiesMutex.unlock(); return ret; } void World::AddEntity(Entity entity) { - entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == entity.entityId) { LOG(ERROR) << "Adding already existing entity: " << entity.entityId; - entitiesMutex.unlock(); return; } } entities.push_back(entity); - entitiesMutex.unlock(); } void World::DeleteEntity(unsigned int EntityId) { - entitiesMutex.lock(); auto it = entities.begin(); for (; it != entities.end(); ++it) { if (it->entityId == EntityId) { @@ -298,7 +278,6 @@ void World::DeleteEntity(unsigned int EntityId) { } if (it != entities.end()) entities.erase(it); - entitiesMutex.unlock(); } void World::ParseChunkData(std::shared_ptr<PacketBlockChange> packet) { @@ -333,7 +312,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { } void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { - std::vector<std::map<Vector, std::unique_ptr<Section>>::iterator> toRemove; + std::vector<std::map<Vector, Section>::iterator> toRemove; for (auto it = sections.begin(); it != sections.end(); ++it) { if (it->first.x == packet->ChunkX && it->first.z == packet->ChunkZ) toRemove.push_back(it); @@ -346,12 +325,10 @@ void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { } void World::UpdateSectionsList() { - sectionsListMutex.lock(); sectionsList.clear(); for (auto& it : sections) { sectionsList.push_back(it.first); } - sectionsListMutex.unlock(); } BlockId World::GetBlockId(Vector pos) { @@ -399,18 +376,15 @@ Section *World::GetSectionPtr(Vector position) { if (it == sections.end()) return nullptr; - return it->second.get(); + return &it->second; } Entity* World::GetEntityPtr(unsigned int EntityId) { - entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == EntityId) { - entitiesMutex.unlock(); return ⁢ } } - entitiesMutex.unlock(); return nullptr; } diff --git a/src/World.hpp b/src/World.hpp index 165e73d..94e17f6 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -11,8 +11,8 @@ #include "Entity.hpp" #include "Block.hpp" #include "Vector.hpp" +#include "Section.hpp" -class Section; class PacketChunkData; class PacketBlockChange; class PacketMultiBlockChange; @@ -28,24 +28,17 @@ struct RaycastResult { class World { int dimension = 0; - std::map<Vector, std::unique_ptr<Section>> sections; + std::map<Vector, Section> sections; Section ParseSection(StreamInput *data, Vector position); std::list<Entity> entities; - std::mutex entitiesMutex; - std::vector<Vector> sectionsList; - std::mutex sectionsListMutex; - void UpdateSectionsList(); public: - World(); - - ~World(); void ParseChunkData(std::shared_ptr<PacketChunkData> packet); |