Skip to content

Commit

Permalink
Add server-side object events
Browse files Browse the repository at this point in the history
  • Loading branch information
FileEX committed May 1, 2024
1 parent 1f2c6e7 commit 2fc8b7b
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 2 deletions.
57 changes: 55 additions & 2 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4774,12 +4774,22 @@ bool CClientGame::ObjectDamageHandler(CObjectSAInterface* pObjectInterface, floa
Arguments.PushNumber(fLoss);

CClientEntity* pClientAttacker = pPools->GetClientEntity((DWORD*)pAttackerInterface);
ElementID attackerID;

if (pClientAttacker)
{
attackerID = pClientAttacker->GetID();
Arguments.PushElement(pClientAttacker);
}
else
Arguments.PushNil();

return pClientObject->CallEvent("onClientObjectDamage", Arguments, true);
bool bResult = pClientObject->CallEvent("onClientObjectDamage", Arguments, true);

if (bResult && !pClientObject->IsLocalEntity())
SendObjectDamagePacket(pClientObject, fLoss, attackerID);

return bResult;
}
}
return true;
Expand Down Expand Up @@ -4808,12 +4818,22 @@ bool CClientGame::ObjectBreakHandler(CObjectSAInterface* pObjectInterface, CEnti
CLuaArguments Arguments;

CClientEntity* pClientAttacker = pPools->GetClientEntity((DWORD*)pAttackerInterface);
ElementID attackerID = INVALID_ELEMENT_ID;

if (pClientAttacker)
{
attackerID = pClientAttacker->GetID();
Arguments.PushElement(pClientAttacker);
}
else
Arguments.PushNil();

return pClientObject->CallEvent("onClientObjectBreak", Arguments, true);
bool bResult = pClientObject->CallEvent("onClientObjectBreak", Arguments, true);

if (bResult && !pClientObject->IsLocalEntity())
SendObjectBreakPacket(pClientObject, attackerID);

return bResult;
}
}
return true;
Expand Down Expand Up @@ -5582,6 +5602,39 @@ void CClientGame::ResetMapInfo()
ReinitMarkers();
}

void CClientGame::SendObjectBreakPacket(CClientObject* Object, ElementID attackerID)
{
if (Object && Object->GetHealth() == 0.0f)
{
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
pBitStream->Write(Object->GetID());
pBitStream->Write(attackerID);

g_pNet->SendPacket(PACKET_ID_OBJECT_BREAK, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_RELIABLE_ORDERED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
}

void CClientGame::SendObjectDamagePacket(CClientObject* Object, float fLoss, ElementID attackerID)
{
if (Object)
{
NetBitStreamInterface* pBitStream = g_pNet->AllocateNetBitStream();
if (pBitStream)
{
pBitStream->Write(Object->GetID());
pBitStream->Write(fLoss);
pBitStream->Write(attackerID);

g_pNet->SendPacket(PACKET_ID_OBJECT_DAMAGE, pBitStream, PACKET_PRIORITY_MEDIUM, PACKET_RELIABILITY_RELIABLE_ORDERED);
g_pNet->DeallocateNetBitStream(pBitStream);
}
}
}

void CClientGame::SendPedWastedPacket(CClientPed* Ped, ElementID damagerID, unsigned char ucWeapon, unsigned char ucBodyPiece, AssocGroupId animGroup,
AnimationId animID)
{
Expand Down
3 changes: 3 additions & 0 deletions Client/mods/deathmatch/logic/CClientGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ class CClientGame
void SendPedWastedPacket(CClientPed* Ped, ElementID damagerID = INVALID_ELEMENT_ID, unsigned char ucWeapon = 0xFF, unsigned char ucBodyPiece = 0xFF,
AssocGroupId animGroup = 0, AnimationId animID = 15);

void SendObjectBreakPacket(CClientObject* Object, ElementID attackerID);
void SendObjectDamagePacket(CClientObject* Object, float fHealthLoss, ElementID attackerID);

CClientGUIElement* GetClickedGUIElement() { return m_pClickedGUIElement; }
void SetClickedGUIElement(CClientGUIElement* pElement) { m_pClickedGUIElement = NULL; }

Expand Down
60 changes: 60 additions & 0 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,18 @@ bool CGame::ProcessPacket(CPacket& Packet)
return true;
}

case PACKET_ID_OBJECT_BREAK:
{
Packet_ObjectBreak(static_cast<CObjectBreakPacket&>(Packet));
return true;
}

case PACKET_ID_OBJECT_DAMAGE:
{
Packet_ObjectDamage(static_cast<CObjectDamagePacket&>(Packet));
return true;
}

case PACKET_ID_WEAPON_BULLETSYNC:
{
Packet_WeaponBulletsync(static_cast<CCustomWeaponBulletSyncPacket&>(Packet));
Expand Down Expand Up @@ -1546,6 +1558,8 @@ void CGame::AddBuiltInEvents()
m_Events.AddEvent("onPlayerVoiceStop", "", NULL, false);

// Object events
m_Events.AddEvent("onObjectBreak", "attacker", NULL, false);
m_Events.AddEvent("onObjectDamage", "loss, atacker", NULL, false);

// Pickup events
m_Events.AddEvent("onPickupHit", "player", NULL, false);
Expand Down Expand Up @@ -2325,6 +2339,52 @@ void CGame::Packet_Command(CCommandPacket& Packet)
}
}

void CGame::Packet_ObjectBreak(CObjectBreakPacket& Packet)
{
CPlayer* pPlayer = Packet.GetSourcePlayer();
if (!pPlayer || !pPlayer->IsJoined())
return;

CObject* pObject = static_cast<CObject*>(CElementIDs::GetElement(Packet.m_ObjectID));

if (pObject != nullptr)
{
CElement* pAttacker = (Packet.m_Attacker != INVALID_ELEMENT_ID) ? CElementIDs::GetElement(Packet.m_Attacker) : NULL;

CLuaArguments Arguments;
if (pAttacker)
Arguments.PushElement(pAttacker);
else
Arguments.PushNil();

pObject->CallEvent("onObjectBreak", Arguments);
}
}

void CGame::Packet_ObjectDamage(CObjectDamagePacket& Packet)
{
CPlayer* pPlayer = Packet.GetSourcePlayer();
if (!pPlayer || !pPlayer->IsJoined())
return;

CObject* pObject = static_cast<CObject*>(CElementIDs::GetElement(Packet.m_ObjectID));

if (pObject != nullptr)
{
CElement* pAttacker = (Packet.m_Attacker != INVALID_ELEMENT_ID) ? CElementIDs::GetElement(Packet.m_Attacker) : NULL;

CLuaArguments Arguments;
Arguments.PushNumber(Packet.m_fLoss);

if (pAttacker)
Arguments.PushElement(pAttacker);
else
Arguments.PushNil();

pObject->CallEvent("onObjectDamage", Arguments);
}
}

void CGame::Packet_VehicleDamageSync(CVehicleDamageSyncPacket& Packet)
{
// Grab the source player
Expand Down
4 changes: 4 additions & 0 deletions Server/mods/deathmatch/logic/CGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CGame;
#include "packets/CPlayerModInfoPacket.h"
#include "packets/CPlayerACInfoPacket.h"
#include "packets/CPlayerScreenShotPacket.h"
#include "packets/CObjectBreakPacket.h"
#include "packets/CObjectDamagePacket.h"

#include "CRPCFunctions.h"

Expand Down Expand Up @@ -485,6 +487,8 @@ class CGame
void Packet_ExplosionSync(class CExplosionSyncPacket& Packet);
void Packet_ProjectileSync(class CProjectileSyncPacket& Packet);
void Packet_Command(class CCommandPacket& Packet);
void Packet_ObjectBreak(class CObjectBreakPacket& Packet);
void Packet_ObjectDamage(class CObjectDamagePacket& Packet);
void Packet_VehicleDamageSync(class CVehicleDamageSyncPacket& Packet);
void Packet_VehiclePuresync(class CVehiclePuresyncPacket& Packet);
void Packet_Keysync(class CKeysyncPacket& Packet);
Expand Down
10 changes: 10 additions & 0 deletions Server/mods/deathmatch/logic/CPacketTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "packets/CLuaEventPacket.h"
#include "packets/CCustomDataPacket.h"
#include "packets/CCameraSyncPacket.h"
#include "packets/CObjectBreakPacket.h"
#include "packets/CObjectDamagePacket.h"
#include "packets/CObjectSyncPacket.h"
#include "packets/CPlayerTransgressionPacket.h"
#include "packets/CPlayerDiagnosticPacket.h"
Expand Down Expand Up @@ -176,6 +178,14 @@ CPacket* CPacketTranslator::Translate(const NetServerPlayerID& Socket, ePacketID
pTemp = new CObjectSyncPacket;
break;

case PACKET_ID_OBJECT_BREAK:
pTemp = new CObjectBreakPacket;
break;

case PACKET_ID_OBJECT_DAMAGE:
pTemp = new CObjectDamagePacket;
break;

case PACKET_ID_PLAYER_TRANSGRESSION:
pTemp = new CPlayerTransgressionPacket;
break;
Expand Down
45 changes: 45 additions & 0 deletions Server/mods/deathmatch/logic/packets/CObjectBreakPacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/packets/CObjectBreakPacket.cpp
* PURPOSE: Header for object break packet class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include "CObjectBreakPacket.h"
#include <CObject.h>
#include <net/SyncStructures.h>

CObjectBreakPacket::CObjectBreakPacket()
{
m_ObjectID = INVALID_ELEMENT_ID;
m_Attacker = INVALID_ELEMENT_ID;
}

CObjectBreakPacket::CObjectBreakPacket(CObject* pObject, CElement* pAttacker)
{
m_ObjectID = pObject->GetID();
m_Attacker = (pAttacker) ? pAttacker->GetID() : INVALID_ELEMENT_ID;
}

bool CObjectBreakPacket::Read(NetBitStreamInterface& BitStream)
{
if (BitStream.Read(m_ObjectID) && BitStream.Read(m_Attacker))
{
return true;
}

return false;
}

bool CObjectBreakPacket::Write(NetBitStreamInterface& BitStream) const
{
BitStream.Write(m_ObjectID);
BitStream.Write(m_Attacker);

return true;
}
32 changes: 32 additions & 0 deletions Server/mods/deathmatch/logic/packets/CObjectBreakPacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/packets/CObjectBreakPacket.h
* PURPOSE: Object break state class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

#include "CPacket.h"

class CObject;

class CObjectBreakPacket final : public CPacket
{
public:
CObjectBreakPacket();
CObjectBreakPacket(CObject* pObject, CElement* pAttacker);

ePacketID GetPacketID() const { return PACKET_ID_OBJECT_BREAK; };
unsigned long GetFlags() const { return PACKET_MEDIUM_PRIORITY | PACKET_RELIABLE; };

bool Read(NetBitStreamInterface& BitStream);
bool Write(NetBitStreamInterface& BitStream) const;

ElementID m_ObjectID;
ElementID m_Attacker;
};
48 changes: 48 additions & 0 deletions Server/mods/deathmatch/logic/packets/CObjectDamagePacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/packets/CObjectDamagePacket.cpp
* PURPOSE: Header for object damage packet class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include "CObjectDamagePacket.h"
#include <CObject.h>
#include <net/SyncStructures.h>

CObjectDamagePacket::CObjectDamagePacket()
{
m_ObjectID = INVALID_ELEMENT_ID;
m_Attacker = INVALID_ELEMENT_ID;
m_fLoss = 0;
}

CObjectDamagePacket::CObjectDamagePacket(CObject* pObject, float fLoss, CElement* pAttacker)
{
m_ObjectID = pObject->GetID();
m_Attacker = (pAttacker) ? pAttacker->GetID() : INVALID_ELEMENT_ID;
m_fLoss = fLoss;
}

bool CObjectDamagePacket::Read(NetBitStreamInterface& BitStream)
{
if (BitStream.Read(m_ObjectID) && BitStream.Read(m_fLoss) && BitStream.Read(m_Attacker))
{
return true;
}

return false;
}

bool CObjectDamagePacket::Write(NetBitStreamInterface& BitStream) const
{
BitStream.Write(m_ObjectID);
BitStream.Write(m_fLoss);
BitStream.Write(m_Attacker);

return true;
}
33 changes: 33 additions & 0 deletions Server/mods/deathmatch/logic/packets/CObjectDamagePacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/packets/CObjectDamagePacket.h
* PURPOSE: Object damage state class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

#include "CPacket.h"

class CObject;

class CObjectDamagePacket final : public CPacket
{
public:
CObjectDamagePacket();
CObjectDamagePacket(CObject* pObject, float fLoss, CElement* pAttacker);

ePacketID GetPacketID() const { return PACKET_ID_OBJECT_DAMAGE; };
unsigned long GetFlags() const { return PACKET_MEDIUM_PRIORITY | PACKET_RELIABLE; };

bool Read(NetBitStreamInterface& BitStream);
bool Write(NetBitStreamInterface& BitStream) const;

ElementID m_ObjectID;
float m_fLoss;
ElementID m_Attacker;
};
3 changes: 3 additions & 0 deletions Shared/sdk/net/Packets.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ enum ePacketID
PACKET_ID_OBJECT_STOPSYNC,
PACKET_ID_OBJECT_SYNC,

PACKET_ID_OBJECT_BREAK,
PACKET_ID_OBJECT_DAMAGE,

PACKET_ID_UPDATE_INFO,
PACKET_ID_DISCONNECT_MESSAGE,
PACKET_ID_PLAYER_TRANSGRESSION,
Expand Down

0 comments on commit 2fc8b7b

Please sign in to comment.