From ad774fcb3fc1051754be66463b0301c8dfe9b1e8 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Wed, 17 Aug 2016 15:49:08 +0300 Subject: [PATCH] Implement a new event to receive notifications when a player team has changed. --- source/Core.hpp | 3 +++ source/CoreEvents.cpp | 8 ++++++++ source/CoreUtils.cpp | 4 ++++ source/Entity/Player.cpp | 21 ++++++++++++++++++--- source/Entity/Player.hpp | 3 ++- source/SqBase.hpp | 1 + 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/source/Core.hpp b/source/Core.hpp index b927f2a4..655e35be 100644 --- a/source/Core.hpp +++ b/source/Core.hpp @@ -424,6 +424,7 @@ protected: Function mOnOption; Function mOnAdmin; Function mOnWorld; + Function mOnTeam; }; /* -------------------------------------------------------------------------------------------- @@ -996,6 +997,7 @@ public: void EmitPlayerOption(Int32 player_id, Int32 option_id, bool value, Int32 header, Object & payload); void EmitPlayerAdmin(Int32 player_id, bool old_admin, bool new_admin); void EmitPlayerWorld(Int32 player_id, Int32 old_world, Int32 new_world, bool secondary); + void EmitPlayerTeam(Int32 player_id, Int32 old_team, Int32 new_team); void EmitVehicleColour(Int32 vehicle_id, Int32 changed); void EmitVehicleHealth(Int32 vehicle_id, Float32 old_health, Float32 new_health); void EmitVehiclePosition(Int32 vehicle_id); @@ -1135,6 +1137,7 @@ private: Function mOnPlayerOption; Function mOnPlayerAdmin; Function mOnPlayerWorld; + Function mOnPlayerTeam; Function mOnVehicleColour; Function mOnVehicleHealth; Function mOnVehiclePosition; diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index 41b8cb9a..2fd614dd 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -741,6 +741,14 @@ void Core::EmitPlayerWorld(Int32 player_id, Int32 old_world, Int32 new_world, bo Emit(mOnPlayerWorld, _player.mObj, old_world, new_world, secondary); } +// ------------------------------------------------------------------------------------------------ +void Core::EmitPlayerTeam(Int32 player_id, Int32 old_team, Int32 new_team) +{ + PlayerInst & _player = m_Players.at(player_id); + Emit(_player.mOnTeam, old_team, new_team); + Emit(mOnPlayerTeam, _player.mObj, old_team, new_team); +} + // ------------------------------------------------------------------------------------------------ void Core::EmitVehicleColour(Int32 vehicle_id, Int32 changed) { diff --git a/source/CoreUtils.cpp b/source/CoreUtils.cpp index ca942721..4c68bf91 100644 --- a/source/CoreUtils.cpp +++ b/source/CoreUtils.cpp @@ -229,6 +229,7 @@ void Core::ResetFunc(PlayerInst & inst) inst.mOnOption.ReleaseGently(); inst.mOnAdmin.ReleaseGently(); inst.mOnWorld.ReleaseGently(); + inst.mOnTeam.ReleaseGently(); } // ------------------------------------------------------------------------------------------------ @@ -345,6 +346,7 @@ void Core::ResetFunc() Core::Get().mOnPlayerOption.ReleaseGently(); Core::Get().mOnPlayerAdmin.ReleaseGently(); Core::Get().mOnPlayerWorld.ReleaseGently(); + Core::Get().mOnPlayerTeam.ReleaseGently(); Core::Get().mOnVehicleColour.ReleaseGently(); Core::Get().mOnVehicleHealth.ReleaseGently(); Core::Get().mOnVehiclePosition.ReleaseGently(); @@ -453,6 +455,7 @@ Function & Core::GetEvent(Int32 evid) case EVT_PLAYEROPTION: return mOnPlayerOption; case EVT_PLAYERADMIN: return mOnPlayerAdmin; case EVT_PLAYERWORLD: return mOnPlayerWorld; + case EVT_PLAYERTEAM: return mOnPlayerTeam; case EVT_VEHICLECOLOUR: return mOnVehicleColour; case EVT_VEHICLEHEALTH: return mOnVehicleHealth; case EVT_VEHICLEPOSITION: return mOnVehiclePosition; @@ -611,6 +614,7 @@ Function & Core::GetPlayerEvent(Int32 id, Int32 evid) case EVT_PLAYEROPTION: return inst.mOnOption; case EVT_PLAYERADMIN: return inst.mOnAdmin; case EVT_PLAYERWORLD: return inst.mOnWorld; + case EVT_PLAYERTEAM: return inst.mOnTeam; default: return NullFunction(); } } diff --git a/source/Entity/Player.cpp b/source/Entity/Player.cpp index ffd67c52..a9a90d3c 100644 --- a/source/Entity/Player.cpp +++ b/source/Entity/Player.cpp @@ -510,15 +510,30 @@ Int32 CPlayer::GetTeam() const } // ------------------------------------------------------------------------------------------------ -void CPlayer::SetTeam(Int32 team) const +void CPlayer::SetTeam(Int32 team) { // Validate the managed identifier Validate(); - // Perform the requested operation - if (_Func->SetPlayerTeam(m_ID, team) == vcmpErrorArgumentOutOfBounds) + // Grab the current value for this property + const Int32 current = _Func->GetPlayerTeam(m_ID); + // Don't even bother if it's the same value + if (current == team) + { + return; + } + // Avoid property unwind from a recursive call + else if (_Func->SetPlayerTeam(m_ID, team) == vcmpErrorArgumentOutOfBounds) { STHROWF("Invalid team identifier: %d", team); } + // Avoid infinite recursive event loops + else if (!(m_CircularLocks & PCL_EMIT_PLAYER_TEAM)) + { + // Prevent this event from triggering while executed + BitGuardU32 bg(m_CircularLocks, PCL_EMIT_PLAYER_TEAM); + // Now forward the event call + Core::Get().EmitPlayerTeam(m_ID, current, team); + } } // ------------------------------------------------------------------------------------------------ diff --git a/source/Entity/Player.hpp b/source/Entity/Player.hpp index 85287cbc..fb2d600a 100644 --- a/source/Entity/Player.hpp +++ b/source/Entity/Player.hpp @@ -16,6 +16,7 @@ enum PlayerCircularLocks PCL_EMIT_PLAYER_OPTION = (1 << 0) PCL_EMIT_PLAYER_ADMIN = (2 << 0), PCL_EMIT_PLAYER_WORLD = (3 << 0), + PCL_EMIT_PLAYER_TEAM = (4 << 0), }; /* ------------------------------------------------------------------------------------------------ @@ -365,7 +366,7 @@ public: /* -------------------------------------------------------------------------------------------- * Modify the team of the managed player entity. */ - void SetTeam(Int32 team) const; + void SetTeam(Int32 team); /* -------------------------------------------------------------------------------------------- * Retrieve the skin identifier of the managed player entity. diff --git a/source/SqBase.hpp b/source/SqBase.hpp index 82713cf2..682b415c 100644 --- a/source/SqBase.hpp +++ b/source/SqBase.hpp @@ -378,6 +378,7 @@ enum EventType EVT_PLAYEROPTION, EVT_PLAYERADMIN, EVT_PLAYERWORLD, + EVT_PLAYERTEAM, EVT_VEHICLECOLOUR, EVT_VEHICLEHEALTH, EVT_VEHICLEPOSITION,