#include #include #include "WorldLoader.h" #include "Game.h" #include "AddChunkUpdate.h" using namespace Framework; WorldLoader::WorldLoader( Game *zGame ) : Thread(), zGame( zGame ), exit( 0 ) { start(); } WorldLoader::~WorldLoader() {} void WorldLoader::thread() { while( !exit ) { cs.Enter(); Area next; bool hasNext = false; if( requestQueue.getEintragAnzahl() > 0 ) { next = requestQueue.get( 0 ); requestQueue.remove( 0 ); } cs.Leave(); if( !hasNext ) { sleep( 1 ); continue; } Punkt start = zGame->getChunkCenter( next.startX, next.startY ); Punkt end = zGame->getChunkCenter( next.endX, next.endY ); int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 ); int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 ); for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir ) { for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir ) { if( !zGame->doesChunkExist( x, y, next.dimensionId ) ) { Datei *file = new Datei(); Text filePath = zGame->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 ), zGame, next.dimensionId, file ); zGame->requestWorldUpdate( new AddChunkUpdate( chunk ) ); } file->close(); file->release(); } } } } } void WorldLoader::requestGeneration( Area request ) { cs.Enter(); requestQueue.add( request ); cs.Leave(); } void WorldLoader::exitAndWait() { exit = 1; warteAufThread( 10000 ); ende(); }