BiomGenerator.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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(int x, int y, int z, int dimensionId)
  106. {
  107. for (EntityGenerator* entityGen : entityGenerators)
  108. {
  109. if (entityGen->isGenerated(x, y, z, dimensionId))
  110. {
  111. Entity* entity = entityGen->generate(
  112. Framework::Vec3<float>((float)x, (float)y, (float)z),
  113. dimensionId);
  114. Dimension* dimension = Game::INSTANCE->zDimension(dimensionId);
  115. if (dimension)
  116. {
  117. dimension->addEntity(entity);
  118. }
  119. else
  120. {
  121. entity->release();
  122. }
  123. }
  124. }
  125. }
  126. const Framework::RCArray<StructureTemplateCollection>&
  127. BiomGenerator::getTemplates() const
  128. {
  129. return templates;
  130. }
  131. Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
  132. {
  133. return minStructureOffset;
  134. }
  135. Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
  136. {
  137. return maxStructureOffset;
  138. }
  139. void BiomGenerator::setName(Framework::Text name)
  140. {
  141. this->name = name;
  142. }
  143. Framework::Text BiomGenerator::getName() const
  144. {
  145. return name;
  146. }
  147. void BiomGenerator::setCondition(JBoolExpression* condition)
  148. {
  149. if (this->condition) this->condition->release();
  150. this->condition = condition;
  151. }
  152. JBoolExpression* BiomGenerator::getCondition() const
  153. {
  154. return condition;
  155. }
  156. void BiomGenerator::addTemplate(StructureTemplateCollection* collection)
  157. {
  158. templates.add(collection);
  159. Framework::Vec3<int> min = collection->getMinAffected();
  160. Framework::Vec3<int> max = collection->getMaxAffected();
  161. if (templates.getEintragAnzahl())
  162. {
  163. minStructureOffset = min;
  164. maxStructureOffset = max;
  165. }
  166. else
  167. {
  168. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  169. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  170. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  171. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  172. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  173. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  174. }
  175. }
  176. const Framework::RCArray<StructureTemplateCollection>&
  177. BiomGenerator::getTemplates()
  178. {
  179. return templates;
  180. }
  181. void BiomGenerator::addGeneratorRule(GeneratorRule* rule)
  182. {
  183. rules.add(rule);
  184. }
  185. const Framework::RCArray<GeneratorRule>&
  186. BiomGenerator::getGeneratorRules() const
  187. {
  188. return rules;
  189. }
  190. void BiomGenerator::addEntityGenerator(EntityGenerator* generator)
  191. {
  192. entityGenerators.add(generator);
  193. }
  194. const Framework::RCArray<EntityGenerator>&
  195. BiomGenerator::getEntityGenerators() const
  196. {
  197. return entityGenerators;
  198. }
  199. void BiomGenerator::addPlantConfig(PlantConfig* config)
  200. {
  201. plantConfigs.add(config);
  202. }
  203. const Framework::RCArray<PlantConfig>& BiomGenerator::getPlantConfigs() const
  204. {
  205. return plantConfigs;
  206. }
  207. BiomGeneratorFactory::BiomGeneratorFactory()
  208. : ObjectTypeFactory()
  209. {}
  210. BiomGenerator* BiomGeneratorFactory::fromJson(
  211. Framework::JSON::JSONObject* zJson) const
  212. {
  213. BiomGenerator* result = new BiomGenerator();
  214. result->setName(zJson->zValue("name")->asString()->getString());
  215. if (zJson->hasValue("condition"))
  216. {
  217. result->setCondition(
  218. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  219. zJson->zValue("condition")));
  220. }
  221. bool first = 1;
  222. for (Framework::JSON::JSONValue* value :
  223. *zJson->zValue("structurCollections")->asArray())
  224. {
  225. StructureTemplateCollection* collection
  226. = Game::INSTANCE->zTypeRegistry()
  227. ->fromJson<StructureTemplateCollection>(value);
  228. result->addTemplate(collection);
  229. }
  230. for (Framework::JSON::JSONValue* value :
  231. *zJson->asObject()->zValue("blocks")->asArray())
  232. {
  233. result->addGeneratorRule(
  234. Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
  235. }
  236. for (Framework::JSON::JSONValue* value :
  237. *zJson->asObject()->zValue("entities")->asArray())
  238. {
  239. result->addEntityGenerator(
  240. Game::INSTANCE->zTypeRegistry()->fromJson<EntityGenerator>(value));
  241. }
  242. for (Framework::JSON::JSONValue* value :
  243. *zJson->asObject()->zValue("plants")->asArray())
  244. {
  245. result->addPlantConfig(
  246. Game::INSTANCE->zTypeRegistry()->fromJson<PlantConfig>(value));
  247. }
  248. return result;
  249. }
  250. Framework::JSON::JSONObject* BiomGeneratorFactory::toJsonObject(
  251. BiomGenerator* zObject) const
  252. {
  253. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  254. result->addValue(
  255. "name", new Framework::JSON::JSONString(zObject->getName()));
  256. if (zObject->getCondition())
  257. {
  258. result->addValue("condition",
  259. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  260. zObject->getCondition()));
  261. }
  262. Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
  263. for (StructureTemplateCollection* collection : zObject->getTemplates())
  264. {
  265. collections->addValue(Game::INSTANCE->zTypeRegistry()
  266. ->toJson<StructureTemplateCollection>(collection));
  267. }
  268. result->addValue("structurCollections", collections);
  269. Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
  270. for (GeneratorRule* rule : zObject->getGeneratorRules())
  271. {
  272. rules->addValue(
  273. Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
  274. }
  275. result->addValue("blocks", rules);
  276. Framework::JSON::JSONArray* entities = new Framework::JSON::JSONArray();
  277. for (EntityGenerator* entity : zObject->getEntityGenerators())
  278. {
  279. entities->addValue(
  280. Game::INSTANCE->zTypeRegistry()->toJson<EntityGenerator>(entity));
  281. }
  282. result->addValue("entities", entities);
  283. Framework::JSON::JSONArray* plants = new Framework::JSON::JSONArray();
  284. for (PlantConfig* plant : zObject->getPlantConfigs())
  285. {
  286. plants->addValue(
  287. Game::INSTANCE->zTypeRegistry()->toJson<PlantConfig>(plant));
  288. }
  289. result->addValue("plants", plants);
  290. return result;
  291. }
  292. JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
  293. JSONObjectValidationBuilder* builder) const
  294. {
  295. return builder->withRequiredString("name")
  296. ->finishString()
  297. ->withRequiredAttribute("condition",
  298. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>(),
  299. false,
  300. true)
  301. ->withRequiredArray("structurCollections")
  302. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  303. ->getValidator<StructureTemplateCollection>())
  304. ->finishArray()
  305. ->withRequiredArray("blocks")
  306. ->addAcceptedTypeInArray(
  307. Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
  308. ->finishArray()
  309. ->withRequiredArray("entities")
  310. ->addAcceptedTypeInArray(
  311. Game::INSTANCE->zTypeRegistry()->getValidator<EntityGenerator>())
  312. ->finishArray()
  313. ->withRequiredArray("plants")
  314. ->addAcceptedTypeInArray(
  315. Game::INSTANCE->zTypeRegistry()->getValidator<PlantConfig>())
  316. ->finishArray();
  317. }