Chunk.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "Chunk.h"
  2. #include "Constants.h"
  3. #include "Block.h"
  4. #include "Globals.h"
  5. #include "Registries.h"
  6. Chunk::Chunk( Framework::Punkt location, int dimensionId )
  7. : ReferenceCounter(),
  8. dimensionId( dimensionId ),
  9. location( location ),
  10. added( 0 )
  11. {
  12. blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
  13. blockIds = new unsigned short[ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
  14. memset( blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( Block* ) );
  15. memset( blockIds, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
  16. zNeighbours[ 0 ] = 0;
  17. zNeighbours[ 1 ] = 0;
  18. zNeighbours[ 2 ] = 0;
  19. zNeighbours[ 3 ] = 0;
  20. }
  21. Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader )
  22. : Chunk( location, dimensionId )
  23. {
  24. load( zReader );
  25. }
  26. Chunk::~Chunk()
  27. {
  28. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  29. {
  30. if( blocks[ i ] )
  31. blocks[ i ]->release();
  32. }
  33. delete[] blocks;
  34. delete[] blockIds;
  35. }
  36. Framework::Either<Block*, int> Chunk::zBlockNeighbor( Framework::Vec3<int> location )
  37. {
  38. if( location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT )
  39. {
  40. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  41. if( blocks[ index ] )
  42. return blocks[ index ];
  43. else
  44. return (int)blockIds[ index ];
  45. }
  46. if( added && location.z >= 0 && location.z < WORLD_HEIGHT )
  47. return currentGame->zBlockAt( { location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId );
  48. return 0;
  49. }
  50. bool Chunk::updateVisibility()
  51. {
  52. bool update = false;
  53. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  54. {
  55. if( blocks[ i ] )
  56. update |= blocks[ i ]->updateVisibility();
  57. }
  58. return update;
  59. }
  60. Framework::Either<Block*, int> Chunk::zBlockAt( Framework::Vec3<int> location ) const
  61. {
  62. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  63. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  64. if( blocks[ index ] )
  65. return blocks[ index ];
  66. else
  67. return (int)blockIds[ index ];
  68. }
  69. const Block* Chunk::zBlockConst( Framework::Vec3<int> location ) const
  70. {
  71. Block* b = zBlockAt( location );
  72. if( b )
  73. return b;
  74. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  75. if( blockIds[ index ] )
  76. return STATIC_REGISTRY( BlockType ).zElement( blockIds[ index ] )->zDefault();
  77. return 0;
  78. }
  79. void Chunk::putBlockAt( Framework::Vec3<int> location, Block* block )
  80. {
  81. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  82. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  83. Block* old = blocks[ index ];
  84. if( block )
  85. blockIds[ index ] = (unsigned short)block->zBlockType()->getId();
  86. blocks[ index ] = block;
  87. Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
  88. if( neighbor.isA() )
  89. ((Block*)neighbor)->setNeighbour( SOUTH, block );
  90. if( block )
  91. block->setNeighbour( NORTH, neighbor );
  92. neighbor = zBlockNeighbor( location + getDirection( EAST ) );
  93. if( neighbor.isA() )
  94. ((Block*)neighbor)->setNeighbour( WEST, block );
  95. if( block )
  96. block->setNeighbour( EAST, neighbor );
  97. neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
  98. if( neighbor.isA() )
  99. ((Block*)neighbor)->setNeighbour( NORTH, block );
  100. if( block )
  101. block->setNeighbour( SOUTH, neighbor );
  102. neighbor = zBlockNeighbor( location + getDirection( WEST ) );
  103. if( neighbor.isA() )
  104. ((Block*)neighbor)->setNeighbour( EAST, block );
  105. if( block )
  106. block->setNeighbour( WEST, neighbor );
  107. neighbor = zBlockNeighbor( location + getDirection( TOP ) );
  108. if( neighbor.isA() )
  109. ((Block*)neighbor)->setNeighbour( BOTTOM, block );
  110. if( block )
  111. block->setNeighbour( TOP, neighbor );
  112. neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
  113. if( neighbor.isA() )
  114. ((Block*)neighbor)->setNeighbour( TOP, block );
  115. if( block )
  116. block->setNeighbour( BOTTOM, neighbor );
  117. if( old )
  118. old->release();
  119. }
  120. void Chunk::putBlockTypeAt( Framework::Vec3<int> location, int type )
  121. {
  122. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  123. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  124. blockIds[ index ] = (unsigned short)type;
  125. Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
  126. if( neighbor.isA() )
  127. ((Block*)neighbor)->setNeighbourType( SOUTH, type );
  128. neighbor = zBlockNeighbor( location + getDirection( EAST ) );
  129. if( neighbor.isA() )
  130. ((Block*)neighbor)->setNeighbourType( WEST, type );
  131. neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
  132. if( neighbor.isA() )
  133. ((Block*)neighbor)->setNeighbourType( NORTH, type );
  134. neighbor = zBlockNeighbor( location + getDirection( WEST ) );
  135. if( neighbor.isA() )
  136. ((Block*)neighbor)->setNeighbourType( EAST, type );
  137. neighbor = zBlockNeighbor( location + getDirection( TOP ) );
  138. if( neighbor.isA() )
  139. ((Block*)neighbor)->setNeighbourType( BOTTOM, type );
  140. neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
  141. if( neighbor.isA() )
  142. ((Block*)neighbor)->setNeighbourType( TOP, type );
  143. }
  144. void Chunk::setNeighbor( Direction dir, Chunk* zChunk )
  145. {
  146. zNeighbours[ getDirectionIndex( dir ) ] = zChunk;
  147. for( int i = 0; i < CHUNK_SIZE; i++ )
  148. {
  149. for( int z = 0; z < WORLD_HEIGHT; z++ )
  150. {
  151. if( dir == NORTH )
  152. {
  153. int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  154. if( blocks[ index ] )
  155. {
  156. int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  157. if( zChunk && zChunk->blocks[ j ] )
  158. blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ j ] );
  159. else
  160. {
  161. blocks[ index ]->setNeighbour( NORTH, 0 );
  162. blocks[ index ]->setNeighbourType( NORTH, zChunk ? zChunk->blockIds[ j ] : 0 );
  163. }
  164. }
  165. }
  166. else if( dir == EAST )
  167. {
  168. int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  169. if( blocks[ index ] )
  170. {
  171. int j = i * WORLD_HEIGHT + z;
  172. if( zChunk && zChunk->blocks[ j ] )
  173. blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ j ] );
  174. else
  175. {
  176. blocks[ index ]->setNeighbour( EAST, 0 );
  177. blocks[ index ]->setNeighbourType( EAST, zChunk ? zChunk->blockIds[ j ] : 0 );
  178. }
  179. }
  180. }
  181. else if( dir == SOUTH )
  182. {
  183. int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  184. if( blocks[ index ] )
  185. {
  186. int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  187. if( zChunk && zChunk->blocks[ j ] )
  188. blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ j ] );
  189. else
  190. {
  191. blocks[ index ]->setNeighbour( SOUTH, 0 );
  192. blocks[ index ]->setNeighbourType( SOUTH, zChunk ? zChunk->blockIds[ j ] : 0 );
  193. }
  194. }
  195. }
  196. else if( dir == WEST )
  197. {
  198. int index = i * WORLD_HEIGHT + z;
  199. if( blocks[ index ] )
  200. {
  201. int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  202. if( zChunk && zChunk->blocks[ j ] )
  203. blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ j ] );
  204. else
  205. {
  206. blocks[ index ]->setNeighbour( WEST, 0 );
  207. blocks[ index ]->setNeighbourType( WEST, zChunk ? zChunk->blockIds[ j ] : 0 );
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. void Chunk::load( Framework::StreamReader* zReader )
  215. {
  216. zReader->lese( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
  217. for( int x = 0; x < CHUNK_SIZE; x++ )
  218. {
  219. for( int y = 0; y < CHUNK_SIZE; y++ )
  220. {
  221. for( int z = 0; z < WORLD_HEIGHT; z++ )
  222. {
  223. unsigned short blockType;
  224. zReader->lese( (char*)&blockType, 2 );
  225. Block* block = STATIC_REGISTRY( BlockType ).zElement( blockType )->loadBlock( Framework::Vec3<int>( x + location.x - CHUNK_SIZE / 2, y + location.y - CHUNK_SIZE / 2, z ), zReader );
  226. if( block )
  227. putBlockAt( { x, y, z }, block );
  228. else
  229. {
  230. if( STATIC_REGISTRY( BlockType ).zElement( blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] )->needsInstance() )
  231. putBlockAt( { x, y, z }, STATIC_REGISTRY( BlockType ).zElement( blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] )->createBlock( { x + location.x - CHUNK_SIZE / 2, y + location.y - CHUNK_SIZE / 2, z } ) );
  232. else
  233. putBlockTypeAt( { x, y, z }, blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] );
  234. }
  235. }
  236. }
  237. }
  238. }
  239. int Chunk::getDimensionId() const
  240. {
  241. return dimensionId;
  242. }
  243. Framework::Punkt Chunk::getCenter() const
  244. {
  245. return location;
  246. }
  247. Framework::Vec3<int> Chunk::getMin() const
  248. {
  249. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  250. }
  251. Framework::Vec3<int> Chunk::getMax() const
  252. {
  253. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  254. }
  255. void Chunk::prepareRemove()
  256. {
  257. added = 0;
  258. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  259. {
  260. if( blocks[ i ] )
  261. blocks[ i ]->remove();
  262. }
  263. for( int i = 0; i < 4; i++ )
  264. {
  265. if( zNeighbours[ i ] )
  266. {
  267. zNeighbours[ i ]->setNeighbor( getOppositeDirection( getDirectionFromIndex( i ) ), 0 );
  268. zNeighbours[ i ] = 0;
  269. }
  270. }
  271. }
  272. void Chunk::setAdded()
  273. {
  274. added = 1;
  275. }