Renamed PickRandom to SelectRandom

This commit is contained in:
hondacrx
2017-06-21 11:57:04 -04:00
parent d16d6d57f0
commit 430507a10a
28 changed files with 54 additions and 53 deletions
+11 -10
View File
@@ -80,16 +80,6 @@ namespace System.Collections.Generic
collection.RemoveAll(check.Invoke); collection.RemoveAll(check.Invoke);
} }
public static T PickRandom<T>(this IEnumerable<T> source)
{
return source.PickRandom(1).Single();
}
public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, uint count)
{
return source.Shuffle().Take((int)count);
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{ {
return source.OrderBy(x => Guid.NewGuid()); return source.OrderBy(x => Guid.NewGuid());
@@ -136,6 +126,17 @@ namespace System.Collections.Generic
list = listCopy; list = listCopy;
} }
public static T SelectRandom<T>(this IEnumerable<T> source)
{
return source.SelectRandom(1).Single();
}
public static IEnumerable<T> SelectRandom<T>(this IEnumerable<T> source, uint count)
{
return source.Shuffle().Take((int)count);
}
public static T SelectRandomElementByWeight<T>(this IEnumerable<T> sequence, Func<T, float> weightSelector) public static T SelectRandomElementByWeight<T>(this IEnumerable<T> sequence, Func<T, float> weightSelector)
{ {
float totalWeight = sequence.Sum(weightSelector); float totalWeight = sequence.Sum(weightSelector);
+2 -2
View File
@@ -159,7 +159,7 @@ namespace Game.AI
} }
case SelectAggroTarget.Random: case SelectAggroTarget.Random:
{ {
return targetList.PickRandom(); return targetList.SelectRandom();
} }
default: default:
break; break;
@@ -197,7 +197,7 @@ namespace Game.AI
targetList.Reverse(); targetList.Reverse();
if (targetType == SelectAggroTarget.Random) if (targetType == SelectAggroTarget.Random)
targetList = targetList.PickRandom(maxTargets).ToList(); targetList = targetList.SelectRandom(maxTargets).ToList();
else else
targetList.Resize(maxTargets); targetList.Resize(maxTargets);
+1 -1
View File
@@ -2302,7 +2302,7 @@ namespace Game.AI
{ {
if (IsUnit(obj)) if (IsUnit(obj))
{ {
uint sound = sounds.PickRandom(); uint sound = sounds.SelectRandom();
obj.PlayDirectSound(sound, onlySelf ? obj.ToPlayer() : null); obj.PlayDirectSound(sound, onlySelf ? obj.ToPlayer() : null);
Log.outDebug(LogFilter.ScriptsAi, "SmartScript.ProcessAction:: SMART_ACTION_RANDOM_SOUND: target: {0} ({1}), sound: {2}, onlyself: {3}", Log.outDebug(LogFilter.ScriptsAi, "SmartScript.ProcessAction:: SMART_ACTION_RANDOM_SOUND: target: {0} ({1}), sound: {2}, onlyself: {3}",
obj.GetName(), obj.GetGUID().ToString(), sound, onlySelf); obj.GetName(), obj.GetGUID().ToString(), sound, onlySelf);
+2 -2
View File
@@ -129,7 +129,7 @@ namespace Game.BattlePets
if (list.Empty()) if (list.Empty())
return 3; // default B/B return 3; // default B/B
return list.PickRandom(); return list.SelectRandom();
} }
public static byte GetDefaultPetQuality(uint species) public static byte GetDefaultPetQuality(uint species)
@@ -263,7 +263,7 @@ namespace Game.BattlePets
byte quality = 0; byte quality = 0;
if (_availableBreedsPerSpecies.ContainsKey(species)) if (_availableBreedsPerSpecies.ContainsKey(species))
breed = _availableBreedsPerSpecies[species].PickRandom(); breed = _availableBreedsPerSpecies[species].SelectRandom();
if (_defaultQualityPerSpecies.ContainsKey(species)) if (_defaultQualityPerSpecies.ContainsKey(species))
quality = _defaultQualityPerSpecies[species]; quality = _defaultQualityPerSpecies[species];
+1 -1
View File
@@ -963,7 +963,7 @@ namespace Game.DataStorage
if (listNameGen[gender].Empty()) if (listNameGen[gender].Empty())
return ""; return "";
LocalizedString data = listNameGen[gender].PickRandom().Name; LocalizedString data = listNameGen[gender].SelectRandom().Name;
if (data.HasString(locale)) if (data.HasString(locale))
return data[locale]; return data[locale];
+1 -1
View File
@@ -526,7 +526,7 @@ namespace Game.DungeonFinding
proposal.cancelTime = Time.UnixTime + SharedConst.LFGTimeProposal; proposal.cancelTime = Time.UnixTime + SharedConst.LFGTimeProposal;
proposal.state = LfgProposalState.Initiating; proposal.state = LfgProposalState.Initiating;
proposal.leader = ObjectGuid.Empty; proposal.leader = ObjectGuid.Empty;
proposal.dungeonId = proposalDungeons.PickRandom(); proposal.dungeonId = proposalDungeons.SelectRandom();
bool leader = false; bool leader = false;
foreach (var rolePair in proposalRoles) foreach (var rolePair in proposalRoles)
+1 -1
View File
@@ -1613,7 +1613,7 @@ namespace Game.Entities
for (uint i = 0; i < info.Ritual.casterTargetSpellTargets; i++) for (uint i = 0; i < info.Ritual.casterTargetSpellTargets; i++)
{ {
// m_unique_users can contain only player GUIDs // m_unique_users can contain only player GUIDs
Player target = Global.ObjAccessor.GetPlayer(this, m_unique_users.PickRandom()); Player target = Global.ObjAccessor.GetPlayer(this, m_unique_users.SelectRandom());
if (target != null) if (target != null)
spellCaster.CastSpell(target, info.Ritual.casterTargetSpell, true); spellCaster.CastSpell(target, info.Ritual.casterTargetSpell, true);
} }
+1 -1
View File
@@ -800,7 +800,7 @@ namespace Game.Entities
return null; return null;
// select random // select random
return targets.PickRandom(); return targets.SelectRandom();
} }
public void EnterVehicle(Unit Base, sbyte seatId = -1) public void EnterVehicle(Unit Base, sbyte seatId = -1)
+1 -1
View File
@@ -204,7 +204,7 @@ namespace Game
}).ToList(); }).ToList();
if (!usableDisplays.Empty()) if (!usableDisplays.Empty())
preferredMountDisplay = usableDisplays.PickRandom().DisplayID; preferredMountDisplay = usableDisplays.SelectRandom().DisplayID;
} }
} }
} }
+1 -1
View File
@@ -1063,7 +1063,7 @@ namespace Game.Loots
possibleLoot = EqualChanced; possibleLoot = EqualChanced;
possibleLoot.RemoveAll(new LootGroupInvalidSelector(loot, lootMode).Check); possibleLoot.RemoveAll(new LootGroupInvalidSelector(loot, lootMode).Check);
if (!possibleLoot.Empty()) // If nothing selected yet - an item is taken from equal-chanced part if (!possibleLoot.Empty()) // If nothing selected yet - an item is taken from equal-chanced part
return possibleLoot.PickRandom(); return possibleLoot.SelectRandom();
return null; // Empty drop from the group return null; // Empty drop from the group
} }
+2 -2
View File
@@ -822,7 +822,7 @@ namespace Game
{ {
do do
{ {
ulong questId = currentQuests.PickRandom(); ulong questId = currentQuests.SelectRandom();
newQuests.Add(questId); newQuests.Add(questId);
currentQuests.Remove(questId); currentQuests.Remove(questId);
} while (newQuests.Count < limit && !currentQuests.Empty()); // failsafe } while (newQuests.Count < limit && !currentQuests.Empty()); // failsafe
@@ -834,7 +834,7 @@ namespace Game
// activate <limit> random quests // activate <limit> random quests
do do
{ {
ulong questId = newQuests.PickRandom(); ulong questId = newQuests.SelectRandom();
spawns.ActivateObject<Quest>(questId, poolId); spawns.ActivateObject<Quest>(questId, poolId);
PoolObject tempObj = new PoolObject(questId, 0.0f); PoolObject tempObj = new PoolObject(questId, 0.0f);
Spawn1Object(tempObj); Spawn1Object(tempObj);
+1 -1
View File
@@ -2110,7 +2110,7 @@ namespace Game.Spells
}).ToList(); }).ToList();
if (!usableDisplays.Empty()) if (!usableDisplays.Empty())
displayId = usableDisplays.PickRandom().DisplayID; displayId = usableDisplays.SelectRandom().DisplayID;
} }
// TODO: CREATE TABLE mount_vehicle (mountId, vehicleCreatureId) for future mounts that are vehicles (new mounts no longer have proper data in MiscValue) // TODO: CREATE TABLE mount_vehicle (mountId, vehicleCreatureId) for future mounts that are vehicles (new mounts no longer have proper data in MiscValue)
//if (MountVehicle const* mountVehicle = sObjectMgr->GetMountVehicle(mountEntry->Id)) //if (MountVehicle const* mountVehicle = sObjectMgr->GetMountVehicle(mountEntry->Id))
+2 -2
View File
@@ -294,7 +294,7 @@ namespace Game.Spells
int maxTargets = Math.Min(3, attackers.Count); int maxTargets = Math.Min(3, attackers.Count);
for (uint i = 0; i < maxTargets; ++i) for (uint i = 0; i < maxTargets; ++i)
{ {
Unit attacker = attackers.PickRandom(); Unit attacker = attackers.SelectRandom();
AddUnitTarget(attacker, 1 << 1); AddUnitTarget(attacker, 1 << 1);
attackers.Remove(attacker); attackers.Remove(attacker);
} }
@@ -1408,7 +1408,7 @@ namespace Game.Spells
if (!avalibleElixirs.Empty()) if (!avalibleElixirs.Empty())
{ {
// cast random elixir on target // cast random elixir on target
m_caster.CastSpell(unitTarget, (uint)avalibleElixirs.PickRandom(), true, m_CastItem); m_caster.CastSpell(unitTarget, (uint)avalibleElixirs.SelectRandom(), true, m_CastItem);
} }
} }
} }
@@ -158,7 +158,7 @@ namespace Scripts.EasternKingdoms.Karazhan.Midnight
} }
Unit target = null; Unit target = null;
if (!target_list.Empty()) if (!target_list.Empty())
target = target_list.PickRandom(); target = target_list.SelectRandom();
DoCast(target, SpellIds.BerserkerCharge); DoCast(target, SpellIds.BerserkerCharge);
ChargeTimer = 20000; ChargeTimer = 20000;
@@ -210,7 +210,7 @@ namespace Scripts.Northrend.AzjolNerub.Ahnkahet
if (vInitiands.Empty()) if (vInitiands.Empty())
return ObjectGuid.Empty; return ObjectGuid.Empty;
return vInitiands.PickRandom(); return vInitiands.SelectRandom();
} }
case DataTypes.AddJedogaVictim: case DataTypes.AddJedogaVictim:
return JedogaSacrifices; return JedogaSacrifices;
@@ -205,7 +205,7 @@ namespace Scripts.Northrend.AzjolNerub.AzjolNerub.Anubarak
me.GetCreatureListWithEntryInGrid(triggers, CreatureIds.WorldTrigger); me.GetCreatureListWithEntryInGrid(triggers, CreatureIds.WorldTrigger);
if (!triggers.Empty()) if (!triggers.Empty())
{ {
var it = triggers.PickRandom(); var it = triggers.SelectRandom();
it.CastSpell(it, SpellIds.SummonDarter, true); it.CastSpell(it, SpellIds.SummonDarter, true);
_events.Repeat(TimeSpan.FromSeconds(11)); _events.Repeat(TimeSpan.FromSeconds(11));
} }
@@ -889,7 +889,7 @@ namespace Scripts.Northrend.AzjolNerub.AzjolNerub.KrikthirTheGatewatcher
if (!targetList.Empty()) if (!targetList.Empty())
{ {
// If there are, pick one of them at random // If there are, pick one of them at random
target = targetList.PickRandom(); target = targetList.SelectRandom();
} }
// And hit only that one // And hit only that one
targetList.Clear(); targetList.Clear();
@@ -543,7 +543,7 @@ namespace Scripts.Northrend.CrusadersColiseum.TrialOfTheCrusader
if (targets.Empty()) if (targets.Empty())
return; return;
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
+1 -1
View File
@@ -162,7 +162,7 @@ namespace Scripts.Northrend
if (PlayerInDalaranList.Empty()) if (PlayerInDalaranList.Empty())
return null; return null;
return PlayerInDalaranList.PickRandom(); return PlayerInDalaranList.SelectRandom();
} }
void SendMailToPlayer(Player player) void SendMailToPlayer(Player player)
@@ -2188,7 +2188,7 @@ namespace Scripts.Northrend.IcecrownCitadel
if (!targets.Empty()) if (!targets.Empty())
{ {
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
@@ -2250,7 +2250,7 @@ namespace Scripts.Northrend.IcecrownCitadel
{ {
if (!targets.Empty()) if (!targets.Empty())
{ {
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
@@ -1154,7 +1154,7 @@ namespace Scripts.Northrend.IcecrownCitadel
case EventTypes.ArnathPwShield: case EventTypes.ArnathPwShield:
{ {
List<Creature> targets = DoFindFriendlyMissingBuff(40.0f, InstanceSpells.SpellPowerWordShield(IsUndead)); List<Creature> targets = DoFindFriendlyMissingBuff(40.0f, InstanceSpells.SpellPowerWordShield(IsUndead));
DoCast(targets.PickRandom(), InstanceSpells.SpellPowerWordShield(IsUndead)); DoCast(targets.SelectRandom(), InstanceSpells.SpellPowerWordShield(IsUndead));
_events.ScheduleEvent(EventTypes.ArnathPwShield, RandomHelper.URand(15000, 20000)); _events.ScheduleEvent(EventTypes.ArnathPwShield, RandomHelper.URand(15000, 20000));
break; break;
} }
@@ -1656,7 +1656,7 @@ namespace Scripts.Northrend.IcecrownCitadel
if (!targets.Empty()) if (!targets.Empty())
{ {
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
@@ -1737,7 +1737,7 @@ namespace Scripts.Northrend.IcecrownCitadel
return true; return true;
}); });
targets = targets.PickRandom(2).ToList(); targets = targets.SelectRandom(2).ToList();
} }
void Land(uint effIndex) void Land(uint effIndex)
@@ -588,7 +588,7 @@ namespace Scripts.Northrend.IcecrownCitadel
if (temp.Empty()) if (temp.Empty())
return; return;
Creature cultist = temp.PickRandom(); Creature cultist = temp.SelectRandom();
DoCast(cultist, LadySpells.DARK_MARTYRDOM_T, true); DoCast(cultist, LadySpells.DARK_MARTYRDOM_T, true);
} }
@@ -611,7 +611,7 @@ namespace Scripts.Northrend.IcecrownCitadel
return; return;
// select random cultist // select random cultist
Creature cultist = temp.PickRandom(); Creature cultist = temp.SelectRandom();
DoCast(cultist, cultist.GetEntry() == CreatureIds.CultFanatic ? LadySpells.DARK_TRANSFORMATION_T : LadySpells.DARK_EMPOWERMENT_T, true); DoCast(cultist, cultist.GetEntry() == CreatureIds.CultFanatic ? LadySpells.DARK_TRANSFORMATION_T : LadySpells.DARK_EMPOWERMENT_T, true);
Talk(cultist.GetEntry() == CreatureIds.CultFanatic ? LadyTexts.SAY_DARK_TRANSFORMATION : LadyTexts.SAY_DARK_EMPOWERMENT); Talk(cultist.GetEntry() == CreatureIds.CultFanatic ? LadyTexts.SAY_DARK_TRANSFORMATION : LadyTexts.SAY_DARK_EMPOWERMENT);
} }
+1 -1
View File
@@ -1639,7 +1639,7 @@ namespace Scripts.Northrend.Ulduar
else else
{ {
//! In the end, only one target should be selected //! In the end, only one target should be selected
_target = targets.PickRandom(); _target = targets.SelectRandom();
FilterTargetsSubsequently(targets); FilterTargetsSubsequently(targets);
} }
} }
+6 -6
View File
@@ -1829,7 +1829,7 @@ namespace Scripts.Northrend.Ulduar
if (_noTarget) if (_noTarget)
return; return;
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
@@ -1964,12 +1964,12 @@ namespace Scripts.Northrend.Ulduar
if (targets.Empty()) if (targets.Empty())
return; return;
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.RemoveAll(new AllWorldObjectsInRange(GetCaster(), 15.0f).Invoke); targets.RemoveAll(new AllWorldObjectsInRange(GetCaster(), 15.0f).Invoke);
if (!targets.Empty()) if (!targets.Empty())
target = targets.PickRandom(); target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
@@ -2289,12 +2289,12 @@ namespace Scripts.Northrend.Ulduar
if (targets.Empty()) if (targets.Empty())
return; return;
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.RemoveAll(new AllWorldObjectsInRange(GetCaster(), 15.0f).Invoke); targets.RemoveAll(new AllWorldObjectsInRange(GetCaster(), 15.0f).Invoke);
if (!targets.Empty()) if (!targets.Empty())
target = targets.PickRandom(); target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
@@ -2567,7 +2567,7 @@ namespace Scripts.Northrend.Ulduar
if (targets.Empty()) if (targets.Empty())
return; return;
WorldObject target = targets.PickRandom(); WorldObject target = targets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
+1 -1
View File
@@ -1040,7 +1040,7 @@ namespace Scripts.Spells.Druid
return; return;
} }
Unit target = tempTargets.PickRandom(); Unit target = tempTargets.SelectRandom();
targets.Clear(); targets.Clear();
targets.Add(target); targets.Add(target);
} }
+4 -4
View File
@@ -734,7 +734,7 @@ namespace Scripts.Spells.Items
PreventDefaultAction(); PreventDefaultAction();
Unit caster = eventInfo.GetActor(); Unit caster = eventInfo.GetActor();
uint spellId = triggeredSpells[(int)caster.GetClass()].PickRandom(); uint spellId = triggeredSpells[(int)caster.GetClass()].SelectRandom();
caster.CastSpell(caster, spellId, true); caster.CastSpell(caster, spellId, true);
if (RandomHelper.randChance(10)) if (RandomHelper.randChance(10))
@@ -960,7 +960,7 @@ namespace Scripts.Spells.Items
if (randomSpells.Empty()) if (randomSpells.Empty())
return; return;
uint spellId = randomSpells.PickRandom(); uint spellId = randomSpells.SelectRandom();
caster.CastSpell(caster, spellId, true); caster.CastSpell(caster, spellId, true);
} }
@@ -1293,7 +1293,7 @@ namespace Scripts.Spells.Items
return; return;
} }
caster.CastSpell(caster, possibleSpells.PickRandom(), true); caster.CastSpell(caster, possibleSpells.SelectRandom(), true);
} }
public override void Register() public override void Register()
@@ -2357,7 +2357,7 @@ namespace Scripts.Spells.Items
void HandleTeleport(uint effIndex) void HandleTeleport(uint effIndex)
{ {
PreventHitDefaultEffect(effIndex); PreventHitDefaultEffect(effIndex);
uint spellId = SpellIds.WormholeTargetLocations.PickRandom(); uint spellId = SpellIds.WormholeTargetLocations.SelectRandom();
GetCaster().CastSpell(GetHitUnit(), spellId, true); GetCaster().CastSpell(GetHitUnit(), spellId, true);
} }
+1 -1
View File
@@ -94,7 +94,7 @@ namespace Scripts.Spells.Rogue
{ {
while (!_targets.Empty()) while (!_targets.Empty())
{ {
ObjectGuid guid = _targets.PickRandom(); ObjectGuid guid = _targets.SelectRandom();
Unit target = Global.ObjAccessor.GetUnit(GetTarget(), guid); Unit target = Global.ObjAccessor.GetUnit(GetTarget(), guid);
if (target) if (target)
{ {
+1 -1
View File
@@ -769,7 +769,7 @@ namespace Scripts.World.NpcSpecial
if (!targets.Empty()) if (!targets.Empty())
{ {
_lastTargetGUID = targets.PickRandom().GetGUID(); _lastTargetGUID = targets.SelectRandom().GetGUID();
return _lastTargetGUID; return _lastTargetGUID;
} }