|
@@ -0,0 +1,73 @@
|
|
|
+#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>() )
|
|
|
+{}
|
|
|
+
|
|
|
+Dimension::~Dimension()
|
|
|
+{
|
|
|
+ chunks->release();
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::getAddrOf( Punkt cPos, char *addr )
|
|
|
+{
|
|
|
+ *(int *)addr = cPos.x;
|
|
|
+ *( (int *)addr + 1 ) = cPos.y;
|
|
|
+ addr[ 8 ] = 0;
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::getAddrOfWorld( Punkt wPos, char *addr )
|
|
|
+{
|
|
|
+ wPos.x = (int)floor( ( (float)wPos.x + CHUNK_SIZE / 2 ) / CHUNK_SIZE );
|
|
|
+ wPos.y = (int)floor( ( (float)wPos.y + CHUNK_SIZE / 2 ) / CHUNK_SIZE );
|
|
|
+ getAddrOf( wPos, addr );
|
|
|
+}
|
|
|
+
|
|
|
+Chunk *Dimension::zChunk( Punkt wPos )
|
|
|
+{
|
|
|
+ char addr[ 9 ];
|
|
|
+ getAddrOfWorld( wPos, addr );
|
|
|
+ return chunks->z( addr );
|
|
|
+}
|
|
|
+
|
|
|
+Block *Dimension::zBlock( Vec3<int> location )
|
|
|
+{
|
|
|
+ return zChunk( Punkt( location.x, location.y ) )->zBlockAt( Vec3<int>( ( location.x + CHUNK_SIZE / 2 ) % CHUNK_SIZE, ( location.y + CHUNK_SIZE / 2 ) % CHUNK_SIZE, location.z ) );
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::addChunk( Chunk *chunk )
|
|
|
+{
|
|
|
+ char addr[ 9 ];
|
|
|
+ getAddrOf( chunk->getCenter(), addr );
|
|
|
+ if( !chunks->z( addr ) )
|
|
|
+ chunks->set( addr, chunk );
|
|
|
+ else
|
|
|
+ chunk->release();
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::save( Framework::Writer *zWriter )
|
|
|
+{
|
|
|
+ for( auto chunk = chunks->getIterator(); chunk; chunk++ )
|
|
|
+ {
|
|
|
+ if( !chunk._ )
|
|
|
+ continue;
|
|
|
+ Datei *file = new Datei();
|
|
|
+ Text filePath = chunk->zGameObj()->getWorldDirectory() + "/dim/" + chunk->getDimensionId() + "/";
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+}
|