Skip to content

Commit

Permalink
feat(extra-natives/five): GET_VEHICLE_GEAR_RATIO and SET_VEHICLE_GEAR…
Browse files Browse the repository at this point in the history
…_RATIO
  • Loading branch information
RickyB505 committed Mar 5, 2024
1 parent 3211a4d commit a511410
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
30 changes: 30 additions & 0 deletions code/components/extra-natives-five/src/VehicleExtraNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ static int StreamRenderWheelSizeOffset;
static int DrawnWheelAngleMultOffset;
static int TurboBoostOffset; // = 0x8D8;
static int ClutchOffset; // = 0x8C0;
static int VehicleGearRatioOffset;
//static int VisualHeightGetOffset = 0x080; // There is a vanilla native for this.
static int VisualHeightSetOffset = 0x07C;
static int LightMultiplierGetOffset;
Expand Down Expand Up @@ -634,7 +635,18 @@ static HookFunction initFunction([]()
VehiclePitchBiasOffset = *hook::get_pattern<uint32_t>("0F 2F F7 44 0F 28 C0 F3 44 0F 58 83", 12);
VehicleRollBiasOffset = VehiclePitchBiasOffset - 4;
}
if (xbr::IsGameBuildOrGreater<3095>())
{
auto location = hook::get_pattern<char>("48 8D 8F ? ? ? ? 4C 8B C3 F3 0F 11 7C 24");

VehicleGearRatioOffset = *(int*)(location + 3) + 8 + 1 * sizeof(float);
}
else
{
auto location = hook::get_pattern<char>("48 8D 8F ? ? ? ? 4C 8B C3 F3 0F 11 7C 24");

VehicleGearRatioOffset = *(int*)(location + 3) + 8;
}
{
std::initializer_list<PatternPair> list = {
{ "44 38 ? ? ? ? 02 74 ? F3 0F 10 1D", 13 },
Expand Down Expand Up @@ -866,6 +878,24 @@ static HookFunction initFunction([]()
};
};

fx::ScriptEngine::RegisterNativeHandler("GET_VEHICLE_GEAR_RATIO", [](fx::ScriptContext& context)
{
unsigned char gear = context.GetArgument<int>(1);
if (fwEntity* vehicle = getAndCheckVehicle(context, "GET_VEHICLE_GEAR_RATIO"))
{
context.SetResult<float>(*(float*)((char*)vehicle + VehicleGearRatioOffset + gear * sizeof(float)));
}
});

fx::ScriptEngine::RegisterNativeHandler("SET_VEHICLE_GEAR_RATIO", [](fx::ScriptContext& context)
{
unsigned char gear = context.GetArgument<int>(1);
if (fwEntity* vehicle = getAndCheckVehicle(context, "SET_VEHICLE_GEAR_RATIO"))
{
*(float*)((char*)vehicle + VehicleGearRatioOffset + gear * sizeof(float)) = context.GetArgument<float>(2);
}
});

fx::ScriptEngine::RegisterNativeHandler("GET_VEHICLE_WHEEL_BRAKE_PRESSURE", makeWheelFunction([](fx::ScriptContext& context, fwEntity* vehicle, uintptr_t wheelAddr)
{
context.SetResult<float>(*reinterpret_cast<float*>(wheelAddr + WheelBrakePressureOffset));
Expand Down
38 changes: 38 additions & 0 deletions ext/native-decls/GetVehicleGearRatio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
ns: CFX
apiset: client
---
## GET_VEHICLE_GEAR_RATIO

```c
float GET_VEHICLE_GEAR_RATIO(Vehicle vehicle, int gear);
```
Gets vehicles gear ratio on choosen gear.
## Parameters
* **vehicle**: The vehicle handle.
* **gear**: The vehicles gear you want to get.
## Examples
```lua
local Vehicle = GetVehiclePedIsIn(PlayerPedId(-1))
local currentGear = GetVehicleCurrentGear(Vehicle)
print(GetVehicleGearRatio(Vehicle, currentGear)) -- will print current vehicle gear to console
```

```js
const Vehicle = GetVehiclePedIsIn(PlayerPedId(-1));
const currentGear = GetVehicleCurrentGear(Vehicle);

console.log(GetVehicleGearRatio(Vehicle, currentGear)); // will print current vehicle gear to console
```

```cs
using static CitizenFX.Core.API;

Vehicle veh = Game.PlayerPed.CurrentVehicle;
int currentGear = GetVehicleCurrentGear(veh.Handle);

Debug.WriteLine($"{GetVehicleGearRatio(veh.Handle, currentGear)}");
```
63 changes: 63 additions & 0 deletions ext/native-decls/SetVehicleGearRatio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
ns: CFX
apiset: client
---
## SET_VEHICLE_GEAR_RATIO

```c
void SET_VEHICLE_GEAR_RATIO(Vehicle vehicle, int gear, float ratio);
```
Sets the vehicles gear ratio on choosen gear, reverse gear needs to be a negative float and forward moving gear needs to be a positive float. Refer to the examples if confused.
## Parameters
* **vehicle**: The vehicle handle.
* **gear**: The vehicles gear you want to change.
* **ratio**: The gear ratio you want to use.
## Examples
```lua
local function Set8SpeedVehicleGears(Vehicle)
SetVehicleGearRatio(Vehicle, 0, -3.32) -- reverse gear at -3.21:1
SetVehicleGearRatio(Vehicle, 1, 4.71) -- 1st gear at 4.71:1
SetVehicleGearRatio(Vehicle, 2, 3.14) -- 2nd gear at 3.14:1
SetVehicleGearRatio(Vehicle, 3, 2.11) -- 3rd gear at 2.11:1
SetVehicleGearRatio(Vehicle, 4, 1.67) -- 4th gear at 1.67:1
SetVehicleGearRatio(Vehicle, 5, 1.29) -- 5th gear at 1.29:1
SetVehicleGearRatio(Vehicle, 6, 1.0) -- 6th gear at 1.0:1
SetVehicleGearRatio(Vehicle, 7, 0.84) -- 7th gear at 0.84:1
SetVehicleGearRatio(Vehicle, 8, 0.67) -- 8th gear at 0.67:1
return
end
```

```js
function Set8SpeedVehicleGears(Vehicle) {
SetVehicleGearRatio(Vehicle, 0, -3.32); // reverse gear at -3.21:1
SetVehicleGearRatio(Vehicle, 1, 4.71); // 1st gear at 4.71:1
SetVehicleGearRatio(Vehicle, 2, 3.14); // 2nd gear at 3.14:1
SetVehicleGearRatio(Vehicle, 3, 2.11); // 3rd gear at 2.11:1
SetVehicleGearRatio(Vehicle, 4, 1.67); // 4th gear at 1.67:1
SetVehicleGearRatio(Vehicle, 5, 1.29); // 5th gear at 1.29:1
SetVehicleGearRatio(Vehicle, 6, 1.0); // 6th gear at 1.0:1
SetVehicleGearRatio(Vehicle, 7, 0.84); // 7th gear at 0.84:1
SetVehicleGearRatio(Vehicle, 8, 0.67); // 8th gear at 0.67:1
return;
}
```

```cs
using static CitizenFX.Core.API;

public static void Set8SpeedVehicleGears(int Vehicle)
{
SetVehicleGearRatio(Vehicle, 0, -3.32); // reverse gear at -3.21:1
SetVehicleGearRatio(Vehicle, 1, 4.71); // 1st gear at 4.71:1
SetVehicleGearRatio(Vehicle, 2, 3.14); // 2nd gear at 3.14:1
SetVehicleGearRatio(Vehicle, 3, 2.11); // 3rd gear at 2.11:1
SetVehicleGearRatio(Vehicle, 4, 1.67); // 4th gear at 1.67:1
SetVehicleGearRatio(Vehicle, 5, 1.29); // 5th gear at 1.29:1
SetVehicleGearRatio(Vehicle, 6, 1.0); // 6th gear at 1.0:1
SetVehicleGearRatio(Vehicle, 7, 0.84); // 7th gear at 0.84:1
SetVehicleGearRatio(Vehicle, 8, 0.67); // 8th gear at 0.67:1
return;
}
```

0 comments on commit a511410

Please sign in to comment.