Explorar o código

fix some issues

Kolja Strohm %!s(int64=3) %!d(string=hai) anos
pai
achega
44d960c781

+ 29 - 13
FactoryCraft/Chunk.cpp

@@ -34,6 +34,15 @@ Chunk::~Chunk()
     delete[] blocks;
     delete[] blocks;
 }
 }
 
 
+Block *Chunk::zBlockNeighbor( Framework::Vec3<int> location )
+{
+    if( location.x >= 0 && location.x < CHUNK_SIZE && location.y <= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT )
+        return blocks[ (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z ];
+    if( location.z >= 0 && location.z < WORLD_HEIGHT )
+        return currentGame->zBlockAt( { location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId );
+    return 0;
+}
+
 bool Chunk::updateVisibility()
 bool Chunk::updateVisibility()
 {
 {
     bool update = false;
     bool update = false;
@@ -60,33 +69,40 @@ Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
 
 
 void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
 void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
 {
 {
-    int index = (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z;
+    int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    assert( index < CHUNK_SIZE *CHUNK_SIZE *WORLD_HEIGHT );
     Block *old = blocks[ index ];
     Block *old = blocks[ index ];
     blocks[ index ] = block;
     blocks[ index ] = block;
-    Block *neighbor = currentGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
+    Block *neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( SOUTH, block );
         neighbor->setNeighbour( SOUTH, block );
-    block->setNeighbour( NORTH, neighbor );
-    neighbor = currentGame->zBlockAt( location + getDirection( EAST ), dimensionId );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( NORTH, neighbor );
+    neighbor = zBlockNeighbor( location + getDirection( EAST ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( WEST, block );
         neighbor->setNeighbour( WEST, block );
-    block->setNeighbour( EAST, neighbor );
-    neighbor = currentGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( EAST, neighbor );
+    neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( NORTH, block );
         neighbor->setNeighbour( NORTH, block );
-    block->setNeighbour( SOUTH, neighbor );
-    neighbor = currentGame->zBlockAt( location + getDirection( WEST ), dimensionId );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( SOUTH, neighbor );
+    neighbor = zBlockNeighbor( location + getDirection( WEST ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( EAST, block );
         neighbor->setNeighbour( EAST, block );
-    block->setNeighbour( WEST, neighbor );
-    neighbor = currentGame->zBlockAt( location + getDirection( TOP ), dimensionId );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( WEST, neighbor );
+    neighbor = zBlockNeighbor( location + getDirection( TOP ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( BOTTOM, block );
         neighbor->setNeighbour( BOTTOM, block );
-    block->setNeighbour( TOP, neighbor );
-    neighbor = currentGame->zBlockAt( location + getDirection( BOTTOM ), dimensionId );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( TOP, neighbor );
+    neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
     if( IS_BLOCK( neighbor ) )
     if( IS_BLOCK( neighbor ) )
         neighbor->setNeighbour( TOP, block );
         neighbor->setNeighbour( TOP, block );
-    block->setNeighbour( BOTTOM, neighbor );
+    if( IS_BLOCK( block ) )
+        block->setNeighbour( BOTTOM, neighbor );
     if( IS_BLOCK( old ) )
     if( IS_BLOCK( old ) )
         old->release();
         old->release();
 }
 }

+ 1 - 0
FactoryCraft/Chunk.h

@@ -15,6 +15,7 @@ private:
     Framework::Punkt location;
     Framework::Punkt location;
     Block **blocks;
     Block **blocks;
     Chunk *zNeighbours[ 4 ];
     Chunk *zNeighbours[ 4 ];
+    Block *zBlockNeighbor( Framework::Vec3<int> location );
 
 
 public:
 public:
     Chunk( Framework::Punkt location, int dimensionId );
     Chunk( Framework::Punkt location, int dimensionId );

+ 1 - 1
FactoryCraft/Dimension.cpp

@@ -94,7 +94,7 @@ void Dimension::addChunk( Chunk *chunk )
         zChunk = chunks->z( addr );
         zChunk = chunks->z( addr );
         if( zChunk )
         if( zChunk )
         {
         {
-            zChunk->setNeighbor( SOUTH, chunk ); // TODO: correct this in setBlock
+            zChunk->setNeighbor( SOUTH, chunk );
             chunk->setNeighbor( NORTH, chunk );
             chunk->setNeighbor( NORTH, chunk );
         }
         }
     }
     }

+ 1 - 1
FactoryCraft/Inventory.h

@@ -21,6 +21,6 @@ protected:
     virtual void loadInventory( Framework::StreamReader *zReader );
     virtual void loadInventory( Framework::StreamReader *zReader );
 
 
 public:
 public:
-    Inventory( const Framework::Vec3<float> location );
+    Inventory( const Framework::Vec3<float> location, bool hasInventory );
     virtual ~Inventory();
     virtual ~Inventory();
 };
 };

+ 26 - 13
FactoryCraft/Inventoty.cpp

@@ -4,32 +4,45 @@
 using namespace Framework;
 using namespace Framework;
 
 
 
 
-Inventory::Inventory( const Framework::Vec3<float> location )
+Inventory::Inventory( const Framework::Vec3<float> location, bool hasInventory )
     : ReferenceCounter(),
     : ReferenceCounter(),
     location( location )
     location( location )
 {
 {
-    pullSlotsOrder = new Framework::RCArray<ItemSlot>();
-    pushSlotsOrder = new Framework::RCArray<ItemSlot>();
+    if( hasInventory )
+    {
+        pullSlotsOrder = new Framework::RCArray<ItemSlot>();
+        pushSlotsOrder = new Framework::RCArray<ItemSlot>();
+    }
+    else
+    {
+        pullSlotsOrder = 0;
+        pushSlotsOrder = 0;
+    }
 }
 }
 
 
 Inventory::~Inventory()
 Inventory::~Inventory()
 {
 {
-    pullSlotsOrder->release();
-    pushSlotsOrder->release();
+    if( pullSlotsOrder )
+        pullSlotsOrder->release();
+    if( pushSlotsOrder )
+        pushSlotsOrder->release();
 }
 }
 
 
 void Inventory::loadInventory( Framework::StreamReader *zReader )
 void Inventory::loadInventory( Framework::StreamReader *zReader )
 {
 {
-    for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
+    if( pushSlotsOrder )
     {
     {
-        int size = 0;
-        zReader->lese( (char *)&size, 4 );
-        if( size != 0 )
+        for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
         {
         {
-            int id = 0;
-            zReader->lese( (char *)&id, 4 );
-            Item *item = StaticRegistry<ItemType>::INSTANCE.zElement( id )->loadItem( zReader );
-            iterator->setItems( new ItemStack( item, size ) );
+            int size = 0;
+            zReader->lese( (char *)&size, 4 );
+            if( size != 0 )
+            {
+                int id = 0;
+                zReader->lese( (char *)&id, 4 );
+                Item *item = StaticRegistry<ItemType>::INSTANCE.zElement( id )->loadItem( zReader );
+                iterator->setItems( new ItemStack( item, size ) );
+            }
         }
         }
     }
     }
 }
 }