DimensionGenerator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 DimensionGeneratorFactory : public virtual Framework::ReferenceCounter
  12. {
  13. private:
  14. Framework::Text name;
  15. int dimensionId;
  16. public:
  17. DimensionGeneratorFactory(Framework::Text name, int dimensionId);
  18. virtual DimensionGenerator* createDimensionGenerator(
  19. int worldSeed, Framework::JSON::JSONValue* config)
  20. = 0;
  21. virtual Framework::JSON::Validator::JSONValidator* getConfigValidator() = 0;
  22. virtual Framework::JSON::JSONValue* getDefaultConfig() = 0;
  23. Framework::Text getName() const;
  24. int getDimensionId() const;
  25. };
  26. class WorldHeightLayer : public virtual Framework::ReferenceCounter
  27. {
  28. private:
  29. Noise* noise;
  30. Framework::Text name;
  31. JFloatExpression* value;
  32. public:
  33. WorldHeightLayer(
  34. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory);
  35. ~WorldHeightLayer();
  36. void calculateValue(JExpressionMemory* zMemory);
  37. static Framework::JSON::Validator::JSONValidator* getValidator();
  38. };
  39. class DimensionGenerator : public virtual Framework::ReferenceCounter
  40. {
  41. private:
  42. JExpressionMemory* jExpressionMemory;
  43. Framework::RCArray<WorldHeightLayer> heightLayers;
  44. const int dimensionId;
  45. protected:
  46. DimensionGenerator(Framework::JSON::JSONValue* zConfig, int worldSeed);
  47. ~DimensionGenerator();
  48. JExpressionMemory* zMemory() const;
  49. void calculateHeightLayers();
  50. public:
  51. virtual Chunk* generateChunk(int centerX, int centerY) = 0;
  52. virtual Framework::Either<Block*, int> generateBlock(
  53. Framework::Vec3<int> location)
  54. = 0;
  55. virtual bool spawnStructure(Framework::Vec3<int> location,
  56. std::function<bool(GeneratorTemplate* tmpl)> filter)
  57. = 0;
  58. int getDimensionId() const;
  59. template<typename T>
  60. static Framework::JSON::Validator::ObjectValidationBuilder<T>*
  61. addConfigAttributes(
  62. Framework::JSON::Validator::ObjectValidationBuilder<T>* zBuilder)
  63. {
  64. return zBuilder
  65. ->withRequiredAttribute(
  66. "dimensionSeed", JExpressionParser::getFloatValidator())
  67. ->withRequiredAttribute("heightLayers",
  68. Framework::JSON::Validator::JSONValidator::buildForArray()
  69. ->withDefault(new Framework::JSON::JSONArray())
  70. ->removeInvalidEntries()
  71. ->addAcceptedTypeInArray(WorldHeightLayer::getValidator())
  72. ->finishArray());
  73. }
  74. };
  75. class BiomGenerator;
  76. class BiomedCavedDimensionGenerator : public DimensionGenerator
  77. {
  78. private:
  79. Framework::RCArray<BiomGenerator> biomGenerators;
  80. CaveGenerator* caveGenerator;
  81. Noise* biomNoise;
  82. Framework::Vec3<int> minStructureOffset;
  83. Framework::Vec3<int> maxStructureOffset;
  84. BiomGenerator* zBiomGenerator();
  85. protected:
  86. Framework::RCArray<GeneratedStructure>* getGeneratedStructoresForArea(
  87. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos);
  88. public:
  89. BiomedCavedDimensionGenerator(
  90. Framework::JSON::JSONValue* zConfig, int worldSeed);
  91. ~BiomedCavedDimensionGenerator();
  92. Chunk* generateChunk(int centerX, int centerY);
  93. Framework::Either<Block*, int> generateBlock(Framework::Vec3<int> location);
  94. bool spawnStructure(Framework::Vec3<int> location,
  95. std::function<bool(GeneratorTemplate* tmpl)> filter);
  96. template<typename T>
  97. static Framework::JSON::Validator::ObjectValidationBuilder<T>*
  98. addConfigAttributes(
  99. Framework::JSON::Validator::ObjectValidationBuilder<T>* zBuilder)
  100. {
  101. return DimensionGenerator::addConfigAttributes(zBuilder
  102. ->withRequiredArray("bioms")
  103. ->addAcceptedTypeInArray(BiomGenerator::getConfigValidator())
  104. ->finishArray()
  105. ->withRequiredAttribute("biomNoise", JNoise::getValidator(false)));
  106. }
  107. };