Dimension.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. gravity( 9.8f ),
  9. chunks( new Trie<Chunk>() ),
  10. entities( new RCArray<Entity>() )
  11. {}
  12. Dimension::~Dimension()
  13. {
  14. entities->release();
  15. chunks->release();
  16. }
  17. void Dimension::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  18. {
  19. // TODO: switch type chunck, block, entity
  20. }
  21. void Dimension::tickEntities()
  22. {
  23. for( auto entity : *entities )
  24. {
  25. if( zChunk( Punkt( (int)entity->getPosition().x, (int)entity->getPosition().y ) ) )
  26. entity->prepareTick( this );
  27. }
  28. int index = 0;
  29. Array<int> removed;
  30. for( auto entity : *entities )
  31. {
  32. if( zChunk( Punkt( (int)entity->getPosition().x, (int)entity->getPosition().y ) ) )
  33. entity->tick( this );
  34. if( entity->isRemoved() )
  35. removed.add( index, 0 );
  36. index++;
  37. }
  38. for( int i : removed )
  39. entities->remove( i );
  40. }
  41. void Dimension::getAddrOf( Punkt cPos, char* addr ) const
  42. {
  43. *(int*)addr = cPos.x;
  44. *((int*)addr + 1) = cPos.y;
  45. }
  46. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  47. {
  48. if( wPos.x < 0 )
  49. wPos.x -= CHUNK_SIZE;
  50. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  51. wPos.y -= CHUNK_SIZE;
  52. wPos /= CHUNK_SIZE;
  53. getAddrOf( wPos, addr );
  54. }
  55. Chunk* Dimension::zChunk( Punkt wPos ) const
  56. {
  57. char addr[ 8 ];
  58. getAddrOfWorld( wPos, addr );
  59. return chunks->z( addr, 8 );
  60. }
  61. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
  62. {
  63. Chunk* c = zChunk( Game::INSTANCE->getChunkCenter( location.x, location.y ) );
  64. if( c )
  65. {
  66. int x = location.x % CHUNK_SIZE;
  67. int y = location.y % CHUNK_SIZE;
  68. if( x < 0 )
  69. x += CHUNK_SIZE;
  70. if( y < 0 )
  71. y += CHUNK_SIZE;
  72. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  73. }
  74. return 0;
  75. }
  76. Block* Dimension::zRealBlockInstance( Framework::Vec3<int> location )
  77. {
  78. Chunk* c = zChunk( Game::INSTANCE->getChunkCenter( location.x, location.y ) );
  79. if( c )
  80. {
  81. int x = location.x % CHUNK_SIZE;
  82. int y = location.y % CHUNK_SIZE;
  83. if( x < 0 )
  84. x += CHUNK_SIZE;
  85. if( y < 0 )
  86. y += CHUNK_SIZE;
  87. c->instantiateBlock( Vec3<int>( x, y, location.z ) );
  88. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  89. }
  90. return 0;
  91. }
  92. void Dimension::placeBlock( Block* block )
  93. {
  94. Chunk* c = zChunk( Game::getChunkCenter( block->getPos().x, block->getPos().y ) );
  95. if( c )
  96. {
  97. int x = block->getPos().x % CHUNK_SIZE;
  98. int y = block->getPos().y % CHUNK_SIZE;
  99. if( x < 0 )
  100. x += CHUNK_SIZE;
  101. if( y < 0 )
  102. y += CHUNK_SIZE;
  103. c->putBlockAt( block->getPos(), block );
  104. }
  105. else
  106. block->release();
  107. }
  108. void Dimension::addEntity( Entity* entity )
  109. {
  110. entities->add( entity );
  111. }
  112. void Dimension::setChunk( Chunk* chunk, Punkt center )
  113. {
  114. char addr[ 8 ];
  115. getAddrOfWorld( center, addr );
  116. Chunk* old = chunks->z( addr, 8 );
  117. if( old )
  118. {
  119. for( int i = 0; i < chunkList.getEintragAnzahl(); i++ )
  120. {
  121. if( chunkList.get( i ) == old )
  122. {
  123. chunkList.remove( i );
  124. break;
  125. }
  126. }
  127. }
  128. chunks->set( addr, 8, chunk );
  129. if( chunk )
  130. {
  131. chunkList.add( chunk );
  132. chunk->setAdded();
  133. }
  134. getAddrOfWorld( center + Punkt( CHUNK_SIZE, 0 ), addr );
  135. Chunk* zChunk = chunks->z( addr, 8 );
  136. if( zChunk )
  137. {
  138. zChunk->setNeighbor( WEST, chunk );
  139. if( chunk )
  140. chunk->setNeighbor( EAST, zChunk );
  141. }
  142. getAddrOfWorld( center + Punkt( -CHUNK_SIZE, 0 ), addr );
  143. zChunk = chunks->z( addr, 8 );
  144. if( zChunk )
  145. {
  146. zChunk->setNeighbor( EAST, chunk );
  147. if( chunk )
  148. chunk->setNeighbor( WEST, zChunk );
  149. }
  150. getAddrOfWorld( center + Punkt( 0, CHUNK_SIZE ), addr );
  151. zChunk = chunks->z( addr, 8 );
  152. if( zChunk )
  153. {
  154. zChunk->setNeighbor( NORTH, chunk );
  155. if( chunk )
  156. chunk->setNeighbor( SOUTH, zChunk );
  157. }
  158. getAddrOfWorld( center + Punkt( 0, -CHUNK_SIZE ), addr );
  159. zChunk = chunks->z( addr, 8 );
  160. if( zChunk )
  161. {
  162. zChunk->setNeighbor( SOUTH, chunk );
  163. if( chunk )
  164. chunk->setNeighbor( NORTH, zChunk );
  165. }
  166. }
  167. void Dimension::save( Text worldDir ) const
  168. {
  169. for( auto chunk = chunks->getIterator(); chunk; chunk++ )
  170. {
  171. if( !chunk._ )
  172. continue;
  173. Datei* file = new Datei();
  174. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  175. filePath.appendHex( chunk->getCenter().x );
  176. filePath += "_";
  177. filePath.appendHex( chunk->getCenter().y );
  178. filePath += ".chunk";
  179. file->setDatei( filePath );
  180. if( file->open( Datei::Style::schreiben ) )
  181. chunk->save( file, StreamTarget::FULL );
  182. file->close();
  183. file->release();
  184. }
  185. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  186. Datei* file = new Datei();
  187. file->setDatei( filePath );
  188. if( file->open( Datei::Style::schreiben ) )
  189. {
  190. for( Entity* entity : *entities )
  191. {
  192. if( entity->zType()->getId() != PlayerEntityType::ID )
  193. {
  194. if( !entity->isRemoved() )
  195. {
  196. int type = entity->zType()->getId();
  197. file->schreibe( (char*)&type, 4 );
  198. StaticRegistry<EntityType>::INSTANCE.zElement( type )->saveEntity( entity, file );
  199. }
  200. }
  201. else
  202. {
  203. Datei pFile;
  204. pFile.setDatei( worldDir + "/player/" + ((Player*)entity)->getName() );
  205. if( pFile.open( Datei::Style::schreiben ) )
  206. PlayerEntityType::INSTANCE->saveEntity( entity, &pFile );
  207. }
  208. }
  209. file->close();
  210. }
  211. }
  212. int Dimension::getDimensionId() const
  213. {
  214. return dimensionId;
  215. }
  216. bool Dimension::hasChunck( int x, int y ) const
  217. {
  218. return zChunk( Punkt( x, y ) );
  219. }
  220. float Dimension::getGravity() const
  221. {
  222. return gravity;
  223. }
  224. void Dimension::removeOldChunks()
  225. {
  226. Array<int> removed;
  227. int index = 0;
  228. for( Chunk* chunk : chunkList )
  229. {
  230. if( !chunk->hasViews() )
  231. removed.add( index, 0 );
  232. index++;
  233. }
  234. for( int i : removed )
  235. {
  236. Chunk* chunk = chunkList.get( i );
  237. // TODO: save chunk
  238. chunk->prepareRemove();
  239. setChunk( 0, chunk->getCenter() );
  240. }
  241. }