bulletadd
This commit is contained in:
@@ -95,7 +95,7 @@ void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*
|
||||
if (outs >= 1000) {
|
||||
outs -= 1000;
|
||||
Sync sync;
|
||||
sync.hp = player.get()->GetHP();
|
||||
sync.hp = player->GetHP();
|
||||
sync.timestamp = out.timestamp;
|
||||
sync.position = out.position;
|
||||
sync.sid = m_loginfo.sid;
|
||||
@@ -109,13 +109,14 @@ void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::Run(World* world) {
|
||||
Timestamp Connection::Run(World* world) {
|
||||
Input in, last;
|
||||
Output out;
|
||||
Timestamp tstamp = 0;
|
||||
float el;
|
||||
|
||||
if (m_input_manifest.size() < 2)
|
||||
return;
|
||||
return tstamp;
|
||||
|
||||
while (m_last_in < m_input_vector.size() - 1) {
|
||||
in = m_input_vector.at(m_last_in + 1);
|
||||
@@ -129,30 +130,39 @@ void Connection::Run(World* world) {
|
||||
m_shoot_acc = 0;
|
||||
}
|
||||
|
||||
player.get()->SetDirection(in.direction);
|
||||
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward,
|
||||
in.keys.backward,
|
||||
in.keys.left,
|
||||
in.keys.right,
|
||||
in.keys.jump, false, el), world, el);
|
||||
player->SetDirection(in.direction);
|
||||
player->ApplyPhysics(player->GetInput(in.keys.forward,
|
||||
in.keys.backward,
|
||||
in.keys.left,
|
||||
in.keys.right,
|
||||
in.keys.jump, false, el), world, el);
|
||||
|
||||
//if (in.keys.block)
|
||||
// ChunkDiffs.push_back()
|
||||
if (in.keys.block) {
|
||||
bool block = false;
|
||||
ChunkMod* cmod = world->ChangeBlockAtCursor(BLOCK_TYPE::BTYPE_METAL,
|
||||
player->GetPosition(),
|
||||
player->GetDirection(),
|
||||
block, true);
|
||||
if (cmod)
|
||||
ChunkDiffs.emplace_back(cmod);
|
||||
}
|
||||
|
||||
if (in.keys.shoot && m_shoot_acc <= 0.)
|
||||
Bullets.push_back(Bullet(player.get()->GetPOV() + player.get()->GetDirection(), player.get()->GetDirection()));
|
||||
Bullets.push_back(Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection()));
|
||||
|
||||
|
||||
out.position = player.get()->GetPositionAbs();
|
||||
out.position = player->GetPositionAbs();
|
||||
out.direction = in.direction;
|
||||
out.timestamp = in.timestamp;
|
||||
out.id = m_playinfo.id;
|
||||
|
||||
m_output_manifest[out.timestamp] = out;
|
||||
m_output_vector.push_back(out);
|
||||
tstamp = out.timestamp;
|
||||
|
||||
++m_last_in;
|
||||
}
|
||||
|
||||
return tstamp;
|
||||
}
|
||||
|
||||
void Connection::CleanInputManifest(Timestamp time) {
|
||||
@@ -161,3 +171,5 @@ void Connection::CleanInputManifest(Timestamp time) {
|
||||
// while (wat != m_input_manifest.begin())
|
||||
// m_input_manifest.erase(wat--);
|
||||
}
|
||||
|
||||
Timestamp Connection::GetTStamp() const { return m_tstamp; }
|
||||
|
@@ -20,7 +20,7 @@ public:
|
||||
PlayerInfo play);
|
||||
~Connection();
|
||||
|
||||
std::unique_ptr<Player> player = nullptr;
|
||||
Player* player = nullptr;
|
||||
|
||||
uint64_t GetHash(bool self = true) const;
|
||||
uint64_t GetTeamHash() const;
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
void getPacks(SOCKET sock);
|
||||
void sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns, const uint32_t timer);
|
||||
|
||||
void Run(World* world);
|
||||
Timestamp Run(World* world);
|
||||
|
||||
void CleanInputManifest(Timestamp time);
|
||||
|
||||
@@ -45,6 +45,8 @@ public:
|
||||
std::vector<Bullet> Bullets;
|
||||
std::vector<ChunkMod> ChunkDiffs;
|
||||
|
||||
Timestamp GetTStamp() const;
|
||||
|
||||
private:
|
||||
std::unordered_map<Timestamp, Input> m_input_manifest;
|
||||
std::vector<Input> m_input_vector;
|
||||
@@ -53,6 +55,7 @@ private:
|
||||
std::unordered_map<Timestamp, Chat> m_chatlog;
|
||||
|
||||
float m_shoot_acc = 0;
|
||||
Timestamp m_tstamp = 0;
|
||||
|
||||
SOCKET m_sock;
|
||||
sockaddr_in m_addr;
|
||||
|
@@ -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
|
||||
|
@@ -36,7 +36,8 @@ private:
|
||||
|
||||
Buffer m_buf;
|
||||
|
||||
std::unordered_map<uint64_t, Connection*> m_players;
|
||||
std::unordered_map<uint64_t, Player*> m_players;
|
||||
std::unordered_map<uint64_t, Connection*> m_conns;
|
||||
std::unordered_map<Timestamp, Chat> m_chatlog;
|
||||
std::vector<uint64_t> m_ids;
|
||||
GameInfo m_game;
|
||||
|
Reference in New Issue
Block a user