DimensionGenerator.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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::postprocessChunk(Chunk* zChunk)
  436. {
  437. zMemory()->lock();
  438. int borderOffset = 1;
  439. bool generatedMore = true;
  440. while (generatedMore && borderOffset <= CHUNK_SIZE / 2)
  441. {
  442. int centerX = zChunk->getCenter().x;
  443. int centerY = zChunk->getCenter().y;
  444. generatedMore = false;
  445. for (int d = 0; d < 4; d++)
  446. {
  447. Direction dir = getDirectionFromIndex(d);
  448. Chunk* neighbor = zChunk->zNeighbor(dir);
  449. if (!neighbor) continue;
  450. int xOffset = 0;
  451. int yOffset = 0;
  452. int xj = 0;
  453. int yj = 0;
  454. switch (dir)
  455. {
  456. case WEST:
  457. yj = 1;
  458. xOffset = 1;
  459. break;
  460. case NORTH:
  461. xj = 1;
  462. yOffset = 1;
  463. break;
  464. case EAST:
  465. yj = 1;
  466. xOffset = -1;
  467. break;
  468. case SOUTH:
  469. xj = 1;
  470. yOffset = -1;
  471. break;
  472. }
  473. for (int o = 0; o < borderOffset; o++)
  474. {
  475. for (int j = 0; j < CHUNK_SIZE; j++)
  476. {
  477. int x = xOffset * o + xj * j;
  478. if (xOffset < 0)
  479. {
  480. x += CHUNK_SIZE - 1;
  481. }
  482. int y = yOffset * o + yj * j;
  483. if (yOffset < 0)
  484. {
  485. y += CHUNK_SIZE - 1;
  486. }
  487. *xPos = (float)x + (float)centerX - CHUNK_SIZE / 2;
  488. *yPos = (float)y + (float)centerY - CHUNK_SIZE / 2;
  489. // calculate height layers
  490. calculateHeightLayers();
  491. // calculate biom
  492. BiomGenerator* biom = zBiomGenerator();
  493. // generate blocks
  494. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  495. {
  496. *zPos = (float)z;
  497. int type = zChunk->getBlockTypeAt(
  498. Framework::Vec3<int>(x, y, z));
  499. if (!type)
  500. {
  501. bool needed = false;
  502. for (int i = 0; i < 6; i++)
  503. {
  504. Framework::Vec3<int> pos
  505. = getDirection(
  506. (Directions)getDirectionFromIndex(i))
  507. + Framework::Vec3<int>(x, y, z);
  508. const Block* neighborBlock = 0;
  509. if (pos.x >= 0 && pos.x < CHUNK_SIZE
  510. && pos.y >= 0 && pos.y < CHUNK_SIZE
  511. && pos.z >= 0 && pos.z < WORLD_HEIGHT)
  512. {
  513. neighborBlock = zChunk->zBlockConst(pos);
  514. }
  515. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT
  516. && i == d)
  517. {
  518. neighborBlock = neighbor->zBlockConst(
  519. {pos.x + centerX
  520. - neighbor->getCenter().x,
  521. pos.y + centerY
  522. - neighbor->getCenter().y,
  523. pos.z});
  524. }
  525. if (neighborBlock
  526. && neighborBlock->isTransparent())
  527. {
  528. needed = true;
  529. break;
  530. }
  531. }
  532. if (needed)
  533. {
  534. auto generated = biom->generateBlock(
  535. x + centerX - CHUNK_SIZE / 2,
  536. y + centerY - CHUNK_SIZE / 2,
  537. z,
  538. getDimensionId(),
  539. zChunk);
  540. if (generated.isA())
  541. zChunk->putBlockAt(
  542. Framework::Vec3<int>(x, y, z),
  543. generated);
  544. else
  545. zChunk->putBlockTypeAt(
  546. Framework::Vec3<int>(x, y, z),
  547. generated);
  548. generatedMore = true;
  549. }
  550. }
  551. }
  552. }
  553. }
  554. }
  555. borderOffset++;
  556. }
  557. zMemory()->unlock();
  558. }
  559. void BiomedCavedDimensionGenerator::generateEntities(Chunk* zChunk)
  560. {
  561. zMemory()->lock();
  562. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  563. *xPos = (float)zChunk->getCenter().x;
  564. *yPos = (float)zChunk->getCenter().y;
  565. calculateHeightLayers();
  566. BiomGenerator* biom = zBiomGenerator();
  567. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  568. {
  569. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  570. {
  571. for (int z = 0; z < WORLD_HEIGHT; z++)
  572. {
  573. if (zChunk->getBlockTypeAt(Framework::Vec3<int>(
  574. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z))
  575. == BlockTypeEnum::AIR)
  576. {
  577. *xPos = (float)x + (float)zChunk->getCenter().x;
  578. *yPos = (float)y + (float)zChunk->getCenter().y;
  579. *zPos = (float)z;
  580. biom->generateEntities(x + zChunk->getCenter().x,
  581. y + zChunk->getCenter().y,
  582. z,
  583. getDimensionId());
  584. }
  585. }
  586. }
  587. }
  588. zMemory()->unlock();
  589. }
  590. Framework::Either<Block*, int> BiomedCavedDimensionGenerator::generateBlock(
  591. Framework::Vec3<int> location)
  592. {
  593. Chunk* zChunk
  594. = Game::INSTANCE->zDimension(getDimensionId())
  595. ->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  596. if (!zChunk)
  597. {
  598. return BlockTypeEnum::NO_BLOCK;
  599. }
  600. zMemory()->lock();
  601. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  602. Framework::RCArray<GeneratedStructure>* structures
  603. = getGeneratedStructoresForArea(location, location);
  604. *xPos = (float)location.x;
  605. *yPos = (float)location.y;
  606. calculateHeightLayers();
  607. BiomGenerator* biom = zBiomGenerator();
  608. *zPos = (float)location.z;
  609. for (auto structure : *structures)
  610. {
  611. if (structure->isBlockAffected(location))
  612. {
  613. auto generated
  614. = structure->generateBlockAt(location, getDimensionId());
  615. structures->release();
  616. zMemory()->unlock();
  617. return generated;
  618. }
  619. }
  620. structures->release();
  621. Framework::Punkt chunkCenter = Game::getChunkCenter(location.x, location.y);
  622. CaveChunkGenerator* caveGen
  623. = caveGenerator->getGeneratorForChunk(chunkCenter.x, chunkCenter.y);
  624. if (caveGen->isInCave(location.x, location.y, location.z))
  625. {
  626. caveGen->release();
  627. zMemory()->unlock();
  628. return BlockTypeEnum::AIR;
  629. }
  630. caveGen->release();
  631. auto generated = biom->generateBlock(
  632. location.x, location.y, location.z, getDimensionId(), zChunk);
  633. zMemory()->unlock();
  634. return generated;
  635. }
  636. bool BiomedCavedDimensionGenerator::spawnStructure(
  637. Framework::Vec3<int> location,
  638. std::function<bool(GeneratorTemplate* tmpl)> filter)
  639. {
  640. zMemory()->lock();
  641. *xPos = (float)location.x;
  642. *yPos = (float)location.y;
  643. BiomGenerator* biom = zBiomGenerator();
  644. zMemory()->unlock();
  645. for (StructureTemplateCollection* tc : biom->getTemplates())
  646. {
  647. for (GeneratorTemplate* t : tc->getStructures())
  648. {
  649. if (filter(t))
  650. {
  651. RandNoise noise((int)time(0));
  652. GeneratedStructure* genStr
  653. = t->generateAt(location, &noise, getDimensionId());
  654. if (genStr)
  655. {
  656. int minSearchX = location.x + t->getMinAffectedOffset().x;
  657. int minSearchY = location.y + t->getMinAffectedOffset().y;
  658. int minSearchZ
  659. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  660. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  661. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  662. int maxSearchZ
  663. = MIN(location.z + t->getMaxAffectedOffset().z,
  664. WORLD_HEIGHT - 1);
  665. for (int x = minSearchX; x <= maxSearchX; x++)
  666. {
  667. for (int y = minSearchY; y <= maxSearchY; y++)
  668. {
  669. for (int z = minSearchZ; z <= maxSearchZ; z++)
  670. {
  671. if (genStr->isBlockAffected(
  672. Framework::Vec3<int>(x, y, z)))
  673. {
  674. auto gen = genStr->generateBlockAt(
  675. Framework::Vec3<int>(x, y, z),
  676. getDimensionId());
  677. Game::INSTANCE->zDimension(getDimensionId())
  678. ->placeBlock(
  679. Framework::Vec3<int>(x, y, z), gen);
  680. }
  681. }
  682. }
  683. }
  684. genStr->release();
  685. zMemory()->unlock();
  686. return 1;
  687. }
  688. }
  689. }
  690. }
  691. return 0;
  692. }
  693. void BiomedCavedDimensionGenerator::addBiomGenerator(
  694. BiomGenerator* biomGenerator)
  695. {
  696. biomGenerators.add(biomGenerator);
  697. if (biomGenerators.getEintragAnzahl() == 1)
  698. {
  699. minStructureOffset = biomGenerator->getMinStructureOffset();
  700. maxStructureOffset = biomGenerator->getMaxStructureOffset();
  701. }
  702. else
  703. {
  704. Framework::Vec3<int> min = biomGenerator->getMinStructureOffset();
  705. Framework::Vec3<int> max = biomGenerator->getMaxStructureOffset();
  706. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  707. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  708. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  709. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  710. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  711. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  712. }
  713. }
  714. const Framework::RCArray<BiomGenerator>&
  715. BiomedCavedDimensionGenerator::getBiomGenerators() const
  716. {
  717. return biomGenerators;
  718. }
  719. void BiomedCavedDimensionGenerator::setBiomNoiseConfig(
  720. Framework::JSON::JSONObject* biomNoiseConfig)
  721. {
  722. if (noiseConfig) noiseConfig->release();
  723. noiseConfig = biomNoiseConfig;
  724. }
  725. Framework::JSON::JSONObject*
  726. BiomedCavedDimensionGenerator::zBiomNoiseConfig() const
  727. {
  728. return noiseConfig;
  729. }
  730. void BiomedCavedDimensionGenerator::setGlobalSeaLevel(int seaLevel)
  731. {
  732. globalSeaLevel = seaLevel;
  733. }
  734. int BiomedCavedDimensionGenerator::getGlobalSeaLevel() const
  735. {
  736. return globalSeaLevel;
  737. }
  738. void BiomedCavedDimensionGenerator::setTerrainHeightLayerName(
  739. Framework::Text name)
  740. {
  741. terrainHeightLayerName = name;
  742. }
  743. Framework::Text BiomedCavedDimensionGenerator::getTerrainHeightLayerName() const
  744. {
  745. return terrainHeightLayerName;
  746. }
  747. void BiomedCavedDimensionGenerator::setSeaFluidBlockType(Framework::Text type)
  748. {
  749. seaFluidBlockType = type;
  750. }
  751. Framework::Text BiomedCavedDimensionGenerator::getSeaFluidBlockType() const
  752. {
  753. return seaFluidBlockType;
  754. }
  755. BiomedCavedDimensionGeneratorFactory::BiomedCavedDimensionGeneratorFactory() {}
  756. BiomedCavedDimensionGenerator*
  757. BiomedCavedDimensionGeneratorFactory::createValue(
  758. Framework::JSON::JSONObject* zJson) const
  759. {
  760. return new BiomedCavedDimensionGenerator();
  761. }
  762. BiomedCavedDimensionGenerator* BiomedCavedDimensionGeneratorFactory::fromJson(
  763. Framework::JSON::JSONObject* zJson) const
  764. {
  765. BiomedCavedDimensionGenerator* result
  766. = DimensionGeneratorFactory::fromJson(zJson);
  767. result->setBiomNoiseConfig(zJson->getValue("biomNoise")->asObject());
  768. for (Framework::JSON::JSONValue* value : *zJson->zValue("bioms")->asArray())
  769. {
  770. result->addBiomGenerator(
  771. Game::INSTANCE->zTypeRegistry()->fromJson<BiomGenerator>(value));
  772. }
  773. result->setGlobalSeaLevel(
  774. (int)zJson->zValue("globalSeaLevel")->asNumber()->getNumber());
  775. result->setTerrainHeightLayerName(
  776. zJson->zValue("terrainHeightLeyerName")->asString()->getString());
  777. result->setSeaFluidBlockType(
  778. zJson->zValue("seaFluidBlockType")->asString()->getString());
  779. return result;
  780. }
  781. Framework::JSON::JSONObject* BiomedCavedDimensionGeneratorFactory::toJsonObject(
  782. BiomedCavedDimensionGenerator* zObject) const
  783. {
  784. Framework::JSON::JSONObject* result
  785. = DimensionGeneratorFactory::toJsonObject(zObject);
  786. Framework::JSON::JSONArray* bioms = new Framework::JSON::JSONArray();
  787. for (BiomGenerator* biom : zObject->getBiomGenerators())
  788. {
  789. bioms->addValue(
  790. Game::INSTANCE->zTypeRegistry()->toJson<BiomGenerator>(biom));
  791. }
  792. result->addValue("bioms", bioms);
  793. result->addValue("biomNoise",
  794. dynamic_cast<Framework::JSON::JSONValue*>(
  795. zObject->zBiomNoiseConfig()->getThis()));
  796. result->addValue("globalSeaLevel",
  797. new Framework::JSON::JSONNumber(zObject->getGlobalSeaLevel()));
  798. result->addValue("terrainHeightLeyerName",
  799. new Framework::JSON::JSONString(zObject->getTerrainHeightLayerName()));
  800. result->addValue("seaFluidBlockType",
  801. new Framework::JSON::JSONString(zObject->getSeaFluidBlockType()));
  802. return result;
  803. }
  804. JSONObjectValidationBuilder*
  805. BiomedCavedDimensionGeneratorFactory::addToValidator(
  806. JSONObjectValidationBuilder* builder) const
  807. {
  808. return DimensionGeneratorFactory::addToValidator(
  809. builder->withRequiredArray("bioms")
  810. ->addAcceptedTypeInArray(
  811. Game::INSTANCE->zTypeRegistry()->getValidator<BiomGenerator>())
  812. ->finishArray()
  813. ->withRequiredAttribute("biomNoise", JNoise::getValidator(false))
  814. ->withRequiredNumber("globalSeaLevel")
  815. ->finishNumber()
  816. ->withRequiredString("terrainHeightLeyerName")
  817. ->finishString()
  818. ->withRequiredAttribute("seaFluidBlockType",
  819. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  820. BlockTypeNameFactory::TYPE_ID)));
  821. }
  822. const char* BiomedCavedDimensionGeneratorFactory::getTypeToken() const
  823. {
  824. return "cavedBioms";
  825. }