]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIGAVFRTraffic.cxx
dd1827ee7bbf0a78587038f2acd0a69b29cf152b
[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         // Set the initial track
88         track = GetHeadingFromTo(_pos, _wp);
89         // And set the plane to keep following it.
90         SetTrack(GetHeadingFromTo(_pos, _wp));
91         _roll = 0.0;
92         _pitch = 0.0;
93         slope = 0.0;
94         // TODO - set climbout if altitude is below normal cruising altitude?
95         //Transform();
96         // Assume it's OK to set the plane visible
97         _aip.setVisible(true);
98         //cout << "Setting visible true\n";
99         Transform();
100         return(true);
101 }
102
103 // Init at srcID to fly to destID
104 bool FGAIGAVFRTraffic::Init(string srcID, string destID, const string& callsign, OperatingState state) {
105         _enroute = false;
106         FGAILocalTraffic::Init(callsign, srcID, PARKED);
107         return(true);
108 }
109
110 void FGAIGAVFRTraffic::Update(double dt) {
111         if(_enroute) {
112                 //cout << "_enroute\n";
113                 //cout << "e" << flush;
114                 FlyPlane(dt);
115                 //cout << "f" << flush;
116                 Transform();
117                 //cout << "g" << flush;
118                 FGAIPlane::Update(dt);
119                 //cout << "h" << flush;
120                 responseCounter += dt;
121                 
122                 // we shouldn't really need this since there's a LOD of 10K on the whole plane anyway I think.
123                 // There are two _aip.setVisible statements set when _local = true that can be removed if the below is removed.
124                 if(dclGetHorizontalSeparation(_pos, Point3D(fgGetDouble("/position/longitude-deg"), fgGetDouble("/position/latitude-deg"), 0.0)) > 8000) _aip.setVisible(false);
125                 else _aip.setVisible(true);
126                 
127         } else if(_local) {
128                 //cout << "L";
129                 //cout << "_local\n";
130                 FGAILocalTraffic::Update(dt);
131         }
132 }
133
134 void FGAIGAVFRTraffic::FlyPlane(double dt) {
135         if(_climbout) {
136                 // Check whether to level off
137                 if(_pos.elev() >= _cruise_alt) {
138                         slope = 0.0;
139                         _pitch = 0.0;
140                         IAS = _cruise_ias;              // FIXME - use smooth transistion to new speed and attitude.
141                         _climbout = false;
142                 } else {
143                         slope = 4.0;
144                         _pitch = 5.0;
145                         IAS = _cruise_climb_ias;
146                 }
147         } else {
148                 // TESTING
149                 /*
150                 if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.1) {
151                         if(!_towerContactPrinted) {
152                                 if(airportID == "KSQL") {
153                                         cout << "****************************************************************\n";
154                                         cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
155                                         cout << "****************************************************************\n";
156                                 }
157                                 _towerContactPrinted = true;
158                         }
159                 }
160                 */
161                 
162                 // if distance to destination is less than 6 - 9 miles contact tower
163                 // and prepare to become _incoming after response.
164                 // Possibly check whether to start descent before this?
165                 //cout << "." << flush;
166                 //cout << "sep = " << dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 << '\n';
167                 if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.0) {
168                         //cout << "-" << flush;
169                         if(!_towerContactedIncoming) {
170                                 //cout << "_" << flush;
171                                 GetAirportDetails(airportID);
172                                 //cout << "L" << flush;
173                                 // TODO FIXME TODO - need to check that tower is valid before this else if problem -> BOOM!
174                                 freq = (double)tower->get_freq() / 100.0;
175                                 tuned_station = tower;
176                                 //cout << "freq = " << freq << endl;
177                                 GetRwyDetails(airportID);
178                                 //"@AP Tower @CS @MI miles @CD of the airport for full stop with the 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                                 pending_transmission = tower->get_name();
184                                 pending_transmission += " Tower ";
185                                 pending_transmission += plane.callsign;
186                                 //char buf[10];
187                                 int dist_miles = (int)dclGetHorizontalSeparation(_pos, _destPos) / 1600;
188                                 //sprintf(buf, " %i ", dist_miles);
189                                 pending_transmission += " ";
190                                 pending_transmission += ConvertNumToSpokenDigits(dist_miles);
191                                 if(dist_miles > 1) pending_transmission += " miles ";
192                                 else pending_transmission += " mile ";
193                                 pending_transmission += GetCompassDirection(GetHeadingFromTo(_destPos, _pos));
194                                 pending_transmission += " of the airport for full stop with the ATIS";
195                                 //cout << pending_transmission << endl;
196                                 Transmit(14);   // 14 is the callback code, NOT the timeout!
197                                 responseCounter = 0;
198                                 _towerContactedIncoming = true;
199                         } else {
200                                 //cout << "?" << flush;
201                                 if(_clearedStraightIn && responseCounter > 5.5) {
202                                         //cout << "5 " << flush;
203                                         _clearedStraightIn = false;
204                                         _straightIn = true;
205                                         _incoming = true;
206                                         _wp = GetPatternApproachPos();
207                                         //_hdg = GetHeadingFromTo(_pos, _wp);   // TODO - turn properly!
208                                         SetTrack(GetHeadingFromTo(_pos, _wp));
209                                         slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
210                                         double thesh_offset = 0.0;
211                                         Point3D opos = ortho.ConvertToLocal(_pos);
212                                         double angToApt = atan((_pos.elev() - dclGetAirportElev(airportID)) / (opos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
213                                         //cout << "angToApt = " << angToApt << ' ';
214                                         slope = (angToApt > -5.0 ? 0.0 : angToApt);
215                                         //cout << "slope = " << slope << '\n';
216                                         pending_transmission = "Straight-in ";
217                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
218                                         pending_transmission += " ";
219                                         pending_transmission += plane.callsign;
220                                         //cout << pending_transmission << '\n';
221                                         ConditionalTransmit(4);
222                                 } else if(_clearedDownwindEntry && responseCounter > 5.5) {
223                                         //cout << "6" << flush;
224                                         _clearedDownwindEntry = false;
225                                         _downwindEntry = true;
226                                         _incoming = true;
227                                         _wp = GetPatternApproachPos();
228                                         SetTrack(GetHeadingFromTo(_pos, _wp));
229                                         slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
230                                         //cout << "slope = " << slope << '\n';
231                                         pending_transmission = "Report ";
232                                         pending_transmission += (patternDirection == 1 ? "right downwind " : "left downwind ");
233                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
234                                         pending_transmission += " ";
235                                         pending_transmission += plane.callsign;
236                                         //cout << pending_transmission << '\n';
237                                         ConditionalTransmit(4);
238                                 }
239                         }
240                         if(_pos.elev() < (dclGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;     
241                 }
242         }
243         if(_incoming) {
244                 //cout << "i" << '\n';
245                 Point3D orthopos = ortho.ConvertToLocal(_pos);
246                 // TODO - Check whether to start descent
247                 // become _local after the 3 mile report.
248                 if(_pos.elev() < (dclGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;     
249                 // TODO - work out why I needed to add the above line to stop the plane going underground!!!
250                 // (Although it's worth leaving it in as a robustness check anyway).
251                 if(_straightIn) {
252                         //cout << "A " << flush;
253                         if(fabs(orthopos.x()) < 10.0 && !_established) {
254                                 SetTrack(rwy.hdg);
255                                 _established = true;
256                                 //cout << "Established at " << orthopos << '\n';
257                         }
258                         double thesh_offset = 30.0;
259                         //cout << "orthopos.y = " << orthopos.y() << " alt = " << _pos.elev() - dclGetAirportElev(airportID) << '\n';
260                         if(_established && (orthopos.y() > -5400.0)) {
261                                 slope = atan((_pos.elev() - dclGetAirportElev(airportID)) / (orthopos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
262                                 //cout << "slope0 = " << slope << '\n';
263                         }
264                         //cout << "slope1 = " << slope << '\n';
265                         if(slope > -5.5) slope = 0.0;   // ie we're too low.
266                         //cout << "slope2 = " << slope << '\n';
267                         slope += 0.001;         // To avoid yo-yoing with the above.
268                         //if(_established && (orthopos.y() > -5400.0)) slope = -5.5;
269                         if(_established && (orthopos.y() > -4800.0)) {
270                                 pending_transmission = "3 mile final Runway ";
271                                 pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
272                                 pending_transmission += " ";
273                                 pending_transmission += plane.callsign;
274                                 //cout << pending_transmission << '\n';
275                                 ConditionalTransmit(35);
276                                 _local = true;
277                                 _aip.setVisible(true);  // HACK
278                                 _enroute = false;
279                                 StraightInEntry(true);
280                         }
281                 } else if(_downwindEntry) {
282                         //cout << "B" << flush;
283                         if(_entering) {
284                                 //cout << "C" << flush;
285                                 if(_turning) {
286                                         if(fabs(_hdg - (rwy.hdg + 180)) < 2.0) {        // TODO - use track instead of _hdg?
287                                                 //cout << "Going Local...\n";
288                                                 leg = DOWNWIND;
289                                                 _local = true;
290                                                 _aip.setVisible(true);  // HACK
291                                                 _enroute = false;
292                                                 _entering = false;
293                                                 _turning = false;
294                                                 DownwindEntry();
295                                         }
296                                 }
297                                 if(fabs(orthopos.x() - (patternDirection == 1 ? 1000 : -1000)) < (_e45 ? 175 : 550)) {  // Caution - hardwired turn clearances.
298                                         //cout << "_turning...\n";
299                                         _turning = true;
300                                         SetTrack(rwy.hdg + 180.0);
301                                 }       // TODO - need to check for other traffic in the pattern and enter much more integilently than that!!!
302                         } else {
303                                 //cout << "D" << flush;
304                                 //cout << '\n' << dclGetHorizontalSeparation(_wp, _pos) << '\n';
305                                 //cout << ortho.ConvertToLocal(_pos);
306                                 //cout << ortho.ConvertToLocal(_wp);
307                                 if(dclGetHorizontalSeparation(_wp, _pos) < 100.0) {
308                                         pending_transmission = "2 miles out for ";
309                                         pending_transmission += (patternDirection == 1 ? "right " : "left ");
310                                         pending_transmission += "downwind Runway ";
311                                         pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
312                                         pending_transmission += " ";
313                                         pending_transmission += plane.callsign;
314                                         //cout << pending_transmission << '\n';
315                                         // TODO - are we at pattern altitude??
316                                         slope = 0.0;
317                                         ConditionalTransmit(30);
318                                         if(_e45) {
319                                                 SetTrack(patternDirection == 1 ? rwy.hdg - 135.0 : rwy.hdg + 135.0);
320                                         } else {
321                                                 SetTrack(patternDirection == 1 ? rwy.hdg + 90.0 : rwy.hdg - 90.0);
322                                         }
323                                         //if(_hdg < 0.0) _hdg += 360.0;
324                                         _entering = true;
325                                 } else {
326                                         SetTrack(GetHeadingFromTo(_pos, _wp));
327                                 }
328                         }       
329                 }
330         } else {
331                 // !_incoming
332                 slope = 0.0;
333         }
334         // FIXME - lots of hackery in the next six lines!!!!
335         //double track = _hdg;
336         double crab = 0.0;      // This is a placeholder for when we take wind into account.    
337         _hdg = track + crab;
338         double vel = _cruise_ias;
339         double dist = vel * 0.514444 * dt;
340         _pos = dclUpdatePosition(_pos, track, slope, dist);
341 }
342
343 void FGAIGAVFRTraffic::RegisterTransmission(int code) {
344         switch(code) {
345         case 1: // taxi request cleared
346                 FGAILocalTraffic::RegisterTransmission(code);
347                 break;
348         case 2: // contact tower
349                 FGAILocalTraffic::RegisterTransmission(code);
350                 break;
351         case 3: // Cleared to line up
352                 FGAILocalTraffic::RegisterTransmission(code);
353                 break;
354         case 4: // cleared to take-off
355                 FGAILocalTraffic::RegisterTransmission(code);
356                 break;
357         case 5: // contact ground
358                 FGAILocalTraffic::RegisterTransmission(code);
359                 break;
360         case 6: // taxi to the GA parking
361                 FGAILocalTraffic::RegisterTransmission(code);
362                 break;
363         case 7: // Cleared to land
364                 FGAILocalTraffic::RegisterTransmission(code);
365                 break;
366         case 13: // Go around!
367                 FGAILocalTraffic::RegisterTransmission(code);
368                 break;
369         case 14: // VFR approach for straight-in
370                 responseCounter = 0;
371                 _clearedStraightIn = true;
372                 break;
373         case 15: // VFR approach for downwind entry
374                 responseCounter = 0;
375                 _clearedDownwindEntry = true;
376                 break;
377         default:
378                 break;
379         }
380 }
381
382 // Callback handler
383 // TODO - Really should enumerate these coded values.
384 void FGAIGAVFRTraffic::ProcessCallback(int code) {
385         // 1 - Request Departure from ground
386         // 2 - Report at hold short
387         // 10 - report crosswind
388         // 11 - report downwind
389         // 12 - report base
390         // 13 - report final
391         // 14 - Contact Tower for VFR arrival
392         if(code < 14) {
393                 FGAILocalTraffic::ProcessCallback(code);
394         } else if(code == 14) {
395                 tower->VFRArrivalContact(plane, this, FULL_STOP);
396         }
397 }
398
399 // Return an appropriate altitude to fly at based on the desired altitude and direction
400 // whilst respecting the quadrangle rule.
401 int FGAIGAVFRTraffic::GetQuadrangleAltitude(int dir, int des_alt) {
402         return(8888);
403         // TODO - implement me!
404 }
405
406 // Calculates the position needed to set up for either pattern entry or straight in approach.
407 // Currently returns one of three positions dependent on initial position wrt threshold of active rwy.
408 // 1/ A few miles out on extended centreline for straight-in.
409 // 2/ At an appropriate point on circuit side of rwy for a 45deg entry to downwind.
410 // 3/ At and appropriate point on non-circuit side of rwy at take-off end for perpendicular entry to circuit overflying end-of-rwy.
411 Point3D FGAIGAVFRTraffic::GetPatternApproachPos() {
412         //cout << "\n\n";
413         //cout << "Calculating pattern approach pos for " << plane.callsign << '\n';
414         Point3D orthopos = ortho.ConvertToLocal(_pos);
415         Point3D tmp;
416         //cout << "patternDirection = " << patternDirection << '\n';
417         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.
418                 //cout << "orthopos.x = " << orthopos.x() << '\n';
419                 if((orthopos.x() * patternDirection) > 0.0) {   // 45 deg entry
420                         tmp.setx(2000 * patternDirection);
421                         tmp.sety((rwy.end2ortho.y() / 2.0) + 2000);
422                         tmp.setelev(dclGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
423                         _e45 = true;
424                         //cout << "45 deg entry... ";
425                 } else {
426                         tmp.setx(1000 * patternDirection * -1);
427                         tmp.sety(rwy.end2ortho.y());
428                         tmp.setelev(dclGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
429                         _e45 = false;
430                         //cout << "90 deg entry... ";
431                 }
432         } else {
433                 tmp.setx(0);
434                 tmp.sety(-5400);
435                 tmp.setelev((5400.0 / 6.0) + dclGetAirportElev(airportID) + 10.0);
436                 //cout << "Straight in... ";
437         }
438         //cout << "Waypoint is " << tmp << '\n';
439         //cout << ortho.ConvertFromLocal(tmp) << '\n';
440         //cout << '\n';
441         //exit(-1);
442         return ortho.ConvertFromLocal(tmp);
443 }
444
445 //FGAIGAVFRTraffic::
446
447 //FGAIGAVFRTraffic::
448
449 //FGAIGAVFRTraffic::