| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #include "PlantConfig.h"
- #include "Chunk.h"
- #include "Dimension.h"
- #include "Game.h"
- #include "JNoise.h"
- PlantConfig::PlantConfig()
- : Framework::ReferenceCounter(),
- condition(0),
- noise(0),
- noiseConfig(0),
- threshold(0.0),
- locations(0),
- plantBlockTypeName(""),
- plantblockTypeId(0),
- plantHeight(0),
- direction(PlantDirection::UP),
- orientationConfig(0)
- {}
- PlantConfig::~PlantConfig()
- {
- if (condition)
- {
- condition->release();
- }
- if (noise)
- {
- noise->release();
- }
- if (noiseConfig)
- {
- noiseConfig->release();
- }
- if (orientationConfig)
- {
- orientationConfig->release();
- }
- }
- void PlantConfig::initialize(JExpressionMemory* zMemory)
- {
- condition->compile(zMemory);
- if (noise)
- {
- noise->release();
- }
- noise = JNoise::parseNoise(noiseConfig, zMemory);
- plantblockTypeId = Game::INSTANCE->getBlockTypeId(plantBlockTypeName);
- }
- double PlantConfig::doesGeneratePlant(int x,
- int y,
- int z,
- int dimensionId,
- Chunk* zChunk,
- bool underground,
- bool underwater,
- bool surface,
- int seaFluidBlockTypeId)
- {
- if (underwater && !(locations & PlantLocation::UNDERWATER))
- {
- return 0.0;
- }
- if (surface && !(locations & PlantLocation::SURFACE))
- {
- return 0.0;
- }
- if (!underwater && underground && !(locations & PlantLocation::CAVE))
- {
- return 0.0;
- }
- if (!underwater && !underground && !surface
- && !(locations & PlantLocation::ABOVE_SURFACE))
- {
- return 0.0;
- }
- if (z + plantHeight > WORLD_HEIGHT)
- {
- return 0.0;
- }
- if (!condition->getValue())
- {
- return 0.0;
- }
- if (!noise)
- {
- return 0.0;
- }
- return noise->getNoise((double)x, (double)y, (double)z) - threshold;
- }
- void PlantConfig::generatePlantAt(
- int x, int y, int z, int dimensionId, Chunk* zChunk)
- {
- Direction frontDirection = orientationConfig
- ? orientationConfig->calculateOrientation(
- {x, y, z}, dimensionId, zChunk)
- : NO_DIRECTION;
- for (int i = 0; i < plantHeight; i++)
- {
- Framework::Vec3<int> pos(x, y, z);
- if (direction == PlantDirection::UP)
- {
- pos = {x, y, z + i};
- }
- else if (direction == PlantDirection::DOWN)
- {
- pos = {x, y, z - i};
- }
- int currentType = zChunk->getBlockTypeAtWC(pos.x, pos.y, pos.z);
- if (currentType != BlockTypeEnum::AIR
- && currentType != BlockTypeEnum::NO_BLOCK)
- {
- break;
- }
- if (orientationConfig
- && frontDirection
- != Game::INSTANCE->zBlockType(plantblockTypeId)
- ->getDefaultFrontDirection())
- {
- Block* block = Game::INSTANCE->zBlockType(plantblockTypeId)
- ->createBlockAt(pos, dimensionId, 0);
- block->setFrontDirection(frontDirection);
- zChunk->putBlockAt(Dimension::chunkCoordinates(pos), block);
- }
- else
- {
- zChunk->putBlockTypeAt(
- Dimension::chunkCoordinates(pos), plantblockTypeId);
- }
- }
- }
- void PlantConfig::setCondition(JBoolExpression* condition)
- {
- if (this->condition)
- {
- this->condition->release();
- }
- this->condition = condition;
- }
- JBoolExpression* PlantConfig::zCondition() const
- {
- return condition;
- }
- void PlantConfig::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
- {
- if (this->noiseConfig)
- {
- this->noiseConfig->release();
- }
- this->noiseConfig = noiseConfig;
- }
- Framework::JSON::JSONObject* PlantConfig::zNoiseConfig() const
- {
- return noiseConfig;
- }
- void PlantConfig::setThreshold(double threshold)
- {
- this->threshold = threshold;
- }
- double PlantConfig::getThreshold() const
- {
- return threshold;
- }
- void PlantConfig::setLocations(int locations)
- {
- this->locations = locations;
- }
- int PlantConfig::getLocations() const
- {
- return locations;
- }
- void PlantConfig::setPlantBlockTypeName(Framework::Text name)
- {
- this->plantBlockTypeName = name;
- }
- Framework::Text PlantConfig::getPlantBlockTypeName() const
- {
- return plantBlockTypeName;
- }
- void PlantConfig::setPlantHeight(int height)
- {
- this->plantHeight = height;
- }
- int PlantConfig::getPlantHeight() const
- {
- return plantHeight;
- }
- void PlantConfig::setDirection(int direction)
- {
- this->direction = direction;
- }
- int PlantConfig::getDirection() const
- {
- return direction;
- }
- void PlantConfig::setOrientationConfig(OrientationConfig* orientationConfig)
- {
- if (this->orientationConfig)
- {
- this->orientationConfig->release();
- }
- this->orientationConfig = orientationConfig;
- }
- OrientationConfig* PlantConfig::zOrientationConfig() const
- {
- return orientationConfig;
- }
- PlantConfigFactory::PlantConfigFactory()
- : ObjectTypeFactory<PlantConfig>()
- {}
- PlantConfig* PlantConfigFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- PlantConfig* config = new PlantConfig();
- config->setCondition(
- Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
- zJson->zValue("condition")));
- config->setNoiseConfig(zJson->getValue("noise")->asObject());
- config->setThreshold(zJson->zValue("threshold")->asNumber()->getNumber());
- config->setLocations(0);
- Framework::JSON::JSONArray* zLocationsArray
- = zJson->zValue("locations")->asArray();
- for (int i = 0; i < zLocationsArray->getLength(); i++)
- {
- Framework::Text locationStr
- = zLocationsArray->zValue(i)->asString()->getString();
- if (locationStr.isEqual("CAVE"))
- {
- config->setLocations(config->getLocations() | PlantLocation::CAVE);
- }
- else if (locationStr.isEqual("UNDERWATER"))
- {
- config->setLocations(
- config->getLocations() | PlantLocation::UNDERWATER);
- }
- else if (locationStr.isEqual("SURFACE"))
- {
- config->setLocations(
- config->getLocations() | PlantLocation::SURFACE);
- }
- else if (locationStr.isEqual("ABOVE_SURFACE"))
- {
- config->setLocations(
- config->getLocations() | PlantLocation::ABOVE_SURFACE);
- }
- }
- config->setPlantBlockTypeName(
- zJson->zValue("plantBlock")->asString()->getString());
- config->setPlantHeight(
- (int)zJson->zValue("plantHeight")->asNumber()->getNumber());
- Framework::Text locationStr
- = zJson->zValue("direction")->asString()->getString();
- if (locationStr.isEqual("UP"))
- {
- config->setDirection(PlantDirection::UP);
- }
- else if (locationStr.isEqual("DOWN"))
- {
- config->setDirection(PlantDirection::DOWN);
- }
- if (zJson->hasValue("orientation"))
- {
- config->setOrientationConfig(
- Game::INSTANCE->zTypeRegistry()->fromJson<OrientationConfig>(
- zJson->zValue("orientation")));
- }
- return config;
- }
- Framework::JSON::JSONObject* PlantConfigFactory::toJsonObject(
- PlantConfig* zObject) const
- {
- Framework::JSON::JSONObject* zJson = new Framework::JSON::JSONObject();
- zJson->addValue("condition",
- Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
- zObject->zCondition()));
- zJson->addValue("noise",
- dynamic_cast<Framework::JSON::JSONObject*>(
- zObject->zNoiseConfig()->getThis()));
- zJson->addValue(
- "threshold", new Framework::JSON::JSONNumber(zObject->getThreshold()));
- Framework::JSON::JSONArray* zLocationsArray
- = new Framework::JSON::JSONArray();
- int locations = zObject->getLocations();
- if (locations & PlantLocation::CAVE)
- {
- zLocationsArray->addValue(new Framework::JSON::JSONString("CAVE"));
- }
- if (locations & PlantLocation::UNDERWATER)
- {
- zLocationsArray->addValue(
- new Framework::JSON::JSONString("UNDERWATER"));
- }
- if (locations & PlantLocation::SURFACE)
- {
- zLocationsArray->addValue(new Framework::JSON::JSONString("SURFACE"));
- }
- if (locations & PlantLocation::ABOVE_SURFACE)
- {
- zLocationsArray->addValue(
- new Framework::JSON::JSONString("ABOVE_SURFACE"));
- }
- zJson->addValue("locations", zLocationsArray);
- zJson->addValue("plantBlock",
- new Framework::JSON::JSONString(zObject->getPlantBlockTypeName()));
- zJson->addValue("plantHeight",
- new Framework::JSON::JSONNumber(zObject->getPlantHeight()));
- if (zObject->getDirection() == PlantDirection::UP)
- {
- zJson->addValue("direction", new Framework::JSON::JSONString("UP"));
- }
- else if (zObject->getDirection() == PlantDirection::DOWN)
- {
- zJson->addValue("direction", new Framework::JSON::JSONString("DOWN"));
- }
- if (zObject->zOrientationConfig())
- {
- zJson->addValue("orientation",
- Game::INSTANCE->zTypeRegistry()->toJson<OrientationConfig>(
- zObject->zOrientationConfig()));
- }
- return zJson;
- }
- JSONObjectValidationBuilder* PlantConfigFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder
- ->withRequiredAttribute("condition",
- Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
- ->withRequiredAttribute("noise", JNoise::getValidator(false))
- ->withRequiredNumber("threshold")
- ->withDefault(0.5)
- ->finishNumber()
- ->withRequiredArray("locations")
- ->addAcceptedStringInArray()
- ->whichIsOneOf({"CAVE", "UNDERWATER", "SURFACE", "ABOVE_SURFACE"})
- ->finishString()
- ->finishArray()
- ->withRequiredAttribute("plantBlock",
- Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
- BlockTypeNameFactory::TYPE_ID))
- ->withRequiredNumber("plantHeight")
- ->whichIsGreaterOrEqual(1)
- ->finishNumber()
- ->withRequiredString("direction")
- ->whichIsOneOf({"UP", "DOWN"})
- ->withDefault("UP")
- ->finishString()
- ->withOptionalAttribute("orientation",
- Game::INSTANCE->zTypeRegistry()->getValidator<OrientationConfig>());
- }
|