| 1 | /*************************************************************************** |
| 2 | commercialsim.cpp - description |
| 3 | ------------------- |
| 4 | begin : jan 31th, 2004 |
| 5 | copyright : (C) 2004-2010 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: commercialsim.cpp 450 2010-11-21 19:11:43Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | // Useful enumeration |
| 21 | #include "opencity_structure_type.h" |
| 22 | |
| 23 | // OpenCity headers |
| 24 | #include "commercialsim.h" |
| 25 | #include "buildinglayer.h" |
| 26 | #include "structure.h" |
| 27 | |
| 28 | |
| 29 | /*======================================================================*/ |
| 30 | CommercialSim::CommercialSim( |
| 31 | SDL_mutex* mutex, |
| 32 | BuildingLayer* pblayer, |
| 33 | Map* pmap ): |
| 34 | Simulator( mutex, pblayer, pmap ) |
| 35 | { |
| 36 | OPENCITY_DEBUG( "CSim param ctor" ); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /*======================================================================*/ |
| 41 | CommercialSim::~CommercialSim() |
| 42 | { |
| 43 | OPENCITY_DEBUG( "CSim dtor" ); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /*======================================================================*/ |
| 48 | int |
| 49 | CommercialSim::Main() |
| 50 | { |
| 51 | static uint w, l; |
| 52 | static Structure* pstruct; |
| 53 | static bool boolLevelUp; |
| 54 | static int iRandom; |
| 55 | static OPENCITY_GRAPHIC_CODE oldGC; |
| 56 | |
| 57 | |
| 58 | if (_eSimState != SIMULATOR_RUNNING) |
| 59 | return 0; |
| 60 | |
| 61 | // Get a random commercial structure |
| 62 | pstruct = _pBuildLayer->GetRandomStructure(w, l, OC_STRUCTURE_COM); |
| 63 | if (pstruct == NULL) |
| 64 | return 0; |
| 65 | |
| 66 | boolLevelUp = false; |
| 67 | // Try to lock the mutex to prevent the others from deleting the structure |
| 68 | // pointed by "pstruct" while we're playing with |
| 69 | SDL_LockMutex( _pMutexMain ); |
| 70 | |
| 71 | pstruct->Unset( |
| 72 | OC_STRUCTURE_W | OC_STRUCTURE_G | |
| 73 | OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I | |
| 74 | OC_STRUCTURE_P ); |
| 75 | pstruct->Set( OC_STRUCTURE_C ); |
| 76 | |
| 77 | // is there a P in range ? |
| 78 | if (CheckRange(w, l, OC_C_P_RANGE, OC_STRUCTURE_ROAD) == true) |
| 79 | pstruct->Set( OC_STRUCTURE_P ); |
| 80 | else |
| 81 | _tiVariation[Simulator::OC_TRAFFIC]++; |
| 82 | |
| 83 | // is there a R in range ? |
| 84 | if (CheckRange(w, l, OC_C_R_RANGE, OC_STRUCTURE_RES) == true) |
| 85 | pstruct->Set( OC_STRUCTURE_R ); |
| 86 | else |
| 87 | _tiVariation[Simulator::OC_RESIDENTIAL]++; |
| 88 | |
| 89 | // is there a I in range ? |
| 90 | if (CheckRange(w, l, OC_C_I_RANGE, OC_STRUCTURE_IND) == true) |
| 91 | pstruct->Set( OC_STRUCTURE_I ); |
| 92 | else |
| 93 | _tiVariation[Simulator::OC_INDUSTRIAL]++; |
| 94 | |
| 95 | if (pstruct->IsSet( |
| 96 | OC_STRUCTURE_E | |
| 97 | OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I | |
| 98 | OC_STRUCTURE_P ) == true ) |
| 99 | boolLevelUp = true; |
| 100 | |
| 101 | iRandom = rand() % 100; |
| 102 | oldGC = pstruct->GetGraphicCode(); |
| 103 | if (boolLevelUp == true) { |
| 104 | // really levelup ? |
| 105 | if (iRandom < OC_SIMULATOR_UP) { |
| 106 | if ((this->CheckLevelUp(w, l, pstruct) == true) |
| 107 | && (pstruct->LevelUp() == true)) { |
| 108 | _pBuildLayer->ResizeStructure( w, l, oldGC ); |
| 109 | _iValue++; |
| 110 | _tiVariation[Simulator::OC_COMMERCIAL]--; |
| 111 | } |
| 112 | } |
| 113 | } // end if levelup |
| 114 | else { |
| 115 | // really level down ? |
| 116 | if (iRandom < OC_SIMULATOR_DOWN) |
| 117 | if ((this->CheckLevelDown(w, l, pstruct) == true) |
| 118 | && (pstruct->LevelDown() == true)) { |
| 119 | _pBuildLayer->ResizeStructure( w, l, oldGC ); |
| 120 | _iValue--; |
| 121 | _tiVariation[Simulator::OC_COMMERCIAL]++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | SDL_UnlockMutex( _pMutexMain ); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /*=====================================================================*/ |
| 132 | void |
| 133 | CommercialSim::RemoveStructure( |
| 134 | const uint& w1, |
| 135 | const uint& h1, |
| 136 | const uint& w2, |
| 137 | const uint& h2 ) |
| 138 | { |
| 139 | Structure* pstruct = _pBuildLayer->GetStructure( w1, h1 ); |
| 140 | |
| 141 | // IF this is a C zone |
| 142 | // AND it has a positive value according to its level |
| 143 | // THEN we remove its value from the sim. |
| 144 | // and if level is 0 ? |
| 145 | if (pstruct != NULL) |
| 146 | if (pstruct->GetCode() == OC_STRUCTURE_COM) |
| 147 | if (pstruct->GetLevel() - 1 > 0) |
| 148 | _iValue -= pstruct->GetLevel()-1; |
| 149 | } |