DimensionGenerator.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. virtual void generateEntities(Chunk* zChunk) = 0;
  67. int getDimensionId() const;
  68. void addHeightLayer(WorldHeightLayer* layer);
  69. const Framework::RCArray<WorldHeightLayer>& getHeightLayers() const;
  70. void setName(Framework::Text name);
  71. Framework::Text getName() const;
  72. void setId(int id);
  73. int getId() const;
  74. void setSeed(JFloatExpression* seed);
  75. JFloatExpression* zSeed() const;
  76. };
  77. template<typename S> class DimensionGeneratorFactory
  78. : public SubTypeFactory<DimensionGenerator, S>
  79. {
  80. public:
  81. DimensionGeneratorFactory()
  82. : SubTypeFactory<DimensionGenerator, S>()
  83. {}
  84. void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
  85. {
  86. DimensionGenerator* zGenerator
  87. = dynamic_cast<DimensionGenerator*>(zResult);
  88. zGenerator->setName(zJson->zValue("name")->asString()->getString());
  89. zGenerator->setId((int)zJson->zValue("id")->asNumber()->getNumber());
  90. zGenerator->setSeed(
  91. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  92. zJson->zValue("dimensionSeed")));
  93. for (Framework::JSON::JSONValue* layer :
  94. *zJson->zValue("heightLayers")->asArray())
  95. {
  96. zGenerator->addHeightLayer(
  97. Game::INSTANCE->zTypeRegistry()->fromJson<WorldHeightLayer>(
  98. layer));
  99. }
  100. }
  101. void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
  102. {
  103. DimensionGenerator* zGenerator
  104. = dynamic_cast<DimensionGenerator*>(zObject);
  105. zResult->addValue(
  106. "name", new Framework::JSON::JSONString(zGenerator->getName()));
  107. zResult->addValue(
  108. "id", new Framework::JSON::JSONNumber(zGenerator->getId()));
  109. zResult->addValue("dimensionSeed",
  110. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  111. zGenerator->zSeed()));
  112. Framework::JSON::JSONArray* hightLayers
  113. = new Framework::JSON::JSONArray();
  114. for (WorldHeightLayer* layer : zGenerator->getHeightLayers())
  115. {
  116. hightLayers->addValue(
  117. Game::INSTANCE->zTypeRegistry()->toJson(layer));
  118. }
  119. zResult->addValue("heightLayers", hightLayers);
  120. }
  121. JSONObjectValidationBuilder* addToValidator(
  122. JSONObjectValidationBuilder* builder) const override
  123. {
  124. return builder
  125. ->withRequiredAttribute("dimensionSeed",
  126. Game::INSTANCE->zTypeRegistry()
  127. ->getValidator<JFloatExpression>())
  128. ->withRequiredArray("heightLayers")
  129. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  130. ->getValidator<WorldHeightLayer>())
  131. ->finishArray()
  132. ->withRequiredString("name")
  133. ->finishString()
  134. ->withRequiredNumber("id")
  135. ->finishNumber();
  136. }
  137. };
  138. class BiomGenerator;
  139. class BiomedCavedDimensionGenerator : public DimensionGenerator
  140. {
  141. private:
  142. Framework::RCArray<BiomGenerator> biomGenerators;
  143. CaveGenerator* caveGenerator;
  144. Framework::JSON::JSONObject* noiseConfig;
  145. Noise* biomNoise;
  146. Framework::Vec3<int> minStructureOffset;
  147. Framework::Vec3<int> maxStructureOffset;
  148. BiomGenerator* zBiomGenerator();
  149. protected:
  150. Framework::RCArray<GeneratedStructure>* getGeneratedStructoresForArea(
  151. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos);
  152. public:
  153. BiomedCavedDimensionGenerator();
  154. ~BiomedCavedDimensionGenerator();
  155. virtual void initialize(int worldSeed) override;
  156. Chunk* generateChunk(int centerX, int centerY);
  157. void generateEntities(Chunk* zChunk);
  158. Framework::Either<Block*, int> generateBlock(Framework::Vec3<int> location);
  159. bool spawnStructure(Framework::Vec3<int> location,
  160. std::function<bool(GeneratorTemplate* tmpl)> filter);
  161. void addBiomGenerator(BiomGenerator* biomGenerator);
  162. const Framework::RCArray<BiomGenerator>& getBiomGenerators() const;
  163. void setBiomNoiseConfig(Framework::JSON::JSONObject* biomNoiseConfig);
  164. Framework::JSON::JSONObject* zBiomNoiseConfig() const;
  165. };
  166. class BiomedCavedDimensionGeneratorFactory
  167. : public DimensionGeneratorFactory<BiomedCavedDimensionGenerator>
  168. {
  169. public:
  170. BiomedCavedDimensionGeneratorFactory();
  171. BiomedCavedDimensionGenerator* createValue(
  172. Framework::JSON::JSONObject* zJson) const override;
  173. void fromJson(BiomedCavedDimensionGenerator* zResult,
  174. Framework::JSON::JSONObject* zJson) const override;
  175. void toJson(BiomedCavedDimensionGenerator* zObject,
  176. Framework::JSON::JSONObject* zResult) const override;
  177. JSONObjectValidationBuilder* addToValidator(
  178. JSONObjectValidationBuilder* builder) const override;
  179. Framework::Text getTypeToken() const override;
  180. };