Skip to content

Commit 4657228

Browse files
committed
feat(extra-natives/five): add getters for new track junction
1 parent 7173d32 commit 4657228

File tree

4 files changed

+189
-2
lines changed

4 files changed

+189
-2
lines changed

code/components/extra-natives-five/src/TrackNatives.cpp

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ static hook::cdecl_stub<rage::CTrainTrack*(uint32_t)> CTrainTrack__getTrainTrack
149149
return hook::get_call(hook::get_pattern("E8 ? ? ? ? 33 DB 45 0F 57 DB"));
150150
});
151151

152-
153152
static int kMaxTracks = xbr::IsGameBuildOrGreater<2545>() ? 127 : 27;
154153

155154
static int32_t FindClosestTrack(rage::Vector3& position, int8_t* outTrack)
@@ -447,7 +446,6 @@ static InitFunction initFunction([]()
447446

448447
context.SetResult<int>(junctionIndex);
449448
g_trackJunctions[junctionIndex] = CTrainJunction(trackIndex, trackNode, newTrackIndex, newTrackNode, direction);
450-
451449
});
452450

453451
fx::ScriptEngine::RegisterNativeHandler("REMOVE_TRACK_JUNCTION", [](fx::ScriptContext& context)
@@ -487,6 +485,97 @@ static InitFunction initFunction([]()
487485
context.SetResult<bool>(true);
488486
});
489487

488+
fx::ScriptEngine::RegisterNativeHandler("IS_TRACK_JUNCTION_ACTIVE", [](fx::ScriptContext& context)
489+
{
490+
size_t junctionIndex = context.GetArgument<size_t>(0);
491+
492+
const std::lock_guard _(g_trackJunctionLock);
493+
494+
if (!IsTrackJunctionIdValid(junctionIndex))
495+
{
496+
fx::scripting::Warningf("natives", "IS_TRACK_JUNCTION_ACTIVE: Invalid junction id (%i) provided.\n", junctionIndex);
497+
context.SetResult<bool>(false);
498+
return;
499+
}
500+
501+
context.SetResult<bool>(true);
502+
*context.GetArgument<bool*>(1) = g_trackJunctions[junctionIndex].isActive;
503+
});
504+
505+
fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_JUNCTION_INFO", [](fx::ScriptContext& context)
506+
{
507+
size_t junctionIndex = context.GetArgument<size_t>(0);
508+
509+
const std::lock_guard _(g_trackJunctionLock);
510+
511+
if (!IsTrackJunctionIdValid(junctionIndex))
512+
{
513+
fx::scripting::Warningf("natives", "GET_TRACK_JUNCTION_INFO: Invalid junction id (%i) provided.\n", junctionIndex);
514+
context.SetResult<bool>(false);
515+
return;
516+
}
517+
518+
context.SetResult<bool>(true);
519+
520+
*context.GetArgument<int*>(1) = g_trackJunctions[junctionIndex].onTrack;
521+
*context.GetArgument<uint32_t*>(2) = g_trackJunctions[junctionIndex].onNode;
522+
*context.GetArgument<int*>(3) = g_trackJunctions[junctionIndex].newTrack;
523+
*context.GetArgument<uint32_t*>(4) = g_trackJunctions[junctionIndex].newNode;
524+
*context.GetArgument<bool*>(5) = g_trackJunctions[junctionIndex].direction;
525+
});
526+
527+
fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_JUNCTION_FROM_NODES", [](fx::ScriptContext& context)
528+
{
529+
rage::CTrainTrack* testOnTrack = GetAndCheckTrack<0>(context, "GET_TRACK_JUNCTION_FROM_NODES");
530+
if (!testOnTrack)
531+
{
532+
context.SetResult<int>(-1);
533+
return;
534+
}
535+
536+
rage::CTrainTrack* testNewTrack = GetAndCheckTrack<2>(context, "GET_TRACK_JUNCTION_FROM_NODES");
537+
if (!testNewTrack)
538+
{
539+
context.SetResult<int>(-1);
540+
return;
541+
}
542+
543+
int8_t onTrack = context.GetArgument<int8_t>(0);
544+
uint32_t onNode = context.GetArgument<uint32_t>(1);
545+
int8_t newTrack = context.GetArgument<int8_t>(2);
546+
uint32_t newNode = context.GetArgument<uint32_t>(3);
547+
bool direction = context.GetArgument<bool>(4);
548+
549+
if(!GetAndCheckTrackNode(testOnTrack, onTrack, onNode, "GET_TRACK_JUNCTION_FROM_NODES"))
550+
{
551+
context.SetResult<int>(-1);
552+
return;
553+
}
554+
555+
if(!GetAndCheckTrackNode(testNewTrack, newTrack, newNode, "GET_TRACK_JUNCTION_FROM_NODES"))
556+
{
557+
context.SetResult<int>(-1);
558+
return;
559+
}
560+
561+
562+
const std::lock_guard _(g_trackJunctionLock);
563+
564+
for (auto& [index, junction] : g_trackJunctions)
565+
{
566+
if (junction.onTrack == onTrack
567+
&& junction.onNode == onNode
568+
&& junction.newTrack == newTrack
569+
&& junction.newNode == newNode
570+
&& junction.direction == direction)
571+
{
572+
context.SetResult<int>(index);
573+
return;
574+
}
575+
}
576+
context.SetResult<int>(-1);
577+
});
578+
490579
fx::ScriptEngine::RegisterNativeHandler("GET_TRACK_NODE_COORDS", [](fx::ScriptContext& context)
491580
{
492581
int8_t trackIndex = context.GetArgument<int8_t>(0);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
ns: CFX
3+
apiset: client
4+
game: gta5
5+
---
6+
## GET_TRACK_JUNCTION_FROM_NODES
7+
8+
```c
9+
int GET_TRACK_JUNCTION_FROM_NODES(int trackIndex, int trackNode, int newIndex, int newNode, bool direction);
10+
```
11+
12+
## Examples
13+
```lua
14+
local onTrack = 0
15+
local onNode = 3899
16+
local newTrack = 1
17+
local newNode = 83
18+
local direction = true
19+
20+
local junctionId = RegisterTrackJunction(onTrack, onNode, newTrack, newNode, direction)
21+
print(("The junctionId is %s"):format(junctionId))
22+
23+
local retrievedJunctionId = GetTrackJunctionFromNodes(onTrack, onNode, newTrack, newNode, direction)
24+
25+
if retrievedJunctionId ~= -1 then
26+
print(('The junction is valid, junctionId %i'):format(retrievedJunctionId))
27+
else
28+
print('The junctionId is invalid')
29+
end
30+
```
31+
32+
## Parameters
33+
* **trackIndex**: The track index a train should be on
34+
* **trackNode**: The node a train should be on
35+
* **newIndex**: The new track index for a train to be placed on
36+
* **newNode**: The new track node for a train to be placed on
37+
* **direction**: The direction a train should be traveling for this junction
38+
39+
## Return value
40+
Returns the junction id for the given nodes, or -1 if no junction exists.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
ns: CFX
3+
apiset: client
4+
game: gta5
5+
---
6+
## GET_TRACK_JUNCTION_INFO
7+
8+
```c
9+
bool GET_TRACK_JUNCTION_INFO(int junctionId, int* trackIndex, int* trackNode, int* newIndex, int* newNode, bool* direction);
10+
```
11+
12+
## Examples
13+
14+
```lua
15+
local onTrack = 0
16+
local onNode = 3899
17+
local newTrack = 1
18+
local newNode = 83
19+
local direction = true
20+
21+
local junctionId = RegisterTrackJunction(onTrack, onNode, newTrack, newNode, direction)
22+
print(("The junctionId is %s"):format(junctionId))
23+
24+
local isValid, _onTrack, _onNode, _newTrack, _newNode, _direction = GetTrackJunctionInfo(junctionId)
25+
if isValid then
26+
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'))
27+
else
28+
print('The junctionId is invalid')
29+
end
30+
```
31+
32+
## Parameters
33+
* **junctionId**: The track junction handle
34+
* **trackIndex**: The track index a train should be on
35+
* **trackNode**: The node a train should be on
36+
* **newIndex**: The new track index for a train to be placed on
37+
* **newNode**: The new track node for a train to be placed on
38+
* **direction**: The direction a train should be traveling for this junction
39+
40+
## Return value
41+
Returns true if junction id is valid, false otherwise.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
ns: CFX
3+
apiset: client
4+
game: gta5
5+
---
6+
## IS_TRACK_JUNCTION_ACTIVE
7+
8+
```c
9+
bool IS_TRACK_JUNCTION_ACTIVE(int junctionId, bool* isActive);
10+
```
11+
12+
## Parameters
13+
* **junctionId**: The track junction handle
14+
* **isActive**: Whether the track junction is active
15+
16+
## Return value
17+
Returns true if junction id is valid, false otherwise.

0 commit comments

Comments
 (0)