Chunk.cpp 10 KB

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