bulletadd

This commit is contained in:
MarcEricMartel 2023-12-05 06:25:48 -05:00
parent c5700ae9fd
commit 2446b90bff
8 changed files with 304 additions and 47 deletions

View File

@ -430,6 +430,77 @@ void netprot::Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen) {
*buflen = sizeof(uint32_t) * 3 + 3; *buflen = sizeof(uint32_t) * 3 + 3;
} }
void netprot::Serialize(BulletAdd* bull, char* buf[], uint32_t* buflen) {
*buf[0] = (char)netprot::PACKET_TYPE::BULLET;
uint64_t tstamp = bull->tstamp;
uint8_t ts8[sizeof(uint64_t)] = {
(uint8_t)((tstamp >> 56) & 0xFF),
(uint8_t)((tstamp >> 48) & 0xFF),
(uint8_t)((tstamp >> 40) & 0xFF),
(uint8_t)((tstamp >> 32) & 0xFF),
(uint8_t)((tstamp >> 24) & 0xFF),
(uint8_t)((tstamp >> 16) & 0xFF),
(uint8_t)((tstamp >> 8) & 0xFF),
(uint8_t)(tstamp & 0xFF)
};
memcpy(*buf + 1, ts8, sizeof(uint64_t));
uint64_t tid = bull->tid;
uint8_t tid8[sizeof(uint64_t)] = {
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
};
memcpy(*buf + 1 + sizeof(uint64_t), tid8, sizeof(uint64_t));
uint32_t vec[3];
memcpy(vec, &bull->pos, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + 1 + sizeof(uint64_t) * 2, vec8, sizeof(uint32_t) * 3);
memcpy(vec, &bull->dir, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t dir8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 3, dir8, sizeof(uint32_t) * 3);
*buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 6;
}
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
*buf[0] = (char)netprot::PACKET_TYPE::ERRLOG; *buf[0] = (char)netprot::PACKET_TYPE::ERRLOG;
@ -876,6 +947,74 @@ bool netprot::Deserialize(ChunkMod* chmod, char* buf, uint32_t* buflen) {
return true; return true;
} }
bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(BulletAdd))
return false;
uint8_t tst[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(tst, &buf[1], sizeof(uint64_t));
bull->tstamp =
(uint64_t)tst[0] << 56 |
(uint64_t)tst[1] << 48 |
(uint64_t)tst[2] << 40 |
(uint64_t)tst[3] << 32 |
(uint64_t)tst[4] << 24 |
(uint64_t)tst[5] << 16 |
(uint64_t)tst[6] << 8 |
(uint64_t)tst[7];
memcpy(tst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
bull->tid =
(uint64_t)tst[0] << 56 |
(uint64_t)tst[1] << 48 |
(uint64_t)tst[2] << 40 |
(uint64_t)tst[3] << 32 |
(uint64_t)tst[4] << 24 |
(uint64_t)tst[5] << 16 |
(uint64_t)tst[6] << 8 |
(uint64_t)tst[7];
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&bull->pos, vec, sizeof(uint32_t) * 3);
memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint8_t) * 12], sizeof(uint8_t) * 12);
uint32_t dir[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&bull->dir, dir, sizeof(uint32_t) * 3);
*buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint8_t) * 24;
return true;
}
bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) { bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) {
if (*buflen <= sizeof(ErrorLog)) if (*buflen <= sizeof(ErrorLog))
return false; return false;
@ -969,9 +1108,7 @@ netprot::Packet netprot::getPack(char* buf, uint32_t *buflen) {
return pck; return pck;
} }
netprot::Packet netprot::getPack(netprot::Buffer* buf) { netprot::Packet netprot::getPack(netprot::Buffer* buf) { return netprot::getPack(buf->ptr, &buf->len); }
return netprot::getPack(buf->ptr, &buf->len);
}
bool netprot::emptyPack(netprot::Packet pck) { bool netprot::emptyPack(netprot::Packet pck) {
switch (pck.type) { switch (pck.type) {

View File

@ -12,8 +12,8 @@ namespace netprot {
ERR, INPUT, OUTPUT, SYNC, ERR, INPUT, OUTPUT, SYNC,
TEAMINF, SELFINF, PLAYINF, LOGINF, TEAMINF, SELFINF, PLAYINF, LOGINF,
CHUNKMOD, PLAYERMOD, PICKUPMOD, CHUNKMOD, PLAYERMOD, PICKUPMOD,
GAMEINFO, ENDINFO , CHAT, ERRLOG, GAMEINFO, ENDINFO , BULLET,
LAST_PACK CHAT, ERRLOG, LAST_PACK
}; };
/* Structures */ /* Structures */
@ -135,6 +135,12 @@ namespace netprot {
BlockType b_type, old_b_type; BlockType b_type, old_b_type;
}; };
struct BulletAdd {
Timestamp tstamp;
Vector3f pos, dir;
uint64_t id;
};
struct ErrorLog { // srv -> cli TCP event struct ErrorLog { // srv -> cli TCP event
char mess[140]; char mess[140];
bool is_fatal; bool is_fatal;
@ -153,6 +159,7 @@ namespace netprot {
void Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen); // cli/srv void Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen); // srv void Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen); // srv
void Serialize(BulletAdd* bull, char* buf[], uint32_t* buflen); // srv
void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv
bool Deserialize(Input* in, char* buf, uint32_t* buflen); // srv bool Deserialize(Input* in, char* buf, uint32_t* buflen); // srv
@ -164,6 +171,7 @@ namespace netprot {
bool Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli bool Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli
bool Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli bool Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli
bool Deserialize(ChunkMod* chmod, char* buf, uint32_t* buflen); // cli bool Deserialize(ChunkMod* chmod, char* buf, uint32_t* buflen); // cli
bool Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen); // cli
bool Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv bool Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv
PACKET_TYPE getType(char* buf, uint32_t buflen); PACKET_TYPE getType(char* buf, uint32_t buflen);

View File

@ -95,7 +95,7 @@ void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*
if (outs >= 1000) { if (outs >= 1000) {
outs -= 1000; outs -= 1000;
Sync sync; Sync sync;
sync.hp = player.get()->GetHP(); sync.hp = player->GetHP();
sync.timestamp = out.timestamp; sync.timestamp = out.timestamp;
sync.position = out.position; sync.position = out.position;
sync.sid = m_loginfo.sid; sync.sid = m_loginfo.sid;
@ -109,13 +109,14 @@ void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*
} }
} }
void Connection::Run(World* world) { Timestamp Connection::Run(World* world) {
Input in, last; Input in, last;
Output out; Output out;
Timestamp tstamp = 0;
float el; float el;
if (m_input_manifest.size() < 2) if (m_input_manifest.size() < 2)
return; return tstamp;
while (m_last_in < m_input_vector.size() - 1) { while (m_last_in < m_input_vector.size() - 1) {
in = m_input_vector.at(m_last_in + 1); in = m_input_vector.at(m_last_in + 1);
@ -129,30 +130,39 @@ void Connection::Run(World* world) {
m_shoot_acc = 0; m_shoot_acc = 0;
} }
player.get()->SetDirection(in.direction); player->SetDirection(in.direction);
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, player->ApplyPhysics(player->GetInput(in.keys.forward,
in.keys.backward, in.keys.backward,
in.keys.left, in.keys.left,
in.keys.right, in.keys.right,
in.keys.jump, false, el), world, el); in.keys.jump, false, el), world, el);
//if (in.keys.block) if (in.keys.block) {
// ChunkDiffs.push_back() bool block = false;
ChunkMod* cmod = world->ChangeBlockAtCursor(BLOCK_TYPE::BTYPE_METAL,
player->GetPosition(),
player->GetDirection(),
block, true);
if (cmod)
ChunkDiffs.emplace_back(cmod);
}
if (in.keys.shoot && m_shoot_acc <= 0.) if (in.keys.shoot && m_shoot_acc <= 0.)
Bullets.push_back(Bullet(player.get()->GetPOV() + player.get()->GetDirection(), player.get()->GetDirection())); Bullets.push_back(Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection()));
out.position = player->GetPositionAbs();
out.position = player.get()->GetPositionAbs();
out.direction = in.direction; out.direction = in.direction;
out.timestamp = in.timestamp; out.timestamp = in.timestamp;
out.id = m_playinfo.id; out.id = m_playinfo.id;
m_output_manifest[out.timestamp] = out; m_output_manifest[out.timestamp] = out;
m_output_vector.push_back(out); m_output_vector.push_back(out);
tstamp = out.timestamp;
++m_last_in; ++m_last_in;
} }
return tstamp;
} }
void Connection::CleanInputManifest(Timestamp time) { void Connection::CleanInputManifest(Timestamp time) {
@ -161,3 +171,5 @@ void Connection::CleanInputManifest(Timestamp time) {
// while (wat != m_input_manifest.begin()) // while (wat != m_input_manifest.begin())
// m_input_manifest.erase(wat--); // m_input_manifest.erase(wat--);
} }
Timestamp Connection::GetTStamp() const { return m_tstamp; }

View File

@ -20,7 +20,7 @@ public:
PlayerInfo play); PlayerInfo play);
~Connection(); ~Connection();
std::unique_ptr<Player> player = nullptr; Player* player = nullptr;
uint64_t GetHash(bool self = true) const; uint64_t GetHash(bool self = true) const;
uint64_t GetTeamHash() const; uint64_t GetTeamHash() const;
@ -36,7 +36,7 @@ public:
void getPacks(SOCKET sock); void getPacks(SOCKET sock);
void sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns, const uint32_t timer); void sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns, const uint32_t timer);
void Run(World* world); Timestamp Run(World* world);
void CleanInputManifest(Timestamp time); void CleanInputManifest(Timestamp time);
@ -45,6 +45,8 @@ public:
std::vector<Bullet> Bullets; std::vector<Bullet> Bullets;
std::vector<ChunkMod> ChunkDiffs; std::vector<ChunkMod> ChunkDiffs;
Timestamp GetTStamp() const;
private: private:
std::unordered_map<Timestamp, Input> m_input_manifest; std::unordered_map<Timestamp, Input> m_input_manifest;
std::vector<Input> m_input_vector; std::vector<Input> m_input_vector;
@ -53,6 +55,7 @@ private:
std::unordered_map<Timestamp, Chat> m_chatlog; std::unordered_map<Timestamp, Chat> m_chatlog;
float m_shoot_acc = 0; float m_shoot_acc = 0;
Timestamp m_tstamp = 0;
SOCKET m_sock; SOCKET m_sock;
sockaddr_in m_addr; sockaddr_in m_addr;

View File

@ -18,9 +18,9 @@ Server::~Server() {
closesocket(m_sock_udp); closesocket(m_sock_udp);
if (m_sock_tcp) if (m_sock_tcp)
closesocket(m_sock_tcp); closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players) for (const auto& [key, player] : m_conns)
closesocket(player->getSock()); closesocket(player->getSock());
m_players.clear(); m_conns.clear();
delete m_world; delete m_world;
#ifdef _WIN32 #ifdef _WIN32
WSACleanup(); WSACleanup();
@ -165,7 +165,7 @@ int Server::Ready() {
std::cout << m_game.seed << std::endl; std::cout << m_game.seed << std::endl;
Connection* conn = new Connection(sock, sockad, *log, play); Connection* conn = new Connection(sock, sockad, *log, play);
m_players[log->sid] = conn; m_conns[log->sid] = conn;
delete log; delete log;
@ -174,8 +174,8 @@ int Server::Ready() {
} }
} }
} }
for (auto& [keyin, playin] : m_players) // Not pretty, but it works. for (auto& [keyin, playin] : m_conns) // Not pretty, but it works.
for (auto& [keyout, playout] : m_players) { for (auto& [keyout, playout] : m_conns) {
if (keyin == keyout) if (keyin == keyout)
continue; continue;
sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur. sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur.
@ -192,17 +192,18 @@ void Server::Run() {
Log("Debut de la partie...", false, false); Log("Debut de la partie...", false, false);
int players = m_players.size(); int players = m_conns.size();
m_world = new World(); m_world = new World();
m_world->SetSeed(m_game.seed); m_world->SetSeed(m_game.seed);
m_world->GetChunks().Reset(nullptr); m_world->GetChunks().Reset(nullptr);
m_world->BuildWorld(); m_world->BuildWorld();
for (auto& [key, conn] : m_players) { // Creation des instances de joueurs et premier sync. for (auto& [key, conn] : m_conns) { // Creation des instances de joueurs et premier sync.
int x = (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1)) - (CHUNK_SIZE_X * WORLD_SIZE_X / 2), int x = (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1)) - (CHUNK_SIZE_X * WORLD_SIZE_X / 2),
y = (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1)) - (CHUNK_SIZE_Y * WORLD_SIZE_Y / 2); y = (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1)) - (CHUNK_SIZE_Y * WORLD_SIZE_Y / 2);
conn->player = std::make_unique<Player>(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f)); conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
m_players[key] = conn->player;
Sync sync; Sync sync;
sync.position = conn->player->GetPositionAbs(); sync.position = conn->player->GetPositionAbs();
sync.hp = conn->player->GetHP(); sync.hp = conn->player->GetHP();
@ -217,6 +218,9 @@ void Server::Run() {
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
Timestamp last = 0; Timestamp last = 0;
std::vector<Chat> chatlog; std::vector<Chat> chatlog;
std::vector<ChunkMod*> chunkdiffs;
std::vector<Bullet*> bullets, outbox_bullets;
std::vector<BulletAdd*> netbull;
while (!endgame) { while (!endgame) {
using namespace std::chrono; using namespace std::chrono;
@ -230,13 +234,14 @@ void Server::Run() {
--timer; --timer;
} }
for (auto& [key, conn] : m_players) { for (auto& [key, conn] : m_conns) {
/* In */ /* In */
int deadplayers = 0; int deadplayers = 0;
std::vector<char*> lsPck; std::vector<char*> lsPck;
Input in; Chat chat; Sync sync; Input in; Chat chat; Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf); lsPck = recvPacks(m_sock_udp, &m_buf);
for (auto& pck : lsPck) { for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr); uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
@ -244,7 +249,7 @@ void Server::Run() {
using enum netprot::PACKET_TYPE; using enum netprot::PACKET_TYPE;
case INPUT: case INPUT:
if (Deserialize(&in, pck, &bsize)) if (Deserialize(&in, pck, &bsize))
m_players[in.sid]->AddInput(in); m_conns[in.sid]->AddInput(in);
break; break;
case SYNC: case SYNC:
if (Deserialize(&sync, pck, &bsize)) {} if (Deserialize(&sync, pck, &bsize)) {}
@ -264,25 +269,80 @@ void Server::Run() {
if (conn->player->AmIDead()) { if (conn->player->AmIDead()) {
++deadplayers; ++deadplayers;
conn->m_nsync == false; conn->m_nsync == false;
continue;
} }
conn->Run(m_world); else {
Timestamp tstamp = conn->Run(m_world);
for (auto& chmo : conn->ChunkDiffs)
chunkdiffs.emplace_back(chmo);
conn->ChunkDiffs.clear();
for (auto& bull : conn->Bullets) {
bullets.emplace_back(bull);
BulletAdd* nbul = new BulletAdd();
nbul->pos = conn->player->GetPosition();
nbul->dir = conn->player->GetDirection();
nbul->id = key;
nbul->tstamp = tstamp;
netbull.emplace_back(nbul);
}
conn->Bullets.clear();
}
/* Out */ /* Out */
conn->sendPacks(m_sock_udp, m_players, timer); conn->sendPacks(m_sock_udp, m_conns, timer);
} }
if (deadplayers == players - 1 || timer <= 0) if (deadplayers == players - 1 || timer <= 0)
endgame = true; endgame = true;
} }
for (auto& bull : netbull) {
for (auto& [key, conn] : m_conns)
sendPackTo<BulletAdd>(m_sock_udp, bull, &m_buf, conn->getAddr());
}
for (auto& bull : bullets) {
ChunkMod* cmod = nullptr;
if (bull->Update(m_world, (1. / 60.), 20, m_players, &cmod)) {
if (cmod)
chunkdiffs.emplace_back(cmod);
outbox_bullets.emplace_back(std::move(bull));
}
}
for (auto& chat : chatlog) for (auto& chat : chatlog)
for (auto& [key, conn] : m_players) for (auto& [key, conn] : m_conns)
sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr()); sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr());
for (auto& chmo : chunkdiffs) {
for (auto& [key, conn] : m_conns)
sendPackTo<ChunkMod>(m_sock_udp, chmo, &m_buf, conn->getAddr());
delete chmo;
}
for (auto& bull : outbox_bullets)
delete bull;
for (auto& bull : netbull)
delete bull;
outbox_bullets.clear();
netbull.clear();
chunkdiffs.clear();
chatlog.clear(); chatlog.clear();
} }
Chat end;
end.src_id = 0;
char endmess[] = "Game over, motherfuckers.";
strcpy(end.mess, endmess);
for (auto& [key, conn] : m_conns)
sendPackTo<Chat>(m_sock_udp, &end, &m_buf, conn->getAddr());
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game. // TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
} }
@ -325,11 +385,11 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
closesocket(m_sock_udp); closesocket(m_sock_udp);
if (m_sock_tcp) if (m_sock_tcp)
closesocket(m_sock_tcp); closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players) { for (const auto& [key, player] : m_conns) {
closesocket(player->getSock()); closesocket(player->getSock());
} }
delete m_world; delete m_world;
m_players.clear(); m_conns.clear();
#ifdef _WIN32 #ifdef _WIN32
WSACleanup(); WSACleanup();
#endif #endif

View File

@ -36,7 +36,8 @@ private:
Buffer m_buf; Buffer m_buf;
std::unordered_map<uint64_t, Connection*> m_players; std::unordered_map<uint64_t, Player*> m_players;
std::unordered_map<uint64_t, Connection*> m_conns;
std::unordered_map<Timestamp, Chat> m_chatlog; std::unordered_map<Timestamp, Chat> m_chatlog;
std::vector<uint64_t> m_ids; std::vector<uint64_t> m_ids;
GameInfo m_game; GameInfo m_game;

View File

@ -1362,7 +1362,7 @@ void Engine::Render(float elapsedTime) {
Sync sync; Sync sync;
uint64_t id = m_conn.getId(); uint64_t id = m_conn.getId();
static std::vector<char*> lsPck; static std::vector<char*> lsPck;
static int sync_acc = 0; static int sync_acc = 0, cmod_acc = 0;
if (cmod) if (cmod)
m_chunkmod_manifest.emplace_back(cmod); m_chunkmod_manifest.emplace_back(cmod);
@ -1371,6 +1371,7 @@ void Engine::Render(float elapsedTime) {
last = tstamp; last = tstamp;
sync_acc += tstamp - last; sync_acc += tstamp - last;
cmod_acc += tstamp - last;
if (sync_acc >= 1000) { if (sync_acc >= 1000) {
sync_acc -= 1000; sync_acc -= 1000;
@ -1385,6 +1386,16 @@ void Engine::Render(float elapsedTime) {
m_syncs[sync.timestamp] = sync; m_syncs[sync.timestamp] = sync;
} }
if (cmod_acc >= 1000) {
cmod_acc -= 1000;
if (!m_chunkmod_manifest.empty()) {
ChunkMod* cmod = m_chunkmod_manifest.front();
m_chunkmod_manifest.pop_front();
m_world.ChangeBlockAtPosition(cmod->old_b_type, cmod->pos);
delete cmod;
}
}
input.sid = id; input.sid = id;
input.direction = m_player.GetDirection(); input.direction = m_player.GetDirection();
input.timestamp = tstamp; input.timestamp = tstamp;
@ -1401,8 +1412,7 @@ void Engine::Render(float elapsedTime) {
lsPck = recvPacks(m_conn.m_sock_udp, &m_buf); lsPck = recvPacks(m_conn.m_sock_udp, &m_buf);
char* prevptr = nullptr; char* prevptr = nullptr;
for (auto& pck : lsPck) { // We could make a few threads out of this. for (auto& pck : lsPck) { // We could make a few threads out of this.
Sync sync; Sync sync; Output out; ChunkMod cmod; BulletAdd bull;
Output out;
if (!prevptr) if (!prevptr)
prevptr = m_buf.ptr; prevptr = m_buf.ptr;
uint32_t bsize = m_buf.len - (pck - prevptr); uint32_t bsize = m_buf.len - (pck - prevptr);
@ -1447,6 +1457,33 @@ void Engine::Render(float elapsedTime) {
rt->Feed(out); rt->Feed(out);
} }
break; break;
case CHUNKMOD:
if (Deserialize(&cmod, pck, &bsize)) {
if (!std::erase_if(m_chunkmod_manifest, // Efface le chunkmod du manifeste s'il est dedans et reset le countdown, sinon fait la modification.
[cmod](ChunkMod* c) {
return cmod.pos == c->pos &&
cmod.b_type == c->b_type &&
cmod.old_b_type == c->old_b_type;
}))
m_world.ChangeBlockAtPosition(cmod.b_type, cmod.pos);
else cmod_acc = 0;
}
break;
case BULLET:
if (Deserialize(&bull, pck, &bsize)) {
Bullet* bult = new Bullet(bull.pos, bull.dir, bull.id);
for (int x = 0; x < MAX_BULLETS; ++x) // Ajouter une balle dans l'array (aussi connu sous le nom de "faire pow pow").
if (!m_bullets[x]) {
m_bullets[x] = bult;
break;
}
else if (x == MAX_BULLETS - 1) { // S'il y a pas d'espace dans l'array, prendre la place de la première balle de l'array.
m_bullets[0]->~Bullet();
m_bullets[0] = bult;
}
m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir * 10, false, .5f);
}
break;
default: default:
std::cout << "packet be no good."; std::cout << "packet be no good.";
break; break;

View File

@ -5,6 +5,7 @@
#include <chrono> #include <chrono>
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
#include <set>
#include "../SQCSim-common/array2d.h" #include "../SQCSim-common/array2d.h"
#include "../SQCSim-common/blockinfo.h" #include "../SQCSim-common/blockinfo.h"
#include "../SQCSim-common/boostinfo.h" #include "../SQCSim-common/boostinfo.h"
@ -87,8 +88,6 @@ private:
Bullet* m_bullets[MAX_BULLETS]; Bullet* m_bullets[MAX_BULLETS];
//Menu //Menu
enum class GameState: uint8_t { MAIN_MENU, OPTIONS, QUIT, NEWG, PLAY, PAUSE, SINGLEMULTI };
Vector3f m_otherplayerpos = Vector3f(999, 999, 999); Vector3f m_otherplayerpos = Vector3f(999, 999, 999);
World m_world = World(); World m_world = World();
@ -214,7 +213,7 @@ private:
bool m_networkgame = false; bool m_networkgame = false;
Connector m_conn; Connector m_conn;
std::vector<netprot::ChunkMod*> m_chunkmod_manifest; std::deque<netprot::ChunkMod*> m_chunkmod_manifest;
std::chrono::high_resolution_clock::time_point m_startTime; std::chrono::high_resolution_clock::time_point m_startTime;
std::unordered_map<uint64_t, Player*> m_players; std::unordered_map<uint64_t, Player*> m_players;
netprot::Buffer m_buf, m_bufout; netprot::Buffer m_buf, m_bufout;