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

mainsim.cpp

00001 /***************************************************************************
00002                         mainsim.cpp  -  description
00003                             -------------------
00004     begin                : febuary 21th, 2006
00005     copyright            : (C) 2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007 
00008     $Id: mainsim.cpp 78 2006-11-02 20:38:34Z 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 "mainsim.h"
00021 #include "residentialsim.h"             // RCI, WEG and traffic management
00022 #include "commercialsim.h"
00023 #include "industrialsim.h"
00024 #include "electricitysim.h"
00025 #include "trafficsim.h"
00026 #include "structure.h"
00027 #include "buildinglayer.h"
00028 
00029 #include "globalvar.h"
00030 extern GlobalVar gVars;
00031 
00032 
00033    /*======================================================================*/
00034 MainSim::MainSim(
00035     SDL_mutex* mutex,
00036     BuildingLayer* pblayer,
00037     Map* pmap ):
00038 Simulator( mutex, pblayer, pmap )
00039 {
00040     OPENCITY_DEBUG( "MainSim param ctor" );
00041 
00042 // Simulators' initialization
00043     _tpSimulator[OC_MICROSIM_RES] = new ResidentialSim( mutex, pblayer, pmap );
00044     _tpSimulator[OC_MICROSIM_COM] = new CommercialSim( mutex, pblayer, pmap );
00045     _tpSimulator[OC_MICROSIM_IND] = new IndustrialSim( mutex, pblayer, pmap );
00046     _tpSimulator[OC_MICROSIM_ELE] = new ElectricitySim( mutex, pblayer, pmap );
00047     _tpSimulator[OC_MICROSIM_TRA] = new TrafficSim( mutex, pblayer, pmap, gVars.gpPathFinder, gVars.gpMoveMgr );
00048 }
00049 
00050 
00051    /*======================================================================*/
00052 MainSim::~MainSim()
00053 {
00054     OPENCITY_DEBUG( "MainSim dtor" );
00055 
00056     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00057         delete _tpSimulator[ui];
00058     }
00059 }
00060 
00061 
00062    /*======================================================================*/
00063 void
00064 MainSim::SaveTo( std::fstream& rfs )
00065 {
00066     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00067         _tpSimulator[ui]->SaveTo(rfs);
00068     }
00069 }
00070 
00071 
00072    /*======================================================================*/
00073 void
00074 MainSim::LoadFrom( std::fstream& rfs )
00075 {
00076     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00077         _tpSimulator[ui]->LoadFrom(rfs);
00078     }
00079 }
00080 
00081 
00082    /*======================================================================*/
00083 int
00084 MainSim::Main()
00085 {
00086     static uint times = 0;
00087 
00088 // Call the Main method of each micro simulator
00089     while (this->enumSimState != SIMULATOR_RETURN) {
00090         if (this->enumSimState == SIMULATOR_RUNNING) {
00091             for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00092                 _tpSimulator[ui]->Main();
00093             }
00094         }
00095 
00096     // Refresh the simulator values every 5 turns
00097         if (times == 0)
00098             RefreshSimValue();
00099     
00100     // Wait a bit
00101         Simulator::RCIDelay();
00102         times = (times+1) % 5;
00103     } // while
00104 
00105     return 0;
00106 }
00107 
00108 
00109    /*======================================================================*/
00110 void
00111 MainSim::AddStructure
00112 (
00113     const uint w1, const uint l1,
00114     const uint w2, const uint l2,
00115     const OPENCITY_MICROSIM sim
00116 )
00117 {
00118     if (sim == OC_MICROSIM_DEFAULT) {
00119         for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00120             _tpSimulator[ui]->AddStructure( w1, l1, w2, l2 );
00121         }
00122     }
00123     else {
00124         _tpSimulator[sim]->AddStructure( w1, l1, w2, l2 );
00125     }
00126 }
00127 
00128 
00129    /*======================================================================*/
00130 void
00131 MainSim::RemoveStructure
00132 (
00133     const uint w1, const uint l1,
00134     const uint w2, const uint l2,
00135     const OPENCITY_MICROSIM sim
00136 )
00137 {
00138     if (sim == OC_MICROSIM_DEFAULT) {
00139         for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00140             _tpSimulator[ui]->RemoveStructure( w1, l1, w2, l2 );
00141         }
00142     }
00143     else {
00144         _tpSimulator[sim]->RemoveStructure( w1, l1, w2, l2 );
00145     }
00146 }
00147 
00148 
00149    /*======================================================================*/
00150 void
00151 MainSim::Run()
00152 {
00153     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00154         _tpSimulator[ui]->Run();
00155     }
00156     Simulator::Run();
00157 }
00158 
00159 
00160    /*======================================================================*/
00161 void
00162 MainSim::Stop()
00163 {
00164     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00165         _tpSimulator[ui]->Stop();
00166     }
00167     Simulator::Stop();
00168 }
00169 
00170 
00171    /*======================================================================*/
00172 void
00173 MainSim::Return()
00174 {
00175     for (uint ui = 0; ui < OC_MICROSIM_MAX; ui++) {
00176         _tpSimulator[ui]->Return();
00177     }
00178     Simulator::Return();
00179 }
00180 
00181 
00182    /*======================================================================*/
00183 void
00184 MainSim::RefreshSimValue()
00185 {
00186 /*
00187     Iterate over each structure and calculate the micro simulators' values.
00188 The values of the micro simulators depend on the level of the structure.
00189 The calculation is exact however, the micro simulators can do some
00190 fluctuations in between.
00191 */
00192     int resVal = 0, comVal = 0, indVal = 0;
00193     int eleVal = 0;
00194     uint linear = 0, maxLinear = 0;
00195     uint level = 0, eleMultiplier = 0;
00196     Structure* pstruct = NULL;
00197 
00198 
00199 // FOR each structre DO
00200     maxLinear = pbuildlayer->GetMaxLinear();
00201     for ( linear = 0; linear <= maxLinear; linear++ ) {
00202         pstruct = pbuildlayer->GetLinearStructure(linear);
00203         if (pstruct == NULL)
00204             continue;
00205 
00206         level = pstruct->GetLevel();
00207         eleMultiplier = (pstruct->IsSet(OC_STRUCTURE_E)) ? 1 : 0;
00208         switch (pstruct->GetCode()) {
00209             case OC_STRUCTURE_RES:
00210                 resVal += level;
00211                 eleVal -= level * eleMultiplier;
00212                 break;
00213 
00214             case OC_STRUCTURE_COM:
00215                 comVal += level;
00216                 eleVal -= level * eleMultiplier;
00217                 break;
00218 
00219             case OC_STRUCTURE_IND:
00220                 indVal += level;
00221                 eleVal -= level * eleMultiplier;
00222                 break;
00223 
00224             case OC_STRUCTURE_FIREDEPT:
00225             case OC_STRUCTURE_POLICEDEPT:
00226             case OC_STRUCTURE_EDUCATIONDEPT:
00227             case OC_STRUCTURE_HOSPITALDEPT:
00228                 eleVal -= OC_DEPT_POWER_CONSUMPTION * eleMultiplier;
00229                 break;
00230 
00231             case OC_STRUCTURE_PART:
00232             case OC_STRUCTURE_ELINE:
00233                 eleVal -= 1 * eleMultiplier;
00234                 break;
00235 
00236             case OC_STRUCTURE_EPLANT_COAL:
00237                 eleVal += OC_EPLANT_COAL_POWER;
00238                 break;
00239 
00240         // Nothing to do here
00241             case OC_STRUCTURE_PARK:
00242             case OC_STRUCTURE_FLORA:
00243             case OC_STRUCTURE_ROAD:
00244             case OC_STRUCTURE_TEST:         // Development test feature
00245                 break;
00246 
00247             default:
00248                 OPENCITY_DEBUG( "What is this ?" );
00249                 assert( 0 );
00250         }
00251     } // for
00252 
00253 // Update the micro simulators' values
00254     _tpSimulator[OC_MICROSIM_RES]->SetValue(resVal);
00255     _tpSimulator[OC_MICROSIM_COM]->SetValue(comVal);
00256     _tpSimulator[OC_MICROSIM_IND]->SetValue(indVal);
00257     _tpSimulator[OC_MICROSIM_ELE]->SetValue(eleVal);
00258 }
00259 
00260 
00261    /*======================================================================*/
00262 const int
00263 MainSim::GetValue
00264 (
00265     const OPENCITY_MICROSIM sim
00266 ) const
00267 {
00268     return _tpSimulator[sim]->GetValue();
00269 }
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 

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