Skip to content

Commit

Permalink
hijack motion sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
cipherxof committed Nov 18, 2023
1 parent 3e1c7d0 commit fc0ca54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
29 changes: 22 additions & 7 deletions MGS3CrouchWalk/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bool CrouchWalkEnabled = false;
bool CrouchMoving = false;
bool CrouchMovingSlow = false;
bool IgnoreButtonHold = false;
bool HijackSequence = false;

// config values
float CamoIndexModifier = 1.0f;
Expand All @@ -31,6 +32,7 @@ PlayerStatusCheckDelegate* PlayerStatusCheck;
PlayerStatusSetDelegate* PlayerStatusSet;
ActMovementDelegate* ActMovement;
GetButtonHoldingStateDelegate* GetButtonHoldingState;
MotionPlaySequenceDelegate* MotionPlaySequence;

uintptr_t PlayerSetMotion(int64_t work, PlayerMotion motion)
{
Expand Down Expand Up @@ -64,9 +66,12 @@ void __fastcall SetMotionDataHook(MotionControl* motionControl, int layer, Playe

time = (int)*currentTime;
motion = PlayerMotion::SquatMove;

HijackSequence = true;
}

SetMotionData(motionControl, layer, motion, time, mask);
HijackSequence = false;
}

int64_t __fastcall GetButtonHoldingStateHook(int64_t work, MovementWork* plWork)
Expand All @@ -77,6 +82,14 @@ int64_t __fastcall GetButtonHoldingStateHook(int64_t work, MovementWork* plWork)
return GetButtonHoldingState(work, plWork);
}

int64_t __fastcall MotionPlaySequenceHook(__int64 mtsq_ctrl, int layer, int num, int flag, int loop_time)
{
if (HijackSequence)
num = CrouchMovingSlow ? PlayerMotion::StandMoveSlow : PlayerMotion::StandMoveStalk;

return MotionPlaySequence(mtsq_ctrl, layer, num, flag, loop_time);
}

int* __fastcall CalculateCamoIndexHook(int* a1, int a2)
{
int* result = CalculateCamoIndex(a1, a2);
Expand Down Expand Up @@ -123,6 +136,8 @@ int* __fastcall ActionSquatStillHook(int64_t work, MovementWork* plWork, int64_t

// check that the pad is being held down
int16_t padForce = *(int16_t*)(work + 2016);
bool wasMoving = CrouchMoving;
bool wasSlow = CrouchMovingSlow;
CrouchMoving = padForce > plWork->padForceLimit;
CrouchMovingSlow = padForce < 180;

Expand All @@ -134,14 +149,12 @@ int* __fastcall ActionSquatStillHook(int64_t work, MovementWork* plWork, int64_t
{
mCtrlGlobal->mtcmControl->motionTimeBase = CrouchMovingSlow ? CrouchStalkSpeed : CrouchWalkSpeed;

auto mtsq_cntrl = *((uintptr_t*)mCtrlGlobal + 15);
auto sound = (uint8_t*)mtsq_cntrl + 0x3C;
auto soundType = (uint8_t*)mtsq_cntrl + 0x48;
auto motionId = (uint8_t*)mtsq_cntrl + 0x40;
auto mtsqCntrl = *((uintptr_t*)mCtrlGlobal + 15);

*sound = 5;
*soundType = CrouchMovingSlow ? 0x40 : 0x60;
*motionId = CrouchMovingSlow ? PlayerMotion::StandMoveStalk : PlayerMotion::StandMoveSlow;
if (wasMoving && wasSlow != CrouchMovingSlow)
{
MotionPlaySequence(mtsqCntrl, 0, CrouchMovingSlow ? PlayerMotion::StandMoveSlow : PlayerMotion::StandMoveStalk, 5, 0x1b0);
}
}

PlayerStatusSet(11, 14, 0x10C, 0xFFFFFFFF); // enables grass movement sounds
Expand All @@ -163,6 +176,7 @@ void InstallHooks()
uintptr_t setMotionDataOffset = (uintptr_t)Memory::PatternScan(GameModule, "48 85 C9 0F 84 42 03 00 00 4C 8B DC 55 53 56 41");
uintptr_t calcuateCamoIndexOffset = (uintptr_t)Memory::PatternScan(GameModule, "48 83 EC 30 0F 29 74 24 20 48 8B F9 48 63 F2 E8") - 0x10;
uintptr_t getBtnHoldStateOffset = (uintptr_t)Memory::PatternScan(GameModule, "44 0F B7 8A 8E 00 00 00 4C 8B C2 66 45 85 C9 78");
uintptr_t motionPlaySeqOffset = (uintptr_t)Memory::PatternScan(GameModule, "4D 63 D8 48 85 C9 74 6B 48 63 C2 48 8D 14 40 48");
uint8_t* disableCrouchProneOffset = Memory::PatternScan(GameModule, "00 00 7E 19 83 4F 68 10");

ActSquatStillOffset = (uintptr_t)Memory::PatternScan(GameModule, "4C 8B DC 55 57 41 56 49 8D 6B A1 48 81 EC 00 01");
Expand All @@ -175,6 +189,7 @@ void InstallHooks()
Memory::DetourFunction(setMotionDataOffset, (LPVOID)SetMotionDataHook, (LPVOID*)&SetMotionData);
Memory::DetourFunction(calcuateCamoIndexOffset, (LPVOID)CalculateCamoIndexHook, (LPVOID*)&CalculateCamoIndex);
Memory::DetourFunction(getBtnHoldStateOffset, (LPVOID)GetButtonHoldingStateHook, (LPVOID*)&GetButtonHoldingState);
Memory::DetourFunction(motionPlaySeqOffset, (LPVOID)MotionPlaySequenceHook, (LPVOID*)&MotionPlaySequence);
Memory::DetourFunction(ActSquatStillOffset, (LPVOID)ActionSquatStillHook, (LPVOID*)&ActionSquatStill);

CamoIndexData = InitializeCamoIndex(0, 0);
Expand Down
3 changes: 2 additions & 1 deletion MGS3CrouchWalk/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ typedef void __fastcall SetMotionDataDelegate(MotionControl* motionControl, int
typedef int64_t __fastcall PlayerStatusCheckDelegate(unsigned int a1);
typedef int64_t __fastcall ActMovementDelegate(MovementWork* plWork, int64_t work, int flag);
typedef int64_t __fastcall GetButtonHoldingStateDelegate(int64_t work, MovementWork* plWork);
typedef int64_t __fastcall PlayerStatusSetDelegate(int a1, int64_t a2, int64_t a3, int64_t a4);
typedef int64_t __fastcall PlayerStatusSetDelegate(int a1, int64_t a2, int64_t a3, int64_t a4);
typedef int64_t __fastcall MotionPlaySequenceDelegate(__int64 mtsqCtrl, int layer, int num, int flag, int loop_time);

0 comments on commit fc0ca54

Please sign in to comment.