|
@@ -10,7 +10,7 @@ Chunk::Chunk( Framework::Punkt location, Game *zGame, int dimensionId )
|
|
|
location( location )
|
|
|
{
|
|
|
blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
|
|
|
- memset( blocks, 0, sizeof( Block * ) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT );
|
|
|
+ memset( blocks, AIR_BLOCK, sizeof( Block * ) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT );
|
|
|
zNeighbours[ 0 ] = 0;
|
|
|
zNeighbours[ 1 ] = 0;
|
|
|
zNeighbours[ 2 ] = 0;
|
|
@@ -27,7 +27,7 @@ Chunk::~Chunk()
|
|
|
{
|
|
|
for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
{
|
|
|
- if( blocks[ i ] )
|
|
|
+ if( IS_BLOCK( blocks[ i ] ) )
|
|
|
blocks[ i ]->release();
|
|
|
}
|
|
|
delete[] blocks;
|
|
@@ -61,42 +61,30 @@ void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
|
|
|
Block *old = blocks[ index ];
|
|
|
blocks[ index ] = block;
|
|
|
Block *neighbor = zGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
|
|
|
- if( neighbor )
|
|
|
- {
|
|
|
- neighbor->neighbours[ SOUTH ] = block;
|
|
|
- block->neighbours[ NORTH ] = neighbor;
|
|
|
- }
|
|
|
+ if( IS_BLOCK( neighbor ) )
|
|
|
+ neighbor->setNeighbour( SOUTH, block );
|
|
|
+ block->setNeighbour( NORTH, neighbor );
|
|
|
neighbor = zGame->zBlockAt( location + getDirection( EAST ), dimensionId );
|
|
|
- if( neighbor )
|
|
|
- {
|
|
|
- neighbor->neighbours[ WEST ] = block;
|
|
|
- block->neighbours[ EAST ] = neighbor;
|
|
|
- }
|
|
|
+ if( IS_BLOCK( neighbor ) )
|
|
|
+ neighbor->setNeighbour( WEST, block );
|
|
|
+ block->setNeighbour( EAST, neighbor );
|
|
|
neighbor = zGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
|
|
|
- if( neighbor ) {}
|
|
|
- {
|
|
|
- neighbor->neighbours[ NORTH ] = block;
|
|
|
- block->neighbours[ SOUTH ] = neighbor;
|
|
|
- }
|
|
|
+ if( IS_BLOCK( neighbor ) ) {}
|
|
|
+ neighbor->setNeighbour( NORTH, block );
|
|
|
+ block->setNeighbour( SOUTH, neighbor );
|
|
|
neighbor = zGame->zBlockAt( location + getDirection( WEST ), dimensionId );
|
|
|
- if( neighbor )
|
|
|
- {
|
|
|
- neighbor->neighbours[ EAST ] = block;
|
|
|
- block->neighbours[ WEST ] = neighbor;
|
|
|
- }
|
|
|
+ if( IS_BLOCK( neighbor ) )
|
|
|
+ neighbor->setNeighbour( EAST, block );
|
|
|
+ block->setNeighbour( WEST, neighbor );
|
|
|
neighbor = zGame->zBlockAt( location + getDirection( TOP ), dimensionId );
|
|
|
- if( neighbor )
|
|
|
- {
|
|
|
- neighbor->neighbours[ BOTTOM ] = block;
|
|
|
- block->neighbours[ TOP ] = neighbor;
|
|
|
- }
|
|
|
+ if( IS_BLOCK( neighbor ) )
|
|
|
+ neighbor->setNeighbour( BOTTOM, block );
|
|
|
+ block->setNeighbour( TOP, neighbor );
|
|
|
neighbor = zGame->zBlockAt( location + getDirection( BOTTOM ), dimensionId );
|
|
|
- if( neighbor )
|
|
|
- {
|
|
|
- neighbor->neighbours[ TOP ] = block;
|
|
|
- block->neighbours[ BOTTOM ] = neighbor;
|
|
|
- }
|
|
|
- if( old )
|
|
|
+ if( IS_BLOCK( neighbor ) )
|
|
|
+ neighbor->setNeighbour( TOP, block );
|
|
|
+ block->setNeighbour( BOTTOM, neighbor );
|
|
|
+ if( IS_BLOCK( old ) )
|
|
|
old->release();
|
|
|
}
|
|
|
|
|
@@ -111,25 +99,25 @@ void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
|
|
|
{
|
|
|
int index = i * CHUNK_SIZE * CHUNK_SIZE + z;
|
|
|
if( blocks[ index ] )
|
|
|
- blocks[ index ]->neighbours[ NORTH ] = zChunk->blocks[ ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z ];
|
|
|
+ blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z ] );
|
|
|
}
|
|
|
else if( dir == EAST )
|
|
|
{
|
|
|
int index = ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z;
|
|
|
if( blocks[ index ] )
|
|
|
- blocks[ index ]->neighbours[ EAST ] = zChunk->blocks[ i * CHUNK_SIZE + z ];
|
|
|
+ blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * CHUNK_SIZE + z ] );
|
|
|
}
|
|
|
else if( dir == SOUTH )
|
|
|
{
|
|
|
int index = ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z;
|
|
|
if( blocks[ index ] )
|
|
|
- blocks[ index ]->neighbours[ SOUTH ] = zChunk->blocks[ i * CHUNK_SIZE * CHUNK_SIZE + z ];
|
|
|
+ blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * CHUNK_SIZE + z ] );
|
|
|
}
|
|
|
else if( dir == WEST )
|
|
|
{
|
|
|
int index = i * CHUNK_SIZE + z;
|
|
|
if( blocks[ index ] )
|
|
|
- blocks[ index ]->neighbours[ WEST ] = zChunk->blocks[ ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z ];
|
|
|
+ blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z ] );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -148,7 +136,7 @@ void Chunk::load( Framework::StreamReader *zReader )
|
|
|
if( blockType >= 0 )
|
|
|
{
|
|
|
Block *block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zGame, zReader );
|
|
|
- blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ] = block;
|
|
|
+ putBlockAt( { x, y, z }, block );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -164,7 +152,7 @@ void Chunk::save( Framework::StreamWriter *zWriter )
|
|
|
for( int z = 0; z < WORLD_HEIGHT; z++ )
|
|
|
{
|
|
|
int index = ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z;
|
|
|
- int blockType = blocks[ index ] ? blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ]->zType->getId() : -1;
|
|
|
+ int blockType = blocks[ index ] ? blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ]->zBlockType()->getId() : -1;
|
|
|
zWriter->schreibe( (char *)&blockType, 4 );
|
|
|
if( blockType >= 0 )
|
|
|
StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );
|