diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-10-31 20:20:00 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-10-31 20:20:00 +0100 |
commit | 9894f6b802c974bb36acd7fbb0d083455a1f0f1b (patch) | |
tree | 458f167a846d70d4b2391c805423d9342b36f89a | |
parent | a0fdac0ecc119b4ba85d41f86891b3cd35f7acde (diff) |
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)
22 files changed, 47 insertions, 45 deletions
diff --git a/src/common/Utilities/Random.cpp b/src/common/Utilities/Random.cpp index 64b4f0c57d3..973832a1cf3 100644 --- a/src/common/Utilities/Random.cpp +++ b/src/common/Utilities/Random.cpp @@ -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.0f, 100.0f); return urd(engine); } diff --git a/src/common/Utilities/Random.h b/src/common/Utilities/Random.h index 82a498e42eb..c658c477ab1 100644 --- a/src/common/Utilities/Random.h +++ b/src/common/Utilities/Random.h @@ -40,11 +40,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); diff --git a/src/server/game/Entities/Object/Object.cpp b/src/server/game/Entities/Object/Object.cpp index d36f19f4c5f..fbbb8f3aa0a 100644 --- a/src/server/game/Entities/Object/Object.cpp +++ b/src/server/game/Entities/Object/Object.cpp @@ -1328,8 +1328,8 @@ void WorldObject::GetRandomPoint(Position const& 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); @@ -2913,6 +2913,8 @@ SpellCastResult WorldObject::CastSpell(CastSpellTargetArg const& targets, uint32 } spell->m_customArg = args.CustomArg; + spell->m_scriptResult = args.ScriptResult; + spell->m_scriptWaitsForSpellHit = args.ScriptWaitsForSpellHit; return spell->prepare(*targets.Targets, args.TriggeringAura); } @@ -3410,7 +3412,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; } diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index b7ef17f136f..609e0dfb606 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -1714,7 +1714,7 @@ void Unit::HandleEmoteCommand(Emote emoteId, Player* target /*=nullptr*/, Trinit 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; diff --git a/src/server/game/Loot/LootMgr.cpp b/src/server/game/Loot/LootMgr.cpp index dec8215dc50..992e75baca7 100644 --- a/src/server/game/Loot/LootMgr.cpp +++ b/src/server/game/Loot/LootMgr.cpp @@ -386,7 +386,7 @@ LootStoreItem const* LootTemplate::LootGroup::Roll(uint16 lootMode, Player const 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. { diff --git a/src/server/game/Pools/PoolMgr.cpp b/src/server/game/Pools/PoolMgr.cpp index 8399dbe5345..8fd7ba164cc 100644 --- a/src/server/game/Pools/PoolMgr.cpp +++ b/src/server/game/Pools/PoolMgr.cpp @@ -304,7 +304,7 @@ void PoolGroup<T>::SpawnObject(SpawnedPoolData& spawns, uint32 limit, uint64 tri // roll objects to be spawned if (!ExplicitlyChanced.empty()) { - float roll = (float)rand_chance(); + float roll = rand_chance(); for (PoolObject& obj : ExplicitlyChanced) { diff --git a/src/server/game/Skills/SkillDiscovery.cpp b/src/server/game/Skills/SkillDiscovery.cpp index 38e103cd7d0..53026662d3d 100644 --- a/src/server/game/Skills/SkillDiscovery.cpp +++ b/src/server/game/Skills/SkillDiscovery.cpp @@ -174,7 +174,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) { diff --git a/src/server/game/Spells/Spell.cpp b/src/server/game/Spells/Spell.cpp index 921b7db6183..700ac261294 100644 --- a/src/server/game/Spells/Spell.cpp +++ b/src/server/game/Spells/Spell.cpp @@ -1502,7 +1502,7 @@ void Spell::SelectImplicitCasterDestTargets(SpellEffectInfo const& spellEffectIn 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); @@ -1604,7 +1604,7 @@ void Spell::SelectImplicitCasterDestTargets(SpellEffectInfo const& spellEffectIn break; 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: @@ -1656,7 +1656,7 @@ void Spell::SelectImplicitTargetDestTargets(SpellEffectInfo const& spellEffectIn float angle = targetType.CalcDirectionAngle(); float dist = spellEffectInfo.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); @@ -1710,7 +1710,7 @@ void Spell::SelectImplicitDestDestTargets(SpellEffectInfo const& spellEffectInfo float angle = targetType.CalcDirectionAngle(); float dist = spellEffectInfo.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); diff --git a/src/server/game/Spells/SpellEffects.cpp b/src/server/game/Spells/SpellEffects.cpp index b2c1ad91459..7cb4fc675d8 100644 --- a/src/server/game/Spells/SpellEffects.cpp +++ b/src/server/game/Spells/SpellEffects.cpp @@ -4394,7 +4394,7 @@ void Spell::EffectTransmitted() //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(); diff --git a/src/server/game/Spells/SpellInfo.cpp b/src/server/game/Spells/SpellInfo.cpp index 7be8c52f8d5..656bd37e3a7 100644 --- a/src/server/game/Spells/SpellInfo.cpp +++ b/src/server/game/Spells/SpellInfo.cpp @@ -123,7 +123,7 @@ float SpellImplicitTargetInfo::CalcDirectionAngle() const 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); default: return 0.0f; } diff --git a/src/server/game/Spells/SpellScript.cpp b/src/server/game/Spells/SpellScript.cpp index 1542ff14c55..79087620742 100644 --- a/src/server/game/Spells/SpellScript.cpp +++ b/src/server/game/Spells/SpellScript.cpp @@ -431,7 +431,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); } diff --git a/src/server/game/Spells/SpellScript.h b/src/server/game/Spells/SpellScript.h index e672657d4d2..abe0608b45a 100644 --- a/src/server/game/Spells/SpellScript.h +++ b/src/server/game/Spells/SpellScript.h @@ -937,7 +937,7 @@ public: // returns: WorldLocation which was selected as a spell destination or NULL WorldLocation const* GetExplTargetDest() const; - void SetExplTargetDest(WorldLocation& loc); + void SetExplTargetDest(WorldLocation const& loc); // returns: WorldObject which was selected as an explicit spell target or NULL if there's no target WorldObject* GetExplTargetWorldObject() const; diff --git a/src/server/game/Weather/Weather.cpp b/src/server/game/Weather/Weather.cpp index e9b0dbb3c79..3cdce2d2bc3 100644 --- a/src/server/game/Weather/Weather.cpp +++ b/src/server/game/Weather/Weather.cpp @@ -174,16 +174,16 @@ bool Weather::ReGenerate() } else if (u < 90) { - m_intensity = (float)rand_norm() * 0.3333f; + m_intensity = rand_norm() * 0.3333f; } else { // Severe change, but how severe? rnd = urand(0, 99); if (rnd < 50) - m_intensity = (float)rand_norm() * 0.3333f + 0.3334f; + m_intensity = rand_norm() * 0.3333f + 0.3334f; else - m_intensity = (float)rand_norm() * 0.3333f + 0.6667f; + m_intensity = rand_norm() * 0.3333f + 0.6667f; } // return true only in case weather changes diff --git a/src/server/scripts/EasternKingdoms/ZulGurub/boss_mandokir.cpp b/src/server/scripts/EasternKingdoms/ZulGurub/boss_mandokir.cpp index d5e0e4f31d5..145f5acce61 100644 --- a/src/server/scripts/EasternKingdoms/ZulGurub/boss_mandokir.cpp +++ b/src/server/scripts/EasternKingdoms/ZulGurub/boss_mandokir.cpp @@ -530,7 +530,7 @@ class spell_mandokir_devastating_slam : public SpellScript // HACK: Need better way for pos calculation for (uint8 i = 0; i <= 50; ++i) { - angle = float(rand_norm()) * static_cast<float>(M_PI * 35.0f / 180.0f) - static_cast<float>(M_PI * 17.5f / 180.0f); + angle = rand_norm() * static_cast<float>(M_PI * 35.0f / 180.0f) - static_cast<float>(M_PI * 17.5f / 180.0f); caster->GetClosePoint(x, y, z, 4.0f, frand(-2.5f, 50.0f), angle); caster->CastSpell(Position{ x, y, z }, SPELL_DEVASTATING_SLAM_DAMAGE, true); diff --git a/src/server/scripts/Events/midsummer.cpp b/src/server/scripts/Events/midsummer.cpp index c6e638e0b22..29cac39c3dc 100644 --- a/src/server/scripts/Events/midsummer.cpp +++ b/src/server/scripts/Events/midsummer.cpp @@ -307,7 +307,7 @@ class spell_midsummer_fling_torch : public SpellScript void HandleDummy(SpellEffIndex /*effIndex*/) { - Position dest = GetCaster()->GetFirstCollisionPosition(30.0f, (float)rand_norm() * static_cast<float>(2 * M_PI)); + Position dest = GetCaster()->GetFirstCollisionPosition(30.0f, rand_norm() * static_cast<float>(2 * M_PI)); GetCaster()->CastSpell(dest, SPELL_FLING_TORCH_TRIGGERED, true); GetCaster()->CastSpell(dest, SPELL_FLING_TORCH_SHADOW); } @@ -391,7 +391,7 @@ class spell_midsummer_fling_torch_catch : public SpellScript } else { - Position dest = player->GetFirstCollisionPosition(15.0f, (float)rand_norm() * static_cast<float>(2 * M_PI)); + Position dest = player->GetFirstCollisionPosition(15.0f, rand_norm() * static_cast<float>(2 * M_PI)); player->CastSpell(player, SPELL_TORCHES_CAUGHT); player->CastSpell(dest, SPELL_FLING_TORCH_TRIGGERED, true); player->CastSpell(dest, SPELL_FLING_TORCH_SHADOW); diff --git a/src/server/scripts/Events/zalazane_fall.cpp b/src/server/scripts/Events/zalazane_fall.cpp index f655b7db555..8526025716e 100644 --- a/src/server/scripts/Events/zalazane_fall.cpp +++ b/src/server/scripts/Events/zalazane_fall.cpp @@ -287,7 +287,7 @@ struct npc_troll_volunteer : public ScriptedAI } me->SetDisplayId(trollmodel[urand(0, 39)]); if (Player* player = me->GetOwner()->ToPlayer()) - me->GetMotionMaster()->MoveFollow(player, 5.0f, float(rand_norm() + 1.0f) * float(M_PI) / 3.0f * 4.0f); + me->GetMotionMaster()->MoveFollow(player, 5.0f, (rand_norm() + 1.0f) * float(M_PI) / 3.0f * 4.0f); } void Reset() override diff --git a/src/server/scripts/ExilesReach/zone_exiles_reach.cpp b/src/server/scripts/ExilesReach/zone_exiles_reach.cpp index 5a73e74bb1b..1c56bbe29b5 100644 --- a/src/server/scripts/ExilesReach/zone_exiles_reach.cpp +++ b/src/server/scripts/ExilesReach/zone_exiles_reach.cpp @@ -359,7 +359,7 @@ struct npc_sparring_partner_exiles_reach : public ScriptedAI { case POSITION_SPARPOINT_ADVERTISMENT: me->SetWalk(true); - me->GetMotionMaster()->MovePoint(POSITION_SPARPOINT_READY, me->GetFirstCollisionPosition(2.0f, (float)rand_norm() * static_cast<float>(2 * M_PI))); + me->GetMotionMaster()->MovePoint(POSITION_SPARPOINT_READY, me->GetFirstCollisionPosition(2.0f, rand_norm() * static_cast<float>(2 * M_PI))); break; case POSITION_SPARPOINT_READY: if (Player* player = ObjectAccessor::GetPlayer(*me, _playerGUID)) diff --git a/src/server/scripts/Kalimdor/CavernsOfTime/CullingOfStratholme/instance_culling_of_stratholme.cpp b/src/server/scripts/Kalimdor/CavernsOfTime/CullingOfStratholme/instance_culling_of_stratholme.cpp index 87a89e0d80c..15fecf2e221 100644 --- a/src/server/scripts/Kalimdor/CavernsOfTime/CullingOfStratholme/instance_culling_of_stratholme.cpp +++ b/src/server/scripts/Kalimdor/CavernsOfTime/CullingOfStratholme/instance_culling_of_stratholme.cpp @@ -405,8 +405,8 @@ class instance_culling_of_stratholme : public InstanceMapScript if (player->GetGUID() == guid || !player->IsGameMaster()) { player->CombatStop(true); - const float offsetDist = 10; - float myAngle = rand_norm() * 2.0 * M_PI; + constexpr float offsetDist = 10.0f; + float myAngle = rand_norm() * static_cast<float>(2.0f * M_PI); Position myTarget(target.GetPositionX() + std::sin(myAngle) * offsetDist, target.GetPositionY() + std::sin(myAngle) * offsetDist, target.GetPositionZ(), myAngle + M_PI); player->NearTeleportTo(myTarget); } diff --git a/src/server/scripts/Kalimdor/HallsOfOrigination/boss_anraphet.cpp b/src/server/scripts/Kalimdor/HallsOfOrigination/boss_anraphet.cpp index f5da2934c7d..86e2c5ebe49 100644 --- a/src/server/scripts/Kalimdor/HallsOfOrigination/boss_anraphet.cpp +++ b/src/server/scripts/Kalimdor/HallsOfOrigination/boss_anraphet.cpp @@ -538,8 +538,8 @@ public: // Do our own calculations for the destination position. /// TODO: Remove this once we find a general rule for WorldObject::MovePosition (this spell shouldn't take the Z change into consideration) Unit* caster = GetCaster(); - float angle = float(rand_norm()) * static_cast<float>(2 * M_PI); - uint32 dist = caster->GetCombatReach() + GetSpellInfo()->GetEffect(EFFECT_0).CalcRadius(caster, SpellTargetIndex::TargetB) * (float)rand_norm(); + float angle = rand_norm() * static_cast<float>(2 * M_PI); + uint32 dist = caster->GetCombatReach() + GetSpellInfo()->GetEffect(EFFECT_0).CalcRadius(caster, SpellTargetIndex::TargetB) * rand_norm(); float x = caster->GetPositionX() + dist * std::cos(angle); float y = caster->GetPositionY() + dist * std::sin(angle); diff --git a/src/server/scripts/Northrend/IcecrownCitadel/boss_sindragosa.cpp b/src/server/scripts/Northrend/IcecrownCitadel/boss_sindragosa.cpp index d6ab270a5fc..86c9ee09c49 100644 --- a/src/server/scripts/Northrend/IcecrownCitadel/boss_sindragosa.cpp +++ b/src/server/scripts/Northrend/IcecrownCitadel/boss_sindragosa.cpp @@ -522,8 +522,8 @@ struct boss_sindragosa : public BossAI case EVENT_FROST_BOMB: { float destX, destY, destZ; - destX = float(rand_norm()) * 75.0f + 4350.0f; - destY = float(rand_norm()) * 75.0f + 2450.0f; + destX = rand_norm() * 75.0f + 4350.0f; + destY = rand_norm() * 75.0f + 2450.0f; destZ = 205.0f; // random number close to ground, get exact in next call me->UpdateGroundPositionZ(destX, destY, destZ); me->CastSpell(Position{ destX, destY, destZ }, SPELL_FROST_BOMB_TRIGGER, false); @@ -1025,13 +1025,13 @@ class spell_sindragosa_s_fury : public SpellScript void SelectDest() { - if (Position* dest = const_cast<WorldLocation*>(GetExplTargetDest())) + if (WorldLocation const* dest = GetExplTargetDest()) { - float destX = float(rand_norm()) * 75.0f + 4350.0f; - float destY = float(rand_norm()) * 75.0f + 2450.0f; + float destX = rand_norm() * 75.0f + 4350.0f; + float destY = rand_norm() * 75.0f + 2450.0f; float destZ = 205.0f; // random number close to ground, get exact in next call GetCaster()->UpdateGroundPositionZ(destX, destY, destZ); - dest->Relocate(destX, destY, destZ); + SetExplTargetDest(WorldLocation(dest->GetMapId(), destX, destY, destZ)); } } diff --git a/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp b/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp index 1a30c3c5bee..ba5dcb80c28 100644 --- a/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp +++ b/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp @@ -1642,7 +1642,7 @@ struct npc_strangulate_vehicle : public ScriptedAI { if (me->GetExactDist(lichKing) > 10.0f) { - Position pos = lichKing->GetNearPosition(float(rand_norm()) * 5.0f + 7.5f, lichKing->GetAbsoluteAngle(me)); + Position pos = lichKing->GetNearPosition(rand_norm() * 5.0f + 7.5f, lichKing->GetAbsoluteAngle(me)); me->GetMotionMaster()->MovePoint(0, pos); } } diff --git a/src/server/scripts/Northrend/Naxxramas/boss_kelthuzad.cpp b/src/server/scripts/Northrend/Naxxramas/boss_kelthuzad.cpp index 72e07397e3e..85572114efc 100644 --- a/src/server/scripts/Northrend/Naxxramas/boss_kelthuzad.cpp +++ b/src/server/scripts/Northrend/Naxxramas/boss_kelthuzad.cpp @@ -159,11 +159,11 @@ static inline Position const& GetRandomMinionSpawnPoint() // uniformly distribute on the circle static Position GetRandomPositionOnCircle(Position const& center, float radius) { - double angle = rand_norm() * 2.0 * M_PI; - double relDistance = rand_norm() + rand_norm(); + float angle = float(M_PI * rand_norm() * 2.0); + float relDistance = rand_norm() + rand_norm(); if (relDistance > 1) relDistance = 1 - relDistance; - return Position(center.GetPositionX() + std::sin(angle)*relDistance*radius, center.GetPositionY() + std::cos(angle)*relDistance*radius, center.GetPositionZ()); + return Position(center.GetPositionX() + std::sin(angle) * relDistance * radius, center.GetPositionY() + std::cos(angle) * relDistance * radius, center.GetPositionZ()); } class KelThuzadCharmedPlayerAI : public SimpleCharmedPlayerAI |