Debian Codegraph @

engine/core/model/metamodel/grids/squaregrid.cpp

raw
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
40 SquareGrid::SquareGrid():
41 CellGrid() {
42 }
43
44 CellGrid* SquareGrid::clone() {
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);
52 nGrid->setAllowDiagonals(m_allow_diagonals);
53
54 return nGrid;
55 }
56
57 SquareGrid::~SquareGrid() {
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
100 ExactModelCoordinate SquareGrid::toMapCoordinates(const ExactModelCoordinate& layer_coords) {
101 return m_matrix * layer_coords;
102 }
103
104 ExactModelCoordinate SquareGrid::toExactLayerCoordinates(const ExactModelCoordinate& map_coord) {
105 return m_inverse_matrix * map_coord;
106 }
107
108 ModelCoordinate SquareGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
109 ExactModelCoordinate dblpt = toExactLayerCoordinates(map_coord);
110 return toLayerCoordinatesFromExactLayerCoordinates(dblpt);
111 }
112
113 ModelCoordinate SquareGrid::toLayerCoordinatesFromExactLayerCoordinates(const ExactModelCoordinate& exact_layer_coords) {
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 }