123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #include "DimensionGenerator.h"
- #include <iostream>
- #include "Constants.h"
- #include "Game.h"
- #include "NoBlock.h"
- #include "Noise.h"
- #include "RandNoise.h"
- DimensionGenerator::DimensionGenerator(
- int dimensionId, CaveGenerator* caveGenerator)
- : ReferenceCounter(),
- dimensionId(dimensionId),
- minTemplateAffectedPosition(0, 0, 0),
- maxTemplateAffectedPosition(0, 0, 0),
- caveGenerator(caveGenerator)
- {
- StaticRegistry<DimensionGenerator>::INSTANCE.registerT(this, dimensionId);
- }
- DimensionGenerator::~DimensionGenerator() {}
- void DimensionGenerator::initialize(int seed)
- {
- this->seed = seed + dimensionId;
- caveGenerator->initialize(this->seed);
- for (auto biomGen : biomGenerators)
- {
- biomGen->setSeed(this->seed);
- }
- }
- BiomGenerator* DimensionGenerator::zBiomGenerator(int x, int y)
- {
- double noise = zBiomNoise()->getNoise((double)x, (double)y, 0.0);
- double border = 0;
- BiomGenerator* gen = 0;
- auto genI = biomGenerators.begin();
- auto distI = biomDistribution.begin();
- do
- {
- border += (double)distI++;
- gen = genI++;
- } while (border < noise && (bool)distI && (bool)genI);
- return gen;
- }
- void DimensionGenerator::registerBiom(
- BiomGenerator* generator, double possibility)
- {
- biomGenerators.add(generator);
- biomDistribution.add(possibility);
- for (auto t : generator->getTemplates())
- {
- minTemplateAffectedPosition.x
- = MIN(minTemplateAffectedPosition.x, t->getMinAffectedOffset().x);
- minTemplateAffectedPosition.y
- = MIN(minTemplateAffectedPosition.y, t->getMinAffectedOffset().y);
- minTemplateAffectedPosition.z
- = MIN(minTemplateAffectedPosition.z, t->getMinAffectedOffset().z);
- maxTemplateAffectedPosition.x
- = MAX(maxTemplateAffectedPosition.x, t->getMaxAffectedOffset().x);
- maxTemplateAffectedPosition.y
- = MAX(maxTemplateAffectedPosition.y, t->getMaxAffectedOffset().y);
- maxTemplateAffectedPosition.z
- = MAX(maxTemplateAffectedPosition.z, t->getMaxAffectedOffset().z);
- }
- generator->setSeed(seed);
- }
- Framework::RCArray<GeneratedStructure>*
- DimensionGenerator::getGeneratedStructoresForArea(
- Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
- {
- Framework::RCArray<GeneratedStructure>* result
- = new Framework::RCArray<GeneratedStructure>();
- int minSearchX = minPos.x - maxTemplateAffectedPosition.x;
- int minSearchY = minPos.y - maxTemplateAffectedPosition.y;
- int minSearchZ = MAX(minPos.z - maxTemplateAffectedPosition.z, 0);
- int maxSearchX = maxPos.x - minTemplateAffectedPosition.x;
- int maxSearchY = maxPos.y - minTemplateAffectedPosition.y;
- int maxSearchZ
- = MIN(maxPos.z - minTemplateAffectedPosition.z, WORLD_HEIGHT - 1);
- Noise* structureNoise = zStructureNoise();
- for (int x = minSearchX; x <= maxSearchX; x++)
- {
- for (int y = minSearchY; y <= maxSearchY; y++)
- {
- BiomGenerator* biom = zBiomGenerator(x, y);
- int height = MIN_AIR_LEVEL
- + (int)(biom->zHeightMapNoise()->getNoise(
- (double)(x), (double)(y), 0.0)
- * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
- for (int z = minSearchZ; z <= maxSearchZ; z++)
- {
- if (z < height)
- {
- double rValue = structureNoise->getNoise(
- (double)x, (double)y, (double)z);
- double probSum = 0;
- for (auto t : biom->getTemplates())
- {
- if (t->isGenerationPossable(
- Framework::Vec3<int>(x, y, z), height - z))
- {
- if (rValue - probSum <= t->getPropability())
- {
- result->add(
- t->generateAt(Framework::Vec3<int>(x, y, z),
- structureNoise,
- dimensionId));
- break;
- }
- }
- probSum += t->getPropability();
- }
- }
- }
- }
- }
- return result;
- }
- Chunk* DimensionGenerator::generateChunk(int centerX, int centerY)
- {
- std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
- double structureTime = 0;
- double caveTime = 0;
- double blockGenTime = 0;
- double biomTime = 0;
- double heightTime = 0;
- ZeitMesser zm;
- ZeitMesser zmGlobal;
- zm.messungStart();
- zmGlobal.messungStart();
- Framework::RCArray<GeneratedStructure>* structures
- = getGeneratedStructoresForArea(
- Framework::Vec3<int>(
- centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0),
- Framework::Vec3<int>(centerX + CHUNK_SIZE / 2,
- centerY + CHUNK_SIZE / 2,
- WORLD_HEIGHT - 1));
- zm.messungEnde();
- structureTime += zm.getSekunden();
- zm.messungStart();
- CaveChunkGenerator* caveGen
- = caveGenerator->getGeneratorForChunk(centerX, centerY);
- zm.messungEnde();
- caveTime += zm.getSekunden();
- Chunk* chunk = new Chunk(Framework::Punkt(centerX, centerY), dimensionId);
- for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
- {
- for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
- {
- zm.messungStart();
- BiomGenerator* biom = zBiomGenerator(x + centerX, y + centerY);
- zm.messungEnde();
- biomTime += zm.getSekunden();
- // TODO: use Noise interpolator for height map between different
- // bioms
- zm.messungStart();
- int height
- = MIN_AIR_LEVEL
- + (int)(biom->zHeightMapNoise()->getNoise(
- (double)(x + centerX), (double)(y + centerY), 0.0)
- * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
- int maxSurfaceHeight
- = (int)(MAX_SURFACE_HEIGHT
- * (1.f
- - (float)(height - MIN_AIR_LEVEL)
- / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
- int actualSurfaceHeight
- = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
- + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
- * (float)biom->zHeightMapNoise()->getNoise(
- (double)(x + centerX),
- (double)(y + centerY),
- 10.0)));
- zm.messungEnde();
- heightTime += zm.getSekunden();
- for (int z = 0; z < WORLD_HEIGHT; z++)
- {
- Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
- bool structureAffected = 0;
- for (auto structure : *structures)
- {
- zm.messungStart();
- if (structure->isBlockAffected(
- Framework::Vec3<int>(x + centerX, y + centerY, z)))
- {
- generated = structure->generateBlockAt(
- Framework::Vec3<int>(x + centerX, y + centerY, z));
- structureAffected = 1;
- zm.messungEnde();
- structureTime += zm.getSekunden();
- break;
- }
- zm.messungEnde();
- structureTime += zm.getSekunden();
- }
- if (!structureAffected)
- {
- zm.messungStart();
- bool inCave
- = caveGen->isInCave(x + centerX, y + centerY, z);
- zm.messungEnde();
- caveTime += zm.getSekunden();
- zm.messungStart();
- if (inCave)
- generated = biom->generateCaveBlock(
- x + centerX, y + centerY, z);
- else if (z < height && z >= height - actualSurfaceHeight)
- generated = biom->generateSurfaceBlock(
- x + centerX, y + centerY, z);
- else if (z < height)
- generated = biom->generateBelowSurfaceBlock(
- x + centerX, y + centerY, z);
- zm.messungEnde();
- blockGenTime += zm.getSekunden();
- }
- if (generated.isA())
- chunk->putBlockAt(
- Framework::Vec3<int>(
- x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
- generated);
- else
- chunk->putBlockTypeAt(
- Framework::Vec3<int>(
- x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z),
- generated);
- }
- }
- }
- caveGen->release();
- structures->release();
- zmGlobal.messungEnde();
- std::cout << "structureTime: " << structureTime << "\n";
- std::cout << "caveTime: " << caveTime << "\n";
- std::cout << "blockGenTime: " << blockGenTime << "\n";
- std::cout << "biomTime: " << biomTime << "\n";
- std::cout << "heightTime: " << heightTime << "\n";
- std::cout << "totalTime: " << zmGlobal.getSekunden() << "\n";
- return chunk;
- }
- Framework::Either<Block*, int> DimensionGenerator::generateBlock(
- Framework::Vec3<int> location)
- {
- Framework::RCArray<GeneratedStructure>* structures
- = getGeneratedStructoresForArea(location, location);
- BiomGenerator* biom = zBiomGenerator(location.x, location.y);
- // TODO: use Noise interpolator for height map between different bioms
- int height = MIN_AIR_LEVEL
- + (int)(biom->zHeightMapNoise()->getNoise(
- (double)(location.x), (double)(location.y), 0.0)
- * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
- int maxSurfaceHeight
- = (int)(MAX_SURFACE_HEIGHT
- * (1.f
- - (float)(height - MIN_AIR_LEVEL)
- / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
- int actualSurfaceHeight
- = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART)
- + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART
- * (float)biom->zHeightMapNoise()->getNoise(
- (double)(location.x), (double)(location.y), 10.0)));
- for (auto structure : *structures)
- {
- if (structure->isBlockAffected(location))
- {
- auto generated = structure->generateBlockAt(location);
- structures->release();
- return generated;
- }
- }
- structures->release();
- if (location.z < height && location.z >= height - actualSurfaceHeight)
- return biom->generateSurfaceBlock(location.x, location.y, location.z);
- else if (location.z < height)
- return biom->generateBelowSurfaceBlock(
- location.x, location.y, location.z);
- return BlockTypeEnum::AIR;
- }
- bool DimensionGenerator::spawnStructure(Framework::Vec3<int> location,
- std::function<bool(GenerationTemplate* tmpl)> filter)
- {
- BiomGenerator* biom = zBiomGenerator(location.x, location.y);
- for (auto t : biom->getTemplates())
- {
- if (filter(t))
- {
- RandNoise noise((int)time(0));
- GeneratedStructure* genStr
- = t->generateAt(location, &noise, dimensionId);
- if (genStr)
- {
- int minSearchX = location.x + t->getMinAffectedOffset().x;
- int minSearchY = location.y + t->getMinAffectedOffset().y;
- int minSearchZ
- = MAX(location.z + t->getMinAffectedOffset().z, 0);
- int maxSearchX = location.x + t->getMaxAffectedOffset().x;
- int maxSearchY = location.y + t->getMaxAffectedOffset().y;
- int maxSearchZ = MIN(
- location.z + t->getMaxAffectedOffset().z, WORLD_HEIGHT - 1);
- for (int x = minSearchX; x <= maxSearchX; x++)
- {
- for (int y = minSearchY; y <= maxSearchY; y++)
- {
- for (int z = minSearchZ; z <= maxSearchZ; z++)
- {
- if (genStr->isBlockAffected(
- Framework::Vec3<int>(x, y, z)))
- {
- auto gen = genStr->generateBlockAt(
- Framework::Vec3<int>(x, y, z));
- Game::INSTANCE->zDimension(dimensionId)
- ->placeBlock(
- Framework::Vec3<int>(x, y, z), gen);
- }
- }
- }
- }
- genStr->release();
- return 1;
- }
- }
- }
- return 0;
- }
- int DimensionGenerator::getDimensionId() const
- {
- return dimensionId;
- }
|