GrasslandBiom.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "GrasslandBiom.h"
  2. #include "BasicBlocks.h"
  3. #include "BlockType.h"
  4. #include "FastNoiseLite.h"
  5. #include "FastNoiseWrapper.h"
  6. #include "NoBlock.h"
  7. #include "TreeTemplate.h"
  8. #include "Chunk.h"
  9. #include "Constants.h"
  10. GrasslandBiom::GrasslandBiom()
  11. : BiomGenerator()
  12. {
  13. addTemplateGenerator(new TreeTemplate(0.002f,
  14. BlockTypeEnum::WOOD_BIRCH,
  15. BlockTypeEnum::LEAVES_WOOD_BIRCH,
  16. 8,
  17. 15));
  18. addTemplateGenerator(new TreeTemplate(0.001f,
  19. BlockTypeEnum::WOOD_BEECH,
  20. BlockTypeEnum::LEAVES_WOOD_BEECH,
  21. 8,
  22. 13));
  23. addTemplateGenerator(new TreeTemplate(0.0005f,
  24. BlockTypeEnum::WOOD_OAK,
  25. BlockTypeEnum::LEAVES_WOOD_OAK,
  26. 10,
  27. 15));
  28. addTemplateGenerator(new TreeTemplate(0.00025f,
  29. BlockTypeEnum::WOOD_PINE,
  30. BlockTypeEnum::LEAVES_WOOD_PINE,
  31. 15,
  32. 24));
  33. heightNoise = 0;
  34. undergroundDirdNoise = 0;
  35. surfaceSandNoise = 0;
  36. undergroundGravelNoise = 0;
  37. }
  38. GrasslandBiom::~GrasslandBiom()
  39. {
  40. if (heightNoise) heightNoise->release();
  41. }
  42. Framework::Either<Block*, int> GrasslandBiom::generateAboveSurfaceBlock(
  43. int x, int y, int z, int surfaceHeight, Chunk* partialGeneratedChunk)
  44. {
  45. int cx = x % CHUNK_SIZE;
  46. int cy = y % CHUNK_SIZE;
  47. if (cx < 0) cx += CHUNK_SIZE;
  48. if (cy < 0) cy += CHUNK_SIZE;
  49. auto below = partialGeneratedChunk->zBlockAt(
  50. Framework::Vec3<int>(cx, cy, z - 1));
  51. if ((below.isA() && below.getA()->zBlockType()->getId() == BlockTypeEnum::DIRT)
  52. || (below.isB() && below.getB() == BlockTypeEnum::DIRT))
  53. return BlockTypeEnum::GRASS;
  54. return BlockTypeEnum::AIR;
  55. }
  56. Framework::Either<Block*, int> GrasslandBiom::generateSurfaceBlock(
  57. int x, int y, int z)
  58. {
  59. if (surfaceSandNoise->getNoise((double)x, (double)y, (double)z) < 0.35)
  60. {
  61. return BlockTypeEnum::SAND;
  62. }
  63. if (undergroundGravelNoise->getNoise((double)x, (double)y, (double)z)
  64. < 0.35)
  65. {
  66. return BlockTypeEnum::GRAVEL;
  67. }
  68. return BlockTypeEnum::DIRT;
  69. }
  70. Framework::Either<Block*, int> GrasslandBiom::generateBelowSurfaceBlock(
  71. int x, int y, int z)
  72. {
  73. if (undergroundDirdNoise->getNoise((double)x, (double)y, (double)z)
  74. < 0.35)
  75. {
  76. return BlockTypeEnum::DIRT;
  77. }
  78. if (undergroundGravelNoise->getNoise((double)x, (double)y, (double)z)
  79. < 0.35)
  80. {
  81. return BlockTypeEnum::GRAVEL;
  82. }
  83. return BlockTypeEnum::STONE;
  84. }
  85. Framework::Either<Block*, int> GrasslandBiom::generateCaveBlock(
  86. int x, int y, int z)
  87. {
  88. return BlockTypeEnum::AIR;
  89. }
  90. void GrasslandBiom::setSeed(int seed)
  91. {
  92. BiomGenerator::setSeed(seed);
  93. if (heightNoise) heightNoise->release();
  94. FastNoiseLite* noise = new FastNoiseLite(seed);
  95. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  96. FastNoiseWrapper* wrapper = new FastNoiseWrapper(noise, seed);
  97. wrapper->setMultiplier(0.2f);
  98. heightNoise = wrapper;
  99. noise = new FastNoiseLite(seed);
  100. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  101. noise->SetFrequency(0.15f);
  102. undergroundDirdNoise = new FastNoiseWrapper(noise, seed);
  103. noise = new FastNoiseLite(seed + 1);
  104. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  105. noise->SetFrequency(0.125f);
  106. undergroundGravelNoise = new FastNoiseWrapper(noise, seed + 1);
  107. noise = new FastNoiseLite(seed + 2);
  108. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  109. noise->SetFrequency(0.1f);
  110. surfaceSandNoise = new FastNoiseWrapper(noise, seed + 2);
  111. }
  112. Noise* GrasslandBiom::zHeightMapNoise()
  113. {
  114. return heightNoise;
  115. }