| 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 |
|
| 25 |
// 3rd party library includes |
| 26 |
|
| 27 |
// FIFE includes |
| 28 |
// These includes are split up in two parts, separated by one empty line |
| 29 |
// First block: files included from the FIFE root src directory |
| 30 |
// Second block: files included from the same folder |
| 31 |
#include "util/log/logger.h" |
| 32 |
|
| 33 |
#include "cellgrid.h" |
| 34 |
|
| 35 |
namespace FIFE { |
| 36 |
static Logger _log(LM_CELLGRID); |
| 37 |
|
| 38 |
CellGrid::CellGrid(): |
| 39 |
FifeClass(), |
| 40 |
m_matrix(), |
| 41 |
m_inverse_matrix(), |
| 42 |
m_xshift(0), |
| 43 |
m_yshift(0), |
| 44 |
m_zshift(0), |
| 45 |
m_xscale(1), |
| 46 |
m_yscale(1), |
| 47 |
m_zscale(1), |
| 48 |
m_rotation(0), |
| 49 |
m_allow_diagonals(false) { |
| 50 |
updateMatrices(); |
| 51 |
} |
| 52 |
|
| 53 |
CellGrid::~CellGrid() { |
| 54 |
} |
| 55 |
|
| 56 |
void CellGrid::getAccessibleCoordinates(const ModelCoordinate& curpos, std::vector<ModelCoordinate>& coordinates) { |
| 57 |
coordinates.clear(); |
| 58 |
for (int32_t x = curpos.x - 1; x <= curpos.x + 1; x++) { |
| 59 |
for (int32_t y = curpos.y - 1; y <= curpos.y + 1; y++) { |
| 60 |
ModelCoordinate pt(x, y); |
| 61 |
if (isAccessible(curpos, pt)) { |
| 62 |
coordinates.push_back(pt); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void CellGrid::updateMatrices() { |
| 69 |
m_matrix.loadRotate(m_rotation, 0.0, 0.0, 1.0); |
| 70 |
m_matrix.applyScale(m_xscale, m_yscale, m_zscale); |
| 71 |
m_matrix.applyTranslate(m_xshift, m_yshift, m_zshift); |
| 72 |
m_inverse_matrix = m_matrix.inverse(); |
| 73 |
} |
| 74 |
|
| 75 |
ExactModelCoordinate CellGrid::toMapCoordinates(const ModelCoordinate& layer_coords) { |
| 76 |
return toMapCoordinates(intPt2doublePt(layer_coords)); |
| 77 |
} |
| 78 |
|
| 79 |
int32_t CellGrid::orientation(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2) { |
| 80 |
double o = (pt2.x - pt1.x) * (pt.y - pt1.y) - (pt.x - pt1.x) * (pt2.y - pt1.y); |
| 81 |
if (o > 0.0) { |
| 82 |
return 1; |
| 83 |
} else if (o < 0.0) { |
| 84 |
return -1; |
| 85 |
} |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
bool CellGrid::ptInTriangle(const ExactModelCoordinate& pt, const ExactModelCoordinate& pt1, const ExactModelCoordinate& pt2, const ExactModelCoordinate& pt3) { |
| 90 |
double o1 = orientation(pt1, pt2, pt); |
| 91 |
double o2 = orientation(pt2, pt3, pt); |
| 92 |
double o3 = orientation(pt3, pt1, pt); |
| 93 |
bool result = (o1 == o2) && (o2 == o3); |
| 94 |
FL_DBG(_log, LMsg("ptInTriangle, pt=") << pt << " pt1=" << pt1 << " pt2=" << pt2 << " pt3=" << pt3 << " in=" << result); |
| 95 |
return result; |
| 96 |
} |
| 97 |
} |