123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #include "DimensionGenerator.h"
- #include <iostream>
- #include "Constants.h"
- #include "Game.h"
- #include "NoBlock.h"
- #include "Noise.h"
- #include "RandNoise.h"
- #include "WormCaveGenerator.h"
- DimensionGeneratorFactory::DimensionGeneratorFactory(
- Framework::Text name, int dimensionId)
- : ReferenceCounter(),
- name(name),
- dimensionId(dimensionId)
- {}
- Framework::Text DimensionGeneratorFactory::getName() const
- {
- return name;
- }
- int DimensionGeneratorFactory::getDimensionId() const
- {
- return dimensionId;
- }
- WorldHeightLayer::WorldHeightLayer(
- Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
- : ReferenceCounter()
- {
- noise = JNoise::parseNoise(zConfig->asObject()->zValue("noise"), zMemory);
- name = zConfig->asObject()->zValue("name")->asString()->getString();
- value = JExpressionParser::parseFloatExpression(
- zConfig->asObject()->zValue("value"));
- zMemory->setNoise(name, dynamic_cast<Noise*>(noise->getThis()));
- }
- WorldHeightLayer::~WorldHeightLayer()
- {
- noise->release();
- value->release();
- }
- void WorldHeightLayer::calculateValue(JExpressionMemory* zMemory)
- {
- zMemory->setFloatVariable(name, value->getValue(zMemory));
- }
- Framework::JSON::Validator::JSONValidator* WorldHeightLayer::getValidator()
- {
- return JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredAttribute("noise", JNoise::getValidator(false))
- ->withRequiredAttribute("value", JExpressionParser::getFloatValidator())
- ->finishObject();
- }
- DimensionGenerator::DimensionGenerator(
- Framework::JSON::JSONValue* zConfig, int worldSeed, int dimensionId)
- : ReferenceCounter(),
- jExpressionMemory(new JExpressionMemory()),
- dimensionId(dimensionId)
- {
- jExpressionMemory->setFloatVariable("worldSeed", (float)worldSeed);
- JFloatExpression* expr = JExpressionParser::parseFloatExpression(
- zConfig->asObject()->zValue("dimensionSeed"));
- jExpressionMemory->setFloatVariable(
- "dimensionSeed", expr->getValue(jExpressionMemory));
- for (Framework::JSON::JSONValue* value :
- *zConfig->asObject()->zValue("heightLayers")->asArray())
- {
- heightLayers.add(new WorldHeightLayer(value, jExpressionMemory));
- }
- }
- DimensionGenerator::~DimensionGenerator()
- {
- jExpressionMemory->release();
- }
- JExpressionMemory* DimensionGenerator::zMemory() const
- {
- return jExpressionMemory;
- }
- void DimensionGenerator::calculateHeightLayers()
- {
- for (WorldHeightLayer* layer : heightLayers)
- {
- layer->calculateValue(jExpressionMemory);
- }
- }
- int DimensionGenerator::getDimensionId() const
- {
- return dimensionId;
- }
- BiomedCavedDimensionGenerator::BiomedCavedDimensionGenerator(
- Framework::JSON::JSONValue* zConfig, int worldSeed, int dimensionId)
- : DimensionGenerator(zConfig, worldSeed, dimensionId),
- caveGenerator(new WormCaveGenerator(75, 150, 1, 6, 0.1f, worldSeed - 1))
- {
- biomNoise = JNoise::parseNoise(
- zConfig->asObject()->zValue("biomNoise"), zMemory());
- bool first = 1;
- for (Framework::JSON::JSONValue* value :
- *zConfig->asObject()->zValue("bioms")->asArray())
- {
- BiomGenerator* gen = new BiomGenerator(value, zMemory());
- biomGenerators.add(gen);
- if (first)
- {
- minStructureOffset = gen->getMinStructureOffset();
- maxStructureOffset = gen->getMaxStructureOffset();
- first = 0;
- }
- else
- {
- Framework::Vec3<int> min = gen->getMinStructureOffset();
- Framework::Vec3<int> max = gen->getMaxStructureOffset();
- if (minStructureOffset.x > min.x) minStructureOffset.x = min.x;
- if (minStructureOffset.y > min.y) minStructureOffset.y = min.y;
- if (minStructureOffset.z > min.z) minStructureOffset.z = min.z;
- if (maxStructureOffset.x < max.x) maxStructureOffset.x = max.x;
- if (maxStructureOffset.y < max.y) maxStructureOffset.y = max.y;
- if (maxStructureOffset.z < max.z) maxStructureOffset.z = max.z;
- }
- }
- }
- BiomedCavedDimensionGenerator::~BiomedCavedDimensionGenerator()
- {
- biomNoise->release();
- caveGenerator->release();
- }
- BiomGenerator* BiomedCavedDimensionGenerator::zBiomGenerator()
- {
- for (BiomGenerator* generator : biomGenerators)
- {
- if (generator->isApplicable(zMemory())) return generator;
- }
- return 0;
- }
- Framework::RCArray<GeneratedStructure>*
- BiomedCavedDimensionGenerator::getGeneratedStructoresForArea(
- Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos)
- {
- int minSearchX = minPos.x - maxStructureOffset.x;
- int minSearchY = minPos.y - maxStructureOffset.y;
- int minSearchZ = MAX(minPos.z - maxStructureOffset.z, 0);
- int maxSearchX = maxPos.x - minStructureOffset.x;
- int maxSearchY = maxPos.y - minStructureOffset.y;
- int maxSearchZ = MIN(maxPos.z - minStructureOffset.z, WORLD_HEIGHT - 1);
- Framework::RCArray<GeneratedStructure>* result
- = new Framework::RCArray<GeneratedStructure>();
- for (int x = minSearchX; x <= maxSearchX; x++)
- {
- for (int y = minSearchY; y <= maxSearchY; y++)
- {
- zMemory()->setFloatVariable("x", (float)x);
- zMemory()->setFloatVariable("y", (float)y);
- calculateHeightLayers();
- BiomGenerator* gen = zBiomGenerator();
- for (int z = minSearchZ; z <= maxSearchZ; z++)
- {
- zMemory()->setFloatVariable("z", (float)z);
- gen->generateStructures(x,
- y,
- z,
- getDimensionId(),
- zMemory(),
- minPos,
- maxPos,
- result);
- }
- }
- }
- return result;
- }
- Chunk* BiomedCavedDimensionGenerator::generateChunk(int centerX, int centerY)
- {
- zMemory()->lock();
- std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
- double structureTime = 0;
- double structureTime2 = 0;
- double structureTime3 = 0;
- double caveTime = 0;
- double caveTime2 = 0;
- double blockGenTime = 0;
- double biomTime = 0;
- double layerTime = 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), getDimensionId());
- zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(chunk->getThis()));
- for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
- {
- for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
- {
- zMemory()->setFloatVariable("x", (float)x + (float)centerX);
- zMemory()->setFloatVariable("y", (float)y + (float)centerY);
- // calculate height layers
- zm.messungStart();
- calculateHeightLayers();
- zm.messungEnde();
- layerTime += zm.getSekunden();
- // calculate biom
- zm.messungStart();
- BiomGenerator* biom = zBiomGenerator();
- zm.messungEnde();
- biomTime += zm.getSekunden();
- // generate blocks
- for (int z = 0; z < WORLD_HEIGHT; z++)
- {
- zMemory()->setFloatVariable(
- "z", (float)z);
- Framework::Either<Block*, int> generated = BlockTypeEnum::AIR;
- bool structureAffected = 0;
- // check if the block is inside of a structure
- zm.messungStart();
- for (auto structure : *structures)
- {
- if (structure->isBlockAffected(
- Framework::Vec3<int>(x + centerX, y + centerY, z)))
- {
- zm.messungEnde();
- structureTime2 += zm.getSekunden();
- zm.messungStart();
- generated = structure->generateBlockAt(
- Framework::Vec3<int>(x + centerX, y + centerY, z),
- getDimensionId());
- structureAffected = 1;
- zm.messungEnde();
- structureTime3 += zm.getSekunden();
- zm.messungStart();
- break;
- }
- }
- zm.messungEnde();
- structureTime2 += zm.getSekunden();
- if (!structureAffected)
- {
- // check if block is a cave block
- zm.messungStart();
- bool inCave
- = caveGen->isInCave(x + centerX, y + centerY, z);
- zm.messungEnde();
- caveTime2 += zm.getSekunden();
- if (!inCave)
- {
- // generate biom block
- zm.messungStart();
- generated = biom->generateBlock(x + centerX,
- y + centerY,
- z,
- getDimensionId(),
- zMemory(),
- chunk);
- 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 << "structureGenerationTime: " << structureTime << "\n";
- std::cout << "structure.isBlockAffected: " << structureTime2 << "\n";
- std::cout << "structure.generateBlockAt: " << structureTime3 << "\n";
- std::cout << "caveGenerationTime: " << caveTime << "\n";
- std::cout << "caveEvaluationTime: " << caveTime2 << "\n";
- std::cout << "blockGenTime: " << blockGenTime << "\n";
- std::cout << "biomTime: " << biomTime << "\n";
- std::cout << "layerTime: " << layerTime << "\n";
- std::cout << "totalTime: " << zmGlobal.getSekunden() << "\n";
- zMemory()->unlock();
- return chunk;
- }
- Framework::Either<Block*, int> BiomedCavedDimensionGenerator::generateBlock(
- Framework::Vec3<int> location)
- {
- Chunk* zChunk
- = Game::INSTANCE->zDimension(getDimensionId())
- ->zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
- if (!zChunk)
- {
- return BlockTypeEnum::NO_BLOCK;
- }
- zMemory()->lock();
- zMemory()->setCurrentChunk(dynamic_cast<Chunk*>(zChunk->getThis()));
- Framework::RCArray<GeneratedStructure>* structures
- = getGeneratedStructoresForArea(location, location);
- zMemory()->setFloatVariable("x", (float)location.x);
- zMemory()->setFloatVariable("y", (float)location.y);
- calculateHeightLayers();
- BiomGenerator* biom = zBiomGenerator();
- zMemory()->setFloatVariable("z", (float)location.z);
- for (auto structure : *structures)
- {
- if (structure->isBlockAffected(location))
- {
- auto generated = structure->generateBlockAt(location, getDimensionId());
- structures->release();
- zMemory()->unlock();
- return generated;
- }
- }
- structures->release();
- Punkt chunkCenter = Game::getChunkCenter(location.x, location.y);
- CaveChunkGenerator* caveGen
- = caveGenerator->getGeneratorForChunk(chunkCenter.x, chunkCenter.y);
- if (caveGen->isInCave(location.x, location.y, location.z))
- {
- caveGen->release();
- zMemory()->unlock();
- return BlockTypeEnum::AIR;
- }
- caveGen->release();
- auto generated = biom->generateBlock(location.x,
- location.y,
- location.z,
- getDimensionId(),
- zMemory(),
- zChunk);
- zMemory()->unlock();
- return generated;
- }
- bool BiomedCavedDimensionGenerator::spawnStructure(
- Framework::Vec3<int> location,
- std::function<bool(GeneratorTemplate* tmpl)> filter)
- {
- zMemory()->lock();
- zMemory()->setFloatVariable("x", (float)location.x);
- zMemory()->setFloatVariable("y", (float)location.y);
- BiomGenerator* biom = zBiomGenerator();
- zMemory()->unlock();
- for (StructureTemplateCollection *tc : biom->getTemplates())
- {
- for (GeneratorTemplate* t : tc->getStructures())
- {
- if (filter(t))
- {
- RandNoise noise((int)time(0));
- GeneratedStructure* genStr
- = t->generateAt(location, &noise, getDimensionId());
- 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),
- getDimensionId());
- Game::INSTANCE->zDimension(getDimensionId())
- ->placeBlock(
- Framework::Vec3<int>(x, y, z), gen);
- }
- }
- }
- }
- genStr->release();
- zMemory()->unlock();
- return 1;
- }
- }
- }
- }
- return 0;
- }
|