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  return new SquareGrid(this);
46  }
47 
48  SquareGrid::~SquareGrid() {
49  }
50 
51  bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
52  if (curpos == target)
53  return true;
54  if ((curpos.x == target.x) && (curpos.y - 1 == target.y))
55  return true;
56  if ((curpos.x == target.x) && (curpos.y + 1 == target.y))
57  return true;
58  if ((curpos.x + 1 == target.x) && (curpos.y == target.y))
59  return true;
60  if ((curpos.x - 1 == target.x) && (curpos.y == target.y))
61  return true;
62 
63  if (m_allow_diagonals) {
64  return isAccessibleDiagonal(curpos, target);
65  }
66 
67  return false;
68  }
69 
70  bool SquareGrid::isAccessibleDiagonal(const ModelCoordinate& curpos, const ModelCoordinate& target) {
71  if ((curpos.x - 1 == target.x) && (curpos.y - 1 == target.y))
72  return true;
73  if ((curpos.x - 1 == target.x) && (curpos.y + 1 == target.y))
74  return true;
75  if ((curpos.x + 1 == target.x) && (curpos.y - 1 == target.y))
76  return true;
77  if ((curpos.x + 1 == target.x) && (curpos.y + 1 == target.y))
78  return true;
79  return false;
80  }
81 
82  float SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
83  assert(isAccessible(curpos, target));
84  if (curpos == target) {
85  return 0;
86  }
87  if (isAccessibleDiagonal(curpos, target)) {
88  return sqrt(m_xscale*m_xscale + m_yscale*m_yscale);
89  }
90  if (curpos.x == target.x) {
91  return m_xscale;
92  }
93  return m_yscale;
94  }
95 
96  const std::string& SquareGrid::getType() const {
97  static std::string type("square");
98  return type;
99  }
100 
101  const std::string& SquareGrid::getName() const {
102  static std::string squareGrid("Square Grid");
103  return squareGrid;
104  }
105 
106  ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
107  return m_matrix * layer_coords;
108  }
109 
110  ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
111  return m_inverse_matrix * map_coord;
112  }
113 
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));
119 
120  if ((dblpt.x - static_cast<double>(result.x)) > 0.5) {
121  result.x++;
122  }
123  if ((dblpt.y - static_cast<double>(result.y)) > 0.5) {
124  result.y++;
125  }
126 
127  return result;
128  }
129 
130  void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
131  vtx.clear();
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));
138  }
139 }