Chunk.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. bool Chunk::updateVisibility()
  33. {
  34. bool update = false;
  35. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  36. {
  37. if( IS_BLOCK( blocks[ i ] ) )
  38. update |= blocks[ i ]->updateVisibility();
  39. }
  40. return update;
  41. }
  42. Block *Chunk::getBlockAt( Framework::Vec3<int> location ) const
  43. {
  44. Block *result = zBlockAt( location );
  45. if( result )
  46. return dynamic_cast<Block *>(result->getThis());
  47. return 0;
  48. }
  49. Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
  50. {
  51. return blocks[ (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z ];
  52. }
  53. void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
  54. {
  55. int index = (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z;
  56. Block *old = blocks[ index ];
  57. blocks[ index ] = block;
  58. Block *neighbor = currentGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
  59. if( IS_BLOCK( neighbor ) )
  60. neighbor->setNeighbour( SOUTH, block );
  61. block->setNeighbour( NORTH, neighbor );
  62. neighbor = currentGame->zBlockAt( location + getDirection( EAST ), dimensionId );
  63. if( IS_BLOCK( neighbor ) )
  64. neighbor->setNeighbour( WEST, block );
  65. block->setNeighbour( EAST, neighbor );
  66. neighbor = currentGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
  67. if( IS_BLOCK( neighbor ) )
  68. neighbor->setNeighbour( NORTH, block );
  69. block->setNeighbour( SOUTH, neighbor );
  70. neighbor = currentGame->zBlockAt( location + getDirection( WEST ), dimensionId );
  71. if( IS_BLOCK( neighbor ) )
  72. neighbor->setNeighbour( EAST, block );
  73. block->setNeighbour( WEST, neighbor );
  74. neighbor = currentGame->zBlockAt( location + getDirection( TOP ), dimensionId );
  75. if( IS_BLOCK( neighbor ) )
  76. neighbor->setNeighbour( BOTTOM, block );
  77. block->setNeighbour( TOP, neighbor );
  78. neighbor = currentGame->zBlockAt( location + getDirection( BOTTOM ), dimensionId );
  79. if( IS_BLOCK( neighbor ) )
  80. neighbor->setNeighbour( TOP, block );
  81. block->setNeighbour( BOTTOM, neighbor );
  82. if( IS_BLOCK( old ) )
  83. old->release();
  84. }
  85. void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
  86. {
  87. zNeighbours[ dir ] = zChunk;
  88. for( int i = 0; i < CHUNK_SIZE; i++ )
  89. {
  90. for( int z = 0; z < WORLD_HEIGHT; z++ )
  91. {
  92. if( dir == NORTH )
  93. {
  94. int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  95. if( IS_BLOCK( blocks[ index ] ) )
  96. blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z ] );
  97. }
  98. else if( dir == EAST )
  99. {
  100. int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  101. if( IS_BLOCK( blocks[ index ] ) )
  102. blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * WORLD_HEIGHT + z ] );
  103. }
  104. else if( dir == SOUTH )
  105. {
  106. int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  107. if( IS_BLOCK( blocks[ index ] ) )
  108. blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * WORLD_HEIGHT + z ] );
  109. }
  110. else if( dir == WEST )
  111. {
  112. int index = i * WORLD_HEIGHT + z;
  113. if( IS_BLOCK( blocks[ index ] ) )
  114. blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z ] );
  115. }
  116. }
  117. }
  118. }
  119. void Chunk::load( Framework::StreamReader *zReader )
  120. {
  121. for( int x = 0; x < CHUNK_SIZE; x++ )
  122. {
  123. for( int y = 0; y < CHUNK_SIZE; y++ )
  124. {
  125. for( int z = 0; z < WORLD_HEIGHT; z++ )
  126. {
  127. int blockType;
  128. zReader->lese( (char *)&blockType, 4 );
  129. if( blockType >= 0 )
  130. {
  131. Block *block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zReader );
  132. putBlockAt( { x, y, z }, block );
  133. }
  134. }
  135. }
  136. }
  137. }
  138. int Chunk::getDimensionId() const
  139. {
  140. return dimensionId;
  141. }
  142. Framework::Punkt Chunk::getCenter() const
  143. {
  144. return location;
  145. }
  146. Framework::Vec3<int> Chunk::getMin() const
  147. {
  148. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  149. }
  150. Framework::Vec3<int> Chunk::getMax() const
  151. {
  152. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  153. }