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( 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. if( names )
  16. {
  17. for( auto name = names->getIterator(); name; name++ )
  18. {
  19. Datei entities;
  20. entities.setDatei( zGame->getWorldDirectory() + "/dim/" + Text( name->getText() ) + "/entities" );
  21. if( entities.open( Datei::Style::lesen ) )
  22. {
  23. Dimension *dim = new Dimension( *name.val() );
  24. while( !entities.istEnde() )
  25. {
  26. int type = 0;
  27. entities.lese( (char *)&type, 4 );
  28. dim->addEntity( StaticRegistry<EntityType>::INSTANCE.zElement( type )->loadEntity( zGame, &entities ) );
  29. }
  30. zGame->addDimension( dim );
  31. }
  32. }
  33. names->release();
  34. }
  35. start();
  36. }
  37. WorldLoader::~WorldLoader()
  38. {}
  39. void WorldLoader::thread()
  40. {
  41. while( !exit )
  42. {
  43. cs.Enter();
  44. Area next;
  45. bool hasNext = false;
  46. if( requestQueue.getEintragAnzahl() > 0 )
  47. {
  48. next = requestQueue.get( 0 );
  49. requestQueue.remove( 0 );
  50. }
  51. cs.Leave();
  52. if( !hasNext )
  53. {
  54. Sleep( 1000 );
  55. continue;
  56. }
  57. Punkt start = zGame->getChunkCenter( next.startX, next.startY );
  58. Punkt end = zGame->getChunkCenter( next.endX, next.endY );
  59. int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 );
  60. int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 );
  61. for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
  62. {
  63. for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
  64. {
  65. if( !zGame->doesChunkExist( x, y, next.dimensionId ) )
  66. {
  67. Datei *file = new Datei();
  68. Text filePath = zGame->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 ), zGame, next.dimensionId, file );
  77. zGame->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.Enter();
  89. requestQueue.add( request );
  90. cs.Leave();
  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 = zGame->getWorldDirectory() + "/dim/" + dimension + "/";
  101. filePath.appendHex( x );
  102. filePath += "_";
  103. filePath.appendHex( y );
  104. filePath += ".chunk";
  105. return DateiExistiert( filePath );
  106. }