Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

parameters.cpp

Go to the documentation of this file.
00001 #include "borqueror.h"
00002 
00004 
00005 Parameters::Parameters( Window *aParent )
00006 : BqBase( eParameters , aParent ) , res( Cfg::GetCfg().GetFilename() )
00007 {
00008         double  t = 0.46875 * time( 0 );
00009         while( t > 1.0 )
00010                 t /= 10.0;
00011    Random::Set( t );
00012 
00013    LoadStarsName();
00014 }
00015 
00016 Parameters::~Parameters( void )
00017 {
00018 }
00019 
00020 int Parameters::GenPosition( void )
00021 {
00022         xy4Shape.clear();
00023 
00027         const int       id = Graf::LoadImg( "Shape" , img2Shape[parShape] );
00028         SDL_Surface *sfc = Graf::Img( id );
00029 
00030         for( int i = 0 ; i < sfc->w ; i++ )
00031                 for( int j = 0 ; j < sfc->h ; j++ )
00032                 {
00033                         XPoint  p( i , j );
00034                         if( ReadPoint( sfc , p ) != RGBColor( 255 , 255 , 255 ) )
00035                                 xy4Shape.push_back( make_pair( p , true ) );
00036                 }
00037 
00038         return (int)xy4Shape.size();
00039 }
00040 
00041 int     cntplanet[20] = { 0 } , cntsun[10] = { 0 };
00042 
00043 void Parameters::CreateGalaxy( void )
00044 {
00045         int     i;
00046         double  avgAge = 0 , toPlanets = 0;
00047         int     cnt = 0;
00048         MainGame::galaxy->Reset();
00049         xstNames.clear();
00050 #ifdef WIN32
00051 SET_TRACE_MEASURE( tm1 , "Creating Galaxy" );
00052 #endif
00053         Cfg::Handler    &h = Cfg::GetCfg( "@Generator" );
00054         //      Star generator is function f the galaxy age
00055         ExprParser      e;
00056         SumRandom       *sr = e.Apply( e.Eval( h.GetCfgString( "StarGen" , img2Age[parAge] ) ) );
00057         const int nrStars = h.GetCfgInt( "GalaxySize" , img2Size[parSize] , 0 );
00058         const int       n = 100 * nrStars;
00059         const int       gx = h.GetCfgInt( "Galaxy-coord" , "playW" );
00060         const int       gy = h.GetCfgInt( "Galaxy-coord" , "playH" );
00061 
00062         Uniform u1 , u2;
00063 
00064         ProbaHolder     urnd( new Uniform );
00065 
00066         const int gp = GenPosition();
00068         ProbaHolder     starAtxy( n , u1 , gp );
00072         SDL_Surface *sfc = Graf::Img( Graf::LoadImg( "Shape" , img2Shape[parShape] ) );
00073         const double    squareSize = sqrt( (sfc->h * sfc->w) / (double)starName.size() );
00074         ratio = max( gx / sfc->w , gy / sfc->h );
00075         Cfg::GetCfg().SetCfgInt( "DefaultParameters" , "Ratio" , (int)ratio );
00076         ProbaHolder     offsetAtxy( n , u2 , (int)ratio );
00077 
00079         ProbaHolder     starGen( nrStars , *sr , starInfo.size() );
00080 
00082         Free( eProgress );
00083         Form    *f = Load( "Progress" , eProgress );
00084         f->InitDialog( this );
00085         f->GetArsObject( 1 )->SetWndText( "Generating the galaxy" );
00086         XProgress       *progress = dynamic_cast<XProgress *>( f->GetArsObject( 2 ) );
00087         progress->SetProgress( 0 );
00088         progress->SetLimits( 0 , nrStars );
00089         f->Draw();
00090 #ifdef WIN32
00091 TRACE_MEASURE( tm1 , "Initialization" )
00092 #endif
00093         XPoint  center( gx / 2 , gy / 2 );
00094         double oneSection = center.dist() / (double)starInfo.back()->ageSection;
00095         int     atxy = 0 , offset = 0;
00096         i = 0;
00097 
00098         while( atxy < starAtxy.n && GetGalaxy().size() < (unsigned)nrStars )
00099         {
00100 #ifdef WIN32
00101 TRACE_MEASURE( tm1 , "finding a new Star" );
00102 #endif
00103                 while( atxy < starAtxy.n && !xy4Shape[starAtxy[atxy]].second )
00104                         atxy += 1;
00105                 XPoint  p1 , p2;
00106                 XPoint  p = xy4Shape[starAtxy[atxy]].first;
00107                 p1 = p;
00108                 p2 = p;
00109                 p2 *= ratio;
00110                 p2.x += offsetAtxy[offset++];
00111                 p2.y += offsetAtxy[offset++];
00112 
00113                 if( img2Shape[parShape] != "Irregular" )
00114                 {
00115                         XPoint  p3 = p2 - center;
00116                         int     atdist = (int)(p3.dist() / oneSection);
00117                         while( abs( starInfo[starGen[i]]->ageSection - atdist ) > 3 )
00118                                 if( ++i >= nrStars )
00119                                         i = 0;
00120                 }
00121                 StarInfo        *si = starInfo[starGen[i]];     
00122                 const int StarType = si->sid;
00123                 const int StarSize = si->ssize;
00124                 avgAge = avgAge + si->age;
00125                 if( ++i >= nrStars )
00126                         i = 0;
00127 #ifdef WIN32
00128 TRACE_MEASURE( tm1 , "Star Found... Compute the # planets" );
00129 #endif
00130                 const int nrPlanets = (int)si->nrPlanets->Next( 1 + si->size() );
00131 
00132                 cntsun[StarType] += 1;
00133 
00134                 string  starName , starDescr;
00135                 GetStarName( squareSize , p1 , starName , starDescr );
00136                 StarSystem      *ss = new StarSystem( starName , starDescr , GetDenomination( p2 , StarType , StarSize , nrPlanets , gx , gy ) , StarType , StarSize );
00137                 static_cast<XPoint &>( *ss ) = p2;
00138                 vector<PlanetInfo *>    pl1 = planetInfo;
00139                 const EcoZone   *ez = GetEcoZone( si->temperature );
00140                 
00141                 toPlanets += nrPlanets;
00142 #ifdef WIN32
00143 TRACE_MEASURE( tm1 , "Star system created" );
00144 #endif          
00145                 for( int pl = 0 ; pl < nrPlanets ; pl++ )
00146                 {
00147                         for( int atEcoZone = 0 ; atEcoZone < (int)ez->size() ; atEcoZone++ )
00148                                 if( pl < (*ez)[atEcoZone] )
00149                                         break;
00150 
00151                         vector<PlanetInfo *>    pl2 = pl1;
00152                         int     y = RestrictToZone( atEcoZone , pl2 );
00153                         int     PlId = -1;
00154                         const int       PlSize = (*si)[pl]->Next();
00155                         y = RestrictWithSize( PlSize , pl2 );
00156 
00157                         if( (y = RestrictWithAge( si->age , pl2 )) != 0 )
00158                                 PlId = pl2[(int)urnd.Next( pl2.size() )]->id;
00159                         else
00160                         {
00161 //                              tron << "Star " << GetGalaxy().size() << " sun type " << si->sid << "  - At planet " << pl << ": Zone : " << atEcoZone << " - size " << PlSize << endl;
00162                                 cnt += 1;
00163                         }
00164 
00165                         if( PlId > -1 )
00166                                 cntplanet[PlId] += 1;
00167                         ss->push_back( new Planet( ss , ss->GetPlanetName( pl ) , PlId , PlSize , pl ) );
00168                 }
00169 #ifdef WIN32
00170 TRACE_MEASURE( tm1 , "Planets created" );
00171 #endif
00172                 GetGalaxy().push_back( ss );
00173 
00174                 xy4Shape[starAtxy[atxy]].second = false;
00175 
00176                 progress->SetProgress( GetGalaxy().size() , true );
00177 
00178                 f->DoDialog( 0 );
00179         }
00180         tron << img2Age[parAge] << " Average galaxy age = " << avgAge / GetGalaxy().size() << endl;
00181         tron << "Number of gap in ss: " << cnt << endl;
00182         double  sm = 0.0;
00183         for( i = 0 ; i < 20 ; sm += cntplanet[i++] )    ;
00184         for( i = 0 ; i < 20 ; i++ )
00185                 tron << "Planet " << i << " : " << cntplanet[i] << " - Avg of " << cntplanet[i] * 100.0 / sm  << "%\n";
00186 
00187         for( i = 0 ; i < 10 ; i++ )
00188                 tron << "sun type " << i << " : " << cntsun[i] << " - Avg of " << cntsun[i] * 100.0 / GetGalaxy().size() << "\n";
00189 
00190         tron.flush();
00191         delete sr;
00192 
00194 /*
00195         tron << "sun Type\tSun Size\tplanet\tzone\tsize\tclass\tageMin\n";
00196         for( vector<StarInfo *>::iterator a = starInfo.begin() ; a != starInfo.end() ; ++a )
00197         {
00198 //              tron << "Sun type " << (*a)->name << " - size " << (*a)->ssize << endl;
00199                 vector<PlanetInfo *>    pl1 = planetInfo;
00200                 const EcoZone   *ez = GetEcoZone( (*a)->temperature );
00201                 for( int pl = 0 ; pl < 9 ; pl++ )
00202                 {
00203                         for( int atEcoZone = 0 ; atEcoZone < (int)ez->size() ; atEcoZone++ )
00204                                 if( pl < (*ez)[atEcoZone] )
00205                                         break;
00206 //                      tron << "\tplanet " << pl << " - Zone : " << atEcoZone << endl;
00207 
00208                         vector<PlanetInfo *>    pl2 = pl1;
00209                         int     y = RestrictToZone( atEcoZone , pl2 );
00210                         for( int s = 0 ; s < 6 ; s++ )
00211                         {
00212 //                              tron << "\t\tsize " << s << endl;
00213                                 vector<PlanetInfo *>    pl3 = pl2;
00214                                 y = RestrictWithSize( s , pl3 );
00215                                 y = RestrictWithAge( (*a)->age , pl3 );
00216                                 if( !pl3.empty() )
00217                                 {
00218                                         for( vector<PlanetInfo *>::iterator b = pl3.begin() ; b != pl3.end() ; ++b )
00219                                                 tron << (*a)->name << "\t" << (*a)->ssize << "\t" << pl << "\t" << atEcoZone << "\t" << s << "\t" << (*b)->type << "\t" << (*b)->ageMin << endl;
00220 //                                              tron << "\t\t\tplanet: " << (*b)->id << " class " << (*b)->type << endl;
00221                                 }
00222                                 else
00223                                         tron << "*** Sun type " << (*a)->sid << "  - At planet " << pl << ": Zone : " << atEcoZone << " - size " << s << endl;
00224                         }
00225                 }
00226         }
00227 */
00228 
00229 //      SetupSpecies();
00230 
00231         icstring        playSpecies = Cfg::GetCfg( "@ai" ).GetCfgString( "Player" , "id" ).c_str();
00232         {
00233                 for( vector<SpeciesInfo *>::iterator it = allSpecies.begin() ; it != allSpecies.end() ; ++it )
00234                         if( (*it)->name == playSpecies )
00235                         {
00236                                 find_if( Planet::allPlanets.begin() , Planet::allPlanets.end() , bind2nd( mem_fun( &Planet::IsCompatible ) , (*it) ) );
00237                                 if( (*it)->hwPlanet )
00238                                 {
00239                                         tron << (*it)->name.c_str() << " with residue of " << (*it)->residue << "\n";
00240                                         Planet  *pl = (*it)->hwPlanet;
00241 
00243                                         (*it)->humidity = (int)pl->GetTotalHumidity();
00244                                         (*it)->atmPressure = (int)pl->GetTotalPressure();
00245                                         (*it)->surfaceTemperature = (int)pl->GetTotalSurfaceTemperature();
00246 
00247                                         species.insert( make_pair( (*it)->name , (*it) ) );
00248                                         pl->Colonize( (*it)->name , true );
00249                                 }
00250                                 break;
00251                         }
00252         }
00253         random_shuffle( allSpecies.begin() , allSpecies.end() );
00254         f->GetArsObject( 1 )->SetWndText( "Generating and locating the species..." );
00255         progress->SetProgress( 0 );
00256         progress->SetLimits( 0 , nrAliens );
00257         f->Draw();
00258         int     j = 0;
00259         {
00260 
00261                 for( vector<SpeciesInfo *>::iterator it = allSpecies.begin() ; it != allSpecies.end() && j < (int)nrAliens ; ++it )
00262                 {
00263                         if( (*it)->name != playSpecies )
00264                         {
00265                                 find_if( Planet::allPlanets.begin() , Planet::allPlanets.end() , bind2nd( mem_fun( &Planet::IsCompatible ) , (*it) ) );
00266                                 if( (*it)->hwPlanet )
00267                                 {
00268                                         tron << (*it)->name.c_str() << " with residue of " << (*it)->residue << "\n";
00269                                         
00270                                         Planet  *pl = (*it)->hwPlanet;
00272                                         (*it)->humidity = (int)pl->GetTotalHumidity();
00273                                         (*it)->atmPressure = (int)pl->GetTotalPressure();
00274                                         (*it)->surfaceTemperature = (int)pl->GetTotalSurfaceTemperature();
00275 
00276                                         species.insert( make_pair( (*it)->name , (*it) ) );
00277                                         pl->Colonize( (*it)->name , true );
00278                                         progress->SetProgress( ++j , true );
00279                                 }
00280                         }
00281                 }
00282         }
00283         f->ShutdownDialog( this );
00284 }
00285 
00286 const bool Parameters::SetupWindow( const Message &aMsg )
00287 {
00288         BqBase::SetupWindow( aMsg );
00289 
00290         Rsrc    res( "@Images" );
00291 
00292         img2Size.push_back( "Tiny" );
00293         img2Size.push_back( "Small" );
00294         img2Size.push_back( "Medium" );
00295         img2Size.push_back( "Large" );
00296         img2Size.push_back( "Big" );
00297 
00298         img2Age.push_back( "Young" );
00299         img2Age.push_back( "Recent" );
00300         img2Age.push_back( "Mature" );
00301         img2Age.push_back( "Aged" );
00302         img2Age.push_back( "Old" );
00303 
00304         img2Shape.push_back( "Elliptical" );
00305         img2Shape.push_back( "Ring" );
00306         img2Shape.push_back( "Irregular" );
00307         img2Shape.push_back( "Spiral" );
00308         img2Shape.push_back( "Weird" );
00309         img2Shape.push_back( "Pleiad" );
00310 
00311         MessageServer::Instance().RegisterMessageClient( this , Message::CTRL_VALUECHANGE );
00312         MessageServer::Instance().RegisterMessageClient( this , Message::CTRL_VALUECHANGING );
00313 
00314         return true;
00315 }
00316 
00317 void Parameters::InitDialog( void )
00318 {
00319         BqBase::InitDialog();
00320 
00321         {
00322                 Cfg::Handler    &h = Cfg::GetCfg();
00323                 parShape = h.GetCfgInt( "DefaultParameters" , "Shape" , 2 );
00324                 parAge = h.GetCfgInt( "DefaultParameters" , "Age" , 1 );
00325                 parSize = h.GetCfgInt( "DefaultParameters" , "Size" , 2 );
00326                 nrAliens = h.GetCfgInt( "DefaultParameters" , "nrAliens" , 10 );
00327         }
00328 
00329         Cfg::Handler    &h = Cfg::GetCfg( "@Images" );
00330 
00331         if( f )
00332         {
00333                 dynamic_cast<XScrollBar *>( f->GetArsObject( 42 ) )->SetPosition( 5 );
00334                 f->GetArsObject( 30 )->SetWndText( Cfg::GetCfg().GetCfgString( "DefaultParameters" , "PlayerName" , "PTY-1" ) );
00335                 UpdateParameters();
00336         }
00337 }
00338 
00339 void Parameters::UpdateParameters( void )
00340 {
00341         f->GetArsObject( 10 )->EnableWindow( parSize > 0 );
00342         dynamic_cast<XImage *>( f->GetArsObject( 11 ) )->ChangeImage( "" , img2Size[parSize] );
00343         f->GetArsObject( 12 )->SetWndText( img2Size[parSize] );
00344         f->GetArsObject( 13 )->EnableWindow( (parSize + 1) < img2Size.size() );
00345 
00346         f->GetArsObject( 14 )->EnableWindow( parAge > 0 );
00347         dynamic_cast<XImage *>( f->GetArsObject( 15 ) )->ChangeImage( "" , img2Age[parAge] );
00348         f->GetArsObject( 16 )->SetWndText( img2Age[parAge] );
00349         f->GetArsObject( 17 )->EnableWindow( (parAge + 1) < img2Age.size() );
00350 
00351         f->GetArsObject( 18 )->EnableWindow( parShape > 0 );
00352         dynamic_cast<XImage *>( f->GetArsObject( 19 ) )->ChangeImage( "" , img2Shape[parShape] );
00353         f->GetArsObject( 20 )->SetWndText( img2Shape[parShape] );
00354         f->GetArsObject( 21 )->EnableWindow( (parShape + 1) < img2Shape.size() );
00355 
00356         f->GetArsObject( 40 )->SetWndText( nrAliens );
00357         dynamic_cast<XScrollBar *>( f->GetArsObject( 42 ) )->SetPosition( nrAliens - 5 );
00358 }
00359 
00360 const bool Parameters::MainLoop( void )
00361 {
00362         SetupDialog( "Parameters" );
00363         ans = 0;
00364 
00365         while( isDisplayed )
00366                 f->DoDialog();
00367 
00368         if( ans == 2 )
00369         {
00371                 Cfg::Handler    &h = Cfg::GetCfg();
00372                 h.SetCfgInt( "DefaultParameters" , "Size" , parSize );
00373                 h.SetCfgInt( "DefaultParameters" , "Age" , parAge );
00374                 h.SetCfgInt( "DefaultParameters" , "Shape" , parShape );
00375                 h.SetCfgString( "DefaultParameters" , "ShapeName" , img2Shape[parShape] );
00376                 h.SetCfgInt( "DefaultParameters" , "nrAliens" , nrAliens );
00377                 h.SetCfgString( "DefaultParameters" , "PlayerName" , f->GetArsObject( 30 )->GetWindowText() );
00378 
00380                 CreateGalaxy();
00381 
00383                 Cfg::Handler as( h.GetCfgString( "Paths" , "Saves" ) + "autosave.cfg" );
00384                 h.SetCfgString( "SavedGames" , "0" , h.GetCfgString( "Paths" , "Saves" ) + "autosave" );
00385                 h.Flush();
00386 
00387                 SDL_Surface *sfc = Graf::Img( Graf::LoadImg( "Shape" , img2Shape[parShape] ) );
00388                 as.SetCfgInt( "Galaxy" , "Shape" , parShape );
00389                 as.SetCfgInt( "Galaxy" , "Age" , parAge );
00390                 as.SetCfgInt( "Galaxy" , "Size" , parSize );
00391 
00392                 as.SetCfgString( "Player" , "Name" , f->GetArsObject( 30 )->GetWindowText() );
00393                 char    temp[60];
00394                 time_t  ltime;
00395                 time( &ltime );
00396                 struct tm *today = localtime( &ltime );
00397                 strftime( temp , sizeof( temp ) , "%d/%m %H:%M" , today );
00398                 as.SetCfgString( "Game" , "DateCreated" , temp );
00399                 as.SetCfgString( "Game" , "Name" , "Auto Save" );
00400                 as.Flush();
00401 
00402                 f->ShutdownDialog( this );
00403 
00404                 ArsPostMessage( UserMessage( Message::USER , 0 , SAVE_GALAXY , 0 ) );
00405                 ArsPostMessage( UserMessage( Message::USER , 0 , SAVE_SPECIES , 0 ) );
00406 
00408                 ArsPostMessage( UserMessage( Message::USER , GetParent() , 4 ) );
00409         }
00410         else
00411                 f->ShutdownDialog( this );
00412 
00413         return true;
00414 }
00415 
00416 const bool Parameters::evMouseLClick( const CtrlMessage &msg )
00417 {
00418         BqBase::evMouseLClick( msg );
00419 
00420         if( isDisplayed )
00421                 if( (ans = ((Window *)msg.source)->GetId()) < 9 )
00422                 {
00423                         isDisplayed= false;     
00424                         return !isDisplayed;
00425                 }
00426                 else
00427                 {
00428                         switch( ((Window *)msg.source)->GetId() )
00429                         {
00430                                 case 10:        parSize -= 1;   break;
00431                                 case 13:        parSize += 1;   break;
00432                                 case 14:        parAge -= 1;    break;
00433                                 case 17:        parAge += 1;    break;
00434                                 case 18:        parShape -= 1;  break;
00435                                 case 21:        parShape += 1;  break;
00436                         }
00437                         UpdateParameters();
00438                 }
00439 
00440         return isDisplayed;
00441 }
00442 
00443 const bool Parameters::evValueChange( const CtrlMessage &msg )
00444 {
00445         bool ans = BqBase::evValueChange( msg );
00446 
00447         if( ((Window *)msg.source)->GetId() == 42 )
00448         {
00449                 nrAliens = 5 + msg.value;
00450                 f->GetArsObject( 40 )->SetWndText( nrAliens );
00451                 ans = true;
00452         }
00453         return ans;
00454 }
00455 
00456 const bool Parameters::evValueChanging( const CtrlMessage &msg )
00457 {
00458         bool ans = BqBase::evValueChanging( msg );
00459 
00460         if( ((Window *)msg.source)->GetId() == 42 )
00461         {
00462                 nrAliens = 5 + msg.value;
00463                 f->GetArsObject( 40 )->SetWndText( nrAliens );
00464                 ans = true;
00465         }
00466         return ans;
00467 }
00468 
00470 vector<string> Parameters::starName;
00471 
00472 void Parameters::LoadStarsName( void )
00473 {
00474         Rsrc    res( "@GenStars" );
00475 
00476         icstring        s = res.FindCombination( icstring( "PlanetNameDB" ) );
00477         while( !s.empty() )
00478                 starName.push_back( res.GetList( s ).c_str() );
00479 }
00480 
00481 void Parameters::GetStarName( const double sqSize , const XPoint &p , string &sname , string &StarDescr )
00482 {
00483         const string GrLetters[] =
00484         {
00485                 "Alpha" , "Beta" ,"Gamma" ,"Delta" ,
00486                 "Epsilon" ,"Zeta" ,"Eta" ,"Theta" ,"Iota" ,
00487                 "Kappa" ,"Lambda" ,"Mu" ,"Nu" ,"Xi" ,"Omicron" ,
00488                 "Phi" , "Omega" , "Lambda" , "Kala" , "Malaka" ,
00489                 "Manfou" , "Enaimar" , "Pabo" , "Principal" , "Secundary",
00490                 "Forgotten" , "Jalan" , ""
00491         };
00492         int     atsn = (int)(p.x / sqSize + p.y / sqSize);
00493         if( atsn < (int)starName.size() )
00494         {
00495                 icstring        tt = icstring( starName[atsn].c_str() );
00496                 string  t1 = Rsrc::GetStr( tt );
00497                 StarDescr = Rsrc::GetStr( tt );
00498                 sname = t1;
00499                 int     i = 0;
00500                 while( !GrLetters[i].empty() && xstNames.find( sname ) != xstNames.end() )
00501                 {
00502                         sname = t1 + " ";
00503                         sname += GrLetters[i++];
00504                 }
00505                 xstNames.insert( sname );
00506         }
00507 }
00508 
00509 string Parameters::GetDenomination( const XPoint &p2 , const int StarType , const int StarSize , const int nrPlanets , const int gx , const int gy )
00510 {
00511         const string    TypeStr = "ROYGWBD";
00512         const string    SizeStr = " TSMLBG";
00513         char    tmp[10];
00514         string  denomination = TypeStr.substr( StarType , 1 ) + SizeStr.substr( StarSize , 1 ) + "-";
00515         denomination += itoa( nrPlanets , tmp , 10 );
00516         denomination += "-";
00517         denomination += 'A' + (p2.x * 26) / gx;
00518         denomination += 'A' + (p2.y * 26) / gy;
00519 
00520         return denomination;
00521 }
00522 
00524 vector<StarInfo *> Parameters::starInfo;
00525 map<int,int>    Parameters::SIFromSunSize;
00526 const bool cmpByAge( const StarInfo *si0 , const StarInfo *si1 ){       return si0->age < si1->age;     }
00527 
00528 double Parameters::ratio;
00529 
00530 void Parameters::SetupStarInfo( void )
00531 {
00532         ratio = Cfg::GetCfg().GetCfgInt( "DefaultParameters" , "Ratio" , 80 );
00533 
00534         Rsrc    res( "@GenStars" );
00535 
00536         icstring        tmp = res.FindCombination( icstring( "star-description" ) );
00537         icstring        s = strip( tmp );
00538 
00539         while( !s.empty() )
00540         {
00541 
00542                 icstring        aStr = res.GetList( s );
00543                 const string name = Rsrc::GetId( aStr ).c_str();
00544                 const int starId = Rsrc::GetInt( aStr );
00545 
00546                 while( !aStr.empty() )
00547                 {
00548                         icstring        sdsz = res.GetList( aStr );
00549 
00550                         tmp = Rsrc::FindCrdOf( sdsz , "Temperature" );
00551                         int temperature = Rsrc::GetInt( tmp );
00552                         tmp = Rsrc::FindCrdOf( sdsz , "Age" );
00553                         const int age = Rsrc::GetInt( tmp );
00554                         tmp = Rsrc::FindCrdOf( sdsz , "Size" );
00555                         icstring sz = tmp;
00556                         tmp = Rsrc::FindCrdOf( sdsz , "PNrPlanets" );
00557                         const string ProbaExpr = tmp.c_str();
00558 
00559 
00560                         icstring        PPlSize = Rsrc::FindCrdOf( sdsz , "PSize" );
00561 
00562                         tmp = Rsrc::FindCrdOf( sdsz , "class" );
00563                         const string classInf = Rsrc::GetStr( tmp );
00564                         tmp = Rsrc::FindCrdOf( sdsz , "approxAge" );
00565                         const string ageInf = Rsrc::GetStr( tmp );
00566                         tmp = Rsrc::FindCrdOf( sdsz , "approxTemperature" );
00567                         const string temInf = Rsrc::GetStr( tmp );
00568                         tmp = Rsrc::FindCrdOf( sdsz , "Example" );
00569                         const string exInf = Rsrc::GetStr( tmp );
00570                         tmp = Rsrc::FindCrdOf( sdsz , "Description" );
00571                         const string descrInf = Rsrc::GetStr( tmp );
00572 
00573                         while( !sz.empty() )
00574                         {
00575                                 int     size = Rsrc::GetInt( sz );
00576                                 if( size == -1 )
00577                                         break;
00578                                 StarInfo        *si = new StarInfo;
00579 
00580                                 si->temperature = temperature;
00581                                 si->age = age;
00582                                 si->classInf = classInf;
00583                                 si->ageInf = ageInf;
00584                                 si->temInf = temInf;
00585                                 si->exInf = exInf;
00586                                 si->descrInf = descrInf;
00587                                 si->name = name;
00588                                 si->ssize = size;
00589                                 si->sid = starId;
00590                                 si->ageSection = 0;
00591 
00592                                 SIFromSunSize.insert( make_pair( starId << 8 | size , starInfo.size() ) );
00593                                 int     nrPlts = 0;
00594                                 icstring        s = PPlSize;
00595                                 while( !s.empty() )
00596                                 {
00597                                         icstring        aS = res.GetList( s );
00598                                         int     n = Rsrc::GetInt( aS );
00599                                         nrPlts = max( nrPlts , n );
00600                                         si->Setup( aS );
00601                                 }
00602                                 assert( nrPlts != 0 );
00603                                 ExprParser      e;
00604                                 SumRandom *sr = e.Apply( e.Eval( ProbaExpr ) );
00605                                 si->nrPlanets = new ProbaHolder( nrPlts ,  *sr , 1000 );
00606                                 delete sr;
00607                                 starInfo.push_back( si );
00608                                 temperature -= temperature / 10 ;
00609                         }
00610                 }
00611         }
00612 
00613         std::sort( starInfo.begin() , starInfo.end() , cmpByAge );
00614 
00615         for( unsigned i = 1 ; i < starInfo.size() ; i++ )
00616         {
00617                 // SDL_Delay( 50 );
00618                 if( starInfo[i - 1]->age != starInfo[i]->age )
00619                         starInfo[i]->ageSection = starInfo[i - 1]->ageSection + 1;
00620                 else
00621                         starInfo[i]->ageSection = starInfo[i - 1]->ageSection;
00622         }
00623 }
00624 
00625 void Parameters::ResetInfo( void )
00626 {
00627         {
00628                 for( vector<StarInfo *>::iterator it = starInfo.begin() ; it != starInfo.end() ; ++it )
00629                         delete (*it);
00630                 starInfo.clear();
00631         }{
00632                 for( vector<PlanetInfo *>::iterator it = planetInfo.begin() ; it != planetInfo.end() ; ++it )
00633                         delete (*it);
00634                 planetInfo.clear();
00635         }{
00636                 for( vector<SpeciesInfo *>::iterator it = allSpecies.begin() ; it != allSpecies.end() ; ++it )
00637                         delete (*it);
00638                 allSpecies.clear();
00639         }{
00640                 for( vector<EcoZone *>::iterator it = ecoZones.begin() ; it != ecoZones.end() ; ++it )
00641                         delete (*it);
00642                 ecoZones.clear();
00643         }
00644 }
00645 
00647 vector<PlanetInfo *> Parameters::planetInfo;
00648 map<int,PlanetInfo *> Parameters::plIndice;
00649 const bool cmpPlanetByAge( const PlanetInfo *pl0 , const PlanetInfo *pl1 ){     return pl0->ageMin < pl1->ageMin;       }
00650 
00651 void Parameters::SetupPlanetInfo( void )
00652 {
00653         Rsrc    res( "@GenPlanets" );
00654         icstring        tmp = res.FindCombination( icstring( "planet-description" ) );
00655         icstring        s = strip( tmp );
00656 
00657         while( !s.empty() )
00658         {
00659                 icstring        aStr = res.GetList( s );
00660                 PlanetInfo      *pi = new PlanetInfo;
00661                 // SDL_Delay( 50 );
00662                 pi->id = Rsrc::GetInt( aStr );
00663                 tmp = Rsrc::FindCrdOf( aStr , "description" );
00664                 pi->Description = Rsrc::GetStr( tmp );
00665                 tmp = Rsrc::FindCrdOf( aStr , "class" );
00666                 pi->type = Rsrc::GetStr( tmp );
00667                 tmp = Rsrc::FindCrdOf( aStr , "athmosphere" );
00668                 pi->athmosphere = Rsrc::GetStr( tmp );
00669                 tmp = Rsrc::FindCrdOf( aStr , "description" );
00670                 pi->Description = Rsrc::GetStr( tmp );
00671                 tmp = Rsrc::FindCrdOf( aStr , "LifeForm" );
00672                 pi->lifeForm = Rsrc::GetStr( tmp );
00673                 tmp = Rsrc::FindCrdOf( aStr , "surface" );
00674                 pi->surface = Rsrc::GetStr( tmp );
00675                 tmp = Rsrc::FindCrdOf( aStr , "Example" );
00676                 pi->example = Rsrc::GetStr( tmp );
00677 
00678                 // SDL_Delay( 50 );
00679                 icstring        str = Rsrc::FindCrdOf( aStr , "Size" );
00680                 pi->sizeMin = Rsrc::GetInt( str );
00681                 pi->sizeMax = Rsrc::GetInt( str );
00682 
00683                 str = Rsrc::FindCrdOf( aStr  , "Age");
00684                 pi->ageMin = Rsrc::GetInt( str );
00685                 pi->ageMax = Rsrc::GetInt( str );
00686 
00687                 str = Rsrc::FindCrdOf( aStr , "soil" );
00688                 pi->soil[0] = Rsrc::GetInt( str );
00689                 pi->soil[1] = Rsrc::GetInt( str );
00690 
00691                 str = Rsrc::FindCrdOf( aStr , "surfaceTemperature" );
00692                 pi->surfaceTemperature[0] = Rsrc::GetInt( str );
00693                 pi->surfaceTemperature[1] = Rsrc::GetInt( str );
00694 
00695                 str = Rsrc::FindCrdOf( aStr , "atmPressure" );
00696                 pi->atmPressure[0] = Rsrc::GetInt( str );
00697                 pi->atmPressure[1] = Rsrc::GetInt( str );
00698 
00699                 str = Rsrc::FindCrdOf( aStr , "ore" );
00700                 pi->ore[0] = Rsrc::GetInt( str );
00701                 pi->ore[1] = Rsrc::GetInt( str );
00702 
00703                 str = Rsrc::FindCrdOf( aStr , "energy" );
00704                 pi->energy[0] = Rsrc::GetInt( str );
00705                 pi->energy[1] = Rsrc::GetInt( str );
00706 
00707                 str = Rsrc::FindCrdOf( aStr , "Humidity" );
00708                 pi->humidity[0] = Rsrc::GetInt( str );
00709                 pi->humidity[1] = Rsrc::GetInt( str );
00710 
00711                 str = Rsrc::FindCrdOf( aStr , "fauna" );
00712                 pi->fauna[0] = Rsrc::GetInt( str );
00713                 pi->fauna[1] = Rsrc::GetInt( str );
00714 
00715                 str = Rsrc::FindCrdOf( aStr , "flora" );
00716                 pi->flora[0] = Rsrc::GetInt( str );
00717                 pi->flora[1] = Rsrc::GetInt( str );
00718 
00719                 str = Rsrc::FindCrdOf( aStr , "lithium" );
00720                 pi->lithium[0] = Rsrc::GetInt( str );
00721                 pi->lithium[1] = Rsrc::GetInt( str );
00722 
00723                 str = Rsrc::FindCrdOf( aStr , "deuterium" );
00724                 pi->deuterium[0] = Rsrc::GetInt( str );
00725                 pi->deuterium[1] = Rsrc::GetInt( str );
00726 
00727                 str = Rsrc::FindCrdOf( aStr , "water" );
00728                 pi->water[0] = Rsrc::GetInt( str );
00729                 pi->water[1] = Rsrc::GetInt( str );
00730 
00731                 // SDL_Delay( 50 );
00732                 str = Rsrc::FindCrdOf( aStr , "EcoZone" );
00733                 while( !str.empty() )
00734                         pi->push_back( Rsrc::GetInt( str ) );
00735 
00736                 // SDL_Delay( 50 );
00737                 str = Rsrc::FindCrdOf( aStr , "Become" );
00738                 while( !str.empty() )
00739                         pi->become.push_back( Rsrc::GetInt( str ) );
00740 
00741                 // SDL_Delay( 50 );
00742                 str = Rsrc::FindCrdOf( aStr , "PDelta" );
00743                 ExprParser      e;
00744                 pi->delta = new ProbaHolder( e.Apply( e.Eval( str.c_str() ) ) );
00745 
00746                 // SDL_Delay( 50 );
00747                 planetInfo.push_back( pi );
00748                 plIndice.insert( make_pair( pi->id , pi ) );
00749                 // SDL_Delay( 50 );
00750         }
00751         std::sort( planetInfo.begin() , planetInfo.end() , cmpPlanetByAge );
00752 
00753 }
00754 
00755 const int Parameters::RestrictWithAge( const int sunAge , vector<PlanetInfo *> &v )
00756 {
00757         vector<int>     toAdd;
00758         for( vector<PlanetInfo *>::iterator it = v.begin() ; it != v.end() ; )
00759         {
00760                 if( (*it)->ageMax < sunAge )
00761                 {
00762                         for( vector<int>::iterator a = (*it)->become.begin() ; a != (*it)->become.end() ; ++a )
00763                                 toAdd.push_back( *a );
00764                         v.erase( it );
00765                         it = v.begin();
00766                 }
00767                 else
00768                         ++it;
00769         }
00770         for( vector<int>::iterator a = toAdd.begin() ; a != toAdd.end() ; ++a )
00771                 v.push_back( plIndice[*a] );
00772 
00773         std::sort( v.begin() , v.end() , cmpPlanetByAge );
00774         return v.size();
00775 }
00776 
00778 vector<pair<int , int> >        Parameters::planetSize;
00779 map<icstring , vector<double> > Parameters::compensator;
00780 map<int,string> Parameters::sizeStr;
00781 
00782 void Parameters::SetupPlanetSize( void )
00783 {
00784         Rsrc    res( "@GenPlanets" );
00785         // SDL_Delay( 50 );
00786         icstring        tmp = res.FindCombination( icstring( "PLanetSizeRef" ) );
00787         icstring        s = strip( tmp );
00788         // SDL_Delay( 50 );
00789         while( !s.empty() )
00790         {
00791                 icstring        aStr = res.GetList( s );
00792                 int     id = Rsrc::GetInt( aStr );
00793                 string  sStr = Rsrc::GetId( aStr ).c_str();
00794                 pair<int,int>   p;
00795                 // SDL_Delay( 50 );
00796                 p.first = Rsrc::GetInt( aStr );
00797                 p.second = Rsrc::GetInt( aStr );
00798                 planetSize.push_back( p );
00799                 sizeStr.insert( make_pair( id , sStr ) );
00800                 // SDL_Delay( 50 );
00801         }
00802 
00803         tmp = res.FindCombination( icstring( "Compensator" ) );
00804         s = strip( tmp );
00805 
00806         // SDL_Delay( 50 );
00807         while( !s.empty() )
00808         {
00809                 icstring        aStr = res.GetList( s );
00810                 icstring        id = Rsrc::GetId( aStr ).c_str();
00811                 vector<double>  vdbl;
00812                 // SDL_Delay( 50 );
00813                 while( !aStr.empty() )
00814                         vdbl.push_back( Rsrc::GetDbl( aStr ) );
00815                 compensator.insert( make_pair( id , vdbl ) );
00816                 // SDL_Delay( 50 );
00817         }
00818 }
00819 
00820 const int Parameters::RestrictWithSize( const int atSize , vector<PlanetInfo *> &v )
00821 {
00822         const pair<int,int>     &p = planetSize[atSize];
00823         for( vector<PlanetInfo *>::iterator it = v.begin() ; it != v.end() ; )
00824         {
00825                 if( (*it)->sizeMin > p.second || (*it)->sizeMax <= p.first )
00826                 {
00827                         v.erase( it );
00828                         it = v.begin();
00829                 }
00830                 else
00831                         ++it;
00832         }
00833         return v.size();
00834 }
00836 vector<EcoZone *>       Parameters::ecoZones;
00837 void Parameters::SetupEcoZones( void )
00838 {
00839         Rsrc    res( "@GenPlanets" );
00840         // SDL_Delay( 50 );
00841         icstring        tmp = res.FindCombination( icstring( "EcoZone" ) );
00842         icstring        s = strip( tmp );
00843         // SDL_Delay( 50 );
00844 
00845         while( !s.empty() )
00846         {
00847                 icstring        aStr = res.GetList( s );
00848                 EcoZone *ez = new EcoZone;
00849                 ez->sunTemp = Rsrc::GetInt( aStr );
00850                 while( !aStr.empty() )
00851                         ez->push_back( Rsrc::GetInt( aStr ) );
00852                 ecoZones.push_back( ez );
00853                 // SDL_Delay( 50 );
00854         }
00855 }
00856 
00857 EcoZone *Parameters::GetEcoZone( const int sunTemperature )
00858 {
00859         for( vector<EcoZone *>::iterator it = ecoZones.begin() ; it != ecoZones.end() ; ++it )
00860                 if( (*it)->sunTemp > sunTemperature )
00861                         return (*it);
00862         return ecoZones.back();
00863 }
00864 
00865 const int Parameters::RestrictToZone( const int ez , vector<PlanetInfo *> &v )
00866 {
00867         for( vector<PlanetInfo *>::iterator it = v.begin() ; it != v.end() ; )
00868         {
00869                 for( vector<int>::iterator k = (*it)->begin() ; k != (*it)->end() ; ++k )
00870                         if( *k == ez )
00871                                 break;
00872                 if( k == (*it)->end() )
00873                 {
00874                         v.erase( it );
00875                         it = v.begin();
00876                 }
00877                 else
00878                         ++it;
00879         }
00880         return v.size();
00881 }
00882 
00884 vector<SpeciesInfo *> Parameters::allSpecies;
00885 map<icstring,SpeciesInfo *> Parameters::species;
00886 
00887 void Parameters::SetupSpecies( void )
00888 {
00889         for( vector<SpeciesInfo *>::iterator it = allSpecies.begin() ; it != allSpecies.end() ; ++it )
00890                 delete (*it);
00891         allSpecies.clear();
00892         species.clear();
00893 
00894         Rsrc    res( "@Colony" );
00895         // SDL_Delay( 50 );
00896         icstring tmp = res.FindCombination( icstring( "species" ) );
00897         icstring        s = strip( tmp );
00898         // SDL_Delay( 50 );
00899         while( !s.empty() )
00900         {
00901                 icstring        aStr = res.GetList( s );
00902                 SpeciesInfo     *si = new SpeciesInfo;
00903                 si->name = Rsrc::GetId( aStr );
00904                 
00905                 icstring str = Rsrc::FindCrdOf( aStr , "color" );
00906                 si->color = Rsrc::GetColor( str , si->color );
00907                 
00908                 str = Rsrc::FindCrdOf( aStr , "humidity" );
00909                 si->humidity = Rsrc::GetInt( str );
00910 
00911                 str = Rsrc::FindCrdOf( aStr , "SurfaceTemperature" );
00912                 si->surfaceTemperature = Rsrc::GetInt( str );
00913 
00914                 str = Rsrc::FindCrdOf( aStr , "AtmPressure" );
00915                 si->atmPressure = Rsrc::GetInt( str );
00916 
00917                 // SDL_Delay( 50 );
00918                 str = Rsrc::FindCrdOf( aStr , "Potential" );
00919                 tmp = Rsrc::FindCrdOf( str , "Power" );
00920                 si->powerPotential = Rsrc::GetInt( tmp );
00921                 tmp = Rsrc::FindCrdOf( str , "Industry" );
00922                 si->industryPotential = Rsrc::GetInt( tmp );
00923                 tmp = Rsrc::FindCrdOf( str , "Medical" );
00924                 si->medicalPotential = Rsrc::GetInt( tmp );
00925                 tmp = Rsrc::FindCrdOf( str , "Farming" );
00926                 si->farmingPotential = Rsrc::GetInt( tmp );
00927                 allSpecies.push_back( si );
00928                 // SDL_Delay( 50 );
00929         }
00930 }
00931 

Generated on Fri Dec 5 04:06:00 2003 for Borqueror by doxygen 1.3.3