]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIEscort.cxx
Merge branch 'next' into attenuation
[flightgear.git] / src / AIModel / AIEscort.cxx
1 // FGAIEscort - FGAIShip-derived class creates an AI Ground Vehicle
2 // by adding a ground following utility
3 //
4 // Written by Vivian Meazza, started August 2009.
5 // - vivian.meazza at lineone.net
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28
29 #include <simgear/sg_inlines.h>
30 #include <simgear/math/SGMath.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
32
33 #include <math.h>
34 #include <Main/util.hxx>
35 #include <Main/viewer.hxx>
36
37 #include <Scenery/scenery.hxx>
38
39 #include "AIEscort.hxx"
40
41 using std::string;
42
43 FGAIEscort::FGAIEscort() :
44 FGAIShip(otEscort),
45
46 _relbrg (0),
47 _parent_speed(0),
48 _interval(0),
49 _stn_truebrg(0),
50 _stn_height(0),
51 _stn_speed(0),
52 _stn_angle_limit(0),
53 _stn_limit(0),
54 _max_speed(0),
55 _MPControl(false),
56 _patrol(false),
57 _stn_deg_true(false)
58
59 {
60     invisible = false;
61 }
62
63 FGAIEscort::~FGAIEscort() {}
64
65 void FGAIEscort::readFromScenario(SGPropertyNode* scFileNode) {
66     if (!scFileNode)
67         return;
68
69     FGAIShip::readFromScenario(scFileNode);
70
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", ""));
85
86     if(_patrol)
87         sg_srandom_time();
88
89 }
90
91 void FGAIEscort::bind() {
92     FGAIShip::bind();
93
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));
118 }
119
120 void FGAIEscort::unbind() {
121     FGAIShip::unbind();
122
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");
131
132 }
133
134 bool FGAIEscort::init(bool search_in_AI_path) {
135     if (!FGAIShip::init(search_in_AI_path))
136         return false;
137     reinit();
138     return true;
139 }
140
141 void FGAIEscort::reinit() {
142     invisible = false;
143     no_roll = false;
144
145     props->setStringValue("controls/parent-name", _parent.c_str());
146
147     if (setParentNode()){
148         setParent();
149         pos = _tgtpos;
150         speed = _parent_speed;
151         hdg = _parent_hdg;
152     }
153
154     FGAIShip::reinit();
155 }
156
157 void FGAIEscort::update(double dt) {
158     FGAIShip::update(dt);
159
160     RunEscort(dt);
161 }
162
163 void FGAIEscort::setStnRange(double r) {
164     _stn_range = r;
165 }
166
167 void FGAIEscort::setStnBrg(double b) {
168     _stn_brg = b;
169 }
170
171 void FGAIEscort::setStnLimit(double l) {
172     _stn_limit = l;
173 }
174
175 void FGAIEscort::setStnAngleLimit(double al) {
176     _stn_angle_limit = al;
177 }
178
179 void FGAIEscort::setStnSpeed(double s) {
180     _stn_speed = s;
181 }
182
183 void FGAIEscort::setStnHtFt(double h) {
184     _stn_height = h;
185 }
186
187 void FGAIEscort::setStnDegTrue(bool t) {
188     _stn_deg_true = t;
189 }
190
191 void FGAIEscort::setMaxSpeed(double m) {
192     _max_speed = m;
193 }
194
195 void FGAIEscort::setUpdateInterval(double i) {
196     _interval = i;
197 }
198
199 void FGAIEscort::setStnPatrol(bool p) {
200     _patrol = p;
201 }
202
203 bool FGAIEscort::getGroundElev(SGGeod inpos) {
204
205     double height_m ;
206
207     if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(inpos, 3000), height_m, &_material,0)){
208         _ht_agl_ft = inpos.getElevationFt() - height_m * SG_METER_TO_FEET;
209
210         if (_material) {
211             const vector<string>& names = _material->get_names();
212
213             _solid = _material->get_solid();
214
215             if (!names.empty())
216                 props->setStringValue("material/name", names[0].c_str());
217             else
218                 props->setStringValue("material/name", "");
219
220             //cout << "material " << names[0].c_str()
221             //    << " _elevation_m " << _elevation_m
222             //    << " solid " << _solid
223             //    << " load " << _load_resistance
224             //    << " frictionFactor " << _frictionFactor
225             //    << endl;
226
227         }
228
229         return true;
230     } else {
231         return false;
232     }
233
234 }
235
236 void FGAIEscort::setParent()
237 {
238     double lat = _selected_ac->getDoubleValue("position/latitude-deg");
239     double lon = _selected_ac->getDoubleValue("position/longitude-deg");
240     double elevation = _selected_ac->getDoubleValue("position/altitude-ft");
241     _MPControl = _selected_ac->getBoolValue("controls/mp-control");
242
243     _selectedpos.setLatitudeDeg(lat);
244     _selectedpos.setLongitudeDeg(lon);
245     _selectedpos.setElevationFt(elevation);
246
247     _parent_speed    = _selected_ac->getDoubleValue("velocities/speed-kts");
248     _parent_hdg      = _selected_ac->getDoubleValue("orientation/true-heading-deg");
249
250     if(!_stn_deg_true){
251         _stn_truebrg = calcTrueBearingDeg(_stn_brg, _parent_hdg);
252         _stn_relbrg = _stn_brg;
253         //cout << _name <<" set rel"<<endl;
254     } else {
255         _stn_truebrg = _stn_brg;
256         _stn_relbrg = calcRelBearingDeg(_stn_brg, _parent_hdg); 
257         //cout << _name << " set true"<<endl;
258     }
259
260     double course2;
261
262     SGGeodesy::direct( _selectedpos, _stn_truebrg, _stn_range * SG_NM_TO_METER,
263         _tgtpos, course2);
264
265     _tgtpos.setElevationFt(_stn_height);
266
267     calcRangeBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
268         _tgtpos.getLatitudeDeg(), _tgtpos.getLongitudeDeg(), _tgtrange, _tgtbrg);
269
270     _relbrg = calcRelBearingDeg(_tgtbrg, hdg);
271
272 }
273
274 void FGAIEscort::calcRangeBearing(double lat, double lon, double lat2, double lon2,
275                                   double &range, double &bearing) const
276 {
277     // calculate the bearing and range of the second pos from the first
278     double az2, distance;
279     geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
280     range = distance * SG_METER_TO_NM;
281 }
282
283 double FGAIEscort::calcTrueBearingDeg(double bearing, double heading)
284 {
285     double angle = bearing + heading;
286     SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
287     return angle;
288 }
289
290 SGVec3d FGAIEscort::getCartHitchPosAt(const SGVec3d& _off) const {
291     double hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg");
292     double pitch = _selected_ac->getDoubleValue("orientation/pitch-deg");
293     double roll = _selected_ac->getDoubleValue("orientation/roll-deg");
294
295     // Transform that one to the horizontal local coordinate system.
296     SGQuatd hlTrans = SGQuatd::fromLonLat(_selectedpos);
297
298     // and postrotate the orientation of the AIModel wrt the horizontal
299     // local frame
300     hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
301
302     // The offset converted to the usual body fixed coordinate system
303     // rotated to the earth fiexed coordinates axis
304     SGVec3d off = hlTrans.backTransform(_off);
305
306     // Add the position offset of the AIModel to gain the earth centered position
307     SGVec3d cartPos = SGVec3d::fromGeod(_selectedpos);
308
309     return cartPos + off;
310 }
311
312
313 void FGAIEscort::setStationSpeed(){
314
315     double speed = 0;
316     double angle = 0;
317
318     // these are the AI rules for the manoeuvring of escorts
319
320     if (_MPControl && _tgtrange > 4 * _stn_limit){
321         SG_LOG(SG_AI, SG_ALERT, "AIEscort: " << _name
322             << " re-aligning to MP pos");
323         pos = _tgtpos;
324         speed = 0;
325         angle = 0;
326     }else if ((_relbrg < -90 || _relbrg > 90) && _tgtrange > _stn_limit ){
327         angle =_relbrg;
328
329         if(_tgtrange > 4 * _stn_limit)
330             speed = 4 * -_stn_speed;
331         else
332             speed = -_stn_speed;
333
334     }else if ((_relbrg >= -90 || _relbrg <= 90) && _tgtrange > _stn_limit){
335         angle = _relbrg;
336
337         if(_tgtrange > 4 * _stn_limit)
338             speed = 4 * _stn_speed;
339         else
340             speed = _stn_speed;
341
342     } else {
343
344         if(_patrol){
345             angle = 15 * sg_random();
346             speed =  5 * sg_random();
347         } else {
348             angle = 0;
349             speed = 0;
350         }
351
352     }
353
354     double station_speed = _parent_speed + speed;
355
356     SG_CLAMP_RANGE(station_speed, 5.0, _max_speed);
357     SG_CLAMP_RANGE(angle, -_stn_angle_limit, _stn_angle_limit);
358
359     AccelTo(station_speed);
360     TurnTo(_parent_hdg + angle);
361     ClimbTo(_stn_height);
362
363 }
364
365 void FGAIEscort::RunEscort(double dt){
366
367     _dt_count += dt;
368
369
370
371     ///////////////////////////////////////////////////////////////////////////
372     // Check execution time (currently once every 0.05 sec or 20 fps)
373     // Add a bit of randomization to prevent the execution of all flight plans
374     // in synchrony, which can add significant periodic framerate flutter.
375     // Randomization removed to get better appearance
376     ///////////////////////////////////////////////////////////////////////////
377
378     //cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
379     if (_dt_count < _next_run)
380         return;
381     _next_run = _interval /*+ (0.015 * sg_random())*/;
382
383     if(_parent == ""){
384         return;
385     }
386
387     setParent();
388     setStationSpeed();
389
390     _dt_count = 0;
391
392 }
393
394 // end AIEscort