This commit is contained in:
MarcEricMartel 2023-11-28 11:38:38 -05:00
parent e89b5914fb
commit 5c346c48e6
3 changed files with 67 additions and 61 deletions

View File

@ -170,11 +170,10 @@ int Server::Ready() {
}
}
}
for (auto& [keyin, playin] : m_players)
for (auto& [keyin, playin] : m_players) // Not pretty, but it works.
for (auto& [keyout, playout] : m_players) {
if (keyin == keyout)
continue;
//sendPackTo<PlayerInfo>(m_sock_udp, &play, &m_buf, &sockad); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
continue;
sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur.
}
@ -186,7 +185,7 @@ void Server::Run() {
Input in;
sockaddr_in sockad;
addrlen_t socklen = sizeof(sockad);
Log("Debut de la partie...", false, false);
int players = m_players.size();
@ -195,9 +194,10 @@ void Server::Run() {
m_world->SetSeed(m_game.seed);
m_world->GetChunks().Reset(nullptr);
m_world->BuildWorld();
for (auto& [key, conn] : m_players) { // Creation des instances de joueurs et premier sync.
conn->player = std::make_unique<Player>(Vector3f(.5f, CHUNK_SIZE_Y + 1.8f, .5f));
int x = rand() % (CHUNK_SIZE_X + WORLD_SIZE_X - 1), y = rand() % (CHUNK_SIZE_Y + WORLD_SIZE_Y - 1);
conn->player = std::make_unique<Player>(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
Sync sync;
sync.position = conn->player->GetPositionAbs();
sync.hp = conn->player->GetHP();
@ -206,65 +206,67 @@ void Server::Run() {
sync.timestamp = 0;
sync.timer = m_game.countdown;
sendPackTo<Sync>(m_sock_udp, &sync, &m_buf, conn->getAddr());
}
while (!endgame) {
for (auto& [key, conn] : m_players) {
std::vector<char*> lsPck;
Input in;
Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf);
for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
switch (netprot::getType(pck, 1)) {
using enum netprot::PACKET_TYPE;
case INPUT:
if (Deserialize(&in, pck, &bsize)) {
m_players[in.sid]->AddInput(in);
}
break;
case SYNC:
if (Deserialize(&sync, pck, &bsize)) {}
break;
default: break;
}
int timer = m_game.countdown, sync_acc;
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
Timestamp last = 0;
int sync_acc = 0;
while (!endgame) {
using namespace std::chrono;
Timestamp tstamp = duration_cast<milliseconds>(high_resolution_clock::now() - start).count();
if (last == 0)
last = tstamp;
sync_acc += tstamp - last;
if (sync_acc >= 1000) {
sync_acc -= 1000;
--timer;
}
lsPck.clear();
if (conn->m_nsync) {
conn->Run(m_world);
conn->sendPacks(m_sock_udp, m_players, 0);
for (auto& [key, conn] : m_players) {
int deadplayers = 0;
std::vector<char*> lsPck;
Input in;
Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf);
for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
switch (netprot::getType(pck, 1)) {
using enum netprot::PACKET_TYPE;
case INPUT:
if (Deserialize(&in, pck, &bsize)) {
m_players[in.sid]->AddInput(in);
}
break;
case SYNC:
if (Deserialize(&sync, pck, &bsize)) {}
break;
case CHAT:
// TODO: Pitcher les chats à tous le monde.
break;
default: break;
}
}
lsPck.clear();
if (conn->m_nsync) {
if (conn->player->AmIDead()) {
++deadplayers;
continue;
}
conn->Run(m_world);
conn->sendPacks(m_sock_udp, m_players, timer);
}
if (deadplayers == players - 1 || timer <= 0)
endgame = true;
}
}
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
}
//while (true) {
// if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) {
// Packet pck = getPack(&m_buf);
// switch (pck.type) {
// using enum netprot::PACKET_TYPE;
// 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;
// }
// netprot::emptyPack(pck);
// }
//}
}
inline std::string Server::LogTimestamp() {

View File

@ -99,7 +99,6 @@ int Connector::Connect(const char* srv_addr, std::string name) {
++errors;
else {
m_players[pl->id] = pl;
std::cout << pl->name << std::endl;
}
break;
case TEAMINF:

View File

@ -373,7 +373,7 @@ void Engine::Init() {
if (!m_conn.Connect(srvname.c_str(), playname)) {
// setup jeu en reseau.
std::cout << "ID recu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl;
std::cout << "Seed recu du serveur: " << std::to_string(m_conn.getSeed()) << "!" << std::endl;
//std::cout << "Seed recu du serveur: " << std::to_string(m_conn.getSeed()) << "!" << std::endl;
m_player = Player(m_conn.getOrigin().position);
for (auto& [key, player] : m_conn.m_players) {
@ -1335,6 +1335,9 @@ void Engine::Render(float elapsedTime) {
m_player.InflictDamage(sync.hp - comp.hp);
Vector3f diff = sync.position - comp.position;
if (diff.y < 1.)
diff.y = 0;
//std::cout << sync.timestamp << " ms: " << comp.position << " - " << sync.position << " Diff: " << diff.Length() << std::endl;
@ -1344,6 +1347,8 @@ void Engine::Render(float elapsedTime) {
}
// TODO: Syncer sync.timer avec le timer
m_syncs.erase(sync.timestamp);
}
else std::cout << "sync be no good.";
}