From 2ee0f834487cfe4b6bd9424ca2715685a8db16e4 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 15 Apr 2017 15:17:53 +0500 Subject: 2017-04-15 --- World.cpp | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 World.cpp (limited to 'World.cpp') diff --git a/World.cpp b/World.cpp new file mode 100644 index 0000000..dfd3b60 --- /dev/null +++ b/World.cpp @@ -0,0 +1,218 @@ +#include +#include +#include "World.hpp" + + +Block &World::GetBlock(PositionI pos) { + //unsigned long long pos = (unsigned long long) x << 32; + //pos |= y; + return m_blocks[pos]; +} + +void World::SetBlock(PositionI pos, Block block) { + //unsigned long long pos = (unsigned long long) x << 32; + //pos |= y; + m_blocks[pos] = block; +} + +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]; + packet.GetField(5).CopyToBuff(content); + + std::cout << "Chunk " << chunkX << "x" << chunkZ << std::endl; + std::cout << "\tGround continuous: " << (isGroundContinuous ? "true" : "false") << std::endl; + std::cout << "\tPrimary bitmask: " << bitmask << std::endl; + std::cout << "\tDataLen: " << dataLen << std::endl; + std::cout << "\tEntities: " << entities << std::endl; + + if (isGroundContinuous) + dataLen -= 256; + + byte *biomes = content + packet.GetField(5).GetLength() - 256; + for (int i = 0; i < 1; i++) { + if (bitmask[i]) { + size_t len = ParseSectionData(chunkX, chunkZ, isGroundContinuous, i, content); + //content += len; + //std::cout << "\t Size of section is " << len << std::endl; + } + } + std::cout << std::dec << std::endl; + fflush(stdout); +} + +size_t World::ParseSectionData(int chunkX, int chunkZ, bool isGroundContinous, int section, byte *data) { + Field fBitsPerBlock = FieldParser::Parse(UnsignedByte, data); + byte bitsPerBlock = fBitsPerBlock.GetUByte(); + data += fBitsPerBlock.GetLength(); + + bool usePalette = bitsPerBlock <= 8; + Field fPaletteLength = FieldParser::Parse(VarInt, data); + int paletteLength = fPaletteLength.GetVarInt(); + data += fPaletteLength.GetLength(); + + std::cout << "Section: " << chunkX << "x" << chunkZ << "x" << section << std::endl; + std::cout << "\tBits per block: " << (int) bitsPerBlock << std::endl; + std::cout << "\tPalette length: " << paletteLength << std::endl; + std::vector palette; + std::map pal; + if (paletteLength > 0) { + for (unsigned char i = 0; i < paletteLength; i++) { + endswap(&i); + Field f = FieldParser::Parse(VarInt, data); + data += f.GetLength(); + palette.push_back(f.GetVarInt()); + std::cout << "\t\tPalette[" << std::bitset<8>(i) << "]: " << std::bitset<13>(palette[i]) << std::endl; + pal[i] = f.GetVarInt(); + endswap(&i); + } + } + + Field fDataLength = FieldParser::Parse(VarInt, data); + data += fDataLength.GetLength(); + int dataLength = fDataLength.GetVarInt(); + size_t dataSize = dataLength * 8; + std::cout << "\tData length: " << dataLength << " (" << dataSize << ")" << std::endl; + + std::vector blocks = ParseBlocks(data, dataLength, palette, bitsPerBlock); + std::cout << "\tChunk content: "; + int i = 0; + for (int y = 0; y < SECTION_LENGTH; y++) { + for (int z = 0; z < SECTION_HEIGHT; z++) { + for (int x = 0; x < SECTION_WIDTH; x++) { + int X = chunkX * SECTION_WIDTH + x; + int Y = section * SECTION_HEIGHT + y; + int Z = chunkZ * SECTION_LENGTH + z; + //std::cerr<<"Block at "<(usePalette ? pal[blocks[i]] : blocks[i]) << "\t"; + i++; + } + } + } + std::cout << std::endl; + /*for (auto it:blocks) { + //auto m = std::bitset<4>(it).to_string(); + //std::cout<(pal[it])<<" "; + //std::cout << (m=="1000" || m =="0100" || m=="0010"?"":m+" "); + }*/ + return (dataSize + (m_dimension == 0 ? dataSize : 0) / 2 + dataSize / 2); +} + +std::vector +World::ParseBlocks(byte *bytes, int dataLength, std::vector palette, byte bitsPerBlock) { + std::vector blocks; + for (int i = 0; i < blocks.size(); i++) { + blocks[i] = 0; + } + byte *data = new byte[dataLength * 8]; + size_t arrLength = dataLength * 8; + byte *ptr = data; + for (int i = 0; i < dataLength; i++) { + Field fData = FieldParser::Parse(Long, bytes); + bytes += fData.GetLength(); + *reinterpret_cast(ptr) = fData.GetLong(); + ptr += 8; + } + int bitPos = 0; + unsigned short t = 0; + for (int i = 0; i < arrLength; i++) { + //endswap(&data[i]); + //std::bitset<8> bitset1 = std::bitset<8>(data[i]); + //std::cout << bitset1 << ": "; + for (int j = 0; j < 8; j++) { + //std::bitset<8> bitset2 = std::bitset<8>(t); + /*std::cout << bitset2 << "_"; + std::cout << bitset1 << "\t"; + fflush(stdout);*/ + + t |= (data[i] & 0x01) ? 0x80 : 0x00; + t >>= 1; + data[i] >>= 1; + bitPos++; + if (bitPos >= bitsPerBlock) { + bitPos = 0; + //endswap(&t); + t >>= bitsPerBlock - 1; + blocks.push_back(t); + //std::cout << bitset2 << "\t"; + t = 0; + } + /*bitset1 = std::bitset<8>(data[i]); + bitset2 = std::bitset<8>(t);*/ + } + } + //std::cout << std::endl; + /*int bitPos = 0, bytePos = 0, pushedBlocks = 0, bits = 0; + unsigned short t = 0; + while (bytePos < arrLength) { + //std::cout << std::bitset<8>(data[bytePos]) << " "; + //int bit = data[bytePos] & 1; + t|=(data[bytePos] & 1)<>1; + + + //t = t | ((data[bytePos]<(t)<<"\t"; + if (bitPos >= 7) { + bitPos = 0; + bytePos++; + blocks[pushedBlocks] = t; + std::cout<(t)<<"\t"; + t = 0; + } else { + bitPos++; + } + + bits++; + if (bits>=bitsPerBlock){ + bits=0; + bitPos=0; + bytePos++; + blocks[pushedBlocks] = t; + std::cout<(t)<<"+\t"; + t = 0; + } else { + t <<= 1; + } + fflush(stdout); + }*/ + // + /*int shift = 0; + int bx = 0; + for (int bytmass = 0; bytmass < arrLength; bytmass++) { + unsigned char bitinbyte = 7; + int block = 0; + for (int posbit = 7; posbit > 0; posbit--) { + int b = (data[bytmass] >> posbit); + blocks[block + shift] = blocks[block + shift] | (b << bx); + bx++; + if (bx > bitsPerBlock) { + bx = 0; + if (shift == 0) shift = 1; + if (shift == 1) { + shift = 0; + block++; + if ((block * 2) > blocks.size()) { + std::cout<"<