Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

simulator.cpp

00001 /***************************************************************************
00002                         simulator.cpp  -  description
00003                             -------------------
00004     begin                : dim sep 21 2003
00005     copyright            : (C) 2003-2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007     
00008     $Id: simulator.cpp 63 2006-10-17 20:45:12Z neoneurone $
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   any later version.                                                    *
00017  *                                                                         *
00018  ***************************************************************************/
00019 
00020 #include "simulator.h"
00021 #include "buildinglayer.h"
00022 #include "structure.h"
00023 
00024 #include "globalvar.h"
00025 extern GlobalVar gVars;
00026 
00027 #include <cmath>                // For log10
00028 
00029 
00030    /*=====================================================================*/
00031 Simulator::Simulator():
00032 _iVariation( 0 ),
00033 _iValue( 0 ),
00034 enumSimState( SIMULATOR_STOPED ),
00035 mutexMain( NULL )
00036 {
00037     OPENCITY_DEBUG( "Sim ctor , do not use" );
00038 }
00039 
00040 
00041    /*=====================================================================*/
00042 Simulator::Simulator( SDL_mutex* mutex, BuildingLayer* pblayer, Map* pmap ):
00043 _iVariation( 0 ),
00044 _iValue( 0 ),
00045 enumSimState( SIMULATOR_STOPED ),
00046 mutexMain( mutex ),
00047 pbuildlayer( pblayer ),
00048 pmapOfCity( pmap )
00049 {
00050     OPENCITY_DEBUG( "Sim param ctor" );
00051 
00052    // it is critical to have a functional mutex
00053     assert( mutex != NULL );
00054 }
00055 
00056 
00057    /*=====================================================================*/
00058 Simulator::~Simulator()
00059 {
00060     OPENCITY_DEBUG( "Sim dtor" );
00061     mutexMain = NULL;
00062 }
00063 
00064 
00065    /*======================================================================*/
00066 void
00067 Simulator::SaveTo( std::fstream& rfs )
00068 {
00069     OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "saving" );
00070 
00071     rfs << _iVariation << std::ends;
00072     rfs << _iValue << std::ends;
00073 }
00074 
00075 
00076    /*======================================================================*/
00077 void
00078 Simulator::LoadFrom( std::fstream& rfs )
00079 {
00080     OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "loading" );
00081 
00082     rfs >> _iVariation; rfs.ignore();
00083     rfs >> _iValue; rfs.ignore();
00084 }
00085 
00086 
00087    /*======================================================================*/
00088 void
00089 Simulator::Run()
00090 {
00091     this->enumSimState = SIMULATOR_RUNNING;
00092 }
00093 
00094 
00095    /*======================================================================*/
00096 void
00097 Simulator::Stop()
00098 {
00099     this->enumSimState = SIMULATOR_STOPED;
00100 }
00101 
00102 
00103    /*======================================================================*/
00104 void
00105 Simulator::Return()
00106 {
00107     this->enumSimState = SIMULATOR_RETURN;
00108 }
00109 
00110 
00111    /*======================================================================*/
00112 const bool
00113 Simulator::CheckRange(
00114     const uint & w,
00115     const uint & l,
00116     const uint & range,
00117     const OPENCITY_STRUCTURE_CODE & enumStructCode ) const
00118 {
00119     static uint W1, L1, W2, L2;
00120 
00121    // calculate the possible W1,L1 and W2, L2
00122     W1 = w; L1 = l;
00123     W2 = w; L2 = l;
00124     pmapOfCity->GetPossibleWH( W1, L1, -range, -range );
00125     pmapOfCity->GetPossibleWH( W2, L2,  range,  range );
00126 
00127    // check if the requested structure is within the range
00128     return this->pbuildlayer->ContainStructure(
00129         W1, L1, W2, L2, enumStructCode );
00130 }
00131 
00132 
00133    /*======================================================================*/
00134 const bool
00135 Simulator::CheckLevelUp(
00136     const uint w,
00137     const uint l,
00138     const Structure* pStruct ) const
00139 {
00140     OPENCITY_DEBUG("CheckLevelUp");
00141 
00142     static OPENCITY_GRAPHIC_CODE nextGC;
00143     static uint ow = 0, ol = 0, oh = 0, nw = 0, nl = 0, nh = 0;     // Old, and New WLH
00144     static uint w2 = 0, l2 = 0;
00145     static uint layerW = 0, layerL = 0;
00146 
00147     assert( pStruct != NULL );          // Ya, we need it !
00148     nextGC = pStruct->GetNextLevelGraphicCode();
00149 
00150 // IF it's the same THEN ok
00151     if (nextGC == pStruct->GetGraphicCode()) {
00152         return true;
00153     }
00154 
00155 // Get the old WLH
00156     gVars.gpPropertyMgr->GetWLH(
00157         pStruct->GetGraphicCode(),
00158         ow, 0,
00159         ol, 0,
00160         oh, 0 );
00161     assert( ow != 0 );
00162 
00163 // Get the new WLH
00164     gVars.gpPropertyMgr->GetWLH(
00165         nextGC,
00166         nw, 0,
00167         nl, 0,
00168         nh, 0 );
00169     assert( nw != 0 );
00170 
00171 // IF they are the same THEN ok
00172     if ((ow == nw) && (ol == nl) && (oh == nh)) {
00173         return true;
00174     }
00175 
00176 // IF the old ones are bigger THEN shout out
00177     if ((ow > nw) || (ol > nl) || (oh > nh)) {
00178         OPENCITY_DEBUG("The higher level should have bigger dimensions");
00179     }
00180 
00181 // IF the new structure is out of the map THEN
00182     w2 = w + nw - 1;
00183     l2 = l + nl - 1;
00184     pbuildlayer->GetLayerSize( layerW, layerL );
00185     if ( w2 >= layerW || l2 >= layerL ) {
00186 //      OPENCITY_DEBUG( "Sorry, it's out of the map !" );
00187         return false;
00188     }
00189 
00190 // The old WLH are smaller
00191 // IF the new required surface does not contains only the required structure THEN
00192     if (this->pbuildlayer->ContainStructureOnly(
00193         w, l, w2, l2,
00194         pStruct->GetCode()) == false ) {
00195         return false;
00196     }
00197 
00198 // OK, next level has passed all the tests
00199     return true;
00200 }
00201 
00202 
00203    /*======================================================================*/
00204 const bool
00205 Simulator::CheckLevelDown(
00206     const uint w,
00207     const uint l,
00208     const Structure* pStruct ) const
00209 {
00210     OPENCITY_DEBUG("CheckLevelDown");
00211 
00212     static OPENCITY_GRAPHIC_CODE prevGC;
00213     static uint ow = 0, ol = 0, oh = 0, nw = 0, nl = 0, nh = 0;     // Old, and New WLH
00214     static uint w2 = 0, l2 = 0;
00215     static uint layerW = 0, layerL = 0;
00216 
00217     assert( pStruct != NULL );          // Ya, we need it !
00218     prevGC = pStruct->GetPreviousLevelGraphicCode();
00219 
00220 // IF it's the same THEN ok
00221     if (prevGC == pStruct->GetGraphicCode()) {
00222         return true;
00223     }
00224 
00225 // Get the old WLH
00226     gVars.gpPropertyMgr->GetWLH(
00227         pStruct->GetGraphicCode(),
00228         ow, 0,
00229         ol, 0,
00230         oh, 0 );
00231     assert( ow != 0 );
00232 
00233 // Get the new WLH
00234     gVars.gpPropertyMgr->GetWLH(
00235         prevGC,
00236         nw, 0,
00237         nl, 0,
00238         nh, 0 );
00239     assert( nw != 0 );
00240 
00241 // IF they are the same THEN ok
00242     if ((ow == nw) && (ol == nl) && (oh == nh)) {
00243         return true;
00244     }
00245 
00246 // IF the old ones are smaller THEN shout out
00247     if ((ow < nw) || (ol < nl) || (oh < nh)) {
00248         OPENCITY_DEBUG("The lower level should have smaller dimensions");
00249     }
00250 
00251 // IF the new structure is out of the map THEN
00252     w2 = w + nw - 1;
00253     l2 = l + nl - 1;
00254     pbuildlayer->GetLayerSize( layerW, layerL );
00255     if ( w2 >= layerW || l2 >= layerL ) {
00256         return false;
00257     }
00258 
00259 // The old WLH are bigger
00260 // IF the new required surface does not contains only the required structure THEN
00261     if (this->pbuildlayer->ContainStructureOnly(
00262         w, l, w2, l2,
00263         pStruct->GetCode()) == false ) {
00264         return false;
00265     }
00266 
00267 // OK, next level has passed all the tests
00268     return true;
00269 }
00270 
00271 
00272    /*======================================================================*/
00273 const int &
00274 Simulator::GetVariation() const
00275 {
00276     return this->_iVariation;
00277 }
00278 
00279 
00280    /*======================================================================*/
00281 const int
00282 Simulator::GetValue() const
00283 {
00284     return _iValue;
00285 }
00286 
00287 
00288    /*======================================================================*/
00289 void
00290 Simulator::SetVariation(
00291     const int rciVariation )
00292 {
00293     _iVariation = rciVariation;
00294 }
00295 
00296 
00297    /*======================================================================*/
00298 void
00299 Simulator::SetValue(
00300     const int rciValue )
00301 {
00302     _iValue = rciValue;
00303 }
00304 
00305 
00306    /*=====================================================================*/
00307    /*                        STATIC      METHODS                          */
00308    /*=====================================================================*/
00309 int
00310 Simulator::ThreadWrapper(
00311     void* pSim )
00312 {
00313     return ((Simulator*)pSim)->Main();
00314 }
00315 
00316 
00317    /*======================================================================*/
00318 void
00319 Simulator::RCIDelay( void )
00320 {
00321 // Added +1 to avoid log10(0)
00322     SDL_Delay((uint)
00323         (gVars.gfMsSimDelayMax - log10((OC_FLOAT)Structure::GetNumber() + 1)
00324         *OC_MS_STRUCTURE_LOG_FACTOR )
00325         );
00326 }
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 

Generated on Sat Nov 11 10:21:10 2006 for OpenCity by  doxygen 1.4.2