Core/Random: Changed random functions returning doubles to return floats

* They were all cast to float at use anyway
* Improves roll_chance_f performance (rand32() is now called internally by uniform_real_distribution once instead of twice)
This commit is contained in:
Shauren authored and Ovahlord committed 2023-10-31 22:00:52 +01:00
1 parent 56dc3bddd5
commit 7d57ef7299
13 files changed
+26 -26

No files matched your search

+4 -4
View File
@@ -72,15 +72,15 @@ uint32 rand32()
return GetRng()->RandomUInt32();
}
double rand_norm()
float rand_norm()
{
std::uniform_real_distribution<double> urd;
std::uniform_real_distribution<float> urd;
return urd(engine);
}
double rand_chance()
float rand_chance()
{
std::uniform_real_distribution<double> urd(0.0, 100.0);
std::uniform_real_distribution<float> urd(0.0, 100.0);
return urd(engine);
}
+4 -4
View File
@@ -41,11 +41,11 @@ TC_COMMON_API Milliseconds randtime(Milliseconds min, Milliseconds max);
/* Return a random number in the range min..max */
TC_COMMON_API float frand(float min, float max);
/* Return a random double from 0.0 to 1.0 (exclusive). */
TC_COMMON_API double rand_norm();
/* Return a random float from 0.0 to 1.0 (exclusive). */
TC_COMMON_API float rand_norm();
/* Return a random double from 0.0 to 100.0 (exclusive). */
TC_COMMON_API double rand_chance();
/* Return a random float from 0.0 to 100.0 (exclusive). */
TC_COMMON_API float rand_chance();
/* Return a random number in the range 0..count (exclusive) with each value having a different chance of happening */
TC_COMMON_API uint32 urandweighted(size_t count, double const* chances);
+3 -3
View File
@@ -1549,8 +1549,8 @@ void WorldObject::GetRandomPoint(const Position &pos, float distance, float &ran
}
// angle to face `obj` to `this`
float angle = (float)rand_norm()*static_cast<float>(2*M_PI);
float new_dist = (float)rand_norm() + (float)rand_norm();
float angle = rand_norm() * static_cast<float>(2 * M_PI);
float new_dist = rand_norm() + rand_norm();
new_dist = distance * (new_dist > 1 ? new_dist - 2 : new_dist);
rand_x = pos.m_positionX + new_dist * std::cos(angle);
@@ -3369,7 +3369,7 @@ Position WorldObject::GetFirstCollisionPosition(float dist, float angle)
Position WorldObject::GetRandomNearPosition(float radius)
{
Position pos = GetPosition();
MovePosition(pos, radius * (float)rand_norm(), (float)rand_norm() * static_cast<float>(2 * M_PI));
MovePosition(pos, radius * rand_norm(), rand_norm() * static_cast<float>(2 * M_PI));
return pos;
}
+1 -1
View File
@@ -1591,7 +1591,7 @@ static float GetArmorReduction(float armor, uint8 attackerLevel)
discreteResistProbability[i] = std::max(0.5f - 2.5f * std::fabs(0.1f * i - averageResist), 0.0f);
}
float roll = float(rand_norm());
float roll = rand_norm();
float probabilitySum = 0.0f;
uint32 resistance = 0;
+1 -1
View File
@@ -399,7 +399,7 @@ LootStoreItem const* LootTemplate::LootGroup::Roll(Loot& loot, uint16 lootMode)
if (!possibleLoot.empty()) // First explicitly chanced entries are checked
{
float roll = (float)rand_chance();
float roll = rand_chance();
for (LootStoreItemList::const_iterator itr = possibleLoot.begin(); itr != possibleLoot.end(); ++itr) // check each explicitly chanced entry in the template and modify its chance based on quality.
{
+1 -1
View File
@@ -300,7 +300,7 @@ void PoolGroup<T>::SpawnObject(SpawnedPoolData& spawns, uint32 limit, uint32 tri
// roll objects to be spawned
if (!ExplicitlyChanced.empty())
{
float roll = (float)rand_chance();
float roll = rand_chance();
for (PoolObject& obj : ExplicitlyChanced)
{
+1 -1
View File
@@ -173,7 +173,7 @@ uint32 GetExplicitDiscoverySpell(uint32 spellId, Player* player)
full_chance += item_iter->chance;
float rate = full_chance / 100.0f;
float roll = (float)rand_chance() * rate; // roll now in range 0..full_chance
float roll = rand_chance() * rate; // roll now in range 0..full_chance
for (SkillDiscoveryList::const_iterator item_iter = tab->second.begin(); item_iter != tab->second.end(); ++item_iter)
{
+4 -4
View File
@@ -1485,7 +1485,7 @@ void Spell::SelectImplicitCasterDestTargets(SpellEffIndex effIndex, SpellImplici
float maxDist = m_spellInfo->GetMaxRange(true);
float dist = frand(minDist, maxDist);
float x, y, z;
float angle = float(rand_norm()) * static_cast<float>(M_PI * 35.0f / 180.0f) - static_cast<float>(M_PI * 17.5f / 180.0f);
float angle = rand_norm() * static_cast<float>(M_PI * 35.0f / 180.0f) - static_cast<float>(M_PI * 17.5f / 180.0f);
m_caster->GetClosePoint(x, y, z, DEFAULT_PLAYER_BOUNDING_RADIUS, dist, angle);
float ground = m_caster->GetMapHeight(x, y, z);
@@ -1573,7 +1573,7 @@ void Spell::SelectImplicitCasterDestTargets(SpellEffIndex effIndex, SpellImplici
{
case TARGET_DEST_CASTER_RANDOM:
if (dist > objSize)
dist = objSize + (dist - objSize) * float(rand_norm());
dist = objSize + (dist - objSize) * rand_norm();
break;
case TARGET_DEST_CASTER_FRONT_LEFT:
case TARGET_DEST_CASTER_BACK_LEFT:
@@ -1621,7 +1621,7 @@ void Spell::SelectImplicitTargetDestTargets(SpellEffIndex effIndex, SpellImplici
float angle = targetType.CalcDirectionAngle(m_spellInfo->Effects[effIndex]);
float dist = m_spellInfo->Effects[effIndex].CalcRadius(nullptr, targetIndex);
if (targetType.GetTarget() == TARGET_DEST_TARGET_RANDOM)
dist *= float(rand_norm());
dist *= rand_norm();
Position pos = dest._position;
target->MovePositionToFirstCollision(pos, dist, angle);
@@ -1657,7 +1657,7 @@ void Spell::SelectImplicitDestDestTargets(SpellEffIndex effIndex, SpellImplicitT
float angle = targetType.CalcDirectionAngle(m_spellInfo->Effects[effIndex]);
float dist = m_spellInfo->Effects[effIndex].CalcRadius(m_caster, targetIndex);
if (targetType.GetTarget() == TARGET_DEST_DEST_RANDOM)
dist *= float(rand_norm());
dist *= rand_norm();
Position pos = dest._position;
m_caster->MovePositionToFirstCollision(pos, dist, angle);
+1 -1
View File
@@ -4914,7 +4914,7 @@ void Spell::EffectTransmitted(SpellEffIndex effIndex)
//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)rand_norm() * (max_dis - min_dis) + min_dis;
float dis = rand_norm() * (max_dis - min_dis) + min_dis;
unitCaster->GetClosePoint(fx, fy, fz, DEFAULT_PLAYER_BOUNDING_RADIUS, dis);
fo = unitCaster->GetOrientation();
+1 -1
View File
@@ -121,7 +121,7 @@ float SpellImplicitTargetInfo::CalcDirectionAngle(SpellEffectInfo const& effectI
case TARGET_DIR_FRONT_LEFT:
return static_cast<float>(M_PI/4);
case TARGET_DIR_RANDOM:
return float(rand_norm())*static_cast<float>(2*M_PI);
return rand_norm() * static_cast<float>(2 * M_PI);
case TARGET_DIR_SUMMON:
// This direction does alter its angle based on what is being summoned.
// Creatures are being summoned on the left, gameobjects infront
+1 -1
View File
@@ -242,7 +242,7 @@ WorldLocation const* SpellScript::GetExplTargetDest() const
return nullptr;
}
void SpellScript::SetExplTargetDest(WorldLocation& loc)
void SpellScript::SetExplTargetDest(WorldLocation const& loc)
{
m_spell->m_targets.SetDst(loc);
}
+1 -1
View File
@@ -646,7 +646,7 @@ class TC_GAME_API SpellScript : public _SpellScript
// returns: WorldLocation which was selected as a spell destination or nullptr
WorldLocation const* GetExplTargetDest() const;
void SetExplTargetDest(WorldLocation& loc);
void SetExplTargetDest(WorldLocation const& loc);
// returns: WorldObject which was selected as an explicit spell target or nullptr if there's no target
WorldObject* GetExplTargetWorldObject() const;
+3 -3
View File
@@ -174,16 +174,16 @@ bool Weather::ReGenerate()
}
else if (u < 90)
{
m_grade = (float)rand_norm() * 0.3333f;
m_grade = rand_norm() * 0.3333f;
}
else
{
// Severe change, but how severe?
rnd = urand(0, 99);
if (rnd < 50)
m_grade = (float)rand_norm() * 0.3333f + 0.3334f;
m_grade = rand_norm() * 0.3333f + 0.3334f;
else
m_grade = (float)rand_norm() * 0.3333f + 0.6667f;
m_grade = rand_norm() * 0.3333f + 0.6667f;
}
// return true only in case weather changes