Просмотр исходного кода

change the way chunks are transfered via network

Kolja Strohm 3 лет назад
Родитель
Сommit
539007de2c
1 измененных файлов с 24 добавлено и 4 удалено
  1. 24 4
      FactoryCraft/Chunk.cpp

+ 24 - 4
FactoryCraft/Chunk.cpp

@@ -242,6 +242,7 @@ void Chunk::setNeighbor( Direction dir, Chunk* zChunk )
 
 void Chunk::load( Framework::StreamReader* zReader )
 {
+    // TODO: adjust this to new way to save chunks
     zReader->lese( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
     for( int x = 0; x < CHUNK_SIZE; x++ )
     {
@@ -263,7 +264,8 @@ void Chunk::load( Framework::StreamReader* zReader )
 
 void Chunk::save( Framework::StreamWriter* zWriter )
 {
-    zWriter->schreibe( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
+    // TODO: check if target is client or file
+    // if client then only transfer visible blocks
     for( int x = 0; x < CHUNK_SIZE; x++ )
     {
         for( int y = 0; y < CHUNK_SIZE; y++ )
@@ -271,12 +273,30 @@ void Chunk::save( Framework::StreamWriter* zWriter )
             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<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );
+                unsigned short blockType = blocks[ index ] ? (unsigned short)blocks[ index ]->zBlockType()->getId() : blockIds[ index ];
+                if( blockType )
+                {
+                    zWriter->schreibe( (char*)&blockType, 2 );
+                    zWriter->schreibe( (char*)&x, 4 );
+                    zWriter->schreibe( (char*)&y, 4 );
+                    zWriter->schreibe( (char*)&z, 4 );
+                    if( blocks[ index ] )
+                    {
+                        bool d = 1;
+                        zWriter->schreibe( (char*)&d, 1 );
+                        StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );
+                    }
+                    else
+                    {
+                        bool d = 0;
+                        zWriter->schreibe( (char*)&d, 1 );
+                    }
+                }
             }
         }
     }
+    unsigned short end = 0;
+    zWriter->schreibe( (char*)&end, 2 );
 }
 
 void Chunk::removeUnusedBlocks()