WorldLoader.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. start();
  13. }
  14. WorldLoader::~WorldLoader()
  15. {}
  16. void WorldLoader::thread()
  17. {
  18. while( !exit )
  19. {
  20. cs.Enter();
  21. Area next;
  22. bool hasNext = false;
  23. if( requestQueue.getEintragAnzahl() > 0 )
  24. {
  25. next = requestQueue.get( 0 );
  26. requestQueue.remove( 0 );
  27. }
  28. cs.Leave();
  29. if( !hasNext )
  30. {
  31. sleep( 1 );
  32. continue;
  33. }
  34. Punkt start = zGame->getChunkCenter( next.startX, next.startY );
  35. Punkt end = zGame->getChunkCenter( next.endX, next.endY );
  36. int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 );
  37. int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 );
  38. for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
  39. {
  40. for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
  41. {
  42. if( !zGame->doesChunkExist( x, y, next.dimensionId ) )
  43. {
  44. Datei *file = new Datei();
  45. Text filePath = zGame->getWorldDirectory() + "/dim/" + next.dimensionId + "/";
  46. filePath.appendHex( x );
  47. filePath += "_";
  48. filePath.appendHex( y );
  49. filePath += ".chunk";
  50. file->setDatei( filePath );
  51. if( file->open( Datei::Style::lesen ) )
  52. {
  53. Chunk *chunk = new Chunk( Framework::Punkt( x, y ), zGame, next.dimensionId, file );
  54. zGame->requestWorldUpdate( new AddChunkUpdate( chunk ) );
  55. }
  56. file->close();
  57. file->release();
  58. }
  59. }
  60. }
  61. }
  62. }
  63. void WorldLoader::requestGeneration( Area request )
  64. {
  65. cs.Enter();
  66. requestQueue.add( request );
  67. cs.Leave();
  68. }
  69. void WorldLoader::exitAndWait()
  70. {
  71. exit = 1;
  72. warteAufThread( 10000 );
  73. ende();
  74. }