Chunk.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. 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, Game* zGame, int dimensionId, Framework::StreamReader* zReader )
  22. : Chunk( location, zGame, 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 zGame->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. void Chunk::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  51. {
  52. // TODO: answer api messages
  53. }
  54. Framework::Either<Block*, int> Chunk::zBlockAt( Framework::Vec3<int> location ) const
  55. {
  56. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  57. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  58. if( blocks[ index ] )
  59. return blocks[ index ];
  60. else
  61. return (int)blockIds[ index ];
  62. }
  63. const Block* Chunk::zBlockConst( Framework::Vec3<int> location ) const
  64. {
  65. Block* b = zBlockAt( location );
  66. if( b )
  67. return b;
  68. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  69. if( blockIds[ index ] )
  70. return StaticRegistry<BlockType>::INSTANCE.zElement( blockIds[ index ] )->zDefault();
  71. return 0;
  72. }
  73. void Chunk::instantiateBlock( Framework::Vec3<int> location )
  74. {
  75. if( zBlockAt( location ) )
  76. return;
  77. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  78. if( !blockIds[ index ] )
  79. generateBlock( location );
  80. if( !zBlockAt( location ) )
  81. putBlockAt( location, StaticRegistry<BlockType>::INSTANCE.zElement( blockIds[ index ] )->createBlockAt( location, zGame, 0 ) );
  82. }
  83. void Chunk::generateBlock( Framework::Vec3<int> location )
  84. {
  85. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  86. if( blockIds[ index ] )
  87. return;
  88. auto generated = zGame->zGenerator()->generateSingleBlock( location, dimensionId );
  89. if( generated.isA() )
  90. putBlockAt( location, generated );
  91. else
  92. putBlockTypeAt( location, generated );
  93. }
  94. void Chunk::putBlockAt( Framework::Vec3<int> location, Block* block )
  95. {
  96. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  97. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  98. Block* old = blocks[ index ];
  99. if( block )
  100. blockIds[ index ] = (unsigned short)block->zBlockType()->getId();
  101. blocks[ index ] = block;
  102. Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
  103. if( neighbor.isA() )
  104. ((Block*)neighbor)->setNeighbour( SOUTH, block );
  105. if( block )
  106. block->setNeighbour( NORTH, neighbor );
  107. neighbor = zBlockNeighbor( location + getDirection( EAST ) );
  108. if( neighbor.isA() )
  109. ((Block*)neighbor)->setNeighbour( WEST, block );
  110. if( block )
  111. block->setNeighbour( EAST, neighbor );
  112. neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
  113. if( neighbor.isA() )
  114. ((Block*)neighbor)->setNeighbour( NORTH, block );
  115. if( block )
  116. block->setNeighbour( SOUTH, neighbor );
  117. neighbor = zBlockNeighbor( location + getDirection( WEST ) );
  118. if( neighbor.isA() )
  119. ((Block*)neighbor)->setNeighbour( EAST, block );
  120. if( block )
  121. block->setNeighbour( WEST, neighbor );
  122. neighbor = zBlockNeighbor( location + getDirection( TOP ) );
  123. if( neighbor.isA() )
  124. ((Block*)neighbor)->setNeighbour( BOTTOM, block );
  125. if( block )
  126. block->setNeighbour( TOP, neighbor );
  127. neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
  128. if( neighbor.isA() )
  129. ((Block*)neighbor)->setNeighbour( TOP, block );
  130. if( block )
  131. block->setNeighbour( BOTTOM, neighbor );
  132. if( old )
  133. old->release();
  134. }
  135. void Chunk::putBlockTypeAt( Framework::Vec3<int> location, int type )
  136. {
  137. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  138. assert( index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT );
  139. blockIds[ index ] = (unsigned short)type;
  140. Either<Block*, int> neighbor = zBlockNeighbor( location + getDirection( NORTH ) );
  141. if( neighbor.isA() )
  142. ((Block*)neighbor)->setNeighbourType( SOUTH, type );
  143. neighbor = zBlockNeighbor( location + getDirection( EAST ) );
  144. if( neighbor.isA() )
  145. ((Block*)neighbor)->setNeighbourType( WEST, type );
  146. neighbor = zBlockNeighbor( location + getDirection( SOUTH ) );
  147. if( neighbor.isA() )
  148. ((Block*)neighbor)->setNeighbourType( NORTH, type );
  149. neighbor = zBlockNeighbor( location + getDirection( WEST ) );
  150. if( neighbor.isA() )
  151. ((Block*)neighbor)->setNeighbourType( EAST, type );
  152. neighbor = zBlockNeighbor( location + getDirection( TOP ) );
  153. if( neighbor.isA() )
  154. ((Block*)neighbor)->setNeighbourType( BOTTOM, type );
  155. neighbor = zBlockNeighbor( location + getDirection( BOTTOM ) );
  156. if( neighbor.isA() )
  157. ((Block*)neighbor)->setNeighbourType( TOP, type );
  158. }
  159. void Chunk::setNeighbor( Direction dir, Chunk* zChunk )
  160. {
  161. zNeighbours[ getDirectionIndex( dir ) ] = zChunk;
  162. for( int i = 0; i < CHUNK_SIZE; i++ )
  163. {
  164. for( int z = 0; z < WORLD_HEIGHT; z++ )
  165. {
  166. if( dir == NORTH )
  167. {
  168. int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  169. if( blocks[ index ] )
  170. {
  171. int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  172. if( zChunk && zChunk->blocks[ j ] )
  173. blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ j ] );
  174. else
  175. {
  176. blocks[ index ]->setNeighbour( NORTH, 0 );
  177. blocks[ index ]->setNeighbourType( NORTH, zChunk ? zChunk->blockIds[ j ] : 0 );
  178. }
  179. }
  180. }
  181. else if( dir == EAST )
  182. {
  183. int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  184. if( blocks[ index ] )
  185. {
  186. int j = i * WORLD_HEIGHT + z;
  187. if( zChunk && zChunk->blocks[ j ] )
  188. blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ j ] );
  189. else
  190. {
  191. blocks[ index ]->setNeighbour( EAST, 0 );
  192. blocks[ index ]->setNeighbourType( EAST, zChunk ? zChunk->blockIds[ j ] : 0 );
  193. }
  194. }
  195. }
  196. else if( dir == SOUTH )
  197. {
  198. int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  199. if( blocks[ index ] )
  200. {
  201. int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  202. if( zChunk && zChunk->blocks[ j ] )
  203. blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ j ] );
  204. else
  205. {
  206. blocks[ index ]->setNeighbour( SOUTH, 0 );
  207. blocks[ index ]->setNeighbourType( SOUTH, zChunk ? zChunk->blockIds[ j ] : 0 );
  208. }
  209. }
  210. }
  211. else if( dir == WEST )
  212. {
  213. int index = i * WORLD_HEIGHT + z;
  214. if( blocks[ index ] )
  215. {
  216. int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  217. if( zChunk && zChunk->blocks[ j ] )
  218. blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ j ] );
  219. else
  220. {
  221. blocks[ index ]->setNeighbour( WEST, 0 );
  222. blocks[ index ]->setNeighbourType( WEST, zChunk ? zChunk->blockIds[ j ] : 0 );
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. void Chunk::load( Framework::StreamReader* zReader )
  230. {
  231. zReader->lese( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
  232. for( int x = 0; x < CHUNK_SIZE; x++ )
  233. {
  234. for( int y = 0; y < CHUNK_SIZE; y++ )
  235. {
  236. for( int z = 0; z < WORLD_HEIGHT; z++ )
  237. {
  238. unsigned short blockType;
  239. zReader->lese( (char*)&blockType, 2 );
  240. Block* block = StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->loadBlock( Framework::Vec3<int>( x, y, z ), zGame, zReader );
  241. if( block )
  242. putBlockAt( { x, y, z }, block );
  243. else
  244. putBlockTypeAt( { x, y, z }, blockIds[ (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z ] );
  245. }
  246. }
  247. }
  248. }
  249. void Chunk::save( Framework::StreamWriter* zWriter )
  250. {
  251. zWriter->schreibe( (char*)blockIds, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof( unsigned short ) );
  252. for( int x = 0; x < CHUNK_SIZE; x++ )
  253. {
  254. for( int y = 0; y < CHUNK_SIZE; y++ )
  255. {
  256. for( int z = 0; z < WORLD_HEIGHT; z++ )
  257. {
  258. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  259. unsigned short blockType = blocks[ index ] ? (unsigned short)blocks[ index ]->zBlockType()->getId() : (unsigned short)NoBlockBlockType::ID;
  260. zWriter->schreibe( (char*)&blockType, 2 );
  261. StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );
  262. }
  263. }
  264. }
  265. }
  266. void Chunk::removeUnusedBlocks()
  267. {
  268. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  269. {
  270. if( blocks[ i ] )
  271. {
  272. if( !blocks[ i ]->isVisible() )
  273. {
  274. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  275. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  276. int z = i % WORLD_HEIGHT;
  277. putBlockAt( { x,y,z }, 0 );
  278. putBlockTypeAt( { x, y, z }, NoBlockBlockType::ID );
  279. }
  280. }
  281. else if( blockIds[ i ] )
  282. {
  283. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  284. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  285. int z = i % WORLD_HEIGHT;
  286. bool visible = 0;
  287. if( CONST_BLOCK( 0, blockIds[ i ] )->isTransparent() || CONST_BLOCK( 0, blockIds[ i ] )->isPassable() )
  288. visible = 1;
  289. else
  290. {
  291. for( int d = 0; d < 6 && !visible; d++ )
  292. {
  293. auto n = zBlockNeighbor( getDirection( (Directions)getDirectionFromIndex( d ) ) + Framework::Vec3<int>( x, y, z ) );
  294. if( n.isA() && (((Block*)n)->isPassable() || ((Block*)n)->isTransparent()) )
  295. visible = 1;
  296. if( n.isB() && (CONST_BLOCK( 0, n )->isTransparent() || CONST_BLOCK( 0, n )->isPassable()) )
  297. visible = 1;
  298. }
  299. }
  300. if( !visible )
  301. {
  302. putBlockAt( { x,y,z }, 0 );
  303. putBlockTypeAt( { x, y, z }, NoBlockBlockType::ID );
  304. }
  305. }
  306. }
  307. int count = 0;
  308. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  309. {
  310. if( blockIds[ i ] && blockIds[ i ] != AirBlockBlockType::ID )
  311. count++;
  312. }
  313. std::cout << "chunk " << location.x << ", " << location.y << " was generated with " << count << " blocks.\n";
  314. }
  315. int Chunk::getDimensionId() const
  316. {
  317. return dimensionId;
  318. }
  319. Framework::Punkt Chunk::getCenter() const
  320. {
  321. return location;
  322. }
  323. Framework::Vec3<int> Chunk::getMin() const
  324. {
  325. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  326. }
  327. Framework::Vec3<int> Chunk::getMax() const
  328. {
  329. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  330. }
  331. Game* Chunk::zGameObj() const
  332. {
  333. return zGame;
  334. }
  335. void Chunk::prepareRemove()
  336. {
  337. added = 0;
  338. for( int i = 0; i < 4; i++ )
  339. {
  340. if( zNeighbours[ i ] )
  341. {
  342. zNeighbours[ i ]->setNeighbor( getOppositeDirection( getDirectionFromIndex( i ) ), 0 );
  343. zNeighbours[ i ] = 0;
  344. }
  345. }
  346. }
  347. void Chunk::setAdded()
  348. {
  349. added = 1;
  350. }
  351. void Chunk::addView( Entity* zEntity )
  352. {
  353. cs.lock();
  354. bool found = 0;
  355. for( Entity* e : views )
  356. {
  357. if( e == zEntity )
  358. {
  359. found = 1;
  360. break;
  361. }
  362. }
  363. if( !found )
  364. views.add( zEntity );
  365. cs.unlock();
  366. }
  367. void Chunk::removeView( Entity* zEntity )
  368. {
  369. cs.lock();
  370. int i = 0;
  371. for( Entity* e : views )
  372. {
  373. if( e == zEntity )
  374. {
  375. views.remove( i );
  376. break;
  377. }
  378. i++;
  379. }
  380. cs.unlock();
  381. }
  382. bool Chunk::hasViews() const
  383. {
  384. return views.getEintragAnzahl() > 0;
  385. }