WorldGenerator.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. zGame->requestWorldUpdate( new AddChunkUpdate( generatedChunk ) );
  50. }
  51. }
  52. }
  53. }
  54. }
  55. void WorldGenerator::requestGeneration( Area request )
  56. {
  57. cs.Enter();
  58. requestQueue.add( request );
  59. cs.Leave();
  60. }
  61. void WorldGenerator::exitAndWait()
  62. {
  63. exit = 1;
  64. warteAufThread( 10000 );
  65. ende();
  66. }