Dimension.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. Block* Dimension::getBlock( Vec3<int> location )
  47. {
  48. cs.lock();
  49. Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
  50. if( c )
  51. {
  52. Block* b = c->zBlockAt( location );
  53. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  54. cs.unlock();
  55. return b;
  56. }
  57. cs.unlock();
  58. return 0;
  59. }
  60. void Dimension::addEntity( Entity* entity )
  61. {
  62. entities->add( entity );
  63. }
  64. void Dimension::setChunk( Chunk* chunk, Punkt center, World* zWorld )
  65. {
  66. char addr[ 8 ];
  67. getAddrOfWorld( center, addr );
  68. Chunk* old = chunks->z( addr, 8 );
  69. cs.lock();
  70. if( old )
  71. {
  72. zWorld->setVisibility( old, 0 );
  73. int index = 0;
  74. for( auto iterator = chunkList.begin(); iterator; ++iterator, ++index )
  75. {
  76. if( (Chunk*)iterator == old )
  77. {
  78. if( chunk )
  79. iterator.set( chunk );
  80. else
  81. chunkList.remove( index );
  82. break;
  83. }
  84. }
  85. }
  86. else if( chunk )
  87. chunkList.add( chunk );
  88. chunks->set( addr, 8, chunk );
  89. if( chunk )
  90. {
  91. zWorld->setVisibility( chunk, 1 );
  92. }
  93. cs.unlock();
  94. }
  95. int Dimension::getDimensionId() const
  96. {
  97. return dimensionId;
  98. }
  99. bool Dimension::hasChunck( int x, int y ) const
  100. {
  101. return zChunk( Punkt( x, y ) );
  102. }
  103. void Dimension::removeDistantChunks( Punkt wPos, World* zWorld )
  104. {
  105. Array<int> removed;
  106. int index = 0;
  107. for( Chunk* chunk : chunkList )
  108. {
  109. if( (chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE )
  110. removed.add( index, 0 );
  111. index++;
  112. }
  113. for( int i : removed )
  114. {
  115. Chunk* chunk = chunkList.get( i );
  116. zWorld->setVisibility( chunk, 0 );
  117. setChunk( 0, chunk->getCenter(), zWorld );
  118. }
  119. }
  120. void Dimension::setBlock( Block* block )
  121. {
  122. cs.lock();
  123. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( block->getPos().x ), (int)floor( block->getPos().y ) ) );
  124. if( c )
  125. c->setBlock( block );
  126. else
  127. block->release();
  128. cs.unlock();
  129. }
  130. void Dimension::removeBlock( Block* zBlock )
  131. {
  132. cs.lock();
  133. Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( zBlock->getPos().x ), (int)floor( zBlock->getPos().y ) ) );
  134. if( c )
  135. c->removeBlock( zBlock );
  136. cs.unlock();
  137. }
  138. Entity* Dimension::zEntity( int id )
  139. {
  140. cs.lock();
  141. for( Entity* e : *entities )
  142. {
  143. if( e->getId() == id )
  144. {
  145. cs.unlock();
  146. return e;
  147. }
  148. }
  149. cs.unlock();
  150. return 0;
  151. }
  152. Entity* Dimension::getEntity( int id )
  153. {
  154. cs.lock();
  155. for( Entity* e : *entities )
  156. {
  157. if( e->getId() == id )
  158. {
  159. Entity* result = dynamic_cast<Entity*>(e->getThis());
  160. cs.unlock();
  161. return result;
  162. }
  163. }
  164. cs.unlock();
  165. return 0;
  166. }
  167. void Dimension::removeEntity( int id )
  168. {
  169. cs.lock();
  170. int index = 0;
  171. for( Entity* e : *entities )
  172. {
  173. if( e->getId() == id )
  174. {
  175. currentGame->setVisibility( e, 0 );
  176. entities->remove( index );
  177. cs.unlock();
  178. return;
  179. }
  180. index++;
  181. }
  182. cs.unlock();
  183. }