summaryrefslogtreecommitdiffstats
path: root/src/world
diff options
context:
space:
mode:
Diffstat (limited to 'src/world')
-rw-r--r--src/world/Block.hpp15
-rw-r--r--src/world/Collision.hpp8
-rw-r--r--src/world/GameState.cpp2
-rw-r--r--src/world/GameState.hpp71
-rw-r--r--src/world/Section.cpp55
-rw-r--r--src/world/Section.hpp51
-rw-r--r--src/world/World.hpp38
7 files changed, 213 insertions, 27 deletions
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 <GameState.hpp>
+#include <world/GameState.hpp>
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 <nlohmann/json.hpp>
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+
+#include <world/World.hpp>
+#include <network/NetworkClient.hpp>
+#include <Vector.hpp>
+
+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<unsigned short> 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<double, std::milli> 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<double, std::milli> time = end - begin;
+ totalParsingTime += time.count();
+ }
std::vector<byte> 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<std::string>{}(str);
+ if (m_blocks.empty()) return 0;
+
+ unsigned char *from = reinterpret_cast<unsigned char *>(m_blocks.data());
+ size_t length = m_blocks.size() * sizeof(Block);
+
+ std::string str(from, from + length);
+ return std::hash<std::string>{}(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 <vector>
+#include <map>
+#include <condition_variable>
+#include <functional>
+
+#include <easylogging++.h>
+
+#include <world/Block.hpp>
+#include <Vector.hpp>
+#include <Utility.hpp>
+
+const int SECTION_WIDTH = 16;
+const int SECTION_LENGTH = 16;
+const int SECTION_HEIGHT = 16;
+
+class Section {
+ std::vector<unsigned short> m_palette;
+ byte *m_dataBlocks = nullptr;
+ size_t m_dataBlocksLen;
+ byte *m_dataLight = nullptr;
+ byte *m_dataSkyLight = nullptr;
+ byte m_bitsPerBlock = 0;
+ std::vector<Block> 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<unsigned short> 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 <map>
+#include <bitset>
+
+#include <easylogging++.h>
+
+#include <world/Block.hpp>
+#include <world/Section.hpp>
+#include <network/Packet.hpp>
+#include <world/Collision.hpp>
+
+class World {
+ std::map<Vector, Section> sections;
+ std::map<Vector, std::mutex> 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<PacketChunkData> packet);
+
+ bool isPlayerCollides(double X, double Y, double Z);
+
+ Block &GetBlock(Vector pos);
+
+ std::vector<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