DimensionGenerator.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. }
  30. void WorldHeightLayer::calculateValue(JExpressionMemory* zMemory)
  31. {
  32. zMemory->setFloatVariable(name, value->getValue(zMemory));
  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::createValue(
  62. Framework::JSON::JSONObject* zJson) const
  63. {
  64. return new WorldHeightLayer();
  65. }
  66. void WorldHeightLayerFactory::fromJson(
  67. WorldHeightLayer* zResult, Framework::JSON::JSONObject* zJson) const
  68. {
  69. zResult->setName(zJson->zValue("name")->asString()->getString());
  70. zResult->setNoiseConfig(zJson->getValue("noise")->asObject());
  71. zResult->setValue(
  72. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  73. zJson->zValue("value")));
  74. }
  75. void WorldHeightLayerFactory::toJson(
  76. WorldHeightLayer* zObject, Framework::JSON::JSONObject* zResult) const
  77. {
  78. zResult->addValue(
  79. "name", new Framework::JSON::JSONString(zObject->getName()));
  80. zResult->addValue("noise", zObject->zNoiseConfig());
  81. zResult->addValue("value",
  82. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  83. zObject->zValue()));
  84. }
  85. JSONObjectValidationBuilder* WorldHeightLayerFactory::addToValidator(
  86. JSONObjectValidationBuilder* builder) const
  87. {
  88. return builder->withRequiredString("name")
  89. ->finishString()
  90. ->withRequiredAttribute("noise", JNoise::getValidator(false))
  91. ->withRequiredAttribute("value",
  92. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  93. }
  94. DimensionGenerator::DimensionGenerator()
  95. : ReferenceCounter(),
  96. jExpressionMemory(new JExpressionMemory()),
  97. seedExpression(0),
  98. dimensionId(0)
  99. {}
  100. DimensionGenerator::~DimensionGenerator()
  101. {
  102. jExpressionMemory->release();
  103. if (seedExpression) seedExpression->release();
  104. }
  105. JExpressionMemory* DimensionGenerator::zMemory() const
  106. {
  107. return jExpressionMemory;
  108. }
  109. void DimensionGenerator::calculateHeightLayers()
  110. {
  111. for (WorldHeightLayer* layer : heightLayers)
  112. {
  113. layer->calculateValue(jExpressionMemory);
  114. }
  115. }
  116. Dimension* DimensionGenerator::createDimension()
  117. {
  118. return new Dimension(getId());
  119. }
  120. void DimensionGenerator::initialize(int worldSeed)
  121. {
  122. jExpressionMemory->setFloatVariable("worldSeed", (float)worldSeed);
  123. jExpressionMemory->setFloatVariable(
  124. "dimensionSeed", seedExpression->getValue(jExpressionMemory));
  125. for (WorldHeightLayer* layer : heightLayers)
  126. {
  127. layer->initialize(jExpressionMemory);
  128. }
  129. }
  130. int DimensionGenerator::getDimensionId() const
  131. {
  132. return dimensionId;
  133. }
  134. void DimensionGenerator::addHeightLayer(WorldHeightLayer* layer)
  135. {
  136. heightLayers.add(layer);
  137. }
  138. const Framework::RCArray<WorldHeightLayer>&
  139. DimensionGenerator::getHeightLayers() const
  140. {
  141. return heightLayers;
  142. }
  143. void DimensionGenerator::setName(Framework::Text name)
  144. {
  145. this->name = name;
  146. }
  147. Framework::Text DimensionGenerator::getName() const
  148. {
  149. return name;
  150. }
  151. void DimensionGenerator::setId(int id)
  152. {
  153. dimensionId = id;
  154. }
  155. int DimensionGenerator::getId() const
  156. {
  157. return dimensionId;
  158. }
  159. void DimensionGenerator::setSeed(JFloatExpression* seed)
  160. {
  161. if (seedExpression) seedExpression->release();
  162. seedExpression = seed;
  163. }
  164. JFloatExpression* DimensionGenerator::zSeed() const
  165. {
  166. return seedExpression;
  167. }
  168. BiomedCavedDimensionGenerator::BiomedCavedDimensionGenerator()
  169. : DimensionGenerator(),
  170. caveGenerator(0),
  171. noiseConfig(0),
  172. biomNoise(0)
  173. {}
  174. BiomedCavedDimensionGenerator::~BiomedCavedDimensionGenerator()
  175. {
  176. if (noiseConfig) noiseConfig->release();
  177. if (biomNoise) biomNoise->release();
  178. if (caveGenerator) caveGenerator->release();
  179. }
  180. void BiomedCavedDimensionGenerator::initialize(int worldSeed)
  181. {
  182. if (biomNoise) biomNoise->release();
  183. if (caveGenerator) caveGenerator->release();
  184. DimensionGenerator::initialize(worldSeed);
  185. biomNoise = JNoise::parseNoise(noiseConfig, zMemory());
  186. for (BiomGenerator* gen : biomGenerators)
  187. {
  188. gen->initialize(zMemory());
  189. }
  190. caveGenerator = new WormCaveGenerator(75, 150, 1, 6, 0.1f, worldSeed - 1);
  191. }
  192. BiomGenerator* BiomedCavedDimensionGenerator::zBiomGenerator()
  193. {
  194. for (BiomGenerator* generator : biomGenerators)
  195. {
  196. if (generator->isApplicable(zMemory())) return generator;
  197. }
  198. return 0;
  199. }
  200. Framework::RCArray<GeneratedStructure>*
  201. BiomedCavedDimensionGenerator::getGeneratedStructoresForArea(
  202. Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
  203. {
  204. int minSearchX = minPos.x - maxStructureOffset.x;
  205. int minSearchY = minPos.y - maxStructureOffset.y;
  206. int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
  207. int maxSearchX = maxPos.x - minStructureOffset.x;
  208. int maxSearchY = maxPos.y - minStructureOffset.y;
  209. int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
  210. Framework::RCArray<GeneratedStructure>* result
  211. = new Framework::RCArray<GeneratedStructure>();
  212. for (int x = minSearchX; x <= maxSearchX; x++)
  213. {
  214. for (int y = minSearchY; y <= maxSearchY; y++)
  215. {
  216. zMemory()->setFloatVariable("x", (float)x);
  217. zMemory()->setFloatVariable("y", (float)y);
  218. calculateHeightLayers();
  219. BiomGenerator* gen = zBiomGenerator();
  220. for (int z = minSearchZ; z <= maxSearchZ; z++)
  221. {
  222. zMemory()->setFloatVariable("z", (float)z);
  223. gen->generateStructures(x,
  224. y,
  225. z,
  226. getDimensionId(),
  227. zMemory(),
  228. minPos,
  229. maxPos,
  230. result);
  231. }
  232. }
  233. }
  234. return result;
  235. }
  236. Chunk* BiomedCavedDimensionGenerator::generateChunk(int centerX, int centerY)
  237. {
  238. zMemory()->lock();
  239. Framework::Logging::debug()
  240. << "generating chunk " << centerX << ", " << centerY;
  241. double structureTime = 0;
  242. double structureTime2 = 0;
  243. double structureTime3 = 0;
  244. double caveTime = 0;
  245. double caveTime2 = 0;
  246. double blockGenTime = 0;
  247. double biomTime = 0;
  248. double layerTime = 0;
  249. Framework::ZeitMesser zm;
  250. Framework::ZeitMesser zmGlobal;
  251. zm.messungStart();
  252. zmGlobal.messungStart();
  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. zm.messungEnde();
  261. structureTime += zm.getSekunden();
  262. zm.messungStart();
  263. CaveChunkGenerator* caveGen
  264. = caveGenerator->getGeneratorForChunk(centerX, centerY);
  265. zm.messungEnde();
  266. caveTime += zm.getSekunden();
  267. Chunk* chunk
  268. = new Chunk(Framework::Punkt(centerX, centerY), getDimensionId());
  269. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(chunk->getThis()));
  270. for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
  271. {
  272. for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
  273. {
  274. zMemory()->setFloatVariable("x", (float)x + (float)centerX);
  275. zMemory()->setFloatVariable("y", (float)y + (float)centerY);
  276. // calculate height layers
  277. zm.messungStart();
  278. calculateHeightLayers();
  279. zm.messungEnde();
  280. layerTime += zm.getSekunden();
  281. // calculate biom
  282. zm.messungStart();
  283. BiomGenerator* biom = zBiomGenerator();
  284. zm.messungEnde();
  285. biomTime += zm.getSekunden();
  286. // generate blocks
  287. for (int z = 0; z < WORLD_HEIGHT; z++)
  288. {
  289. zMemory()->setFloatVariable("z", (float)z);
  290. Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
  291. bool structureAffected = 0;
  292. // check if the block is inside of a structure
  293. zm.messungStart();
  294. for (auto structure : *structures)
  295. {
  296. if (structure->isBlockAffected(
  297. Framework::Vec3<int>(x + centerX, y + centerY, z)))
  298. {
  299. zm.messungEnde();
  300. structureTime2 += zm.getSekunden();
  301. zm.messungStart();
  302. generated = structure->generateBlockAt(
  303. Framework::Vec3<int>(x + centerX, y + centerY, z),
  304. getDimensionId());
  305. structureAffected = 1;
  306. zm.messungEnde();
  307. structureTime3 += zm.getSekunden();
  308. zm.messungStart();
  309. break;
  310. }
  311. }
  312. zm.messungEnde();
  313. structureTime2 += zm.getSekunden();
  314. if (!structureAffected)
  315. {
  316. // check if block is a cave block
  317. zm.messungStart();
  318. bool inCave
  319. = caveGen->isInCave(x + centerX, y + centerY, z);
  320. zm.messungEnde();
  321. caveTime2 += zm.getSekunden();
  322. if (!inCave)
  323. {
  324. // generate biom block
  325. zm.messungStart();
  326. generated = biom->generateBlock(x + centerX,
  327. y + centerY,
  328. z,
  329. getDimensionId(),
  330. zMemory(),
  331. chunk);
  332. zm.messungEnde();
  333. blockGenTime += zm.getSekunden();
  334. }
  335. }
  336. if (generated.isA())
  337. chunk->putBlockAt(
  338. Framework::Vec3<int>(
  339. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  340. generated);
  341. else
  342. chunk->putBlockTypeAt(
  343. Framework::Vec3<int>(
  344. x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
  345. generated);
  346. }
  347. }
  348. }
  349. caveGen->release();
  350. structures->release();
  351. zmGlobal.messungEnde();
  352. Framework::Logging::trace() << "structureGenerationTime: " << structureTime;
  353. Framework::Logging::trace()
  354. << "structure.isBlockAffected: " << structureTime2;
  355. Framework::Logging::trace()
  356. << "structure.generateBlockAt: " << structureTime3;
  357. Framework::Logging::trace() << "caveGenerationTime: " << caveTime;
  358. Framework::Logging::trace() << "caveEvaluationTime: " << caveTime2;
  359. Framework::Logging::trace() << "blockGenTime: " << blockGenTime;
  360. Framework::Logging::trace() << "biomTime: " << biomTime;
  361. Framework::Logging::trace() << "layerTime: " << layerTime;
  362. Framework::Logging::debug() << "totalTime: " << zmGlobal.getSekunden();
  363. zMemory()->unlock();
  364. return chunk;
  365. }
  366. Framework::Either<Block*, int> BiomedCavedDimensionGenerator::generateBlock(
  367. Framework::Vec3<int> location)
  368. {
  369. Chunk* zChunk
  370. = Game::INSTANCE->zDimension(getDimensionId())
  371. ->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  372. if (!zChunk)
  373. {
  374. return BlockTypeEnum::NO_BLOCK;
  375. }
  376. zMemory()->lock();
  377. zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
  378. Framework::RCArray<GeneratedStructure>* structures
  379. = getGeneratedStructoresForArea(location, location);
  380. zMemory()->setFloatVariable("x", (float)location.x);
  381. zMemory()->setFloatVariable("y", (float)location.y);
  382. calculateHeightLayers();
  383. BiomGenerator* biom = zBiomGenerator();
  384. zMemory()->setFloatVariable("z", (float)location.z);
  385. for (auto structure : *structures)
  386. {
  387. if (structure->isBlockAffected(location))
  388. {
  389. auto generated
  390. = structure->generateBlockAt(location, getDimensionId());
  391. structures->release();
  392. zMemory()->unlock();
  393. return generated;
  394. }
  395. }
  396. structures->release();
  397. Framework::Punkt chunkCenter = Game::getChunkCenter(location.x, location.y);
  398. CaveChunkGenerator* caveGen
  399. = caveGenerator->getGeneratorForChunk(chunkCenter.x, chunkCenter.y);
  400. if (caveGen->isInCave(location.x, location.y, location.z))
  401. {
  402. caveGen->release();
  403. zMemory()->unlock();
  404. return BlockTypeEnum::AIR;
  405. }
  406. caveGen->release();
  407. auto generated = biom->generateBlock(location.x,
  408. location.y,
  409. location.z,
  410. getDimensionId(),
  411. zMemory(),
  412. zChunk);
  413. zMemory()->unlock();
  414. return generated;
  415. }
  416. bool BiomedCavedDimensionGenerator::spawnStructure(
  417. Framework::Vec3<int> location,
  418. std::function<bool(GeneratorTemplate* tmpl)> filter)
  419. {
  420. zMemory()->lock();
  421. zMemory()->setFloatVariable("x", (float)location.x);
  422. zMemory()->setFloatVariable("y", (float)location.y);
  423. BiomGenerator* biom = zBiomGenerator();
  424. zMemory()->unlock();
  425. for (StructureTemplateCollection* tc : biom->getTemplates())
  426. {
  427. for (GeneratorTemplate* t : tc->getStructures())
  428. {
  429. if (filter(t))
  430. {
  431. RandNoise noise((int)time(0));
  432. GeneratedStructure* genStr
  433. = t->generateAt(location, &noise, getDimensionId());
  434. if (genStr)
  435. {
  436. int minSearchX = location.x + t->getMinAffectedOffset().x;
  437. int minSearchY = location.y + t->getMinAffectedOffset().y;
  438. int minSearchZ
  439. = MAX(location.z + t->getMinAffectedOffset().z, 0);
  440. int maxSearchX = location.x + t->getMaxAffectedOffset().x;
  441. int maxSearchY = location.y + t->getMaxAffectedOffset().y;
  442. int maxSearchZ
  443. = MIN(location.z + t->getMaxAffectedOffset().z,
  444. WORLD_HEIGHT - 1);
  445. for (int x = minSearchX; x <= maxSearchX; x++)
  446. {
  447. for (int y = minSearchY; y <= maxSearchY; y++)
  448. {
  449. for (int z = minSearchZ; z <= maxSearchZ; z++)
  450. {
  451. if (genStr->isBlockAffected(
  452. Framework::Vec3<int>(x, y, z)))
  453. {
  454. auto gen = genStr->generateBlockAt(
  455. Framework::Vec3<int>(x, y, z),
  456. getDimensionId());
  457. Game::INSTANCE->zDimension(getDimensionId())
  458. ->placeBlock(
  459. Framework::Vec3<int>(x, y, z), gen);
  460. }
  461. }
  462. }
  463. }
  464. genStr->release();
  465. zMemory()->unlock();
  466. return 1;
  467. }
  468. }
  469. }
  470. }
  471. return 0;
  472. }
  473. void BiomedCavedDimensionGenerator::addBiomGenerator(
  474. BiomGenerator* biomGenerator)
  475. {
  476. biomGenerators.add(biomGenerator);
  477. if (biomGenerators.getEintragAnzahl() == 1)
  478. {
  479. minStructureOffset = biomGenerator->getMinStructureOffset();
  480. maxStructureOffset = biomGenerator->getMaxStructureOffset();
  481. }
  482. else
  483. {
  484. Framework::Vec3<int> min = biomGenerator->getMinStructureOffset();
  485. Framework::Vec3<int> max = biomGenerator->getMaxStructureOffset();
  486. if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
  487. if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
  488. if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
  489. if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
  490. if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
  491. if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
  492. }
  493. }
  494. const Framework::RCArray<BiomGenerator>&
  495. BiomedCavedDimensionGenerator::getBiomGenerators() const
  496. {
  497. return biomGenerators;
  498. }
  499. void BiomedCavedDimensionGenerator::setBiomNoiseConfig(
  500. Framework::JSON::JSONObject* biomNoiseConfig)
  501. {
  502. if (noiseConfig) noiseConfig->release();
  503. noiseConfig = biomNoiseConfig;
  504. }
  505. Framework::JSON::JSONObject*
  506. BiomedCavedDimensionGenerator::zBiomNoiseConfig() const
  507. {
  508. return noiseConfig;
  509. }
  510. BiomedCavedDimensionGeneratorFactory::BiomedCavedDimensionGeneratorFactory() {}
  511. BiomedCavedDimensionGenerator*
  512. BiomedCavedDimensionGeneratorFactory::createValue(
  513. Framework::JSON::JSONObject* zJson) const
  514. {
  515. return new BiomedCavedDimensionGenerator();
  516. }
  517. void BiomedCavedDimensionGeneratorFactory::fromJson(
  518. BiomedCavedDimensionGenerator* zResult,
  519. Framework::JSON::JSONObject* zJson) const
  520. {
  521. zResult->setBiomNoiseConfig(zJson->getValue("biomNoise")->asObject());
  522. for (Framework::JSON::JSONValue* value : *zJson->zValue("bioms")->asArray())
  523. {
  524. zResult->addBiomGenerator(
  525. Game::INSTANCE->zTypeRegistry()->fromJson<BiomGenerator>(value));
  526. }
  527. DimensionGeneratorFactory::fromJson(zResult, zJson);
  528. }
  529. void BiomedCavedDimensionGeneratorFactory::toJson(
  530. BiomedCavedDimensionGenerator* zObject,
  531. Framework::JSON::JSONObject* zResult) const
  532. {
  533. Framework::JSON::JSONArray* bioms = new Framework::JSON::JSONArray();
  534. for (BiomGenerator* biom : zObject->getBiomGenerators())
  535. {
  536. bioms->addValue(
  537. Game::INSTANCE->zTypeRegistry()->toJson<BiomGenerator>(biom));
  538. }
  539. zResult->addValue("bioms", bioms);
  540. zResult->addValue("biomNoise",
  541. dynamic_cast<Framework::JSON::JSONValue*>(
  542. zObject->zBiomNoiseConfig()->getThis()));
  543. DimensionGeneratorFactory::toJson(zObject, zResult);
  544. }
  545. JSONObjectValidationBuilder*
  546. BiomedCavedDimensionGeneratorFactory::addToValidator(
  547. JSONObjectValidationBuilder* builder) const
  548. {
  549. return DimensionGeneratorFactory::addToValidator(
  550. builder->withRequiredArray("bioms")
  551. ->addAcceptedTypeInArray(
  552. Game::INSTANCE->zTypeRegistry()->getValidator<BiomGenerator>())
  553. ->finishArray()
  554. ->withRequiredAttribute("biomNoise", JNoise::getValidator(false)));
  555. }
  556. Framework::Text BiomedCavedDimensionGeneratorFactory::getTypeToken() const
  557. {
  558. return "cavedBioms";
  559. }