DimensionGenerator.cpp 32 KB

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