FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
location.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 
24 // 3rd party library includes
25 #include <SDL.h>
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/base/exception.h"
32 #include "util/structures/purge.h"
33 #include "model/metamodel/grids/cellgrid.h"
34 
35 #include "layer.h"
36 #include "instance.h"
37 
38 namespace FIFE {
39  static std::string INVALID_LAYER_SET = "Cannot set layer coordinates, given layer is not initialized properly";
40  static std::string INVALID_LAYER_GET = "Cannot get layer coordinates, layer is not initialized properly";
41 
42  Location::Location() {
43  reset();
44  }
45 
46  Location::Location(const Location& loc) {
47  reset();
48  m_layer = loc.m_layer;
49  m_exact_layer_coords = loc.m_exact_layer_coords;
50  }
51 
52  Location::Location(Layer* layer) {
53  reset();
54  m_layer = layer;
55  }
56 
57  Location::~Location() {
58  reset();
59  }
60 
61  void Location::reset() {
62  m_exact_layer_coords.x = 0;
63  m_exact_layer_coords.y = 0;
64  m_layer = NULL;
65  }
66 
67  Location& Location::operator=(const Location& rhs) {
68  m_layer = rhs.m_layer;
69  m_exact_layer_coords.x = rhs.m_exact_layer_coords.x;
70  m_exact_layer_coords.y = rhs.m_exact_layer_coords.y;
71  return *this;
72  }
73 
74  Map* Location::getMap() const {
75  if (!m_layer) {
76  return NULL;
77  }
78  return m_layer->getMap();
79  }
80 
81  void Location::setLayer(Layer* layer) {
82  m_layer = layer;
83  }
84 
85  Layer* Location::getLayer() const {
86  return m_layer;
87  }
88 
89  void Location::setExactLayerCoordinates(const ExactModelCoordinate& coordinates) {
90  if (!isValid()) {
91  throw NotSet(INVALID_LAYER_SET);
92  }
93  m_exact_layer_coords = coordinates;
94  }
95 
96  void Location::setLayerCoordinates(const ModelCoordinate& coordinates) {
97  setExactLayerCoordinates(intPt2doublePt(coordinates));
98  }
99 
100  void Location::setMapCoordinates(const ExactModelCoordinate& coordinates) {
101  if (!isValid()) {
102  throw NotSet(INVALID_LAYER_SET);
103  }
104  m_exact_layer_coords = m_layer->getCellGrid()->toExactLayerCoordinates(coordinates);
105  }
106 
107  ExactModelCoordinate& Location::getExactLayerCoordinatesRef() {
108  return m_exact_layer_coords;
109  }
110 
111  ExactModelCoordinate Location::getExactLayerCoordinates() const {
112  return m_exact_layer_coords;
113  }
114 
115  ModelCoordinate Location::getLayerCoordinates() const {
116  return getLayerCoordinates(m_layer);
117  }
118 
119  ExactModelCoordinate Location::getMapCoordinates() const {
120  return m_layer->getCellGrid()->toMapCoordinates(m_exact_layer_coords);
121  }
122 
123  bool Location::isValid() const {
124  return isValid(m_layer);
125  }
126 
127  bool Location::isValid(const Layer* layer) const {
128  return (layer && layer->getCellGrid());
129  }
130 
131  ExactModelCoordinate Location::getExactLayerCoordinates(const Layer* layer) const {
132  return m_exact_layer_coords;
133  }
134 
135  ModelCoordinate Location::getLayerCoordinates(const Layer* layer) const {
136  if (!isValid(layer)) {
137  throw NotSet(INVALID_LAYER_GET);
138  }
139  CellGrid* cg1 = m_layer->getCellGrid();
140  CellGrid* cg2 = layer->getCellGrid();
141  return cg2->toLayerCoordinates(cg1->toMapCoordinates(m_exact_layer_coords));
142  }
143 
144  double Location::getCellOffsetDistance() const {
145  const ExactModelCoordinate& pt = m_exact_layer_coords;
146  double dx = pt.x - static_cast<double>(static_cast<int>(pt.x));
147  double dy = pt.y - static_cast<double>(static_cast<int>(pt.y));
148  return sqrt(dx*dx + dy*dy);
149  }
150 
151  std::ostream& operator<<(std::ostream& os, const Location& l) {
152  ExactModelCoordinate p = l.getExactLayerCoordinates();
153  return os << "x=" << p.x << ", y=" << p.y;
154  }
155 
156  double Location::getMapDistanceTo(const Location& location) const{
157  ExactModelCoordinate current = getMapCoordinates();
158  ExactModelCoordinate target = location.getMapCoordinates();
159 
160  double rx = current.x - target.x;
161  double ry = current.y - target.y;
162  double rz = current.z - target.z;
163 
164  return sqrt(rx*rx + ry*ry + rz*rz);
165  }
166 
167  double Location::getLayerDistanceTo(const Location& location) const{
168  ModelCoordinate current = getLayerCoordinates();
169  ModelCoordinate target = location.getLayerCoordinates(m_layer);
170 
171  double rx = current.x - target.x;
172  double ry = current.y - target.y;
173  double rz = current.z - target.z;
174 
175  return sqrt(rx*rx + ry*ry + rz*rz);
176  }
177 }