WorldGenerator.cpp 1.9 KB

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