BiomGenerator.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. for (PlantConfig* plantConfig : plantConfigs)
  30. {
  31. plantConfig->initialize(zMemory);
  32. }
  33. if (condition)
  34. {
  35. condition->compile(zMemory);
  36. }
  37. }
  38. Framework::Either<Block*, int> BiomGenerator::generateBlock(
  39. int x, int y, int z, int dimensionId, Chunk* partialGeneratedChunk)
  40. {
  41. for (GeneratorRule* rule : rules)
  42. {
  43. if (rule->checkCondition(x, y, z))
  44. {
  45. auto result = rule->generateBlock(x, y, z, dimensionId);
  46. if ((result.isA() && result.getA())
  47. || (result.isB() && result.getB()))
  48. {
  49. return result;
  50. }
  51. }
  52. }
  53. return BlockTypeEnum::AIR;
  54. }
  55. bool BiomGenerator::isApplicable()
  56. {
  57. return !condition || condition->getValue();
  58. }
  59. void BiomGenerator::generateStructures(int x,
  60. int y,
  61. int z,
  62. int dimensionId,
  63. Framework::Vec3<int>& minPos,
  64. Framework::Vec3<int>& maxPos,
  65. Framework::RCArray<GeneratedStructure>* zResult)
  66. {
  67. for (StructureTemplateCollection* collection : templates)
  68. {
  69. collection->generateStructures(
  70. x, y, z, dimensionId, minPos, maxPos, zResult);
  71. }
  72. }
  73. void BiomGenerator::generatePlants(int x,
  74. int y,
  75. int z,
  76. int dimensionId,
  77. Chunk* zChunk,
  78. bool underground,
  79. bool underwater,
  80. int seaFluidBlockTypeId)
  81. {
  82. PlantConfig* chosenConfig = 0;
  83. double maxValue = 0.0;
  84. for (PlantConfig* plantConfig : plantConfigs)
  85. {
  86. double value = plantConfig->doesGeneratePlant(x,
  87. y,
  88. z,
  89. dimensionId,
  90. zChunk,
  91. underground,
  92. underwater,
  93. seaFluidBlockTypeId);
  94. if (value > maxValue)
  95. {
  96. maxValue = value;
  97. chosenConfig = plantConfig;
  98. }
  99. }
  100. if (chosenConfig)
  101. {
  102. chosenConfig->generatePlantAt(x, y, z, dimensionId, zChunk);
  103. }
  104. }
  105. void BiomGenerator::generateEntities(
  106. int x, int y, int z, int dimensionId, Chunk* zChunk)
  107. {
  108. for (EntityGenerator* entityGen : entityGenerators)
  109. {
  110. if (entityGen->isGenerated(x, y, z, dimensionId))
  111. {
  112. Entity* entity = entityGen->generate(
  113. Framework::Vec3<float>((float)x, (float)y, (float)z),
  114. dimensionId);
  115. zChunk->addGeneratedEntity(entity);
  116. }
  117. }
  118. }
  119. const Framework::RCArray<StructureTemplateCollection>&
  120. BiomGenerator::getTemplates() const
  121. {
  122. return templates;
  123. }
  124. Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
  125. {
  126. return minStructureOffset;
  127. }
  128. Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
  129. {
  130. return maxStructureOffset;
  131. }
  132. void BiomGenerator::setName(Framework::Text name)
  133. {
  134. this->name = name;
  135. }
  136. Framework::Text BiomGenerator::getName() const
  137. {
  138. return name;
  139. }
  140. void BiomGenerator::setCondition(JBoolExpression* condition)
  141. {
  142. if (this->condition) this->condition->release();
  143. this->condition = condition;
  144. }
  145. JBoolExpression* BiomGenerator::getCondition() const
  146. {
  147. return condition;
  148. }
  149. void BiomGenerator::addTemplate(StructureTemplateCollection* collection)
  150. {
  151. templates.add(collection);
  152. Framework::Vec3<int> min = collection->getMinAffected();
  153. Framework::Vec3<int> max = collection->getMaxAffected();
  154. if (templates.getEintragAnzahl())
  155. {
  156. minStructureOffset = min;
  157. maxStructureOffset = max;
  158. }
  159. else
  160. {
  161. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  162. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  163. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  164. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  165. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  166. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  167. }
  168. }
  169. const Framework::RCArray<StructureTemplateCollection>&
  170. BiomGenerator::getTemplates()
  171. {
  172. return templates;
  173. }
  174. void BiomGenerator::addGeneratorRule(GeneratorRule* rule)
  175. {
  176. rules.add(rule);
  177. }
  178. const Framework::RCArray<GeneratorRule>&
  179. BiomGenerator::getGeneratorRules() const
  180. {
  181. return rules;
  182. }
  183. void BiomGenerator::addEntityGenerator(EntityGenerator* generator)
  184. {
  185. entityGenerators.add(generator);
  186. }
  187. const Framework::RCArray<EntityGenerator>&
  188. BiomGenerator::getEntityGenerators() const
  189. {
  190. return entityGenerators;
  191. }
  192. void BiomGenerator::addPlantConfig(PlantConfig* config)
  193. {
  194. plantConfigs.add(config);
  195. }
  196. const Framework::RCArray<PlantConfig>& BiomGenerator::getPlantConfigs() const
  197. {
  198. return plantConfigs;
  199. }
  200. BiomGeneratorFactory::BiomGeneratorFactory()
  201. : ObjectTypeFactory()
  202. {}
  203. BiomGenerator* BiomGeneratorFactory::fromJson(
  204. Framework::JSON::JSONObject* zJson) const
  205. {
  206. BiomGenerator* result = new BiomGenerator();
  207. result->setName(zJson->zValue("name")->asString()->getString());
  208. if (zJson->hasValue("condition"))
  209. {
  210. result->setCondition(
  211. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  212. zJson->zValue("condition")));
  213. }
  214. bool first = 1;
  215. for (Framework::JSON::JSONValue* value :
  216. *zJson->zValue("structurCollections")->asArray())
  217. {
  218. StructureTemplateCollection* collection
  219. = Game::INSTANCE->zTypeRegistry()
  220. ->fromJson<StructureTemplateCollection>(value);
  221. result->addTemplate(collection);
  222. }
  223. for (Framework::JSON::JSONValue* value :
  224. *zJson->asObject()->zValue("blocks")->asArray())
  225. {
  226. result->addGeneratorRule(
  227. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
  228. }
  229. for (Framework::JSON::JSONValue* value :
  230. *zJson->asObject()->zValue("entities")->asArray())
  231. {
  232. result->addEntityGenerator(
  233. Game::INSTANCE->zTypeRegistry()->fromJson<EntityGenerator>(value));
  234. }
  235. for (Framework::JSON::JSONValue* value :
  236. *zJson->asObject()->zValue("plants")->asArray())
  237. {
  238. result->addPlantConfig(
  239. Game::INSTANCE->zTypeRegistry()->fromJson<PlantConfig>(value));
  240. }
  241. return result;
  242. }
  243. Framework::JSON::JSONObject* BiomGeneratorFactory::toJsonObject(
  244. BiomGenerator* zObject) const
  245. {
  246. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  247. result->addValue(
  248. "name", new Framework::JSON::JSONString(zObject->getName()));
  249. if (zObject->getCondition())
  250. {
  251. result->addValue("condition",
  252. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  253. zObject->getCondition()));
  254. }
  255. Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
  256. for (StructureTemplateCollection* collection : zObject->getTemplates())
  257. {
  258. collections->addValue(Game::INSTANCE->zTypeRegistry()
  259. ->toJson<StructureTemplateCollection>(collection));
  260. }
  261. result->addValue("structurCollections", collections);
  262. Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
  263. for (GeneratorRule* rule : zObject->getGeneratorRules())
  264. {
  265. rules->addValue(
  266. Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
  267. }
  268. result->addValue("blocks", rules);
  269. Framework::JSON::JSONArray* entities = new Framework::JSON::JSONArray();
  270. for (EntityGenerator* entity : zObject->getEntityGenerators())
  271. {
  272. entities->addValue(
  273. Game::INSTANCE->zTypeRegistry()->toJson<EntityGenerator>(entity));
  274. }
  275. result->addValue("entities", entities);
  276. Framework::JSON::JSONArray* plants = new Framework::JSON::JSONArray();
  277. for (PlantConfig* plant : zObject->getPlantConfigs())
  278. {
  279. plants->addValue(
  280. Game::INSTANCE->zTypeRegistry()->toJson<PlantConfig>(plant));
  281. }
  282. result->addValue("plants", plants);
  283. return result;
  284. }
  285. JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
  286. JSONObjectValidationBuilder* builder) const
  287. {
  288. return builder->withRequiredString("name")
  289. ->finishString()
  290. ->withRequiredAttribute("condition",
  291. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>(),
  292. false,
  293. true)
  294. ->withRequiredArray("structurCollections")
  295. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  296. ->getValidator<StructureTemplateCollection>())
  297. ->finishArray()
  298. ->withRequiredArray("blocks")
  299. ->addAcceptedTypeInArray(
  300. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
  301. ->finishArray()
  302. ->withRequiredArray("entities")
  303. ->addAcceptedTypeInArray(
  304. Game::INSTANCE->zTypeRegistry()->getValidator<EntityGenerator>())
  305. ->finishArray()
  306. ->withRequiredArray("plants")
  307. ->addAcceptedTypeInArray(
  308. Game::INSTANCE->zTypeRegistry()->getValidator<PlantConfig>())
  309. ->finishArray();
  310. }