|
@@ -34,6 +34,15 @@ Chunk::~Chunk()
|
|
|
delete[] blocks;
|
|
|
}
|
|
|
|
|
|
+Block *Chunk::zBlockNeighbor( Framework::Vec3<int> location )
|
|
|
+{
|
|
|
+ if( location.x >= 0 && location.x < CHUNK_SIZE && location.y <= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT )
|
|
|
+ return blocks[ (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z ];
|
|
|
+ if( location.z >= 0 && location.z < WORLD_HEIGHT )
|
|
|
+ return currentGame->zBlockAt( { location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId );
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
bool Chunk::updateVisibility()
|
|
|
{
|
|
|
bool update = false;
|
|
@@ -60,33 +69,40 @@ Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
|
|
|
|
|
|
void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
|
|
|
{
|
|
|
- int index = (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z;
|
|
|
+ int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
+ assert( index < CHUNK_SIZE *CHUNK_SIZE *WORLD_HEIGHT );
|
|
|
Block *old = blocks[ index ];
|
|
|
blocks[ index ] = block;
|
|
|
- Block *neighbor = currentGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
|
|
|
+ Block *neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( SOUTH, block );
|
|
|
- block->setNeighbour( NORTH, neighbor );
|
|
|
- neighbor = currentGame->zBlockAt( location + getDirection( EAST ), dimensionId );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( NORTH, neighbor );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( EAST ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( WEST, block );
|
|
|
- block->setNeighbour( EAST, neighbor );
|
|
|
- neighbor = currentGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( EAST, neighbor );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( NORTH, block );
|
|
|
- block->setNeighbour( SOUTH, neighbor );
|
|
|
- neighbor = currentGame->zBlockAt( location + getDirection( WEST ), dimensionId );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( SOUTH, neighbor );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( WEST ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( EAST, block );
|
|
|
- block->setNeighbour( WEST, neighbor );
|
|
|
- neighbor = currentGame->zBlockAt( location + getDirection( TOP ), dimensionId );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( WEST, neighbor );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( TOP ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( BOTTOM, block );
|
|
|
- block->setNeighbour( TOP, neighbor );
|
|
|
- neighbor = currentGame->zBlockAt( location + getDirection( BOTTOM ), dimensionId );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( TOP, neighbor );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
|
|
|
if( IS_BLOCK( neighbor ) )
|
|
|
neighbor->setNeighbour( TOP, block );
|
|
|
- block->setNeighbour( BOTTOM, neighbor );
|
|
|
+ if( IS_BLOCK( block ) )
|
|
|
+ block->setNeighbour( BOTTOM, neighbor );
|
|
|
if( IS_BLOCK( old ) )
|
|
|
old->release();
|
|
|
}
|