DimensionGenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "DimensionGenerator.h"
  2. #include <iostream>
  3. #include "Constants.h"
  4. #include "Game.h"
  5. #include "NoBlock.h"
  6. #include "Noise.h"
  7. #include "RandNoise.h"
  8. DimensionGenerator::DimensionGenerator(
  9. int dimensionId, CaveGenerator* caveGenerator)
  10. : ReferenceCounter(),
  11. dimensionId(dimensionId),
  12. minTemplateAffectedPosition(0, 0, 0),
  13. maxTemplateAffectedPosition(0, 0, 0),
  14. caveGenerator(caveGenerator)
  15. {
  16. StaticRegistry<DimensionGenerator>::INSTANCE.registerT(this, dimensionId);
  17. }
  18. DimensionGenerator::~DimensionGenerator() {}
  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(
  39. BiomGenerator* generator, double possibility)
  40. {
  41. biomGenerators.add(generator);
  42. biomDistribution.add(possibility);
  43. for (auto t : generator->getTemplates())
  44. {
  45. minTemplateAffectedPosition.x
  46. = MIN(minTemplateAffectedPosition.x, t->getMinAffectedOffset().x);
  47. minTemplateAffectedPosition.y
  48. = MIN(minTemplateAffectedPosition.y, t->getMinAffectedOffset().y);
  49. minTemplateAffectedPosition.z
  50. = MIN(minTemplateAffectedPosition.z, t->getMinAffectedOffset().z);
  51. maxTemplateAffectedPosition.x
  52. = MAX(maxTemplateAffectedPosition.x, t->getMaxAffectedOffset().x);
  53. maxTemplateAffectedPosition.y
  54. = MAX(maxTemplateAffectedPosition.y, t->getMaxAffectedOffset().y);
  55. maxTemplateAffectedPosition.z
  56. = MAX(maxTemplateAffectedPosition.z, t->getMaxAffectedOffset().z);
  57. }
  58. }
  59. Framework::RCArray<GeneratedStructure>*
  60. DimensionGenerator::getGeneratedStructoresForArea(
  61. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  62. {
  63. Framework::RCArray<GeneratedStructure>* result
  64. = new Framework::RCArray<GeneratedStructure>();
  65. int minSearchX = minPos.x - maxTemplateAffectedPosition.x;
  66. int minSearchY = minPos.y - maxTemplateAffectedPosition.y;
  67. int minSearchZ = MAX(minPos.z - maxTemplateAffectedPosition.z, 0);
  68. int maxSearchX = maxPos.x - minTemplateAffectedPosition.x;
  69. int maxSearchY = maxPos.y - minTemplateAffectedPosition.y;
  70. int maxSearchZ
  71. = MIN(maxPos.z - minTemplateAffectedPosition.z, WORLD_HEIGHT - 1);
  72. Noise* structureNoise = zStructureNoise();
  73. for (int x = minSearchX; x <= maxSearchX; x++)
  74. {
  75. for (int y = minSearchY; y <= maxSearchY; y++)
  76. {
  77. BiomGenerator* biom = zBiomGenerator(x, y);
  78. int height = MIN_AIR_LEVEL
  79. + (int)(biom->zHeightMapNoise(seed)->getNoise(
  80. (double)(x), (double)(y), 0.0)
  81. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  82. for (int z = minSearchZ; z <= maxSearchZ; z++)
  83. {
  84. if (z < height)
  85. {
  86. double rValue = structureNoise->getNoise(
  87. (double)x, (double)y, (double)z);
  88. double probSum = 0;
  89. for (auto t : biom->getTemplates())
  90. {
  91. if (t->isGenerationPossable(
  92. Framework::Vec3<int>(x, y, z), height - z))
  93. {
  94. if (rValue - probSum <= t->getPropability())
  95. {
  96. result->add(
  97. t->generateAt(Framework::Vec3<int>(x, y, z),
  98. structureNoise,
  99. dimensionId));
  100. break;
  101. }
  102. }
  103. probSum += t->getPropability();
  104. }
  105. }
  106. }
  107. }
  108. }
  109. return result;
  110. }
  111. Chunk* DimensionGenerator::generateChunk(int centerX, int centerY)
  112. {
  113. Framework::RCArray<GeneratedStructure>* structures
  114. = getGeneratedStructoresForArea(
  115. Framework::Vec3<int>(
  116. centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
  117. Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
  118. centerY + CHUNK_SIZE / 2,
  119. WORLD_HEIGHT - 1));
  120. std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
  121. CaveChunkGenerator* caveGen
  122. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  123. Chunk* chunk = new Chunk(Framework::Punkt(centerX, centerY), dimensionId);
  124. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  125. {
  126. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  127. {
  128. BiomGenerator* biom = zBiomGenerator(x + centerX, y + centerY);
  129. // TODO: use Noise interpolator for height map between different
  130. // bioms
  131. int height
  132. = MIN_AIR_LEVEL
  133. + (int)(biom->zHeightMapNoise(seed)->getNoise(
  134. (double)(x + centerX), (double)(y + centerY), 0.0)
  135. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  136. int maxSurfaceHeight
  137. = (int)(MAX_SURFACE_HEIGHT
  138. * (1.f
  139. - (float)(height - MIN_AIR_LEVEL)
  140. / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  141. int actualSurfaceHeight
  142. = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
  143. + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
  144. * (float)biom->zHeightMapNoise(seed)->getNoise(
  145. (double)(x + centerX),
  146. (double)(y + centerY),
  147. 10.0)));
  148. for (int z = 0; z < WORLD_HEIGHT; z++)
  149. {
  150. Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
  151. bool structureAffected = 0;
  152. for (auto structure : *structures)
  153. {
  154. if (structure->isBlockAffected(
  155. Framework::Vec3<int>(x + centerX, y + centerY, z)))
  156. {
  157. generated = structure->generateBlockAt(
  158. Framework::Vec3<int>(x + centerX, y + centerY, z));
  159. structureAffected = 1;
  160. break;
  161. }
  162. }
  163. if (!structureAffected)
  164. {
  165. if (caveGen->isInCave(x + centerX, y + centerY, z))
  166. generated = biom->generateCaveBlock(
  167. x + centerX, y + centerY, z);
  168. else if (z < height && z >= height - actualSurfaceHeight)
  169. generated = biom->generateSurfaceBlock(
  170. x + centerX, y + centerY, z);
  171. else if (z < height)
  172. generated = biom->generateBelowSurfaceBlock(
  173. x + centerX, y + centerY, z);
  174. }
  175. if (generated.isA())
  176. chunk->putBlockAt(
  177. Framework::Vec3<int>(
  178. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  179. generated);
  180. else
  181. chunk->putBlockTypeAt(
  182. Framework::Vec3<int>(
  183. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  184. generated);
  185. }
  186. }
  187. }
  188. caveGen->release();
  189. structures->release();
  190. return chunk;
  191. }
  192. Framework::Either<Block*, int> DimensionGenerator::generateBlock(
  193. Framework::Vec3<int> location)
  194. {
  195. Framework::RCArray<GeneratedStructure>* structures
  196. = getGeneratedStructoresForArea(location, location);
  197. BiomGenerator* biom = zBiomGenerator(location.x, location.y);
  198. // TODO: use Noise interpolator for height map between different bioms
  199. int height = MIN_AIR_LEVEL
  200. + (int)(biom->zHeightMapNoise(seed)->getNoise(
  201. (double)(location.x), (double)(location.y), 0.0)
  202. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  203. int maxSurfaceHeight
  204. = (int)(MAX_SURFACE_HEIGHT
  205. * (1.f
  206. - (float)(height - MIN_AIR_LEVEL)
  207. / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  208. int actualSurfaceHeight
  209. = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
  210. + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
  211. * (float)biom->zHeightMapNoise(seed)->getNoise(
  212. (double)(location.x), (double)(location.y), 10.0)));
  213. for (auto structure : *structures)
  214. {
  215. if (structure->isBlockAffected(location))
  216. {
  217. auto generated = structure->generateBlockAt(location);
  218. structures->release();
  219. return generated;
  220. }
  221. }
  222. structures->release();
  223. if (location.z < height && location.z >= height - actualSurfaceHeight)
  224. return biom->generateSurfaceBlock(location.x, location.y, location.z);
  225. else if (location.z < height)
  226. return biom->generateBelowSurfaceBlock(
  227. location.x, location.y, location.z);
  228. return BlockTypeEnum::AIR;
  229. }
  230. bool DimensionGenerator::spawnStructure(Framework::Vec3<int> location,
  231. std::function<bool(GenerationTemplate* tmpl)> filter)
  232. {
  233. BiomGenerator* biom = zBiomGenerator(location.x, location.y);
  234. for (auto t : biom->getTemplates())
  235. {
  236. if (filter(t))
  237. {
  238. RandNoise noise((int)time(0));
  239. GeneratedStructure* genStr
  240. = t->generateAt(location, &noise, dimensionId);
  241. if (genStr)
  242. {
  243. int minSearchX = location.x + t->getMinAffectedOffset().x;
  244. int minSearchY = location.y + t->getMinAffectedOffset().y;
  245. int minSearchZ
  246. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  247. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  248. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  249. int maxSearchZ = MIN(
  250. location.z + t->getMaxAffectedOffset().z, WORLD_HEIGHT - 1);
  251. for (int x = minSearchX; x <= maxSearchX; x++)
  252. {
  253. for (int y = minSearchY; y <= maxSearchY; y++)
  254. {
  255. for (int z = minSearchZ; z <= maxSearchZ; z++)
  256. {
  257. if (genStr->isBlockAffected(
  258. Framework::Vec3<int>(x, y, z)))
  259. {
  260. auto gen = genStr->generateBlockAt(
  261. Framework::Vec3<int>(x, y, z));
  262. Game::INSTANCE->zDimension(dimensionId)
  263. ->placeBlock(
  264. Framework::Vec3<int>(x, y, z), gen);
  265. }
  266. }
  267. }
  268. }
  269. genStr->release();
  270. return 1;
  271. }
  272. }
  273. }
  274. return 0;
  275. }
  276. int DimensionGenerator::getDimensionId() const
  277. {
  278. return dimensionId;
  279. }