From 6a00886804c53883d919f008f6ec47a574d86607 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 24 Jul 2017 19:52:24 +0500 Subject: 2017-07-24 --- src/world/Block.hpp | 15 +++++++++++ src/world/Collision.hpp | 8 ++++++ src/world/GameState.cpp | 2 +- src/world/GameState.hpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ src/world/Section.cpp | 55 ++++++++++++++++++++------------------ src/world/Section.hpp | 51 +++++++++++++++++++++++++++++++++++ src/world/World.hpp | 38 ++++++++++++++++++++++++++ 7 files changed, 213 insertions(+), 27 deletions(-) create mode 100644 src/world/Block.hpp create mode 100644 src/world/Collision.hpp create mode 100644 src/world/GameState.hpp create mode 100644 src/world/Section.hpp create mode 100644 src/world/World.hpp (limited to 'src/world') diff --git a/src/world/Block.hpp b/src/world/Block.hpp new file mode 100644 index 0000000..2f823fe --- /dev/null +++ b/src/world/Block.hpp @@ -0,0 +1,15 @@ +#pragma once + +struct Block { + Block(); + + Block(unsigned short id, unsigned char state); + + ~Block(); + + 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/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/GameState.cpp b/src/world/GameState.cpp index 79e2f1b..d3a6bd3 100644 --- a/src/world/GameState.cpp +++ b/src/world/GameState.cpp @@ -1,4 +1,4 @@ -#include +#include GameState::GameState(NetworkClient *Net, bool &quit) : nc(Net), isRunning(quit) { Front = glm::vec3(0.0f, 0.0f, -1.0f); diff --git a/src/world/GameState.hpp b/src/world/GameState.hpp new file mode 100644 index 0000000..6741882 --- /dev/null +++ b/src/world/GameState.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +class GameState { + NetworkClient *nc; +public: + GameState(NetworkClient *NetClient, bool &quit); + + void Update(float deltaTime); + + //Navigation + enum Direction { + FORWARD, BACKWARD, LEFT, RIGHT, JUMP + }; + void HandleMovement(GameState::Direction direction, float deltaTime); + void HandleRotation(double yaw, double pitch); + glm::mat4 GetViewMatrix(); + void updateCameraVectors(); + + float Yaw(); + float Pitch(); + void SetYaw(float yaw); + void SetPitch(float pitch); + + glm::vec3 Position(); + void SetPosition(glm::vec3 Position); + glm::vec3 Front; + glm::vec3 Up; + glm::vec3 Right; + glm::vec3 WorldUp; + + //Everything other + World world; + bool &isRunning; + + std::string g_PlayerUuid; + std::string g_PlayerName; + bool g_IsGameStarted; + int g_PlayerEid; + int g_Gamemode; + int g_Dimension; + byte g_Difficulty; + byte g_MaxPlayers; + std::string g_LevelType; + bool g_ReducedDebugInfo; + Vector g_SpawnPosition; + bool g_PlayerInvulnerable; + bool g_PlayerFlying; + bool g_PlayerAllowFlying; + bool g_PlayerCreativeMode; + float g_PlayerFlyingSpeed; + float g_PlayerFovModifier; + float g_PlayerPitch; + float g_PlayerYaw; + double g_PlayerX; + double g_PlayerY; + double g_PlayerZ; + float g_PlayerHealth; + + bool g_OnGround = true; + double g_PlayerVelocityX = 0; + double g_PlayerVelocityY = 0; + double g_PlayerVelocityZ = 0; +}; diff --git a/src/world/Section.cpp b/src/world/Section.cpp index 279d2b2..ff2a4fb 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -47,28 +47,28 @@ void Section::Parse() { endswap(&longArray[i]); std::vector blocks; blocks.reserve(4096); - { - auto begin = std::chrono::steady_clock::now(); - 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; - } - } - } - auto end = std::chrono::steady_clock::now(); - std::chrono::duration time = end - begin; - totalParsingTime += time.count(); - } + { + auto begin = std::chrono::steady_clock::now(); + 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; + } + } + } + auto end = std::chrono::steady_clock::now(); + std::chrono::duration time = end - begin; + totalParsingTime += time.count(); + } std::vector light; light.reserve(4096); for (int i = 0; i < 2048; i++) { @@ -137,8 +137,11 @@ Vector Section::GetPosition() { } size_t Section::GetHash() { - if (m_blocks.empty()) - return 0; - std::string str((unsigned char*)m_blocks.data(), (unsigned char*)m_blocks.data() + m_blocks.size() * sizeof(Block)); - return std::hash{}(str); + if (m_blocks.empty()) return 0; + + unsigned char *from = reinterpret_cast(m_blocks.data()); + size_t length = m_blocks.size() * sizeof(Block); + + std::string str(from, from + length); + return std::hash{}(str); } \ No newline at end of file diff --git a/src/world/Section.hpp b/src/world/Section.hpp new file mode 100644 index 0000000..2df0cfe --- /dev/null +++ b/src/world/Section.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include +#include + +#include + +#include +#include +#include + +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; + + Section(); + + Vector worldPosition; + +public: + void Parse(); + + Section(Vector position, byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, + std::vector palette); + + ~Section(); + + Block &GetBlock(Vector pos); + + Section &operator=(Section other); + + friend void swap(Section &a, Section &b); + + Section(const Section &other); + + Vector GetPosition(); + + size_t GetHash(); +}; \ No newline at end of file diff --git a/src/world/World.hpp b/src/world/World.hpp new file mode 100644 index 0000000..6b09f1f --- /dev/null +++ b/src/world/World.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include + +#include +#include +#include +#include + +class World { + std::map sections; + std::map sectionMutexes; + int dimension = 0; + + Section ParseSection(StreamInput *data, Vector position); + + World(const World &other); + World &operator=(const World &other); +public: + World(); + + ~World(); + + void ParseChunkData(std::shared_ptr packet); + + bool isPlayerCollides(double X, double Y, double Z); + + Block &GetBlock(Vector pos); + + std::vector GetSectionsList(); + + Section &GetSection(Vector sectionPos); + + glm::vec3 Raycast(glm::vec3 position, glm::vec3 direction, float maxLength = 1000.0f, float minPrecision = 0.01f); +}; \ No newline at end of file -- cgit v1.2.3