DimensionGenerator.cpp 32 KB

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