WorldGenerator.cpp 3.0 KB

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