GeneratorRule.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "GeneratorRule.h"
  2. #include "JNoise.h"
  3. GeneratorRule::GeneratorRule()
  4. : ReferenceCounter(),
  5. noiseConfig(0),
  6. noise(0),
  7. threshold(0.f),
  8. condition(0),
  9. bottomLayer(""),
  10. topLayer("")
  11. {}
  12. GeneratorRule::~GeneratorRule()
  13. {
  14. if (noiseConfig) noiseConfig->release();
  15. if (noise) noise->release();
  16. if (condition) condition->release();
  17. }
  18. void GeneratorRule::initialize(JExpressionMemory* zMemory)
  19. {
  20. if (noiseConfig)
  21. {
  22. if (noise) noise->release();
  23. noise = JNoise::parseNoise(noiseConfig, zMemory);
  24. }
  25. }
  26. bool GeneratorRule::checkCondition(
  27. int x, int y, int z, JExpressionMemory* zMemory)
  28. {
  29. if ((topLayer.getLength() && y > zMemory->getFloatVariable(topLayer))
  30. || (bottomLayer.getLength()
  31. && y < zMemory->getFloatVariable(bottomLayer)))
  32. {
  33. return false;
  34. }
  35. return (!condition || condition->getValue(zMemory))
  36. && (!noise
  37. || noise->getNoise((double)x, (double)y, (double)z) <= threshold);
  38. }
  39. Framework::Either<Block*, int> GeneratorRule::generateBlock(
  40. int x, int y, int z, int dimensionId)
  41. {
  42. return createBlock(x, y, z, dimensionId);
  43. }
  44. void GeneratorRule::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
  45. {
  46. if (this->noiseConfig) this->noiseConfig->release();
  47. this->noiseConfig = noiseConfig;
  48. }
  49. Framework::JSON::JSONObject* GeneratorRule::zNoiseConfig() const
  50. {
  51. return noiseConfig;
  52. }
  53. void GeneratorRule::setThreshold(float threshold)
  54. {
  55. this->threshold = threshold;
  56. }
  57. float GeneratorRule::getThreshold() const
  58. {
  59. return threshold;
  60. }
  61. void GeneratorRule::setCondition(JBoolExpression* condition)
  62. {
  63. if (this->condition) this->condition->release();
  64. this->condition = condition;
  65. }
  66. JBoolExpression* GeneratorRule::zCondition() const
  67. {
  68. return condition;
  69. }
  70. void GeneratorRule::setBottomLayer(Framework::Text bottomLayer)
  71. {
  72. this->bottomLayer = bottomLayer;
  73. }
  74. Framework::Text GeneratorRule::getBottomLayer() const
  75. {
  76. return bottomLayer;
  77. }
  78. void GeneratorRule::setTopLayer(Framework::Text topLayer)
  79. {
  80. this->topLayer = topLayer;
  81. }
  82. Framework::Text GeneratorRule::getTopLayer() const
  83. {
  84. return topLayer;
  85. }