From cdc0ac7585f7a15a2e7f501d6ad9afe743744270 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 19 Aug 2016 18:21:41 +0300 Subject: [PATCH] Implement a new event to receive notifications when a pickup automatic status has changed. --- source/Core.hpp | 3 +++ source/CoreEvents.cpp | 8 ++++++++ source/CoreUtils.cpp | 8 ++++++-- source/Entity/Pickup.cpp | 19 +++++++++++++++++-- source/Entity/Pickup.hpp | 5 +++-- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/source/Core.hpp b/source/Core.hpp index cbbe40fe..e011a8fd 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -305,6 +305,7 @@ protected: Function mOnCollected; Function mOnWorld; Function mOnAlpha; + Function mOnAutomatic; }; /* -------------------------------------------------------------------------------------------- @@ -1015,6 +1016,7 @@ public: void EmitObjectAlpha(Int32 object_id, Int32 old_alpha, Int32 new_alpha, Int32 time); void EmitPickupWorld(Int32 pickup_id, Int32 old_world, Int32 new_world); void EmitPickupAlpha(Int32 pickup_id, Int32 old_alpha, Int32 new_alpha); + void EmitPickupAutomatic(Int32 pickup_id, bool old_status, bool new_status); 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); @@ -1168,6 +1170,7 @@ private: Function mOnPickupRespawn; Function mOnPickupWorld; Function mOnPickupAlpha; + Function mOnPickupAutomatic; Function mOnCheckpointEntered; Function mOnCheckpointExited; Function mOnCheckpointWorld; diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index 604a090d..5dfa32b3 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -724,6 +724,14 @@ void Core::EmitPickupAlpha(Int32 pickup_id, Int32 old_alpha, Int32 new_alpha) Emit(mOnPickupAlpha, _pickup.mObj, old_alpha, new_alpha); } +// ------------------------------------------------------------------------------------------------ +void Core::EmitPickupAutomatic(Int32 pickup_id, bool old_status, bool new_status) +{ + PickupInst & _pickup = m_Pickups.at(pickup_id); + Emit(_pickup.mOnAutomatic, old_status, new_status); + Emit(mOnPickupAutomatic, _pickup.mObj, old_status, new_status); +} + // ------------------------------------------------------------------------------------------------ void Core::EmitObjectReport(Int32 object_id, bool old_status, bool new_status, bool touched) { diff --git a/source/CoreUtils.cpp b/source/CoreUtils.cpp index d609318d..d07144d3 100644 --- a/source/CoreUtils.cpp +++ b/source/CoreUtils.cpp @@ -167,6 +167,7 @@ void Core::ResetFunc(PickupInst & inst) inst.mOnCollected.ReleaseGently(); inst.mOnWorld.ReleaseGently(); inst.mOnAlpha.ReleaseGently(); + inst.mOnAutomatic.ReleaseGently(); } // ------------------------------------------------------------------------------------------------ @@ -354,9 +355,10 @@ void Core::ResetFunc() Core::Get().mOnObjectReport.ReleaseGently(); Core::Get().mOnPickupClaimed.ReleaseGently(); Core::Get().mOnPickupCollected.ReleaseGently(); + Core::Get().mOnPickupRespawn.ReleaseGently(); Core::Get().mOnPickupWorld.ReleaseGently(); Core::Get().mOnPickupAlpha.ReleaseGently(); - Core::Get().mOnPickupRespawn.ReleaseGently(); + Core::Get().mOnPickupAutomatic.ReleaseGently(); Core::Get().mOnCheckpointEntered.ReleaseGently(); Core::Get().mOnCheckpointExited.ReleaseGently(); Core::Get().mOnCheckpointWorld.ReleaseGently(); @@ -483,9 +485,10 @@ Function & Core::GetEvent(Int32 evid) case EVT_OBJECTREPORT: return mOnObjectReport; case EVT_PICKUPCLAIMED: return mOnPickupClaimed; case EVT_PICKUPCOLLECTED: return mOnPickupCollected; + case EVT_PICKUPRESPAWN: return mOnPickupRespawn; case EVT_PICKUPWORLD: return mOnPickupWorld; case EVT_PICKUPALPHA: return mOnPickupAlpha; - case EVT_PICKUPRESPAWN: return mOnPickupRespawn; + case EVT_PICKUPAUTOMATIC: return mOnPickupAutomatic; case EVT_CHECKPOINTENTERED: return mOnCheckpointEntered; case EVT_CHECKPOINTEXITED: return mOnCheckpointExited; case EVT_CHECKPOINTWORLD: return mOnCheckpointWorld; @@ -606,6 +609,7 @@ Function & Core::GetPickupEvent(Int32 id, Int32 evid) case EVT_PICKUPCOLLECTED: return inst.mOnCollected; case EVT_PICKUPWORLD: return inst.mOnWorld; case EVT_PICKUPALPHA: return inst.mOnAlpha; + case EVT_PICKUPAUTOMATIC: return inst.mOnAutomatic; default: return NullFunction(); } } diff --git a/source/Entity/Pickup.cpp b/source/Entity/Pickup.cpp index 8f9491af..fa26af6b 100644 --- a/source/Entity/Pickup.cpp +++ b/source/Entity/Pickup.cpp @@ -233,12 +233,27 @@ bool CPickup::GetAutomatic() const } // ------------------------------------------------------------------------------------------------ -void CPickup::SetAutomatic(bool toggle) const +void CPickup::SetAutomatic(bool toggle) { // Validate the managed identifier Validate(); - // Perform the requested operation + // Grab the current value for this property + const bool current = _Func->IsPickupAutomatic(m_ID); + // Don't even bother if it's the same value + if (current == toggle) + { + return; + } + // Avoid property unwind from a recursive call _Func->SetPickupIsAutomatic(m_ID, toggle); + // Avoid infinite recursive event loops + if (!(m_CircularLocks & PICKUPCL_EMIT_PICKUP_AUTOMATIC)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, PICKUPCL_EMIT_PICKUP_AUTOMATIC); + // Now forward the event call + Core::Get().EmitPickupAutomatic(m_ID, current, toggle); + } } // ------------------------------------------------------------------------------------------------ diff --git a/source/Entity/Pickup.hpp b/source/Entity/Pickup.hpp index cb783e47..248400e0 100644 --- a/source/Entity/Pickup.hpp +++ b/source/Entity/Pickup.hpp @@ -13,7 +13,8 @@ namespace SqMod { enum PickupCircularLocks { PICKUPCL_EMIT_PICKUP_WORLD = (1 << 0), - PICKUPCL_EMIT_PICKUP_ALPHA = (2 << 0) + PICKUPCL_EMIT_PICKUP_ALPHA = (2 << 0), + PICKUPCL_EMIT_PICKUP_AUTOMATIC = (3 << 0) }; /* ------------------------------------------------------------------------------------------------ @@ -222,7 +223,7 @@ public: /* -------------------------------------------------------------------------------------------- * Set whether the managed pickup entity is automatic. */ - void SetAutomatic(bool toggle) const; + void SetAutomatic(bool toggle); /* -------------------------------------------------------------------------------------------- * Retrieve the automatic timer of the managed pickup entity.