From ed5f1a86deb0c80871556611ba038dfb7fb21a55 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 18 Aug 2016 17:12:00 +0300 Subject: [PATCH] Implement a new event to receive notifications when an object shot or touched report status has changed. --- source/Core.hpp | 3 +++ source/CoreEvents.cpp | 8 ++++++++ source/CoreUtils.cpp | 4 ++++ source/Entity/Object.cpp | 38 ++++++++++++++++++++++++++++++++++---- source/Entity/Object.hpp | 7 ++++--- source/SqBase.hpp | 1 + 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/source/Core.hpp b/source/Core.hpp index 4d0f0a72..c05f2cd6 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -257,6 +257,7 @@ protected: Function mOnTouched; Function mOnWorld; Function mOnAlpha; + Function mOnReport; }; /* -------------------------------------------------------------------------------------------- @@ -1010,6 +1011,7 @@ public: void EmitCheckpointRadius(Int32 checkpoint_id, Float32 old_radius, Float32 new_radius); void EmitObjectWorld(Int32 object_id, Int32 old_world, Int32 new_world); void EmitObjectAlpha(Int32 object_id, Int32 old_alpha, Int32 new_alpha, Int32 time); + void EmitObjectReport(Int32 object_id, bool old_status, bool new_status, bool touched); void EmitPlayerHealth(Int32 player_id, Float32 old_health, Float32 new_health); void EmitPlayerArmour(Int32 player_id, Float32 old_armour, Float32 new_armour); void EmitPlayerWeapon(Int32 player_id, Int32 old_weapon, Int32 new_weapon); @@ -1156,6 +1158,7 @@ private: Function mOnObjectTouched; Function mOnObjectWorld; Function mOnObjectAlpha; + Function mOnObjectReport; Function mOnPickupClaimed; Function mOnPickupCollected; Function mOnPickupRespawn; diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index 70b5138a..e0c7d556 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -708,6 +708,14 @@ void Core::EmitObjectAlpha(Int32 object_id, Int32 old_alpha, Int32 new_alpha, In Emit(mOnObjectAlpha, _object.mObj, old_alpha, new_alpha, time); } +// ------------------------------------------------------------------------------------------------ +void Core::EmitObjectReport(Int32 object_id, bool old_status, bool new_status, bool touched) +{ + ObjectInst & _object = m_Objects.at(object_id); + Emit(_object.mOnReport, old_status, new_status, touched); + Emit(mOnObjectReport, _object.mObj, old_status, new_status, touched); +} + // ------------------------------------------------------------------------------------------------ void Core::EmitPlayerHealth(Int32 player_id, Float32 old_health, Float32 new_health) { diff --git a/source/CoreUtils.cpp b/source/CoreUtils.cpp index 0aa0eab8..4d48e139 100644 --- a/source/CoreUtils.cpp +++ b/source/CoreUtils.cpp @@ -154,6 +154,7 @@ void Core::ResetFunc(ObjectInst & inst) inst.mOnTouched.ReleaseGently(); inst.mOnWorld.ReleaseGently(); inst.mOnAlpha.ReleaseGently(); + inst.mOnReport.ReleaseGently(); } // ------------------------------------------------------------------------------------------------ @@ -348,6 +349,7 @@ void Core::ResetFunc() Core::Get().mOnObjectTouched.ReleaseGently(); Core::Get().mOnObjectWorld.ReleaseGently(); Core::Get().mOnObjectAlpha.ReleaseGently(); + Core::Get().mOnObjectReport.ReleaseGently(); Core::Get().mOnPickupClaimed.ReleaseGently(); Core::Get().mOnPickupCollected.ReleaseGently(); Core::Get().mOnPickupRespawn.ReleaseGently(); @@ -474,6 +476,7 @@ Function & Core::GetEvent(Int32 evid) case EVT_OBJECTTOUCHED: return mOnObjectTouched; case EVT_OBJECTWORLD: return mOnObjectWorld; case EVT_OBJECTALPHA: return mOnObjectAlpha; + case EVT_OBJECTREPORT: return mOnObjectReport; case EVT_PICKUPCLAIMED: return mOnPickupClaimed; case EVT_PICKUPCOLLECTED: return mOnPickupCollected; case EVT_PICKUPRESPAWN: return mOnPickupRespawn; @@ -578,6 +581,7 @@ Function & Core::GetObjectEvent(Int32 id, Int32 evid) case EVT_OBJECTTOUCHED: return inst.mOnTouched; case EVT_OBJECTWORLD: return inst.mOnWorld; case EVT_OBJECTALPHA: return inst.mOnAlpha; + case EVT_OBJECTREPORT: return inst.mOnReport; default: return NullFunction(); } } diff --git a/source/Entity/Object.cpp b/source/Entity/Object.cpp index 3fd8b810..4c5e02c8 100644 --- a/source/Entity/Object.cpp +++ b/source/Entity/Object.cpp @@ -421,12 +421,27 @@ bool CObject::GetShotReport() const } // ------------------------------------------------------------------------------------------------ -void CObject::SetShotReport(bool toggle) const +void CObject::SetShotReport(bool toggle) { // Validate the managed identifier Validate(); - // Perform the requested operation + // Grab the current value for this property + const bool current = _Func->IsObjectShotReportEnabled(m_ID); + // Don't even bother if it's the same value + if (current == toggle) + { + return; + } + // Avoid property unwind from a recursive call _Func->SetObjectShotReportEnabled(m_ID, toggle); + // Avoid infinite recursive event loops + if (!(m_CircularLocks & OBJECTCL_EMIT_OBJECT_REPORT)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, OBJECTCL_EMIT_OBJECT_REPORT); + // Now forward the event call + Core::Get().EmitObjectReport(m_ID, current, toggle, false); + } } // ------------------------------------------------------------------------------------------------ @@ -439,12 +454,27 @@ bool CObject::GetTouchedReport() const } // ------------------------------------------------------------------------------------------------ -void CObject::SetTouchedReport(bool toggle) const +void CObject::SetTouchedReport(bool toggle) { // Validate the managed identifier Validate(); - // Perform the requested operation + // Grab the current value for this property + const bool current = _Func->IsObjectTouchedReportEnabled(m_ID); + // Don't even bother if it's the same value + if (current == toggle) + { + return; + } + // Avoid property unwind from a recursive call _Func->SetObjectTouchedReportEnabled(m_ID, toggle); + // Avoid infinite recursive event loops + if (!(m_CircularLocks & OBJECTCL_EMIT_OBJECT_REPORT)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, OBJECTCL_EMIT_OBJECT_REPORT); + // Now forward the event call + Core::Get().EmitObjectReport(m_ID, current, toggle, true); + } } // ------------------------------------------------------------------------------------------------ diff --git a/source/Entity/Object.hpp b/source/Entity/Object.hpp index 8c4f779c..bf108be6 100644 --- a/source/Entity/Object.hpp +++ b/source/Entity/Object.hpp @@ -13,7 +13,8 @@ namespace SqMod { enum ObjectCircularLocks { OBJECTCL_EMIT_OBJECT_WORLD = (1 << 0), - OBJECTCL_EMIT_OBJECT_ALPHA = (2 << 0) + OBJECTCL_EMIT_OBJECT_ALPHA = (2 << 0), + OBJECTCL_EMIT_OBJECT_REPORT = (3 << 0) }; /* ------------------------------------------------------------------------------------------------ @@ -336,7 +337,7 @@ public: /* -------------------------------------------------------------------------------------------- * Set whether the managed object entity reports gunshots. */ - void SetShotReport(bool toggle) const; + void SetShotReport(bool toggle); /* -------------------------------------------------------------------------------------------- * See whether the managed object entity reports player bumps. @@ -346,7 +347,7 @@ public: /* -------------------------------------------------------------------------------------------- * Set whether the managed object entity reports player bumps. */ - void SetTouchedReport(bool toggle) const; + void SetTouchedReport(bool toggle); /* -------------------------------------------------------------------------------------------- * Retrieve the position on the x axis of the managed object entity. diff --git a/source/SqBase.hpp b/source/SqBase.hpp index dcc7efb7..7d38f9c8 100644 --- a/source/SqBase.hpp +++ b/source/SqBase.hpp @@ -363,6 +363,7 @@ enum EventType EVT_OBJECTTOUCHED, EVT_OBJECTWORLD, EVT_OBJECTALPHA, + EVT_OBJECTREPORT, EVT_PICKUPCLAIMED, EVT_PICKUPCOLLECTED, EVT_PICKUPRESPAWN,