StructureCollection.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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::parseNoisse(
  9. zConfig->asObject()->zValue("activeNoise"), zMemory);
  10. structureNoise = JNoise::parseNoisse(
  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 (condition->getValue(zMemory))
  67. {
  68. if (activeNoise->getNoise((double)x, (double)y, (double)z)
  69. < threshold)
  70. {
  71. double rValue
  72. = structureNoise->getNoise((double)x, (double)y, (double)z);
  73. double probSum = 0;
  74. for (auto t : structures)
  75. {
  76. if (rValue - probSum <= t->getPropability())
  77. {
  78. zResult->add(
  79. t->generateAt(Framework::Vec3<int>(x, y, z),
  80. structureNoise,
  81. dimensionId));
  82. break;
  83. }
  84. probSum += t->getPropability();
  85. }
  86. }
  87. }
  88. }
  89. }
  90. const Framework::RCArray<GeneratorTemplate>&
  91. StructureTemplateCollection::getStructures() const
  92. {
  93. return structures;
  94. }
  95. Framework::Vec3<int> StructureTemplateCollection::getMinAffected() const
  96. {
  97. return minAffected;
  98. }
  99. Framework::Vec3<int> StructureTemplateCollection::getMaxAffected() const
  100. {
  101. return maxAffected;
  102. }
  103. Framework::JSON::Validator::JSONValidator*
  104. StructureTemplateCollection::getConfigValidator()
  105. {
  106. return Framework::JSON::Validator::JSONValidator::buildForObject()
  107. ->withRequiredAttribute("activeNoise", JNoise::getValidator(false))
  108. ->withRequiredAttribute("structureNoise", JNoise::getValidator(false))
  109. ->withRequiredNumber("threshold")
  110. ->whichIsGreaterThen(0)
  111. ->finishNumber()
  112. ->withRequiredAttribute(
  113. "condition", JExpressionParser::getBoolValidator())
  114. ->withRequiredArray("structures")
  115. ->addAcceptedTypeInArray(
  116. Game::INSTANCE->zTypeRegistry()->getGeneratorTemplateValidator())
  117. ->finishArray()
  118. ->finishObject();
  119. }