DimensionGenerator.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include <Array.h>
  3. #include <Either.h>
  4. #include <ReferenceCounter.h>
  5. #include "BiomGenerator.h"
  6. #include "CaveGenerator.h"
  7. #include "Chunk.h"
  8. #include "JNoise.h"
  9. #include "JsonExpression.h"
  10. class DimensionGenerator;
  11. class WorldHeightLayer : public virtual Framework::ReferenceCounter
  12. {
  13. private:
  14. Framework::JSON::JSONObject* noiseConfig;
  15. Noise* noise;
  16. Framework::Text name;
  17. JFloatExpression* value;
  18. public:
  19. WorldHeightLayer();
  20. ~WorldHeightLayer();
  21. void initialize(JExpressionMemory* zMemory);
  22. void calculateValue(JExpressionMemory* zMemory);
  23. void setNoiseConfig(Framework::JSON::JSONObject* noiseConfig);
  24. Framework::JSON::JSONObject* zNoiseConfig() const;
  25. void setName(Framework::Text name);
  26. Framework::Text getName() const;
  27. void setValue(JFloatExpression* value);
  28. JFloatExpression* zValue() const;
  29. };
  30. class WorldHeightLayerFactory : public TypeFactory<WorldHeightLayer>
  31. {
  32. public:
  33. WorldHeightLayerFactory();
  34. WorldHeightLayer* createValue(
  35. Framework::JSON::JSONObject* zJson) const override;
  36. void fromJson(WorldHeightLayer* zResult,
  37. Framework::JSON::JSONObject* zJson) const override;
  38. void toJson(WorldHeightLayer* zObject,
  39. Framework::JSON::JSONObject* zResult) const override;
  40. JSONObjectValidationBuilder* addToValidator(
  41. JSONObjectValidationBuilder* builder) const override;
  42. };
  43. class DimensionGenerator : public virtual Framework::ReferenceCounter
  44. {
  45. private:
  46. JExpressionMemory* jExpressionMemory;
  47. JFloatExpression* seedExpression;
  48. Framework::RCArray<WorldHeightLayer> heightLayers;
  49. Framework::Text name;
  50. int dimensionId;
  51. protected:
  52. DimensionGenerator();
  53. ~DimensionGenerator();
  54. JExpressionMemory* zMemory() const;
  55. void calculateHeightLayers();
  56. public:
  57. Dimension *createDimension();
  58. virtual void initialize(int worldSeed);
  59. virtual Chunk* generateChunk(int centerX, int centerY) = 0;
  60. virtual Framework::Either<Block*, int> generateBlock(
  61. Framework::Vec3<int> location)
  62. = 0;
  63. virtual bool spawnStructure(Framework::Vec3<int> location,
  64. std::function<bool(GeneratorTemplate* tmpl)> filter)
  65. = 0;
  66. int getDimensionId() const;
  67. void addHeightLayer(WorldHeightLayer* layer);
  68. const Framework::RCArray<WorldHeightLayer>& getHeightLayers() const;
  69. void setName(Framework::Text name);
  70. Framework::Text getName() const;
  71. void setId(int id);
  72. int getId() const;
  73. void setSeed(JFloatExpression* seed);
  74. JFloatExpression* zSeed() const;
  75. };
  76. template<typename S> class DimensionGeneratorFactory
  77. : public SubTypeFactory<DimensionGenerator, S>
  78. {
  79. public:
  80. DimensionGeneratorFactory()
  81. : SubTypeFactory<DimensionGenerator, S>()
  82. {}
  83. void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
  84. {
  85. DimensionGenerator* zGenerator
  86. = dynamic_cast<DimensionGenerator*>(zResult);
  87. zGenerator->setName(zJson->zValue("name")->asString()->getString());
  88. zGenerator->setId((int)zJson->zValue("id")->asNumber()->getNumber());
  89. zGenerator->setSeed(
  90. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  91. zJson->zValue("dimensionSeed")));
  92. for (Framework::JSON::JSONValue* layer :
  93. *zJson->zValue("heightLayers")->asArray())
  94. {
  95. zGenerator->addHeightLayer(
  96. Game::INSTANCE->zTypeRegistry()->fromJson<WorldHeightLayer>(
  97. layer));
  98. }
  99. }
  100. void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
  101. {
  102. DimensionGenerator* zGenerator
  103. = dynamic_cast<DimensionGenerator*>(zObject);
  104. zResult->addValue(
  105. "name", new Framework::JSON::JSONString(zGenerator->getName()));
  106. zResult->addValue(
  107. "id", new Framework::JSON::JSONNumber(zGenerator->getId()));
  108. zResult->addValue("dimensionSeed",
  109. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  110. zGenerator->zSeed()));
  111. Framework::JSON::JSONArray* hightLayers
  112. = new Framework::JSON::JSONArray();
  113. for (WorldHeightLayer* layer : zGenerator->getHeightLayers())
  114. {
  115. hightLayers->addValue(
  116. Game::INSTANCE->zTypeRegistry()->toJson(layer));
  117. }
  118. zResult->addValue("heightLayers", hightLayers);
  119. }
  120. JSONObjectValidationBuilder* addToValidator(
  121. JSONObjectValidationBuilder* builder) const override
  122. {
  123. return builder
  124. ->withRequiredAttribute("dimensionSeed",
  125. Game::INSTANCE->zTypeRegistry()
  126. ->getValidator<JFloatExpression>())
  127. ->withRequiredArray("heightLayers")
  128. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  129. ->getValidator<WorldHeightLayer>())
  130. ->finishArray()
  131. ->withRequiredString("name")
  132. ->finishString()
  133. ->withRequiredNumber("id")
  134. ->finishNumber();
  135. }
  136. };
  137. class BiomGenerator;
  138. class BiomedCavedDimensionGenerator : public DimensionGenerator
  139. {
  140. private:
  141. Framework::RCArray<BiomGenerator> biomGenerators;
  142. CaveGenerator* caveGenerator;
  143. Framework::JSON::JSONObject* noiseConfig;
  144. Noise* biomNoise;
  145. Framework::Vec3<int> minStructureOffset;
  146. Framework::Vec3<int> maxStructureOffset;
  147. BiomGenerator* zBiomGenerator();
  148. protected:
  149. Framework::RCArray<GeneratedStructure>* getGeneratedStructoresForArea(
  150. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos);
  151. public:
  152. BiomedCavedDimensionGenerator();
  153. ~BiomedCavedDimensionGenerator();
  154. virtual void initialize(int worldSeed) override;
  155. Chunk* generateChunk(int centerX, int centerY);
  156. Framework::Either<Block*, int> generateBlock(Framework::Vec3<int> location);
  157. bool spawnStructure(Framework::Vec3<int> location,
  158. std::function<bool(GeneratorTemplate* tmpl)> filter);
  159. void addBiomGenerator(BiomGenerator* biomGenerator);
  160. const Framework::RCArray<BiomGenerator>& getBiomGenerators() const;
  161. void setBiomNoiseConfig(Framework::JSON::JSONObject* biomNoiseConfig);
  162. Framework::JSON::JSONObject* zBiomNoiseConfig() const;
  163. };
  164. class BiomedCavedDimensionGeneratorFactory
  165. : public DimensionGeneratorFactory<BiomedCavedDimensionGenerator>
  166. {
  167. public:
  168. BiomedCavedDimensionGeneratorFactory();
  169. BiomedCavedDimensionGenerator* createValue(
  170. Framework::JSON::JSONObject* zJson) const override;
  171. void fromJson(BiomedCavedDimensionGenerator* zResult,
  172. Framework::JSON::JSONObject* zJson) const override;
  173. void toJson(BiomedCavedDimensionGenerator* zObject,
  174. Framework::JSON::JSONObject* zResult) const override;
  175. JSONObjectValidationBuilder* addToValidator(
  176. JSONObjectValidationBuilder* builder) const override;
  177. Framework::Text getTypeToken() const override;
  178. };