From b192dcec842cf043ae936c446bb252259b415ab5 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Wed, 7 Jun 2017 19:21:39 +0500 Subject: 2017-06-07 --- src/world/Block.cpp | 16 ++-- src/world/Block.hpp | 12 +-- src/world/Section.cpp | 222 +++++++++++++++++++++++++------------------------- src/world/World.cpp | 153 +++++++++++++++++----------------- 4 files changed, 200 insertions(+), 203 deletions(-) (limited to 'src/world') 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 palette) { - m_dataBlocksLen = dataBlocksLength; - m_dataBlocks = new byte[m_dataBlocksLen]; - std::copy(dataBlocks, dataBlocks + m_dataBlocksLen, m_dataBlocks); + std::vector 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 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 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(m_dataBlocks); - for (size_t i = 0; i < m_dataBlocksLen / 8; i++) - 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; - } - } - } - - std::vector 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 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)<(m_dataBlocks); + for (size_t i = 0; i < m_dataBlocksLen / 8; i++) + 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; + } + } + } + + std::vector 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 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)< 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 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 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 -- cgit v1.2.3