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

mapmaker.cpp

00001 /***************************************************************************
00002                         mapmaker.cpp    -  description
00003                             -------------------
00004     begin                : july 2nd, 2006
00005     copyright            : (C) 2006 by Frédéric RODRIGO
00006     email                : f.rodrigo free.fr
00007     
00008     $Id: mapmaker.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 #include "mapmaker.h"
00021 
00022 #include "diamon.h"
00023 #include "gaussblur.h"
00024 #include "flattern.h"
00025 #include "normalize.h"
00026 
00027 #include <cmath>
00028 #include <vector>
00029 using std::vector;
00030 
00031 namespace MapGen
00032 {
00033 
00034    /*=====================================================================*/
00035 MapMaker::MapMaker(
00036     const uint w,
00037     const uint h,
00038     const MAP_TYPE mapType,
00039     const WATER_TYPE waterType,
00040     const TREE_DENSITY_TYPE treeDensityType ):
00041 _w(w),
00042 _h(h),
00043 _mapType(mapType),
00044 _waterType(waterType),
00045 _map(NULL),
00046 _treeDensityType(treeDensityType),
00047 _treeDensity(NULL)
00048 {
00049     MAP_DEBUG( "ctor" );
00050 
00051     _generateMap();
00052     _generateTreeDensity();
00053 }
00054 
00055 
00056    /*=====================================================================*/
00057 MapMaker::~MapMaker()
00058 {
00059     MAP_DEBUG( "dtor" );
00060 
00061     if( _map != NULL )
00062         delete _map;
00063 
00064     if( _treeDensity != NULL )
00065         delete _treeDensity;
00066 }
00067 
00068 
00069    /*=====================================================================*/
00070 Map* MapMaker::_generate(
00071     const Generator* generator,
00072     vector<Filter*> filters ) const
00073 {
00074     // Render the map
00075     Map* map = generator->render();
00076     delete generator;
00077 
00078     // Apply filters
00079     for( vector<Filter*>::iterator i=filters.begin() ; i!=filters.end() ; i++ )
00080     {
00081         (*i)->apply( map );
00082         delete *i;
00083     }
00084 
00085     Map *cropedMap = map->crop( _w, _h );
00086     delete map;
00087     return cropedMap;
00088 }
00089 
00090 
00091    /*=====================================================================*/
00092 void MapMaker::_generateMap()
00093 {
00094     Generator* generator;
00095     vector<Filter*> filters;
00096 
00097     // Select generator
00098     switch( _mapType )
00099     {
00100         default:
00101             {
00102                 uint largerSide = _w > _h ? _w : _h;
00103                 uint side = (uint) ceil( log2( (float)largerSide ) );
00104                 generator = new Diamon( side );
00105             } break;
00106     }
00107 
00108     // Add filter
00109     switch( _mapType )
00110     {
00111         case PLAIN :
00112             filters.push_back( new Normalize(-3*_waterType,5) );
00113             filters.push_back( new Flattern(3) );
00114             filters.push_back( new GaussBlur(2) );
00115             break;
00116         case MOUNTAIN :
00117             filters.push_back( new Normalize(-10*_waterType,20) );
00118             filters.push_back( new Flattern(1) );
00119             filters.push_back( new GaussBlur(2) );
00120             break;
00121         default:
00122         case HILL :
00123             filters.push_back( new Normalize(-5*_waterType,10) );
00124             filters.push_back( new Flattern(2) );
00125             filters.push_back( new GaussBlur(2) );
00126             break;
00127     }
00128 
00129     _map = _generate( generator, filters );
00130 }
00131 
00132 
00133    /*=====================================================================*/
00134 void MapMaker::_generateTreeDensity()
00135 {
00136     Generator* generator;
00137     vector<Filter*> filters;
00138 
00139     // Select generator
00140     switch( _treeDensityType )
00141     {
00142         default:
00143             {
00144                 uint largerSide = _w > _h ? _w : _h;
00145                 uint side = (uint) ceil( log2( (float)largerSide ) );
00146                 generator = new Diamon( side );
00147             } break;
00148     }
00149 
00150     // Add filter
00151     switch( _treeDensityType )
00152     {
00153         case SPARSE :
00154             filters.push_back( new Flattern(8) );
00155             filters.push_back( new GaussBlur(2) );
00156             filters.push_back( new Normalize(0,8) );
00157             break;
00158         case DENSE :
00159             filters.push_back( new Flattern(1) );
00160             filters.push_back( new GaussBlur(2) );
00161             filters.push_back( new Normalize(3,20) );
00162             break;
00163         default:
00164         case NORMAL :
00165             filters.push_back( new Flattern(4) );
00166             filters.push_back( new GaussBlur(2) );
00167             filters.push_back( new Normalize(0,10) );
00168             break;
00169     }
00170 
00171     _treeDensity = _generate( generator, filters );
00172 }
00173 
00174 
00175 
00176 int *MapMaker::getMap()
00177 {
00178     return _map->toIntArray();
00179 }
00180 
00181 
00182 int *MapMaker::getTreeDensity()
00183 {
00184     return _treeDensity->toIntArray();
00185 }
00186 
00187 }

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