Skip to content

Commit

Permalink
feat(extra-natives/five): add GET_VEHICLE_HAS_FLAG
Browse files Browse the repository at this point in the history
  • Loading branch information
kams3 committed Jan 19, 2024
1 parent c18c321 commit fdaa332
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions code/components/extra-natives-five/src/VehicleExtraNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ static int VisualHeightSetOffset = 0x07C;
static int LightMultiplierGetOffset;
static int VehiclePitchBiasOffset;
static int VehicleRollBiasOffset;
static int VehicleFlagsOffset;

// TODO: Wheel class.
static int WheelYRotOffset = 0x008;
Expand Down Expand Up @@ -621,6 +622,11 @@ static HookFunction initFunction([]()
VehicleRollBiasOffset = VehiclePitchBiasOffset - 4;
}

{
auto location = hook::get_pattern<char>("48 85 C0 74 3C 8B 80 ? ? ? ? C1 E8 0F");
VehicleFlagsOffset = *(uint32_t*)(location + 7);
}

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

fx::ScriptEngine::RegisterNativeHandler("GET_VEHICLE_HAS_FLAG", [](fx::ScriptContext& context)
{
int flagIndex = context.GetArgument<int>(1);

if (flagIndex < 0 || flagIndex >= (32 * 7)) return;

if (fwEntity* vehicle = getAndCheckVehicle(context, "GET_VEHICLE_HAS_FLAG"))
{
auto addr = *reinterpret_cast<uint64_t *>((unsigned char *)vehicle + 0x020);
auto target_block = *(uint32_t*)(addr + VehicleFlagsOffset + sizeof(uint32_t) * (flagIndex / 32));
context.SetResult<bool>((target_block & (1 << (flagIndex & 31))) != 0);
}
});

fx::ScriptEngine::RegisterNativeHandler("IS_VEHICLE_WANTED", std::bind(readVehicleMemoryBit<&IsWantedOffset, 3>, _1, "IS_VEHICLE_WANTED"));

fx::ScriptEngine::RegisterNativeHandler("IS_VEHICLE_PREVIOUSLY_OWNED_BY_PLAYER", std::bind(readVehicleMemoryBit<&PreviouslyOwnedByPlayerOffset, 1>, _1, "IS_VEHICLE_PREVIOUSLY_OWNED_BY_PLAYER"));
Expand Down
21 changes: 21 additions & 0 deletions ext/native-decls/GetVehicleHasFlag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
ns: CFX
apiset: client
---
## GET_VEHICLE_HAS_FLAG

```c
bool GET_VEHICLE_HAS_FLAG(Vehicle vehicle, int flagIndex);
```
Get vehicle.meta flag by index. Useful examples include FLAG_LAW_ENFORCEMENT (31), FLAG_RICH_CAR (36), FLAG_IS_ELECTRIC (43), FLAG_IS_OFFROAD_VEHICLE (48).
Complete list of flags: https://gtamods.com/wiki/Vehicles.meta#flags
Compilation of all vehicles' metadata files (including their flags): https://forum.cfx.re/t/vehicle-meta-files-last-dlc/5142301
## Parameters
* **vehicle**: The vehicle to obtain data for.
* **flagIndex**: Flag index (0-203)
## Return value
A boolean for whether the flag is set.

0 comments on commit fdaa332

Please sign in to comment.