Dimension.cpp 4.6 KB

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