From a5bc0d0d9b0007401f3ea535d56e2d8ba394598e Mon Sep 17 00:00:00 2001 From: hondacrx Date: Fri, 2 Feb 2024 14:39:02 -0500 Subject: [PATCH] Core/Random: Changed random functions returning doubles to return floats Port From (https://github.com/TrinityCore/TrinityCore/commit/9894f6b802c974bb36acd7fbb0d083455a1f0f1b) --- Source/Framework/Util/CollectionExtensions.cs | 2 +- Source/Framework/Util/RandomHelper.cs | 4 ++-- Source/Game/Entities/Object/WorldObject.cs | 10 +++++----- Source/Game/Entities/Unit/Unit.cs | 2 +- Source/Game/Spells/Spell.cs | 10 +++++----- Source/Game/Spells/SpellEffects.cs | 2 +- Source/Game/Spells/SpellInfo.cs | 6 +++--- Source/Game/Weather/WeatherManager.cs | 6 +++--- Source/Scripts/Events/Midsummer.cs | 4 ++-- Source/Scripts/Events/ZalazaneFall.cs | 2 +- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Source/Framework/Util/CollectionExtensions.cs b/Source/Framework/Util/CollectionExtensions.cs index 9b855adf5..5cfd81772 100644 --- a/Source/Framework/Util/CollectionExtensions.cs +++ b/Source/Framework/Util/CollectionExtensions.cs @@ -158,7 +158,7 @@ namespace System.Collections.Generic { float totalWeight = sequence.Sum(weightSelector); // The weight we are after... - float itemWeightIndex = (float)RandomHelper.NextDouble() * totalWeight; + float itemWeightIndex = RandomHelper.NextSingle() * totalWeight; float currentWeightIndex = 0; foreach (var item in from weightedItem in sequence select new { Value = weightedItem, Weight = weightSelector(weightedItem) }) diff --git a/Source/Framework/Util/RandomHelper.cs b/Source/Framework/Util/RandomHelper.cs index 2881de035..5e4c13e96 100644 --- a/Source/Framework/Util/RandomHelper.cs +++ b/Source/Framework/Util/RandomHelper.cs @@ -16,9 +16,9 @@ public class RandomHelper /// Returns a random number between 0.0 and 1.0. /// /// - public static double NextDouble() + public static float NextSingle() { - return rand.NextDouble(); + return rand.NextSingle(); } /// diff --git a/Source/Game/Entities/Object/WorldObject.cs b/Source/Game/Entities/Object/WorldObject.cs index b99d850a2..bdb4f1ee4 100644 --- a/Source/Game/Entities/Object/WorldObject.cs +++ b/Source/Game/Entities/Object/WorldObject.cs @@ -3389,12 +3389,12 @@ namespace Game.Entities } // angle to face `obj` to `this` - float angle = (float)RandomHelper.NextDouble() * (2 * MathFunctions.PI); - float new_dist = (float)RandomHelper.NextDouble() + (float)RandomHelper.NextDouble(); + float angle = RandomHelper.NextSingle() * (2 * MathFunctions.PI); + float new_dist = RandomHelper.NextSingle() + RandomHelper.NextSingle(); new_dist = distance * (new_dist > 1 ? new_dist - 2 : new_dist); - rand_x = (float)(pos.posX + new_dist * Math.Cos(angle)); - rand_y = (float)(pos.posY + new_dist * Math.Sin(angle)); + rand_x = (pos.posX + new_dist * MathF.Cos(angle)); + rand_y = (pos.posY + new_dist * MathF.Sin(angle)); rand_z = pos.posZ; GridDefines.NormalizeMapCoord(ref rand_x); @@ -3570,7 +3570,7 @@ namespace Game.Entities public Position GetRandomNearPosition(float radius) { var pos = GetPosition(); - MovePosition(pos, radius * (float)RandomHelper.NextDouble(), (float)RandomHelper.NextDouble() * MathFunctions.PI * 2); + MovePosition(pos, radius * RandomHelper.NextSingle(), RandomHelper.NextSingle() * MathFunctions.PI * 2); return pos; } diff --git a/Source/Game/Entities/Unit/Unit.cs b/Source/Game/Entities/Unit/Unit.cs index fd60ba294..024975e8f 100644 --- a/Source/Game/Entities/Unit/Unit.cs +++ b/Source/Game/Entities/Unit/Unit.cs @@ -3410,7 +3410,7 @@ namespace Game.Entities discreteResistProbability[i] = Math.Max(0.5f - 2.5f * Math.Abs(0.1f * i - averageResist), 0.0f); } - float roll = (float)RandomHelper.NextDouble(); + float roll = RandomHelper.NextSingle(); float probabilitySum = 0.0f; uint resistance = 0; diff --git a/Source/Game/Spells/Spell.cs b/Source/Game/Spells/Spell.cs index 64093a733..bc1fcd003 100644 --- a/Source/Game/Spells/Spell.cs +++ b/Source/Game/Spells/Spell.cs @@ -995,9 +995,9 @@ namespace Game.Spells { float minDist = m_spellInfo.GetMinRange(true); float maxDist = m_spellInfo.GetMaxRange(true); - float dis = (float)RandomHelper.NextDouble() * (maxDist - minDist) + minDist; + float dis = RandomHelper.NextSingle() * (maxDist - minDist) + minDist; float x, y, z; - float angle = (float)RandomHelper.NextDouble() * (MathFunctions.PI * 35.0f / 180.0f) - (float)(Math.PI * 17.5f / 180.0f); + float angle = RandomHelper.NextSingle() * (MathF.PI * 35.0f / 180.0f) - (MathF.PI * 17.5f / 180.0f); m_caster.GetClosePoint(out x, out y, out z, SharedConst.DefaultPlayerBoundingRadius, dis, angle); float ground = m_caster.GetMapHeight(x, y, z); @@ -1114,7 +1114,7 @@ namespace Game.Spells break; case Targets.DestCasterRandom: if (dist > objSize) - dist = objSize + (dist - objSize) * (float)RandomHelper.NextDouble(); + dist = objSize + (dist - objSize) * RandomHelper.NextSingle(); break; case Targets.DestCasterFrontLeft: case Targets.DestCasterBackLeft: @@ -1165,7 +1165,7 @@ namespace Game.Spells float angle = targetType.CalcDirectionAngle(); float dist = spellEffectInfo.CalcRadius(null, targetIndex); if (targetType.GetTarget() == Targets.DestRandom) - dist *= (float)RandomHelper.NextDouble(); + dist *= RandomHelper.NextSingle(); Position pos = new(dest.Position); target.MovePositionToFirstCollision(pos, dist, angle); @@ -1219,7 +1219,7 @@ namespace Game.Spells float angle = targetType.CalcDirectionAngle(); float dist = spellEffectInfo.CalcRadius(m_caster, targetIndex); if (targetType.GetTarget() == Targets.DestRandom) - dist *= (float)RandomHelper.NextDouble(); + dist *= RandomHelper.NextSingle(); Position pos = new(m_targets.GetDstPos()); m_caster.MovePositionToFirstCollision(pos, dist, angle); diff --git a/Source/Game/Spells/SpellEffects.cs b/Source/Game/Spells/SpellEffects.cs index 8c917ff48..2d0364a31 100644 --- a/Source/Game/Spells/SpellEffects.cs +++ b/Source/Game/Spells/SpellEffects.cs @@ -4129,7 +4129,7 @@ namespace Game.Spells //GO is always friendly to it's creator, get range for friends float min_dis = m_spellInfo.GetMinRange(true); float max_dis = m_spellInfo.GetMaxRange(true); - float dis = (float)RandomHelper.NextDouble() * (max_dis - min_dis) + min_dis; + float dis = RandomHelper.NextSingle() * (max_dis - min_dis) + min_dis; unitCaster.GetClosePoint(out fx, out fy, out fz, SharedConst.DefaultPlayerBoundingRadius, dis); fo = unitCaster.GetOrientation(); diff --git a/Source/Game/Spells/SpellInfo.cs b/Source/Game/Spells/SpellInfo.cs index 00cddda1c..48a8196b2 100644 --- a/Source/Game/Spells/SpellInfo.cs +++ b/Source/Game/Spells/SpellInfo.cs @@ -28,7 +28,7 @@ namespace Game.Spells continue; _effects.EnsureWritableListIndex((uint)spellEffect.EffectIndex, new SpellEffectInfo(this)); - _effects[(int)spellEffect.EffectIndex] = new SpellEffectInfo(this, spellEffect); + _effects[spellEffect.EffectIndex] = new SpellEffectInfo(this, spellEffect); } // Correct EffectIndex for blank effects @@ -1007,7 +1007,7 @@ namespace Game.Spells if (HasAttribute(SpellAttr8.OnlyTargetIfSameCreator)) { - ObjectGuid getCreatorOrSelf(WorldObject obj) + var getCreatorOrSelf = (WorldObject obj) => { ObjectGuid creator = obj.GetCreatorGUID(); if (creator.IsEmpty()) @@ -5005,7 +5005,7 @@ namespace Game.Spells case SpellTargetDirectionTypes.FrontLeft: return pi / 4; case SpellTargetDirectionTypes.Random: - return (float)RandomHelper.NextDouble() * (2 * pi); + return RandomHelper.NextSingle() * (2 * pi); default: return 0.0f; } diff --git a/Source/Game/Weather/WeatherManager.cs b/Source/Game/Weather/WeatherManager.cs index e62d71c11..6f7a64a55 100644 --- a/Source/Game/Weather/WeatherManager.cs +++ b/Source/Game/Weather/WeatherManager.cs @@ -217,16 +217,16 @@ namespace Game } else if (u < 90) { - m_intensity = (float)RandomHelper.NextDouble() * 0.3333f; + m_intensity = RandomHelper.NextSingle() * 0.3333f; } else { // Severe change, but how severe? rn = RandomHelper.URand(0, 99); if (rn < 50) - m_intensity = (float)RandomHelper.NextDouble() * 0.3333f + 0.3334f; + m_intensity = RandomHelper.NextSingle() * 0.3333f + 0.3334f; else - m_intensity = (float)RandomHelper.NextDouble() * 0.3333f + 0.6667f; + m_intensity = RandomHelper.NextSingle() * 0.3333f + 0.6667f; } // return true only in case weather changes diff --git a/Source/Scripts/Events/Midsummer.cs b/Source/Scripts/Events/Midsummer.cs index fd37d30e3..c0b53e2c5 100644 --- a/Source/Scripts/Events/Midsummer.cs +++ b/Source/Scripts/Events/Midsummer.cs @@ -276,7 +276,7 @@ namespace Scripts.Events void HandleDummy(uint effIndex) { - Position dest = GetCaster().GetFirstCollisionPosition(30.0f, (float)RandomHelper.NextDouble() * (float)(2 * MathF.PI)); + Position dest = GetCaster().GetFirstCollisionPosition(30.0f, RandomHelper.NextSingle() * (float)(2 * MathF.PI)); GetCaster().CastSpell(dest, FlingTorch.SpellFlingTorchTriggered, true); GetCaster().CastSpell(dest, FlingTorch.SpellFlingTorchShadow); } @@ -355,7 +355,7 @@ namespace Scripts.Events } else { - Position dest = player.GetFirstCollisionPosition(15.0f, (float)RandomHelper.NextDouble() * (float)(2 * MathF.PI)); + Position dest = player.GetFirstCollisionPosition(15.0f, RandomHelper.NextSingle() * (2 * MathF.PI)); player.CastSpell(player, FlingTorch.SpellTorchesCaught); player.CastSpell(dest, FlingTorch.SpellFlingTorchTriggered, true); player.CastSpell(dest, FlingTorch.SpellFlingTorchShadow); diff --git a/Source/Scripts/Events/ZalazaneFall.cs b/Source/Scripts/Events/ZalazaneFall.cs index 28e85cdf9..71e06b0de 100644 --- a/Source/Scripts/Events/ZalazaneFall.cs +++ b/Source/Scripts/Events/ZalazaneFall.cs @@ -261,7 +261,7 @@ namespace Scripts.Events.ZalazaneFall me.SetDisplayId(trollmodel[RandomHelper.URand(0, 39)]); Player player = me.GetOwner().ToPlayer(); if (player != null) - me.GetMotionMaster().MoveFollow(player, 5.0f, (float)(RandomHelper.NextDouble() + 1.0f) * (float)(MathF.PI) / 3.0f * 4.0f); + me.GetMotionMaster().MoveFollow(player, 5.0f, (RandomHelper.NextSingle() + 1.0f) * (MathF.PI) / 3.0f * 4.0f); } public override void Reset()