Misc/Util: Add a template function for returning a random element from any container
This commit is contained in:
@@ -160,11 +160,7 @@ class UnitAI
|
||||
return *ritr;
|
||||
}
|
||||
case SELECT_TARGET_RANDOM:
|
||||
{
|
||||
std::list<Unit*>::iterator itr = targetList.begin();
|
||||
std::advance(itr, urand(position, targetList.size()-1));
|
||||
return *itr;
|
||||
}
|
||||
return SelectRandomContainerElement(targetList);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -934,11 +934,8 @@ bool LFGMgr::CheckCompatibility(LfgGuidList check, LfgProposal*& pProposal)
|
||||
|
||||
// Select a random dungeon from the compatible list
|
||||
// TODO - Select the dungeon based on group item Level, not just random
|
||||
LfgDungeonSet::const_iterator itDungeon = compatibleDungeons.begin();
|
||||
std::advance(itDungeon, urand(0, compatibleDungeons.size() - 1));
|
||||
|
||||
// Create a new proposal
|
||||
pProposal = new LfgProposal(*itDungeon);
|
||||
pProposal = new LfgProposal(SelectRandomContainerElement(compatibleDungeons));
|
||||
pProposal->cancelTime = time_t(time(NULL)) + LFG_TIME_PROPOSAL;
|
||||
pProposal->state = LFG_PROPOSAL_INITIATING;
|
||||
pProposal->queues = check;
|
||||
|
||||
@@ -1400,10 +1400,7 @@ void GameObject::Use(Unit* user)
|
||||
{
|
||||
for (int i = 0; i < info->summoningRitual.casterTargetSpellTargets; i++)
|
||||
{
|
||||
std::set<uint32>::const_iterator itr = m_unique_users.begin();
|
||||
std::advance(itr, rand() % m_unique_users.size());
|
||||
|
||||
if (Unit* target = Unit::GetUnit(*this, uint64(*itr)))
|
||||
if (Unit* target = Unit::GetUnit(*this, (uint64)SelectRandomContainerElement(m_unique_users)))
|
||||
spellCaster->CastSpell(target, info->summoningRitual.casterTargetSpell, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14938,9 +14938,7 @@ Unit* Unit::SelectNearbyTarget(float dist) const
|
||||
return NULL;
|
||||
|
||||
// select random
|
||||
std::list<Unit*>::const_iterator tcIter = targets.begin();
|
||||
std::advance(tcIter, urand(0, targets.size()-1));
|
||||
return *tcIter;
|
||||
return SelectRandomContainerElement(targets);
|
||||
}
|
||||
|
||||
void Unit::ApplyAttackTimePercentMod(WeaponAttackType att, float val, bool apply)
|
||||
|
||||
@@ -1075,9 +1075,7 @@ void LootTemplate::LootGroup::Process(Loot& loot, uint16 lootMode) const
|
||||
if (item == NULL && !EqualPossibleDrops.empty()) // If nothing selected yet - an item is taken from equal-chanced part
|
||||
{
|
||||
itemSource = 2;
|
||||
itr = EqualPossibleDrops.begin();
|
||||
std::advance(itr, irand(0, EqualPossibleDrops.size()-1));
|
||||
item = &*itr;
|
||||
item = const_cast<LootStoreItem*>(&SelectRandomContainerElement(EqualPossibleDrops));
|
||||
}
|
||||
// finish rolling
|
||||
|
||||
|
||||
@@ -476,10 +476,9 @@ void PoolGroup<Quest>::SpawnObject(ActivePoolData& spawns, uint32 limit, uint32
|
||||
{
|
||||
do
|
||||
{
|
||||
ActivePoolObjects::iterator itr = currentQuests.begin();
|
||||
std::advance(itr, urand(0, currentQuests.size()-1));
|
||||
newQuests.insert(*itr);
|
||||
currentQuests.erase(*itr);
|
||||
uint32 questId = SelectRandomContainerElement(currentQuests);
|
||||
newQuests.insert(questId);
|
||||
currentQuests.erase(questId);
|
||||
} while (newQuests.size() < limit && !currentQuests.empty()); // failsafe
|
||||
}
|
||||
|
||||
@@ -489,12 +488,11 @@ void PoolGroup<Quest>::SpawnObject(ActivePoolData& spawns, uint32 limit, uint32
|
||||
// activate <limit> random quests
|
||||
do
|
||||
{
|
||||
ActivePoolObjects::iterator itr = newQuests.begin();
|
||||
std::advance(itr, urand(0, newQuests.size()-1));
|
||||
spawns.ActivateObject<Quest>(*itr, poolId);
|
||||
PoolObject tempObj(*itr, 0.0f);
|
||||
uint32 questId = SelectRandomContainerElement(newQuests);
|
||||
spawns.ActivateObject<Quest>(questId, poolId);
|
||||
PoolObject tempObj(questId, 0.0f);
|
||||
Spawn1Object(&tempObj);
|
||||
newQuests.erase(itr);
|
||||
newQuests.erase(questId);
|
||||
--limit;
|
||||
} while (limit && !newQuests.empty());
|
||||
|
||||
|
||||
@@ -5748,9 +5748,7 @@ void AuraEffect::HandlePeriodicDummyAuraTick(Unit* target, Unit* caster) const
|
||||
if (targets.empty())
|
||||
return;
|
||||
|
||||
UnitList::const_iterator itr = targets.begin();
|
||||
std::advance(itr, rand()%targets.size());
|
||||
Unit* spellTarget = *itr;
|
||||
Unit* spellTarget = SelectRandomContainerElement(targets);
|
||||
|
||||
target->CastSpell(spellTarget, 57840, true);
|
||||
target->CastSpell(spellTarget, 57841, true);
|
||||
|
||||
@@ -1373,10 +1373,9 @@ void Spell::EffectDummy(SpellEffIndex effIndex)
|
||||
uint32 maxTargets = std::min<uint32>(3, attackers.size());
|
||||
for (uint32 i = 0; i < maxTargets; ++i)
|
||||
{
|
||||
Unit::AttackerSet::iterator aItr = attackers.begin();
|
||||
std::advance(aItr, urand(0, attackers.size() - 1));
|
||||
AddUnitTarget(*aItr, 1);
|
||||
attackers.erase(aItr);
|
||||
Unit* attacker = SelectRandomContainerElement(attackers);
|
||||
AddUnitTarget(attacker, 1);
|
||||
attackers.erase(attacker);
|
||||
}
|
||||
|
||||
// now let next effect cast spell at each target.
|
||||
@@ -2621,10 +2620,7 @@ void Spell::EffectEnergize(SpellEffIndex effIndex)
|
||||
if (!avalibleElixirs.empty())
|
||||
{
|
||||
// cast random elixir on target
|
||||
uint32 rand_spell = urand(0, avalibleElixirs.size()-1);
|
||||
std::set<uint32>::iterator itr = avalibleElixirs.begin();
|
||||
std::advance(itr, rand_spell);
|
||||
m_caster->CastSpell(unitTarget, *itr, true, m_CastItem);
|
||||
m_caster->CastSpell(unitTarget, SelectRandomContainerElement(avalibleElixirs), true, m_CastItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2556,9 +2556,7 @@ void World::SendAutoBroadcast()
|
||||
|
||||
std::string msg;
|
||||
|
||||
std::list<std::string>::const_iterator itr = m_Autobroadcasts.begin();
|
||||
std::advance(itr, rand() % m_Autobroadcasts.size());
|
||||
msg = *itr;
|
||||
msg = SelectRandomContainerElement(m_Autobroadcasts);
|
||||
|
||||
uint32 abcenter = sWorld->getIntConfig(CONFIG_AUTOBROADCAST_CENTER);
|
||||
|
||||
|
||||
@@ -104,14 +104,7 @@ class boss_sulfuron : public CreatureScript
|
||||
Creature* target = NULL;
|
||||
std::list<Creature*> healers = DoFindFriendlyMissingBuff(45.0f, SPELL_INSPIRE);
|
||||
if (!healers.empty())
|
||||
{
|
||||
std::list<Creature*>::const_iterator itr = healers.begin();
|
||||
std::advance(itr, urand(0, healers.size()-1));
|
||||
target = *itr;
|
||||
}
|
||||
|
||||
if (target)
|
||||
DoCast(target, SPELL_INSPIRE);
|
||||
DoCast(SelectRandomContainerElement(healers), SPELL_INSPIRE);
|
||||
|
||||
DoCast(me, SPELL_INSPIRE);
|
||||
events.ScheduleEvent(EVENT_INSPIRE, urand(20000, 26000));
|
||||
|
||||
@@ -480,9 +480,7 @@ class boss_blood_queen_lana_thel : public CreatureScript
|
||||
return tempTargets.front();
|
||||
}
|
||||
|
||||
std::list<Player*>::iterator itr = tempTargets.begin();
|
||||
std::advance(itr, urand(0, tempTargets.size() - 1));
|
||||
return *itr;
|
||||
return SelectRandomContainerElement(tempTargets);
|
||||
}
|
||||
|
||||
std::set<uint64> _vampires;
|
||||
|
||||
@@ -1189,9 +1189,7 @@ class spell_deathbringer_blood_nova_targeting : public SpellScriptLoader
|
||||
if (targetsAtRange < minTargets)
|
||||
targetsAtRange = std::min<uint32>(unitList.size() - 1, minTargets);
|
||||
|
||||
std::list<Unit*>::iterator itr = unitList.begin();
|
||||
std::advance(itr, urand(0, targetsAtRange));
|
||||
target = *itr;
|
||||
target = SelectRandomContainerElement(unitList);
|
||||
unitList.clear();
|
||||
unitList.push_back(target);
|
||||
}
|
||||
@@ -1241,9 +1239,7 @@ class spell_deathbringer_boiling_blood : public SpellScriptLoader
|
||||
if (unitList.empty())
|
||||
return;
|
||||
|
||||
std::list<Unit*>::iterator itr = unitList.begin();
|
||||
std::advance(itr, urand(0, unitList.size() - 1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(unitList);
|
||||
unitList.clear();
|
||||
unitList.push_back(target);
|
||||
}
|
||||
|
||||
@@ -605,10 +605,7 @@ class boss_lady_deathwhisper : public CreatureScript
|
||||
return;
|
||||
|
||||
// select random cultist
|
||||
std::list<Creature*>::iterator cultistItr = temp.begin();
|
||||
std::advance(cultistItr, urand(0, temp.size()-1));
|
||||
|
||||
Creature* cultist = *cultistItr;
|
||||
Creature* cultist = SelectRandomContainerElement(temp);
|
||||
DoCast(cultist, cultist->GetEntry() == NPC_CULT_FANATIC ? SPELL_DARK_TRANSFORMATION_T : SPELL_DARK_EMPOWERMENT_T, true);
|
||||
Talk(uint8(cultist->GetEntry() == NPC_CULT_FANATIC ? SAY_DARK_TRANSFORMATION : SAY_DARK_EMPOWERMENT));
|
||||
}
|
||||
|
||||
@@ -775,9 +775,7 @@ class spell_putricide_ooze_channel : public SpellScriptLoader
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<Unit*>::iterator itr = targetList.begin();
|
||||
std::advance(itr, urand(0, targetList.size() - 1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(targetList);
|
||||
targetList.clear();
|
||||
targetList.push_back(target);
|
||||
_target = target;
|
||||
|
||||
@@ -493,9 +493,7 @@ class spell_rotface_mutated_infection : public SpellScriptLoader
|
||||
if (targets.empty())
|
||||
return;
|
||||
|
||||
std::list<Unit*>::iterator itr = targets.begin();
|
||||
std::advance(itr, urand(0, targets.size() - 1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(targets);
|
||||
targets.clear();
|
||||
targets.push_back(target);
|
||||
_target = target;
|
||||
|
||||
@@ -1341,9 +1341,7 @@ class spell_frostwarden_handler_order_whelp : public SpellScriptLoader
|
||||
++itr;
|
||||
}
|
||||
|
||||
std::list<Unit*>::iterator itr = unitList.begin();
|
||||
std::advance(itr, urand(0, unitList.size()-1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(unitList);
|
||||
unitList.clear();
|
||||
unitList.push_back(target);
|
||||
}
|
||||
@@ -1357,9 +1355,7 @@ class spell_frostwarden_handler_order_whelp : public SpellScriptLoader
|
||||
if (Creature* creature = GetCaster()->ToCreature())
|
||||
unitList.remove_if(OrderWhelpTargetSelector(creature));
|
||||
|
||||
std::list<Creature*>::iterator itr = unitList.begin();
|
||||
std::advance(itr, urand(0, unitList.size()-1));
|
||||
(*itr)->CastSpell(GetHitUnit(), uint32(GetEffectValue()), true);
|
||||
SelectRandomContainerElement(unitList)->CastSpell(GetHitUnit(), uint32(GetEffectValue()), true);
|
||||
}
|
||||
|
||||
void Register()
|
||||
|
||||
@@ -1182,9 +1182,7 @@ class spell_dreamwalker_summoner : public SpellScriptLoader
|
||||
if (targets.empty())
|
||||
return;
|
||||
|
||||
std::list<Unit*>::iterator itr = targets.begin();
|
||||
std::advance(itr, urand(0, targets.size() - 1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(targets);
|
||||
targets.clear();
|
||||
targets.push_back(target);
|
||||
}
|
||||
|
||||
@@ -1361,9 +1361,7 @@ class npc_captain_arnath : public CreatureScript
|
||||
case EVENT_ARNATH_PW_SHIELD:
|
||||
{
|
||||
std::list<Creature*> targets = DoFindFriendlyMissingBuff(40.0f, SPELL_POWER_WORD_SHIELD);
|
||||
std::list<Creature*>::iterator itr = targets.begin();
|
||||
std::advance(itr, urand(0, targets.size() - 1));
|
||||
DoCast(*itr, SPELL_POWER_WORD_SHIELD);
|
||||
DoCast(SelectRandomContainerElement(targets), SPELL_POWER_WORD_SHIELD);
|
||||
Events.ScheduleEvent(EVENT_ARNATH_PW_SHIELD, urand(15000, 20000));
|
||||
break;
|
||||
}
|
||||
@@ -1817,9 +1815,7 @@ class spell_frost_giant_death_plague : public SpellScriptLoader
|
||||
unitList.remove_if(DeathPlagueTargetSelector(GetCaster()));
|
||||
if (!unitList.empty())
|
||||
{
|
||||
std::list<Unit*>::iterator itr = unitList.begin();
|
||||
std::advance(itr, urand(0, unitList.size()-1));
|
||||
Unit* target = *itr;
|
||||
Unit* target = SelectRandomContainerElement(unitList);
|
||||
unitList.clear();
|
||||
unitList.push_back(target);
|
||||
}
|
||||
|
||||
@@ -290,9 +290,7 @@ class boss_general_vezax : public CreatureScript
|
||||
if (size < playersMin)
|
||||
return NULL;
|
||||
|
||||
std::list<Player*>::const_iterator itr = PlayerList.begin();
|
||||
std::advance(itr, urand(0, size - 1));
|
||||
return *itr;
|
||||
return SelectRandomContainerElement(PlayerList);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
@@ -139,9 +139,7 @@ class boss_emalon : public CreatureScript
|
||||
case EVENT_OVERCHARGE:
|
||||
if (!summons.empty())
|
||||
{
|
||||
std::list<uint64>::const_iterator itr = summons.begin();
|
||||
std::advance(itr, urand(0, summons.size()-1));
|
||||
Creature* minion = Unit::GetCreature(*me, *itr);
|
||||
Creature* minion = Unit::GetCreature(*me, SelectRandomContainerElement(summons));
|
||||
if (minion && minion->isAlive())
|
||||
{
|
||||
minion->CastSpell(me, SPELL_OVERCHARGED, true);
|
||||
|
||||
@@ -817,9 +817,7 @@ public:
|
||||
}
|
||||
if (!UnitsWithMana.empty())
|
||||
{
|
||||
std::list<Unit*>::const_iterator it = UnitsWithMana.begin();
|
||||
std::advance(it, rand() % UnitsWithMana.size());
|
||||
DoCast(*it, SPELL_MANA_BURN);
|
||||
DoCast(SelectRandomContainerElement(UnitsWithMana), SPELL_MANA_BURN);
|
||||
ManaBurnTimer = 8000 + (rand() % 10 * 1000); // 8-18 sec cd
|
||||
}
|
||||
else
|
||||
|
||||
@@ -251,10 +251,9 @@ class spell_dru_t10_restoration_4p_bonus : public SpellScriptLoader
|
||||
return;
|
||||
}
|
||||
|
||||
std::list<Unit*>::const_iterator it2 = tempTargets.begin();
|
||||
std::advance(it2, urand(0, tempTargets.size() - 1));
|
||||
Unit* target = SelectRandomContainerElement(tempTargets);
|
||||
unitList.clear();
|
||||
unitList.push_back(*it2);
|
||||
unitList.push_back(target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -648,4 +648,13 @@ public:
|
||||
return (part[el]);
|
||||
};
|
||||
};
|
||||
|
||||
/* Select a random element from a container. Note: make sure you explicitly empty check the container */
|
||||
template <class C> typename C::value_type const& SelectRandomContainerElement(C const& container)
|
||||
{
|
||||
C::const_iterator it = container.begin();
|
||||
std::advance(it, urand(0, container.size() - 1));
|
||||
return *it;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user