🏥
This commit is contained in:
parent
e89b5914fb
commit
5c346c48e6
@ -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
|
||||
sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur.
|
||||
}
|
||||
|
||||
@ -197,7 +196,8 @@ void Server::Run() {
|
||||
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,10 +206,26 @@ void Server::Run() {
|
||||
sync.timestamp = 0;
|
||||
sync.timer = m_game.countdown;
|
||||
sendPackTo<Sync>(m_sock_udp, &sync, &m_buf, conn->getAddr());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (auto& [key, conn] : m_players) {
|
||||
int deadplayers = 0;
|
||||
std::vector<char*> lsPck;
|
||||
Input in;
|
||||
Sync sync;
|
||||
@ -226,46 +242,32 @@ void Server::Run() {
|
||||
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, 0);
|
||||
conn->sendPacks(m_sock_udp, m_players, timer);
|
||||
}
|
||||
|
||||
if (deadplayers == players - 1 || timer <= 0)
|
||||
endgame = true;
|
||||
}
|
||||
}
|
||||
|
||||
//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);
|
||||
// }
|
||||
//}
|
||||
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string Server::LogTimestamp() {
|
||||
time_t rawtime;
|
||||
|
@ -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:
|
||||
|
@ -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) {
|
||||
@ -1336,6 +1336,9 @@ void Engine::Render(float elapsedTime) {
|
||||
|
||||
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;
|
||||
|
||||
if (diff.Length() > 1.5) {
|
||||
@ -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.";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user