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