#include "DimensionGenerator.h" #include #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::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* DimensionGenerator::getGeneratedStructoresForArea( Framework::Vec3 minPos, Framework::Vec3 maxPos) { Framework::RCArray* result = new Framework::RCArray(); 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 < height; z++) { double rValue = structureNoise->getNoise((double)x, (double)y, (double)z); double probSum = 0; for (auto t : biom->getTemplates()) { if (t->isGenerationPossable( Framework::Vec3(x, y, z), height - z)) { if (rValue - probSum <= t->getPropability()) { result->add( t->generateAt(Framework::Vec3(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 structureTime2 = 0; double structureTime3 = 0; double caveTime = 0; double blockGenTime = 0; double biomTime = 0; double heightTime = 0; ZeitMesser zm; ZeitMesser zmGlobal; zm.messungStart(); zmGlobal.messungStart(); Framework::RCArray* structures = getGeneratedStructoresForArea( Framework::Vec3( centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0), Framework::Vec3(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 generated = BlockTypeEnum::AIR; bool structureAffected = 0; for (auto structure : *structures) { zm.messungStart(); if (structure->isBlockAffected( Framework::Vec3(x + centerX, y + centerY, z))) { zm.messungEnde(); structureTime2 += zm.getSekunden(); zm.messungStart(); generated = structure->generateBlockAt( Framework::Vec3(x + centerX, y + centerY, z), dimensionId); structureAffected = 1; zm.messungEnde(); structureTime3 += zm.getSekunden(); break; } zm.messungEnde(); structureTime2 += 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, dimensionId); else if (z < height && z >= height - actualSurfaceHeight) generated = biom->generateSurfaceBlock( x + centerX, y + centerY, z, dimensionId); else if (z < height) generated = biom->generateBelowSurfaceBlock( x + centerX, y + centerY, z, dimensionId); else if (z >= height && z < WATER_LEVEL) { generated = biom->generateUnderWaterBlock(x + centerX, y + centerY, z, dimensionId, height, chunk); } else { generated = biom->generateAboveSurfaceBlock(x + centerX, y + centerY, z, dimensionId, height, chunk); } zm.messungEnde(); blockGenTime += zm.getSekunden(); } if (generated.isA()) chunk->putBlockAt( Framework::Vec3( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z), generated); else chunk->putBlockTypeAt( Framework::Vec3( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z), generated); } } } caveGen->release(); structures->release(); zmGlobal.messungEnde(); std::cout << "structureGenerationTime: " << structureTime << "\n"; std::cout << "structure.isBlockAffected: " << structureTime2 << "\n"; std::cout << "structure.generateBlockAt: " << structureTime3 << "\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 DimensionGenerator::generateBlock( Framework::Vec3 location) { Framework::RCArray* 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, dimensionId); structures->release(); return generated; } } structures->release(); if (location.z < height && location.z >= height - actualSurfaceHeight) return biom->generateSurfaceBlock( location.x, location.y, location.z, dimensionId); else if (location.z < height) return biom->generateBelowSurfaceBlock( location.x, location.y, location.z, dimensionId); else if (location.z >= height && location.z < WATER_LEVEL) { return biom->generateUnderWaterBlock(location.x, location.y, location.z, dimensionId, height, Game::INSTANCE->zDimension(getDimensionId()) ->zChunk( Game::INSTANCE->getChunkCenter(location.x, location.y))); } if (location.z >= height) { return biom->generateAboveSurfaceBlock(location.x, location.x, location.z, dimensionId, height, Game::INSTANCE->zDimension(getDimensionId()) ->zChunk( Game::INSTANCE->getChunkCenter(location.x, location.y))); } return BlockTypeEnum::AIR; } bool DimensionGenerator::spawnStructure(Framework::Vec3 location, std::function 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(x, y, z))) { auto gen = genStr->generateBlockAt( Framework::Vec3(x, y, z), dimensionId); Game::INSTANCE->zDimension(dimensionId) ->placeBlock( Framework::Vec3(x, y, z), gen); } } } } genStr->release(); return 1; } } } return 0; } int DimensionGenerator::getDimensionId() const { return dimensionId; }