Skip to content

Fix bone related functions #4203

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
181 changes: 109 additions & 72 deletions Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ void CLuaPedDefs::LoadFunctions()
{"givePedWeapon", GivePedWeapon},

{"setPedVoice", SetPedVoice},
{"setElementBonePosition", ArgumentParser<SetElementBonePosition>},
{"setElementBoneRotation", ArgumentParser<SetElementBoneRotation>},
{"setElementBoneQuaternion", ArgumentParser<SetElementBoneQuaternion>},
{"setElementBoneMatrix", ArgumentParser<SetElementBoneMatrix>},
{"setElementBonePosition", ArgumentParserWarn<false, SetElementBonePosition>},
{"setElementBoneRotation", ArgumentParserWarn<false, SetElementBoneRotation>},
{"setElementBoneQuaternion", ArgumentParserWarn<false, SetElementBoneQuaternion>},
{"setElementBoneMatrix", ArgumentParserWarn<false, SetElementBoneMatrix>},
{"setPedRotation", SetPedRotation},
{"setPedWeaponSlot", SetPedWeaponSlot},
{"setPedCanBeKnockedOffBike", SetPedCanBeKnockedOffBike},
Expand All @@ -65,10 +65,10 @@ void CLuaPedDefs::LoadFunctions()
{"playPedVoiceLine", ArgumentParser<PlayPedVoiceLine>},

{"getPedVoice", GetPedVoice},
{"getElementBonePosition", ArgumentParser<GetElementBonePosition>},
{"getElementBoneRotation", ArgumentParser<GetElementBoneRotation>},
{"getElementBoneQuaternion", ArgumentParser<GetElementBoneQuaternion>},
{"getElementBoneMatrix", ArgumentParser<GetElementBoneMatrix>},
{"getElementBonePosition", ArgumentParserWarn<false, GetElementBonePosition>},
{"getElementBoneRotation", ArgumentParserWarn<false, GetElementBoneRotation>},
{"getElementBoneQuaternion", ArgumentParserWarn<false, GetElementBoneQuaternion>},
{"getElementBoneMatrix", ArgumentParserWarn<false, GetElementBoneMatrix>},
{"getPedRotation", GetPedRotation},
{"getPedWeaponSlot", GetPedWeaponSlot},
{"canPedBeKnockedOffBike", CanPedBeKnockedOffBike},
Expand Down Expand Up @@ -995,110 +995,147 @@ int CLuaPedDefs::CanPedBeKnockedOffBike(lua_State* luaVM)
return 1;
}

bool CLuaPedDefs::SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CVector position)
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBonePosition(CClientPed* ped, const std::uint16_t bone)
{
CEntity* theEntity = entity->GetGameEntity();
if (!theEntity)
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();
CVector position;

if (!entity || !entity->GetBonePosition(static_cast<eBone>(bone), position))
return false;
return theEntity->SetBonePosition(static_cast<eBone>(boneId), position);

return CLuaMultiReturn<float, float, float>(position.fX, position.fY, position.fZ);
}

bool CLuaPedDefs::SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float yaw, float pitch, float roll)
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBoneRotation(CClientPed* ped, const std::uint16_t bone)
{
if (boneId > BONE_RIGHTFOOT)
throw LuaFunctionError("Invalid bone ID", false);
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* theEntity = entity->GetGameEntity();
if (!theEntity)
CEntity* entity = ped->GetGameEntity();
float yaw = 0.0f;
float pitch = 0.0f;
float roll = 0.0f;

if (!entity || !entity->GetBoneRotation(static_cast<eBone>(bone), yaw, pitch, roll))
return false;
return theEntity->SetBoneRotation(static_cast<eBone>(boneId), yaw, pitch, roll);

return CLuaMultiReturn<float, float, float>(yaw, pitch, roll);
}

bool CLuaPedDefs::SetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float x, float y, float z, float w)
std::variant<bool, CLuaMultiReturn<float, float, float, float>> CLuaPedDefs::GetElementBoneQuaternion(CClientPed* ped, const std::uint16_t bone)
{
if (boneId > BONE_RIGHTFOOT)
throw LuaFunctionError("Invalid bone ID", false);
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float w = 0.0f;

CEntity* theEntity = entity->GetGameEntity();
return theEntity ? theEntity->SetBoneRotationQuat(static_cast<eBone>(boneId), x, y, z, w) : false;
if (!entity || !entity->GetBoneRotationQuat(static_cast<eBone>(bone), x, y, z, w))
return false;

return CLuaMultiReturn<float, float, float, float>(x, y, z, w);
}

std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
std::variant<bool, std::array<std::array<float, 4>, 4>> CLuaPedDefs::GetElementBoneMatrix(CClientPed* ped, const std::uint16_t bone)
{
CEntity* theEntity = entity->GetGameEntity();
CVector position;
if (!theEntity || !theEntity->GetBonePosition(static_cast<eBone>(boneId), position))
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();

if (!entity)
return false;

return std::make_tuple(position.fX, position.fY, position.fZ);
RwMatrix* rwmatrix = entity->GetBoneRwMatrix(static_cast<eBone>(bone));

if (!rwmatrix)
return false;

CMatrix matrix;

g_pGame->GetRenderWare()->RwMatrixToCMatrix(*rwmatrix, matrix);

return matrix.To4x4Array();
}

std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
bool CLuaPedDefs::SetElementBonePosition(CClientPed* ped, const std::uint16_t bone, const CVector position)
{
if (boneId > BONE_RIGHTFOOT)
throw LuaFunctionError("Invalid bone ID", false);
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();

float yaw = 0.0f, pitch = 0.0f, roll = 0.0f;
CEntity* theEntity = entity->GetGameEntity();
if (!theEntity || !theEntity->GetBoneRotation(static_cast<eBone>(boneId), yaw, pitch, roll))
if (!entity)
return false;

return std::make_tuple(yaw, pitch, roll);
return entity->SetBonePosition(static_cast<eBone>(bone), position);
}

std::variant<bool, CLuaMultiReturn<float, float, float, float>> CLuaPedDefs::GetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
bool CLuaPedDefs::SetElementBoneRotation(CClientPed* ped, const std::uint16_t bone, const float yaw, const float pitch, const float roll)
{
if (boneId > BONE_RIGHTFOOT)
throw LuaFunctionError("Invalid bone ID", false);
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

float x = 0.0f, y = 0.0f, z = 0.0f, w = 0.0f;
CEntity* theEntity = entity->GetGameEntity();
if (!theEntity || !theEntity->GetBoneRotationQuat(static_cast<eBone>(boneId), x, y, z, w))
CEntity* entity = ped->GetGameEntity();

if (!entity)
return false;

return std::make_tuple(x, y, z, w);
return entity->SetBoneRotation(static_cast<eBone>(bone), yaw, pitch, roll);
}

bool CLuaPedDefs::SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CMatrix boneMatrix)
bool CLuaPedDefs::SetElementBoneQuaternion(CClientPed* ped, const std::uint16_t bone, const float x, const float y, const float z, const float w)
{
CEntity* theEntity = entity->GetGameEntity();
return theEntity ? theEntity->SetBoneMatrix(static_cast<eBone>(boneId), boneMatrix) : false;
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();

if (!entity)
return false;

return entity->SetBoneRotationQuat(static_cast<eBone>(bone), x, y, z, w);
}

std::variant<bool, std::array<std::array<float, 4>, 4>> CLuaPedDefs::GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
bool CLuaPedDefs::SetElementBoneMatrix(CClientPed* ped, const std::uint16_t bone, const CMatrix matrix)
{
CEntity* theEntity = entity->GetGameEntity();
if (theEntity)
{
RwMatrix* boneRwMatrix = theEntity->GetBoneRwMatrix(static_cast<eBone>(boneId));
if (boneRwMatrix)
{
CMatrix matrix;
g_pGame->GetRenderWare()->RwMatrixToCMatrix(*boneRwMatrix, matrix);
return matrix.To4x4Array();
}
}
return false;
if (bone < BONE_ROOT || bone > BONE_LEFTBREAST)
throw std::invalid_argument("Invalid bone: " + std::to_string(bone));

CEntity* entity = ped->GetGameEntity();

if (!entity)
return false;

return entity->SetBoneMatrix(static_cast<eBone>(bone), matrix);
}

bool CLuaPedDefs::UpdateElementRpHAnim(lua_State* const luaVM, CClientEntity* entity)
bool CLuaPedDefs::UpdateElementRpHAnim(CClientPed* ped)
{
CEntity* theEntity = entity->GetGameEntity();
if (theEntity)
{
theEntity->UpdateRpHAnim();
CEntity* entity = ped->GetGameEntity();

if (theEntity->GetModelIndex() == 0) // CJ skin
{
RpClump* clump = theEntity->GetRpClump();
if (clump)
{
((void(__cdecl*)(RpClump*))0x5DF560)(clump); // CPed::ShoulderBoneRotation
}
}
if (!entity)
return false;

entity->UpdateRpHAnim();

if (entity->GetModelIndex() != 0)
return true;

RpClump* clump = entity->GetRpClump();

if (clump)
{
((void(__cdecl*)(RpClump*))0x5DF560)(clump); // CPed::ShoulderBoneRotation
}
return false;

return true;
}

int CLuaPedDefs::GetPedBonePosition(lua_State* luaVM)
Expand Down
18 changes: 9 additions & 9 deletions Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ class CLuaPedDefs : public CLuaDefs
LUA_DECLARE(GetPedContactElement);
LUA_DECLARE(GetPedRotation);
LUA_DECLARE(CanPedBeKnockedOffBike);
static bool SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CVector position);
static bool SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float yaw, float pitch, float roll);
static bool SetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float x, float y, float z, float w);
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);

static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
static std::variant<bool, CLuaMultiReturn<float, float, float, float>> GetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBonePosition(CClientPed* ped, const std::uint16_t bone);
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBoneRotation(CClientPed* ped, const std::uint16_t bone);
static std::variant<bool, CLuaMultiReturn<float, float, float, float>> GetElementBoneQuaternion(CClientPed* ped, const std::uint16_t bone);
static std::variant<bool, std::array<std::array<float, 4>, 4>> GetElementBoneMatrix(CClientPed* ped, const std::uint16_t bone);

static bool SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CMatrix boneMatrix);
static bool SetElementBonePosition(CClientPed* ped, const std::uint16_t bone, const CVector position);
static bool SetElementBoneRotation(CClientPed* ped, const std::uint16_t bone, const float yaw, const float pitch, const float roll);
static bool SetElementBoneQuaternion(CClientPed* ped, const std::uint16_t bone, const float x, const float y, const float z, const float w);
static bool SetElementBoneMatrix(CClientPed* ped, const std::uint16_t bone, const CMatrix matrix);

static std::variant<bool, std::array<std::array<float, 4>, 4>> GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
static bool UpdateElementRpHAnim(CClientPed* ped);

static bool UpdateElementRpHAnim(lua_State* const luaVM, CClientEntity* entity);
LUA_DECLARE_OOP(GetPedBonePosition);
LUA_DECLARE(GetPedClothes);
static bool GetPedControlState(std::variant<CClientPed*, std::string> first, std::optional<std::string> maybeControl);
Expand Down
Loading