DimensionGenerator.cpp 19 KB

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