WorldLoader.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = 0;
  46. if( requestQueue.getEintragAnzahl() > 0 )
  47. {
  48. next = requestQueue.get( 0 );
  49. requestQueue.remove( 0 );
  50. hasNext = 1;
  51. }
  52. cs.Leave();
  53. if( !hasNext )
  54. {
  55. Sleep( 1000 );
  56. continue;
  57. }
  58. Punkt start = zGame->getChunkCenter( next.startX, next.startY );
  59. Punkt end = zGame->getChunkCenter( next.endX, next.endY );
  60. int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 );
  61. int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 );
  62. for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
  63. {
  64. for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
  65. {
  66. if( !zGame->isChunkLoaded( x, y, next.dimensionId ) )
  67. {
  68. Datei *file = new Datei();
  69. Text filePath = zGame->getWorldDirectory() + "/dim/" + next.dimensionId + "/";
  70. filePath.appendHex( x );
  71. filePath += "_";
  72. filePath.appendHex( y );
  73. filePath += ".chunk";
  74. file->setDatei( filePath );
  75. if( file->open( Datei::Style::lesen ) )
  76. {
  77. Chunk *chunk = new Chunk( Framework::Punkt( x, y ), zGame, next.dimensionId, file );
  78. zGame->requestWorldUpdate( new AddChunkUpdate( chunk ) );
  79. }
  80. file->close();
  81. file->release();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void WorldLoader::requestLoading( Area request )
  88. {
  89. cs.Enter();
  90. requestQueue.add( request );
  91. cs.Leave();
  92. }
  93. void WorldLoader::exitAndWait()
  94. {
  95. exit = 1;
  96. warteAufThread( 10000 );
  97. ende();
  98. }
  99. bool WorldLoader::existsChunk( int x, int y, int dimension ) const
  100. {
  101. Text filePath = zGame->getWorldDirectory() + "/dim/" + dimension + "/";
  102. filePath.appendHex( x );
  103. filePath += "_";
  104. filePath.appendHex( y );
  105. filePath += ".chunk";
  106. return DateiExistiert( filePath );
  107. }