Timer iznogoud.

This commit is contained in:
MarcEricMartel 2023-12-06 11:16:39 -05:00
parent 9c1cd885cf
commit c975265901
12 changed files with 118 additions and 54 deletions

View File

@ -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) : 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() {} Bullet::~Bullet() {}
@ -14,8 +14,19 @@ bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordere
m_currentpos += m_velocity * elapsedtime; m_currentpos += m_velocity * elapsedtime;
for (auto& [key, player] : mapPlayer) { 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); player->InflictDamage(damage);
if (player->AmIDead())
player->Killer = m_shooter_id;
return true; return true;
} }
} }
@ -56,6 +67,6 @@ Vector3f Bullet::getVel() const {
return m_velocity; return m_velocity;
} }
uint64_t Bullet::getTeamID(){ //uint64_t Bullet::getTeamID(){
return m_tid; // return m_tid;
} //}

View File

@ -20,13 +20,13 @@ public:
void Transpose(int& x, int& z); void Transpose(int& x, int& z);
Vector3f getPos() const; Vector3f getPos() const;
Vector3f getVel() const; Vector3f getVel() const;
uint64_t getTeamID(); //uint64_t getTeamID();
private: private:
Vector3f m_startpos, Vector3f m_startpos,
m_currentpos, m_currentpos,
m_velocity; m_velocity;
uint64_t m_tid = 0; uint64_t m_shooter_id = 0;
}; };

View File

@ -447,7 +447,7 @@ void netprot::Serialize(BulletAdd* bull, char* buf[], uint32_t* buflen) {
memcpy(*buf + 1, ts8, sizeof(uint64_t)); 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 tid8[sizeof(uint64_t)] = {
(uint8_t)((tid >> 56) & 0xFF), (uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF), (uint8_t)((tid >> 48) & 0xFF),
@ -964,7 +964,7 @@ bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) {
(uint64_t)tst[7]; (uint64_t)tst[7];
memcpy(tst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t)); memcpy(tst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
bull->tid = bull->id =
(uint64_t)tst[0] << 56 | (uint64_t)tst[0] << 56 |
(uint64_t)tst[1] << 48 | (uint64_t)tst[1] << 48 |
(uint64_t)tst[2] << 40 | (uint64_t)tst[2] << 40 |

View File

@ -282,20 +282,17 @@ bool Player::AmIDead()
} }
void Player::InflictDamage(float hitPoints) void Player::InflictDamage(float hitPoints) {
{
m_hp -= 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; } uint64_t Player::getId() const { return id; }

View File

@ -37,6 +37,9 @@ public:
bool AmIDead(); bool AmIDead();
void InflictDamage(float hitPoints); void InflictDamage(float hitPoints);
int getScore() const;
void addPoint();
uint64_t Killer = 0;
private: private:
uint64_t getId() const; uint64_t getId() const;
@ -48,6 +51,7 @@ protected:
std::string m_username; std::string m_username;
uint64_t id = 0; uint64_t id = 0;
int m_score = 0;
float m_rotX = 0; float m_rotX = 0;
float m_rotY = 0; float m_rotY = 0;

View File

@ -137,9 +137,19 @@ Timestamp Connection::Run(World* world) {
in.keys.right, in.keys.right,
in.keys.jump, false, el), world, el); 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.jumping = in.keys.jump;
out.states.running = player->GetVelocity().Length() > .3f; 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) { if (in.keys.block) {
bool block = false; bool block = false;
@ -152,7 +162,7 @@ Timestamp Connection::Run(World* world) {
} }
if (in.keys.shoot && m_shoot_acc <= 0.) { 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; out.states.shooting = true;
} }

View File

@ -42,8 +42,8 @@ public:
bool m_nsync = true; bool m_nsync = true;
std::vector<Bullet> Bullets; std::vector<Bullet*> Bullets;
std::vector<ChunkMod> ChunkDiffs; std::vector<ChunkMod*> ChunkDiffs;
Timestamp GetTStamp() const; Timestamp GetTStamp() const;

View File

@ -11,13 +11,20 @@
#define ID_LIST_SIZE 127 #define ID_LIST_SIZE 127
#define SRV_MANUAL_SETUP true #define SRV_MANUAL_SETUP true
// @ = Dead guy, $ = Killer.
const std::vector<std::string> DEATHMESSAGES = { "@ has gone to meet their maker.", const std::vector<std::string> DEATHMESSAGES = { "@ has gone to meet their maker.",
"@ has bit the dust, if you know what I mean.", "@ has bit the dust, if you know what I mean.",
"@ has ceased to be.", "@ has ceased to be.",
"@ is no more.", "@ is no more.",
"@ is like, super dead.", "@ is like, super dead.",
"RIP, @.", "Requiescat In Pace, @.",
"So long, @, and thanks for all the lols!", "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 #endif

View File

@ -200,8 +200,8 @@ void Server::Run() {
m_world->BuildWorld(); m_world->BuildWorld();
for (auto& [key, conn] : m_conns) { // 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), int x = 0,// (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); 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)); conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
m_players[key] = conn->player; m_players[key] = conn->player;
Sync sync; Sync sync;
@ -270,8 +270,10 @@ void Server::Run() {
if (conn->player->AmIDead()) { if (conn->player->AmIDead()) {
Chat chat; Chat chat;
chat.dest_id = chat.dest_team_id = chat.src_id = 0; 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; m_chatlog[tstamp] = chat;
@ -285,9 +287,10 @@ void Server::Run() {
for (auto& bull : conn->Bullets) { for (auto& bull : conn->Bullets) {
bullets.emplace_back(bull); bullets.emplace_back(bull);
Log("POW!", false, false);
BulletAdd* nbul = new BulletAdd(); BulletAdd* nbul = new BulletAdd();
nbul->pos = conn->player->GetPosition(); nbul->pos = bull->getPos();
nbul->dir = conn->player->GetDirection(); nbul->dir = bull->getVel();
nbul->id = key; nbul->id = key;
nbul->tstamp = tstamp; nbul->tstamp = tstamp;
@ -300,10 +303,10 @@ void Server::Run() {
conn->sendPacks(m_sock_udp, m_conns, timer); conn->sendPacks(m_sock_udp, m_conns, timer);
} }
if (deadplayers == players - 1 || timer <= 0) //if (/*(deadplayers == players - 1 && deadplayers != 0) || */timer <= 0)
endgame = true; //endgame = true;
} }
for (auto& bull : netbull) for (auto& bull : netbull)
for (auto& [key, conn] : m_conns) for (auto& [key, conn] : m_conns)
if (bull->id != conn->GetHash(false)) // Pour pas repitcher au joueur sa propre balle. 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& chat : chatlog) {
for (auto& [key, conn] : m_conns) Log(chat.mess, false, false);
for (auto& [key, conn] : m_conns)
sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr()); sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr());
}
for (auto& chmo : chunkdiffs) { for (auto& chmo : chunkdiffs) {
for (auto& [key, conn] : m_conns) for (auto& [key, conn] : m_conns)
@ -420,13 +425,26 @@ uint64_t Server::getUniqueId() {
return id; 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 mess;
std::string temp = DEATHMESSAGES.at(rand() % DEATHMESSAGES.size()); std::string temp = DEATHMESSAGES.at(rand() % DEATHMESSAGES.size());
size_t ind = temp.find('@'); size_t ind = temp.find('@');
mess.append(temp.substr(0, ind)); size_t indk = temp.find('$');
mess.append(username));
mess.append(temp.substr(ind + 1)); 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; return mess;
} }

View File

@ -50,7 +50,7 @@ private:
void buildIdList(size_t size); void buildIdList(size_t size);
uint64_t getUniqueId(); uint64_t getUniqueId();
std::string getDeathMessage(std::string username) const; std::string getDeathMessage(std::string username, std::string killer) const;
}; };

View File

@ -530,12 +530,12 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) {
glPushMatrix(); glPushMatrix();
int timer = GetCountdown(elapsedTime); 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) { if (timer <= COUNTDOWN - m_timerReductionChunk * i) {
m_world.RemoveChunk(m_nbReductionChunk * i); m_world.RemoveChunk(m_nbReductionChunk * i);
m_renderer.RemoveChunk(m_nbReductionChunk * i); m_renderer.RemoveChunk(m_nbReductionChunk * i);
} }
} }*/
if (m_keyK) { if (m_keyK) {
SystemNotification(m_messageNotification); SystemNotification(m_messageNotification);
m_keyK = false; m_keyK = false;
@ -663,8 +663,8 @@ int Engine::GetCountdown(float elapsedTime) {
m_countdown = m_time + COUNTDOWN; m_countdown = m_time + COUNTDOWN;
m_resetcountdown = false; m_resetcountdown = false;
} }
if (m_countdown < m_time) if (m_countdown < m_time) {}
Stop(); //Stop();
if (!m_stopcountdown) if (!m_stopcountdown)
m_time += elapsedTime; m_time += elapsedTime;
return m_countdown - (int)m_time; return m_countdown - (int)m_time;
@ -1409,10 +1409,12 @@ void Engine::Render(float elapsedTime) {
sendPackTo<Input>(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr); sendPackTo<Input>(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr);
std::cout << ".";
lsPck = recvPacks(m_conn.m_sock_udp, &m_buf); lsPck = recvPacks(m_conn.m_sock_udp, &m_buf);
char* prevptr = nullptr; char* prevptr = nullptr;
for (auto& pck : lsPck) { // We could make a few threads out of this. 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) if (!prevptr)
prevptr = m_buf.ptr; prevptr = m_buf.ptr;
uint32_t bsize = m_buf.len - (pck - prevptr); uint32_t bsize = m_buf.len - (pck - prevptr);
@ -1420,9 +1422,10 @@ void Engine::Render(float elapsedTime) {
switch (getType(pck, 1)) { switch (getType(pck, 1)) {
using enum PACKET_TYPE; using enum PACKET_TYPE;
case SYNC: case SYNC:
std::cout << "snyc";
if (Deserialize(&sync, pck, &bsize)) { if (Deserialize(&sync, pck, &bsize)) {
if (sync.sid != m_conn.getId()) { if (sync.sid != m_conn.getId()) {
std::cout << "syncsid be no good."; SystemNotification("syncsid be no good.");
break; break;
} }
if (m_syncs.count(sync.timestamp)) { if (m_syncs.count(sync.timestamp)) {
@ -1439,18 +1442,18 @@ void Engine::Render(float elapsedTime) {
m_player.Move(-diff); m_player.Move(-diff);
} }
// TODO: Syncer sync.timer avec le timer m_countdown = sync.timer;
m_syncs.erase(sync.timestamp); m_syncs.erase(sync.timestamp);
} }
else std::cout << "sync be no good."; else SystemNotification("sync be no good.");
} }
break; break;
case OUTPUT: case OUTPUT:
std::cout << "out";
if (Deserialize(&out, pck, &bsize)) { if (Deserialize(&out, pck, &bsize)) {
if (!m_players.contains(out.id)) { if (!m_players.contains(out.id)) {
SystemNotification(std::to_string(out.id).append(" is id no good."));
std::cout << out.id << " is id no good." << std::endl;
break; break;
} }
RemotePlayer* rt = static_cast<RemotePlayer*>(m_players[out.id]); RemotePlayer* rt = static_cast<RemotePlayer*>(m_players[out.id]);
@ -1458,6 +1461,7 @@ void Engine::Render(float elapsedTime) {
} }
break; break;
case CHUNKMOD: case CHUNKMOD:
std::cout << "cmod";
if (Deserialize(&cmod, pck, &bsize)) { 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. 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) { [cmod](ChunkMod* c) {
@ -1468,10 +1472,12 @@ void Engine::Render(float elapsedTime) {
m_world.ChangeBlockAtPosition(cmod.b_type, cmod.pos); m_world.ChangeBlockAtPosition(cmod.b_type, cmod.pos);
else cmod_acc = 0; else cmod_acc = 0;
} }
else SystemNotification("cmod iznogoud.");
break; break;
case BULLET: case BULLET:
std::cout << "pow";
if (Deserialize(&bull, pck, &bsize)) { 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"). 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]) { if (!m_bullets[x]) {
m_bullets[x] = bult; 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); 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; break;
default: default:
std::cout << "packet be no good."; std::cout << "wtf";
SystemNotification("packet be no good.");
break; break;
} }
} }
lsPck.clear(); lsPck.clear();
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
for (auto& [key, player] : m_players) { for (auto& [key, player] : m_players) {
RemotePlayer* rt = static_cast<RemotePlayer*>(player); RemotePlayer* rt = static_cast<RemotePlayer*>(player);
@ -1607,6 +1622,8 @@ void Engine::KeyPressEvent(unsigned char key) {
} }
break; break;
case 36: // ESC - Quitter case 36: // ESC - Quitter
if (m_networkgame)
break;
if (m_gamestate == GameState::PLAY) { if (m_gamestate == GameState::PLAY) {
m_gamestate = GameState::PAUSE; m_gamestate = GameState::PAUSE;
} }

View File

@ -99,7 +99,7 @@ private:
BlockInfo* m_blockinfo[BTYPE_LAST]; BlockInfo* m_blockinfo[BTYPE_LAST];
BoostInfo* m_boostinfo[BTYPE_BOOST_LAST]; BoostInfo* m_boostinfo[BTYPE_BOOST_LAST];
GameState m_gamestate = GameState::SPLASH; GameState m_gamestate = GameState::PLAY;
Shader m_shader01; Shader m_shader01;