From 789f70b6f1a9067843dfc1ff73d86b645efe1da9 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 17 Jun 2017 19:23:53 +0500 Subject: 2017-06-17 --- src/world/Collision.cpp | 28 +++++++++ src/world/Collision.hpp | 8 +++ src/world/Section.cpp | 31 ++++------ src/world/Section.hpp | 37 ++++++------ src/world/World.cpp | 151 +++++++++++++++++++++++++----------------------- src/world/World.hpp | 27 +++++---- 6 files changed, 161 insertions(+), 121 deletions(-) create mode 100644 src/world/Collision.cpp create mode 100644 src/world/Collision.hpp (limited to 'src/world') diff --git a/src/world/Collision.cpp b/src/world/Collision.cpp new file mode 100644 index 0000000..4f2c837 --- /dev/null +++ b/src/world/Collision.cpp @@ -0,0 +1,28 @@ +#include "Collision.hpp" + +bool TestCollision(AABB first, AABB second) { + double firstXl = first.x; + double firstXr = first.x + first.w; + + double firstYl = first.y; + double firstYr = first.y + first.h; + + double firstZl = first.z; + double firstZr = first.z + first.l; + + + double secondXl = second.x; + double secondXr = second.x + second.w; + + double secondYl = second.y; + double secondYr = second.y + second.h; + + double secondZl = second.z; + double secondZr = second.z + second.l; + + bool collidesOnX = firstXr >= secondXl && firstXl <= secondXr; + bool collidesOnY = firstYr >= secondYl && firstYl <= secondYr; + bool collidesOnZ = firstZr >= secondZl && firstZl <= secondZr; + + return collidesOnX && collidesOnY && collidesOnZ; +} diff --git a/src/world/Collision.hpp b/src/world/Collision.hpp new file mode 100644 index 0000000..b88fbf7 --- /dev/null +++ b/src/world/Collision.hpp @@ -0,0 +1,8 @@ +#pragma once + +struct AABB { + double x,y,z; + double w,l,h; +}; + +bool TestCollision(AABB first, AABB second); \ No newline at end of file diff --git a/src/world/Section.cpp b/src/world/Section.cpp index 63c7f97..8f94ad7 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -1,7 +1,8 @@ #include "Section.hpp" + Section::Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, - std::vector palette) { + std::vector palette) { m_dataBlocksLen = dataBlocksLength; m_dataBlocks = new byte[m_dataBlocksLen]; std::copy(dataBlocks, dataBlocks + m_dataBlocksLen, m_dataBlocks); @@ -93,30 +94,22 @@ void Section::Parse() { 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)< #include #include "Block.hpp" -#include "../packet/Field.hpp" +#include "../utility/Vector.hpp" +#include "../utility/utility.h" const int SECTION_WIDTH = 16; const int SECTION_LENGTH = 16; const int SECTION_HEIGHT = 16; class Section { - std::vector m_palette; - byte *m_dataBlocks = nullptr; - size_t m_dataBlocksLen; - byte *m_dataLight = nullptr; - byte *m_dataSkyLight = nullptr; - byte m_bitsPerBlock = 0; - std::vector m_blocks; - std::condition_variable parseWaiter; + std::vector m_palette; + byte *m_dataBlocks = nullptr; + size_t m_dataBlocksLen; + byte *m_dataLight = nullptr; + byte *m_dataSkyLight = nullptr; + byte m_bitsPerBlock = 0; + std::vector m_blocks; + std::condition_variable parseWaiter; - Section(); + Section(); public: - void Parse(); + void Parse(); - Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, - std::vector palette); + Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, + std::vector palette); - ~Section(); + ~Section(); - Block &GetBlock(Vector pos); + Block &GetBlock(Vector pos); - Section &operator=(Section other); + Section &operator=(Section other); - void swap(Section &other); + friend void swap(Section &a, Section& b); - Section(const Section &other); + Section(const Section &other); }; \ No newline at end of file diff --git a/src/world/World.cpp b/src/world/World.cpp index 2220627..394598b 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,84 +1,39 @@ #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; +void World::ParseChunkData(std::shared_ptr packet) { + StreamBuffer chunkData(packet->Data.data(), packet->Data.size()); + std::bitset<16> bitmask(packet->PrimaryBitMask); 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; + Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ); + Section section = ParseSection(&chunkData); + auto it = sections.find(chunkPosition); + if (it == sections.end()) { + sections.insert(std::make_pair(chunkPosition, section)); + } else { + using std::swap; + swap(it->second, section); + } + sections.find(chunkPosition)->second.Parse(); } } - 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(); - +Section World::ParseSection(StreamInput *data) { + unsigned char bitsPerBlock = data->ReadUByte(); + int paletteLength = data->ReadVarInt(); 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); - } + for (int i = 0; i < paletteLength; i++) { + palette.push_back(data->ReadVarInt()); } - - 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); + int dataArrayLength = data->ReadVarInt(); + auto dataArray = data->ReadByteArray(dataArrayLength * 8); + auto blockLight = data->ReadByteArray(4096 / 2); + std::vector skyLight; + if (dimension == 0) + skyLight = data->ReadByteArray(4096 / 2); + return Section(dataArray.data(), dataArray.size(), blockLight.data(), + (skyLight.size() > 0 ? skyLight.data() : nullptr), bitsPerBlock, palette); } World::~World() { @@ -86,4 +41,56 @@ World::~World() { World::World() { -} \ No newline at end of file +} + +bool World::isPlayerCollides(double X, double Y, double Z) { + Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); + std::vector closestSectionsCoordinates = { + Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ()), + Vector(PlayerChunk.GetX() + 1, PlayerChunk.GetY(), PlayerChunk.GetZ()), + Vector(PlayerChunk.GetX() - 1, PlayerChunk.GetY(), PlayerChunk.GetZ()), + Vector(PlayerChunk.GetX(), PlayerChunk.GetY() + 1, PlayerChunk.GetZ()), + Vector(PlayerChunk.GetX(), PlayerChunk.GetY() - 1, PlayerChunk.GetZ()), + Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ() + 1), + Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ() - 1), + }; + std::vector::iterator> closestSections; + for (auto &coord:closestSectionsCoordinates) { + auto it = sections.find(coord); + if (it != sections.end()) + closestSections.push_back(it); + } + if (closestSections.empty()) + return false; + + for (auto &it:closestSections) { + + const double PlayerWidth = 0.6; + const double PlayerHeight = 1.82; + const double PlayerLength = 0.6; + + AABB playerColl; + playerColl.x = X - PlayerWidth / 2 - 0.5; + playerColl.w = PlayerWidth; + playerColl.y = Y - 0.5f; + playerColl.h = PlayerHeight; + playerColl.z = Z - PlayerLength / 2 - 0.5; + playerColl.l = PlayerLength; + + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; 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}; + if (TestCollision(playerColl, blockColl)) + return true; + } + } + } + } + return false; +} diff --git a/src/world/World.hpp b/src/world/World.hpp index b33499c..e315baf 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -8,27 +8,30 @@ #include #include #include "Block.hpp" -#include "../packet/Packet.hpp" #include "Section.hpp" +#include "../network/Packet.hpp" +#include "Collision.hpp" class World { - //utility vars - World(const World &other); + //utility vars + World(const World &other); - World &operator=(const World &other); + World &operator=(const World &other); - //game vars - int m_dimension = 0; + //game vars + int dimension = 0; - //game methods - Section ParseSection(byte *data, size_t &dataLen); + //game methods + Section ParseSection(StreamInput *data); public: - World(); + World(); - ~World(); + ~World(); - void ParseChunkData(Packet packet); + void ParseChunkData(std::shared_ptr packet); - std::map m_sections; + std::map sections; + + bool isPlayerCollides(double X, double Y, double Z); }; \ No newline at end of file -- cgit v1.2.3