Skip to content

Commit

Permalink
feat(gamestate/server): train sync node getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehbw committed Dec 5, 2024
1 parent f3958ce commit 0b10649
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,18 @@ struct CTrainGameStateDataNodeData
bool isEngine;
bool isCaboose;

bool unk12;
bool isMissionTrain;

bool direction;

bool unk14;
bool hasPassengerCarriages;

bool renderDerailed;

// 2372 {
bool unk198;
bool unk224;
bool unk199;
bool allowRemovalByPopulation;
bool highPrecisionBlending;
bool stopAtStations;
// }

bool forceDoorsOpen;
Expand Down
32 changes: 21 additions & 11 deletions code/components/citizen-server-impl/include/state/SyncTrees_Five.h
Original file line number Diff line number Diff line change
Expand Up @@ -2886,21 +2886,31 @@ struct CTrainGameStateDataNode : GenericSerializeDataNode<CTrainGameStateDataNod
// 0 = Moving, 1 = Slowing down, 2 = Doors opening, 3 = Stopped, 4 = Doors closing, 5 = Before depart
s.Serialize(3, data.trainState);

s.Serialize(data.isEngine);
s.Serialize(data.isEngine);

//2372 inserted a bool between isEngine and isCaboose
if (Is2372())
{
//Modified by 0x2310A8F9421EBF43
s.Serialize(data.allowRemovalByPopulation);
}

s.Serialize(data.isCaboose);
s.Serialize(data.unk12);
s.Serialize(data.isMissionTrain);
s.Serialize(data.direction);
s.Serialize(data.unk14);
s.Serialize(data.hasPassengerCarriages);
s.Serialize(data.renderDerailed);

if (Is2372()) // Sequence of bits need to be verified for 2732
{
s.Serialize(data.unk198);
s.Serialize(data.unk224);
s.Serialize(data.unk199);
}


s.Serialize(data.forceDoorsOpen);

if (Is2372())
{
// Set on population trains or with SET_TRAIN_STOP_AT_STATIONS
s.Serialize(data.stopAtStations);

// Modified by _NETWORK_USE_HIGH_PRECISION_VEHICLE_BLENDING
s.Serialize(data.highPrecisionBlending);
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,99 @@ static void Init()
auto train = entity->syncTree->GetTrainState();

return train ? train->carriageIndex : -1;
}));
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_STATE", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->trainState : -1;
}));

fx::ScriptEngine::RegisterNativeHandler("IS_TRAIN_CABOOSE", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->isCaboose : false;
}));

fx::ScriptEngine::RegisterNativeHandler("DOES_TRAIN_STOP_AT_STATIONS", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->stopAtStations : false;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_CRUISE_SPEED", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->cruiseSpeed : 0.0f;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_TRACK_INDEX", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->trackId : -1;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_FORWARD_CARRIAGE", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

if (!train)
{
return uint32_t(0);
}

if (train->isEngine)
{
return uint32_t(0);
}

auto resourceManager = fx::ResourceManager::GetCurrent();

auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();

auto gameState = instance->GetComponent<fx::ServerGameState>();

auto forwardCarriage = gameState->GetEntity(0, train->linkedToForwardId);

return forwardCarriage ? gameState->MakeScriptHandle(forwardCarriage) : 0;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_BACKWARD_CARRIAGE", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

if (!train)
{
return uint32_t(0);
}

if (train->isCaboose)
{
return uint32_t(0);
}

auto resourceManager = fx::ResourceManager::GetCurrent();

auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();

auto gameState = instance->GetComponent<fx::ServerGameState>();

auto backwardCarriage = gameState->GetEntity(0, train->linkedToBackwardId);

return backwardCarriage ? gameState->MakeScriptHandle(backwardCarriage) : 0;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_TRAIN_DIRECTION", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto train = entity->syncTree->GetTrainState();

return train ? train->direction : false;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_FAKE_WANTED_LEVEL", MakePlayerEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
Expand Down
2 changes: 1 addition & 1 deletion ext/native-decls/DoesTrainStopAtStations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ns: CFX
apiset: client
apiset: shared
game: gta5
---
## DOES_TRAIN_STOP_AT_STATIONS
Expand Down
16 changes: 16 additions & 0 deletions ext/native-decls/GetTrainBackwardCarriage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
ns: CFX
apiset: shared
game: gta5
---
## GET_TRAIN_BACKWARD_CARRIAGE

```c
int GET_TRAIN_BACKWARD_CARRIAGE(Vehicle train);
```
## Parameters
* **train**: The train handle
## Return value
The handle of the carriage behind this train in the chain. Otherwise returns 0 if the train is the caboose of the chain.
2 changes: 1 addition & 1 deletion ext/native-decls/GetTrainCruiseSpeed.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ns: CFX
apiset: client
apiset: shared
game: gta5
---
## GET_TRAIN_CRUISE_SPEED
Expand Down
2 changes: 1 addition & 1 deletion ext/native-decls/GetTrainDirection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ns: CFX
apiset: client
apiset: shared
game: gta5
---
## GET_TRAIN_DIRECTION
Expand Down
16 changes: 16 additions & 0 deletions ext/native-decls/GetTrainForwardCarriage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
ns: CFX
apiset: shared
game: gta5
---
## GET_TRAIN_FORWARD_CARRIAGE

```c
int GET_TRAIN_FORWARD_CARRIAGE(Vehicle train);
```
## Parameters
* **train**: The train handle
## Return value
The handle of the carriage in front of this train in the chain. Otherwise returns 0 if the train has no carriage in front of it
2 changes: 1 addition & 1 deletion ext/native-decls/GetTrainState.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ns: CFX
apiset: client
apiset: shared
game: gta5
---
## GET_TRAIN_STATE
Expand Down
16 changes: 16 additions & 0 deletions ext/native-decls/GetTrainTrackIndex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
ns: CFX
apiset: server
game: gta5
---
## GET_TRAIN_TRACK_INDEX

```c
int GET_TRAIN_TRACK_INDEX(Vehicle train);
```
## Parameters
* **train**: The train handle
## Return value
The track index the train is currently on.
16 changes: 16 additions & 0 deletions ext/native-decls/IsTrainCaboose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
ns: CFX
apiset: server
game: gta5
---
## IS_TRAIN_CABOOSE

```c
bool IS_TRAIN_CABOOSE(Vehicle train);
```
## Parameters
* **train**: The train handle
## Return value
Returns true if the train is the caboose of the chain.

0 comments on commit 0b10649

Please sign in to comment.