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

industrialsim.cpp

00001 /***************************************************************************
00002                         industrialsim.cpp  -  description
00003                             -------------------
00004     begin                : dim fév  1 2004
00005     copyright            : (C) 2004-2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007     
00008     $Id: industrialsim.cpp 46 2006-09-16 10:02:10Z 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 "industrialsim.h"
00021 
00022 #include "buildinglayer.h"
00023 #include "structure.h"
00024 
00025 
00026 
00027    /*======================================================================*/
00028 IndustrialSim::IndustrialSim(
00029     SDL_mutex* mutex,
00030     BuildingLayer* pblayer,
00031     Map* pmap ):
00032 Simulator( mutex, pblayer, pmap )
00033 {
00034     OPENCITY_DEBUG( "ISim param ctor" );
00035 }
00036 
00037 
00038    /*======================================================================*/
00039 IndustrialSim::~IndustrialSim()
00040 {
00041     OPENCITY_DEBUG( "ISim dtor" );
00042 }
00043 
00044 
00045    /*======================================================================*/
00046 int
00047 IndustrialSim::Main()
00048 {
00049     static uint w, l;
00050     static Structure* pstruct;
00051     static bool boolLevelUp;
00052     static int iRandom;
00053     static OPENCITY_GRAPHIC_CODE oldGC;
00054 
00055 
00056     if (this->enumSimState == SIMULATOR_RUNNING) {
00057     // get a random industrial structure
00058         pstruct = pbuildlayer->GetRandomStructure(
00059             w, l, OC_STRUCTURE_IND );
00060 
00061         if (pstruct != NULL) {
00062             boolLevelUp = false;
00063 
00064         // try to lock the mutex
00065         // prevent the others from deleting the structure
00066         // pointed by "pstruct" while we're playing with
00067             SDL_LockMutex( this->mutexMain );
00068 
00069             pstruct->Unset(
00070                 OC_STRUCTURE_W                  | OC_STRUCTURE_G |
00071                 OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I |
00072                 OC_STRUCTURE_P );
00073             pstruct->Set( OC_STRUCTURE_I );
00074 
00075         // is there a P in range ?
00076             if (CheckRange(
00077                 w, l, OC_I_P_RANGE, OC_STRUCTURE_ROAD ) == true)
00078                 pstruct->Set( OC_STRUCTURE_P );
00079         // is there a R in range ?
00080             if (CheckRange(
00081                 w, l, OC_I_R_RANGE, OC_STRUCTURE_RES ) == true)
00082                 pstruct->Set( OC_STRUCTURE_R );
00083         // is there a C in range ?
00084             if (CheckRange(
00085                 w, l, OC_I_C_RANGE, OC_STRUCTURE_COM ) == true)
00086                 pstruct->Set( OC_STRUCTURE_C );
00087 
00088             if (pstruct->IsSet(
00089                 OC_STRUCTURE_E |
00090                 OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I |
00091                 OC_STRUCTURE_P ) == true )
00092                 boolLevelUp = true;
00093 
00094 
00095 //debug
00096 //cout << "IndustrialSim speaking: " << _iValue
00097 //     << " / w: " << w << " / l: " << l << endl;
00098 
00099             iRandom = rand() % 100;
00100             oldGC = pstruct->GetGraphicCode();
00101             if (boolLevelUp == true) {
00102             // really levelup ?
00103                 if (iRandom < OC_SIMULATOR_UP) {
00104                     if ((this->CheckLevelUp(w, l, pstruct) == true)
00105                     &&  (pstruct->LevelUp() == true)) {
00106                         pbuildlayer->ResizeStructure( w, l, oldGC );
00107                         _iValue++;
00108                     }
00109                 }
00110             }  // end if levelup
00111             else {
00112             // really level down ?
00113                 if (iRandom < OC_SIMULATOR_DOWN)
00114                     if ((this->CheckLevelDown(w, l, pstruct) == true)
00115                     &&  (pstruct->LevelDown() == true)) {
00116                         pbuildlayer->ResizeStructure( w, l, oldGC );
00117                         _iValue--;
00118                     }
00119             }
00120 
00121             SDL_UnlockMutex( this->mutexMain );
00122         } // if pstruct != NULL
00123     }  // if running
00124 
00125     return 0;
00126 }
00127 
00128 
00129    /*=====================================================================*/
00130 void
00131 IndustrialSim::RemoveStructure(
00132     const uint & w1,
00133     const uint & h1,
00134     const uint & w2,
00135     const uint & h2 )
00136 {
00137     Structure* pstruct = pbuildlayer->GetStructure( w1, h1 );
00138 
00139    // if this is a I zone
00140    // and it has a positive value according to its level
00141    // then we remove its value from the sim.
00142     if (pstruct != NULL)
00143     if (pstruct->GetCode() == OC_STRUCTURE_IND)
00144     if (pstruct->GetLevel() - 1 > 0)
00145         _iValue -= pstruct->GetLevel()-1;
00146 }
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 

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