WorldGenerator.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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::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. DataValidator* configValidator
  22. = DataValidator::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<ValidationResult> invalidParts;
  33. JSONValue* valid
  34. = configValidator->getValidParts(zValue, &invalidParts);
  35. for (ValidationResult* 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. Framework::JSON::JSONObject* schema = configValidator->getJsonSchema();
  53. Framework::Datei syntaxFile;
  54. syntaxFile.setDatei("data/syntax/schema/generator.json");
  55. syntaxFile.erstellen();
  56. syntaxFile.open(Framework::Datei::Style::schreiben);
  57. syntaxFile.schreibe(schema->toString(), schema->toString().getLength());
  58. syntaxFile.close();
  59. schema->release();
  60. configValidator->release();
  61. start();
  62. }
  63. WorldGenerator::~WorldGenerator() {}
  64. Dimension* WorldGenerator::createDimension(int dimensionId)
  65. {
  66. for (DimensionGenerator* generator : dimensionGenerators)
  67. {
  68. if (generator->getDimensionId() == dimensionId)
  69. return generator->createDimension();
  70. }
  71. Framework::Logging::error()
  72. << "no dimension generator found for dimension " << dimensionId;
  73. return 0;
  74. }
  75. DimensionGenerator* WorldGenerator::zGenerator(int dimensionId)
  76. {
  77. for (DimensionGenerator* generator : dimensionGenerators)
  78. {
  79. if (generator->getDimensionId() == dimensionId) return generator;
  80. }
  81. return 0;
  82. }
  83. void WorldGenerator::thread()
  84. {
  85. while (!exit)
  86. {
  87. cs.lock();
  88. Area next;
  89. bool hasNext = 0;
  90. if (requestQueue.getEintragAnzahl() > 0)
  91. {
  92. next = requestQueue.get(0);
  93. requestQueue.remove(0);
  94. hasNext = 1;
  95. }
  96. cs.unlock();
  97. if (!hasNext)
  98. {
  99. Sleep(1000);
  100. continue;
  101. }
  102. Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
  103. Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
  104. int xDir = start.x > end.x ? -1 : 1;
  105. int yDir = start.y > end.y ? -1 : 1;
  106. for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x;
  107. x += CHUNK_SIZE * xDir)
  108. {
  109. for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y;
  110. y += CHUNK_SIZE * yDir)
  111. {
  112. if (!Game::INSTANCE->doesChunkExist(x, y, next.dimensionId))
  113. {
  114. Chunk* generatedChunk
  115. = zGenerator(next.dimensionId)->generateChunk(x, y);
  116. ZeitMesser zm;
  117. zm.messungStart();
  118. generatedChunk->initializeLightning();
  119. zm.messungEnde();
  120. Framework::Logging::trace()
  121. << "light calculation: " << zm.getSekunden();
  122. zm.messungStart();
  123. generatedChunk->removeUnusedBlocks();
  124. zm.messungEnde();
  125. Framework::Logging::trace()
  126. << "unused block removal: " << zm.getSekunden();
  127. zm.messungStart();
  128. Dimension* dim
  129. = Game::INSTANCE->zDimension(next.dimensionId);
  130. if (!dim)
  131. {
  132. dim = new Dimension(next.dimensionId);
  133. Game::INSTANCE->addDimension(dim);
  134. }
  135. generatedChunk->getThis();
  136. dim->setChunk(generatedChunk, Punkt(x, y));
  137. zGenerator(next.dimensionId)
  138. ->generateEntities(generatedChunk);
  139. generatedChunk->release();
  140. zm.messungEnde();
  141. Framework::Logging::trace()
  142. << "adding chunk to map: " << zm.getSekunden();
  143. }
  144. }
  145. }
  146. }
  147. Framework::Logging::info() << "World Generator thread exited";
  148. }
  149. void WorldGenerator::requestGeneration(Area request)
  150. {
  151. cs.lock();
  152. requestQueue.add(request);
  153. cs.unlock();
  154. }
  155. void WorldGenerator::exitAndWait()
  156. {
  157. exit = 1;
  158. warteAufThread(10000);
  159. ende();
  160. }
  161. Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(
  162. Framework::Vec3<int> location, int dimensionId)
  163. {
  164. return zGenerator(dimensionId)->generateBlock(location);
  165. }
  166. bool WorldGenerator::spawnStructure(Framework::Vec3<int> location,
  167. int dimensionId,
  168. std::function<bool(GeneratorTemplate* tmpl)> filter)
  169. {
  170. return zGenerator(dimensionId)->spawnStructure(location, filter);
  171. }