Repository navigation
Expand file tree
/
Copy pathMovingTarget.cpp
More file actions
203 lines (188 loc) · 4.89 KB
/
Copy pathMovingTarget.cpp
File metadata and controls
203 lines (188 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#define _USE_MATH_DEFINES
#include "MovingTarget.h"
#include <cmath>
namespace OpenXcom
{
/**
* Initializes a moving target with blank coordinates.
*/
MovingTarget::MovingTarget() : Target(), _dest(0), _speedLon(0.0), _speedLat(0.0), _speed(0)
{
}
MovingTarget::~MovingTarget()
{
}
/**
* Loads the moving target from a YAML file.
* @param node YAML node.
*/
void MovingTarget::load(const YAML::Node &node)
{
Target::load(node);
node["speedLon"] >> _speedLon;
node["speedLat"] >> _speedLat;
node["speed"] >> _speed;
}
/**
* Saves the moving target to a YAML file.
* @param out YAML emitter.
*/
void MovingTarget::save(YAML::Emitter &out) const
{
Target::save(out);
if (_dest != 0)
{
out << YAML::Key << "dest" << YAML::Value;
_dest->saveId(out);
}
out << YAML::Key << "speedLon" << YAML::Value << _speedLon;
out << YAML::Key << "speedLat" << YAML::Value << _speedLat;
out << YAML::Key << "speed" << YAML::Value << _speed;
}
/**
* Returns the destination the moving target is heading to.
* @return Pointer to destination.
*/
Target *const MovingTarget::getDestination() const
{
return _dest;
}
/**
* Changes the destination the moving target is heading to.
* @param dest Pointer to destination.
*/
void MovingTarget::setDestination(Target *dest)
{
// Remove moving target from old destination's followers
if (_dest != 0)
{
for (std::vector<Target*>::iterator i = _dest->getFollowers()->begin(); i != _dest->getFollowers()->end(); i++)
{
if ((*i) == this)
{
_dest->getFollowers()->erase(i);
break;
}
}
}
_dest = dest;
// Add moving target to new destination's followers
if (_dest != 0)
{
_dest->getFollowers()->push_back(this);
}
calculateSpeed();
}
/**
* Returns the speed of the moving target.
* @return Speed in knots.
*/
int MovingTarget::getSpeed() const
{
return _speed;
}
/**
* Changes the speed of the moving target.
* @param speed Speed in knots.
*/
void MovingTarget::setSpeed(int speed)
{
_speed = speed;
calculateSpeed();
}
/**
* Converts the speed from the standard knots (nautical miles per hour),
* and converts it into radians per 5 in-game seconds.
* @return Speed in radians.
*/
double MovingTarget::getRadianSpeed() const
{
// Each nautical mile is 1/60th of a degree.
// Each hour contains 300 5-seconds.
return _speed * (1 / 60.0) * (M_PI / 180) / 300.0;
}
/**
* Returns the distance between this moving target and any other
* target on the globe, accounting for the wrap-around.
* @param target Pointer to other target.
* @param dLon Returned longitude difference.
* @param dLat Returned latitude difference.
* @returns Distance in radian.
*/
double MovingTarget::getDistance(Target *target, double *dLon, double *dLat) const
{
double minLength = 2*M_PI, lat = target->getLatitude();
for (double lon = target->getLongitude() - 2*M_PI; lon <= target->getLongitude() + 2*M_PI; lon += 2*M_PI)
{
double dx = lon - _lon;
double dy = lat - _lat;
double length = sqrt(dx * dx + dy * dy);
if (length < minLength)
{
minLength = length;
*dLon = dx;
*dLat = dy;
}
}
return minLength;
}
/**
* Calculates the speed vector for the moving target
* based on the current raw speed and destination.
*/
void MovingTarget::calculateSpeed()
{
if (_dest != 0)
{
double dLon, dLat;
double length = getDistance(_dest, &dLon, &dLat);
_speedLon = dLon / length * getRadianSpeed();
_speedLat = dLat / length * getRadianSpeed();
}
else
{
_speedLon = 0;
_speedLat = 0;
}
}
/**
* Checks if the moving target has finished its route by checking
* if it has exceeded the destination position based on the speed vector.
* @return True if it has, False otherwise.
*/
bool MovingTarget::finishedRoute() const
{
return (((_speedLon > 0 && _lon >= _dest->getLongitude()) || (_speedLon < 0 && _lon <= _dest->getLongitude()) || _speedLon == 0) &&
((_speedLat > 0 && _lat >= _dest->getLatitude()) || (_speedLat < 0 && _lat <= _dest->getLatitude()) || _speedLat == 0));
}
/**
* Checks if the moving target has reached its destination.
* @return True if it has, False otherwise.
*/
bool MovingTarget::reachedDestination() const
{
if (_dest == 0)
{
return false;
}
return (_lon == _dest->getLongitude() && _lat == _dest->getLatitude());
}
}