From 252220ed6e353e3a3ef47aa7bc69f418e0149ff9 Mon Sep 17 00:00:00 2001 From: DDuarte Date: Tue, 26 Jan 2016 03:37:07 +0000 Subject: [PATCH] Core/SAI: Code improvements to SMART_ACTION_RANDOM_SOUND Warning fixes and extra sanity checks Ref #16376 (cherry picked from commit 77087db79318443fff1b6fe3c91b55a315c5bf92) --- .../game/AI/SmartScripts/SmartScript.cpp | 23 +++++++----------- .../game/AI/SmartScripts/SmartScriptMgr.cpp | 14 ++++++++--- .../game/AI/SmartScripts/SmartScriptMgr.h | 24 +++++++++---------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/server/game/AI/SmartScripts/SmartScript.cpp b/src/server/game/AI/SmartScripts/SmartScript.cpp index 662ccf4ca7f94..899a8cfedc65a 100644 --- a/src/server/game/AI/SmartScripts/SmartScript.cpp +++ b/src/server/game/AI/SmartScripts/SmartScript.cpp @@ -2340,26 +2340,21 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u case SMART_ACTION_RANDOM_SOUND: { std::vector sounds; - - for (uint8 i = 0; i < SMART_ACTION_PARAM_COUNT - 1; i++) - { - if (e.action.randomSound.sound[i]) - sounds.push_back(e.action.randomSound.sound[i]); - } + std::copy_if(e.action.randomSound.sounds.begin(), e.action.randomSound.sounds.end(), + std::back_inserter(sounds), [](uint32 sound) { return sound != 0; }); bool onlySelf = e.action.randomSound.onlySelf != 0; - ObjectList* targets = GetTargets(e, unit); - if (targets) + if (ObjectList* targets = GetTargets(e, unit)) { - for (ObjectList::const_iterator itr = targets->begin(); itr != targets->end(); ++itr) + for (WorldObject* const obj : *targets) { - if (IsUnit(*itr)) + if (IsUnit(obj)) { - uint32 sound = sounds[urand(0, sounds.size() - 1)]; - (*itr)->PlayDirectSound(sound, e.action.randomSound.onlySelf ? (*itr)->ToPlayer() : nullptr); - TC_LOG_DEBUG("scripts.ai", "SmartScript::ProcessAction:: SMART_ACTION_RANDOM_SOUND: target: %s (%s), sound: %u, onlyself: %u", - (*itr)->GetName().c_str(), (*itr)->GetGUID().ToString().c_str(), sound, e.action.randomSound.onlySelf); + uint32 sound = Trinity::Containers::SelectRandomContainerElement(sounds); + obj->PlayDirectSound(sound, onlySelf ? obj->ToPlayer() : nullptr); + TC_LOG_DEBUG("scripts.ai", "SmartScript::ProcessAction:: SMART_ACTION_RANDOM_SOUND: target: %s (%s), sound: %u, onlyself: %b", + obj->GetName().c_str(), obj->GetGUID().ToString().c_str(), sound, onlySelf); } } diff --git a/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp b/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp index b5317f64401fc..070b98a9c274f 100644 --- a/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp +++ b/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp @@ -841,12 +841,20 @@ bool SmartAIMgr::IsEventValid(SmartScriptHolder& e) return false; break; case SMART_ACTION_RANDOM_SOUND: - for (uint8 i = 0; i < SMART_ACTION_PARAM_COUNT - 1; i++) + { + if (std::all_of(e.action.randomSound.sounds.begin(), e.action.randomSound.sounds.end(), [](uint32 sound) { return sound == 0; })) { - if (e.action.randomSound.sound[i] && !IsSoundValid(e, e.action.randomSound.sound[i])) - return false; + TC_LOG_ERROR("sql.sql", "SmartAIMgr: Entry %d SourceType %u Event %u Action %u does not have any non-zero sound", + e.entryOrGuid, e.GetScriptType(), e.event_id, e.GetActionType()); + return false; } + + for (uint32 sound : e.action.randomSound.sounds) + if (sound && !IsSoundValid(e, sound)) + return false; + break; + } case SMART_ACTION_CAST: { if (!IsSpellValid(e, e.action.cast.spell)) diff --git a/src/server/game/AI/SmartScripts/SmartScriptMgr.h b/src/server/game/AI/SmartScripts/SmartScriptMgr.h index 7bcf875b961a6..ea0c01df63b4b 100644 --- a/src/server/game/AI/SmartScripts/SmartScriptMgr.h +++ b/src/server/game/AI/SmartScripts/SmartScriptMgr.h @@ -44,6 +44,16 @@ struct WayPoint float z; }; +enum eSmartAI +{ + SMART_EVENT_PARAM_COUNT = 4, + SMART_ACTION_PARAM_COUNT = 6, + SMART_SUMMON_COUNTER = 0xFFFFFF, + SMART_ESCORT_LAST_OOC_POINT = 0xFFFFFF, + SMART_RANDOM_POINT = 0xFFFFFE, + SMART_ESCORT_TARGETS = 0xFFFFFF +}; + enum SMART_EVENT_PHASE { SMART_EVENT_PHASE_ALWAYS = 0, @@ -541,7 +551,7 @@ enum SMART_ACTION SMART_ACTION_GAME_EVENT_START = 112, // GameEventId SMART_ACTION_START_CLOSEST_WAYPOINT = 113, // wp1, wp2, wp3, wp4, wp5, wp6, wp7 SMART_ACTION_RISE_UP = 114, // distance - SMART_ACTION_RANDOM_SOUND = 115, // SoundId1, SoundId2, SoundId3, SoundId4, SoundId5, onlySelf + SMART_ACTION_RANDOM_SOUND = 115, // soundId1, soundId2, soundId3, soundId4, soundId5, onlySelf SMART_ACTION_END = 116 }; @@ -1028,7 +1038,7 @@ struct SmartAction struct { - uint32 sound[5]; + std::array sounds; uint32 onlySelf; } randomSound; @@ -1195,16 +1205,6 @@ struct SmartTarget }; }; -enum eSmartAI -{ - SMART_EVENT_PARAM_COUNT = 4, - SMART_ACTION_PARAM_COUNT = 6, - SMART_SUMMON_COUNTER = 0xFFFFFF, - SMART_ESCORT_LAST_OOC_POINT = 0xFFFFFF, - SMART_RANDOM_POINT = 0xFFFFFE, - SMART_ESCORT_TARGETS = 0xFFFFFF -}; - enum SmartScriptType { SMART_SCRIPT_TYPE_CREATURE = 0, //done