Dimension.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. addr[ 8 ] = 0;
  40. }
  41. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  42. {
  43. if( wPos.x < 0 )
  44. wPos.x -= CHUNK_SIZE;
  45. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  46. wPos.y -= CHUNK_SIZE;
  47. wPos /= CHUNK_SIZE;
  48. getAddrOf( wPos, addr );
  49. }
  50. Chunk* Dimension::zChunk( Punkt wPos ) const
  51. {
  52. char addr[ 9 ];
  53. getAddrOfWorld( wPos, addr );
  54. return chunks->z( addr, 9 );
  55. }
  56. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location, const Game* zGame )
  57. {
  58. Chunk* c = zChunk( zGame->getChunkCenter( location.x, location.y ) );
  59. if( c )
  60. {
  61. int x = location.x % CHUNK_SIZE;
  62. int y = location.y % CHUNK_SIZE;
  63. if( x < 0 )
  64. x += CHUNK_SIZE;
  65. if( y < 0 )
  66. y += CHUNK_SIZE;
  67. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  68. }
  69. return 0;
  70. }
  71. void Dimension::addEntity( Entity* entity )
  72. {
  73. entities->add( entity );
  74. }
  75. void Dimension::addChunk( Chunk* chunk )
  76. {
  77. char addr[ 9 ];
  78. getAddrOfWorld( chunk->getCenter(), addr );
  79. if( !chunks->z( addr, 9 ) )
  80. {
  81. chunks->set( addr, 9, chunk );
  82. getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
  83. Chunk* zChunk = chunks->z( addr, 9 );
  84. if( zChunk )
  85. {
  86. zChunk->setNeighbor( WEST, chunk );
  87. chunk->setNeighbor( EAST, chunk );
  88. }
  89. getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
  90. zChunk = chunks->z( addr, 9 );
  91. if( zChunk )
  92. {
  93. zChunk->setNeighbor( EAST, chunk );
  94. chunk->setNeighbor( WEST, chunk );
  95. }
  96. getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
  97. zChunk = chunks->z( addr, 9 );
  98. if( zChunk )
  99. {
  100. zChunk->setNeighbor( NORTH, chunk );
  101. chunk->setNeighbor( SOUTH, chunk );
  102. }
  103. getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
  104. zChunk = chunks->z( addr, 9 );
  105. if( zChunk )
  106. {
  107. zChunk->setNeighbor( SOUTH, chunk );
  108. chunk->setNeighbor( NORTH, chunk );
  109. }
  110. }
  111. else
  112. chunk->release();
  113. }
  114. void Dimension::save( Text worldDir ) const
  115. {
  116. for( auto chunk = chunks->getIterator(); chunk; chunk++ )
  117. {
  118. if( !chunk._ )
  119. continue;
  120. Datei* file = new Datei();
  121. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  122. filePath.appendHex( chunk->getCenter().x );
  123. filePath += "_";
  124. filePath.appendHex( chunk->getCenter().y );
  125. filePath += ".chunk";
  126. file->setDatei( filePath );
  127. if( file->open( Datei::Style::schreiben ) )
  128. chunk->save( file );
  129. file->close();
  130. file->release();
  131. }
  132. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  133. Datei* file = new Datei();
  134. file->setDatei( filePath );
  135. if( file->open( Datei::Style::schreiben ) )
  136. {
  137. for( Entity* entity : *entities )
  138. {
  139. if( entity->zType()->getId() != PlayerEntityType::ID )
  140. {
  141. if( !entity->isRemoved() )
  142. {
  143. int type = entity->zType()->getId();
  144. file->schreibe( (char*)&type, 4 );
  145. StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, file );
  146. }
  147. }
  148. else
  149. {
  150. Datei pFile;
  151. pFile.setDatei( worldDir + "/player/" + ((Player*)entity)->getName() );
  152. if( pFile.open( Datei::Style::schreiben ) )
  153. PlayerEntityType::INSTANCE->saveEntity( entity, &pFile );
  154. }
  155. }
  156. file->close();
  157. }
  158. }
  159. int Dimension::getDimensionId() const
  160. {
  161. return dimensionId;
  162. }
  163. bool Dimension::hasChunck( int x, int y ) const
  164. {
  165. return zChunk( Punkt( x, y ) );
  166. }