Dimension.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Block* 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. zWorld->setVisibility( old, 0 );
  59. int index = 0;
  60. for( auto iterator = chunkList.begin(); iterator; ++iterator, ++index )
  61. {
  62. if( (Chunk*)iterator == old )
  63. {
  64. if( chunk )
  65. iterator.set( chunk );
  66. else
  67. chunkList.remove( index );
  68. break;
  69. }
  70. }
  71. }
  72. else if( chunk )
  73. chunkList.add( chunk );
  74. chunks->set( addr, 8, chunk );
  75. if( chunk )
  76. {
  77. zWorld->setVisibility( chunk, 1 );
  78. }
  79. cs.unlock();
  80. }
  81. int Dimension::getDimensionId() const
  82. {
  83. return dimensionId;
  84. }
  85. bool Dimension::hasChunck( int x, int y ) const
  86. {
  87. return zChunk( Punkt( x, y ) );
  88. }
  89. void Dimension::removeDistantChunks( Punkt wPos, World* zWorld )
  90. {
  91. Array<int> removed;
  92. int index = 0;
  93. for( Chunk* chunk : chunkList )
  94. {
  95. if( (chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE )
  96. removed.add( index, 0 );
  97. index++;
  98. }
  99. for( int i : removed )
  100. {
  101. Chunk* chunk = chunkList.get( i );
  102. zWorld->setVisibility( chunk, 0 );
  103. setChunk( 0, chunk->getCenter(), zWorld );
  104. }
  105. }
  106. void Dimension::setBlock( Block* block )
  107. {
  108. cs.lock();
  109. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( block->getPos().x ), (int)floor( block->getPos().y ) ) );
  110. if( c )
  111. c->setBlock( block );
  112. else
  113. block->release();
  114. cs.unlock();
  115. }
  116. void Dimension::removeBlock( Block* zBlock )
  117. {
  118. cs.lock();
  119. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( zBlock->getPos().x ), (int)floor( zBlock->getPos().y ) ) );
  120. if( c )
  121. c->removeBlock( zBlock );
  122. cs.unlock();
  123. }
  124. Entity* Dimension::zEntity( int id )
  125. {
  126. cs.lock();
  127. for( Entity* e : *entities )
  128. {
  129. if( e->getId() == id )
  130. {
  131. cs.unlock();
  132. return e;
  133. }
  134. }
  135. cs.unlock();
  136. return 0;
  137. }
  138. void Dimension::removeEntity( int id )
  139. {
  140. cs.lock();
  141. int index = 0;
  142. for( Entity* e : *entities )
  143. {
  144. if( e->getId() == id )
  145. {
  146. currentGame->setVisibility( e, 0 );
  147. entities->remove( index );
  148. cs.unlock();
  149. return;
  150. }
  151. index++;
  152. }
  153. cs.unlock();
  154. }