Skip to content

Commit 9d65bb6

Browse files
FileEXtederis
andauthored
Object respawn functions (#3450)
* Object respawn functions * Assertion fix * Update CPerfStat.RPCPacketUsage.cpp * Review * Review * Fix build --------- Co-authored-by: TEDERIs <xcplay@gmail.com>
1 parent 023821e commit 9d65bb6

File tree

15 files changed

+123
-3
lines changed

15 files changed

+123
-3
lines changed

Client/mods/deathmatch/logic/CPacketHandler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,7 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream)
27242724
// bool (1) - static
27252725
// SObjectHealthSync (?) - health
27262726
// bool (1) - is break
2727+
// bool (1) - respawnable
27272728

27282729
// Pickups:
27292730
// CVector (12) - position
@@ -3089,7 +3090,10 @@ void CPacketHandler::Packet_EntityAdd(NetBitStreamInterface& bitStream)
30893090
if (bitStream.ReadBit())
30903091
pObject->Break();
30913092
}
3092-
3093+
3094+
if (bitStream.Can(eBitStreamVersion::RespawnObject_Serverside))
3095+
pObject->SetRespawnEnabled(bitStream.ReadBit());
3096+
30933097
pObject->SetCollisionEnabled(bCollisonsEnabled);
30943098
if (ucEntityTypeID == CClientGame::WEAPON)
30953099
{

Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void CLuaObjectDefs::LoadFunctions()
2525
{"getObjectMass", GetObjectMass},
2626
{"getObjectProperty", GetObjectProperty},
2727
{"isObjectMoving", ArgumentParser<IsObjectMoving>},
28+
{"isObjectRespawnable", ArgumentParser<IsObjectRespawnable>},
2829

2930
// Object set funcs
3031
{"moveObject", MoveObject},
@@ -61,6 +62,7 @@ void CLuaObjectDefs::AddClass(lua_State* luaVM)
6162
lua_classfunction(luaVM, "getProperties", GetObjectProperties);
6263
lua_classfunction(luaVM, "getProperty", "getObjectProperty");
6364
lua_classfunction(luaVM, "isMoving", "isObjectMoving");
65+
lua_classfunction(luaVM, "isRespawnable", "isObjectRespawnable");
6466

6567
lua_classfunction(luaVM, "setScale", "setObjectScale");
6668
lua_classfunction(luaVM, "setBreakable", "setObjectBreakable");
@@ -707,3 +709,15 @@ int CLuaObjectDefs::SetObjectProperty(lua_State* luaVM)
707709
lua_pushboolean(luaVM, false);
708710
return 1;
709711
}
712+
713+
bool CLuaObjectDefs::IsObjectRespawnable(CClientEntity* const pEntity) noexcept
714+
{
715+
if (!IS_OBJECT(pEntity))
716+
return false;
717+
718+
auto* pObject = static_cast<CDeathmatchObject*>(pEntity);
719+
if (!pObject)
720+
return false;
721+
722+
return pObject->IsRespawnEnabled();
723+
}

Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CLuaObjectDefs : public CLuaDefs
2929
LUA_DECLARE(GetObjectMass);
3030
LUA_DECLARE(GetObjectProperty);
3131
LUA_DECLARE(GetObjectProperties);
32+
static bool IsObjectRespawnable(CClientEntity* const pEntity) noexcept;
3233

3334
// Object set funcs
3435
LUA_DECLARE(SetObjectRotation);

Client/mods/deathmatch/logic/rpc/CObjectRPCs.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void CObjectRPCs::LoadFunctions()
2222
AddHandler(SET_OBJECT_VISIBLE_IN_ALL_DIMENSIONS, SetObjectVisibleInAllDimensions, "SetObjectVisibleInAllDimensions");
2323
AddHandler(SET_OBJECT_BREAKABLE, SetObjectBreakable, "SetObjectBreakable");
2424
AddHandler(BREAK_OBJECT, BreakObject, "BreakObject");
25+
AddHandler(RESPAWN_OBJECT, RespawnObject, "RespawnObject");
26+
AddHandler(TOGGLE_OBJECT_RESPAWN, ToggleObjectRespawn, "ToggleObjectRespawn");
2527
}
2628

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

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

135-
if (pObject != nullptr)
137+
if (pObject)
136138
pObject->Break();
137139
}
140+
141+
void CObjectRPCs::RespawnObject(CClientEntity* pSource, NetBitStreamInterface& bitStream)
142+
{
143+
auto* pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));
144+
145+
if (pObject)
146+
g_pClientGame->GetObjectRespawner()->Respawn(pObject);
147+
}
148+
149+
void CObjectRPCs::ToggleObjectRespawn(CClientEntity* pSource, NetBitStreamInterface& bitStream)
150+
{
151+
auto* pObject = static_cast<CDeathmatchObject*>(m_pObjectManager->Get(pSource->GetID()));
152+
153+
if (pObject)
154+
pObject->SetRespawnEnabled(bitStream.ReadBit());
155+
}

Client/mods/deathmatch/logic/rpc/CObjectRPCs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ class CObjectRPCs : public CRPCFunctions
2727
DECLARE_ELEMENT_RPC(SetObjectVisibleInAllDimensions);
2828
DECLARE_ELEMENT_RPC(SetObjectBreakable);
2929
DECLARE_ELEMENT_RPC(BreakObject);
30+
DECLARE_ELEMENT_RPC(RespawnObject);
31+
DECLARE_ELEMENT_RPC(ToggleObjectRespawn);
3032
};

Server/mods/deathmatch/logic/CObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CObject::CObject(CElement* pParent, CObjectManager* pObjectManager, bool bIsLowL
3333
m_bIsFrozen = false;
3434
m_bDoubleSided = false;
3535
m_bBreakable = false;
36+
m_bRespawnable = false;
3637

3738
m_bCollisionsEnabled = true;
3839

@@ -58,6 +59,7 @@ CObject::CObject(const CObject& Copy) : CElement(Copy.m_pParent), m_bIsLowLod(Co
5859
m_bBreakable = Copy.m_bBreakable;
5960
m_vecPosition = Copy.m_vecPosition;
6061
m_vecRotation = Copy.m_vecRotation;
62+
m_bRespawnable = Copy.m_bRespawnable;
6163

6264
m_pMoveAnimation = NULL;
6365
if (Copy.m_pMoveAnimation != NULL)

Server/mods/deathmatch/logic/CObject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class CObject : public CElement
8181
bool IsBreakable() { return m_bBreakable; }
8282
void SetBreakable(bool bBreakable) { m_bBreakable = bBreakable; }
8383

84+
bool IsRespawnEnabled() const noexcept { return m_bRespawnable; }
85+
void SetRespawnEnabled(bool bRespawn) noexcept { m_bRespawnable = bRespawn; }
86+
8487
protected:
8588
bool ReadSpecialData(const int iLine) override;
8689

@@ -96,6 +99,7 @@ class CObject : public CElement
9699
bool m_bSyncable;
97100
CPlayer* m_pSyncer;
98101
bool m_bVisibleInAllDimensions = false;
102+
bool m_bRespawnable;
99103

100104
protected:
101105
bool m_bCollisionsEnabled;

Server/mods/deathmatch/logic/CPerfStat.RPCPacketUsage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ ADD_ENUM1(SET_COLPOLYGON_HEIGHT)
226226
ADD_ENUM1(SET_OBJECT_BREAKABLE)
227227
ADD_ENUM1(BREAK_OBJECT)
228228
ADD_ENUM1(SET_PLAYER_SCRIPT_DEBUG_LEVEL)
229+
ADD_ENUM1(RESPAWN_OBJECT)
230+
ADD_ENUM1(TOGGLE_OBJECT_RESPAWN)
229231
IMPLEMENT_ENUM_END("eElementRPCFunctions")
230232

231233
DECLARE_ENUM(CRPCFunctions::eRPCFunctions);

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8358,6 +8358,43 @@ bool CStaticFunctionDefinitions::SetObjectBreakable(CElement* pElement, const bo
83588358
return false;
83598359
}
83608360

8361+
bool CStaticFunctionDefinitions::RespawnObject(CElement* const pElement) noexcept
8362+
{
8363+
RUN_CHILDREN(RespawnObject(*iter));
8364+
8365+
if (!IS_OBJECT(pElement))
8366+
return false;
8367+
8368+
CObject* pObject = static_cast<CObject*>(pElement);
8369+
if (!pObject)
8370+
return false;
8371+
8372+
CBitStream BitStream;
8373+
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pObject, RESPAWN_OBJECT, *BitStream.pBitStream));
8374+
8375+
return true;
8376+
}
8377+
8378+
bool CStaticFunctionDefinitions::ToggleObjectRespawn(CElement* const pElement, const bool bRespawn) noexcept
8379+
{
8380+
RUN_CHILDREN(ToggleObjectRespawn(*iter, bRespawn));
8381+
8382+
if (!IS_OBJECT(pElement))
8383+
return false;
8384+
8385+
CObject* pObject = static_cast<CObject*>(pElement);
8386+
if (!pObject)
8387+
return false;
8388+
8389+
pObject->SetRespawnEnabled(bRespawn);
8390+
8391+
CBitStream BitStream;
8392+
BitStream->WriteBit(bRespawn);
8393+
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pObject, TOGGLE_OBJECT_RESPAWN, *BitStream.pBitStream));
8394+
8395+
return true;
8396+
}
8397+
83618398
CRadarArea* CStaticFunctionDefinitions::CreateRadarArea(CResource* pResource, const CVector2D& vecPosition2D, const CVector2D& vecSize, const SColor color,
83628399
CElement* pVisibleTo)
83638400
{

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ class CStaticFunctionDefinitions
424424
static bool BreakObject(CElement* pElement);
425425
static bool SetObjectVisibleInAllDimensions(CElement* pElement, bool bVisible, unsigned short usNewDimension = 0);
426426
static bool SetObjectBreakable(CElement* pElement, const bool bBreakable);
427+
static bool RespawnObject(CElement* const pElement) noexcept;
428+
static bool ToggleObjectRespawn(CElement* const pElement, const bool bRespawn) noexcept;
427429

428430
// Radar area create/destroy funcs
429431
static CRadarArea* CreateRadarArea(CResource* pResource, const CVector2D& vecPosition, const CVector2D& vecSize, const SColor color, CElement* pVisibleTo);

Server/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ void CLuaObjectDefs::LoadFunctions()
1919
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
2020
// Object create/destroy funcs
2121
{"createObject", CreateObject},
22+
{"respawnObject", ArgumentParser<RespawnObject>},
2223

2324
// Object get funcs
2425
{"getObjectRotation", GetObjectRotation},
2526
{"getObjectScale", GetObjectScale},
2627
{"isObjectBreakable", ArgumentParser<IsObjectBreakable>},
2728
{"isObjectMoving", ArgumentParser<IsObjectMoving>},
29+
{"isObjectRespawnable", ArgumentParser<IsObjectRespawnable>},
2830

2931
// Object set funcs
3032
{"setObjectRotation", SetObjectRotation},
@@ -33,6 +35,7 @@ void CLuaObjectDefs::LoadFunctions()
3335
{"moveObject", MoveObject},
3436
{"stopObject", StopObject},
3537
{"breakObject", ArgumentParser<BreakObject>},
38+
{"toggleObjectRespawn", ArgumentParser<ToggleObjectRespawn>},
3639
};
3740

3841
// Add functions
@@ -48,16 +51,19 @@ void CLuaObjectDefs::AddClass(lua_State* luaVM)
4851
lua_classfunction(luaVM, "move", "moveObject");
4952
lua_classfunction(luaVM, "stop", "stopObject");
5053
lua_classfunction(luaVM, "break", "breakObject");
54+
lua_classfunction(luaVM, "respawn", "respawnObject");
5155

5256
lua_classfunction(luaVM, "getScale", "getObjectScale");
5357
lua_classfunction(luaVM, "setScale", "setObjectScale");
5458
lua_classfunction(luaVM, "isBreakable", "isObjectBreakable");
5559
lua_classfunction(luaVM, "setBreakable", "setObjectBreakable");
5660
lua_classfunction(luaVM, "isMoving", "isObjectMoving");
61+
lua_classfunction(luaVM, "toggleRespawn", "toggleObjectRespawn");
5762

5863
lua_classvariable(luaVM, "scale", "setObjectScale", "getObjectScale");
5964
lua_classvariable(luaVM, "breakable", "setObjectBreakable", "isObjectBreakable");
6065
lua_classvariable(luaVM, "moving", nullptr, "isObjectMoving");
66+
lua_classvariable(luaVM, "isRespawnable", nullptr, "isObjectRespawnable");
6167

6268
lua_registerclass(luaVM, "Object", "Element");
6369
}
@@ -312,3 +318,18 @@ bool CLuaObjectDefs::BreakObject(CObject* const pObject)
312318
{
313319
return CStaticFunctionDefinitions::BreakObject(pObject);
314320
}
321+
322+
bool CLuaObjectDefs::RespawnObject(CObject* const pObject) noexcept
323+
{
324+
return CStaticFunctionDefinitions::RespawnObject(pObject);
325+
}
326+
327+
bool CLuaObjectDefs::ToggleObjectRespawn(CObject* const pObject, const bool bRespawn) noexcept
328+
{
329+
return CStaticFunctionDefinitions::ToggleObjectRespawn(pObject, bRespawn);
330+
}
331+
332+
bool CLuaObjectDefs::IsObjectRespawnable(CObject* const pObject) noexcept
333+
{
334+
return pObject->IsRespawnEnabled();
335+
}

Server/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ class CLuaObjectDefs : public CLuaDefs
2020

2121
// Object create/destroy functions
2222
LUA_DECLARE(CreateObject);
23+
static bool RespawnObject(CObject* const pObject) noexcept;
2324

2425
// Object get functions
2526
LUA_DECLARE(GetObjectName);
2627
LUA_DECLARE(GetObjectRotation);
2728
LUA_DECLARE(GetObjectScale);
2829
static bool IsObjectBreakable(CObject* const pObject);
2930
static bool IsObjectMoving(CObject* const pObject);
31+
static bool IsObjectRespawnable(CObject* const pObject) noexcept;
3032

3133
// Object set functions
3234
LUA_DECLARE(SetObjectName);
@@ -36,4 +38,5 @@ class CLuaObjectDefs : public CLuaDefs
3638
LUA_DECLARE(MoveObject);
3739
LUA_DECLARE(StopObject);
3840
static bool BreakObject(CObject* const pObject);
41+
static bool ToggleObjectRespawn(CObject* const pObject, const bool bRespawn) noexcept;
3942
};

Server/mods/deathmatch/logic/packets/CEntityAddPacket.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ bool CEntityAddPacket::Write(NetBitStreamInterface& BitStream) const
301301
if (BitStream.Can(eBitStreamVersion::BreakObject_Serverside))
302302
BitStream.WriteBit(pObject->GetHealth() <= 0);
303303

304+
// Respawnable
305+
if (BitStream.Can(eBitStreamVersion::RespawnObject_Serverside))
306+
BitStream.WriteBit(pObject->IsRespawnEnabled());
307+
304308
if (ucEntityTypeID == CElement::WEAPON)
305309
{
306310
CCustomWeapon* pWeapon = static_cast<CCustomWeapon*>(pElement);

Shared/sdk/net/bitstream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ enum class eBitStreamVersion : unsigned short
552552
// 2024-05-31
553553
BreakObject_Serverside,
554554

555+
// Add respawnObject and toggleObjectRespawn to serverside
556+
// 2024-06-09
557+
RespawnObject_Serverside,
558+
555559
// Ped syncronization revision
556560
// 2024-06-16
557561
PedSync_Revision,

Shared/sdk/net/rpc_enums.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ enum eElementRPCFunctions
278278
SET_WORLD_SPECIAL_PROPERTY,
279279

280280
BREAK_OBJECT,
281+
RESPAWN_OBJECT,
282+
TOGGLE_OBJECT_RESPAWN,
281283

282284
SET_PLAYER_SCRIPT_DEBUG_LEVEL,
283285

0 commit comments

Comments
 (0)