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

commercialsim.cpp

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

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