From db522913d3e01876d78bb740b354fc11404685ee Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 18 Aug 2016 15:13:33 +0300 Subject: [PATCH] Implement a new event to receive notifications when a vehicle radio has changed. --- source/Core.hpp | 3 +++ source/CoreEvents.cpp | 7 +++++++ source/CoreUtils.cpp | 4 ++++ source/Entity/Vehicle.cpp | 19 +++++++++++++++++-- source/Entity/Vehicle.hpp | 5 +++-- source/SqBase.hpp | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/source/Core.hpp b/source/Core.hpp index 1256ac29..c83cbec6 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -500,6 +500,7 @@ protected: Function mOnPartStatus; Function mOnTyreStatus; Function mOnDamageData; + Function mOnRadio; }; public: @@ -1025,6 +1026,7 @@ public: void EmitVehiclePartStatus(Int32 vehicle_id, Int32 old_status, Int32 new_status); void EmitVehicleTyreStatus(Int32 vehicle_id, Int32 old_status, Int32 new_status); void EmitVehicleDamageData(Int32 vehicle_id, Uint32 old_data, Uint32 new_data); + void EmitVehicleRadio(Int32 vehicle_id, Int32 old_radio, Int32 new_radio); void EmitServerOption(Int32 option, bool value, Int32 header, Object & payload); void EmitScriptReload(Int32 header, Object & payload); void EmitScriptLoaded(); @@ -1176,6 +1178,7 @@ private: Function mOnVehiclePartStatus; Function mOnVehicleTyreStatus; Function mOnVehicleDamageData; + Function mOnVehicleRadio; Function mOnServerOption; Function mOnScriptReload; Function mOnScriptLoaded; diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index d1ec582d..b3c4b84e 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -878,6 +878,13 @@ void Core::EmitVehicleDamageData(Int32 vehicle_id, Uint32 old_data, Uint32 new_d Emit(mOnVehicleDamageData, _vehicle.mObj, old_data, new_data); } +// ------------------------------------------------------------------------------------------------ +void Core::EmitVehicleRadio(Int32 vehicle_id, Int32 old_radio, Int32 new_radio) +{ + VehicleInst & _vehicle = m_Vehicles.at(vehicle_id); + Emit(_vehicle.mOnRadio, old_radio, new_radio); + Emit(mOnVehicleRadio, _vehicle.mObj, old_radio, new_radio); +} // ------------------------------------------------------------------------------------------------ void Core::EmitServerOption(Int32 option, bool value, Int32 header, Object & payload) { diff --git a/source/CoreUtils.cpp b/source/CoreUtils.cpp index d12374d7..74b9349c 100644 --- a/source/CoreUtils.cpp +++ b/source/CoreUtils.cpp @@ -259,6 +259,7 @@ void Core::ResetFunc(VehicleInst & inst) inst.mOnPartStatus.ReleaseGently(); inst.mOnTyreStatus.ReleaseGently(); inst.mOnDamageData.ReleaseGently(); + inst.mOnRadio.ReleaseGently(); } // ------------------------------------------------------------------------------------------------ @@ -374,6 +375,7 @@ void Core::ResetFunc() Core::Get().mOnVehiclePartStatus.ReleaseGently(); Core::Get().mOnVehicleTyreStatus.ReleaseGently(); Core::Get().mOnVehicleDamageData.ReleaseGently(); + Core::Get().mOnVehicleRadio.ReleaseGently(); Core::Get().mOnServerOption.ReleaseGently(); Core::Get().mOnScriptReload.ReleaseGently(); Core::Get().mOnScriptLoaded.ReleaseGently(); @@ -494,6 +496,7 @@ Function & Core::GetEvent(Int32 evid) case EVT_VEHICLEPARTSTATUS: return mOnVehiclePartStatus; case EVT_VEHICLETYRESTATUS: return mOnVehicleTyreStatus; case EVT_VEHICLEDAMAGEDATA: return mOnVehicleDamageData; + case EVT_VEHICLERADIO: return mOnVehicleRadio; case EVT_SERVEROPTION: return mOnServerOption; case EVT_SCRIPTRELOAD: return mOnScriptReload; case EVT_SCRIPTLOADED: return mOnScriptLoaded; @@ -681,6 +684,7 @@ Function & Core::GetVehicleEvent(Int32 id, Int32 evid) case EVT_VEHICLEPARTSTATUS: return inst.mOnPartStatus; case EVT_VEHICLETYRESTATUS: return inst.mOnTyreStatus; case EVT_VEHICLEDAMAGEDATA: return inst.mOnDamageData; + case EVT_VEHICLERADIO: return inst.mOnRadio; default: return NullFunction(); } } diff --git a/source/Entity/Vehicle.cpp b/source/Entity/Vehicle.cpp index 70924cd3..b6406f7e 100644 --- a/source/Entity/Vehicle.cpp +++ b/source/Entity/Vehicle.cpp @@ -964,12 +964,27 @@ Int32 CVehicle::GetRadio() const } // ------------------------------------------------------------------------------------------------ -void CVehicle::SetRadio(Int32 radio) const +void CVehicle::SetRadio(Int32 radio) { // Validate the managed identifier Validate(); - // Perform the requested operation + // Grab the current value for this property + const Int32 current = _Func->GetVehicleRadio(m_ID); + // Don't even bother if it's the same value + if (current == radio) + { + return; + } + // Avoid property unwind from a recursive call _Func->SetVehicleRadio(m_ID, radio); + // Avoid infinite recursive event loops + if (!(m_CircularLocks & VEHICLECL_EMIT_VEHICLE_RADIO)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, VEHICLECL_EMIT_VEHICLE_RADIO); + // Now forward the event call + Core::Get().EmitVehicleRadio(m_ID, current, radio); + } } // ------------------------------------------------------------------------------------------------ diff --git a/source/Entity/Vehicle.hpp b/source/Entity/Vehicle.hpp index 1ab6609f..f7c02387 100644 --- a/source/Entity/Vehicle.hpp +++ b/source/Entity/Vehicle.hpp @@ -17,7 +17,8 @@ enum VehicleCircularLocks VEHICLECL_EMIT_VEHICLE_IMMUNITY = (3 << 0), VEHICLECL_EMIT_VEHICLE_PARTSTATUS = (4 << 0), VEHICLECL_EMIT_VEHICLE_TYRESTATUS = (5 << 0), - VEHICLECL_EMIT_VEHICLE_DAMAGEDATA = (6 << 0) + VEHICLECL_EMIT_VEHICLE_DAMAGEDATA = (6 << 0), + VEHICLECL_EMIT_VEHICLE_RADIO = (7 << 0) }; /* ------------------------------------------------------------------------------------------------ @@ -563,7 +564,7 @@ public: /* -------------------------------------------------------------------------------------------- * Retrieve the radio of the managed vehicle entity. */ - void SetRadio(Int32 radio) const; + void SetRadio(Int32 radio); /* -------------------------------------------------------------------------------------------- * Retrieve the turret rotation of the managed vehicle entity. diff --git a/source/SqBase.hpp b/source/SqBase.hpp index dafc5b2f..743f612f 100644 --- a/source/SqBase.hpp +++ b/source/SqBase.hpp @@ -395,6 +395,7 @@ enum EventType EVT_VEHICLEPARTSTATUS, EVT_VEHICLETYRESTATUS, EVT_VEHICLEDAMAGEDATA, + EVT_VEHICLERADIO, EVT_SERVEROPTION, EVT_SCRIPTRELOAD, EVT_SCRIPTLOADED,