DimensionGenerator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. for (auto biomGen : biomGenerators)
  24. {
  25. biomGen->setSeed(this->seed);
  26. }
  27. }
  28. BiomGenerator* DimensionGenerator::zBiomGenerator(int x, int y)
  29. {
  30. double noise = zBiomNoise()->getNoise((double)x, (double)y, 0.0);
  31. double border = 0;
  32. BiomGenerator* gen = 0;
  33. auto genI = biomGenerators.begin();
  34. auto distI = biomDistribution.begin();
  35. do
  36. {
  37. border += (double)distI++;
  38. gen = genI++;
  39. } while (border < noise && (bool)distI && (bool)genI);
  40. return gen;
  41. }
  42. void DimensionGenerator::registerBiom(
  43. BiomGenerator* generator, double possibility)
  44. {
  45. biomGenerators.add(generator);
  46. biomDistribution.add(possibility);
  47. for (auto t : generator->getTemplates())
  48. {
  49. minTemplateAffectedPosition.x
  50. = MIN(minTemplateAffectedPosition.x, t->getMinAffectedOffset().x);
  51. minTemplateAffectedPosition.y
  52. = MIN(minTemplateAffectedPosition.y, t->getMinAffectedOffset().y);
  53. minTemplateAffectedPosition.z
  54. = MIN(minTemplateAffectedPosition.z, t->getMinAffectedOffset().z);
  55. maxTemplateAffectedPosition.x
  56. = MAX(maxTemplateAffectedPosition.x, t->getMaxAffectedOffset().x);
  57. maxTemplateAffectedPosition.y
  58. = MAX(maxTemplateAffectedPosition.y, t->getMaxAffectedOffset().y);
  59. maxTemplateAffectedPosition.z
  60. = MAX(maxTemplateAffectedPosition.z, t->getMaxAffectedOffset().z);
  61. }
  62. generator->setSeed(seed);
  63. }
  64. Framework::RCArray<GeneratedStructure>*
  65. DimensionGenerator::getGeneratedStructoresForArea(
  66. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  67. {
  68. Framework::RCArray<GeneratedStructure>* result
  69. = new Framework::RCArray<GeneratedStructure>();
  70. int minSearchX = minPos.x - maxTemplateAffectedPosition.x;
  71. int minSearchY = minPos.y - maxTemplateAffectedPosition.y;
  72. int minSearchZ = MAX(minPos.z - maxTemplateAffectedPosition.z, 0);
  73. int maxSearchX = maxPos.x - minTemplateAffectedPosition.x;
  74. int maxSearchY = maxPos.y - minTemplateAffectedPosition.y;
  75. int maxSearchZ
  76. = MIN(maxPos.z - minTemplateAffectedPosition.z, WORLD_HEIGHT - 1);
  77. Noise* structureNoise = zStructureNoise();
  78. for (int x = minSearchX; x <= maxSearchX; x++)
  79. {
  80. for (int y = minSearchY; y <= maxSearchY; y++)
  81. {
  82. BiomGenerator* biom = zBiomGenerator(x, y);
  83. int height = MIN_AIR_LEVEL
  84. + (int)(biom->zHeightMapNoise()->getNoise(
  85. (double)(x), (double)(y), 0.0)
  86. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  87. for (int z = minSearchZ; z <= maxSearchZ && z < height; z++)
  88. {
  89. double rValue = structureNoise->getNoise((double)x, (double)y, (double)z);
  90. double probSum = 0;
  91. for (auto t : biom->getTemplates())
  92. {
  93. if (t->isGenerationPossable(
  94. Framework::Vec3<int>(x, y, z), height - z))
  95. {
  96. if (rValue - probSum <= t->getPropability())
  97. {
  98. result->add(
  99. t->generateAt(Framework::Vec3<int>(x, y, z),
  100. structureNoise,
  101. dimensionId));
  102. break;
  103. }
  104. }
  105. probSum += t->getPropability();
  106. }
  107. }
  108. }
  109. }
  110. return result;
  111. }
  112. Chunk* DimensionGenerator::generateChunk(int centerX, int centerY)
  113. {
  114. std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
  115. double structureTime = 0;
  116. double structureTime2 = 0;
  117. double structureTime3 = 0;
  118. double caveTime = 0;
  119. double blockGenTime = 0;
  120. double biomTime = 0;
  121. double heightTime = 0;
  122. ZeitMesser zm;
  123. ZeitMesser zmGlobal;
  124. zm.messungStart();
  125. zmGlobal.messungStart();
  126. Framework::RCArray<GeneratedStructure>* structures
  127. = getGeneratedStructoresForArea(
  128. Framework::Vec3<int>(
  129. centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
  130. Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
  131. centerY + CHUNK_SIZE / 2,
  132. WORLD_HEIGHT - 1));
  133. zm.messungEnde();
  134. structureTime += zm.getSekunden();
  135. zm.messungStart();
  136. CaveChunkGenerator* caveGen
  137. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  138. zm.messungEnde();
  139. caveTime += zm.getSekunden();
  140. Chunk* chunk = new Chunk(Framework::Punkt(centerX, centerY), dimensionId);
  141. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  142. {
  143. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  144. {
  145. zm.messungStart();
  146. BiomGenerator* biom = zBiomGenerator(x + centerX, y + centerY);
  147. zm.messungEnde();
  148. biomTime += zm.getSekunden();
  149. // TODO: use Noise interpolator for height map between different
  150. // bioms
  151. zm.messungStart();
  152. int height
  153. = MIN_AIR_LEVEL
  154. + (int)(biom->zHeightMapNoise()->getNoise(
  155. (double)(x + centerX), (double)(y + centerY), 0.0)
  156. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  157. int maxSurfaceHeight
  158. = (int)(MAX_SURFACE_HEIGHT
  159. * (1.f
  160. - (float)(height - MIN_AIR_LEVEL)
  161. / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  162. int actualSurfaceHeight
  163. = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
  164. + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
  165. * (float)biom->zHeightMapNoise()->getNoise(
  166. (double)(x + centerX),
  167. (double)(y + centerY),
  168. 10.0)));
  169. zm.messungEnde();
  170. heightTime += zm.getSekunden();
  171. for (int z = 0; z < WORLD_HEIGHT; z++)
  172. {
  173. Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
  174. bool structureAffected = 0;
  175. for (auto structure : *structures)
  176. {
  177. zm.messungStart();
  178. if (structure->isBlockAffected(
  179. Framework::Vec3<int>(x + centerX, y + centerY, z)))
  180. {
  181. zm.messungEnde();
  182. structureTime2 += zm.getSekunden();
  183. zm.messungStart();
  184. generated = structure->generateBlockAt(
  185. Framework::Vec3<int>(x + centerX, y + centerY, z));
  186. structureAffected = 1;
  187. zm.messungEnde();
  188. structureTime3 += zm.getSekunden();
  189. break;
  190. }
  191. zm.messungEnde();
  192. structureTime2 += zm.getSekunden();
  193. }
  194. if (!structureAffected)
  195. {
  196. zm.messungStart();
  197. bool inCave
  198. = caveGen->isInCave(x + centerX, y + centerY, z);
  199. zm.messungEnde();
  200. caveTime += zm.getSekunden();
  201. zm.messungStart();
  202. if (inCave)
  203. generated = biom->generateCaveBlock(
  204. x + centerX, y + centerY, z);
  205. else if (z < height && z >= height - actualSurfaceHeight)
  206. generated = biom->generateSurfaceBlock(
  207. x + centerX, y + centerY, z);
  208. else if (z < height)
  209. generated = biom->generateBelowSurfaceBlock(
  210. x + centerX, y + centerY, z);
  211. else if (z >= height && z < WATER_LEVEL)
  212. {
  213. generated = biom->generateUnderWaterBlock(
  214. x + centerX, y + centerY, z, height, chunk);
  215. }
  216. else
  217. {
  218. generated = biom->generateAboveSurfaceBlock(
  219. x + centerX, y + centerY, z, height, chunk);
  220. }
  221. zm.messungEnde();
  222. blockGenTime += zm.getSekunden();
  223. }
  224. if (generated.isA())
  225. chunk->putBlockAt(
  226. Framework::Vec3<int>(
  227. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  228. generated);
  229. else
  230. chunk->putBlockTypeAt(
  231. Framework::Vec3<int>(
  232. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  233. generated);
  234. }
  235. }
  236. }
  237. caveGen->release();
  238. structures->release();
  239. zmGlobal.messungEnde();
  240. std::cout << "structureGenerationTime: " << structureTime << "\n";
  241. std::cout << "structure.isBlockAffected: " << structureTime2 << "\n";
  242. std::cout << "structure.generateBlockAt: " << structureTime3 << "\n";
  243. std::cout << "caveTime: " << caveTime << "\n";
  244. std::cout << "blockGenTime: " << blockGenTime << "\n";
  245. std::cout << "biomTime: " << biomTime << "\n";
  246. std::cout << "heightTime: " << heightTime << "\n";
  247. std::cout << "totalTime: " << zmGlobal.getSekunden() << "\n";
  248. return chunk;
  249. }
  250. Framework::Either<Block*, int> DimensionGenerator::generateBlock(
  251. Framework::Vec3<int> location)
  252. {
  253. Framework::RCArray<GeneratedStructure>* structures
  254. = getGeneratedStructoresForArea(location, location);
  255. BiomGenerator* biom = zBiomGenerator(location.x, location.y);
  256. // TODO: use Noise interpolator for height map between different bioms
  257. int height = MIN_AIR_LEVEL
  258. + (int)(biom->zHeightMapNoise()->getNoise(
  259. (double)(location.x), (double)(location.y), 0.0)
  260. * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  261. int maxSurfaceHeight
  262. = (int)(MAX_SURFACE_HEIGHT
  263. * (1.f
  264. - (float)(height - MIN_AIR_LEVEL)
  265. / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  266. int actualSurfaceHeight
  267. = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
  268. + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
  269. * (float)biom->zHeightMapNoise()->getNoise(
  270. (double)(location.x), (double)(location.y), 10.0)));
  271. for (auto structure : *structures)
  272. {
  273. if (structure->isBlockAffected(location))
  274. {
  275. auto generated = structure->generateBlockAt(location);
  276. structures->release();
  277. return generated;
  278. }
  279. }
  280. structures->release();
  281. if (location.z < height && location.z >= height - actualSurfaceHeight)
  282. return biom->generateSurfaceBlock(location.x, location.y, location.z);
  283. else if (location.z < height)
  284. return biom->generateBelowSurfaceBlock(
  285. location.x, location.y, location.z);
  286. else if (location.z >= height && location.z < WATER_LEVEL)
  287. {
  288. return biom->generateUnderWaterBlock(location.x,
  289. location.y,
  290. location.z,
  291. height,
  292. Game::INSTANCE->zDimension(getDimensionId())
  293. ->zChunk(
  294. Game::INSTANCE->getChunkCenter(location.x, location.y)));
  295. }
  296. if (location.z >= height)
  297. {
  298. return biom->generateAboveSurfaceBlock(location.x,
  299. location.x,
  300. location.z,
  301. height,
  302. Game::INSTANCE->zDimension(getDimensionId())
  303. ->zChunk(
  304. Game::INSTANCE->getChunkCenter(location.x, location.y)));
  305. }
  306. return BlockTypeEnum::AIR;
  307. }
  308. bool DimensionGenerator::spawnStructure(Framework::Vec3<int> location,
  309. std::function<bool(GenerationTemplate* tmpl)> filter)
  310. {
  311. BiomGenerator* biom = zBiomGenerator(location.x, location.y);
  312. for (auto t : biom->getTemplates())
  313. {
  314. if (filter(t))
  315. {
  316. RandNoise noise((int)time(0));
  317. GeneratedStructure* genStr
  318. = t->generateAt(location, &noise, dimensionId);
  319. if (genStr)
  320. {
  321. int minSearchX = location.x + t->getMinAffectedOffset().x;
  322. int minSearchY = location.y + t->getMinAffectedOffset().y;
  323. int minSearchZ
  324. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  325. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  326. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  327. int maxSearchZ = MIN(
  328. location.z + t->getMaxAffectedOffset().z, WORLD_HEIGHT - 1);
  329. for (int x = minSearchX; x <= maxSearchX; x++)
  330. {
  331. for (int y = minSearchY; y <= maxSearchY; y++)
  332. {
  333. for (int z = minSearchZ; z <= maxSearchZ; z++)
  334. {
  335. if (genStr->isBlockAffected(
  336. Framework::Vec3<int>(x, y, z)))
  337. {
  338. auto gen = genStr->generateBlockAt(
  339. Framework::Vec3<int>(x, y, z));
  340. Game::INSTANCE->zDimension(dimensionId)
  341. ->placeBlock(
  342. Framework::Vec3<int>(x, y, z), gen);
  343. }
  344. }
  345. }
  346. }
  347. genStr->release();
  348. return 1;
  349. }
  350. }
  351. }
  352. return 0;
  353. }
  354. int DimensionGenerator::getDimensionId() const
  355. {
  356. return dimensionId;
  357. }