2011-12-26 18:23:05 -05:00
2012-01-29 14:28:19 -05:00
# include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
2011-10-03 14:41:19 -04:00
# ifndef _WIN32
2012-02-01 07:46:44 -05:00
# include <cstdlib>
2011-10-03 14:41:19 -04:00
# endif
2011-12-22 16:36:24 -05:00
2011-10-03 14:41:19 -04:00
# include "cChunk.h"
# include "cWorld.h"
2011-12-22 16:36:24 -05:00
# include "cWaterSimulator.h"
# include "cLavaSimulator.h"
2011-10-03 14:41:19 -04:00
# include "cClientHandle.h"
# include "cServer.h"
# include "zlib.h"
# include "Defines.h"
# include "cChestEntity.h"
# include "cFurnaceEntity.h"
# include "cSignEntity.h"
# include "cTorch.h"
# include "cLadder.h"
# include "cPickup.h"
2011-11-07 17:59:29 -05:00
# include "cRedstone.h"
2011-10-03 14:41:19 -04:00
# include "cItem.h"
# include "cNoise.h"
# include "cRoot.h"
2011-12-25 21:39:43 -05:00
# include "cWorldGenerator.h"
2011-11-07 17:59:29 -05:00
# include "cBlockToPickup.h"
2011-12-26 04:09:47 -05:00
# include "MersenneTwister.h"
2012-02-13 16:47:03 -05:00
# include "cPlayer.h"
2011-10-03 14:41:19 -04:00
# include "packets/cPacket_DestroyEntity.h"
# include "packets/cPacket_PreChunk.h"
# include "packets/cPacket_BlockChange.h"
# include "packets/cPacket_MapChunk.h"
# include "packets/cPacket_MultiBlock.h"
# include <json/json.h>
2012-01-29 14:28:19 -05:00
2011-10-21 17:25:29 -04:00
2011-10-03 14:41:19 -04:00
extern bool g_bWaterPhysics ;
2012-01-29 14:28:19 -05:00
2012-01-30 11:01:45 -05:00
2012-02-18 15:10:57 -05:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// sSetBlock:
sSetBlock : : sSetBlock ( int a_X , int a_Y , int a_Z , char a_BlockType , char a_BlockMeta ) // absolute block position
: x ( a_X )
, y ( a_Y )
, z ( a_Z )
, BlockType ( a_BlockType )
, BlockMeta ( a_BlockMeta )
{
cChunkMap : : AbsoluteToRelative ( x , y , z , ChunkX , ChunkZ ) ;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cChunk:
2012-02-21 11:27:30 -05:00
cChunk : : cChunk ( int a_X , int a_Y , int a_Z , cChunkMap * a_ChunkMap , cWorld * a_World )
2012-02-13 16:47:03 -05:00
: m_bCalculateLighting ( false )
2012-01-30 11:01:45 -05:00
, m_PosX ( a_X )
, m_PosY ( a_Y )
, m_PosZ ( a_Z )
, m_BlockType ( m_BlockData ) // Offset the pointers
, m_BlockMeta ( m_BlockType + c_NumBlocks )
2012-02-13 16:47:03 -05:00
, m_BlockLight ( m_BlockMeta + c_NumBlocks / 2 )
, m_BlockSkyLight ( m_BlockLight + c_NumBlocks / 2 )
2012-01-30 11:01:45 -05:00
, m_BlockTickNum ( 0 )
, m_BlockTickX ( 0 )
, m_BlockTickY ( 0 )
, m_BlockTickZ ( 0 )
, m_World ( a_World )
2012-02-21 11:27:30 -05:00
, m_ChunkMap ( a_ChunkMap )
2012-02-13 16:47:03 -05:00
, m_IsValid ( false )
2012-02-16 08:42:35 -05:00
, m_IsDirty ( false )
, m_IsSaving ( false )
2012-02-26 11:15:09 -05:00
, m_StayCount ( 0 )
2012-01-30 11:01:45 -05:00
{
2012-02-13 16:47:03 -05:00
// LOGINFO("### new cChunk (%i, %i) at %p, thread 0x%x ###", a_X, a_Z, this, GetCurrentThreadId());
2012-01-30 11:01:45 -05:00
}
2011-10-03 14:41:19 -04:00
cChunk : : ~ cChunk ( )
{
2012-02-13 16:47:03 -05:00
// LOGINFO("### delete cChunk() (%i, %i) from %p, thread 0x%x ###", m_PosX, m_PosZ, this, GetCurrentThreadId() );
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
delete * itr ;
}
2012-02-13 16:47:03 -05:00
m_BlockEntities . clear ( ) ;
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
// Remove and destroy all entities that are not players:
cEntityList Entities ;
for ( cEntityList : : const_iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
if ( ( * itr ) - > GetEntityType ( ) ! = cEntity : : E_PLAYER )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
Entities . push_back ( * itr ) ;
2011-10-03 14:41:19 -04:00
}
}
2012-02-13 16:47:03 -05:00
for ( cEntityList : : iterator itr = Entities . begin ( ) ; itr ! = Entities . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
( * itr ) - > RemoveFromChunk ( ) ;
( * itr ) - > Destroy ( ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
m_Entities . clear ( ) ;
2011-10-03 14:41:19 -04:00
}
2012-01-30 11:01:45 -05:00
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
void cChunk : : SetValid ( bool a_SendToClients )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
m_IsValid = true ;
2012-02-18 12:53:22 -05:00
m_World - > GetChunkMap ( ) - > ChunkValidated ( ) ;
2012-02-13 16:47:03 -05:00
if ( ! a_SendToClients )
{
return ;
}
if ( m_LoadedByClient . empty ( ) )
{
return ;
}
// Sending the chunk here interferes with the lighting done in the tick thread and results in the "invalid compressed data" on the client
/*
cPacket_PreChunk PreChunk ;
PreChunk . m_PosX = m_PosX ;
PreChunk . m_PosZ = m_PosZ ;
PreChunk . m_bLoad = true ;
cPacket_MapChunk MapChunk ( this ) ;
Broadcast ( & PreChunk ) ;
Broadcast ( & MapChunk ) ;
// Let all clients of this chunk know that it has been already sent to the client
for ( cClientHandleList : : iterator itr = m_LoadedByClient . begin ( ) ; itr ! = m_LoadedByClient . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
( * itr ) - > ChunkJustSent ( this ) ;
} // for itr - m_LoadedByClient[]
*/
}
2011-10-03 14:41:19 -04:00
2011-12-24 18:34:30 -05:00
2012-02-13 16:47:03 -05:00
bool cChunk : : CanUnload ( void )
{
2012-02-26 11:15:09 -05:00
return m_LoadedByClient . empty ( ) & & ! m_IsDirty & & ( m_StayCount = = 0 ) ;
2012-02-16 08:42:35 -05:00
}
void cChunk : : MarkSaving ( void )
{
m_IsSaving = true ;
}
void cChunk : : MarkSaved ( void )
{
if ( ! m_IsSaving )
{
return ;
}
m_IsDirty = false ;
}
void cChunk : : MarkLoaded ( void )
{
m_IsDirty = false ;
2012-02-18 12:53:22 -05:00
SetValid ( ) ;
2012-02-16 08:42:35 -05:00
}
2012-02-28 07:11:14 -05:00
void cChunk : : MarkLoadFailed ( void )
{
if ( m_IsValid )
{
return ;
}
m_HasLoadFailed = true ;
}
2012-02-16 08:42:35 -05:00
void cChunk : : GetAllData ( cChunkDataCallback * a_Callback )
{
a_Callback - > BlockData ( m_BlockData ) ;
for ( cEntityList : : iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
{
a_Callback - > Entity ( * itr ) ;
}
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
{
a_Callback - > BlockEntity ( * itr ) ;
}
}
void cChunk : : SetAllData ( const char * a_BlockData , cEntityList & a_Entities , cBlockEntityList & a_BlockEntities )
{
memcpy ( m_BlockData , a_BlockData , sizeof ( m_BlockData ) ) ;
// Clear the internal entities:
for ( cEntityList : : iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
{
if ( ( * itr ) - > GetEntityType ( ) = = cEntity : : E_PLAYER )
{
// Move players into the new entity list
a_Entities . push_back ( * itr ) ;
}
else
{
// Delete other entities (there should not be any, since we're now loading / generating the chunk)
LOGWARNING ( " cChunk: There is an unexpected entity #%d of type %s in chunk [%d, %d]; it will be deleted " ,
( * itr ) - > GetUniqueID ( ) , ( * itr ) - > GetClass ( ) ,
m_PosX , m_PosZ
) ;
2012-02-22 15:33:33 -05:00
// Assert because this is a very curious case. These lines were executed once before, when a player died, re spawned, and walked around a bit. It's uncertain why an entity would be in the chunk in this case.
ASSERT ( ! " Unexpected entity in chunk! " ) ;
( * itr ) - > Destroy ( ) ;
2012-02-16 08:42:35 -05:00
}
}
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
{
delete * itr ;
}
// Swap the entity lists:
std : : swap ( a_Entities , m_Entities ) ;
std : : swap ( a_BlockEntities , m_BlockEntities ) ;
// Create block entities that the loader didn't load; fill them with defaults
CreateBlockEntities ( ) ;
2012-02-16 10:40:38 -05:00
CalculateHeightmap ( ) ;
2012-02-28 07:11:14 -05:00
m_HasLoadFailed = false ;
2012-02-16 08:42:35 -05:00
}
2012-02-18 14:18:16 -05:00
/// Copies m_BlockData into a_Blocks, only the block types
void cChunk : : GetBlocks ( char * a_Blocks )
{
memcpy ( a_Blocks , m_BlockData , cChunk : : c_NumBlocks ) ;
}
2012-03-05 11:41:57 -05:00
/// Copies m_BlockData into a_Blocks, only the block types
void cChunk : : GetBlockData ( char * a_BlockData )
{
memcpy ( a_BlockData , m_BlockData , cChunk : : c_BlockDataSize ) ;
}
2012-02-16 08:42:35 -05:00
/// Returns true if there is a block entity at the coords specified
bool cChunk : : HasBlockEntityAt ( int a_BlockX , int a_BlockY , int a_BlockZ )
{
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
{
if (
( ( * itr ) - > GetPosX ( ) = = a_BlockX ) & &
( ( * itr ) - > GetPosY ( ) = = a_BlockY ) & &
( ( * itr ) - > GetPosZ ( ) = = a_BlockZ )
)
{
return true ;
}
} // for itr - m_BlockEntities[]
return false ;
2011-10-03 14:41:19 -04:00
}
2012-01-30 11:01:45 -05:00
2012-02-26 11:15:09 -05:00
/// Sets or resets the internal flag that prevents chunk from being unloaded
void cChunk : : Stay ( bool a_Stay )
{
m_StayCount + = ( a_Stay ? 1 : - 1 ) ;
ASSERT ( m_StayCount > = 0 ) ;
}
2012-02-08 07:36:54 -05:00
void cChunk : : Tick ( float a_Dt , MTRand & a_TickRandom )
2011-10-03 14:41:19 -04:00
{
2012-01-30 11:01:45 -05:00
if ( m_bCalculateLighting )
{
2011-10-03 14:41:19 -04:00
CalculateLighting ( ) ;
2012-01-30 11:01:45 -05:00
}
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
cCSLock Lock ( m_CSBlockLists ) ;
unsigned int PendingSendBlocks = m_PendingSendBlocks . size ( ) ;
2012-01-26 15:39:46 -05:00
if ( PendingSendBlocks > 1 )
{
cPacket_MultiBlock MultiBlock ;
MultiBlock . m_ChunkX = m_PosX ;
MultiBlock . m_ChunkZ = m_PosZ ;
MultiBlock . m_NumBlocks = ( short ) PendingSendBlocks ;
2012-03-01 20:22:06 -05:00
# if (MINECRAFT_1_2_2 == 1)
MultiBlock . m_Data = new cPacket_MultiBlock : : sBlockChange [ PendingSendBlocks ] ;
MultiBlock . m_DataSize = PendingSendBlocks * sizeof ( cPacket_MultiBlock : : sBlockChange ) ;
# else
2012-01-26 15:39:46 -05:00
MultiBlock . m_BlockCoordinates = new unsigned short [ PendingSendBlocks ] ;
MultiBlock . m_BlockTypes = new char [ PendingSendBlocks ] ;
MultiBlock . m_BlockMetas = new char [ PendingSendBlocks ] ;
2012-03-01 20:22:06 -05:00
# endif
2012-01-26 15:39:46 -05:00
//LOG("Sending multiblock packet for %i blocks", PendingSendBlocks );
for ( unsigned int i = 0 ; i < PendingSendBlocks ; i + + )
{
2012-02-13 16:47:03 -05:00
unsigned int index = m_PendingSendBlocks [ i ] ;
2012-03-03 15:55:16 -05:00
unsigned int Y = index % c_ChunkHeight ;
unsigned int Z = ( index / c_ChunkHeight ) % c_ChunkWidth ;
unsigned int X = ( index / ( c_ChunkHeight * c_ChunkWidth ) ) ;
2012-01-26 15:39:46 -05:00
2012-03-01 20:22:06 -05:00
# if (MINECRAFT_1_2_2 == 1)
2012-03-02 08:41:42 -05:00
unsigned int Coords = Y | Z < < 8 | X < < 12 ;
2012-03-05 11:41:57 -05:00
unsigned int Blocks = GetNibble ( m_BlockMeta , index ) | ( m_BlockType [ index ] < < 4 ) ;
2012-03-02 08:41:42 -05:00
MultiBlock . m_Data [ i ] . Data = Coords < < 16 | Blocks ;
2012-03-01 20:22:06 -05:00
# else
2012-01-26 15:39:46 -05:00
MultiBlock . m_BlockCoordinates [ i ] = ( Z & 0xf ) | ( X & 0xf ) < < 4 | ( Y & 0xff ) < < 8 ;
//LOG("X: %i Y: %i Z: %i Combo: 0x%04x", X, Y, Z, MultiBlock.m_BlockCoordinates[i] );
MultiBlock . m_BlockTypes [ i ] = m_BlockType [ index ] ;
2012-03-05 11:41:57 -05:00
MultiBlock . m_BlockMetas [ i ] = GetNibble ( m_BlockMeta , index ) ;
2012-03-01 20:22:06 -05:00
# endif
2012-01-26 15:39:46 -05:00
}
2012-02-13 16:47:03 -05:00
m_PendingSendBlocks . clear ( ) ;
PendingSendBlocks = m_PendingSendBlocks . size ( ) ;
2012-01-26 15:39:46 -05:00
Broadcast ( MultiBlock ) ;
}
2011-10-03 14:41:19 -04:00
if ( PendingSendBlocks > 0 )
{
for ( unsigned int i = 0 ; i < PendingSendBlocks ; i + + )
{
2012-02-13 16:47:03 -05:00
unsigned int index = m_PendingSendBlocks [ i ] ;
2012-03-03 15:55:16 -05:00
int Y = index % c_ChunkHeight ;
int Z = ( index / c_ChunkHeight ) % c_ChunkWidth ;
int X = ( index / ( c_ChunkHeight * c_ChunkWidth ) ) ;
2011-10-03 14:41:19 -04:00
cPacket_BlockChange BlockChange ;
2012-03-03 15:55:16 -05:00
BlockChange . m_PosX = X + m_PosX * c_ChunkWidth ;
BlockChange . m_PosY = ( char ) ( Y + m_PosY * c_ChunkHeight ) ;
BlockChange . m_PosZ = Z + m_PosZ * c_ChunkWidth ;
2011-10-03 14:41:19 -04:00
BlockChange . m_BlockType = m_BlockType [ index ] ;
2012-03-05 11:41:57 -05:00
BlockChange . m_BlockMeta = GetNibble ( m_BlockMeta , index ) ;
2011-10-03 14:41:19 -04:00
Broadcast ( BlockChange ) ;
}
2012-02-13 16:47:03 -05:00
m_PendingSendBlocks . clear ( ) ;
2011-10-03 14:41:19 -04:00
}
2012-01-30 16:54:40 -05:00
Lock . Unlock ( ) ;
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
while ( ! m_UnloadQuery . empty ( ) )
2011-10-03 14:41:19 -04:00
{
cPacket_PreChunk UnloadPacket ;
UnloadPacket . m_PosX = GetPosX ( ) ;
UnloadPacket . m_PosZ = GetPosZ ( ) ;
UnloadPacket . m_bLoad = false ; // Unload
2012-02-13 16:47:03 -05:00
( * m_UnloadQuery . begin ( ) ) - > Send ( UnloadPacket ) ;
m_UnloadQuery . remove ( * m_UnloadQuery . begin ( ) ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
cCSLock Lock2 ( m_CSBlockLists ) ;
2012-02-28 09:22:03 -05:00
unsigned int NumTickBlocks = m_ToTickBlocks . size ( ) ;
2012-01-30 16:54:40 -05:00
Lock2 . Unlock ( ) ;
2011-10-03 14:41:19 -04:00
2012-02-28 09:22:03 -05:00
if ( NumTickBlocks > 0 )
{
Lock2 . Lock ( ) ;
std : : deque < unsigned int > ToTickBlocks = m_ToTickBlocks ;
m_ToTickBlocks . clear ( ) ;
Lock2 . Unlock ( ) ;
bool isRedstone = false ;
for ( std : : deque < unsigned int > : : iterator itr = ToTickBlocks . begin ( ) ; itr ! = ToTickBlocks . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-28 09:22:03 -05:00
unsigned int index = ( * itr ) ;
2012-03-03 15:55:16 -05:00
int Y = index % c_ChunkHeight ;
int Z = ( index / c_ChunkHeight ) % c_ChunkWidth ;
int X = ( index / ( c_ChunkHeight * c_ChunkWidth ) ) ;
2012-02-28 09:22:03 -05:00
char BlockID = GetBlock ( index ) ;
switch ( BlockID )
2011-10-03 14:41:19 -04:00
{
2012-02-28 09:22:03 -05:00
case E_BLOCK_REDSTONE_REPEATER_OFF :
case E_BLOCK_REDSTONE_REPEATER_ON :
case E_BLOCK_REDSTONE_WIRE :
{
isRedstone = true ;
}
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
2011-10-03 14:41:19 -04:00
{
2012-02-28 09:22:03 -05:00
if ( GetBlock ( X , Y - 1 , Z ) = = E_BLOCK_AIR )
{
SetBlock ( X , Y , Z , E_BLOCK_AIR , 0 ) ;
2011-12-22 16:36:24 -05:00
2012-02-28 09:22:03 -05:00
int wX , wY , wZ ;
PositionToWorldPosition ( X , Y , Z , wX , wY , wZ ) ;
2011-12-22 16:36:24 -05:00
2012-02-28 09:22:03 -05:00
m_World - > GetSimulatorManager ( ) - > WakeUp ( wX , wY , wZ ) ;
if ( isRedstone ) {
cRedstone Redstone ( m_World ) ;
2012-03-03 15:55:16 -05:00
Redstone . ChangeRedstone ( ( X + m_PosX * c_ChunkWidth ) , ( Y + m_PosY * c_ChunkWidth ) , ( Z + m_PosZ * c_ChunkWidth ) , false ) ;
2012-02-28 09:22:03 -05:00
}
2012-03-03 15:55:16 -05:00
cPickup * Pickup = new cPickup ( ( X + m_PosX * c_ChunkWidth ) * 32 + 16 , ( Y + m_PosY * c_ChunkHeight ) * 32 + 16 , ( Z + m_PosZ * c_ChunkWidth ) * 32 + 16 , cItem ( cBlockToPickup : : ToPickup ( ( ENUM_ITEM_ID ) BlockID , E_ITEM_EMPTY ) , 1 ) ) ;
2012-02-28 09:22:03 -05:00
Pickup - > Initialize ( m_World ) ;
2011-11-07 17:59:29 -05:00
}
2011-10-03 14:41:19 -04:00
}
2012-02-28 09:22:03 -05:00
break ;
case E_BLOCK_REDSTONE_TORCH_OFF :
case E_BLOCK_REDSTONE_TORCH_ON :
isRedstone = true ;
case E_BLOCK_TORCH :
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
char Dir = cTorch : : MetaDataToDirection ( GetNibble ( m_BlockMeta , X , Y , Z ) ) ;
2012-02-28 09:22:03 -05:00
LOG ( " MetaData: %i " , Dir ) ;
2012-03-03 15:55:16 -05:00
int XX = X + m_PosX * c_ChunkWidth ;
2012-03-04 08:54:33 -05:00
int YY = Y + m_PosY * c_ChunkHeight ;
2012-03-03 15:55:16 -05:00
int ZZ = Z + m_PosZ * c_ChunkWidth ;
2012-02-28 09:22:03 -05:00
AddDirection ( XX , YY , ZZ , Dir , true ) ;
if ( m_World - > GetBlock ( XX , YY , ZZ ) = = E_BLOCK_AIR )
{
SetBlock ( X , Y , Z , 0 , 0 ) ;
2012-03-01 10:18:59 -05:00
Vector3i wPos = PositionToWorldPosition ( Vector3i ( X , Y , Z ) ) ;
m_World - > GetSimulatorManager ( ) - > WakeUp ( wPos . x , wPos . y , wPos . z ) ;
2012-02-28 09:22:03 -05:00
if ( isRedstone ) {
cRedstone Redstone ( m_World ) ;
2012-03-03 15:55:16 -05:00
Redstone . ChangeRedstone ( ( X + m_PosX * c_ChunkWidth ) , ( Y + m_PosY * c_ChunkWidth ) , ( Z + m_PosZ * c_ChunkWidth ) , false ) ;
2012-02-28 09:22:03 -05:00
}
2012-03-03 15:55:16 -05:00
cPickup * Pickup = new cPickup ( ( X + m_PosX * c_ChunkWidth ) * 32 + 16 , ( Y + m_PosY * c_ChunkHeight ) * 32 + 16 , ( Z + m_PosZ * c_ChunkWidth ) * 32 + 16 , cItem ( cBlockToPickup : : ToPickup ( ( ENUM_ITEM_ID ) BlockID , E_ITEM_EMPTY ) , 1 ) ) ;
2012-02-28 09:22:03 -05:00
Pickup - > Initialize ( m_World ) ;
2011-11-07 17:59:29 -05:00
}
2011-10-03 14:41:19 -04:00
}
2012-02-28 09:22:03 -05:00
break ;
case E_BLOCK_LADDER :
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
char Dir = cLadder : : MetaDataToDirection ( GetNibble ( m_BlockMeta , X , Y , Z ) ) ;
2012-03-03 15:55:16 -05:00
int XX = X + m_PosX * c_ChunkWidth ;
2012-03-04 08:54:33 -05:00
int YY = Y + m_PosY * c_ChunkHeight ;
2012-03-03 15:55:16 -05:00
int ZZ = Z + m_PosZ * c_ChunkWidth ;
2012-02-28 09:22:03 -05:00
AddDirection ( XX , YY , ZZ , Dir , true ) ;
if ( m_World - > GetBlock ( XX , YY , ZZ ) = = E_BLOCK_AIR )
{
SetBlock ( X , Y , Z , E_BLOCK_AIR , 0 ) ;
2012-03-03 15:55:16 -05:00
cPickup * Pickup = new cPickup ( ( X + m_PosX * c_ChunkWidth ) * 32 + 16 , ( Y + m_PosY * c_ChunkHeight ) * 32 + 16 , ( Z + m_PosZ * c_ChunkWidth ) * 32 + 16 , cItem ( ( ENUM_ITEM_ID ) BlockID , 1 ) ) ;
2012-02-28 09:22:03 -05:00
Pickup - > Initialize ( m_World ) ;
}
2011-10-03 14:41:19 -04:00
}
2012-02-28 09:22:03 -05:00
break ;
default :
break ;
} ;
}
2011-10-03 14:41:19 -04:00
}
2011-12-28 09:52:31 -05:00
2011-10-03 14:41:19 -04:00
// Tick dem blocks
2012-02-08 07:36:54 -05:00
int RandomX = a_TickRandom . randInt ( ) ;
int RandomY = a_TickRandom . randInt ( ) ;
int RandomZ = a_TickRandom . randInt ( ) ;
2011-10-03 14:41:19 -04:00
for ( int i = 0 ; i < 50 ; i + + )
{
2012-03-03 15:55:16 -05:00
m_BlockTickX = ( m_BlockTickX + RandomX ) % c_ChunkWidth ;
m_BlockTickY = ( m_BlockTickY + RandomY ) % c_ChunkHeight ;
m_BlockTickZ = ( m_BlockTickZ + RandomZ ) % c_ChunkWidth ;
2011-10-03 14:41:19 -04:00
//LOG("%03i %03i %03i", m_BlockTickX, m_BlockTickY, m_BlockTickZ);
2012-03-03 15:55:16 -05:00
if ( m_BlockTickY > m_HeightMap [ m_BlockTickX + m_BlockTickZ * c_ChunkWidth ] ) continue ; // It's all air up here
2011-10-03 14:41:19 -04:00
//m_BlockTickNum = (m_BlockTickNum + 1 ) % c_NumBlocks;
unsigned int Index = MakeIndex ( m_BlockTickX , m_BlockTickY , m_BlockTickZ ) ;
char ID = m_BlockType [ Index ] ;
switch ( ID )
{
2012-02-13 16:47:03 -05:00
/*
// TODO: re-enable
case E_BLOCK_DIRT :
2011-10-03 14:41:19 -04:00
{
char AboveBlock = GetBlock ( Index + 1 ) ;
2012-03-05 11:41:57 -05:00
if ( ( AboveBlock = = 0 ) & & GetNibble ( m_BlockSkyLight , Index ) > 0xf / 2 ) // Half lit //changed to not allow grass if any one hit object is on top
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
FastSetBlock ( m_BlockTickX , m_BlockTickY , m_BlockTickZ , E_BLOCK_GRASS , GetNibble ( m_BlockMeta , Index ) ) ;
2011-10-03 14:41:19 -04:00
}
2012-03-05 11:41:57 -05:00
if ( ( g_BlockOneHitDig [ AboveBlock ] ) & & GetNibble ( m_BlockSkyLight , Index + 1 ) > 0xf / 2 ) // Half lit //ch$
2011-11-08 20:31:19 -05:00
{
2012-03-05 11:41:57 -05:00
FastSetBlock ( m_BlockTickX , m_BlockTickY , m_BlockTickZ , E_BLOCK_GRASS , GetNibble ( m_BlockMeta , Index ) ) ;
2011-11-08 20:31:19 -05:00
}
2011-11-05 15:31:25 -04:00
2012-02-13 16:47:03 -05:00
break ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
*/
case E_BLOCK_GRASS :
2011-10-03 14:41:19 -04:00
{
char AboveBlock = GetBlock ( Index + 1 ) ;
2011-11-06 04:23:20 -05:00
if ( ! ( ( AboveBlock = = 0 ) | | ( g_BlockOneHitDig [ AboveBlock ] ) | | ( g_BlockTransparent [ AboveBlock ] ) ) ) //changed to not allow grass if any one hit object is on top
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
FastSetBlock ( m_BlockTickX , m_BlockTickY , m_BlockTickZ , E_BLOCK_DIRT , GetNibble ( m_BlockMeta , Index ) ) ;
2011-10-03 14:41:19 -04:00
}
}
break ;
2011-11-05 15:31:25 -04:00
case E_BLOCK_SAPLING : //todo: check meta of sapling. change m_World->GrowTree to look change trunk and leaves based on meta of sapling
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
FastSetBlock ( m_BlockTickX , m_BlockTickY , m_BlockTickZ , E_BLOCK_AIR , GetNibble ( m_BlockMeta , Index ) ) ;
2012-03-03 15:55:16 -05:00
m_World - > GrowTree ( m_BlockTickX + m_PosX * c_ChunkWidth , m_BlockTickY , m_BlockTickZ + m_PosZ * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
}
2011-11-09 22:35:46 -05:00
break ;
case E_BLOCK_LEAVES : //todo, http://www.minecraftwiki.net/wiki/Data_values#Leaves
{
}
break ;
2011-10-03 14:41:19 -04:00
default :
break ;
}
}
2012-02-13 16:47:03 -05:00
// Tick block entities (furnaces)
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
if ( ( * itr ) - > GetBlockType ( ) = = E_BLOCK_FURNACE )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
( ( cFurnaceEntity * ) ( * itr ) ) - > Tick ( a_Dt ) ;
2011-10-03 14:41:19 -04:00
}
}
}
2012-02-08 07:36:54 -05:00
2012-02-18 12:53:22 -05:00
int cChunk : : GetHeight ( int a_X , int a_Z )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
ASSERT ( ( a_X > = 0 ) & & ( a_X < c_ChunkWidth ) & & ( a_Z > = 0 ) & & ( a_Z < c_ChunkWidth ) ) ;
2012-02-21 08:44:06 -05:00
2012-03-03 15:55:16 -05:00
if ( ( a_X > = 0 ) & & ( a_X < c_ChunkWidth ) & & ( a_Z > = 0 ) & & ( a_Z < c_ChunkWidth ) )
2012-02-13 16:47:03 -05:00
{
2012-03-03 15:55:16 -05:00
return m_HeightMap [ a_X + a_Z * c_ChunkWidth ] ;
2012-02-13 16:47:03 -05:00
}
2011-10-03 14:41:19 -04:00
return 0 ;
}
2012-02-08 14:49:57 -05:00
2012-02-16 08:42:35 -05:00
void cChunk : : CreateBlockEntities ( void )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
for ( int z = 0 ; z < c_ChunkWidth ; z + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
for ( int y = 0 ; y < c_ChunkHeight ; y + + )
2011-10-03 14:41:19 -04:00
{
ENUM_BLOCK_ID BlockType = ( ENUM_BLOCK_ID ) m_BlockData [ MakeIndex ( x , y , z ) ] ;
2012-02-13 16:47:03 -05:00
switch ( BlockType )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
case E_BLOCK_CHEST :
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
if ( ! HasBlockEntityAt ( x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth ) )
2012-02-16 08:42:35 -05:00
{
2012-03-03 15:55:16 -05:00
m_BlockEntities . push_back ( new cChestEntity ( x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2012-02-16 08:42:35 -05:00
}
2012-02-13 16:47:03 -05:00
break ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
case E_BLOCK_FURNACE :
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
if ( ! HasBlockEntityAt ( x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth ) )
2012-02-16 08:42:35 -05:00
{
2012-03-03 15:55:16 -05:00
m_BlockEntities . push_back ( new cFurnaceEntity ( x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2012-02-16 08:42:35 -05:00
}
2012-02-13 16:47:03 -05:00
break ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
case E_BLOCK_SIGN_POST :
case E_BLOCK_WALLSIGN :
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
if ( ! HasBlockEntityAt ( x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth ) )
2012-02-16 08:42:35 -05:00
{
2012-03-03 15:55:16 -05:00
m_BlockEntities . push_back ( new cSignEntity ( BlockType , x + m_PosX * c_ChunkWidth , y + m_PosY * c_ChunkHeight , z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2012-02-16 08:42:35 -05:00
}
2012-02-13 16:47:03 -05:00
break ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
} // switch (BlockType)
} // for y
} // for z
} // for x
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : CalculateHeightmap ( )
{
2012-03-03 15:55:16 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
for ( int z = 0 ; z < c_ChunkWidth ; z + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
for ( int y = c_ChunkHeight - 1 ; y > - 1 ; y - - )
2011-10-03 14:41:19 -04:00
{
int index = MakeIndex ( x , y , z ) ;
2012-02-13 16:47:03 -05:00
if ( m_BlockData [ index ] ! = E_BLOCK_AIR )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
m_HeightMap [ x + z * c_ChunkWidth ] = ( char ) y ;
2011-10-03 14:41:19 -04:00
break ;
}
2012-02-13 16:47:03 -05:00
} // for y
} // for z
} // for x
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : CalculateLighting ( )
{
// Calculate sunlight
2012-02-13 16:47:03 -05:00
memset ( m_BlockSkyLight , 0xff , c_NumBlocks / 2 ) ; // Set all to fully lit, so everything above HeightMap is lit
2012-03-05 11:41:57 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + )
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
for ( int z = 0 ; z < c_ChunkWidth ; z + + )
2011-10-03 14:41:19 -04:00
{
char sunlight = 0xf ;
2012-03-05 11:41:57 -05:00
for ( int y = m_HeightMap [ x + z * c_ChunkWidth ] ; y > - 1 ; y - - )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( z * c_ChunkHeight ) + ( x * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockTransparent [ ( int ) m_BlockData [ index ] ] = = false )
{
sunlight = 0x0 ;
}
2012-03-05 11:41:57 -05:00
SetNibble ( m_BlockSkyLight , x , y , z , sunlight ) ;
2011-10-03 14:41:19 -04:00
}
}
}
// Calculate blocklights
2012-03-05 11:41:57 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + )
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
for ( int z = 0 ; z < c_ChunkWidth ; z + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int MaxHeight = m_HeightMap [ x + z * c_ChunkWidth ] ;
2012-03-05 11:41:57 -05:00
for ( int y = 0 ; y < MaxHeight ; y + + )
2011-10-03 14:41:19 -04:00
{
char BlockID = GetBlock ( x , y , z ) ;
2012-03-05 11:41:57 -05:00
SetNibble ( m_BlockLight , x , y , z , g_BlockLightValue [ ( int ) BlockID ] ) ;
2011-10-03 14:41:19 -04:00
}
}
}
SpreadLight ( m_BlockSkyLight ) ;
SpreadLight ( m_BlockLight ) ;
2012-03-05 11:41:57 -05:00
MarkDirty ( ) ;
2011-10-03 14:41:19 -04:00
// Stop it from calculating again :P
m_bCalculateLighting = false ;
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : SpreadLight ( char * a_LightBuffer )
{
// Spread the sunlight
2012-03-03 15:55:16 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + ) for ( int z = 0 ; z < c_ChunkWidth ; z + + ) for ( int y = 0 ; y < c_ChunkHeight ; y + + )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( z * c_ChunkHeight ) + ( x * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
SpreadLightOfBlock ( a_LightBuffer , x , y , z , g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ;
}
}
2012-03-03 15:55:16 -05:00
for ( int x = c_ChunkWidth - 1 ; x > - 1 ; x - - ) for ( int z = c_ChunkWidth - 1 ; z > - 1 ; z - - ) for ( int y = c_ChunkHeight - 1 ; y > - 1 ; y - - )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( z * c_ChunkHeight ) + ( x * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
SpreadLightOfBlock ( a_LightBuffer , x , y , z , g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ;
}
}
bool bCalcLeft , bCalcRight , bCalcFront , bCalcBack ;
bCalcLeft = bCalcRight = bCalcFront = bCalcBack = false ;
2012-02-13 16:47:03 -05:00
2011-10-03 14:41:19 -04:00
// Spread to neighbour chunks X-axis
2012-02-21 11:27:30 -05:00
cChunkPtr LeftChunk = m_ChunkMap - > GetChunkNoGen ( m_PosX - 1 , m_PosY , m_PosZ ) ;
cChunkPtr RightChunk = m_ChunkMap - > GetChunkNoGen ( m_PosX + 1 , m_PosY , m_PosZ ) ;
2012-02-13 16:47:03 -05:00
char * LeftSky = NULL , * RightSky = NULL ;
if ( LeftChunk - > IsValid ( ) )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
LeftSky = ( a_LightBuffer = = m_BlockSkyLight ) ? LeftChunk - > pGetSkyLight ( ) : LeftChunk - > pGetLight ( ) ;
}
if ( RightChunk - > IsValid ( ) )
{
RightSky = ( a_LightBuffer = = m_BlockSkyLight ) ? RightChunk - > pGetSkyLight ( ) : RightChunk - > pGetLight ( ) ;
}
2012-03-03 15:55:16 -05:00
for ( int z = 0 ; z < c_ChunkWidth ; z + + ) for ( int y = 0 ; y < c_ChunkHeight ; y + + )
2012-02-13 16:47:03 -05:00
{
if ( LeftSky ! = NULL )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( z * c_ChunkHeight ) + ( 0 * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
2012-03-05 11:41:57 -05:00
char CurrentLight = GetNibble ( a_LightBuffer , 0 , y , z ) ;
char LeftLight = GetNibble ( LeftSky , c_ChunkWidth - 1 , y , z ) ;
2011-10-03 14:41:19 -04:00
if ( LeftLight < CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] )
{
2012-03-05 11:41:57 -05:00
SetNibble ( LeftSky , c_ChunkWidth - 1 , y , z , MAX ( 0 , CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ) ;
2011-10-03 14:41:19 -04:00
bCalcLeft = true ;
}
}
}
2012-02-13 16:47:03 -05:00
if ( RightSky ! = NULL )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( z * c_ChunkHeight ) + ( ( c_ChunkWidth - 1 ) * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
2012-03-05 11:41:57 -05:00
char CurrentLight = GetNibble ( a_LightBuffer , c_ChunkWidth - 1 , y , z ) ;
char RightLight = GetNibble ( RightSky , 0 , y , z ) ;
2011-10-03 14:41:19 -04:00
if ( RightLight < CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] )
{
2012-03-05 11:41:57 -05:00
SetNibble ( RightSky , 0 , y , z , MAX ( 0 , CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ) ;
2011-10-03 14:41:19 -04:00
bCalcRight = true ;
}
}
}
}
// Spread to neighbour chunks Z-axis
2012-02-21 11:27:30 -05:00
cChunkPtr FrontChunk = m_ChunkMap - > GetChunkNoGen ( m_PosX , m_PosY , m_PosZ - 1 ) ;
cChunkPtr BackChunk = m_ChunkMap - > GetChunkNoGen ( m_PosX , m_PosY , m_PosZ + 1 ) ;
2012-02-13 16:47:03 -05:00
char * FrontSky = NULL , * BackSky = NULL ;
if ( FrontChunk - > IsValid ( ) )
{
FrontSky = ( a_LightBuffer = = m_BlockSkyLight ) ? FrontChunk - > pGetSkyLight ( ) : FrontChunk - > pGetLight ( ) ;
}
if ( BackChunk - > IsValid ( ) )
{
BackSky = ( a_LightBuffer = = m_BlockSkyLight ) ? BackChunk - > pGetSkyLight ( ) : BackChunk - > pGetLight ( ) ;
}
2012-03-03 15:55:16 -05:00
for ( int x = 0 ; x < c_ChunkWidth ; x + + ) for ( int y = 0 ; y < c_ChunkHeight ; y + + )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
if ( FrontSky ! = NULL )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( 0 * c_ChunkHeight ) + ( x * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
2012-03-05 11:41:57 -05:00
char CurrentLight = GetNibble ( a_LightBuffer , x , y , 0 ) ;
char FrontLight = GetNibble ( FrontSky , x , y , c_ChunkWidth - 1 ) ;
2011-10-03 14:41:19 -04:00
if ( FrontLight < CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] )
{
2012-03-05 11:41:57 -05:00
SetNibble ( FrontSky , x , y , c_ChunkWidth - 1 , MAX ( 0 , CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ) ;
2011-10-03 14:41:19 -04:00
bCalcFront = true ;
}
}
}
2012-02-13 16:47:03 -05:00
if ( BackSky ! = NULL )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
int index = y + ( ( c_ChunkWidth - 1 ) * c_ChunkHeight ) + ( x * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
if ( g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] > 0 )
{
2012-03-05 11:41:57 -05:00
char CurrentLight = GetNibble ( a_LightBuffer , x , y , c_ChunkWidth - 1 ) ;
char BackLight = GetNibble ( BackSky , x , y , 0 ) ;
2012-02-13 16:47:03 -05:00
if ( BackLight < CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] )
2011-10-03 14:41:19 -04:00
{
2012-03-05 11:41:57 -05:00
SetNibble ( BackSky , x , y , 0 , MAX ( 0 , CurrentLight - g_BlockSpreadLightFalloff [ m_BlockData [ index ] ] ) ) ;
2011-10-03 14:41:19 -04:00
bCalcBack = true ;
}
}
}
}
2012-03-05 11:41:57 -05:00
if ( bCalcLeft ) m_World - > ReSpreadLighting ( m_PosX - 1 , m_PosY , m_PosZ ) ;
if ( bCalcRight ) m_World - > ReSpreadLighting ( m_PosX + 1 , m_PosY , m_PosZ ) ;
if ( bCalcFront ) m_World - > ReSpreadLighting ( m_PosX , m_PosY , m_PosZ - 1 ) ;
if ( bCalcBack ) m_World - > ReSpreadLighting ( m_PosX , m_PosY , m_PosZ + 1 ) ;
// No need to set those neighbors dirty, they will recalc their light anyway so they'll get marked dirty there
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : SetBlock ( int a_X , int a_Y , int a_Z , char a_BlockType , char a_BlockMeta )
{
2012-03-03 15:55:16 -05:00
if ( a_X < 0 | | a_X > = c_ChunkWidth | | a_Y < 0 | | a_Y > = c_ChunkHeight | | a_Z < 0 | | a_Z > = c_ChunkWidth )
2011-10-03 14:41:19 -04:00
{
2012-02-08 14:49:57 -05:00
return ; // Clip
2011-10-03 14:41:19 -04:00
}
2012-02-19 18:00:00 -05:00
ASSERT ( IsValid ( ) ) ; // Is this chunk loaded / generated?
2012-02-13 16:47:03 -05:00
2012-03-03 15:55:16 -05:00
int index = a_Y + ( a_Z * c_ChunkHeight ) + ( a_X * c_ChunkHeight * c_ChunkWidth ) ;
2012-03-05 11:41:57 -05:00
char OldBlockMeta = GetNibble ( m_BlockMeta , index ) ;
2011-10-03 14:41:19 -04:00
char OldBlockType = m_BlockType [ index ] ;
m_BlockType [ index ] = a_BlockType ;
2012-03-05 11:41:57 -05:00
SetNibble ( m_BlockMeta , index , a_BlockMeta ) ;
2011-10-03 14:41:19 -04:00
2012-02-08 14:49:57 -05:00
if ( ( OldBlockType = = a_BlockType ) & & ( OldBlockMeta = = a_BlockMeta ) )
{
return ;
}
2012-02-16 12:45:26 -05:00
MarkDirty ( ) ;
2012-02-21 08:44:06 -05:00
{
cCSLock Lock ( m_CSBlockLists ) ;
m_PendingSendBlocks . push_back ( index ) ;
}
// ONLY recalculate lighting if it's necessary!
if (
( g_BlockLightValue [ OldBlockType ] ! = g_BlockLightValue [ a_BlockType ] ) | |
( g_BlockSpreadLightFalloff [ OldBlockType ] ! = g_BlockSpreadLightFalloff [ a_BlockType ] ) | |
( g_BlockTransparent [ OldBlockType ] ! = g_BlockTransparent [ a_BlockType ] )
)
{
RecalculateLighting ( ) ;
}
// Update heightmap, if needed:
2012-03-03 15:55:16 -05:00
if ( a_Y > = m_HeightMap [ a_X + a_Z * c_ChunkWidth ] )
2012-02-21 08:44:06 -05:00
{
2012-02-21 10:18:02 -05:00
if ( a_BlockType ! = E_BLOCK_AIR )
{
2012-03-04 08:54:33 -05:00
m_HeightMap [ a_X + a_Z * c_ChunkWidth ] = ( unsigned char ) a_Y ;
2012-02-21 10:18:02 -05:00
}
else
{
for ( int y = a_Y - 1 ; y > 0 ; - - y )
{
if ( m_BlockData [ MakeIndex ( a_X , y , a_Z ) ] ! = E_BLOCK_AIR )
{
2012-03-04 08:54:33 -05:00
m_HeightMap [ a_X + a_Z * c_ChunkWidth ] = ( unsigned char ) y ;
2012-02-21 10:18:02 -05:00
break ;
}
} // for y - column in m_BlockData
}
2012-02-21 08:44:06 -05:00
}
2012-02-08 14:49:57 -05:00
2012-02-28 09:22:03 -05:00
m_ToTickBlocks . push_back ( MakeIndex ( a_X , a_Y , a_Z ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X + 1 , a_Y , a_Z ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X - 1 , a_Y , a_Z ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X , a_Y + 1 , a_Z ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X , a_Y - 1 , a_Z ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X , a_Y , a_Z + 1 ) ) ;
m_ToTickBlocks . push_back ( MakeIndex ( a_X , a_Y , a_Z - 1 ) ) ;
2012-02-08 14:49:57 -05:00
2012-03-03 15:55:16 -05:00
cBlockEntity * BlockEntity = GetBlockEntity ( a_X + m_PosX * c_ChunkWidth , a_Y + m_PosY * c_ChunkHeight , a_Z + m_PosZ * c_ChunkWidth ) ;
2012-02-08 14:49:57 -05:00
if ( BlockEntity )
{
BlockEntity - > Destroy ( ) ;
RemoveBlockEntity ( BlockEntity ) ;
delete BlockEntity ;
}
switch ( a_BlockType )
2011-10-03 14:41:19 -04:00
{
case E_BLOCK_CHEST :
2012-02-08 14:49:57 -05:00
{
2012-03-03 15:55:16 -05:00
AddBlockEntity ( new cChestEntity ( a_X + m_PosX * c_ChunkWidth , a_Y + m_PosY * c_ChunkHeight , a_Z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-02-08 14:49:57 -05:00
}
2011-10-03 14:41:19 -04:00
case E_BLOCK_FURNACE :
2012-02-08 14:49:57 -05:00
{
2012-03-03 15:55:16 -05:00
AddBlockEntity ( new cFurnaceEntity ( a_X + m_PosX * c_ChunkWidth , a_Y + m_PosY * c_ChunkHeight , a_Z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-02-08 14:49:57 -05:00
}
2011-10-03 14:41:19 -04:00
case E_BLOCK_SIGN_POST :
case E_BLOCK_WALLSIGN :
2012-02-08 14:49:57 -05:00
{
2012-03-03 15:55:16 -05:00
AddBlockEntity ( new cSignEntity ( ( ENUM_BLOCK_ID ) a_BlockType , a_X + m_PosX * c_ChunkWidth , a_Y + m_PosY * c_ChunkHeight , a_Z + m_PosZ * c_ChunkWidth , m_World ) ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-02-08 14:49:57 -05:00
}
} // switch (a_BlockType)
}
2011-12-28 09:52:31 -05:00
2011-10-03 14:41:19 -04:00
2012-02-18 15:10:57 -05:00
void cChunk : : FastSetBlock ( int a_X , int a_Y , int a_Z , char a_BlockType , char a_BlockMeta )
2011-10-03 14:41:19 -04:00
{
2012-03-03 15:55:16 -05:00
ASSERT ( ! ( ( a_X < 0 | | a_X > = c_ChunkWidth | | a_Y < 0 | | a_Y > = c_ChunkHeight | | a_Z < 0 | | a_Z > = c_ChunkWidth ) ) ) ;
2011-10-03 14:41:19 -04:00
2012-02-19 18:00:00 -05:00
ASSERT ( IsValid ( ) ) ;
2012-02-13 16:47:03 -05:00
2012-03-03 15:55:16 -05:00
const int index = a_Y + ( a_Z * c_ChunkHeight ) + ( a_X * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
const char OldBlock = m_BlockType [ index ] ;
2012-03-05 11:41:57 -05:00
const char OldBlockMeta = GetNibble ( m_BlockMeta , index ) ;
2012-03-01 10:18:59 -05:00
if ( OldBlock = = a_BlockType & & OldBlockMeta = = a_BlockMeta )
2012-02-13 16:47:03 -05:00
{
return ;
}
2012-02-16 12:45:26 -05:00
MarkDirty ( ) ;
2011-10-03 14:41:19 -04:00
m_BlockType [ index ] = a_BlockType ;
2011-12-28 09:52:31 -05:00
2012-02-13 16:47:03 -05:00
{
cCSLock Lock ( m_CSBlockLists ) ;
m_PendingSendBlocks . push_back ( index ) ;
}
2012-03-05 11:41:57 -05:00
SetNibble ( m_BlockMeta , index , a_BlockMeta ) ;
2011-10-03 14:41:19 -04:00
2011-12-24 18:34:30 -05:00
// ONLY recalculate lighting if it's necessary!
2012-02-18 15:10:57 -05:00
if (
( g_BlockLightValue [ OldBlock ] ! = g_BlockLightValue [ a_BlockType ] ) | |
( g_BlockSpreadLightFalloff [ OldBlock ] ! = g_BlockSpreadLightFalloff [ a_BlockType ] ) | |
( g_BlockTransparent [ OldBlock ] ! = g_BlockTransparent [ a_BlockType ] )
)
2011-10-03 14:41:19 -04:00
{
RecalculateLighting ( ) ;
}
2012-02-21 08:44:06 -05:00
// Update heightmap, if needed:
2012-03-03 15:55:16 -05:00
if ( a_Y > = m_HeightMap [ a_X + a_Z * c_ChunkWidth ] )
2012-02-21 08:44:06 -05:00
{
2012-02-21 10:18:02 -05:00
if ( a_BlockType ! = E_BLOCK_AIR )
{
2012-03-04 08:54:33 -05:00
m_HeightMap [ a_X + a_Z * c_ChunkWidth ] = ( unsigned char ) a_Y ;
2012-02-21 10:18:02 -05:00
}
else
{
for ( int y = a_Y - 1 ; y > 0 ; - - y )
{
if ( m_BlockData [ MakeIndex ( a_X , y , a_Z ) ] ! = E_BLOCK_AIR )
{
2012-03-04 08:54:33 -05:00
m_HeightMap [ a_X + a_Z * c_ChunkWidth ] = ( unsigned char ) y ;
2012-02-21 10:18:02 -05:00
break ;
}
} // for y - column in m_BlockData
}
2012-02-21 08:44:06 -05:00
}
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : SendBlockTo ( int a_X , int a_Y , int a_Z , cClientHandle * a_Client )
{
if ( a_Client = = 0 )
{
2012-02-13 16:47:03 -05:00
cCSLock Lock ( m_CSBlockLists ) ;
2012-03-04 08:54:33 -05:00
unsigned int index = MakeIndex ( a_X , a_Y , a_Z ) ;
if ( index ! = INDEX_OUT_OF_RANGE )
{
m_PendingSendBlocks . push_back ( MakeIndex ( a_X , a_Y , a_Z ) ) ;
}
2011-10-03 14:41:19 -04:00
return ;
}
2012-02-13 16:47:03 -05:00
for ( cClientHandleList : : iterator itr = m_LoadedByClient . begin ( ) ; itr ! = m_LoadedByClient . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
if ( * itr = = a_Client )
2011-10-03 14:41:19 -04:00
{
unsigned int index = MakeIndex ( a_X , a_Y , a_Z ) ;
cPacket_BlockChange BlockChange ;
2012-03-03 15:55:16 -05:00
BlockChange . m_PosX = a_X + m_PosX * c_ChunkWidth ;
2012-03-04 09:09:35 -05:00
BlockChange . m_PosY = ( unsigned char ) ( a_Y + m_PosY * c_ChunkHeight ) ;
2012-03-03 15:55:16 -05:00
BlockChange . m_PosZ = a_Z + m_PosZ * c_ChunkWidth ;
2012-03-04 08:54:33 -05:00
if ( index ! = INDEX_OUT_OF_RANGE )
{
BlockChange . m_BlockType = m_BlockType [ index ] ;
2012-03-05 11:41:57 -05:00
BlockChange . m_BlockMeta = GetNibble ( m_BlockMeta , index ) ;
2012-03-04 08:54:33 -05:00
} // else it's both 0
2011-10-03 14:41:19 -04:00
a_Client - > Send ( BlockChange ) ;
break ;
}
}
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : AddBlockEntity ( cBlockEntity * a_BlockEntity )
{
2012-02-13 16:47:03 -05:00
cCSLock Lock ( m_CSBlockLists ) ;
m_BlockEntities . push_back ( a_BlockEntity ) ;
}
cBlockEntity * cChunk : : GetBlockEntity ( int a_X , int a_Y , int a_Z )
{
// Assumes that the m_CSBlockList is already locked, we're being called from SetBlock()
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
{
if (
( ( * itr ) - > GetPosX ( ) = = a_X ) & &
( ( * itr ) - > GetPosY ( ) = = a_Y ) & &
( ( * itr ) - > GetPosZ ( ) = = a_Z )
)
{
return * itr ;
}
} // for itr - m_BlockEntities[]
return NULL ;
}
2012-02-15 09:22:44 -05:00
void cChunk : : UseBlockEntity ( cPlayer * a_Player , int a_X , int a_Y , int a_Z )
{
cBlockEntity * be = GetBlockEntity ( a_X , a_Y , a_Z ) ;
if ( be ! = NULL )
{
be - > UsedBy ( a_Player ) ;
}
}
2012-02-13 16:47:03 -05:00
void cChunk : : CollectPickupsByPlayer ( cPlayer * a_Player )
{
double PosX = a_Player - > GetPosX ( ) ;
double PosY = a_Player - > GetPosY ( ) ;
double PosZ = a_Player - > GetPosZ ( ) ;
for ( cEntityList : : iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
{
if ( ( * itr ) - > GetEntityType ( ) ! = cEntity : : E_PICKUP )
{
continue ; // Only pickups
}
float DiffX = ( float ) ( ( * itr ) - > GetPosX ( ) - PosX ) ;
float DiffY = ( float ) ( ( * itr ) - > GetPosY ( ) - PosY ) ;
float DiffZ = ( float ) ( ( * itr ) - > GetPosZ ( ) - PosZ ) ;
float SqrDist = DiffX * DiffX + DiffY * DiffY + DiffZ * DiffZ ;
if ( SqrDist < 1.5f * 1.5f ) // 1.5 block
{
2012-02-16 08:42:35 -05:00
MarkDirty ( ) ;
2012-02-13 16:47:03 -05:00
( reinterpret_cast < cPickup * > ( * itr ) ) - > CollectedBy ( a_Player ) ;
}
}
}
void cChunk : : UpdateSign ( int a_PosX , int a_PosY , int a_PosZ , const AString & a_Line1 , const AString & a_Line2 , const AString & a_Line3 , const AString & a_Line4 )
{
// Also sends update packets to all clients in the chunk
for ( cBlockEntityList : : iterator itr = m_BlockEntities . begin ( ) ; itr ! = m_BlockEntities . end ( ) ; + + itr )
{
if (
( ( * itr ) - > GetPosX ( ) = = a_PosX ) & &
( ( * itr ) - > GetPosY ( ) = = a_PosY ) & &
( ( * itr ) - > GetPosZ ( ) = = a_PosZ ) & &
(
( ( * itr ) - > GetBlockType ( ) = = E_BLOCK_WALLSIGN ) | |
( ( * itr ) - > GetBlockType ( ) = = E_BLOCK_SIGN_POST )
)
)
{
2012-02-16 08:42:35 -05:00
MarkDirty ( ) ;
2012-02-13 16:47:03 -05:00
( reinterpret_cast < cSignEntity * > ( * itr ) ) - > SetLines ( a_Line1 , a_Line2 , a_Line3 , a_Line4 ) ;
( * itr ) - > SendTo ( NULL ) ;
}
} // for itr - m_BlockEntities[]
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : RemoveBlockEntity ( cBlockEntity * a_BlockEntity )
{
2012-02-13 16:47:03 -05:00
cCSLock Lock ( m_CSBlockLists ) ;
2012-02-16 08:42:35 -05:00
MarkDirty ( ) ;
2012-02-13 16:47:03 -05:00
m_BlockEntities . remove ( a_BlockEntity ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2012-02-21 10:18:02 -05:00
bool cChunk : : AddClient ( cClientHandle * a_Client )
2011-10-03 14:41:19 -04:00
{
2012-02-23 16:21:37 -05:00
for ( cClientHandleList : : iterator itr = m_LoadedByClient . begin ( ) ; itr ! = m_LoadedByClient . end ( ) ; + + itr )
2012-02-13 16:47:03 -05:00
{
2012-02-23 16:21:37 -05:00
if ( a_Client = = * itr )
2012-02-21 10:18:02 -05:00
{
2012-02-23 16:21:37 -05:00
// Already there, nothing needed
return false ;
2012-02-21 10:18:02 -05:00
}
2012-02-13 16:47:03 -05:00
}
2012-02-23 16:21:37 -05:00
m_LoadedByClient . push_back ( a_Client ) ;
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
for ( cEntityList : : iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-02-21 10:18:02 -05:00
LOGD ( " cChunk: Entity #%d (%s) at [%i, %i, %i] spawning for player \" %s \" " , ( * itr ) - > GetUniqueID ( ) , ( * itr ) - > GetClass ( ) , m_PosX , m_PosY , m_PosZ , a_Client - > GetUsername ( ) . c_str ( ) ) ;
2011-10-03 14:41:19 -04:00
( * itr ) - > SpawnOn ( a_Client ) ;
}
2012-02-21 10:18:02 -05:00
return true ;
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
void cChunk : : RemoveClient ( cClientHandle * a_Client )
{
2012-02-23 16:21:37 -05:00
m_LoadedByClient . remove ( a_Client ) ;
2011-10-03 14:41:19 -04:00
2012-02-13 16:47:03 -05:00
if ( ! a_Client - > IsDestroyed ( ) )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
for ( cEntityList : : iterator itr = m_Entities . begin ( ) ; itr ! = m_Entities . end ( ) ; + + itr )
2011-11-02 16:19:57 -04:00
{
2012-02-13 16:47:03 -05:00
LOG ( " chunk [%i, %i] destroying entity #%i for player \" %s \" " , m_PosX , m_PosZ , ( * itr ) - > GetUniqueID ( ) , a_Client - > GetUsername ( ) . c_str ( ) ) ;
2011-11-02 16:19:57 -04:00
cPacket_DestroyEntity DestroyEntity ( * itr ) ;
a_Client - > Send ( DestroyEntity ) ;
}
2011-10-03 14:41:19 -04:00
}
}
2012-02-08 14:49:57 -05:00
2012-02-13 16:47:03 -05:00
bool cChunk : : HasClient ( cClientHandle * a_Client )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
for ( cClientHandleList : : const_iterator itr = m_LoadedByClient . begin ( ) ; itr ! = m_LoadedByClient . end ( ) ; + + itr )
{
if ( ( * itr ) = = a_Client )
{
return true ;
}
}
return false ;
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2012-02-16 12:45:26 -05:00
bool cChunk : : HasAnyClients ( void )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
return ! m_LoadedByClient . empty ( ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2012-02-20 11:39:00 -05:00
void cChunk : : AddEntity ( cEntity * a_Entity )
2011-10-03 14:41:19 -04:00
{
2012-02-16 08:42:35 -05:00
if ( a_Entity - > GetEntityType ( ) ! = cEntity : : E_PLAYER )
{
MarkDirty ( ) ;
}
2012-02-13 16:47:03 -05:00
m_Entities . push_back ( a_Entity ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2012-02-13 16:47:03 -05:00
void cChunk : : RemoveEntity ( cEntity * a_Entity )
2011-10-03 14:41:19 -04:00
{
2012-02-23 16:21:37 -05:00
size_t SizeBefore = m_Entities . size ( ) ;
m_Entities . remove ( a_Entity ) ;
size_t SizeAfter = m_Entities . size ( ) ;
2012-02-20 11:39:00 -05:00
if ( SizeBefore ! = SizeAfter )
2012-02-16 08:42:35 -05:00
{
2012-02-20 11:39:00 -05:00
// Mark as dirty if it was a server-generated entity:
if ( a_Entity - > GetEntityType ( ) ! = cEntity : : E_PLAYER )
{
MarkDirty ( ) ;
}
2012-02-16 08:42:35 -05:00
}
2011-10-03 14:41:19 -04:00
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
char cChunk : : GetBlock ( int a_X , int a_Y , int a_Z )
{
2012-03-03 15:55:16 -05:00
if ( ( a_X < 0 ) | | ( a_X > = c_ChunkWidth ) | | ( a_Y < 0 ) | | ( a_Y > = c_ChunkHeight ) | | ( a_Z < 0 ) | | ( a_Z > = c_ChunkWidth ) ) return 0 ; // Clip
2011-10-03 14:41:19 -04:00
2012-03-03 15:55:16 -05:00
int index = a_Y + ( a_Z * c_ChunkHeight ) + ( a_X * c_ChunkHeight * c_ChunkWidth ) ;
2011-10-03 14:41:19 -04:00
return m_BlockType [ index ] ;
}
2012-02-08 14:49:57 -05:00
2011-10-03 14:41:19 -04:00
char cChunk : : GetBlock ( int a_BlockIdx )
{
if ( a_BlockIdx < 0 | | a_BlockIdx > = c_NumBlocks ) return 0 ;
return m_BlockType [ a_BlockIdx ] ;
}
2012-02-08 14:49:57 -05:00
2012-02-23 16:21:37 -05:00
/*
// _X 2012_02_23: Loading in old format not supported anymore
2012-01-30 11:01:45 -05:00
/// Loads the chunk from the old-format disk file, erases the file afterwards. Returns true if successful
2011-10-03 14:41:19 -04:00
bool cChunk : : LoadFromDisk ( )
{
2012-02-01 08:43:47 -05:00
AString SourceFile ;
Printf ( SourceFile , " world/X%i_Y%i_Z%i.bin " , m_PosX , m_PosY , m_PosZ ) ;
2012-01-30 11:01:45 -05:00
cFile f ;
if ( ! f . Open ( SourceFile , cFile : : fmRead ) )
2011-10-03 14:41:19 -04:00
{
2012-01-30 11:01:45 -05:00
return false ;
}
2011-10-03 14:41:19 -04:00
2012-01-30 11:01:45 -05:00
if ( f . Read ( m_BlockData , sizeof ( m_BlockData ) ) ! = sizeof ( m_BlockData ) )
{
2012-02-01 08:50:09 -05:00
LOGERROR ( " ERROR READING FROM FILE %s " , SourceFile . c_str ( ) ) ;
2012-01-30 11:01:45 -05:00
return false ;
}
2012-02-23 16:21:37 -05:00
// Now load Block Entities:
2012-01-30 11:01:45 -05:00
ENUM_BLOCK_ID BlockType ;
while ( f . Read ( & BlockType , sizeof ( ENUM_BLOCK_ID ) ) = = sizeof ( ENUM_BLOCK_ID ) )
{
switch ( BlockType )
2011-10-03 14:41:19 -04:00
{
case E_BLOCK_CHEST :
2012-01-30 11:01:45 -05:00
{
2012-02-13 16:47:03 -05:00
cChestEntity * ChestEntity = new cChestEntity ( 0 , 0 , 0 , m_World ) ;
2012-01-30 11:01:45 -05:00
if ( ! ChestEntity - > LoadFromFile ( f ) )
2011-10-03 14:41:19 -04:00
{
2012-02-01 08:50:09 -05:00
LOGERROR ( " ERROR READING CHEST FROM FILE %s " , SourceFile . c_str ( ) ) ;
2012-01-30 11:01:45 -05:00
delete ChestEntity ;
return false ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
m_BlockEntities . push_back ( ChestEntity ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-01-30 11:01:45 -05:00
}
2011-10-03 14:41:19 -04:00
case E_BLOCK_FURNACE :
2012-01-30 11:01:45 -05:00
{
2012-02-13 16:47:03 -05:00
cFurnaceEntity * FurnaceEntity = new cFurnaceEntity ( 0 , 0 , 0 , m_World ) ;
2012-01-30 11:01:45 -05:00
if ( ! FurnaceEntity - > LoadFromFile ( f ) )
2011-10-03 14:41:19 -04:00
{
2012-02-01 08:50:09 -05:00
LOGERROR ( " ERROR READING FURNACE FROM FILE %s " , SourceFile . c_str ( ) ) ;
2012-01-30 11:01:45 -05:00
delete FurnaceEntity ;
return false ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
m_BlockEntities . push_back ( FurnaceEntity ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-01-30 11:01:45 -05:00
}
2011-10-03 14:41:19 -04:00
case E_BLOCK_SIGN_POST :
case E_BLOCK_WALLSIGN :
2012-01-30 11:01:45 -05:00
{
2012-02-13 16:47:03 -05:00
cSignEntity * SignEntity = new cSignEntity ( BlockType , 0 , 0 , 0 , m_World ) ;
2012-01-30 11:01:45 -05:00
if ( ! SignEntity - > LoadFromFile ( f ) )
2011-10-03 14:41:19 -04:00
{
2012-02-01 08:50:09 -05:00
LOGERROR ( " ERROR READING SIGN FROM FILE %s " , SourceFile . c_str ( ) ) ;
2012-01-30 11:01:45 -05:00
delete SignEntity ;
return false ;
2011-10-03 14:41:19 -04:00
}
2012-02-13 16:47:03 -05:00
m_BlockEntities . push_back ( SignEntity ) ;
2011-10-03 14:41:19 -04:00
break ;
2012-01-30 11:01:45 -05:00
}
2011-10-03 14:41:19 -04:00
default :
2012-01-30 11:01:45 -05:00
{
2012-02-19 18:00:00 -05:00
ASSERT ( ! " Unhandled block entity in file " ) ;
2011-10-03 14:41:19 -04:00
break ;
}
}
2012-01-30 11:01:45 -05:00
}
f . Close ( ) ;
2011-10-03 14:41:19 -04:00
2012-01-30 11:01:45 -05:00
// Delete old format file
2012-02-01 08:43:47 -05:00
if ( std : : remove ( SourceFile . c_str ( ) ) ! = 0 )
2012-01-30 11:01:45 -05:00
{
2012-02-01 08:50:09 -05:00
LOGERROR ( " Could not delete file %s " , SourceFile . c_str ( ) ) ;
2011-10-03 14:41:19 -04:00
}
else
{
2012-02-01 08:50:09 -05:00
LOGINFO ( " Successfully deleted old format file \" %s \" " , SourceFile . c_str ( ) ) ;
2011-10-03 14:41:19 -04:00
}
2012-02-16 08:42:35 -05:00
m_IsDirty = false ;
2012-01-30 11:01:45 -05:00
return true ;
2011-10-03 14:41:19 -04:00
}
2012-02-23 16:21:37 -05:00
*/
2011-10-03 14:41:19 -04:00
2012-01-30 11:01:45 -05:00
2012-02-13 16:47:03 -05:00
void cChunk : : Broadcast ( const cPacket * a_Packet , cClientHandle * a_Exclude )
2011-10-03 14:41:19 -04:00
{
2012-02-13 16:47:03 -05:00
for ( cClientHandleList : : const_iterator itr = m_LoadedByClient . begin ( ) ; itr ! = m_LoadedByClient . end ( ) ; + + itr )
2011-10-03 14:41:19 -04:00
{
2012-01-30 11:01:45 -05:00
if ( * itr = = a_Exclude )
{
continue ;
}
2011-10-03 14:41:19 -04:00
( * itr ) - > Send ( a_Packet ) ;
2012-01-30 11:01:45 -05:00
} // for itr - LoadedByClient[]
2011-10-03 14:41:19 -04:00
}
2012-01-30 11:01:45 -05:00
2012-02-13 16:47:03 -05:00
void cChunk : : CopyBlockDataFrom ( const char * a_NewBlockData )
{
// Copies all blockdata, recalculates heightmap (used by chunk loaders)
memcpy ( m_BlockData , a_NewBlockData , sizeof ( m_BlockData ) ) ;
CalculateHeightmap ( ) ;
}
2011-12-22 16:36:24 -05:00
void cChunk : : PositionToWorldPosition ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , int & a_X , int & a_Y , int & a_Z )
{
a_Y = a_ChunkY ;
2012-03-03 15:55:16 -05:00
a_X = m_PosX * c_ChunkWidth + a_ChunkX ;
a_Z = m_PosZ * c_ChunkWidth + a_ChunkZ ;
2011-12-31 16:08:23 -05:00
}
2012-01-30 11:01:45 -05:00
2012-03-01 10:18:59 -05:00
Vector3i cChunk : : PositionToWorldPosition ( const Vector3i & a_InChunkPos )
{
2012-03-03 15:55:16 -05:00
return Vector3i ( m_PosX * c_ChunkWidth + a_InChunkPos . x , m_PosY * c_ChunkHeight + a_InChunkPos . y , m_PosZ * c_ChunkWidth + a_InChunkPos . z ) ;
2012-03-01 10:18:59 -05:00
}
2011-12-31 16:08:23 -05:00
# if !C_CHUNK_USE_INLINE
# include "cChunk.inc"
2012-01-30 11:01:45 -05:00
# endif