]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIGAVFRTraffic.cxx
Move the mechanics of turning out of the derived classes into AIPlane. The user...
[flightgear.git] / src / ATC / AIGAVFRTraffic.cxx
1 // FGAILocalTraffic - AIEntity derived class with enough logic to
2 // fly and interact with the traffic pattern.
3 //
4 // Written by David Luff, started March 2002.
5 //
6 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 //#include <simgear/scene/model/location.hxx>
27
28 #include <Airports/runways.hxx>
29 #include <Main/globals.hxx>
30 //#include <Scenery/scenery.hxx>
31 //#include <Scenery/tilemgr.hxx>
32 #include <simgear/math/point3d.hxx>
33 //#include <simgear/math/sg_geodesy.hxx>
34 //#include <simgear/misc/sg_path.hxx>
35 #include <string>
36 #include <math.h>
37
38 SG_USING_STD(string);
39
40 #include "ATCmgr.hxx"
41 #include "AILocalTraffic.hxx"
42 #include "AIGAVFRTraffic.hxx"
43 #include "ATCutils.hxx"
44
45 FGAIGAVFRTraffic::FGAIGAVFRTraffic() {
46         ATC = globals->get_ATC_mgr();
47         _towerContactedIncoming = false;
48         _clearedStraightIn = false;
49         _clearedDownwindEntry = false;
50         _incoming = false;
51         _straightIn = false;
52         _downwindEntry = false;
53         _climbout = false;
54         _local = false;
55         _established = false;
56         _e45 = false;
57         _entering = false;
58         _turning = false;
59         _cruise_climb_ias = 90.0;
60         _cruise_ias = 110.0;
61         patternDirection = -1.0;
62         
63         // TESTING - REMOVE OR COMMENT OUT BEFORE COMMIT!!!
64         //_towerContactPrinted = false;
65 }
66
67 FGAIGAVFRTraffic::~FGAIGAVFRTraffic() {
68 }
69
70 // We should never need to Init FGAIGAVFRTraffic in the pattern since that implies arrivel
71 // and we can just use an FGAILocalTraffic instance for that instead.
72
73 // Init en-route to destID at point pt.
74 // TODO - no idea what to do if pt is above planes ceiling due mountains!!
75 bool FGAIGAVFRTraffic::Init(Point3D pt, string destID, const string& callsign) {
76         FGAILocalTraffic::Init(callsign, destID, EN_ROUTE);
77         // TODO FIXME - to get up and running we're going to ignore elev and get FGAIMgr to 
78         // pass in known good values for the test location.  Need to fix this!!! (or at least canonically decide who has responsibility for setting elev).
79         _enroute = true;
80         _destID = destID;
81         _pos = pt;
82         _destPos = dclGetAirportPos(destID);    // TODO - check if we are within the tower catchment area already.
83         _cruise_alt = (_destPos.elev() + 2500.0) * SG_FEET_TO_METER;    // TODO look at terrain elevation as well
84         _pos.setelev(_cruise_alt);
85         // initially set waypoint as airport location
86         _wp = _destPos;
87         //_hdg = GetHeadingFromTo(_pos, _wp);
88         SetTrack(GetHeadingFromTo(_pos, _wp));
89         _roll = 0.0;
90         _pitch = 0.0;
91         slope = 0.0;
92         // TODO - set climbout if altitude is below normal cruising altitude?
93         //Transform();
94         // Assume it's OK to set the plane visible
95         _aip.setVisible(true);
96         //cout << "Setting visible true\n";
97         Transform();
98         return(true);
99 }
100
101 // Init at srcID to fly to destID
102 bool FGAIGAVFRTraffic::Init(string srcID, string destID, const string& callsign, OperatingState state) {
103         _enroute = false;
104         FGAILocalTraffic::Init(callsign, srcID, PARKED);
105         return(true);
106 }
107
108 void FGAIGAVFRTraffic::Update(double dt) {
109         if(_enroute) {
110                 //cout << "_enroute\n";
111                 //cout << "e" << flush;
112                 FlyPlane(dt);
113                 //cout << "f" << flush;
114                 Transform();
115                 //cout << "g" << flush;
116                 FGAIPlane::Update(dt);
117                 //cout << "h" << flush;
118                 responseCounter += dt;
119                 
120                 // we shouldn't really need this since there's a LOD of 10K on the whole plane anyway I think.
121                 // There are two _aip.setVisible statements set when _local = true that can be removed if the below is removed.
122                 if(dclGetHorizontalSeparation(_pos, Point3D(fgGetDouble("/position/longitude-deg"), fgGetDouble("/position/latitude-deg"), 0.0)) > 8000) _aip.setVisible(false);
123                 else _aip.setVisible(true);
124                 
125         } else if(_local) {
126                 //cout << "L";
127                 //cout << "_local\n";
128                 FGAILocalTraffic::Update(dt);
129         }
130 }
131
132 void FGAIGAVFRTraffic::FlyPlane(double dt) {
133         if(_climbout) {
134                 // Check whether to level off
135                 if(_pos.elev() >= _cruise_alt) {
136                         slope = 0.0;
137                         _pitch = 0.0;
138                         IAS = _cruise_ias;              // FIXME - use smooth transistion to new speed and attitude.
139                         _climbout = false;
140                 } else {
141                         slope = 4.0;
142                         _pitch = 5.0;
143                         IAS = _cruise_climb_ias;
144                 }
145         } else {
146                 // TESTING
147                 /*
148                 if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.1) {
149                         if(!_towerContactPrinted) {
150                                 if(airportID == "KSQL") {
151                                         cout << "****************************************************************\n";
152                                         cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
153                                         cout << "****************************************************************\n";
154                                 }
155                                 _towerContactPrinted = true;
156                         }
157                 }
158                 */
159                 
160                 // if distance to destination is less than 6 - 9 miles contact tower
161                 // and prepare to become _incoming after response.
162                 // Possibly check whether to start descent before this?
163                 //cout << "." << flush;
164                 //cout << "sep = " << dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 << '\n';
165                 if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.0) {
166                         //cout << "-" << flush;
167                         if(!_towerContactedIncoming) {
168                                 //cout << "_" << flush;
169                                 GetAirportDetails(airportID);
170                                 //cout << "L" << flush;
171                                 // TODO FIXME TODO - need to check that tower is valid before this else if problem -> BOOM!
172                                 freq = (double)tower->get_freq() / 100.0;
173                                 tuned_station = tower;
174                                 //cout << "freq = " << freq << endl;
175                                 GetRwyDetails(airportID);
176                                 //"@AP Tower @CS @MI miles @CD of the airport for full stop with the ATIS"
177                                 // At the bare minimum we ought to make sure it goes the right way at dual parallel rwy airports!
178                                 if(rwy.rwyID.size() == 3) {
179                                         patternDirection = (rwy.rwyID.substr(2,1) == "R" ? 1 : -1);
180                                 }
181                                 pending_transmission = tower->get_name();
182                                 pending_transmission += " Tower ";
183                                 pending_transmission += plane.callsign;
184                                 //char buf[10];
185                                 int dist_miles = (int)dclGetHorizontalSeparation(_pos, _destPos) / 1600;
186                                 //sprintf(buf, " %i ", dist_miles);
187                                 pending_transmission += " ";
188                                 pending_transmission += ConvertNumToSpokenDigits(dist_miles);
189                                 if(dist_miles > 1) pending_transmission += " miles ";
190                                 else pending_transmission += " mile ";
191                                 pending_transmission += GetCompassDirection(GetHeadingFromTo(_destPos, _pos));
192                                 pending_transmission += " of the airport for full stop with the ATIS";
193                                 //cout << pending_transmission << endl;
194                                 Transmit(14);   // 14 is the callback code, NOT the timeout!
195                                 responseCounter = 0;
196                                 _towerContactedIncoming = true;
197                         } else {
198                                 //cout << "?" << flush;
199                                 if(_clearedStraightIn && responseCounter > 5.5) {
200                                         //cout << "5 " << flush;
201                                         _clearedStraightIn = false;
202                                         _straightIn = true;
203                                         _incoming = true;
204                                         _wp = GetPatternApproachPos();
205                                         //_hdg = GetHeadingFromTo(_pos, _wp);   // TODO - turn properly!
206                                         SetTrack(GetHeadingFromTo(_pos, _wp));
207                                         slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
208                                         double thesh_offset = 0.0;
209                                         Point3D opos = ortho.ConvertToLocal(_pos);
210                                         double angToApt = atan((_pos.elev() - dclGetAirportElev(airportID)) / (opos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
211                                         //cout << "angToApt = " << angToApt << ' ';
212                                         slope = (angToApt > -5.0 ? 0.0 : angToApt);
213                                         //cout << "slope = " << slope << '\n';
214                                         pending_transmission = "Straight-in ";
215                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
216                                         pending_transmission += " ";
217                                         pending_transmission += plane.callsign;
218                                         //cout << pending_transmission << '\n';
219                                         ConditionalTransmit(4);
220                                 } else if(_clearedDownwindEntry && responseCounter > 5.5) {
221                                         //cout << "6" << flush;
222                                         _clearedDownwindEntry = false;
223                                         _downwindEntry = true;
224                                         _incoming = true;
225                                         _wp = GetPatternApproachPos();
226                                         SetTrack(GetHeadingFromTo(_pos, _wp));
227                                         slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
228                                         //cout << "slope = " << slope << '\n';
229                                         pending_transmission = "Report ";
230                                         pending_transmission += (patternDirection == 1 ? "right downwind " : "left downwind ");
231                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
232                                         pending_transmission += " ";
233                                         pending_transmission += plane.callsign;
234                                         //cout << pending_transmission << '\n';
235                                         ConditionalTransmit(4);
236                                 }
237                         }
238                         if(_pos.elev() < (dclGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;     
239                 }
240         }
241         if(_incoming) {
242                 //cout << "i" << '\n';
243                 Point3D orthopos = ortho.ConvertToLocal(_pos);
244                 // TODO - Check whether to start descent
245                 // become _local after the 3 mile report.
246                 if(_pos.elev() < (dclGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;     
247                 // TODO - work out why I needed to add the above line to stop the plane going underground!!!
248                 // (Although it's worth leaving it in as a robustness check anyway).
249                 if(_straightIn) {
250                         //cout << "A " << flush;
251                         if(fabs(orthopos.x()) < 10.0 && !_established) {
252                                 SetTrack(rwy.hdg);
253                                 _established = true;
254                                 //cout << "Established at " << orthopos << '\n';
255                         }
256                         double thesh_offset = 30.0;
257                         //cout << "orthopos.y = " << orthopos.y() << " alt = " << _pos.elev() - dclGetAirportElev(airportID) << '\n';
258                         if(_established && (orthopos.y() > -5400.0)) {
259                                 slope = atan((_pos.elev() - dclGetAirportElev(airportID)) / (orthopos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
260                                 //cout << "slope0 = " << slope << '\n';
261                         }
262                         //cout << "slope1 = " << slope << '\n';
263                         if(slope > -5.5) slope = 0.0;   // ie we're too low.
264                         //cout << "slope2 = " << slope << '\n';
265                         slope += 0.001;         // To avoid yo-yoing with the above.
266                         //if(_established && (orthopos.y() > -5400.0)) slope = -5.5;
267                         if(_established && (orthopos.y() > -4800.0)) {
268                                 pending_transmission = "3 mile final Runway ";
269                                 pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
270                                 pending_transmission += " ";
271                                 pending_transmission += plane.callsign;
272                                 //cout << pending_transmission << '\n';
273                                 ConditionalTransmit(35);
274                                 _local = true;
275                                 _aip.setVisible(true);  // HACK
276                                 _enroute = false;
277                                 StraightInEntry(true);
278                         }
279                 } else if(_downwindEntry) {
280                         //cout << "B" << flush;
281                         if(_entering) {
282                                 //cout << "C" << flush;
283                                 if(_turning) {
284                                         if(fabs(_hdg - (rwy.hdg + 180)) < 2.0) {        // TODO - use track instead of _hdg?
285                                                 //cout << "Going Local...\n";
286                                                 leg = DOWNWIND;
287                                                 _local = true;
288                                                 _aip.setVisible(true);  // HACK
289                                                 _enroute = false;
290                                                 _entering = false;
291                                                 _turning = false;
292                                                 DownwindEntry();
293                                         }
294                                 }
295                                 if(fabs(orthopos.x() - (patternDirection == 1 ? 1000 : -1000)) < (_e45 ? 175 : 550)) {  // Caution - hardwired turn clearances.
296                                         //cout << "_turning...\n";
297                                         _turning = true;
298                                         SetTrack(rwy.hdg + 180.0);
299                                 }       // TODO - need to check for other traffic in the pattern and enter much more integilently than that!!!
300                         } else {
301                                 //cout << "D" << flush;
302                                 //cout << '\n' << dclGetHorizontalSeparation(_wp, _pos) << '\n';
303                                 //cout << ortho.ConvertToLocal(_pos);
304                                 //cout << ortho.ConvertToLocal(_wp);
305                                 if(dclGetHorizontalSeparation(_wp, _pos) < 100.0) {
306                                         pending_transmission = "2 miles out for ";
307                                         pending_transmission += (patternDirection == 1 ? "right " : "left ");
308                                         pending_transmission += "downwind Runway ";
309                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
310                                         pending_transmission += " ";
311                                         pending_transmission += plane.callsign;
312                                         //cout << pending_transmission << '\n';
313                                         // TODO - are we at pattern altitude??
314                                         slope = 0.0;
315                                         ConditionalTransmit(30);
316                                         if(_e45) {
317                                                 SetTrack(patternDirection == 1 ? rwy.hdg - 135.0 : rwy.hdg + 135.0);
318                                         } else {
319                                                 SetTrack(patternDirection == 1 ? rwy.hdg + 90.0 : rwy.hdg - 90.0);
320                                         }
321                                         //if(_hdg < 0.0) _hdg += 360.0;
322                                         _entering = true;
323                                 } else {
324                                         SetTrack(GetHeadingFromTo(_pos, _wp));
325                                 }
326                         }       
327                 }
328         } else {
329                 // !_incoming
330                 slope = 0.0;
331         }
332         // FIXME - lots of hackery in the next six lines!!!!
333         //double track = _hdg;
334         double crab = 0.0;      // This is a placeholder for when we take wind into account.    
335         _hdg = track + crab;
336         double vel = _cruise_ias;
337         double dist = vel * 0.514444 * dt;
338         _pos = dclUpdatePosition(_pos, track, slope, dist);
339 }
340
341 void FGAIGAVFRTraffic::RegisterTransmission(int code) {
342         switch(code) {
343         case 1: // taxi request cleared
344                 FGAILocalTraffic::RegisterTransmission(code);
345                 break;
346         case 2: // contact tower
347                 FGAILocalTraffic::RegisterTransmission(code);
348                 break;
349         case 3: // Cleared to line up
350                 FGAILocalTraffic::RegisterTransmission(code);
351                 break;
352         case 4: // cleared to take-off
353                 FGAILocalTraffic::RegisterTransmission(code);
354                 break;
355         case 5: // contact ground
356                 FGAILocalTraffic::RegisterTransmission(code);
357                 break;
358         case 6: // taxi to the GA parking
359                 FGAILocalTraffic::RegisterTransmission(code);
360                 break;
361         case 7: // Cleared to land
362                 FGAILocalTraffic::RegisterTransmission(code);
363                 break;
364         case 13: // Go around!
365                 FGAILocalTraffic::RegisterTransmission(code);
366                 break;
367         case 14: // VFR approach for straight-in
368                 responseCounter = 0;
369                 _clearedStraightIn = true;
370                 break;
371         case 15: // VFR approach for downwind entry
372                 responseCounter = 0;
373                 _clearedDownwindEntry = true;
374                 break;
375         default:
376                 break;
377         }
378 }
379
380 // Callback handler
381 // TODO - Really should enumerate these coded values.
382 void FGAIGAVFRTraffic::ProcessCallback(int code) {
383         // 1 - Request Departure from ground
384         // 2 - Report at hold short
385         // 10 - report crosswind
386         // 11 - report downwind
387         // 12 - report base
388         // 13 - report final
389         // 14 - Contact Tower for VFR arrival
390         if(code < 14) {
391                 FGAILocalTraffic::ProcessCallback(code);
392         } else if(code == 14) {
393                 tower->VFRArrivalContact(plane, this, FULL_STOP);
394         }
395 }
396
397 // Return an appropriate altitude to fly at based on the desired altitude and direction
398 // whilst respecting the quadrangle rule.
399 int FGAIGAVFRTraffic::GetQuadrangleAltitude(int dir, int des_alt) {
400         return(8888);
401         // TODO - implement me!
402 }
403
404 // Calculates the position needed to set up for either pattern entry or straight in approach.
405 // Currently returns one of three positions dependent on initial position wrt threshold of active rwy.
406 // 1/ A few miles out on extended centreline for straight-in.
407 // 2/ At an appropriate point on circuit side of rwy for a 45deg entry to downwind.
408 // 3/ At and appropriate point on non-circuit side of rwy at take-off end for perpendicular entry to circuit overflying end-of-rwy.
409 Point3D FGAIGAVFRTraffic::GetPatternApproachPos() {
410         //cout << "\n\n";
411         //cout << "Calculating pattern approach pos for " << plane.callsign << '\n';
412         Point3D orthopos = ortho.ConvertToLocal(_pos);
413         Point3D tmp;
414         //cout << "patternDirection = " << patternDirection << '\n';
415         if(orthopos.y() >= -1000.0) {   // Note that this has to be set the same as the calculation in tower.cxx - at the moment approach type is not transmitted properly between the two.
416                 //cout << "orthopos.x = " << orthopos.x() << '\n';
417                 if((orthopos.x() * patternDirection) > 0.0) {   // 45 deg entry
418                         tmp.setx(2000 * patternDirection);
419                         tmp.sety((rwy.end2ortho.y() / 2.0) + 2000);
420                         tmp.setelev(dclGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
421                         _e45 = true;
422                         //cout << "45 deg entry... ";
423                 } else {
424                         tmp.setx(1000 * patternDirection * -1);
425                         tmp.sety(rwy.end2ortho.y());
426                         tmp.setelev(dclGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
427                         _e45 = false;
428                         //cout << "90 deg entry... ";
429                 }
430         } else {
431                 tmp.setx(0);
432                 tmp.sety(-5400);
433                 tmp.setelev((5400.0 / 6.0) + dclGetAirportElev(airportID) + 10.0);
434                 //cout << "Straight in... ";
435         }
436         //cout << "Waypoint is " << tmp << '\n';
437         //cout << ortho.ConvertFromLocal(tmp) << '\n';
438         //cout << '\n';
439         //exit(-1);
440         return ortho.ConvertFromLocal(tmp);
441 }
442
443 //FGAIGAVFRTraffic::
444
445 //FGAIGAVFRTraffic::
446
447 //FGAIGAVFRTraffic::