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

pathstructure.cpp

00001 /***************************************************************************
00002                         pathstructure.cpp  -  description
00003                             -------------------
00004     begin                : june 17th, 2003
00005     copyright            : (C) 2003-2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007     
00008     $Id: pathstructure.cpp 77 2006-11-01 18:27:53Z neoneurone $
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   any later version.                                                    *
00017  *                                                                         *
00018  ***************************************************************************/
00019 
00020 #include "pathstructure.h"
00021 
00022 #include "globalvar.h"
00023 extern GlobalVar gVars;
00024 
00025 
00026    /*======================================================================*/
00027 PathStructure::PathStructure():
00028 Structure(),
00029 ubNumberNeighbour( 0 ),
00030 ubTraffic( 0 )
00031 {
00032     OPENCITY_DEBUG( "ctor" );
00033 }
00034 
00035 
00036    /*======================================================================*/
00037 PathStructure::PathStructure
00038 (
00039     const OPENCITY_STRUCTURE_CODE & enumStructCode
00040 ):
00041 Structure( enumStructCode ),
00042 ubNumberNeighbour( 0 ),
00043 ubTraffic( 0 )
00044 {
00045     OPENCITY_DEBUG( "ctor param" );
00046 
00047     _eGC = gVars.gpPropertyMgr->GetGC( enumStructCode );
00048     _eType = gVars.gpPropertyMgr->GetST( enumStructCode );
00049 
00050 //debug
00051 //cout << "struct: " << (int)enumStructCode
00052 //     << "/ graphic: " << (int)_eGC << endl;
00053 
00054 }
00055 
00056 
00057    /*======================================================================*/
00058 PathStructure::~PathStructure()
00059 {
00060     OPENCITY_DEBUG( "dtor" );
00061 }
00062 
00063 
00064    /*======================================================================*/
00065 void
00066 PathStructure::SaveTo( std::fstream& rfs )
00067 {
00068     Structure::SaveTo( rfs );
00069 
00070     rfs << (uint)ubNumberNeighbour << std::ends;
00071     rfs << (uint)ubTraffic << std::ends;
00072 }
00073 
00074 
00075    /*======================================================================*/
00076 void
00077 PathStructure::LoadFrom( std::fstream& rfs )
00078 {
00079     Structure::LoadFrom( rfs );
00080     uint temp = 0;
00081 
00082     rfs >> temp; rfs.ignore(); ubNumberNeighbour = (OC_UBYTE)temp; 
00083     rfs >> temp; rfs.ignore(); ubTraffic         = (OC_UBYTE)temp; 
00084 }
00085 
00086 
00087    /*======================================================================*/
00088 void
00089 PathStructure::AddNeighbour(
00090     const PathStructure & rcNeighbour,
00091     const OPENCITY_DIRECTION & enumDir )
00092 {
00093     OPENCITY_GRAPHIC_CODE baseGC;
00094     uint newGC;
00095 
00096 //debug cout << "adding 1 neighbour" << endl;
00097 
00098     switch (_eSC) {
00099         case OC_STRUCTURE_ROAD:
00100             baseGC = OC_ROAD_O_N;
00101             break;
00102         case OC_STRUCTURE_ELINE:
00103             baseGC = OC_ELINE_O_N;
00104             break;
00105         default:
00106             OPENCITY_DEBUG( "Game design ERROR" );
00107             break;
00108     }
00109 
00110 // We will update the _eGC with the newGC value
00111 // when we return, so we keep with this
00112     newGC = _eGC;
00113 
00114     if (ubNumberNeighbour == 0) {
00115     // The current _eGC is already OC_ROAD_O_N we don't process further in such case
00116         if (enumDir == OC_DIR_E)
00117             newGC = baseGC + 1;
00118         else if (enumDir == OC_DIR_S)
00119             newGC = baseGC + 2;
00120         else if (enumDir == OC_DIR_W)
00121             newGC = baseGC + 3;
00122     } else
00123 
00124         /*-----------------------------------------*/
00125     if (ubNumberNeighbour == 1) {
00126 //debug cout << "number neighbour == 1" << endl;
00127         if (enumDir == OC_DIR_N) {
00128         // We don't process the case OC_ROAD_O_N
00129             if (_eGC == baseGC + 1)
00130                 newGC = baseGC + 6;
00131             else if (_eGC == baseGC + 2)
00132                 newGC = baseGC + 4;
00133             else if (_eGC == baseGC + 3)
00134                 newGC = baseGC + 7;
00135             else
00136                 return;
00137         } else
00138 
00139         if (enumDir == OC_DIR_E) {
00140            // we don't process the case OC_ROAD_O_E
00141             if (_eGC == baseGC)
00142                 newGC = baseGC + 6;
00143             else if (_eGC == baseGC + 2)
00144                 newGC = baseGC + 8;
00145             else if (_eGC == baseGC + 3)
00146                 newGC = baseGC + 5;
00147             else
00148                 return;
00149         } else
00150 
00151         if (enumDir == OC_DIR_S) {
00152            // we don't process the case OC_ROAD_O_S
00153             if (_eGC == baseGC)
00154                 newGC = baseGC + 4;
00155             else if (_eGC == baseGC + 1)
00156                 newGC = baseGC + 8;
00157             else if (_eGC == baseGC + 3)
00158                 newGC = baseGC + 9;
00159             else
00160                 return;
00161         } else
00162 
00163         if (enumDir == OC_DIR_W) {
00164            // we don't process the case OC_ROAD_O_W
00165             if (_eGC == baseGC)
00166                 newGC = baseGC + 7;
00167             else if (_eGC == baseGC + 1)
00168                 newGC = baseGC + 5;
00169             else if (_eGC == baseGC + 2)
00170                 newGC = baseGC + 9;
00171             else
00172                 return;
00173         }
00174     } else
00175 
00176         //-----------------------------------------
00177     if (ubNumberNeighbour == 2) {
00178         if (enumDir == OC_DIR_N) {
00179         // We don't process the case OC_ROAD_S_N, N_E, N_W
00180             if (_eGC == baseGC + 8)         // S_E
00181                 newGC = baseGC + 10;        // S_N_E
00182             else if (_eGC == baseGC + 9)    // S_W
00183                 newGC = baseGC + 12;        // S_N_W
00184             else if (_eGC == baseGC + 5)    // W_E
00185                 newGC = baseGC + 13;        // N_W_E
00186             else
00187                 return;
00188         } else
00189 
00190         if (enumDir == OC_DIR_E) {
00191         // We don't process the case OC_ROAD_W_E, N_E, S_E
00192             if (_eGC == baseGC + 9)
00193                 newGC = baseGC + 11;
00194             else if (_eGC == baseGC + 7)
00195                 newGC = baseGC + 13;
00196             else if (_eGC == baseGC + 4)
00197                 newGC = baseGC + 10;
00198             else
00199                 return;
00200         } else
00201 
00202         if (enumDir == OC_DIR_S) {
00203         // We don't process the case OC_ROAD_S_N, S_E, S_W
00204             if (_eGC == baseGC + 6)
00205                 newGC = baseGC + 10;
00206             else if (_eGC == baseGC + 7)
00207                 newGC = baseGC + 12;
00208             else if (_eGC == baseGC + 5)
00209                 newGC = baseGC + 11;
00210             else
00211                 return;
00212         } else
00213 
00214         if (enumDir == OC_DIR_W) {
00215         // We don't process the case OC_ROAD_W_E, N_W, S_W
00216             if (_eGC == baseGC + 8)
00217                 newGC = baseGC + 11;
00218             else if (_eGC == baseGC + 6)
00219                 newGC = baseGC + 13;
00220             else if (_eGC == baseGC + 4)
00221                 newGC = baseGC + 12;
00222             else
00223                 return;
00224         }
00225     } else
00226 
00227         //-----------------------------------------
00228     if (ubNumberNeighbour == 3) {
00229         if (enumDir == OC_DIR_N) {
00230             if (_eGC == baseGC + 11)
00231                 newGC = baseGC + 14;
00232             else
00233                 return;
00234         } else
00235         if (enumDir == OC_DIR_E) {
00236             if (_eGC == baseGC + 12)
00237                 newGC = baseGC + 14;
00238             else
00239                 return;
00240         } else
00241         if (enumDir == OC_DIR_S) {
00242             if (_eGC == baseGC + 13)
00243                 newGC = baseGC + 14;
00244             else
00245                 return;
00246         } else
00247         if (enumDir == OC_DIR_W) {
00248             if (_eGC == baseGC + 10)
00249                 newGC = baseGC + 14;
00250             else
00251                 return;
00252         }
00253     }
00254     else {
00255     // impossible case !
00256         OPENCITY_FATAL( "Internal game design error !" );
00257         exit(-1);
00258         return;
00259     }
00260 
00261     _eGC = (OPENCITY_GRAPHIC_CODE)newGC;
00262     ubNumberNeighbour++;
00263 
00264 //debug
00265 //cout << "newGC: " << (int) newGC
00266 //     << " / nNeighbours: " << (int)ubNumberNeighbour << endl;
00267 //debug end
00268 }
00269 
00270 
00271    /*=====================================================================*/
00272 void
00273 PathStructure::RemoveNeighbour(
00274     const PathStructure & rcNeighbour,
00275     const OPENCITY_DIRECTION & enumDir )
00276 {
00277     OPENCITY_GRAPHIC_CODE baseGC;
00278     uint newGC;
00279 
00280    // note: we do nothing when number of neighbours is 0
00281     if (ubNumberNeighbour == 0) {
00282         return;
00283     }
00284 
00285     switch (_eSC) {
00286         case OC_STRUCTURE_ROAD:
00287             baseGC = OC_ROAD_O_N;
00288             break;
00289         case OC_STRUCTURE_ELINE:
00290             baseGC = OC_ELINE_O_N;
00291             break;
00292         default:
00293             cout << "WARNING: game design error" << endl;
00294             break;
00295     }
00296 
00297    // we will update the _eGC with the newGC value
00298    // when we return, so we keep with this
00299     newGC = _eGC;
00300 
00301     if (ubNumberNeighbour == 1) {
00302        // we have no neighbour so the default path structure
00303        // look to the NORTH
00304         newGC = baseGC;
00305     } else
00306 
00307         //-----------------------------------------
00308 
00309     if (ubNumberNeighbour == 2) {
00310 //debug cout << "number neighbour == 1" << endl;
00311         if (enumDir == OC_DIR_N) {
00312             if (_eGC == baseGC + 4)
00313                 newGC = baseGC + 2;
00314             else if (_eGC == baseGC + 6)
00315                 newGC = baseGC + 1;
00316             else if (_eGC == baseGC + 7)
00317                 newGC = baseGC + 3;
00318             else
00319                 return;
00320         } else
00321 
00322         if (enumDir == OC_DIR_E) {
00323             if (_eGC == baseGC + 6)
00324                 newGC = baseGC;
00325             else if (_eGC == baseGC + 8)
00326                 newGC = baseGC + 2;
00327             else if (_eGC == baseGC + 5)
00328                 newGC = baseGC + 3;
00329             else
00330                 return;
00331         } else
00332 
00333         if (enumDir == OC_DIR_S) {
00334             if (_eGC == baseGC + 4)
00335                 newGC = baseGC;
00336             else if (_eGC == baseGC + 8)
00337                 newGC = baseGC + 1;
00338             else if (_eGC == baseGC + 9)
00339                 newGC = baseGC + 3;
00340             else
00341                 return;
00342         } else
00343 
00344         if (enumDir == OC_DIR_W) {
00345             if (_eGC == baseGC + 7)
00346                 newGC = baseGC;
00347             else if (_eGC == baseGC + 5)
00348                 newGC = baseGC + 1;
00349             else if (_eGC == baseGC + 9)
00350                 newGC = baseGC + 2;
00351             else
00352                 return;
00353         }
00354     } else
00355 
00356         //-----------------------------------------
00357     if (ubNumberNeighbour == 3) {
00358         if (enumDir == OC_DIR_N) {
00359             if (_eGC == baseGC + 10)
00360                 newGC = baseGC + 8;
00361             else if (_eGC == baseGC + 12)
00362                 newGC = baseGC + 9;
00363             else if (_eGC == baseGC + 13)
00364                 newGC = baseGC + 5;
00365             else
00366                 return;
00367         } else
00368 
00369         if (enumDir == OC_DIR_E) {
00370             if (_eGC == baseGC + 11)
00371                 newGC = baseGC + 9;
00372             else if (_eGC == baseGC + 13)
00373                 newGC = baseGC + 7;
00374             else if (_eGC == baseGC + 10)
00375                 newGC = baseGC + 4;
00376             else
00377                 return;
00378         } else
00379 
00380         if (enumDir == OC_DIR_S) {
00381             if (_eGC == baseGC + 10)
00382                 newGC = baseGC + 6;
00383             else if (_eGC == baseGC + 12)
00384                 newGC = baseGC + 7;
00385             else if (_eGC == baseGC + 11)
00386                 newGC = baseGC + 5;
00387             else
00388                 return;
00389         } else
00390 
00391         if (enumDir == OC_DIR_W) {
00392             if (_eGC == baseGC + 11)
00393                 newGC = baseGC + 8;
00394             else if (_eGC == baseGC + 13)
00395                 newGC = baseGC + 6;
00396             else if (_eGC == baseGC + 12)
00397                 newGC = baseGC + 4;
00398             else
00399                 return;
00400         }
00401     } else
00402 
00403         //-----------------------------------------
00404     if (ubNumberNeighbour == 4) {
00405         if (enumDir == OC_DIR_N) {
00406             newGC = baseGC + 11;
00407         } else
00408         if (enumDir == OC_DIR_E) {
00409             newGC = baseGC + 12;
00410         } else
00411         if (enumDir == OC_DIR_S) {
00412             newGC = baseGC + 13;
00413         } else
00414         if (enumDir == OC_DIR_W) {
00415             newGC = baseGC + 10;
00416         }
00417     }
00418 
00419     _eGC = (OPENCITY_GRAPHIC_CODE)newGC;
00420     ubNumberNeighbour--;
00421 }
00422 
00423 
00424    /*======================================================================*/
00425 // Old code, kept for future reference
00426 /*
00427 void
00428 pathstructureAddNeighbour2(
00429     const PathStructure & rcNeighbour,
00430     const OPENCITY_DIRECTION & enumDir )
00431 {
00432 //debug
00433 cout << "adding 1 neighbour" << endl;
00434 
00435     if (ubNumberNeighbour == 0) {
00436        // the current _eGC is already OC_ROAD_O_N
00437        // we don't process further in such case
00438         if (enumDir == OC_DIR_E)
00439             _eGC = OC_ROAD_O_E;
00440         else if (enumDir == OC_DIR_S)
00441             _eGC = OC_ROAD_O_S;
00442         else if (enumDir == OC_DIR_W)
00443             _eGC = OC_ROAD_O_W;
00444     } else
00445 
00446         *-----------------------------------------*
00447     if (ubNumberNeighbour == 1) {
00448 //debug
00449 cout << "number neighbour == 1" << endl;
00450         if (enumDir == OC_DIR_N) {
00451            // we don't process the case OC_ROAD_O_N
00452             if (_eGC == OC_ROAD_O_E)
00453                 _eGC = OC_ROAD_N_E;
00454             else if (_eGC == OC_ROAD_O_S)
00455                 _eGC = OC_ROAD_S_N;
00456             else if (_eGC == OC_ROAD_O_W)
00457                 _eGC = OC_ROAD_N_W;
00458             else
00459                 return;
00460         } else
00461 
00462         if (enumDir == OC_DIR_E) {
00463            // we don't process the case OC_ROAD_O_E
00464             if (_eGC == OC_ROAD_O_N)
00465                 _eGC = OC_ROAD_N_E;
00466             else if (_eGC == OC_ROAD_O_S)
00467                 _eGC = OC_ROAD_S_E;
00468             else if (_eGC == OC_ROAD_O_W)
00469                 _eGC = OC_ROAD_W_E;
00470             else
00471                 return;
00472         } else
00473 
00474         if (enumDir == OC_DIR_S) {
00475            // we don't process the case OC_ROAD_O_S
00476             if (_eGC == OC_ROAD_O_N)
00477                 _eGC = OC_ROAD_S_N;
00478             else if (_eGC == OC_ROAD_O_E)
00479                 _eGC = OC_ROAD_S_E;
00480             else if (_eGC == OC_ROAD_O_W)
00481                 _eGC = OC_ROAD_S_W;
00482             else
00483                 return;
00484         } else
00485 
00486         if (enumDir == OC_DIR_W) {
00487            // we don't process the case OC_ROAD_O_W
00488             if (_eGC == OC_ROAD_O_N)
00489                 _eGC = OC_ROAD_N_W;
00490             else if (_eGC == OC_ROAD_O_E)
00491                 _eGC = OC_ROAD_W_E;
00492             else if (_eGC == OC_ROAD_O_S)
00493                 _eGC = OC_ROAD_S_W;
00494             else
00495                 return;
00496         }
00497     } else
00498 
00499         *-----------------------------------------*
00500     if (ubNumberNeighbour == 2) {
00501         if (enumDir == OC_DIR_N) {
00502            // we don't process the case OC_ROAD_S_N, N_E, N_W
00503             if (_eGC == OC_ROAD_S_E)
00504                 _eGC = OC_ROAD_S_N_E;
00505             else if (_eGC == OC_ROAD_S_W)
00506                 _eGC = OC_ROAD_S_W_E;
00507             else if (_eGC == OC_ROAD_W_E)
00508                 _eGC = OC_ROAD_N_W_E;
00509             else
00510                 return;
00511         } else
00512 
00513         if (enumDir == OC_DIR_E) {
00514            // we don't process the case OC_ROAD_W_E, N_E, S_E
00515             if (_eGC == OC_ROAD_S_W)
00516                 _eGC = OC_ROAD_S_W_E;
00517             else if (_eGC == OC_ROAD_N_W)
00518                 _eGC = OC_ROAD_N_W_E;
00519             else if (_eGC == OC_ROAD_S_N)
00520                 _eGC = OC_ROAD_S_N_E;
00521             else
00522                 return;
00523         } else
00524 
00525         if (enumDir == OC_DIR_S) {
00526            // we don't process the case OC_ROAD_S_N, S_E, S_W
00527             if (_eGC == OC_ROAD_N_E)
00528                 _eGC = OC_ROAD_S_N_E;
00529             else if (_eGC == OC_ROAD_N_W)
00530                 _eGC = OC_ROAD_S_N_W;
00531             else if (_eGC == OC_ROAD_W_E)
00532                 _eGC = OC_ROAD_S_W_E;
00533             else
00534                 return;
00535         } else
00536 
00537         if (enumDir == OC_DIR_W) {
00538            // we don't process the case OC_ROAD_W_E, N_W, S_W
00539             if (_eGC == OC_ROAD_S_E)
00540                 _eGC = OC_ROAD_S_W_E;
00541             else if (_eGC == OC_ROAD_N_E)
00542                 _eGC = OC_ROAD_N_W_E;
00543             else if (_eGC == OC_ROAD_S_N)
00544                 _eGC = OC_ROAD_S_N_W;
00545             else
00546                 return;
00547         }
00548     } else
00549 
00550         *-----------------------------------------*
00551     if (ubNumberNeighbour == 3) {
00552         if (enumDir == OC_DIR_N) {
00553             if (_eGC == OC_ROAD_S_W_E)
00554                 _eGC = OC_ROAD_S_N_W_E;
00555             else
00556                 return;
00557         } else
00558         if (enumDir == OC_DIR_E) {
00559             if (_eGC == OC_ROAD_S_N_W)
00560                 _eGC = OC_ROAD_S_N_W_E;
00561             else
00562                 return;
00563         } else
00564         if (enumDir == OC_DIR_S) {
00565             if (_eGC == OC_ROAD_N_W_E)
00566                 _eGC = OC_ROAD_S_N_W_E;
00567             else
00568                 return;
00569         } else
00570         if (enumDir == OC_DIR_W) {
00571             if (_eGC == OC_ROAD_S_N_E)
00572                 _eGC = OC_ROAD_S_N_W_E;
00573             else
00574                 return;
00575         }
00576     }
00577     else {
00578        // impossible case !
00579         cout << "WARNING: internal game design error !" << endl;
00580         return;
00581     }
00582 
00583     ubNumberNeighbour++;
00584 }
00585 */
00586 
00587 
00588    /*======================================================================*/
00589 const OC_UBYTE &
00590 PathStructure::GetTraffic() const
00591 {
00592     return this->ubTraffic;
00593 }
00594 
00595 
00596    /*======================================================================*/
00597 void
00598 PathStructure::SetTraffic(
00599     const OC_UBYTE & rubTraffic )
00600 {
00601     this->ubTraffic = rubTraffic;
00602 }
00603 
00604 
00605    /*======================================================================*/
00606 const uint &
00607 PathStructure::GetLength() const
00608 {
00609     return this->uiLength;
00610 }
00611 
00612 
00613    /*======================================================================*/
00614 void
00615 PathStructure::SetLength(
00616     const uint & rcuiValue )
00617 {
00618     this->uiLength = rcuiValue;
00619 }
00620 
00621 
00622 
00623 
00624 
00625 
00626 
00627 
00628 
00629 
00630 
00631 
00632 
00633 
00634 
00635 
00636 
00637 
00638 
00639 
00640 
00641 
00642 
00643 
00644 
00645 
00646 
00647 
00648 
00649 
00650 
00651 

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