diff --git a/SQCSim-common/SQCSim-common.vcxproj b/SQCSim-common/SQCSim-common.vcxproj
index 15265fb..991cd1d 100644
--- a/SQCSim-common/SQCSim-common.vcxproj
+++ b/SQCSim-common/SQCSim-common.vcxproj
@@ -138,6 +138,7 @@
+
@@ -148,6 +149,7 @@
+
diff --git a/SQCSim-common/SQCSim-common.vcxproj.filters b/SQCSim-common/SQCSim-common.vcxproj.filters
index a98dd18..26c6c05 100644
--- a/SQCSim-common/SQCSim-common.vcxproj.filters
+++ b/SQCSim-common/SQCSim-common.vcxproj.filters
@@ -51,6 +51,9 @@
Fichiers d%27en-tête
+
+ Fichiers d%27en-tête
+
@@ -74,5 +77,8 @@
Fichiers sources
+
+ Fichiers sources
+
\ No newline at end of file
diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp
index 52824d5..5c4e6c4 100644
--- a/SQCSim-common/bullet.cpp
+++ b/SQCSim-common/bullet.cpp
@@ -1,10 +1,9 @@
#include "bullet.h"
#include "world.h"
-Bullet::Bullet(Player& player) {
- m_startpos = m_currentpos = player.GetPOV() + player.GetDirection();
- m_velocity = player.GetDirection();
-}
+Bullet::Bullet(Vector3f pos, Vector3f dir) : m_startpos(pos), m_currentpos(pos), m_velocity(dir) {}
+
+Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_currentpos(pos), m_velocity(dir), m_tid(tid) {}
Bullet::~Bullet() {}
@@ -31,6 +30,10 @@ void Bullet::Transpose(int& x, int& z) {
m_startpos.z -= z * CHUNK_SIZE_Z;
}
-Vector3f& Bullet::getPos() {
+Vector3f Bullet::getPos() {
return m_currentpos;
}
+
+uint64_t Bullet::getTeamID(){
+ return m_tid;
+}
diff --git a/SQCSim-common/bullet.h b/SQCSim-common/bullet.h
index 599eab6..e3ae989 100644
--- a/SQCSim-common/bullet.h
+++ b/SQCSim-common/bullet.h
@@ -1,23 +1,27 @@
#ifndef BULLET_H__
#define BULLET_H__
-#include "player.h"
+#include "define.h"
+#include "vector3.h"
class World;
class Bullet {
public:
- Bullet(Player& player);
+ Bullet(Vector3f pos, Vector3f dir);
+ Bullet(Vector3f pos, Vector3f dir, uint64_t tid);
~Bullet();
bool Update(World* world, float elapsedtime);
void Transpose(int& x, int& z);
- Vector3f& getPos();
+ Vector3f getPos();
+ uint64_t getTeamID();
private:
- Vector3f m_startpos;
- Vector3f m_currentpos;
- Vector3f m_velocity;
+ Vector3f m_startpos,
+ m_currentpos,
+ m_velocity;
+ uint64_t m_tid = 0;
};
#endif // BULLET_H__
diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp
index 7bfc690..cb93218 100644
--- a/SQCSim-common/chunk.cpp
+++ b/SQCSim-common/chunk.cpp
@@ -1,13 +1,13 @@
#include "chunk.h"
#include "world.h"
-Chunk::Chunk(unsigned int x, unsigned int y) : m_posX(x), m_posY(y) {
+Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y) {
//std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position.
//pos << CHUNK_PATH << x << '_' << y << ".chunk";
//std::ifstream input(pos.str(), std::fstream::binary);
//if (input.fail()) {
- OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(SEED);
+ OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(seed);
m_blocks.Reset(BTYPE_AIR);
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Montagnes
@@ -98,14 +98,45 @@ Chunk::~Chunk() {
void Chunk::RemoveBlock(int x, int y, int z, World* world) {
m_blocks.Set(x, y, z, BTYPE_AIR);
+ CheckNeighbors(x, y, world);
+ m_isDirty = true;
}
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
m_blocks.Set(x, y, z, type);
+ if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas vérifier les chunks voisines.
+ m_isDirty = true;
}
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
+void Chunk::CheckNeighbors(unsigned int x, unsigned int z, World* world) {
+ unsigned int cx, cy;
+
+ world->GetScope(cx, cy);
+
+ if (x == 0 && m_posX - cx >= 0 &&
+ world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z))
+ world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty();
+ else if (x == CHUNK_SIZE_X - 1 && m_posX - cx < WORLD_SIZE_X &&
+ world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z))
+ world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty();
+
+ if (z == 0 && m_posY - cy >= 0 &&
+ world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z))
+ world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z)->MakeDirty();
+ else if (z == CHUNK_SIZE_X - 1 && m_posY - cy < WORLD_SIZE_Y &&
+ world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z))
+ world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z)->MakeDirty();
+}
+
void Chunk::GetPosition(unsigned int& x, unsigned int& y) const { x = m_posX; y = m_posY; }
+bool Chunk::IsDirty() const { return m_isDirty; }
+
+void Chunk::MakeDirty() { m_isDirty = true; }
+
+void Chunk::MakeClean() { m_isDirty = false; }
+
void Chunk::MakeModified() { m_isModified = true; }
+
diff --git a/SQCSim-common/chunk.h b/SQCSim-common/chunk.h
index ec015ba..b053928 100644
--- a/SQCSim-common/chunk.h
+++ b/SQCSim-common/chunk.h
@@ -1,30 +1,36 @@
#ifndef CHUNK_H__
#define CHUNK_H__
+
#include "define.h"
-#include "array3d.h"
#include "array2d.h"
+#include "array3d.h"
#include "blockinfo.h"
#include "opensimplex.h"
class World;
class Chunk {
- protected:
+ private:
Array3d m_blocks = Array3d(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z);
+ bool m_isDirty = true;
bool m_isModified = false;
unsigned int m_posX; // Position du chunk dans l'array constituant le monde.
unsigned int m_posY;
public:
- Chunk(unsigned int x, unsigned int y);
+ Chunk(unsigned int x, unsigned int y, int64_t seed);
~Chunk();
void RemoveBlock(int x, int y, int z, World* world);
void SetBlock(int x, int y, int z, BlockType type, World* world);
BlockType GetBlock(int x, int y, int z);
+ void CheckNeighbors(unsigned int x, unsigned int z, World* world);
void GetPosition(unsigned int& x, unsigned int& y) const;
+ bool IsDirty() const;
+ void MakeDirty();
+ void MakeClean();
void MakeModified();
};
diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h
index 0342359..f02d719 100644
--- a/SQCSim-common/define.h
+++ b/SQCSim-common/define.h
@@ -17,7 +17,15 @@
#define WORLD_SIZE_X 64
#define WORLD_SIZE_Y 64
-#define VIEW_DISTANCE 512
+#define FRAMES_RENDER_CHUNKS 1
+#define FRAMES_UPDATE_CHUNKS 1
+#define FRAMES_DELETE_CHUNKS 1
+
+#define THREADS_GENERATE_CHUNKS 8
+#define THREADS_UPDATE_CHUNKS 3
+#define THREADS_DELETE_CHUNKS 3
+
+#define VIEW_DISTANCE 512 // Si les chunks arrêtent de s'afficher pendant une game et qu'il y a un access violation quand tu quitte, il faut augmenter ce chiffre.
#define TEXTURE_SIZE 512
#define MAX_BULLETS 512
diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp
index b692201..d4edca2 100644
--- a/SQCSim-common/netprotocol.cpp
+++ b/SQCSim-common/netprotocol.cpp
@@ -1,7 +1,64 @@
#include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
+ *buf[0] = netprot::PACKET_TYPE::INPUT;
+ uint64_t time = in->timestamp;
+ uint8_t time8[sizeof(uint64_t)] = {(time >> 56) & 0xFF,
+ (time >> 48) & 0xFF,
+ (time >> 40) & 0xFF,
+ (time >> 32) & 0xFF,
+ (time >> 24) & 0xFF,
+ (time >> 16) & 0xFF,
+ (time >> 8 ) & 0xFF,
+ time & 0xFF};
+
+ memcpy(*buf + 1, time8, sizeof(uint64_t));
+
+ uint64_t sid = in->sid;
+ uint8_t sid8[sizeof(uint64_t)] = {(sid >> 56) & 0xFF,
+ (sid >> 48) & 0xFF,
+ (sid >> 40) & 0xFF,
+ (sid >> 32) & 0xFF,
+ (sid >> 24) & 0xFF,
+ (sid >> 16) & 0xFF,
+ (sid >> 8 ) & 0xFF,
+ sid & 0xFF};
+
+ memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
+
+ Keys keys = in->keys;
+ uint8_t keys8 = // Reste un bit.
+ keys.forward & 0b10000000 |
+ keys.backward & 0b01000000 |
+ keys.left & 0b00100000 |
+ keys.right & 0b00010000 |
+ keys.jump & 0b00001000 |
+ keys.shoot & 0b00000100 |
+ keys.block & 0b00000010 ;
+
+ memcpy(*buf + sizeof(uint64_t) + 2, &keys8, sizeof(uint8_t));
+
+ uint32_t vec[3];
+ memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour dénaturer les floats.
+
+ uint8_t vec8[3 * sizeof(uint32_t)] = {
+ (vec[0] >> 24) & 0xFF,
+ (vec[0] >> 16) & 0xFF,
+ (vec[0] >> 8) & 0xFF,
+ vec[0] & 0xFF,
+ (vec[1] >> 24) & 0xFF,
+ (vec[1] >> 16) & 0xFF,
+ (vec[1] >> 8) & 0xFF,
+ vec[1] & 0xFF,
+ (vec[2] >> 24) & 0xFF,
+ (vec[2] >> 16) & 0xFF,
+ (vec[2] >> 8) & 0xFF,
+ vec[2] & 0xFF};
+
+ memcpy(*buf + sizeof(uint64_t) + 3, vec8, sizeof(uint32_t) * 3);
+
+ *buflen = sizeof(uint64_t) + 3 + sizeof(uint32_t) * 3;
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h
index 7f93349..d7c7a93 100644
--- a/SQCSim-common/netprotocol.h
+++ b/SQCSim-common/netprotocol.h
@@ -1,5 +1,6 @@
#ifndef NETPROTOCOL_H__
#define NETPROTOCOL_H__
+
#include "define.h"
#include
#include "vector3.h"
@@ -24,7 +25,8 @@ namespace netprot {
left,
right,
jump,
- shoot;
+ shoot,
+ block;
};
struct States {
diff --git a/SQCSim-common/player.cpp b/SQCSim-common/player.cpp
index b42968a..2043dd9 100644
--- a/SQCSim-common/player.cpp
+++ b/SQCSim-common/player.cpp
@@ -4,6 +4,8 @@
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) {
m_velocity = Vector3f(0, 0, 0);
m_airborne = true;
+ m_hp = 0.75f; //TODO: Remettre à 1.0f
+ m_username = "Zelda Bee-Bop";
}
void Player::TurnLeftRight(float value) {
@@ -64,7 +66,8 @@ Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jum
return delta;
}
-void Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
+Player::Sound Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
+ Player::Sound snd = Player::Sound::NOSOUND;
static float timing = 0.f;
/* Gestion de collisions */
BlockType bt1, bt2, bt3;
@@ -85,6 +88,7 @@ void Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
if (bt3 != BTYPE_AIR) {
m_velocity.y = 0;
if (timing == 0.f) {
+ if (m_airborne) snd = Player::Sound::FALL;
timing = .3f;
}
m_airborne = false;
@@ -137,10 +141,10 @@ void Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
m_velocity.x += input.x * 2.f * elapsedTime;
m_velocity.z += input.z * 2.f * elapsedTime;
- if (input.x == 0.f)
+ if (input.x == 0.f)
m_velocity.x *= .8f;
-
- if (input.z == 0.f)
+
+ if (input.z == 0.f)
m_velocity.z *= .8f;
}
else {
@@ -160,6 +164,30 @@ void Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
m_velocity.y = vy;
m_position += m_velocity;
+
+ static float bobbingtime = 0; // Gestion de la caméra
+ static bool leftright = false;
+ static bool isStep = false;
+ if (bobbingtime <= 360.f)
+ bobbingtime += elapsedTime * 20.f; else bobbingtime = 0;
+
+ if ((sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) < -.2f && !m_airborne) {
+ if (!isStep) {
+ snd = Player::Sound::STEP;
+ }
+ isStep = true;
+ }
+ else isStep = false;
+ m_POV = m_position.y;
+ m_POV += m_airborne ? 0 : (sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) * .2f;
+
+ return snd;
+}
+
+void Player::ApplyTransformation(Transformation& transformation, bool rel) const {
+ transformation.ApplyRotation(-m_rotX, 1, 0, 0);
+ transformation.ApplyRotation(-m_rotY, 0, 1, 0);
+ if (rel) transformation.ApplyTranslation(-GetPOV());
}
Vector3f Player::GetPosition() const { return Vector3f(m_position.x + CHUNK_SIZE_X * WORLD_SIZE_X / 2, m_position.y, m_position.z + CHUNK_SIZE_Z * WORLD_SIZE_Y / 2); }
@@ -170,6 +198,10 @@ Vector3f Player::GetPOV() const { return Vector3f(GetPosition().x, m_POV, GetPos
Vector3f Player::GetDirection() const { return m_direction; }
+std::string Player::GetUsername() const { return m_username; }
+
+float Player::GetHP() const { return m_hp; }
+
void Player::Teleport(int& x, int& z) {
m_position.x -= x * CHUNK_SIZE_X;
m_position.z -= z * CHUNK_SIZE_Z;
diff --git a/SQCSim-common/player.h b/SQCSim-common/player.h
index f889b68..e439fa2 100644
--- a/SQCSim-common/player.h
+++ b/SQCSim-common/player.h
@@ -1,33 +1,44 @@
-#ifndef _PLAYER_H__
-#define _PLAYER_H__
-#include "vector3.h"
-#include
+#ifndef PLAYER_H__
+#define PLAYER_H__
-class World;
+#include
+#include "transformation.h"
+#include "vector3.h"
+
+class World;
class Player {
public:
+ enum Sound { NOSOUND, STEP, FALL };
+
Player(const Vector3f& position, float rotX = 0, float rotY = 0);
void TurnLeftRight(float value);
void TurnTopBottom(float value);
Vector3f GetInput(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime);
- void ApplyPhysics(Vector3f input, World* world, float elapsedTime);
+ Sound ApplyPhysics(Vector3f input, World* world, float elapsedTime);
+ void ApplyTransformation(Transformation& transformation, bool rel = true) const;
Vector3f GetPosition() const;
Vector3f GetDirection() const;
Vector3f GetVelocity() const;
Vector3f GetPOV() const;
+ std::string GetUsername() const;
+ float GetHP() const;
void Teleport(int& x, int& z);
-protected:
+private:
Vector3f m_position;
Vector3f m_velocity;
Vector3f m_direction;
+ std::string m_username;
+
float m_rotX = 0;
float m_rotY = 0;
float m_POV;
+ float m_hp;
+
bool m_airborne;
};
#endif //_PLAYER_H__
diff --git a/SQCSim2021/transformation.cpp b/SQCSim-common/transformation.cpp
similarity index 91%
rename from SQCSim2021/transformation.cpp
rename to SQCSim-common/transformation.cpp
index 7629f52..c4169a3 100644
--- a/SQCSim2021/transformation.cpp
+++ b/SQCSim-common/transformation.cpp
@@ -51,10 +51,10 @@ void Transformation::ApplyScale(const Vector3f& v)
ApplyScale(v.x, v.y, v.z);
}
-void Transformation::Use() const
-{
- glLoadMatrixf(m_stack.top().GetInternalValues());
-}
+//void Transformation::Use() const
+//{
+// glLoadMatrixf(m_stack.top().GetInternalValues());
+//}
const Matrix4f& Transformation::GetMatrix() const
{
diff --git a/SQCSim2021/transformation.h b/SQCSim-common/transformation.h
similarity index 87%
rename from SQCSim2021/transformation.h
rename to SQCSim-common/transformation.h
index 72e0690..f6ac105 100644
--- a/SQCSim2021/transformation.h
+++ b/SQCSim-common/transformation.h
@@ -2,9 +2,9 @@
#define TRANSFORMATION_H__
#include
-#include "../SQCSim-common/matrix4.h"
-#include "../SQCSim-common/vector3.h"
#include "define.h"
+#include "matrix4.h"
+#include "vector3.h"
class Transformation
{
@@ -25,7 +25,7 @@ class Transformation
void ApplyScale(float x, float y, float z);
void ApplyScale(const Vector3f& v);
- void Use() const;
+ //void Use() const;
const Matrix4f& GetMatrix() const;
diff --git a/SQCSim-common/world.cpp b/SQCSim-common/world.cpp
index 6b4ccf6..4d7c292 100644
--- a/SQCSim-common/world.cpp
+++ b/SQCSim-common/world.cpp
@@ -6,6 +6,10 @@ World::~World() {}
Array2d& World::GetChunks() { return m_chunks; }
+void World::SetSeed(uint64_t seed) {
+ m_seed = seed;
+}
+
Chunk* World::ChunkAt(float x, float y, float z) const {
int cx = (int)x / CHUNK_SIZE_X;
int cz = (int)z / CHUNK_SIZE_Z;
@@ -38,23 +42,112 @@ BlockType World::BlockAt(const Vector3f& pos, BlockType defaultBlockType) const
return BlockAt(pos.x, pos.y, pos.z, defaultBlockType);
}
+void World::TransposeWorld(Vector3f& player, Bullet* bullets[MAX_BULLETS]) {
+ int x = 0, y = 0;
+
+ if (player.x > (WORLD_SIZE_X * CHUNK_SIZE_X) * .6f) ++x;
+ else if (player.x < (WORLD_SIZE_X * CHUNK_SIZE_X) * .4f) --x;
+ if (player.z > (WORLD_SIZE_Y * CHUNK_SIZE_Z) * .6f) ++y;
+ else if (player.z < (WORLD_SIZE_Y * CHUNK_SIZE_Z) * .4f) --y;
+
+ if (!x && !y) return;
+
+ if (x > 0) {
+ for (int ax = 0; ax < WORLD_SIZE_X; ++ax)
+ for (int ay = 0; ay < WORLD_SIZE_Y; ++ay)
+ if (ax - x >= 0) {
+ m_chunks.Set(ax - x, ay,
+ m_chunks.Remove(ax, ay));
+ if (ax == WORLD_SIZE_X - 1 && m_chunks.Get(ax - x, ay))
+ m_chunks.Get(ax - x, ay)->MakeDirty();
+ }
+ else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay));
+ }
+ else if (x < 0) {
+ for (int ax = WORLD_SIZE_X - 1; ax >= 0; --ax)
+ for (int ay = WORLD_SIZE_Y - 1; ay >= 0; --ay)
+ if (ax - x < WORLD_SIZE_X) {
+ m_chunks.Set(ax - x, ay,
+ m_chunks.Remove(ax, ay));
+ if (ax == 0 && m_chunks.Get(ax - x, ay))
+ m_chunks.Get(ax - x, ay)->MakeDirty();
+ }
+ else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay));
+ }
+
+ if (y > 0) {
+ for (int ax = 0; ax < WORLD_SIZE_X; ++ax)
+ for (int ay = 0; ay < WORLD_SIZE_Y; ++ay)
+ if (ay - y >= 0) {
+ m_chunks.Set(ax, ay - y,
+ m_chunks.Remove(ax, ay));
+ if (ay == WORLD_SIZE_Y - 1 && m_chunks.Get(ax, ay - y))
+ m_chunks.Get(ax, ay - y)->MakeDirty();
+ }
+ else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay));
+ }
+ else if (y < 0) {
+ for (int ax = WORLD_SIZE_X - 1; ax >= 0; --ax)
+ for (int ay = WORLD_SIZE_Y - 1; ay >= 0; --ay)
+ if (ay - y < WORLD_SIZE_Y) {
+ m_chunks.Set(ax, ay - y,
+ m_chunks.Remove(ax, ay));
+ if (ay == 0 && m_chunks.Get(ax, ay - y))
+ m_chunks.Get(ax, ay - y)->MakeDirty();
+ }
+ else if (m_chunks.Get(ax, ay)) m_tbDeleted.emplace_back(m_chunks.Remove(ax, ay));
+ }
+
+ m_center[0] += x; m_center[1] += y;
+ player.x -= x * CHUNK_SIZE_X;
+ player.z -= y * CHUNK_SIZE_Z;
+
+ for (int index = 0; index < MAX_BULLETS; ++index)
+ if (bullets[index]) bullets[index]->Transpose(x, y);
+}
+
+void World::CleanUpWorld(int& deleteframes, bool clear = false) {
+ if (clear) {
+ while (m_tbDeleted.size() > 0) {
+ delete m_tbDeleted.back();
+ m_tbDeleted.pop_back();
+ }
+ }
+ if (!m_tbDeleted.empty() && !deleteframes) {
+ int deleted = 0;
+ while (deleted < THREADS_DELETE_CHUNKS) {
+
+
+ }
+ delete m_tbDeleted.back();
+ m_tbDeleted.pop_back();
+ deleteframes = FRAMES_DELETE_CHUNKS;
+ }
+}
+
void World::GetScope(unsigned int& x, unsigned int& y) {
x = m_center[0];
y = m_center[1];
}
-void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) {
- int bx = (int)pos.x % CHUNK_SIZE_X;
- int by = (int)pos.y % CHUNK_SIZE_Y;
- int bz = (int)pos.z % CHUNK_SIZE_Z;
-
- ChunkAt(pos)->SetBlock(bx, by, bz, blockType, this);
+void World::Update(Bullet* bullets[MAX_BULLETS], Vector3f& player_pos, BlockInfo* blockinfo[BTYPE_LAST]) {
+ UpdateWorld(player_pos, blockinfo);
+ //TransposeWorld(player_pos, bullets);
}
+//
+//void World::UpdateChunk(int& updates, unsigned int chx, unsigned int chy, BlockInfo* blockinfo[BTYPE_LAST]) {
+// if (updates == 0 && ChunkAt(chx, 1, chy) &&
+// ChunkAt(chx, 1, chy)->IsDirty()) {
+// ChunkAt(chx, 1, chy)->Update(blockinfo, this);
+// updates = FRAMES_UPDATE_CHUNKS;
+// }
+//
+//}
-void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block) {
- Vector3f currentPos = player.GetPosition();
+void World::ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block) {
+ Vector3f currentPos = player_pos;
Vector3f currentBlock = currentPos;
- Vector3f ray = player.GetDirection();
+ Vector3f ray = player_dir;
bool found = false;
if (block) return;
@@ -76,7 +169,7 @@ void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block
BlockType bt = BlockAt(currentBlock);
- if (bt == BTYPE_AIR) { // V?rification pour ?tre s?r que le bloc ? changer n'est pas dans le joueur.
+ if (bt == BTYPE_AIR) { // Vérification pour être sûr que le bloc à changer n'est pas dans le joueur.
int Bx = (int)currentBlock.x;
int By = (int)currentBlock.y;
int Bz = (int)currentBlock.z;
@@ -107,3 +200,211 @@ void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block
block = true;
}
}
+
+void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) {
+ int bx = (int)pos.x % CHUNK_SIZE_X;
+ int by = (int)pos.y % CHUNK_SIZE_Y;
+ int bz = (int)pos.z % CHUNK_SIZE_Z;
+
+ ChunkAt(pos)->SetBlock(bx, by, bz, blockType, this);
+ ChunkAt(pos)->MakeModified();
+}
+
+void World::UpdateWorld(const Vector3f& player, BlockInfo* blockinfo[BTYPE_LAST]) {
+ int cx = player.x;
+ int cy = player.z;
+ static int frameGenerate = 1;
+ static int frameUpdate = 2;
+ static int frameDelete = 3;
+ int side = 0;
+ int threads = 0;
+ std::future genThList[THREADS_GENERATE_CHUNKS];
+ //std::future delThList[THREADS_DELETE_CHUNKS];
+
+ if (frameGenerate > 0) --frameGenerate;
+ if (frameUpdate > 0) --frameUpdate;
+ if (frameDelete > 0) --frameDelete;
+
+ if (!frameGenerate)
+ while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2 + CHUNK_SIZE_X) {
+ int tx = -side, ty = -side;
+ int chx = 0;
+ int chy = 0;
+
+ for (; tx <= side; ++tx) {
+ if (frameGenerate)
+ break;
+ chx = cx + tx * CHUNK_SIZE_X;
+ chy = cy + ty * CHUNK_SIZE_Z;
+ if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
+ chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
+ genThList[threads++] = std::async(std::launch::async,
+ [](unsigned int x, unsigned int y, uint64_t seed) {
+ return new Chunk(x, y, seed); },
+ chx / CHUNK_SIZE_X + m_center[0],
+ chy / CHUNK_SIZE_Z + m_center[1],
+ m_seed);
+ if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
+ }
+ for (; ty <= side; ++ty) {
+ if (frameGenerate)
+ break;
+ chx = cx + tx * CHUNK_SIZE_X;
+ chy = cy + ty * CHUNK_SIZE_Z;
+ if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
+ chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
+ genThList[threads++] = std::async(std::launch::async,
+ [](unsigned int x, unsigned int y, uint64_t seed) {
+ return new Chunk(x, y, seed); },
+ chx / CHUNK_SIZE_X + m_center[0],
+ chy / CHUNK_SIZE_Z + m_center[1],
+ m_seed);
+ if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
+ }
+ for (; tx >= -side; --tx) {
+ if (frameGenerate)
+ break;
+ chx = cx + tx * CHUNK_SIZE_X;
+ chy = cy + ty * CHUNK_SIZE_Z;
+ if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
+ chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
+ genThList[threads++] = std::async(std::launch::async,
+ [](unsigned int x, unsigned int y, uint64_t seed) {
+ return new Chunk(x, y, seed); },
+ chx / CHUNK_SIZE_X + m_center[0],
+ chy / CHUNK_SIZE_Z + m_center[1],
+ m_seed);
+ if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
+ }
+ for (; ty >= -side; --ty) {
+ if (frameGenerate)
+ break;
+ chx = cx + tx * CHUNK_SIZE_X;
+ chy = cy + ty * CHUNK_SIZE_Z;
+ if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
+ chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
+ genThList[threads++] = std::async(std::launch::async,
+ [](unsigned int x, unsigned int y, uint64_t seed) {
+ return new Chunk(x, y, seed); },
+ chx / CHUNK_SIZE_X + m_center[0],
+ chy / CHUNK_SIZE_Z + m_center[1],
+ m_seed);
+ if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
+ }
+ if (frameGenerate)
+ break;
+ ++side;
+ }
+
+ if (threads > 0) {
+ for (int i = 0; i < threads; ++i)
+ genThList[i].wait();
+
+ for (int i = 0; i < threads; ++i) {
+ unsigned int x, y;
+ Chunk* chunk = genThList[i].get();
+ chunk->GetPosition(x, y);
+ m_chunks.Set(x - m_center[0], y - m_center[1], chunk);
+ }
+ }
+
+ side = 0;
+ threads = 0;
+
+ //if (!frameUpdate)
+ // while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
+ // int tx = -side, ty = -side;
+
+ // for (; tx <= side; ++tx) {
+ // if (frameUpdate)
+ // break;
+ // unsigned int chx = cx + tx * CHUNK_SIZE_X, chy = cy + ty * CHUNK_SIZE_Z;
+ // if (ChunkAt(chx, 1, chy) &&
+ // ChunkAt(chx, 1, chy)->IsDirty()) {
+ // updateThList[threads++] =
+ // std::async(std::launch::async,
+ // [](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
+ // chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
+ // if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
+ // }
+ // }
+ // for (; ty <= side; ++ty) {
+ // if (frameUpdate)
+ // break;
+ // unsigned int chx = cx + tx * CHUNK_SIZE_X, chy = cy + ty * CHUNK_SIZE_Z;
+ // if (ChunkAt(chx, 1, chy) &&
+ // ChunkAt(chx, 1, chy)->IsDirty()) {
+ // updateThList[threads++] =
+ // std::async(std::launch::async,
+ // [](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
+ // chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
+ // if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
+ // }
+ // }
+ // for (; tx >= -side; --tx) {
+ // if (frameUpdate)
+ // break;
+ // unsigned int chx = cx + tx * CHUNK_SIZE_X, chy = cy + ty * CHUNK_SIZE_Z;
+ // if (ChunkAt(chx, 1, chy) &&
+ // ChunkAt(chx, 1, chy)->IsDirty()) {
+ // updateThList[threads++] =
+ // std::async(std::launch::async,
+ // [](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
+ // chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
+ // if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
+ // }
+ // }
+ // for (; ty >= -side; --ty) {
+ // if (frameUpdate)
+ // break;
+ // unsigned int chx = cx + tx * CHUNK_SIZE_X, chy = cy + ty * CHUNK_SIZE_Z;
+ // if (ChunkAt(chx, 1, chy) &&
+ // ChunkAt(chx, 1, chy)->IsDirty()) {
+ // updateThList[threads++] =
+ // std::async(std::launch::async,
+ // [](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
+ // chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
+ // if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
+ // }
+ // }
+ // if (frameUpdate)
+ // break;
+ // ++side;
+ // }
+
+ //if (threads > 0) {
+ // for (int i = 0; i < threads; ++i) {
+ // updateThList[i].wait();
+ // Chunk* chunk = updateThList[i].get();
+ // chunk->FlushMeshToVBO();
+ // }
+ //}
+
+ threads = 0;
+
+ //int del = THREADS_DELETE_CHUNKS;
+ //while (!m_tbDeleted.empty() && del--) { // Moins rapide que le bout en dessous, mais -beaucoup- plus stable.
+ // m_tbDeleted.back()->FlushVBO();
+ // m_tbDeleted.back()->~Chunk();
+ // m_tbDeleted.pop_back();
+ //}
+
+ /*while (!m_tbDeleted.empty() && !frameDelete) {
+ if (m_tbDeleted.back()) {
+ m_tbDeleted.back()->FlushVBO();
+ delThList[threads] =
+ std::async(std::launch::async,
+ [](Chunk* chunk) { delete chunk; }, m_tbDeleted.back());
+ m_tbDeleted.pop_back();
+ if (++threads > THREADS_DELETE_CHUNKS) frameDelete = FRAMES_DELETE_CHUNKS;
+ }
+ else m_tbDeleted.pop_back();
+ }*/
+
+ /*for (int x = 0; x < threads; ++x) {
+ delThList[x].wait();
+ delThList[x].get();
+ }*/
+}
+
+int World::GettbDeleted() const { return m_tbDeleted.size(); }
\ No newline at end of file
diff --git a/SQCSim-common/world.h b/SQCSim-common/world.h
index dd9967a..d341ee8 100644
--- a/SQCSim-common/world.h
+++ b/SQCSim-common/world.h
@@ -1,19 +1,18 @@
#ifndef WORLD_H__
#define WORLD_H__
+
#include
#include
#include
#include
#include
#include "define.h"
-#include "chunk.h"
-#include "array2d.h"
#include "vector3.h"
-#include "player.h"
+#include "array2d.h"
#include "bullet.h"
+#include "chunk.h"
class Chunk;
-class Player;
class Bullet;
class World {
@@ -23,23 +22,33 @@ public:
Array2d& GetChunks();
+ void SetSeed(uint64_t seed);
+
Chunk* ChunkAt(float x, float y, float z) const;
Chunk* ChunkAt(const Vector3f& pos) const;
BlockType BlockAt(float x, float y, float z, BlockType defaultBlockType = BTYPE_AIR) const;
BlockType BlockAt(const Vector3f& pos, BlockType defaultBlockType = BTYPE_AIR) const;
+ void Update(Bullet* bullets[MAX_BULLETS], Vector3f& player_pos, BlockInfo* blockinfo[BTYPE_LAST]);
+
void GetScope(unsigned int& x, unsigned int& y);
- void ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block);
+ void ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block);
void ChangeBlockAtPosition(BlockType blockType, Vector3f pos);
-
+ void CleanUpWorld(int& deleteframes, bool clear);
+ int GettbDeleted() const;
private:
Array2d m_chunks = Array2d(WORLD_SIZE_X, WORLD_SIZE_Y);
std::vector m_tbDeleted;
+ uint64_t m_seed = 0;
unsigned int m_center[2] = { UINT16_MAX / 2 - WORLD_SIZE_X, UINT16_MAX / 2 - WORLD_SIZE_Y };
+ void UpdateChunk(int& updates, unsigned int chx, unsigned int chy, BlockInfo* blockinfo[BTYPE_LAST]);
+ void UpdateWorld(const Vector3f& player, BlockInfo* blockinfo[BTYPE_LAST]);
+ void TransposeWorld(Vector3f& player, Bullet* bullets[MAX_BULLETS]);
+
};
#endif // WORLD_H__
diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h
index dc1df11..6dee988 100644
--- a/SQCSim-srv/connection.h
+++ b/SQCSim-srv/connection.h
@@ -1,5 +1,6 @@
#ifndef CONNECTION_H__
#define CONNECTION_H__
+
#include
#include