WorldGenerator.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "WorldGenerator.h"
  2. #include "StaticRegistry.h"
  3. #include "Game.h"
  4. #include "NoiseInterpolator.h"
  5. #include <functional>
  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(); i++)
  14. StaticRegistry<DimensionGenerator>::INSTANCE.zElement(i)->initialize(seed);
  15. start();
  16. }
  17. WorldGenerator::~WorldGenerator()
  18. {}
  19. void WorldGenerator::thread()
  20. {
  21. while (!exit)
  22. {
  23. cs.lock();
  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.unlock();
  33. if (!hasNext)
  34. {
  35. sleep(1);
  36. continue;
  37. }
  38. Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
  39. Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
  40. int xDir = start.x > end.x ? -1 : 1;
  41. int yDir = start.y > end.y ? -1 : 1;
  42. for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir)
  43. {
  44. for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir)
  45. {
  46. if (!Game::INSTANCE->doesChunkExist(x, y, next.dimensionId))
  47. {
  48. Chunk* generatedChunk = StaticRegistry<DimensionGenerator>::INSTANCE.zElement(next.dimensionId)->generateChunk(x, y);
  49. generatedChunk->initializeLightning();
  50. generatedChunk->removeUnusedBlocks();
  51. Dimension* dim = Game::INSTANCE->zDimension(next.dimensionId);
  52. if (!dim)
  53. {
  54. dim = new Dimension(next.dimensionId);
  55. Game::INSTANCE->addDimension(dim);
  56. }
  57. dim->setChunk(generatedChunk, Punkt(x, y));
  58. }
  59. }
  60. }
  61. }
  62. }
  63. void WorldGenerator::requestGeneration(Area request)
  64. {
  65. cs.lock();
  66. requestQueue.add(request);
  67. cs.unlock();
  68. }
  69. void WorldGenerator::exitAndWait()
  70. {
  71. exit = 1;
  72. warteAufThread(10000);
  73. ende();
  74. }
  75. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(Framework::Vec3<int> location, int dimensionId)
  76. {
  77. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)->generateBlock(location);
  78. }
  79. bool WorldGenerator::spawnStructure(Framework::Vec3<int> location, int dimensionId, std::function<bool(GenerationTemplate* tmpl)> filter)
  80. {
  81. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)->spawnStructure(location, filter);
  82. }