GeneratorRule.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. topLayerF(0),
  12. botomLayerF(0)
  13. {}
  14. GeneratorRule::~GeneratorRule()
  15. {
  16. if (noiseConfig) noiseConfig->release();
  17. if (noise) noise->release();
  18. if (condition) condition->release();
  19. }
  20. void GeneratorRule::initialize(JExpressionMemory* zMemory)
  21. {
  22. if (noiseConfig)
  23. {
  24. if (noise) noise->release();
  25. noise = JNoise::parseNoise(noiseConfig, zMemory);
  26. }
  27. if (topLayer.getLength())
  28. {
  29. topLayerF = zMemory->getFloatVariableP(topLayer);
  30. }
  31. if (bottomLayer.getLength())
  32. {
  33. botomLayerF = zMemory->getFloatVariableP(bottomLayer);
  34. }
  35. if (condition)
  36. {
  37. condition->compile(zMemory);
  38. }
  39. }
  40. bool GeneratorRule::checkCondition(int x, int y, int z)
  41. {
  42. if ((topLayerF && z > *topLayerF) || (botomLayerF && z < *botomLayerF))
  43. {
  44. return false;
  45. }
  46. return (!condition || condition->getValue())
  47. && (!noise
  48. || noise->getNoise((double)x, (double)y, (double)z) <= threshold);
  49. }
  50. Framework::Either<Block*, int> GeneratorRule::generateBlock(
  51. int x, int y, int z, int dimensionId)
  52. {
  53. return createBlock(x, y, z, dimensionId);
  54. }
  55. void GeneratorRule::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
  56. {
  57. if (this->noiseConfig) this->noiseConfig->release();
  58. this->noiseConfig = noiseConfig;
  59. }
  60. Framework::JSON::JSONObject* GeneratorRule::zNoiseConfig() const
  61. {
  62. return noiseConfig;
  63. }
  64. void GeneratorRule::setThreshold(float threshold)
  65. {
  66. this->threshold = threshold;
  67. }
  68. float GeneratorRule::getThreshold() const
  69. {
  70. return threshold;
  71. }
  72. void GeneratorRule::setCondition(JBoolExpression* condition)
  73. {
  74. if (this->condition) this->condition->release();
  75. this->condition = condition;
  76. }
  77. JBoolExpression* GeneratorRule::zCondition() const
  78. {
  79. return condition;
  80. }
  81. void GeneratorRule::setBottomLayer(Framework::Text bottomLayer)
  82. {
  83. this->bottomLayer = bottomLayer;
  84. }
  85. Framework::Text GeneratorRule::getBottomLayer() const
  86. {
  87. return bottomLayer;
  88. }
  89. void GeneratorRule::setTopLayer(Framework::Text topLayer)
  90. {
  91. this->topLayer = topLayer;
  92. }
  93. Framework::Text GeneratorRule::getTopLayer() const
  94. {
  95. return topLayer;
  96. }