Skip to content

Commit

Permalink
feat(server/gamestate): vehicles natives
Browse files Browse the repository at this point in the history
* Added IS_VEHICLE_IN_AIR
* Added IS_VEHICLE_DROWNING
* Added GET_DEATH_STATE to check if a player is alive, dying or dead.
  • Loading branch information
spacevx committed Jan 15, 2025
1 parent c95b323 commit b171ffa
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ struct CPedGameStateNodeData
bool actionModeEnabled;
bool isFlashlightOn;

int deathState;

inline CPedGameStateNodeData()
: lastVehicle(-1), lastVehicleSeat(-1), lastVehiclePedWasIn(-1)
{
Expand Down Expand Up @@ -742,6 +744,12 @@ struct CVehicleDamageStatusNodeData
bool windowsState[8];
};

struct CVehicleScriptGameStateNodeData
{
bool isDrowning;
bool isVehicleInAir;
};

struct CBoatGameStateNodeData
{
bool lockedToXY;
Expand Down Expand Up @@ -805,6 +813,8 @@ struct SyncTreeBase

virtual CVehicleGameStateNodeData* GetVehicleGameState() = 0;

virtual CVehicleScriptGameStateNodeData* GetVehicleGameScript() = 0;

virtual CVehicleAppearanceNodeData* GetVehicleAppearance() = 0;

virtual CPedTaskTreeDataNodeData* GetPedTaskTree() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,15 @@ struct CPhysicalScriptGameStateDataNode { };

struct CVehicleScriptGameStateDataNode
{
CVehicleScriptGameStateNodeData data;
ePopType m_popType;

bool Parse(SyncParseState& state)
{
// Strings pulled from X360 TU0
state.buffer.ReadBit(); // Has Freebies
state.buffer.ReadBit(); // Can Be Visibly Damaged
state.buffer.ReadBit(); // Is Drowning
data.isDrowning = state.buffer.ReadBit(); // Is Drowning
state.buffer.ReadBit(); // Part Of Convoy
state.buffer.ReadBit(); // Vehicle Can Be Targeted
state.buffer.ReadBit(); // Take Less Damage
Expand Down Expand Up @@ -491,7 +492,7 @@ struct CVehicleScriptGameStateDataNode
state.buffer.ReadBit();
}

state.buffer.ReadBit(); // "Is Vehicle In Air"
data.isVehicleInAir = state.buffer.ReadBit(); // "Is Vehicle In Air"
bool isParachuting = state.buffer.ReadBit();
if (isParachuting)
{
Expand Down Expand Up @@ -1324,7 +1325,7 @@ struct CPedGameStateDataNode
}

auto arrestState = state.buffer.Read<int>(1);
auto deathState = state.buffer.Read<int>(2);
data.deathState = state.buffer.Read<int>(2);

auto hasWeapon = state.buffer.ReadBit();
int weapon = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ struct SyncTree : public SyncTreeBaseImpl<TNode, true>
return nullptr;
}

virtual CVehicleScriptGameStateNodeData* GetVehicleGameScript() override
{
return nullptr;
}

virtual void CalculatePosition() override
{
// TODO: cache it?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,27 @@ static void Init()
return vn ? vn->isEngineStarting : false;
}));

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

return vn ? vn->isVehicleInAir : false;
}));

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

return vn ? vn->isDrowning : false;
}));

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

return vn ? vn->deathState : 0;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_VEHICLE_HANDBRAKE", makeEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
auto vn = entity->syncTree->GetVehicleGameState();
Expand Down
26 changes: 26 additions & 0 deletions ext/native-decls/GetPedDeathState.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
ns: CFX
apiset: server
---
## GET_PED_DEATH_STATE

```c
int GET_PED_DEATH_STATE(Ped ped);
```
Retrieves the death state of the ped.
```c
enum eDeathState
{
DeathState_Alive = 0, // The ped is alive
DeathState_Dying = 1, // The ped is in the process of dying
DeathState_Dead = 2, // The ped is dead
DeathState_Max = 3 // Maximum health? maybe
};
```

## Parameters
* **ped**: The target ped.

## Return value
Returns the current death state of the ped as a value from the `eDeathState` enumeration.
17 changes: 17 additions & 0 deletions ext/native-decls/IsVehicleDrowning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
ns: CFX
apiset: server
---
## IS_VEHICLE_DROWNING

```c
bool IS_VEHICLE_DROWNING(Vehicle vehicle);
```
Getter to check if the current vehicle is drowning.
## Parameters
* **vehicle**: The target vehicle.
## Return value
Return `true` if the vehicle is drowning (and the vehicle is underwater), `false` otherwise.
17 changes: 17 additions & 0 deletions ext/native-decls/IsVehicleInAir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
ns: CFX
apiset: server
---
## IS_VEHICLE_IN_AIR

```c
bool IS_VEHICLE_IN_AIR(Vehicle vehicle);
```
Getter to check if the current vehicle is in the air.
## Parameters
* **vehicle**: The target vehicle.
## Return value
Return `true` if the vehicle is in the air, `false` otherwise.

0 comments on commit b171ffa

Please sign in to comment.