Dimension.cpp 4.5 KB

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