Chunk.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "Chunk.h"
  2. #include "Constants.h"
  3. #include "Block.h"
  4. #include "Globals.h"
  5. Chunk::Chunk( Framework::Punkt location, int dimensionId )
  6. : ReferenceCounter(),
  7. dimensionId( dimensionId ),
  8. location( location )
  9. {
  10. blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
  11. Block *val = (Block *)AIR_BLOCK;
  12. std::uninitialized_fill_n( blocks, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT, val );
  13. zNeighbours[ 0 ] = 0;
  14. zNeighbours[ 1 ] = 0;
  15. zNeighbours[ 2 ] = 0;
  16. zNeighbours[ 3 ] = 0;
  17. }
  18. Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader *zReader )
  19. : Chunk( location, dimensionId )
  20. {
  21. load( zReader );
  22. }
  23. Chunk::~Chunk()
  24. {
  25. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  26. {
  27. if( IS_BLOCK( blocks[ i ] ) )
  28. blocks[ i ]->release();
  29. }
  30. delete[] blocks;
  31. }
  32. Block *Chunk::zBlockNeighbor( Framework::Vec3<int> location )
  33. {
  34. if( location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT )
  35. return blocks[ (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z ];
  36. if( location.z >= 0 && location.z < WORLD_HEIGHT )
  37. return currentGame->zBlockAt( { location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId );
  38. return 0;
  39. }
  40. bool Chunk::updateVisibility()
  41. {
  42. bool update = false;
  43. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  44. {
  45. if( IS_BLOCK( blocks[ i ] ) )
  46. update |= blocks[ i ]->updateVisibility();
  47. }
  48. return update;
  49. }
  50. Block *Chunk::getBlockAt( Framework::Vec3<int> location ) const
  51. {
  52. Block *result = zBlockAt( location );
  53. if( result )
  54. return dynamic_cast<Block *>(result->getThis());
  55. return 0;
  56. }
  57. Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
  58. {
  59. return blocks[ (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z ];
  60. }
  61. void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
  62. {
  63. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  64. assert( index < CHUNK_SIZE *CHUNK_SIZE *WORLD_HEIGHT );
  65. Block *old = blocks[ index ];
  66. blocks[ index ] = block;
  67. Block *neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
  68. if( IS_BLOCK( neighbor ) )
  69. neighbor->setNeighbour( SOUTH, block );
  70. if( IS_BLOCK( block ) )
  71. block->setNeighbour( NORTH, neighbor );
  72. neighbor = zBlockNeighbor( location + getDirection( EAST ) );
  73. if( IS_BLOCK( neighbor ) )
  74. neighbor->setNeighbour( WEST, block );
  75. if( IS_BLOCK( block ) )
  76. block->setNeighbour( EAST, neighbor );
  77. neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
  78. if( IS_BLOCK( neighbor ) )
  79. neighbor->setNeighbour( NORTH, block );
  80. if( IS_BLOCK( block ) )
  81. block->setNeighbour( SOUTH, neighbor );
  82. neighbor = zBlockNeighbor( location + getDirection( WEST ) );
  83. if( IS_BLOCK( neighbor ) )
  84. neighbor->setNeighbour( EAST, block );
  85. if( IS_BLOCK( block ) )
  86. block->setNeighbour( WEST, neighbor );
  87. neighbor = zBlockNeighbor( location + getDirection( TOP ) );
  88. if( IS_BLOCK( neighbor ) )
  89. neighbor->setNeighbour( BOTTOM, block );
  90. if( IS_BLOCK( block ) )
  91. block->setNeighbour( TOP, neighbor );
  92. neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
  93. if( IS_BLOCK( neighbor ) )
  94. neighbor->setNeighbour( TOP, block );
  95. if( IS_BLOCK( block ) )
  96. block->setNeighbour( BOTTOM, neighbor );
  97. if( IS_BLOCK( old ) )
  98. old->release();
  99. }
  100. void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
  101. {
  102. zNeighbours[ getDirectionIndex( dir ) ] = zChunk;
  103. for( int i = 0; i < CHUNK_SIZE; i++ )
  104. {
  105. for( int z = 0; z < WORLD_HEIGHT; z++ )
  106. {
  107. if( dir == NORTH )
  108. {
  109. int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  110. if( IS_BLOCK( blocks[ index ] ) )
  111. blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z ] );
  112. }
  113. else if( dir == EAST )
  114. {
  115. int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  116. if( IS_BLOCK( blocks[ index ] ) )
  117. blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * WORLD_HEIGHT + z ] );
  118. }
  119. else if( dir == SOUTH )
  120. {
  121. int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  122. if( IS_BLOCK( blocks[ index ] ) )
  123. blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * WORLD_HEIGHT + z ] );
  124. }
  125. else if( dir == WEST )
  126. {
  127. int index = i * WORLD_HEIGHT + z;
  128. if( IS_BLOCK( blocks[ index ] ) )
  129. blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z ] );
  130. }
  131. }
  132. }
  133. }
  134. void Chunk::load( Framework::StreamReader *zReader )
  135. {
  136. for( int x = 0; x < CHUNK_SIZE; x++ )
  137. {
  138. for( int y = 0; y < CHUNK_SIZE; y++ )
  139. {
  140. for( int z = 0; z < WORLD_HEIGHT; z++ )
  141. {
  142. int blockType;
  143. zReader->lese( (char *)&blockType, 4 );
  144. if( blockType >= 0 )
  145. {
  146. Block *block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zReader );
  147. putBlockAt( { x, y, z }, block );
  148. }
  149. }
  150. }
  151. }
  152. }
  153. int Chunk::getDimensionId() const
  154. {
  155. return dimensionId;
  156. }
  157. Framework::Punkt Chunk::getCenter() const
  158. {
  159. return location;
  160. }
  161. Framework::Vec3<int> Chunk::getMin() const
  162. {
  163. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  164. }
  165. Framework::Vec3<int> Chunk::getMax() const
  166. {
  167. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  168. }