From 3a9e4ee067bf3dc9fc945afee7da31a2a55ab813 Mon Sep 17 00:00:00 2001 From: Alexander Akulich Date: Fri, 10 Feb 2023 23:09:27 +0300 Subject: [PATCH] Extract SnapPickup() to CGameContext helpers --- src/game/server/entities/pickup.cpp | 26 ++----------------- src/game/server/gamecontext.cpp | 39 +++++++++++++++++++++++++++++ src/game/server/gamecontext.h | 7 ++++-- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/game/server/entities/pickup.cpp b/src/game/server/entities/pickup.cpp index 31288d714..31f343988 100644 --- a/src/game/server/entities/pickup.cpp +++ b/src/game/server/entities/pickup.cpp @@ -173,6 +173,7 @@ void CPickup::Snap(int SnappingClient) pChar = GameServer()->GetPlayerChar(GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID); int SnappingClientVersion = GameServer()->GetClientVersion(SnappingClient); + bool Sixup = SnappingClientVersion == VERSION_NONE ? Server()->IsSixup(SnappingClient) : false; CNetObj_EntityEx *pEntData = 0; if(SnappingClientVersion >= VERSION_DDNET_SWITCH && (m_Layer == LAYER_SWITCH || length(m_Core) > 0)) @@ -191,30 +192,7 @@ void CPickup::Snap(int SnappingClient) return; } - int Size = Server()->IsSixup(SnappingClient) ? sizeof(protocol7::CNetObj_Pickup) : sizeof(CNetObj_Pickup); - CNetObj_Pickup *pPickup = static_cast(Server()->SnapNewItem(NETOBJTYPE_PICKUP, GetID(), Size)); - if(!pPickup) - return; - - pPickup->m_X = (int)m_Pos.x; - pPickup->m_Y = (int)m_Pos.y; - pPickup->m_Type = m_Type; - if(SnappingClientVersion < VERSION_DDNET_WEAPON_SHIELDS) - { - if(m_Type >= POWERUP_ARMOR_SHOTGUN && m_Type <= POWERUP_ARMOR_LASER) - { - pPickup->m_Type = POWERUP_ARMOR; - } - } - if(Server()->IsSixup(SnappingClient)) - { - if(m_Type == POWERUP_WEAPON) - pPickup->m_Type = m_Subtype == WEAPON_SHOTGUN ? protocol7::PICKUP_SHOTGUN : m_Subtype == WEAPON_GRENADE ? protocol7::PICKUP_GRENADE : protocol7::PICKUP_LASER; - else if(m_Type == POWERUP_NINJA) - pPickup->m_Type = protocol7::PICKUP_NINJA; - } - else - pPickup->m_Subtype = m_Subtype; + GameServer()->SnapPickup(CSnapContext(SnappingClientVersion, Sixup), GetID(), m_Pos, m_Type, m_Subtype); } void CPickup::Move() diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 7bd281641..b104c8790 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -404,6 +404,45 @@ bool CGameContext::SnapLaserObject(const CSnapContext &Context, int SnapID, cons return true; } +bool CGameContext::SnapPickup(const CSnapContext &Context, int SnapID, const vec2 &Pos, int Type, int SubType) +{ + if(Context.IsSixup()) + { + protocol7::CNetObj_Pickup *pPickup = Server()->SnapNewItem(SnapID); + if(!pPickup) + return false; + + pPickup->m_X = (int)Pos.x; + pPickup->m_Y = (int)Pos.y; + + if(Type == POWERUP_WEAPON) + pPickup->m_Type = SubType == WEAPON_SHOTGUN ? protocol7::PICKUP_SHOTGUN : SubType == WEAPON_GRENADE ? protocol7::PICKUP_GRENADE : protocol7::PICKUP_LASER; + else if(Type == POWERUP_NINJA) + pPickup->m_Type = protocol7::PICKUP_NINJA; + } + else + { + CNetObj_Pickup *pPickup = Server()->SnapNewItem(SnapID); + if(!pPickup) + return false; + + pPickup->m_X = (int)Pos.x; + pPickup->m_Y = (int)Pos.y; + + pPickup->m_Type = Type; + if(Context.GetClientVersion() < VERSION_DDNET_WEAPON_SHIELDS) + { + if(Type >= POWERUP_ARMOR_SHOTGUN && Type <= POWERUP_ARMOR_LASER) + { + pPickup->m_Type = POWERUP_ARMOR; + } + } + pPickup->m_Subtype = SubType; + } + + return true; +} + void CGameContext::CallVote(int ClientID, const char *pDesc, const char *pCmd, const char *pReason, const char *pChatmsg, const char *pSixupDesc) { // check if a vote is already running diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index f0549a7e3..03652dcae 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -61,15 +61,17 @@ struct CScoreRandomMapResult; struct CSnapContext { - CSnapContext(int Version) : - m_ClientVersion(Version) + CSnapContext(int Version, bool Sixup = false) : + m_ClientVersion(Version), m_Sixup(Sixup) { } int GetClientVersion() const { return m_ClientVersion; } + bool IsSixup() const { return m_Sixup; } private: int m_ClientVersion; + bool m_Sixup; }; class CGameContext : public IGameServer @@ -227,6 +229,7 @@ public: void CreateSoundGlobal(int Sound, int Target = -1); bool SnapLaserObject(const CSnapContext &Context, int SnapID, const vec2 &To, const vec2 &From, int StartTick, int Owner = -1, int LaserType = -1); + bool SnapPickup(const CSnapContext &Context, int SnapID, const vec2 &Pos, int Type, int SubType); enum {