squaregrid.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <cassert>
00024 #include <iostream>
00025
00026
00027
00028
00029
00030
00031
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 }