BiomGenerator.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "BiomGenerator.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Constants.h"
  4. #include "Game.h"
  5. #include "JNoise.h"
  6. BiomGenerator::BiomGenerator()
  7. : ReferenceCounter(),
  8. condition(0)
  9. {}
  10. BiomGenerator::~BiomGenerator()
  11. {
  12. if (condition) condition->release();
  13. }
  14. void BiomGenerator::initialize(JExpressionMemory* zMemory)
  15. {
  16. for (GeneratorRule* rule : rules)
  17. {
  18. rule->initialize(zMemory);
  19. }
  20. for (EntityGenerator* entity : entityGenerators)
  21. {
  22. entity->initialize(zMemory);
  23. }
  24. for (StructureTemplateCollection* collection : templates)
  25. {
  26. collection->initialize(zMemory);
  27. }
  28. }
  29. Framework::Either<Block*, int> BiomGenerator::generateBlock(int x,
  30. int y,
  31. int z,
  32. int dimensionId,
  33. JExpressionMemory* zMemory,
  34. Chunk* partialGeneratedChunk)
  35. {
  36. for (GeneratorRule* rule : rules)
  37. {
  38. if (rule->checkCondition(x, y, z, zMemory))
  39. {
  40. auto result = rule->generateBlock(x, y, z, dimensionId);
  41. if ((result.isA() && result.getA())
  42. || (result.isB() && result.getB()))
  43. {
  44. return result;
  45. }
  46. }
  47. }
  48. return BlockTypeEnum::AIR;
  49. }
  50. bool BiomGenerator::isApplicable(JExpressionMemory* zMemory)
  51. {
  52. return condition->getValue(zMemory);
  53. }
  54. void BiomGenerator::generateStructures(int x,
  55. int y,
  56. int z,
  57. int dimensionId,
  58. JExpressionMemory* zMemory,
  59. Framework::Vec3<int> minPos,
  60. Framework::Vec3<int> maxPos,
  61. Framework::RCArray<GeneratedStructure>* zResult)
  62. {
  63. int minSearchX = minPos.x - maxStructureOffset.x;
  64. int minSearchY = minPos.y - maxStructureOffset.y;
  65. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  66. int maxSearchX = maxPos.x - minStructureOffset.x;
  67. int maxSearchY = maxPos.y - minStructureOffset.y;
  68. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  69. if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY
  70. && z >= minSearchZ && z <= maxSearchZ)
  71. {
  72. for (StructureTemplateCollection* collection : templates)
  73. {
  74. collection->generateStructures(
  75. x, y, z, dimensionId, zMemory, minPos, maxPos, zResult);
  76. }
  77. }
  78. }
  79. void BiomGenerator::generateEntities(
  80. int x, int y, int z, int dimensionId, JExpressionMemory* zMemory)
  81. {
  82. for (EntityGenerator* entityGen : entityGenerators)
  83. {
  84. if (entityGen->isGenerated(x, y, z, dimensionId, zMemory))
  85. {
  86. Entity* entity = entityGen->generate(
  87. Framework::Vec3<float>((float)x, (float)y, (float)z),
  88. dimensionId);
  89. Game::INSTANCE->requestWorldUpdate(
  90. new AddEntityUpdate(entity, dimensionId));
  91. }
  92. }
  93. }
  94. const Framework::RCArray<StructureTemplateCollection>&
  95. BiomGenerator::getTemplates() const
  96. {
  97. return templates;
  98. }
  99. Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
  100. {
  101. return minStructureOffset;
  102. }
  103. Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
  104. {
  105. return maxStructureOffset;
  106. }
  107. void BiomGenerator::setName(Framework::Text name)
  108. {
  109. this->name = name;
  110. }
  111. Framework::Text BiomGenerator::getName() const
  112. {
  113. return name;
  114. }
  115. void BiomGenerator::setCondition(JBoolExpression* condition)
  116. {
  117. if (this->condition) this->condition->release();
  118. this->condition = condition;
  119. }
  120. JBoolExpression* BiomGenerator::getCondition() const
  121. {
  122. return condition;
  123. }
  124. void BiomGenerator::addTemplate(StructureTemplateCollection* collection)
  125. {
  126. templates.add(collection);
  127. Framework::Vec3<int> min = collection->getMinAffected();
  128. Framework::Vec3<int> max = collection->getMaxAffected();
  129. if (templates.getEintragAnzahl())
  130. {
  131. minStructureOffset = min;
  132. maxStructureOffset = max;
  133. }
  134. else
  135. {
  136. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  137. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  138. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  139. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  140. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  141. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  142. }
  143. }
  144. const Framework::RCArray<StructureTemplateCollection>&
  145. BiomGenerator::getTemplates()
  146. {
  147. return templates;
  148. }
  149. void BiomGenerator::addGeneratorRule(GeneratorRule* rule)
  150. {
  151. rules.add(rule);
  152. }
  153. const Framework::RCArray<GeneratorRule>&
  154. BiomGenerator::getGeneratorRules() const
  155. {
  156. return rules;
  157. }
  158. BiomGeneratorFactory::BiomGeneratorFactory()
  159. : TypeFactory()
  160. {}
  161. BiomGenerator* BiomGeneratorFactory::createValue(
  162. Framework::JSON::JSONObject* zJson) const
  163. {
  164. return new BiomGenerator();
  165. }
  166. void BiomGeneratorFactory::fromJson(
  167. BiomGenerator* zResult, Framework::JSON::JSONObject* zJson) const
  168. {
  169. zResult->setName(zJson->zValue("name")->asString()->getString());
  170. zResult->setCondition(
  171. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  172. zJson->zValue("condition")));
  173. bool first = 1;
  174. for (Framework::JSON::JSONValue* value :
  175. *zJson->zValue("structurCollections")->asArray())
  176. {
  177. StructureTemplateCollection* collection
  178. = Game::INSTANCE->zTypeRegistry()
  179. ->fromJson<StructureTemplateCollection>(value);
  180. zResult->addTemplate(collection);
  181. }
  182. for (Framework::JSON::JSONValue* value :
  183. *zJson->asObject()->zValue("blocks")->asArray())
  184. {
  185. zResult->addGeneratorRule(
  186. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
  187. }
  188. for (Framework::JSON::JSONValue* value :
  189. *zJson->asObject()->zValue("entities")->asArray())
  190. {
  191. zResult->addEntityGenerator(
  192. Game::INSTANCE->zTypeRegistry()->fromJson<EntityGenerator>(value));
  193. }
  194. }
  195. void BiomGeneratorFactory::toJson(
  196. BiomGenerator* zObject, Framework::JSON::JSONObject* zResult) const
  197. {
  198. zResult->addValue(
  199. "name", new Framework::JSON::JSONString(zObject->getName()));
  200. zResult->addValue("condition",
  201. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  202. zObject->getCondition()));
  203. Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
  204. for (StructureTemplateCollection* collection : zObject->getTemplates())
  205. {
  206. collections->addValue(
  207. Game::INSTANCE->zTypeRegistry()
  208. ->toJson<StructureTemplateCollection>(collection));
  209. }
  210. zResult->addValue("structurCollections", collections);
  211. Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
  212. for (GeneratorRule* rule : zObject->getGeneratorRules())
  213. {
  214. rules->addValue(
  215. Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
  216. }
  217. zResult->addValue("blocks", rules);
  218. Framework::JSON::JSONArray* entities = new Framework::JSON::JSONArray();
  219. for (EntityGenerator* entity : zObject->getEntityGenerators())
  220. {
  221. entities->addValue(
  222. Game::INSTANCE->zTypeRegistry()->toJson<EntityGenerator>(entity));
  223. }
  224. zResult->addValue("entities", entities);
  225. }
  226. JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
  227. JSONObjectValidationBuilder* builder) const
  228. {
  229. return builder->withRequiredString("name")
  230. ->finishString()
  231. ->withRequiredAttribute("condition",
  232. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  233. ->withRequiredArray("structurCollections")
  234. ->addAcceptedTypeInArray(
  235. Game::INSTANCE->zTypeRegistry()
  236. ->getValidator<StructureTemplateCollection>())
  237. ->finishArray()
  238. ->withRequiredArray("blocks")
  239. ->addAcceptedTypeInArray(
  240. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
  241. ->finishArray()
  242. ->withRequiredArray("entities")
  243. ->addAcceptedTypeInArray(
  244. Game::INSTANCE->zTypeRegistry()->getValidator<EntityGenerator>())
  245. ->finishArray();
  246. }