1
0

Implement random ticks more faithfully

+ Make it pick 3 blocks per section, instead of 50 randomly throughout the chunk
This commit is contained in:
Tiger Wang 2021-04-10 20:01:01 +01:00
parent 7080c4d3e2
commit d06930de75
2 changed files with 22 additions and 14 deletions

View File

@ -849,27 +849,35 @@ void cChunk::CheckBlocks()
void cChunk::TickBlocks(void) void cChunk::TickBlocks(void)
{ {
cChunkInterface ChunkInterface(this->GetWorld()->GetChunkMap()); cChunkInterface ChunkInterface(m_World->GetChunkMap());
cBlockInServerPluginInterface PluginInterface(*this->GetWorld()); cBlockInServerPluginInterface PluginInterface(*m_World);
// Tick random blocks, but the first one should be m_BlockToTick (so that SetNextBlockToTick() works):
cBlockHandler::For(GetBlock(m_BlockToTick)).OnUpdate(ChunkInterface, *m_World, PluginInterface, *this, m_BlockToTick);
// Tick random blocks, but the first one should be m_BlockToTick (so that SetNextBlockToTick() works)
auto Idx = cChunkDef::MakeIndexNoCheck(m_BlockToTick);
auto & Random = GetRandomProvider(); auto & Random = GetRandomProvider();
for (int i = 0; i < 50; ++i) // Set a new random coord for the next tick:
m_BlockToTick = cChunkDef::IndexToCoordinate(Random.RandInt<size_t>(cChunkDef::NumBlocks - 1));
// Choose a number of blocks for each section to randomly tick.
// http://minecraft.fandom.com/wiki/Tick#Random_tick
for (size_t Y = 0; Y < cChunkDef::NumSections; ++Y)
{ {
auto Pos = cChunkDef::IndexToCoordinate(static_cast<size_t>(Idx)); const auto Section = m_BlockData.GetSection(Y);
Idx = Random.RandInt(cChunkDef::NumBlocks - 1); if (Section == nullptr)
if (Pos.y > cChunkDef::GetHeight(m_HeightMap, Pos.x, Pos.z))
{ {
continue; // It's all air up here continue;
} }
cBlockHandler::For(GetBlock(Pos)).OnUpdate(ChunkInterface, *this->GetWorld(), PluginInterface, *this, Pos); for (int Tick = 0; Tick != 3; Tick++) // TODO: configurability via gamerule randomTickSpeed
} // for i {
const auto Index = Random.RandInt<size_t>(ChunkBlockData::SectionBlockCount - 1);
const auto Position = cChunkDef::IndexToCoordinate(Y * ChunkBlockData::SectionBlockCount + Index);
// Set a new random coord for the next tick: cBlockHandler::For((*Section)[Index]).OnUpdate(ChunkInterface, *m_World, PluginInterface, *this, Position);
m_BlockToTick = cChunkDef::IndexToCoordinate(static_cast<size_t>(Idx)); }
}
} }

View File

@ -552,7 +552,7 @@ private:
/** Checks the block scheduled for checking in m_ToTickBlocks[] */ /** Checks the block scheduled for checking in m_ToTickBlocks[] */
void CheckBlocks(); void CheckBlocks();
/** Ticks several random blocks in the chunk */ /** Ticks several random blocks in the chunk. */
void TickBlocks(void); void TickBlocks(void);
/** Adds snow to the top of snowy biomes and hydrates farmland / fills cauldrons in rainy biomes */ /** Adds snow to the top of snowy biomes and hydrates farmland / fills cauldrons in rainy biomes */