#include #include #include "WorldLoader.h" #include "Game.h" #include "AddChunkUpdate.h" using namespace Framework; WorldLoader::WorldLoader( Game *zGame ) : Thread(), zGame( zGame ), exit( 0 ) { oader i Datei d; d.setDatei( zGame->getWorldDirectory() + "/dim" ); RCArray *names = d.getDateiListe(); if( names ) { for( auto name = names->getIterator(); name; name++ ) { Datei entities; entities.setDatei( zGame->getWorldDirectory() + "/dim/" + Text( name->getText() ) + "/entities" ); if( entities.open( Datei::Style::lesen ) ) { Dimension *dim = new Dimension( *name.val() ); while( !entities.istEnde() ) { int type = 0; entities.lese( (char *)&type, 4 ); dim->addEntity( StaticRegistry::INSTANCE.zElement( type )->loadEntity( zGame, &entities ) ); } zGame->addDimension( dim ); } } names->release(); } 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( 1000 ); 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::requestLoading( Area request ) { cs.Enter(); requestQueue.add( request ); cs.Leave(); } void WorldLoader::exitAndWait() { exit = 1; warteAufThread( 10000 ); ende(); } bool WorldLoader::existsChunk( int x, int y, int dimension ) const { Text filePath = zGame->getWorldDirectory() + "/dim/" + dimension + "/"; filePath.appendHex( x ); filePath += "_"; filePath.appendHex( y ); filePath += ".chunk"; return DateiExistiert( filePath ); }