diff options
Diffstat (limited to '')
-rw-r--r-- | code/Block.cpp | 10 | ||||
-rw-r--r-- | code/Block.hpp | 15 | ||||
-rw-r--r-- | code/CMakeLists.txt | 95 | ||||
-rw-r--r-- | code/FieldParser.cpp | 106 | ||||
-rw-r--r-- | code/FieldParser.hpp | 30 | ||||
-rw-r--r-- | code/Game.cpp | 159 | ||||
-rw-r--r-- | code/Nbt.hpp | 516 | ||||
-rw-r--r-- | code/NetworkClient.hpp | 42 | ||||
-rw-r--r-- | code/Packet.cpp | 100 | ||||
-rw-r--r-- | code/Packet.hpp | 130 | ||||
-rw-r--r-- | code/PacketBuilder.cpp | 65 | ||||
-rw-r--r-- | code/PacketBuilder.hpp | 17 | ||||
-rw-r--r-- | code/PacketParser.cpp | 147 | ||||
-rw-r--r-- | code/PacketParser.hpp | 38 | ||||
-rw-r--r-- | code/PositionF.cpp | 50 | ||||
-rw-r--r-- | code/PositionF.hpp | 29 | ||||
-rw-r--r-- | code/PositionI.cpp | 120 | ||||
-rw-r--r-- | code/PositionI.hpp | 50 | ||||
-rw-r--r-- | code/graphics/AssetManager.hpp | 52 | ||||
-rw-r--r-- | code/graphics/Display.cpp | 268 | ||||
-rw-r--r-- | code/main.cpp | 10 | ||||
-rw-r--r-- | code/shaders/simple.fs | 22 | ||||
-rw-r--r-- | code/shaders/simple.vs | 15 | ||||
-rw-r--r-- | src/gamestate/Game.hpp (renamed from code/Game.hpp) | 10 | ||||
-rw-r--r-- | src/graphics/AssetManager.cpp (renamed from code/graphics/AssetManager.cpp) | 2 | ||||
-rw-r--r-- | src/graphics/Camera3D.cpp (renamed from code/graphics/Camera3D.cpp) | 0 | ||||
-rw-r--r-- | src/graphics/Camera3D.hpp (renamed from code/graphics/Camera3D.hpp) | 0 | ||||
-rw-r--r-- | src/graphics/Display.hpp (renamed from code/graphics/Display.hpp) | 4 | ||||
-rw-r--r-- | src/graphics/Shader.cpp (renamed from code/graphics/Shader.cpp) | 0 | ||||
-rw-r--r-- | src/graphics/Shader.hpp (renamed from code/graphics/Shader.hpp) | 0 | ||||
-rw-r--r-- | src/graphics/Texture.cpp (renamed from code/graphics/Texture.cpp) | 0 | ||||
-rw-r--r-- | src/graphics/Texture.hpp (renamed from code/graphics/Texture.hpp) | 0 | ||||
-rw-r--r-- | src/network/Network.cpp (renamed from code/Network.cpp) | 2 | ||||
-rw-r--r-- | src/network/Network.hpp (renamed from code/Network.hpp) | 2 | ||||
-rw-r--r-- | src/network/NetworkClient.cpp (renamed from code/NetworkClient.cpp) | 6 | ||||
-rw-r--r-- | src/packet/Field.cpp (renamed from code/Field.cpp) | 9 | ||||
-rw-r--r-- | src/packet/Field.hpp (renamed from code/Field.hpp) | 8 | ||||
-rw-r--r-- | src/utility/utility.cpp (renamed from code/utility.cpp) | 0 | ||||
-rw-r--r-- | src/utility/utility.h (renamed from code/utility.h) | 0 | ||||
-rw-r--r-- | src/world/Section.cpp (renamed from code/Section.cpp) | 5 | ||||
-rw-r--r-- | src/world/Section.hpp (renamed from code/Section.hpp) | 9 | ||||
-rw-r--r-- | src/world/World.cpp (renamed from code/World.cpp) | 16 | ||||
-rw-r--r-- | src/world/World.hpp (renamed from code/World.hpp) | 6 |
43 files changed, 42 insertions, 2123 deletions
diff --git a/code/Block.cpp b/code/Block.cpp deleted file mode 100644 index 64e5330..0000000 --- a/code/Block.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "Block.hpp" - -Block::~Block() {} - -Block::Block(unsigned short idAndState, unsigned char light) : id(idAndState >> 4), state(idAndState & 0x0F), - light(light) {} - -Block::Block(unsigned short id, unsigned char state, unsigned char light) : id(id), state(state), light(light) {} - -Block::Block() : id(0), state(0), light(0) {} diff --git a/code/Block.hpp b/code/Block.hpp deleted file mode 100644 index 7c780c1..0000000 --- a/code/Block.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -struct Block { - Block(unsigned short idAndState, unsigned char light); - - Block(unsigned short id, unsigned char state, unsigned char light); - - Block(); - - ~Block(); - - unsigned short id:13; - unsigned char state:4; - unsigned char light:4; -};
\ No newline at end of file diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt deleted file mode 100644 index c084e3e..0000000 --- a/code/CMakeLists.txt +++ /dev/null @@ -1,95 +0,0 @@ -cmake_minimum_required(VERSION 3.0) -project(AltCraft) -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules) - -if (CMAKE_COMPILER_IS_GNUCXX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -w -Werror") - #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") - #set(CMAKE_CXX_FLASG "${CMAKE_CXX_FLAGS} -g -O0") - #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") - #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pg") - set(PLATFORM_LIBRARIES "pthread") -endif () - -set(SOURCE_FILES main.cpp Field.hpp utility.cpp Packet.hpp FieldParser.hpp Network.hpp Network.cpp NetworkClient.cpp - NetworkClient.hpp json.hpp PacketBuilder.cpp PacketBuilder.hpp Packet.cpp FieldParser.cpp Field.cpp - PacketParser.cpp PacketParser.hpp PositionF.cpp PositionF.hpp PositionI.cpp PositionI.hpp Game.cpp - Game.hpp World.cpp World.hpp Block.cpp Block.hpp Section.cpp Section.hpp Nbt.hpp graphics/AssetManager.cpp - graphics/AssetManager.hpp graphics/Display.cpp graphics/Display.hpp graphics/Camera3D.hpp graphics/Camera3D.cpp - graphics/Shader.hpp graphics/Shader.cpp graphics/Texture.hpp graphics/Texture.cpp) - -add_executable(AltCraft ${SOURCE_FILES}) -target_link_libraries(AltCraft ${PLATFORM_LIBRARIES}) - -add_subdirectory(depedencies) -target_include_directories(AltCraft PUBLIC ./depedencies/include) -target_link_libraries(AltCraft deps) - -#Setup SFML -find_package(SFML 2 COMPONENTS system window graphics network REQUIRED) -if (SFML_FOUND) - target_link_libraries(AltCraft ${SFML_LIBRARIES}) - target_include_directories(AltCraft PUBLIC ${SFML_INCLUDE_DIR}) -else () - message(FATAL_ERROR "SFML not found!") -endif () - -#Setup OpenGL -find_package(OpenGL REQUIRED) -if (OPENGL_FOUND) - target_link_libraries(AltCraft ${OPENGL_LIBRARIES}) - target_include_directories(AltCraft PUBLIC ${OPENGL_INCLUDE_DIRS}) -else () - message(FATAL_ERROR "OpenGL not found!") -endif () - -#Setup Zlib -find_package(ZLIB REQUIRED) -if (ZLIB_FOUND) - target_link_libraries(AltCraft ${ZLIB_LIBRARIES}) - target_include_directories(AltCraft PUBLIC ${ZLIB_INCLUDE_DIR}) -else () - message(FATAL_ERROR "Zlib not found!") -endif () - - -#[[ -#Setup SFML -find_package(SFML 2 COMPONENTS system network graphics window REQUIRED) -include_directories(${SFML_INCLUDE_DIR}) -if (NOT SFML_FOUND) - message(FATAL_ERROR "SFML not found!") -endif () - -#Setup Zlib -find_package(ZLIB REQUIRED) -include_directories(${ZLIB_INCLUDE_DIR}) -if (NOT ZLIB_FOUND) - message(FATAL_ERROR "Zlib not found!") -endif () - -#Setup GLEW -find_package(GLEW REQUIRED) -include_directories(${GLEW_INCLUDE_DIRS}) -if (NOT GLEW_FOUND) - message(FATAL_ERROR "GLEW not found!") -endif () - -#Setup OpenGL -find_package(OpenGL REQUIRED) -include_directories(${OPENGL_INCLUDE_DIRS}) -if (NOT OPENGL_FOUND) - message(FATAL_ERROR "OpenGL not found!") -endif () - -set(SOURCE_FILES main.cpp Field.hpp utility.cpp Packet.hpp FieldParser.hpp Network.hpp Network.cpp NetworkClient.cpp - NetworkClient.hpp json.hpp PacketBuilder.cpp PacketBuilder.hpp Packet.cpp FieldParser.cpp Field.cpp - PacketParser.cpp PacketParser.hpp PositionF.cpp PositionF.hpp PositionI.cpp PositionI.hpp Game.cpp - Game.hpp World.cpp World.hpp Block.cpp Block.hpp Section.cpp Section.hpp Nbt.hpp graphics/AssetManager.cpp - graphics/AssetManager.hpp) - -add_executable(AltCraft ${SOURCE_FILES}) -target_link_libraries(AltCraft ${PLATFORM_LIBRARIES} ${SFML_LIBRARIES} ${ZLIB_LIBRARIES} ${SOIL_LIBRARY} - ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES}) -]]
\ No newline at end of file diff --git a/code/FieldParser.cpp b/code/FieldParser.cpp deleted file mode 100644 index 500a973..0000000 --- a/code/FieldParser.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "FieldParser.hpp" - -Field FieldParser::Parse(FieldType type, byte *data, size_t len) { - switch (type) { - case VarInt: - return ParseVarInt(data, len); - case Boolean: - return ParseBool(data, len); - case String: - return ParseString(data, len); - case Long: - return ParseLong(data, len); - case Int: - return ParseInt(data, len); - case UnsignedByte: - return ParseUByte(data, len); - case Byte8_t: - return ParseByte(data, len); - case Float: - return ParseFloat(data, len); - case Position: - return ParsePosition(data, len); - case Double: - return ParseDouble(data, len); - case ByteArray: - return ParseByteArray(data, len); - default: - throw 105; - } -} - -Field FieldParser::ParseString(byte *data, size_t len) { - Field fLen = ParseVarInt(data, 0); - Field f; - f.SetRaw(data, fLen.GetLength() + fLen.GetVarInt(), String); - return f; -} - -Field FieldParser::ParseBool(byte *data, size_t len) { - Field f; - f.SetRaw(data,1,Boolean); - return f; -} - -Field FieldParser::ParseVarInt(byte *data, size_t len) { - if (len != 0) { - Field f; - f.SetRaw(data, len, VarInt); - return f; - } - int val = VarIntRead(data, len); - Field f; - f.SetVarInt(val); - return f; -} - -Field FieldParser::ParseLong(byte *data, size_t len) { - Field f; - f.SetRaw(data, 8, Long); - return f; -} - -Field FieldParser::ParseInt(byte *data, size_t len) { - Field f; - f.SetRaw(data, 4, Int); - return f; -} - -Field FieldParser::ParseUByte(byte *data, size_t len) { - Field f; - f.SetRaw(data, 1, UnsignedByte); - return f; -} - -Field FieldParser::ParseByte(byte *data, size_t len) { - Field f; - f.SetRaw(data, 1, Byte8_t); - return f; -} - -Field FieldParser::ParseFloat(byte *data, size_t len) { - Field f; - f.SetRaw(data, 4, Float); - return f; -} - -Field FieldParser::ParsePosition(byte *data, size_t len) { - Field f; - f.SetRaw(data, 8, Position); - return f; -} - -Field FieldParser::ParseDouble(byte *data, size_t len) { - Field f; - f.SetRaw(data, 8, Double); - return f; -} - -Field FieldParser::ParseByteArray(byte *data, size_t len) { - if (len == 0) - throw 119; - Field f; - f.SetRaw(data, len, Byte8_t); - //f.SetRaw(data, len, ByteArray); - return f; -} diff --git a/code/FieldParser.hpp b/code/FieldParser.hpp deleted file mode 100644 index 274ab9e..0000000 --- a/code/FieldParser.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "Field.hpp" - -class FieldParser { -public: - static Field ParseVarInt(byte *data, size_t len); - - static Field ParseBool(byte *data, size_t len); - - static Field ParseString(byte *data, size_t len); - - static Field Parse(FieldType type, byte* data, size_t len=0); - - static Field ParseLong(byte *data, size_t len); - - static Field ParseInt(byte *data, size_t len); - - static Field ParseUByte(byte *data, size_t len); - - static Field ParseByte(byte *data, size_t len); - - static Field ParseFloat(byte *data, size_t len); - - static Field ParsePosition(byte *data, size_t len); - - static Field ParseDouble(byte *data, size_t len); - - static Field ParseByteArray(byte *data, size_t len); -};
\ No newline at end of file diff --git a/code/Game.cpp b/code/Game.cpp deleted file mode 100644 index db38977..0000000 --- a/code/Game.cpp +++ /dev/null @@ -1,159 +0,0 @@ -#include "Game.hpp" -#include "PacketParser.hpp" -#include "PacketBuilder.hpp" -#include "json.hpp" - -Game::Game() { - m_display = new Display(1280, 720, "AltCraft", &m_world); - m_nc = new NetworkClient("127.0.0.1", 25565, "HelloOne"); - Packet &response = *m_nc->GetPacket(); - if (response.GetId() != 0x02) { - std::cout << response.GetId() << std::endl; - throw 127; - } - PacketParser::Parse(response, Login); - g_PlayerUuid = response.GetField(0).GetString(); - g_PlayerName = response.GetField(1).GetString(); - delete &response; - m_networkState = ConnectionState::Play; - std::cout << g_PlayerName << "'s UUID is " << g_PlayerUuid << std::endl; -} - -Game::~Game() { - std::cout << "Stopping game thread..." << std::endl; - m_exit=true; - m_gameThread.join(); - std::cout << "Stopping graphics..." << std::endl; - delete m_display; - std::cout << "Stopping network..." << std::endl; - delete m_nc; -} - -void Game::MainLoop() { - while (!m_exit) { - ParsePackets(); - if (m_display->IsClosed()) - m_exit = true; - } -} - -void Game::ParsePackets() { - Packet *packetPtr = m_nc->GetPacket(); - if (!packetPtr) { - using namespace std::chrono_literals; - std::this_thread::sleep_for(16ms); - return; - } - Packet packet = *packetPtr; - delete packetPtr; - PacketParser::Parse(packet); - nlohmann::json json; - - switch (packet.GetId()) { - case 0x23: - g_PlayerEid = packet.GetField(0).GetInt(); - g_Gamemode = (packet.GetField(1).GetUByte() & 0b11111011); - g_Dimension = packet.GetField(2).GetInt(); - g_Difficulty = packet.GetField(3).GetUByte(); - g_MaxPlayers = packet.GetField(4).GetUByte(); - g_LevelType = packet.GetField(5).GetString(); - g_ReducedDebugInfo = packet.GetField(6).GetBool(); - std::cout << "Gamemode is " << (int) g_Gamemode << ", Difficulty is " << (int) g_Difficulty - << ", Level Type is " << g_LevelType << std::endl; - break; - case 0x0D: - g_Difficulty = packet.GetField(0).GetUByte(); - std::cout << "Difficulty now is " << (int) g_Difficulty << std::endl; - break; - case 0x43: - g_SpawnPosition = packet.GetField(0).GetPosition(); - std::cout << "Spawn position is " << g_SpawnPosition.GetX() << "," << g_SpawnPosition.GetY() << "," - << g_SpawnPosition.GetZ() << std::endl; - break; - case 0x2B: - g_PlayerInvulnerable = (packet.GetField(0).GetByte() & 0x01) != 0; - g_PlayerFlying = (packet.GetField(0).GetByte() & 0x02) != 0; - g_PlayerAllowFlying = (packet.GetField(0).GetByte() & 0x04) != 0; - g_PlayerCreativeMode = (packet.GetField(0).GetByte() & 0x08) != 0; - g_PlayerFlyingSpeed = packet.GetField(1).GetFloat(); - g_PlayerFovModifier = packet.GetField(2).GetFloat(); - std::cout << "Fov modifier is " << g_PlayerFovModifier << std::endl; - break; - case 0x2E: - if ((packet.GetField(5).GetByte() & 0x10) != 0) { - g_PlayerPitch += packet.GetField(4).GetFloat(); - } else { - g_PlayerPitch = packet.GetField(4).GetFloat(); - }; - - if ((packet.GetField(5).GetByte() & 0x08) != 0) { - g_PlayerYaw += packet.GetField(3).GetFloat(); - } else { - g_PlayerYaw = packet.GetField(3).GetFloat(); - } - - if ((packet.GetField(5).GetByte() & 0x01) != 0) { - g_PlayerX += packet.GetField(0).GetDouble(); - } else { - g_PlayerX = packet.GetField(0).GetDouble(); - } - - if ((packet.GetField(5).GetByte() & 0x02) != 0) { - g_PlayerY += packet.GetField(1).GetDouble(); - } else { - g_PlayerY = packet.GetField(1).GetDouble(); - } - - if ((packet.GetField(5).GetByte() & 0x04) != 0) { - g_PlayerZ += packet.GetField(2).GetDouble(); - } else { - g_PlayerZ = packet.GetField(2).GetDouble(); - } - - g_IsGameStarted = true; - m_nc->AddPacketToQueue(PacketBuilder::CPlay0x03(0)); - m_nc->AddPacketToQueue(PacketBuilder::CPlay0x00(packet.GetField(6).GetVarInt())); - std::cout << "Game is started! " << std::endl; - std::cout << "PlayerPos is " << g_PlayerX << "," << g_PlayerY << "," << g_PlayerZ << "\tAngle: " << g_PlayerYaw - << "," << g_PlayerPitch << std::endl; - m_display->SetPlayerPos(g_PlayerX, g_PlayerY,g_PlayerZ); - gameStartWaiter.notify_all(); - break; - case 0x1A: - json = nlohmann::json::parse(packet.GetField(0).GetString()); - std::cout << "Disconnect reason: " << json["text"].get<std::string>() << std::endl; - throw 119; - break; - case 0x20: - m_world.ParseChunkData(packet); - break; - case 0x07: - std::cout << "Statistics:" << std::endl; - //int items = packet.GetField(0).GetVarInt(); - for (int i = 0; i < packet.GetField(0).GetVarInt(); i++) { - std::cout << "\t" << packet.GetField(1).GetArray()[0].GetString() << ": " - << packet.GetField(1).GetArray()[1].GetVarInt() << std::endl; - } - break; - default: - //std::cout << std::hex << packet.GetId() << std::dec << std::endl; - break; - } - if (g_IsGameStarted) { - std::chrono::steady_clock clock; - static auto timeOfPreviousSendedPpalPacket(clock.now()); - std::chrono::duration<double, std::milli> delta = clock.now() - timeOfPreviousSendedPpalPacket; - if (delta.count() >= 50) { - m_nc->AddPacketToQueue( - PacketBuilder::CPlay0x0D(g_PlayerX, g_PlayerY, g_PlayerZ, g_PlayerYaw, g_PlayerPitch, true)); - timeOfPreviousSendedPpalPacket = clock.now(); - /*std::cout << "PlayerPos is " << g_PlayerX << "," << g_PlayerY << "," << g_PlayerZ << " " << g_PlayerYaw - << "," << g_PlayerPitch << std::endl;*/ - } - } -} - -void Game::Exec() { - m_gameThread = std::thread(&Game::MainLoop, this); - m_display->MainLoop(); -} diff --git a/code/Nbt.hpp b/code/Nbt.hpp deleted file mode 100644 index 3e43db7..0000000 --- a/code/Nbt.hpp +++ /dev/null @@ -1,516 +0,0 @@ -#pragma once - -#include <cstddef> -#include <vector> -#include <iostream> -#include <zlib.h> -#include <fstream> -#include "utility.h" - -namespace nbt { - enum TagType { - End, //nullptr - Byte, //int8_t - Short, //int16_t - Int, //int32_t - Long, //int64_t - Float, //float - Double, //double - ByteArray, //std::vector<signed char> - String, //std::string - List, //std::vector<NbtTag> - Compound, //std::vector<NbtTag> - IntArray, //std::vector<int32_t> - Unknown, //dummy value - }; - - class NbtTag; - - typedef std::vector<NbtTag> compound_t; - - typedef std::string string_t; - - typedef std::vector<signed char> byteArray_t; - - typedef std::vector<int> intArray_t; - - class NbtTag { - TagType type = Unknown; - string_t name = ""; - unsigned char *data = nullptr; - public: - NbtTag(TagType type, string_t name) : type(type), name(name) { - switch (type) { - case End: - data = nullptr; - break; - case Compound: - data = (unsigned char *) new compound_t; - break; - case String: - data = (unsigned char *) new string_t; - break; - case Int: - data = (unsigned char *) new int32_t; - break; - case Long: - data = (unsigned char *) new int64_t; - break; - case Byte: - data = (unsigned char *) new int8_t; - break; - case Short: - data = (unsigned char *) new int16_t; - break; - case Float: - data = (unsigned char *) new float; - break; - case Double: - data = (unsigned char *) new double; - break; - case ByteArray: - data = (unsigned char *) new byteArray_t; - break; - case List: - data = (unsigned char *) new compound_t; - break; - case IntArray: - data = (unsigned char *) new intArray_t; - } - } - - NbtTag(const NbtTag &other) : type(other.type), name(other.name) { - switch (type) { - case Byte: - data = (unsigned char *) new int8_t; - *((int8_t *) data) = *((int8_t *) other.data); - break; - case Short: - data = (unsigned char *) new int16_t; - *((int16_t *) data) = *((int16_t *) other.data); - break; - case Int: - data = (unsigned char *) new int32_t; - *((int32_t *) data) = *((int32_t *) other.data); - break; - case Long: - data = (unsigned char *) new int64_t; - *((int64_t *) data) = *((int64_t *) other.data); - break; - case Float: - data = (unsigned char *) new float; - *((float *) data) = *((float *) other.data); - break; - case Double: - data = (unsigned char *) new double; - *((double *) data) = *((double *) other.data); - break; - case ByteArray: - data = (unsigned char *) new byteArray_t; - *((std::vector<signed char> *) data) = *((std::vector<signed char> *) other.data); - break; - case String: - data = (unsigned char *) new string_t; - *((std::string *) data) = *((std::string *) other.data); - break; - case List: - data = (unsigned char *) new compound_t; - *((std::vector<NbtTag> *) data) = *((std::vector<NbtTag> *) other.data); - break; - case Compound: - data = (unsigned char *) new compound_t; - *((std::vector<NbtTag> *) data) = *((std::vector<NbtTag> *) other.data); - break; - case IntArray: - data = (unsigned char *) new intArray_t; - *((std::vector<int> *) data) = *((std::vector<int> *) other.data); - break; - } - } - - ~NbtTag() { - switch (type) { - case Byte: - delete ((int8_t *) data); - break; - case Short: - delete ((int16_t *) data); - break; - case Int: - delete ((int32_t *) data); - break; - case Long: - delete ((int64_t *) data); - break; - case Float: - delete ((float *) data); - break; - case Double: - delete ((double *) data); - break; - case ByteArray: - delete ((std::vector<signed char> *) data); - break; - case String: - delete ((std::string *) data); - break; - case List: - delete ((std::vector<NbtTag> *) data); - break; - case Compound: - delete ((std::vector<NbtTag> *) data); - break; - case IntArray: - delete ((std::vector<int> *) data); - break; - } - }; - - void swap(NbtTag &other) { - std::swap(other.data, data); - std::swap(other.name, name); - std::swap(other.type, type); - } - - NbtTag &operator=(NbtTag other) { - other.swap(*this); - return *this; - } - - TagType GetType() const{ - return type; - } - - string_t GetName() const{ - return name; - } - - - string_t &GetString() { - string_t &val = *reinterpret_cast<std::string *>(data); - return val; - } - - compound_t &GetCompound() { - std::vector<NbtTag> &val = *reinterpret_cast<std::vector<NbtTag> *>(data); - return val; - } - - compound_t &GetList() { - std::vector<NbtTag> &val = *reinterpret_cast<std::vector<NbtTag> *>(data); - return val; - } - - int64_t &GetLong() { - int64_t &val = *reinterpret_cast<int64_t *>(data); - return val; - } - - float &GetFloat() { - float &val = *reinterpret_cast<float *>(data); - return val; - } - - double &GetDouble() { - double &val = *reinterpret_cast<double *>(data); - return val; - } - - byteArray_t &GetByteArray() { - auto &val = *reinterpret_cast<byteArray_t *>(data); - return val; - } - - intArray_t &GetIntArray() { - auto &val = *reinterpret_cast<intArray_t *>(data); - return val; - } - - int16_t &GetShort() { - auto &val = *reinterpret_cast<int16_t *>(data); - return val; - } - - int32_t &GetInt() { - auto &val = *reinterpret_cast<int32_t *>(data); - return val; - } - - int8_t &GetByte() { - auto &val = *reinterpret_cast<int8_t *>(data); - return val; - } - }; - - NbtTag ParseTag(unsigned char *data, size_t &size, TagType listItemType = Unknown) { - size = 0; - TagType type; - if (listItemType == Unknown) { - type = (TagType) *data; - data += 1; - size += 1; - } else - type = listItemType; - string_t name; - if (listItemType == Unknown && type != End) { - short nameLen = *((short *) data); - data += 2; - size += 2; - endswap(&nameLen); - name = std::string((char *) data, nameLen); - data += nameLen; - size += nameLen; - } - NbtTag tag(type, name); - switch (type) { - case Compound: { - do { - size_t s; - tag.GetCompound().push_back(ParseTag(data, s)); - data += s; - size += s; - } while (tag.GetCompound().back().GetType() != End); - tag.GetCompound().pop_back(); - return tag; - } - case String: { - short len = *((short *) data); - data += 2; - size += 2; - endswap(&len); - string_t str((char *) data, len); - data += len; - size += len; - tag.GetString() = str; - return tag; - } - case End: - return tag; - case Long: - tag.GetLong() = *((int64_t *) data); - endswap(&tag.GetLong()); - data += 8; - size += 8; - return tag; - case Short: - tag.GetShort() = *((int16_t *) data); - endswap(&tag.GetShort()); - data += 2; - size += 2; - return tag; - case Float: - tag.GetFloat() = *((float *) data); - endswap(&tag.GetFloat()); - data += 4; - size += 4; - return tag; - case Double: - tag.GetDouble() = *((double *) data); - endswap(&tag.GetDouble()); - data += 8; - size += 8; - return tag; - case Byte: - tag.GetByte() = *((int8_t *) data); - endswap(&tag.GetByte()); - data += 1; - size += 1; - return tag; - case Int: - tag.GetInt() = *((int32_t *) data); - endswap(&tag.GetInt()); - data += 4; - size += 4; - return tag; - case List: { - TagType listType = *((TagType *) data); - data += 1; - size += 1; - int32_t listLength = *((int32_t *) data); - endswap(&listLength); - data += 4; - size += 4; - for (int i = 0; i < listLength; i++) { - size_t s = 0; - std::vector<NbtTag> &vec = tag.GetCompound(); - vec.push_back(ParseTag(data, s, listType)); - data += s; - size += s; - } - return tag; - } - case ByteArray: { - int32_t arrLength = *((int32_t *) data); - endswap(&arrLength); - data += 4; - size += 4; - for (int i = 0; i < arrLength; i++) { - signed char val = (signed char) data[i]; - std::vector<signed char> &vec = tag.GetByteArray(); - vec.push_back(val); - } - data += arrLength; - size += arrLength; - return tag; - } - default: - throw 13; - } - } - - NbtTag ParseTag(unsigned char *data, size_t *optionalSize = nullptr) { - size_t s = 0; - size_t &size = (optionalSize ? *optionalSize : s); - return ParseTag(data, size); - } - - std::vector<unsigned char> Decompress(unsigned char *data, size_t dataLen) { - const size_t decompBuffSize = 1024 * 16; - unsigned char *decompBuff = new unsigned char[decompBuffSize]; - std::vector<unsigned char> uncompressed; - for (int i = 0; i < decompBuffSize; i++) - decompBuff[i] = 0; - - - z_stream stream; - stream.zalloc = Z_NULL; - stream.zfree = Z_NULL; - stream.opaque = Z_NULL; - stream.next_in = data; - stream.avail_in = dataLen; - stream.next_out = decompBuff; - stream.avail_out = decompBuffSize; - - if (inflateInit2(&stream, 15 + 32) != Z_OK) { - delete[] decompBuff; - throw 171; - } - - int res; - do { - stream.avail_out = decompBuffSize; - - switch ((res = inflate(&stream, Z_NO_FLUSH))) { - case Z_MEM_ERROR: - throw 172; - case Z_DATA_ERROR: - throw 173; - case Z_NEED_DICT: - throw 174; - } - - uncompressed.resize(uncompressed.size() + decompBuffSize); - std::copy(decompBuff, decompBuff + decompBuffSize, uncompressed.end() - decompBuffSize); - } while (stream.avail_out == 0); - if (res != Z_STREAM_END) - throw 175; - if (inflateEnd(&stream) != Z_OK) - throw 176; - delete[] decompBuff; - return uncompressed; - } - - NbtTag ParseCompressed(unsigned char *data, size_t dataLen) { - auto uncompressed = Decompress(data, dataLen); - NbtTag root = ParseTag(uncompressed.data()); - return root; - } - - NbtTag Parse(unsigned char *data, size_t dataLen) { - bool isCompressed = *data!=10; - if (isCompressed) - return ParseCompressed(data,dataLen); - else - return ParseTag(data); - } - - void PrintTree(NbtTag &tree, int deepness = 0, std::ostream &ostream = std::cout) { - ostream << std::string(deepness, '\t') << "Tag "; - switch (tree.GetType()) { - case Byte: - ostream << "byte"; - break; - case Short: - ostream << "short"; - break; - case Int: - ostream << "int"; - break; - case Long: - ostream << "long"; - break; - case Float: - ostream << "float"; - break; - case Double: - ostream << "double"; - break; - case ByteArray: - ostream << "byte array"; - break; - case String: - ostream << "string"; - break; - case List: - ostream << "list"; - break; - case Compound: - ostream << "compound"; - break; - case IntArray: - ostream << "int array"; - break; - case End: - ostream << "end"; - break; - } - if (tree.GetName().length() > 0) - ostream << " (" << tree.GetName() << ")"; - ostream << ": "; - - if (tree.GetType() == Compound || tree.GetType() == List) { - std::vector<NbtTag> &vec = (tree.GetType() == Compound ? tree.GetCompound() : tree.GetList()); - ostream << vec.size() << " entries {" << std::endl; - for (auto it = vec.begin(); it != vec.end(); ++it) { - PrintTree(*it, deepness + 1, std::cout); - } - ostream << std::string(deepness, '\t') << "}" << std::endl; - return; - } else { - switch (tree.GetType()) { - case Int: - ostream << tree.GetInt(); - break; - case String: - ostream << "\"" << tree.GetString() << "\""; - break; - case Double: - ostream << tree.GetDouble(); - break; - case Float: - ostream << tree.GetFloat(); - break; - case Short: - ostream << tree.GetShort(); - break; - case Byte: - ostream << (int) tree.GetByte(); - break; - case Long: - ostream << tree.GetLong(); - break; - case ByteArray: - ostream << "[" << tree.GetByteArray().size() << " bytes]: "; - for (int i = 0; i < (tree.GetByteArray().size() > 10 ? 10 : tree.GetByteArray().size()); i++) { - ostream << std::hex << "0x" << (tree.GetByteArray()[i] > 15 ? "" : "0") - << (int) tree.GetByteArray()[i] - << std::dec << " "; - } - break; - case IntArray: - break; - } - ostream << std::endl; - } - } -}
\ No newline at end of file diff --git a/code/NetworkClient.hpp b/code/NetworkClient.hpp deleted file mode 100644 index a41b5f4..0000000 --- a/code/NetworkClient.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include <queue> -#include <thread> -#include <mutex> -#include "Network.hpp" - -struct ServerInfo{ - std::string version; - int protocol = 0; - int players_max = 0; - int players_online = 0; - std::vector<std::pair<std::string, std::string>> players; - std::string description; - double ping = 0; - std::string favicon; - std::string json; -}; -class NetworkClient { -public: - NetworkClient(std::string address, unsigned short port, std::string username); - ~NetworkClient(); - - void Update(); - - void MainLoop(); - - Packet * GetPacket(); - void AddPacketToQueue(Packet packet); - - static ServerInfo ServerPing(std::string address,unsigned short port); -private: - std::mutex m_updateMutex; - std::thread m_networkThread; - bool isContinue=true; - NetworkClient (const NetworkClient&); - NetworkClient&operator=(const NetworkClient&); - Network m_network; - std::queue <Packet> m_received; - std::queue <Packet> m_toSend; -}; - diff --git a/code/Packet.cpp b/code/Packet.cpp deleted file mode 100644 index 695e371..0000000 --- a/code/Packet.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "Packet.hpp" - -Packet::Packet(int id) { - Field fLen; - fLen.SetVarInt(0); - m_fields.push_back(fLen); - Field fId; - fId.SetVarInt(id); - m_fields.push_back(fId); -} - -Packet Packet::operator=(Packet other) { - other.swap(*this); - return *this; -} - -void Packet::swap(Packet &other) { - std::swap(m_fields, other.m_fields); - std::swap(m_data, other.m_data); - std::swap(m_parsePtr, other.m_parsePtr); - std::swap(m_dataLength, other.m_dataLength); -} - -void Packet::CopyToBuff(byte *ptr) { - m_fields[0].SetVarInt(GetLength() - m_fields[0].GetLength()); - for (int i = 0; i < m_fields.size(); i++) { - m_fields[i].CopyToBuff(ptr); - ptr += m_fields[i].GetLength(); - } -} - -void Packet::ParseField(FieldType type, size_t len) { - if (type == ByteArray && len == 0) - throw 118; - Field f = FieldParser::Parse(type, m_parsePtr, len); - m_fields.push_back(f); - m_parsePtr += f.GetLength(); - if (m_parsePtr == m_data + m_dataLength) { - delete[] m_data; - m_data = nullptr; - m_dataLength = 0; - m_parsePtr = nullptr; - } -} - -Packet::Packet(byte *data) { - Field fLen = FieldParser::Parse(VarInt, data); - data += fLen.GetLength(); - Field fId = FieldParser::Parse(VarInt, data); - data += fId.GetLength(); - m_dataLength = fLen.GetVarInt() - fId.GetLength(); - m_data = new byte[m_dataLength]; - std::copy(data,data+m_dataLength,m_data); - m_parsePtr = m_data; - m_fields.push_back(fLen); - m_fields.push_back(fId); -} - -Field &Packet::GetField(int id) { - if (id < -2 || id >= m_fields.size() - 2) - throw 111; - return m_fields[id + 2]; -} - -size_t Packet::GetLength() { - size_t len = 0; - for (int i = 0; i < m_fields.size(); i++) - len += m_fields[i].GetLength(); - return len + m_dataLength; -} - -void Packet::AddField(Field field) { - m_fields.push_back(field); -} - -int Packet::GetId() { - return m_fields[1].GetVarInt(); -} - -Packet::Packet(const Packet &other) { - if (other.m_dataLength > 0) { - m_dataLength = other.m_dataLength; - m_data = new byte[m_dataLength]; - m_parsePtr = m_data + (other.m_data - other.m_parsePtr); - std::copy(other.m_data, other.m_data + m_dataLength, m_data); - } - m_fields = other.m_fields; -} - -void Packet::ParseFieldArray(Field &field, FieldType type, size_t len) { - Field f = FieldParser::Parse(type, m_parsePtr, len); - field.Attach(f); - m_parsePtr += f.GetLength(); - if (m_parsePtr == m_data + m_dataLength) { - delete[] m_data; - m_data = nullptr; - m_dataLength = 0; - m_parsePtr = nullptr; - } -} diff --git a/code/Packet.hpp b/code/Packet.hpp deleted file mode 100644 index 67e95e5..0000000 --- a/code/Packet.hpp +++ /dev/null @@ -1,130 +0,0 @@ -#pragma once - -#include <vector> -#include "Field.hpp" -#include "FieldParser.hpp" - -enum ConnectionState { - Login, - Handshaking, - Play, - Status, -}; - -enum PacketsClientBound{ - SpawnObject=0x00, - SpawnExperienceOrb, - SpawnGlobalEntity, - SpawnMob, - SpawnPainting, - SpawnPlayer, - Animation, - Statistics, - BlockBreakAnimation, - UpdateBlockEntity, - BlockAction, - BlockChange, - BossBar, - ServerDifficulty, - Tab, - ChatMessage, - MultiBlockChange, - ConfirmTransaction, - CloseWindow, - OpenWindow, - WindowItems, - WindowProperty, - SetSlot, - SetCooldown, - PluginMessage, - NamedSoundEffect, - Disconnect, - EntityStatus, - Explosion, - UnloadChunk, - ChangeGameState, - KeepAlive, - ChunkData, - Effect, - Particle, - JoinGame, - Map, - EntityRelativeMove, - EntityLookAndRelativeMove, - EntityLook, - Entity, - VehicleMove, - OpenSignEditor, - PlayerAbilities, - CombatEvent, - PlayerListItem, - PlayerPositionAndLook, - UseBed, - DestroyEntities, - RemoveEntityEffect, - ResourcePackSend, - Respawn, - EntityHeadLook, - WorldBorder, - Camera, - HeldItemChange, - DisplayScoreboard, - EntityMetadata_, - AttachEntity, - EntityVelocity, - EntityEquipment, - SetExperience, - UpdateHealth, - ScoreboardObjective, - SetPassengers, - Teams, - UpdateScore, - SpawnPosition, - TimeUpdate, - Title, - SoundEffect, - PlayerListHeaderAndFooter, - CollectItem, - EntityTeleport, - EntityProperties, - EntityEffect, -}; - -class Packet { -public: - Packet(int id); - - Packet(byte *data); - - Packet(const Packet &other); - - ~Packet() { - delete[] m_data; - } - - int GetId(); - - void AddField(Field field); - - void ParseField(FieldType type, size_t len = 0); - - void ParseFieldArray(Field &field, FieldType type, size_t len); - - Field & GetField(int id); - - size_t GetLength(); - - void CopyToBuff(byte *ptr); - - void swap(Packet &other); - - Packet operator=(Packet other); - -private: - Packet(); - - std::vector<Field> m_fields; - byte *m_data = nullptr; - byte *m_parsePtr = nullptr; - size_t m_dataLength = 0; -};
\ No newline at end of file diff --git a/code/PacketBuilder.cpp b/code/PacketBuilder.cpp deleted file mode 100644 index 4083ea3..0000000 --- a/code/PacketBuilder.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "PacketBuilder.hpp" - -Packet PacketBuilder::CHandshaking0x00(int protocolVerison, std::string address, unsigned short port, int nextState) { - Packet handshakePacket(0); - Field fProtocol; - fProtocol.SetVarInt(protocolVerison); - Field fAddress; - fAddress.SetString(address); - Field fPort; - fPort.SetUShort(port); - Field fNextState; - fNextState.SetVarInt(nextState); - handshakePacket.AddField(fProtocol); - handshakePacket.AddField(fAddress); - handshakePacket.AddField(fPort); - handshakePacket.AddField(fNextState); - return handshakePacket; -} - -Packet PacketBuilder::CPlay0x0B(int keepAliveId) { - Packet keepAlivePacket(0x0B); - Field fKeepAlive; - fKeepAlive.SetVarInt(keepAliveId); - keepAlivePacket.AddField(fKeepAlive); - return keepAlivePacket; -} - -Packet PacketBuilder::CPlay0x03(int actionId) { - Packet clientStatusPacket(0x03); - Field fActionId; - fActionId.SetVarInt(actionId); - clientStatusPacket.AddField(fActionId); - return clientStatusPacket; -} - -Packet PacketBuilder::CPlay0x00(int teleportId) { - Packet teleportConfirmPacket(0x00); - Field fTeleportId; - fTeleportId.SetVarInt(teleportId); - teleportConfirmPacket.AddField(fTeleportId); - return teleportConfirmPacket; -} - -Packet PacketBuilder::CPlay0x0D(double x, double y, double z, float yaw, float pitch, bool onGround) { - Packet playerPositionAndLookPacket(0x0D); - Field fX; - Field fY; - Field fZ; - Field fYaw; - Field fPitch; - Field fOnGround; - fX.SetDouble(x); - fY.SetDouble(y); - fZ.SetDouble(z); - fYaw.SetFloat(yaw); - fPitch.SetFloat(pitch); - fOnGround.SetBool(onGround); - playerPositionAndLookPacket.AddField(fX); - playerPositionAndLookPacket.AddField(fY); - playerPositionAndLookPacket.AddField(fZ); - playerPositionAndLookPacket.AddField(fYaw); - playerPositionAndLookPacket.AddField(fPitch); - playerPositionAndLookPacket.AddField(fOnGround); - return playerPositionAndLookPacket; -} diff --git a/code/PacketBuilder.hpp b/code/PacketBuilder.hpp deleted file mode 100644 index 2fcb737..0000000 --- a/code/PacketBuilder.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - - -#include "Packet.hpp" - -class PacketBuilder { -public: - static Packet CHandshaking0x00(int protocolVerison, std::string address, unsigned short port, int nextState); - static Packet CPlay0x0B(int keepAliveId); - - static Packet CPlay0x03(int actionId); - - static Packet CPlay0x00(int teleportId); - - static Packet CPlay0x0D(double x, double y, double z, float yaw, float pitch, bool onGround); -}; - diff --git a/code/PacketParser.cpp b/code/PacketParser.cpp deleted file mode 100644 index 488c812..0000000 --- a/code/PacketParser.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include "PacketParser.hpp" - -void PacketParser::Parse(Packet &packet, ConnectionState state, bool ClientBound) { - if (ClientBound) { - switch (state) { - case Login: - ParseLogin(packet); - break; - case Handshaking: - break; - case Play: - ParsePlay(packet); - break; - case Status: - - break; - } - } else { - ParseServerBound(packet, state); - } -} - -void PacketParser::ParseServerBound(Packet &packet, ConnectionState state) { - throw 107; -} - -void PacketParser::ParseLogin(Packet &packet) { - switch (packet.GetId()) { - case 0x00: - ParseLogin0x00(packet); - break; - case 0x02: - ParseLogin0x02(packet); - break; - default: - { - int i = packet.GetId(); - //throw 112; - } - } -} - -void PacketParser::ParsePlay(Packet &packet) { - switch (packet.GetId()) { - case 0x23: - ParsePlay0x23(packet); - break; - case 0x1F: - ParsePlay0x1F(packet); - break; - case 0x0D: - ParsePlay0x0D(packet); - break; - case 0x2B: - ParsePlay0x2B(packet); - break; - case 0x43: - ParsePlay0x43(packet); - break; - case 0x2E: - ParsePlay0x2E(packet); - break; - case 0x1A: - ParsePlay0x1A(packet); - break; - case 0x20: - ParsePlay0x20(packet); - break; - case 0x07: - ParsePlay0x07(packet); - default: - //throw 113; - break; - } -} - -void PacketParser::ParseLogin0x00(Packet &packet) { - packet.ParseField(String); -} - -void PacketParser::ParseLogin0x02(Packet &packet) { - packet.ParseField(String); - packet.ParseField(String); -} - -void PacketParser::ParsePlay0x23(Packet &packet) { - packet.ParseField(Int); - packet.ParseField(UnsignedByte); - packet.ParseField(Int); - packet.ParseField(UnsignedByte); - packet.ParseField(UnsignedByte); - packet.ParseField(String); - packet.ParseField(Boolean); -} - -void PacketParser::ParsePlay0x1F(Packet &packet) { - packet.ParseField(VarInt); -} - -void PacketParser::ParsePlay0x0D(Packet &packet) { - packet.ParseField(UnsignedByte); -} - -void PacketParser::ParsePlay0x2B(Packet &packet) { - packet.ParseField(Byte8_t); - packet.ParseField(Float); - packet.ParseField(Float); -} - -void PacketParser::ParsePlay0x43(Packet &packet) { - packet.ParseField(Position); -} - -void PacketParser::ParsePlay0x2E(Packet &packet) { - packet.ParseField(Double); - packet.ParseField(Double); - packet.ParseField(Double); - packet.ParseField(Float); - packet.ParseField(Float); - packet.ParseField(Byte8_t); - packet.ParseField(VarInt); -} - -void PacketParser::ParsePlay0x1A(Packet &packet) { - packet.ParseField(String); -} - -void PacketParser::ParsePlay0x20(Packet &packet) { - packet.ParseField(Int); - packet.ParseField(Int); - packet.ParseField(Boolean); - packet.ParseField(VarInt); - packet.ParseField(VarInt); - packet.ParseField(ByteArray, packet.GetField(4).GetVarInt()); - packet.ParseField(VarInt); - //packet.ParseField(NbtTag); - //packet.GetField(7).SetArray(packet.GetField(6).GetVarInt()); -} - -void PacketParser::ParsePlay0x07(Packet &packet) { - packet.ParseField(VarInt); - packet.AddField(Field()); - for (int i=0;i<packet.GetField(0).GetVarInt();i++){ - packet.ParseFieldArray(packet.GetField(1), String, 0); - packet.ParseFieldArray(packet.GetField(1), VarInt, 0); - } -} diff --git a/code/PacketParser.hpp b/code/PacketParser.hpp deleted file mode 100644 index 8ca6195..0000000 --- a/code/PacketParser.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - - -#include "Packet.hpp" - -class PacketParser { -public: - static void Parse(Packet &packet, ConnectionState state = Play, bool ClientBound = true); - - static void ParseServerBound(Packet &packet, ConnectionState state); - - static void ParseLogin(Packet &packet); - - static void ParsePlay(Packet &packet); - - static void ParseLogin0x00(Packet &packet); - - static void ParseLogin0x02(Packet &packet); - - static void ParsePlay0x23(Packet &packet); - - static void ParsePlay0x1F(Packet &packet); - - static void ParsePlay0x0D(Packet &packet); - - static void ParsePlay0x2B(Packet &packet); - - static void ParsePlay0x43(Packet &packet); - - static void ParsePlay0x2E(Packet &packet); - - static void ParsePlay0x1A(Packet &packet); - - static void ParsePlay0x20(Packet &packet); - - static void ParsePlay0x07(Packet &packet); -}; - diff --git a/code/PositionF.cpp b/code/PositionF.cpp deleted file mode 100644 index 8502b91..0000000 --- a/code/PositionF.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "PositionF.hpp" - -PositionF::PositionF(double x, double z, double y) : m_x(x), m_y(y), m_z(z) { - -} - -PositionF::~PositionF() { - -} - -double PositionF::GetX() const { - return m_x; -} - -double PositionF::GetY() const { - return m_y; -} - -double PositionF::GetZ() const { - return m_z; -} - -void PositionF::SetX(double x) { - m_x = x; -} - -void PositionF::SetY(double y) { - m_y = y; -} - -void PositionF::setZ(double z) { - m_z = z; -} - -bool PositionF::operator==(const PositionF &other) const { - return other.m_x == m_x && other.m_z == m_z && other.m_y == other.m_y; -} - -PositionF &PositionF::operator=(const PositionF &other) { - m_y = other.m_y; - m_z = other.m_z; - m_x = other.m_x; - return *this; -} - -PositionF::PositionF(const PositionF &other) { - m_y = other.m_y; - m_z = other.m_z; - m_x = other.m_x; -} diff --git a/code/PositionF.hpp b/code/PositionF.hpp deleted file mode 100644 index b90a2a1..0000000 --- a/code/PositionF.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -class PositionF { -public: - PositionF(double x, double z, double y); - - ~PositionF(); - - double GetX() const; - - double GetY() const; - - double GetZ() const; - - void SetX(double x); - - void SetY(double y); - - void setZ(double z); - - bool operator==(const PositionF &other) const; - - PositionF &operator=(const PositionF &other); - - PositionF(const PositionF &other); - -private: - double m_x, m_y, m_z; -};
\ No newline at end of file diff --git a/code/PositionI.cpp b/code/PositionI.cpp deleted file mode 100644 index 7de5dc0..0000000 --- a/code/PositionI.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include <cmath> -#include "PositionI.hpp" - -PositionI::PositionI(int x, int z, int y) : m_x(x), m_y(y), m_z(z) { - -} - -PositionI::~PositionI() { - -} - -int PositionI::GetX() const { - return m_x; -} - -int PositionI::GetY() const { - return m_y; -} - -int PositionI::GetZ() const { - return m_z; -} - -void PositionI::SetX(int x) { - m_x = x; -} - -void PositionI::SetY(int y) { - m_y = y; -} - -void PositionI::setZ(int z) { - m_z = z; -} - -bool PositionI::operator==(const PositionI &other) const { - return other.m_x == m_x && other.m_z == m_z && other.m_y == other.m_y; -} - -PositionI &PositionI::operator=(const PositionI &other) { - m_y = other.m_y; - m_z = other.m_z; - m_x = other.m_x; - return *this; -} - -PositionI::PositionI(const PositionI &other) { - m_y = other.m_y; - m_z = other.m_z; - m_x = other.m_x; -} - -PositionI::PositionI() : m_x(0), m_y(0), m_z(0) { - -} - -bool PositionI::operator<(const PositionI &rhs) const { - if (m_x < rhs.m_x) - return true; - if (rhs.m_x < m_x) - return false; - if (m_y < rhs.m_y) - return true; - if (rhs.m_y < m_y) - return false; - return m_z < rhs.m_z; -} - -bool PositionI::operator>(const PositionI &rhs) const { - return rhs < *this; -} - -bool PositionI::operator<=(const PositionI &rhs) const { - return !(rhs < *this); -} - -bool PositionI::operator>=(const PositionI &rhs) const { - return !(*this < rhs); -} - -PositionI PositionI::operator-(const PositionI &other) const { - return PositionI( - m_x - other.m_x, - m_z - other.m_z, - m_y - other.m_y - ); -} - -double PositionI::GetDistance() { - return (std::sqrt(std::pow(m_x, 2) + std::pow(m_y, 2) + std::pow(m_z, 2))); -} - -PositionI PositionI::operator*(int other) const { - return PositionI( - m_x * other, - m_z * other, - m_y * other - ); -} - -PositionI PositionI::operator*(const PositionI &other) const { - return PositionI( - m_x * other.m_x, - m_z * other.m_z, - m_y * other.m_y - ); -} - -PositionI PositionI::operator/(int other) const { - return PositionI( - m_x / other, - m_z / other, - m_y / other - ); -} - -std::ostream &operator<<(std::ostream &os, const PositionI &i) { - os << "(" << i.m_x << ", " << i.m_y << ", " << i.m_z << ")"; - return os; -} diff --git a/code/PositionI.hpp b/code/PositionI.hpp deleted file mode 100644 index 25455ef..0000000 --- a/code/PositionI.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include <ostream> - -class PositionI { -public: - PositionI(int x, int z, int y); - - PositionI(); - - ~PositionI(); - - int GetX() const; - - int GetY() const; - - int GetZ() const; - - void SetX(int x); - - void SetY(int y); - - void setZ(int z); - - double GetDistance(); - - bool operator==(const PositionI &other) const; - - PositionI &operator=(const PositionI &other); - - PositionI(const PositionI &other); - - bool operator<(const PositionI &rhs) const; - - bool operator>(const PositionI &rhs) const; - - bool operator<=(const PositionI &rhs) const; - - bool operator>=(const PositionI &rhs) const; - - PositionI operator-(const PositionI &other)const; - PositionI operator*(int other)const; - PositionI operator*(const PositionI &other)const; - PositionI operator/(int other)const; - - friend std::ostream &operator<<(std::ostream &os, const PositionI &i); - -private: - int m_x, m_y, m_z; -};
\ No newline at end of file diff --git a/code/graphics/AssetManager.hpp b/code/graphics/AssetManager.hpp deleted file mode 100644 index c7ef81a..0000000 --- a/code/graphics/AssetManager.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include <fstream> -#include <string> -#include <map> -#include "../json.hpp" -#include "Texture.hpp" - -struct Asset { - std::string name = ""; - std::string hash = ""; - union AssetData{ - Texture *texture; - } data; - size_t size = 0; - enum AssetType { - Unknown, - Texture, - Sound, - Model, - Lang, - } type = Unknown; - bool isParsed(); - ~Asset(); -}; - -class AssetManager { - AssetManager(); - - ~AssetManager(); - - AssetManager(const AssetManager &); - - AssetManager &operator=(const AssetManager &); - - std::map<std::string, Asset> assets; - - static AssetManager &instance() { - static AssetManager assetManager; - return assetManager; - } - - static std::string GetPathToAsset(std::string AssetName); -public: - - static Asset &GetAsset(std::string AssetName); - - static void LoadAsset(std::string AssetName); - - static std::string GetAssetNameByBlockId(unsigned short id); -}; - diff --git a/code/graphics/Display.cpp b/code/graphics/Display.cpp deleted file mode 100644 index a68c35b..0000000 --- a/code/graphics/Display.cpp +++ /dev/null @@ -1,268 +0,0 @@ -#include <iomanip> -#include "Display.hpp" -#include "AssetManager.hpp" - -Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winTitle, World *worldPtr) : world( - worldPtr) { - sf::ContextSettings contextSetting; - contextSetting.majorVersion = 3; - contextSetting.minorVersion = 3; - contextSetting.attributeFlags = contextSetting.Core; - contextSetting.depthBits = 24; - window = new sf::Window(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Default, contextSetting); - window->setVerticalSyncEnabled(true); - window->setMouseCursorVisible(false); - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window); - - //Glew - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) { - std::cout << "Failed to initialize GLEW" << std::endl; - throw 3; - } - glViewport(0, 0, width(), height()); - glEnable(GL_DEPTH_TEST); -} - -bool Display::IsClosed() { - return !window->isOpen(); -} - -void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { - camera.Position = glm::vec3(playerX, playerY, playerZ); - const int ChunkDistance = 1; - PositionI playerChunk = PositionI((int) playerX / 16, (int) playerZ / 16, (int) playerY / 16); - /*std::cout << "Player chunk position: " << playerChunk.GetX() << " " - << playerChunk.GetZ() << " " << playerChunk.GetY() << std::endl;*/ - for (auto &it:world->m_sections) { - PositionI chunkPosition = it.first; - PositionI delta = chunkPosition - playerChunk; - if (delta.GetDistance() > ChunkDistance) - continue; - /*std::cout << "Rendering " << delta.GetDistance() << " Detailed: " << delta.GetX() << " " << delta.GetZ() << " " - << delta.GetY() << std::endl << - "\t" << chunkPosition.GetX() << " " << chunkPosition.GetZ() << " " - << chunkPosition.GetY() << std::endl;*/ - toRender.push_back(it.first); - } - std::cout << "Chunks to render: " << toRender.size() << std::endl; -} - -void Display::MainLoop() { - Shader shader("./shaders/simple.vs", "./shaders/simple.fs"); - - GLfloat vertices[] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f - }; - GLuint indices[] = { - 0, 1, 2, - 0, 2, 3 - }; - GLuint VBO, VAO, EBO; - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - glGenVertexArrays(1, &VAO); - - glBindVertexArray(VAO); - { - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(0); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *) (3 * sizeof(GLfloat))); - glEnableVertexAttribArray(2); - } - glBindVertexArray(0); - - shader.Use(); - - bool captureMouse = true; - - bool isRunning = true; - while (isRunning) { - static sf::Clock clock, clock1; - float deltaTime = clock.getElapsedTime().asSeconds(); - float absTime = clock1.getElapsedTime().asSeconds(); - clock.restart(); - sf::Event event; - while (window->pollEvent(event)) { - switch (event.type) { - case sf::Event::Closed: - window->close(); - isRunning = false; - break; - case sf::Event::Resized: - glViewport(0, 0, width(), height()); - break; - case sf::Event::KeyPressed: - switch (event.key.code) { - case sf::Keyboard::Escape: - isRunning = false; - break; - case sf::Keyboard::T: - captureMouse = !captureMouse; - window->setMouseCursorVisible(!captureMouse); - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), - *window); - break; - case sf::Keyboard::R: - shader.Reload(); - break; - default: - break; - } - case sf::Event::MouseWheelScrolled: - camera.ProcessMouseScroll(event.mouseWheelScroll.delta); - break; - default: - break; - } - } - std::ostringstream toWindow; - glm::highp_vec3 cameraPosition(camera.Position); - toWindow << std::setprecision(2) << std::fixed << "Pos: " << cameraPosition.x << ", " << cameraPosition.y - << ", " << cameraPosition.z << "; "; - toWindow << "FPS: " << (1.0f / deltaTime) << " "; - window->setTitle(toWindow.str()); - if (captureMouse) { - sf::Vector2i mousePos = sf::Mouse::getPosition(*window); - sf::Vector2i center = sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2); - sf::Mouse::setPosition(center, *window); - int deltaX = (mousePos - center).x, deltaY = (center - mousePos).y; - camera.ProcessMouseMovement(deltaX, deltaY); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) - camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) - camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) - camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) - camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime); - - - //Render code - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader.Use(); - - 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 timeLoc = glGetUniformLocation(shader.Program, "time"); - glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f); - glm::mat4 view = camera.GetViewMatrix(); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glUniform1f(timeLoc, absTime); - - glBindVertexArray(VAO); - /*for (GLuint i = 0; i < 10; i++) { - glm::mat4 model; - glm::vec3 cubePositions[] = { - glm::vec3(0, 0, 0), - glm::vec3(0, 0, 1), - glm::vec3(0, 0, 2), - glm::vec3(1, 0, 0), - glm::vec3(1, 0, 1), - glm::vec3(1, 0, 2), - glm::vec3(2, 0, 0), - glm::vec3(2, 0, 1), - glm::vec3(2, 0, 2), - glm::vec3(3, 0, 3), - }; - if (toRender.size()<1) - continue; - model = glm::translate(model, - glm::vec3(toRender[0].GetX() * 16, toRender[0].GetZ() * 16, - toRender[0].GetY() * 16)); - model = glm::translate(model, cubePositions[i]); - - GLfloat angle = 20.0f * (i); - //model = glm::rotate(model, glm::radians(angle * absTime), glm::vec3(1.0f, 0.3f, 0.5f)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - - glDrawArrays(GL_TRIANGLES, 0, 36); - }*/ - - for (auto §ionPos:toRender) { - Section §ion = world->m_sections[sectionPos]; - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - for (int x = 0; x < 16; x++) { - glm::mat4 model; - model = glm::translate(model, - glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, - sectionPos.GetZ() * 16)); - model = glm::translate(model, glm::vec3(x, y, z)); - - Block block = section.GetBlock(PositionI(x, z, y)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform1i(blockLoc, block.id); - - std::string textureName = AssetManager::GetAssetNameByBlockId(block.id); - if (textureName.find("air") != std::string::npos) - continue; - Texture &texture1 = *(AssetManager::GetAsset(textureName).data.texture); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader.Program, "blockTexture"), 0); - - glDrawArrays(GL_TRIANGLES, 0, 36); - } - } - } - } - glBindVertexArray(0); - - //End of render code - - window->display(); - } - -} diff --git a/code/main.cpp b/code/main.cpp deleted file mode 100644 index a88baf9..0000000 --- a/code/main.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include <iostream> -#include "Game.hpp" -#include "graphics/AssetManager.hpp" - -int main() { - - Game game; - game.Exec(); - return 0; -}
\ No newline at end of file diff --git a/code/shaders/simple.fs b/code/shaders/simple.fs deleted file mode 100644 index 2d2a582..0000000 --- a/code/shaders/simple.fs +++ /dev/null @@ -1,22 +0,0 @@ -#version 330 core -in vec2 TexCoord; - -out vec4 color; - -uniform sampler2D blockTexture; -uniform int block; -uniform float time; - -void main() -{ - //color = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.0); - /*if (block==1) - color = vec4(0.2,0.2,0.2,1); - else if (block==0) - color = vec4(0,0,0,1); - else - color = vec4(1,1,1,1);*/ - color = texture(blockTexture,TexCoord); - //color = vec4(TexCoord.x,TexCoord.y,0,1); -} - diff --git a/code/shaders/simple.vs b/code/shaders/simple.vs deleted file mode 100644 index 7233efc..0000000 --- a/code/shaders/simple.vs +++ /dev/null @@ -1,15 +0,0 @@ -#version 330 core -layout (location = 0) in vec3 position; -layout (location = 2) in vec2 texCoord; - -out vec2 TexCoord; - -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; - -void main() -{ - gl_Position = projection * view * model * vec4(position, 1.0f); - TexCoord = vec2(texCoord.x, texCoord.y); -}
\ No newline at end of file diff --git a/code/Game.hpp b/src/gamestate/Game.hpp index b9096a5..adf9059 100644 --- a/code/Game.hpp +++ b/src/gamestate/Game.hpp @@ -1,9 +1,9 @@ #pragma once -#include "PositionI.hpp" -#include "NetworkClient.hpp" -#include "World.hpp" -#include "graphics/Display.hpp" +#include "../utility/Vector.hpp" +#include "../network/NetworkClient.hpp" +#include "../world/World.hpp" +#include "../graphics/Display.hpp" class Game { public: @@ -41,7 +41,7 @@ private: byte g_MaxPlayers; std::string g_LevelType; bool g_ReducedDebugInfo; - PositionI g_SpawnPosition; + Vector g_SpawnPosition; bool g_PlayerInvulnerable; bool g_PlayerFlying; bool g_PlayerAllowFlying; diff --git a/code/graphics/AssetManager.cpp b/src/graphics/AssetManager.cpp index 1840c63..f2f756d 100644 --- a/code/graphics/AssetManager.cpp +++ b/src/graphics/AssetManager.cpp @@ -4,7 +4,6 @@ const std::string pathToAssets = "./assets/"; const std::string pathToObjects = pathToAssets + "objects/"; const std::string pathToIndexFile = pathToAssets + "indexes/1.11.json"; const std::string pathToAssetsMc = "./assetsMc/"; - const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{ std::make_pair(Asset::AssetType::Texture, ".png"), std::make_pair(Asset::AssetType::Lang, ".lang"), @@ -12,6 +11,7 @@ const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{ }; AssetManager::AssetManager() { + return; std::ifstream indexFile(pathToIndexFile); if (!indexFile) { std::cerr << "Can't open file " << pathToIndexFile << std::endl; diff --git a/code/graphics/Camera3D.cpp b/src/graphics/Camera3D.cpp index eb740e4..eb740e4 100644 --- a/code/graphics/Camera3D.cpp +++ b/src/graphics/Camera3D.cpp diff --git a/code/graphics/Camera3D.hpp b/src/graphics/Camera3D.hpp index eac1f47..eac1f47 100644 --- a/code/graphics/Camera3D.hpp +++ b/src/graphics/Camera3D.hpp diff --git a/code/graphics/Display.hpp b/src/graphics/Display.hpp index 314ef5d..7458bcd 100644 --- a/code/graphics/Display.hpp +++ b/src/graphics/Display.hpp @@ -1,7 +1,7 @@ #pragma once #include <SFML/Window.hpp> -#include "../World.hpp" +#include "../world/World.hpp" #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> @@ -12,7 +12,7 @@ class Display { sf::Window *window; World* world; - std::vector<PositionI> toRender; + std::vector<Vector> toRender; Camera3D camera; public: Display(unsigned int winWidth, unsigned int winHeight, const char winTitle[9], World *worldPtr); diff --git a/code/graphics/Shader.cpp b/src/graphics/Shader.cpp index c84e169..c84e169 100644 --- a/code/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp diff --git a/code/graphics/Shader.hpp b/src/graphics/Shader.hpp index 8178d2a..8178d2a 100644 --- a/code/graphics/Shader.hpp +++ b/src/graphics/Shader.hpp diff --git a/code/graphics/Texture.cpp b/src/graphics/Texture.cpp index 0104530..0104530 100644 --- a/code/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp diff --git a/code/graphics/Texture.hpp b/src/graphics/Texture.hpp index 8e3f1af..8e3f1af 100644 --- a/code/graphics/Texture.hpp +++ b/src/graphics/Texture.hpp diff --git a/code/Network.cpp b/src/network/Network.cpp index d979037..a3023ff 100644 --- a/code/Network.cpp +++ b/src/network/Network.cpp @@ -1,6 +1,6 @@ #include <iostream> #include "Network.hpp" -#include "PacketBuilder.hpp" +#include "../packet/PacketBuilder.hpp" Network::Network(std::string address, unsigned short port) : m_address(address), m_port(port) { std::cout << "Connecting to server " << m_address << ":" << m_port << std::endl; diff --git a/code/Network.hpp b/src/network/Network.hpp index 74df92c..ea9042c 100644 --- a/code/Network.hpp +++ b/src/network/Network.hpp @@ -2,7 +2,7 @@ #include <string> #include <SFML/Network.hpp> -#include "Packet.hpp" +#include "../packet/Packet.hpp" class Network { diff --git a/code/NetworkClient.cpp b/src/network/NetworkClient.cpp index d6835e0..cbe705b 100644 --- a/code/NetworkClient.cpp +++ b/src/network/NetworkClient.cpp @@ -1,7 +1,7 @@ #include "NetworkClient.hpp" -#include "PacketParser.hpp" -#include "PacketBuilder.hpp" -#include "json.hpp" +#include "../packet/PacketParser.hpp" +#include "../packet/PacketBuilder.hpp" +#include <nlohmann/json.hpp> ServerInfo NetworkClient::ServerPing(std::string address, unsigned short port) { ServerInfo info; diff --git a/code/Field.cpp b/src/packet/Field.cpp index c95c32d..69402f1 100644 --- a/code/Field.cpp +++ b/src/packet/Field.cpp @@ -200,7 +200,7 @@ void Field::SetFloat(float value) { *p = value; } -PositionI Field::GetPosition() { +Vector Field::GetPosition() { unsigned long long t = *reinterpret_cast<unsigned long long *>(m_data); endswap(&t); int x = t >> 38; @@ -215,14 +215,11 @@ PositionI Field::GetPosition() { if (z >= pow(2, 25)) { z -= pow(2, 26); } - PositionI val; - val.SetX(x); - val.setZ(z); - val.SetY(y); + Vector val(x,y,z); return val; } -void Field::SetPosition(PositionI value) { +void Field::SetPosition(Vector value) { Clear(); m_type = Position; m_dataLength = 8; diff --git a/code/Field.hpp b/src/packet/Field.hpp index 43769dc..8be9c9b 100644 --- a/code/Field.hpp +++ b/src/packet/Field.hpp @@ -4,8 +4,8 @@ #include <cstdint> #include <string> #include <vector> -#include "utility.h" -#include "PositionI.hpp" +#include "../utility/utility.h" +#include "../utility/Vector.hpp" typedef unsigned char byte; typedef signed char sbyte; @@ -101,9 +101,9 @@ public: void SetFloat(float value); - PositionI GetPosition(); + Vector GetPosition(); - void SetPosition(PositionI value); + void SetPosition(Vector value); double GetDouble(); diff --git a/code/utility.cpp b/src/utility/utility.cpp index aa50e9f..aa50e9f 100644 --- a/code/utility.cpp +++ b/src/utility/utility.cpp diff --git a/code/utility.h b/src/utility/utility.h index 32120cb..32120cb 100644 --- a/code/utility.h +++ b/src/utility/utility.h diff --git a/code/Section.cpp b/src/world/Section.cpp index 8df5953..f53c987 100644 --- a/code/Section.cpp +++ b/src/world/Section.cpp @@ -19,9 +19,6 @@ Section::Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byt m_bitsPerBlock = bitsPerBlock; } -Section::Section() { -} - Section::~Section() { delete[] m_dataBlocks; m_dataBlocksLen = 0; @@ -32,7 +29,7 @@ Section::~Section() { m_dataSkyLight = nullptr; } -Block &Section::GetBlock(PositionI pos) { +Block &Section::GetBlock(Vector pos) { if (m_dataBlocks != nullptr) { std::mutex parseMutex; std::unique_lock<std::mutex> parseLocker(parseMutex); diff --git a/code/Section.hpp b/src/world/Section.hpp index 8e1a0d1..3065cbd 100644 --- a/code/Section.hpp +++ b/src/world/Section.hpp @@ -4,7 +4,7 @@ #include <map> #include <condition_variable> #include "Block.hpp" -#include "Field.hpp" +#include "../packet/Field.hpp" const int SECTION_WIDTH = 16; const int SECTION_LENGTH = 16; @@ -19,17 +19,18 @@ class Section { byte m_bitsPerBlock = 0; std::vector<Block> m_blocks; std::condition_variable parseWaiter; + + Section(); + public: void Parse(); Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, std::vector<unsigned short> palette); - Section(); - ~Section(); - Block &GetBlock(PositionI pos); + Block &GetBlock(Vector pos); Section &operator=(Section other); diff --git a/code/World.cpp b/src/world/World.cpp index c19f88c..adbb3e1 100644 --- a/code/World.cpp +++ b/src/world/World.cpp @@ -21,13 +21,23 @@ void World::ParseChunkData(Packet packet) { for (int i = 0; i < 16; i++) { if (bitmask[i]) { size_t len = 0; - m_sections[PositionI(chunkX, chunkZ, i)] = ParseSection(content, len); - m_sectionToParse.push(m_sections.find(PositionI(chunkX, chunkZ, i))); - m_parseSectionWaiter.notify_one(); + 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) { diff --git a/code/World.hpp b/src/world/World.hpp index 1f35585..7b7ea60 100644 --- a/code/World.hpp +++ b/src/world/World.hpp @@ -6,7 +6,7 @@ #include <condition_variable> #include <queue> #include "Block.hpp" -#include "Packet.hpp" +#include "../packet/Packet.hpp" #include "Section.hpp" class World { @@ -17,7 +17,7 @@ class World { std::mutex m_parseSectionMutex; std::condition_variable m_parseSectionWaiter; std::thread m_sectionParseThread; - std::queue<std::map<PositionI,Section>::iterator> m_sectionToParse; + std::queue<std::map<Vector,Section>::iterator> m_sectionToParse; //utility methods void SectionParsingThread(); //game vars @@ -28,5 +28,5 @@ public: World(); ~World(); void ParseChunkData(Packet packet); - std::map<PositionI, Section> m_sections; + std::map<Vector, Section> m_sections; };
\ No newline at end of file |