Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serverside isPedReloadingWeapon #3295

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/CNetAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ void CNetAPI::WritePlayerPuresync(CClientPlayer* pPlayerModel, NetBitStreamInter
flags.data.bSyncingVelocity = (!flags.data.bIsOnGround || (pPlayerModel->GetPlayerSyncCount() % 4) == 0);
flags.data.bStealthAiming = (pPlayerModel->IsStealthAiming() == true);

flags.data2.bIsReloadingWeapon = (pPlayerModel->IsReloadingWeapon() == true);

if (pPlayerWeapon->GetSlot() > 15)
flags.data.bHasAWeapon = false;

Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CPed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ CPed::CPed(CPedManager* pPedManager, CElement* pParent, unsigned short usModel)
m_fGravity = 0.008f;
m_bDoingGangDriveby = false;
m_bStealthAiming = false;
m_bReloadingWeapon = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to move it into the member initializer list.


m_pVehicle = NULL;
m_uiVehicleSeat = INVALID_VEHICLE_SEAT;
Expand Down
4 changes: 4 additions & 0 deletions Server/mods/deathmatch/logic/CPed.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ class CPed : public CElement
bool GetCollisionEnabled() { return m_bCollisionsEnabled; }
void SetCollisionEnabled(bool bCollisionEnabled) { m_bCollisionsEnabled = bCollisionEnabled; }

bool IsReloadingWeapon() const { return m_bReloadingWeapon; }
void SetReloadingWeapon(bool bState) { m_bReloadingWeapon = bState; }
Comment on lines +272 to +273
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noexcept


long long GetLastFarSyncTick() { return m_llLastFarSyncTick; }
void SetLastFarSyncTick(long long llLastSyncTick) { m_llLastFarSyncTick = llLastSyncTick; }

Expand Down Expand Up @@ -316,6 +319,7 @@ class CPed : public CElement
bool m_bFrozen;
bool m_bStealthAiming;
CVehicle* m_pJackingVehicle;
bool m_bReloadingWeapon;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could, please don't use hungarian notation: bool m_reloadingWeapon;


CVehicle* m_pVehicle;
unsigned int m_uiVehicleSeat;
Expand Down
3 changes: 3 additions & 0 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4473,6 +4473,9 @@ bool CStaticFunctionDefinitions::reloadPedWeapon(CElement* pElement)
{
CPed* pPed = static_cast<CPed*>(pElement);
CBitStream BitStream;

pPed->SetReloadingWeapon(true);

m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pPed, RELOAD_PED_WEAPON, *BitStream.pBitStream));
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void CLuaPedDefs::LoadFunctions()
{"takeAllWeapons", TakeAllWeapons},
{"giveWeaponAmmo", GiveWeapon},
{"takeWeaponAmmo", TakeWeapon},
{"isPedReloadingWeapon", ArgumentParser<IsPedReloadingWeapon>},
};

// Add functions
Expand Down Expand Up @@ -117,6 +118,7 @@ void CLuaPedDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "isFrozen", "isPedFrozen");
lua_classfunction(luaVM, "isHeadless", "isPedHeadless");
lua_classfunction(luaVM, "isWearingJetpack", "isPedWearingJetpack"); // introduced in 1.5.5-9.13846
lua_classfunction(luaVM, "isReloadingWeapon", "isPedReloadingWeapon");

lua_classfunction(luaVM, "getArmor", "getPedArmor");
lua_classfunction(luaVM, "getFightingStyle", "getPedFightingStyle");
Expand Down Expand Up @@ -168,6 +170,7 @@ void CLuaPedDefs::AddClass(lua_State* luaVM)
lua_classvariable(luaVM, "vehicle", "warpPedIntoVehicle", "getPedOccupiedVehicle", OOP_WarpPedIntoVehicle, GetPedOccupiedVehicle);
lua_classvariable(luaVM, "walkingStyle", "setPedWalkingStyle", "getPedWalkingStyle");
lua_classvariable(luaVM, "jetpack", "setPedWearingJetpack", "isPedWearingJetpack"); // introduced in 1.5.5-9.13846
lua_classvariable(luaVM, "reloadingWeapon", nullptr, "isPedReloadingWeapon");

// TODO(qaisjp): setting this to any value will kill the ped. add OOP_KillPed that only allows `true`.
lua_classvariable(luaVM, "dead", "killPed", "isPedDead");
Expand Down Expand Up @@ -1617,3 +1620,8 @@ int CLuaPedDefs::TakeAllWeapons(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

bool CLuaPedDefs::IsPedReloadingWeapon(CPed* const ped)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noexcept

{
return ped->IsReloadingWeapon();
}
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaPedDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ class CLuaPedDefs : public CLuaDefs
LUA_DECLARE(SetPedHeadless);
LUA_DECLARE(SetPedFrozen);
LUA_DECLARE(reloadPedWeapon);

static bool IsPedReloadingWeapon(CPed* const ped);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noexcept

};
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ bool CPlayerPuresyncPacket::Read(NetBitStreamInterface& BitStream)
pSourcePlayer->SetOnFire(flags.data.bIsOnFire);
pSourcePlayer->SetStealthAiming(flags.data.bStealthAiming);

pSourcePlayer->SetReloadingWeapon(flags.data2.bIsReloadingWeapon);

// Contact element
CElement* pContactElement = NULL;
if (flags.data.bHasContact)
Expand Down Expand Up @@ -314,6 +316,8 @@ bool CPlayerPuresyncPacket::Write(NetBitStreamInterface& BitStream) const
flags.data.bSyncingVelocity = (!flags.data.bIsOnGround || pSourcePlayer->IsSyncingVelocity());
flags.data.bStealthAiming = (pSourcePlayer->IsStealthAiming() == true);

flags.data2.bIsReloadingWeapon = (pSourcePlayer->IsReloadingWeapon() == true);

CVector vecPosition = pSourcePlayer->GetPosition();
if (pContactElement)
pSourcePlayer->GetContactPosition(vecPosition);
Expand Down
31 changes: 29 additions & 2 deletions Shared/sdk/net/SyncStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,29 @@ struct SPlayerPuresyncFlags : public ISyncStructure
BITCOUNT = 12
};

bool Read(NetBitStreamInterface& bitStream) { return bitStream.ReadBits((char*)&data, BITCOUNT); }
void Write(NetBitStreamInterface& bitStream) const { bitStream.WriteBits((const char*)&data, BITCOUNT); }
enum
{
BITCOUNT2 = 1
};

bool Read(NetBitStreamInterface& bitStream)
{
bool bOk = bitStream.ReadBits((char*)&data, BITCOUNT);

if (bitStream.Can(eBitStreamVersion::CPed_isReloadingWeapon))
bOk &= bitStream.ReadBits((char*)&data2, BITCOUNT2);
else
data2.bIsReloadingWeapon = 0;

return bOk;
}
void Write(NetBitStreamInterface& bitStream) const
{
bitStream.WriteBits((const char*)&data, BITCOUNT);

if (bitStream.Can(eBitStreamVersion::CPed_isReloadingWeapon))
bitStream.WriteBits((const char*)&data2, BITCOUNT2);
}

struct
{
Expand All @@ -565,6 +586,12 @@ struct SPlayerPuresyncFlags : public ISyncStructure
bool bSyncingVelocity : 1;
bool bStealthAiming : 1;
} data;

// Add new ones in separate structs
struct
{
bool bIsReloadingWeapon : 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just put it into the existing data struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to maintain compatibility with older BitStream version on the server/client.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this it's enough to permute BITCOUNT depending on a bitstream version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier, I tried to apply it like this, but there were issues with comparing the bitstream versions with different client <-> server versions.

} data2;
};

struct SPedRotationSync : public ISyncStructure
Expand Down
4 changes: 4 additions & 0 deletions Shared/sdk/net/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ enum class eBitStreamVersion : unsigned short
// 2023-10-12
CPlayerJoinCompletePacket_ServerName,

// Add serverside isPedReloadingWeapon
// 2024-01-16
CPed_isReloadingWeapon,

// This allows us to automatically increment the BitStreamVersion when things are added to this enum.
// Make sure you only add things above this comment.
Next,
Expand Down