DimensionGenerator.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. #include "DimensionGenerator.h"
  2. #include <iostream>
  3. #include <Logging.h>
  4. #include <Zeit.h>
  5. #include "Constants.h"
  6. #include "Dimension.h"
  7. #include "Game.h"
  8. #include "NoBlock.h"
  9. #include "Noise.h"
  10. #include "RandNoise.h"
  11. #include "WormCaveGenerator.h"
  12. WorldHeightLayer::WorldHeightLayer()
  13. : ReferenceCounter(),
  14. noiseConfig(0),
  15. noise(0),
  16. value(0)
  17. {}
  18. WorldHeightLayer::~WorldHeightLayer()
  19. {
  20. if (noiseConfig) noiseConfig->release();
  21. if (noise) noise->release();
  22. if (value) value->release();
  23. }
  24. void WorldHeightLayer::initialize(JExpressionMemory* zMemory)
  25. {
  26. if (noise) noise->release();
  27. noise = JNoise::parseNoise(noiseConfig, zMemory);
  28. zMemory->setNoise(name, dynamic_cast<Noise*>(noise->getThis()));
  29. valueP = zMemory->getFloatVariableP(name);
  30. value->compile(zMemory);
  31. }
  32. void WorldHeightLayer::calculateValue()
  33. {
  34. *valueP = value->getValue();
  35. }
  36. void WorldHeightLayer::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
  37. {
  38. if (this->noiseConfig) this->noiseConfig->release();
  39. this->noiseConfig = noiseConfig;
  40. }
  41. Framework::JSON::JSONObject* WorldHeightLayer::zNoiseConfig() const
  42. {
  43. return noiseConfig;
  44. }
  45. void WorldHeightLayer::setName(Framework::Text name)
  46. {
  47. this->name = name;
  48. }
  49. Framework::Text WorldHeightLayer::getName() const
  50. {
  51. return name;
  52. }
  53. void WorldHeightLayer::setValue(JFloatExpression* value)
  54. {
  55. if (this->value) this->value->release();
  56. this->value = value;
  57. }
  58. JFloatExpression* WorldHeightLayer::zValue() const
  59. {
  60. return value;
  61. }
  62. WorldHeightLayerFactory::WorldHeightLayerFactory() {}
  63. WorldHeightLayer* WorldHeightLayerFactory::fromJson(
  64. Framework::JSON::JSONObject* zJson) const
  65. {
  66. WorldHeightLayer* result = new WorldHeightLayer();
  67. result->setName(zJson->zValue("name")->asString()->getString());
  68. result->setNoiseConfig(zJson->getValue("noise")->asObject());
  69. result->setValue(
  70. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  71. zJson->zValue("value")));
  72. return result;
  73. }
  74. Framework::JSON::JSONObject* WorldHeightLayerFactory::toJsonObject(
  75. WorldHeightLayer* zObject) const
  76. {
  77. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  78. result->addValue(
  79. "name", new Framework::JSON::JSONString(zObject->getName()));
  80. result->addValue("noise", zObject->zNoiseConfig());
  81. result->addValue("value",
  82. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  83. zObject->zValue()));
  84. return result;
  85. }
  86. JSONObjectValidationBuilder* WorldHeightLayerFactory::addToValidator(
  87. JSONObjectValidationBuilder* builder) const
  88. {
  89. return builder->withRequiredString("name")
  90. ->finishString()
  91. ->withRequiredAttribute("noise", JNoise::getValidator(false))
  92. ->withRequiredAttribute("value",
  93. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  94. }
  95. DimensionGenerator::DimensionGenerator()
  96. : ReferenceCounter(),
  97. jExpressionMemory(new JExpressionMemory()),
  98. seedExpression(0),
  99. dimensionId(0)
  100. {}
  101. DimensionGenerator::~DimensionGenerator()
  102. {
  103. jExpressionMemory->release();
  104. if (seedExpression) seedExpression->release();
  105. }
  106. JExpressionMemory* DimensionGenerator::zMemory() const
  107. {
  108. return jExpressionMemory;
  109. }
  110. void DimensionGenerator::calculateHeightLayers()
  111. {
  112. for (WorldHeightLayer* layer : heightLayers)
  113. {
  114. layer->calculateValue();
  115. }
  116. }
  117. Dimension* DimensionGenerator::createDimension()
  118. {
  119. return new Dimension(getId());
  120. }
  121. void DimensionGenerator::initialize(int worldSeed)
  122. {
  123. this->worldSeed = jExpressionMemory->getFloatVariableP("worldSeed");
  124. *this->worldSeed = (float)worldSeed;
  125. this->dimensionSeed = jExpressionMemory->getFloatVariableP("dimensionSeed");
  126. seedExpression->compile(jExpressionMemory);
  127. *this->dimensionSeed = seedExpression->getValue();
  128. for (WorldHeightLayer* layer : heightLayers)
  129. {
  130. layer->initialize(jExpressionMemory);
  131. }
  132. xPos = jExpressionMemory->getFloatVariableP("x");
  133. yPos = jExpressionMemory->getFloatVariableP("y");
  134. zPos = jExpressionMemory->getFloatVariableP("z");
  135. }
  136. int DimensionGenerator::getDimensionId() const
  137. {
  138. return dimensionId;
  139. }
  140. void DimensionGenerator::addHeightLayer(WorldHeightLayer* layer)
  141. {
  142. heightLayers.add(layer);
  143. }
  144. const Framework::RCArray<WorldHeightLayer>&
  145. DimensionGenerator::getHeightLayers() const
  146. {
  147. return heightLayers;
  148. }
  149. void DimensionGenerator::setName(Framework::Text name)
  150. {
  151. this->name = name;
  152. }
  153. Framework::Text DimensionGenerator::getName() const
  154. {
  155. return name;
  156. }
  157. void DimensionGenerator::setId(int id)
  158. {
  159. dimensionId = id;
  160. }
  161. int DimensionGenerator::getId() const
  162. {
  163. return dimensionId;
  164. }
  165. void DimensionGenerator::setSeed(JFloatExpression* seed)
  166. {
  167. if (seedExpression) seedExpression->release();
  168. seedExpression = seed;
  169. }
  170. JFloatExpression* DimensionGenerator::zSeed() const
  171. {
  172. return seedExpression;
  173. }
  174. BiomedCavedDimensionGenerator::BiomedCavedDimensionGenerator()
  175. : DimensionGenerator(),
  176. caveGenerator(0),
  177. noiseConfig(0),
  178. biomNoise(0)
  179. {}
  180. BiomedCavedDimensionGenerator::~BiomedCavedDimensionGenerator()
  181. {
  182. if (noiseConfig) noiseConfig->release();
  183. if (biomNoise) biomNoise->release();
  184. if (caveGenerator) caveGenerator->release();
  185. }
  186. void BiomedCavedDimensionGenerator::initialize(int worldSeed)
  187. {
  188. if (biomNoise) biomNoise->release();
  189. if (caveGenerator) caveGenerator->release();
  190. DimensionGenerator::initialize(worldSeed);
  191. biomNoise = JNoise::parseNoise(noiseConfig, zMemory());
  192. for (BiomGenerator* gen : biomGenerators)
  193. {
  194. gen->initialize(zMemory());
  195. }
  196. caveGenerator = new WormCaveGenerator(75, 150, 1, 6, 0.1f, worldSeed - 1);
  197. terrainHeightP = zMemory()->getFloatVariableP(terrainHeightLayerName);
  198. seaFluidBlockTypeId = Game::INSTANCE->getBlockTypeId(seaFluidBlockType);
  199. }
  200. BiomGenerator* BiomedCavedDimensionGenerator::zBiomGenerator()
  201. {
  202. for (BiomGenerator* generator : biomGenerators)
  203. {
  204. if (generator->isApplicable()) return generator;
  205. }
  206. return 0;
  207. }
  208. Framework::RCArray<GeneratedStructure>*
  209. BiomedCavedDimensionGenerator::getGeneratedStructoresForArea(
  210. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  211. {
  212. int minSearchX = minPos.x - maxStructureOffset.x;
  213. int minSearchY = minPos.y - maxStructureOffset.y;
  214. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  215. int maxSearchX = maxPos.x - minStructureOffset.x;
  216. int maxSearchY = maxPos.y - minStructureOffset.y;
  217. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  218. Framework::RCArray<GeneratedStructure>* result
  219. = new Framework::RCArray<GeneratedStructure>();
  220. for (int x = minSearchX; x <= maxSearchX; x++)
  221. {
  222. for (int y = minSearchY; y <= maxSearchY; y++)
  223. {
  224. *xPos = (float)x;
  225. *yPos = (float)y;
  226. calculateHeightLayers();
  227. BiomGenerator* gen = zBiomGenerator();
  228. for (int z = minSearchZ; z <= maxSearchZ; z++)
  229. {
  230. *zPos = (float)z;
  231. gen->generateStructures(
  232. x, y, z, getDimensionId(), minPos, maxPos, result);
  233. }
  234. }
  235. }
  236. return result;
  237. }
  238. Chunk* BiomedCavedDimensionGenerator::generateChunk(int centerX, int centerY)
  239. {
  240. zMemory()->lock();
  241. #ifdef CHUNK_GENERATION_DEBUG_LOG
  242. Framework::Logging::debug()
  243. << "generating chunk " << centerX << ", " << centerY;
  244. double structureTime = 0;
  245. double caveTime = 0;
  246. double blockGenTime = 0;
  247. Framework::ZeitMesser zm;
  248. Framework::ZeitMesser zmGlobal;
  249. zm.messungStart();
  250. zmGlobal.messungStart();
  251. #endif
  252. Framework::RCArray<GeneratedStructure>* structures
  253. = getGeneratedStructoresForArea(
  254. Framework::Vec3<int>(
  255. centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
  256. Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
  257. centerY + CHUNK_SIZE / 2,
  258. WORLD_HEIGHT - 1));
  259. #ifdef CHUNK_GENERATION_DEBUG_LOG
  260. zm.messungEnde();
  261. structureTime += zm.getSekunden();
  262. zm.messungStart();
  263. #endif
  264. CaveChunkGenerator* caveGen
  265. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  266. #ifdef CHUNK_GENERATION_DEBUG_LOG
  267. zm.messungEnde();
  268. caveTime += zm.getSekunden();
  269. #endif
  270. Chunk* chunk
  271. = new Chunk(Framework::Punkt(centerX, centerY), getDimensionId());
  272. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(chunk->getThis()));
  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.messungStart();
  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.messungEnde();
  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. chunk->putBlockAt(
  334. Framework::Vec3<int>(
  335. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  336. generated);
  337. else
  338. chunk->putBlockTypeAt(
  339. Framework::Vec3<int>(
  340. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  341. generated);
  342. }
  343. }
  344. }
  345. }
  346. bool generatedMore = true;
  347. while (generatedMore)
  348. {
  349. generatedMore = false;
  350. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  351. {
  352. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  353. {
  354. *xPos = (float)x + (float)centerX;
  355. *yPos = (float)y + (float)centerY;
  356. // calculate height layers
  357. calculateHeightLayers();
  358. // calculate biom
  359. BiomGenerator* biom = zBiomGenerator();
  360. // generate blocks
  361. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  362. {
  363. *zPos = (float)z;
  364. int type = chunk->getBlockTypeAt(Framework::Vec3<int>(
  365. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z));
  366. if (!type)
  367. {
  368. bool needed = false;
  369. for (int i = 0; i < 6; i++)
  370. {
  371. Framework::Vec3<int> pos
  372. = getDirection(
  373. (Directions)getDirectionFromIndex(i))
  374. + Framework::Vec3<int>(
  375. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z);
  376. const Block* neighborBlock = 0;
  377. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  378. && pos.y < CHUNK_SIZE && pos.z >= 0
  379. && pos.z < WORLD_HEIGHT)
  380. {
  381. neighborBlock = chunk->zBlockConst(pos);
  382. }
  383. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  384. {
  385. neighborBlock = Game::INSTANCE->zConstBlock(
  386. Framework::Vec3<int>(pos.x + centerX,
  387. pos.y + centerY,
  388. pos.z),
  389. getDimensionId());
  390. }
  391. if (neighborBlock && neighborBlock->isTransparent())
  392. {
  393. needed = true;
  394. break;
  395. }
  396. }
  397. if (needed)
  398. {
  399. auto generated = biom->generateBlock(x + centerX,
  400. y + centerY,
  401. z,
  402. getDimensionId(),
  403. chunk);
  404. if (generated.isA())
  405. chunk->putBlockAt(
  406. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  407. y + CHUNK_SIZE / 2,
  408. z),
  409. generated);
  410. else
  411. chunk->putBlockTypeAt(
  412. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  413. y + CHUNK_SIZE / 2,
  414. z),
  415. generated);
  416. generatedMore = true;
  417. }
  418. }
  419. }
  420. }
  421. }
  422. }
  423. caveGen->release();
  424. structures->release();
  425. #ifdef CHUNK_GENERATION_DEBUG_LOG
  426. zmGlobal.messungEnde();
  427. Framework::Logging::trace() << "structureGenerationTime: " << structureTime;
  428. Framework::Logging::trace() << "caveGenerationTime: " << caveTime;
  429. Framework::Logging::trace() << "blockGenTime: " << blockGenTime;
  430. Framework::Logging::debug() << "totalTime: " << zmGlobal.getSekunden();
  431. #endif
  432. zMemory()->unlock();
  433. return chunk;
  434. }
  435. void BiomedCavedDimensionGenerator::generateEntities(Chunk* zChunk)
  436. {
  437. zMemory()->lock();
  438. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  439. *xPos = (float)zChunk->getCenter().x;
  440. *yPos = (float)zChunk->getCenter().y;
  441. calculateHeightLayers();
  442. BiomGenerator* biom = zBiomGenerator();
  443. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  444. {
  445. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  446. {
  447. for (int z = 0; z < WORLD_HEIGHT; z++)
  448. {
  449. if (zChunk->getBlockTypeAt(Framework::Vec3<int>(
  450. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z))
  451. == BlockTypeEnum::AIR)
  452. {
  453. *xPos = (float)x + (float)zChunk->getCenter().x;
  454. *yPos = (float)y + (float)zChunk->getCenter().y;
  455. *zPos = (float)z;
  456. biom->generateEntities(x + zChunk->getCenter().x,
  457. y + zChunk->getCenter().y,
  458. z,
  459. getDimensionId());
  460. }
  461. }
  462. }
  463. }
  464. zMemory()->unlock();
  465. }
  466. Framework::Either<Block*, int> BiomedCavedDimensionGenerator::generateBlock(
  467. Framework::Vec3<int> location)
  468. {
  469. Chunk* zChunk
  470. = Game::INSTANCE->zDimension(getDimensionId())
  471. ->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  472. if (!zChunk)
  473. {
  474. return BlockTypeEnum::NO_BLOCK;
  475. }
  476. zMemory()->lock();
  477. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  478. Framework::RCArray<GeneratedStructure>* structures
  479. = getGeneratedStructoresForArea(location, location);
  480. *xPos = (float)location.x;
  481. *yPos = (float)location.y;
  482. calculateHeightLayers();
  483. BiomGenerator* biom = zBiomGenerator();
  484. *zPos = (float)location.z;
  485. for (auto structure : *structures)
  486. {
  487. if (structure->isBlockAffected(location))
  488. {
  489. auto generated
  490. = structure->generateBlockAt(location, getDimensionId());
  491. structures->release();
  492. zMemory()->unlock();
  493. return generated;
  494. }
  495. }
  496. structures->release();
  497. Framework::Punkt chunkCenter = Game::getChunkCenter(location.x, location.y);
  498. CaveChunkGenerator* caveGen
  499. = caveGenerator->getGeneratorForChunk(chunkCenter.x, chunkCenter.y);
  500. if (caveGen->isInCave(location.x, location.y, location.z))
  501. {
  502. caveGen->release();
  503. zMemory()->unlock();
  504. return BlockTypeEnum::AIR;
  505. }
  506. caveGen->release();
  507. auto generated = biom->generateBlock(
  508. location.x, location.y, location.z, getDimensionId(), zChunk);
  509. zMemory()->unlock();
  510. return generated;
  511. }
  512. bool BiomedCavedDimensionGenerator::spawnStructure(
  513. Framework::Vec3<int> location,
  514. std::function<bool(GeneratorTemplate* tmpl)> filter)
  515. {
  516. zMemory()->lock();
  517. *xPos = (float)location.x;
  518. *yPos = (float)location.y;
  519. BiomGenerator* biom = zBiomGenerator();
  520. zMemory()->unlock();
  521. for (StructureTemplateCollection* tc : biom->getTemplates())
  522. {
  523. for (GeneratorTemplate* t : tc->getStructures())
  524. {
  525. if (filter(t))
  526. {
  527. RandNoise noise((int)time(0));
  528. GeneratedStructure* genStr
  529. = t->generateAt(location, &noise, getDimensionId());
  530. if (genStr)
  531. {
  532. int minSearchX = location.x + t->getMinAffectedOffset().x;
  533. int minSearchY = location.y + t->getMinAffectedOffset().y;
  534. int minSearchZ
  535. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  536. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  537. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  538. int maxSearchZ
  539. = MIN(location.z + t->getMaxAffectedOffset().z,
  540. WORLD_HEIGHT - 1);
  541. for (int x = minSearchX; x <= maxSearchX; x++)
  542. {
  543. for (int y = minSearchY; y <= maxSearchY; y++)
  544. {
  545. for (int z = minSearchZ; z <= maxSearchZ; z++)
  546. {
  547. if (genStr->isBlockAffected(
  548. Framework::Vec3<int>(x, y, z)))
  549. {
  550. auto gen = genStr->generateBlockAt(
  551. Framework::Vec3<int>(x, y, z),
  552. getDimensionId());
  553. Game::INSTANCE->zDimension(getDimensionId())
  554. ->placeBlock(
  555. Framework::Vec3<int>(x, y, z), gen);
  556. }
  557. }
  558. }
  559. }
  560. genStr->release();
  561. zMemory()->unlock();
  562. return 1;
  563. }
  564. }
  565. }
  566. }
  567. return 0;
  568. }
  569. void BiomedCavedDimensionGenerator::addBiomGenerator(
  570. BiomGenerator* biomGenerator)
  571. {
  572. biomGenerators.add(biomGenerator);
  573. if (biomGenerators.getEintragAnzahl() == 1)
  574. {
  575. minStructureOffset = biomGenerator->getMinStructureOffset();
  576. maxStructureOffset = biomGenerator->getMaxStructureOffset();
  577. }
  578. else
  579. {
  580. Framework::Vec3<int> min = biomGenerator->getMinStructureOffset();
  581. Framework::Vec3<int> max = biomGenerator->getMaxStructureOffset();
  582. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  583. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  584. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  585. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  586. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  587. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  588. }
  589. }
  590. const Framework::RCArray<BiomGenerator>&
  591. BiomedCavedDimensionGenerator::getBiomGenerators() const
  592. {
  593. return biomGenerators;
  594. }
  595. void BiomedCavedDimensionGenerator::setBiomNoiseConfig(
  596. Framework::JSON::JSONObject* biomNoiseConfig)
  597. {
  598. if (noiseConfig) noiseConfig->release();
  599. noiseConfig = biomNoiseConfig;
  600. }
  601. Framework::JSON::JSONObject*
  602. BiomedCavedDimensionGenerator::zBiomNoiseConfig() const
  603. {
  604. return noiseConfig;
  605. }
  606. void BiomedCavedDimensionGenerator::setGlobalSeaLevel(int seaLevel)
  607. {
  608. globalSeaLevel = seaLevel;
  609. }
  610. int BiomedCavedDimensionGenerator::getGlobalSeaLevel() const
  611. {
  612. return globalSeaLevel;
  613. }
  614. void BiomedCavedDimensionGenerator::setTerrainHeightLayerName(
  615. Framework::Text name)
  616. {
  617. terrainHeightLayerName = name;
  618. }
  619. Framework::Text BiomedCavedDimensionGenerator::getTerrainHeightLayerName() const
  620. {
  621. return terrainHeightLayerName;
  622. }
  623. void BiomedCavedDimensionGenerator::setSeaFluidBlockType(Framework::Text type)
  624. {
  625. seaFluidBlockType = type;
  626. }
  627. Framework::Text BiomedCavedDimensionGenerator::getSeaFluidBlockType() const
  628. {
  629. return seaFluidBlockType;
  630. }
  631. BiomedCavedDimensionGeneratorFactory::BiomedCavedDimensionGeneratorFactory() {}
  632. BiomedCavedDimensionGenerator*
  633. BiomedCavedDimensionGeneratorFactory::createValue(
  634. Framework::JSON::JSONObject* zJson) const
  635. {
  636. return new BiomedCavedDimensionGenerator();
  637. }
  638. BiomedCavedDimensionGenerator* BiomedCavedDimensionGeneratorFactory::fromJson(
  639. Framework::JSON::JSONObject* zJson) const
  640. {
  641. BiomedCavedDimensionGenerator* result
  642. = DimensionGeneratorFactory::fromJson(zJson);
  643. result->setBiomNoiseConfig(zJson->getValue("biomNoise")->asObject());
  644. for (Framework::JSON::JSONValue* value : *zJson->zValue("bioms")->asArray())
  645. {
  646. result->addBiomGenerator(
  647. Game::INSTANCE->zTypeRegistry()->fromJson<BiomGenerator>(value));
  648. }
  649. result->setGlobalSeaLevel(
  650. (int)zJson->zValue("globalSeaLevel")->asNumber()->getNumber());
  651. result->setTerrainHeightLayerName(
  652. zJson->zValue("terrainHeightLeyerName")->asString()->getString());
  653. result->setSeaFluidBlockType(
  654. zJson->zValue("seaFluidBlockType")->asString()->getString());
  655. return result;
  656. }
  657. Framework::JSON::JSONObject* BiomedCavedDimensionGeneratorFactory::toJsonObject(
  658. BiomedCavedDimensionGenerator* zObject) const
  659. {
  660. Framework::JSON::JSONObject* result
  661. = DimensionGeneratorFactory::toJsonObject(zObject);
  662. Framework::JSON::JSONArray* bioms = new Framework::JSON::JSONArray();
  663. for (BiomGenerator* biom : zObject->getBiomGenerators())
  664. {
  665. bioms->addValue(
  666. Game::INSTANCE->zTypeRegistry()->toJson<BiomGenerator>(biom));
  667. }
  668. result->addValue("bioms", bioms);
  669. result->addValue("biomNoise",
  670. dynamic_cast<Framework::JSON::JSONValue*>(
  671. zObject->zBiomNoiseConfig()->getThis()));
  672. result->addValue("globalSeaLevel",
  673. new Framework::JSON::JSONNumber(zObject->getGlobalSeaLevel()));
  674. result->addValue("terrainHeightLeyerName",
  675. new Framework::JSON::JSONString(zObject->getTerrainHeightLayerName()));
  676. result->addValue("seaFluidBlockType",
  677. new Framework::JSON::JSONString(zObject->getSeaFluidBlockType()));
  678. return result;
  679. }
  680. JSONObjectValidationBuilder*
  681. BiomedCavedDimensionGeneratorFactory::addToValidator(
  682. JSONObjectValidationBuilder* builder) const
  683. {
  684. return DimensionGeneratorFactory::addToValidator(
  685. builder->withRequiredArray("bioms")
  686. ->addAcceptedTypeInArray(
  687. Game::INSTANCE->zTypeRegistry()->getValidator<BiomGenerator>())
  688. ->finishArray()
  689. ->withRequiredAttribute("biomNoise", JNoise::getValidator(false))
  690. ->withRequiredNumber("globalSeaLevel")
  691. ->finishNumber()
  692. ->withRequiredString("terrainHeightLeyerName")
  693. ->finishString()
  694. ->withRequiredAttribute("seaFluidBlockType",
  695. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  696. BlockTypeNameFactory::TYPE_ID)));
  697. }
  698. const char* BiomedCavedDimensionGeneratorFactory::getTypeToken() const
  699. {
  700. return "cavedBioms";
  701. }