DimensionGenerator.cpp 32 KB

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