DimensionGenerator.cpp 15 KB

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