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/Blocks/BlockBigFlower.h | 3 +-- src/Blocks/BlockCocoaPod.h | 4 +--- src/Blocks/BlockCrops.h | 22 +++++++++++----------- src/Blocks/BlockDeadBush.h | 11 +++++------ src/Blocks/BlockDirt.h | 8 ++++---- src/Blocks/BlockGlowstone.h | 4 +--- src/Blocks/BlockGravel.h | 3 +-- src/Blocks/BlockHandler.cpp | 6 +++--- src/Blocks/BlockLeaves.h | 12 ++++++------ src/Blocks/BlockMelon.h | 3 +-- src/Blocks/BlockMobHead.h | 6 +++--- src/Blocks/BlockMobSpawner.h | 4 ++-- src/Blocks/BlockNetherWart.h | 4 ++-- src/Blocks/BlockOre.h | 16 ++++++++-------- src/Blocks/BlockPlant.h | 3 +-- src/Blocks/BlockPortal.h | 3 +-- src/Blocks/BlockSapling.h | 6 +++--- src/Blocks/BlockSeaLantern.h | 3 +-- src/Blocks/BlockTallGrass.h | 9 ++++----- 19 files changed, 59 insertions(+), 71 deletions(-) (limited to 'src/Blocks') diff --git a/src/Blocks/BlockBigFlower.h b/src/Blocks/BlockBigFlower.h index fe7f47b71..8ff07fdcd 100644 --- a/src/Blocks/BlockBigFlower.h +++ b/src/Blocks/BlockBigFlower.h @@ -67,8 +67,7 @@ public: ) ) { - MTRand r1; - if (r1.randInt(10) == 5) + if (GetRandomProvider().RandBool(0.10)) { cItems Pickups; if (FlowerMeta == E_META_BIG_FLOWER_DOUBLE_TALL_GRASS) diff --git a/src/Blocks/BlockCocoaPod.h b/src/Blocks/BlockCocoaPod.h index a48bfdfc2..035cc2f4f 100644 --- a/src/Blocks/BlockCocoaPod.h +++ b/src/Blocks/BlockCocoaPod.h @@ -30,9 +30,7 @@ public: virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override { - cFastRandom Random; - - if (Random.NextInt(5) == 0) + if (GetRandomProvider().RandBool(0.20)) { NIBBLETYPE Meta = a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ); NIBBLETYPE TypeMeta = Meta & 0x03; diff --git a/src/Blocks/BlockCrops.h b/src/Blocks/BlockCrops.h index 5ca264774..378505430 100644 --- a/src/Blocks/BlockCrops.h +++ b/src/Blocks/BlockCrops.h @@ -26,7 +26,7 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_Meta) override { - cFastRandom rand; + auto & rand = GetRandomProvider(); // If not fully grown, drop the "seed" of whatever is growing: if (a_Meta < RipeMeta) @@ -51,30 +51,30 @@ public: { case E_BLOCK_BEETROOTS: { - char SeedCount = static_cast(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2); // [1 .. 3] with high preference of 2 - a_Pickups.push_back(cItem(E_ITEM_BEETROOT_SEEDS, SeedCount, 0)); - char BeetrootCount = static_cast(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2); // [1 .. 3] with high preference of 2 - a_Pickups.push_back(cItem(E_ITEM_BEETROOT, BeetrootCount, 0)); + char SeedCount = 1 + ((rand.RandInt(2) + rand.RandInt(2)) / 2); // [1 .. 3] with high preference of 2 + a_Pickups.emplace_back(E_ITEM_BEETROOT_SEEDS, SeedCount, 0); + char BeetrootCount = 1 + ((rand.RandInt(2) + rand.RandInt(2)) / 2); // [1 .. 3] with high preference of 2 + a_Pickups.emplace_back(E_ITEM_BEETROOT, BeetrootCount, 0); break; } case E_BLOCK_CROPS: { - a_Pickups.push_back(cItem(E_ITEM_WHEAT, 1, 0)); - a_Pickups.push_back(cItem(E_ITEM_SEEDS, static_cast(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0)); // [1 .. 3] with high preference of 2 + a_Pickups.emplace_back(E_ITEM_WHEAT, 1, 0); + a_Pickups.emplace_back(E_ITEM_SEEDS, 1 + ((rand.RandInt(2) + rand.RandInt(2)) / 2), 0); // [1 .. 3] with high preference of 2 break; } case E_BLOCK_CARROTS: { - a_Pickups.push_back(cItem(E_ITEM_CARROT, static_cast(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0)); // [1 .. 3] with high preference of 2 + a_Pickups.emplace_back(E_ITEM_CARROT, 1 + ((rand.RandInt(2) + rand.RandInt(2)) / 2), 0); // [1 .. 3] with high preference of 2 break; } case E_BLOCK_POTATOES: { - a_Pickups.push_back(cItem(E_ITEM_POTATO, static_cast(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0)); // [1 .. 3] with high preference of 2 - if (rand.NextInt(21) == 0) + a_Pickups.emplace_back(E_ITEM_POTATO, 1 + ((rand.RandInt(2) + rand.RandInt(2)) / 2), 0); // [1 .. 3] with high preference of 2 + if (rand.RandBool(0.05)) { // With a 5% chance, drop a poisonous potato as well - a_Pickups.push_back(cItem(E_ITEM_POISONOUS_POTATO, 1, 0)); + a_Pickups.emplace_back(E_ITEM_POISONOUS_POTATO, 1, 0); } break; } diff --git a/src/Blocks/BlockDeadBush.h b/src/Blocks/BlockDeadBush.h index 0e81d6c2f..f9ce9a9ff 100644 --- a/src/Blocks/BlockDeadBush.h +++ b/src/Blocks/BlockDeadBush.h @@ -46,11 +46,10 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { // Drop 0-3 sticks - cFastRandom random; - int chance = random.NextInt(3); + char chance = GetRandomProvider().RandInt(3); if (chance != 0) { - a_Pickups.push_back(cItem(E_ITEM_STICK, static_cast(chance), 0)); + a_Pickups.emplace_back(E_ITEM_STICK, chance, 0); } } @@ -74,7 +73,7 @@ public: // Spawn the pickups: if (!Drops.empty()) { - MTRand r1; + auto & r1 = GetRandomProvider(); // Mid-block position first double MicroX, MicroY, MicroZ; @@ -83,8 +82,8 @@ public: MicroZ = a_BlockZ + 0.5; // Add random offset second - MicroX += r1.rand(1) - 0.5; - MicroZ += r1.rand(1) - 0.5; + MicroX += r1.RandReal(-0.5, 0.5); + MicroZ += r1.RandReal(-0.5, 0.5); a_WorldInterface.SpawnItemPickups(Drops, MicroX, MicroY, MicroZ); } diff --git a/src/Blocks/BlockDirt.h b/src/Blocks/BlockDirt.h index 33325d53a..3712e22f7 100644 --- a/src/Blocks/BlockDirt.h +++ b/src/Blocks/BlockDirt.h @@ -70,12 +70,12 @@ public: } // Grass spreads to adjacent dirt blocks: - cFastRandom rand; + auto & rand = GetRandomProvider(); for (int i = 0; i < 2; i++) // Pick two blocks to grow to { - int OfsX = rand.NextInt(3) - 1; // [-1 .. 1] - int OfsY = rand.NextInt(5) - 3; // [-3 .. 1] - int OfsZ = rand.NextInt(3) - 1; // [-1 .. 1] + int OfsX = rand.RandInt(-1, 1); + int OfsY = rand.RandInt(-3, 1); + int OfsZ = rand.RandInt(-1, 1); BLOCKTYPE DestBlock; NIBBLETYPE DestMeta; diff --git a/src/Blocks/BlockGlowstone.h b/src/Blocks/BlockGlowstone.h index cb36b9a73..4b0d72a93 100644 --- a/src/Blocks/BlockGlowstone.h +++ b/src/Blocks/BlockGlowstone.h @@ -18,10 +18,8 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { - cFastRandom Random; - // Add more than one dust - a_Pickups.push_back(cItem(E_ITEM_GLOWSTONE_DUST, static_cast(2 + Random.NextInt(3)), 0)); + a_Pickups.emplace_back(E_ITEM_GLOWSTONE_DUST, GetRandomProvider().RandInt(2, 4), 0); } virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override diff --git a/src/Blocks/BlockGravel.h b/src/Blocks/BlockGravel.h index 7bd68a050..6229d1d75 100644 --- a/src/Blocks/BlockGravel.h +++ b/src/Blocks/BlockGravel.h @@ -18,8 +18,7 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { - cFastRandom Random; - if (Random.NextInt(10) == 0) + if (GetRandomProvider().RandBool(0.10)) { a_Pickups.Add(E_ITEM_FLINT, 1, 0); } diff --git a/src/Blocks/BlockHandler.cpp b/src/Blocks/BlockHandler.cpp index faed798ff..36b20088c 100644 --- a/src/Blocks/BlockHandler.cpp +++ b/src/Blocks/BlockHandler.cpp @@ -526,7 +526,7 @@ void cBlockHandler::DropBlock(cChunkInterface & a_ChunkInterface, cWorldInterfac if (!Pickups.empty()) { - MTRand r1; + auto & r1 = GetRandomProvider(); // Mid-block position first double MicroX, MicroY, MicroZ; @@ -535,8 +535,8 @@ void cBlockHandler::DropBlock(cChunkInterface & a_ChunkInterface, cWorldInterfac MicroZ = a_BlockZ + 0.5; // Add random offset second - MicroX += r1.rand(1) - 0.5; - MicroZ += r1.rand(1) - 0.5; + MicroX += r1.RandReal(-0.5, 0.5); + MicroZ += r1.RandReal(-0.5, 0.5); a_WorldInterface.SpawnItemPickups(Pickups, MicroX, MicroY, MicroZ); } diff --git a/src/Blocks/BlockLeaves.h b/src/Blocks/BlockLeaves.h index 73c93116e..1f25ac49e 100644 --- a/src/Blocks/BlockLeaves.h +++ b/src/Blocks/BlockLeaves.h @@ -37,22 +37,22 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { - cFastRandom rand; + auto & rand = GetRandomProvider(); // There is a chance to drop a sapling that varies depending on the type of leaf broken. // TODO: Take into account fortune for sapling drops. - int chance; + double chance = 0.0; if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_JUNGLE)) { // Jungle leaves have a 2.5% chance of dropping a sapling. - chance = rand.NextInt(40); + chance = 0.025; } else { // Other leaves have a 5% chance of dropping a sapling. - chance = rand.NextInt(20); + chance = 0.05; } - if (chance == 0) + if (rand.RandBool(chance)) { a_Pickups.push_back( cItem( @@ -66,7 +66,7 @@ public: // 0.5 % chance of dropping an apple, if the leaves' type is Apple Leaves if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_APPLE)) { - if (rand.NextInt(200) == 0) + if (rand.RandBool(0.005)) { a_Pickups.push_back(cItem(E_ITEM_RED_APPLE, 1, 0)); } diff --git a/src/Blocks/BlockMelon.h b/src/Blocks/BlockMelon.h index a9723dcec..baf3053e1 100644 --- a/src/Blocks/BlockMelon.h +++ b/src/Blocks/BlockMelon.h @@ -18,8 +18,7 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { - cFastRandom Random; - a_Pickups.push_back(cItem(E_ITEM_MELON_SLICE, static_cast(3 + Random.NextInt(5)), 0)); + a_Pickups.emplace_back(E_ITEM_MELON_SLICE, GetRandomProvider().RandInt(3, 7), 0); } virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override diff --git a/src/Blocks/BlockMobHead.h b/src/Blocks/BlockMobHead.h index a271c3b43..930a1a921 100644 --- a/src/Blocks/BlockMobHead.h +++ b/src/Blocks/BlockMobHead.h @@ -42,7 +42,7 @@ public: cItems Pickups; Pickups.Add(E_ITEM_HEAD, 1, static_cast(MobHeadEntity->GetType())); - MTRand r1; + auto & r1 = GetRandomProvider(); // Mid-block position first double MicroX, MicroY, MicroZ; @@ -51,8 +51,8 @@ public: MicroZ = MobHeadEntity->GetPosZ() + 0.5; // Add random offset second - MicroX += r1.rand(1) - 0.5; - MicroZ += r1.rand(1) - 0.5; + MicroX += r1.RandReal(-0.5, 0.5); + MicroZ += r1.RandReal(-0.5, 0.5); MobHeadEntity->GetWorld()->SpawnItemPickups(Pickups, MicroX, MicroY, MicroZ); return false; diff --git a/src/Blocks/BlockMobSpawner.h b/src/Blocks/BlockMobSpawner.h index a6205490f..b6493c5b3 100644 --- a/src/Blocks/BlockMobSpawner.h +++ b/src/Blocks/BlockMobSpawner.h @@ -45,8 +45,8 @@ public: return; } - cFastRandom Random; - int Reward = 15 + Random.NextInt(15) + Random.NextInt(15); + auto & Random = GetRandomProvider(); + int Reward = 15 + Random.RandInt(14) + Random.RandInt(14); a_WorldInterface.SpawnExperienceOrb(static_cast(a_BlockX), static_cast(a_BlockY + 1), static_cast(a_BlockZ), Reward); } } ; diff --git a/src/Blocks/BlockNetherWart.h b/src/Blocks/BlockNetherWart.h index 0e3497688..aa7144458 100644 --- a/src/Blocks/BlockNetherWart.h +++ b/src/Blocks/BlockNetherWart.h @@ -21,12 +21,12 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_Meta) override { - cFastRandom rand; + auto & rand = GetRandomProvider(); if (a_Meta == 0x3) { // Fully grown, drop the entire produce: - a_Pickups.push_back(cItem(E_ITEM_NETHER_WART, static_cast(1 + (rand.NextInt(3) + rand.NextInt(3))) / 2, 0)); + a_Pickups.emplace_back(E_ITEM_NETHER_WART, 1 + (rand.RandInt(2) + rand.RandInt(2)) / 2, 0); } else { diff --git a/src/Blocks/BlockOre.h b/src/Blocks/BlockOre.h index 70d599843..202672cb5 100644 --- a/src/Blocks/BlockOre.h +++ b/src/Blocks/BlockOre.h @@ -20,19 +20,19 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { - cFastRandom Random; + auto & Random = GetRandomProvider(); switch (m_BlockType) { case E_BLOCK_LAPIS_ORE: { - a_Pickups.push_back(cItem(E_ITEM_DYE, static_cast(4 + Random.NextInt(5)), 4)); + a_Pickups.emplace_back(E_ITEM_DYE, Random.RandInt(4, 8), 4); break; } case E_BLOCK_REDSTONE_ORE: case E_BLOCK_REDSTONE_ORE_GLOWING: { - a_Pickups.push_back(cItem(E_ITEM_REDSTONE_DUST, static_cast(4 + Random.NextInt(2)), 0)); + a_Pickups.emplace_back(E_ITEM_REDSTONE_DUST, Random.RandInt(4, 5), 0); break; } case E_BLOCK_DIAMOND_ORE: @@ -84,7 +84,7 @@ public: return; } - cFastRandom Random; + auto & Random = GetRandomProvider(); int Reward = 0; switch (m_BlockType) @@ -93,27 +93,27 @@ public: case E_BLOCK_LAPIS_ORE: { // Lapis and nether quartz get 2 - 5 experience - Reward = Random.NextInt(4) + 2; + Reward = Random.RandInt(2, 5); break; } case E_BLOCK_REDSTONE_ORE: case E_BLOCK_REDSTONE_ORE_GLOWING: { // Redstone gets 1 - 5 experience - Reward = Random.NextInt(5) + 1; + Reward = Random.RandInt(1, 5); break; } case E_BLOCK_DIAMOND_ORE: case E_BLOCK_EMERALD_ORE: { // Diamond and emerald get 3 - 7 experience - Reward = Random.NextInt(5) + 3; + Reward = Random.RandInt(3, 7); break; } case E_BLOCK_COAL_ORE: { // Coal gets 0 - 2 experience - Reward = Random.NextInt(3); + Reward = Random.RandInt(2); break; } diff --git a/src/Blocks/BlockPlant.h b/src/Blocks/BlockPlant.h index 0155dc466..d7c5d9f83 100644 --- a/src/Blocks/BlockPlant.h +++ b/src/Blocks/BlockPlant.h @@ -75,10 +75,9 @@ protected: */ virtual PlantAction CanGrow(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) { - cFastRandom rand; // Plant can grow if it has the required amount of light, and it passes a random chance based on surrounding blocks PlantAction Action = HasEnoughLight(a_Chunk, a_RelX, a_RelY, a_RelZ); - if ((Action == paGrowth) && (rand.NextInt(GetGrowthChance(a_Chunk, a_RelX, a_RelY, a_RelZ)) != 0)) + if ((Action == paGrowth) && !GetRandomProvider().RandBool(1.0 / GetGrowthChance(a_Chunk, a_RelX, a_RelY, a_RelZ))) { Action = paStay; } diff --git a/src/Blocks/BlockPortal.h b/src/Blocks/BlockPortal.h index 504c018fb..6b5c5c0e0 100644 --- a/src/Blocks/BlockPortal.h +++ b/src/Blocks/BlockPortal.h @@ -40,8 +40,7 @@ public: virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override { - cFastRandom Random; - if (Random.NextInt(2000) != 0) + if (GetRandomProvider().RandBool(0.9995)) { return; } diff --git a/src/Blocks/BlockSapling.h b/src/Blocks/BlockSapling.h index b7d2f163b..a356eda8f 100644 --- a/src/Blocks/BlockSapling.h +++ b/src/Blocks/BlockSapling.h @@ -37,16 +37,16 @@ public: // Only grow if we have the right amount of light if (Light > 8) { - cFastRandom random; + auto & random = GetRandomProvider(); // Only grow if we are in the right growth stage and have the right amount of space around them. - if (((Meta & 0x08) != 0) && (random.NextInt(99) < 45) && CanGrowAt(a_Chunk, a_RelX, a_RelY, a_RelZ, Meta)) + if (((Meta & 0x08) != 0) && random.RandBool(0.45) && CanGrowAt(a_Chunk, a_RelX, a_RelY, a_RelZ, Meta)) { int BlockX = a_RelX + a_Chunk.GetPosX() * cChunkDef::Width; int BlockZ = a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width; a_Chunk.GetWorld()->GrowTree(BlockX, a_RelY, BlockZ); } // Only move to the next growth stage if we haven't gone there yet - else if (((Meta & 0x08) == 0) && (random.NextInt(99) < 45)) + else if (((Meta & 0x08) == 0) && random.RandBool(0.45)) { a_Chunk.SetMeta(a_RelX, a_RelY, a_RelZ, Meta | 0x08); } diff --git a/src/Blocks/BlockSeaLantern.h b/src/Blocks/BlockSeaLantern.h index 3454259b5..691e7de10 100644 --- a/src/Blocks/BlockSeaLantern.h +++ b/src/Blocks/BlockSeaLantern.h @@ -20,8 +20,7 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { // Reset meta to 0 - cFastRandom Random; - a_Pickups.push_back(cItem(E_ITEM_PRISMARINE_CRYSTALS, static_cast(2 + Random.NextInt(2)), 0)); + a_Pickups.emplace_back(E_ITEM_PRISMARINE_CRYSTALS, GetRandomProvider().RandInt(2, 3), 0); } } ; diff --git a/src/Blocks/BlockTallGrass.h b/src/Blocks/BlockTallGrass.h index 746c67617..fb65bca65 100644 --- a/src/Blocks/BlockTallGrass.h +++ b/src/Blocks/BlockTallGrass.h @@ -26,8 +26,7 @@ public: virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override { // Drop seeds, sometimes - cFastRandom Random; - if (Random.NextInt(8) == 0) + if (GetRandomProvider().RandBool(0.125)) { a_Pickups.push_back(cItem(E_ITEM_SEEDS, 1, 0)); } @@ -47,7 +46,7 @@ public: // Spawn the pickups: if (!Drops.empty()) { - MTRand r1; + auto & r1 = GetRandomProvider(); // Mid-block position first double MicroX, MicroY, MicroZ; @@ -56,8 +55,8 @@ public: MicroZ = a_BlockZ + 0.5; // Add random offset second - MicroX += r1.rand(1) - 0.5; - MicroZ += r1.rand(1) - 0.5; + MicroX += r1.RandReal(-0.5, 0.5); + MicroZ += r1.RandReal(-0.5, 0.5); a_WorldInterface.SpawnItemPickups(Drops, MicroX, MicroY, MicroZ); } -- cgit v1.2.3