SQC-15_online #1

Merged
memartel_loc merged 349 commits from SQC-15_online into master 2023-12-10 17:41:26 -05:00
9 changed files with 56 additions and 71 deletions
Showing only changes of commit b54f8c31e5 - Show all commits

View File

@ -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::Packet> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<Packet> lsPck;
int len = 0, end = 0;

View File

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

View File

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

View File

@ -21,6 +21,7 @@ public:
~World();
Array2d<Chunk*>& 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]);

View File

@ -19,7 +19,7 @@ public:
PlayerInfo play);
~Connection();
Player* player = nullptr;
std::unique_ptr<Player> player = nullptr;
uint64_t GetHash(bool self = true) const;
uint64_t GetTeamHash() const;

View File

@ -1,9 +1,8 @@
#include "server.h"
int main() {
Server* server = new Server();
std::unique_ptr<Server> server = std::make_unique<Server>();
if (server->Init() == 0)
if (server->Ready() == 0)
server->Run();
delete server;
}

View File

@ -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<GameInfo>(sock, &m_game, &m_buf.ptr, &m_buf.len);
Connection* conn = new Connection(sock, sockad, *log, play);
std::unique_ptr<Connection> conn = std::make_unique<Connection>(sock, sockad, *log, play);
for (auto& [key, player] : m_players) {
sendPack<PlayerInfo>(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
sendPack<PlayerInfo>(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<World>();
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<Player>(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<uint64_t> 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<uint64_t>(lst.begin(), lst.end());

View File

@ -35,12 +35,12 @@ private:
Buffer m_buf;
std::map<uint64_t, Connection*> m_players;
std::map<uint64_t, std::unique_ptr<Connection>> m_players;
std::map <Timestamp, Chat> m_chatlog;
std::vector<uint64_t> m_ids;
GameInfo m_game;
World* m_world = nullptr;
std::unique_ptr<World> m_world = nullptr;
const bool m_manual_setup = SRV_MANUAL_SETUP;
std::string LogTimestamp();

View File

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