WorldGenerator.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. }