123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- #include "DimensionGenerator.h"
- #include <iostream>
- #include <Zeit.h>
- #include "Constants.h"
- #include "Dimension.h"
- #include "Game.h"
- #include "NoBlock.h"
- #include "Noise.h"
- #include "RandNoise.h"
- #include "WormCaveGenerator.h"
- WorldHeightLayer::WorldHeightLayer()
- : ReferenceCounter(),
- noiseConfig(0),
- noise(0),
- value(0)
- {}
- WorldHeightLayer::~WorldHeightLayer()
- {
- if (noiseConfig) noiseConfig->release();
- if (noise) noise->release();
- if (value) value->release();
- }
- void WorldHeightLayer::initialize(JExpressionMemory* zMemory)
- {
- if (noise) noise->release();
- noise = JNoise::parseNoise(noiseConfig, zMemory);
- zMemory->setNoise(name, dynamic_cast<Noise*>(noise->getThis()));
- }
- void WorldHeightLayer::calculateValue(JExpressionMemory* zMemory)
- {
- zMemory->setFloatVariable(name, value->getValue(zMemory));
- }
- void WorldHeightLayer::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
- {
- if (this->noiseConfig) this->noiseConfig->release();
- this->noiseConfig = noiseConfig;
- }
- Framework::JSON::JSONObject* WorldHeightLayer::zNoiseConfig() const
- {
- return noiseConfig;
- }
- void WorldHeightLayer::setName(Framework::Text name)
- {
- this->name = name;
- }
- Framework::Text WorldHeightLayer::getName() const
- {
- return name;
- }
- void WorldHeightLayer::setValue(JFloatExpression* value)
- {
- if (this->value) this->value->release();
- this->value = value;
- }
- JFloatExpression* WorldHeightLayer::zValue() const
- {
- return value;
- }
- WorldHeightLayerFactory::WorldHeightLayerFactory() {}
- WorldHeightLayer* WorldHeightLayerFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new WorldHeightLayer();
- }
- void WorldHeightLayerFactory::fromJson(
- WorldHeightLayer* zResult, Framework::JSON::JSONObject* zJson) const
- {
- zResult->setName(zJson->zValue("name")->asString()->getString());
- zResult->setNoiseConfig(zJson->getValue("noise")->asObject());
- zResult->setValue(
- Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
- zJson->zValue("value")));
- }
- void WorldHeightLayerFactory::toJson(
- WorldHeightLayer* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue(
- "name", new Framework::JSON::JSONString(zObject->getName()));
- zResult->addValue("noise", zObject->zNoiseConfig());
- zResult->addValue("value",
- Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
- zObject->zValue()));
- }
- JSONObjectValidationBuilder* WorldHeightLayerFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->withRequiredString("name")
- ->finishString()
- ->withRequiredAttribute("noise", JNoise::getValidator(false))
- ->withRequiredAttribute("value",
- Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
- }
- DimensionGenerator::DimensionGenerator()
- : ReferenceCounter(),
- jExpressionMemory(new JExpressionMemory()),
- seedExpression(0),
- dimensionId(0)
- {}
- DimensionGenerator::~DimensionGenerator()
- {
- jExpressionMemory->release();
- if (seedExpression) seedExpression->release();
- }
- JExpressionMemory* DimensionGenerator::zMemory() const
- {
- return jExpressionMemory;
- }
- void DimensionGenerator::calculateHeightLayers()
- {
- for (WorldHeightLayer* layer : heightLayers)
- {
- layer->calculateValue(jExpressionMemory);
- }
- }
- Dimension* DimensionGenerator::createDimension()
- {
- return new Dimension(getId());
- }
- void DimensionGenerator::initialize(int worldSeed)
- {
- jExpressionMemory->setFloatVariable("worldSeed", (float)worldSeed);
- jExpressionMemory->setFloatVariable(
- "dimensionSeed", seedExpression->getValue(jExpressionMemory));
- for (WorldHeightLayer* layer : heightLayers)
- {
- layer->initialize(jExpressionMemory);
- }
- }
- int DimensionGenerator::getDimensionId() const
- {
- return dimensionId;
- }
- void DimensionGenerator::addHeightLayer(WorldHeightLayer* layer)
- {
- heightLayers.add(layer);
- }
- const Framework::RCArray<WorldHeightLayer>&
- DimensionGenerator::getHeightLayers() const
- {
- return heightLayers;
- }
- void DimensionGenerator::setName(Framework::Text name)
- {
- this->name = name;
- }
- Framework::Text DimensionGenerator::getName() const
- {
- return name;
- }
- void DimensionGenerator::setId(int id)
- {
- dimensionId = id;
- }
- int DimensionGenerator::getId() const
- {
- return dimensionId;
- }
- void DimensionGenerator::setSeed(JFloatExpression* seed)
- {
- if (seedExpression) seedExpression->release();
- seedExpression = seed;
- }
- JFloatExpression* DimensionGenerator::zSeed() const
- {
- return seedExpression;
- }
- BiomedCavedDimensionGenerator::BiomedCavedDimensionGenerator()
- : DimensionGenerator(),
- caveGenerator(0),
- noiseConfig(0),
- biomNoise(0)
- {}
- BiomedCavedDimensionGenerator::~BiomedCavedDimensionGenerator()
- {
- if (noiseConfig) noiseConfig->release();
- if (biomNoise) biomNoise->release();
- if (caveGenerator) caveGenerator->release();
- }
- void BiomedCavedDimensionGenerator::initialize(int worldSeed)
- {
- if (biomNoise) biomNoise->release();
- if (caveGenerator) caveGenerator->release();
- DimensionGenerator::initialize(worldSeed);
- biomNoise = JNoise::parseNoise(noiseConfig, zMemory());
- for (BiomGenerator* gen : biomGenerators)
- {
- gen->initialize(zMemory());
- }
- caveGenerator = new WormCaveGenerator(75, 150, 1, 6, 0.1f, worldSeed - 1);
- }
- 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;
- Framework::ZeitMesser zm;
- Framework::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();
- Framework::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;
- }
- void BiomedCavedDimensionGenerator::addBiomGenerator(
- BiomGenerator* biomGenerator)
- {
- biomGenerators.add(biomGenerator);
- if (biomGenerators.getEintragAnzahl() == 1)
- {
- minStructureOffset = biomGenerator->getMinStructureOffset();
- maxStructureOffset = biomGenerator->getMaxStructureOffset();
- }
- else
- {
- Framework::Vec3<int> min = biomGenerator->getMinStructureOffset();
- Framework::Vec3<int> max = biomGenerator->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;
- }
- }
- const Framework::RCArray<BiomGenerator>&
- BiomedCavedDimensionGenerator::getBiomGenerators() const
- {
- return biomGenerators;
- }
- void BiomedCavedDimensionGenerator::setBiomNoiseConfig(
- Framework::JSON::JSONObject* biomNoiseConfig)
- {
- if (noiseConfig) noiseConfig->release();
- noiseConfig = biomNoiseConfig;
- }
- Framework::JSON::JSONObject*
- BiomedCavedDimensionGenerator::zBiomNoiseConfig() const
- {
- return noiseConfig;
- }
- BiomedCavedDimensionGeneratorFactory::BiomedCavedDimensionGeneratorFactory() {}
- BiomedCavedDimensionGenerator*
- BiomedCavedDimensionGeneratorFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BiomedCavedDimensionGenerator();
- }
- void BiomedCavedDimensionGeneratorFactory::fromJson(
- BiomedCavedDimensionGenerator* zResult,
- Framework::JSON::JSONObject* zJson) const
- {
- zResult->setBiomNoiseConfig(zJson->getValue("biomNoise")->asObject());
- for (Framework::JSON::JSONValue* value : *zJson->zValue("bioms")->asArray())
- {
- zResult->addBiomGenerator(
- Game::INSTANCE->zTypeRegistry()->fromJson<BiomGenerator>(value));
- }
- DimensionGeneratorFactory::fromJson(zResult, zJson);
- }
- void BiomedCavedDimensionGeneratorFactory::toJson(
- BiomedCavedDimensionGenerator* zObject,
- Framework::JSON::JSONObject* zResult) const
- {
- Framework::JSON::JSONArray* bioms = new Framework::JSON::JSONArray();
- for (BiomGenerator* biom : zObject->getBiomGenerators())
- {
- bioms->addValue(
- Game::INSTANCE->zTypeRegistry()->toJson<BiomGenerator>(biom));
- }
- zResult->addValue("bioms", bioms);
- zResult->addValue("biomNoise",
- dynamic_cast<Framework::JSON::JSONValue*>(
- zObject->zBiomNoiseConfig()->getThis()));
- DimensionGeneratorFactory::toJson(zObject, zResult);
- }
- JSONObjectValidationBuilder*
- BiomedCavedDimensionGeneratorFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return DimensionGeneratorFactory::addToValidator(
- builder->withRequiredArray("bioms")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<BiomGenerator>())
- ->finishArray()
- ->withRequiredAttribute("biomNoise", JNoise::getValidator(false)));
- }
- Framework::Text BiomedCavedDimensionGeneratorFactory::getTypeToken() const
- {
- return "cavedBioms";
- }
|