123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #pragma once
- #include <Array.h>
- #include <Either.h>
- #include <ReferenceCounter.h>
- #include "BiomGenerator.h"
- #include "CaveGenerator.h"
- #include "Chunk.h"
- #include "JNoise.h"
- #include "JsonExpression.h"
- class DimensionGenerator;
- class WorldHeightLayer : public virtual Framework::ReferenceCounter
- {
- private:
- Framework::JSON::JSONObject* noiseConfig;
- Noise* noise;
- Framework::Text name;
- JFloatExpression* value;
- public:
- WorldHeightLayer();
- ~WorldHeightLayer();
- void initialize(JExpressionMemory* zMemory);
- void calculateValue(JExpressionMemory* zMemory);
- void setNoiseConfig(Framework::JSON::JSONObject* noiseConfig);
- Framework::JSON::JSONObject* zNoiseConfig() const;
- void setName(Framework::Text name);
- Framework::Text getName() const;
- void setValue(JFloatExpression* value);
- JFloatExpression* zValue() const;
- };
- class WorldHeightLayerFactory : public TypeFactory<WorldHeightLayer>
- {
- public:
- WorldHeightLayerFactory();
- WorldHeightLayer* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(WorldHeightLayer* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(WorldHeightLayer* zObject,
- Framework::JSON::JSONObject* zResult) const override;
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override;
- };
- class DimensionGenerator : public virtual Framework::ReferenceCounter
- {
- private:
- JExpressionMemory* jExpressionMemory;
- JFloatExpression* seedExpression;
- Framework::RCArray<WorldHeightLayer> heightLayers;
- Framework::Text name;
- int dimensionId;
- protected:
- DimensionGenerator();
- ~DimensionGenerator();
- JExpressionMemory* zMemory() const;
- void calculateHeightLayers();
- public:
- Dimension* createDimension();
- virtual void initialize(int worldSeed);
- virtual Chunk* generateChunk(int centerX, int centerY) = 0;
- virtual Framework::Either<Block*, int> generateBlock(
- Framework::Vec3<int> location)
- = 0;
- virtual bool spawnStructure(Framework::Vec3<int> location,
- std::function<bool(GeneratorTemplate* tmpl)> filter)
- = 0;
- virtual void generateEntities(Chunk* zChunk) = 0;
- int getDimensionId() const;
- void addHeightLayer(WorldHeightLayer* layer);
- const Framework::RCArray<WorldHeightLayer>& getHeightLayers() const;
- void setName(Framework::Text name);
- Framework::Text getName() const;
- void setId(int id);
- int getId() const;
- void setSeed(JFloatExpression* seed);
- JFloatExpression* zSeed() const;
- };
- template<typename S> class DimensionGeneratorFactory
- : public SubTypeFactory<DimensionGenerator, S>
- {
- public:
- DimensionGeneratorFactory()
- : SubTypeFactory<DimensionGenerator, S>()
- {}
- void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- DimensionGenerator* zGenerator
- = dynamic_cast<DimensionGenerator*>(zResult);
- zGenerator->setName(zJson->zValue("name")->asString()->getString());
- zGenerator->setId((int)zJson->zValue("id")->asNumber()->getNumber());
- zGenerator->setSeed(
- Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
- zJson->zValue("dimensionSeed")));
- for (Framework::JSON::JSONValue* layer :
- *zJson->zValue("heightLayers")->asArray())
- {
- zGenerator->addHeightLayer(
- Game::INSTANCE->zTypeRegistry()->fromJson<WorldHeightLayer>(
- layer));
- }
- }
- void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- DimensionGenerator* zGenerator
- = dynamic_cast<DimensionGenerator*>(zObject);
- zResult->addValue(
- "name", new Framework::JSON::JSONString(zGenerator->getName()));
- zResult->addValue(
- "id", new Framework::JSON::JSONNumber(zGenerator->getId()));
- zResult->addValue("dimensionSeed",
- Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
- zGenerator->zSeed()));
- Framework::JSON::JSONArray* hightLayers
- = new Framework::JSON::JSONArray();
- for (WorldHeightLayer* layer : zGenerator->getHeightLayers())
- {
- hightLayers->addValue(
- Game::INSTANCE->zTypeRegistry()->toJson(layer));
- }
- zResult->addValue("heightLayers", hightLayers);
- }
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- return builder
- ->withRequiredAttribute("dimensionSeed",
- Game::INSTANCE->zTypeRegistry()
- ->getValidator<JFloatExpression>())
- ->withRequiredArray("heightLayers")
- ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
- ->getValidator<WorldHeightLayer>())
- ->finishArray()
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredNumber("id")
- ->finishNumber();
- }
- };
- class BiomGenerator;
- class BiomedCavedDimensionGenerator : public DimensionGenerator
- {
- private:
- Framework::RCArray<BiomGenerator> biomGenerators;
- CaveGenerator* caveGenerator;
- Framework::JSON::JSONObject* noiseConfig;
- Noise* biomNoise;
- Framework::Vec3<int> minStructureOffset;
- Framework::Vec3<int> maxStructureOffset;
- BiomGenerator* zBiomGenerator();
- protected:
- Framework::RCArray<GeneratedStructure>* getGeneratedStructoresForArea(
- Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos);
- public:
- BiomedCavedDimensionGenerator();
- ~BiomedCavedDimensionGenerator();
- virtual void initialize(int worldSeed) override;
- Chunk* generateChunk(int centerX, int centerY);
- void generateEntities(Chunk* zChunk);
- Framework::Either<Block*, int> generateBlock(Framework::Vec3<int> location);
- bool spawnStructure(Framework::Vec3<int> location,
- std::function<bool(GeneratorTemplate* tmpl)> filter);
- void addBiomGenerator(BiomGenerator* biomGenerator);
- const Framework::RCArray<BiomGenerator>& getBiomGenerators() const;
- void setBiomNoiseConfig(Framework::JSON::JSONObject* biomNoiseConfig);
- Framework::JSON::JSONObject* zBiomNoiseConfig() const;
- };
- class BiomedCavedDimensionGeneratorFactory
- : public DimensionGeneratorFactory<BiomedCavedDimensionGenerator>
- {
- public:
- BiomedCavedDimensionGeneratorFactory();
- BiomedCavedDimensionGenerator* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(BiomedCavedDimensionGenerator* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(BiomedCavedDimensionGenerator* zObject,
- Framework::JSON::JSONObject* zResult) const override;
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override;
- Framework::Text getTypeToken() const override;
- };
|