Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Core/Random: Changed random functions returning doubles to return floats
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Shauren authored and Ovahlord committed Oct 31, 2023
1 parent 56dc3bd commit 7d57ef7
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/common/Utilities/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/common/Utilities/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/Entities/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Entities/Unit/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Loot/LootMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Pools/PoolMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Skills/SkillDiscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/Spells/Spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Spells/SpellEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Spells/SpellInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Spells/SpellScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Spells/SpellScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/Weather/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d57ef7

Please sign in to comment.