WorldGenerator.cpp 3.6 KB

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