Skip to content

Commit

Permalink
Object respawn functions (multitheftauto#3450)
Browse files Browse the repository at this point in the history
* Object respawn functions

* Assertion fix

* Update CPerfStat.RPCPacketUsage.cpp

* Review

* Review

* Fix build

---------

Co-authored-by: TEDERIs <[email protected]>
  • Loading branch information
FileEX and tederis authored Sep 4, 2024
1 parent 023821e commit 9d65bb6
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Client/mods/deathmatch/logic/CPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,7 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream)
// bool (1) - static
// SObjectHealthSync (?) - health
// bool (1) - is break
// bool (1) - respawnable

// Pickups:
// CVector (12) - position
Expand Down Expand Up @@ -3089,7 +3090,10 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream)
if (bitStream.ReadBit())
pObject->Break();
}


if (bitStream.Can(eBitStreamVersion::RespawnObject_Serverside))
pObject->SetRespawnEnabled(bitStream.ReadBit());

pObject->SetCollisionEnabled(bCollisonsEnabled);
if (ucEntityTypeID == CClientGame::WEAPON)
{
Expand Down
14 changes: 14 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void CLuaObjectDefs::LoadFunctions()
{"getObjectMass", GetObjectMass},
{"getObjectProperty", GetObjectProperty},
{"isObjectMoving", ArgumentParser<IsObjectMoving>},
{"isObjectRespawnable", ArgumentParser<IsObjectRespawnable>},

// Object set funcs
{"moveObject", MoveObject},
Expand Down Expand Up @@ -61,6 +62,7 @@ void CLuaObjectDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "getProperties", GetObjectProperties);
lua_classfunction(luaVM, "getProperty", "getObjectProperty");
lua_classfunction(luaVM, "isMoving", "isObjectMoving");
lua_classfunction(luaVM, "isRespawnable", "isObjectRespawnable");

lua_classfunction(luaVM, "setScale", "setObjectScale");
lua_classfunction(luaVM, "setBreakable", "setObjectBreakable");
Expand Down Expand Up @@ -707,3 +709,15 @@ int CLuaObjectDefs::SetObjectProperty(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

bool CLuaObjectDefs::IsObjectRespawnable(CClientEntity* const pEntity) noexcept
{
if (!IS_OBJECT(pEntity))
return false;

auto* pObject = static_cast<CDeathmatchObject*>(pEntity);
if (!pObject)
return false;

return pObject->IsRespawnEnabled();
}
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CLuaObjectDefs : public CLuaDefs
LUA_DECLARE(GetObjectMass);
LUA_DECLARE(GetObjectProperty);
LUA_DECLARE(GetObjectProperties);
static bool IsObjectRespawnable(CClientEntity* const pEntity) noexcept;

// Object set funcs
LUA_DECLARE(SetObjectRotation);
Expand Down
22 changes: 20 additions & 2 deletions Client/mods/deathmatch/logic/rpc/CObjectRPCs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void CObjectRPCs::LoadFunctions()
AddHandler(SET_OBJECT_VISIBLE_IN_ALL_DIMENSIONS, SetObjectVisibleInAllDimensions, "SetObjectVisibleInAllDimensions");
AddHandler(SET_OBJECT_BREAKABLE, SetObjectBreakable, "SetObjectBreakable");
AddHandler(BREAK_OBJECT, BreakObject, "BreakObject");
AddHandler(RESPAWN_OBJECT, RespawnObject, "RespawnObject");
AddHandler(TOGGLE_OBJECT_RESPAWN, ToggleObjectRespawn, "ToggleObjectRespawn");
}

void CObjectRPCs::DestroyAllObjects(NetBitStreamInterface& bitStream)
Expand Down Expand Up @@ -130,8 +132,24 @@ void CObjectRPCs::SetObjectBreakable(CClientEntity* pSource, NetBitStreamInterfa

void CObjectRPCs::BreakObject(CClientEntity* pSource, NetBitStreamInterface& bitStream)
{
auto pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));
auto* pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));

if (pObject != nullptr)
if (pObject)
pObject->Break();
}

void CObjectRPCs::RespawnObject(CClientEntity* pSource, NetBitStreamInterface& bitStream)
{
auto* pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));

if (pObject)
g_pClientGame->GetObjectRespawner()->Respawn(pObject);
}

void CObjectRPCs::ToggleObjectRespawn(CClientEntity* pSource, NetBitStreamInterface& bitStream)
{
auto* pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));

if (pObject)
pObject->SetRespawnEnabled(bitStream.ReadBit());
}
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/rpc/CObjectRPCs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ class CObjectRPCs : public CRPCFunctions
DECLARE_ELEMENT_RPC(SetObjectVisibleInAllDimensions);
DECLARE_ELEMENT_RPC(SetObjectBreakable);
DECLARE_ELEMENT_RPC(BreakObject);
DECLARE_ELEMENT_RPC(RespawnObject);
DECLARE_ELEMENT_RPC(ToggleObjectRespawn);
};
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ CObject::CObject(CElement* pParent, CObjectManager* pObjectManager, bool bIsLowL
m_bIsFrozen = false;
m_bDoubleSided = false;
m_bBreakable = false;
m_bRespawnable = false;

m_bCollisionsEnabled = true;

Expand All @@ -58,6 +59,7 @@ CObject::CObject(const CObject& Copy) : CElement(Copy.m_pParent), m_bIsLowLod(Co
m_bBreakable = Copy.m_bBreakable;
m_vecPosition = Copy.m_vecPosition;
m_vecRotation = Copy.m_vecRotation;
m_bRespawnable = Copy.m_bRespawnable;

m_pMoveAnimation = NULL;
if (Copy.m_pMoveAnimation != NULL)
Expand Down
4 changes: 4 additions & 0 deletions Server/mods/deathmatch/logic/CObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class CObject : public CElement
bool IsBreakable() { return m_bBreakable; }
void SetBreakable(bool bBreakable) { m_bBreakable = bBreakable; }

bool IsRespawnEnabled() const noexcept { return m_bRespawnable; }
void SetRespawnEnabled(bool bRespawn) noexcept { m_bRespawnable = bRespawn; }

protected:
bool ReadSpecialData(const int iLine) override;

Expand All @@ -96,6 +99,7 @@ class CObject : public CElement
bool m_bSyncable;
CPlayer* m_pSyncer;
bool m_bVisibleInAllDimensions = false;
bool m_bRespawnable;

protected:
bool m_bCollisionsEnabled;
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CPerfStat.RPCPacketUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ ADD_ENUM1(SET_COLPOLYGON_HEIGHT)
ADD_ENUM1(SET_OBJECT_BREAKABLE)
ADD_ENUM1(BREAK_OBJECT)
ADD_ENUM1(SET_PLAYER_SCRIPT_DEBUG_LEVEL)
ADD_ENUM1(RESPAWN_OBJECT)
ADD_ENUM1(TOGGLE_OBJECT_RESPAWN)
IMPLEMENT_ENUM_END("eElementRPCFunctions")

DECLARE_ENUM(CRPCFunctions::eRPCFunctions);
Expand Down
37 changes: 37 additions & 0 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8358,6 +8358,43 @@ bool CStaticFunctionDefinitions::SetObjectBreakable(CElement* pElement, const bo
return false;
}

bool CStaticFunctionDefinitions::RespawnObject(CElement* const pElement) noexcept
{
RUN_CHILDREN(RespawnObject(*iter));

if (!IS_OBJECT(pElement))
return false;

CObject* pObject = static_cast<CObject*>(pElement);
if (!pObject)
return false;

CBitStream BitStream;
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pObject, RESPAWN_OBJECT, *BitStream.pBitStream));

return true;
}

bool CStaticFunctionDefinitions::ToggleObjectRespawn(CElement* const pElement, const bool bRespawn) noexcept
{
RUN_CHILDREN(ToggleObjectRespawn(*iter, bRespawn));

if (!IS_OBJECT(pElement))
return false;

CObject* pObject = static_cast<CObject*>(pElement);
if (!pObject)
return false;

pObject->SetRespawnEnabled(bRespawn);

CBitStream BitStream;
BitStream->WriteBit(bRespawn);
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pObject, TOGGLE_OBJECT_RESPAWN, *BitStream.pBitStream));

return true;
}

CRadarArea* CStaticFunctionDefinitions::CreateRadarArea(CResource* pResource, const CVector2D& vecPosition2D, const CVector2D& vecSize, const SColor color,
CElement* pVisibleTo)
{
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ class CStaticFunctionDefinitions
static bool BreakObject(CElement* pElement);
static bool SetObjectVisibleInAllDimensions(CElement* pElement, bool bVisible, unsigned short usNewDimension = 0);
static bool SetObjectBreakable(CElement* pElement, const bool bBreakable);
static bool RespawnObject(CElement* const pElement) noexcept;
static bool ToggleObjectRespawn(CElement* const pElement, const bool bRespawn) noexcept;

// Radar area create/destroy funcs
static CRadarArea* CreateRadarArea(CResource* pResource, const CVector2D& vecPosition, const CVector2D& vecSize, const SColor color, CElement* pVisibleTo);
Expand Down
21 changes: 21 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ void CLuaObjectDefs::LoadFunctions()
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
// Object create/destroy funcs
{"createObject", CreateObject},
{"respawnObject", ArgumentParser<RespawnObject>},

// Object get funcs
{"getObjectRotation", GetObjectRotation},
{"getObjectScale", GetObjectScale},
{"isObjectBreakable", ArgumentParser<IsObjectBreakable>},
{"isObjectMoving", ArgumentParser<IsObjectMoving>},
{"isObjectRespawnable", ArgumentParser<IsObjectRespawnable>},

// Object set funcs
{"setObjectRotation", SetObjectRotation},
Expand All @@ -33,6 +35,7 @@ void CLuaObjectDefs::LoadFunctions()
{"moveObject", MoveObject},
{"stopObject", StopObject},
{"breakObject", ArgumentParser<BreakObject>},
{"toggleObjectRespawn", ArgumentParser<ToggleObjectRespawn>},
};

// Add functions
Expand All @@ -48,16 +51,19 @@ void CLuaObjectDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "move", "moveObject");
lua_classfunction(luaVM, "stop", "stopObject");
lua_classfunction(luaVM, "break", "breakObject");
lua_classfunction(luaVM, "respawn", "respawnObject");

lua_classfunction(luaVM, "getScale", "getObjectScale");
lua_classfunction(luaVM, "setScale", "setObjectScale");
lua_classfunction(luaVM, "isBreakable", "isObjectBreakable");
lua_classfunction(luaVM, "setBreakable", "setObjectBreakable");
lua_classfunction(luaVM, "isMoving", "isObjectMoving");
lua_classfunction(luaVM, "toggleRespawn", "toggleObjectRespawn");

lua_classvariable(luaVM, "scale", "setObjectScale", "getObjectScale");
lua_classvariable(luaVM, "breakable", "setObjectBreakable", "isObjectBreakable");
lua_classvariable(luaVM, "moving", nullptr, "isObjectMoving");
lua_classvariable(luaVM, "isRespawnable", nullptr, "isObjectRespawnable");

lua_registerclass(luaVM, "Object", "Element");
}
Expand Down Expand Up @@ -312,3 +318,18 @@ bool CLuaObjectDefs::BreakObject(CObject* const pObject)
{
return CStaticFunctionDefinitions::BreakObject(pObject);
}

bool CLuaObjectDefs::RespawnObject(CObject* const pObject) noexcept
{
return CStaticFunctionDefinitions::RespawnObject(pObject);
}

bool CLuaObjectDefs::ToggleObjectRespawn(CObject* const pObject, const bool bRespawn) noexcept
{
return CStaticFunctionDefinitions::ToggleObjectRespawn(pObject, bRespawn);
}

bool CLuaObjectDefs::IsObjectRespawnable(CObject* const pObject) noexcept
{
return pObject->IsRespawnEnabled();
}
3 changes: 3 additions & 0 deletions Server/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class CLuaObjectDefs : public CLuaDefs

// Object create/destroy functions
LUA_DECLARE(CreateObject);
static bool RespawnObject(CObject* const pObject) noexcept;

// Object get functions
LUA_DECLARE(GetObjectName);
LUA_DECLARE(GetObjectRotation);
LUA_DECLARE(GetObjectScale);
static bool IsObjectBreakable(CObject* const pObject);
static bool IsObjectMoving(CObject* const pObject);
static bool IsObjectRespawnable(CObject* const pObject) noexcept;

// Object set functions
LUA_DECLARE(SetObjectName);
Expand All @@ -36,4 +38,5 @@ class CLuaObjectDefs : public CLuaDefs
LUA_DECLARE(MoveObject);
LUA_DECLARE(StopObject);
static bool BreakObject(CObject* const pObject);
static bool ToggleObjectRespawn(CObject* const pObject, const bool bRespawn) noexcept;
};
4 changes: 4 additions & 0 deletions Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ bool CEntityAddPacket::Write(NetBitStreamInterface& BitStream) const
if (BitStream.Can(eBitStreamVersion::BreakObject_Serverside))
BitStream.WriteBit(pObject->GetHealth() <= 0);

// Respawnable
if (BitStream.Can(eBitStreamVersion::RespawnObject_Serverside))
BitStream.WriteBit(pObject->IsRespawnEnabled());

if (ucEntityTypeID == CElement::WEAPON)
{
CCustomWeapon* pWeapon = static_cast<CCustomWeapon*>(pElement);
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 @@ -552,6 +552,10 @@ enum class eBitStreamVersion : unsigned short
// 2024-05-31
BreakObject_Serverside,

// Add respawnObject and toggleObjectRespawn to serverside
// 2024-06-09
RespawnObject_Serverside,

// Ped syncronization revision
// 2024-06-16
PedSync_Revision,
Expand Down
2 changes: 2 additions & 0 deletions Shared/sdk/net/rpc_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ enum eElementRPCFunctions
SET_WORLD_SPECIAL_PROPERTY,

BREAK_OBJECT,
RESPAWN_OBJECT,
TOGGLE_OBJECT_RESPAWN,

SET_PLAYER_SCRIPT_DEBUG_LEVEL,

Expand Down

0 comments on commit 9d65bb6

Please sign in to comment.