From 360d8eade0332f2c1aa5c205ca772cd506c35b26 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Tue, 13 Jun 2017 20:35:30 +0100 Subject: FastRandom rewrite (#3754) --- src/World.cpp | 64 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 28 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index f396aad86..0fa719670 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -245,22 +245,20 @@ void cWorld::CastThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) int cWorld::GetDefaultWeatherInterval(eWeather a_Weather) { + auto & Random = GetRandomProvider(); switch (a_Weather) { case eWeather_Sunny: { - auto dif = m_MaxSunnyTicks - m_MinSunnyTicks + 1; - return m_MinSunnyTicks + (m_TickRand.randInt() % dif); + return Random.RandInt(m_MinSunnyTicks, m_MaxSunnyTicks); } case eWeather_Rain: { - auto dif = m_MaxRainTicks - m_MinRainTicks + 1; - return m_MinRainTicks + (m_TickRand.randInt() % dif); + return Random.RandInt(m_MinRainTicks, m_MaxRainTicks); } case eWeather_ThunderStorm: { - auto dif = m_MaxThunderStormTicks - m_MinThunderStormTicks + 1; - return m_MinThunderStormTicks + (m_TickRand.randInt() % dif); + return Random.RandInt(m_MinThunderStormTicks, m_MaxThunderStormTicks); } } @@ -812,7 +810,7 @@ eWeather cWorld::ChooseNewWeather() case eWeather_Rain: { // 1 / 8 chance of turning into a thunderstorm - return ((m_TickRand.randInt() % 256) < 32) ? eWeather_ThunderStorm : eWeather_Sunny; + return GetRandomProvider().RandBool(0.125) ? eWeather_ThunderStorm : eWeather_Sunny; } } @@ -1074,7 +1072,7 @@ void cWorld::TickWeather(float a_Dt) if (m_Weather == eWeather_ThunderStorm) { // 0.5% chance per tick of thunderbolt - if (m_TickRand.randInt() % 199 == 0) + if (GetRandomProvider().RandBool(0.005)) { CastThunderbolt(0, 0, 0); // TODO: find random positions near players to cast thunderbolts. } @@ -1697,7 +1695,7 @@ void cWorld::GrowTreeImage(const sSetBlockVector & a_Blocks) bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsByBonemeal) { - cFastRandom random; + auto & random = GetRandomProvider(); BLOCKTYPE BlockType; NIBBLETYPE BlockMeta; GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1735,7 +1733,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } else { - BlockMeta += random.NextInt(4) + 2; + BlockMeta += random.RandInt(2, 5); BlockMeta = std::min(BlockMeta, static_cast(7)); } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1770,7 +1768,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } else { - BlockMeta += random.NextInt(4) + 2; + BlockMeta += random.RandInt(2, 5); BlockMeta = std::min(BlockMeta, static_cast(7)); } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1793,7 +1791,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } else { - BlockMeta += random.NextInt(4) + 2; + BlockMeta += random.RandInt(2, 5); BlockMeta = std::min(BlockMeta, static_cast(7)); } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1825,7 +1823,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } else { - BlockMeta += random.NextInt(4) + 2; + BlockMeta += random.RandInt(2, 5); BlockMeta = std::min(BlockMeta, static_cast(7)); } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1848,7 +1846,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } else { - BlockMeta += random.NextInt(4) + 2; + BlockMeta += random.RandInt(2, 5); BlockMeta = std::min(BlockMeta, static_cast(7)); } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); @@ -1884,14 +1882,14 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy { ++GrowState; } - else if (random.NextInt(99) < 45) + else if (random.RandBool(0.45)) { ++GrowState; } FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, static_cast(GrowState << 3 | TypeMeta)); } - else if (random.NextInt(99) < 45) + else if (random.RandBool(0.45)) { GrowTreeFromSapling(a_BlockX, a_BlockY, a_BlockZ, BlockMeta); } @@ -1905,12 +1903,12 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy { return false; } - MTRand r1; + auto & r1 = GetRandomProvider(); for (int i = 0; i < 60; i++) { - int OfsX = static_cast(r1.randInt(3) + r1.randInt(3) + r1.randInt(3) + r1.randInt(3)) / 2 - 3; - int OfsY = static_cast(r1.randInt(3) + r1.randInt(3)) - 3; - int OfsZ = static_cast(r1.randInt(3) + r1.randInt(3) + r1.randInt(3) + r1.randInt(3)) / 2 - 3; + int OfsX = (r1.RandInt(3) + r1.RandInt(3) + r1.RandInt(3) + r1.RandInt(3)) / 2 - 3; + int OfsY = r1.RandInt(3) + r1.RandInt(3) - 3; + int OfsZ = (r1.RandInt(3) + r1.RandInt(3) + r1.RandInt(3) + r1.RandInt(3)) / 2 - 3; BLOCKTYPE Ground = GetBlock(a_BlockX + OfsX, a_BlockY + OfsY, a_BlockZ + OfsZ); if (Ground != E_BLOCK_GRASS) { @@ -1923,7 +1921,7 @@ bool cWorld::GrowRipePlant(int a_BlockX, int a_BlockY, int a_BlockZ, bool a_IsBy } BLOCKTYPE SpawnType; NIBBLETYPE SpawnMeta = 0; - switch (r1.randInt(10)) + switch (r1.RandInt(10)) { case 0: SpawnType = E_BLOCK_YELLOW_FLOWER; break; case 1: SpawnType = E_BLOCK_RED_ROSE; break; @@ -2031,8 +2029,7 @@ int cWorld::GrowCactus(int a_BlockX, int a_BlockY, int a_BlockZ, int a_NumBlocks bool cWorld::GrowMelonPumpkin(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType) { - MTRand Rand; - return m_ChunkMap->GrowMelonPumpkin(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, Rand); + return m_ChunkMap->GrowMelonPumpkin(a_BlockX, a_BlockY, a_BlockZ, a_BlockType); } @@ -2162,6 +2159,7 @@ bool cWorld::WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlock void cWorld::SpawnItemPickups(const cItems & a_Pickups, double a_BlockX, double a_BlockY, double a_BlockZ, double a_FlyAwaySpeed, bool IsPlayerCreated) { + auto & Random = GetRandomProvider(); a_FlyAwaySpeed /= 100; // Pre-divide, so that we don't have to divide each time inside the loop for (cItems::const_iterator itr = a_Pickups.begin(); itr != a_Pickups.end(); ++itr) { @@ -2171,9 +2169,9 @@ void cWorld::SpawnItemPickups(const cItems & a_Pickups, double a_BlockX, double continue; } - float SpeedX = static_cast(a_FlyAwaySpeed * (GetTickRandomNumber(10) - 5)); - float SpeedY = static_cast(a_FlyAwaySpeed * GetTickRandomNumber(50)); - float SpeedZ = static_cast(a_FlyAwaySpeed * (GetTickRandomNumber(10) - 5)); + float SpeedX = static_cast(a_FlyAwaySpeed * Random.RandInt(-5, 5)); + float SpeedY = static_cast(a_FlyAwaySpeed * Random.RandInt(50)); + float SpeedZ = static_cast(a_FlyAwaySpeed * Random.RandInt(-5, 5)); cPickup * Pickup = new cPickup( a_BlockX, a_BlockY, a_BlockZ, @@ -2310,10 +2308,11 @@ UInt32 cWorld::SpawnPrimedTNT(double a_X, double a_Y, double a_Z, int a_FuseTick TNT = nullptr; return cEntity::INVALID_ID; } + auto & Random = GetRandomProvider(); TNT->SetSpeed( - a_InitialVelocityCoeff * (GetTickRandomNumber(2) - 1), /** -1, 0, 1 */ + a_InitialVelocityCoeff * Random.RandInt(-1, 1), a_InitialVelocityCoeff * 2, - a_InitialVelocityCoeff * (GetTickRandomNumber(2) - 1) + a_InitialVelocityCoeff * Random.RandInt(-1, 1) ); return TNT->GetUniqueID(); } @@ -3793,6 +3792,15 @@ UInt32 cWorld::CreateProjectile(double a_PosX, double a_PosY, double a_PosZ, cPr +int cWorld::GetTickRandomNumber(int a_Range) +{ + return GetRandomProvider().RandInt(a_Range); +} + + + + + void cWorld::TabCompleteUserName(const AString & a_Text, AStringVector & a_Results) { typedef std::pair pair_t; -- cgit v1.2.3