WorldGenerator.cpp 2.1 KB

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