Skip to content

Commit a38e6ac

Browse files
authored
Fix unintended behavior for ped control states (#3964)
1 parent 04f297b commit a38e6ac

File tree

5 files changed

+38
-96
lines changed

5 files changed

+38
-96
lines changed

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,42 +1710,39 @@ bool CStaticFunctionDefinitions::GetPedClothes(CClientPed& Ped, unsigned char uc
17101710
return false;
17111711
}
17121712

1713-
bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& Ped, const char* szControl, bool& bState)
1713+
bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& const ped, const std::string control, bool& state) noexcept
17141714
{
1715-
if (&Ped == GetLocalPlayer())
1716-
{
1717-
return GetControlState(szControl, bState);
1718-
}
1715+
if (&ped == GetLocalPlayer())
1716+
return GetControlState(control.c_str(), state);
17191717

1720-
if (Ped.GetType() == CCLIENTPLAYER)
1718+
if (ped.GetType() == CCLIENTPLAYER)
17211719
{
1722-
CControllerState cs;
1723-
Ped.GetControllerState(cs);
1724-
bool bOnFoot = (!Ped.GetRealOccupiedVehicle());
1725-
bState = CClientPad::GetControlState(szControl, cs, bOnFoot);
1726-
float fState = 0;
1727-
unsigned int uiIndex;
1728-
// Check it's Analog
1729-
if (CClientPad::GetAnalogControlIndex(szControl, uiIndex))
1720+
CControllerState controller;
1721+
ped.GetControllerState(controller);
1722+
1723+
bool foot = !ped.GetRealOccupiedVehicle();
1724+
state = CClientPad::GetControlState(control.c_str(), controller, foot);
1725+
1726+
float analog = 0;
1727+
std::uint32_t index;
1728+
1729+
if (CClientPad::GetAnalogControlIndex(control.c_str(), index))
17301730
{
1731-
if (CClientPad::GetAnalogControlState(szControl, cs, bOnFoot, fState, false))
1731+
if (CClientPad::GetAnalogControlState(control.c_str(), controller, foot, analog, false))
17321732
{
1733-
bState = fState > 0;
1733+
state = analog > 0;
17341734
return true;
17351735
}
17361736
}
1737-
// or binary.
17381737
else
17391738
{
1740-
bState = CClientPad::GetControlState(szControl, cs, bOnFoot);
1739+
state = CClientPad::GetControlState(control.c_str(), controller, foot);
17411740
return true;
17421741
}
17431742
}
17441743

1745-
if (Ped.m_Pad.GetControlState(szControl, bState))
1746-
{
1744+
if (ped.m_Pad.GetControlState(control.c_str(), state))
17471745
return true;
1748-
}
17491746

17501747
return false;
17511748
}
@@ -2366,24 +2363,13 @@ bool CStaticFunctionDefinitions::RemovePedClothes(CClientEntity& Entity, unsigne
23662363
return false;
23672364
}
23682365

2369-
bool CStaticFunctionDefinitions::SetPedControlState(CClientEntity& Entity, const char* szControl, bool bState)
2366+
bool CStaticFunctionDefinitions::SetPedControlState(CClientPed& const ped, const std::string control, const bool state) noexcept
23702367
{
2371-
RUN_CHILDREN(SetPedControlState(**iter, szControl, bState))
2372-
if (IS_PED(&Entity))
2373-
{
2374-
CClientPed& Ped = static_cast<CClientPed&>(Entity);
2375-
2376-
if (&Ped == GetLocalPlayer())
2377-
{
2378-
return SetControlState(szControl, bState);
2379-
}
2368+
if (&ped == GetLocalPlayer())
2369+
return SetControlState(control.c_str(), state);
23802370

2381-
if (Ped.m_Pad.SetControlState(szControl, bState))
2382-
{
2383-
return true;
2384-
}
2385-
}
2386-
return false;
2371+
if (ped.m_Pad.SetControlState(control.c_str(), state))
2372+
return true;
23872373
}
23882374

23892375
bool CStaticFunctionDefinitions::SetPedDoingGangDriveby(CClientEntity& Entity, bool bGangDriveby)

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class CStaticFunctionDefinitions
137137
static bool IsPedDoingTask(CClientPed& Ped, const char* szTaskName, bool& bIsDoingTask);
138138
static bool GetPedBonePosition(CClientPed& Ped, eBone bone, CVector& vecPosition);
139139
static bool GetPedClothes(CClientPed& Ped, unsigned char ucType, SString& strOutTexture, SString& strOutModel);
140-
static bool GetPedControlState(CClientPed& Ped, const char* szControl, bool& bState);
140+
static bool GetPedControlState(CClientPed& const ped, const std::string control, bool& state) noexcept;
141141
static bool GetPedAnalogControlState(CClientPed& Ped, const char* szControl, float& fState, bool bRawInput);
142142
static bool IsPedDoingGangDriveby(CClientPed& Ped, bool& bDoingGangDriveby);
143143
static bool GetPedFightingStyle(CClientPed& Ped, unsigned char& ucStyle);
@@ -174,7 +174,7 @@ class CStaticFunctionDefinitions
174174
static bool SetPedMoveAnim(CClientEntity& Entity, unsigned int iMoveAnim);
175175
static bool AddPedClothes(CClientEntity& Entity, const char* szTexture, const char* szModel, unsigned char ucType);
176176
static bool RemovePedClothes(CClientEntity& Entity, unsigned char ucType);
177-
static bool SetPedControlState(CClientEntity& Entity, const char* szControl, bool bState);
177+
static bool SetPedControlState(CClientPed& const ped, const std::string control, const bool state) noexcept;
178178
static bool SetPedAnalogControlState(CClientEntity& Entity, const char* szControl, float fState);
179179
static bool SetPedDoingGangDriveby(CClientEntity& Entity, bool bGangDriveby);
180180
static bool SetPedFightingStyle(CClientEntity& Entity, unsigned char ucStyle);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void CLuaCompatibilityDefs::LoadFunctions()
6767
{"isPlayerDead", CLuaPedDefs::IsPedDead},
6868
{"guiEditSetCaratIndex", CLuaGUIDefs::GUIEditSetCaretIndex},
6969
{"guiMemoSetCaratIndex", CLuaGUIDefs::GUIMemoSetCaretIndex},
70-
{"setControlState", CLuaPedDefs::SetPedControlState},
71-
{"getControlState", CLuaPedDefs::GetPedControlState},
70+
{"setControlState", ArgumentParserWarn<false, CLuaPedDefs::SetPedControlState>},
71+
{"getControlState", ArgumentParserWarn<false, CLuaPedDefs::GetPedControlState>},
7272
{"setCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::SetCameraDrunkLevel>},
7373
{"getCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::GetCameraDrunkLevel>},
7474
};

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

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void CLuaPedDefs::LoadFunctions()
4545
{"setPedAnimationProgress", SetPedAnimationProgress},
4646
{"setPedAnimationSpeed", SetPedAnimationSpeed},
4747
{"setPedWalkingStyle", SetPedMoveAnim},
48-
{"setPedControlState", SetPedControlState},
48+
{"setPedControlState", ArgumentParserWarn<false, SetPedControlState>},
4949
{"setPedAnalogControlState", SetPedAnalogControlState},
5050
{"setPedDoingGangDriveby", SetPedDoingGangDriveby},
5151
{"setPedFightingStyle", ArgumentParser<SetPedFightingStyle>},
@@ -75,7 +75,7 @@ void CLuaPedDefs::LoadFunctions()
7575
{"getPedAnimationSpeed", ArgumentParser<GetPedAnimationSpeed>},
7676
{"getPedAnimationLength", ArgumentParser<GetPedAnimationLength>},
7777
{"getPedWalkingStyle", GetPedMoveAnim},
78-
{"getPedControlState", GetPedControlState},
78+
{"getPedControlState", ArgumentParserWarn<false, GetPedControlState>},
7979
{"getPedAnalogControlState", GetPedAnalogControlState},
8080
{"isPedDoingGangDriveby", IsPedDoingGangDriveby},
8181
{"getPedFightingStyle", GetPedFightingStyle},
@@ -1247,33 +1247,14 @@ int CLuaPedDefs::GetPedClothes(lua_State* luaVM)
12471247
return 1;
12481248
}
12491249

1250-
int CLuaPedDefs::GetPedControlState(lua_State* luaVM)
1250+
bool CLuaPedDefs::GetPedControlState(CClientPed* const ped, const std::string control) noexcept
12511251
{
1252-
// Verify the argument
1253-
CClientPed* pPed = CStaticFunctionDefinitions::GetLocalPlayer();
1254-
SString strControl = "";
1255-
CScriptArgReader argStream(luaVM);
1252+
bool state;
12561253

1257-
if (argStream.NextIsUserData())
1258-
{
1259-
argStream.ReadUserData(pPed);
1260-
}
1261-
argStream.ReadString(strControl);
1262-
1263-
if (!argStream.HasErrors())
1264-
{
1265-
bool bState;
1266-
if (CStaticFunctionDefinitions::GetPedControlState(*pPed, strControl, bState))
1267-
{
1268-
lua_pushboolean(luaVM, bState);
1269-
return 1;
1270-
}
1271-
}
1272-
else
1273-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
1254+
if (!CStaticFunctionDefinitions::GetPedControlState(*ped, control, state))
1255+
return false;
12741256

1275-
lua_pushboolean(luaVM, false);
1276-
return 1;
1257+
return state;
12771258
}
12781259

12791260
int CLuaPedDefs::GetPedAnalogControlState(lua_State* luaVM)
@@ -1822,34 +1803,9 @@ int CLuaPedDefs::RemovePedClothes(lua_State* luaVM)
18221803
return 1;
18231804
}
18241805

1825-
int CLuaPedDefs::SetPedControlState(lua_State* luaVM)
1806+
bool CLuaPedDefs::SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept
18261807
{
1827-
// Verify the argument
1828-
CClientEntity* pEntity = CStaticFunctionDefinitions::GetLocalPlayer();
1829-
SString strControl = "";
1830-
bool bState = false;
1831-
CScriptArgReader argStream(luaVM);
1832-
1833-
if (argStream.NextIsUserData())
1834-
{
1835-
argStream.ReadUserData(pEntity);
1836-
}
1837-
argStream.ReadString(strControl);
1838-
argStream.ReadBool(bState);
1839-
1840-
if (!argStream.HasErrors())
1841-
{
1842-
if (CStaticFunctionDefinitions::SetPedControlState(*pEntity, strControl, bState))
1843-
{
1844-
lua_pushboolean(luaVM, true);
1845-
return 1;
1846-
}
1847-
}
1848-
else
1849-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
1850-
1851-
lua_pushboolean(luaVM, false);
1852-
return 1;
1808+
return CStaticFunctionDefinitions::SetPedControlState(*ped, control, state);
18531809
}
18541810

18551811
int CLuaPedDefs::SetPedDoingGangDriveby(lua_State* luaVM)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CLuaPedDefs : public CLuaDefs
6565
static bool UpdateElementRpHAnim(lua_State* const luaVM, CClientEntity* entity);
6666
LUA_DECLARE_OOP(GetPedBonePosition);
6767
LUA_DECLARE(GetPedClothes);
68-
LUA_DECLARE(GetPedControlState);
68+
static bool GetPedControlState(CClientPed* const ped, const std::string control) noexcept;
6969
LUA_DECLARE(GetPedAnalogControlState);
7070
LUA_DECLARE(IsPedSunbathing);
7171
LUA_DECLARE(IsPedDoingGangDriveby);
@@ -96,7 +96,7 @@ class CLuaPedDefs : public CLuaDefs
9696
static bool IsPedReloadingWeapon(CClientPed* const ped) noexcept;
9797
LUA_DECLARE(AddPedClothes);
9898
LUA_DECLARE(RemovePedClothes);
99-
LUA_DECLARE(SetPedControlState);
99+
static bool SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept;
100100
LUA_DECLARE(SetPedAnalogControlState);
101101
LUA_DECLARE(SetPedDoingGangDriveby);
102102
static bool SetPedFightingStyle(CClientEntity* const entity, const unsigned int style);

0 commit comments

Comments
 (0)