diff options
Diffstat (limited to '')
-rw-r--r-- | src/World.cpp | 108 |
1 files changed, 45 insertions, 63 deletions
diff --git a/src/World.cpp b/src/World.cpp index 9a4bfd1..da0a33b 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" @@ -15,16 +14,16 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { for (int i = 0; i < 16; i++) { if (bitmask[i]) { Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ); - Section section = ParseSection(&chunkData, chunkPosition); + auto section = std::make_shared<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,13 +57,7 @@ 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) { +bool World::isPlayerCollides(double X, double Y, double Z) const { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); if (sections.find(PlayerChunk) == sections.end() || sections.find(PlayerChunk - Vector(0, 1, 0)) == sections.end()) @@ -118,28 +111,26 @@ bool World::isPlayerCollides(double X, double Y, double Z) { return false; } -std::vector<Vector> World::GetSectionsList() { - sectionsListMutex.lock(); +std::vector<Vector> World::GetSectionsList() const { auto vec = sectionsList; - sectionsListMutex.unlock(); return vec; } static Section fallbackSection; -const Section &World::GetSection(Vector sectionPos) { +const Section &World::GetSection(Vector sectionPos) const { auto result = sections.find(sectionPos); if (result == sections.end()) { //LOG(ERROR) << "Accessed not loaded section " << sectionPos; return fallbackSection; } else { - return *result->second.get(); + return *result->second; } } // TODO: skip liquid blocks -RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { +RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) const { const float maxLen = 5.0; const float step = 0.01; glm::vec3 pos = glm::vec3(0.0); @@ -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,50 @@ 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(); +const Entity &World::GetEntity(unsigned int EntityId) const { + for (auto& it : entities) { + if (it.entityId == EntityId) { + return it; + } + } + + static Entity fallback; + return fallback; +} + +std::vector<unsigned int> World::GetEntitiesList() const { 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 +289,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 +323,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, std::shared_ptr<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,20 +336,18 @@ 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) { +BlockId World::GetBlockId(Vector pos) const { Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); - Section* section = GetSectionPtr(sectionPos); + const Section* section = GetSectionPtr(sectionPos); return !section ? BlockId{0, 0} : section->GetBlockId(pos - (sectionPos * 16)); } @@ -368,8 +356,9 @@ void World::SetBlockId(Vector pos, BlockId block) { std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); Vector blockPos = pos - (sectionPos * 16); - Section* section = GetSectionPtr(sectionPos); + auto section = std::make_shared<Section>(*GetSectionPtr(sectionPos)); section->SetBlockId(blockPos, block); + sections[sectionPos] = section; PUSH_EVENT("ChunkChanged",sectionPos); if (blockPos.x == 0) PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(-1, 0, 0)); @@ -393,42 +382,38 @@ void World::SetBlockSkyLight(Vector pos, unsigned char light) { } -Section *World::GetSectionPtr(Vector position) { +const Section *World::GetSectionPtr(Vector position) const { auto it = sections.find(position); if (it == sections.end()) return nullptr; - return it->second.get(); + return it->second.get(); } Entity* World::GetEntityPtr(unsigned int EntityId) { - entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == EntityId) { - entitiesMutex.unlock(); return ⁢ } } - entitiesMutex.unlock(); return nullptr; } -unsigned char World::GetBlockLight(Vector pos) -{ +unsigned char World::GetBlockLight(Vector pos) const { 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* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); - Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); - Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); - Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); - Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); - Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); + const Section* section = GetSectionPtr(sectionPos); + const Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); + const Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); + const Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); + const Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); + const Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); + const Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); if (!section) return 0; @@ -436,8 +421,7 @@ unsigned char World::GetBlockLight(Vector pos) return GetBlockLight(blockPos, section, xp, xn, yp, yn, zp, zn); } -unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) -{ +unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const { static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), @@ -475,21 +459,20 @@ unsigned char World::GetBlockLight(const Vector &blockPos, const Section *sectio return value; } -unsigned char World::GetBlockSkyLight(Vector pos) -{ +unsigned char World::GetBlockSkyLight(Vector pos) const { 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* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); - Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); - Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); - Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); - Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); - Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); + const Section* section = GetSectionPtr(sectionPos); + const Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); + const Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); + const Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); + const Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); + const Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); + const Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); if (!section) return 0; @@ -497,8 +480,7 @@ unsigned char World::GetBlockSkyLight(Vector pos) return GetBlockSkyLight(blockPos, section, xp, xn, yp, yn, zp, zn); } -unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) -{ +unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const { static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), |