squaregrid.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 <cassert>
00024 #include <iostream>
00025 
00026 // 3rd party library includes
00027 
00028 // FIFE includes
00029 // These includes are split up in two parts, separated by one empty line
00030 // First block: files included from the FIFE root src directory
00031 // Second block: files included from the same folder
00032 #include "util/math/fife_math.h"
00033 #include "util/log/logger.h"
00034 
00035 #include "squaregrid.h"
00036 
00037 namespace FIFE {
00038     static Logger _log(LM_SQUAREGRID);
00039 
00040     SquareGrid::SquareGrid(bool allow_diagonals): 
00041         CellGrid(allow_diagonals) {
00042     }
00043 
00044     CellGrid* SquareGrid::clone() {
00045         return new SquareGrid(this);
00046     }
00047 
00048     SquareGrid::~SquareGrid() {
00049     }
00050 
00051     bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00052         if (curpos == target)
00053             return true;
00054         if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
00055             return true;
00056         if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
00057             return true;
00058         if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
00059             return true;
00060         if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
00061             return true;
00062 
00063         if (m_allow_diagonals) {
00064             return isAccessibleDiagonal(curpos, target);
00065         }
00066 
00067         return false;
00068     }
00069 
00070     bool SquareGrid::isAccessibleDiagonal(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00071         if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
00072             return true;
00073         if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
00074             return true;
00075         if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
00076             return true;
00077         if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
00078             return true;
00079         return false;
00080     }
00081 
00082     float SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00083         assert(isAccessible(curpos, target));
00084         if (curpos == target) {
00085             return 0;
00086         }
00087         if (isAccessibleDiagonal(curpos, target)) {
00088             return sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
00089         }
00090         if (curpos.x == target.x) {
00091             return m_xscale;
00092         }
00093         return m_yscale;
00094     }
00095 
00096     const std::string& SquareGrid::getType() const {
00097         static std::string type("square");
00098         return type;
00099     }
00100 
00101     const std::string& SquareGrid::getName() const {
00102         static std::string squareGrid("Square Grid");
00103         return squareGrid;
00104     }
00105 
00106     ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
00107         return m_matrix * layer_coords;
00108     }
00109 
00110     ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
00111         return m_inverse_matrix * map_coord;
00112     }
00113 
00114     ModelCoordinate SquareGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
00115         ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
00116         ModelCoordinate result;
00117         result.x = static_cast<int>(floor(dblpt.x));
00118         result.y = static_cast<int>(floor(dblpt.y));
00119         
00120         if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
00121             result.x++;
00122         }
00123         if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
00124             result.y++;
00125         }
00126         
00127         return result;
00128     }
00129     
00130     void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
00131         vtx.clear();
00132         double x = static_cast<double>(cell.x);
00133         double y = static_cast<double>(cell.y);
00134         vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
00135         vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
00136         vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
00137         vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
00138     }
00139 }
Generated on Wed Nov 23 13:04:43 2011 for FIFE by  doxygen 1.6.3