123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include "BiomGenerator.h"
- #include "Constants.h"
- #include "Dimension.h"
- #include "Entity.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 (EntityGenerator* entity : entityGenerators)
- {
- entity->initialize(zMemory);
- }
- for (StructureTemplateCollection* collection : templates)
- {
- collection->initialize(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);
- }
- }
- }
- void BiomGenerator::generateEntities(
- int x, int y, int z, int dimensionId, JExpressionMemory* zMemory)
- {
- for (EntityGenerator* entityGen : entityGenerators)
- {
- if (entityGen->isGenerated(x, y, z, dimensionId, zMemory))
- {
- Entity* entity = entityGen->generate(
- Framework::Vec3<float>((float)x, (float)y, (float)z),
- dimensionId);
- Dimension* dimension = Game::INSTANCE->zDimension(dimensionId);
- if (dimension)
- {
- dimension->addEntity(entity);
- }
- else
- {
- entity->release();
- }
- }
- }
- }
- 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;
- }
- 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<int> min = collection->getMinAffected();
- Framework::Vec3<int> 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<StructureTemplateCollection>&
- BiomGenerator::getTemplates()
- {
- return templates;
- }
- void BiomGenerator::addGeneratorRule(GeneratorRule* rule)
- {
- rules.add(rule);
- }
- const Framework::RCArray<GeneratorRule>&
- BiomGenerator::getGeneratorRules() const
- {
- return rules;
- }
- void BiomGenerator::addEntityGenerator(EntityGenerator* generator)
- {
- entityGenerators.add(generator);
- }
- const Framework::RCArray<EntityGenerator>&
- BiomGenerator::getEntityGenerators() const
- {
- return entityGenerators;
- }
- 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<JBoolExpression>(
- zJson->zValue("condition")));
- bool first = 1;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("structurCollections")->asArray())
- {
- StructureTemplateCollection* collection
- = Game::INSTANCE->zTypeRegistry()
- ->fromJson<StructureTemplateCollection>(value);
- zResult->addTemplate(collection);
- }
- for (Framework::JSON::JSONValue* value :
- *zJson->asObject()->zValue("blocks")->asArray())
- {
- zResult->addGeneratorRule(
- Game::INSTANCE->zTypeRegistry()->fromJson<GeneratorRule>(value));
- }
- for (Framework::JSON::JSONValue* value :
- *zJson->asObject()->zValue("entities")->asArray())
- {
- zResult->addEntityGenerator(
- Game::INSTANCE->zTypeRegistry()->fromJson<EntityGenerator>(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<JBoolExpression>(
- zObject->getCondition()));
- Framework::JSON::JSONArray* collections = new Framework::JSON::JSONArray();
- for (StructureTemplateCollection* collection : zObject->getTemplates())
- {
- collections->addValue(
- Game::INSTANCE->zTypeRegistry()
- ->toJson<StructureTemplateCollection>(collection));
- }
- zResult->addValue("structurCollections", collections);
- Framework::JSON::JSONArray* rules = new Framework::JSON::JSONArray();
- for (GeneratorRule* rule : zObject->getGeneratorRules())
- {
- rules->addValue(
- Game::INSTANCE->zTypeRegistry()->toJson<GeneratorRule>(rule));
- }
- zResult->addValue("blocks", rules);
- Framework::JSON::JSONArray* entities = new Framework::JSON::JSONArray();
- for (EntityGenerator* entity : zObject->getEntityGenerators())
- {
- entities->addValue(
- Game::INSTANCE->zTypeRegistry()->toJson<EntityGenerator>(entity));
- }
- zResult->addValue("entities", entities);
- }
- JSONObjectValidationBuilder* BiomGeneratorFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->withRequiredString("name")
- ->finishString()
- ->withRequiredAttribute("condition",
- Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
- ->withRequiredArray("structurCollections")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()
- ->getValidator<StructureTemplateCollection>())
- ->finishArray()
- ->withRequiredArray("blocks")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<GeneratorRule>())
- ->finishArray()
- ->withRequiredArray("entities")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<EntityGenerator>())
- ->finishArray();
- }
|