00001 #include "../borqueror.h"
00002
00003 Galaxy::Galaxy( void )
00004 : mutex( 0 )
00005 {
00006 mutex = SDL_CreateMutex();
00007 MessageServer::Instance().RegisterMessageClient( this , Message::USER );
00008 }
00009
00010 Galaxy::~Galaxy( void )
00011 {
00012 Reset();
00013
00014 if( mutex )
00015 SDL_DestroyMutex( mutex );
00016 mutex = 0;
00017
00018 }
00019
00020 void Galaxy::Reset( void )
00021 {
00022 SDL_mutexP( mutex );
00023 filename = "";
00024
00025 for( vector<StarSystem *>::iterator it = begin() ; it != end() ; ++it )
00026 delete (*it);
00027 clear();
00028 Colony::ResetAllColonies();
00029 Planet::allPlanets.clear();
00030 Parameters::species.clear();
00031 SDL_mutexV( mutex );
00032 }
00033
00034 long Galaxy::Run( void * )
00035 {
00036 SDL_mutexP( (SDL_mutex *)arg );
00037
00038 ofstream of( filename.c_str() , ios::out );
00039 if( of.good() && !of.fail() )
00040 {
00041 of << "//tFormat of the file as followed:\n"
00042 << "//\t( \"<StarName>\" \"<StarName Description>\" <coord x> <coord-y> <Type> <size> <nrPlanets>\n"
00043 << "//\t\tfor each planet:\n"
00044 << "//\t\t( <planetNo> <size> <type> <ore> <energy> <soil> <fauna> <flora> <lithium> <deuterium> <humidity> <surface-temperature> <gravity> <atmPressure> <sunEnergy> <water> <humidityTerraformedDelta> <surfaceTemperatureTerraformedDelta> <pressureTerraformedDelta> )\n"
00045 << "//\t)~\n";
00046 for_each( begin() , end() , bind2nd( mem_fun( &StarSystem::Save ) , &of ) );
00047 }
00048
00049 return SDL_mutexV( (SDL_mutex *)arg );
00050 }
00051
00052 void Galaxy::Load( const string fname )
00053 {
00054 SDL_mutexP( mutex );
00055
00056 ifstream inf( fname.c_str() , ios::in );
00057 if( inf.good() && !inf.fail() )
00058 {
00059 Cfg::Handler &h = Cfg::GetCfg( "@Generator" );
00060
00061 const int gw = h.GetCfgInt( "Galaxy-coord" , "playW" );
00062 const int gh = h.GetCfgInt( "Galaxy-coord" , "playH" );
00063 char buffer[4096];
00064 inf.getline( buffer , sizeof( buffer ) , '~' );
00065 inf.getline( buffer , sizeof( buffer ) , '~' );
00066 while( !inf.eof() )
00067 {
00068 icstring ics = buffer;
00069 ics = Rsrc::GetList( ics );
00070 const string StarName = Rsrc::GetStr( ics );
00071 const string StraNameDescr = Rsrc::GetStr( ics );
00072 XPoint p;
00073 p.x = Rsrc::GetInt( ics );
00074 p.y = Rsrc::GetInt( ics );
00075 const int StarType = Rsrc::GetInt( ics );
00076 const int StarSize = Rsrc::GetInt( ics );
00077 const int nrPlanet = Rsrc::GetInt( ics );
00078
00079 StarSystem *ss = new StarSystem( StarName , StraNameDescr , Parameters::GetDenomination( p , StarType , StarSize , nrPlanet , gw , gh ) , StarType , StarSize );
00080 static_cast<XPoint &>( *ss ) = p;
00081 while( !ics.empty() )
00082 {
00083 icstring str = Rsrc::GetList( ics );
00084 const int PlanetNo = Rsrc::GetInt( str );
00085 const int Size = Rsrc::GetInt( str );
00086 const int Type = Rsrc::GetInt( str );
00087 Planet *pl = new Planet( ss , ss->GetPlanetName( PlanetNo ) , Type , Size , PlanetNo );
00088 pl->ore = Rsrc::GetInt( str );
00089 pl->energy = Rsrc::GetInt( str );
00090 pl->soil = Rsrc::GetInt( str );
00091 pl->fauna = Rsrc::GetInt( str );
00092 pl->flora = Rsrc::GetInt( str );
00093 pl->lithium = Rsrc::GetInt( str );
00094 pl->deuterium= Rsrc::GetInt( str );
00095 pl->humidity = Rsrc::GetDbl( str );
00096 pl->surfaceTemperature = Rsrc::GetDbl( str );
00097 pl->gravity = Rsrc::GetDbl( str );
00098 pl->atmPressure = Rsrc::GetDbl( str );
00099 pl->sunEnergy = Rsrc::GetDbl( str );
00100 pl->water = Rsrc::GetDbl( str );
00101 pl->humidityTerraformedDelta = Rsrc::GetDbl( str );
00102 pl->surfaceTemperatureTerraformedDelta = Rsrc::GetDbl( str );
00103 pl->pressureTerraformedDelta= Rsrc::GetDbl( str );
00104 ss->push_back( pl );
00105 }
00106 GetGalaxy().push_back( ss );
00107
00108 inf.getline( buffer , sizeof( buffer ) , '~' );
00109 }
00110 }
00111 SDL_mutexV( mutex );
00112 }
00113
00114 const bool Galaxy::evMessage( const UserMessage &msg )
00115 {
00116 Cfg::Handler &h = Cfg::GetCfg();
00117
00118 if( msg.SubMessage() == SAVE_GALAXY )
00119 {
00120 char temp[10];
00121 filename = h.GetCfgString( "SavedGames" , itoa( (int)msg.Data() , temp , 10 ) ) + ".glx";
00122
00123 Start( mutex );
00124 return true;
00125 }
00126 else if( msg.SubMessage() == LOAD_GALAXY )
00127 {
00128 Reset();
00129 char temp[10];
00130 filename = h.GetCfgString( "SavedGames" , itoa( (int)msg.Data() , temp , 10 ) );
00131 Load( filename + ".glx" );
00132 return true;
00133 }
00134 return false;
00135 }