123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #include "Dimension.h"
- #include "Constants.h"
- #include "Datei.h"
- #include "Game.h"
- using namespace Framework;
- Dimension::Dimension( int id )
- : dimensionId( id ),
- chunks( new Trie<Chunk>() ),
- entities( new RCArray<Entity>() )
- {}
- Dimension::~Dimension()
- {
- entities->release();
- chunks->release();
- }
- void Dimension::api( Framework::StreamReader *zRequest, NetworkResponse *zResponse )
- {
- // TODO: switch type chunck, block, entity
- }
- void Dimension::tickEntities( Game *zGame )
- {
- int index = 0;
- Array<int> removed;
- for( auto entity = entities->getIterator(); entity; entity++, index++ )
- {
- if( zChunk( Punkt( (int)entity->getPosition().x, (int)entity->getPosition().y ) ) )
- entity->tick( this, zGame );
- if( entity->isRemoved() )
- removed.add( index, 0 );
- }
- for( auto i = removed.getIterator(); i; i++ )
- entities->remove( i );
- }
- void Dimension::getAddrOf( Punkt cPos, char *addr ) const
- {
- *(int *)addr = cPos.x;
- *( (int *)addr + 1 ) = cPos.y;
- addr[ 8 ] = 0;
- }
- void Dimension::getAddrOfWorld( Punkt wPos, char *addr ) const
- {
- if( wPos.x < 0 )
- wPos.x -= CHUNK_SIZE;
- if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
- wPos.y -= CHUNK_SIZE;
- wPos /= CHUNK_SIZE;
- getAddrOf( wPos, addr );
- }
- Chunk *Dimension::zChunk( Punkt wPos ) const
- {
- char addr[ 9 ];
- getAddrOfWorld( wPos, addr );
- return chunks->z( addr, 9 );
- }
- Block *Dimension::zBlock( Vec3<int> location, const Game *zGame )
- {
- Chunk *c = zChunk( zGame->getChunkCenter( location.x, location.y ) );
- if( c )
- {
- int x = location.x % CHUNK_SIZE;
- int y = location.y % CHUNK_SIZE;
- if( x < 0 )
- x += CHUNK_SIZE;
- if( y < 0 )
- y += CHUNK_SIZE;
- return c->zBlockAt( Vec3<int>( x, y, location.z ) );
- }
- return 0;
- }
- void Dimension::addEntity( Entity *entity )
- {
- entities->add( entity );
- }
- void Dimension::addChunk( Chunk *chunk )
- {
- char addr[ 9 ];
- getAddrOfWorld( chunk->getCenter(), addr );
- if( !chunks->z( addr, 9 ) )
- {
- chunks->set( addr, 9, chunk );
- getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
- Chunk *zChunk = chunks->z( addr, 9 );
- if( zChunk )
- {
- zChunk->setNeighbor( WEST, chunk );
- chunk->setNeighbor( EAST, chunk );
- }
- getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
- zChunk = chunks->z( addr, 9 );
- if( zChunk )
- {
- zChunk->setNeighbor( EAST, chunk );
- chunk->setNeighbor( WEST, chunk );
- }
- getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
- zChunk = chunks->z( addr, 9 );
- if( zChunk )
- {
- zChunk->setNeighbor( NORTH, chunk );
- chunk->setNeighbor( SOUTH, chunk );
- }
- getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
- zChunk = chunks->z( addr, 9 );
- if( zChunk )
- {
- zChunk->setNeighbor( SOUTH, chunk );
- chunk->setNeighbor( NORTH, chunk );
- }
- }
- else
- chunk->release();
- }
- void Dimension::save( Text worldDir ) const
- {
- for( auto chunk = chunks->getIterator(); chunk; chunk++ )
- {
- if( !chunk._ )
- continue;
- Datei *file = new Datei();
- Text filePath = worldDir + "/dim/" + dimensionId + "/";
- filePath.appendHex( chunk->getCenter().x );
- filePath += "_";
- filePath.appendHex( chunk->getCenter().y );
- filePath += ".chunk";
- file->setDatei( filePath );
- if( file->open( Datei::Style::schreiben ) )
- chunk->save( file );
- file->close();
- file->release();
- }
- Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
- Datei *file = new Datei();
- file->setDatei( filePath );
- if( file->open( Datei::Style::schreiben ) )
- {
- for( auto entity = entities->getIterator(); entity; entity++ )
- {
- if( entity->zType()->getId() != PlayerEntityType::ID )
- {
- if( !entity->isRemoved() )
- {
- int type = entity->zType()->getId();
- file->schreibe( (char *)&type, 4 );
- StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, file );
- }
- }
- else
- {
- Datei pFile;
- pFile.setDatei( worldDir + "/player/" + ( (Player *)entity.val() )->getName() );
- if( pFile.open( Datei::Style::schreiben ) )
- PlayerEntityType::INSTANCE->saveEntity( entity, &pFile );
- }
- }
- file->close();
- }
- }
- int Dimension::getDimensionId() const
- {
- return dimensionId;
- }
- bool Dimension::hasChunck( int x, int y ) const
- {
- return zChunk( Punkt( x, y ) );
- }
|