DimensionGenerator.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. #include "DimensionGenerator.h"
  2. #include "BiomGenerator.h"
  3. #include "Constants.h"
  4. #include "Dimension.h"
  5. #include "Game.h"
  6. #include "Noise.h"
  7. #include "RandNoise.h"
  8. #include "WormCaveGenerator.h"
  9. WorldHeightLayer::WorldHeightLayer()
  10. : ReferenceCounter(),
  11. noiseConfig(0),
  12. noise(0),
  13. value(0),
  14. valueP(0)
  15. {}
  16. WorldHeightLayer::~WorldHeightLayer()
  17. {
  18. if (noiseConfig) noiseConfig->release();
  19. if (noise) noise->release();
  20. if (value) value->release();
  21. }
  22. void WorldHeightLayer::initialize(JExpressionMemory* zMemory)
  23. {
  24. if (noise) noise->release();
  25. noise = JNoise::parseNoise(noiseConfig, zMemory);
  26. zMemory->setNoise(name, dynamic_cast<Noise*>(noise->getThis()));
  27. valueP = zMemory->getFloatVariableP(name);
  28. value->compile(zMemory);
  29. }
  30. void WorldHeightLayer::calculateValue()
  31. {
  32. *valueP = value->getValue();
  33. }
  34. void WorldHeightLayer::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
  35. {
  36. if (this->noiseConfig) this->noiseConfig->release();
  37. this->noiseConfig = noiseConfig;
  38. }
  39. Framework::JSON::JSONObject* WorldHeightLayer::zNoiseConfig() const
  40. {
  41. return noiseConfig;
  42. }
  43. void WorldHeightLayer::setName(Framework::Text name)
  44. {
  45. this->name = name;
  46. }
  47. Framework::Text WorldHeightLayer::getName() const
  48. {
  49. return name;
  50. }
  51. void WorldHeightLayer::setValue(JFloatExpression* value)
  52. {
  53. if (this->value) this->value->release();
  54. this->value = value;
  55. }
  56. JFloatExpression* WorldHeightLayer::zValue() const
  57. {
  58. return value;
  59. }
  60. WorldHeightLayerFactory::WorldHeightLayerFactory() {}
  61. WorldHeightLayer* WorldHeightLayerFactory::fromJson(
  62. Framework::JSON::JSONObject* zJson) const
  63. {
  64. WorldHeightLayer* result = new WorldHeightLayer();
  65. result->setName(zJson->zValue("name")->asString()->getString());
  66. result->setNoiseConfig(zJson->getValue("noise")->asObject());
  67. result->setValue(
  68. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  69. zJson->zValue("value")));
  70. return result;
  71. }
  72. Framework::JSON::JSONObject* WorldHeightLayerFactory::toJsonObject(
  73. WorldHeightLayer* zObject) const
  74. {
  75. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  76. result->addValue(
  77. "name", new Framework::JSON::JSONString(zObject->getName()));
  78. result->addValue("noise", zObject->zNoiseConfig());
  79. result->addValue("value",
  80. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  81. zObject->zValue()));
  82. return result;
  83. }
  84. JSONObjectValidationBuilder* WorldHeightLayerFactory::addToValidator(
  85. JSONObjectValidationBuilder* builder) const
  86. {
  87. return builder->withRequiredString("name")
  88. ->finishString()
  89. ->withRequiredAttribute("noise", JNoise::getValidator(false))
  90. ->withRequiredAttribute("value",
  91. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  92. }
  93. DimensionGenerator::DimensionGenerator()
  94. : ReferenceCounter(),
  95. jExpressionMemory(new JExpressionMemory()),
  96. seedExpression(0),
  97. dimensionId(0)
  98. {}
  99. DimensionGenerator::~DimensionGenerator()
  100. {
  101. jExpressionMemory->release();
  102. if (seedExpression) seedExpression->release();
  103. }
  104. JExpressionMemory* DimensionGenerator::zMemory() const
  105. {
  106. return jExpressionMemory;
  107. }
  108. void DimensionGenerator::calculateHeightLayers()
  109. {
  110. for (WorldHeightLayer* layer : heightLayers)
  111. {
  112. layer->calculateValue();
  113. }
  114. }
  115. Dimension* DimensionGenerator::createDimension() const
  116. {
  117. return new Dimension(getId());
  118. }
  119. void DimensionGenerator::initialize(int worldSeed)
  120. {
  121. this->worldSeed = jExpressionMemory->getFloatVariableP("worldSeed");
  122. *this->worldSeed = (float)worldSeed;
  123. this->dimensionSeed = jExpressionMemory->getFloatVariableP("dimensionSeed");
  124. seedExpression->compile(jExpressionMemory);
  125. *this->dimensionSeed = seedExpression->getValue();
  126. for (WorldHeightLayer* layer : heightLayers)
  127. {
  128. layer->initialize(jExpressionMemory);
  129. }
  130. xPos = jExpressionMemory->getFloatVariableP("x");
  131. yPos = jExpressionMemory->getFloatVariableP("y");
  132. zPos = jExpressionMemory->getFloatVariableP("z");
  133. }
  134. int DimensionGenerator::getDimensionId() const
  135. {
  136. return dimensionId;
  137. }
  138. void DimensionGenerator::addHeightLayer(WorldHeightLayer* layer)
  139. {
  140. heightLayers.add(layer);
  141. }
  142. const Framework::RCArray<WorldHeightLayer>&
  143. DimensionGenerator::getHeightLayers() const
  144. {
  145. return heightLayers;
  146. }
  147. void DimensionGenerator::setName(Framework::Text name)
  148. {
  149. this->name = name;
  150. }
  151. Framework::Text DimensionGenerator::getName() const
  152. {
  153. return name;
  154. }
  155. void DimensionGenerator::setId(int id)
  156. {
  157. dimensionId = id;
  158. }
  159. int DimensionGenerator::getId() const
  160. {
  161. return dimensionId;
  162. }
  163. void DimensionGenerator::setSeed(JFloatExpression* seed)
  164. {
  165. if (seedExpression) seedExpression->release();
  166. seedExpression = seed;
  167. }
  168. JFloatExpression* DimensionGenerator::zSeed() const
  169. {
  170. return seedExpression;
  171. }
  172. BiomedCavedDimensionGenerator::BiomedCavedDimensionGenerator()
  173. : DimensionGenerator(),
  174. caveGenerator(0),
  175. noiseConfig(0),
  176. biomNoise(0)
  177. {}
  178. BiomedCavedDimensionGenerator::~BiomedCavedDimensionGenerator()
  179. {
  180. if (noiseConfig) noiseConfig->release();
  181. if (biomNoise) biomNoise->release();
  182. if (caveGenerator) caveGenerator->release();
  183. }
  184. void BiomedCavedDimensionGenerator::initialize(int worldSeed)
  185. {
  186. if (biomNoise) biomNoise->release();
  187. if (caveGenerator) caveGenerator->release();
  188. DimensionGenerator::initialize(worldSeed);
  189. biomNoise = JNoise::parseNoise(noiseConfig, zMemory());
  190. for (BiomGenerator* gen : biomGenerators)
  191. {
  192. gen->initialize(zMemory());
  193. }
  194. caveGenerator = new WormCaveGenerator(75, 150, 1, 6, 0.1f, worldSeed - 1);
  195. terrainHeightP = zMemory()->getFloatVariableP(terrainHeightLayerName);
  196. seaFluidBlockTypeId = Game::INSTANCE->getBlockTypeId(seaFluidBlockType);
  197. }
  198. BiomGenerator* BiomedCavedDimensionGenerator::zBiomGenerator()
  199. {
  200. for (BiomGenerator* generator : biomGenerators)
  201. {
  202. if (generator->isApplicable()) return generator;
  203. }
  204. return 0;
  205. }
  206. Framework::RCArray<GeneratedStructure>*
  207. BiomedCavedDimensionGenerator::getGeneratedStructoresForArea(
  208. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  209. {
  210. int minSearchX = minPos.x - maxStructureOffset.x;
  211. int minSearchY = minPos.y - maxStructureOffset.y;
  212. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  213. int maxSearchX = maxPos.x - minStructureOffset.x;
  214. int maxSearchY = maxPos.y - minStructureOffset.y;
  215. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  216. Framework::RCArray<GeneratedStructure>* result
  217. = new Framework::RCArray<GeneratedStructure>();
  218. for (int x = minSearchX; x <= maxSearchX; x++)
  219. {
  220. for (int y = minSearchY; y <= maxSearchY; y++)
  221. {
  222. *xPos = (float)x;
  223. *yPos = (float)y;
  224. calculateHeightLayers();
  225. BiomGenerator* gen = zBiomGenerator();
  226. for (int z = minSearchZ; z <= maxSearchZ; z++)
  227. {
  228. *zPos = (float)z;
  229. gen->generateStructures(
  230. x, y, z, getDimensionId(), minPos, maxPos, result);
  231. }
  232. }
  233. }
  234. return result;
  235. }
  236. Chunk* BiomedCavedDimensionGenerator::generateChunk(int centerX, int centerY)
  237. {
  238. zMemory()->lock();
  239. #ifdef CHUNK_GENERATION_DEBUG_LOG
  240. Framework::Logging::debug()
  241. << "generating chunk " << centerX << ", " << centerY;
  242. double structureTime = 0;
  243. double caveTime = 0;
  244. double blockGenTime = 0;
  245. Framework::Timer zm;
  246. Framework::Timer zmGlobal;
  247. Framework::Timer entityGenTime;
  248. zm.measureStart();
  249. zmGlobal.measureStart();
  250. #endif
  251. Framework::RCArray<GeneratedStructure>* structures
  252. = getGeneratedStructoresForArea(
  253. Framework::Vec3<int>(
  254. centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
  255. Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
  256. centerY + CHUNK_SIZE / 2,
  257. WORLD_HEIGHT - 1));
  258. #ifdef CHUNK_GENERATION_DEBUG_LOG
  259. zm.measureEnd();
  260. structureTime += zm.getSekunden();
  261. zm.measureStart();
  262. #endif
  263. CaveChunkGenerator* caveGen
  264. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  265. #ifdef CHUNK_GENERATION_DEBUG_LOG
  266. zm.measureEnd();
  267. caveTime += zm.getSekunden();
  268. #endif
  269. Chunk* chunk
  270. = new Chunk(Framework::Point(centerX, centerY), getDimensionId());
  271. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(chunk->getThis()));
  272. int maxZ = 0;
  273. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  274. {
  275. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  276. {
  277. *xPos = (float)x + (float)centerX;
  278. *yPos = (float)y + (float)centerY;
  279. // calculate height layers
  280. calculateHeightLayers();
  281. // calculate biom
  282. BiomGenerator* biom = zBiomGenerator();
  283. int terrainHeight = (int)round(*terrainHeightP);
  284. // generate blocks
  285. for (int z = 0; z < WORLD_HEIGHT; z++)
  286. {
  287. *zPos = (float)z;
  288. Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
  289. bool structureAffected = 0;
  290. // check if the block is inside of a structure
  291. for (auto structure : *structures)
  292. {
  293. if (structure->isBlockAffected(
  294. Framework::Vec3<int>(x + centerX, y + centerY, z)))
  295. {
  296. generated = structure->generateBlockAt(
  297. Framework::Vec3<int>(x + centerX, y + centerY, z),
  298. getDimensionId());
  299. structureAffected = 1;
  300. break;
  301. }
  302. }
  303. bool inCave = false;
  304. if (!structureAffected)
  305. {
  306. // check if block is a cave block
  307. inCave = caveGen->isInCave(x + centerX, y + centerY, z);
  308. if (!inCave && z == terrainHeight - 1)
  309. {
  310. // generate biom block
  311. #ifdef CHUNK_GENERATION_DEBUG_LOG
  312. zm.measureStart();
  313. #endif
  314. generated = biom->generateBlock(x + centerX,
  315. y + centerY,
  316. z,
  317. getDimensionId(),
  318. chunk);
  319. #ifdef CHUNK_GENERATION_DEBUG_LOG
  320. zm.measureEnd();
  321. blockGenTime += zm.getSekunden();
  322. #endif
  323. }
  324. }
  325. if (inCave || structureAffected || z >= terrainHeight - 1)
  326. {
  327. if (!inCave && !structureAffected && z > terrainHeight - 1
  328. && z < globalSeaLevel)
  329. {
  330. generated = seaFluidBlockTypeId;
  331. }
  332. if (generated.isA()
  333. || generated.getB() != BlockTypeEnum::AIR)
  334. {
  335. if (maxZ < z)
  336. {
  337. maxZ = z;
  338. }
  339. }
  340. if (generated.isA())
  341. chunk->putBlockAt(
  342. Framework::Vec3<int>(
  343. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  344. generated);
  345. else
  346. chunk->putBlockTypeAt(
  347. Framework::Vec3<int>(
  348. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  349. generated);
  350. }
  351. }
  352. }
  353. }
  354. // generate cave borders
  355. bool generatedMore = true;
  356. while (generatedMore)
  357. {
  358. generatedMore = false;
  359. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  360. {
  361. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  362. {
  363. *xPos = (float)x + (float)centerX;
  364. *yPos = (float)y + (float)centerY;
  365. // calculate height layers
  366. calculateHeightLayers();
  367. // calculate biom
  368. BiomGenerator* biom = zBiomGenerator();
  369. // generate blocks
  370. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  371. {
  372. *zPos = (float)z;
  373. int type = chunk->getBlockTypeAt(Framework::Vec3<int>(
  374. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z));
  375. if (!type)
  376. {
  377. bool needed = false;
  378. for (int i = 0; i < 6; i++)
  379. {
  380. Framework::Vec3<int> pos
  381. = getDirection(getDirectionFromIndex(i))
  382. + Framework::Vec3<int>(
  383. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z);
  384. const Block* neighborBlock = 0;
  385. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  386. && pos.y < CHUNK_SIZE && pos.z >= 0
  387. && pos.z < WORLD_HEIGHT)
  388. {
  389. neighborBlock = chunk->zBlockConst(pos);
  390. }
  391. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  392. {
  393. neighborBlock = Game::INSTANCE->zConstBlock(
  394. Framework::Vec3<int>(pos.x + centerX,
  395. pos.y + centerY,
  396. pos.z),
  397. getDimensionId());
  398. }
  399. if (neighborBlock && neighborBlock->isTransparent())
  400. {
  401. needed = true;
  402. break;
  403. }
  404. }
  405. if (needed)
  406. {
  407. auto generated = biom->generateBlock(x + centerX,
  408. y + centerY,
  409. z,
  410. getDimensionId(),
  411. chunk);
  412. if (generated.isA()
  413. || generated.getB() != BlockTypeEnum::AIR)
  414. {
  415. if (maxZ < z)
  416. {
  417. maxZ = z;
  418. }
  419. }
  420. if (generated.isA())
  421. chunk->putBlockAt(
  422. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  423. y + CHUNK_SIZE / 2,
  424. z),
  425. generated);
  426. else
  427. chunk->putBlockTypeAt(
  428. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  429. y + CHUNK_SIZE / 2,
  430. z),
  431. generated);
  432. generatedMore = true;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. // generate plants
  440. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  441. {
  442. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  443. {
  444. *xPos = (float)x + (float)centerX;
  445. *yPos = (float)y + (float)centerY;
  446. // calculate height layers
  447. calculateHeightLayers();
  448. // calculate biom
  449. BiomGenerator* biom = zBiomGenerator();
  450. // generate blocks
  451. for (int z = maxZ + 1; z > 0; z--)
  452. {
  453. auto current = chunk->getBlockTypeAt(Framework::Vec3<int>(
  454. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z));
  455. if ((current == BlockTypeEnum::AIR
  456. || current == seaFluidBlockTypeId))
  457. {
  458. *zPos = (float)z;
  459. bool underwater = current == seaFluidBlockTypeId;
  460. bool underground = (float)z < *terrainHeightP;
  461. bool surface = z == (int)round(*terrainHeightP);
  462. biom->generatePlants(x + centerX,
  463. y + centerY,
  464. z,
  465. getDimensionId(),
  466. chunk,
  467. underground,
  468. underwater,
  469. surface,
  470. seaFluidBlockTypeId);
  471. }
  472. }
  473. }
  474. }
  475. caveGen->release();
  476. structures->release();
  477. #ifdef CHUNK_GENERATION_DEBUG_LOG
  478. entityGenTime.measureStart();
  479. #endif
  480. *xPos = (float)chunk->getCenter().x;
  481. *yPos = (float)chunk->getCenter().y;
  482. calculateHeightLayers();
  483. BiomGenerator* biom = zBiomGenerator();
  484. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  485. {
  486. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  487. {
  488. for (int z = 0; z < WORLD_HEIGHT; z++)
  489. {
  490. if (chunk->getBlockTypeAt(Framework::Vec3<int>(
  491. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z))
  492. == BlockTypeEnum::AIR)
  493. {
  494. *xPos = (float)x + (float)chunk->getCenter().x;
  495. *yPos = (float)y + (float)chunk->getCenter().y;
  496. *zPos = (float)z;
  497. biom->generateEntities(x + chunk->getCenter().x,
  498. y + chunk->getCenter().y,
  499. z,
  500. getDimensionId(),
  501. chunk);
  502. }
  503. }
  504. }
  505. }
  506. #ifdef CHUNK_GENERATION_DEBUG_LOG
  507. entityGenTime.measureEnd();
  508. zmGlobal.measureEnd();
  509. Framework::Logging::trace() << "structureGenerationTime: " << structureTime;
  510. Framework::Logging::trace() << "caveGenerationTime: " << caveTime;
  511. Framework::Logging::trace() << "blockGenTime: " << blockGenTime;
  512. Framework::Logging::debug() << "totalTime: " << zmGlobal.getSekunden();
  513. #endif
  514. zMemory()->unlock();
  515. return chunk;
  516. }
  517. void BiomedCavedDimensionGenerator::postprocessChunk(Chunk* zChunk)
  518. {
  519. zMemory()->lock();
  520. int borderOffset = 1;
  521. bool generatedMore = true;
  522. while (generatedMore && borderOffset <= CHUNK_SIZE / 2)
  523. {
  524. int centerX = zChunk->getCenter().x;
  525. int centerY = zChunk->getCenter().y;
  526. generatedMore = false;
  527. for (int d = 0; d < 4; d++)
  528. {
  529. Direction dir = getDirectionFromIndex(d);
  530. Chunk* neighbor = zChunk->zNeighbor(dir);
  531. if (!neighbor) continue;
  532. int xOffset = 0;
  533. int yOffset = 0;
  534. int xj = 0;
  535. int yj = 0;
  536. switch (dir)
  537. {
  538. case WEST:
  539. yj = 1;
  540. xOffset = 1;
  541. break;
  542. case NORTH:
  543. xj = 1;
  544. yOffset = 1;
  545. break;
  546. case EAST:
  547. yj = 1;
  548. xOffset = -1;
  549. break;
  550. case SOUTH:
  551. xj = 1;
  552. yOffset = -1;
  553. break;
  554. default:
  555. break;
  556. }
  557. for (int o = 0; o < borderOffset; o++)
  558. {
  559. for (int j = 0; j < CHUNK_SIZE; j++)
  560. {
  561. int x = xOffset * o + xj * j;
  562. if (xOffset < 0)
  563. {
  564. x += CHUNK_SIZE - 1;
  565. }
  566. int y = yOffset * o + yj * j;
  567. if (yOffset < 0)
  568. {
  569. y += CHUNK_SIZE - 1;
  570. }
  571. *xPos = (float)x + (float)centerX - CHUNK_SIZE / 2;
  572. *yPos = (float)y + (float)centerY - CHUNK_SIZE / 2;
  573. // calculate height layers
  574. calculateHeightLayers();
  575. // calculate biom
  576. BiomGenerator* biom = zBiomGenerator();
  577. // generate blocks
  578. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  579. {
  580. *zPos = (float)z;
  581. int type = zChunk->getBlockTypeAt(
  582. Framework::Vec3<int>(x, y, z));
  583. if (!type)
  584. {
  585. bool needed = false;
  586. for (int i = 0; i < 6; i++)
  587. {
  588. Framework::Vec3<int> pos
  589. = getDirection(getDirectionFromIndex(i))
  590. + Framework::Vec3<int>(x, y, z);
  591. const Block* neighborBlock = 0;
  592. if (pos.x >= 0 && pos.x < CHUNK_SIZE
  593. && pos.y >= 0 && pos.y < CHUNK_SIZE
  594. && pos.z >= 0 && pos.z < WORLD_HEIGHT)
  595. {
  596. neighborBlock = zChunk->zBlockConst(pos);
  597. }
  598. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT
  599. && i == d)
  600. {
  601. neighborBlock = neighbor->zBlockConst(
  602. {pos.x + centerX
  603. - neighbor->getCenter().x,
  604. pos.y + centerY
  605. - neighbor->getCenter().y,
  606. pos.z});
  607. }
  608. if (neighborBlock
  609. && neighborBlock->isTransparent())
  610. {
  611. needed = true;
  612. break;
  613. }
  614. }
  615. if (needed)
  616. {
  617. auto generated = biom->generateBlock(
  618. x + centerX - CHUNK_SIZE / 2,
  619. y + centerY - CHUNK_SIZE / 2,
  620. z,
  621. getDimensionId(),
  622. zChunk);
  623. if (generated.isA())
  624. zChunk->putBlockAt(
  625. Framework::Vec3<int>(x, y, z),
  626. generated);
  627. else
  628. zChunk->putBlockTypeAt(
  629. Framework::Vec3<int>(x, y, z),
  630. generated);
  631. generatedMore = true;
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. borderOffset++;
  639. }
  640. zMemory()->unlock();
  641. }
  642. Framework::Either<Block*, int> BiomedCavedDimensionGenerator::generateBlock(
  643. Framework::Vec3<int> location)
  644. {
  645. Chunk* zChunk
  646. = Game::INSTANCE->zDimension(getDimensionId())
  647. ->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  648. if (!zChunk)
  649. {
  650. return BlockTypeEnum::NO_BLOCK;
  651. }
  652. zMemory()->lock();
  653. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  654. Framework::RCArray<GeneratedStructure>* structures
  655. = getGeneratedStructoresForArea(location, location);
  656. *xPos = (float)location.x;
  657. *yPos = (float)location.y;
  658. calculateHeightLayers();
  659. BiomGenerator* biom = zBiomGenerator();
  660. *zPos = (float)location.z;
  661. for (auto structure : *structures)
  662. {
  663. if (structure->isBlockAffected(location))
  664. {
  665. auto generated
  666. = structure->generateBlockAt(location, getDimensionId());
  667. structures->release();
  668. zMemory()->unlock();
  669. return generated;
  670. }
  671. }
  672. structures->release();
  673. Framework::Point chunkCenter = Game::getChunkCenter(location.x, location.y);
  674. CaveChunkGenerator* caveGen
  675. = caveGenerator->getGeneratorForChunk(chunkCenter.x, chunkCenter.y);
  676. if (caveGen->isInCave(location.x, location.y, location.z))
  677. {
  678. caveGen->release();
  679. zMemory()->unlock();
  680. return BlockTypeEnum::AIR;
  681. }
  682. caveGen->release();
  683. auto generated = biom->generateBlock(
  684. location.x, location.y, location.z, getDimensionId(), zChunk);
  685. zMemory()->unlock();
  686. return generated;
  687. }
  688. bool BiomedCavedDimensionGenerator::spawnStructure(
  689. Framework::Vec3<int> location,
  690. std::function<bool(GeneratorTemplate* tmpl)> filter)
  691. {
  692. zMemory()->lock();
  693. *xPos = (float)location.x;
  694. *yPos = (float)location.y;
  695. BiomGenerator* biom = zBiomGenerator();
  696. zMemory()->unlock();
  697. for (StructureTemplateCollection* tc : biom->getTemplates())
  698. {
  699. for (GeneratorTemplate* t : tc->getStructures())
  700. {
  701. if (filter(t))
  702. {
  703. RandNoise noise((int)time(0));
  704. GeneratedStructure* genStr
  705. = t->generateAt(location, &noise, getDimensionId());
  706. if (genStr)
  707. {
  708. int minSearchX = location.x + t->getMinAffectedOffset().x;
  709. int minSearchY = location.y + t->getMinAffectedOffset().y;
  710. int minSearchZ
  711. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  712. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  713. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  714. int maxSearchZ
  715. = MIN(location.z + t->getMaxAffectedOffset().z,
  716. WORLD_HEIGHT - 1);
  717. for (int x = minSearchX; x <= maxSearchX; x++)
  718. {
  719. for (int y = minSearchY; y <= maxSearchY; y++)
  720. {
  721. for (int z = minSearchZ; z <= maxSearchZ; z++)
  722. {
  723. if (genStr->isBlockAffected(
  724. Framework::Vec3<int>(x, y, z)))
  725. {
  726. auto gen = genStr->generateBlockAt(
  727. Framework::Vec3<int>(x, y, z),
  728. getDimensionId());
  729. Game::INSTANCE->zDimension(getDimensionId())
  730. ->placeBlock(
  731. Framework::Vec3<int>(x, y, z), gen);
  732. }
  733. }
  734. }
  735. }
  736. genStr->release();
  737. zMemory()->unlock();
  738. return 1;
  739. }
  740. }
  741. }
  742. }
  743. return 0;
  744. }
  745. void BiomedCavedDimensionGenerator::addBiomGenerator(
  746. BiomGenerator* biomGenerator)
  747. {
  748. biomGenerators.add(biomGenerator);
  749. if (biomGenerators.getEntryCount() == 1)
  750. {
  751. minStructureOffset = biomGenerator->getMinStructureOffset();
  752. maxStructureOffset = biomGenerator->getMaxStructureOffset();
  753. }
  754. else
  755. {
  756. Framework::Vec3<int> min = biomGenerator->getMinStructureOffset();
  757. Framework::Vec3<int> max = biomGenerator->getMaxStructureOffset();
  758. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  759. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  760. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  761. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  762. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  763. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  764. }
  765. }
  766. const Framework::RCArray<BiomGenerator>&
  767. BiomedCavedDimensionGenerator::getBiomGenerators() const
  768. {
  769. return biomGenerators;
  770. }
  771. void BiomedCavedDimensionGenerator::setBiomNoiseConfig(
  772. Framework::JSON::JSONObject* biomNoiseConfig)
  773. {
  774. if (noiseConfig) noiseConfig->release();
  775. noiseConfig = biomNoiseConfig;
  776. }
  777. Framework::JSON::JSONObject*
  778. BiomedCavedDimensionGenerator::zBiomNoiseConfig() const
  779. {
  780. return noiseConfig;
  781. }
  782. void BiomedCavedDimensionGenerator::setGlobalSeaLevel(int seaLevel)
  783. {
  784. globalSeaLevel = seaLevel;
  785. }
  786. int BiomedCavedDimensionGenerator::getGlobalSeaLevel() const
  787. {
  788. return globalSeaLevel;
  789. }
  790. void BiomedCavedDimensionGenerator::setTerrainHeightLayerName(
  791. Framework::Text name)
  792. {
  793. terrainHeightLayerName = name;
  794. }
  795. Framework::Text BiomedCavedDimensionGenerator::getTerrainHeightLayerName() const
  796. {
  797. return terrainHeightLayerName;
  798. }
  799. void BiomedCavedDimensionGenerator::setSeaFluidBlockType(Framework::Text type)
  800. {
  801. seaFluidBlockType = type;
  802. }
  803. Framework::Text BiomedCavedDimensionGenerator::getSeaFluidBlockType() const
  804. {
  805. return seaFluidBlockType;
  806. }
  807. BiomedCavedDimensionGeneratorFactory::BiomedCavedDimensionGeneratorFactory() {}
  808. BiomedCavedDimensionGenerator*
  809. BiomedCavedDimensionGeneratorFactory::createValue(
  810. Framework::JSON::JSONObject* zJson) const
  811. {
  812. return new BiomedCavedDimensionGenerator();
  813. }
  814. BiomedCavedDimensionGenerator* BiomedCavedDimensionGeneratorFactory::fromJson(
  815. Framework::JSON::JSONObject* zJson) const
  816. {
  817. BiomedCavedDimensionGenerator* result
  818. = DimensionGeneratorFactory::fromJson(zJson);
  819. result->setBiomNoiseConfig(zJson->getValue("biomNoise")->asObject());
  820. for (Framework::JSON::JSONValue* value : *zJson->zValue("bioms")->asArray())
  821. {
  822. result->addBiomGenerator(
  823. Game::INSTANCE->zTypeRegistry()->fromJson<BiomGenerator>(value));
  824. }
  825. result->setGlobalSeaLevel(
  826. (int)zJson->zValue("globalSeaLevel")->asNumber()->getNumber());
  827. result->setTerrainHeightLayerName(
  828. zJson->zValue("terrainHeightLeyerName")->asString()->getString());
  829. result->setSeaFluidBlockType(
  830. zJson->zValue("seaFluidBlockType")->asString()->getString());
  831. return result;
  832. }
  833. Framework::JSON::JSONObject* BiomedCavedDimensionGeneratorFactory::toJsonObject(
  834. BiomedCavedDimensionGenerator* zObject) const
  835. {
  836. Framework::JSON::JSONObject* result
  837. = DimensionGeneratorFactory::toJsonObject(zObject);
  838. Framework::JSON::JSONArray* bioms = new Framework::JSON::JSONArray();
  839. for (BiomGenerator* biom : zObject->getBiomGenerators())
  840. {
  841. bioms->addValue(
  842. Game::INSTANCE->zTypeRegistry()->toJson<BiomGenerator>(biom));
  843. }
  844. result->addValue("bioms", bioms);
  845. result->addValue("biomNoise",
  846. dynamic_cast<Framework::JSON::JSONValue*>(
  847. zObject->zBiomNoiseConfig()->getThis()));
  848. result->addValue("globalSeaLevel",
  849. new Framework::JSON::JSONNumber(zObject->getGlobalSeaLevel()));
  850. result->addValue("terrainHeightLeyerName",
  851. new Framework::JSON::JSONString(zObject->getTerrainHeightLayerName()));
  852. result->addValue("seaFluidBlockType",
  853. new Framework::JSON::JSONString(zObject->getSeaFluidBlockType()));
  854. return result;
  855. }
  856. JSONObjectValidationBuilder*
  857. BiomedCavedDimensionGeneratorFactory::addToValidator(
  858. JSONObjectValidationBuilder* builder) const
  859. {
  860. return DimensionGeneratorFactory::addToValidator(
  861. builder->withRequiredArray("bioms")
  862. ->addAcceptedTypeInArray(
  863. Game::INSTANCE->zTypeRegistry()->getValidator<BiomGenerator>())
  864. ->finishArray()
  865. ->withRequiredAttribute("biomNoise", JNoise::getValidator(false))
  866. ->withRequiredNumber("globalSeaLevel")
  867. ->finishNumber()
  868. ->withRequiredString("terrainHeightLeyerName")
  869. ->finishString()
  870. ->withRequiredAttribute("seaFluidBlockType",
  871. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  872. BlockTypeNameFactory::TYPE_ID)));
  873. }
  874. const char* BiomedCavedDimensionGeneratorFactory::getTypeToken() const
  875. {
  876. return "cavedBioms";
  877. }