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 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 }