WorldGenerator.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = false;
  26. if( requestQueue.getEintragAnzahl() > 0 )
  27. {
  28. next = requestQueue.get( 0 );
  29. requestQueue.remove( 0 );
  30. }
  31. cs.Leave();
  32. if( !hasNext )
  33. {
  34. sleep( 1 );
  35. continue;
  36. }
  37. Punkt start = zGame->getChunkCenter( next.startX, next.startY );
  38. Punkt end = zGame->getChunkCenter( next.endX, next.endY );
  39. int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 );
  40. int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 );
  41. for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
  42. {
  43. for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
  44. {
  45. if( !zGame->doesChunkExist( x, y, next.dimensionId ) )
  46. {
  47. Chunk *generatedChunk = StaticRegistry<DimensionGenerator>::INSTANCE.zElement( next.dimensionId )->generateChunk( noise, zGame, x, y );
  48. zGame->requestWorldUpdate( new AddChunkUpdate( generatedChunk ) );
  49. }
  50. }
  51. }
  52. }
  53. }
  54. void WorldGenerator::requestGeneration( Area request )
  55. {
  56. cs.Enter();
  57. requestQueue.add( request );
  58. cs.Leave();
  59. }
  60. void WorldGenerator::exitAndWait()
  61. {
  62. exit = 1;
  63. warteAufThread( 10000 );
  64. ende();
  65. }