StructureCollection.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. {
  71. this->threshold = threshold;
  72. }
  73. double StructureTemplateCollection::getThreshold() const
  74. {
  75. return threshold;
  76. }
  77. void StructureTemplateCollection::setCondition(JBoolExpression* condition)
  78. {
  79. if (this->condition) this->condition->release();
  80. this->condition = condition;
  81. }
  82. JBoolExpression* StructureTemplateCollection::zCondition() const
  83. {
  84. return condition;
  85. }
  86. void StructureTemplateCollection::setActiveNoiseConfig(
  87. Framework::JSON::JSONObject* activeNoiseConfig)
  88. {
  89. if (this->activeNoiseConfig) this->activeNoiseConfig->release();
  90. this->activeNoiseConfig = activeNoiseConfig;
  91. }
  92. Framework::JSON::JSONObject*
  93. StructureTemplateCollection::zActiveNoiseConfig() const
  94. {
  95. return activeNoiseConfig;
  96. }
  97. void StructureTemplateCollection::setStructureNoiseConfig(
  98. Framework::JSON::JSONObject* structureNoiseConfig)
  99. {
  100. if (this->structureNoiseConfig) this->structureNoiseConfig->release();
  101. this->structureNoiseConfig = structureNoiseConfig;
  102. }
  103. Framework::JSON::JSONObject*
  104. StructureTemplateCollection::zStructureNoiseConfig() const
  105. {
  106. return structureNoiseConfig;
  107. }
  108. void StructureTemplateCollection::addStructure(GeneratorTemplate* structure)
  109. {
  110. structures.add(structure);
  111. if (structures.getEintragAnzahl() == 1)
  112. {
  113. minAffected = structure->getMinAffectedOffset();
  114. maxAffected = structure->getMaxAffectedOffset();
  115. }
  116. else
  117. {
  118. Framework::Vec3<int> min = structure->getMinAffectedOffset();
  119. Framework::Vec3<int> max = structure->getMaxAffectedOffset();
  120. if (minAffected.x > min.x) minAffected.x = min.x;
  121. if (minAffected.y > min.y) minAffected.y = min.y;
  122. if (minAffected.z > min.z) minAffected.z = min.z;
  123. if (maxAffected.x < max.x) maxAffected.x = max.x;
  124. if (maxAffected.y < max.y) maxAffected.y = max.y;
  125. if (maxAffected.z < max.z) maxAffected.z = max.z;
  126. }
  127. }
  128. const Framework::RCArray<GeneratorTemplate>&
  129. StructureTemplateCollection::getStructures() const
  130. {
  131. return structures;
  132. }
  133. Framework::Vec3<int> StructureTemplateCollection::getMinAffected() const
  134. {
  135. return minAffected;
  136. }
  137. Framework::Vec3<int> StructureTemplateCollection::getMaxAffected() const
  138. {
  139. return maxAffected;
  140. }
  141. StructureTemplateCollectionFactory::StructureTemplateCollectionFactory()
  142. : ObjectTypeFactory<StructureTemplateCollection>()
  143. {}
  144. StructureTemplateCollection* StructureTemplateCollectionFactory::fromJson(
  145. Framework::JSON::JSONObject* zJson) const
  146. {
  147. StructureTemplateCollection* result = new StructureTemplateCollection();
  148. result->setActiveNoiseConfig(zJson->getValue("activeNoise")->asObject());
  149. result->setStructureNoiseConfig(
  150. zJson->getValue("structureNoise")->asObject());
  151. result->setThreshold(zJson->zValue("threshold")->asNumber()->getNumber());
  152. result->setCondition(
  153. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  154. zJson->zValue("condition")));
  155. for (Framework::JSON::JSONValue* structure :
  156. *zJson->zValue("structures")->asArray())
  157. {
  158. result->addStructure(
  159. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorTemplate>(
  160. structure->asObject()));
  161. }
  162. return result;
  163. }
  164. Framework::JSON::JSONObject* StructureTemplateCollectionFactory::toJsonObject(
  165. StructureTemplateCollection* zObject) const
  166. {
  167. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  168. result->addValue("activeNoise",
  169. dynamic_cast<Framework::JSON::JSONValue*>(
  170. zObject->zActiveNoiseConfig()->getThis()));
  171. result->addValue("structureNoise",
  172. dynamic_cast<Framework::JSON::JSONValue*>(
  173. zObject->zStructureNoiseConfig()->getThis()));
  174. result->addValue(
  175. "threshold", new Framework::JSON::JSONNumber(zObject->getThreshold()));
  176. result->addValue("condition",
  177. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zCondition()));
  178. Framework::JSON::JSONArray* structures = new Framework::JSON::JSONArray();
  179. for (GeneratorTemplate* t : zObject->getStructures())
  180. {
  181. structures->addValue(Game::INSTANCE->zTypeRegistry()->toJson(t));
  182. }
  183. result->addValue("structures", structures);
  184. return result;
  185. }
  186. JSONObjectValidationBuilder* StructureTemplateCollectionFactory::addToValidator(
  187. JSONObjectValidationBuilder* builder) const
  188. {
  189. return builder
  190. ->withRequiredAttribute("activeNoise", JNoise::getValidator(false))
  191. ->withRequiredAttribute("structureNoise", JNoise::getValidator(false))
  192. ->withRequiredNumber("threshold")
  193. ->whichIsGreaterThen(0)
  194. ->finishNumber()
  195. ->withRequiredAttribute("condition",
  196. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  197. ->withRequiredArray("structures")
  198. ->addAcceptedTypeInArray(
  199. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorTemplate>())
  200. ->finishArray();
  201. }