From b2cdc64a1018b598bca866cb2b7079225702893f Mon Sep 17 00:00:00 2001 From: Ehbw Date: Sun, 1 Dec 2024 20:33:11 +0000 Subject: [PATCH] feat(extra-natives/five): train movement natives --- .../src/VehicleExtraNatives.cpp | 33 +++++++++++++++++++ ext/native-decls/GetTrainCruiseSpeed.md | 18 ++++++++++ ext/native-decls/GetTrainDirection.md | 18 ++++++++++ ext/native-decls/GetTrainSpeed.md | 18 ++++++++++ ext/native-decls/GetTrainState.md | 28 ++++++++++++++++ ext/native-decls/SetTrainState.md | 17 ++++++++++ 6 files changed, 132 insertions(+) create mode 100644 ext/native-decls/GetTrainCruiseSpeed.md create mode 100644 ext/native-decls/GetTrainDirection.md create mode 100644 ext/native-decls/GetTrainSpeed.md create mode 100644 ext/native-decls/GetTrainState.md create mode 100644 ext/native-decls/SetTrainState.md diff --git a/code/components/extra-natives-five/src/VehicleExtraNatives.cpp b/code/components/extra-natives-five/src/VehicleExtraNatives.cpp index 47a7bf4ae7..c6c28e5f90 100644 --- a/code/components/extra-natives-five/src/VehicleExtraNatives.cpp +++ b/code/components/extra-natives-five/src/VehicleExtraNatives.cpp @@ -56,6 +56,11 @@ static hook::cdecl_stub isDriverAPlayer([] return hook::get_call(hook::get_pattern("E8 ? ? ? ? 84 C0 74 ? 83 BE ? ? ? ? ? 75 ? F3 0F 59 35")); }); +static hook::cdecl_stub setTrainState([] +{ + return hook::get_call(hook::get_pattern("E8 ? ? ? ? 4C 89 AF ? ? ? ? 49 8B 0F")); +}); + struct PatternPair { std::string_view pattern; @@ -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; @@ -591,6 +600,9 @@ static HookFunction initFunction([]() LightMultiplierGetOffset = *hook::get_pattern("00 00 48 8B CE F3 0F 59 ? ? ? 00 00 F3 41", 9); VehicleRepairMethodVtableOffset = *hook::get_pattern("C1 E8 19 A8 01 74 ? 48 8B 81", -14); TrainFlagOffset = *hook::get_pattern("80 8B ? ? ? ? ? 8B 05 ? ? ? ? FF C8", 2); + TrainStateOffset = *hook::get_pattern("89 91 ? ? ? ? 80 3D ? ? ? ? ? 0F 84", 2); + TrainCruiseSpeedOffset = *hook::get_pattern("C7 87 ? ? ? ? ? ? ? ? E8 ? ? ? ? 4C 89 AF", 2); + TrainSpeedOffset = *hook::get_pattern("4C 89 AF ? ? ? ? 44 89 AF ? ? ? ? 4C 89 AF ? ? ? ? 49 8B 0E", 3); } { @@ -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, _1, "GET_TRAIN_STATE")); + + fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_CRUISE_SPEED", std::bind(readVehicleMemory, _1, "GET_TRAIN_CRUISE_SPEED")); + + fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_SPEED", std::bind(readVehicleMemory, _1, "GET_TRAIN_SPEED")); + + fx::ScriptEngine::RegisterNativeHandler("SET_TRAIN_STATE", makeTrainFunction([](fx::ScriptContext& context, fwEntity* train) + { + int state = context.GetArgument(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(*reinterpret_cast(wheelAddr + WheelXOffsetOffset)); diff --git a/ext/native-decls/GetTrainCruiseSpeed.md b/ext/native-decls/GetTrainCruiseSpeed.md new file mode 100644 index 0000000000..dbe5a45abb --- /dev/null +++ b/ext/native-decls/GetTrainCruiseSpeed.md @@ -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 \ No newline at end of file diff --git a/ext/native-decls/GetTrainDirection.md b/ext/native-decls/GetTrainDirection.md new file mode 100644 index 0000000000..3c6b671903 --- /dev/null +++ b/ext/native-decls/GetTrainDirection.md @@ -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 \ No newline at end of file diff --git a/ext/native-decls/GetTrainSpeed.md b/ext/native-decls/GetTrainSpeed.md new file mode 100644 index 0000000000..3b9fa362ae --- /dev/null +++ b/ext/native-decls/GetTrainSpeed.md @@ -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 \ No newline at end of file diff --git a/ext/native-decls/GetTrainState.md b/ext/native-decls/GetTrainState.md new file mode 100644 index 0000000000..238f23d37f --- /dev/null +++ b/ext/native-decls/GetTrainState.md @@ -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, +} +``` \ No newline at end of file diff --git a/ext/native-decls/SetTrainState.md b/ext/native-decls/SetTrainState.md new file mode 100644 index 0000000000..99ed47db9e --- /dev/null +++ b/ext/native-decls/SetTrainState.md @@ -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) \ No newline at end of file