Dimension.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. using namespace Framework;
  6. Dimension::Dimension( int id )
  7. : dimensionId( id ),
  8. chunks( new Trie<Chunk>() ),
  9. entities( new RCArray<Entity>() )
  10. {}
  11. Dimension::~Dimension()
  12. {
  13. entities->release();
  14. chunks->release();
  15. }
  16. void Dimension::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  17. {
  18. // TODO: switch type chunck, block, entity
  19. }
  20. void Dimension::tickEntities( Game* zGame )
  21. {
  22. int index = 0;
  23. Array<int> removed;
  24. for( auto entity : *entities )
  25. {
  26. if( zChunk( Punkt( (int)entity->getPosition().x, (int)entity->getPosition().y ) ) )
  27. entity->tick( this, zGame );
  28. if( entity->isRemoved() )
  29. removed.add( index, 0 );
  30. index++;
  31. }
  32. for( int i : removed )
  33. entities->remove( i );
  34. }
  35. void Dimension::getAddrOf( Punkt cPos, char* addr ) const
  36. {
  37. *(int*)addr = cPos.x;
  38. *((int*)addr + 1) = cPos.y;
  39. }
  40. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  41. {
  42. if( wPos.x < 0 )
  43. wPos.x -= CHUNK_SIZE;
  44. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  45. wPos.y -= CHUNK_SIZE;
  46. wPos /= CHUNK_SIZE;
  47. getAddrOf( wPos, addr );
  48. }
  49. Chunk* Dimension::zChunk( Punkt wPos ) const
  50. {
  51. char addr[ 8 ];
  52. getAddrOfWorld( wPos, addr );
  53. return chunks->z( addr, 8 );
  54. }
  55. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location, const Game* zGame )
  56. {
  57. Chunk* c = zChunk( zGame->getChunkCenter( location.x, location.y ) );
  58. if( c )
  59. {
  60. int x = location.x % CHUNK_SIZE;
  61. int y = location.y % CHUNK_SIZE;
  62. if( x < 0 )
  63. x += CHUNK_SIZE;
  64. if( y < 0 )
  65. y += CHUNK_SIZE;
  66. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  67. }
  68. return 0;
  69. }
  70. void Dimension::addEntity( Entity* entity )
  71. {
  72. entities->add( entity );
  73. }
  74. void Dimension::addChunk( Chunk* chunk )
  75. {
  76. char addr[ 8 ];
  77. getAddrOfWorld( chunk->getCenter(), addr );
  78. if( !chunks->z( addr, 8 ) )
  79. {
  80. chunks->set( addr, 8, chunk );
  81. getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
  82. Chunk* zChunk = chunks->z( addr, 8 );
  83. if( zChunk )
  84. {
  85. zChunk->setNeighbor( WEST, chunk );
  86. chunk->setNeighbor( EAST, chunk );
  87. }
  88. getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
  89. zChunk = chunks->z( addr, 8 );
  90. if( zChunk )
  91. {
  92. zChunk->setNeighbor( EAST, chunk );
  93. chunk->setNeighbor( WEST, chunk );
  94. }
  95. getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
  96. zChunk = chunks->z( addr, 8 );
  97. if( zChunk )
  98. {
  99. zChunk->setNeighbor( NORTH, chunk );
  100. chunk->setNeighbor( SOUTH, chunk );
  101. }
  102. getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
  103. zChunk = chunks->z( addr, 8 );
  104. if( zChunk )
  105. {
  106. zChunk->setNeighbor( SOUTH, chunk );
  107. chunk->setNeighbor( NORTH, chunk );
  108. }
  109. }
  110. else
  111. {
  112. std::cout << "WARNING: chunk duplication at dimension " << dimensionId << " at chunk " << chunk->getCenter().x << ", " << chunk->getCenter().y << "\n";
  113. assert( false );
  114. chunk->release();
  115. }
  116. }
  117. void Dimension::save( Text worldDir ) const
  118. {
  119. for( auto chunk = chunks->getIterator(); chunk; chunk++ )
  120. {
  121. if( !chunk._ )
  122. continue;
  123. Datei* file = new Datei();
  124. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  125. filePath.appendHex( chunk->getCenter().x );
  126. filePath += "_";
  127. filePath.appendHex( chunk->getCenter().y );
  128. filePath += ".chunk";
  129. file->setDatei( filePath );
  130. if( file->open( Datei::Style::schreiben ) )
  131. chunk->save( file );
  132. file->close();
  133. file->release();
  134. }
  135. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  136. Datei* file = new Datei();
  137. file->setDatei( filePath );
  138. if( file->open( Datei::Style::schreiben ) )
  139. {
  140. for( Entity* entity : *entities )
  141. {
  142. if( entity->zType()->getId() != PlayerEntityType::ID )
  143. {
  144. if( !entity->isRemoved() )
  145. {
  146. int type = entity->zType()->getId();
  147. file->schreibe( (char*)&type, 4 );
  148. StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, file );
  149. }
  150. }
  151. else
  152. {
  153. Datei pFile;
  154. pFile.setDatei( worldDir + "/player/" + ((Player*)entity)->getName() );
  155. if( pFile.open( Datei::Style::schreiben ) )
  156. PlayerEntityType::INSTANCE->saveEntity( entity, &pFile );
  157. }
  158. }
  159. file->close();
  160. }
  161. }
  162. int Dimension::getDimensionId() const
  163. {
  164. return dimensionId;
  165. }
  166. bool Dimension::hasChunck( int x, int y ) const
  167. {
  168. return zChunk( Punkt( x, y ) );
  169. }