1 // FGAIEscort - FGAIShip-derived class creates an AI Ground Vehicle
2 // by adding a ground following utility
4 // Written by Vivian Meazza, started August 2009.
5 // - vivian.meazza at lineone.net
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/sg_inlines.h>
30 #include <simgear/math/SGMath.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
34 #include <Main/util.hxx>
35 #include <Main/viewer.hxx>
37 #include <Scenery/scenery.hxx>
39 #include "AIEscort.hxx"
43 FGAIEscort::FGAIEscort() :
63 FGAIEscort::~FGAIEscort() {}
65 void FGAIEscort::readFromScenario(SGPropertyNode* scFileNode) {
69 FGAIShip::readFromScenario(scFileNode);
71 setName(scFileNode->getStringValue("name", "Escort"));
72 setSMPath(scFileNode->getStringValue("submodel-path", ""));
73 setStnRange(scFileNode->getDoubleValue("station/range-nm", 1));
74 setStnBrg(scFileNode->getDoubleValue("station/brg-deg", 0.0));
75 setStnLimit(scFileNode->getDoubleValue("station/range-limit-nm", 0.2));
76 setStnAngleLimit(scFileNode->getDoubleValue("station/angle-limit-deg", 15.0));
77 setStnSpeed(scFileNode->getDoubleValue("station/speed-kts", 2.5));
78 setStnPatrol(scFileNode->getBoolValue("station/patrol", false));
79 setStnHtFt(scFileNode->getDoubleValue("station/height-ft", 0.0));
80 setStnDegTrue(scFileNode->getBoolValue("station/deg-true", false));
81 setParentName(scFileNode->getStringValue("station/parent", ""));
82 setMaxSpeed(scFileNode->getDoubleValue("max-speed-kts", 30.0));
83 setUpdateInterval(scFileNode->getDoubleValue("update-interval-sec", 10.0));
84 setCallSign(scFileNode->getStringValue("callsign", ""));
91 void FGAIEscort::bind() {
94 props->tie("station/rel-bearing-deg",
95 SGRawValuePointer<double>(&_stn_relbrg));
96 props->tie("station/true-bearing-deg",
97 SGRawValuePointer<double>(&_stn_truebrg));
98 props->tie("station/range-nm",
99 SGRawValuePointer<double>(&_stn_range));
100 props->tie("station/range-limit-nm",
101 SGRawValuePointer<double>(&_stn_limit));
102 props->tie("station/angle-limit-deg",
103 SGRawValuePointer<double>(&_stn_angle_limit));
104 props->tie("station/speed-kts",
105 SGRawValuePointer<double>(&_stn_speed));
106 props->tie("station/height-ft",
107 SGRawValuePointer<double>(&_stn_height));
108 props->tie("controls/update-interval-sec",
109 SGRawValuePointer<double>(&_interval));
110 props->tie("controls/parent-mp-control",
111 SGRawValuePointer<bool>(&_MPControl));
112 props->tie("station/target-range-nm",
113 SGRawValuePointer<double>(&_tgtrange));
114 props->tie("station/target-brg-deg-t",
115 SGRawValuePointer<double>(&_tgtbrg));
116 props->tie("station/patrol",
117 SGRawValuePointer<bool>(&_patrol));
120 void FGAIEscort::unbind() {
123 props->untie("station/rel-bearing-deg");
124 props->untie("station/true-bearing-deg");
125 props->untie("station/range-nm");
126 props->untie("station/range-limit-nm");
127 props->untie("station/angle-limit-deg");
128 props->untie("station/speed-kts");
129 props->untie("station/height-ft");
130 props->untie("controls/update-interval-sec");
134 bool FGAIEscort::init(bool search_in_AI_path) {
135 if (!FGAIShip::init(search_in_AI_path))
141 props->setStringValue("controls/parent-name", _parent.c_str());
143 if (setParentNode()){
146 speed = _parent_speed;
153 void FGAIEscort::update(double dt) {
154 FGAIShip::update(dt);
159 void FGAIEscort::setStnRange(double r) {
163 void FGAIEscort::setStnBrg(double b) {
167 void FGAIEscort::setStnLimit(double l) {
171 void FGAIEscort::setStnAngleLimit(double al) {
172 _stn_angle_limit = al;
175 void FGAIEscort::setStnSpeed(double s) {
179 void FGAIEscort::setStnHtFt(double h) {
183 void FGAIEscort::setStnDegTrue(bool t) {
187 void FGAIEscort::setMaxSpeed(double m) {
191 void FGAIEscort::setUpdateInterval(double i) {
195 void FGAIEscort::setStnPatrol(bool p) {
199 bool FGAIEscort::getGroundElev(SGGeod inpos) {
203 if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(inpos, 3000), height_m, &_material,0)){
204 _ht_agl_ft = inpos.getElevationFt() - height_m * SG_METER_TO_FEET;
207 const vector<string>& names = _material->get_names();
209 _solid = _material->get_solid();
212 props->setStringValue("material/name", names[0].c_str());
214 props->setStringValue("material/name", "");
216 //cout << "material " << names[0].c_str()
217 // << " _elevation_m " << _elevation_m
218 // << " solid " << _solid
219 // << " load " << _load_resistance
220 // << " frictionFactor " << _frictionFactor
232 void FGAIEscort::setParent()
234 double lat = _selected_ac->getDoubleValue("position/latitude-deg");
235 double lon = _selected_ac->getDoubleValue("position/longitude-deg");
236 double elevation = _selected_ac->getDoubleValue("position/altitude-ft");
237 _MPControl = _selected_ac->getBoolValue("controls/mp-control");
239 _selectedpos.setLatitudeDeg(lat);
240 _selectedpos.setLongitudeDeg(lon);
241 _selectedpos.setElevationFt(elevation);
243 _parent_speed = _selected_ac->getDoubleValue("velocities/speed-kts");
244 _parent_hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg");
247 _stn_truebrg = calcTrueBearingDeg(_stn_brg, _parent_hdg);
248 _stn_relbrg = _stn_brg;
249 //cout << _name <<" set rel"<<endl;
251 _stn_truebrg = _stn_brg;
252 _stn_relbrg = calcRelBearingDeg(_stn_brg, _parent_hdg);
253 //cout << _name << " set true"<<endl;
258 SGGeodesy::direct( _selectedpos, _stn_truebrg, _stn_range * SG_NM_TO_METER,
261 _tgtpos.setElevationFt(_stn_height);
263 calcRangeBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
264 _tgtpos.getLatitudeDeg(), _tgtpos.getLongitudeDeg(), _tgtrange, _tgtbrg);
266 _relbrg = calcRelBearingDeg(_tgtbrg, hdg);
270 void FGAIEscort::calcRangeBearing(double lat, double lon, double lat2, double lon2,
271 double &range, double &bearing) const
273 // calculate the bearing and range of the second pos from the first
274 double az2, distance;
275 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
276 range = distance * SG_METER_TO_NM;
279 double FGAIEscort::calcTrueBearingDeg(double bearing, double heading)
281 double angle = bearing + heading;
282 SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
286 SGVec3d FGAIEscort::getCartHitchPosAt(const SGVec3d& _off) const {
287 double hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg");
288 double pitch = _selected_ac->getDoubleValue("orientation/pitch-deg");
289 double roll = _selected_ac->getDoubleValue("orientation/roll-deg");
291 // Transform that one to the horizontal local coordinate system.
292 SGQuatd hlTrans = SGQuatd::fromLonLat(_selectedpos);
294 // and postrotate the orientation of the AIModel wrt the horizontal
296 hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
298 // The offset converted to the usual body fixed coordinate system
299 // rotated to the earth fiexed coordinates axis
300 SGVec3d off = hlTrans.backTransform(_off);
302 // Add the position offset of the AIModel to gain the earth centered position
303 SGVec3d cartPos = SGVec3d::fromGeod(_selectedpos);
305 return cartPos + off;
309 void FGAIEscort::setStationSpeed(){
314 // these are the AI rules for the manoeuvring of escorts
316 if (_MPControl && _tgtrange > 4 * _stn_limit){
317 SG_LOG(SG_GENERAL, SG_ALERT, "AIEscort: " << _name
318 << " re-aligning to MP pos");
322 }else if ((_relbrg < -90 || _relbrg > 90) && _tgtrange > _stn_limit ){
325 if(_tgtrange > 4 * _stn_limit)
326 speed = 4 * -_stn_speed;
330 }else if ((_relbrg >= -90 || _relbrg <= 90) && _tgtrange > _stn_limit){
333 if(_tgtrange > 4 * _stn_limit)
334 speed = 4 * _stn_speed;
341 angle = 15 * sg_random();
342 speed = 5 * sg_random();
350 double station_speed = _parent_speed + speed;
352 SG_CLAMP_RANGE(station_speed, 5.0, _max_speed);
353 SG_CLAMP_RANGE(angle, -_stn_angle_limit, _stn_angle_limit);
355 AccelTo(station_speed);
356 TurnTo(_parent_hdg + angle);
357 ClimbTo(_stn_height);
361 void FGAIEscort::RunEscort(double dt){
367 ///////////////////////////////////////////////////////////////////////////
368 // Check execution time (currently once every 0.05 sec or 20 fps)
369 // Add a bit of randomization to prevent the execution of all flight plans
370 // in synchrony, which can add significant periodic framerate flutter.
371 // Randomization removed to get better appearance
372 ///////////////////////////////////////////////////////////////////////////
374 //cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
375 if (_dt_count < _next_run)
377 _next_run = _interval /*+ (0.015 * sg_random())*/;