GrasslandBiom.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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(int x,
  48. int y,
  49. int z,
  50. int dimensionId,
  51. int surfaceHeight,
  52. Chunk* partialGeneratedChunk)
  53. {
  54. int cx = x % CHUNK_SIZE;
  55. int cy = y % CHUNK_SIZE;
  56. if (cx < 0) cx += CHUNK_SIZE;
  57. if (cy < 0) cy += CHUNK_SIZE;
  58. auto below
  59. = partialGeneratedChunk->zBlockAt(Framework::Vec3<int>(cx, cy, z - 1));
  60. if ((below.isA()
  61. && below.getA()->zBlockType()->getId() == BlockTypeEnum::DIRT)
  62. || (below.isB() && below.getB() == BlockTypeEnum::DIRT))
  63. {
  64. if (grassNoise->getNoise((double)x, (double)y, (double)z) > 0.75)
  65. {
  66. return BlockTypeEnum::GRASS;
  67. }
  68. }
  69. return BlockTypeEnum::AIR;
  70. }
  71. Framework::Either<Block*, int> GrasslandBiom::generateSurfaceBlock(
  72. int x, int y, int z, int dimensionId)
  73. {
  74. if (surfaceSandNoise->getNoise((double)x, (double)y, (double)z) < 0.35)
  75. {
  76. return BlockTypeEnum::SAND;
  77. }
  78. if (undergroundGravelNoise->getNoise((double)x, (double)y, (double)z)
  79. < 0.35)
  80. {
  81. return BlockTypeEnum::GRAVEL;
  82. }
  83. return BlockTypeEnum::DIRT;
  84. }
  85. Framework::Either<Block*, int> GrasslandBiom::generateBelowSurfaceBlock(
  86. int x, int y, int z, int dimensionId)
  87. {
  88. if (undergroundDirdNoise->getNoise((double)x, (double)y, (double)z) < 0.35)
  89. {
  90. return BlockTypeEnum::DIRT;
  91. }
  92. if (undergroundGravelNoise->getNoise((double)x, (double)y, (double)z)
  93. < 0.35)
  94. {
  95. return BlockTypeEnum::GRAVEL;
  96. }
  97. return BlockTypeEnum::STONE;
  98. }
  99. Framework::Either<Block*, int> GrasslandBiom::generateCaveBlock(
  100. int x, int y, int z, int dimensionId)
  101. {
  102. return BlockTypeEnum::AIR;
  103. }
  104. Framework::Either<Block*, int> GrasslandBiom::generateUnderWaterBlock(int x,
  105. int y,
  106. int z,
  107. int dimensionId,
  108. int surfaceHeight,
  109. Chunk* partialGeneratedChunk)
  110. {
  111. return StaticRegistry<BlockType>::INSTANCE.zElement(BlockTypeEnum::WATER)
  112. ->createBlockAt(Framework::Vec3<int>(x, y, z), dimensionId, 0);
  113. }
  114. void GrasslandBiom::setSeed(int seed)
  115. {
  116. BiomGenerator::setSeed(seed);
  117. if (heightNoise) heightNoise->release();
  118. FastNoiseLite* noise = new FastNoiseLite(seed);
  119. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  120. FastNoiseWrapper* wrapper = new FastNoiseWrapper(noise, seed);
  121. wrapper->setMultiplier(0.2f);
  122. heightNoise = wrapper;
  123. noise = new FastNoiseLite(seed);
  124. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  125. noise->SetFrequency(0.15f);
  126. undergroundDirdNoise = new FastNoiseWrapper(noise, seed);
  127. noise = new FastNoiseLite(seed + 1);
  128. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  129. noise->SetFrequency(0.125f);
  130. undergroundGravelNoise = new FastNoiseWrapper(noise, seed + 1);
  131. noise = new FastNoiseLite(seed + 2);
  132. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  133. noise->SetFrequency(0.1f);
  134. surfaceSandNoise = new FastNoiseWrapper(noise, seed + 2);
  135. grassNoise = new RandNoise(seed + 3);
  136. }
  137. Noise* GrasslandBiom::zHeightMapNoise()
  138. {
  139. return heightNoise;
  140. }