DimensionGenerator.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "DimensionGenerator.h"
  2. #include "Constants.h"
  3. #include "Noise.h"
  4. #include "NoBlock.h"
  5. #include <iostream>
  6. DimensionGenerator::DimensionGenerator(int dimensionId, CaveGenerator* caveGenerator)
  7. : ReferenceCounter(),
  8. dimensionId(dimensionId),
  9. minTemplateAffectedPosition(0, 0, 0),
  10. maxTemplateAffectedPosition(0, 0, 0),
  11. caveGenerator(caveGenerator)
  12. {
  13. StaticRegistry<DimensionGenerator>::INSTANCE.registerT(this, dimensionId);
  14. }
  15. DimensionGenerator::~DimensionGenerator()
  16. {}
  17. void DimensionGenerator::initialize(int seed)
  18. {
  19. this->seed = seed + dimensionId;
  20. caveGenerator->initialize(this->seed);
  21. }
  22. BiomGenerator* DimensionGenerator::zBiomGenerator(int x, int y)
  23. {
  24. double noise = zBiomNoise()->getNoise((double)x, (double)y, 0.0);
  25. double border = 0;
  26. BiomGenerator* gen = 0;
  27. auto genI = biomGenerators.begin();
  28. auto distI = biomDistribution.begin();
  29. do
  30. {
  31. border += (double)distI++;
  32. gen = genI++;
  33. } while (border < noise && (bool)distI && (bool)genI);
  34. return gen;
  35. }
  36. void DimensionGenerator::registerBiom(BiomGenerator* generator, double possibility)
  37. {
  38. biomGenerators.add(generator);
  39. biomDistribution.add(possibility);
  40. for (auto t : generator->getTemplates())
  41. {
  42. minTemplateAffectedPosition.x = MIN(minTemplateAffectedPosition.x, t->getMinAffectedOffset().x);
  43. minTemplateAffectedPosition.y = MIN(minTemplateAffectedPosition.y, t->getMinAffectedOffset().y);
  44. minTemplateAffectedPosition.z = MIN(minTemplateAffectedPosition.z, t->getMinAffectedOffset().z);
  45. maxTemplateAffectedPosition.x = MAX(maxTemplateAffectedPosition.x, t->getMaxAffectedOffset().x);
  46. maxTemplateAffectedPosition.y = MAX(maxTemplateAffectedPosition.y, t->getMaxAffectedOffset().y);
  47. maxTemplateAffectedPosition.z = MAX(maxTemplateAffectedPosition.z, t->getMaxAffectedOffset().z);
  48. }
  49. }
  50. Framework::RCArray<GeneratedStructure>* DimensionGenerator::getGeneratedStructoresForArea(Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  51. {
  52. Framework::RCArray<GeneratedStructure>* result = new Framework::RCArray<GeneratedStructure>();
  53. int minSearchX = minPos.x - maxTemplateAffectedPosition.x;
  54. int minSearchY = minPos.y - maxTemplateAffectedPosition.y;
  55. int minSearchZ = MAX(minPos.z - maxTemplateAffectedPosition.z, 0);
  56. int maxSearchX = maxPos.x - minTemplateAffectedPosition.x;
  57. int maxSearchY = maxPos.y - minTemplateAffectedPosition.y;
  58. int maxSearchZ = MIN(maxPos.z - minTemplateAffectedPosition.z, WORLD_HEIGHT - 1);
  59. Noise* structureNoise = zStructureNoise();
  60. for (int x = minSearchX; x <= maxSearchX; x++)
  61. {
  62. for (int y = minSearchY; y <= maxSearchY; y++)
  63. {
  64. BiomGenerator* biom = zBiomGenerator(x, y);
  65. int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise(seed)->getNoise((double)(x), (double)(y), 0.0) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  66. for (int z = minSearchZ; z <= maxSearchZ; z++)
  67. {
  68. if (z < height)
  69. {
  70. double rValue = structureNoise->getNoise((double)x, (double)y, (double)z);
  71. double probSum = 0;
  72. for (auto t : biom->getTemplates())
  73. {
  74. if (t->isGenerationPossable(Framework::Vec3<int>(x, y, z), height - z))
  75. {
  76. if (rValue - probSum <= t->getPropability())
  77. {
  78. result->add(t->generateAt(Framework::Vec3<int>(x, y, z), structureNoise, dimensionId));
  79. break;
  80. }
  81. }
  82. probSum += t->getPropability();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. Chunk* DimensionGenerator::generateChunk(int centerX, int centerY)
  91. {
  92. Framework::RCArray<GeneratedStructure>* structures = getGeneratedStructoresForArea(Framework::Vec3<int>(centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0), Framework::Vec3<int>(centerX + CHUNK_SIZE / 2, centerY + CHUNK_SIZE / 2, WORLD_HEIGHT - 1));
  93. std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
  94. CaveChunkGenerator* caveGen = caveGenerator->getGeneratorForChunk(centerX, centerY);
  95. Chunk* chunk = new Chunk(Framework::Punkt(centerX, centerY), dimensionId);
  96. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  97. {
  98. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  99. {
  100. BiomGenerator* biom = zBiomGenerator(x + centerX, y + centerY);
  101. // TODO: use Noise interpolator for height map between different bioms
  102. int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise(seed)->getNoise((double)(x + centerX), (double)(y + centerY), 0.0) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  103. int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  104. int actualSurfaceHeight = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART) + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART * (float)biom->zHeightMapNoise(seed)->getNoise((double)(x + centerX), (double)(y + centerY), 10.0)));
  105. for (int z = 0; z < WORLD_HEIGHT; z++)
  106. {
  107. Framework::Either<Block*, int> generated = AirBlockBlockType::ID;
  108. bool structureAffected = 0;
  109. for (auto structure : *structures)
  110. {
  111. if (structure->isBlockAffected(Framework::Vec3<int>(x + centerX, y + centerY, z)))
  112. {
  113. generated = structure->generateBlockAt(Framework::Vec3<int>(x + centerX, y + centerY, z));
  114. structureAffected = 1;
  115. break;
  116. }
  117. }
  118. if (!structureAffected)
  119. {
  120. if (caveGen->isInCave(x + centerX, y + centerY, z))
  121. generated = biom->generateCaveBlock(x + centerX, y + centerY, z);
  122. else if (z < height && z >= height - actualSurfaceHeight)
  123. generated = biom->generateSurfaceBlock(x + centerX, y + centerY, z);
  124. else if (z < height)
  125. generated = biom->generateBelowSurfaceBlock(x + centerX, y + centerY, z);
  126. }
  127. if (generated.isA())
  128. chunk->putBlockAt(Framework::Vec3<int>(x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z), generated);
  129. else
  130. chunk->putBlockTypeAt(Framework::Vec3<int>(x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z), generated);
  131. }
  132. }
  133. }
  134. caveGen->release();
  135. structures->release();
  136. return chunk;
  137. }
  138. Framework::Either<Block*, int> DimensionGenerator::generateBlock(Framework::Vec3<int> location)
  139. {
  140. Framework::RCArray<GeneratedStructure>* structures = getGeneratedStructoresForArea(location, location);
  141. BiomGenerator* biom = zBiomGenerator(location.x, location.y);
  142. // TODO: use Noise interpolator for height map between different bioms
  143. int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise(seed)->getNoise((double)(location.x), (double)(location.y), 0.0) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  144. int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  145. int actualSurfaceHeight = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART) + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART * (float)biom->zHeightMapNoise(seed)->getNoise((double)(location.x), (double)(location.y), 10.0)));
  146. for (auto structure : *structures)
  147. {
  148. if (structure->isBlockAffected(location))
  149. {
  150. auto generated = structure->generateBlockAt(location);
  151. structures->release();
  152. return generated;
  153. }
  154. }
  155. structures->release();
  156. if (location.z < height && location.z >= height - actualSurfaceHeight)
  157. return biom->generateSurfaceBlock(location.x, location.y, location.z);
  158. else if (location.z < height)
  159. return biom->generateBelowSurfaceBlock(location.x, location.y, location.z);
  160. return AirBlockBlockType::ID;
  161. }
  162. int DimensionGenerator::getDimensionId() const
  163. {
  164. return dimensionId;
  165. }