GrasslandBiom.cpp 4.2 KB

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