Browse Source

corrected block neighbour system

Kolja Strohm 3 years ago
parent
commit
0587311ef1
5 changed files with 60 additions and 43 deletions
  1. 17 0
      FactoryCraft/Area.cpp
  2. 2 1
      FactoryCraft/Area.h
  3. 8 0
      FactoryCraft/Block.cpp
  4. 6 3
      FactoryCraft/Block.h
  5. 27 39
      FactoryCraft/Chunk.cpp

+ 17 - 0
FactoryCraft/Area.cpp

@@ -55,4 +55,21 @@ Framework::Vec3<int> getDirection( Directions dir )
     if( ( dir | BOTTOM ) == dir )
         --result.z;
     return result;
+}
+
+int getDirectionIndex( Direction dir )
+{
+    if( dir == NORTH )
+        return 0;
+    if( dir == EAST )
+        return 1;
+    if( dir == SOUTH )
+        return 2;
+    if( dir == WEST )
+        return 3;
+    if( dir == TOP )
+        return 4;
+    if( dir == BOTTOM )
+        return 5;
+    return -1;
 }

+ 2 - 1
FactoryCraft/Area.h

@@ -25,4 +25,5 @@ typedef int Directions;
 
 Direction opposite( Direction dir );
 Directions getDirections( Framework::Vec3<float> currentPos, Framework::Vec3<float> otherPos );
-Framework::Vec3<int> getDirection( Directions dir );
+Framework::Vec3<int> getDirection( Directions dir );
+int getDirectionIndex( Direction dir );

+ 8 - 0
FactoryCraft/Block.cpp

@@ -22,6 +22,9 @@ Block::Block( const BlockType *zType, ItemType *zTool, Framework::Vec3<int> pos
     dimansionId = 0;
 }
 
+Block::~Block()
+{}
+
 void Block::tick( TickQueue *zQueue )
 {
     if( wasTicked )
@@ -61,6 +64,11 @@ void Block::postTick()
     }
 }
 
+void Block::setNeighbour( Direction dir, Block *zN )
+{
+    zNeighbours[ getDirectionIndex( dir ) ] = zN;
+}
+
 void Block::setDimensionId( int id )
 {
     dimansionId = id;

+ 6 - 3
FactoryCraft/Block.h

@@ -9,13 +9,15 @@
 #include <Trie.h>
 #include <Vec3.h>
 
-class BlockType;
 class ItemType;
 class Chunk;
 class BasicBlockItemType;
 
 class TickQueue;
 
+#define AIR_BLOCK -1
+#define IS_BLOCK(b) b > 0
+
 class Block : public Inventory
 {
 private:
@@ -34,7 +36,7 @@ protected:
     const BlockType *zType;
     ItemType *zTool;
     float speedModifier;
-    Block *neighbours[ 6 ];
+    Block *zNeighbours[ 6 ];
 
     int minTickTimeout;
     int maxTickTimeout;
@@ -56,10 +58,12 @@ protected:
 
 public:
     Block( const BlockType *zType, ItemType *zTool, Framework::Vec3<int> pos );
+    virtual ~Block();
 
     void tick( TickQueue *zQueue );
     void postTick();
     void setDimensionId( int id );
+    virtual void setNeighbour( Direction dir, Block *zN );
 
     void api( Framework::StreamReader *zRequest, NetworkResponse *zResponse );
 
@@ -75,7 +79,6 @@ public:
     const Framework::Vec3<int> getPos() const;
     int getDimensionId() const;
 
-    friend Chunk;
     friend BlockType;
 };
 

+ 27 - 39
FactoryCraft/Chunk.cpp

@@ -10,7 +10,7 @@ Chunk::Chunk( Framework::Punkt location, Game *zGame, int dimensionId )
     location( location )
 {
     blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
-    memset( blocks, 0, sizeof( Block * ) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT );
+    memset( blocks, AIR_BLOCK, sizeof( Block * ) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT );
     zNeighbours[ 0 ] = 0;
     zNeighbours[ 1 ] = 0;
     zNeighbours[ 2 ] = 0;
@@ -27,7 +27,7 @@ Chunk::~Chunk()
 {
     for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
     {
-        if( blocks[ i ] )
+        if( IS_BLOCK( blocks[ i ] ) )
             blocks[ i ]->release();
     }
     delete[] blocks;
@@ -61,42 +61,30 @@ void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
     Block *old = blocks[ index ];
     blocks[ index ] = block;
     Block *neighbor = zGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
-    if( neighbor )
-    {
-        neighbor->neighbours[ SOUTH ] = block;
-        block->neighbours[ NORTH ] = neighbor;
-    }
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( SOUTH, block );
+    block->setNeighbour( NORTH, neighbor );
     neighbor = zGame->zBlockAt( location + getDirection( EAST ), dimensionId );
-    if( neighbor )
-    {
-        neighbor->neighbours[ WEST ] = block;
-        block->neighbours[ EAST ] = neighbor;
-    }
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( WEST, block );
+    block->setNeighbour( EAST, neighbor );
     neighbor = zGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
-    if( neighbor ) {}
-    {
-        neighbor->neighbours[ NORTH ] = block;
-        block->neighbours[ SOUTH ] = neighbor;
-    }
+    if( IS_BLOCK( neighbor ) ) {}
+    neighbor->setNeighbour( NORTH, block );
+    block->setNeighbour( SOUTH, neighbor );
     neighbor = zGame->zBlockAt( location + getDirection( WEST ), dimensionId );
-    if( neighbor )
-    {
-        neighbor->neighbours[ EAST ] = block;
-        block->neighbours[ WEST ] = neighbor;
-    }
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( EAST, block );
+    block->setNeighbour( WEST, neighbor );
     neighbor = zGame->zBlockAt( location + getDirection( TOP ), dimensionId );
-    if( neighbor )
-    {
-        neighbor->neighbours[ BOTTOM ] = block;
-        block->neighbours[ TOP ] = neighbor;
-    }
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( BOTTOM, block );
+    block->setNeighbour( TOP, neighbor );
     neighbor = zGame->zBlockAt( location + getDirection( BOTTOM ), dimensionId );
-    if( neighbor )
-    {
-        neighbor->neighbours[ TOP ] = block;
-        block->neighbours[ BOTTOM ] = neighbor;
-    }
-    if( old )
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( TOP, block );
+    block->setNeighbour( BOTTOM, neighbor );
+    if( IS_BLOCK( old ) )
         old->release();
 }
 
@@ -111,25 +99,25 @@ void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
             {
                 int index = i * CHUNK_SIZE * CHUNK_SIZE + z;
                 if( blocks[ index ] )
-                    blocks[ index ]->neighbours[ NORTH ] = zChunk->blocks[ ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z ];
+                    blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z ] );
             }
             else if( dir == EAST )
             {
                 int index = ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z;
                 if( blocks[ index ] )
-                    blocks[ index ]->neighbours[ EAST ] = zChunk->blocks[ i * CHUNK_SIZE + z ];
+                    blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * CHUNK_SIZE + z ] );
             }
             else if( dir == SOUTH )
             {
                 int index = ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z;
                 if( blocks[ index ] )
-                    blocks[ index ]->neighbours[ SOUTH ] = zChunk->blocks[ i * CHUNK_SIZE * CHUNK_SIZE + z ];
+                    blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * CHUNK_SIZE + z ] );
             }
             else if( dir == WEST )
             {
                 int index = i * CHUNK_SIZE + z;
                 if( blocks[ index ] )
-                    blocks[ index ]->neighbours[ WEST ] = zChunk->blocks[ ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z ];
+                    blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z ] );
             }
         }
     }
@@ -148,7 +136,7 @@ void Chunk::load( Framework::StreamReader *zReader )
                 if( blockType >= 0 )
                 {
                     Block *block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zGame, zReader );
-                    blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ] = block;
+                    putBlockAt( { x, y, z }, block );
                 }
             }
         }
@@ -164,7 +152,7 @@ void Chunk::save( Framework::StreamWriter *zWriter )
             for( int z = 0; z < WORLD_HEIGHT; z++ )
             {
                 int index = ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z;
-                int blockType = blocks[ index ] ? blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ]->zType->getId() : -1;
+                int blockType = blocks[ index ] ? blocks[ ( x * CHUNK_SIZE + y ) * CHUNK_SIZE + z ]->zBlockType()->getId() : -1;
                 zWriter->schreibe( (char *)&blockType, 4 );
                 if( blockType >= 0 )
                     StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );