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