WorldGenerator.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  70. void WorldGenerator::requestGeneration(Area request)
  71. {
  72. cs.lock();
  73. requestQueue.add(request);
  74. cs.unlock();
  75. }
  76. void WorldGenerator::exitAndWait()
  77. {
  78. exit = 1;
  79. warteAufThread(10000);
  80. ende();
  81. }
  82. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(
  83. Framework::Vec3<int> location, int dimensionId)
  84. {
  85. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)
  86. ->generateBlock(location);
  87. }
  88. bool WorldGenerator::spawnStructure(Framework::Vec3<int> location,
  89. int dimensionId,
  90. std::function<bool(GenerationTemplate* tmpl)> filter)
  91. {
  92. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)
  93. ->spawnStructure(location, filter);
  94. }