Fix disconnected players removing the flag in CTF

This commit is contained in:
Benau 2018-10-20 15:22:07 +08:00
parent 67105fc915
commit 768d52b1c8
3 changed files with 49 additions and 35 deletions

View File

@ -510,45 +510,52 @@ bool CaptureTheFlag::isRaceOver()
return false;
} // isRaceOver
// ----------------------------------------------------------------------------
void CaptureTheFlag::loseFlagForKart(unsigned int kart_id)
{
if (!(m_red_holder == kart_id || m_blue_holder == kart_id))
return;
bool reset_red_flag = m_red_holder == kart_id;
btTransform dropped_trans = reset_red_flag ?
m_orig_red_trans : m_orig_blue_trans;
bool succeed = getDroppedFlagTrans(getKart(kart_id)->getTrans(),
&dropped_trans);
NetworkString p(PROTOCOL_GAME_EVENTS);
p.setSynchronous(true);
// If reset red flag
uint8_t reset_info = reset_red_flag ? 1 : 0;
reset_info <<= 1;
// With custom transform
if (succeed)
reset_info |= 1;
p.addUInt8(GameEventsProtocol::GE_CTF_RESET).addUInt8(reset_info)
.addUInt8(((int8_t)-1));
if (succeed)
{
p.add(Vec3(dropped_trans.getOrigin()))
.add(dropped_trans.getRotation());
}
STKHost::get()->sendPacketToAllPeers(&p, true);
if (reset_red_flag)
{
m_red_holder = -1;
m_red_trans = dropped_trans;
}
else
{
m_blue_holder = -1;
m_blue_trans = dropped_trans;
}
} // loseFlagForKart
// ----------------------------------------------------------------------------
bool CaptureTheFlag::kartHit(int kart_id, int hitter)
{
if (!FreeForAll::kartHit(kart_id, hitter))
return false;
if (m_red_holder == kart_id || m_blue_holder == kart_id)
{
bool reset_red_flag = m_red_holder == kart_id;
btTransform dropped_trans = reset_red_flag ?
m_orig_red_trans : m_orig_blue_trans;
bool succeed = getDroppedFlagTrans(
getKart(kart_id)->getTrans(), &dropped_trans);
NetworkString p(PROTOCOL_GAME_EVENTS);
p.setSynchronous(true);
// If reset red flag
uint8_t reset_info = reset_red_flag ? 1 : 0;
reset_info <<= 1;
// With custom transform
if (succeed)
reset_info |= 1;
p.addUInt8(GameEventsProtocol::GE_CTF_RESET).addUInt8(reset_info)
.addUInt8(((int8_t)-1));
if (succeed)
{
p.add(Vec3(dropped_trans.getOrigin()))
.add(dropped_trans.getRotation());
}
STKHost::get()->sendPacketToAllPeers(&p, true);
if (reset_red_flag)
{
m_red_holder = -1;
m_red_trans = dropped_trans;
}
else
{
m_blue_holder = -1;
m_blue_trans = dropped_trans;
}
}
loseFlagForKart(kart_id);
return true;
} // kartHit

View File

@ -129,6 +129,8 @@ public:
const Vec3& getRedFlag() const { return (Vec3&)m_red_trans.getOrigin(); }
// ------------------------------------------------------------------------
const Vec3& getBlueFlag() const { return (Vec3&)m_blue_trans.getOrigin(); }
// ------------------------------------------------------------------------
void loseFlagForKart(unsigned int kart_id);
}; // CaptureTheFlag

View File

@ -21,7 +21,7 @@
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "karts/abstract_kart.hpp"
#include "modes/world.hpp"
#include "modes/capture_the_flag.hpp"
#include "network/network_config.hpp"
#include "network/network_player_profile.hpp"
#include "network/protocols/game_events_protocol.hpp"
@ -113,6 +113,11 @@ void GameSetup::update(bool remove_disconnected_players)
AbstractKart* k = World::getWorld()->getKart(i);
if (!k->isEliminated())
{
CaptureTheFlag* ctf = dynamic_cast<CaptureTheFlag*>
(World::getWorld());
if (ctf)
ctf->loseFlagForKart(k->getWorldKartId());
World::getWorld()->eliminateKart(i,
false/*notify_of_elimination*/);
k->setPosition(