Début protocole

This commit is contained in:
MarcEricMartel 2023-09-25 08:23:52 -04:00
parent 6b2f7face7
commit 20d15a1559
18 changed files with 308 additions and 104 deletions

View File

@ -137,6 +137,7 @@
<ClInclude Include="matrix4.h" />
<ClInclude Include="opensimplex.h" />
<ClInclude Include="player.h" />
<ClInclude Include="netprotocol.h" />
<ClInclude Include="vector3.h" />
<ClInclude Include="world.h" />
</ItemGroup>

View File

@ -48,6 +48,9 @@
<ClInclude Include="vector3.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="netprotocol.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="world.cpp">

View File

@ -2,51 +2,53 @@
#define DEFINE_H__
#include <iostream>
#define CHUNK_SIZE_X 16
#define CHUNK_SIZE_Y 128
#define CHUNK_SIZE_Z 16
#define MAX_SELECTION_DISTANCE 5
#define SEED 12345
#include <chrono>
#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 <Windows.h>
#include <cstdio>
#include <ctime>
#define popen _popen
#define pclose _pclose
#else // Pas _WIN32
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SOCKET int
#define INVALID_SOCKET -1
#define closesocket close
#endif // _WIN32
#endif // DEFINE_H__

View File

@ -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

View File

@ -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; }

View File

@ -4,42 +4,24 @@
#include <map>
#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<Timestamp, Input> m_input_manifest;
std::map<Timestamp, Output> m_output_manifest;
in_addr m_addr;
UINT64 m_id,
uint64_t m_id,
m_sid,
m_tid;
std::string m_name;

View File

@ -6,38 +6,9 @@
#include <sstream>
#include <cctype>
#include <string>
#include <chrono>
#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 <Windows.h>
#include <cstdio>
#include <ctime>
#define popen _popen
#define pclose _pclose
#else // Pas _WIN32
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SOCKET int
#define INVALID_SOCKET -1
#define closesocket close
#endif // _WIN32
#endif

View File

@ -25,6 +25,7 @@
<ClInclude Include="blockinfo.h" />
<ClInclude Include="bullet.h" />
<ClInclude Include="chunk.h" />
<ClInclude Include="connector.h" />
<ClInclude Include="define.h" />
<ClInclude Include="engine.h" />
<ClInclude Include="matrix4.h" />
@ -46,6 +47,7 @@
<ClCompile Include="blockinfo.cpp" />
<ClCompile Include="bullet.cpp" />
<ClCompile Include="chunk.cpp" />
<ClCompile Include="connector.cpp" />
<ClCompile Include="engine.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="openglcontext.cpp" />

View File

@ -74,6 +74,9 @@
<ClInclude Include="opensimplex.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="connector.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="blockinfo.cpp">
@ -127,5 +130,8 @@
<ClCompile Include="opensimplex.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="connector.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -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

View File

@ -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);

21
SQCSim2021/connector.cpp Normal file
View File

@ -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; }

46
SQCSim2021/connector.h Normal file
View File

@ -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

View File

@ -4,6 +4,7 @@
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <chrono>
#ifdef _WIN32
#include <windows.h>
@ -12,6 +13,11 @@
#include <gl/GLU.h>
#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 <Windows.h>
#include <cstdio>
#include <ctime>
#define popen _popen
#define pclose _pclose
#else // Pas _WIN32
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#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/"

View File

@ -1,8 +1,4 @@
#include "engine.h"
#include <algorithm>
#include <cmath>
#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);

View File

@ -1,6 +1,8 @@
#ifndef ENGINE_H__
#define ENGINE_H__
#include <algorithm>
#include <cmath>
#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);

View File

@ -6,6 +6,10 @@ World::~World() {}
Array2d<Chunk*>& 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)

View File

@ -26,6 +26,8 @@ public:
Array2d<Chunk*>& 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<Chunk*> m_chunks = Array2d<Chunk*>(WORLD_SIZE_X, WORLD_SIZE_Y);
std::vector<Chunk*> m_tbDeleted;
uint64_t m_seed = 0;
unsigned int m_center[2] = { UINT16_MAX / 2 - WORLD_SIZE_X, UINT16_MAX / 2 - WORLD_SIZE_Y };