StructureCollection.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "StructureCollection.h"
  2. #include "Constants.h"
  3. #include "Game.h"
  4. #include "JNoise.h"
  5. StructureTemplateCollection::StructureTemplateCollection()
  6. : ReferenceCounter(),
  7. activeNoise(0),
  8. activeNoiseConfig(0),
  9. structureNoise(0),
  10. structureNoiseConfig(0),
  11. condition(0)
  12. {}
  13. StructureTemplateCollection::~StructureTemplateCollection()
  14. {
  15. if (activeNoise) activeNoise->release();
  16. if (activeNoiseConfig) activeNoiseConfig->release();
  17. if (structureNoise) structureNoise->release();
  18. if (structureNoiseConfig) structureNoiseConfig->release();
  19. if (condition) condition->release();
  20. }
  21. void StructureTemplateCollection::initialize(JExpressionMemory* zMemory)
  22. {
  23. if (activeNoise) activeNoise->release();
  24. if (structureNoise) structureNoise->release();
  25. activeNoise = JNoise::parseNoise(activeNoiseConfig, zMemory);
  26. structureNoise = JNoise::parseNoise(structureNoiseConfig, zMemory);
  27. condition->compile(zMemory);
  28. }
  29. void StructureTemplateCollection::generateStructures(int x,
  30. int y,
  31. int z,
  32. int dimensionId,
  33. Framework::Vec3<int>& minPos,
  34. Framework::Vec3<int>& maxPos,
  35. Framework::RCArray<GeneratedStructure>* zResult)
  36. {
  37. if (condition->getValue())
  38. {
  39. int minSearchX = minPos.x - maxAffected.x;
  40. int minSearchY = minPos.y - maxAffected.y;
  41. int minSearchZ = minPos.z - maxAffected.z;
  42. int maxSearchX = maxPos.x - minAffected.x;
  43. int maxSearchY = maxPos.y - minAffected.y;
  44. int maxSearchZ = maxPos.z - minAffected.z;
  45. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY
  46. && y <= maxSearchY && z >= minSearchZ && z <= maxSearchZ)
  47. {
  48. if (activeNoise->getNoise((double)x, (double)y, (double)z)
  49. < threshold)
  50. {
  51. double rValue
  52. = structureNoise->getNoise((double)x, (double)y, (double)z);
  53. double probSum = 0;
  54. for (auto t : structures)
  55. {
  56. if (rValue - probSum <= t->getPropability())
  57. {
  58. zResult->add(
  59. t->generateAt(Framework::Vec3<int>(x, y, z),
  60. structureNoise,
  61. dimensionId));
  62. break;
  63. }
  64. probSum += t->getPropability();
  65. }
  66. }
  67. }
  68. }
  69. }
  70. void StructureTemplateCollection::setThreshold(double threshold)
  71. {
  72. this->threshold = threshold;
  73. }
  74. double StructureTemplateCollection::getThreshold() const
  75. {
  76. return threshold;
  77. }
  78. void StructureTemplateCollection::setCondition(JBoolExpression* condition)
  79. {
  80. if (this->condition) this->condition->release();
  81. this->condition = condition;
  82. }
  83. JBoolExpression* StructureTemplateCollection::zCondition() const
  84. {
  85. return condition;
  86. }
  87. void StructureTemplateCollection::setActiveNoiseConfig(
  88. Framework::JSON::JSONObject* activeNoiseConfig)
  89. {
  90. if (this->activeNoiseConfig) this->activeNoiseConfig->release();
  91. this->activeNoiseConfig = activeNoiseConfig;
  92. }
  93. Framework::JSON::JSONObject*
  94. StructureTemplateCollection::zActiveNoiseConfig() const
  95. {
  96. return activeNoiseConfig;
  97. }
  98. void StructureTemplateCollection::setStructureNoiseConfig(
  99. Framework::JSON::JSONObject* structureNoiseConfig)
  100. {
  101. if (this->structureNoiseConfig) this->structureNoiseConfig->release();
  102. this->structureNoiseConfig = structureNoiseConfig;
  103. }
  104. Framework::JSON::JSONObject*
  105. StructureTemplateCollection::zStructureNoiseConfig() const
  106. {
  107. return structureNoiseConfig;
  108. }
  109. void StructureTemplateCollection::addStructure(GeneratorTemplate* structure)
  110. {
  111. structures.add(structure);
  112. if (structures.getEintragAnzahl() == 1)
  113. {
  114. minAffected = structure->getMinAffectedOffset();
  115. maxAffected = structure->getMaxAffectedOffset();
  116. }
  117. else
  118. {
  119. Framework::Vec3<int> min = structure->getMinAffectedOffset();
  120. Framework::Vec3<int> max = structure->getMaxAffectedOffset();
  121. if (minAffected.x > min.x) minAffected.x = min.x;
  122. if (minAffected.y > min.y) minAffected.y = min.y;
  123. if (minAffected.z > min.z) minAffected.z = min.z;
  124. if (maxAffected.x < max.x) maxAffected.x = max.x;
  125. if (maxAffected.y < max.y) maxAffected.y = max.y;
  126. if (maxAffected.z < max.z) maxAffected.z = max.z;
  127. }
  128. }
  129. const Framework::RCArray<GeneratorTemplate>&
  130. StructureTemplateCollection::getStructures() const
  131. {
  132. return structures;
  133. }
  134. Framework::Vec3<int> StructureTemplateCollection::getMinAffected() const
  135. {
  136. return minAffected;
  137. }
  138. Framework::Vec3<int> StructureTemplateCollection::getMaxAffected() const
  139. {
  140. return maxAffected;
  141. }
  142. StructureTemplateCollectionFactory::StructureTemplateCollectionFactory()
  143. : ObjectTypeFactory<StructureTemplateCollection>()
  144. {}
  145. StructureTemplateCollection* StructureTemplateCollectionFactory::fromJson(
  146. Framework::JSON::JSONObject* zJson) const
  147. {
  148. StructureTemplateCollection* result = new StructureTemplateCollection();
  149. result->setActiveNoiseConfig(zJson->getValue("activeNoise")->asObject());
  150. result->setStructureNoiseConfig(
  151. zJson->getValue("structureNoise")->asObject());
  152. result->setThreshold(zJson->zValue("threshold")->asNumber()->getNumber());
  153. result->setCondition(
  154. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  155. zJson->zValue("condition")));
  156. for (Framework::JSON::JSONValue* structure :
  157. *zJson->zValue("structures")->asArray())
  158. {
  159. result->addStructure(
  160. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorTemplate>(
  161. structure->asObject()));
  162. }
  163. return result;
  164. }
  165. Framework::JSON::JSONObject* StructureTemplateCollectionFactory::toJsonObject(
  166. StructureTemplateCollection* zObject) const
  167. {
  168. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  169. result->addValue("activeNoise",
  170. dynamic_cast<Framework::JSON::JSONValue*>(
  171. zObject->zActiveNoiseConfig()->getThis()));
  172. result->addValue("structureNoise",
  173. dynamic_cast<Framework::JSON::JSONValue*>(
  174. zObject->zStructureNoiseConfig()->getThis()));
  175. result->addValue(
  176. "threshold", new Framework::JSON::JSONNumber(zObject->getThreshold()));
  177. result->addValue("condition",
  178. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zCondition()));
  179. Framework::JSON::JSONArray* structures = new Framework::JSON::JSONArray();
  180. for (GeneratorTemplate* t : zObject->getStructures())
  181. {
  182. structures->addValue(Game::INSTANCE->zTypeRegistry()->toJson(t));
  183. }
  184. result->addValue("structures", structures);
  185. return result;
  186. }
  187. JSONObjectValidationBuilder* StructureTemplateCollectionFactory::addToValidator(
  188. JSONObjectValidationBuilder* builder) const
  189. {
  190. return builder
  191. ->withRequiredAttribute("activeNoise", JNoise::getValidator(false))
  192. ->withRequiredAttribute("structureNoise", JNoise::getValidator(false))
  193. ->withRequiredNumber("threshold")
  194. ->whichIsGreaterThen(0)
  195. ->finishNumber()
  196. ->withRequiredAttribute("condition",
  197. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  198. ->withRequiredArray("structures")
  199. ->addAcceptedTypeInArray(
  200. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorTemplate>())
  201. ->finishArray();
  202. }