BiomGenerator.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. : ObjectTypeFactory()
  177. {}
  178. BiomGenerator* BiomGeneratorFactory::fromJson(
  179. Framework::JSON::JSONObject* zJson) const
  180. {
  181. BiomGenerator* result = new BiomGenerator();
  182. result->setName(zJson->zValue("name")->asString()->getString());
  183. result->setCondition(
  184. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  185. zJson->zValue("condition")));
  186. bool first = 1;
  187. for (Framework::JSON::JSONValue* value :
  188. *zJson->zValue("structurCollections")->asArray())
  189. {
  190. StructureTemplateCollection* collection
  191. = Game::INSTANCE->zTypeRegistry()
  192. ->fromJson<StructureTemplateCollection>(value);
  193. result->addTemplate(collection);
  194. }
  195. for (Framework::JSON::JSONValue* value :
  196. *zJson->asObject()->zValue("blocks")->asArray())
  197. {
  198. result->addGeneratorRule(
  199. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
  200. }
  201. for (Framework::JSON::JSONValue* value :
  202. *zJson->asObject()->zValue("entities")->asArray())
  203. {
  204. result->addEntityGenerator(
  205. Game::INSTANCE->zTypeRegistry()->fromJson<EntityGenerator>(value));
  206. }
  207. return result;
  208. }
  209. Framework::JSON::JSONObject* BiomGeneratorFactory::toJsonObject(
  210. BiomGenerator* zObject) const
  211. {
  212. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  213. result->addValue(
  214. "name", new Framework::JSON::JSONString(zObject->getName()));
  215. result->addValue("condition",
  216. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  217. zObject->getCondition()));
  218. Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
  219. for (StructureTemplateCollection* collection : zObject->getTemplates())
  220. {
  221. collections->addValue(
  222. Game::INSTANCE->zTypeRegistry()
  223. ->toJson<StructureTemplateCollection>(collection));
  224. }
  225. result->addValue("structurCollections", collections);
  226. Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
  227. for (GeneratorRule* rule : zObject->getGeneratorRules())
  228. {
  229. rules->addValue(
  230. Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
  231. }
  232. result->addValue("blocks", rules);
  233. Framework::JSON::JSONArray* entities = new Framework::JSON::JSONArray();
  234. for (EntityGenerator* entity : zObject->getEntityGenerators())
  235. {
  236. entities->addValue(
  237. Game::INSTANCE->zTypeRegistry()->toJson<EntityGenerator>(entity));
  238. }
  239. result->addValue("entities", entities);
  240. return result;
  241. }
  242. JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
  243. JSONObjectValidationBuilder* builder) const
  244. {
  245. return builder->withRequiredString("name")
  246. ->finishString()
  247. ->withRequiredAttribute("condition",
  248. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  249. ->withRequiredArray("structurCollections")
  250. ->addAcceptedTypeInArray(
  251. Game::INSTANCE->zTypeRegistry()
  252. ->getValidator<StructureTemplateCollection>())
  253. ->finishArray()
  254. ->withRequiredArray("blocks")
  255. ->addAcceptedTypeInArray(
  256. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
  257. ->finishArray()
  258. ->withRequiredArray("entities")
  259. ->addAcceptedTypeInArray(
  260. Game::INSTANCE->zTypeRegistry()->getValidator<EntityGenerator>())
  261. ->finishArray();
  262. }