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

Implement BREAK_OFF_VEHICLE_WHEEL for FiveM #1909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions code/components/extra-natives-five/src/VehicleExtraNatives.cpp
Expand Up @@ -36,6 +36,11 @@

using namespace winrt::Windows::Gaming::Input;

static hook::cdecl_stub<void(void*, int, float, float, float, bool, bool)> breakOffVehicleWheel([]
{
return hook::get_call(hook::get_pattern("F3 44 0F 11 4C 24 ? E8 ? ? ? ? EB 7A", 7));
});

struct PatternPair
{
std::string_view pattern;
Expand Down Expand Up @@ -277,6 +282,8 @@ static int WheelFlagsOffset;
static char* VehicleTopSpeedModifierPtr;
static int VehicleCheatPowerIncreaseOffset;

static int VehicleDamageStructOffset;

static bool* g_trainsForceDoorsOpen;
static int TrainDoorCountOffset;
static int TrainDoorArrayPointerOffset;
Expand Down Expand Up @@ -617,6 +624,11 @@ static HookFunction initFunction([]()
hook::put<int32_t>(location, (intptr_t)g_flyThroughWindscreenDisabled - (intptr_t)location - 4);
}

{
auto location = hook::get_pattern<char>("F3 44 0F 11 4C 24 ? E8 ? ? ? ? EB 7A");
VehicleDamageStructOffset = *(uint32_t*)(location - 11);
}

{
std::initializer_list<PatternPair> list = {
{ "44 38 ? ? ? ? 02 74 ? F3 0F 10 1D", 13 },
Expand Down Expand Up @@ -1541,6 +1553,30 @@ static HookFunction initFunction([]()
}
});

fx::ScriptEngine::RegisterNativeHandler("BREAK_OFF_VEHICLE_WHEEL", [](fx::ScriptContext& context)
{
if (fwEntity* vehicle = getAndCheckVehicle(context, "BREAK_OFF_VEHICLE_WHEEL"))
{
auto damageStruct = (void*)((char*)vehicle + VehicleDamageStructOffset);

auto wheelIndex = context.GetArgument<uint32_t>(1);
auto numWheels = readValue<unsigned char>(vehicle, NumWheelsOffset);

if (wheelIndex >= numWheels)
{
return;
}

auto leaveDebrisTrail = context.GetArgument<bool>(2);
auto deleteWheel = context.GetArgument<bool>(3);
auto unknownFlag = context.GetArgument<bool>(4); // setting some flag inside CVehicleDrawHandler
auto putOnFire = context.GetArgument<bool>(5);

// last argument is a network flag
breakOffVehicleWheel(damageStruct, wheelIndex, leaveDebrisTrail ? 1.0f : 0.0f, deleteWheel ? 1.0f : 0.0f, unknownFlag ? 1.0f : 0.0f, putOnFire, true);
}
});

MH_Initialize();
MH_CreateHook(hook::get_pattern("E8 ? ? ? ? 8A 83 DA 00 00 00 24 0F 3C 02", -0x32), DeleteVehicleWrap, (void**)&g_origDeleteVehicle);
MH_CreateHook(hook::get_pattern("80 7A 4B 00 45 8A F9", -0x1D), DeleteNetworkCloneWrap, (void**)&g_origDeleteNetworkClone);
Expand Down
31 changes: 31 additions & 0 deletions ext/native-decls/BreakOffVehicleWheel.md
@@ -0,0 +1,31 @@
---
ns: CFX
apiset: client
---
## BREAK_OFF_VEHICLE_WHEEL

```c
void BREAK_OFF_VEHICLE_WHEEL(Vehicle vehicle, int wheelIndex, BOOL leaveDebrisTrail, BOOL deleteWheel, BOOL unknownFlag, BOOL putOnFire);
```

Break off vehicle wheel by index. The `leaveDebrisTrail` flag requires `putOnFire` to be true.

## Examples
blattersturm marked this conversation as resolved.
Show resolved Hide resolved

```lua
local vehicle = GetVehiclePedIsIn(PlayerPedId())

if DoesEntityExist(vehicle) then
for i = 0, 3 do
BreakOffVehicleWheel(vehicle, i, true, false, true, false)
end
end
```

## Parameters
* **vehicle**: The vehicle handle.
* **wheelIndex**: The wheel index.
* **leaveDebrisTrail**: Start "veh_debris_trail" ptfx.
* **deleteWheel**: True to delete wheel, otherwise detach.
* **unknownFlag**: Unknown flag.
* **putOnFire**: Set wheel on fire once detached.