StructureCollection.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. }
  28. void StructureTemplateCollection::generateStructures(int x,
  29. int y,
  30. int z,
  31. int dimensionId,
  32. JExpressionMemory* zMemory,
  33. Framework::Vec3<int> minPos,
  34. Framework::Vec3<int> maxPos,
  35. Framework::RCArray<GeneratedStructure>* zResult)
  36. {
  37. int minSearchX = minPos.x - maxAffected.x;
  38. int minSearchY = minPos.y - maxAffected.y;
  39. int minSearchZ = MAX(minPos.z - maxAffected.z, 0);
  40. int maxSearchX = maxPos.x - minAffected.x;
  41. int maxSearchY = maxPos.y - minAffected.y;
  42. int maxSearchZ = MIN(maxPos.z - minAffected.z, WORLD_HEIGHT - 1);
  43. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY
  44. && z >= minSearchZ && z <= maxSearchZ)
  45. {
  46. if (activeNoise->getNoise((double)x, (double)y, (double)z) < threshold)
  47. {
  48. if (condition->getValue(zMemory))
  49. {
  50. double rValue
  51. = structureNoise->getNoise((double)x, (double)y, (double)z);
  52. double probSum = 0;
  53. for (auto t : structures)
  54. {
  55. if (rValue - probSum <= t->getPropability())
  56. {
  57. zResult->add(
  58. t->generateAt(Framework::Vec3<int>(x, y, z),
  59. structureNoise,
  60. dimensionId));
  61. break;
  62. }
  63. probSum += t->getPropability();
  64. }
  65. }
  66. }
  67. }
  68. }
  69. void StructureTemplateCollection::setThreshold(double threshold) {
  70. this->threshold = threshold;
  71. }
  72. double StructureTemplateCollection::getThreshold() const
  73. {
  74. return threshold;
  75. }
  76. void StructureTemplateCollection::setCondition(JBoolExpression* condition) {
  77. if (this->condition) this->condition->release();
  78. this->condition = condition;
  79. }
  80. JBoolExpression* StructureTemplateCollection::zCondition() const
  81. {
  82. return condition;
  83. }
  84. void StructureTemplateCollection::setActiveNoiseConfig(
  85. Framework::JSON::JSONObject* activeNoiseConfig)
  86. {
  87. if (this->activeNoiseConfig) this->activeNoiseConfig->release();
  88. this->activeNoiseConfig = activeNoiseConfig;
  89. }
  90. Framework::JSON::JSONObject*
  91. StructureTemplateCollection::zActiveNoiseConfig() const
  92. {
  93. return activeNoiseConfig;
  94. }
  95. void StructureTemplateCollection::setStructureNoiseConfig(
  96. Framework::JSON::JSONObject* structureNoiseConfig)
  97. {
  98. if (this->structureNoiseConfig) this->structureNoiseConfig->release();
  99. this->structureNoiseConfig = structureNoiseConfig;
  100. }
  101. Framework::JSON::JSONObject*
  102. StructureTemplateCollection::zStructureNoiseConfig() const
  103. {
  104. return structureNoiseConfig;
  105. }
  106. void StructureTemplateCollection::addStructure(GeneratorTemplate* structure)
  107. {
  108. structures.add(structure);
  109. if (structures.getEintragAnzahl() == 1)
  110. {
  111. minAffected = structure->getMinAffectedOffset();
  112. maxAffected = structure->getMaxAffectedOffset();
  113. }
  114. else
  115. {
  116. Framework::Vec3<int> min = structure->getMinAffectedOffset();
  117. Framework::Vec3<int> max = structure->getMaxAffectedOffset();
  118. if (minAffected.x > min.x) minAffected.x = min.x;
  119. if (minAffected.y > min.y) minAffected.y = min.y;
  120. if (minAffected.z > min.z) minAffected.z = min.z;
  121. if (maxAffected.x < max.x) maxAffected.x = max.x;
  122. if (maxAffected.y < max.y) maxAffected.y = max.y;
  123. if (maxAffected.z < max.z) maxAffected.z = max.z;
  124. }
  125. }
  126. const Framework::RCArray<GeneratorTemplate>&
  127. StructureTemplateCollection::getStructures() const
  128. {
  129. return structures;
  130. }
  131. Framework::Vec3<int> StructureTemplateCollection::getMinAffected() const
  132. {
  133. return minAffected;
  134. }
  135. Framework::Vec3<int> StructureTemplateCollection::getMaxAffected() const
  136. {
  137. return maxAffected;
  138. }
  139. StructureTemplateCollectionFactory::StructureTemplateCollectionFactory()
  140. : TypeFactory<StructureTemplateCollection>()
  141. {}
  142. StructureTemplateCollection* StructureTemplateCollectionFactory::createValue(
  143. Framework::JSON::JSONObject* zJson) const
  144. {
  145. return new StructureTemplateCollection();
  146. }
  147. void StructureTemplateCollectionFactory::fromJson(
  148. StructureTemplateCollection* zResult,
  149. Framework::JSON::JSONObject* zJson) const
  150. {
  151. zResult->setActiveNoiseConfig(zJson->getValue("activeNoise")->asObject());
  152. zResult->setStructureNoiseConfig(
  153. zJson->getValue("structureNoise")->asObject());
  154. zResult->setThreshold(zJson->zValue("threshold")->asNumber()->getNumber());
  155. zResult->setCondition(
  156. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  157. zJson->zValue("condition")));
  158. for (Framework::JSON::JSONValue* structure :
  159. *zJson->zValue("structures")->asArray())
  160. {
  161. zResult->addStructure(
  162. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorTemplate>(
  163. structure->asObject()));
  164. }
  165. }
  166. void StructureTemplateCollectionFactory::toJson(
  167. StructureTemplateCollection* zObject,
  168. Framework::JSON::JSONObject* zResult) const
  169. {
  170. zResult->addValue("activeNoise",
  171. dynamic_cast<Framework::JSON::JSONValue*>(
  172. zObject->zActiveNoiseConfig()->getThis()));
  173. zResult->addValue("structureNoise",
  174. dynamic_cast<Framework::JSON::JSONValue*>(
  175. zObject->zStructureNoiseConfig()->getThis()));
  176. zResult->addValue(
  177. "threshold", new Framework::JSON::JSONNumber(zObject->getThreshold()));
  178. zResult->addValue("condition",
  179. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zCondition()));
  180. Framework::JSON::JSONArray* structures = new Framework::JSON::JSONArray();
  181. for (GeneratorTemplate* t : zObject->getStructures())
  182. {
  183. structures->addValue(Game::INSTANCE->zTypeRegistry()->toJson(t));
  184. }
  185. zResult->addValue("structures", structures);
  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. }