Skip to content

Commit fdaced0

Browse files
authored
Refactor entity hierarchy checks (PR #3662)
1 parent 23bf187 commit fdaced0

File tree

2 files changed

+46
-51
lines changed

2 files changed

+46
-51
lines changed

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,47 +1247,44 @@ bool CStaticFunctionDefinitions::SetElementAngularVelocity(CClientEntity& Entity
12471247

12481248
bool CStaticFunctionDefinitions::SetElementParent(CClientEntity& Entity, CClientEntity& Parent, CLuaMain* pLuaMain)
12491249
{
1250-
if (&Entity != &Parent && !Entity.IsMyChild(&Parent, true))
1250+
if (&Entity == &Parent || Entity.IsMyChild(&Parent, true))
1251+
return false;
1252+
1253+
if (Entity.GetType() == CCLIENTCAMERA || Parent.GetType() == CCLIENTCAMERA)
1254+
return false;
1255+
1256+
if (Entity.GetType() == CCLIENTGUI)
12511257
{
1252-
if (Entity.GetType() == CCLIENTCAMERA || Parent.GetType() == CCLIENTCAMERA)
1253-
{
1258+
if (Parent.GetType() != CCLIENTGUI && &Parent != pLuaMain->GetResource()->GetResourceGUIEntity())
12541259
return false;
1255-
}
1256-
else if (Entity.GetType() == CCLIENTGUI)
1257-
{
1258-
if (Parent.GetType() == CCLIENTGUI || &Parent == pLuaMain->GetResource()->GetResourceGUIEntity())
1259-
{
1260-
CClientGUIElement& GUIElement = static_cast<CClientGUIElement&>(Entity);
12611260

1262-
GUIElement.SetParent(&Parent);
1263-
return true;
1264-
}
1265-
}
1266-
else
1267-
{
1268-
CClientEntity* pTemp = &Parent;
1269-
CClientEntity* pRoot = m_pRootEntity;
1270-
bool bValidParent = false;
1271-
while (pTemp != pRoot && pTemp != NULL)
1272-
{
1273-
const char* szTypeName = pTemp->GetTypeName();
1274-
if (szTypeName && strcmp(szTypeName, "map") == 0)
1275-
{
1276-
bValidParent = true; // parents must be a map
1277-
break;
1278-
}
1261+
CClientGUIElement& GUIElement = static_cast<CClientGUIElement&>(Entity);
12791262

1280-
pTemp = pTemp->GetParent();
1281-
}
1263+
GUIElement.SetParent(&Parent);
1264+
return true;
1265+
}
12821266

1283-
// Make sure the entity we move is a client entity or we get a problem
1284-
if (bValidParent && Entity.IsLocalEntity())
1285-
{
1286-
// Set the new parent
1287-
Entity.SetParent(&Parent);
1288-
return true;
1289-
}
1267+
CClientEntity* pTemp = &Parent;
1268+
CClientEntity* pRoot = m_pRootEntity;
1269+
bool bValidParent = false;
1270+
while (pTemp != pRoot && pTemp != NULL)
1271+
{
1272+
const char* szTypeName = pTemp->GetTypeName();
1273+
if (szTypeName && strcmp(szTypeName, "map") == 0)
1274+
{
1275+
bValidParent = true; // parents must be a map
1276+
break;
12901277
}
1278+
1279+
pTemp = pTemp->GetParent();
1280+
}
1281+
1282+
// Make sure the entity we move is a client entity or we get a problem
1283+
if (bValidParent && Entity.IsLocalEntity())
1284+
{
1285+
// Set the new parent
1286+
Entity.SetParent(&Parent);
1287+
return true;
12911288
}
12921289

12931290
return false;

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,20 @@ void CElementRPCs::SetElementParent(CClientEntity* pSource, NetBitStreamInterfac
6464
{
6565
// Read out the entity id and parent id
6666
ElementID ParentID;
67-
if (bitStream.Read(ParentID))
68-
{
69-
CClientEntity* pParent = CElementIDs::GetElement(ParentID);
70-
if (pParent)
71-
{
72-
pSource->SetParent(pParent);
73-
}
74-
else
75-
{
76-
// TODO: raise an error
77-
}
78-
}
79-
else
80-
{
81-
// TODO: raise an error
82-
}
67+
if (!bitStream.Read(ParentID))
68+
return;
69+
70+
CClientEntity* pParent = CElementIDs::GetElement(ParentID);
71+
if (!pParent)
72+
return;
73+
74+
if (pParent->IsMyChild(pSource, true))
75+
return;
76+
77+
if (pSource->IsMyChild(pParent, true))
78+
return;
79+
80+
pSource->SetParent(pParent);
8381
}
8482

8583
void CElementRPCs::SetElementData(CClientEntity* pSource, NetBitStreamInterface& bitStream)

0 commit comments

Comments
 (0)