summaryrefslogtreecommitdiffstats
path: root/src/world/World.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-18 16:03:05 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-18 16:03:05 +0200
commit4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79 (patch)
tree548911d4e341fd5717acff3b5bc752c7b5650bf4 /src/world/World.cpp
parent2017-05-13 (diff)
downloadAltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.gz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.bz2
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.lz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.xz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.zst
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.zip
Diffstat (limited to 'src/world/World.cpp')
-rw-r--r--src/world/World.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/world/World.cpp b/src/world/World.cpp
new file mode 100644
index 0000000..adbb3e1
--- /dev/null
+++ b/src/world/World.cpp
@@ -0,0 +1,117 @@
+#include <iostream>
+#include <bitset>
+#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)
+ std::cout<<"Chunk not created: "<<chunkPosition<<std::endl;
+ auto sectionIter = m_sections.find(chunkPosition);
+ if (sectionIter==m_sections.end())
+ std::cout<<"Created chunk not found: "<<chunkPosition<<std::endl;
+ else
+ sectionIter->second.Parse();
+ /*m_sections[chunkPosition] = ParseSection(content, len);
+ m_sections[chunkPosition].Parse();*/
+ /*m_sectionToParse.push(m_sections.find(Vector(chunkX, i, chunkZ)));
+ m_parseSectionWaiter.notify_one();*/
+ content += len;
+ }
+ }
+ delete[] contentOrigPtr;
+ //std::cout<<m_sections.size()<<std::endl;
+}
+
+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(VarInt, 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(VarInt, data);
+ data += f.GetLength();
+ dataLen += f.GetLength();
+ palette.push_back(f.GetVarInt());
+ endswap(&i);
+ }
+ }
+
+ Field fDataLength = FieldParser::Parse(VarInt, 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() {
+ isContinue = false;
+ m_parseSectionWaiter.notify_all();
+ m_sectionParseThread.join();
+}
+
+void World::SectionParsingThread() {
+ while (isContinue) {
+ std::unique_lock<std::mutex> sectionParseLocker(m_parseSectionMutex);
+ m_parseSectionWaiter.wait(sectionParseLocker);
+ while (m_sectionToParse.size() == 0 && isContinue) {
+ m_parseSectionWaiter.wait(sectionParseLocker);
+ }
+ while (m_sectionToParse.size() > 0) {
+ auto it = m_sectionToParse.front();
+ m_sectionToParse.pop();
+ it->second.Parse();
+ /*std::cout << "Parsed chunk" << it->first.GetX() << "x" << it->first.GetY() << "x" << it->first.GetZ()
+ << std::endl;*/
+ }
+ }
+}
+
+World::World() {
+ m_sectionParseThread = std::thread(&World::SectionParsingThread, this);
+}
+