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

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