|
@@ -3,6 +3,8 @@
|
|
|
#include "Block.h"
|
|
|
#include "Globals.h"
|
|
|
|
|
|
+#include "Registries.h"
|
|
|
+
|
|
|
|
|
|
Chunk::Chunk( Framework::Punkt location, int dimensionId )
|
|
|
: ReferenceCounter(),
|
|
@@ -10,15 +12,16 @@ Chunk::Chunk( Framework::Punkt location, int dimensionId )
|
|
|
location( location )
|
|
|
{
|
|
|
blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
|
|
|
- Block *val = (Block *)AIR_BLOCK;
|
|
|
- std::uninitialized_fill_n( blocks, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT, val );
|
|
|
+ blockIds = new unsigned short[ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
|
|
|
+ memset( blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( Block* ) );
|
|
|
+ memset( blockIds, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
|
|
|
zNeighbours[ 0 ] = 0;
|
|
|
zNeighbours[ 1 ] = 0;
|
|
|
zNeighbours[ 2 ] = 0;
|
|
|
zNeighbours[ 3 ] = 0;
|
|
|
}
|
|
|
|
|
|
-Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader *zReader )
|
|
|
+Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader )
|
|
|
: Chunk( location, dimensionId )
|
|
|
{
|
|
|
load( zReader );
|
|
@@ -28,16 +31,23 @@ Chunk::~Chunk()
|
|
|
{
|
|
|
for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
{
|
|
|
- if( IS_BLOCK( blocks[ i ] ) )
|
|
|
+ if( blocks[ i ] )
|
|
|
blocks[ i ]->release();
|
|
|
}
|
|
|
delete[] blocks;
|
|
|
+ delete[] blockIds;
|
|
|
}
|
|
|
|
|
|
-Block *Chunk::zBlockNeighbor( Framework::Vec3<int> location )
|
|
|
+Framework::Either<Block*, int> 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 ];
|
|
|
+ {
|
|
|
+ int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
+ if( blocks[ index ] )
|
|
|
+ return blocks[ index ];
|
|
|
+ else
|
|
|
+ return (int)blockIds[ index ];
|
|
|
+ }
|
|
|
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;
|
|
@@ -48,66 +58,101 @@ bool Chunk::updateVisibility()
|
|
|
bool update = false;
|
|
|
for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
{
|
|
|
- if( IS_BLOCK( blocks[ i ] ) )
|
|
|
+ if( blocks[ i ] )
|
|
|
update |= blocks[ i ]->updateVisibility();
|
|
|
}
|
|
|
return update;
|
|
|
}
|
|
|
|
|
|
-Block *Chunk::getBlockAt( Framework::Vec3<int> location ) const
|
|
|
+Framework::Either<Block*, int> Chunk::zBlockAt( Framework::Vec3<int> location ) const
|
|
|
{
|
|
|
- Block *result = zBlockAt( location );
|
|
|
- if( result )
|
|
|
- return dynamic_cast<Block *>(result->getThis());
|
|
|
- return 0;
|
|
|
+ int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
+ assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
|
|
|
+ if( blocks[ index ] )
|
|
|
+ return blocks[ index ];
|
|
|
+ else
|
|
|
+ return (int)blockIds[ index ];
|
|
|
}
|
|
|
|
|
|
-Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
|
|
|
+const Block* Chunk::zBlockConst( Framework::Vec3<int> location ) const
|
|
|
{
|
|
|
- return blocks[ (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z ];
|
|
|
+ Block* b = zBlockAt( location );
|
|
|
+ if( b )
|
|
|
+ return b;
|
|
|
+ int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
+ if( blockIds[ index ] )
|
|
|
+ return STATIC_REGISTRY( BlockType ).zElement( blockIds[ index ] )->zDefault();
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
|
|
|
+void Chunk::putBlockAt( Framework::Vec3<int> location, Block* block )
|
|
|
{
|
|
|
int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
- assert( index < CHUNK_SIZE *CHUNK_SIZE *WORLD_HEIGHT );
|
|
|
- Block *old = blocks[ index ];
|
|
|
+ assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
|
|
|
+ Block* old = blocks[ index ];
|
|
|
+ if( block )
|
|
|
+ blockIds[ index ] = (unsigned short)block->zBlockType()->getId();
|
|
|
blocks[ index ] = block;
|
|
|
- Block *neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( SOUTH, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( SOUTH, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( NORTH, neighbor );
|
|
|
neighbor = zBlockNeighbor( location + getDirection( EAST ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( WEST, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( WEST, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( EAST, neighbor );
|
|
|
neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( NORTH, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( NORTH, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( SOUTH, neighbor );
|
|
|
neighbor = zBlockNeighbor( location + getDirection( WEST ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( EAST, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( EAST, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( WEST, neighbor );
|
|
|
neighbor = zBlockNeighbor( location + getDirection( TOP ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( BOTTOM, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( BOTTOM, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( TOP, neighbor );
|
|
|
neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
|
|
|
- if( IS_BLOCK( neighbor ) )
|
|
|
- neighbor->setNeighbour( TOP, block );
|
|
|
- if( IS_BLOCK( block ) )
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbour( TOP, block );
|
|
|
+ if( block )
|
|
|
block->setNeighbour( BOTTOM, neighbor );
|
|
|
- if( IS_BLOCK( old ) )
|
|
|
+ if( old )
|
|
|
old->release();
|
|
|
}
|
|
|
|
|
|
-void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
|
|
|
+void Chunk::putBlockTypeAt( Framework::Vec3<int> location, int type )
|
|
|
+{
|
|
|
+ int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
+ assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
|
|
|
+ blockIds[ index ] = (unsigned short)type;
|
|
|
+ Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( SOUTH, type );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( EAST ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( WEST, type );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( NORTH, type );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( WEST ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( EAST, type );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( TOP ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( BOTTOM, type );
|
|
|
+ neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
|
|
|
+ if( neighbor.isA() )
|
|
|
+ ((Block*)neighbor)->setNeighbourType( TOP, type );
|
|
|
+}
|
|
|
+
|
|
|
+void Chunk::setNeighbor( Direction dir, Chunk* zChunk )
|
|
|
{
|
|
|
zNeighbours[ getDirectionIndex( dir ) ] = zChunk;
|
|
|
for( int i = 0; i < CHUNK_SIZE; i++ )
|
|
@@ -117,45 +162,57 @@ void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
|
|
|
if( dir == NORTH )
|
|
|
{
|
|
|
int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
- if( IS_BLOCK( blocks[ index ] ) )
|
|
|
- blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z ] );
|
|
|
+ if( blocks[ index ] )
|
|
|
+ {
|
|
|
+ int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
+ if( zChunk->blocks[ j ] )
|
|
|
+ blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ j ] );
|
|
|
+ else
|
|
|
+ blocks[ index ]->setNeighbourType( NORTH, zChunk->blockIds[ j ] );
|
|
|
+ }
|
|
|
}
|
|
|
else if( dir == EAST )
|
|
|
{
|
|
|
int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
- if( IS_BLOCK( blocks[ index ] ) )
|
|
|
+ if( blocks[ index ] )
|
|
|
blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * WORLD_HEIGHT + z ] );
|
|
|
}
|
|
|
else if( dir == SOUTH )
|
|
|
{
|
|
|
int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
- if( IS_BLOCK( blocks[ index ] ) )
|
|
|
+ if( blocks[ index ] )
|
|
|
blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * WORLD_HEIGHT + z ] );
|
|
|
}
|
|
|
else if( dir == WEST )
|
|
|
{
|
|
|
int index = i * WORLD_HEIGHT + z;
|
|
|
- if( IS_BLOCK( blocks[ index ] ) )
|
|
|
+ if( blocks[ index ] )
|
|
|
blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z ] );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void Chunk::load( Framework::StreamReader *zReader )
|
|
|
+void Chunk::load( Framework::StreamReader* zReader )
|
|
|
{
|
|
|
+ zReader->lese( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
|
|
|
for( int x = 0; x < CHUNK_SIZE; x++ )
|
|
|
{
|
|
|
for( int y = 0; y < CHUNK_SIZE; y++ )
|
|
|
{
|
|
|
for( int z = 0; z < WORLD_HEIGHT; z++ )
|
|
|
{
|
|
|
- int blockType;
|
|
|
- zReader->lese( (char *)&blockType, 4 );
|
|
|
- if( blockType >= 0 )
|
|
|
- {
|
|
|
- Block *block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zReader );
|
|
|
+ unsigned short blockType;
|
|
|
+ zReader->lese( (char*)&blockType, 2 );
|
|
|
+ Block* block = STATIC_REGISTRY( BlockType ).zElement( blockType )->loadBlock( Framework::Vec3<int>( x + location.x - CHUNK_SIZE / 2, y + location.y - CHUNK_SIZE / 2, z ), zReader );
|
|
|
+ if( block )
|
|
|
putBlockAt( { x, y, z }, block );
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if( STATIC_REGISTRY( BlockType ).zElement( blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] )->needsInstance() )
|
|
|
+ putBlockAt( { x, y, z }, STATIC_REGISTRY( BlockType ).zElement( blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] )->createBlock( { x + location.x - CHUNK_SIZE / 2, y + location.y - CHUNK_SIZE / 2, z } ) );
|
|
|
+ else
|
|
|
+ putBlockTypeAt( { x, y, z }, blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] );
|
|
|
}
|
|
|
}
|
|
|
}
|