DimensionGenerator.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. Framework::ZeitMesser entityGenTime;
  250. zm.messungStart();
  251. zmGlobal.messungStart();
  252. #endif
  253. Framework::RCArray<GeneratedStructure>* structures
  254. = getGeneratedStructoresForArea(
  255. Framework::Vec3<int>(
  256. centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
  257. Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
  258. centerY + CHUNK_SIZE / 2,
  259. WORLD_HEIGHT - 1));
  260. #ifdef CHUNK_GENERATION_DEBUG_LOG
  261. zm.messungEnde();
  262. structureTime += zm.getSekunden();
  263. zm.messungStart();
  264. #endif
  265. CaveChunkGenerator* caveGen
  266. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  267. #ifdef CHUNK_GENERATION_DEBUG_LOG
  268. zm.messungEnde();
  269. caveTime += zm.getSekunden();
  270. #endif
  271. Chunk* chunk
  272. = new Chunk(Framework::Punkt(centerX, centerY), getDimensionId());
  273. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(chunk->getThis()));
  274. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  275. {
  276. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  277. {
  278. *xPos = (float)x + (float)centerX;
  279. *yPos = (float)y + (float)centerY;
  280. // calculate height layers
  281. calculateHeightLayers();
  282. // calculate biom
  283. BiomGenerator* biom = zBiomGenerator();
  284. int terrainHeight = (int)round(*terrainHeightP);
  285. // generate blocks
  286. for (int z = 0; z < WORLD_HEIGHT; z++)
  287. {
  288. *zPos = (float)z;
  289. Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
  290. bool structureAffected = 0;
  291. // check if the block is inside of a structure
  292. for (auto structure : *structures)
  293. {
  294. if (structure->isBlockAffected(
  295. Framework::Vec3<int>(x + centerX, y + centerY, z)))
  296. {
  297. generated = structure->generateBlockAt(
  298. Framework::Vec3<int>(x + centerX, y + centerY, z),
  299. getDimensionId());
  300. structureAffected = 1;
  301. break;
  302. }
  303. }
  304. bool inCave = false;
  305. if (!structureAffected)
  306. {
  307. // check if block is a cave block
  308. inCave = caveGen->isInCave(x + centerX, y + centerY, z);
  309. if (!inCave && z == terrainHeight - 1)
  310. {
  311. // generate biom block
  312. #ifdef CHUNK_GENERATION_DEBUG_LOG
  313. zm.messungStart();
  314. #endif
  315. generated = biom->generateBlock(x + centerX,
  316. y + centerY,
  317. z,
  318. getDimensionId(),
  319. chunk);
  320. #ifdef CHUNK_GENERATION_DEBUG_LOG
  321. zm.messungEnde();
  322. blockGenTime += zm.getSekunden();
  323. #endif
  324. }
  325. }
  326. if (inCave || structureAffected || z >= terrainHeight - 1)
  327. {
  328. if (!inCave && !structureAffected && z > terrainHeight - 1
  329. && z < globalSeaLevel)
  330. {
  331. generated = seaFluidBlockTypeId;
  332. }
  333. if (generated.isA())
  334. chunk->putBlockAt(
  335. Framework::Vec3<int>(
  336. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  337. generated);
  338. else
  339. chunk->putBlockTypeAt(
  340. Framework::Vec3<int>(
  341. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  342. generated);
  343. }
  344. }
  345. }
  346. }
  347. // generate cave borders
  348. bool generatedMore = true;
  349. while (generatedMore)
  350. {
  351. generatedMore = false;
  352. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  353. {
  354. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  355. {
  356. *xPos = (float)x + (float)centerX;
  357. *yPos = (float)y + (float)centerY;
  358. // calculate height layers
  359. calculateHeightLayers();
  360. // calculate biom
  361. BiomGenerator* biom = zBiomGenerator();
  362. // generate blocks
  363. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  364. {
  365. *zPos = (float)z;
  366. int type = chunk->getBlockTypeAt(Framework::Vec3<int>(
  367. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z));
  368. if (!type)
  369. {
  370. bool needed = false;
  371. for (int i = 0; i < 6; i++)
  372. {
  373. Framework::Vec3<int> pos
  374. = getDirection(
  375. (Directions)getDirectionFromIndex(i))
  376. + Framework::Vec3<int>(
  377. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z);
  378. const Block* neighborBlock = 0;
  379. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  380. && pos.y < CHUNK_SIZE && pos.z >= 0
  381. && pos.z < WORLD_HEIGHT)
  382. {
  383. neighborBlock = chunk->zBlockConst(pos);
  384. }
  385. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  386. {
  387. neighborBlock = Game::INSTANCE->zConstBlock(
  388. Framework::Vec3<int>(pos.x + centerX,
  389. pos.y + centerY,
  390. pos.z),
  391. getDimensionId());
  392. }
  393. if (neighborBlock && neighborBlock->isTransparent())
  394. {
  395. needed = true;
  396. break;
  397. }
  398. }
  399. if (needed)
  400. {
  401. auto generated = biom->generateBlock(x + centerX,
  402. y + centerY,
  403. z,
  404. getDimensionId(),
  405. chunk);
  406. if (generated.isA())
  407. chunk->putBlockAt(
  408. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  409. y + CHUNK_SIZE / 2,
  410. z),
  411. generated);
  412. else
  413. chunk->putBlockTypeAt(
  414. Framework::Vec3<int>(x + CHUNK_SIZE / 2,
  415. y + CHUNK_SIZE / 2,
  416. z),
  417. generated);
  418. generatedMore = true;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. // generate plants
  426. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  427. {
  428. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  429. {
  430. *xPos = (float)x + (float)centerX;
  431. *yPos = (float)y + (float)centerY;
  432. // calculate height layers
  433. calculateHeightLayers();
  434. // calculate biom
  435. BiomGenerator* biom = zBiomGenerator();
  436. // generate blocks
  437. for (int z = (int)round(*terrainHeightP) + 1; z > 0; z--)
  438. {
  439. auto current = chunk->getBlockTypeAt(Framework::Vec3<int>(
  440. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z));
  441. auto below = chunk->getBlockTypeAt(Framework::Vec3<int>(
  442. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z - 1));
  443. if ((current == BlockTypeEnum::AIR
  444. || current == seaFluidBlockTypeId)
  445. && below != BlockTypeEnum::AIR
  446. && below != seaFluidBlockTypeId)
  447. {
  448. *zPos = (float)z;
  449. bool underwater = current == seaFluidBlockTypeId;
  450. bool underground = z < *terrainHeightP;
  451. biom->generatePlants(x + centerX,
  452. y + centerY,
  453. z,
  454. getDimensionId(),
  455. chunk,
  456. underground,
  457. underwater,
  458. seaFluidBlockTypeId);
  459. }
  460. }
  461. }
  462. }
  463. caveGen->release();
  464. structures->release();
  465. #ifdef CHUNK_GENERATION_DEBUG_LOG
  466. entityGenTime.messungStart();
  467. #endif
  468. *xPos = (float)chunk->getCenter().x;
  469. *yPos = (float)chunk->getCenter().y;
  470. calculateHeightLayers();
  471. BiomGenerator* biom = zBiomGenerator();
  472. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  473. {
  474. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  475. {
  476. for (int z = 0; z < WORLD_HEIGHT; z++)
  477. {
  478. if (chunk->getBlockTypeAt(Framework::Vec3<int>(
  479. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z))
  480. == BlockTypeEnum::AIR)
  481. {
  482. *xPos = (float)x + (float)chunk->getCenter().x;
  483. *yPos = (float)y + (float)chunk->getCenter().y;
  484. *zPos = (float)z;
  485. biom->generateEntities(x + chunk->getCenter().x,
  486. y + chunk->getCenter().y,
  487. z,
  488. getDimensionId(),
  489. chunk);
  490. }
  491. }
  492. }
  493. }
  494. #ifdef CHUNK_GENERATION_DEBUG_LOG
  495. entityGenTime.messungEnde();
  496. zmGlobal.messungEnde();
  497. Framework::Logging::trace() << "structureGenerationTime: " << structureTime;
  498. Framework::Logging::trace() << "caveGenerationTime: " << caveTime;
  499. Framework::Logging::trace() << "blockGenTime: " << blockGenTime;
  500. Framework::Logging::debug() << "totalTime: " << zmGlobal.getSekunden();
  501. #endif
  502. zMemory()->unlock();
  503. return chunk;
  504. }
  505. void BiomedCavedDimensionGenerator::postprocessChunk(Chunk* zChunk)
  506. {
  507. zMemory()->lock();
  508. int borderOffset = 1;
  509. bool generatedMore = true;
  510. while (generatedMore && borderOffset <= CHUNK_SIZE / 2)
  511. {
  512. int centerX = zChunk->getCenter().x;
  513. int centerY = zChunk->getCenter().y;
  514. generatedMore = false;
  515. for (int d = 0; d < 4; d++)
  516. {
  517. Direction dir = getDirectionFromIndex(d);
  518. Chunk* neighbor = zChunk->zNeighbor(dir);
  519. if (!neighbor) continue;
  520. int xOffset = 0;
  521. int yOffset = 0;
  522. int xj = 0;
  523. int yj = 0;
  524. switch (dir)
  525. {
  526. case WEST:
  527. yj = 1;
  528. xOffset = 1;
  529. break;
  530. case NORTH:
  531. xj = 1;
  532. yOffset = 1;
  533. break;
  534. case EAST:
  535. yj = 1;
  536. xOffset = -1;
  537. break;
  538. case SOUTH:
  539. xj = 1;
  540. yOffset = -1;
  541. break;
  542. }
  543. for (int o = 0; o < borderOffset; o++)
  544. {
  545. for (int j = 0; j < CHUNK_SIZE; j++)
  546. {
  547. int x = xOffset * o + xj * j;
  548. if (xOffset < 0)
  549. {
  550. x += CHUNK_SIZE - 1;
  551. }
  552. int y = yOffset * o + yj * j;
  553. if (yOffset < 0)
  554. {
  555. y += CHUNK_SIZE - 1;
  556. }
  557. *xPos = (float)x + (float)centerX - CHUNK_SIZE / 2;
  558. *yPos = (float)y + (float)centerY - CHUNK_SIZE / 2;
  559. // calculate height layers
  560. calculateHeightLayers();
  561. // calculate biom
  562. BiomGenerator* biom = zBiomGenerator();
  563. // generate blocks
  564. for (int z = (int)round(*terrainHeightP) - 1; z >= 0; z--)
  565. {
  566. *zPos = (float)z;
  567. int type = zChunk->getBlockTypeAt(
  568. Framework::Vec3<int>(x, y, z));
  569. if (!type)
  570. {
  571. bool needed = false;
  572. for (int i = 0; i < 6; i++)
  573. {
  574. Framework::Vec3<int> pos
  575. = getDirection(
  576. (Directions)getDirectionFromIndex(i))
  577. + Framework::Vec3<int>(x, y, z);
  578. const Block* neighborBlock = 0;
  579. if (pos.x >= 0 && pos.x < CHUNK_SIZE
  580. && pos.y >= 0 && pos.y < CHUNK_SIZE
  581. && pos.z >= 0 && pos.z < WORLD_HEIGHT)
  582. {
  583. neighborBlock = zChunk->zBlockConst(pos);
  584. }
  585. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT
  586. && i == d)
  587. {
  588. neighborBlock = neighbor->zBlockConst(
  589. {pos.x + centerX
  590. - neighbor->getCenter().x,
  591. pos.y + centerY
  592. - neighbor->getCenter().y,
  593. pos.z});
  594. }
  595. if (neighborBlock
  596. && neighborBlock->isTransparent())
  597. {
  598. needed = true;
  599. break;
  600. }
  601. }
  602. if (needed)
  603. {
  604. auto generated = biom->generateBlock(
  605. x + centerX - CHUNK_SIZE / 2,
  606. y + centerY - CHUNK_SIZE / 2,
  607. z,
  608. getDimensionId(),
  609. zChunk);
  610. if (generated.isA())
  611. zChunk->putBlockAt(
  612. Framework::Vec3<int>(x, y, z),
  613. generated);
  614. else
  615. zChunk->putBlockTypeAt(
  616. Framework::Vec3<int>(x, y, z),
  617. generated);
  618. generatedMore = true;
  619. }
  620. }
  621. }
  622. }
  623. }
  624. }
  625. borderOffset++;
  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. }