From 4c1030c76e3342a5d532f037bcd9af0932220427 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 4 May 2020 11:58:30 +0300 Subject: [PATCH] Implement simple distance tracking for player and vehicle entities. Doesn't differentiate from in-air and on-ground. Just sums up the distance from last position on each position update. --- module/Core.hpp | 2 ++ module/Core/Events.inc | 14 ++++++++++-- module/Core/Inst.inc | 2 ++ module/Entity/Player.cpp | 45 +++++++++++++++++++++++++++++++++++++++ module/Entity/Player.hpp | 20 +++++++++++++++++ module/Entity/Vehicle.cpp | 45 +++++++++++++++++++++++++++++++++++++++ module/Entity/Vehicle.hpp | 24 +++++++++++++++++++-- module/SqBase.hpp | 3 ++- 8 files changed, 150 insertions(+), 5 deletions(-) diff --git a/module/Core.hpp b/module/Core.hpp index 38c5497b..8c63eb53 100644 --- a/module/Core.hpp +++ b/module/Core.hpp @@ -422,6 +422,7 @@ protected: // ---------------------------------------------------------------------------------------- AreaList mAreas{}; // Areas the player is currently in. + Float64 mDistance; // Distance traveled while tracking was enabled. // ---------------------------------------------------------------------------------------- SQInteger mTrackPosition{0}; // The number of times to track position changes. @@ -582,6 +583,7 @@ protected: // ---------------------------------------------------------------------------------------- AreaList mAreas{}; // Areas the vehicle is currently in. + Float64 mDistance; // Distance traveled while tracking was enabled. // ---------------------------------------------------------------------------------------- SQInteger mTrackPosition{0}; // The number of times to track position changes. diff --git a/module/Core/Events.inc b/module/Core/Events.inc index cfcfd134..bc0de00c 100644 --- a/module/Core/Events.inc +++ b/module/Core/Events.inc @@ -1481,7 +1481,12 @@ void Core::EmitPlayerUpdate(Int32 player_id, vcmpPlayerUpdate update_type) // Now emit the event EmitPlayerPosition(player_id); } - // Should we check for area collision + // Should we check for distance traveled? + if (inst.mFlags & ENF_DIST_TRACK) + { + inst.mDistance += inst.mLastPosition.GetDistanceTo(pos); + } + // Should we check for area collision? if (inst.mFlags & ENF_AREA_TRACK) { // Eliminate existing areas first, if the player is not in them anymore @@ -1716,7 +1721,12 @@ void Core::EmitVehicleUpdate(Int32 vehicle_id, vcmpVehicleUpdate update_type) Vector3 pos; // Retrieve the current vehicle position _Func->GetVehiclePosition(vehicle_id, &pos.x, &pos.y, &pos.z); - // Should we check for area collision + // Should we check for distance traveled? + if (inst.mFlags & ENF_DIST_TRACK) + { + inst.mDistance += inst.mLastPosition.GetDistanceTo(pos); + } + // Should we check for area collision? if (inst.mFlags & ENF_AREA_TRACK) { // Eliminate existing areas first, if the vehicle is not in them anymore diff --git a/module/Core/Inst.inc b/module/Core/Inst.inc index c3829396..cfd5a4b2 100644 --- a/module/Core/Inst.inc +++ b/module/Core/Inst.inc @@ -344,6 +344,7 @@ void Core::PlayerInst::ResetInstance() mID = -1; mFlags = ENF_DEFAULT; mAreas.clear(); + mDistance = 0; mTrackPosition = 0; mTrackHeading = 0; mTrackPositionHeader = 0; @@ -364,6 +365,7 @@ void Core::VehicleInst::ResetInstance() mID = -1; mFlags = ENF_DEFAULT; mAreas.clear(); + mDistance = 0; mTrackPosition = 0; mTrackRotation = 0; mLastPrimaryColor = -1; diff --git a/module/Entity/Player.cpp b/module/Entity/Player.cpp index 57ff8dee..69c9279f 100644 --- a/module/Entity/Player.cpp +++ b/module/Entity/Player.cpp @@ -1780,6 +1780,49 @@ const Vector3 & CPlayer::GetLastPosition() const return Core::Get().GetPlayer(m_ID).mLastPosition; } +// ------------------------------------------------------------------------------------------------ +SQFloat CPlayer::GetDistance() const +{ + // Validate the managed identifier + Validate(); + // Return the requested information + return static_cast< SQFloat >(Core::Get().GetPlayer(m_ID).mDistance); +} + +// ------------------------------------------------------------------------------------------------ +void CPlayer::SetDistance(SQFloat distance) const +{ + // Validate the managed identifier + Validate(); + // Assign the requested information + Core::Get().GetPlayer(m_ID).mDistance = static_cast< Float64 >(distance); +} + +// ------------------------------------------------------------------------------------------------ +bool CPlayer::GetTrackDistance() const +{ + // Validate the managed identifier + Validate(); + // Return the requested information + return static_cast< bool >(Core::Get().GetPlayer(m_ID).mFlags & ENF_DIST_TRACK); +} + +// ------------------------------------------------------------------------------------------------ +void CPlayer::SetTrackDistance(bool toggle) const +{ + // Validate the managed identifier + Validate(); + // Assign the requested information + if (toggle) + { + Core::Get().GetPlayer(m_ID).mFlags |= ENF_DIST_TRACK; + } + else + { + Core::Get().GetPlayer(m_ID).mFlags ^= ENF_DIST_TRACK; + } +} + // ------------------------------------------------------------------------------------------------ void CPlayer::StartStream() { @@ -2796,6 +2839,8 @@ void Register_CPlayer(HSQUIRRELVM vm) .Prop(_SC("LastArmour"), &CPlayer::GetLastArmour) .Prop(_SC("LastHeading"), &CPlayer::GetLastHeading) .Prop(_SC("LastPosition"), &CPlayer::GetLastPosition) + .Prop(_SC("Distance"), &CPlayer::GetDistance, &CPlayer::SetDistance) + .Prop(_SC("TrackDistance"), &CPlayer::GetTrackDistance, &CPlayer::SetTrackDistance) .Prop(_SC("BufferCursor"), &CPlayer::GetBufferCursor, &CPlayer::SetBufferCursor) .Prop(_SC("BufferCapacity"), &CPlayer::GetBufferCapacity) .Prop(_SC("PosX"), &CPlayer::GetPositionX, &CPlayer::SetPositionX) diff --git a/module/Entity/Player.hpp b/module/Entity/Player.hpp index b5ea29b1..fdac4858 100644 --- a/module/Entity/Player.hpp +++ b/module/Entity/Player.hpp @@ -925,6 +925,26 @@ public: */ const Vector3 & GetLastPosition() const; + /* -------------------------------------------------------------------------------------------- + * Retrieve the distance traveled by the managed player entity while tracking was enabled. + */ + SQFloat GetDistance() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the distance traveled by the managed player entity. + */ + void SetDistance(SQFloat distance) const; + + /* -------------------------------------------------------------------------------------------- + * Check whether distance tracking is enabled for the managed player entity. + */ + bool GetTrackDistance() const; + + /* -------------------------------------------------------------------------------------------- + * Set whether distance tracking is enabled for the managed player entity. + */ + void SetTrackDistance(bool toggle) const; + /* -------------------------------------------------------------------------------------------- * Start a new stream with the default size. */ diff --git a/module/Entity/Vehicle.cpp b/module/Entity/Vehicle.cpp index 9a6cc021..34b967d7 100644 --- a/module/Entity/Vehicle.cpp +++ b/module/Entity/Vehicle.cpp @@ -1349,6 +1349,49 @@ const Quaternion & CVehicle::GetLastRotation() const return Core::Get().GetVehicle(m_ID).mLastRotation; } +// ------------------------------------------------------------------------------------------------ +SQFloat CVehicle::GetDistance() const +{ + // Validate the managed identifier + Validate(); + // Return the requested information + return static_cast< SQFloat >(Core::Get().GetVehicle(m_ID).mDistance); +} + +// ------------------------------------------------------------------------------------------------ +void CVehicle::SetDistance(SQFloat distance) const +{ + // Validate the managed identifier + Validate(); + // Assign the requested information + Core::Get().GetVehicle(m_ID).mDistance = static_cast< Float64 >(distance); +} + +// ------------------------------------------------------------------------------------------------ +bool CVehicle::GetTrackDistance() const +{ + // Validate the managed identifier + Validate(); + // Return the requested information + return static_cast< bool >(Core::Get().GetVehicle(m_ID).mFlags & ENF_DIST_TRACK); +} + +// ------------------------------------------------------------------------------------------------ +void CVehicle::SetTrackDistance(bool toggle) const +{ + // Validate the managed identifier + Validate(); + // Assign the requested information + if (toggle) + { + Core::Get().GetVehicle(m_ID).mFlags |= ENF_DIST_TRACK; + } + else + { + Core::Get().GetVehicle(m_ID).mFlags ^= ENF_DIST_TRACK; + } +} + // ------------------------------------------------------------------------------------------------ Float32 CVehicle::GetPositionX() const { @@ -2027,6 +2070,8 @@ void Register_CVehicle(HSQUIRRELVM vm) .Prop(_SC("LastHealth"), &CVehicle::GetLastHealth) .Prop(_SC("LastPosition"), &CVehicle::GetLastPosition) .Prop(_SC("LastRotation"), &CVehicle::GetLastRotation) + .Prop(_SC("Distance"), &CVehicle::GetDistance, &CVehicle::SetDistance) + .Prop(_SC("TrackDistance"), &CVehicle::GetTrackDistance, &CVehicle::SetTrackDistance) .Prop(_SC("PosX"), &CVehicle::GetPositionX, &CVehicle::SetPositionX) .Prop(_SC("PosY"), &CVehicle::GetPositionY, &CVehicle::SetPositionY) .Prop(_SC("PosZ"), &CVehicle::GetPositionZ, &CVehicle::SetPositionZ) diff --git a/module/Entity/Vehicle.hpp b/module/Entity/Vehicle.hpp index 57b0b14e..b0d04909 100644 --- a/module/Entity/Vehicle.hpp +++ b/module/Entity/Vehicle.hpp @@ -687,15 +687,35 @@ public: Float32 GetLastHealth() const; /* -------------------------------------------------------------------------------------------- - * Retrieve the last known position for the managed player entity. + * Retrieve the last known position for the managed vehicle entity. */ const Vector3 & GetLastPosition() const; /* -------------------------------------------------------------------------------------------- - * Retrieve the last known rotation for the managed player entity. + * Retrieve the last known rotation for the managed vehicle entity. */ const Quaternion & GetLastRotation() const; + /* -------------------------------------------------------------------------------------------- + * Retrieve the distance traveled by the managed vehicle entity while tracking was enabled. + */ + SQFloat GetDistance() const; + + /* -------------------------------------------------------------------------------------------- + * Modify the distance traveled by the managed vehicle entity. + */ + void SetDistance(SQFloat distance) const; + + /* -------------------------------------------------------------------------------------------- + * Check whether distance tracking is enabled for the managed vehicle entity. + */ + bool GetTrackDistance() const; + + /* -------------------------------------------------------------------------------------------- + * Set whether distance tracking is enabled for the managed vehicle entity. + */ + void SetTrackDistance(bool toggle) const; + /* -------------------------------------------------------------------------------------------- * Retrieve the position on the x axis of the managed vehicle entity. */ diff --git a/module/SqBase.hpp b/module/SqBase.hpp index 63204db8..9f3ce5e7 100644 --- a/module/SqBase.hpp +++ b/module/SqBase.hpp @@ -438,7 +438,8 @@ enum EntityFlags ENF_DEFAULT = (0), ENF_OWNED = (1u << 1u), ENF_LOCKED = (1u << 2u), - ENF_AREA_TRACK = (1u << 3u) + ENF_AREA_TRACK = (1u << 3u), + ENF_DIST_TRACK = (1u << 4u) }; /* ------------------------------------------------------------------------------------------------