DimensionGenerator.cpp 32 KB

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