diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/AssetManager.cpp | 19 | ||||
-rw-r--r-- | src/core/AssetManager.hpp | 8 | ||||
-rw-r--r-- | src/core/Core.cpp | 109 | ||||
-rw-r--r-- | src/network/Network.cpp | 3 | ||||
-rw-r--r-- | src/world/Block.cpp | 16 | ||||
-rw-r--r-- | src/world/Block.hpp | 12 | ||||
-rw-r--r-- | src/world/Section.cpp | 222 | ||||
-rw-r--r-- | src/world/World.cpp | 153 |
8 files changed, 270 insertions, 272 deletions
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp index eaa002f..bcf50bc 100644 --- a/src/core/AssetManager.cpp +++ b/src/core/AssetManager.cpp @@ -3,9 +3,11 @@ namespace fs = std::experimental::filesystem; -const fs::path pathToAssets = "./assets/"; -const fs::path pathToAssetsList = "./items.json"; -const fs::path pathToTextureIndex = "./textures.json"; +//const fs::path pathToAssets = "./assets/"; +//const fs::path pathToAssetsList = "./items.json"; +//const fs::path pathToTextureIndex = "./textures.json"; +const std::string pathToAssetsList = "./items.json"; +const std::string pathToTextureIndex = "./textures.json"; AssetManager::AssetManager() { LoadIds(); @@ -63,7 +65,7 @@ TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) { std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) { //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side - std::map<BlockTextureId, std::string> lookupTable = { + const std::map<BlockTextureId, std::string> lookupTable = { {BlockTextureId(0, 0), "minecraft/textures/blocks/air"}, {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"}, {BlockTextureId(1, 1), "minecraft/textures/blocks/stone_granite"}, @@ -78,7 +80,11 @@ std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) { {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"}, {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"}, }; - return lookupTable[block]; + auto ret = lookupTable.find(block); + if (ret == lookupTable.end()) + return ""; + else + return ret->second; } GLuint AssetManager::GetTextureAtlas() { @@ -86,5 +92,6 @@ GLuint AssetManager::GetTextureAtlas() { } TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) { - return this->GetTextureByAssetName(this->GetTextureAssetNameByBlockId(block)); + std::string assetName = this->GetTextureAssetNameByBlockId(block); + return this->GetTextureByAssetName(assetName); } diff --git a/src/core/AssetManager.hpp b/src/core/AssetManager.hpp index b378764..ebb339f 100644 --- a/src/core/AssetManager.hpp +++ b/src/core/AssetManager.hpp @@ -33,12 +33,18 @@ struct BlockTextureId { int state:4; int side:3; + + bool operator<(const BlockTextureId &rhs) const { if (id < rhs.id) return true; if (rhs.id < id) return false; - return state < rhs.state; + if (state < rhs.state) + return true; + if (rhs.state < state) + return false; + return side < rhs.side; } }; diff --git a/src/core/Core.cpp b/src/core/Core.cpp index ade043e..3257cb6 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -185,7 +185,7 @@ const GLfloat uv_coords[] = { Core::Core() { LOG(INFO) << "Core initializing..."; - InitSfml(1280, 720, "AltCraft"); + InitSfml(900, 450, "AltCraft"); glCheckError(); InitGlew(); glCheckError(); @@ -275,7 +275,8 @@ void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string W contextSetting.depthBits = 24; window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting); glCheckError(); - window->setVerticalSyncEnabled(true); + //window->setVerticalSyncEnabled(true); + //window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2, sf::VideoMode::getDesktopMode().height / 2)); window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); @@ -377,7 +378,8 @@ void Core::RenderWorld() { GLint modelLoc = glGetUniformLocation(shader->Program, "model"); GLint projectionLoc = glGetUniformLocation(shader->Program, "projection"); GLint viewLoc = glGetUniformLocation(shader->Program, "view"); - GLint blockLoc = glGetUniformLocation(shader->Program, "block"); + GLint blockLoc = glGetUniformLocation(shader->Program, "Block"); + GLint stateLoc = glGetUniformLocation(shader->Program, "State"); GLint timeLoc = glGetUniformLocation(shader->Program, "time"); glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 10000000.0f); glm::mat4 view = camera.GetViewMatrix(); @@ -389,7 +391,7 @@ void Core::RenderWorld() { glBindVertexArray(VAO); - for (auto §ionPos:toRender) { + for (auto §ionPos : toRender) { Section §ion = gameState->world.m_sections.find(sectionPos)->second; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { @@ -405,6 +407,7 @@ void Core::RenderWorld() { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); + glUniform1i(stateLoc, block.state); glDrawArrays(GL_TRIANGLES, 0, 36); } @@ -457,22 +460,24 @@ void Core::PrepareToWorldRendering() { std::vector<glm::vec4> textureCoordinates; std::vector<GLint> indexes; GLint totalTextures; - for (int id = 0; id < 4096; id++) { + for (int id = 1; id < 4096; id++) { bool isReachedEnd = true; for (int state = 0; state < 16; state++) { - if (!assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) || + BlockTextureId blockTextureId(id, state, 6); + if (!assetManager->GetTextureByBlock(blockTextureId) && !assetManager->GetTextureByBlock(BlockTextureId(id, state, 0))) { continue; } isReachedEnd = false; - int side = assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ? 6 : 0; + int side = assetManager->GetTextureByBlock(blockTextureId) ? 6 : 0; do { int index = (side << 16) | (id << 4) | state; TextureCoordinates tc = assetManager->GetTextureByBlock(BlockTextureId(id, state, side)); textureCoordinates.push_back(glm::vec4(tc.x, tc.y, tc.w, tc.h)); indexes.push_back(index); - /*LOG(ERROR) << "Encoded (" << side << " " << id << " " << state << ") as " << index << " (" - << std::bitset<20>(index) << ")";*/ + /*LOG(ERROR) << "Encoded texture (" << id << " " << state << " " << side << ") as " << index << " (" + << std::bitset<19>(index) << ")" << " = " << tc.x << "," << tc.y << "," << tc.w << "," + << tc.h;*/ /*LOG(FATAL)<<std::bitset<18>(index); side = 0x7; id = 0xFFF; @@ -484,20 +489,20 @@ void Core::PrepareToWorldRendering() { st = state; index = i | si | st; LOG(FATAL) << std::bitset<18>(index) << " (" << index << "): " << std::bitset<18>(si) << " " - << std::bitset<18>(i) << " " << std::bitset<18>(st);*/ + << std::bitset<18>(i) << " " << std::bitset<18>(st);*/ /*if (rand() == 73) //Almost impossible(Almost==1/32768) { - int index = 393233; - LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")"; - int side = (index & 0xE0000) >> 16; - int id = (index & 0xFF0) >> 4; - int state = index & 0xF; - LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " " - << std::bitset<20>(state); - LOG(FATAL) << side << " " << id << " " << state; + int index = 393233; + LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")"; + int side = (index & 0xE0000) >> 16; + int id = (index & 0xFF0) >> 4; + int state = index & 0xF; + LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " " + << std::bitset<20>(state); + LOG(FATAL) << side << " " << id << " " << state; }*/ side++; - } while (side < 7); + } while (side < 6); } if (isReachedEnd) break; @@ -506,52 +511,45 @@ void Core::PrepareToWorldRendering() { totalTextures = indexes.size(); LOG(INFO) << "Created " << totalTextures << " texture indexes"; CHECK_EQ(indexes.size(), textureCoordinates.size()) << "Arrays of textureCoordinates and of indexes is not equals"; - CHECK_LE(totalTextures, 2048) << "There is more texture indexes, than GLSL buffer allows"; - - for (auto& it:indexes){ - LOG(WARNING)<<it; - } - - indexes.insert(indexes.begin(), totalTextures); - indexes.resize(2048); - - + CHECK_LE(totalTextures, 1023) << "There is more texture indexes, than GLSL buffer allows"; + GLuint bp1 = 0; GLuint ubo = glGetUniformBlockIndex(shader->Program, "TextureIndexes"); - glUniformBlockBinding(shader->Program, ubo, 0); + glUniformBlockBinding(shader->Program, ubo, bp1); glGenBuffers(1, &UBO); glBindBuffer(GL_UNIFORM_BUFFER, UBO); - glBufferData(GL_UNIFORM_BUFFER, indexes.size() * sizeof(GLint), NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 0, UBO, 0, indexes.size() * sizeof(GLint)); - glBufferSubData(GL_UNIFORM_BUFFER, 0, indexes.size() * sizeof(GLint), &indexes[0]); + glBindBufferBase(GL_UNIFORM_BUFFER, bp1, UBO); + glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) + sizeof(glm::vec4) * 1023, NULL, GL_STATIC_DRAW); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &totalTextures); //copy totalTextures + for (int i = 0; i < indexes.size(); i++) { + size_t baseOffset = sizeof(glm::vec4); + size_t itemOffset = sizeof(glm::vec4); + size_t offset = baseOffset + i * itemOffset; + /*int index = indexes[i]; + int side = (index & 0x70000) >> 16; + int id = (index & 0xFF0) >> 4; + int state = index & 0xF; + LOG(WARNING) << "Copying " << indexes[i] << " at " << offset<<" side is "<<side;*/ + glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(GLint), &indexes[i]); //copy indexes' item + } glCheckError(); - LOG(WARNING)<<"Uploaded "<<indexes.size() * sizeof(GLint)<<" bytes"; - - /*GLuint ubo2 = glGetUniformBlockIndex(shader->Program, "TextureData"); - glUniformBlockBinding(shader->Program, ubo2, 1); + GLuint bp2 = 1; + GLuint ubo2_index = glGetUniformBlockIndex(shader->Program, "TextureData"); + glUniformBlockBinding(shader->Program, ubo2_index, bp2); glGenBuffers(1, &UBO2); glBindBuffer(GL_UNIFORM_BUFFER, UBO2); + glBindBufferBase(GL_UNIFORM_BUFFER, bp2, UBO2); glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 1, UBO2, 0, 1024 * sizeof(glm::vec4)); - glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data());*/ - - /* - GLuint ubo3 = glGetUniformBlockIndex(shader->Program, "TextureData2"); - glUniformBlockBinding(shader->Program, ubo3, 2); - glGenBuffers(1, &UBO3); - glBindBuffer(GL_UNIFORM_BUFFER, UBO3); - glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 2, UBO3, 0, 1024 * sizeof(glm::vec4));*/ - - glBindBuffer(GL_UNIFORM_BUFFER,0); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data()); + glBindBuffer(GL_UNIFORM_BUFFER, 0); glCheckError(); } void Core::UpdateChunksToRender() { camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ); toRender.clear(); - const float ChunkDistance = 1; + const float ChunkDistance = 2; Vector playerChunk = Vector(floor(gameState->g_PlayerX / 16.0f), floor(gameState->g_PlayerY / 16.0f), floor(gameState->g_PlayerZ / 16.0f)); for (auto &it:gameState->world.m_sections) { @@ -562,17 +560,6 @@ void Core::UpdateChunksToRender() { toRender.push_back(chunkPosition); } LOG(INFO) << "Chunks to render: " << toRender.size(); - - /*std::map<Block, int> totalBlocks; - for (auto §ion:toRender) - for (int x = 0; x < 16; x++) - for (int y = 0; y < 16; y++) - for (int z = 0; z < 16; z++) - totalBlocks[gameState->world.m_sections.find(section)->second.GetBlock(Vector(x, y, z))]++; - for (auto &it:totalBlocks) { - LOG(WARNING) << it.first.id << ":" << (int) it.first.state << " = " << it.second << " (" - << std::bitset<13>(it.first.id) << ")"; - }*/ } void Core::UpdateGameState() { diff --git a/src/network/Network.cpp b/src/network/Network.cpp index 7757be9..399ce20 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -9,6 +9,7 @@ Network::Network(std::string address, unsigned short port) : m_address(address), LOG(ERROR) << "Can't connect to remote server"; } else { LOG(ERROR) << "Connection failed with unknown reason"; + throw std::runtime_error("Connection is failed"); throw 13; } } @@ -69,7 +70,7 @@ Packet Network::ReceivePacket() { } if (dataLen < packetLen) { LOG(ERROR) << "Received data is "<<dataLen<<" but "<<packetLen<<" is promoted"; - throw std::runtime_error(""); + throw std::runtime_error("Data is losted"); } else { Packet p(bufPack); delete[] bufPack; diff --git a/src/world/Block.cpp b/src/world/Block.cpp index 74423e0..e88068a 100644 --- a/src/world/Block.cpp +++ b/src/world/Block.cpp @@ -2,16 +2,16 @@ Block::~Block() {} -Block::Block(unsigned short id, unsigned short state) : id(id), state(state) {} +Block::Block(unsigned short id, unsigned char state) : id(id), state(state) {} Block::Block() : id(0), state(0) {} bool operator<(const Block &lhs, const Block &rhs) { - if (lhs.id < rhs.id) - return true; - if (lhs.id == rhs.id) { - if (lhs.state != rhs.state) - return lhs.state < rhs.state; - } - return false; + if (lhs.id < rhs.id) + return true; + if (lhs.id == rhs.id) { + if (lhs.state != rhs.state) + return lhs.state < rhs.state; + } + return false; } diff --git a/src/world/Block.hpp b/src/world/Block.hpp index 50268f3..2f823fe 100644 --- a/src/world/Block.hpp +++ b/src/world/Block.hpp @@ -1,15 +1,15 @@ #pragma once struct Block { - Block(); + Block(); - Block(unsigned short id, unsigned short state); + Block(unsigned short id, unsigned char state); - ~Block(); + ~Block(); - unsigned short id:13; - unsigned char state:4; - //unsigned char light:4; + unsigned short id : 13; + unsigned char state : 4; + //unsigned char light:4; }; bool operator<(const Block &lhs, const Block &rhs);
\ No newline at end of file diff --git a/src/world/Section.cpp b/src/world/Section.cpp index 6147295..63c7f97 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -1,139 +1,137 @@ #include "Section.hpp" Section::Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, - std::vector<unsigned short> palette) { - m_dataBlocksLen = dataBlocksLength; - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(dataBlocks, dataBlocks + m_dataBlocksLen, m_dataBlocks); + std::vector<unsigned short> palette) { + m_dataBlocksLen = dataBlocksLength; + m_dataBlocks = new byte[m_dataBlocksLen]; + std::copy(dataBlocks, dataBlocks + m_dataBlocksLen, m_dataBlocks); - m_dataLight = new byte[2048]; - std::copy(dataLight, dataLight + 2048, m_dataLight); + m_dataLight = new byte[2048]; + std::copy(dataLight, dataLight + 2048, m_dataLight); - if (dataSky) { - m_dataSkyLight = new byte[2048]; - std::copy(dataSky, dataSky + 2048, m_dataSkyLight); - } + if (dataSky) { + m_dataSkyLight = new byte[2048]; + std::copy(dataSky, dataSky + 2048, m_dataSkyLight); + } - m_palette = palette; - m_bitsPerBlock = bitsPerBlock; + m_palette = palette; + m_bitsPerBlock = bitsPerBlock; } Section::~Section() { - delete[] m_dataBlocks; - m_dataBlocksLen = 0; - m_dataBlocks = nullptr; - delete[] m_dataLight; - m_dataLight = nullptr; - delete[] m_dataSkyLight; - m_dataSkyLight = nullptr; + delete[] m_dataBlocks; + m_dataBlocksLen = 0; + m_dataBlocks = nullptr; + delete[] m_dataLight; + m_dataLight = nullptr; + delete[] m_dataSkyLight; + m_dataSkyLight = nullptr; } Block &Section::GetBlock(Vector pos) { - if (m_dataBlocks != nullptr) { - std::mutex parseMutex; - std::unique_lock<std::mutex> 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()]; + if (m_dataBlocks != nullptr) { + std::mutex parseMutex; + std::unique_lock<std::mutex> 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()]; } void Section::Parse() { - if (m_dataBlocks == nullptr) - return; - - long long *longArray = reinterpret_cast<long long *>(m_dataBlocks); - for (size_t i = 0; i < m_dataBlocksLen / 8; i++) - endswap(&longArray[i]); - std::vector<unsigned short> 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; - } - } - } - - std::vector<byte> light; - light.reserve(4096); - for (int i = 0; i < 2048; i++) { - byte t = m_dataLight[i]; - byte first = t & 0b11110000; - byte second = t >> 4; - light.push_back(first); - light.push_back(second); - } - for (int i = 0; i < 4096; i++) { - unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; - Block block(blockId>>4, blockId>>4 & 0xF); - m_blocks.push_back(block); - } - if ((light.size() + blocks.size()) / 2 != 4096) { - throw 118; - } - delete[] m_dataBlocks; - m_dataBlocksLen = 0; - m_dataBlocks = nullptr; - delete[] m_dataLight; - m_dataLight = nullptr; - delete[] m_dataSkyLight; - m_dataSkyLight = nullptr; - - parseWaiter.notify_all(); - /*static std::map<Block,int> totalBlocks; - for (int x=0;x<16;x++) - for (int y=0;y<16;y++) - for (int z=0;z<16;z++) - totalBlocks[GetBlock(Vector(x,y,z))]++; - LOG(ERROR)<<"Logging chunk"; - for (auto& it:totalBlocks){ - LOG(WARNING)<<it.first.id<<":"<<(int)it.first.state<<" = "<<it.second; - }*/ + if (m_dataBlocks == nullptr) + return; + + long long *longArray = reinterpret_cast<long long *>(m_dataBlocks); + for (size_t i = 0; i < m_dataBlocksLen / 8; i++) + endswap(&longArray[i]); + std::vector<unsigned short> 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; + } + } + } + + std::vector<byte> light; + light.reserve(4096); + for (int i = 0; i < 2048; i++) { + byte t = m_dataLight[i]; + byte first = t & 0b11110000; + byte second = t >> 4; + light.push_back(first); + light.push_back(second); + } + for (int i = 0; i < 4096; i++) { + unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; + 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; + delete[] m_dataLight; + m_dataLight = nullptr; + delete[] m_dataSkyLight; + m_dataSkyLight = nullptr; + + parseWaiter.notify_all(); + /*static std::map<Block,int> totalBlocks; + for (int x=0;x<16;x++) + for (int y=0;y<16;y++) + for (int z=0;z<16;z++) + totalBlocks[GetBlock(Vector(x,y,z))]++; + LOG(ERROR)<<"Logging chunk"; + for (auto& it:totalBlocks){ + LOG(WARNING)<<it.first.id<<":"<<(int)it.first.state<<" = "<<it.second; + }*/ } Section &Section::operator=(Section other) { - other.swap(*this); - return *this; + other.swap(*this); + return *this; } void Section::swap(Section &other) { - std::swap(other.m_dataBlocksLen, m_dataBlocksLen); - std::swap(other.m_dataBlocks, m_dataBlocks); - std::swap(other.m_dataLight, m_dataLight); - std::swap(other.m_dataSkyLight, m_dataSkyLight); - std::swap(other.m_blocks, m_blocks); - std::swap(other.m_palette, m_palette); - std::swap(other.m_bitsPerBlock, m_bitsPerBlock); + std::swap(other.m_dataBlocksLen, m_dataBlocksLen); + std::swap(other.m_dataBlocks, m_dataBlocks); + std::swap(other.m_dataLight, m_dataLight); + std::swap(other.m_dataSkyLight, m_dataSkyLight); + std::swap(other.m_blocks, m_blocks); + std::swap(other.m_palette, m_palette); + std::swap(other.m_bitsPerBlock, m_bitsPerBlock); } Section::Section(const Section &other) { - m_dataBlocksLen = other.m_dataBlocksLen; - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(other.m_dataBlocks, other.m_dataBlocks + m_dataBlocksLen, m_dataBlocks); + m_dataBlocksLen = other.m_dataBlocksLen; + 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); - - if (other.m_dataSkyLight) { - m_dataSkyLight = new byte[2048]; - std::copy(other.m_dataSkyLight, other.m_dataSkyLight + 2048, m_dataSkyLight); - } - - m_palette = other.m_palette; - m_bitsPerBlock = other.m_bitsPerBlock; -} + 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); + } + m_palette = other.m_palette; + m_bitsPerBlock = other.m_bitsPerBlock; +}
\ No newline at end of file diff --git a/src/world/World.cpp b/src/world/World.cpp index 121b904..2220627 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,84 +1,84 @@ #include "World.hpp" void World::ParseChunkData(Packet packet) { - int chunkX = packet.GetField(0).GetInt(); - int chunkZ = packet.GetField(1).GetInt(); - bool isGroundContinuous = packet.GetField(2).GetBool(); - std::bitset<16> bitmask(packet.GetField(3).GetVarInt()); - int entities = packet.GetField(5).GetVarInt(); - - size_t dataLen = packet.GetField(5).GetLength(); - byte *content = new byte[dataLen]; - byte *contentOrigPtr = content; - packet.GetField(5).CopyToBuff(content); - - if (isGroundContinuous) - dataLen -= 256; - - byte *biomes = content + packet.GetField(5).GetLength() - 256; - for (int i = 0; i < 16; i++) { - if (bitmask[i]) { - size_t len = 0; - Vector chunkPosition = Vector(chunkX, i, chunkZ); - if (!m_sections.insert(std::make_pair(chunkPosition, ParseSection(content, len))).second) - LOG(ERROR) << "Chunk not created: " << chunkPosition; - auto sectionIter = m_sections.find(chunkPosition); - if (sectionIter == m_sections.end()) - LOG(ERROR)<< "Created chunk not found: " << chunkPosition; - else - sectionIter->second.Parse(); - content += len; - } - } - delete[] contentOrigPtr; + int chunkX = packet.GetField(0).GetInt(); + int chunkZ = packet.GetField(1).GetInt(); + bool isGroundContinuous = packet.GetField(2).GetBool(); + std::bitset<16> bitmask(packet.GetField(3).GetVarInt()); + int entities = packet.GetField(5).GetVarInt(); + + size_t dataLen = packet.GetField(5).GetLength(); + byte *content = new byte[dataLen]; + byte *contentOrigPtr = content; + packet.GetField(5).CopyToBuff(content); + + if (isGroundContinuous) + dataLen -= 256; + + byte *biomes = content + packet.GetField(5).GetLength() - 256; + for (int i = 0; i < 16; i++) { + if (bitmask[i]) { + size_t len = 0; + Vector chunkPosition = Vector(chunkX, i, chunkZ); + if (!m_sections.insert(std::make_pair(chunkPosition, ParseSection(content, len))).second) + LOG(ERROR) << "Chunk not created: " << chunkPosition; + auto sectionIter = m_sections.find(chunkPosition); + if (sectionIter == m_sections.end()) + LOG(ERROR) << "Created chunk not found: " << chunkPosition; + else + sectionIter->second.Parse(); + content += len; + } + } + delete[] contentOrigPtr; } Section World::ParseSection(byte *data, size_t &dataLen) { - dataLen = 0; - - Field fBitsPerBlock = FieldParser::Parse(UnsignedByte, data); - byte bitsPerBlock = fBitsPerBlock.GetUByte(); - data += fBitsPerBlock.GetLength(); - dataLen += fBitsPerBlock.GetLength(); - - Field fPaletteLength = FieldParser::Parse(VarIntType, data); - int paletteLength = fPaletteLength.GetVarInt(); - data += fPaletteLength.GetLength(); - dataLen += fPaletteLength.GetLength(); - - std::vector<unsigned short> palette; - if (paletteLength > 0) { - for (unsigned char i = 0; i < paletteLength; i++) { - endswap(&i); - Field f = FieldParser::Parse(VarIntType, data); - data += f.GetLength(); - dataLen += f.GetLength(); - palette.push_back(f.GetVarInt()); - endswap(&i); - } - } - - Field fDataLength = FieldParser::Parse(VarIntType, data); - data += fDataLength.GetLength(); - dataLen += fDataLength.GetLength(); - - int dataLength = fDataLength.GetVarInt(); - size_t dataSize = dataLength * 8; - dataLen += dataSize; - byte *dataBlocks = data; - - data += 2048; - dataLen += 2048; - byte *dataLight = data; - - byte *dataSky = nullptr; - if (m_dimension == 0) { - data += 2048; - dataLen += 2048; - dataSky = data; - } - - return Section(dataBlocks, dataSize, dataLight, dataSky, bitsPerBlock, palette); + dataLen = 0; + + Field fBitsPerBlock = FieldParser::Parse(UnsignedByte, data); + byte bitsPerBlock = fBitsPerBlock.GetUByte(); + data += fBitsPerBlock.GetLength(); + dataLen += fBitsPerBlock.GetLength(); + + Field fPaletteLength = FieldParser::Parse(VarIntType, data); + int paletteLength = fPaletteLength.GetVarInt(); + data += fPaletteLength.GetLength(); + dataLen += fPaletteLength.GetLength(); + + std::vector<unsigned short> palette; + if (paletteLength > 0) { + for (unsigned char i = 0; i < paletteLength; i++) { + endswap(&i); + Field f = FieldParser::Parse(VarIntType, data); + data += f.GetLength(); + dataLen += f.GetLength(); + palette.push_back(f.GetVarInt()); + endswap(&i); + } + } + + Field fDataLength = FieldParser::Parse(VarIntType, data); + data += fDataLength.GetLength(); + dataLen += fDataLength.GetLength(); + + int dataLength = fDataLength.GetVarInt(); + size_t dataSize = dataLength * 8; + dataLen += dataSize; + byte *dataBlocks = data; + + data += 2048; + dataLen += 2048; + byte *dataLight = data; + + byte *dataSky = nullptr; + if (m_dimension == 0) { + data += 2048; + dataLen += 2048; + dataSky = data; + } + + return Section(dataBlocks, dataSize, dataLight, dataSky, bitsPerBlock, palette); } World::~World() { @@ -86,5 +86,4 @@ World::~World() { World::World() { -} - +}
\ No newline at end of file |