Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions Server/mods/deathmatch/logic/packets/CBulletsyncPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "CWeaponStatManager.h"
#include "CElementIDs.h"
#include "CElement.h"
#include "CWeaponNames.h"
#include <cstdint>

CBulletsyncPacket::CBulletsyncPacket(CPlayer* player)
: m_weapon(WEAPONTYPE_UNARMED)
Expand Down Expand Up @@ -68,13 +70,13 @@ bool CBulletsyncPacket::ValidateTrajectory() const noexcept
const float dz = m_end.fZ - m_start.fZ;

const float movementSq = (dx * dx) + (dy * dy) + (dz * dz);

if (IsNaN(movementSq))
return false;

if (movementSq < MIN_DISTANCE_SQ)
return false;

if (movementSq > MAX_DISTANCE_SQ)
return false;

Expand Down Expand Up @@ -104,13 +106,13 @@ bool CBulletsyncPacket::ReadWeaponAndPositions(NetBitStreamInterface& stream)

if (!stream.Read(reinterpret_cast<char*>(&m_end), sizeof(CVector)))
return false;

if (!IsValidVector(m_start))
return false;

if (!IsValidVector(m_end))
return false;

if (!ValidateVectorBounds(m_start))
return false;

Expand Down Expand Up @@ -158,7 +160,7 @@ bool CBulletsyncPacket::ReadOptionalDamage(NetBitStreamInterface& stream)
ResetDamageData();
return false;
}

// Validate that target element exists
if (m_damaged != INVALID_ELEMENT_ID)
{
Expand All @@ -171,8 +173,8 @@ bool CBulletsyncPacket::ReadOptionalDamage(NetBitStreamInterface& stream)

// Check element type is valid for damage
auto elementType = pElement->GetType();
if (elementType != CElement::PLAYER &&
elementType != CElement::PED &&
if (elementType != CElement::PLAYER &&
elementType != CElement::PED &&
elementType != CElement::VEHICLE)
{
ResetDamageData();
Expand All @@ -187,14 +189,14 @@ bool CBulletsyncPacket::Read(NetBitStreamInterface& stream)
{
if (!m_pSourceElement)
return false;

CPlayer* pPlayer = static_cast<CPlayer*>(m_pSourceElement);
if (pPlayer)
{
// Check if player is spawned and alive
if (!pPlayer->IsSpawned() || pPlayer->IsDead())
return false;

// Check player position is reasonable relative to bullet start
const CVector& playerPos = pPlayer->GetPosition();
const float maxShootDistance = 50.0f; // Max distance from player to bullet start
Expand All @@ -204,26 +206,26 @@ bool CBulletsyncPacket::Read(NetBitStreamInterface& stream)

if (!ReadWeaponAndPositions(stream))
return false;

// Now validate player position relative to shot origin
if (pPlayer)
{
const CVector& playerPos = pPlayer->GetPosition();
float dx = m_start.fX - playerPos.fX;
float dy = m_start.fY - playerPos.fY;
float dz = m_start.fZ - playerPos.fZ;
float distSq = dx*dx + dy*dy + dz*dz;
float distSq = dx * dx + dy * dy + dz * dz;

const float maxShootDistanceSq = 50.0f * 50.0f;
if (distSq > maxShootDistanceSq)
return false;

// Check if player has this weapon
if (!pPlayer->HasWeaponType(static_cast<unsigned char>(m_weapon)))
if (!pPlayer->HasWeaponType(static_cast<std::uint8_t>(m_weapon)))
return false;

// Check if weapon has ammo
if (pPlayer->GetWeaponAmmoInClip(static_cast<unsigned char>(m_weapon)) == 0)
if (pPlayer->GetWeaponTotalAmmo(static_cast<std::uint8_t>(m_weapon)) <= 0)
return false;
}

Expand All @@ -232,7 +234,7 @@ bool CBulletsyncPacket::Read(NetBitStreamInterface& stream)

if (!ReadOptionalDamage(stream))
return false;

return true;
}

Expand All @@ -244,21 +246,21 @@ bool CBulletsyncPacket::Write(NetBitStreamInterface& stream) const
const auto* pPlayer = static_cast<const CPlayer*>(m_pSourceElement);
if (!pPlayer)
return false;

const ElementID id = pPlayer->GetID();

if (id == INVALID_ELEMENT_ID)
return false;

if (id == 0)
return false;

if (!IsValidVector(m_start))
return false;

if (!IsValidVector(m_end))
return false;

if (!ValidateVectorBounds(m_start))
return false;

Expand Down Expand Up @@ -289,4 +291,4 @@ bool CBulletsyncPacket::Write(NetBitStreamInterface& stream) const
}

return true;
}
}