| 1 | /*************************************************************************** |
| 2 | mainsim.cpp - description |
| 3 | ------------------- |
| 4 | begin : february 21th, 2006 |
| 5 | copyright : (C) 2006-2007 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: mainsim.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_direction.h" |
| 22 | #include "opencity_structure_type.h" |
| 23 | |
| 24 | // OpenCity headers |
| 25 | #include "mainsim.h" |
| 26 | #include "residentialsim.h" // RCI, WEG and traffic management |
| 27 | #include "commercialsim.h" |
| 28 | #include "industrialsim.h" |
| 29 | #include "electricitysim.h" |
| 30 | #include "trafficsim.h" |
| 31 | #include "structure.h" |
| 32 | #include "buildinglayer.h" |
| 33 | |
| 34 | // Global settings |
| 35 | #include "globalvar.h" |
| 36 | extern GlobalVar gVars; |
| 37 | |
| 38 | // Standard header |
| 39 | #include <sstream> // Debug string stream |
| 40 | |
| 41 | |
| 42 | /*======================================================================*/ |
| 43 | MainSim::MainSim( |
| 44 | SDL_mutex* mutex, |
| 45 | BuildingLayer* pblayer, |
| 46 | Map* pmap ): |
| 47 | Simulator( mutex, pblayer, pmap ) |
| 48 | { |
| 49 | OPENCITY_DEBUG( "MainSim param ctor" ); |
| 50 | |
| 51 | // Simulators' initialization |
| 52 | _tpSimulator[Simulator::OC_RESIDENTIAL] = new ResidentialSim( mutex, pblayer, pmap ); |
| 53 | _tpSimulator[Simulator::OC_COMMERCIAL] = new CommercialSim( mutex, pblayer, pmap ); |
| 54 | _tpSimulator[Simulator::OC_INDUSTRIAL] = new IndustrialSim( mutex, pblayer, pmap ); |
| 55 | _tpSimulator[Simulator::OC_ELECTRIC] = new ElectricitySim( mutex, pblayer, pmap ); |
| 56 | _tpSimulator[Simulator::OC_TRAFFIC] = new TrafficSim( mutex, pblayer, pmap, gVars.gpPathFinder, gVars.gpMoveMgr ); |
| 57 | |
| 58 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 59 | Simulator::_tiVariation[ui] = 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /*======================================================================*/ |
| 65 | MainSim::~MainSim() |
| 66 | { |
| 67 | OPENCITY_DEBUG( "MainSim dtor" ); |
| 68 | |
| 69 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 70 | delete _tpSimulator[ui]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /*======================================================================*/ |
| 76 | void |
| 77 | MainSim::SaveTo( std::fstream& rfs ) |
| 78 | { |
| 79 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 80 | _tpSimulator[ui]->SaveTo(rfs); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /*======================================================================*/ |
| 86 | void |
| 87 | MainSim::LoadFrom( std::fstream& rfs ) |
| 88 | { |
| 89 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 90 | _tpSimulator[ui]->LoadFrom(rfs); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /*======================================================================*/ |
| 96 | int |
| 97 | MainSim::Main() |
| 98 | { |
| 99 | static uint times = 0; |
| 100 | // ostringstream oss; |
| 101 | |
| 102 | // Call the Main method of each micro simulator |
| 103 | while (_eSimState != SIMULATOR_RETURN) { |
| 104 | if (_eSimState == SIMULATOR_RUNNING) { |
| 105 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 106 | _tpSimulator[ui]->Main(); |
| 107 | // oss << ui << "=" << (int)Simulator::_tiVariation[ui] << " "; |
| 108 | } |
| 109 | } |
| 110 | // OPENCITY_DEBUG( "RCIET: " << oss.str() ); |
| 111 | // oss.str(""); |
| 112 | |
| 113 | // Refresh the simulator values every 5 turns |
| 114 | if (times == 0) |
| 115 | RefreshSimValue(); |
| 116 | |
| 117 | // Wait a bit |
| 118 | Simulator::RCIDelay(); |
| 119 | // debug |
| 120 | // SDL_Delay(50); |
| 121 | times = (times+1) % 5; |
| 122 | } // while |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*======================================================================*/ |
| 129 | void |
| 130 | MainSim::AddStructure |
| 131 | ( |
| 132 | const uint w1, const uint l1, |
| 133 | const uint w2, const uint l2, |
| 134 | const OPENCITY_SIMULATOR sim |
| 135 | ) |
| 136 | { |
| 137 | if (sim == OC_SIMULATOR_DEFAULT) { |
| 138 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 139 | _tpSimulator[ui]->AddStructure( w1, l1, w2, l2 ); |
| 140 | } |
| 141 | } |
| 142 | else { |
| 143 | _tpSimulator[sim]->AddStructure( w1, l1, w2, l2 ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /*======================================================================*/ |
| 149 | void |
| 150 | MainSim::RemoveStructure |
| 151 | ( |
| 152 | const uint w1, const uint l1, |
| 153 | const uint w2, const uint l2, |
| 154 | const OPENCITY_SIMULATOR sim |
| 155 | ) |
| 156 | { |
| 157 | if (sim == OC_SIMULATOR_DEFAULT) { |
| 158 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 159 | _tpSimulator[ui]->RemoveStructure( w1, l1, w2, l2 ); |
| 160 | } |
| 161 | } |
| 162 | else { |
| 163 | _tpSimulator[sim]->RemoveStructure( w1, l1, w2, l2 ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /*======================================================================*/ |
| 169 | void |
| 170 | MainSim::Run() |
| 171 | { |
| 172 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 173 | _tpSimulator[ui]->Run(); |
| 174 | } |
| 175 | Simulator::Run(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /*======================================================================*/ |
| 180 | void |
| 181 | MainSim::Stop() |
| 182 | { |
| 183 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 184 | _tpSimulator[ui]->Stop(); |
| 185 | } |
| 186 | Simulator::Stop(); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /*======================================================================*/ |
| 191 | void |
| 192 | MainSim::Return() |
| 193 | { |
| 194 | for (uint ui = 0; ui < Simulator::OC_SIMULATOR_NUMBER; ui++) { |
| 195 | _tpSimulator[ui]->Return(); |
| 196 | } |
| 197 | Simulator::Return(); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /*======================================================================*/ |
| 202 | void |
| 203 | MainSim::RefreshSimValue() |
| 204 | { |
| 205 | /* |
| 206 | Iterate over each structure and calculate the micro simulators' values. |
| 207 | The values of the micro simulators depend on the level of the structure. |
| 208 | The calculation is exact however, the micro simulators can do some |
| 209 | fluctuations in between. |
| 210 | */ |
| 211 | int resVal = 0, comVal = 0, indVal = 0; |
| 212 | int eleVal = 0; |
| 213 | uint linear = 0, maxLinear = 0; |
| 214 | uint level = 0, eleMultiplier = 0; |
| 215 | Structure* pstruct = NULL; |
| 216 | |
| 217 | |
| 218 | // FOR each structre DO |
| 219 | maxLinear = _pBuildLayer->GetMaxLinear(); |
| 220 | for ( linear = 0; linear <= maxLinear; linear++ ) { |
| 221 | pstruct = _pBuildLayer->GetLinearStructure(linear); |
| 222 | if (pstruct == NULL) |
| 223 | continue; |
| 224 | |
| 225 | level = pstruct->GetLevel(); |
| 226 | eleMultiplier = (pstruct->IsSet(OC_STRUCTURE_E)) ? 1 : 0; |
| 227 | switch (pstruct->GetCode()) { |
| 228 | case OC_STRUCTURE_RES: |
| 229 | resVal += level; |
| 230 | eleVal -= level * eleMultiplier; |
| 231 | break; |
| 232 | |
| 233 | case OC_STRUCTURE_COM: |
| 234 | comVal += level; |
| 235 | eleVal -= level * eleMultiplier; |
| 236 | break; |
| 237 | |
| 238 | case OC_STRUCTURE_IND: |
| 239 | indVal += level; |
| 240 | eleVal -= level * eleMultiplier; |
| 241 | break; |
| 242 | |
| 243 | case OC_STRUCTURE_FIREDEPT: |
| 244 | case OC_STRUCTURE_POLICEDEPT: |
| 245 | case OC_STRUCTURE_EDUCATIONDEPT: |
| 246 | case OC_STRUCTURE_HOSPITALDEPT: |
| 247 | eleVal -= OC_DEPT_POWER_CONSUMPTION * eleMultiplier; |
| 248 | break; |
| 249 | |
| 250 | case OC_STRUCTURE_PART: |
| 251 | case OC_STRUCTURE_ELINE: |
| 252 | eleVal -= 1 * eleMultiplier; |
| 253 | break; |
| 254 | |
| 255 | case OC_STRUCTURE_EPLANT_COAL: |
| 256 | eleVal += OC_EPLANT_COAL_POWER; |
| 257 | break; |
| 258 | case OC_STRUCTURE_EPLANT_NUCLEAR: |
| 259 | eleVal += OC_EPLANT_NUCLEAR_POWER; |
| 260 | break; |
| 261 | |
| 262 | // Nothing to do here |
| 263 | case OC_STRUCTURE_PARK: |
| 264 | case OC_STRUCTURE_FLORA: |
| 265 | case OC_STRUCTURE_ROAD: |
| 266 | case OC_STRUCTURE_TEST: // Development test feature |
| 267 | break; |
| 268 | |
| 269 | default: |
| 270 | OPENCITY_DEBUG( "What is this structure code: " << pstruct->GetCode() ); |
| 271 | assert( 0 ); |
| 272 | } |
| 273 | } // for |
| 274 | |
| 275 | // Update the micro simulators' values |
| 276 | _tpSimulator[Simulator::OC_RESIDENTIAL]->SetValue(resVal); |
| 277 | _tpSimulator[Simulator::OC_COMMERCIAL]->SetValue(comVal); |
| 278 | _tpSimulator[Simulator::OC_INDUSTRIAL]->SetValue(indVal); |
| 279 | _tpSimulator[Simulator::OC_ELECTRIC]->SetValue(eleVal); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /*======================================================================*/ |
| 284 | const int |
| 285 | MainSim::GetValue |
| 286 | ( |
| 287 | const OPENCITY_SIMULATOR sim |
| 288 | ) const |
| 289 | { |
| 290 | return _tpSimulator[sim]->GetValue(); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /*======================================================================*/ |
| 295 | const int |
| 296 | MainSim::GetMaxValue |
| 297 | ( |
| 298 | const OPENCITY_SIMULATOR sim |
| 299 | ) const |
| 300 | { |
| 301 | return _tpSimulator[sim]->GetMaxValue(); |
| 302 | } |