Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

propertymanager.cpp

00001 /***************************************************************************
00002                           propertymanager.cpp  -  description
00003       $Id: propertymanager.cpp 78 2006-11-02 20:38:34Z neoneurone $
00004                              -------------------
00005     begin                : feb 6th, 2004
00006     copyright            : (C) 2004-2006 by Duong-Khang NGUYEN
00007     email                : neoneurone @ users sourceforge net
00008  ***************************************************************************/
00009 
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   any later version.                                                    *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "propertymanager.h"
00020 
00021 #include "structure.h"
00022 
00023 #include <algorithm>        // We use STL algorithms
00024 #include <sstream>          // We use stringstream or data conversion
00025 
00026 
00027    /*=====================================================================*/
00028 PropertyManager::PropertyManager():
00029 vpConf(OC_GRAPHIC_CODE_MAX, NULL)
00030 {
00031     uint i;
00032     string str;
00033     string::size_type pos;
00034     string strAc = string(".ac");
00035     string strConf = string(".conf");
00036     std::stringstream ss;
00037     Conf* pconfFile;
00038 
00039     OPENCITY_DEBUG( "ctor" );
00040 
00041 // read the main config file
00042     Conf* pConf = new Conf();
00043     if (pConf->Open( ocHomeDirPrefix(OC_GRAPHISM_FILE_FILENAME) ) == OC_ERR_FREE) {
00044     // initialize the Conf* vector
00045         for (i = 0; i < OC_GRAPHIC_CODE_MAX; i++ ) {
00046             ss << i;
00047 //debugcout << "Mub: " << ss.str() << " / i: " << i << endl;
00048             str = pConf->GetValue(ss.str(), "");
00049             ss.str("");
00050 
00051         // IF the key is not defined in the config file THEN
00052             if (str != "") {
00053             // Reverse search for the ".ac" file extension
00054                 pos = str.rfind( strAc );
00055                 if (pos != str.npos ) {
00056                 // Replace ".ac" by ".conf"
00057                     str.replace( pos, strConf.size(), strConf );
00058                 }
00059 
00060                 pconfFile = new Conf();
00061                 if (pconfFile->Open( ocHomeDirPrefix(str) ) == OC_ERR_FREE) {
00062 //debug cout << "Loading: " << str.c_str() << " / at: " << i << endl;
00063                     this->vpConf[i] = pconfFile;
00064                 }
00065                 else {
00066                     delete pconfFile;
00067                 }
00068             } // if (str=="")
00069         } // for
00070 
00071         pConf->Close();
00072     }
00073     else {
00074         cerr << "WARNING: Error opening graphism config file." << endl;
00075         abort();
00076     }
00077     delete pConf;
00078 }
00079 
00080 
00081    /*=====================================================================*/
00082 PropertyManager::~PropertyManager()
00083 {
00084     OPENCITY_DEBUG( "dtor" );
00085 
00086 // delete the Conf* vector
00087     for (int i = 0; i < OC_GRAPHIC_CODE_MAX; i++ ) {
00088         if (this->vpConf[i] != NULL) {
00089             this->vpConf[i]->Close();       // Close the file
00090             delete this->vpConf[i];         // NOTE: delete NULL pointer has no effect
00091         }
00092     }
00093 }
00094 
00095 
00096    /*=====================================================================*/
00097 const int
00098 PropertyManager::Get(
00099     const OPENCITY_PROPERTY_CODE & pCode,
00100     const OPENCITY_STRUCTURE_CODE & sCode,
00101     const Structure* pcStruct ) const
00102 {
00103     static int value;
00104 
00105     switch (pCode) {
00106         case OC_BUILD_COST:
00107             switch (sCode) {
00108                 case OC_STRUCTURE_RES:
00109                 case OC_STRUCTURE_COM:
00110                     value = 5; break;
00111                 case OC_STRUCTURE_IND:
00112                     value = 8; break;
00113                 case OC_STRUCTURE_PARK:
00114                     value = 100; break;
00115                 case OC_STRUCTURE_ELINE:
00116                     value = 3; break;
00117                 case OC_STRUCTURE_EPLANT_COAL:
00118                     value = 2000; break;
00119                 case OC_STRUCTURE_FIREDEPT:
00120                 case OC_STRUCTURE_POLICEDEPT:
00121                 case OC_STRUCTURE_HOSPITALDEPT:
00122                 case OC_STRUCTURE_EDUCATIONDEPT:
00123                     value = 1500; break;
00124                 case OC_STRUCTURE_ROAD:
00125                 case OC_STRUCTURE_FLORA:
00126                     value = 5; break;
00127 
00128                 case OC_STRUCTURE_TEST:         // Development test feature
00129                     value = 0; break;
00130                 default:
00131                     value = 0;
00132                     OPENCITY_DEBUG("WARNING: game design error");
00133                     assert( 0 );
00134             }
00135             break;
00136 
00137         case OC_DESTROY_COST:
00138             assert( pcStruct != NULL );
00139             switch (sCode) {
00140                 case OC_STRUCTURE_RES:
00141                 case OC_STRUCTURE_COM:
00142                     value = 2*(pcStruct->GetLevel() + 1); break;
00143                 case OC_STRUCTURE_IND:
00144                     value = 3*(pcStruct->GetLevel() + 1); break;
00145 
00146                 case OC_STRUCTURE_PARK:
00147                     value = 10; break;
00148                 case OC_STRUCTURE_ELINE:
00149                     value = 1; break;
00150                 case OC_STRUCTURE_ROAD:
00151                 case OC_STRUCTURE_FLORA:
00152                     value = 4; break;
00153 
00154                 case OC_STRUCTURE_PART:
00155                 case OC_STRUCTURE_EPLANT_COAL:
00156                 case OC_STRUCTURE_FIREDEPT:
00157                 case OC_STRUCTURE_POLICEDEPT:
00158                 case OC_STRUCTURE_HOSPITALDEPT:
00159                 case OC_STRUCTURE_EDUCATIONDEPT:
00160                     value = 200; break;
00161 
00162                 case OC_STRUCTURE_TEST:         // Development test feature
00163                     value = 0; break;
00164                 default:
00165                     value = 0;
00166                     OPENCITY_DEBUG("WARNING: game design error");
00167                     assert( 0 );
00168             }
00169             break;
00170 
00171         case OC_MAINTENANCE_COST:
00172             switch (sCode) {
00173                 case OC_STRUCTURE_RES:
00174                 case OC_STRUCTURE_COM:
00175                 case OC_STRUCTURE_IND:
00176                 case OC_STRUCTURE_PART:
00177                     value = 0; break;
00178                 case OC_STRUCTURE_PARK:
00179                     value = 1; break;
00180                 case OC_STRUCTURE_ELINE:
00181                     value = 1; break;
00182                 case OC_STRUCTURE_EPLANT_COAL:
00183                     value = 20; break;
00184                 case OC_STRUCTURE_FIREDEPT:
00185                 case OC_STRUCTURE_POLICEDEPT:
00186                 case OC_STRUCTURE_HOSPITALDEPT:
00187                 case OC_STRUCTURE_EDUCATIONDEPT:
00188                     value = 15; break;
00189                 case OC_STRUCTURE_ROAD:
00190                 case OC_STRUCTURE_FLORA:
00191                     value = 1; break;
00192 
00193                 case OC_STRUCTURE_TEST:         // Development test feature
00194                     value = 0; break;
00195                 default:
00196                     value = 0;
00197                     OPENCITY_DEBUG( "WARNING: game design error" );
00198                     assert( 0 );
00199             }
00200             break;
00201 
00202         default:
00203             value = 0;
00204             OPENCITY_DEBUG("WARNING: game design error");
00205             assert( 0 );
00206     }
00207 
00208 /*
00209 #ifndef NDEBUG
00210 // Debug code
00211     if ( value == 0 ) {
00212         cerr << "pCode: " << pCode << "/ sCode: " << sCode 
00213              << "/ pcStruct: " << pcStruct << endl;
00214         assert( 0 );
00215     }
00216 #endif
00217 */
00218 
00219     return value;
00220 }
00221 
00222 
00223    /*=====================================================================*/
00224 const OPENCITY_GRAPHIC_CODE
00225 PropertyManager::GetGC(
00226     const OPENCITY_STRUCTURE_CODE scode ) const
00227 {
00228 // For better readability and performance (use of "static" keyword)
00229     static OPENCITY_GRAPHIC_CODE gcode;
00230 
00231     switch (scode) {
00232         case OC_STRUCTURE_UNDEFINED:
00233             gcode = OC_EMPTY;
00234             break;
00235 
00236     // Multi level graphic code
00237         case OC_STRUCTURE_RES:
00238             gcode = OC_RES_ZONE0;
00239             break;
00240         case OC_STRUCTURE_COM:
00241             gcode = OC_COM_ZONE0;
00242             break;
00243         case OC_STRUCTURE_IND:
00244             gcode = OC_IND_ZONE0;
00245             break;
00246         case OC_STRUCTURE_PARK:
00247             gcode = OC_PARK0;
00248             break;
00249         case OC_STRUCTURE_FLORA:
00250             gcode = OC_TREE_BEGIN;
00251             break;
00252 
00253     // Special path type structure
00254         case OC_STRUCTURE_ROAD:
00255             gcode = OC_ROAD_O_N;
00256             break;
00257         case OC_STRUCTURE_ELINE:
00258             gcode = OC_ELINE_O_N;
00259             break;
00260 
00261     // Single level graphic code
00262         case OC_STRUCTURE_TEST:
00263             gcode = OC_TEST_BUILDING;
00264             break;
00265         case OC_STRUCTURE_PART:
00266             gcode = OC_EMPTY;
00267             break;
00268         case OC_STRUCTURE_EPLANT_COAL:
00269             gcode = OC_EPLANT_COAL_BOX;
00270             break;
00271         case OC_STRUCTURE_FIREDEPT:
00272             gcode = OC_FIRE_DEPT;
00273             break;
00274         case OC_STRUCTURE_POLICEDEPT:
00275             gcode = OC_POLICE_DEPT;
00276             break;
00277         case OC_STRUCTURE_HOSPITALDEPT:
00278             gcode = OC_HOSPITAL_DEPT;
00279             break;
00280         case OC_STRUCTURE_EDUCATIONDEPT:
00281             gcode = OC_EDUCATION_DEPT;
00282             break;
00283 
00284         default:
00285             gcode = OC_EMPTY;
00286             OPENCITY_DEBUG( "WARNING: game design error, unknown structure !" );
00287             assert( 0 );
00288             break;
00289     }
00290 
00291     return gcode;
00292 }
00293 
00294 
00295    /*=====================================================================*/
00296 const OPENCITY_STRUCTURE_TYPE
00297 PropertyManager::GetST(
00298     const OPENCITY_STRUCTURE_CODE scode ) const
00299 {
00300 // For better readability and performance (use of "static" keyword)
00301     static OPENCITY_STRUCTURE_TYPE tcode;
00302 
00303     switch (scode) {
00304         case OC_STRUCTURE_RES:
00305         case OC_STRUCTURE_PARK:
00306             tcode = OC_TYPE_RESIDENCE; break;
00307         case OC_STRUCTURE_COM:
00308             tcode = OC_TYPE_COMMERCE; break;
00309         case OC_STRUCTURE_IND:
00310             tcode = OC_TYPE_INDUSTRY; break;
00311 
00312         case OC_STRUCTURE_EPLANT_COAL:
00313             tcode = OC_TYPE_ELECTRICITY; break;
00314 
00315         case OC_STRUCTURE_ROAD:
00316         case OC_STRUCTURE_ELINE:
00317             tcode = OC_TYPE_PATH; break;
00318 
00319         case OC_STRUCTURE_FIREDEPT:
00320         case OC_STRUCTURE_POLICEDEPT:
00321         case OC_STRUCTURE_HOSPITALDEPT:
00322         case OC_STRUCTURE_MILITARYDEPT:
00323         case OC_STRUCTURE_EDUCATIONDEPT:
00324             tcode = OC_TYPE_GOVERNMENT; break;
00325 
00326         case OC_STRUCTURE_FLORA:
00327             tcode = OC_TYPE_TREE; break;
00328 
00329         case OC_STRUCTURE_PART:
00330             tcode = OC_TYPE_UNUSED; break;
00331 
00332     // TODO
00333         case OC_STRUCTURE_UNDEFINED:
00334         case OC_STRUCTURE_ANY:
00335         case OC_STRUCTURE_ELECTRIC:
00336             tcode = OC_TYPE_UNUSED; assert( 0 ); break;
00337 
00338         case OC_STRUCTURE_TEST:         // Development test feature
00339             tcode = OC_TYPE_UNUSED; break;
00340         default:
00341             tcode = OC_TYPE_UNUSED;
00342             OPENCITY_DEBUG( "WARNING: game design error" );
00343             assert( 0 );
00344     }
00345 
00346     return tcode;
00347 }
00348 
00349 
00350    /*=====================================================================*/
00351 void
00352 PropertyManager::GetWLH(
00353     const OPENCITY_GRAPHIC_CODE & gcode,
00354     uint & w, const uint defw,
00355     uint & l, const uint defl,
00356     uint & h, const uint defh)
00357 {
00358     static OC_LINT lint;
00359 
00360     propertymanagerGetLint( gcode, "width", lint, defw );
00361     w = lint;
00362     propertymanagerGetLint( gcode, "length", lint, defl );
00363     l = lint;
00364     propertymanagerGetLint( gcode, "height", lint, defh );
00365     h = lint;
00366 
00367 /* debug
00368     cout << "code: " << gcode
00369          << "/ w: " << w
00370          << "/ l: " << l
00371          << "/ h: " << h << endl;
00372 */
00373 }
00374 
00375 
00376 
00377 
00378 
00379    /*=====================================================================*/
00380    /*                           PRIVATE METHODS                           */
00381    /*=====================================================================*/
00382 const string
00383 PropertyManager::propertymanagerGetStr(
00384     const OPENCITY_GRAPHIC_CODE & gcode,
00385     const string & key,
00386     const string & def)
00387 {
00388     return "";
00389 }
00390 
00391 
00392    /*=====================================================================*/
00393 const OPENCITY_ERR_CODE
00394 PropertyManager::propertymanagerGetLint(
00395     const OPENCITY_GRAPHIC_CODE & gcode,
00396     const string & key,
00397     OC_LINT & rlint,
00398     const OC_LINT & def)
00399 {
00400 // Avoid developers' mistakes
00401     assert( (uint)gcode < this->vpConf.size() );
00402     assert( this->vpConf[gcode] != NULL );
00403 
00404     return this->vpConf[gcode]->GetLint( key, rlint, def );
00405 }
00406 
00407 
00408 
00409 
00410 
00411 
00412 
00413 
00414 
00415 
00416 
00417 
00418 
00419 
00420 
00421 
00422 
00423 
00424 
00425 
00426 
00427 
00428 
00429 
00430 
00431 
00432 
00433 

Generated on Sat Nov 11 10:21:10 2006 for OpenCity by  doxygen 1.4.2