BiomGenerator.cpp 9.3 KB

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