Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unintended behavior for ped control states #3964

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_view control, bool& state) noexcept
{
if (&Ped == GetLocalPlayer())
{
return GetControlState(szControl, bState);
}
if (&ped == GetLocalPlayer())
return GetControlState(control.data(), 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.data(), controller, foot);

float analog = 0;
std::uint32_t index;

if (CClientPad::GetAnalogControlIndex(control.data(), index))
{
if (CClientPad::GetAnalogControlState(szControl, cs, bOnFoot, fState, false))
if (CClientPad::GetAnalogControlState(control.data(), 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.data(), controller, foot);
return true;
}
}

if (Ped.m_Pad.GetControlState(szControl, bState))
{
if (ped.m_Pad.GetControlState(control.data(), 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_view 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.data(), state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although std::string_view is acceptable in CLuaPedDefs, it's not recommended to use it here because SetPedControlState can potentially be called from any place and a string isn't guaranteed to be null-terminated. The same for GetPedControlState.


if (Ped.m_Pad.SetControlState(szControl, bState))
{
return true;
}
}
return false;
if (ped.m_Pad.SetControlState(control.data(), 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_view 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_view 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 @@ -1264,33 +1264,14 @@ int CLuaPedDefs::GetPedClothes(lua_State* luaVM)
return 1;
}

int CLuaPedDefs::GetPedControlState(lua_State* luaVM)
bool CLuaPedDefs::GetPedControlState(CClientPed* const ped, const std::string_view 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 @@ -1839,34 +1820,9 @@ int CLuaPedDefs::RemovePedClothes(lua_State* luaVM)
return 1;
}

int CLuaPedDefs::SetPedControlState(lua_State* luaVM)
bool CLuaPedDefs::SetPedControlState(CClientPed* const ped, const std::string_view 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_view 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_view control, const bool state) noexcept;
LUA_DECLARE(SetPedAnalogControlState);
LUA_DECLARE(SetPedDoingGangDriveby);
static bool SetPedFightingStyle(CClientEntity* const entity, const unsigned int style);
Expand Down
Loading