#include "Chunk.h" #include "Constants.h" #include "Game.h" #include "NoBlock.h" Chunk::Chunk( Framework::Punkt location, Game* zGame, int dimensionId ) : ReferenceCounter(), zGame( zGame ), dimensionId( dimensionId ), location( location ) { 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; } Chunk::Chunk( Framework::Punkt location, Game* zGame, int dimensionId, Framework::StreamReader* zReader ) : Chunk( location, zGame, dimensionId ) { load( zReader ); } 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 Chunk::zBlockNeighbor( Framework::Vec3 location ) { if( location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT ) { 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 zGame->zBlockAt( { location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId ); return 0; } void Chunk::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse ) { // TODO: answer api messages } Framework::Either Chunk::zBlockAt( Framework::Vec3 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 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 StaticRegistry::INSTANCE.zElement( blockIds[ index ] )->zDefault(); return 0; } void Chunk::instantiateBlock( Framework::Vec3 location ) { if( zBlockAt( location ) ) return; int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z; if( !blockIds[ index ] ) generateBlock( location ); if( !zBlockAt( location ) ) putBlockAt( location, StaticRegistry::INSTANCE.zElement( blockIds[ index ] )->createBlockAt( location, zGame, 0 ) ); } void Chunk::generateBlock( Framework::Vec3 location ) { int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z; if( blockIds[ index ] ) return; auto generated = zGame->zGenerator()->generateSingleBlock( location, dimensionId ); if( generated.isA() ) putBlockAt( location, generated ); else putBlockTypeAt( location, generated ); } void Chunk::putBlockAt( Framework::Vec3 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; Block* neighbor = zBlockNeighbor( location + getDirection( NORTH ) ); if( neighbor ) neighbor->setNeighbour( SOUTH, block ); if( block ) block->setNeighbour( NORTH, neighbor ); neighbor = zBlockNeighbor( location + getDirection( EAST ) ); if( neighbor ) neighbor->setNeighbour( WEST, block ); if( block ) block->setNeighbour( EAST, neighbor ); neighbor = zBlockNeighbor( location + getDirection( SOUTH ) ); if( neighbor ) neighbor->setNeighbour( NORTH, block ); if( block ) block->setNeighbour( SOUTH, neighbor ); neighbor = zBlockNeighbor( location + getDirection( WEST ) ); if( neighbor ) neighbor->setNeighbour( EAST, block ); if( block ) block->setNeighbour( WEST, neighbor ); neighbor = zBlockNeighbor( location + getDirection( TOP ) ); if( neighbor ) neighbor->setNeighbour( BOTTOM, block ); if( block ) block->setNeighbour( TOP, neighbor ); neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) ); if( neighbor ) neighbor->setNeighbour( TOP, block ); if( block ) block->setNeighbour( BOTTOM, neighbor ); if( old ) old->release(); } void Chunk::putBlockTypeAt( Framework::Vec3 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; Block* neighbor = zBlockNeighbor( location + getDirection( NORTH ) ); if( neighbor ) neighbor->setNeighbourType( SOUTH, type ); neighbor = zBlockNeighbor( location + getDirection( EAST ) ); if( neighbor ) neighbor->setNeighbourType( WEST, type ); neighbor = zBlockNeighbor( location + getDirection( SOUTH ) ); if( neighbor ) neighbor->setNeighbourType( NORTH, type ); neighbor = zBlockNeighbor( location + getDirection( WEST ) ); if( neighbor ) neighbor->setNeighbourType( EAST, type ); neighbor = zBlockNeighbor( location + getDirection( TOP ) ); if( neighbor ) neighbor->setNeighbourType( BOTTOM, type ); neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) ); if( neighbor ) 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( 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->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( 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( 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( blocks[ index ] ) blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z ] ); } } } } 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++ ) { unsigned short blockType; zReader->lese( (char*)&blockType, 2 ); Block* block = StaticRegistry::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3( x, y, z ), zGame, zReader ); if( block ) putBlockAt( { x, y, z }, block ); else putBlockTypeAt( { x, y, z }, blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] ); } } } } void Chunk::save( Framework::StreamWriter* zWriter ) { zWriter->schreibe( (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 index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z; unsigned short blockType = blocks[ index ] ? (unsigned short)blocks[ index ]->zBlockType()->getId() : (unsigned short)NoBlockBlockType::ID; zWriter->schreibe( (char*)&blockType, 2 ); StaticRegistry::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter ); } } } } void Chunk::removeUnusedBlocks() { bool removed = true; while( removed ) { removed = false; for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ ) { if( blocks[ i ] && !blocks[ i ]->isVisible() ) { int x = (i / WORLD_HEIGHT) / CHUNK_SIZE; int y = (i / WORLD_HEIGHT) % CHUNK_SIZE; int z = i % WORLD_HEIGHT; putBlockAt( { x,y,z }, 0 ); putBlockTypeAt( { x, y, z }, NoBlockBlockType::ID ); removed = true; } } } } int Chunk::getDimensionId() const { return dimensionId; } Framework::Punkt Chunk::getCenter() const { return location; } Framework::Vec3 Chunk::getMin() const { return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 }; } Framework::Vec3 Chunk::getMax() const { return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT }; } Game* Chunk::zGameObj() const { return zGame; }