Skip to content

Make getPedControlState and setPedControlState functions backward compatible #4008

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

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 13 additions & 11 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,38 +1710,38 @@ bool CStaticFunctionDefinitions::GetPedClothes(CClientPed& Ped, unsigned char uc
return false;
}

bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& const ped, const std::string control, bool& state) noexcept
bool CStaticFunctionDefinitions::GetPedControlState(CClientPed& ped, const std::string_view& control, bool& state) noexcept
Copy link
Member

Choose a reason for hiding this comment

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

Don't use references for small types. string_view and bool are small types

{
if (&ped == GetLocalPlayer())
return GetControlState(control.c_str(), state);
return GetControlState(control.data(), state);

if (ped.GetType() == CCLIENTPLAYER)
{
CControllerState controller;
ped.GetControllerState(controller);

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

float analog = 0;
std::uint32_t index;

if (CClientPad::GetAnalogControlIndex(control.c_str(), index))
if (CClientPad::GetAnalogControlIndex(control.data(), index))
{
if (CClientPad::GetAnalogControlState(control.c_str(), controller, foot, analog, false))
if (CClientPad::GetAnalogControlState(control.data(), controller, foot, analog, false))
{
state = analog > 0;
return true;
}
}
else
{
state = CClientPad::GetControlState(control.c_str(), controller, foot);
state = CClientPad::GetControlState(control.data(), controller, foot);
return true;
}
}

if (ped.m_Pad.GetControlState(control.c_str(), state))
if (ped.m_Pad.GetControlState(control.data(), state))
return true;

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

bool CStaticFunctionDefinitions::SetPedControlState(CClientPed& const ped, const std::string control, const bool state) noexcept
bool CStaticFunctionDefinitions::SetPedControlState(CClientPed& ped, const std::string_view& control, const bool state) noexcept
{
if (&ped == GetLocalPlayer())
return SetControlState(control.c_str(), state);
return SetControlState(control.data(), state);

if (ped.m_Pad.SetControlState(control.c_str(), state))
if (ped.m_Pad.SetControlState(control.data(), state))
return true;

return false;
}

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& const ped, const std::string control, bool& state) noexcept;
static bool GetPedControlState(CClientPed& 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(CClientPed& const ped, const std::string control, const bool state) noexcept;
static bool SetPedControlState(CClientPed& 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
66 changes: 62 additions & 4 deletions Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,11 +1247,31 @@ int CLuaPedDefs::GetPedClothes(lua_State* luaVM)
return 1;
}

bool CLuaPedDefs::GetPedControlState(CClientPed* const ped, const std::string control) noexcept
bool CLuaPedDefs::GetPedControlState(const std::variant<CClientPed*, std::string_view> first, const std::optional<std::string_view> second) noexcept
{
bool state;
bool state;
CClientPed* ped;
std::string_view control;

if (!CStaticFunctionDefinitions::GetPedControlState(*ped, control, state))
if (std::holds_alternative<CClientPed*>(first))
{
ped = std::get<CClientPed*>(first);

if (!second)
return false;

control = *second;
}
else if (std::holds_alternative<std::string_view>(first))
{
ped = CStaticFunctionDefinitions::GetLocalPlayer();
control = std::get<std::string_view>(first);
}

if (!ped)
return false;

if (!CStaticFunctionDefinitions::GetPedControlState(*ped, std::string(control), state))
return false;

return state;
Expand Down Expand Up @@ -1803,8 +1823,46 @@ int CLuaPedDefs::RemovePedClothes(lua_State* luaVM)
return 1;
}

bool CLuaPedDefs::SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept
bool CLuaPedDefs::SetPedControlState(const std::variant<CClientPed*, std::string_view> first, const std::variant<std::string_view, bool> second, const std::optional<bool> third) noexcept
{
CClientPed* ped;
std::string_view control;
bool state;

if (std::holds_alternative<CClientPed*>(first))
{
ped = std::get<CClientPed*>(first);

if (std::holds_alternative<std::string_view>(second))
{
control = std::get<std::string_view>(second);

if (third)
state = *third;
else
return false;
}
else if (std::holds_alternative<bool>(second))
state = std::get<bool>(second);
else
return false;
}
else if (std::holds_alternative<std::string_view>(first))
{
ped = CStaticFunctionDefinitions::GetLocalPlayer();
control = std::get<std::string_view>(first);

if (std::holds_alternative<bool>(second))
state = std::get<bool>(second);
else if (third)
state = *third;
else
return false;
}

if (!ped)
return false;

Comment on lines +1826 to +1865
Copy link
Member

Choose a reason for hiding this comment

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

It's a mess. And has no error messages for bad usage. I can write setPedControlState("string1", "fire", false) and get silent false. Maybe it's better to keep the old parser here. What do you think?

return CStaticFunctionDefinitions::SetPedControlState(*ped, control, state);
}

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);
static bool GetPedControlState(CClientPed* const ped, const std::string control) noexcept;
static bool GetPedControlState(const std::variant<CClientPed*, std::string_view> first, const std::optional<std::string_view> second) 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);
static bool SetPedControlState(CClientPed* const ped, const std::string control, const bool state) noexcept;
static bool SetPedControlState(const std::variant<CClientPed*, std::string_view> first, const std::variant<std::string_view, bool> second, const std::optional<bool> third) noexcept;
LUA_DECLARE(SetPedAnalogControlState);
LUA_DECLARE(SetPedDoingGangDriveby);
static bool SetPedFightingStyle(CClientEntity* const entity, const unsigned int style);
Expand Down
Loading