| 1 | /*************************************************************************** |
| 2 | industrialsim.cpp - description |
| 3 | ------------------- |
| 4 | begin : feb 1st, 2004 |
| 5 | copyright : (C) 2004-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: industrialsim.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 enumerations |
| 21 | #include "opencity_structure_type.h" |
| 22 | |
| 23 | // OpenCity headers |
| 24 | #include "industrialsim.h" |
| 25 | #include "buildinglayer.h" |
| 26 | #include "structure.h" |
| 27 | |
| 28 | |
| 29 | /*======================================================================*/ |
| 30 | IndustrialSim::IndustrialSim( |
| 31 | SDL_mutex* mutex, |
| 32 | BuildingLayer* pblayer, |
| 33 | Map* pmap ): |
| 34 | Simulator( mutex, pblayer, pmap ) |
| 35 | { |
| 36 | OPENCITY_DEBUG( "ISim param ctor" ); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /*======================================================================*/ |
| 41 | IndustrialSim::~IndustrialSim() |
| 42 | { |
| 43 | OPENCITY_DEBUG( "ISim dtor" ); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /*======================================================================*/ |
| 48 | int |
| 49 | IndustrialSim::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 industrial structure |
| 62 | pstruct = _pBuildLayer->GetRandomStructure(w, l, OC_STRUCTURE_IND); |
| 63 | if (pstruct == NULL) |
| 64 | return 0; |
| 65 | |
| 66 | boolLevelUp = false; |
| 67 | |
| 68 | // Try to lock the mutex to prevent the others from deleting the structure |
| 69 | // pointed by "pstruct" while we're playing with |
| 70 | SDL_LockMutex( _pMutexMain ); |
| 71 | |
| 72 | pstruct->Unset( |
| 73 | OC_STRUCTURE_W | OC_STRUCTURE_G | |
| 74 | OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I | |
| 75 | OC_STRUCTURE_P ); |
| 76 | pstruct->Set( OC_STRUCTURE_I ); |
| 77 | |
| 78 | // is there a P in range ? |
| 79 | if (CheckRange(w, l, OC_I_P_RANGE, OC_STRUCTURE_ROAD) == true) |
| 80 | pstruct->Set( OC_STRUCTURE_P ); |
| 81 | else |
| 82 | _tiVariation[Simulator::OC_TRAFFIC]++; |
| 83 | |
| 84 | // is there a R in range ? |
| 85 | if (CheckRange( |
| 86 | w, l, OC_I_R_RANGE, OC_STRUCTURE_RES ) == true) |
| 87 | pstruct->Set( OC_STRUCTURE_R ); |
| 88 | else |
| 89 | _tiVariation[Simulator::OC_RESIDENTIAL]++; |
| 90 | |
| 91 | // is there a C in range ? |
| 92 | if (CheckRange(w, l, OC_I_C_RANGE, OC_STRUCTURE_COM) == true) |
| 93 | pstruct->Set( OC_STRUCTURE_C ); |
| 94 | else |
| 95 | _tiVariation[Simulator::OC_COMMERCIAL]++; |
| 96 | |
| 97 | if (pstruct->IsSet( |
| 98 | OC_STRUCTURE_E | |
| 99 | OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I | |
| 100 | OC_STRUCTURE_P ) == true ) |
| 101 | boolLevelUp = true; |
| 102 | |
| 103 | iRandom = rand() % 100; |
| 104 | oldGC = pstruct->GetGraphicCode(); |
| 105 | if (boolLevelUp == true) { |
| 106 | // really levelup ? |
| 107 | if (iRandom < OC_SIMULATOR_UP) { |
| 108 | if ((this->CheckLevelUp(w, l, pstruct) == true) |
| 109 | && (pstruct->LevelUp() == true)) { |
| 110 | _pBuildLayer->ResizeStructure( w, l, oldGC ); |
| 111 | _iValue++; |
| 112 | _tiVariation[Simulator::OC_INDUSTRIAL]--; |
| 113 | } |
| 114 | } |
| 115 | } // end if levelup |
| 116 | else { |
| 117 | // really level down ? |
| 118 | if (iRandom < OC_SIMULATOR_DOWN) |
| 119 | if ((this->CheckLevelDown(w, l, pstruct) == true) |
| 120 | && (pstruct->LevelDown() == true)) { |
| 121 | _pBuildLayer->ResizeStructure( w, l, oldGC ); |
| 122 | _iValue--; |
| 123 | _tiVariation[Simulator::OC_INDUSTRIAL]++; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | SDL_UnlockMutex( _pMutexMain ); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /*=====================================================================*/ |
| 134 | void |
| 135 | IndustrialSim::RemoveStructure( |
| 136 | const uint & w1, |
| 137 | const uint & h1, |
| 138 | const uint & w2, |
| 139 | const uint & h2 ) |
| 140 | { |
| 141 | Structure* pstruct = _pBuildLayer->GetStructure( w1, h1 ); |
| 142 | |
| 143 | // if this is a I zone |
| 144 | // and it has a positive value according to its level |
| 145 | // then we remove its value from the sim. |
| 146 | if (pstruct != NULL) |
| 147 | if (pstruct->GetCode() == OC_STRUCTURE_IND) |
| 148 | if (pstruct->GetLevel() - 1 > 0) |
| 149 | _iValue -= pstruct->GetLevel()-1; |
| 150 | } |