BiomGenerator.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "BiomGenerator.h"
  2. #include "Constants.h"
  3. #include "Game.h"
  4. #include "JNoise.h"
  5. BiomGenerator::BiomGenerator()
  6. : ReferenceCounter(),
  7. condition(0)
  8. {}
  9. BiomGenerator::~BiomGenerator()
  10. {
  11. if (condition) condition->release();
  12. }
  13. void BiomGenerator::initialize(JExpressionMemory* zMemory)
  14. {
  15. for (GeneratorRule* rule : rules)
  16. {
  17. rule->initialize(zMemory);
  18. }
  19. for (StructureTemplateCollection* collection : templates)
  20. {
  21. collection->initialize(zMemory);
  22. }
  23. }
  24. Framework::Either<Block*, int> BiomGenerator::generateBlock(int x,
  25. int y,
  26. int z,
  27. int dimensionId,
  28. JExpressionMemory* zMemory,
  29. Chunk* partialGeneratedChunk)
  30. {
  31. for (GeneratorRule* rule : rules)
  32. {
  33. if (rule->checkCondition(x, y, z, zMemory))
  34. {
  35. auto result = rule->generateBlock(x, y, z, dimensionId);
  36. if ((result.isA() && result.getA())
  37. || (result.isB() && result.getB()))
  38. {
  39. return result;
  40. }
  41. }
  42. }
  43. return BlockTypeEnum::AIR;
  44. }
  45. bool BiomGenerator::isApplicable(JExpressionMemory* zMemory)
  46. {
  47. return condition->getValue(zMemory);
  48. }
  49. void BiomGenerator::generateStructures(int x,
  50. int y,
  51. int z,
  52. int dimensionId,
  53. JExpressionMemory* zMemory,
  54. Framework::Vec3<int> minPos,
  55. Framework::Vec3<int> maxPos,
  56. Framework::RCArray<GeneratedStructure>* zResult)
  57. {
  58. int minSearchX = minPos.x - maxStructureOffset.x;
  59. int minSearchY = minPos.y - maxStructureOffset.y;
  60. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  61. int maxSearchX = maxPos.x - minStructureOffset.x;
  62. int maxSearchY = maxPos.y - minStructureOffset.y;
  63. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  64. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY
  65. && z >= minSearchZ && z <= maxSearchZ)
  66. {
  67. for (StructureTemplateCollection* collection : templates)
  68. {
  69. collection->generateStructures(
  70. x, y, z, dimensionId, zMemory, minPos, maxPos, zResult);
  71. }
  72. }
  73. }
  74. const Framework::RCArray<StructureTemplateCollection>&
  75. BiomGenerator::getTemplates() const
  76. {
  77. return templates;
  78. }
  79. Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
  80. {
  81. return minStructureOffset;
  82. }
  83. Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
  84. {
  85. return maxStructureOffset;
  86. }
  87. void BiomGenerator::setName(Framework::Text name)
  88. {
  89. this->name = name;
  90. }
  91. Framework::Text BiomGenerator::getName() const
  92. {
  93. return name;
  94. }
  95. void BiomGenerator::setCondition(JBoolExpression* condition)
  96. {
  97. if (this->condition) this->condition->release();
  98. this->condition = condition;
  99. }
  100. JBoolExpression* BiomGenerator::getCondition() const
  101. {
  102. return condition;
  103. }
  104. void BiomGenerator::addTemplate(StructureTemplateCollection* collection)
  105. {
  106. templates.add(collection);
  107. Framework::Vec3<int> min = collection->getMinAffected();
  108. Framework::Vec3<int> max = collection->getMaxAffected();
  109. if (templates.getEintragAnzahl())
  110. {
  111. minStructureOffset = min;
  112. maxStructureOffset = max;
  113. }
  114. else
  115. {
  116. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  117. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  118. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  119. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  120. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  121. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  122. }
  123. }
  124. const Framework::RCArray<StructureTemplateCollection>&
  125. BiomGenerator::getTemplates()
  126. {
  127. return templates;
  128. }
  129. void BiomGenerator::addGeneratorRule(GeneratorRule* rule)
  130. {
  131. rules.add(rule);
  132. }
  133. const Framework::RCArray<GeneratorRule>&
  134. BiomGenerator::getGeneratorRules() const
  135. {
  136. return rules;
  137. }
  138. BiomGeneratorFactory::BiomGeneratorFactory()
  139. : TypeFactory()
  140. {}
  141. BiomGenerator* BiomGeneratorFactory::createValue(
  142. Framework::JSON::JSONObject* zJson) const
  143. {
  144. return new BiomGenerator();
  145. }
  146. void BiomGeneratorFactory::fromJson(
  147. BiomGenerator* zResult, Framework::JSON::JSONObject* zJson) const
  148. {
  149. zResult->setName(zJson->zValue("name")->asString()->getString());
  150. zResult->setCondition(
  151. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  152. zJson->zValue("condition")));
  153. bool first = 1;
  154. for (Framework::JSON::JSONValue* value :
  155. *zJson->zValue("structurCollections")->asArray())
  156. {
  157. StructureTemplateCollection* collection
  158. = Game::INSTANCE->zTypeRegistry()
  159. ->fromJson<StructureTemplateCollection>(value);
  160. zResult->addTemplate(collection);
  161. }
  162. for (Framework::JSON::JSONValue* value :
  163. *zJson->asObject()->zValue("blocks")->asArray())
  164. {
  165. zResult->addGeneratorRule(
  166. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
  167. }
  168. }
  169. void BiomGeneratorFactory::toJson(
  170. BiomGenerator* zObject, Framework::JSON::JSONObject* zResult) const
  171. {
  172. zResult->addValue(
  173. "name", new Framework::JSON::JSONString(zObject->getName()));
  174. zResult->addValue("condition",
  175. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  176. zObject->getCondition()));
  177. Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
  178. for (StructureTemplateCollection* collection : zObject->getTemplates())
  179. {
  180. collections->addValue(
  181. Game::INSTANCE->zTypeRegistry()
  182. ->toJson<StructureTemplateCollection>(collection));
  183. }
  184. zResult->addValue("structurCollections", collections);
  185. Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
  186. for (GeneratorRule* rule : zObject->getGeneratorRules())
  187. {
  188. rules->addValue(
  189. Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
  190. }
  191. zResult->addValue("blocks", rules);
  192. }
  193. JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
  194. JSONObjectValidationBuilder* builder) const
  195. {
  196. return builder->withRequiredString("name")
  197. ->finishString()
  198. ->withRequiredAttribute("condition",
  199. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  200. ->withRequiredArray("structurCollections")
  201. ->addAcceptedTypeInArray(
  202. Game::INSTANCE->zTypeRegistry()
  203. ->getValidator<StructureTemplateCollection>())
  204. ->finishArray()
  205. ->withRequiredArray("blocks")
  206. ->addAcceptedTypeInArray(
  207. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
  208. ->finishArray();
  209. }