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(extra-natives/five): add getters for new track junction #3197

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
68 changes: 68 additions & 0 deletions code/components/extra-natives-five/src/TrackNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ static InitFunction initFunction([]()
}
});

fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_JUNCTION_COUNT", [](fx::ScriptContext& context)
{
context.SetResult<int>(g_trackJunctions.size());
});

fx::ScriptEngine::RegisterNativeHandler("REGISTER_TRACK_JUNCTION", [](fx::ScriptContext& context)
{
int8_t trackIndex = context.GetArgument<int8_t>(0);
Expand Down Expand Up @@ -425,6 +430,69 @@ static InitFunction initFunction([]()
context.SetResult<bool>(true);
});

fx::ScriptEngine::RegisterNativeHandler("IS_TRACK_JUNCTION_ACTIVE", [](fx::ScriptContext& context)
{
size_t junctionIndex = context.GetArgument<size_t>(0);

const std::lock_guard _(g_trackJunctionLock);

if (junctionIndex >= g_trackJunctions.size())
{
fx::scripting::Warningf("natives", "IS_TRACK_JUNCTION_ACTIVE: Invalid junction id (%i) provided. There are %i registered junctions\n", junctionIndex, g_trackJunctions.size());
context.SetResult<bool>(false);
return;
}

context.SetResult<bool>(true);
*context.GetArgument<bool*>(1) = g_trackJunctions[junctionIndex].isActive;
});

fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_JUNCTION_INFO", [](fx::ScriptContext& context)
{
size_t junctionIndex = context.GetArgument<size_t>(0);

const std::lock_guard _(g_trackJunctionLock);

if (junctionIndex >= g_trackJunctions.size())
{
fx::scripting::Warningf("natives", "GET_TRACK_JUNCTION_INFO: Invalid junction id (%i) provided. There are %i registered junctions\n", junctionIndex, g_trackJunctions.size());
context.SetResult<bool>(false);
return;
}

context.SetResult<bool>(true);

*context.GetArgument<int*>(1) = g_trackJunctions[junctionIndex].onTrack;
*context.GetArgument<uint32_t*>(2) = g_trackJunctions[junctionIndex].onNode;
*context.GetArgument<int*>(3) = g_trackJunctions[junctionIndex].newTrack;
*context.GetArgument<uint32_t*>(4) = g_trackJunctions[junctionIndex].newNode;
*context.GetArgument<bool*>(5) = g_trackJunctions[junctionIndex].direction;
});

fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_JUNCTION_FROM_NODES", [](fx::ScriptContext& context)
{
int8_t onTrack = context.GetArgument<int8_t>(0);
uint32_t onNode = context.GetArgument<uint32_t>(1);
int8_t newTrack = context.GetArgument<int8_t>(2);
uint32_t newNode = context.GetArgument<uint32_t>(3);
bool direction = context.GetArgument<bool>(4);

const std::lock_guard _(g_trackJunctionLock);
for (size_t i = 0; i < g_trackJunctions.size(); i++)
{
if (g_trackJunctions[i].onTrack == onTrack
&& g_trackJunctions[i].onNode == onNode
&& g_trackJunctions[i].newTrack == newTrack
&& g_trackJunctions[i].newNode == newNode
&& g_trackJunctions[i].direction == direction)
{
context.SetResult<int>(i);
return;
}
}
context.SetResult<int>(-1);
});

fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_NODE_COORDS", [](fx::ScriptContext& context)
{
int8_t trackIndex = context.GetArgument<int8_t>(0);
Expand Down
14 changes: 14 additions & 0 deletions ext/native-decls/GetTrackJunctionCount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRACK_JUNCTION_COUNT

```c
int GET_TRACK_JUNCTION_COUNT();
```


## Return value
Returns the number of track junctions
40 changes: 40 additions & 0 deletions ext/native-decls/GetTrackJunctionFromNodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRACK_JUNCTION_FROM_NODES

```c
int GET_TRACK_JUNCTION_FROM_NODES(int trackIndex, int trackNode, int newIndex, int newNode, bool direction);
```

## Examples
```lua
local onTrack = 0
local onNode = 3899
local newTrack = 1
local newNode = 83
local direction = true

local junctionId = RegisterTrackJunction(onTrack, onNode, newTrack, newNode, direction)
print(("The junctionId is %s"):format(junctionId))

local retrievedJunctionId = GetTrackJunctionFromNodes(onTrack, onNode, newTrack, newNode, direction)

if retrievedJunctionId ~= -1 then
print(('The junction is valid, junctionId %i'):format(retrievedJunctionId))
else
print('The junctionId is invalid')
end
```

## Parameters
* **trackIndex**: The track index a train should be on
* **trackNode**: The node a train should be on
* **newIndex**: The new track index for a train to be placed on
* **newNode**: The new track node for a train to be placed on
* **direction**: The direction a train should be traveling for this junction

## Return value
Returns the junction id for the given nodes, or -1 if no junction exists.
41 changes: 41 additions & 0 deletions ext/native-decls/GetTrackJunctionInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
ns: CFX
apiset: client
game: gta5
---
## GET_TRACK_JUNCTION_INFO

```c
bool GET_TRACK_JUNCTION_INFO(int junctionId, int* trackIndex, int* trackNode, int* newIndex, int* newNode, bool* direction);
```

## Examples

```lua
local onTrack = 0
local onNode = 3899
local newTrack = 1
local newNode = 83
local direction = true

local junctionId = RegisterTrackJunction(onTrack, onNode, newTrack, newNode, direction)
print(("The junctionId is %s"):format(junctionId))

local success, _onTrack, _onNode, _newTrack, _newNode, _direction = GetTrackJunctionInfo(junctionId)
if success then
print(('The junction is valid, on track %i, on node %i, new track %i, new node %i, direction %s'):format(_onTrack, _onNode, _newTrack, _newNode, _direction and 'true' or 'false'))
else
print('The junctionId is invalid')
end
```

## Parameters
* **junctionId**: The track junction handle
* **trackIndex**: The track index a train should be on
* **trackNode**: The node a train should be on
* **newIndex**: The new track index for a train to be placed on
* **newNode**: The new track node for a train to be placed on
* **direction**: The direction a train should be traveling for this junction

## Return value
Returns true if junction id is valid, false otherwise.
17 changes: 17 additions & 0 deletions ext/native-decls/IsTrackJunctionActive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
ns: CFX
apiset: client
game: gta5
---
## IS_TRACK_JUNCTION_ACTIVE

```c
bool IS_TRACK_JUNCTION_ACTIVE(int junctionId, bool* isActive);
```

## Parameters
* **junctionId**: The track junction handle
* **isActive**: Whether the track junction is active

## Return value
Returns true if junction id is valid, false otherwise.
Loading