DimensionGenerator.cpp 8.5 KB

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