diff --git a/SQCSim-common/SQCSim-common.vcxproj b/SQCSim-common/SQCSim-common.vcxproj index 5db21d0..5f61a2a 100644 --- a/SQCSim-common/SQCSim-common.vcxproj +++ b/SQCSim-common/SQCSim-common.vcxproj @@ -137,6 +137,7 @@ + diff --git a/SQCSim-common/SQCSim-common.vcxproj.filters b/SQCSim-common/SQCSim-common.vcxproj.filters index af8b625..a6555fb 100644 --- a/SQCSim-common/SQCSim-common.vcxproj.filters +++ b/SQCSim-common/SQCSim-common.vcxproj.filters @@ -48,6 +48,9 @@ Fichiers d%27en-tête + + Fichiers d%27en-tête + diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h index 3501dd6..78284f5 100644 --- a/SQCSim-common/define.h +++ b/SQCSim-common/define.h @@ -2,51 +2,53 @@ #define DEFINE_H__ #include - -#define CHUNK_SIZE_X 16 -#define CHUNK_SIZE_Y 128 -#define CHUNK_SIZE_Z 16 -#define MAX_SELECTION_DISTANCE 5 -#define SEED 12345 +#include #define SRV_PORT 1025 #define CLI_PORT 1026 -#ifdef _DEBUG +#define CHUNK_SIZE_X 4 +#define CHUNK_SIZE_Y 64 +#define CHUNK_SIZE_Z 4 +#define MAX_SELECTION_DISTANCE 5 +#define SEED 12345 + #define WORLD_SIZE_X 64 #define WORLD_SIZE_Y 64 -#define FRAMES_RENDER_CHUNKS 4 -#define FRAMES_UPDATE_CHUNKS 4 -#define FRAMES_DELETE_CHUNKS 4 - -#define THREADS_GENERATE_CHUNKS 1 -#define THREADS_UPDATE_CHUNKS 1 -#define THREADS_DELETE_CHUNKS 1 - -#define VIEW_DISTANCE 256 -#define TEXTURE_SIZE 128 -#define MAX_BULLETS 64 -#endif - -#ifdef NDEBUG -#define WORLD_SIZE_X 16 -#define WORLD_SIZE_Y 16 - -#define FRAMES_RENDER_CHUNKS 1 -#define FRAMES_UPDATE_CHUNKS 1 -#define FRAMES_DELETE_CHUNKS 1 - -#define THREADS_GENERATE_CHUNKS 12 -#define THREADS_UPDATE_CHUNKS 5 -#define THREADS_DELETE_CHUNKS 2 - -#define VIEW_DISTANCE 1024 +#define VIEW_DISTANCE 512 #define TEXTURE_SIZE 512 #define MAX_BULLETS 512 -#endif typedef uint8_t BlockType; enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST }; +typedef std::chrono::system_clock::time_point Timestamp; + +#ifdef _WIN32 + +#pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject + +#include +#include +#include + +#define popen _popen +#define pclose _pclose + +#else // Pas _WIN32 + +#include +#include +#include +#include +#include +#include + +#define SOCKET int +#define INVALID_SOCKET -1 +#define closesocket close + +#endif // _WIN32 + #endif // DEFINE_H__ diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h new file mode 100644 index 0000000..3609de9 --- /dev/null +++ b/SQCSim-common/netprotocol.h @@ -0,0 +1,89 @@ +#ifndef NETPROTOCOL_H__ +#define NETPROTOCOL_H__ +#include "define.h" +#include "vector3.h" + +/* Protocole Particulier de Partie à Plusieurs Personnes (PPPPP) */ + +namespace netprot { + typedef uint8_t PacketType; + enum PACKET_TYPE { + ERR, INPUT, OUTPUT, SYNC, + TEAMINF, SELFINF, PLAYINF, + CHUNKMOD, PLAYERMOD, PICKUPMOD, + GAMEINFO, ENDINFO , CHAT + }; + + typedef struct { // cli -> srv UDP ~frame + Timestamp timestamp; + uint8_t keys; // 0bFBLRJS__ + Vector3f direction; + } Input; + + typedef struct { // srv -> cli UDP ~frame + Timestamp timestamp; + uint64_t id = 0; + Vector3f position, + direction; + bool is_shooting, + is_jumping; + } Output; + + typedef struct { // srv -> cli TCP ~second + Timestamp timestamp; + uint64_t sid = 0, + timer = 0; + uint8_t hp = 0; + uint16_t ammo = 0; + Vector3f position; + } Sync; + + typedef struct { // cli <-> srv TCP once + char name[32]; + uint64_t id = 0; + } TeamInfo; + + typedef struct { // cli <-> srv TCP once + char name[32]; + uint64_t sid = 0, + tid = 0; + } SelfInfo; + + typedef struct { // cli <-> srv TCP once + char name[32]; + uint64_t id = 0, + tid = 0; + } PlayerInfo; + + typedef struct { + uint64_t seed; + // uint8_t gameType; + // uint8_t time; + } GameInfo; + + typedef struct { // cli <-> srv TCP event + uint64_t src_id = 0, + dest_id = 0, + dest_team_id = 0; + char mess[140]; // Good 'nough for twitr, good 'nough for me. + } Chat; + + inline void Serialize(Input* in, char* buf, uint32_t* buflen); // cli + inline void Serialize(Output* out, char* buf, uint32_t* buflen); // srv + inline void Serialize(Sync* sync, char* buf, uint32_t* buflen); // srv + inline void Serialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv + inline void Serialize(SelfInfo* sinfo, char* buf, uint32_t* buflen); // cli/srv + inline void Serialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // srv + inline void Serialize(Chat* chat, char* buf, uint32_t* buflen); // cli/srv + + inline void Deserialize(Input* in, char* buf, uint32_t* buflen); // srv + inline void Deserialize(Output* out, char* buf, uint32_t* buflen); // cli + inline void Deserialize(Sync* sync, char* buf, uint32_t* buflen); // cli + inline void Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv + inline void Deserialize(SelfInfo* sinfo, char* buf, uint32_t* buflen); // cli/srv + inline void Deserialize(PlayerInfo* spinfoync, char* buf, uint32_t* buflen); // cli + inline void Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli + + inline PacketType getType(char* buf, uint32_t* buflen); // srv/cli +} +#endif diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 2b93114..0e8d252 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -2,9 +2,9 @@ Connection::Connection(in_addr addr, std::string name, - UINT64 id, - UINT64 self_id, - UINT64 team_id): + uint64_t id, + uint64_t self_id, + uint64_t team_id): m_addr(addr), m_id(id), m_sid(self_id), @@ -19,9 +19,9 @@ Connection::~Connection() { in_addr Connection::GetAddr() const { return m_addr; } -UINT64 Connection::GetHash(bool self) const { return self? m_sid: m_id; } +uint64_t Connection::GetHash(bool self) const { return self? m_sid: m_id; } -UINT64 Connection::GetTeamHash() const { return m_tid; } +uint64_t Connection::GetTeamHash() const { return m_tid; } std::string Connection::GetName() const { return m_name; } diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 5b0134d..6db8b06 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -4,42 +4,24 @@ #include #include "../SQCSim-common/player.h" #include "../SQCSim-common/vector3.h" +#include "../SQCSim-common/serialization.h" #include "define.h" -struct Input { - Timestamp timestamp; - UINT8 keys; // 0bFBLRJS__ - Vector3f direction; -}; - -struct Output { - Timestamp timestamp; - UINT64 id = 0; - Vector3f position, direction; - bool is_shooting, is_jumping; -}; - -struct Sync { - Timestamp timestamp; - UINT64 sid = 0; - Vector3f position; -}; - class Connection { public: Connection( in_addr addr, std::string name, - UINT64 hash, - UINT64 self_hash, - UINT64 team_hash); + uint64_t hash, + uint64_t self_hash, + uint64_t team_hash); ~Connection(); Player* player = nullptr; in_addr GetAddr() const; - UINT64 GetHash(bool self = true) const; - UINT64 GetTeamHash() const; + uint64_t GetHash(bool self = true) const; + uint64_t GetTeamHash() const; std::string GetName() const; void AddInput(Input in); @@ -51,7 +33,7 @@ private: std::map m_input_manifest; std::map m_output_manifest; in_addr m_addr; - UINT64 m_id, + uint64_t m_id, m_sid, m_tid; std::string m_name; diff --git a/SQCSim-srv/define.h b/SQCSim-srv/define.h index 74c1c26..297bd15 100644 --- a/SQCSim-srv/define.h +++ b/SQCSim-srv/define.h @@ -6,38 +6,9 @@ #include #include #include -#include #define MAX_CONNECTIONS 16 typedef unsigned char LogDest; enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST }; -typedef std::chrono::system_clock::time_point Timestamp; - - -#ifdef _WIN32 - -#pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject - -#include -#include -#include - -#define popen _popen -#define pclose _pclose - -#else // Pas _WIN32 - -#include -#include -#include -#include -#include -#include - -#define SOCKET int -#define INVALID_SOCKET -1 -#define closesocket close - -#endif // _WIN32 #endif \ No newline at end of file diff --git a/SQCSim2021/SQCSim2021.vcxproj b/SQCSim2021/SQCSim2021.vcxproj index dfed398..ab87f31 100644 --- a/SQCSim2021/SQCSim2021.vcxproj +++ b/SQCSim2021/SQCSim2021.vcxproj @@ -25,6 +25,7 @@ + @@ -46,6 +47,7 @@ + diff --git a/SQCSim2021/SQCSim2021.vcxproj.filters b/SQCSim2021/SQCSim2021.vcxproj.filters index 4b2e6c9..8d96ac0 100644 --- a/SQCSim2021/SQCSim2021.vcxproj.filters +++ b/SQCSim2021/SQCSim2021.vcxproj.filters @@ -74,6 +74,9 @@ Fichiers d%27en-tête + + Fichiers d%27en-tête + @@ -127,5 +130,8 @@ Fichiers sources + + Fichiers sources + \ No newline at end of file diff --git a/SQCSim2021/chunk.cpp b/SQCSim2021/chunk.cpp index fe4c0a5..5d78859 100644 --- a/SQCSim2021/chunk.cpp +++ b/SQCSim2021/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 diff --git a/SQCSim2021/chunk.h b/SQCSim2021/chunk.h index d4fdcb5..2ce15e8 100644 --- a/SQCSim2021/chunk.h +++ b/SQCSim2021/chunk.h @@ -25,7 +25,7 @@ class Chunk { void AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, float u, float v, float s, World* world); 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); diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp new file mode 100644 index 0000000..0bdaa56 --- /dev/null +++ b/SQCSim2021/connector.cpp @@ -0,0 +1,21 @@ +#include "connector.h" + +Connector::Connector() +{ +} + +Connector::~Connector() +{ +} + +int Connector::Init(sockaddr_in srv_addr) { + return 0; +} + +int Connector::Connect(std::string name) { + return 0; +} + +UINT64 Connector::getId() const { return m_sid; } + +unsigned int Connector::getSeed() const { return m_seed; } diff --git a/SQCSim2021/connector.h b/SQCSim2021/connector.h new file mode 100644 index 0000000..d2aee73 --- /dev/null +++ b/SQCSim2021/connector.h @@ -0,0 +1,46 @@ +#ifndef CONNECTOR_H__ +#define CONNECTOR_H__ + +#include "define.h" +#include "vector3.h" + +struct Input { // vers serveur + Timestamp timestamp; + UINT8 keys; // 0bFBLRJS__ + Vector3f direction; +}; + +struct Output { // autres joueurs du serveur + Timestamp timestamp; + UINT64 id = 0; + Vector3f position, direction; + bool is_shooting, is_jumping; +}; + +struct Sync { // du serveur + Timestamp timestamp; + UINT64 sid = 0; + Vector3f position; +}; + +class Connector { +public: + Connector(); + ~Connector(); + + int Init(sockaddr_in srv_addr); + int Connect(std::string name); + UINT64 getId() const; + unsigned int getSeed() const; + + //void SendInput(); + //int Sync(); +private: + SOCKET m_sock = 0; + std::string m_name = ""; + UINT64 m_sid = 0, + m_tid = 0; + unsigned int m_seed = 12345; + +}; +#endif diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index 660b62e..4e0c96f 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -12,6 +13,11 @@ #include #endif +#define NETWORK_TEST false +#define SRV_ADDR "127.0.0.1" +#define SRV_PORT 1025 +#define CLI_PORT 1026 + #define CHUNK_SIZE_X 4 #define CHUNK_SIZE_Y 64 #define CHUNK_SIZE_Z 4 @@ -35,9 +41,35 @@ #define TEXTURE_SIZE 512 #define MAX_BULLETS 512 +#ifdef _WIN32 + +#pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject + +#include +#include +#include + +#define popen _popen +#define pclose _pclose + +#else // Pas _WIN32 + +#include +#include +#include +#include +#include +#include + +#define SOCKET int +#define INVALID_SOCKET -1 +#define closesocket close + +#endif // _WIN32 typedef uint8_t BlockType; enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST }; +typedef std::chrono::system_clock::time_point Timestamp; #define TEXTURE_PATH "./media/textures/" #define SHADER_PATH "./media/shaders/" diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 1177fd3..79043ae 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -1,8 +1,4 @@ #include "engine.h" -#include -#include -#include "transformation.h" -#include "player.h" Engine::Engine() {} @@ -50,6 +46,28 @@ void Engine::Init() { for (int x = 0; x < MAX_BULLETS; ++x) m_bullets[x] = nullptr; + uint64_t seed = 12345; + std::string playname = "John Test"; + const char srvaddr[] = "127.0.0.1"; + if (NETWORK_TEST) { // Test connexion réseau. + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(SRV_PORT); + addr.sin_addr.s_addr = inet_addr(srvaddr); + + if (!m_conn->Init(addr)) { + if (m_conn->Connect(playname)) { + // setup jeu en réseau. + seed = m_conn->getSeed(); + std::cout << "ID reçu du serveur: " << std::to_string(m_conn->getId()) << "!" << std::endl; + } + else std::cout << "Erreur de connexion." << std::endl; + } + else std::cout << "Erreur de création de socket." << std::endl; + } + + m_world.SetSeed(seed); + // Init Chunks m_world.GetChunks().Reset(nullptr); diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index e96a931..947df54 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -1,6 +1,8 @@ #ifndef ENGINE_H__ #define ENGINE_H__ +#include +#include #include "define.h" #include "openglcontext.h" #include "texture.h" @@ -15,6 +17,7 @@ #include "array2d.h" #include "world.h" #include "bullet.h" +#include "connector.h" class Engine : public OpenglContext { public: @@ -45,6 +48,7 @@ private: void DrawHud(float elapsedTime, BlockType bloc); void PrintText(float x, float y, float scale, const std::string& t); + Connector* m_conn = nullptr; Shader m_shader01; BlockInfo* m_blockinfo[BTYPE_LAST]; TextureAtlas m_textureAtlas = TextureAtlas(BTYPE_LAST); diff --git a/SQCSim2021/world.cpp b/SQCSim2021/world.cpp index bbd86c0..dc5e5b6 100644 --- a/SQCSim2021/world.cpp +++ b/SQCSim2021/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; @@ -330,7 +334,12 @@ void World::UpdateWorld(Player& player, BlockInfo* blockinfo[BTYPE_LAST]) { 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) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]); + 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) { @@ -340,7 +349,12 @@ void World::UpdateWorld(Player& player, BlockInfo* blockinfo[BTYPE_LAST]) { 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) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]); + 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) { @@ -350,7 +364,12 @@ void World::UpdateWorld(Player& player, BlockInfo* blockinfo[BTYPE_LAST]) { 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) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]); + 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) { @@ -360,7 +379,12 @@ void World::UpdateWorld(Player& player, BlockInfo* blockinfo[BTYPE_LAST]) { 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) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]); + 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) diff --git a/SQCSim2021/world.h b/SQCSim2021/world.h index d4b1545..55140e3 100644 --- a/SQCSim2021/world.h +++ b/SQCSim2021/world.h @@ -26,6 +26,8 @@ public: Array2d& GetChunks(); + void SetSeed(uint64_t seed); + Chunk* ChunkAt(float x, float y, float z) const; Chunk* ChunkAt(const Vector3f& pos) const; @@ -43,6 +45,7 @@ public: 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 };