| 1 | /*************************************************************************** |
| 2 | simulator.cpp - description |
| 3 | ------------------- |
| 4 | begin : september 21th, 2003 |
| 5 | copyright : (C) 2003-2010 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: simulator.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_direction.h" |
| 22 | #include "opencity_structure_type.h" |
| 23 | |
| 24 | // OpenCity headers |
| 25 | #include "simulator.h" |
| 26 | #include "buildinglayer.h" |
| 27 | #include "structure.h" |
| 28 | |
| 29 | // Global settings |
| 30 | #include "globalvar.h" |
| 31 | extern GlobalVar gVars; |
| 32 | |
| 33 | // Standard headers |
| 34 | #include <cmath> // For log10 |
| 35 | |
| 36 | |
| 37 | // This shared table is initialized by MainSim |
| 38 | // WARNING: this may not be thread safe ! |
| 39 | volatile int Simulator::_tiVariation[Simulator::OC_SIMULATOR_NUMBER]; |
| 40 | |
| 41 | |
| 42 | /*=====================================================================*/ |
| 43 | Simulator::Simulator(): |
| 44 | _iVariation( 0 ), |
| 45 | _iValue( 0 ), |
| 46 | _eSimState( SIMULATOR_STOPED ), |
| 47 | _pMutexMain( NULL ) |
| 48 | { |
| 49 | OPENCITY_DEBUG( "Sim ctor , do not use" ); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /*=====================================================================*/ |
| 54 | Simulator::Simulator( SDL_mutex* mutex, BuildingLayer* pblayer, Map* pmap ): |
| 55 | _iVariation( 0 ), |
| 56 | _iValue( 0 ), |
| 57 | _eSimState( SIMULATOR_STOPED ), |
| 58 | _pMutexMain( mutex ), |
| 59 | _pBuildLayer( pblayer ), |
| 60 | _pMapCity( pmap ) |
| 61 | { |
| 62 | OPENCITY_DEBUG( "Sim param ctor" ); |
| 63 | |
| 64 | // it is critical to have a functional mutex |
| 65 | assert( mutex != NULL ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /*=====================================================================*/ |
| 70 | Simulator::~Simulator() |
| 71 | { |
| 72 | OPENCITY_DEBUG( "Sim dtor" ); |
| 73 | _pMutexMain = NULL; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /*======================================================================*/ |
| 78 | void |
| 79 | Simulator::SaveTo( std::fstream& rfs ) |
| 80 | { |
| 81 | OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "saving" ); |
| 82 | |
| 83 | rfs << _iVariation << std::ends; |
| 84 | rfs << _iValue << std::ends; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /*======================================================================*/ |
| 89 | void |
| 90 | Simulator::LoadFrom( std::fstream& rfs ) |
| 91 | { |
| 92 | OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "loading" ); |
| 93 | |
| 94 | rfs >> _iVariation; rfs.ignore(); |
| 95 | rfs >> _iValue; rfs.ignore(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /*======================================================================*/ |
| 100 | void |
| 101 | Simulator::Run() |
| 102 | { |
| 103 | _eSimState = SIMULATOR_RUNNING; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /*======================================================================*/ |
| 108 | void |
| 109 | Simulator::Stop() |
| 110 | { |
| 111 | _eSimState = SIMULATOR_STOPED; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /*======================================================================*/ |
| 116 | void |
| 117 | Simulator::Return() |
| 118 | { |
| 119 | _eSimState = SIMULATOR_RETURN; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /*======================================================================*/ |
| 124 | const bool |
| 125 | Simulator::CheckRange( |
| 126 | const uint& w, |
| 127 | const uint& l, |
| 128 | const uint& range, |
| 129 | const OPENCITY_STRUCTURE_CODE enumStructCode ) const |
| 130 | { |
| 131 | static uint W1, L1, W2, L2; |
| 132 | |
| 133 | // Calculate the possible W1,L1 and W2, L2 |
| 134 | W1 = w; L1 = l; |
| 135 | W2 = w; L2 = l; |
| 136 | _pMapCity->GetPossibleWH( W1, L1, -range, -range ); |
| 137 | _pMapCity->GetPossibleWH( W2, L2, range, range ); |
| 138 | |
| 139 | // Check if the requested structure is within the range |
| 140 | return _pBuildLayer->ContainStructure( |
| 141 | W1, L1, W2, L2, enumStructCode ); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /*======================================================================*/ |
| 146 | const bool |
| 147 | Simulator::CheckLevelUp( |
| 148 | const uint w, |
| 149 | const uint l, |
| 150 | const Structure* pStruct ) const |
| 151 | { |
| 152 | static OPENCITY_GRAPHIC_CODE nextGC, currentGC; |
| 153 | static uint ow = 0, ol = 0, oh = 0, nw = 0, nl = 0, nh = 0; // Old, and New WLH |
| 154 | static uint w2 = 0, l2 = 0; |
| 155 | static uint layerW = 0, layerL = 0; |
| 156 | |
| 157 | // Get the next graphic code. |
| 158 | assert( pStruct != NULL ); // Ya, we need it ! |
| 159 | nextGC = pStruct->GetNextLevelGraphicCode(); |
| 160 | |
| 161 | // IF the next graphic code is the same as the current graphic code THEN ok |
| 162 | currentGC = pStruct->GetGraphicCode(); |
| 163 | if (nextGC == currentGC) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // Get the old WLH |
| 168 | gVars.gpPropertyMgr->GetWLH( |
| 169 | currentGC, |
| 170 | ow, 0, |
| 171 | ol, 0, |
| 172 | oh, 0 ); |
| 173 | assert( ow != 0 ); |
| 174 | |
| 175 | // Get the new WLH |
| 176 | gVars.gpPropertyMgr->GetWLH( |
| 177 | nextGC, |
| 178 | nw, 0, |
| 179 | nl, 0, |
| 180 | nh, 0 ); |
| 181 | assert( nw != 0 ); |
| 182 | |
| 183 | // IF they are the same THEN ok |
| 184 | if ((ow == nw) and (ol == nl) and (oh == nh)) { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | // IF the old ones are bigger THEN shout out |
| 189 | if ((ow > nw) || (ol > nl) || (oh > nh)) { |
| 190 | OPENCITY_DEBUG("The higher level should have bigger dimensions - " |
| 191 | << "oW/oL/oH " << ow << "/" << ol << "/" << oh |
| 192 | << " - nW/nL/nH " << nw << "/" << nl << "/" << nh ); |
| 193 | } |
| 194 | |
| 195 | // IF the new structure is out of the map THEN |
| 196 | w2 = w + nw - 1; |
| 197 | l2 = l + nl - 1; |
| 198 | _pBuildLayer->GetLayerSize( layerW, layerL ); |
| 199 | if ( w2 >= layerW || l2 >= layerL ) { |
| 200 | // OPENCITY_DEBUG( "Sorry, it's out of the map !" ); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // IF the new surface is not plane THEN return false? |
| 205 | if (!gVars.gpMapMgr->IsSurfacePlane( w, l, w2, l2 )) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // The old WLH are smaller |
| 210 | // IF the new required surface does not contains only the required structure THEN |
| 211 | if (_pBuildLayer->ContainStructureOnly( |
| 212 | w, l, w2, l2, pStruct->GetCode()) == false ) { |
| 213 | // OPENCITY_DEBUG( "ContainStructureOnly failed - W/L/W2/L2 " |
| 214 | // << w << "/" << l << "/" << w2 << "/" << l2 ); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | // OK, next level has passed all the tests |
| 219 | OPENCITY_DEBUG( "CheckLevelUp OK - W/L/Level - " << w << "/" << l << "/" << pStruct->GetLevel() ); |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /*======================================================================*/ |
| 225 | const bool |
| 226 | Simulator::CheckLevelDown( |
| 227 | const uint w, |
| 228 | const uint l, |
| 229 | const Structure* pStruct ) const |
| 230 | { |
| 231 | static OPENCITY_GRAPHIC_CODE prevGC, currentGC; |
| 232 | static uint ow = 0, ol = 0, oh = 0, nw = 0, nl = 0, nh = 0; // Old, and New WLH |
| 233 | static uint w2 = 0, l2 = 0; |
| 234 | static uint layerW = 0, layerL = 0; |
| 235 | |
| 236 | // Get the previous graphic code. |
| 237 | assert( pStruct != NULL ); // Ya, we need it ! |
| 238 | prevGC = pStruct->GetPreviousLevelGraphicCode(); |
| 239 | |
| 240 | // IF it's the same THEN ok |
| 241 | currentGC = pStruct->GetGraphicCode(); |
| 242 | if (prevGC == currentGC ) { |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | // Get the old WLH |
| 247 | gVars.gpPropertyMgr->GetWLH( |
| 248 | currentGC, |
| 249 | ow, 0, |
| 250 | ol, 0, |
| 251 | oh, 0 ); |
| 252 | assert( ow != 0 ); |
| 253 | |
| 254 | // Get the new WLH |
| 255 | gVars.gpPropertyMgr->GetWLH( |
| 256 | prevGC, |
| 257 | nw, 0, |
| 258 | nl, 0, |
| 259 | nh, 0 ); |
| 260 | assert( nw != 0 ); |
| 261 | |
| 262 | // IF they are the same THEN ok |
| 263 | if ((ow == nw) and (ol == nl) and (oh == nh)) { |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | // IF the old ones are smaller THEN shout out |
| 268 | if ((ow < nw) || (ol < nl) || (oh < nh)) { |
| 269 | OPENCITY_DEBUG("The lower level should have smaller dimensions"); |
| 270 | } |
| 271 | |
| 272 | // IF the new structure is out of the map THEN |
| 273 | w2 = w + nw - 1; |
| 274 | l2 = l + nl - 1; |
| 275 | _pBuildLayer->GetLayerSize( layerW, layerL ); |
| 276 | if ( w2 >= layerW || l2 >= layerL ) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // IF the new surface is not plane THEN return false? |
| 281 | if (!gVars.gpMapMgr->IsSurfacePlane( w, l, w2, l2 )) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // The old WLH are bigger |
| 286 | // IF the new required surface does not contains only the required structure THEN |
| 287 | if (_pBuildLayer->ContainStructureOnly( |
| 288 | w, l, w2, l2, |
| 289 | pStruct->GetCode()) == false ) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // OK, next level has passed all the tests |
| 294 | OPENCITY_DEBUG("CheckLevelDown OK"); |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /*======================================================================*/ |
| 300 | const int& |
| 301 | Simulator::GetVariation() const |
| 302 | { |
| 303 | return _iVariation; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /*======================================================================*/ |
| 308 | const int |
| 309 | Simulator::GetValue() const |
| 310 | { |
| 311 | return _iValue; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /*======================================================================*/ |
| 316 | const int |
| 317 | Simulator::GetMaxValue() const |
| 318 | { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /*======================================================================*/ |
| 324 | void |
| 325 | Simulator::SetVariation( |
| 326 | const int rciVariation ) |
| 327 | { |
| 328 | _iVariation = rciVariation; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /*======================================================================*/ |
| 333 | void |
| 334 | Simulator::SetValue( |
| 335 | const int rciValue ) |
| 336 | { |
| 337 | _iValue = rciValue; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /*=====================================================================*/ |
| 342 | /* STATIC METHODS */ |
| 343 | /*=====================================================================*/ |
| 344 | int |
| 345 | Simulator::ThreadWrapper( |
| 346 | void* pSim ) |
| 347 | { |
| 348 | return ((Simulator*)pSim)->Main(); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /*======================================================================*/ |
| 353 | void |
| 354 | Simulator::RCIDelay( void ) |
| 355 | { |
| 356 | // Added +1 to avoid log10(0) |
| 357 | SDL_Delay((uint) |
| 358 | (gVars.gfMsSimDelayMax - log10((OC_FLOAT)Structure::GetNumber() + 1) |
| 359 | *OC_MS_STRUCTURE_LOG_FACTOR ) |
| 360 | ); |
| 361 | } |