#include "BiomGenerator.h" #include "Constants.h" #include "Game.h" #include "JNoise.h" BiomGenerator::BiomGenerator() : ReferenceCounter(), condition(0) {} BiomGenerator::~BiomGenerator() { if (condition) condition->release(); } void BiomGenerator::initialize(JExpressionMemory* zMemory) { for (GeneratorRule* rule : rules) { rule->initialize(zMemory); } for (StructureTemplateCollection* collection : templates) { collection->initialize(zMemory); } } Framework::Either BiomGenerator::generateBlock(int x, int y, int z, int dimensionId, JExpressionMemory* zMemory, Chunk* partialGeneratedChunk) { for (GeneratorRule* rule : rules) { if (rule->checkCondition(x, y, z, zMemory)) { auto result = rule->generateBlock(x, y, z, dimensionId); if ((result.isA() && result.getA()) || (result.isB() && result.getB())) { return result; } } } return BlockTypeEnum::AIR; } bool BiomGenerator::isApplicable(JExpressionMemory* zMemory) { return condition->getValue(zMemory); } void BiomGenerator::generateStructures(int x, int y, int z, int dimensionId, JExpressionMemory* zMemory, Framework::Vec3 minPos, Framework::Vec3 maxPos, Framework::RCArray* zResult) { int minSearchX = minPos.x - maxStructureOffset.x; int minSearchY = minPos.y - maxStructureOffset.y; int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0); int maxSearchX = maxPos.x - minStructureOffset.x; int maxSearchY = maxPos.y - minStructureOffset.y; int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1); if (x >= minSearchX && x <= maxSearchX && y >= minSearchY && y <= maxSearchY && z >= minSearchZ && z <= maxSearchZ) { for (StructureTemplateCollection* collection : templates) { collection->generateStructures( x, y, z, dimensionId, zMemory, minPos, maxPos, zResult); } } } const Framework::RCArray& BiomGenerator::getTemplates() const { return templates; } Framework::Vec3 BiomGenerator::getMinStructureOffset() const { return minStructureOffset; } Framework::Vec3 BiomGenerator::getMaxStructureOffset() const { return maxStructureOffset; } void BiomGenerator::setName(Framework::Text name) { this->name = name; } Framework::Text BiomGenerator::getName() const { return name; } void BiomGenerator::setCondition(JBoolExpression* condition) { if (this->condition) this->condition->release(); this->condition = condition; } JBoolExpression* BiomGenerator::getCondition() const { return condition; } void BiomGenerator::addTemplate(StructureTemplateCollection* collection) { templates.add(collection); Framework::Vec3 min = collection->getMinAffected(); Framework::Vec3 max = collection->getMaxAffected(); if (templates.getEintragAnzahl()) { minStructureOffset = min; maxStructureOffset = max; } else { if (minStructureOffset.x > min.x) minStructureOffset.x = min.x; if (minStructureOffset.y > min.y) minStructureOffset.y = min.y; if (minStructureOffset.z > min.z) minStructureOffset.z = min.z; if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x; if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y; if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z; } } const Framework::RCArray& BiomGenerator::getTemplates() { return templates; } void BiomGenerator::addGeneratorRule(GeneratorRule* rule) { rules.add(rule); } const Framework::RCArray& BiomGenerator::getGeneratorRules() const { return rules; } BiomGeneratorFactory::BiomGeneratorFactory() : TypeFactory() {} BiomGenerator* BiomGeneratorFactory::createValue( Framework::JSON::JSONObject* zJson) const { return new BiomGenerator(); } void BiomGeneratorFactory::fromJson( BiomGenerator* zResult, Framework::JSON::JSONObject* zJson) const { zResult->setName(zJson->zValue("name")->asString()->getString()); zResult->setCondition( Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("condition"))); bool first = 1; for (Framework::JSON::JSONValue* value : *zJson->zValue("structurCollections")->asArray()) { StructureTemplateCollection* collection = Game::INSTANCE->zTypeRegistry() ->fromJson(value); zResult->addTemplate(collection); } for (Framework::JSON::JSONValue* value : *zJson->asObject()->zValue("blocks")->asArray()) { zResult->addGeneratorRule( Game::INSTANCE->zTypeRegistry()->fromJson(value)); } } void BiomGeneratorFactory::toJson( BiomGenerator* zObject, Framework::JSON::JSONObject* zResult) const { zResult->addValue( "name", new Framework::JSON::JSONString(zObject->getName())); zResult->addValue("condition", Game::INSTANCE->zTypeRegistry()->toJson( zObject->getCondition())); Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray(); for (StructureTemplateCollection* collection : zObject->getTemplates()) { collections->addValue( Game::INSTANCE->zTypeRegistry() ->toJson(collection)); } zResult->addValue("structurCollections", collections); Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray(); for (GeneratorRule* rule : zObject->getGeneratorRules()) { rules->addValue( Game::INSTANCE->zTypeRegistry()->toJson(rule)); } zResult->addValue("blocks", rules); } JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredString("name") ->finishString() ->withRequiredAttribute("condition", Game::INSTANCE->zTypeRegistry()->getValidator()) ->withRequiredArray("structurCollections") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry() ->getValidator()) ->finishArray() ->withRequiredArray("blocks") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); }