bulletadd
This commit is contained in:
@@ -18,9 +18,9 @@ Server::~Server() {
|
||||
closesocket(m_sock_udp);
|
||||
if (m_sock_tcp)
|
||||
closesocket(m_sock_tcp);
|
||||
for (const auto& [key, player] : m_players)
|
||||
for (const auto& [key, player] : m_conns)
|
||||
closesocket(player->getSock());
|
||||
m_players.clear();
|
||||
m_conns.clear();
|
||||
delete m_world;
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
@@ -165,7 +165,7 @@ int Server::Ready() {
|
||||
std::cout << m_game.seed << std::endl;
|
||||
Connection* conn = new Connection(sock, sockad, *log, play);
|
||||
|
||||
m_players[log->sid] = conn;
|
||||
m_conns[log->sid] = conn;
|
||||
|
||||
delete log;
|
||||
|
||||
@@ -174,8 +174,8 @@ int Server::Ready() {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto& [keyin, playin] : m_players) // Not pretty, but it works.
|
||||
for (auto& [keyout, playout] : m_players) {
|
||||
for (auto& [keyin, playin] : m_conns) // Not pretty, but it works.
|
||||
for (auto& [keyout, playout] : m_conns) {
|
||||
if (keyin == keyout)
|
||||
continue;
|
||||
sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur.
|
||||
@@ -192,17 +192,18 @@ void Server::Run() {
|
||||
|
||||
Log("Debut de la partie...", false, false);
|
||||
|
||||
int players = m_players.size();
|
||||
int players = m_conns.size();
|
||||
|
||||
m_world = new World();
|
||||
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.
|
||||
for (auto& [key, conn] : m_conns) { // Creation des instances de joueurs et premier sync.
|
||||
int x = (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1)) - (CHUNK_SIZE_X * WORLD_SIZE_X / 2),
|
||||
y = (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1)) - (CHUNK_SIZE_Y * WORLD_SIZE_Y / 2);
|
||||
conn->player = std::make_unique<Player>(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
|
||||
conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
|
||||
m_players[key] = conn->player;
|
||||
Sync sync;
|
||||
sync.position = conn->player->GetPositionAbs();
|
||||
sync.hp = conn->player->GetHP();
|
||||
@@ -212,11 +213,14 @@ void Server::Run() {
|
||||
sync.timer = m_game.countdown;
|
||||
sendPackTo<Sync>(m_sock_udp, &sync, &m_buf, conn->getAddr());
|
||||
}
|
||||
|
||||
|
||||
int timer = m_game.countdown, sync_acc = 0;
|
||||
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
Timestamp last = 0;
|
||||
std::vector<Chat> chatlog;
|
||||
std::vector<ChunkMod*> chunkdiffs;
|
||||
std::vector<Bullet*> bullets, outbox_bullets;
|
||||
std::vector<BulletAdd*> netbull;
|
||||
|
||||
while (!endgame) {
|
||||
using namespace std::chrono;
|
||||
@@ -230,13 +234,14 @@ void Server::Run() {
|
||||
--timer;
|
||||
}
|
||||
|
||||
for (auto& [key, conn] : m_players) {
|
||||
for (auto& [key, conn] : m_conns) {
|
||||
|
||||
/* In */
|
||||
|
||||
int deadplayers = 0;
|
||||
std::vector<char*> lsPck;
|
||||
Input in; Chat chat; Sync sync;
|
||||
|
||||
lsPck = recvPacks(m_sock_udp, &m_buf);
|
||||
for (auto& pck : lsPck) {
|
||||
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
|
||||
@@ -244,7 +249,7 @@ void Server::Run() {
|
||||
using enum netprot::PACKET_TYPE;
|
||||
case INPUT:
|
||||
if (Deserialize(&in, pck, &bsize))
|
||||
m_players[in.sid]->AddInput(in);
|
||||
m_conns[in.sid]->AddInput(in);
|
||||
break;
|
||||
case SYNC:
|
||||
if (Deserialize(&sync, pck, &bsize)) {}
|
||||
@@ -264,25 +269,80 @@ void Server::Run() {
|
||||
if (conn->player->AmIDead()) {
|
||||
++deadplayers;
|
||||
conn->m_nsync == false;
|
||||
continue;
|
||||
}
|
||||
conn->Run(m_world);
|
||||
else {
|
||||
Timestamp tstamp = conn->Run(m_world);
|
||||
|
||||
for (auto& chmo : conn->ChunkDiffs)
|
||||
chunkdiffs.emplace_back(chmo);
|
||||
conn->ChunkDiffs.clear();
|
||||
|
||||
for (auto& bull : conn->Bullets) {
|
||||
bullets.emplace_back(bull);
|
||||
BulletAdd* nbul = new BulletAdd();
|
||||
nbul->pos = conn->player->GetPosition();
|
||||
nbul->dir = conn->player->GetDirection();
|
||||
nbul->id = key;
|
||||
nbul->tstamp = tstamp;
|
||||
|
||||
netbull.emplace_back(nbul);
|
||||
}
|
||||
conn->Bullets.clear();
|
||||
}
|
||||
|
||||
/* Out */
|
||||
|
||||
conn->sendPacks(m_sock_udp, m_players, timer);
|
||||
conn->sendPacks(m_sock_udp, m_conns, timer);
|
||||
}
|
||||
|
||||
if (deadplayers == players - 1 || timer <= 0)
|
||||
endgame = true;
|
||||
}
|
||||
|
||||
for (auto& bull : netbull) {
|
||||
for (auto& [key, conn] : m_conns)
|
||||
sendPackTo<BulletAdd>(m_sock_udp, bull, &m_buf, conn->getAddr());
|
||||
}
|
||||
|
||||
for (auto& bull : bullets) {
|
||||
ChunkMod* cmod = nullptr;
|
||||
if (bull->Update(m_world, (1. / 60.), 20, m_players, &cmod)) {
|
||||
if (cmod)
|
||||
chunkdiffs.emplace_back(cmod);
|
||||
outbox_bullets.emplace_back(std::move(bull));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& chat : chatlog)
|
||||
for (auto& [key, conn] : m_players)
|
||||
for (auto& [key, conn] : m_conns)
|
||||
sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr());
|
||||
|
||||
for (auto& chmo : chunkdiffs) {
|
||||
for (auto& [key, conn] : m_conns)
|
||||
sendPackTo<ChunkMod>(m_sock_udp, chmo, &m_buf, conn->getAddr());
|
||||
delete chmo;
|
||||
}
|
||||
|
||||
for (auto& bull : outbox_bullets)
|
||||
delete bull;
|
||||
|
||||
for (auto& bull : netbull)
|
||||
delete bull;
|
||||
|
||||
outbox_bullets.clear();
|
||||
netbull.clear();
|
||||
chunkdiffs.clear();
|
||||
chatlog.clear();
|
||||
}
|
||||
|
||||
Chat end;
|
||||
end.src_id = 0;
|
||||
char endmess[] = "Game over, motherfuckers.";
|
||||
|
||||
strcpy(end.mess, endmess);
|
||||
|
||||
for (auto& [key, conn] : m_conns)
|
||||
sendPackTo<Chat>(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.
|
||||
|
||||
}
|
||||
@@ -325,11 +385,11 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
|
||||
closesocket(m_sock_udp);
|
||||
if (m_sock_tcp)
|
||||
closesocket(m_sock_tcp);
|
||||
for (const auto& [key, player] : m_players) {
|
||||
for (const auto& [key, player] : m_conns) {
|
||||
closesocket(player->getSock());
|
||||
}
|
||||
delete m_world;
|
||||
m_players.clear();
|
||||
m_conns.clear();
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user