diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp index 413c2cc..a489c55 100644 --- a/SQCSim-common/bullet.cpp +++ b/SQCSim-common/bullet.cpp @@ -38,11 +38,10 @@ bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordere else if (world->BlockAt(m_currentpos) != BTYPE_AIR) { if (chunkmod) { using namespace netprot; - ChunkMod* cmod = *chunkmod; - cmod = new ChunkMod(); - cmod->old_b_type = world->BlockAt(m_currentpos); - cmod->b_type = BTYPE_AIR; - cmod->pos = m_currentpos; + *chunkmod = new ChunkMod(); + (*chunkmod)->old_b_type = world->BlockAt(m_currentpos); + (*chunkmod)->b_type = BTYPE_AIR; + (*chunkmod)->pos = m_currentpos; } world->ChangeBlockAtPosition(BTYPE_AIR, m_currentpos); diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 04f70bc..68feda2 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -198,7 +198,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { (uint8_t)((vec[2] >> 8) & 0xFF), (uint8_t)(vec[2] & 0xFF) }; - memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 2, vec8, sizeof(uint32_t) * 3); + memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 1, vec8, sizeof(uint32_t) * 3); uint32_t hp; @@ -210,6 +210,8 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { (uint8_t)((hp >> 8) & 0xFF), (uint8_t)(hp & 0xFF) }; + memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 1, hp8, sizeof(float)); + *buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + sizeof(float) + 1; } @@ -218,7 +220,8 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) { size_t namesize = std::strlen(tinfo->name) + 1; - memcpy(*buf + 1, &tinfo->name, namesize); + strcpy(*buf + 1, namesize, tinfo->name); + uint64_t tid = tinfo->id; uint8_t tid8[sizeof(uint64_t)] = { (uint8_t)((tid >> 56) & 0xFF), @@ -241,7 +244,8 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { size_t namesize = std::strlen(linfo->name) + 1; - memcpy(*buf + 1, &linfo->name, namesize); + strcpy(*buf + 1, namesize, linfo->name); + uint64_t sid = linfo->sid; uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF), @@ -278,7 +282,8 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { size_t namesize = std::strlen(pinfo->name) + 1; - memcpy(*buf + 1, &pinfo->name, namesize); + strcpy(*buf + 1, namesize, pinfo->name); + uint64_t id = pinfo->id; uint8_t id8[sizeof(uint64_t)] = { (uint8_t)((id >> 56) & 0xFF), @@ -703,7 +708,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) { uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; - memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t) * 12); + memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t) * 12); uint32_t vec[3] = { (uint32_t)subvec[0] << 24 | (uint32_t)subvec[1] << 16 | @@ -722,7 +727,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) { uint8_t hp8[4]; - memcpy(&hp8, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t)); + memcpy(&hp8, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t)); uint32_t hp = (uint32_t)hp8[0] << 24 | (uint32_t)hp8[1] << 16 | @@ -731,7 +736,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) { memcpy(&sync->hp, &hp, sizeof(float)); - *buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + sizeof(float); + *buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + sizeof(float); return true; } @@ -745,7 +750,7 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen) { if (namesize > 32) return false; - memcpy(&tinfo->name, &buf[1], namesize); + strcpy(tinfo->name, namesize, &buf[1]); uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; memcpy(diff, &buf[namesize + 1], sizeof(uint64_t)); @@ -773,7 +778,7 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen) { if (namesize > 32) return false; - memcpy(&linfo->name, &buf[1], namesize); + strcpy(linfo->name, namesize, &buf[1]); uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; memcpy(diff, &buf[namesize + 1], sizeof(uint64_t)); @@ -812,7 +817,7 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen) { if (namesize > 32) return false; - memcpy(&pinfo->name, &buf[1], namesize); + strcpy(pinfo->name, namesize, &buf[1]); uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; memcpy(diff, &buf[namesize + 1], sizeof(uint64_t)); diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 87a1645..6171bcf 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -19,7 +19,7 @@ namespace netprot { /* Structures */ struct Buffer { // Pour pouvoir rendre l'utilisation des buffers plus clean. - char* ptr = new char[BUFFER_LENGTH] { 1 }, * tmp = nullptr; + char *ptr = new char[BUFFER_LENGTH] { 1 }, *tmp = nullptr; uint32_t len = BUFFER_LENGTH; ~Buffer() { delete[] ptr; } @@ -27,7 +27,7 @@ namespace netprot { }; struct Packet { // Pour pouvoir recevoir les paquets du recv() sans avoir à les aiguiller dans la même thread. - void* ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile. + void *ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile. PACKET_TYPE type = PACKET_TYPE::ERR; }; @@ -87,30 +87,30 @@ namespace netprot { }; struct TeamInfo { // cli <-> srv TCP once - char name[32]; + char *name = new char[32]; uint64_t id = 0; TeamInfo() {} - TeamInfo(TeamInfo* tem) : id(tem->id) { strcpy(tem->name, name); } + TeamInfo(TeamInfo* tem) : id(tem->id) { strcpy(name, 32, tem->name); } + ~TeamInfo() { delete[] name; } }; struct LoginInfo { // cli <-> srv TCP once - char name[32]; + char *name = new char[32]; uint64_t sid = 0, tid = 0; LoginInfo() {} - LoginInfo(LoginInfo* ply): sid(ply->sid), tid(ply->tid) { strcpy(ply->name, name); } + LoginInfo(LoginInfo* log): sid(log->sid), tid(log->tid) { strcpy(name, 32, log->name); } + ~LoginInfo() { delete[] name; } }; struct PlayerInfo { // cli <-> srv TCP once - char name[32]; + char *name = new char[32]; uint64_t id = 0, tid = 0; PlayerInfo() {} - PlayerInfo(PlayerInfo* log) : id(log->id), tid(log->tid) { - strcpy(log->name, name); - }; - PlayerInfo(int id, int tid, std::string strname) : id(id), tid(tid) { memcpy((void*)strname.c_str(), name, strname.length()); - } + PlayerInfo(PlayerInfo* ply) : id(ply->id), tid(ply->tid) { strcpy(name, 32, ply->name); }; + PlayerInfo(int id, int tid, std::string strname) : id(id), tid(tid) { strcpy(name, 32, strname.c_str()); } + ~PlayerInfo() { delete[] name; } }; struct GameInfo { // cli <-> srv TCP event (before game start)/ once @@ -125,10 +125,9 @@ namespace netprot { uint64_t src_id = 0, dest_id = 0, dest_team_id = 0; - char* mess = new char[140]; // Good 'nough for twitr, good 'nough for me. + char *mess = new char[140]; // Good 'nough for twitr, good 'nough for me. Chat() {} - Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) { - strcpy(mess, 140, cha->mess); } + Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) { strcpy(mess, 140, cha->mess); } ~Chat() { delete[] mess; } }; @@ -144,10 +143,11 @@ namespace netprot { }; struct ErrorLog { // srv -> cli TCP event - char mess[140]; - bool is_fatal; + char *mess = new char[140]; + bool is_fatal = false; ErrorLog() {}; - ErrorLog(ErrorLog* err) : is_fatal(err->is_fatal) { strcpy(err->mess, mess); } + ErrorLog(ErrorLog* err) : is_fatal(err->is_fatal) { strcpy(mess, 140, err->mess); } + ~ErrorLog() { delete[] mess; } }; /* Fonctions */ diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index dbe7816..669ae8f 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -4,16 +4,18 @@ Connection::Connection(SOCKET sock, sockaddr_in sockaddr, - LoginInfo log, - PlayerInfo play) : + LoginInfo *log, + PlayerInfo *play) : m_sock(sock), m_addr(sockaddr), - m_loginfo(log), - m_playinfo(play) { + m_loginfo(*log), + m_playinfo(*play) { } -Connection::~Connection() { closesocket(m_sock); } +Connection::~Connection() { + delete player; + closesocket(m_sock); } uint64_t Connection::GetHash(bool self) const { return self ? m_loginfo.sid : m_playinfo.id; } @@ -144,7 +146,7 @@ Timestamp Connection::Run(World* world) { player->Killer = GetHash(true); } - out.states.jumping = player->GetIsAirborne(); //abs(player->GetVelocity().y) > .2f; + out.states.jumping = player->GetIsAirborne(); out.states.running = player->GetVelocity().Length() > .5f; out.states.still = !out.states.running && !out.states.jumping; out.states.hit = player->m_hit; @@ -166,7 +168,7 @@ Timestamp Connection::Run(World* world) { player->GetDirection(), block, true); if (cmod) - ChunkDiffs.emplace_back(cmod); + ChunkDiffs.push_back(std::move(cmod)); } } else toggle = false; @@ -178,7 +180,7 @@ Timestamp Connection::Run(World* world) { else out.states.jumpshot = false; if (in.keys.shoot && m_shoot_acc <= 0.) { - Bullets.emplace_back(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true))); + Bullets.push_back(std::move(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true)))); m_shoot_acc = BULLET_TIME; } diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 7a019e2..3c5ac23 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -16,8 +16,8 @@ public: Connection( SOCKET sock, sockaddr_in sockaddr, - LoginInfo log, - PlayerInfo play); + LoginInfo *log, + PlayerInfo *play); ~Connection(); Player* player = nullptr; diff --git a/SQCSim-srv/main.cpp b/SQCSim-srv/main.cpp index 98d39f9..74c28e1 100644 --- a/SQCSim-srv/main.cpp +++ b/SQCSim-srv/main.cpp @@ -3,6 +3,11 @@ int main() { std::unique_ptr server = std::make_unique(); if (server->Init() == 0) - if (server->Ready() == 0) + while (server->Ready() == 0) { server->Run(); + if (!server->NewGameRequested()) + break; + server->Cleanup(); + } + server->DeInit(); } \ No newline at end of file diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index c5c283b..f54db3a 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -132,7 +132,7 @@ int Server::Ready() { str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port)); if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) { - PlayerInfo play; + PlayerInfo* play = new PlayerInfo(); m_buf.len = BUFFER_LENGTH; Packet pck = getPack(&m_buf); @@ -153,22 +153,20 @@ int Server::Ready() { sendPackTo(m_sock_udp, log, &m_buf, &sockad); - play.id = getUniqueId(); - play.tid = log->tid; - strcpy(play.name, log->name); + play->id = getUniqueId(); + play->tid = log->tid; + strcpy(play->name, 32, log->name); - Log(str.append(play.name).append(" SID: [").append(std::to_string(log->sid)).append("]") - .append(" ID: [").append(std::to_string(play.id)).append("]") - .append(" TID: [").append(std::to_string(play.tid)).append("]"), false, false); - play.tid = log->tid; + Log(str.append(play->name).append(" SID: [").append(std::to_string(log->sid)).append("]") + .append(" ID: [").append(std::to_string(play->id)).append("]") + .append(" TID: [").append(std::to_string(play->tid)).append("]"), false, false); + play->tid = log->tid; sendPackTo(m_sock_udp, &m_game, &m_buf, &sockad); - Connection* conn = new Connection(sock, sockad, *log, play); + Connection* conn = new Connection(sock, sockad, log, play); m_conns[log->sid] = conn; - delete log; - if (++nbrconn >= nbrjoueurs) readystart = true; } @@ -377,9 +375,44 @@ void Server::Run() { sendPackTo(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. - system("pause"); + + char* ch = new char[2]; + std::cout << "Nouvelle partie? [o/N] "; + std::cin.getline(ch, 2); + std::cout << std::endl; + + m_exit = true; + if (ch[0] == 'o' || ch[0] == 'O') + m_exit = false; + + delete[] ch; } +void Server::Cleanup() { + for (auto& [key, conn] : m_conns) + delete conn; + + m_conns.clear(); + m_players.clear(); + delete m_world; + m_world = nullptr; +} + +void Server::DeInit() { + if (m_logfile.is_open()) + m_logfile.close(); + if (m_sock_udp) + closesocket(m_sock_udp); + if (m_sock_tcp) + closesocket(m_sock_tcp); + +#ifdef _WIN32 + WSACleanup(); +#endif +} + +bool Server::NewGameRequested() const { return !m_exit; } + inline std::string Server::LogTimestamp() { time_t rawtime; tm timeinfo; @@ -460,7 +493,7 @@ std::string Server::getDeathMessage(std::string username, std::string killer) co mess.append(temp.substr(0, ind)); mess.append(username); if (!bypass) { - mess.append(temp.substr(ind + 1, indk)); + mess.append(temp.substr(ind + 1, indk - 1)); mess.append(killer); mess.append(temp.substr(indk + 1)); } @@ -469,7 +502,7 @@ std::string Server::getDeathMessage(std::string username, std::string killer) co else { mess.append(temp.substr(0, indk)); mess.append(killer); - mess.append(temp.substr(indk + 1, ind)); + mess.append(temp.substr(indk + 1, ind - 1)); mess.append(username); mess.append(temp.substr(ind + 1)); } diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index 72c0945..c70a457 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -24,6 +24,9 @@ public: int Init(); int Ready(); void Run(); + void Cleanup(); + void DeInit(); + bool NewGameRequested() const; private: @@ -44,7 +47,7 @@ private: GameInfo m_game; World* m_world = nullptr; - const bool m_manual_setup = SRV_MANUAL_SETUP; + bool m_exit = true; std::string LogTimestamp(); void Log(std::string str, bool is_error, bool is_fatal); diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index f10b094..bcde34e 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -65,7 +65,7 @@ int Connector::Connect(const char* srv_addr, std::string name) { netprot::Buffer bf; netprot::LoginInfo log; - strcpy(log.name, name.c_str()); + strcpy(log.name, 32, name.c_str()); netprot::sendPack(m_sock_tcp, &log, &bf); diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 848528e..32f9721 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -18,7 +18,7 @@ struct Notification { // Use a vector to manage notifications std::vector notifications; -Engine::Engine() : m_remotePlayer(&m_pinfo), m_pinfo() {} +Engine::Engine() {} Engine::~Engine() { m_world.CleanUpWorld(m_renderCount, true); @@ -103,7 +103,7 @@ void Engine::Init() { m_world.SetSeed(seed); m_startTime = std::chrono::high_resolution_clock::now(); - m_remotePlayer.SetPosition(Vector3f(.5, CHUNK_SIZE_Y + 10., .5)); + // Gestion de souris. CenterMouse(); HideCursor(); @@ -1258,7 +1258,7 @@ void Engine::Render(float elapsedTime) { m_mouseWU = m_mouseWD = m_key1 = m_key2 = false; netprot::ChunkMod* cmod = nullptr; - if (m_mouseL) { + if (!m_player.AmIDead() && m_mouseL) { if (bulletTime <= 0.f) { 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]) { @@ -1282,14 +1282,20 @@ void Engine::Render(float elapsedTime) { else if (m_mouseR) cmod = m_world.ChangeBlockAtCursor(BTYPE_METAL, m_player.GetPosition(), m_player.GetDirection(), m_block, m_networkgame); + static netprot::ChunkMod** wat = &m_chunkmod; for (int x = 0; x < MAX_BULLETS; ++x) { // Array de bullets en jeu. if (m_bullets[x]) { for (int b = 0; b < BULLET_UPDATES_PER_FRAME; ++b) { - if (m_bullets[x]->Update(&m_world, elapsedTime, BULLET_UPDATES_PER_FRAME, m_players, m_networkgame ? &m_chunkmod : nullptr)) { + if (m_bullets[x]->Update(&m_world, elapsedTime, BULLET_UPDATES_PER_FRAME, m_players, m_networkgame ? wat : nullptr)) { m_bullets[x]->~Bullet(); + + if (m_chunkmod) { + m_chunkmod_manifest.push_back(std::move(m_chunkmod)); + m_chunkmod = nullptr; + } + m_bullets[x] = nullptr; //if (m_whoosh[x]) // m_whoosh[x]->drop(); - //m_bullets[x] = nullptr; //m_whoosh[x] = nullptr; break; } @@ -1324,7 +1330,7 @@ void Engine::Render(float elapsedTime) { std::string user = m_player.m_username.append(" (Dead)"); m_player = Player(Vector3f(.5, CHUNK_SIZE_Y + 1.7f, .5), 0, 0); m_player.m_username = user; - + m_player.InflictDamage(-m_player.GetHP()); } m_time += elapsedTime; @@ -1349,6 +1355,7 @@ void Engine::Render(float elapsedTime) { sync_acc += tstamp - last; cmod_acc += tstamp - last; + last = tstamp; if (sync_acc >= 1000) { sync_acc -= 1000; @@ -1363,9 +1370,9 @@ void Engine::Render(float elapsedTime) { m_syncs[sync.timestamp] = sync; } - if (cmod_acc >= 1000) { - while (cmod_acc >= 1000) - cmod_acc -= 1000; + if (cmod_acc >= 3000) { + while (cmod_acc >= 3000) + cmod_acc -= 3000; if (!m_chunkmod_manifest.empty()) { ChunkMod* cmod = m_chunkmod_manifest.front(); m_chunkmod_manifest.pop_front(); @@ -1389,8 +1396,9 @@ void Engine::Render(float elapsedTime) { recvPacks(m_conn.m_sock_udp, &m_buf, &lsPck); char* prevptr = nullptr; + Chat chat; for (auto& pck : lsPck) { // We could make a few threads out of this. - Sync sync; Output out; ChunkMod cmod; BulletAdd bull; Chat chat; + Sync sync; Output out; ChunkMod cmod; BulletAdd bull; if (!prevptr) prevptr = m_buf.ptr; uint32_t bsize = m_buf.len - (pck - prevptr); @@ -1406,7 +1414,9 @@ void Engine::Render(float elapsedTime) { if (m_syncs.count(sync.timestamp)) { Sync comp = m_syncs[sync.timestamp]; - //m_player.InflictDamage(sync.hp - comp.hp); + std::cout << sync.hp << std::endl; + + m_player.InflictDamage(sync.hp - comp.hp); Vector3f diff = sync.position - comp.position; @@ -1463,7 +1473,7 @@ void Engine::Render(float elapsedTime) { m_bullets[0] = bult; break; } - m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir * 10, false, .5f); + m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir, false, 1.f); } else SystemNotification("Bullet is kraput."); break; diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index ef0cb73..18f01b5 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -218,9 +218,8 @@ private: std::unordered_map m_players; netprot::Buffer m_buf, m_bufout; netprot::ChunkMod* m_chunkmod = nullptr; - netprot::PlayerInfo m_pinfo; + std::unordered_map m_syncs; - RemotePlayer m_remotePlayer = RemotePlayer(netprot::PlayerInfo(),Vector3f(5.5f, CHUNK_SIZE_Y + 1.8f, 5.5f)); std::string m_messageNotification = ""; };