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(server/gamestate): vehicles natives #3071

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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this PR is missing the following code in SyncTrees_Five.h. It doesn't build without it as it can't instantiate SyncTree class that is now abstract.

	virtual CVehicleScriptGameStateNodeData* GetVehicleGameScript() override
	{
		auto[hasVdn, vehScriptNode] = this->template GetData<CVehicleScriptGameStateDataNode>();

		return (hasVdn) ? &vehScriptNode->data : nullptr;
	}


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: move this to somewhere around line 800 so its near other GET_PED_* natives (e.g. GET_PED_MAX_HEALTH).

{
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It is just an indicator that the enum has ended. Just a utility - e.g. can be used to iterate through all values of the enum (i = 0; i < DeathState_Max; ++i) or check that enum values never take more than x bits of space (assert(DeathState_Max < (1 << 3))). Some other enums in the game have this _Max value as well

Lets either remove or update the comment accordingly.

};
```

## 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.
Loading