Skip to content

Commit

Permalink
Fix unintended behavior for ped control states (#3964)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 authored Jan 28, 2025
1 parent 04f297b commit a38e6ac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 96 deletions.
60 changes: 23 additions & 37 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,42 +1710,39 @@ bool CStaticFunctionDefinitions::GetPedClothes(CClientPed& Ped, unsigned char uc
return false;
}

bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& Ped, const char* szControl, bool& bState)
bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& const ped, const std::string control, bool& state) noexcept
{
if (&Ped == GetLocalPlayer())
{
return GetControlState(szControl, bState);
}
if (&ped == GetLocalPlayer())
return GetControlState(control.c_str(), state);

if (Ped.GetType() == CCLIENTPLAYER)
if (ped.GetType() == CCLIENTPLAYER)
{
CControllerState cs;
Ped.GetControllerState(cs);
bool bOnFoot = (!Ped.GetRealOccupiedVehicle());
bState = CClientPad::GetControlState(szControl, cs, bOnFoot);
float fState = 0;
unsigned int uiIndex;
// Check it's Analog
if (CClientPad::GetAnalogControlIndex(szControl, uiIndex))
CControllerState controller;
ped.GetControllerState(controller);

bool foot = !ped.GetRealOccupiedVehicle();
state = CClientPad::GetControlState(control.c_str(), controller, foot);

float analog = 0;
std::uint32_t index;

if (CClientPad::GetAnalogControlIndex(control.c_str(), index))
{
if (CClientPad::GetAnalogControlState(szControl, cs, bOnFoot, fState, false))
if (CClientPad::GetAnalogControlState(control.c_str(), controller, foot, analog, false))
{
bState = fState > 0;
state = analog > 0;
return true;
}
}
// or binary.
else
{
bState = CClientPad::GetControlState(szControl, cs, bOnFoot);
state = CClientPad::GetControlState(control.c_str(), controller, foot);
return true;
}
}

if (Ped.m_Pad.GetControlState(szControl, bState))
{
if (ped.m_Pad.GetControlState(control.c_str(), state))
return true;
}

return false;
}
Expand Down Expand Up @@ -2366,24 +2363,13 @@ bool CStaticFunctionDefinitions::RemovePedClothes(CClientEntity& Entity, unsigne
return false;
}

bool CStaticFunctionDefinitions::SetPedControlState(CClientEntity& Entity, const char* szControl, bool bState)
bool CStaticFunctionDefinitions::SetPedControlState(CClientPed& const ped, const std::string control, const bool state) noexcept
{
RUN_CHILDREN(SetPedControlState(**iter, szControl, bState))
if (IS_PED(&Entity))
{
CClientPed& Ped = static_cast<CClientPed&>(Entity);

if (&Ped == GetLocalPlayer())
{
return SetControlState(szControl, bState);
}
if (&ped == GetLocalPlayer())
return SetControlState(control.c_str(), state);

if (Ped.m_Pad.SetControlState(szControl, bState))
{
return true;
}
}
return false;
if (ped.m_Pad.SetControlState(control.c_str(), state))
return true;
}

bool CStaticFunctionDefinitions::SetPedDoingGangDriveby(CClientEntity& Entity, bool bGangDriveby)
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CStaticFunctionDefinitions
static bool IsPedDoingTask(CClientPed& Ped, const char* szTaskName, bool& bIsDoingTask);
static bool GetPedBonePosition(CClientPed& Ped, eBone bone, CVector& vecPosition);
static bool GetPedClothes(CClientPed& Ped, unsigned char ucType, SString& strOutTexture, SString& strOutModel);
static bool GetPedControlState(CClientPed& Ped, const char* szControl, bool& bState);
static bool GetPedControlState(CClientPed& const ped, const std::string control, bool& state) noexcept;
static bool GetPedAnalogControlState(CClientPed& Ped, const char* szControl, float& fState, bool bRawInput);
static bool IsPedDoingGangDriveby(CClientPed& Ped, bool& bDoingGangDriveby);
static bool GetPedFightingStyle(CClientPed& Ped, unsigned char& ucStyle);
Expand Down Expand Up @@ -174,7 +174,7 @@ class CStaticFunctionDefinitions
static bool SetPedMoveAnim(CClientEntity& Entity, unsigned int iMoveAnim);
static bool AddPedClothes(CClientEntity& Entity, const char* szTexture, const char* szModel, unsigned char ucType);
static bool RemovePedClothes(CClientEntity& Entity, unsigned char ucType);
static bool SetPedControlState(CClientEntity& Entity, const char* szControl, bool bState);
static bool SetPedControlState(CClientPed& const ped, const std::string control, const bool state) noexcept;
static bool SetPedAnalogControlState(CClientEntity& Entity, const char* szControl, float fState);
static bool SetPedDoingGangDriveby(CClientEntity& Entity, bool bGangDriveby);
static bool SetPedFightingStyle(CClientEntity& Entity, unsigned char ucStyle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void CLuaCompatibilityDefs::LoadFunctions()
{"isPlayerDead", CLuaPedDefs::IsPedDead},
{"guiEditSetCaratIndex", CLuaGUIDefs::GUIEditSetCaretIndex},
{"guiMemoSetCaratIndex", CLuaGUIDefs::GUIMemoSetCaretIndex},
{"setControlState", CLuaPedDefs::SetPedControlState},
{"getControlState", CLuaPedDefs::GetPedControlState},
{"setControlState", ArgumentParserWarn<false, CLuaPedDefs::SetPedControlState>},
{"getControlState", ArgumentParserWarn<false, CLuaPedDefs::GetPedControlState>},
{"setCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::SetCameraDrunkLevel>},
{"getCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::GetCameraDrunkLevel>},
};
Expand Down
62 changes: 9 additions & 53 deletions Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void CLuaPedDefs::LoadFunctions()
{"setPedAnimationProgress", SetPedAnimationProgress},
{"setPedAnimationSpeed", SetPedAnimationSpeed},
{"setPedWalkingStyle", SetPedMoveAnim},
{"setPedControlState", SetPedControlState},
{"setPedControlState", ArgumentParserWarn<false, SetPedControlState>},
{"setPedAnalogControlState", SetPedAnalogControlState},
{"setPedDoingGangDriveby", SetPedDoingGangDriveby},
{"setPedFightingStyle", ArgumentParser<SetPedFightingStyle>},
Expand Down Expand Up @@ -75,7 +75,7 @@ void CLuaPedDefs::LoadFunctions()
{"getPedAnimationSpeed", ArgumentParser<GetPedAnimationSpeed>},
{"getPedAnimationLength", ArgumentParser<GetPedAnimationLength>},
{"getPedWalkingStyle", GetPedMoveAnim},
{"getPedControlState", GetPedControlState},
{"getPedControlState", ArgumentParserWarn<false, GetPedControlState>},
{"getPedAnalogControlState", GetPedAnalogControlState},
{"isPedDoingGangDriveby", IsPedDoingGangDriveby},
{"getPedFightingStyle", GetPedFightingStyle},
Expand Down Expand Up @@ -1247,33 +1247,14 @@ int CLuaPedDefs::GetPedClothes(lua_State* luaVM)
return 1;
}

int CLuaPedDefs::GetPedControlState(lua_State* luaVM)
bool CLuaPedDefs::GetPedControlState(CClientPed* const ped, const std::string control) noexcept
{
// Verify the argument
CClientPed* pPed = CStaticFunctionDefinitions::GetLocalPlayer();
SString strControl = "";
CScriptArgReader argStream(luaVM);
bool state;

if (argStream.NextIsUserData())
{
argStream.ReadUserData(pPed);
}
argStream.ReadString(strControl);

if (!argStream.HasErrors())
{
bool bState;
if (CStaticFunctionDefinitions::GetPedControlState(*pPed, strControl, bState))
{
lua_pushboolean(luaVM, bState);
return 1;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
if (!CStaticFunctionDefinitions::GetPedControlState(*ped, control, state))
return false;

lua_pushboolean(luaVM, false);
return 1;
return state;
}

int CLuaPedDefs::GetPedAnalogControlState(lua_State* luaVM)
Expand Down Expand Up @@ -1822,34 +1803,9 @@ int CLuaPedDefs::RemovePedClothes(lua_State* luaVM)
return 1;
}

int CLuaPedDefs::SetPedControlState(lua_State* luaVM)
bool CLuaPedDefs::SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept
{
// Verify the argument
CClientEntity* pEntity = CStaticFunctionDefinitions::GetLocalPlayer();
SString strControl = "";
bool bState = false;
CScriptArgReader argStream(luaVM);

if (argStream.NextIsUserData())
{
argStream.ReadUserData(pEntity);
}
argStream.ReadString(strControl);
argStream.ReadBool(bState);

if (!argStream.HasErrors())
{
if (CStaticFunctionDefinitions::SetPedControlState(*pEntity, strControl, bState))
{
lua_pushboolean(luaVM, true);
return 1;
}
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

lua_pushboolean(luaVM, false);
return 1;
return CStaticFunctionDefinitions::SetPedControlState(*ped, control, state);
}

int CLuaPedDefs::SetPedDoingGangDriveby(lua_State* luaVM)
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CLuaPedDefs : public CLuaDefs
static bool UpdateElementRpHAnim(lua_State* const luaVM, CClientEntity* entity);
LUA_DECLARE_OOP(GetPedBonePosition);
LUA_DECLARE(GetPedClothes);
LUA_DECLARE(GetPedControlState);
static bool GetPedControlState(CClientPed* const ped, const std::string control) noexcept;
LUA_DECLARE(GetPedAnalogControlState);
LUA_DECLARE(IsPedSunbathing);
LUA_DECLARE(IsPedDoingGangDriveby);
Expand Down Expand Up @@ -96,7 +96,7 @@ class CLuaPedDefs : public CLuaDefs
static bool IsPedReloadingWeapon(CClientPed* const ped) noexcept;
LUA_DECLARE(AddPedClothes);
LUA_DECLARE(RemovePedClothes);
LUA_DECLARE(SetPedControlState);
static bool SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept;
LUA_DECLARE(SetPedAnalogControlState);
LUA_DECLARE(SetPedDoingGangDriveby);
static bool SetPedFightingStyle(CClientEntity* const entity, const unsigned int style);
Expand Down

0 comments on commit a38e6ac

Please sign in to comment.