BiomGenerator.cpp 8.3 KB

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