FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
squaregrid.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
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 
41  CellGrid() {
42  }
43 
45  SquareGrid* nGrid = new SquareGrid();
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);
53 
54  return nGrid;
55  }
56 
58  }
59 
60  bool SquareGrid::isAccessible(const ModelCoordinate& curpos, const ModelCoordinate& target) {
61  if (curpos == target) {
62  return true;
63  }
64  uint8_t x = ABS(target.x-curpos.x);
65  uint8_t y = ABS(target.y-curpos.y);
66  if ((x<=1) && (y<=1)) {
67  if (m_allow_diagonals) {
68  return true;
69  } else if (x^y) {
70  return true;
71  }
72  }
73 
74  return false;
75  }
76 
77  double SquareGrid::getAdjacentCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
78  if (curpos == target) {
79  return 0.0;
80  } else if (ABS(target.x-curpos.x)^ABS(target.y-curpos.y)) {
81  return 1.0;
82  }
83  return 1.4;
84  }
85 
86  double SquareGrid::getHeuristicCost(const ModelCoordinate& curpos, const ModelCoordinate& target) {
87  return static_cast<double>(ABS(target.x - curpos.x) + ABS(target.y - curpos.y));
88  }
89 
90  const std::string& SquareGrid::getType() const {
91  static std::string type("square");
92  return type;
93  }
94 
95  const std::string& SquareGrid::getName() const {
96  static std::string squareGrid("Square Grid");
97  return squareGrid;
98  }
99 
101  return m_matrix * layer_coords;
102  }
103 
105  return m_inverse_matrix * map_coord;
106  }
107 
111  }
112 
114  ModelCoordinate result(round(exact_layer_coords.x), round(exact_layer_coords.y), round(exact_layer_coords.z));
115  return result;
116  }
117 
118  void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
119  vtx.clear();
120  double x = static_cast<double>(cell.x);
121  double y = static_cast<double>(cell.y);
122  vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
123  vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
124  vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
125  vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
126  }
127 
128  std::vector<ModelCoordinate> SquareGrid::toMultiCoordinates(const ModelCoordinate& position, const std::vector<ModelCoordinate>& orig, bool reverse) {
129  std::vector<ModelCoordinate> coords;
130  std::vector<ModelCoordinate>::const_iterator it = orig.begin();
131  if (reverse) {
132  for (; it != orig.end(); ++it) {
133  ModelCoordinate mc = position;
134  mc.x -= (*it).x;
135  mc.y -= (*it).y;
136  coords.push_back(mc);
137  }
138  } else {
139  for (; it != orig.end(); ++it) {
140  ModelCoordinate mc = position;
141  mc.x += (*it).x;
142  mc.y += (*it).y;
143  coords.push_back(mc);
144  }
145  }
146  return coords;
147  }
148 
149  std::vector<ModelCoordinate> SquareGrid::getCoordinatesInLine(const ModelCoordinate& start, const ModelCoordinate& end) {
150  std::vector<ModelCoordinate> coords;
151  int32_t dx = ABS(end.x - start.x);
152  int32_t dy = ABS(end.y - start.y);
153  int8_t sx = -1;
154  int8_t sy = -1;
155 
156  if (start.x < end.x) {
157  sx = 1;
158  }
159  if (start.y < end.y) {
160  sy = 1;
161  }
162 
163  int32_t err = dx - dy;
164  int32_t err2 = err*2;
165  ModelCoordinate current(start);
166  bool finished = false;
167  while (!finished) {
168  coords.push_back(current);
169  if (current.x == end.x && current.y == end.y) {
170  finished = true;
171  break;
172  }
173 
174  if (err2 > -dy) {
175  err -= dy;
176  current.x += sx;
177  } else if (err2 < dx) {
178  err += dx;
179  current.y += sy;
180  }
181  err2 = err*2;
182  }
183  return coords;
184  }
185 }
const std::string & getName() const
Name of the cellgrid (DEPRECATED? -jwt)
Definition: squaregrid.cpp:95
double m_xscale
Definition: cellgrid.h:255
double getAdjacentCost(const ModelCoordinate &curpos, const ModelCoordinate &target)
Returns distance const from curpos to target point only cells adjacent to curpos are considered in th...
Definition: squaregrid.cpp:77
#define ABS(x)
Definition: fife_math.h:41
void setZShift(const double zshift)
Set the cellgrid z shift.
Definition: cellgrid.h:168
double m_yshift
Definition: cellgrid.h:253
void getVertices(std::vector< ExactModelCoordinate > &vtx, const ModelCoordinate &cell)
Fills given point vector with vertices from selected cell.
Definition: squaregrid.cpp:118
const std::string & getType() const
Type of cellgrid.
Definition: squaregrid.cpp:90
static Logger _log(LM_AUDIO)
DoubleMatrix m_inverse_matrix
Definition: cellgrid.h:251
void setXShift(const double &xshift)
Set the cellgrid x shift.
Definition: cellgrid.h:142
void setYScale(const double scale)
Set the cellgrid y-scaling.
Definition: cellgrid.h:189
virtual ~SquareGrid()
Definition: squaregrid.cpp:57
void setXScale(const double scale)
Set the cellgrid x-scaling.
Definition: cellgrid.h:181
unsigned char uint8_t
Definition: core.h:38
void setYShift(const double yshift)
Set the cellgrid y shift.
Definition: cellgrid.h:155
CellGrid * clone()
Returns clone of this cellgrid.
Definition: squaregrid.cpp:44
bool m_allow_diagonals
Definition: cellgrid.h:259
double m_rotation
Definition: cellgrid.h:258
ModelCoordinate toLayerCoordinates(const ExactModelCoordinate &map_coord)
Transforms given point from map coordinates to layer coordinates.
Definition: squaregrid.cpp:108
DoubleMatrix m_matrix
Definition: cellgrid.h:250
bool isAccessible(const ModelCoordinate &curpos, const ModelCoordinate &target)
Tells if given target point is accessible from curpos only cells adjacent to curpos are considered in...
Definition: squaregrid.cpp:60
ExactModelCoordinate toExactLayerCoordinates(const ExactModelCoordinate &map_coord)
Transforms given point from map coordinates to layer coordinates.
Definition: squaregrid.cpp:104
void setAllowDiagonals(const bool allow_diagonals)
Set whether diagonal cell access is allowed.
Definition: cellgrid.h:233
ModelCoordinate toLayerCoordinatesFromExactLayerCoordinates(const ExactModelCoordinate &exact_layer_coords)
Transforms given point from exact layer coordinates to cell precision layer coordinates.
Definition: squaregrid.cpp:113
DoublePoint3D ExactModelCoordinate
Definition: modelcoords.h:37
double m_zshift
Definition: cellgrid.h:254
double getHeuristicCost(const ModelCoordinate &curpos, const ModelCoordinate &target)
Returns distance const from curpos to target point.
Definition: squaregrid.cpp:86
A 3D Point.
Definition: point.h:205
double m_yscale
Definition: cellgrid.h:256
void setRotation(const double rotation)
Set the cellgrid rotation.
Definition: cellgrid.h:220
double m_xshift
Definition: cellgrid.h:252
std::vector< ModelCoordinate > toMultiCoordinates(const ModelCoordinate &position, const std::vector< ModelCoordinate > &orig, bool reverse)
Returns point vector with coordinates for a multi object.
Definition: squaregrid.cpp:128
std::vector< ModelCoordinate > getCoordinatesInLine(const ModelCoordinate &start, const ModelCoordinate &end)
Returns point vector with coordinates for a line from start to end.
Definition: squaregrid.cpp:149
ExactModelCoordinate toMapCoordinates(const ExactModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: squaregrid.cpp:100