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/Mobs/Chicken.cpp | 2 +- src/Mobs/Horse.cpp | 8 +++--- src/Mobs/Monster.cpp | 62 ++++++++++++++++++++++----------------------- src/Mobs/PassiveMonster.cpp | 3 +-- src/Mobs/Path.cpp | 2 +- src/Mobs/Path.h | 1 - src/Mobs/Rabbit.cpp | 4 +-- src/Mobs/Sheep.cpp | 12 ++++----- src/Mobs/Skeleton.cpp | 6 ++--- src/Mobs/Slime.cpp | 6 ++--- src/Mobs/Villager.cpp | 6 ++--- src/Mobs/Witch.cpp | 6 ++--- src/Mobs/Wolf.cpp | 4 +-- 13 files changed, 60 insertions(+), 62 deletions(-) (limited to 'src/Mobs') diff --git a/src/Mobs/Chicken.cpp b/src/Mobs/Chicken.cpp index 0224078f5..1068295e6 100644 --- a/src/Mobs/Chicken.cpp +++ b/src/Mobs/Chicken.cpp @@ -34,7 +34,7 @@ void cChicken::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) return; // Babies don't lay eggs } - if ((m_EggDropTimer == 6000) && (m_World->GetTickRandomNumber(1) == 0)) + if ((m_EggDropTimer == 6000) && GetRandomProvider().RandBool()) { cItems Drops; m_EggDropTimer = 0; diff --git a/src/Mobs/Horse.cpp b/src/Mobs/Horse.cpp index 978471b8d..acf79d3b1 100644 --- a/src/Mobs/Horse.cpp +++ b/src/Mobs/Horse.cpp @@ -41,16 +41,18 @@ void cHorse::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) return; } + auto & Random = GetRandomProvider(); + if (!m_bIsMouthOpen) { - if (m_World->GetTickRandomNumber(50) == 25) + if (Random.RandBool(0.02)) { m_bIsMouthOpen = true; } } else { - if (m_World->GetTickRandomNumber(10) == 5) + if (Random.RandBool(0.10)) { m_bIsMouthOpen = false; } @@ -60,7 +62,7 @@ void cHorse::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { if (m_TameAttemptTimes < m_TimesToTame) { - if (m_World->GetTickRandomNumber(50) == 25) + if (Random.RandBool(0.02)) { m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, FloorC(GetPosX()), FloorC(GetPosY()), FloorC(GetPosZ()), int(SmokeDirection::SOUTH_EAST)); m_World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, FloorC(GetPosX()), FloorC(GetPosY()), FloorC(GetPosZ()), int(SmokeDirection::SOUTH_WEST)); diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index 00045fc69..ecda6e724 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -512,7 +512,7 @@ void cMonster::KilledBy(TakeDamageInfo & a_TDI) case mtOcelot: case mtWolf: { - Reward = m_World->GetTickRandomNumber(2) + 1; + Reward = GetRandomProvider().RandInt(1, 3); break; } @@ -531,7 +531,7 @@ void cMonster::KilledBy(TakeDamageInfo & a_TDI) case mtSlime: case mtMagmaCube: { - Reward = 6 + (m_World->GetTickRandomNumber(2)); + Reward = GetRandomProvider().RandInt(6, 8); break; } case mtBlaze: @@ -657,13 +657,15 @@ void cMonster::InStateIdle(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) if (m_IdleInterval > std::chrono::seconds(1)) { + auto & Random = GetRandomProvider(); + // At this interval the results are predictable - int rem = m_World->GetTickRandomNumber(6) + 1; + int rem = Random.RandInt(1, 7); m_IdleInterval -= std::chrono::seconds(1); // So nothing gets dropped when the server hangs for a few seconds Vector3d Dist; - Dist.x = static_cast(m_World->GetTickRandomNumber(10)) - 5.0; - Dist.z = static_cast(m_World->GetTickRandomNumber(10)) - 5.0; + Dist.x = static_cast(Random.RandInt(-5, 5)); + Dist.z = static_cast(Random.RandInt(-5, 5)); if ((Dist.SqrLength() > 2) && (rem >= 3)) { @@ -1005,7 +1007,7 @@ cPawn * cMonster::GetTarget () cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) { - cFastRandom Random; + auto & Random = GetRandomProvider(); cMonster * toReturn = nullptr; // Create the mob entity @@ -1013,23 +1015,23 @@ cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) { case mtMagmaCube: { - toReturn = new cMagmaCube(1 << Random.NextInt(3)); // Size 1, 2 or 4 + toReturn = new cMagmaCube(1 << Random.RandInt(2)); // Size 1, 2 or 4 break; } case mtSlime: { - toReturn = new cSlime(1 << Random.NextInt(3)); // Size 1, 2 or 4 + toReturn = new cSlime(1 << Random.RandInt(2)); // Size 1, 2 or 4 break; } case mtSkeleton: { // TODO: Actual detection of spawning in Nether - toReturn = new cSkeleton((Random.NextInt(1) == 0) ? false : true); + toReturn = new cSkeleton(false); break; } case mtVillager: { - int VillagerType = Random.NextInt(6); + int VillagerType = Random.RandInt(6); if (VillagerType == 6) { // Give farmers a better chance of spawning @@ -1042,10 +1044,10 @@ cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) case mtHorse: { // Horses take a type (species), a colour, and a style (dots, stripes, etc.) - int HorseType = Random.NextInt(8); - int HorseColor = Random.NextInt(7); - int HorseStyle = Random.NextInt(5); - int HorseTameTimes = Random.NextInt(6) + 1; + int HorseType = Random.RandInt(7); + int HorseColor = Random.RandInt(6); + int HorseStyle = Random.RandInt(4); + int HorseTameTimes = Random.RandInt(1, 6); if ((HorseType == 5) || (HorseType == 6) || (HorseType == 7)) { @@ -1097,11 +1099,10 @@ cMonster * cMonster::NewMonsterFromType(eMonsterType a_MobType) void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth) { - MTRand r1; - int Count = static_cast(static_cast(r1.randInt()) % (a_Max + 1 - a_Min) + a_Min); + auto Count = GetRandomProvider().RandInt(static_cast(a_Min), static_cast(a_Max)); if (Count > 0) { - a_Drops.push_back(cItem(a_Item, static_cast(Count), a_ItemHealth)); + a_Drops.emplace_back(a_Item, Count, a_ItemHealth); } } @@ -1111,9 +1112,7 @@ void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned void cMonster::AddRandomUncommonDropItem(cItems & a_Drops, float a_Chance, short a_Item, short a_ItemHealth) { - MTRand r1; - int Count = r1.randInt() % 1000; - if (Count < (a_Chance * 10)) + if (GetRandomProvider().RandBool(a_Chance / 100.0)) { a_Drops.push_back(cItem(a_Item, 1, a_ItemHealth)); } @@ -1125,11 +1124,10 @@ void cMonster::AddRandomUncommonDropItem(cItems & a_Drops, float a_Chance, short void cMonster::AddRandomRareDropItem(cItems & a_Drops, cItems & a_Items, unsigned int a_LootingLevel) { - MTRand r1; - unsigned int Count = static_cast(static_cast(r1.randInt()) % 200); - if (Count < (5 + a_LootingLevel)) + auto & r1 = GetRandomProvider(); + if (r1.RandBool((5 + a_LootingLevel) / 200.0)) { - size_t Rare = static_cast(r1.randInt()) % a_Items.Size(); + size_t Rare = r1.RandInt(a_Items.Size() - 1); a_Drops.push_back(a_Items.at(Rare)); } } @@ -1140,8 +1138,11 @@ void cMonster::AddRandomRareDropItem(cItems & a_Drops, cItems & a_Items, unsigne void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLevel) { - MTRand r1; - if (r1.randInt() % 200 < ((m_DropChanceHelmet * 200) + (a_LootingLevel * 2))) + auto & r1 = GetRandomProvider(); + + double LootingBonus = a_LootingLevel / 100.0; + + if (r1.RandBool(m_DropChanceHelmet + LootingBonus)) { if (!GetEquippedHelmet().IsEmpty()) { @@ -1149,7 +1150,7 @@ void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLe } } - if (r1.randInt() % 200 < ((m_DropChanceChestplate * 200) + (a_LootingLevel * 2))) + if (r1.RandBool(m_DropChanceChestplate + LootingBonus)) { if (!GetEquippedChestplate().IsEmpty()) { @@ -1157,7 +1158,7 @@ void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLe } } - if (r1.randInt() % 200 < ((m_DropChanceLeggings * 200) + (a_LootingLevel * 2))) + if (r1.RandBool(m_DropChanceLeggings + LootingBonus)) { if (!GetEquippedLeggings().IsEmpty()) { @@ -1165,7 +1166,7 @@ void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLe } } - if (r1.randInt() % 200 < ((m_DropChanceBoots * 200) + (a_LootingLevel * 2))) + if (r1.RandBool(m_DropChanceBoots + LootingBonus)) { if (!GetEquippedBoots().IsEmpty()) { @@ -1180,8 +1181,7 @@ void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLe void cMonster::AddRandomWeaponDropItem(cItems & a_Drops, unsigned int a_LootingLevel) { - MTRand r1; - if (r1.randInt() % 200 < ((m_DropChanceWeapon * 200) + (a_LootingLevel * 2))) + if (GetRandomProvider().RandBool(m_DropChanceWeapon + (a_LootingLevel / 100.0))) { if (!GetEquippedWeapon().IsEmpty()) { diff --git a/src/Mobs/PassiveMonster.cpp b/src/Mobs/PassiveMonster.cpp index 42884fb56..5646f8e32 100644 --- a/src/Mobs/PassiveMonster.cpp +++ b/src/Mobs/PassiveMonster.cpp @@ -127,8 +127,7 @@ void cPassiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) Callback.Baby->InheritFromParents(this, m_LovePartner); } - cFastRandom Random; - m_World->SpawnExperienceOrb(Pos.x, Pos.y, Pos.z, 1 + Random.NextInt(6)); + m_World->SpawnExperienceOrb(Pos.x, Pos.y, Pos.z, GetRandomProvider().RandInt(1, 6)); m_LovePartner->ResetLoveMode(); ResetLoveMode(); diff --git a/src/Mobs/Path.cpp b/src/Mobs/Path.cpp index 1273b2517..9f34e73d9 100644 --- a/src/Mobs/Path.cpp +++ b/src/Mobs/Path.cpp @@ -148,7 +148,7 @@ bool cPath::StepOnce() // Check if we have a new NearestPoint. if ((m_Destination - CurrentCell->m_Location).Length() < 5) { - if (m_Rand.NextInt(4) == 0) + if (GetRandomProvider().RandBool(0.25)) { m_NearestPointToTarget = CurrentCell; } diff --git a/src/Mobs/Path.h b/src/Mobs/Path.h index f51b7da77..977c5be1c 100644 --- a/src/Mobs/Path.h +++ b/src/Mobs/Path.h @@ -178,7 +178,6 @@ private: double m_HalfWidth; int m_StepsLeft; cPathCell * m_NearestPointToTarget; - cFastRandom m_Rand; /* Control fields */ ePathFinderStatus m_Status; diff --git a/src/Mobs/Rabbit.cpp b/src/Mobs/Rabbit.cpp index 9750ed5d1..f4de0ba0c 100644 --- a/src/Mobs/Rabbit.cpp +++ b/src/Mobs/Rabbit.cpp @@ -10,8 +10,8 @@ cRabbit::cRabbit(void) : - cRabbit(static_cast(cFastRandom().NextInt( - static_cast(eRabbitType::SaltAndPepper) + 1 // Max possible Rabbit-Type + cRabbit(static_cast(GetRandomProvider().RandInt( + static_cast(eRabbitType::SaltAndPepper) // Max possible Rabbit-Type )), 0) { } diff --git a/src/Mobs/Sheep.cpp b/src/Mobs/Sheep.cpp index 810d37a70..4adcedae9 100644 --- a/src/Mobs/Sheep.cpp +++ b/src/Mobs/Sheep.cpp @@ -65,8 +65,8 @@ void cSheep::OnRightClicked(cPlayer & a_Player) a_Player.UseEquippedItem(); cItems Drops; - int NumDrops = m_World->GetTickRandomNumber(2) + 1; - Drops.push_back(cItem(E_BLOCK_WOOL, static_cast(NumDrops), static_cast(m_WoolColor))); + char NumDrops = GetRandomProvider().RandInt(1, 3); + Drops.emplace_back(E_BLOCK_WOOL, NumDrops, static_cast(m_WoolColor)); m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY(), GetPosZ(), 10); m_World->BroadcastSoundEffect("entity.sheep.shear", GetPosX(), GetPosY(), GetPosZ(), 1.0f, 1.0f); } @@ -121,7 +121,7 @@ void cSheep::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } else { - if (m_World->GetTickRandomNumber(600) == 1) + if (GetRandomProvider().RandBool(1.0 / 600.0)) { if (m_World->GetBlock(PosX, PosY, PosZ) == E_BLOCK_GRASS) { @@ -167,8 +167,7 @@ void cSheep::InheritFromParents(cPassiveMonster * a_Parent1, cPassiveMonster * a return; } } - cFastRandom Random; - SetFurColor((Random.NextInt(100) < 50) ? Parent1->GetFurColor() : Parent2->GetFurColor()); + SetFurColor(GetRandomProvider().RandBool() ? Parent1->GetFurColor() : Parent2->GetFurColor()); m_World->BroadcastEntityMetadata(*this); } @@ -178,8 +177,7 @@ void cSheep::InheritFromParents(cPassiveMonster * a_Parent1, cPassiveMonster * a NIBBLETYPE cSheep::GenerateNaturalRandomColor(void) { - cFastRandom Random; - int Chance = Random.NextInt(101); + int Chance = GetRandomProvider().RandInt(100); if (Chance <= 81) { diff --git a/src/Mobs/Skeleton.cpp b/src/Mobs/Skeleton.cpp index 311533b69..0d6d5e428 100644 --- a/src/Mobs/Skeleton.cpp +++ b/src/Mobs/Skeleton.cpp @@ -51,12 +51,12 @@ void cSkeleton::GetDrops(cItems & a_Drops, cEntity * a_Killer) bool cSkeleton::Attack(std::chrono::milliseconds a_Dt) { StopMovingToPosition(); // Todo handle this in a better way, the skeleton does some uneeded recalcs due to inStateChasing - cFastRandom Random; + auto & Random = GetRandomProvider(); if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0)) { - Vector3d Inaccuracy = Vector3d(Random.NextFloat(0.5) - 0.25, Random.NextFloat(0.5) - 0.25, Random.NextFloat(0.5) - 0.25); + Vector3d Inaccuracy = Vector3d(Random.RandReal(-0.25, 0.25), Random.RandReal(-0.25, 0.25), Random.RandReal(-0.25, 0.25)); Vector3d Speed = (GetTarget()->GetPosition() + Inaccuracy - GetPosition()) * 5; - Speed.y = Speed.y - 1 + Random.NextInt(3); + Speed.y += Random.RandInt(-1, 1); cArrowEntity * Arrow = new cArrowEntity(this, GetPosX(), GetPosY() + 1, GetPosZ(), Speed); if (Arrow == nullptr) { diff --git a/src/Mobs/Slime.cpp b/src/Mobs/Slime.cpp index dca5c5887..3f832ae87 100644 --- a/src/Mobs/Slime.cpp +++ b/src/Mobs/Slime.cpp @@ -70,8 +70,8 @@ void cSlime::KilledBy(TakeDamageInfo & a_TDI) if (m_Size != 1) { - cFastRandom Random; - int SpawnAmount = 2 + Random.NextInt(3); + auto & Random = GetRandomProvider(); + int SpawnAmount = Random.RandInt(2, 4); for (int i = 0; i < SpawnAmount; ++i) { @@ -80,7 +80,7 @@ void cSlime::KilledBy(TakeDamageInfo & a_TDI) cSlime * NewSlime = new cSlime(m_Size / 2); NewSlime->SetPosition(GetPosX() + AddX, GetPosY() + 0.5, GetPosZ() + AddZ); - NewSlime->SetYaw(Random.NextFloat(1.0f) * 360.0f); + NewSlime->SetYaw(Random.RandReal(360.0f)); m_World->SpawnMobFinalize(NewSlime); } } diff --git a/src/Mobs/Villager.cpp b/src/Mobs/Villager.cpp index 46a448eb5..26462ba31 100644 --- a/src/Mobs/Villager.cpp +++ b/src/Mobs/Villager.cpp @@ -32,7 +32,7 @@ bool cVillager::DoTakeDamage(TakeDamageInfo & a_TDI) if ((a_TDI.Attacker != nullptr) && a_TDI.Attacker->IsPlayer()) { - if (m_World->GetTickRandomNumber(5) == 3) + if (GetRandomProvider().RandBool(1.0 / 6.0)) { m_World->BroadcastEntityStatus(*this, esVillagerAngry); } @@ -90,7 +90,7 @@ void cVillager::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) } // Don't always try to do a special action. Each tick has 1% to do a special action. - if (m_World->GetTickRandomNumber(99) != 0) + if (GetRandomProvider().RandBool(0.99)) { return; } @@ -150,7 +150,7 @@ void cVillager::HandleFarmerPrepareFarmCrops() m_VillagerAction = true; m_CropsPos = Vector3i(static_cast(GetPosX()) + X - 5, static_cast(GetPosY()) + Y - 3, static_cast(GetPosZ()) + Z - 5); - MoveToPosition(Vector3f(static_cast(m_CropsPos.x + 0.5), static_cast(m_CropsPos.y), static_cast(m_CropsPos.z + 0.5))); + MoveToPosition(Vector3d(m_CropsPos.x + 0.5, m_CropsPos.y + 0.0, m_CropsPos.z + 0.5)); return; } // for Y loop. } // Repeat the procces 5 times. diff --git a/src/Mobs/Witch.cpp b/src/Mobs/Witch.cpp index fa4074a54..3f56108ae 100644 --- a/src/Mobs/Witch.cpp +++ b/src/Mobs/Witch.cpp @@ -24,11 +24,11 @@ void cWitch::GetDrops(cItems & a_Drops, cEntity * a_Killer) { LootingLevel = a_Killer->GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchLooting); } - MTRand r1; - int DropTypeCount = (r1.randInt() % 3) + 1; + auto & r1 = GetRandomProvider(); + int DropTypeCount = r1.RandInt(1, 3); for (int i = 0; i < DropTypeCount; i++) { - int DropType = r1.randInt() % 7; + int DropType = r1.RandInt(6); switch (DropType) { case 0: AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_GLASS_BOTTLE); break; diff --git a/src/Mobs/Wolf.cpp b/src/Mobs/Wolf.cpp index 325c47183..560a6b2fa 100644 --- a/src/Mobs/Wolf.cpp +++ b/src/Mobs/Wolf.cpp @@ -138,7 +138,7 @@ void cWolf::ReceiveNearbyFightInfo(AString a_PlayerID, cPawn * a_Opponent, bool // If a player is asking for help and we already have a target, // there's a 50% chance of helping and a 50% chance of doing nothing // This helps spread a wolf pack's targets over several mobs - else if (m_World->GetTickRandomNumber(9)> 4) + else if (GetRandomProvider().RandBool()) { return; } @@ -179,7 +179,7 @@ void cWolf::OnRightClicked(cPlayer & a_Player) a_Player.GetInventory().RemoveOneEquippedItem(); } - if (m_World->GetTickRandomNumber(7) == 0) + if (GetRandomProvider().RandBool(0.125)) { // Taming succeeded SetMaxHealth(20); -- cgit v1.2.3