GrasslandBiom.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "GrasslandBiom.h"
  2. #include "BasicBlocks.h"
  3. #include "BlockType.h"
  4. #include "Chunk.h"
  5. #include "Constants.h"
  6. #include "FastNoiseLite.h"
  7. #include "FastNoiseWrapper.h"
  8. #include "NoBlock.h"
  9. #include "TreeTemplate.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
  50. = partialGeneratedChunk->zBlockAt(Framework::Vec3<int>(cx, cy, z - 1));
  51. if ((below.isA()
  52. && below.getA()->zBlockType()->getId() == BlockTypeEnum::DIRT)
  53. || (below.isB() && below.getB() == BlockTypeEnum::DIRT))
  54. return BlockTypeEnum::GRASS;
  55. return BlockTypeEnum::AIR;
  56. }
  57. Framework::Either<Block*, int> GrasslandBiom::generateSurfaceBlock(
  58. int x, int y, int z)
  59. {
  60. if (surfaceSandNoise->getNoise((double)x, (double)y, (double)z) < 0.35)
  61. {
  62. return BlockTypeEnum::SAND;
  63. }
  64. if (undergroundGravelNoise->getNoise((double)x, (double)y, (double)z)
  65. < 0.35)
  66. {
  67. return BlockTypeEnum::GRAVEL;
  68. }
  69. return BlockTypeEnum::DIRT;
  70. }
  71. Framework::Either<Block*, int> GrasslandBiom::generateBelowSurfaceBlock(
  72. int x, int y, int z)
  73. {
  74. if (undergroundDirdNoise->getNoise((double)x, (double)y, (double)z) < 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. Framework::Either<Block*, int> GrasslandBiom::generateUnderWaterBlock(
  91. int x, int y, int z, int surfaceHeight, Chunk* partialGeneratedChunk)
  92. {
  93. return StaticRegistry<BlockType>::INSTANCE.zElement(BlockTypeEnum::WATER)
  94. ->createBlockAt(Framework::Vec3<int>(x, y, z), 0);
  95. }
  96. void GrasslandBiom::setSeed(int seed)
  97. {
  98. BiomGenerator::setSeed(seed);
  99. if (heightNoise) heightNoise->release();
  100. FastNoiseLite* noise = new FastNoiseLite(seed);
  101. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  102. FastNoiseWrapper* wrapper = new FastNoiseWrapper(noise, seed);
  103. wrapper->setMultiplier(0.2f);
  104. heightNoise = wrapper;
  105. noise = new FastNoiseLite(seed);
  106. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  107. noise->SetFrequency(0.15f);
  108. undergroundDirdNoise = new FastNoiseWrapper(noise, seed);
  109. noise = new FastNoiseLite(seed + 1);
  110. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  111. noise->SetFrequency(0.125f);
  112. undergroundGravelNoise = new FastNoiseWrapper(noise, seed + 1);
  113. noise = new FastNoiseLite(seed + 2);
  114. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  115. noise->SetFrequency(0.1f);
  116. surfaceSandNoise = new FastNoiseWrapper(noise, seed + 2);
  117. }
  118. Noise* GrasslandBiom::zHeightMapNoise()
  119. {
  120. return heightNoise;
  121. }