WorldGenerator.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "WorldGenerator.h"
  2. #include <Betriebssystem.h>
  3. #include <functional>
  4. #include <Logging.h>
  5. #include "Dimension.h"
  6. #include "Game.h"
  7. #include "JsonUtils.h"
  8. #include "NoiseInterpolator.h"
  9. using namespace Framework;
  10. using namespace Framework::JSON;
  11. using namespace Framework::JSON::Validator;
  12. WorldGenerator::WorldGenerator(int seed)
  13. : Thread(),
  14. exit(0),
  15. seed(seed)
  16. {
  17. setName("World Generator");
  18. Framework::Logging::info()
  19. << "loading world generator configs. Changes at the config files "
  20. "may lead to a sudden change in landscape.";
  21. JSONValidator* configValidator
  22. = JSONValidator::buildForArray()
  23. ->removeInvalidEntries()
  24. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  25. ->getValidator<DimensionGenerator>())
  26. ->finishArray();
  27. loadAllJsonsFromDirectory("data/generator",
  28. [this, configValidator, seed](
  29. Framework::JSON::JSONValue* zValue, Framework::Text path) {
  30. Framework::Logging::info()
  31. << "loading dimension configs from '" << path << "'";
  32. Framework::RCArray<JSONValidationResult> invalidParts;
  33. JSONValue* valid
  34. = configValidator->getValidParts(zValue, &invalidParts);
  35. for (JSONValidationResult* invalidPart : invalidParts)
  36. {
  37. Framework::Logging::error() << invalidPart->getInvalidInfo();
  38. }
  39. if (valid)
  40. {
  41. for (JSONValue* config : *valid->asArray())
  42. {
  43. DimensionGenerator* generator
  44. = Game::INSTANCE->zTypeRegistry()
  45. ->fromJson<DimensionGenerator>(config);
  46. generator->initialize(seed);
  47. dimensionGenerators.add(generator);
  48. }
  49. valid->release();
  50. }
  51. });
  52. configValidator->release();
  53. start();
  54. }
  55. WorldGenerator::~WorldGenerator() {}
  56. Dimension* WorldGenerator::createDimension(int dimensionId)
  57. {
  58. for (DimensionGenerator* generator : dimensionGenerators)
  59. {
  60. if (generator->getDimensionId() == dimensionId)
  61. return generator->createDimension();
  62. }
  63. Framework::Logging::error()
  64. << "no dimension generator found for dimension " << dimensionId;
  65. return 0;
  66. }
  67. DimensionGenerator* WorldGenerator::zGenerator(int dimensionId)
  68. {
  69. for (DimensionGenerator* generator : dimensionGenerators)
  70. {
  71. if (generator->getDimensionId() == dimensionId) return generator;
  72. }
  73. return 0;
  74. }
  75. void WorldGenerator::thread()
  76. {
  77. while (!exit)
  78. {
  79. cs.lock();
  80. Area next;
  81. bool hasNext = 0;
  82. if (requestQueue.getEintragAnzahl() > 0)
  83. {
  84. next = requestQueue.get(0);
  85. requestQueue.remove(0);
  86. hasNext = 1;
  87. }
  88. cs.unlock();
  89. if (!hasNext)
  90. {
  91. Sleep(1000);
  92. continue;
  93. }
  94. Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
  95. Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
  96. int xDir = start.x > end.x ? -1 : 1;
  97. int yDir = start.y > end.y ? -1 : 1;
  98. for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x;
  99. x += CHUNK_SIZE * xDir)
  100. {
  101. for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y;
  102. y += CHUNK_SIZE * yDir)
  103. {
  104. if (!Game::INSTANCE->doesChunkExist(x, y, next.dimensionId))
  105. {
  106. Chunk* generatedChunk
  107. = zGenerator(next.dimensionId)->generateChunk(x, y);
  108. ZeitMesser zm;
  109. zm.messungStart();
  110. generatedChunk->initializeLightning();
  111. zm.messungEnde();
  112. Framework::Logging::trace()
  113. << "light calculation: " << zm.getSekunden();
  114. zm.messungStart();
  115. generatedChunk->removeUnusedBlocks();
  116. zm.messungEnde();
  117. Framework::Logging::trace()
  118. << "unused block removal: " << zm.getSekunden();
  119. zm.messungStart();
  120. Dimension* dim
  121. = Game::INSTANCE->zDimension(next.dimensionId);
  122. if (!dim)
  123. {
  124. dim = new Dimension(next.dimensionId);
  125. Game::INSTANCE->addDimension(dim);
  126. }
  127. dim->setChunk(generatedChunk, Punkt(x, y));
  128. zm.messungEnde();
  129. Framework::Logging::trace()
  130. << "adding chunk to map: " << zm.getSekunden();
  131. }
  132. }
  133. }
  134. }
  135. Framework::Logging::info() << "World Generator thread exited";
  136. }
  137. void WorldGenerator::requestGeneration(Area request)
  138. {
  139. cs.lock();
  140. requestQueue.add(request);
  141. cs.unlock();
  142. }
  143. void WorldGenerator::exitAndWait()
  144. {
  145. exit = 1;
  146. warteAufThread(10000);
  147. ende();
  148. }
  149. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(
  150. Framework::Vec3<int> location, int dimensionId)
  151. {
  152. return zGenerator(dimensionId)->generateBlock(location);
  153. }
  154. bool WorldGenerator::spawnStructure(Framework::Vec3<int> location,
  155. int dimensionId,
  156. std::function<bool(GeneratorTemplate* tmpl)> filter)
  157. {
  158. return zGenerator(dimensionId)->spawnStructure(location, filter);
  159. }