WorldGenerator.cpp 2.0 KB

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