WorldGenerator.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "WorldGenerator.h"
  2. #include "StaticRegistry.h"
  3. #include "Game.h"
  4. #include "AddChunkUpdate.h"
  5. #include "NoiseInterpolator.h"
  6. using namespace Framework;
  7. WorldGenerator::WorldGenerator( int seed )
  8. : Thread(),
  9. exit( 0 ),
  10. seed( seed )
  11. {
  12. start();
  13. }
  14. WorldGenerator::~WorldGenerator()
  15. {}
  16. void WorldGenerator::thread()
  17. {
  18. while( !exit )
  19. {
  20. cs.lock();
  21. Area next;
  22. bool hasNext = 0;
  23. if( requestQueue.getEintragAnzahl() > 0 )
  24. {
  25. next = requestQueue.get( 0 );
  26. requestQueue.remove( 0 );
  27. hasNext = 1;
  28. }
  29. cs.unlock();
  30. if( !hasNext )
  31. {
  32. sleep( 1 );
  33. continue;
  34. }
  35. Punkt start = Game::INSTANCE->getChunkCenter( next.startX, next.startY );
  36. Punkt end = Game::INSTANCE->getChunkCenter( next.endX, next.endY );
  37. int xDir = start.x > end.x ? -1 : 1;
  38. int yDir = start.y > end.y ? -1 : 1;
  39. for( int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir )
  40. {
  41. for( int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir )
  42. {
  43. if( !Game::INSTANCE->doesChunkExist( x, y, next.dimensionId ) )
  44. {
  45. Chunk* generatedChunk = StaticRegistry<DimensionGenerator>::INSTANCE.zElement( next.dimensionId )->generateChunk( seed, x, y );
  46. generatedChunk->removeUnusedBlocks();
  47. Game::INSTANCE->requestWorldUpdate( new AddChunkUpdate( generatedChunk ) );
  48. }
  49. }
  50. }
  51. }
  52. }
  53. void WorldGenerator::requestGeneration( Area request )
  54. {
  55. cs.lock();
  56. requestQueue.add( request );
  57. cs.unlock();
  58. }
  59. void WorldGenerator::exitAndWait()
  60. {
  61. exit = 1;
  62. warteAufThread( 10000 );
  63. ende();
  64. }
  65. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock( Framework::Vec3<int> location, int dimensionId )
  66. {
  67. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement( dimensionId )->generateBlock( seed, location );
  68. }