DimensionGenerator.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 ObjectTypeFactory<WorldHeightLayer>
  31. {
  32. public:
  33. WorldHeightLayerFactory();
  34. WorldHeightLayer* fromJson(
  35. Framework::JSON::JSONObject* zJson) const override;
  36. Framework::JSON::JSONObject* toJsonObject(
  37. WorldHeightLayer* zObject) const override;
  38. JSONObjectValidationBuilder* addToValidator(
  39. JSONObjectValidationBuilder* builder) const override;
  40. };
  41. class DimensionGenerator : public virtual Framework::ReferenceCounter
  42. {
  43. private:
  44. JExpressionMemory* jExpressionMemory;
  45. JFloatExpression* seedExpression;
  46. Framework::RCArray<WorldHeightLayer> heightLayers;
  47. Framework::Text name;
  48. int dimensionId;
  49. protected:
  50. DimensionGenerator();
  51. ~DimensionGenerator();
  52. JExpressionMemory* zMemory() const;
  53. void calculateHeightLayers();
  54. public:
  55. Dimension* createDimension();
  56. virtual void initialize(int worldSeed);
  57. virtual Chunk* generateChunk(int centerX, int centerY) = 0;
  58. virtual Framework::Either<Block*, int> generateBlock(
  59. Framework::Vec3<int> location)
  60. = 0;
  61. virtual bool spawnStructure(Framework::Vec3<int> location,
  62. std::function<bool(GeneratorTemplate* tmpl)> filter)
  63. = 0;
  64. virtual void generateEntities(Chunk* zChunk) = 0;
  65. int getDimensionId() const;
  66. void addHeightLayer(WorldHeightLayer* layer);
  67. const Framework::RCArray<WorldHeightLayer>& getHeightLayers() const;
  68. void setName(Framework::Text name);
  69. Framework::Text getName() const;
  70. void setId(int id);
  71. int getId() const;
  72. void setSeed(JFloatExpression* seed);
  73. JFloatExpression* zSeed() const;
  74. };
  75. template<typename S> class DimensionGeneratorFactory
  76. : public SubTypeFactory<DimensionGenerator, S>
  77. {
  78. public:
  79. DimensionGeneratorFactory()
  80. : SubTypeFactory<DimensionGenerator, S>()
  81. {}
  82. S* fromJson(Framework::JSON::JSONObject* zJson) const override
  83. {
  84. S* result = createValue(zJson);
  85. DimensionGenerator* zGenerator
  86. = dynamic_cast<DimensionGenerator*>(result);
  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. return result;
  100. }
  101. Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  102. {
  103. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  104. DimensionGenerator* zGenerator
  105. = dynamic_cast<DimensionGenerator*>(zObject);
  106. result->addValue(
  107. "name", new Framework::JSON::JSONString(zGenerator->getName()));
  108. result->addValue(
  109. "id", new Framework::JSON::JSONNumber(zGenerator->getId()));
  110. result->addValue("dimensionSeed",
  111. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  112. zGenerator->zSeed()));
  113. Framework::JSON::JSONArray* hightLayers
  114. = new Framework::JSON::JSONArray();
  115. for (WorldHeightLayer* layer : zGenerator->getHeightLayers())
  116. {
  117. hightLayers->addValue(
  118. Game::INSTANCE->zTypeRegistry()->toJson(layer));
  119. }
  120. result->addValue("heightLayers", hightLayers);
  121. return result;
  122. }
  123. JSONObjectValidationBuilder* addToValidator(
  124. JSONObjectValidationBuilder* builder) const override
  125. {
  126. return builder
  127. ->withRequiredAttribute("dimensionSeed",
  128. Game::INSTANCE->zTypeRegistry()
  129. ->getValidator<JFloatExpression>())
  130. ->withRequiredArray("heightLayers")
  131. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  132. ->getValidator<WorldHeightLayer>())
  133. ->finishArray()
  134. ->withRequiredString("name")
  135. ->finishString()
  136. ->withRequiredNumber("id")
  137. ->finishNumber();
  138. }
  139. protected:
  140. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  141. };
  142. class BiomGenerator;
  143. class BiomedCavedDimensionGenerator : public DimensionGenerator
  144. {
  145. private:
  146. Framework::RCArray<BiomGenerator> biomGenerators;
  147. CaveGenerator* caveGenerator;
  148. Framework::JSON::JSONObject* noiseConfig;
  149. Noise* biomNoise;
  150. Framework::Vec3<int> minStructureOffset;
  151. Framework::Vec3<int> maxStructureOffset;
  152. BiomGenerator* zBiomGenerator();
  153. protected:
  154. Framework::RCArray<GeneratedStructure>* getGeneratedStructoresForArea(
  155. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos);
  156. public:
  157. BiomedCavedDimensionGenerator();
  158. ~BiomedCavedDimensionGenerator();
  159. virtual void initialize(int worldSeed) override;
  160. Chunk* generateChunk(int centerX, int centerY);
  161. void generateEntities(Chunk* zChunk);
  162. Framework::Either<Block*, int> generateBlock(Framework::Vec3<int> location);
  163. bool spawnStructure(Framework::Vec3<int> location,
  164. std::function<bool(GeneratorTemplate* tmpl)> filter);
  165. void addBiomGenerator(BiomGenerator* biomGenerator);
  166. const Framework::RCArray<BiomGenerator>& getBiomGenerators() const;
  167. void setBiomNoiseConfig(Framework::JSON::JSONObject* biomNoiseConfig);
  168. Framework::JSON::JSONObject* zBiomNoiseConfig() const;
  169. };
  170. class BiomedCavedDimensionGeneratorFactory
  171. : public DimensionGeneratorFactory<BiomedCavedDimensionGenerator>
  172. {
  173. public:
  174. BiomedCavedDimensionGeneratorFactory();
  175. BiomedCavedDimensionGenerator* createValue(
  176. Framework::JSON::JSONObject* zJson) const override;
  177. BiomedCavedDimensionGenerator* fromJson(
  178. Framework::JSON::JSONObject* zJson) const override;
  179. Framework::JSON::JSONObject* toJsonObject(
  180. BiomedCavedDimensionGenerator* zObject) const override;
  181. JSONObjectValidationBuilder* addToValidator(
  182. JSONObjectValidationBuilder* builder) const override;
  183. const char* getTypeToken() const override;
  184. };