32 #include "util/math/fife_math.h"
33 #include "util/log/logger.h"
35 #include "squaregrid.h"
38 static Logger _log(LM_SQUAREGRID);
40 SquareGrid::SquareGrid(
bool allow_diagonals):
41 CellGrid(allow_diagonals) {
44 CellGrid* SquareGrid::clone() {
45 return new SquareGrid(
this);
48 SquareGrid::~SquareGrid() {
51 bool SquareGrid::isAccessible(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
54 if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
56 if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
58 if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
60 if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
63 if (m_allow_diagonals) {
64 return isAccessibleDiagonal(curpos, target);
70 bool SquareGrid::isAccessibleDiagonal(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
71 if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
73 if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
75 if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
77 if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
82 float SquareGrid::getAdjacentCost(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
83 assert(isAccessible(curpos, target));
84 if (curpos == target) {
87 if (isAccessibleDiagonal(curpos, target)) {
88 return sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
90 if (curpos.x == target.x) {
96 const std::string& SquareGrid::getType()
const {
97 static std::string type(
"square");
101 const std::string& SquareGrid::getName()
const {
102 static std::string squareGrid(
"Square Grid");
106 ExactModelCoordinate SquareGrid::toMapCoordinates(
const ExactModelCoordinate& layer_coords) {
107 return m_matrix * layer_coords;
110 ExactModelCoordinate SquareGrid::toExactLayerCoordinates(
const ExactModelCoordinate& map_coord) {
111 return m_inverse_matrix * map_coord;
114 ModelCoordinate SquareGrid::toLayerCoordinates(
const ExactModelCoordinate& map_coord) {
115 ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
116 ModelCoordinate result;
117 result.x =
static_cast<int>(floor(dblpt.x));
118 result.y =
static_cast<int>(floor(dblpt.y));
120 if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
123 if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
130 void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx,
const ModelCoordinate& cell) {
132 double x =
static_cast<double>(cell.x);
133 double y =
static_cast<double>(cell.y);
134 vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
135 vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
136 vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
137 vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));