From 05443ba2d4e9c67a552a26c410e86144b3690c67 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 18 Aug 2016 14:56:38 +0300 Subject: [PATCH] Implement a new event to receive notifications when a vehicle tyre status has changed. --- source/Core.hpp | 3 +++ source/CoreEvents.cpp | 8 ++++++++ source/CoreUtils.cpp | 4 ++++ source/Entity/Vehicle.cpp | 23 +++++++++++++++++++---- source/Entity/Vehicle.hpp | 5 +++-- source/SqBase.hpp | 1 + 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/source/Core.hpp b/source/Core.hpp index e7511a18..46ec87c6 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -498,6 +498,7 @@ protected: Function mOnWorld; Function mOnImmunity; Function mOnPartStatus; + Function mOnTyreStatus; }; public: @@ -1021,6 +1022,7 @@ public: void EmitVehicleWorld(Int32 vehicle_id, Int32 old_world, Int32 new_world); void EmitVehicleImmunity(Int32 vehicle_id, Int32 old_immunity, Int32 new_immunity); void EmitVehiclePartStatus(Int32 vehicle_id, Int32 old_status, Int32 new_status); + void EmitVehicleTyreStatus(Int32 vehicle_id, Int32 old_status, Int32 new_status); void EmitServerOption(Int32 option, bool value, Int32 header, Object & payload); void EmitScriptReload(Int32 header, Object & payload); void EmitScriptLoaded(); @@ -1170,6 +1172,7 @@ private: Function mOnVehicleWorld; Function mOnVehicleImmunity; Function mOnVehiclePartStatus; + Function mOnVehicleTyreStatus; Function mOnServerOption; Function mOnScriptReload; Function mOnScriptLoaded; diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index fc4fd18b..e660411f 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -862,6 +862,14 @@ void Core::EmitVehiclePartStatus(Int32 vehicle_id, Int32 old_status, Int32 new_s Emit(mOnVehiclePartStatus, _vehicle.mObj, old_status, new_status); } +// ------------------------------------------------------------------------------------------------ +void Core::EmitVehicleTyreStatus(Int32 vehicle_id, Int32 old_status, Int32 new_status) +{ + VehicleInst & _vehicle = m_Vehicles.at(vehicle_id); + Emit(_vehicle.mOnTyreStatus, old_status, new_status); + Emit(mOnVehicleTyreStatus, _vehicle.mObj, old_status, new_status); +} + // ------------------------------------------------------------------------------------------------ void Core::EmitServerOption(Int32 option, bool value, Int32 header, Object & payload) { diff --git a/source/CoreUtils.cpp b/source/CoreUtils.cpp index 965bca59..25337ace 100644 --- a/source/CoreUtils.cpp +++ b/source/CoreUtils.cpp @@ -257,6 +257,7 @@ void Core::ResetFunc(VehicleInst & inst) inst.mOnWorld.ReleaseGently(); inst.mOnImmunity.ReleaseGently(); inst.mOnPartStatus.ReleaseGently(); + inst.mOnTyreStatus.ReleaseGently(); } // ------------------------------------------------------------------------------------------------ @@ -370,6 +371,7 @@ void Core::ResetFunc() Core::Get().mOnVehicleWorld.ReleaseGently(); Core::Get().mOnVehicleImmunity.ReleaseGently(); Core::Get().mOnVehiclePartStatus.ReleaseGently(); + Core::Get().mOnVehicleTyreStatus.ReleaseGently(); Core::Get().mOnServerOption.ReleaseGently(); Core::Get().mOnScriptReload.ReleaseGently(); Core::Get().mOnScriptLoaded.ReleaseGently(); @@ -488,6 +490,7 @@ Function & Core::GetEvent(Int32 evid) case EVT_VEHICLEWORLD: return mOnVehicleWorld; case EVT_VEHICLEIMMUNITY: return mOnVehicleImmunity; case EVT_VEHICLEPARTSTATUS: return mOnVehiclePartStatus; + case EVT_VEHICLETYRESTATUS: return mOnVehicleTyreStatus; case EVT_SERVEROPTION: return mOnServerOption; case EVT_SCRIPTRELOAD: return mOnScriptReload; case EVT_SCRIPTLOADED: return mOnScriptLoaded; @@ -673,6 +676,7 @@ Function & Core::GetVehicleEvent(Int32 id, Int32 evid) case EVT_VEHICLEWORLD: return inst.mOnWorld; case EVT_VEHICLEIMMUNITY: return inst.mOnImmunity; case EVT_VEHICLEPARTSTATUS: return inst.mOnPartStatus; + case EVT_VEHICLETYRESTATUS: return inst.mOnTyreStatus; default: return NullFunction(); } } diff --git a/source/Entity/Vehicle.cpp b/source/Entity/Vehicle.cpp index ff4cea66..e2f563fd 100644 --- a/source/Entity/Vehicle.cpp +++ b/source/Entity/Vehicle.cpp @@ -894,16 +894,31 @@ Int32 CVehicle::GetTyreStatus(Int32 tyre) const // Validate the managed identifier Validate(); // Return the requested information - return _Func->GetVehicleTyreStatus(m_ID, tyre); + return _Func->(m_ID, tyre); } // ------------------------------------------------------------------------------------------------ -void CVehicle::SetTyreStatus(Int32 tyre, Int32 status) const +void CVehicle::SetTyreStatus(Int32 tyre, Int32 status) { // Validate the managed identifier Validate(); - // Perform the requested operation - _Func->SetVehicleTyreStatus(m_ID, tyre, status); + // Grab the current value for this property + const Int32 current = _Func->GetVehicleTyreStatus(m_ID); + // Don't even bother if it's the same value + if (current == status) + { + return; + } + // Avoid property unwind from a recursive call + _Func->SetVehicleTyreStatus(m_ID, status); + // Avoid infinite recursive event loops + if (!(m_CircularLocks & VEHICLECL_EMIT_VEHICLE_TYRESTATUS)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, VEHICLECL_EMIT_VEHICLE_TYRESTATUS); + // Now forward the event call + Core::Get().EmitVehicleTyreStatus(m_ID, current, status); + } } // ------------------------------------------------------------------------------------------------ diff --git a/source/Entity/Vehicle.hpp b/source/Entity/Vehicle.hpp index 6799c0bb..d9fecb0b 100644 --- a/source/Entity/Vehicle.hpp +++ b/source/Entity/Vehicle.hpp @@ -15,7 +15,8 @@ enum VehicleCircularLocks VEHICLECL_EMIT_VEHICLE_OPTION = (1 << 0), VEHICLECL_EMIT_VEHICLE_WORLD = (2 << 0), VEHICLECL_EMIT_VEHICLE_IMMUNITY = (3 << 0), - VEHICLECL_EMIT_VEHICLE_PARTSTATUS = (4 << 0) + VEHICLECL_EMIT_VEHICLE_PARTSTATUS = (4 << 0), + VEHICLECL_EMIT_VEHICLE_TYRESTATUS = (5 << 0) }; /* ------------------------------------------------------------------------------------------------ @@ -541,7 +542,7 @@ public: /* -------------------------------------------------------------------------------------------- * Modify the tyre status of the managed vehicle entity. */ - void SetTyreStatus(Int32 tyre, Int32 status) const; + void SetTyreStatus(Int32 tyre, Int32 status); /* -------------------------------------------------------------------------------------------- * Retrieve the damage data of the managed vehicle entity. diff --git a/source/SqBase.hpp b/source/SqBase.hpp index 375ed41a..b0f04e87 100644 --- a/source/SqBase.hpp +++ b/source/SqBase.hpp @@ -393,6 +393,7 @@ enum EventType EVT_VEHICLEWORLD, EVT_VEHICLEIMMUNITY, EVT_VEHICLEPARTSTATUS, + EVT_VEHICLETYRESTATUS, EVT_SERVEROPTION, EVT_SCRIPTRELOAD, EVT_SCRIPTLOADED,