WormCaveGenerator.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "WormCaveGenerator.h"
  2. #include "FastNoiseWrapper.h"
  3. #include "Game.h"
  4. #include "RandNoise.h"
  5. NoiseWorm3D::NoiseWorm3D(Noise* pitch,
  6. Noise* yaw,
  7. Noise* size,
  8. Framework::Vec3<int> startPos,
  9. int distant,
  10. int minRad,
  11. int maxRad)
  12. : ReferenceCounter(),
  13. startChunk(Game::INSTANCE->getChunkCenter(startPos.x, startPos.y))
  14. {
  15. Vec3<float> lastPos = (Vec3<float>)startPos;
  16. keyPoints.add(lastPos);
  17. this->size.add((float)minRad);
  18. minAffected.x = (int)lastPos.x - minRad;
  19. minAffected.y = (int)lastPos.y - minRad;
  20. maxAffected.x = (int)lastPos.x + minRad;
  21. maxAffected.y = (int)lastPos.y + minRad;
  22. while (keyPoints.getEintragAnzahl() < distant * 20)
  23. {
  24. Vec3<float> defaultDir(1.f, 0.f, 0.f);
  25. if (keyPoints.getEintragAnzahl() > 1)
  26. {
  27. defaultDir = keyPoints.get(keyPoints.getEintragAnzahl() - 1)
  28. - keyPoints.get(keyPoints.getEintragAnzahl() - 2);
  29. }
  30. float n = (float)yaw->getNoise(lastPos.x, lastPos.y, lastPos.z);
  31. defaultDir.rotateZ((n - 0.5f) / 2.f);
  32. defaultDir.normalize();
  33. n = (float)pitch->getNoise(lastPos.x, lastPos.y, lastPos.z);
  34. defaultDir.z = ((float)n - 0.3f) * 2.f;
  35. lastPos = lastPos + defaultDir;
  36. if ((Vec2<float>(lastPos.x, lastPos.y)
  37. - (Vec2<float>((float)startPos.x, (float)startPos.y)))
  38. .getLengthSq()
  39. >= (float)(distant * distant))
  40. break;
  41. keyPoints.add(lastPos);
  42. float rad = (float)size->getNoise(lastPos.x, lastPos.y, lastPos.z)
  43. * (float)(maxRad - minRad)
  44. + (float)minRad;
  45. this->size.add(rad);
  46. minAffected.x = MIN(minAffected.x, (int)(lastPos.x - rad));
  47. minAffected.y = MIN(minAffected.y, (int)(lastPos.y - rad));
  48. maxAffected.x = MAX(maxAffected.x, (int)(lastPos.x + rad));
  49. maxAffected.y = MAX(maxAffected.y, (int)(lastPos.y + rad));
  50. }
  51. }
  52. Framework::Punkt NoiseWorm3D::getStartChunkCenter()
  53. {
  54. return startChunk;
  55. }
  56. bool NoiseWorm3D::isChunkAffected(int x, int y)
  57. {
  58. if (x - CHUNK_SIZE / 2 <= maxAffected.x
  59. && x + CHUNK_SIZE / 2 >= minAffected.x
  60. && y - CHUNK_SIZE / 2 <= maxAffected.y
  61. && y + CHUNK_SIZE / 2 >= minAffected.y)
  62. {
  63. auto pi = keyPoints.begin();
  64. auto si = size.begin();
  65. while (pi && si)
  66. {
  67. if ((Vec2<float>((float)x, (float)y)
  68. - Vec2<float>(pi.val().x, pi.val().y))
  69. .getLengthSq()
  70. < (si.val() + CHUNK_SIZE / 2) * (si.val() + CHUNK_SIZE / 2))
  71. return 1;
  72. ++pi;
  73. ++si;
  74. }
  75. }
  76. return 0;
  77. }
  78. bool NoiseWorm3D::isInside(int x, int y, int z)
  79. {
  80. auto pi = keyPoints.begin();
  81. auto si = size.begin();
  82. while (pi && si)
  83. {
  84. if (Vec3<float>((float)x, (float)y, (float)z).abstand(pi.val())
  85. < si.val())
  86. return 1;
  87. ++pi;
  88. ++si;
  89. }
  90. return 0;
  91. }
  92. WormCaveChunkGenerator::WormCaveChunkGenerator(
  93. Framework::RCArray<NoiseWorm3D> worms)
  94. : CaveChunkGenerator(),
  95. worms(worms)
  96. {}
  97. bool WormCaveChunkGenerator::isInCave(int x, int y, int z)
  98. {
  99. for (NoiseWorm3D* w : worms)
  100. {
  101. if (w->isInside(x, y, z)) return 1;
  102. }
  103. return 0;
  104. }
  105. WormCaveGenerator::WormCaveGenerator(int minDistant,
  106. int maxDistant,
  107. int minRadius,
  108. int maxRadius,
  109. float cavePosibillityPerChunk)
  110. : CaveGenerator(),
  111. maxDistant(maxDistant),
  112. minDistant(minDistant),
  113. maxRadius(maxRadius),
  114. minRadius(minRadius),
  115. cavePosibillity(cavePosibillityPerChunk)
  116. {}
  117. WormCaveGenerator::~WormCaveGenerator()
  118. {
  119. wormStartNoise->release();
  120. }
  121. NoiseWorm3D* WormCaveGenerator::zWormOfChunk(int x, int y)
  122. {
  123. for (NoiseWorm3D* worm : cache)
  124. {
  125. if (worm->getStartChunkCenter().x == x
  126. && worm->getStartChunkCenter().y == y)
  127. {
  128. return worm;
  129. }
  130. }
  131. for (Punkt p : noWormChunks)
  132. {
  133. if (p.x == x && p.y == y) return 0;
  134. }
  135. float cNoise = (float)wormStartNoise->getNoise(x, y, 0);
  136. if (cNoise < cavePosibillity)
  137. {
  138. FastNoiseLite* noise = new FastNoiseLite(seed + x * y + y + x);
  139. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
  140. noise->SetFrequency(0.05f);
  141. FastNoiseWrapper* pitch
  142. = new FastNoiseWrapper(noise, seed + x * y + y + x);
  143. noise = new FastNoiseLite(seed + x * y + y + x);
  144. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
  145. noise->SetFrequency(0.005f);
  146. FastNoiseWrapper* yaw
  147. = new FastNoiseWrapper(noise, seed - x * y - y - x);
  148. Vec3<int> start(
  149. (int)(cNoise / cavePosibillity * CHUNK_SIZE) + x - CHUNK_SIZE / 2,
  150. (int)(cNoise / cavePosibillity * CHUNK_SIZE) + y - CHUNK_SIZE / 2,
  151. (int)(cNoise / cavePosibillity * 200));
  152. noise = new FastNoiseLite(
  153. seed + start.getLengthSq() + start.x + start.y + start.z);
  154. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
  155. noise->SetFrequency(0.005f);
  156. FastNoiseWrapper* size = new FastNoiseWrapper(
  157. noise, seed + start.getLengthSq() + start.x + start.y + start.z);
  158. NoiseWorm3D* worm = new NoiseWorm3D(pitch,
  159. yaw,
  160. size,
  161. start,
  162. (int)(wormStartNoise->getNoise(start.x, start.y, start.z)
  163. * (maxDistant - minDistant)
  164. + minDistant),
  165. minRadius,
  166. maxRadius);
  167. if (cache.getEintragAnzahl()
  168. > (maxDistant * 2) / CHUNK_SIZE * (maxDistant * 2) / CHUNK_SIZE * 3)
  169. cache.remove(0);
  170. cache.add(worm);
  171. return worm;
  172. }
  173. else
  174. {
  175. if (noWormChunks.getEintragAnzahl()
  176. > (maxDistant * 2) / CHUNK_SIZE * (maxDistant * 2) / CHUNK_SIZE * 3)
  177. noWormChunks.remove(0);
  178. noWormChunks.add(Punkt(x, y));
  179. }
  180. return 0;
  181. }
  182. void WormCaveGenerator::initialize(int seed)
  183. {
  184. wormStartNoise = new RandNoise(seed);
  185. CaveGenerator::initialize(seed);
  186. }
  187. CaveChunkGenerator* WormCaveGenerator::getGeneratorForChunk(int x, int y)
  188. {
  189. Framework::RCArray<NoiseWorm3D> affected;
  190. Punkt center = Game::getChunkCenter(x, y);
  191. int offset = (int)ceil((float)maxDistant / CHUNK_SIZE);
  192. for (int cx = -offset; cx <= offset; cx++)
  193. {
  194. for (int cy = -offset; cy <= offset; cy++)
  195. {
  196. NoiseWorm3D* worm = zWormOfChunk(
  197. center.x + cx * CHUNK_SIZE, center.y + cy * CHUNK_SIZE);
  198. if (worm && worm->isChunkAffected(x, y))
  199. affected.add(dynamic_cast<NoiseWorm3D*>(worm->getThis()));
  200. }
  201. }
  202. return new WormCaveChunkGenerator(affected);
  203. }