BiomGenerator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "BiomGenerator.h"
  2. #include "Game.h"
  3. #include "JNoise.h"
  4. BiomGenerator::BiomGenerator(
  5. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  6. : ReferenceCounter()
  7. {
  8. name = zConfig->asObject()->zValue("name")->asString()->getString();
  9. condition = JExpressionParser::parseBoolExpression(
  10. zConfig->asObject()->zValue("condition"));
  11. bool first = 1;
  12. for (Framework::JSON::JSONValue* value :
  13. *zConfig->asObject()->zValue("structurCollections")->asArray())
  14. {
  15. StructureTemplateCollection* collection
  16. = new StructureTemplateCollection(value, zMemory);
  17. templates.add(collection);
  18. if (first)
  19. {
  20. minStructureOffset = collection->getMinAffected();
  21. maxStructureOffset = collection->getMaxAffected();
  22. first = 0;
  23. }
  24. else
  25. {
  26. Framework::Vec3<int> min = collection->getMinAffected();
  27. Framework::Vec3<int> max = collection->getMaxAffected();
  28. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  29. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  30. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  31. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  32. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  33. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  34. }
  35. }
  36. for (Framework::JSON::JSONValue* value :
  37. *zConfig->asObject()->zValue("blocks")->asArray())
  38. {
  39. rules.add(Game::INSTANCE->zTypeRegistry()->createGeneratorRule(
  40. value, zMemory));
  41. }
  42. }
  43. Framework::Either<Block*, int> BiomGenerator::generateBlock(int x,
  44. int y,
  45. int z,
  46. int dimensionId,
  47. JExpressionMemory* zMemory,
  48. Chunk* partialGeneratedChunk)
  49. {
  50. for (GeneratorRule* rule : rules)
  51. {
  52. if (rule->checkCondition(zMemory))
  53. {
  54. auto result = rule->generateBlock(x, y, z, dimensionId);
  55. if ((result.isA() && result.getA())
  56. || (result.isB() && result.getB()))
  57. {
  58. return result;
  59. }
  60. }
  61. }
  62. return BlockTypeEnum::AIR;
  63. }
  64. bool BiomGenerator::isApplicable(JExpressionMemory* zMemory)
  65. {
  66. return condition->getValue(zMemory);
  67. }
  68. void BiomGenerator::generateStructures(int x,
  69. int y,
  70. int z,
  71. int dimensionId,
  72. JExpressionMemory* zMemory,
  73. Framework::Vec3<int> minPos,
  74. Framework::Vec3<int> maxPos,
  75. Framework::RCArray<GeneratedStructure>* zResult)
  76. {
  77. int minSearchX = minPos.x - maxStructureOffset.x;
  78. int minSearchY = minPos.y - maxStructureOffset.y;
  79. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  80. int maxSearchX = maxPos.x - minStructureOffset.x;
  81. int maxSearchY = maxPos.y - minStructureOffset.y;
  82. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  83. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY
  84. && z >= minSearchZ && z <= maxSearchZ)
  85. {
  86. for (StructureTemplateCollection* collection : templates)
  87. {
  88. collection->generateStructures(
  89. x, y, z, dimensionId, zMemory, minPos, maxPos, zResult);
  90. }
  91. }
  92. }
  93. const Framework::RCArray<StructureTemplateCollection>&
  94. BiomGenerator::getTemplates() const
  95. {
  96. return templates;
  97. }
  98. Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
  99. {
  100. return minStructureOffset;
  101. }
  102. Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
  103. {
  104. return maxStructureOffset;
  105. }
  106. Framework::JSON::Validator::JSONValidator* BiomGenerator::getConfigValidator()
  107. {
  108. return Framework::JSON::Validator::JSONValidator::buildForObject()
  109. ->withRequiredString("name")
  110. ->finishString()
  111. ->withRequiredAttribute(
  112. "condition", JExpressionParser::getBoolValidator())
  113. ->withRequiredArray("structurCollections")
  114. ->addAcceptedTypeInArray(
  115. StructureTemplateCollection::getConfigValidator())
  116. ->finishArray()
  117. ->withRequiredArray("blocks")
  118. ->addAcceptedTypeInArray(
  119. Game::INSTANCE->zTypeRegistry()->getGeneratorRuleValidator())
  120. ->finishArray()
  121. ->finishObject();
  122. }