StructureCollection.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "StructureCollection.h"
  2. #include "Game.h"
  3. #include "JNoise.h"
  4. StructureTemplateCollection::StructureTemplateCollection(
  5. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  6. : ReferenceCounter()
  7. {
  8. activeNoise = JNoise::parseNoise(
  9. zConfig->asObject()->zValue("activeNoise"), zMemory);
  10. structureNoise = JNoise::parseNoise(
  11. zConfig->asObject()->zValue("structureNoise"), zMemory);
  12. threshold
  13. = zConfig->asObject()->zValue("threshold")->asNumber()->getNumber();
  14. condition = JExpressionParser::parseBoolExpression(
  15. zConfig->asObject()->zValue("condition"));
  16. bool first = 1;
  17. for (Framework::JSON::JSONValue* value :
  18. *zConfig->asObject()->zValue("structures")->asArray())
  19. {
  20. GeneratorTemplate* templ
  21. = Game::INSTANCE->zTypeRegistry()->createGeneratorTemplate(value);
  22. if (first)
  23. {
  24. minAffected = templ->getMinAffectedOffset();
  25. maxAffected = templ->getMaxAffectedOffset();
  26. first = 0;
  27. }
  28. else
  29. {
  30. Framework::Vec3<int> min = templ->getMinAffectedOffset();
  31. Framework::Vec3<int> max = templ->getMaxAffectedOffset();
  32. if (minAffected.x > min.x) minAffected.x = min.x;
  33. if (minAffected.y > min.y) minAffected.y = min.y;
  34. if (minAffected.z > min.z) minAffected.z = min.z;
  35. if (maxAffected.x < max.x) maxAffected.x = max.x;
  36. if (maxAffected.y < max.y) maxAffected.y = max.y;
  37. if (maxAffected.z < max.z) maxAffected.z = max.z;
  38. }
  39. structures.add(templ);
  40. }
  41. }
  42. StructureTemplateCollection::~StructureTemplateCollection()
  43. {
  44. activeNoise->release();
  45. structureNoise->release();
  46. condition->release();
  47. }
  48. void StructureTemplateCollection::generateStructures(int x,
  49. int y,
  50. int z,
  51. int dimensionId,
  52. JExpressionMemory* zMemory,
  53. Framework::Vec3<int> minPos,
  54. Framework::Vec3<int> maxPos,
  55. Framework::RCArray<GeneratedStructure>* zResult)
  56. {
  57. int minSearchX = minPos.x - maxAffected.x;
  58. int minSearchY = minPos.y - maxAffected.y;
  59. int minSearchZ = MAX(minPos.z - maxAffected.z, 0);
  60. int maxSearchX = maxPos.x - minAffected.x;
  61. int maxSearchY = maxPos.y - minAffected.y;
  62. int maxSearchZ = MIN(maxPos.z - minAffected.z, WORLD_HEIGHT - 1);
  63. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY
  64. && z >= minSearchZ && z <= maxSearchZ)
  65. {
  66. if (activeNoise->getNoise((double)x, (double)y, (double)z) < threshold)
  67. {
  68. if (condition->getValue(zMemory))
  69. {
  70. double rValue
  71. = structureNoise->getNoise((double)x, (double)y, (double)z);
  72. double probSum = 0;
  73. for (auto t : structures)
  74. {
  75. if (rValue - probSum <= t->getPropability())
  76. {
  77. zResult->add(
  78. t->generateAt(Framework::Vec3<int>(x, y, z),
  79. structureNoise,
  80. dimensionId));
  81. break;
  82. }
  83. probSum += t->getPropability();
  84. }
  85. }
  86. }
  87. }
  88. }
  89. const Framework::RCArray<GeneratorTemplate>&
  90. StructureTemplateCollection::getStructures() const
  91. {
  92. return structures;
  93. }
  94. Framework::Vec3<int> StructureTemplateCollection::getMinAffected() const
  95. {
  96. return minAffected;
  97. }
  98. Framework::Vec3<int> StructureTemplateCollection::getMaxAffected() const
  99. {
  100. return maxAffected;
  101. }
  102. Framework::JSON::Validator::JSONValidator*
  103. StructureTemplateCollection::getConfigValidator()
  104. {
  105. return Framework::JSON::Validator::JSONValidator::buildForObject()
  106. ->withRequiredAttribute("activeNoise", JNoise::getValidator(false))
  107. ->withRequiredAttribute("structureNoise", JNoise::getValidator(false))
  108. ->withRequiredNumber("threshold")
  109. ->whichIsGreaterThen(0)
  110. ->finishNumber()
  111. ->withRequiredAttribute(
  112. "condition", JExpressionParser::getBoolValidator())
  113. ->withRequiredArray("structures")
  114. ->addAcceptedTypeInArray(
  115. Game::INSTANCE->zTypeRegistry()->getGeneratorTemplateValidator())
  116. ->finishArray()
  117. ->finishObject();
  118. }