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 SquareGrid* nGrid =
new SquareGrid(m_allow_diagonals);
46 nGrid->setRotation(m_rotation);
47 nGrid->setXScale(m_xscale);
48 nGrid->setYScale(m_yscale);
49 nGrid->setXShift(m_xshift);
50 nGrid->setYShift(m_yshift);
51 nGrid->setZShift(m_zshift);
56 SquareGrid::~SquareGrid() {
59 bool SquareGrid::isAccessible(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
62 if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
64 if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
66 if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
68 if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
71 if (m_allow_diagonals) {
72 return isAccessibleDiagonal(curpos, target);
78 bool SquareGrid::isAccessibleDiagonal(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
79 if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
81 if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
83 if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
85 if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
90 double SquareGrid::getAdjacentCost(
const ModelCoordinate& curpos,
const ModelCoordinate& target) {
91 assert(isAccessible(curpos, target));
92 if (curpos == target) {
95 if (isAccessibleDiagonal(curpos, target)) {
96 return Mathd::Sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
98 if (curpos.x == target.x) {
104 const std::string& SquareGrid::getType()
const {
105 static std::string type(
"square");
109 const std::string& SquareGrid::getName()
const {
110 static std::string squareGrid(
"Square Grid");
114 ExactModelCoordinate SquareGrid::toMapCoordinates(
const ExactModelCoordinate& layer_coords) {
115 return m_matrix * layer_coords;
118 ExactModelCoordinate SquareGrid::toExactLayerCoordinates(
const ExactModelCoordinate& map_coord) {
119 return m_inverse_matrix * map_coord;
122 ModelCoordinate SquareGrid::toLayerCoordinates(
const ExactModelCoordinate& map_coord) {
123 ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
124 ModelCoordinate result;
125 result.x =
static_cast<int32_t
>(floor(dblpt.x));
126 result.y =
static_cast<int32_t
>(floor(dblpt.y));
127 result.z =
static_cast<int32_t
>(floor(dblpt.z));
129 if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
132 if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
135 if ((dblpt.z - static_cast<double>(result.z)) > 0.5) {
142 void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx,
const ModelCoordinate& cell) {
144 double x =
static_cast<double>(cell.x);
145 double y =
static_cast<double>(cell.y);
146 vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
147 vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
148 vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
149 vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...