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

map.cpp

00001 /***************************************************************************
00002                             map.cpp  -  description
00003                                 -------------------
00004     begin                : january 24th, 2004
00005     copyright            : (C) 2004-2006 Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007 
00008     $Id: map.cpp 83 2006-11-05 20:46:42Z 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 
00021 #include "map.h"
00022 #include "layer.h"
00023 
00024 #include "mapmaker.h"           // Map generator
00025 
00026 
00027    /*=====================================================================*/
00028 Map::Map(
00029     const uint width,
00030     const uint height ):
00031 _uiMapWidth( width ),
00032 _uiMapHeight( height ),
00033 _pclayer( NULL )
00034 {
00035     OPENCITY_DEBUG("ctor");
00036 
00037     uint size = (width + 1) * (height + 1);
00038     _btabSquareHeight = new OC_BYTE [ size ];
00039 
00040     MapGen::MapMaker mapMaker =
00041         MapGen::MapMaker(
00042             width+1, height+1, 
00043             MapGen::MapMaker::PLAIN,
00044             MapGen::MapMaker::LAKE,
00045             MapGen::MapMaker::SPARSE
00046         );
00047     int* heightMap = mapMaker.getMap();
00048 
00049     for (uint i = 0; i < size; i++)
00050         _btabSquareHeight[i] = heightMap[i];
00051 
00052     delete [] heightMap;
00053 }
00054 
00055 
00056    /*=====================================================================*/
00057 Map::~Map()
00058 {
00059     OPENCITY_DEBUG("dtor");
00060 
00061     delete [] _btabSquareHeight;
00062     _btabSquareHeight = NULL;
00063 }
00064 
00065 
00066    /*=====================================================================*/
00067 void
00068 Map::SaveTo( std::fstream& rfs )
00069 {
00070     OPENCITY_DEBUG( __PRETTY_FUNCTION__ << " saving...");
00071 
00072     rfs << _uiMapWidth << std::ends;
00073     rfs << _uiMapHeight << std::ends;
00074     uint size = (_uiMapWidth + 1) * (_uiMapHeight + 1);
00075 
00076     for (uint i = 0; i < size; i++)
00077         rfs << (uint)_btabSquareHeight[i] << std::ends;
00078 }
00079 
00080 
00081    /*=====================================================================*/
00082 void
00083 Map::LoadFrom( std::fstream& rfs )
00084 {
00085     OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "loading...");
00086 
00087     rfs >> _uiMapWidth; rfs.ignore();
00088     rfs >> _uiMapHeight; rfs.ignore();
00089     uint size = (_uiMapWidth + 1) * (_uiMapHeight + 1);
00090 
00091     delete [] _btabSquareHeight;
00092     _btabSquareHeight = new OC_BYTE [size];
00093     uint temp = 0;
00094     for (uint i = 0; i < size; i++) {
00095         rfs >> temp; rfs.ignore();
00096         _btabSquareHeight[i] = (OC_BYTE)temp;
00097     }
00098 }
00099 
00100 
00101    /*=====================================================================*/
00102 OPENCITY_ERR_CODE
00103 Map::ChangeHeight(
00104     const uint & rcuiW,
00105     const uint & rcuiH,
00106     const OPENCITY_MAP_VARIATION & enumVar )
00107 {
00108    // debugging
00109     assert(_pclayer!= NULL);
00110 
00111     if (_pclayer != NULL) {
00112         uint w1 = rcuiW;
00113         uint h1 = rcuiH;
00114         uint w2 = rcuiW;
00115         uint h2 = rcuiH;
00116        // if the surface contains something, we return
00117         GetPossibleWH( w1, h1, -1, -1 );
00118         GetPossibleWH( w2, h2,  1,  1 );
00119         if (_pclayer->ContainStructure(
00120             w1, h1, w2, h2, OC_STRUCTURE_ANY ) == true)
00121                 return OC_ERR_SOMETHING;
00122     }
00123 
00124     OC_BYTE tabH [4];
00125     OC_BYTE refH;
00126     uint i;
00127     uint linear;
00128 
00129     GetSquareHeight( rcuiW, rcuiH, tabH );
00130    // check for maximum variation
00131     switch (enumVar) {
00132         case OC_MAP_UP:
00133             refH = GetSquareMinHeight( rcuiW, rcuiH );
00134             if (refH == OC_MAP_HEIGHT_MAX)
00135                 return OC_ERR_SOMETHING;
00136             break;
00137         case OC_MAP_DOWN:
00138             refH = GetSquareMaxHeight( rcuiW, rcuiH );
00139             if (refH == OC_MAP_HEIGHT_MIN )
00140                 return OC_ERR_SOMETHING;
00141             break;
00142     }
00143 
00144     for (i = 0; i < 4; i++)
00145         if (tabH[i] == refH)
00146             tabH[i] += enumVar;
00147 
00148    //--- left-up and right-up heights ---
00149     _WH2Linear( rcuiW, rcuiH, linear );
00150     _btabSquareHeight[ linear ] = tabH[0];
00151     _btabSquareHeight[ linear+1 ] = tabH[3];
00152    //--- left-down and right-down heights ---
00153     _WH2Linear( rcuiW, rcuiH+1, linear );
00154     _btabSquareHeight[ linear ] = tabH[1];
00155     _btabSquareHeight[ linear+1 ] = tabH[2];
00156 
00157     return OC_ERR_FREE;
00158 }
00159 
00160 
00161    /*=====================================================================*/
00162 void
00163 Map::SetLayer( const Layer*  layer )
00164 {
00165     _pclayer = layer;
00166 }
00167 
00168 
00169    /*=====================================================================*/
00170 const OC_BYTE &
00171 Map::GetLinearHeight( const uint & rcuiIndex ) const
00172 {
00173     return _btabSquareHeight[rcuiIndex];
00174 }
00175 
00176 
00177    /*=====================================================================*/
00178 const uint
00179 Map::GetMaxLinear() const
00180 {
00181     return ((_uiMapWidth + 1) * (_uiMapHeight + 1) - 1);
00182 }
00183 
00184 
00185    /*=====================================================================*/
00186 void
00187 Map::GetSquareHeight(
00188     const uint & rcuiW,
00189     const uint & rcuiH,
00190     OC_BYTE btabH[] ) const
00191 {
00192     uint linear;
00193 
00194    /*--- left-up and right-up heights ---*/
00195     _WH2Linear( rcuiW, rcuiH, linear );
00196     btabH[0] = _btabSquareHeight[ linear ];
00197     btabH[3] = _btabSquareHeight[ linear+1 ];
00198    /*--- left-down and right-down heights ---*/
00199     _WH2Linear( rcuiW, rcuiH+1, linear );
00200     btabH[1] = _btabSquareHeight[ linear ];
00201     btabH[2] = _btabSquareHeight[ linear+1 ];
00202 }
00203 
00204 
00205    /*=====================================================================*/
00206 const signed char
00207 Map::GetSquareMinHeight(
00208     const uint & rcuiW,
00209     const uint & rcuiL ) const
00210 {
00211     OC_BYTE btabH [4];
00212     OC_BYTE minH = 127;
00213 
00214     GetSquareHeight( rcuiW, rcuiL, btabH );
00215     for (uint i = 0; i < 4; i++)
00216         if (btabH[i] < minH)
00217             minH = btabH[i];
00218 //debug cout << "minH : " << (int)minH << endl;
00219     return minH;
00220 }
00221 
00222 
00223    /*=====================================================================*/
00224 const signed char
00225 Map::GetSquareMaxHeight(
00226     const uint & rcuiW,
00227     const uint & rcuiH ) const
00228 {
00229     OC_BYTE btabH [4];
00230     signed char maxH = -127;
00231 
00232     GetSquareHeight( rcuiW, rcuiH, btabH );
00233     for (uint i = 0; i < 4; i++)
00234         if (btabH[i] > maxH)
00235             maxH = btabH[i];
00236 
00237     return maxH;
00238 }
00239 
00240 
00241    /*=====================================================================*/
00242 const bool
00243 Map::IsSquarePlane(
00244     const uint & rcuiW,
00245     const uint & rcuiH ) const
00246 {
00247     OC_BYTE btabH [4];
00248 
00249     GetSquareHeight( rcuiW, rcuiH, btabH );
00250 
00251     return   ((btabH[0] == btabH[1])
00252         &&(btabH[0] == btabH[2])
00253         &&(btabH[0] == btabH[3]));
00254 }
00255 
00256 
00257    /*=====================================================================*/
00258 const bool
00259 Map::IsSquareLinearPlane(
00260     const uint & rcuiLinearIndex )
00261 {
00262     uint w, h;
00263 
00264     _Linear2WH( rcuiLinearIndex, w, h );
00265 
00266     return IsSquarePlane( w, h );
00267 }
00268 
00269 
00270    /*=====================================================================*/
00271 const bool
00272 Map::GetNeighbourWH(
00273     const uint & rcuiMapW,
00274     const uint & rcuiMapH,
00275     uint & ruiNbrW,
00276     uint & ruiNbrH,
00277     const OPENCITY_DIRECTION & enumDir ) const
00278 {
00279     int w = rcuiMapW;
00280     int h = rcuiMapH;
00281 
00282 /*debug
00283     cout << "w : " << w
00284          << "/ h : " << h << endl;
00285 */
00286 
00287     switch (enumDir) {
00288         case OC_DIR_N:  h--;      break;
00289         case OC_DIR_NE: h--; w++; break;
00290         case OC_DIR_E:  w++;      break;
00291         case OC_DIR_SE: w++; h++; break;
00292         case OC_DIR_S:  h++;      break;
00293         case OC_DIR_SW: h++; w--; break;
00294         case OC_DIR_W:  w--;      break;
00295         case OC_DIR_NW: w--; h--; break;
00296         default: break;
00297     }
00298 
00299 /*debug
00300     cout << "w : " << w
00301          << "/ h : " << h << endl;
00302 */
00303 
00304     if ((w < 0) || (w >= (int)_uiMapWidth)
00305      || (h < 0) || (h >= (int)_uiMapHeight)) {
00306         return false;
00307     }
00308     else {
00309         ruiNbrW = w;
00310         ruiNbrH = h;
00311         return true;
00312     }
00313 }
00314 
00315 
00316    /*=====================================================================*/
00317 void
00318 Map::GetPossibleWH(
00319     uint & rW,
00320     uint & rH,
00321     const int & deltaW,
00322     const int & deltaH ) const
00323 {
00324     int newW = rW + deltaW;
00325     int newH = rH + deltaH;
00326 
00327     if (newW < 0)
00328         newW = 0;
00329     if (newW >= (int)_uiMapWidth)
00330         newW = _uiMapWidth - 1;
00331     if (newH < 0)
00332         newH = 0;
00333     if (newH >= (int)_uiMapHeight)
00334         newH = _uiMapHeight - 1;
00335 
00336     rW = newW;
00337     rH = newH;
00338 }
00339 
00340 
00341    /*=====================================================================*/
00342 void
00343 Map::_Linear2WH( const uint & linear,
00344               uint & w,
00345               uint & h ) const
00346 {
00347 //debug
00348 cout << "not implemented yet " << endl;
00349 assert(0);
00350 }
00351 
00352 
00353    /*=====================================================================*/
00354 inline uint
00355 Map::_WH2Linear(
00356     const uint & w,
00357     const uint & h,
00358     uint & linear ) const
00359 {
00360     return (linear = h * (_uiMapWidth + 1) + w);
00361 }
00362 
00363 
00364 
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 

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