Skip to content

Commit

Permalink
FastRandom rewrite (cuberite#3754)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbell10 authored and Seadragon91 committed Jun 13, 2017
1 parent 9b0eb11 commit 360d8ea
Show file tree
Hide file tree
Showing 63 changed files with 458 additions and 408 deletions.
2 changes: 1 addition & 1 deletion src/BlockEntities/DropSpenserEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void cDropSpenserEntity::DropFromSlot(cChunk & a_Chunk, int a_SlotNum)
cItems Pickups;
Pickups.push_back(m_Contents.RemoveOneItem(a_SlotNum));

const int PickupSpeed = m_World->GetTickRandomNumber(4) + 2; // At least 2, at most 6
const int PickupSpeed = GetRandomProvider().RandInt(2, 6); // At least 2, at most 6
int PickupSpeedX = 0, PickupSpeedY = 0, PickupSpeedZ = 0;
switch (Meta & E_META_DROPSPENSER_FACING_MASK)
{
Expand Down
12 changes: 6 additions & 6 deletions src/BlockEntities/MobSpawnerEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool cMobSpawnerEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)

void cMobSpawnerEntity::ResetTimer(void)
{
m_SpawnDelay = static_cast<short>(200 + m_World->GetTickRandomNumber(600));
m_SpawnDelay = GetRandomProvider().RandInt<short>(200, 800);
m_World->BroadcastBlockEntity(m_PosX, m_PosY, m_PosZ);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ void cMobSpawnerEntity::SpawnEntity(void)

virtual bool Item(cChunk * a_Chunk)
{
cFastRandom Random;
auto & Random = GetRandomProvider();

bool EntitiesSpawned = false;
for (size_t i = 0; i < 4; i++)
Expand All @@ -148,9 +148,9 @@ void cMobSpawnerEntity::SpawnEntity(void)
break;
}

int RelX = static_cast<int>(m_RelX + static_cast<double>(Random.NextFloat() - Random.NextFloat()) * 4.0);
int RelY = m_RelY + Random.NextInt(3) - 1;
int RelZ = static_cast<int>(m_RelZ + static_cast<double>(Random.NextFloat() - Random.NextFloat()) * 4.0);
int RelX = m_RelX + static_cast<int>((Random.RandReal<double>() - Random.RandReal<double>()) * 4.0);
int RelY = m_RelY + Random.RandInt(-1, 1);
int RelZ = m_RelZ + static_cast<int>((Random.RandReal<double>() - Random.RandReal<double>()) * 4.0);

cChunk * Chunk = a_Chunk->GetRelNeighborChunkAdjustCoords(RelX, RelZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
Expand All @@ -171,7 +171,7 @@ void cMobSpawnerEntity::SpawnEntity(void)
}

Monster->SetPosition(PosX, RelY, PosZ);
Monster->SetYaw(Random.NextFloat() * 360.0f);
Monster->SetYaw(Random.RandReal(360.0f));
if (Chunk->GetWorld()->SpawnMobFinalize(Monster) != cEntity::INVALID_ID)
{
EntitiesSpawned = true;
Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/BlockBigFlower.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ class cBlockBigFlowerHandler :
)
)
{
MTRand r1;
if (r1.randInt(10) == 5)
if (GetRandomProvider().RandBool(0.10))
{
cItems Pickups;
if (FlowerMeta == E_META_BIG_FLOWER_DOUBLE_TALL_GRASS)
Expand Down
4 changes: 1 addition & 3 deletions src/Blocks/BlockCocoaPod.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class cBlockCocoaPodHandler :

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;
Expand Down
22 changes: 11 additions & 11 deletions src/Blocks/BlockCrops.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class cBlockCropsHandler :

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)
Expand All @@ -51,30 +51,30 @@ class cBlockCropsHandler :
{
case E_BLOCK_BEETROOTS:
{
char SeedCount = static_cast<char>(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<char>(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<char>(2) + rand.RandInt<char>(2)) / 2); // [1 .. 3] with high preference of 2
a_Pickups.emplace_back(E_ITEM_BEETROOT_SEEDS, SeedCount, 0);
char BeetrootCount = 1 + ((rand.RandInt<char>(2) + rand.RandInt<char>(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<char>(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<char>(2) + rand.RandInt<char>(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<char>(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<char>(2) + rand.RandInt<char>(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<char>(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<char>(2) + rand.RandInt<char>(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;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Blocks/BlockDeadBush.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ class cBlockDeadBushHandler :
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<char>(3);
if (chance != 0)
{
a_Pickups.push_back(cItem(E_ITEM_STICK, static_cast<char>(chance), 0));
a_Pickups.emplace_back(E_ITEM_STICK, chance, 0);
}
}

Expand All @@ -74,7 +73,7 @@ class cBlockDeadBushHandler :
// Spawn the pickups:
if (!Drops.empty())
{
MTRand r1;
auto & r1 = GetRandomProvider();

// Mid-block position first
double MicroX, MicroY, MicroZ;
Expand All @@ -83,8 +82,8 @@ class cBlockDeadBushHandler :
MicroZ = a_BlockZ + 0.5;

// Add random offset second
MicroX += r1.rand(1) - 0.5;
MicroZ += r1.rand(1) - 0.5;
MicroX += r1.RandReal<double>(-0.5, 0.5);
MicroZ += r1.RandReal<double>(-0.5, 0.5);

a_WorldInterface.SpawnItemPickups(Drops, MicroX, MicroY, MicroZ);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Blocks/BlockDirt.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ class cBlockDirtHandler :
}

// 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;
Expand Down
4 changes: 1 addition & 3 deletions src/Blocks/BlockGlowstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class cBlockGlowstoneHandler :

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<char>(2 + Random.NextInt(3)), 0));
a_Pickups.emplace_back(E_ITEM_GLOWSTONE_DUST, GetRandomProvider().RandInt<char>(2, 4), 0);
}

virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/BlockGravel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class cBlockGravelHandler :

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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Blocks/BlockHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<double>(-0.5, 0.5);
MicroZ += r1.RandReal<double>(-0.5, 0.5);

a_WorldInterface.SpawnItemPickups(Pickups, MicroX, MicroY, MicroZ);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Blocks/BlockLeaves.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ class cBlockLeavesHandler :

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(
Expand All @@ -66,7 +66,7 @@ class cBlockLeavesHandler :
// 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));
}
Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/BlockMelon.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class cBlockMelonHandler :

virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
{
cFastRandom Random;
a_Pickups.push_back(cItem(E_ITEM_MELON_SLICE, static_cast<char>(3 + Random.NextInt(5)), 0));
a_Pickups.emplace_back(E_ITEM_MELON_SLICE, GetRandomProvider().RandInt<char>(3, 7), 0);
}

virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Expand Down
6 changes: 3 additions & 3 deletions src/Blocks/BlockMobHead.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class cBlockMobHeadHandler :

cItems Pickups;
Pickups.Add(E_ITEM_HEAD, 1, static_cast<short>(MobHeadEntity->GetType()));
MTRand r1;
auto & r1 = GetRandomProvider();

// Mid-block position first
double MicroX, MicroY, MicroZ;
Expand All @@ -51,8 +51,8 @@ class cBlockMobHeadHandler :
MicroZ = MobHeadEntity->GetPosZ() + 0.5;

// Add random offset second
MicroX += r1.rand(1) - 0.5;
MicroZ += r1.rand(1) - 0.5;
MicroX += r1.RandReal<double>(-0.5, 0.5);
MicroZ += r1.RandReal<double>(-0.5, 0.5);

MobHeadEntity->GetWorld()->SpawnItemPickups(Pickups, MicroX, MicroY, MicroZ);
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Blocks/BlockMobSpawner.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class cBlockMobSpawnerHandler :
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<double>(a_BlockX), static_cast<double>(a_BlockY + 1), static_cast<double>(a_BlockZ), Reward);
}
} ;
4 changes: 2 additions & 2 deletions src/Blocks/BlockNetherWart.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class cBlockNetherWartHandler :

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<char>(1 + (rand.NextInt(3) + rand.NextInt(3))) / 2, 0));
a_Pickups.emplace_back(E_ITEM_NETHER_WART, 1 + (rand.RandInt<char>(2) + rand.RandInt<char>(2)) / 2, 0);
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions src/Blocks/BlockOre.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ class cBlockOreHandler :

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<char>(4 + Random.NextInt(5)), 4));
a_Pickups.emplace_back(E_ITEM_DYE, Random.RandInt<char>(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<char>(4 + Random.NextInt(2)), 0));
a_Pickups.emplace_back(E_ITEM_REDSTONE_DUST, Random.RandInt<char>(4, 5), 0);
break;
}
case E_BLOCK_DIAMOND_ORE:
Expand Down Expand Up @@ -84,7 +84,7 @@ class cBlockOreHandler :
return;
}

cFastRandom Random;
auto & Random = GetRandomProvider();
int Reward = 0;

switch (m_BlockType)
Expand All @@ -93,27 +93,27 @@ class cBlockOreHandler :
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;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/BlockPlant.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ class cBlockPlant : public cBlockHandler
*/
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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/BlockPortal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class cBlockPortalHandler :

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;
}
Expand Down
Loading

0 comments on commit 360d8ea

Please sign in to comment.