Skip to content

Commit

Permalink
Merge Train movement natives (pr-2972)
Browse files Browse the repository at this point in the history
b2cdc64 - feat(extra-natives/five): train movement natives
  • Loading branch information
prikolium-cfx committed Dec 4, 2024
2 parents 15d5ebe + b2cdc64 commit d3e9bdf
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
33 changes: 33 additions & 0 deletions code/components/extra-natives-five/src/VehicleExtraNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ static hook::cdecl_stub<bool(void*)> isDriverAPlayer([]
return hook::get_call(hook::get_pattern("E8 ? ? ? ? 84 C0 74 ? 83 BE ? ? ? ? ? 75 ? F3 0F 59 35"));
});

static hook::cdecl_stub<void(void*, int)> setTrainState([]
{
return hook::get_call(hook::get_pattern("E8 ? ? ? ? 4C 89 AF ? ? ? ? 49 8B 0F"));
});

struct PatternPair
{
std::string_view pattern;
Expand Down Expand Up @@ -317,6 +322,10 @@ static bool* g_trainsForceDoorsOpen;
static int TrainDoorCountOffset;
static int TrainDoorArrayPointerOffset;
static int TrainFlagOffset;
static int TrainStateOffset;
static int TrainCruiseSpeedOffset;
static int TrainSpeedOffset;

constexpr int TrainStopAtStationsFlag = 4;

static int VehicleRepairMethodVtableOffset;
Expand Down Expand Up @@ -591,6 +600,9 @@ static HookFunction initFunction([]()
LightMultiplierGetOffset = *hook::get_pattern<uint32_t>("00 00 48 8B CE F3 0F 59 ? ? ? 00 00 F3 41", 9);
VehicleRepairMethodVtableOffset = *hook::get_pattern<uint32_t>("C1 E8 19 A8 01 74 ? 48 8B 81", -14);
TrainFlagOffset = *hook::get_pattern<uint32_t>("80 8B ? ? ? ? ? 8B 05 ? ? ? ? FF C8", 2);
TrainStateOffset = *hook::get_pattern<uint32_t>("89 91 ? ? ? ? 80 3D ? ? ? ? ? 0F 84", 2);
TrainCruiseSpeedOffset = *hook::get_pattern<uint32_t>("C7 87 ? ? ? ? ? ? ? ? E8 ? ? ? ? 4C 89 AF", 2);
TrainSpeedOffset = *hook::get_pattern<uint32_t>("4C 89 AF ? ? ? ? 44 89 AF ? ? ? ? 4C 89 AF ? ? ? ? 49 8B 0E", 3);
}

{
Expand Down Expand Up @@ -1473,6 +1485,27 @@ static HookFunction initFunction([]()

fx::ScriptEngine::RegisterNativeHandler("DOES_TRAIN_STOP_AT_STATIONS", std::bind(readVehicleMemoryBit<&TrainFlagOffset, TrainStopAtStationsFlag>, _1, "DOES_TRAIN_STOP_AT_STATIONS"));

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

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_STATE", std::bind(readVehicleMemory<int, &TrainStateOffset>, _1, "GET_TRAIN_STATE"));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_CRUISE_SPEED", std::bind(readVehicleMemory<float, &TrainCruiseSpeedOffset>, _1, "GET_TRAIN_CRUISE_SPEED"));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_SPEED", std::bind(readVehicleMemory<float, &TrainSpeedOffset>, _1, "GET_TRAIN_SPEED"));

fx::ScriptEngine::RegisterNativeHandler("SET_TRAIN_STATE", makeTrainFunction([](fx::ScriptContext& context, fwEntity* train)
{
int state = context.GetArgument<int>(1);

if (state < 0 || state > 5)
{
return;
}

// This resets a timer used to ensure train states don't get stuck for too long.
setTrainState(train, state);
}));

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

```c
float GET_TRAIN_CRUISE_SPEED(Vehicle train);
```
Gets the trains desired speed.
## Parameters
* **train**: The train handle
## Return value
The desired cruise speed of the train. Not the speed the train is currently traveling at
18 changes: 18 additions & 0 deletions ext/native-decls/GetTrainDirection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRAIN_DIRECTION

```c
BOOL GET_TRAIN_DIRECTION(Vehicle train);
```
Gets the direction the train is facing
## Parameters
* **train**: The train handle
## Return value
True if the train is moving forward on the track, False otherwise
18 changes: 18 additions & 0 deletions ext/native-decls/GetTrainSpeed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRAIN_SPEED

```c
float GET_TRAIN_SPEED(Vehicle train);
```
Gets the speed the train is currently going.
## Parameters
* **train**: The train handle
## Return value
The current speed of the train
28 changes: 28 additions & 0 deletions ext/native-decls/GetTrainState.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRAIN_STATE

```c
int GET_TRAIN_STATE(Vehicle train);
```
## Parameters
* **train**: The train handle
## Return value
The trains current state
```c
enum eTrainState
{
MOVING = 0,
ENTERING_STATION,
OPENING_DOORS,
STOPPED,
CLOSING_DOORS,
LEAVING_STATION,
}
```
17 changes: 17 additions & 0 deletions ext/native-decls/SetTrainState.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
ns: CFX
apiset: client
game: gta5
---
## SET_TRAIN_STATE

```c
void SET_TRAIN_STATE(Vehicle train, int state);
```
## Parameters
* **train**: The train handle
* **state**: The trains new state
## Return value
The trains current state, refer to [GET_TRAIN_STATE](?_0x81b50033)

0 comments on commit d3e9bdf

Please sign in to comment.