Skip to content

Commit

Permalink
Core/SAI: Code improvements to SMART_ACTION_RANDOM_SOUND
Browse files Browse the repository at this point in the history
Warning fixes and extra sanity checks

Ref #16376

(cherry picked from commit 77087db)
  • Loading branch information
DDuarte authored and Shauren committed Apr 2, 2016
1 parent c457de1 commit 252220e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
23 changes: 9 additions & 14 deletions src/server/game/AI/SmartScripts/SmartScript.cpp
Expand Up @@ -2340,26 +2340,21 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
case SMART_ACTION_RANDOM_SOUND:
{
std::vector<uint32> 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);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/server/game/AI/SmartScripts/SmartScriptMgr.cpp
Expand Up @@ -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))
Expand Down
24 changes: 12 additions & 12 deletions src/server/game/AI/SmartScripts/SmartScriptMgr.h
Expand Up @@ -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,
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -1028,7 +1038,7 @@ struct SmartAction

struct
{
uint32 sound[5];
std::array<uint32, SMART_ACTION_PARAM_COUNT - 1> sounds;
uint32 onlySelf;
} randomSound;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 252220e

Please sign in to comment.