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

residentialsim.cpp

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

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