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

feat(natives/vehicle): implement getter/setter for vehicle flags #2780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions code/components/extra-natives-five/src/VehicleExtraNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static int VisualHeightSetOffset = 0x07C;
static int LightMultiplierGetOffset;
static int VehiclePitchBiasOffset;
static int VehicleRollBiasOffset;
static int VehicleFlagsOffset;

static int VehicleDamageParentOffset;
static int VehicleHandlingOffset;
Expand Down Expand Up @@ -755,6 +756,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);
}

{
auto location = hook::get_pattern<char>("F3 0F 59 3D ? ? ? ? F3 0F 58 3D ? ? ? ? 48 85 C9", 4);
PassengerMassPtr = hook::get_address<float*>(location);
Expand Down Expand Up @@ -1380,6 +1386,43 @@ static HookFunction initFunction([]()
}
});

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

if (flagIndex < 0 || flagIndex >= 256)
{
return;
}

if (fwEntity* vehicle = getAndCheckVehicle(context, "GET_VEHICLE_HAS_FLAG"))
{
auto addr = readValue<uint64_t>(vehicle, ModelInfoPtrOffset);
auto flagBlocks = reinterpret_cast<std::bitset<256>*>(addr + VehicleFlagsOffset);

context.SetResult<bool>(flagBlocks->test(flagIndex));
}
});

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

if (flagIndex < 0 || flagIndex >= 256)
{
return;
}

if (fwEntity* vehicle = getAndCheckVehicle(context, "SET_VEHICLE_FLAG"))
{
auto addr = readValue<uint64_t>(vehicle, ModelInfoPtrOffset);
auto flagBlocks = reinterpret_cast<std::bitset<256>*>(addr + VehicleFlagsOffset);

flagBlocks->set(flagIndex, value);
}
});

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
67 changes: 67 additions & 0 deletions ext/native-decls/GetVehicleHasFlag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
ns: CFX
apiset: client
---
## GET_VEHICLE_HAS_FLAG

```c
bool GET_VEHICLE_HAS_FLAG(Vehicle vehicle, int flagIndex);
```

**Note**: Flags are not the same based on your `gamebuild`. Please see [here](https://docs.fivem.net/docs/game-references/vehicle-references/vehicle-flags) to see a complete list of all vehicle flags.

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).

## Parameters
* **vehicle**: The vehicle to obtain flags for.
* **flagIndex**: Flag index.

## Return value
A boolean for whether the flag is set.

### Example
```lua
local vehicleFlags = {
FLAG_SMALL_WORKER = 0,
FLAG_BIG = 1,
FLAG_NO_BOOT = 2,
FLAG_ONLY_DURING_OFFICE_HOURS = 3
-- This is just a example, see fivem-docs to see all flags.
}

local function getAllVehicleFlags(vehicle)
local flags = {}
for i = 0, 256 do
if GetVehicleHasFlag(vehicle, i) then
flags[#flags+1] = i
end
end
return flags
end

local flagsVehicle = GetVehiclePedIsIn(PlayerPedId(), false)
print(getAllVehicleFlags)
```

```javascript
const VEHICLE_FLAGS = {
FLAG_SMALL_WORKER: 0,
FLAG_BIG: 1,
FLAG_NO_BOOT: 2,
FLAG_ONLY_DURING_OFFICE_HOURS: 3
// This is just a example, see fivem-docs to see all flags.
};

function getAllVehicleFlags(mVehicle = GetVehiclePedIsIn(PlayerPedId(), false)) {
const flags = [];
for (let i = 0; i < 204; i++) {
if (GetVehicleHasFlag(mVehicle, i)) {
flags.push(i);
}
}
return flags;
}

let flagsVehicle = GetVehiclePedIsIn(PlayerPedId(), false);
console.log(getAllVehicleFlags);
```
32 changes: 32 additions & 0 deletions ext/native-decls/SetVehicleFlag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
ns: CFX
apiset: client
---
## SET_VEHICLE_FLAG

```c
bool SET_VEHICLE_FLAG(Vehicle vehicle, int flagIndex, bool value);
```

This native is a setter for [`GET_VEHICLE_HAS_FLAG`](#_0xD85C9F57).

## Parameters
* **vehicle**: The vehicle to set flag for.
* **flagIndex**: Flag index.
* **value**: `true` to enable the flag, `false` to disable it.


### Example
```lua
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
if not vehicle then return end

SetVehicleFlag(vehicle, 43, true) -- Set the vehicle as electric
```

```javascript
let vehicle = GetVehiclePedIsIn(PlayerPedId(), false);
if (!vehicle) return;

SetVehicleFlag(vehicle, 43, true); // Set the vehicle as electric
```
Loading