FIFE  2008.0
searchspace.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <limits>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "model/structures/instance.h"
00032 #include "model/structures/layer.h"
00033 #include "model/structures/map.h"
00034 
00035 #include "searchspace.h"
00036 
00037 namespace FIFE {
00038 
00039     SearchSpace::SearchSpace(Layer* layer) 
00040     : m_upperX(0), m_upperY(0), m_lowerX(0), m_lowerY(0), m_layer(layer) {
00041         
00042         Map* map = layer->getMap();
00043         const std::list<Layer*>& layers = map->getLayers();
00044         ModelCoordinate min, max;
00045 
00046         for(std::list<Layer*>::const_iterator i = layers.begin();
00047             i != layers.end();
00048             ++i) {
00049 
00050             ModelCoordinate newMin, newMax;
00051             (*i)->getMinMaxCoordinates(newMin, newMax, layer);
00052 
00053             if(newMin.x < min.x) {
00054                 min.x = newMin.x;
00055             } 
00056             
00057             if(newMax.x > max.x) {
00058                 max.x = newMax.x;
00059             }
00060 
00061             if(newMin.y < min.y) {
00062                 min.y = newMin.y;
00063             }
00064             
00065             if(newMax.y > max.y) {
00066                 max.y = newMax.y;
00067             }
00068         }
00069 
00070         m_upperX = max.x;
00071         m_upperY = max.y;
00072         m_lowerX = min.x;
00073         m_lowerY = min.y;
00074     }
00075 
00076     bool SearchSpace::isInSearchSpace(const Location& location) const {
00077         if(location.getLayer() != m_layer) {
00078             return false;
00079         }
00080         ModelCoordinate coordinates = location.getLayerCoordinates();
00081         if(coordinates.x >= m_lowerX && coordinates.x <= m_upperX 
00082             && coordinates.y >= m_lowerY && coordinates.y <= m_upperY) {
00083                 return true;
00084         }
00085         return false;
00086     }
00087 
00088     ModelCoordinate SearchSpace::translateCoordsToSearchSpace(const ModelCoordinate& coords) const {
00089         ModelCoordinate newcoords;
00090         newcoords.x = coords.x - m_lowerX;
00091         newcoords.y = coords.y - m_lowerY;
00092         return newcoords;
00093     }
00094 
00095     int SearchSpace::convertCoordToInt(const ModelCoordinate& coord) const {
00096         ModelCoordinate newcoords = translateCoordsToSearchSpace(coord);
00097         return newcoords.x + (newcoords.y * getWidth());
00098     }
00099 
00100     ModelCoordinate SearchSpace::convertIntToCoord(const int cell) const {
00101         ModelCoordinate coord;
00102         int width = getWidth();
00103         coord.x = (cell % width) + m_lowerX;
00104         coord.y = (cell / width) + m_lowerY;
00105         return coord;
00106     }
00107 
00108     int SearchSpace::getMaxIndex() const {
00109 
00110         //max index = w * h.
00111         int max_index = getWidth() + (getWidth() * getHeight());
00112         return max_index;
00113     }
00114 }