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         SquareGrid* nGrid = new SquareGrid(m_allow_diagonals);
00046         nGrid->setRotation(m_rotation);
00047         nGrid->setXScale(m_xscale);
00048         nGrid->setYScale(m_yscale);
00049         nGrid->setXShift(m_xshift);
00050         nGrid->setYShift(m_yshift);
00051         nGrid->setZShift(m_zshift);
00052 
00053         return nGrid;
00054     }
00055 
00056     SquareGrid::~SquareGrid() {
00057     }
00058 
00059     bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00060         if (curpos == target)
00061             return true;
00062         if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
00063             return true;
00064         if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
00065             return true;
00066         if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
00067             return true;
00068         if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
00069             return true;
00070 
00071         if (m_allow_diagonals) {
00072             return isAccessibleDiagonal(curpos, target);
00073         }
00074 
00075         return false;
00076     }
00077 
00078     bool SquareGrid::isAccessibleDiagonal(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00079         if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
00080             return true;
00081         if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
00082             return true;
00083         if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
00084             return true;
00085         if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
00086             return true;
00087         return false;
00088     }
00089 
00090     double SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
00091         assert(isAccessible(curpos, target));
00092         if (curpos == target) {
00093             return 0;
00094         }
00095         if (isAccessibleDiagonal(curpos, target)) {
00096             return Mathd::Sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
00097         }
00098         if (curpos.x == target.x) {
00099             return m_xscale;
00100         }
00101         return m_yscale;
00102     }
00103 
00104     const std::string& SquareGrid::getType() const {
00105         static std::string type("square");
00106         return type;
00107     }
00108 
00109     const std::string& SquareGrid::getName() const {
00110         static std::string squareGrid("Square Grid");
00111         return squareGrid;
00112     }
00113 
00114     ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
00115         return m_matrix * layer_coords;
00116     }
00117 
00118     ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
00119         return m_inverse_matrix * map_coord;
00120     }
00121 
00122     ModelCoordinate SquareGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
00123         ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
00124         ModelCoordinate result;
00125         result.x = static_cast<int32_t>(floor(dblpt.x));
00126         result.y = static_cast<int32_t>(floor(dblpt.y));
00127         result.z = static_cast<int32_t>(floor(dblpt.z));
00128 
00129         if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
00130             result.x++;
00131         }
00132         if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
00133             result.y++;
00134         }
00135         if ((dblpt.z - static_cast<double>(result.z)) > 0.5) {
00136             result.z++;
00137         }
00138 
00139         return result;
00140     }
00141 
00142     void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
00143         vtx.clear();
00144         double x = static_cast<double>(cell.x);
00145         double y = static_cast<double>(cell.y);
00146         vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
00147         vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
00148         vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
00149         vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
00150     }
00151 }