| 1 | /*************************************************************************** |
| 2 | simulator.h - 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.h 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 | #ifndef _OPENCITY_SIMULATOR_H_ |
| 21 | #define _OPENCITY_SIMULATOR_H_ 1 |
| 22 | |
| 23 | #include "main.h" |
| 24 | #include "persistence.h" |
| 25 | |
| 26 | // the maximum range of other structures |
| 27 | #define OC_R_C_RANGE 4 |
| 28 | #define OC_R_I_RANGE 7 |
| 29 | #define OC_R_P_RANGE 3 |
| 30 | |
| 31 | #define OC_C_R_RANGE 4 |
| 32 | #define OC_C_I_RANGE 8 |
| 33 | #define OC_C_P_RANGE 2 |
| 34 | |
| 35 | #define OC_I_R_RANGE 7 |
| 36 | #define OC_I_C_RANGE 8 |
| 37 | #define OC_I_P_RANGE 4 |
| 38 | |
| 39 | #define OC_E_E_RANGE 1 // for electricity simulation |
| 40 | #define OC_E_RCI_RANGE 2 |
| 41 | |
| 42 | #define OC_P_RCIP_RANGE 2 // for traffic simulation |
| 43 | |
| 44 | // The simulators' contants. |
| 45 | // Bad values can block your system, don't change them ! |
| 46 | #define OC_SIMULATOR_UP 70 ///< 70% of levelup will be done 30% left mean leveldown |
| 47 | #define OC_SIMULATOR_DOWN 40 ///< 40% of leveldown will be done only |
| 48 | |
| 49 | // The power you get when you build it |
| 50 | #define OC_EPLANT_COAL_POWER 20000 |
| 51 | #define OC_EPLANT_NUCLEAR_POWER 50000 |
| 52 | |
| 53 | #define OC_DEPT_POWER_CONSUMPTION 50 ///< Power comsumption of fire, police, school and hospital departments |
| 54 | |
| 55 | |
| 56 | enum OPENCITY_STRUCTURE_CODE; |
| 57 | class BuildingLayer; |
| 58 | class Map; |
| 59 | class Structure; |
| 60 | |
| 61 | |
| 62 | //======================================================================== |
| 63 | /** The class from which all the simulators derive. It contains common |
| 64 | tests used by the micro simulators |
| 65 | */ |
| 66 | class Simulator : public Persistence |
| 67 | { |
| 68 | |
| 69 | public: |
| 70 | |
| 71 | //======================================================================== |
| 72 | /** Each enumeration corresponds to a specific micro simulator |
| 73 | */ |
| 74 | enum OPENCITY_SIMULATOR { |
| 75 | OC_RESIDENTIAL = 0, ///< Residential micro simulator |
| 76 | OC_COMMERCIAL, ///< Commercial micro simulator |
| 77 | OC_INDUSTRIAL, ///< Industrial micro simulator |
| 78 | OC_ELECTRIC, ///< Electric micro simulator |
| 79 | OC_TRAFFIC, ///< Traffic micro simulator |
| 80 | OC_SIMULATOR_NUMBER, ///< The number of micro simulators that we have |
| 81 | OC_SIMULATOR_DEFAULT |
| 82 | }; |
| 83 | |
| 84 | enum SIMULATOR_STATE { |
| 85 | SIMULATOR_RUNNING, |
| 86 | SIMULATOR_STOPED, |
| 87 | SIMULATOR_RETURN |
| 88 | }; |
| 89 | |
| 90 | Simulator(); ///< do NOT use this |
| 91 | Simulator( |
| 92 | SDL_mutex* mutex, |
| 93 | BuildingLayer* pblayer, |
| 94 | Map* pmap ); |
| 95 | virtual ~Simulator(); |
| 96 | |
| 97 | |
| 98 | //======================================================================== |
| 99 | /** Save the data to the specified fstream |
| 100 | \param rfs A reference to a file stream which is ready for writing |
| 101 | */ |
| 102 | void |
| 103 | SaveTo( std::fstream& rfs ); |
| 104 | |
| 105 | |
| 106 | //======================================================================== |
| 107 | /** Load the data from the specified stream |
| 108 | \param rfs A reference to a file stream which is ready for reading |
| 109 | */ |
| 110 | void |
| 111 | LoadFrom( std::fstream& rfs ); |
| 112 | |
| 113 | |
| 114 | virtual int |
| 115 | Main() = 0; |
| 116 | |
| 117 | |
| 118 | virtual void |
| 119 | AddStructure( |
| 120 | const uint & w1, |
| 121 | const uint & h1, |
| 122 | const uint & w2, |
| 123 | const uint & h2 ) {} |
| 124 | |
| 125 | virtual void |
| 126 | RemoveStructure( |
| 127 | const uint & w1, |
| 128 | const uint & h1, |
| 129 | const uint & w2, |
| 130 | const uint & h2 ) {} |
| 131 | |
| 132 | |
| 133 | void |
| 134 | Run(); |
| 135 | |
| 136 | void |
| 137 | Stop(); |
| 138 | |
| 139 | void |
| 140 | Return(); |
| 141 | |
| 142 | |
| 143 | //======================================================================== |
| 144 | /** Given the (w, l) coordinates and a range in integer, this method |
| 145 | return "true" if there's at least one required structure within |
| 146 | the range and "false" otherwise |
| 147 | */ |
| 148 | const bool |
| 149 | CheckRange( |
| 150 | const uint & w, |
| 151 | const uint & l, |
| 152 | const uint & range, |
| 153 | const OPENCITY_STRUCTURE_CODE enumStructCode ) const; |
| 154 | |
| 155 | |
| 156 | //======================================================================== |
| 157 | /** Check if the structure at the specified coodinates can "level up". |
| 158 | Mainly, it consists of structure's size checking |
| 159 | */ |
| 160 | const bool |
| 161 | CheckLevelUp( |
| 162 | const uint w, |
| 163 | const uint l, |
| 164 | const Structure* pStruct ) const; |
| 165 | |
| 166 | |
| 167 | //======================================================================== |
| 168 | /** Check if the structure at the specified coodinates can "level down" |
| 169 | Mainly, it consists of structure's size checking |
| 170 | */ |
| 171 | const bool |
| 172 | CheckLevelDown( |
| 173 | const uint w, |
| 174 | const uint l, |
| 175 | const Structure* pStruct ) const; |
| 176 | |
| 177 | |
| 178 | const int & |
| 179 | GetVariation() const; |
| 180 | |
| 181 | |
| 182 | //======================================================================== |
| 183 | /** Return the current value of the micro simulator |
| 184 | */ |
| 185 | const int |
| 186 | GetValue() const; |
| 187 | |
| 188 | |
| 189 | //======================================================================== |
| 190 | /** Return the possible max value of the micro simulator. Currently, |
| 191 | this method works only on the electricity micro simulator. |
| 192 | */ |
| 193 | virtual const int |
| 194 | GetMaxValue() const; |
| 195 | |
| 196 | |
| 197 | void |
| 198 | SetVariation( |
| 199 | const int rcVariation ); |
| 200 | |
| 201 | |
| 202 | void |
| 203 | SetValue( |
| 204 | const int rcValue ); |
| 205 | |
| 206 | |
| 207 | /*=====================================================================*/ |
| 208 | /* STATIC METHODS */ |
| 209 | /*=====================================================================*/ |
| 210 | static int |
| 211 | ThreadWrapper( |
| 212 | void* pSim ); |
| 213 | |
| 214 | |
| 215 | //======================================================================== |
| 216 | /** Each RCI (residential, commercial, industrial) micro simulator should |
| 217 | call this at the end of each treatement so that the main thread can |
| 218 | execute. |
| 219 | */ |
| 220 | static void |
| 221 | RCIDelay( void ); |
| 222 | |
| 223 | |
| 224 | protected: |
| 225 | int _iVariation; ///< The average variation of the structures stimulated |
| 226 | int _iValue; ///< The current global value of the simulator |
| 227 | |
| 228 | SIMULATOR_STATE _eSimState; ///< The current state of the simulator |
| 229 | SDL_mutex* _pMutexMain; ///< The global mutex |
| 230 | BuildingLayer* _pBuildLayer; ///< The building layer |
| 231 | Map* _pMapCity; ///< The city map |
| 232 | |
| 233 | static volatile int _tiVariation[Simulator::OC_SIMULATOR_NUMBER]; |
| 234 | }; |
| 235 | |
| 236 | #endif |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 |