From b54f8c31e5c6e891adbdc96ade05f2e311e1907e Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Sun, 29 Oct 2023 14:54:36 -0400 Subject: [PATCH] windoes --- SQCSim-common/netprotocol.cpp | 7 +++ SQCSim-common/netprotocol.h | 4 +- SQCSim-common/world.cpp | 6 +++ SQCSim-common/world.h | 3 +- SQCSim-srv/connection.h | 2 +- SQCSim-srv/main.cpp | 3 +- SQCSim-srv/server.cpp | 94 ++++++++++++----------------------- SQCSim-srv/server.h | 4 +- SQCSim2021/connector.cpp | 4 +- 9 files changed, 56 insertions(+), 71 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index b6f0214..d31d425 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -801,6 +801,13 @@ bool netprot::emptyPack(netprot::Packet pck) { } } +netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { + Packet pck; + pck.ptr = ptr; + pck.type = type; + return pck; +} + std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { std::vector lsPck; int len = 0, end = 0; diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 787e0f1..987e5fe 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -166,7 +166,7 @@ namespace netprot { void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { netprot::Serialize(pack, buf, buflen); memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); - buflen += sizeof(uint64_t) + sizeof(uint8_t); + buflen += (sizeof(uint64_t) + sizeof(uint8_t)); send(sock, *buf, *buflen, 0); *buflen = BUFFER_LENGTH; } @@ -176,7 +176,7 @@ namespace netprot { sockaddr_in addr = *sockad; netprot::Serialize(pack, buf, buflen); memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); - buflen += sizeof(uint64_t) + sizeof(uint8_t); + buflen += (sizeof(uint64_t) + sizeof(uint8_t)); sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); *buflen = BUFFER_LENGTH; } diff --git a/SQCSim-common/world.cpp b/SQCSim-common/world.cpp index 1be12e7..7215b6b 100644 --- a/SQCSim-common/world.cpp +++ b/SQCSim-common/world.cpp @@ -6,6 +6,12 @@ World::~World() {} Array2d& World::GetChunks() { return m_chunks; } +void World::BuildWorld() { + for (int x = 0; x < WORLD_SIZE_X; ++x) + for (int y = 0; y < WORLD_SIZE_Y; ++y) + m_chunks.Set(x - m_center[0], y - m_center[1], new Chunk(x + m_center[0], y + m_center[1], m_seed)); +} + void World::SetSeed(uint64_t seed) { m_seed = seed; } diff --git a/SQCSim-common/world.h b/SQCSim-common/world.h index 0cc628d..8a166af 100644 --- a/SQCSim-common/world.h +++ b/SQCSim-common/world.h @@ -21,6 +21,7 @@ public: ~World(); Array2d& GetChunks(); + void BuildWorld(); void SetSeed(uint64_t seed); @@ -45,7 +46,7 @@ private: unsigned int m_center[2] = { UINT16_MAX / 2 - WORLD_SIZE_X, UINT16_MAX / 2 - WORLD_SIZE_Y }; - void UpdateChunk(int& updates, unsigned int chx, unsigned int chy, BlockInfo* blockinfo[BTYPE_LAST]); + //void UpdateChunk(int& updates, unsigned int chx, unsigned int chy, BlockInfo* blockinfo[BTYPE_LAST]); void UpdateWorld(const Vector3f& player, BlockInfo* blockinfo[BTYPE_LAST]); void TransposeWorld(Vector3f& player, Bullet* bullets[MAX_BULLETS]); diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 21bff9b..b7a69ee 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -19,7 +19,7 @@ public: PlayerInfo play); ~Connection(); - Player* player = nullptr; + std::unique_ptr player = nullptr; uint64_t GetHash(bool self = true) const; uint64_t GetTeamHash() const; diff --git a/SQCSim-srv/main.cpp b/SQCSim-srv/main.cpp index a1e104d..98d39f9 100644 --- a/SQCSim-srv/main.cpp +++ b/SQCSim-srv/main.cpp @@ -1,9 +1,8 @@ #include "server.h" int main() { - Server* server = new Server(); + std::unique_ptr server = std::make_unique(); if (server->Init() == 0) if (server->Ready() == 0) server->Run(); - delete server; } \ No newline at end of file diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 045052f..e1b1ff9 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -127,17 +127,16 @@ 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) { - LoginInfo* log; PlayerInfo play; Packet pck = getPack(&m_buf); if (pck.type != PACKET_TYPE::LOGINF) { Log("Paquet invalide.", true, false); if (pck.type != PACKET_TYPE::ERR) - emptyPack(pck); + netprot::emptyPack(pck); continue; // Passer au prochain appel si c'est pas un LoginInfo ou un LoginInfo invalide qui rentre. } - log = (LoginInfo*)pck.ptr; + LoginInfo* log = (LoginInfo*)pck.ptr; log->sid = getUniqueId(); log->tid = 0; // TODO: À changer si on implemente un mode en equipe. @@ -155,14 +154,16 @@ int Server::Ready() { play.tid = log->tid; sendPack(sock, &m_game, &m_buf.ptr, &m_buf.len); - Connection* conn = new Connection(sock, sockad, *log, play); + std::unique_ptr conn = std::make_unique(sock, sockad, *log, play); for (auto& [key, player] : m_players) { sendPack(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d�j� connect�s sendPack(sock, player->getInfo(), &m_buf); // et envoyer les infos des joueurs distants au nouveau joueur. } - m_players[log->sid] = conn; + m_players[log->sid] = std::move(conn); + + delete log; if (++nbrconn >= nbrjoueurs) readystart = true; @@ -179,12 +180,15 @@ void Server::Run() { Log("Debut de la partie...", false, false); - for (auto& [key, conn]: m_players) { // Creation des instances de joueurs et premier sync. - conn->player = new Player(Vector3f(8.5f, CHUNK_SIZE_Y + 1.8f, 8.5f)); - Player *player = conn->player; + m_world = std::make_unique(); + m_world->SetSeed(m_game.seed); + m_world->BuildWorld(); + + for (auto& [key, conn] : m_players) { // Creation des instances de joueurs et premier sync. + conn->player = std::make_unique(Vector3f(8.5f, CHUNK_SIZE_Y + 1.8f, 8.5f)); Sync sync; - sync.position = player->GetPosition(); - sync.hp = player->GetHP(); + sync.position = conn->player->GetPosition(); + sync.hp = conn->player->GetHP(); sync.sid = key; sync.ammo = 0; sync.timestamp = 0; @@ -197,56 +201,25 @@ void Server::Run() { Packet pck = getPack(&m_buf); switch (pck.type) { using enum netprot::PACKET_TYPE; - case ERR: - std::cout << "ERROR!" << std::endl; - break; - case INPUT: - std::cout << "INPUT!" << std::endl; - break; - case OUTPUT: - std::cout << "OUTPUT!" << std::endl; - break; - case SYNC: - std::cout << "SYNC!" << std::endl; - break; - case TEAMINF: - std::cout << "TEAMINF!" << std::endl; - break; - case SELFINF: - std::cout << "SELFINF!" << std::endl; - break; - case PLAYINF: - std::cout << "PLAYINF!" << std::endl; - break; - case LOGINF: - std::cout << "LOGINF!" << std::endl; - break; - case CHUNKMOD: - std::cout << "CHUNKMOD!" << std::endl; - break; - case PLAYERMOD: - std::cout << "PLAYERMOD!" << std::endl; - break; - case PICKUPMOD: - std::cout << "PICKUPMOD!" << std::endl; - break; - case GAMEINFO: - std::cout << "GAMEINFO!" << std::endl; - break; - case ENDINFO: - std::cout << "ENDINFO!" << std::endl; - break; - case CHAT: - std::cout << "CHAT!" << std::endl; - break; - case ERRLOG: - std::cout << "ERRLOG!" << std::endl; - break; - case LAST_PACK: - std::cout << "wtf?!" << std::endl; - break; + case ERR: std::puts("ERROR!"); break; + case INPUT: std::puts("INPUT!"); break; + case OUTPUT: std::puts("OUTPUT!"); break; + case SYNC: std::puts("SYNC!"); break; + case TEAMINF: std::puts("TEAMINF!"); break; + case SELFINF: std::puts("SELFINF!"); break; + case PLAYINF: std::puts("PLAYINF!"); break; + case LOGINF: std::puts("LOGINF!"); break; + case CHUNKMOD: std::puts("CHUNKMOD!"); break; + case PLAYERMOD: std::puts("PLAYERMOD!"); break; + case PICKUPMOD: std::puts("PICKUPMOD!"); break; + case GAMEINFO: std::puts("GAMEINFO!"); break; + case ENDINFO: std::puts("ENDINFO!"); break; + case CHAT: std::puts("CHAT!"); break; + case ERRLOG: std::puts("ERRLOG!"); break; + case LAST_PACK: [[falltrough]]; + default: std::puts("wtf?!"); break; } - emptyPack(pck); + netprot::emptyPack(pck); } } } @@ -290,7 +263,6 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) if (m_sock_tcp) closesocket(m_sock_tcp); for (const auto& [key, player] : m_players) { - delete player->player; closesocket(player->getSock()); } m_players.clear(); @@ -305,7 +277,7 @@ void Server::buildIdList(size_t size) { std::set lst; srand(time(NULL)); - do lst.insert(((uint64_t)rand() << 32 | rand())); // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIiIIiiIiINE! + do lst.insert(((uint64_t)rand() << 32 | rand())); while (lst.size() < size); m_ids = std::vector(lst.begin(), lst.end()); diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index 33b529f..10b7709 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -35,12 +35,12 @@ private: Buffer m_buf; - std::map m_players; + std::map> m_players; std::map m_chatlog; std::vector m_ids; GameInfo m_game; - World* m_world = nullptr; + std::unique_ptr m_world = nullptr; const bool m_manual_setup = SRV_MANUAL_SETUP; std::string LogTimestamp(); diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index d4119a7..57912cb 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -56,12 +56,12 @@ int Connector::Connect(const char* srv_addr, std::string name) { std::cout << "Échec de la connexion." << std::endl; return 2; } - + /* if (ioctl(m_sock_tcp, SOCK_NONBLOCK, nullptr) < 0) { std::cout << "Impossible de mettre le socket en mode non-bloquant." << std::endl; return 3; } - + */ netprot::Buffer bf; netprot::LoginInfo log; strcpy(log.name, name.c_str());