diff options
Diffstat (limited to '')
-rw-r--r-- | source/cChunk.cpp | 704 |
1 files changed, 343 insertions, 361 deletions
diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 3b0dd519c..fec06e947 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -27,6 +27,7 @@ #include "cWorldGenerator.h"
#include "cBlockToPickup.h"
#include "MersenneTwister.h"
+#include "cPlayer.h"
#include "packets/cPacket_DestroyEntity.h"
#include "packets/cPacket_PreChunk.h"
@@ -43,66 +44,27 @@ extern bool g_bWaterPhysics;
-typedef std::map< int, std::string > ReferenceMap;
-typedef std::list< cFurnaceEntity* > FurnaceEntityList;
-typedef std::list< cClientHandle* > ClientHandleList;
-typedef std::list< cBlockEntity* > BlockEntityList;
-typedef std::list< cEntity* > EntityList;
-
-
-struct cChunk::sChunkState
-{
- sChunkState()
- : TotalReferencesEver( 0 )
- , MinusReferences( 0 )
- , NumRefs( 0 )
- {}
-
- FurnaceEntityList TickBlockEntities;
- std::map< unsigned int, int > ToTickBlocks; // Protected by BlockListCriticalSection
- std::vector< unsigned int > PendingSendBlocks; // Protected by BlockListCriticalSection
- ClientHandleList LoadedByClient;
- ClientHandleList UnloadQuery;
- BlockEntityList BlockEntities; // Protected by BlockListCriticalSection
- EntityList Entities;
-
- cCriticalSection BlockListCriticalSection;
-
- // Reference counting
- cCriticalSection ReferenceCriticalSection;
- ReferenceMap References;
- int MinusReferences; // References.size() - MinusReferences = Actual amount of references. This is due to removal of reference without an ID (don't know which to remove, so remove none)
- int TotalReferencesEver; // For creating a unique reference ID
- int NumRefs;
-};
-
-
-
-
-
-cChunk::cChunk(int a_X, int a_Y, int a_Z, cWorld* a_World)
- : m_pState( new sChunkState )
- , m_bCalculateLighting( false )
+cChunk::cChunk(int a_X, int a_Y, int a_Z, cWorld * a_World)
+ : m_bCalculateLighting( false )
, m_bCalculateHeightmap( false )
, 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 )
- , m_BlockLight( m_BlockMeta + c_NumBlocks/2 )
- , m_BlockSkyLight( m_BlockLight + c_NumBlocks/2 )
+ , m_BlockLight( m_BlockMeta + c_NumBlocks / 2 )
+ , m_BlockSkyLight( m_BlockLight + c_NumBlocks / 2 )
, m_BlockTickNum( 0 )
, m_BlockTickX( 0 )
, m_BlockTickY( 0 )
, m_BlockTickZ( 0 )
- , m_EntitiesCriticalSection( 0 )
, m_World( a_World )
+ , m_IsValid(false)
{
- //LOG("cChunk::cChunk(%i, %i, %i)", a_X, a_Y, a_Z);
- m_EntitiesCriticalSection = new cCriticalSection();
+ // LOGINFO("### new cChunk (%i, %i) at %p, thread 0x%x ###", a_X, a_Z, this, GetCurrentThreadId());
}
@@ -111,79 +73,77 @@ cChunk::cChunk(int a_X, int a_Y, int a_Z, cWorld* a_World) cChunk::~cChunk()
{
- //LOG("~cChunk() %i %i %i", m_PosX, m_PosY, m_PosZ );
- if( !m_pState->LoadedByClient.empty() )
- {
- LOGWARN("WARNING: Deleting cChunk while it contains %i clients!", m_pState->LoadedByClient.size() );
- }
-
- m_pState->ReferenceCriticalSection.Lock();
- if( GetReferenceCount() > 0 )
- {
- LOGWARN("WARNING: Deleting cChunk while it still has %i references!", GetReferenceCount() );
- }
- m_pState->ReferenceCriticalSection.Unlock();
-
- m_pState->BlockListCriticalSection.Lock();
- for( std::list<cBlockEntity*>::iterator itr = m_pState->BlockEntities.begin(); itr != m_pState->BlockEntities.end(); ++itr)
+ // LOGINFO("### delete cChunk() (%i, %i) from %p, thread 0x%x ###", m_PosX, m_PosZ, this, GetCurrentThreadId() );
+
+ cCSLock Lock(m_CSEntities);
+ for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr)
{
delete *itr;
}
- m_pState->BlockEntities.clear();
- m_pState->BlockListCriticalSection.Unlock();
+ m_BlockEntities.clear();
- LockEntities();
- if( m_pState->Entities.size() > 0 )
+ // Remove and destroy all entities that are not players:
+ cEntityList Entities;
+ for (cEntityList::const_iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr)
{
- EntityList Entities = m_pState->Entities; // Copy list to a temporary list
- for( EntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr)
+ if ((*itr)->GetEntityType() != cEntity::E_PLAYER)
{
- if( (*itr)->GetEntityType() != cEntity::E_PLAYER )
- {
- (*itr)->RemoveFromChunk( this );
- (*itr)->Destroy();
- }
+ Entities.push_back(*itr);
}
- m_pState->Entities.clear();
}
- UnlockEntities();
-
- if( m_EntitiesCriticalSection )
+ for (cEntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr)
{
- delete m_EntitiesCriticalSection;
- m_EntitiesCriticalSection = 0;
+ (*itr)->RemoveFromChunk();
+ (*itr)->Destroy();
}
- delete m_pState;
+ m_Entities.clear();
}
-void cChunk::Initialize()
+void cChunk::SetValid(bool a_SendToClients)
{
- if (!LoadFromDisk())
+ m_IsValid = true;
+
+ if (!a_SendToClients)
+ {
+ return;
+ }
+
+ cCSLock Lock(m_CSClients);
+ 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)
{
- // Clear memory
- memset( m_BlockData, 0x00, c_BlockDataSize );
+ (*itr)->ChunkJustSent(this);
+ } // for itr - m_LoadedByClient[]
+ */
+}
- m_World->GetWorldGenerator()->GenerateChunk( this );
- CalculateHeightmap();
- CalculateLighting();
- CreateBlockEntities();
- // During generation, some blocks might have been set by using (Fast)SetBlock() causing this list to fill.
- // This chunk has not been sent to anybody yet, so there is no need for separately sending block changes when you can send an entire chunk
- cCSLock Lock(m_pState->BlockListCriticalSection);
- m_pState->PendingSendBlocks.clear();
- }
- else
- {
- //LOGINFO("Successfully loaded from disk");
- CalculateHeightmap();
- }
+
+bool cChunk::CanUnload(void)
+{
+ cCSLock Lock(m_CSClients);
+ return m_LoadedByClient.empty();
}
@@ -201,8 +161,8 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) CalculateHeightmap();
}
- cCSLock Lock(m_pState->BlockListCriticalSection);
- unsigned int PendingSendBlocks = m_pState->PendingSendBlocks.size();
+ cCSLock Lock(m_CSBlockLists);
+ unsigned int PendingSendBlocks = m_PendingSendBlocks.size();
if( PendingSendBlocks > 1 )
{
cPacket_MultiBlock MultiBlock;
@@ -215,7 +175,7 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) //LOG("Sending multiblock packet for %i blocks", PendingSendBlocks );
for( unsigned int i = 0; i < PendingSendBlocks; i++)
{
- unsigned int index = m_pState->PendingSendBlocks[i];
+ unsigned int index = m_PendingSendBlocks[i];
unsigned int Y = index % 128;
unsigned int Z = (index / 128) % 16;
unsigned int X = (index / (128*16));
@@ -225,15 +185,15 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) MultiBlock.m_BlockTypes[i] = m_BlockType[index];
MultiBlock.m_BlockMetas[i] = GetLight( m_BlockMeta, index );
}
- m_pState->PendingSendBlocks.clear();
- PendingSendBlocks = m_pState->PendingSendBlocks.size();
+ m_PendingSendBlocks.clear();
+ PendingSendBlocks = m_PendingSendBlocks.size();
Broadcast( MultiBlock );
}
if( PendingSendBlocks > 0 )
{
for( unsigned int i = 0; i < PendingSendBlocks; i++)
{
- unsigned int index = m_pState->PendingSendBlocks[i];
+ unsigned int index = m_PendingSendBlocks[i];
int Y = index % 128;
int Z = (index / 128) % 16;
int X = (index / (128*16));
@@ -246,23 +206,23 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) BlockChange.m_BlockMeta = GetLight( m_BlockMeta, index );
Broadcast( BlockChange );
}
- m_pState->PendingSendBlocks.clear();
+ m_PendingSendBlocks.clear();
}
Lock.Unlock();
- while( !m_pState->UnloadQuery.empty() )
+ while ( !m_UnloadQuery.empty() )
{
cPacket_PreChunk UnloadPacket;
UnloadPacket.m_PosX = GetPosX();
UnloadPacket.m_PosZ = GetPosZ();
UnloadPacket.m_bLoad = false; // Unload
- (*m_pState->UnloadQuery.begin())->Send( UnloadPacket );
- m_pState->UnloadQuery.remove( *m_pState->UnloadQuery.begin() );
+ (*m_UnloadQuery.begin())->Send( UnloadPacket );
+ m_UnloadQuery.remove( *m_UnloadQuery.begin() );
}
- cCSLock Lock2(m_pState->BlockListCriticalSection);
- std::map< unsigned int, int > ToTickBlocks = m_pState->ToTickBlocks;
- m_pState->ToTickBlocks.clear();
+ cCSLock Lock2(m_CSBlockLists);
+ std::map< unsigned int, int > ToTickBlocks = m_ToTickBlocks;
+ m_ToTickBlocks.clear();
Lock2.Unlock();
bool isRedstone = false;
@@ -376,7 +336,9 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) char ID = m_BlockType[Index];
switch( ID )
{
- case E_BLOCK_DIRT:
+ /*
+ // TODO: re-enable
+ case E_BLOCK_DIRT:
{
char AboveBlock = GetBlock( Index+1 );
if ( (AboveBlock == 0) && GetLight( m_BlockSkyLight, Index ) > 0xf/2 ) // Half lit //changed to not allow grass if any one hit object is on top
@@ -388,9 +350,11 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_GRASS, GetLight( m_BlockMeta, Index ) );
}
+ break;
}
- break;
- case E_BLOCK_GRASS:
+ */
+
+ case E_BLOCK_GRASS:
{
char AboveBlock = GetBlock( Index+1 );
if (!( (AboveBlock == 0) || (g_BlockOneHitDig[AboveBlock]) || (g_BlockTransparent[AboveBlock]) ) ) //changed to not allow grass if any one hit object is on top
@@ -414,13 +378,12 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) }
}
- // Tick block entities (furnace)
- std::list< cFurnaceEntity* > TickBlockEntites = m_pState->TickBlockEntities; // Dangerous stuff, better make a copy.
- for( std::list< cFurnaceEntity* >::iterator itr = TickBlockEntites.begin(); itr != TickBlockEntites.end(); ++itr )
+ // Tick block entities (furnaces)
+ for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr)
{
- if( !(*itr)->Tick( a_Dt ) ) // Remove from list
+ if ((*itr)->GetBlockType() == E_BLOCK_FURNACE)
{
- m_pState->TickBlockEntities.remove( *itr );
+ ((cFurnaceEntity *)(*itr))->Tick( a_Dt );
}
}
}
@@ -432,7 +395,9 @@ void cChunk::Tick(float a_Dt, MTRand & a_TickRandom) char cChunk::GetHeight( int a_X, int a_Z )
{
if( a_X >= 0 && a_X < 16 && a_Z >= 0 && a_Z < 16 )
+ {
return m_HeightMap[a_X + a_Z*16];
+ }
return 0;
}
@@ -442,41 +407,38 @@ char cChunk::GetHeight( int a_X, int a_Z ) void cChunk::CreateBlockEntities()
{
- m_pState->BlockListCriticalSection.Lock();
- for(int x = 0; x < 16; x++)
+ cCSLock Lock(m_CSBlockLists);
+ for (int x = 0; x < 16; x++)
{
- for(int z = 0; z < 16; z++)
+ for (int z = 0; z < 16; z++)
{
- for(int y = 0; y < 128; y++)
+ for (int y = 0; y < 128; y++)
{
ENUM_BLOCK_ID BlockType = (ENUM_BLOCK_ID)m_BlockData[ MakeIndex( x, y, z ) ];
- switch( BlockType )
+ switch ( BlockType )
{
- case E_BLOCK_CHEST:
- {
- m_pState->BlockEntities.push_back( new cChestEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
- }
- break;
- case E_BLOCK_FURNACE:
+ case E_BLOCK_CHEST:
{
- m_pState->BlockEntities.push_back( new cFurnaceEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
+ m_BlockEntities.push_back( new cChestEntity( x + m_PosX * 16, y + m_PosY * 128, z + m_PosZ * 16, m_World) );
+ break;
}
- break;
- case E_BLOCK_SIGN_POST:
- case E_BLOCK_WALLSIGN:
+
+ case E_BLOCK_FURNACE:
{
- m_pState->BlockEntities.push_back( new cSignEntity( BlockType, x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
+ m_BlockEntities.push_back( new cFurnaceEntity( x + m_PosX * 16, y + m_PosY * 128, z + m_PosZ * 16, m_World) );
+ break;
}
- break;
- default:
+
+ case E_BLOCK_SIGN_POST:
+ case E_BLOCK_WALLSIGN:
{
+ m_BlockEntities.push_back( new cSignEntity( BlockType, x + m_PosX * 16, y + m_PosY * 128, z + m_PosZ * 16, m_World) );
+ break;
}
- break;
- }
- }
- }
- }
- m_pState->BlockListCriticalSection.Unlock();
+ } // switch (BlockType)
+ } // for y
+ } // for z
+ } // for x
}
@@ -486,21 +448,21 @@ void cChunk::CreateBlockEntities() void cChunk::CalculateHeightmap()
{
m_bCalculateHeightmap = false;
- for(int x = 0; x < 16; x++)
+ for (int x = 0; x < 16; x++)
{
- for(int z = 0; z < 16; z++)
+ for (int z = 0; z < 16; z++)
{
- for(int y = 127; y > -1; y--)
+ for (int y = 127; y > -1; y--)
{
int index = MakeIndex( x, y, z );
- if(m_BlockData[index] != E_BLOCK_AIR)
+ if (m_BlockData[index] != E_BLOCK_AIR)
{
- m_HeightMap[x + z*16] = (char)y;
+ m_HeightMap[x + z * 16] = (char)y;
break;
}
- }
- }
- }
+ } // for y
+ } // for z
+ } // for x
}
@@ -510,7 +472,7 @@ void cChunk::CalculateHeightmap() void cChunk::CalculateLighting()
{
// Calculate sunlight
- memset(m_BlockSkyLight, 0xff, c_NumBlocks/2 ); // Set all to fully lit, so everything above HeightMap is lit
+ memset(m_BlockSkyLight, 0xff, c_NumBlocks / 2 ); // Set all to fully lit, so everything above HeightMap is lit
for(int x = 0; x < 16; x++)
{
for(int z = 0; z < 16; z++)
@@ -577,15 +539,23 @@ void cChunk::SpreadLight(char* a_LightBuffer) bool bCalcLeft, bCalcRight, bCalcFront, bCalcBack;
bCalcLeft = bCalcRight = bCalcFront = bCalcBack = false;
+
// Spread to neighbour chunks X-axis
- ptr_cChunk LeftChunk = m_World->GetChunkUnreliable( m_PosX-1, m_PosY, m_PosZ );
- ptr_cChunk RightChunk = m_World->GetChunkUnreliable( m_PosX+1, m_PosY, m_PosZ );
- char* LeftSky = 0, *RightSky = 0;
- if(LeftChunk) LeftSky = (a_LightBuffer==m_BlockSkyLight)?LeftChunk->pGetSkyLight():LeftChunk->pGetLight();
- if(RightChunk) RightSky = (a_LightBuffer==m_BlockSkyLight)?RightChunk->pGetSkyLight():RightChunk->pGetLight();
- for(int z = 0; z < 16; z++) for(int y = 0; y < 128; y++)
+ cChunkPtr LeftChunk = m_World->GetChunkNoGen( m_PosX - 1, m_PosY, m_PosZ );
+ cChunkPtr RightChunk = m_World->GetChunkNoGen( m_PosX + 1, m_PosY, m_PosZ );
+ char * LeftSky = NULL, *RightSky = NULL;
+ if (LeftChunk->IsValid())
{
- if( LeftChunk )
+ LeftSky = (a_LightBuffer == m_BlockSkyLight) ? LeftChunk->pGetSkyLight() : LeftChunk->pGetLight();
+ }
+ if (RightChunk->IsValid())
+ {
+ RightSky = (a_LightBuffer == m_BlockSkyLight) ? RightChunk->pGetSkyLight() : RightChunk->pGetLight();
+ }
+
+ for (int z = 0; z < 16; z++) for(int y = 0; y < 128; y++)
+ {
+ if (LeftSky != NULL)
{
int index = y + (z * 128) + (0 * 128 * 16);
if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
@@ -599,7 +569,7 @@ void cChunk::SpreadLight(char* a_LightBuffer) }
}
}
- if( RightChunk )
+ if (RightSky != NULL)
{
int index = y + (z * 128) + (15 * 128 * 16);
if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
@@ -616,14 +586,20 @@ void cChunk::SpreadLight(char* a_LightBuffer) }
// Spread to neighbour chunks Z-axis
- ptr_cChunk FrontChunk = m_World->GetChunkUnreliable( m_PosX, m_PosY, m_PosZ-1 );
- ptr_cChunk BackChunk = m_World->GetChunkUnreliable( m_PosX, m_PosY, m_PosZ+1 );
- char* FrontSky = 0, *BackSky = 0;
- if(FrontChunk) FrontSky = (a_LightBuffer==m_BlockSkyLight)?FrontChunk->pGetSkyLight():FrontChunk->pGetLight();
- if(BackChunk) BackSky = (a_LightBuffer==m_BlockSkyLight)?BackChunk->pGetSkyLight():BackChunk->pGetLight();
+ cChunkPtr FrontChunk = m_World->GetChunkNoGen( m_PosX, m_PosY, m_PosZ - 1 );
+ cChunkPtr BackChunk = m_World->GetChunkNoGen( m_PosX, m_PosY, m_PosZ + 1 );
+ 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();
+ }
for(int x = 0; x < 16; x++) for(int y = 0; y < 128; y++)
{
- if( FrontChunk )
+ if (FrontSky != NULL)
{
int index = y + (0 * 128) + (x * 128 * 16);
if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
@@ -637,14 +613,14 @@ void cChunk::SpreadLight(char* a_LightBuffer) }
}
}
- if( BackChunk )
+ if (BackSky != NULL)
{
int index = y + (15 * 128) + (x * 128 * 16);
if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
{
char CurrentLight = GetLight( a_LightBuffer, x, y, 15 );
char BackLight = GetLight( BackSky, x, y, 0 );
- if( BackLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
+ if ( BackLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
{
SetLight( BackSky, x, y, 0, MAX(0, CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ]) );
bCalcBack = true;
@@ -665,8 +641,8 @@ void cChunk::SpreadLight(char* a_LightBuffer) void cChunk::AsyncUnload( cClientHandle* a_Client )
{
- m_pState->UnloadQuery.remove( a_Client ); // Make sure this client is only in the list once
- m_pState->UnloadQuery.push_back( a_Client );
+ m_UnloadQuery.remove( a_Client ); // Make sure this client is only in the list once
+ m_UnloadQuery.push_back( a_Client );
}
@@ -682,12 +658,11 @@ void cChunk::Send( cClientHandle* a_Client ) a_Client->Send( PreChunk );
a_Client->Send( cPacket_MapChunk( this ) );
- m_pState->BlockListCriticalSection.Lock();
- for( BlockEntityList::iterator itr = m_pState->BlockEntities.begin(); itr != m_pState->BlockEntities.end(); ++itr )
+ cCSLock Lock(m_CSBlockLists);
+ for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr )
{
(*itr)->SendTo( a_Client );
}
- m_pState->BlockListCriticalSection.Unlock();
}
@@ -701,6 +676,8 @@ void cChunk::SetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_Block return; // Clip
}
+ assert(IsValid()); // Is this chunk loaded / generated?
+
int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
char OldBlockMeta = GetLight( m_BlockMeta, index );
char OldBlockType = m_BlockType[index];
@@ -713,17 +690,16 @@ void cChunk::SetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_Block return;
}
- //LOG("Old: %i %i New: %i %i", OldBlockType, OldBlockMeta, a_BlockType, a_BlockMeta );
- cCSLock Lock(m_pState->BlockListCriticalSection);
- m_pState->PendingSendBlocks.push_back( index );
+ cCSLock Lock(m_CSBlockLists);
+ m_PendingSendBlocks.push_back( index );
- m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X+1, a_Y, a_Z ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X-1, a_Y, a_Z ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y+1, a_Z ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y-1, a_Z ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z+1 ) ]++;
- m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z-1 ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X+1, a_Y, a_Z ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X-1, a_Y, a_Z ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X, a_Y+1, a_Z ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X, a_Y-1, a_Z ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z+1 ) ]++;
+ m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z-1 ) ]++;
cBlockEntity* BlockEntity = GetBlockEntity( a_X + m_PosX*16, a_Y+m_PosY*128, a_Z+m_PosZ*16 );
if( BlockEntity )
@@ -736,18 +712,18 @@ void cChunk::SetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_Block {
case E_BLOCK_CHEST:
{
- AddBlockEntity( new cChestEntity( a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16, this ) );
+ AddBlockEntity( new cChestEntity( a_X + m_PosX * 16, a_Y + m_PosY * 128, a_Z + m_PosZ * 16, m_World) );
break;
}
case E_BLOCK_FURNACE:
{
- AddBlockEntity( new cFurnaceEntity( a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16, this ) );
+ AddBlockEntity( new cFurnaceEntity( a_X + m_PosX * 16, a_Y + m_PosY * 128, a_Z + m_PosZ * 16, m_World) );
break;
}
case E_BLOCK_SIGN_POST:
case E_BLOCK_WALLSIGN:
{
- AddBlockEntity( new cSignEntity( (ENUM_BLOCK_ID)a_BlockType, a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16, this ) );
+ AddBlockEntity( new cSignEntity( (ENUM_BLOCK_ID)a_BlockType, a_X + m_PosX * 16, a_Y + m_PosY * 128, a_Z + m_PosZ * 16, m_World) );
break;
}
} // switch (a_BlockType)
@@ -761,17 +737,24 @@ void cChunk::FastSetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_B {
if(a_X < 0 || a_X >= 16 || a_Y < 0 || a_Y >= 128 || a_Z < 0 || a_Z >= 16)
{
- //printf(">>>>>>>>>>>>>>>> CLIPPED SETBLOCK %i %i %i\n", a_X, a_Y, a_Z );
return; // Clip
}
+ assert(IsValid());
+
const int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
const char OldBlock = m_BlockType[index];
+ if (OldBlock == a_BlockType)
+ {
+ return;
+ }
m_BlockType[index] = a_BlockType;
- m_pState->BlockListCriticalSection.Lock();
- m_pState->PendingSendBlocks.push_back( index );
- m_pState->BlockListCriticalSection.Unlock();
+ {
+ cCSLock Lock(m_CSBlockLists);
+ m_PendingSendBlocks.push_back( index );
+ }
+
SetLight( m_BlockMeta, index, a_BlockMeta );
// ONLY recalculate lighting if it's necessary!
@@ -794,15 +777,14 @@ void cChunk::SendBlockTo( int a_X, int a_Y, int a_Z, cClientHandle* a_Client ) {
if( a_Client == 0 )
{
- m_pState->BlockListCriticalSection.Lock();
- m_pState->PendingSendBlocks.push_back( MakeIndex( a_X, a_Y, a_Z ) );
- m_pState->BlockListCriticalSection.Unlock();
+ cCSLock Lock(m_CSBlockLists);
+ m_PendingSendBlocks.push_back( MakeIndex( a_X, a_Y, a_Z ) );
return;
}
- for( std::list< cClientHandle* >::iterator itr = m_pState->LoadedByClient.begin(); itr != m_pState->LoadedByClient.end(); ++itr )
+ for (cClientHandleList::iterator itr = m_LoadedByClient.begin(); itr != m_LoadedByClient.end(); ++itr )
{
- if( *itr == a_Client )
+ if ( *itr == a_Client )
{
unsigned int index = MakeIndex( a_X, a_Y, a_Z );
cPacket_BlockChange BlockChange;
@@ -823,9 +805,85 @@ void cChunk::SendBlockTo( int a_X, int a_Y, int a_Z, cClientHandle* a_Client ) void cChunk::AddBlockEntity( cBlockEntity* a_BlockEntity )
{
- m_pState->BlockListCriticalSection.Lock();
- m_pState->BlockEntities.push_back( a_BlockEntity );
- m_pState->BlockListCriticalSection.Unlock();
+ 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;
+}
+
+
+
+
+
+void cChunk::CollectPickupsByPlayer(cPlayer * a_Player)
+{
+ cCSLock Lock(m_CSEntities);
+
+ 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
+ {
+ (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
+ cCSLock Lock(m_CSEntities);
+ 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)
+ )
+ )
+ {
+ (reinterpret_cast<cSignEntity *>(*itr))->SetLines(a_Line1, a_Line2, a_Line3, a_Line4);
+ (*itr)->SendTo(NULL);
+ }
+ } // for itr - m_BlockEntities[]
}
@@ -834,9 +892,8 @@ void cChunk::AddBlockEntity( cBlockEntity* a_BlockEntity ) void cChunk::RemoveBlockEntity( cBlockEntity* a_BlockEntity )
{
- m_pState->BlockListCriticalSection.Lock();
- m_pState->BlockEntities.remove( a_BlockEntity );
- m_pState->BlockListCriticalSection.Unlock();
+ cCSLock Lock(m_CSBlockLists);
+ m_BlockEntities.remove( a_BlockEntity );
}
@@ -845,16 +902,18 @@ void cChunk::RemoveBlockEntity( cBlockEntity* a_BlockEntity ) void cChunk::AddClient( cClientHandle* a_Client )
{
- m_pState->LoadedByClient.remove( a_Client );
- m_pState->LoadedByClient.push_back( a_Client );
+ {
+ cCSLock Lock(m_CSClients);
+ m_LoadedByClient.remove( a_Client );
+ m_LoadedByClient.push_back( a_Client );
+ }
- LockEntities();
- for( EntityList::iterator itr = m_pState->Entities.begin(); itr != m_pState->Entities.end(); ++itr )
+ cCSLock Lock(m_CSEntities);
+ for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr )
{
- LOG("%i %i %i Spawning on %s", m_PosX, m_PosY, m_PosZ, a_Client->GetUsername().c_str() );
+ LOG("Entity at [%i %i %i] spawning for player \"%s\"", m_PosX, m_PosY, m_PosZ, a_Client->GetUsername().c_str() );
(*itr)->SpawnOn( a_Client );
}
- UnlockEntities();
}
@@ -863,18 +922,20 @@ void cChunk::AddClient( cClientHandle* a_Client ) void cChunk::RemoveClient( cClientHandle* a_Client )
{
- m_pState->LoadedByClient.remove( a_Client );
+ {
+ cCSLock Lock(m_CSClients);
+ m_LoadedByClient.remove( a_Client );
+ }
- if( !a_Client->IsDestroyed() )
+ if ( !a_Client->IsDestroyed() )
{
- LockEntities();
- for( EntityList::iterator itr = m_pState->Entities.begin(); itr != m_pState->Entities.end(); ++itr )
+ cCSLock Lock(m_CSEntities);
+ for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr )
{
- LOG("%i %i %i Destroying on %s", m_PosX, m_PosY, m_PosZ, a_Client->GetUsername().c_str() );
+ LOG("chunk [%i, %i] destroying entity #%i for player \"%s\"", m_PosX, m_PosZ, (*itr)->GetUniqueID(), a_Client->GetUsername().c_str() );
cPacket_DestroyEntity DestroyEntity( *itr );
a_Client->Send( DestroyEntity );
}
- UnlockEntities();
}
}
@@ -882,53 +943,52 @@ void cChunk::RemoveClient( cClientHandle* a_Client ) -void cChunk::AddEntity( cEntity & a_Entity )
+bool cChunk::HasClient( cClientHandle* a_Client )
{
- LockEntities();
- m_pState->Entities.push_back( &a_Entity );
- UnlockEntities();
+ cCSLock Lock(m_CSClients);
+ for (cClientHandleList::const_iterator itr = m_LoadedByClient.begin(); itr != m_LoadedByClient.end(); ++itr)
+ {
+ if ((*itr) == a_Client)
+ {
+ return true;
+ }
+ }
+ return false;
}
-bool cChunk::RemoveEntity( cEntity & a_Entity, cChunk* a_CalledFrom /* = 0 */ )
+bool cChunk::HasAnyClient(void)
{
- LockEntities();
- unsigned int SizeBefore = m_pState->Entities.size();
- m_pState->Entities.remove( &a_Entity );
- if( SizeBefore == m_pState->Entities.size() )
- {
- LOG("WARNING: Entity was not in chunk %i %i %i", m_PosX, m_PosY, m_PosZ );
- if( !a_CalledFrom )
- {
- UnlockEntities();
- return m_World->RemoveEntityFromChunk( a_Entity, this );
- }
- UnlockEntities();
- return false;
- }
- UnlockEntities();
- return true;
+ cCSLock Lock(m_CSClients);
+ return !m_LoadedByClient.empty();
}
-void cChunk::LockEntities()
+void cChunk::AddEntity( cEntity * a_Entity )
{
- m_EntitiesCriticalSection->Lock();
+ cCSLock Lock(m_CSEntities);
+ m_Entities.push_back( a_Entity );
}
-void cChunk::UnlockEntities()
+void cChunk::RemoveEntity(cEntity * a_Entity)
{
- m_EntitiesCriticalSection->Unlock();
+ size_t SizeBefore, SizeAfter;
+ {
+ cCSLock Lock(m_CSEntities);
+ SizeBefore = m_Entities.size();
+ m_Entities.remove(a_Entity);
+ SizeAfter = m_Entities.size();
+ }
}
@@ -937,7 +997,7 @@ void cChunk::UnlockEntities() char cChunk::GetBlock( int a_X, int a_Y, int a_Z )
{
- if(a_X < 0 || a_X >= 16 || a_Y < 0 || a_Y >= 128 || a_Z < 0 || a_Z >= 16) return 0; // Clip
+ if ((a_X < 0) || (a_X >= 16) || (a_Y < 0) || (a_Y >= 128) || (a_Z < 0) || (a_Z >= 16)) return 0; // Clip
int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
return m_BlockType[index];
@@ -957,28 +1017,6 @@ char cChunk::GetBlock( int a_BlockIdx ) -cBlockEntity* cChunk::GetBlockEntity( int a_X, int a_Y, int a_Z )
-{
- m_pState->BlockListCriticalSection.Lock();
- for( std::list<cBlockEntity*>::iterator itr = m_pState->BlockEntities.begin(); itr != m_pState->BlockEntities.end(); ++itr)
- {
- if( (*itr)->GetPosX() == a_X &&
- (*itr)->GetPosY() == a_Y &&
- (*itr)->GetPosZ() == a_Z )
- {
- cBlockEntity* BlockEnt = *itr;
- m_pState->BlockListCriticalSection.Unlock();
- return BlockEnt;
- }
- }
- m_pState->BlockListCriticalSection.Unlock();
- return 0;
-}
-
-
-
-
-
/// Loads the chunk from the old-format disk file, erases the file afterwards. Returns true if successful
bool cChunk::LoadFromDisk()
{
@@ -998,7 +1036,7 @@ bool cChunk::LoadFromDisk() }
// Now load Block Entities
- cCSLock Lock(m_pState->BlockListCriticalSection);
+ cCSLock Lock(m_CSEntities);
ENUM_BLOCK_ID BlockType;
while (f.Read(&BlockType, sizeof(ENUM_BLOCK_ID)) == sizeof(ENUM_BLOCK_ID))
@@ -1007,42 +1045,41 @@ bool cChunk::LoadFromDisk() {
case E_BLOCK_CHEST:
{
- cChestEntity * ChestEntity = new cChestEntity( 0, 0, 0, this );
+ cChestEntity * ChestEntity = new cChestEntity( 0, 0, 0, m_World );
if (!ChestEntity->LoadFromFile(f))
{
LOGERROR("ERROR READING CHEST FROM FILE %s", SourceFile.c_str());
delete ChestEntity;
return false;
}
- m_pState->BlockEntities.push_back( ChestEntity );
+ m_BlockEntities.push_back( ChestEntity );
break;
}
case E_BLOCK_FURNACE:
{
- cFurnaceEntity* FurnaceEntity = new cFurnaceEntity( 0, 0, 0, this );
+ cFurnaceEntity* FurnaceEntity = new cFurnaceEntity( 0, 0, 0, m_World );
if (!FurnaceEntity->LoadFromFile(f))
{
LOGERROR("ERROR READING FURNACE FROM FILE %s", SourceFile.c_str());
delete FurnaceEntity;
return false;
}
- m_pState->BlockEntities.push_back( FurnaceEntity );
- m_pState->TickBlockEntities.push_back( FurnaceEntity ); // They need tickin'
+ m_BlockEntities.push_back( FurnaceEntity );
break;
}
case E_BLOCK_SIGN_POST:
case E_BLOCK_WALLSIGN:
{
- cSignEntity * SignEntity = new cSignEntity(BlockType, 0, 0, 0, this );
+ cSignEntity * SignEntity = new cSignEntity(BlockType, 0, 0, 0, m_World );
if (!SignEntity->LoadFromFile( f ) )
{
LOGERROR("ERROR READING SIGN FROM FILE %s", SourceFile.c_str());
delete SignEntity;
return false;
}
- m_pState->BlockEntities.push_back( SignEntity );
+ m_BlockEntities.push_back( SignEntity );
break;
}
@@ -1072,9 +1109,10 @@ bool cChunk::LoadFromDisk() -void cChunk::Broadcast( const cPacket & a_Packet, cClientHandle* a_Exclude /* = 0 */ ) const
+void cChunk::Broadcast( const cPacket * a_Packet, cClientHandle* a_Exclude)
{
- for( std::list< cClientHandle* >::const_iterator itr = m_pState->LoadedByClient.begin(); itr != m_pState->LoadedByClient.end(); ++itr )
+ cCSLock Lock(m_CSClients);
+ for (cClientHandleList::const_iterator itr = m_LoadedByClient.begin(); itr != m_LoadedByClient.end(); ++itr )
{
if (*itr == a_Exclude)
{
@@ -1088,58 +1126,69 @@ void cChunk::Broadcast( const cPacket & a_Packet, cClientHandle* a_Exclude /* = +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();
+}
+
+
+
+
+
void cChunk::LoadFromJson( const Json::Value & a_Value )
{
- cCSLock Lock(m_pState->BlockListCriticalSection);
+ cCSLock Lock(m_CSEntities);
// Load chests
Json::Value AllChests = a_Value.get("Chests", Json::nullValue);
if (!AllChests.empty())
{
- for( Json::Value::iterator itr = AllChests.begin(); itr != AllChests.end(); ++itr )
+ for ( Json::Value::iterator itr = AllChests.begin(); itr != AllChests.end(); ++itr )
{
Json::Value & Chest = *itr;
- cChestEntity* ChestEntity = new cChestEntity(0,0,0, this);
- if( !ChestEntity->LoadFromJson( Chest ) )
+ cChestEntity* ChestEntity = new cChestEntity(0, 0, 0, m_World);
+ if ( !ChestEntity->LoadFromJson( Chest ) )
{
LOGERROR("ERROR READING CHEST FROM JSON!" );
delete ChestEntity;
}
- else m_pState->BlockEntities.push_back( ChestEntity );
+ else m_BlockEntities.push_back( ChestEntity );
}
}
// Load furnaces
Json::Value AllFurnaces = a_Value.get("Furnaces", Json::nullValue);
- if( !AllFurnaces.empty() )
+ if ( !AllFurnaces.empty() )
{
- for( Json::Value::iterator itr = AllFurnaces.begin(); itr != AllFurnaces.end(); ++itr )
+ for ( Json::Value::iterator itr = AllFurnaces.begin(); itr != AllFurnaces.end(); ++itr )
{
Json::Value & Furnace = *itr;
- cFurnaceEntity* FurnaceEntity = new cFurnaceEntity(0,0,0, this);
- if( !FurnaceEntity->LoadFromJson( Furnace ) )
+ cFurnaceEntity* FurnaceEntity = new cFurnaceEntity(0, 0, 0, m_World);
+ if ( !FurnaceEntity->LoadFromJson( Furnace ) )
{
LOGERROR("ERROR READING FURNACE FROM JSON!" );
delete FurnaceEntity;
}
- else m_pState->BlockEntities.push_back( FurnaceEntity );
+ else m_BlockEntities.push_back( FurnaceEntity );
}
}
// Load signs
Json::Value AllSigns = a_Value.get("Signs", Json::nullValue);
- if( !AllSigns.empty() )
+ if ( !AllSigns.empty() )
{
- for( Json::Value::iterator itr = AllSigns.begin(); itr != AllSigns.end(); ++itr )
+ for ( Json::Value::iterator itr = AllSigns.begin(); itr != AllSigns.end(); ++itr )
{
Json::Value & Sign = *itr;
- cSignEntity* SignEntity = new cSignEntity( E_BLOCK_SIGN_POST, 0,0,0, this);
- if( !SignEntity->LoadFromJson( Sign ) )
+ cSignEntity* SignEntity = new cSignEntity( E_BLOCK_SIGN_POST, 0, 0, 0, m_World);
+ if ( !SignEntity->LoadFromJson( Sign ) )
{
LOGERROR("ERROR READING SIGN FROM JSON!" );
delete SignEntity;
}
- else m_pState->BlockEntities.push_back( SignEntity );
+ else m_BlockEntities.push_back( SignEntity );
}
}
}
@@ -1153,11 +1202,11 @@ void cChunk::SaveToJson( Json::Value & a_Value ) Json::Value AllChests;
Json::Value AllFurnaces;
Json::Value AllSigns;
- cCSLock Lock(m_pState->BlockListCriticalSection);
- for (std::list<cBlockEntity*>::iterator itr = m_pState->BlockEntities.begin(); itr != m_pState->BlockEntities.end(); ++itr)
+ cCSLock Lock(m_CSEntities);
+ for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr)
{
- cBlockEntity* BlockEntity = *itr;
- switch( BlockEntity->GetBlockType() )
+ cBlockEntity * BlockEntity = *itr;
+ switch ( BlockEntity->GetBlockType() )
{
case E_BLOCK_CHEST:
{
@@ -1196,48 +1245,17 @@ void cChunk::SaveToJson( Json::Value & a_Value ) } // for itr - BlockEntities[]
if( !AllChests.empty() )
+ {
a_Value["Chests"] = AllChests;
+ }
if( !AllFurnaces.empty() )
+ {
a_Value["Furnaces"] = AllFurnaces;
+ }
if( !AllSigns.empty() )
+ {
a_Value["Signs"] = AllSigns;
-}
-
-
-
-
-
-EntityList & cChunk::GetEntities()
-{
- return m_pState->Entities;
-}
-
-
-
-
-
-const ClientHandleList & cChunk::GetClients()
-{
- return m_pState->LoadedByClient;
-}
-
-
-
-
-
-void cChunk::AddTickBlockEntity( cFurnaceEntity* a_Entity )
-{
- m_pState->TickBlockEntities.remove( a_Entity );
- m_pState->TickBlockEntities.push_back( a_Entity );
-}
-
-
-
-
-
-void cChunk::RemoveTickBlockEntity( cFurnaceEntity* a_Entity )
-{
- m_pState->TickBlockEntities.remove( a_Entity );
+ }
}
@@ -1255,42 +1273,6 @@ void cChunk::PositionToWorldPosition(int a_ChunkX, int a_ChunkY, int a_ChunkZ, i -void cChunk::AddReference()
-{
- cCSLock Lock(m_pState->ReferenceCriticalSection);
- m_pState->NumRefs++;
-}
-
-
-
-
-
-void cChunk::RemoveReference()
-{
- cCSLock Lock(m_pState->ReferenceCriticalSection);
- m_pState->NumRefs--;
- assert (m_pState->NumRefs >= 0);
- if (m_pState->NumRefs < 0)
- {
- LOGWARN("WARNING: cChunk: Tried to remove reference, but the chunk is not referenced!");
- }
-}
-
-
-
-
-
-int cChunk::GetReferenceCount()
-{
- cCSLock Lock(m_pState->ReferenceCriticalSection);
- int Refs = m_pState->NumRefs;
- return Refs;
-}
-
-
-
-
-
#if !C_CHUNK_USE_INLINE
# include "cChunk.inc"
#endif
|