123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #include "WorldGenerator.h"
- #include <Betriebssystem.h>
- #include <functional>
- #include "Game.h"
- #include "NoiseInterpolator.h"
- #include "StaticRegistry.h"
- using namespace Framework;
- WorldGenerator::WorldGenerator(int seed)
- : Thread(),
- exit(0),
- seed(seed)
- {
- setName("World Generator");
- for (int i = 0; i < StaticRegistry<DimensionGenerator>::INSTANCE.getCount();
- i++)
- StaticRegistry<DimensionGenerator>::INSTANCE.zElement(i)->initialize(
- seed);
- start();
- }
- WorldGenerator::~WorldGenerator() {}
- void WorldGenerator::thread()
- {
- while (!exit)
- {
- cs.lock();
- Area next;
- bool hasNext = 0;
- if (requestQueue.getEintragAnzahl() > 0)
- {
- next = requestQueue.get(0);
- requestQueue.remove(0);
- hasNext = 1;
- }
- cs.unlock();
- if (!hasNext)
- {
- Sleep(1000);
- continue;
- }
- Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
- Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
- int xDir = start.x > end.x ? -1 : 1;
- int yDir = start.y > end.y ? -1 : 1;
- for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x;
- x += CHUNK_SIZE * xDir)
- {
- for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y;
- y += CHUNK_SIZE * yDir)
- {
- if (!Game::INSTANCE->doesChunkExist(x, y, next.dimensionId))
- {
- Chunk* generatedChunk
- = StaticRegistry<DimensionGenerator>::INSTANCE
- .zElement(next.dimensionId)
- ->generateChunk(x, y);
- ZeitMesser zm;
- zm.messungStart();
- generatedChunk->initializeLightning();
- zm.messungEnde();
- std::cout << "light calculation: " << zm.getSekunden()
- << "\n";
- zm.messungStart();
- generatedChunk->removeUnusedBlocks();
- zm.messungEnde();
- std::cout << "unused block removal: " << zm.getSekunden()
- << "\n";
- zm.messungStart();
- Dimension* dim
- = Game::INSTANCE->zDimension(next.dimensionId);
- if (!dim)
- {
- dim = new Dimension(next.dimensionId);
- Game::INSTANCE->addDimension(dim);
- }
- dim->setChunk(generatedChunk, Punkt(x, y));
- zm.messungEnde();
- std::cout << "adding chunk to map: " << zm.getSekunden()
- << "\n";
- }
- }
- }
- }
- std::cout << "World Generator thread exited\n";
- }
- void WorldGenerator::requestGeneration(Area request)
- {
- cs.lock();
- requestQueue.add(request);
- cs.unlock();
- }
- void WorldGenerator::exitAndWait()
- {
- exit = 1;
- warteAufThread(10000);
- ende();
- }
- Framework::Either<Block*, int> WorldGenerator::generateSingleBlock(
- Framework::Vec3<int> location, int dimensionId)
- {
- return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)
- ->generateBlock(location);
- }
- bool WorldGenerator::spawnStructure(Framework::Vec3<int> location,
- int dimensionId,
- std::function<bool(GenerationTemplate* tmpl)> filter)
- {
- return StaticRegistry<DimensionGenerator>::INSTANCE.zElement(dimensionId)
- ->spawnStructure(location, filter);
- }
|