From c975265901bad7709c25558f8ff80ef24f6fded3 Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Wed, 6 Dec 2023 11:16:39 -0500 Subject: [PATCH] Timer iznogoud. --- SQCSim-common/bullet.cpp | 21 ++++++++++++---- SQCSim-common/bullet.h | 4 +-- SQCSim-common/netprotocol.cpp | 4 +-- SQCSim-common/player.cpp | 19 ++++++--------- SQCSim-common/player.h | 4 +++ SQCSim-srv/connection.cpp | 14 +++++++++-- SQCSim-srv/connection.h | 4 +-- SQCSim-srv/define.h | 11 +++++++-- SQCSim-srv/server.cpp | 46 ++++++++++++++++++++++++----------- SQCSim-srv/server.h | 2 +- SQCSim2021/engine.cpp | 41 ++++++++++++++++++++++--------- SQCSim2021/engine.h | 2 +- 12 files changed, 118 insertions(+), 54 deletions(-) diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp index b5a38e4..b2731f6 100644 --- a/SQCSim-common/bullet.cpp +++ b/SQCSim-common/bullet.cpp @@ -3,7 +3,7 @@ Bullet::Bullet(Vector3f pos, Vector3f dir) : m_startpos(pos), m_currentpos(pos), m_velocity(dir) {} -Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_currentpos(pos), m_velocity(dir), m_tid(tid) {} +Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t shooter_id): m_startpos(pos), m_currentpos(pos), m_velocity(dir), m_shooter_id(shooter_id) {} Bullet::~Bullet() {} @@ -14,8 +14,19 @@ bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordere m_currentpos += m_velocity * elapsedtime; for (auto& [key, player] : mapPlayer) { - if ((m_currentpos - player->GetPosition()).Length() < .4f) { + bool hit = false; + if ((m_currentpos - player->GetPosition()).Length() < .6f) + hit = true; + if ((m_currentpos - player->GetPOV()).Length() < .2f) { + damage *= 2; // HEADSHOT! + hit = true; + } + if (hit && !player->AmIDead()) { player->InflictDamage(damage); + + if (player->AmIDead()) + player->Killer = m_shooter_id; + return true; } } @@ -56,6 +67,6 @@ Vector3f Bullet::getVel() const { return m_velocity; } -uint64_t Bullet::getTeamID(){ - return m_tid; -} +//uint64_t Bullet::getTeamID(){ +// return m_tid; +//} diff --git a/SQCSim-common/bullet.h b/SQCSim-common/bullet.h index 34b21bf..3f875d9 100644 --- a/SQCSim-common/bullet.h +++ b/SQCSim-common/bullet.h @@ -20,13 +20,13 @@ public: void Transpose(int& x, int& z); Vector3f getPos() const; Vector3f getVel() const; - uint64_t getTeamID(); + //uint64_t getTeamID(); private: Vector3f m_startpos, m_currentpos, m_velocity; - uint64_t m_tid = 0; + uint64_t m_shooter_id = 0; }; diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index e087313..bcd03a7 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -447,7 +447,7 @@ void netprot::Serialize(BulletAdd* bull, char* buf[], uint32_t* buflen) { memcpy(*buf + 1, ts8, sizeof(uint64_t)); - uint64_t tid = bull->tid; + uint64_t tid = bull->id; uint8_t tid8[sizeof(uint64_t)] = { (uint8_t)((tid >> 56) & 0xFF), (uint8_t)((tid >> 48) & 0xFF), @@ -964,7 +964,7 @@ bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) { (uint64_t)tst[7]; memcpy(tst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t)); - bull->tid = + bull->id = (uint64_t)tst[0] << 56 | (uint64_t)tst[1] << 48 | (uint64_t)tst[2] << 40 | diff --git a/SQCSim-common/player.cpp b/SQCSim-common/player.cpp index 6e18197..dae069e 100644 --- a/SQCSim-common/player.cpp +++ b/SQCSim-common/player.cpp @@ -282,20 +282,17 @@ bool Player::AmIDead() } -void Player::InflictDamage(float hitPoints) -{ - +void Player::InflictDamage(float hitPoints) { m_hp -= hitPoints; - - - if (AmIDead()) - { // Quand le joueur est mort. - - - - } + //if (AmIDead()) + //{ // Quand le joueur est mort. + //} } +int Player::getScore() const { return m_score; } + +void Player::addPoint() { ++m_score; } + uint64_t Player::getId() const { return id; } diff --git a/SQCSim-common/player.h b/SQCSim-common/player.h index b7f02db..ece5699 100644 --- a/SQCSim-common/player.h +++ b/SQCSim-common/player.h @@ -37,6 +37,9 @@ public: bool AmIDead(); void InflictDamage(float hitPoints); + int getScore() const; + void addPoint(); + uint64_t Killer = 0; private: uint64_t getId() const; @@ -48,6 +51,7 @@ protected: std::string m_username; uint64_t id = 0; + int m_score = 0; float m_rotX = 0; float m_rotY = 0; diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 941c7f1..3ced157 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -137,9 +137,19 @@ Timestamp Connection::Run(World* world) { in.keys.right, in.keys.jump, false, el), world, el); + if (player->GetPosition().y < -10.) { + player->InflictDamage(9000); + player->Killer = GetHash(true); + } + out.states.jumping = in.keys.jump; out.states.running = player->GetVelocity().Length() > .3f; - out.states.dead = player->AmIDead(); + + if (player->AmIDead()) { + in.keys.shoot = false; + in.keys.block = false; + out.states.dead = true; + } if (in.keys.block) { bool block = false; @@ -152,7 +162,7 @@ Timestamp Connection::Run(World* world) { } if (in.keys.shoot && m_shoot_acc <= 0.) { - Bullets.push_back(Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection())); + Bullets.emplace_back(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true))); out.states.shooting = true; } diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 091e7f4..7a019e2 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -42,8 +42,8 @@ public: bool m_nsync = true; - std::vector Bullets; - std::vector ChunkDiffs; + std::vector Bullets; + std::vector ChunkDiffs; Timestamp GetTStamp() const; diff --git a/SQCSim-srv/define.h b/SQCSim-srv/define.h index f92b4bc..a334446 100644 --- a/SQCSim-srv/define.h +++ b/SQCSim-srv/define.h @@ -11,13 +11,20 @@ #define ID_LIST_SIZE 127 #define SRV_MANUAL_SETUP true +// @ = Dead guy, $ = Killer. const std::vector DEATHMESSAGES = { "@ has gone to meet their maker.", "@ has bit the dust, if you know what I mean.", "@ has ceased to be.", "@ is no more.", "@ is like, super dead.", - "RIP, @.", + "Requiescat In Pace, @.", "So long, @, and thanks for all the lols!", - "@ has a bad case of being dead." }; + "@ has a bad case of being dead.", + "@ has finally seen the light!", + "Thought @ was hot; guess what? He's not. He is dead, dead, dead.", + "@ did not want to live forever.", + "$ made @ die for their country.", + "$ has become death, destroyer of @.", + "$ did not make @ feel lucky." }; #endif diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 81ab798..aabc548 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -200,8 +200,8 @@ void Server::Run() { m_world->BuildWorld(); 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); + int x = 0,// (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1)),// -(CHUNK_SIZE_X * WORLD_SIZE_X / 2), + y = 0;// (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1));// -(CHUNK_SIZE_Y * WORLD_SIZE_Y / 2); conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f)); m_players[key] = conn->player; Sync sync; @@ -270,8 +270,10 @@ void Server::Run() { if (conn->player->AmIDead()) { Chat chat; chat.dest_id = chat.dest_team_id = chat.src_id = 0; + + std::string killer = m_conns.at(key)->player->GetUsername(); - strcpy(chat.mess, getDeathMessage(conn->player->GetUsername()).c_str()); + strcpy(chat.mess, getDeathMessage(conn->player->GetUsername(), killer).c_str()); m_chatlog[tstamp] = chat; @@ -285,9 +287,10 @@ void Server::Run() { for (auto& bull : conn->Bullets) { bullets.emplace_back(bull); + Log("POW!", false, false); BulletAdd* nbul = new BulletAdd(); - nbul->pos = conn->player->GetPosition(); - nbul->dir = conn->player->GetDirection(); + nbul->pos = bull->getPos(); + nbul->dir = bull->getVel(); nbul->id = key; nbul->tstamp = tstamp; @@ -300,10 +303,10 @@ void Server::Run() { conn->sendPacks(m_sock_udp, m_conns, timer); } - if (deadplayers == players - 1 || timer <= 0) - endgame = true; + //if (/*(deadplayers == players - 1 && deadplayers != 0) || */timer <= 0) + //endgame = true; } - + for (auto& bull : netbull) for (auto& [key, conn] : m_conns) if (bull->id != conn->GetHash(false)) // Pour pas repitcher au joueur sa propre balle. @@ -319,9 +322,11 @@ void Server::Run() { } } - for (auto& chat : chatlog) - for (auto& [key, conn] : m_conns) + for (auto& chat : chatlog) { + Log(chat.mess, false, false); + for (auto& [key, conn] : m_conns) sendPackTo(m_sock_udp, &chat, &m_buf, conn->getAddr()); + } for (auto& chmo : chunkdiffs) { for (auto& [key, conn] : m_conns) @@ -420,13 +425,26 @@ uint64_t Server::getUniqueId() { return id; } -std::string Server::getDeathMessage(std::string username) const { +std::string Server::getDeathMessage(std::string username, std::string killer) const { std::string mess; std::string temp = DEATHMESSAGES.at(rand() % DEATHMESSAGES.size()); size_t ind = temp.find('@'); - mess.append(temp.substr(0, ind)); - mess.append(username)); - mess.append(temp.substr(ind + 1)); + size_t indk = temp.find('$'); + + if (ind < indk) { + mess.append(temp.substr(0, ind)); + mess.append(username); + mess.append(temp.substr(ind + 1, indk)); + mess.append(killer); + mess.append(temp.substr(indk + 1)); + } + else { + mess.append(temp.substr(0, indk)); + mess.append(killer); + mess.append(temp.substr(indk + 1, ind)); + mess.append(username); + mess.append(temp.substr(ind + 1)); + } return mess; } diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index b4f2d55..33fde93 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -50,7 +50,7 @@ private: void buildIdList(size_t size); uint64_t getUniqueId(); - std::string getDeathMessage(std::string username) const; + std::string getDeathMessage(std::string username, std::string killer) const; }; diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 3ab80e7..d623682 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -530,12 +530,12 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) { glPushMatrix(); int timer = GetCountdown(elapsedTime); - for (int i = 1; i < WORLD_SIZE_X; i++) { + /*for (int i = 1; i < WORLD_SIZE_X; i++) { if (timer <= COUNTDOWN - m_timerReductionChunk * i) { m_world.RemoveChunk(m_nbReductionChunk * i); m_renderer.RemoveChunk(m_nbReductionChunk * i); } - } + }*/ if (m_keyK) { SystemNotification(m_messageNotification); m_keyK = false; @@ -663,8 +663,8 @@ int Engine::GetCountdown(float elapsedTime) { m_countdown = m_time + COUNTDOWN; m_resetcountdown = false; } - if (m_countdown < m_time) - Stop(); + if (m_countdown < m_time) {} + //Stop(); if (!m_stopcountdown) m_time += elapsedTime; return m_countdown - (int)m_time; @@ -1409,10 +1409,12 @@ void Engine::Render(float elapsedTime) { sendPackTo(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr); + std::cout << "."; + lsPck = recvPacks(m_conn.m_sock_udp, &m_buf); char* prevptr = nullptr; for (auto& pck : lsPck) { // We could make a few threads out of this. - Sync sync; Output out; ChunkMod cmod; BulletAdd bull; + Sync sync; Output out; ChunkMod cmod; BulletAdd bull; Chat chat; if (!prevptr) prevptr = m_buf.ptr; uint32_t bsize = m_buf.len - (pck - prevptr); @@ -1420,9 +1422,10 @@ void Engine::Render(float elapsedTime) { switch (getType(pck, 1)) { using enum PACKET_TYPE; case SYNC: + std::cout << "snyc"; if (Deserialize(&sync, pck, &bsize)) { if (sync.sid != m_conn.getId()) { - std::cout << "syncsid be no good."; + SystemNotification("syncsid be no good."); break; } if (m_syncs.count(sync.timestamp)) { @@ -1439,18 +1442,18 @@ void Engine::Render(float elapsedTime) { m_player.Move(-diff); } - // TODO: Syncer sync.timer avec le timer + m_countdown = sync.timer; m_syncs.erase(sync.timestamp); } - else std::cout << "sync be no good."; + else SystemNotification("sync be no good."); } break; case OUTPUT: + std::cout << "out"; if (Deserialize(&out, pck, &bsize)) { if (!m_players.contains(out.id)) { - - std::cout << out.id << " is id no good." << std::endl; + SystemNotification(std::to_string(out.id).append(" is id no good.")); break; } RemotePlayer* rt = static_cast(m_players[out.id]); @@ -1458,6 +1461,7 @@ void Engine::Render(float elapsedTime) { } break; case CHUNKMOD: + std::cout << "cmod"; if (Deserialize(&cmod, pck, &bsize)) { if (!std::erase_if(m_chunkmod_manifest, // Efface le chunkmod du manifeste s'il est dedans et reset le countdown, sinon fait la modification. [cmod](ChunkMod* c) { @@ -1468,10 +1472,12 @@ void Engine::Render(float elapsedTime) { m_world.ChangeBlockAtPosition(cmod.b_type, cmod.pos); else cmod_acc = 0; } + else SystemNotification("cmod iznogoud."); break; case BULLET: + std::cout << "pow"; if (Deserialize(&bull, pck, &bsize)) { - Bullet* bult = new Bullet(bull.pos, bull.dir, bull.id); + Bullet* bult = new Bullet(bull.pos, bull.dir); for (int x = 0; x < MAX_BULLETS; ++x) // Ajouter une balle dans l'array (aussi connu sous le nom de "faire pow pow"). if (!m_bullets[x]) { m_bullets[x] = bult; @@ -1483,14 +1489,23 @@ void Engine::Render(float elapsedTime) { } m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir * 10, false, .5f); } + else SystemNotification("Bullet is kraput."); + break; + case CHAT: + std::cout << "cat"; + if (Deserialize(&chat, pck, &bsize)) + SystemNotification(chat.mess); + else SystemNotification("Chat iznogoud."); break; default: - std::cout << "packet be no good."; + std::cout << "wtf"; + SystemNotification("packet be no good."); break; } } lsPck.clear(); + glDisable(GL_CULL_FACE); for (auto& [key, player] : m_players) { RemotePlayer* rt = static_cast(player); @@ -1607,6 +1622,8 @@ void Engine::KeyPressEvent(unsigned char key) { } break; case 36: // ESC - Quitter + if (m_networkgame) + break; if (m_gamestate == GameState::PLAY) { m_gamestate = GameState::PAUSE; } diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index e9f4709..ef0cb73 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -99,7 +99,7 @@ private: BlockInfo* m_blockinfo[BTYPE_LAST]; BoostInfo* m_boostinfo[BTYPE_BOOST_LAST]; - GameState m_gamestate = GameState::SPLASH; + GameState m_gamestate = GameState::PLAY; Shader m_shader01;