Dimension.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "Globals.h"
  6. #include "World.h"
  7. using namespace Framework;
  8. #define MAX_VIEW_DISTANCE CHUNK_SIZE * 8
  9. Dimension::Dimension( int id )
  10. : dimensionId( id ),
  11. chunks( new Trie<Chunk>() ),
  12. entities( new RCArray<Entity>() )
  13. {}
  14. Dimension::~Dimension()
  15. {
  16. entities->release();
  17. chunks->release();
  18. }
  19. void Dimension::getAddrOf( Punkt cPos, char* addr ) const
  20. {
  21. *(int*)addr = cPos.x;
  22. *((int*)addr + 1) = cPos.y;
  23. }
  24. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  25. {
  26. if( wPos.x < 0 )
  27. wPos.x -= CHUNK_SIZE;
  28. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  29. wPos.y -= CHUNK_SIZE;
  30. wPos /= CHUNK_SIZE;
  31. getAddrOf( wPos, addr );
  32. }
  33. Chunk* Dimension::zChunk( Punkt wPos ) const
  34. {
  35. char addr[ 8 ];
  36. getAddrOfWorld( wPos, addr );
  37. return chunks->z( addr, 8 );
  38. }
  39. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
  40. {
  41. Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
  42. if( c )
  43. return c->zBlockAt( location );
  44. return 0;
  45. }
  46. void Dimension::addEntity( Entity* entity )
  47. {
  48. entities->add( entity );
  49. }
  50. void Dimension::setChunk( Chunk* chunk, Punkt center, World* zWorld )
  51. {
  52. char addr[ 8 ];
  53. getAddrOfWorld( center, addr );
  54. Chunk* old = chunks->z( addr, 8 );
  55. cs.lock();
  56. if( old )
  57. {
  58. int index = 0;
  59. for( auto iterator = chunkList.begin(); iterator; ++iterator, ++index )
  60. {
  61. if( (Chunk*)iterator == old )
  62. {
  63. if( chunk )
  64. iterator.set( chunk );
  65. else
  66. chunkList.remove( index );
  67. break;
  68. }
  69. }
  70. }
  71. else if( chunk )
  72. chunkList.add( chunk );
  73. chunks->set( addr, 8, chunk );
  74. if( chunk )
  75. {
  76. zWorld->setVisibility( chunk, 1 );
  77. }
  78. cs.unlock();
  79. }
  80. int Dimension::getDimensionId() const
  81. {
  82. return dimensionId;
  83. }
  84. bool Dimension::hasChunck( int x, int y ) const
  85. {
  86. return zChunk( Punkt( x, y ) );
  87. }
  88. void Dimension::removeDistantChunks( Punkt wPos, World* zWorld )
  89. {
  90. Array<int> removed;
  91. int index = 0;
  92. for( Chunk* chunk : chunkList )
  93. {
  94. if( (chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE )
  95. removed.add( index, 0 );
  96. index++;
  97. }
  98. for( int i : removed )
  99. {
  100. Chunk* chunk = chunkList.get( i );
  101. zWorld->setVisibility( chunk, 0 );
  102. setChunk( 0, chunk->getCenter(), zWorld );
  103. }
  104. }
  105. void Dimension::setBlock( Block* block )
  106. {
  107. cs.lock();
  108. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( block->getPos().x ), (int)floor( block->getPos().y ) ) );
  109. if( c )
  110. c->setBlock( block );
  111. else
  112. block->release();
  113. cs.unlock();
  114. }
  115. void Dimension::removeBlock( Block* zBlock )
  116. {
  117. cs.lock();
  118. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( zBlock->getPos().x ), (int)floor( zBlock->getPos().y ) ) );
  119. if( c )
  120. c->removeBlock( zBlock );
  121. cs.unlock();
  122. }