From 73a30d302bb6c62a0161a8a2f742b1e752f20871 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Thu, 7 Jun 2012 19:06:27 +0000 Subject: [PATCH] BlockChecking split into a separate function git-svn-id: http://mc-server.googlecode.com/svn/trunk@571 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cChunk.cpp | 205 +++++++++++++++++++++++----------------------- source/cChunk.h | 5 +- 2 files changed, 107 insertions(+), 103 deletions(-) diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 0cdebc50..eba434ed 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -402,109 +402,8 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) m_UnloadQuery.remove( *m_UnloadQuery.begin() ); } - cCSLock Lock2(m_CSBlockLists); - unsigned int NumTickBlocks = m_ToTickBlocks.size(); - Lock2.Unlock(); - - if ( NumTickBlocks > 0 ) - { - Lock2.Lock(); - std::deque< unsigned int > ToTickBlocks = m_ToTickBlocks; - m_ToTickBlocks.clear(); - Lock2.Unlock(); + CheckBlocks(); - bool isRedstone = false; - for ( std::deque< unsigned int >::iterator itr = ToTickBlocks.begin(); itr != ToTickBlocks.end(); ++itr ) - { - unsigned int index = (*itr); - Vector3i BlockPos = IndexToCoordinate( index ); - - BLOCKTYPE BlockID = GetBlock( index ); - NIBBLETYPE BlockMeta = GetMeta(index); - switch ( BlockID ) - { - case E_BLOCK_REDSTONE_REPEATER_OFF: - case E_BLOCK_REDSTONE_REPEATER_ON: - case E_BLOCK_REDSTONE_WIRE: - { - isRedstone = true; - // fallthrough - } - - case E_BLOCK_CACTUS: - case E_BLOCK_REEDS: - case E_BLOCK_WOODEN_PRESSURE_PLATE: - case E_BLOCK_STONE_PRESSURE_PLATE: - case E_BLOCK_MINECART_TRACKS: - case E_BLOCK_SIGN_POST: - case E_BLOCK_CROPS: - case E_BLOCK_SAPLING: - case E_BLOCK_YELLOW_FLOWER: - case E_BLOCK_RED_ROSE: - case E_BLOCK_RED_MUSHROOM: - case E_BLOCK_BROWN_MUSHROOM: // Stuff that drops when block below is destroyed - { - if (GetBlock(BlockPos.x, BlockPos.y - 1, BlockPos.z) == E_BLOCK_AIR) - { - SetBlock( BlockPos, E_BLOCK_AIR, 0 ); - - Vector3i WorldPos = PositionToWorldPosition( BlockPos ); - - m_World->GetSimulatorManager()->WakeUp(WorldPos.x, WorldPos.y, WorldPos.z); - - cItems Pickups; - cBlockToPickup::ToPickup(BlockID, BlockMeta, E_ITEM_EMPTY, Pickups); - m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); - } - break; - } - - case E_BLOCK_REDSTONE_TORCH_OFF: - case E_BLOCK_REDSTONE_TORCH_ON: - { - isRedstone = true; - // fallthrough - } - - case E_BLOCK_TORCH: - { - char Dir = cTorch::MetaDataToDirection( GetNibble( m_BlockMeta, BlockPos ) ); - Vector3i WorldPos = PositionToWorldPosition( BlockPos ); - - Vector3i AttachedTo = WorldPos; - AddDirection( AttachedTo.x, AttachedTo.y, AttachedTo.z, Dir, true ); - if( m_World->GetBlock( AttachedTo ) == E_BLOCK_AIR ) - { - SetBlock( BlockPos, E_BLOCK_AIR, 0 ); - - m_World->GetSimulatorManager()->WakeUp(WorldPos.x, WorldPos.y, WorldPos.z); - - cItems Pickups; - cBlockToPickup::ToPickup(BlockID, BlockMeta, E_ITEM_EMPTY, Pickups); - m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); - } - break; - } - - case E_BLOCK_LADDER: - { - char Dir = cLadder::MetaDataToDirection( GetNibble( m_BlockMeta, BlockPos ) ); - Vector3i WorldPos = PositionToWorldPosition( BlockPos ); - Vector3i AttachedTo = WorldPos; - AddDirection( AttachedTo.x, AttachedTo.y, AttachedTo.z, Dir, true ); - if( m_World->GetBlock( AttachedTo ) == E_BLOCK_AIR ) - { - SetBlock( BlockPos, E_BLOCK_AIR, 0 ); - cItems Pickups; - cBlockToPickup::ToPickup(BlockID, BlockMeta, E_ITEM_EMPTY, Pickups); - m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); - } - break; - } - } // switch (BlockType) - } // for itr - ToTickBlocks[] - } - TickBlocks(a_TickRandom); // Tick block entities (furnaces) @@ -520,6 +419,108 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) + +void cChunk::CheckBlocks(void) +{ + cCSLock Lock2(m_CSBlockLists); + unsigned int NumTickBlocks = m_ToTickBlocks.size(); + Lock2.Unlock(); + + if (NumTickBlocks == 0) + { + return; + } + + Lock2.Lock(); + std::deque< unsigned int > ToTickBlocks = m_ToTickBlocks; + m_ToTickBlocks.clear(); + Lock2.Unlock(); + + for (std::deque< unsigned int >::const_iterator itr = ToTickBlocks.begin(); itr != ToTickBlocks.end(); ++itr) + { + unsigned int index = (*itr); + Vector3i BlockPos = IndexToCoordinate(index); + + BLOCKTYPE BlockType = GetBlock(index); + NIBBLETYPE BlockMeta = GetMeta (index); + switch (BlockType) + { + case E_BLOCK_REDSTONE_REPEATER_OFF: + case E_BLOCK_REDSTONE_REPEATER_ON: + case E_BLOCK_REDSTONE_WIRE: + case E_BLOCK_CACTUS: + case E_BLOCK_REEDS: + case E_BLOCK_WOODEN_PRESSURE_PLATE: + case E_BLOCK_STONE_PRESSURE_PLATE: + case E_BLOCK_MINECART_TRACKS: + case E_BLOCK_SIGN_POST: + case E_BLOCK_CROPS: + case E_BLOCK_SAPLING: + case E_BLOCK_YELLOW_FLOWER: + case E_BLOCK_RED_ROSE: + case E_BLOCK_RED_MUSHROOM: + case E_BLOCK_BROWN_MUSHROOM: // Stuff that drops when block below is destroyed + { + if (GetBlock(BlockPos.x, BlockPos.y - 1, BlockPos.z) == E_BLOCK_AIR) + { + SetBlock( BlockPos, E_BLOCK_AIR, 0 ); + + Vector3i WorldPos = PositionToWorldPosition( BlockPos ); + + m_World->GetSimulatorManager()->WakeUp(WorldPos.x, WorldPos.y, WorldPos.z); + + cItems Pickups; + cBlockToPickup::ToPickup(BlockType, BlockMeta, E_ITEM_EMPTY, Pickups); + m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); + } + break; + } + + case E_BLOCK_REDSTONE_TORCH_OFF: + case E_BLOCK_REDSTONE_TORCH_ON: + case E_BLOCK_TORCH: + { + char Dir = cTorch::MetaDataToDirection( GetNibble( m_BlockMeta, BlockPos ) ); + Vector3i WorldPos = PositionToWorldPosition( BlockPos ); + + Vector3i AttachedTo = WorldPos; + AddDirection( AttachedTo.x, AttachedTo.y, AttachedTo.z, Dir, true ); + if( m_World->GetBlock( AttachedTo ) == E_BLOCK_AIR ) + { + SetBlock( BlockPos, E_BLOCK_AIR, 0 ); + + m_World->GetSimulatorManager()->WakeUp(WorldPos.x, WorldPos.y, WorldPos.z); + + cItems Pickups; + cBlockToPickup::ToPickup(BlockType, BlockMeta, E_ITEM_EMPTY, Pickups); + m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); + } + break; + } + + case E_BLOCK_LADDER: + { + char Dir = cLadder::MetaDataToDirection( GetNibble( m_BlockMeta, BlockPos ) ); + Vector3i WorldPos = PositionToWorldPosition( BlockPos ); + Vector3i AttachedTo = WorldPos; + AddDirection( AttachedTo.x, AttachedTo.y, AttachedTo.z, Dir, true ); + if( m_World->GetBlock( AttachedTo ) == E_BLOCK_AIR ) + { + SetBlock( BlockPos, E_BLOCK_AIR, 0 ); + cItems Pickups; + cBlockToPickup::ToPickup(BlockType, BlockMeta, E_ITEM_EMPTY, Pickups); + m_World->SpawnItemPickups(Pickups, WorldPos.x, WorldPos.y, WorldPos.z); + } + break; + } + } // switch (BlockType) + } // for itr - ToTickBlocks[] +} + + + + + void cChunk::TickBlocks(MTRand & a_TickRandom) { // Tick dem blocks diff --git a/source/cChunk.h b/source/cChunk.h index 415205b3..6fe4e77f 100644 --- a/source/cChunk.h +++ b/source/cChunk.h @@ -227,7 +227,10 @@ private: // Makes a copy of the list cClientHandleList GetAllClients(void) const {return m_LoadedByClient; } - void TickBlocks(MTRand & a_TickRandom); + /// Checks the block scheduled for checking in m_ToTickBlocks[] + void CheckBlocks(void); + + void TickBlocks (MTRand & a_TickRandom); void TickGrass (int a_RelX, int a_RelY, int a_RelZ, MTRand & a_TickRandom); void TickMelonPumpkin(int a_RelX, int a_RelY, int a_RelZ, int a_BlockIdx, BLOCKTYPE a_BlockType, MTRand & a_TickRandom); void TickFarmland (int a_RelX, int a_RelY, int a_RelZ);