|
@@ -1,6 +1,5 @@
|
|
|
#include "Chunk.h"
|
|
|
#include "Constants.h"
|
|
|
-#include "Block.h"
|
|
|
#include "Globals.h"
|
|
|
|
|
|
#include "Registries.h"
|
|
@@ -9,18 +8,8 @@
|
|
|
Chunk::Chunk( Framework::Punkt location, int dimensionId )
|
|
|
: ReferenceCounter(),
|
|
|
dimensionId( dimensionId ),
|
|
|
- location( location ),
|
|
|
- added( 0 )
|
|
|
-{
|
|
|
- blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
|
|
|
- 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;
|
|
|
-}
|
|
|
+ location( location )
|
|
|
+{}
|
|
|
|
|
|
Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader )
|
|
|
: Chunk( location, dimensionId )
|
|
@@ -29,224 +18,54 @@ Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReade
|
|
|
}
|
|
|
|
|
|
Chunk::~Chunk()
|
|
|
-{
|
|
|
- for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
- {
|
|
|
- if( blocks[ i ] )
|
|
|
- blocks[ i ]->release();
|
|
|
- }
|
|
|
- delete[] blocks;
|
|
|
- delete[] blockIds;
|
|
|
-}
|
|
|
+{}
|
|
|
|
|
|
-Framework::Either<Block*, int> Chunk::zBlockNeighbor( Framework::Vec3<int> location )
|
|
|
+Block* Chunk::zBlockAt( Framework::Vec3<int> location ) const
|
|
|
{
|
|
|
- if( location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT )
|
|
|
+ for( Block* b : blocks )
|
|
|
{
|
|
|
- int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
- if( blocks[ index ] )
|
|
|
- return blocks[ index ];
|
|
|
- else
|
|
|
- return (int)blockIds[ index ];
|
|
|
+ if( (Framework::Vec3<int>)b->getPos() == location )
|
|
|
+ return b;
|
|
|
}
|
|
|
- if( added && 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()
|
|
|
+void Chunk::setBlock( Block* block )
|
|
|
{
|
|
|
- bool update = false;
|
|
|
- for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
+ Framework::Vec3<int> pos = (Framework::Vec3<int>)block->getPos();
|
|
|
+ for( Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++ )
|
|
|
{
|
|
|
- if( blocks[ i ] )
|
|
|
- update |= blocks[ i ]->updateVisibility();
|
|
|
- }
|
|
|
- return update;
|
|
|
-}
|
|
|
-
|
|
|
-Framework::Either<Block*, int> Chunk::zBlockAt( Framework::Vec3<int> location ) const
|
|
|
-{
|
|
|
- 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 ];
|
|
|
-}
|
|
|
-
|
|
|
-const Block* Chunk::zBlockConst( Framework::Vec3<int> location ) const
|
|
|
-{
|
|
|
- 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 )
|
|
|
-{
|
|
|
- int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
|
|
|
- assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
|
|
|
- Block* old = blocks[ index ];
|
|
|
- if( block )
|
|
|
- blockIds[ index ] = (unsigned short)block->zBlockType()->getId();
|
|
|
- blocks[ index ] = 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( neighbor.isA() )
|
|
|
- ((Block*)neighbor)->setNeighbour( WEST, block );
|
|
|
- if( block )
|
|
|
- block->setNeighbour( EAST, neighbor );
|
|
|
- neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
|
|
|
- if( neighbor.isA() )
|
|
|
- ((Block*)neighbor)->setNeighbour( NORTH, block );
|
|
|
- if( block )
|
|
|
- block->setNeighbour( SOUTH, neighbor );
|
|
|
- neighbor = zBlockNeighbor( location + getDirection( WEST ) );
|
|
|
- if( neighbor.isA() )
|
|
|
- ((Block*)neighbor)->setNeighbour( EAST, block );
|
|
|
- if( block )
|
|
|
- block->setNeighbour( WEST, neighbor );
|
|
|
- neighbor = zBlockNeighbor( location + getDirection( TOP ) );
|
|
|
- if( neighbor.isA() )
|
|
|
- ((Block*)neighbor)->setNeighbour( BOTTOM, block );
|
|
|
- if( block )
|
|
|
- block->setNeighbour( TOP, neighbor );
|
|
|
- neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
|
|
|
- if( neighbor.isA() )
|
|
|
- ((Block*)neighbor)->setNeighbour( TOP, block );
|
|
|
- if( block )
|
|
|
- block->setNeighbour( BOTTOM, neighbor );
|
|
|
- if( old )
|
|
|
- old->release();
|
|
|
-}
|
|
|
-
|
|
|
-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++ )
|
|
|
- {
|
|
|
- for( int z = 0; z < WORLD_HEIGHT; z++ )
|
|
|
+ if( pos == (Framework::Vec3<int>)iterator->getPos() )
|
|
|
{
|
|
|
- if( dir == NORTH )
|
|
|
- {
|
|
|
- int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
- if( blocks[ index ] )
|
|
|
- {
|
|
|
- int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
- if( zChunk && zChunk->blocks[ j ] )
|
|
|
- blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ j ] );
|
|
|
- else
|
|
|
- {
|
|
|
- blocks[ index ]->setNeighbour( NORTH, 0 );
|
|
|
- blocks[ index ]->setNeighbourType( NORTH, zChunk ? zChunk->blockIds[ j ] : 0 );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else if( dir == EAST )
|
|
|
- {
|
|
|
- int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
- if( blocks[ index ] )
|
|
|
- {
|
|
|
- int j = i * WORLD_HEIGHT + z;
|
|
|
- if( zChunk && zChunk->blocks[ j ] )
|
|
|
- blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ j ] );
|
|
|
- else
|
|
|
- {
|
|
|
- blocks[ index ]->setNeighbour( EAST, 0 );
|
|
|
- blocks[ index ]->setNeighbourType( EAST, zChunk ? zChunk->blockIds[ j ] : 0 );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else if( dir == SOUTH )
|
|
|
- {
|
|
|
- int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
|
|
|
- if( blocks[ index ] )
|
|
|
- {
|
|
|
- int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
|
|
|
- if( zChunk && zChunk->blocks[ j ] )
|
|
|
- blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ j ] );
|
|
|
- else
|
|
|
- {
|
|
|
- blocks[ index ]->setNeighbour( SOUTH, 0 );
|
|
|
- blocks[ index ]->setNeighbourType( SOUTH, zChunk ? zChunk->blockIds[ j ] : 0 );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else if( dir == WEST )
|
|
|
- {
|
|
|
- int index = i * WORLD_HEIGHT + z;
|
|
|
- if( blocks[ index ] )
|
|
|
- {
|
|
|
- int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
|
|
|
- if( zChunk && zChunk->blocks[ j ] )
|
|
|
- blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ j ] );
|
|
|
- else
|
|
|
- {
|
|
|
- blocks[ index ]->setNeighbour( WEST, 0 );
|
|
|
- blocks[ index ]->setNeighbourType( WEST, zChunk ? zChunk->blockIds[ j ] : 0 );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ iterator->release();
|
|
|
+ iterator.set( block );
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
+ blocks.add( block );
|
|
|
}
|
|
|
|
|
|
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++ )
|
|
|
+ Framework::Vec3<int> pos = { 0, 0, 0 };
|
|
|
+ unsigned short id;
|
|
|
+ zReader->lese( (char*)&id, 2 );
|
|
|
+ while( id )
|
|
|
{
|
|
|
- for( int y = 0; y < CHUNK_SIZE; y++ )
|
|
|
+ zReader->lese( (char*)&pos.x, 4 );
|
|
|
+ zReader->lese( (char*)&pos.y, 4 );
|
|
|
+ zReader->lese( (char*)&pos.z, 4 );
|
|
|
+ bool d;
|
|
|
+ zReader->lese( (char*)&d, 1 );
|
|
|
+ if( d )
|
|
|
{
|
|
|
- for( int z = 0; z < WORLD_HEIGHT; z++ )
|
|
|
- {
|
|
|
- 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 ] );
|
|
|
- }
|
|
|
- }
|
|
|
+ Block* block = STATIC_REGISTRY( BlockType ).zElement( id )->loadBlock( { pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z }, zReader );
|
|
|
+ if( block )
|
|
|
+ setBlock( block );
|
|
|
}
|
|
|
+ else if( STATIC_REGISTRY( BlockType ).zElement( id )->needsInstance() )
|
|
|
+ setBlock( STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( { pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z } ) );
|
|
|
+ zReader->lese( (char*)&id, 2 );
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -270,25 +89,8 @@ Framework::Vec3<int> Chunk::getMax() const
|
|
|
return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
|
|
|
}
|
|
|
|
|
|
-void Chunk::prepareRemove()
|
|
|
-{
|
|
|
- added = 0;
|
|
|
- for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
|
|
|
- {
|
|
|
- if( blocks[ i ] )
|
|
|
- blocks[ i ]->remove();
|
|
|
- }
|
|
|
- for( int i = 0; i < 4; i++ )
|
|
|
- {
|
|
|
- if( zNeighbours[ i ] )
|
|
|
- {
|
|
|
- zNeighbours[ i ]->setNeighbor( getOppositeDirection( getDirectionFromIndex( i ) ), 0 );
|
|
|
- zNeighbours[ i ] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void Chunk::setAdded()
|
|
|
+void Chunk::forAll( std::function<void( Model3D* )> f )
|
|
|
{
|
|
|
- added = 1;
|
|
|
+ for( Block* b : blocks )
|
|
|
+ f( b );
|
|
|
}
|