WorldLoader.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <Punkt.h>
  2. #include <Text.h>
  3. #include "WorldLoader.h"
  4. #include "Game.h"
  5. #include "AddChunkUpdate.h"
  6. using namespace Framework;
  7. WorldLoader::WorldLoader()
  8. : Thread(),
  9. exit( 0 )
  10. {
  11. Datei d;
  12. d.setDatei( Game::INSTANCE->getWorldDirectory() + "/dim" );
  13. RCArray<Text>* names = d.getDateiListe();
  14. if( names )
  15. {
  16. for( Text* name : *names )
  17. {
  18. Datei entities;
  19. entities.setDatei( Game::INSTANCE->getWorldDirectory() + "/dim/" + Text( name->getText() ) + "/entities" );
  20. if( entities.open( Datei::Style::lesen ) )
  21. {
  22. Dimension* dim = new Dimension( *name );
  23. while( !entities.istEnde() )
  24. {
  25. int type = 0;
  26. entities.lese( (char*)&type, 4 );
  27. dim->addEntity( StaticRegistry<EntityType>::INSTANCE.zElement( type )->loadEntity( &entities ) );
  28. }
  29. Game::INSTANCE->addDimension( dim );
  30. }
  31. }
  32. names->release();
  33. }
  34. start();
  35. }
  36. WorldLoader::~WorldLoader()
  37. {}
  38. void WorldLoader::thread()
  39. {
  40. while( !exit )
  41. {
  42. cs.lock();
  43. Area next;
  44. bool hasNext = 0;
  45. if( requestQueue.getEintragAnzahl() > 0 )
  46. {
  47. next = requestQueue.get( 0 );
  48. requestQueue.remove( 0 );
  49. hasNext = 1;
  50. }
  51. cs.unlock();
  52. if( !hasNext )
  53. {
  54. Sleep( 1000 );
  55. continue;
  56. }
  57. Punkt start = Game::INSTANCE->getChunkCenter( next.startX, next.startY );
  58. Punkt end = Game::INSTANCE->getChunkCenter( next.endX, next.endY );
  59. int xDir = start.x > end.x ? -1 : 1;
  60. int yDir = start.y > end.y ? -1 : 1;
  61. for( int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir )
  62. {
  63. for( int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir )
  64. {
  65. if( !Game::INSTANCE->isChunkLoaded( x, y, next.dimensionId ) )
  66. {
  67. Datei* file = new Datei();
  68. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + next.dimensionId + "/";
  69. filePath.appendHex( x );
  70. filePath += "_";
  71. filePath.appendHex( y );
  72. filePath += ".chunk";
  73. file->setDatei( filePath );
  74. if( file->open( Datei::Style::lesen ) )
  75. {
  76. Chunk* chunk = new Chunk( Framework::Punkt( x, y ), next.dimensionId, file );
  77. Game::INSTANCE->requestWorldUpdate( new AddChunkUpdate( chunk ) );
  78. }
  79. file->close();
  80. file->release();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. void WorldLoader::requestLoading( Area request )
  87. {
  88. cs.lock();
  89. requestQueue.add( request );
  90. cs.unlock();
  91. }
  92. void WorldLoader::exitAndWait()
  93. {
  94. exit = 1;
  95. warteAufThread( 10000 );
  96. ende();
  97. }
  98. bool WorldLoader::existsChunk( int x, int y, int dimension ) const
  99. {
  100. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimension + "/";
  101. filePath.appendHex( x );
  102. filePath += "_";
  103. filePath.appendHex( y );
  104. filePath += ".chunk";
  105. return DateiExistiert( filePath );
  106. }