Dimension.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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->getIterator(); entity; entity++, index++ )
  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. }
  31. for( auto i = removed.getIterator(); i; i++ )
  32. entities->remove( i );
  33. }
  34. void Dimension::getAddrOf( Punkt cPos, char *addr ) const
  35. {
  36. *(int *)addr = cPos.x;
  37. *( (int *)addr + 1 ) = cPos.y;
  38. addr[ 8 ] = 0;
  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[ 9 ];
  52. getAddrOfWorld( wPos, addr );
  53. return chunks->z( addr, 9 );
  54. }
  55. Block *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[ 9 ];
  77. getAddrOfWorld( chunk->getCenter(), addr );
  78. if( !chunks->z( addr, 9 ) )
  79. {
  80. chunks->set( addr, 9, chunk );
  81. getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
  82. Chunk *zChunk = chunks->z( addr, 9 );
  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, 9 );
  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, 9 );
  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, 9 );
  104. if( zChunk )
  105. {
  106. zChunk->setNeighbor( SOUTH, chunk );
  107. chunk->setNeighbor( NORTH, chunk );
  108. }
  109. }
  110. else
  111. chunk->release();
  112. }
  113. void Dimension::save( Text worldDir ) const
  114. {
  115. for( auto chunk = chunks->getIterator(); chunk; chunk++ )
  116. {
  117. if( !chunk._ )
  118. continue;
  119. Datei *file = new Datei();
  120. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  121. filePath.appendHex( chunk->getCenter().x );
  122. filePath += "_";
  123. filePath.appendHex( chunk->getCenter().y );
  124. filePath += ".chunk";
  125. file->setDatei( filePath );
  126. if( file->open( Datei::Style::schreiben ) )
  127. chunk->save( file );
  128. file->close();
  129. file->release();
  130. }
  131. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  132. Datei *file = new Datei();
  133. file->setDatei( filePath );
  134. if( file->open( Datei::Style::schreiben ) )
  135. {
  136. for( auto entity = entities->getIterator(); entity; entity++ )
  137. {
  138. if( entity->zType()->getId() != PlayerEntityType::ID )
  139. {
  140. if( !entity->isRemoved() )
  141. {
  142. int type = entity->zType()->getId();
  143. file->schreibe( (char *)&type, 4 );
  144. StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, file );
  145. }
  146. }
  147. else
  148. {
  149. Datei pFile;
  150. pFile.setDatei( worldDir + "/player/" + ( (Player *)entity.val() )->getName() );
  151. if( pFile.open( Datei::Style::schreiben ) )
  152. PlayerEntityType::INSTANCE->saveEntity( entity, &pFile );
  153. }
  154. }
  155. file->close();
  156. }
  157. }
  158. int Dimension::getDimensionId() const
  159. {
  160. return dimensionId;
  161. }
  162. bool Dimension::hasChunck( int x, int y ) const
  163. {
  164. return zChunk( Punkt( x, y ) );
  165. }