Chunk.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. unsigned short id = 0;
  232. zReader->lese( (char*)&id, 2 );
  233. Framework::Vec3<int> pos;
  234. bool d = 0;
  235. while( id )
  236. {
  237. zReader->lese( (char*)&pos.x, 4 );
  238. zReader->lese( (char*)&pos.y, 4 );
  239. zReader->lese( (char*)&pos.z, 4 );
  240. zReader->lese( (char*)&d, 1 );
  241. if( d )
  242. putBlockAt( pos, StaticRegistry<BlockType>::INSTANCE.zElement( id )->loadBlock( Framework::Vec3<int>( pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z ), zGame, zReader ) );
  243. else
  244. putBlockTypeAt( pos, id );
  245. zReader->lese( (char*)&id, 2 );
  246. }
  247. }
  248. void Chunk::save( Framework::StreamWriter* zWriter, StreamTarget target )
  249. {
  250. for( int x = 0; x < CHUNK_SIZE; x++ )
  251. {
  252. for( int y = 0; y < CHUNK_SIZE; y++ )
  253. {
  254. for( int z = 0; z < WORLD_HEIGHT; z++ )
  255. {
  256. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  257. unsigned short blockType = blocks[ index ] ? (unsigned short)blocks[ index ]->zBlockType()->getId() : blockIds[ index ];
  258. if( blockType )
  259. {
  260. bool visible = target == StreamTarget::FULL;
  261. if( !visible )
  262. {
  263. if( !blocks[ index ] )
  264. {
  265. if( CONST_BLOCK( 0, blockIds[ index ] )->isTransparent() || CONST_BLOCK( 0, blockIds[ index ] )->isPassable() )
  266. visible = 1;
  267. else
  268. {
  269. for( int d = 0; d < 6 && !visible; d++ )
  270. {
  271. auto n = zBlockNeighbor( getDirection( (Directions)getDirectionFromIndex( d ) ) + Framework::Vec3<int>( x, y, z ) );
  272. if( n.isA() && (((Block*)n)->isPassable() || ((Block*)n)->isTransparent()) )
  273. visible = 1;
  274. if( n.isB() && (CONST_BLOCK( 0, n )->isTransparent() || CONST_BLOCK( 0, n )->isPassable()) )
  275. visible = 1;
  276. }
  277. }
  278. }
  279. else
  280. visible = blocks[ index ]->isVisible();
  281. }
  282. if( target == StreamTarget::FULL || (visible && (blocks[ index ] || blockType != AirBlockBlockType::ID)) )
  283. {
  284. zWriter->schreibe( (char*)&blockType, 2 );
  285. zWriter->schreibe( (char*)&x, 4 );
  286. zWriter->schreibe( (char*)&y, 4 );
  287. zWriter->schreibe( (char*)&z, 4 );
  288. if( blocks[ index ] )
  289. {
  290. bool d = 1;
  291. zWriter->schreibe( (char*)&d, 1 );
  292. StaticRegistry<BlockType>::INSTANCE.zElement( blockType )->saveBlock( blocks[ index ], zWriter );
  293. }
  294. else
  295. {
  296. bool d = 0;
  297. zWriter->schreibe( (char*)&d, 1 );
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. unsigned short end = 0;
  305. zWriter->schreibe( (char*)&end, 2 );
  306. }
  307. void Chunk::removeUnusedBlocks()
  308. {
  309. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  310. {
  311. if( blocks[ i ] )
  312. {
  313. if( !blocks[ i ]->isVisible() )
  314. {
  315. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  316. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  317. int z = i % WORLD_HEIGHT;
  318. putBlockAt( { x,y,z }, 0 );
  319. putBlockTypeAt( { x, y, z }, NoBlockBlockType::ID );
  320. }
  321. }
  322. else if( blockIds[ i ] )
  323. {
  324. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  325. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  326. int z = i % WORLD_HEIGHT;
  327. bool visible = 0;
  328. if( CONST_BLOCK( 0, blockIds[ i ] )->isTransparent() || CONST_BLOCK( 0, blockIds[ i ] )->isPassable() )
  329. visible = 1;
  330. else
  331. {
  332. for( int d = 0; d < 6 && !visible; d++ )
  333. {
  334. auto n = zBlockNeighbor( getDirection( (Directions)getDirectionFromIndex( d ) ) + Framework::Vec3<int>( x, y, z ) );
  335. if( n.isA() && (((Block*)n)->isPassable() || ((Block*)n)->isTransparent()) )
  336. visible = 1;
  337. if( n.isB() && (CONST_BLOCK( 0, n )->isTransparent() || CONST_BLOCK( 0, n )->isPassable()) )
  338. visible = 1;
  339. }
  340. }
  341. if( !visible )
  342. {
  343. putBlockAt( { x,y,z }, 0 );
  344. putBlockTypeAt( { x, y, z }, NoBlockBlockType::ID );
  345. }
  346. }
  347. }
  348. int count = 0;
  349. for( int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++ )
  350. {
  351. if( blockIds[ i ] && blockIds[ i ] != AirBlockBlockType::ID )
  352. count++;
  353. }
  354. std::cout << "chunk " << location.x << ", " << location.y << " was generated with " << count << " blocks.\n";
  355. }
  356. int Chunk::getDimensionId() const
  357. {
  358. return dimensionId;
  359. }
  360. Framework::Punkt Chunk::getCenter() const
  361. {
  362. return location;
  363. }
  364. Framework::Vec3<int> Chunk::getMin() const
  365. {
  366. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  367. }
  368. Framework::Vec3<int> Chunk::getMax() const
  369. {
  370. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  371. }
  372. Game* Chunk::zGameObj() const
  373. {
  374. return zGame;
  375. }
  376. void Chunk::prepareRemove()
  377. {
  378. added = 0;
  379. for( int i = 0; i < 4; i++ )
  380. {
  381. if( zNeighbours[ i ] )
  382. {
  383. zNeighbours[ i ]->setNeighbor( getOppositeDirection( getDirectionFromIndex( i ) ), 0 );
  384. zNeighbours[ i ] = 0;
  385. }
  386. }
  387. }
  388. void Chunk::setAdded()
  389. {
  390. added = 1;
  391. }
  392. void Chunk::addView( Entity* zEntity )
  393. {
  394. cs.lock();
  395. bool found = 0;
  396. for( Entity* e : views )
  397. {
  398. if( e == zEntity )
  399. {
  400. found = 1;
  401. break;
  402. }
  403. }
  404. if( !found )
  405. views.add( zEntity );
  406. cs.unlock();
  407. }
  408. void Chunk::removeView( Entity* zEntity )
  409. {
  410. cs.lock();
  411. int i = 0;
  412. for( Entity* e : views )
  413. {
  414. if( e == zEntity )
  415. {
  416. views.remove( i );
  417. break;
  418. }
  419. i++;
  420. }
  421. cs.unlock();
  422. }
  423. bool Chunk::hasView( Entity* zEntity )
  424. {
  425. cs.lock();
  426. for( Entity* e : views )
  427. {
  428. if( e == zEntity )
  429. {
  430. cs.unlock();
  431. return 1;
  432. }
  433. }
  434. cs.unlock();
  435. return 0;
  436. }
  437. bool Chunk::hasViews() const
  438. {
  439. return views.getEintragAnzahl() > 0;
  440. }