FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
squaregrid.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 #include <iostream>
25 
26 // 3rd party library includes
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/math/fife_math.h"
33 #include "util/log/logger.h"
34 
35 #include "squaregrid.h"
36 
37 namespace FIFE {
38  static Logger _log(LM_SQUAREGRID);
39 
40  SquareGrid::SquareGrid(bool allow_diagonals):
41  CellGrid(allow_diagonals) {
42  }
43 
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);
52 
53  return nGrid;
54  }
55 
56  SquareGrid::~SquareGrid() {
57  }
58 
59  bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
60  if (curpos == target)
61  return true;
62  if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
63  return true;
64  if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
65  return true;
66  if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
67  return true;
68  if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
69  return true;
70 
71  if (m_allow_diagonals) {
72  return isAccessibleDiagonal(curpos, target);
73  }
74 
75  return false;
76  }
77 
78  bool SquareGrid::isAccessibleDiagonal(const ModelCoordinate& curpos, const ModelCoordinate& target) {
79  if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
80  return true;
81  if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
82  return true;
83  if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
84  return true;
85  if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
86  return true;
87  return false;
88  }
89 
90  double SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
91  assert(isAccessible(curpos, target));
92  if (curpos == target) {
93  return 0;
94  }
95  if (isAccessibleDiagonal(curpos, target)) {
96  return Mathd::Sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
97  }
98  if (curpos.x == target.x) {
99  return m_xscale;
100  }
101  return m_yscale;
102  }
103 
104  const std::string& SquareGrid::getType() const {
105  static std::string type("square");
106  return type;
107  }
108 
109  const std::string& SquareGrid::getName() const {
110  static std::string squareGrid("Square Grid");
111  return squareGrid;
112  }
113 
114  ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
115  return m_matrix * layer_coords;
116  }
117 
118  ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
119  return m_inverse_matrix * map_coord;
120  }
121 
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));
128 
129  if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
130  result.x++;
131  }
132  if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
133  result.y++;
134  }
135  if ((dblpt.z - static_cast<double>(result.z)) > 0.5) {
136  result.z++;
137  }
138 
139  return result;
140  }
141 
142  void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
143  vtx.clear();
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));
150  }
151 }