123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include "BiomGenerator.h"
- #include "Game.h"
- #include "JNoise.h"
- BiomGenerator::BiomGenerator(
- Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
- : ReferenceCounter()
- {
- name = zConfig->asObject()->zValue("name")->asString()->getString();
- condition = JExpressionParser::parseBoolExpression(
- zConfig->asObject()->zValue("condition"));
- bool first = 1;
- for (Framework::JSON::JSONValue* value :
- *zConfig->asObject()->zValue("structurCollections")->asArray())
- {
- StructureTemplateCollection* collection
- = new StructureTemplateCollection(value, zMemory);
- templates.add(collection);
- if (first)
- {
- minStructureOffset = collection->getMinAffected();
- maxStructureOffset = collection->getMaxAffected();
- first = 0;
- }
- else
- {
- Framework::Vec3<int> min = collection->getMinAffected();
- Framework::Vec3<int> max = collection->getMaxAffected();
- 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;
- }
- }
- for (Framework::JSON::JSONValue* value :
- *zConfig->asObject()->zValue("blocks")->asArray())
- {
- rules.add(Game::INSTANCE->zTypeRegistry()->createGeneratorRule(
- value, zMemory));
- }
- }
- Framework::Either<Block*, int> 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<int> minPos,
- Framework::Vec3<int> maxPos,
- Framework::RCArray<GeneratedStructure>* 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<StructureTemplateCollection>&
- BiomGenerator::getTemplates() const
- {
- return templates;
- }
- Framework::Vec3<int> BiomGenerator::getMinStructureOffset() const
- {
- return minStructureOffset;
- }
- Framework::Vec3<int> BiomGenerator::getMaxStructureOffset() const
- {
- return maxStructureOffset;
- }
- Framework::JSON::Validator::JSONValidator* BiomGenerator::getConfigValidator()
- {
- return Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredAttribute(
- "condition", JExpressionParser::getBoolValidator())
- ->withRequiredArray("structurCollections")
- ->addAcceptedTypeInArray(
- StructureTemplateCollection::getConfigValidator())
- ->finishArray()
- ->withRequiredArray("blocks")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getGeneratorRuleValidator())
- ->finishArray()
- ->finishObject();
- }
|