WorldLoader.cpp 3.2 KB

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