123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "WorldLoader.h"
- #include <Punkt.h>
- #include <Text.h>
- #include "Game.h"
- using namespace Framework;
- WorldLoader::WorldLoader()
- : Thread(),
- exit(0)
- {
- setName("World Loader");
- Datei d;
- d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim");
- RCArray<Text>* names = d.getDateiListe();
- if (names)
- {
- for (Text* name : *names)
- {
- Datei entities;
- entities.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/"
- + Text(name->getText()) + "/entities");
- if (entities.open(Datei::Style::lesen))
- {
- Dimension* dim = new Dimension((int)*name);
- while (!entities.istEnde())
- {
- int type = 0;
- entities.lese((char*)&type, 4);
- dim->addEntity(
- StaticRegistry<EntityType>::INSTANCE.zElement(type)
- ->loadEntity(&entities));
- }
- Game::INSTANCE->addDimension(dim);
- }
- }
- names->release();
- }
- start();
- }
- WorldLoader::~WorldLoader() {}
- void WorldLoader::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)
- {
- Dimension* zDim = Game::INSTANCE->zDimension(next.dimensionId);
- bool revived = 0;
- if (!zDim->zChunk(Punkt(x, y)) && zDim->hasChunck(x, y))
- {
- revived = zDim->reviveChunk(x, y);
- }
- if (!revived
- && !Game::INSTANCE->isChunkLoaded(x, y, next.dimensionId))
- {
- Datei* file = new Datei();
- Text filePath = Game::INSTANCE->getWorldDirectory()
- + "/dim/" + next.dimensionId + "/";
- filePath.appendHex(x);
- filePath += "_";
- filePath.appendHex(y);
- filePath += ".chunk";
- file->setDatei(filePath);
- if (file->open(Datei::Style::lesen))
- {
- Chunk* chunk = new Chunk(
- Framework::Punkt(x, y), next.dimensionId, file);
- Dimension* dim
- = Game::INSTANCE->zDimension(next.dimensionId);
- if (!dim)
- {
- dim = new Dimension(next.dimensionId);
- Game::INSTANCE->addDimension(dim);
- }
- dim->setChunk(chunk, Punkt(x, y));
- }
- file->close();
- file->release();
- }
- }
- }
- }
- std::cout << "World Loader thread exited\n";
- }
- void WorldLoader::requestLoading(Area request)
- {
- cs.lock();
- requestQueue.add(request);
- cs.unlock();
- }
- void WorldLoader::exitAndWait()
- {
- exit = 1;
- warteAufThread(10000);
- ende();
- }
- bool WorldLoader::existsChunk(int x, int y, int dimension) const
- {
- Text filePath
- = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimension + "/";
- filePath.appendHex(x);
- filePath += "_";
- filePath.appendHex(y);
- filePath += ".chunk";
- return DateiExistiert(filePath);
- }
|