1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #define _USE_MATH_DEFINES
20 #include "Target.h"
21 #include <cmath>
22 #include "../Engine/Language.h"
23 #include "Craft.h"
24 
25 namespace OpenXcom
26 {
27 
28 /**
29  * Initializes a target with blank coordinates.
30  */
Target()31 Target::Target() : _lon(0.0), _lat(0.0), _followers()
32 {
33 }
34 
35 /**
36  * Make sure no crafts are chasing this target.
37  */
~Target()38 Target::~Target()
39 {
40 	for (size_t i = 0; i < _followers.size(); ++i)
41 	{
42 		Craft *craft = dynamic_cast<Craft*>(_followers[i]);
43 		if (craft)
44 		{
45 			craft->returnToBase();
46 		}
47 	}
48 }
49 
50 /**
51  * Loads the target from a YAML file.
52  * @param node YAML node.
53  */
load(const YAML::Node & node)54 void Target::load(const YAML::Node &node)
55 {
56 	_lon = node["lon"].as<double>(_lon);
57 	_lat = node["lat"].as<double>(_lat);
58 }
59 
60 /**
61  * Saves the target to a YAML file.
62  * @returns YAML node.
63  */
save() const64 YAML::Node Target::save() const
65 {
66 	YAML::Node node;
67 	node["lon"] = _lon;
68 	node["lat"] = _lat;
69 	return node;
70 }
71 
72 /**
73  * Saves the target's unique identifiers to a YAML file.
74  * @return YAML node.
75  */
saveId() const76 YAML::Node Target::saveId() const
77 {
78 	YAML::Node node;
79 	node["lon"] = _lon;
80 	node["lat"] = _lat;
81 	return node;
82 }
83 
84 /**
85  * Returns the longitude coordinate of the target.
86  * @return Longitude in radian.
87  */
getLongitude() const88 double Target::getLongitude() const
89 {
90 	return _lon;
91 }
92 
93 /**
94  * Changes the longitude coordinate of the target.
95  * @param lon Longitude in radian.
96  */
setLongitude(double lon)97 void Target::setLongitude(double lon)
98 {
99 	_lon = lon;
100 
101 	// Keep between 0 and 2xPI
102 	while (_lon < 0)
103 		_lon += 2 * M_PI;
104 	while (_lon >= 2 * M_PI)
105 		_lon -= 2 * M_PI;
106 }
107 
108 /**
109  * Returns the latitude coordinate of the target.
110  * @return Latitude in radian.
111  */
getLatitude() const112 double Target::getLatitude() const
113 {
114 	return _lat;
115 }
116 
117 /**
118  * Changes the latitude coordinate of the target.
119  * @param lat Latitude in radian.
120  */
setLatitude(double lat)121 void Target::setLatitude(double lat)
122 {
123 	_lat = lat;
124 	// If you travel past a pole, continue on the other side of the globe.
125 	if (_lat < -M_PI/2)
126 	{
127 		_lat = -M_PI - _lat;
128 		setLongitude(_lon + M_PI);
129 	}
130 	else if (_lat > M_PI/2)
131 	{
132 		_lat = M_PI - _lat;
133 		setLongitude(_lon - M_PI);
134 	}
135 }
136 
137 /**
138  * Returns the list of crafts currently
139  * following this target.
140  * @return Pointer to list of crafts.
141  */
getFollowers()142 std::vector<Target*> *Target::getFollowers()
143 {
144 	return &_followers;
145 }
146 
147 /**
148  * Returns the great circle distance to another
149  * target on the globe.
150  * @param target Pointer to other target.
151  * @returns Distance in radian.
152  */
getDistance(const Target * target) const153 double Target::getDistance(const Target *target) const
154 {
155 	return acos(cos(_lat) * cos(target->getLatitude()) * cos(target->getLongitude() - _lon) + sin(_lat) * sin(target->getLatitude()));
156 }
157 
158 }
159