From 9e56348975dbdaf1cce9dd688129208a61fd4eb5 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 21 Jul 2017 19:49:56 +0500 Subject: 2017-07-21 --- src/world/GameState.cpp | 20 +++++-------- src/world/Section.cpp | 80 ++++++++++++++++++++++++++----------------------- src/world/World.cpp | 47 ++++++++++++++++++++++++----- 3 files changed, 90 insertions(+), 57 deletions(-) (limited to 'src/world') diff --git a/src/world/GameState.cpp b/src/world/GameState.cpp index b484b06..79e2f1b 100644 --- a/src/world/GameState.cpp +++ b/src/world/GameState.cpp @@ -325,14 +325,6 @@ void GameState::HandleMovement(GameState::Direction direction, float deltaTime) g_PlayerVelocityX = vel.x; g_PlayerVelocityY = vel.y; g_PlayerVelocityZ = vel.z; - - /*bool isCollides = world.isPlayerCollides(g_PlayerX, g_PlayerY, g_PlayerZ); - if (isCollides) { - SetPosition(previousPos); - return; - } - auto updatePacket = std::make_shared(g_PlayerX, g_PlayerY, g_PlayerZ, true); - nc->SendPacket(updatePacket);*/ } void GameState::HandleRotation(double yaw, double pitch) { @@ -349,7 +341,9 @@ void GameState::HandleRotation(double yaw, double pitch) { } glm::mat4 GameState::GetViewMatrix() { - return glm::lookAt(this->Position(), this->Position() + this->Front, this->Up); + auto pos = this->Position(); + pos.y+=1.62; + return glm::lookAt(pos, pos + this->Front, this->Up); } void GameState::updateCameraVectors() { @@ -379,11 +373,11 @@ void GameState::SetPitch(float pitch) { } glm::vec3 GameState::Position() { - return glm::vec3(g_PlayerX - 0.5, g_PlayerY + 1.12, g_PlayerZ - 0.5); + return glm::vec3(g_PlayerX, g_PlayerY, g_PlayerZ); } void GameState::SetPosition(glm::vec3 Position) { - g_PlayerX = Position.x + 0.5; - g_PlayerY = Position.y - 1.12; - g_PlayerZ = Position.z + 0.5; + g_PlayerX = Position.x; + g_PlayerY = Position.y; + g_PlayerZ = Position.z; } diff --git a/src/world/Section.cpp b/src/world/Section.cpp index a338e49..279d2b2 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -33,20 +33,13 @@ Section::~Section() { } Block &Section::GetBlock(Vector pos) { - if (m_dataBlocks != nullptr) { - std::mutex parseMutex; - std::unique_lock parseLocker(parseMutex); - parseWaiter.wait(parseLocker); - while (m_dataBlocks != nullptr) { - parseWaiter.wait(parseLocker); - } - LOG(WARNING) << "Successfully waited for block render!"; - } return m_blocks[pos.GetY() * 256 + pos.GetZ() * 16 + pos.GetX()]; } +double totalParsingTime = 0; + void Section::Parse() { - if (m_dataBlocks == nullptr) + if (!m_blocks.empty()) return; long long *longArray = reinterpret_cast(m_dataBlocks); @@ -54,23 +47,28 @@ void Section::Parse() { endswap(&longArray[i]); std::vector blocks; blocks.reserve(4096); - int bitPos = 0; - unsigned short t = 0; - for (size_t i = 0; i < m_dataBlocksLen; i++) { - for (int j = 0; j < 8; j++) { - t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00; - t >>= 1; - m_dataBlocks[i] >>= 1; - bitPos++; - if (bitPos >= m_bitsPerBlock) { - bitPos = 0; - t >>= m_bitsPerBlock - 1; - blocks.push_back(t); - t = 0; - } - } - } - + { + auto begin = std::chrono::steady_clock::now(); + int bitPos = 0; + unsigned short t = 0; + for (size_t i = 0; i < m_dataBlocksLen; i++) { + for (int j = 0; j < 8; j++) { + t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00; + t >>= 1; + m_dataBlocks[i] >>= 1; + bitPos++; + if (bitPos >= m_bitsPerBlock) { + bitPos = 0; + t >>= m_bitsPerBlock - 1; + blocks.push_back(t); + t = 0; + } + } + } + auto end = std::chrono::steady_clock::now(); + std::chrono::duration time = end - begin; + totalParsingTime += time.count(); + } std::vector light; light.reserve(4096); for (int i = 0; i < 2048; i++) { @@ -85,9 +83,6 @@ void Section::Parse() { Block block(blockId >> 4, blockId & 0xF); m_blocks.push_back(block); } - if ((light.size() + blocks.size()) / 2 != 4096) { - throw 118; - } delete[] m_dataBlocks; m_dataBlocksLen = 0; m_dataBlocks = nullptr; @@ -118,15 +113,19 @@ void swap(Section &a, Section &b) { Section::Section(const Section &other) { worldPosition = other.worldPosition; m_dataBlocksLen = other.m_dataBlocksLen; - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks); + if (other.m_blocks.empty()) { + m_dataBlocks = new byte[m_dataBlocksLen]; + std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks); - m_dataLight = new byte[2048]; - std::copy(other.m_dataLight, other.m_dataLight + 2048, m_dataLight); + m_dataLight = new byte[2048]; + std::copy(other.m_dataLight, other.m_dataLight + 2048, m_dataLight); - if (other.m_dataSkyLight) { - m_dataSkyLight = new byte[2048]; - std::copy(other.m_dataSkyLight, other.m_dataSkyLight + 2048, m_dataSkyLight); + if (other.m_dataSkyLight) { + m_dataSkyLight = new byte[2048]; + std::copy(other.m_dataSkyLight, other.m_dataSkyLight + 2048, m_dataSkyLight); + } + } else { + std::copy(other.m_blocks.begin(), other.m_blocks.end(), std::back_inserter(m_blocks)); } m_palette = other.m_palette; @@ -136,3 +135,10 @@ Section::Section(const Section &other) { Vector Section::GetPosition() { return worldPosition; } + +size_t Section::GetHash() { + if (m_blocks.empty()) + return 0; + std::string str((unsigned char*)m_blocks.data(), (unsigned char*)m_blocks.data() + m_blocks.size() * sizeof(Block)); + return std::hash{}(str); +} \ No newline at end of file diff --git a/src/world/World.cpp b/src/world/World.cpp index abcfebf..c83bdce 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -7,6 +7,8 @@ void World::ParseChunkData(std::shared_ptr packet) { if (bitmask[i]) { Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ); Section section = ParseSection(&chunkData, chunkPosition); + section.Parse(); + sectionMutexes[chunkPosition].lock(); auto it = sections.find(chunkPosition); if (it == sections.end()) { sections.insert(std::make_pair(chunkPosition, section)); @@ -14,7 +16,7 @@ void World::ParseChunkData(std::shared_ptr packet) { using std::swap; swap(it->second, section); } - sections.find(chunkPosition)->second.Parse(); + sectionMutexes[chunkPosition].unlock(); } } } @@ -70,11 +72,11 @@ bool World::isPlayerCollides(double X, double Y, double Z) { const double PlayerLength = 0.6; AABB playerColl; - playerColl.x = X - PlayerWidth / 2 - 0.5; + playerColl.x = X - PlayerWidth / 2.0; playerColl.w = PlayerWidth; - playerColl.y = Y - 0.5f; + playerColl.y = Y; playerColl.h = PlayerHeight; - playerColl.z = Z - PlayerLength / 2 - 0.5; + playerColl.z = Z - PlayerLength / 2.0; playerColl.l = PlayerLength; for (int x = 0; x < 16; x++) { @@ -83,9 +85,9 @@ bool World::isPlayerCollides(double X, double Y, double Z) { Block block = it->second.GetBlock(Vector(x, y, z)); if (block.id == 0 || block.id == 31) continue; - AABB blockColl{(x + it->first.GetX() * 16) - 0.5, - (y + it->first.GetY() * 16) - 0.5, - (z + it->first.GetZ() * 16) - 0.5, 1, 1, 1}; + AABB blockColl{(x + it->first.GetX() * 16.0), + (y + it->first.GetY() * 16.0), + (z + it->first.GetZ() * 16.0), 1, 1, 1}; if (TestCollision(playerColl, blockColl)) return true; } @@ -94,3 +96,34 @@ bool World::isPlayerCollides(double X, double Y, double Z) { } return false; } + +Block &World::GetBlock(Vector pos) { + Vector sectionPos (floor(pos.GetX() / 16.0f),floor(pos.GetY() / 16.0f),floor(pos.GetZ()/16.0f)); + Vector inSectionPos = pos - (sectionPos * 16); + if (sections.find(sectionPos)==sections.end()){ + static Block block(0,0); + return block; + } + sectionMutexes[sectionPos].lock(); + Block& block = sections.find(sectionPos)->second.GetBlock(inSectionPos); + sectionMutexes[sectionPos].unlock(); + return block; +} + +std::vector World::GetSectionsList() { + std::vector sectionsList; + for (auto& it:sections) { + sectionsList.push_back(it.first); + } + return sectionsList; +} + +Section &World::GetSection(Vector sectionPos) { + sectionMutexes[sectionPos].lock(); + sectionMutexes[sectionPos].unlock(); + return sections.find(sectionPos)->second; +} + +glm::vec3 World::Raycast(glm::vec3 position, glm::vec3 direction, float maxLength, float minPrecision) { + return glm::vec3(position * direction / maxLength * minPrecision); +} -- cgit v1.2.3