WorldGenerator.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "WorldGenerator.h"
  2. #include "StaticRegistry.h"
  3. #include "Game.h"
  4. #include "AddChunkUpdate.h"
  5. #include "NoiseInterpolator.h"
  6. using namespace Framework;
  7. WorldGenerator::WorldGenerator(int seed)
  8. : Thread(),
  9. exit(0),
  10. seed(seed)
  11. {
  12. setName("World Generator");
  13. start();
  14. }
  15. WorldGenerator::~WorldGenerator()
  16. {}
  17. void WorldGenerator::thread()
  18. {
  19. while (!exit)
  20. {
  21. cs.lock();
  22. Area next;
  23. bool hasNext = 0;
  24. if (requestQueue.getEintragAnzahl() > 0)
  25. {
  26. next = requestQueue.get(0);
  27. requestQueue.remove(0);
  28. hasNext = 1;
  29. }
  30. cs.unlock();
  31. if (!hasNext)
  32. {
  33. sleep(1);
  34. continue;
  35. }
  36. Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
  37. Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
  38. int xDir = start.x > end.x ? -1 : 1;
  39. int yDir = start.y > end.y ? -1 : 1;
  40. for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir)
  41. {
  42. for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir)
  43. {
  44. if (!Game::INSTANCE->doesChunkExist(x, y, next.dimensionId))
  45. {
  46. Chunk* generatedChunk = StaticRegistry<DimensionGenerator>::INSTANCE.zElement(next.dimensionId)->generateChunk(seed, x, y);
  47. generatedChunk->removeUnusedBlocks();
  48. Game::INSTANCE->requestWorldUpdate(new AddChunkUpdate(generatedChunk));
  49. }
  50. }
  51. }
  52. }
  53. }
  54. void WorldGenerator::requestGeneration(Area request)
  55. {
  56. cs.lock();
  57. requestQueue.add(request);
  58. cs.unlock();
  59. }
  60. void WorldGenerator::exitAndWait()
  61. {
  62. exit = 1;
  63. warteAufThread(10000);
  64. ende();
  65. }
  66. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(Framework::Vec3<int> location, int dimensionId)
  67. {
  68. return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)->generateBlock(seed, location);
  69. }