1 // FGAILocalTraffic - AIEntity derived class with enough logic to
2 // fly and interact with the traffic pattern.
4 // Written by David Luff, started March 2002.
6 // Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
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.
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.
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.
26 #include <Airports/runways.hxx>
27 #include <Main/globals.hxx>
28 #include <simgear/math/point3d.hxx>
36 #include "AILocalTraffic.hxx"
37 #include "AIGAVFRTraffic.hxx"
38 #include "ATCutils.hxx"
41 FGAIGAVFRTraffic::FGAIGAVFRTraffic() {
42 ATC = globals->get_ATC_mgr();
43 _towerContactedIncoming = false;
44 _clearedStraightIn = false;
45 _clearedDownwindEntry = false;
48 _downwindEntry = false;
55 _cruise_climb_ias = 90.0;
57 patternDirection = -1.0;
59 // TESTING - REMOVE OR COMMENT OUT BEFORE COMMIT!!!
60 //_towerContactPrinted = false;
63 FGAIGAVFRTraffic::~FGAIGAVFRTraffic() {
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.
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).
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
83 // Set the initial track
84 track = GetHeadingFromTo(_pos, _wp);
85 // And set the plane to keep following it.
86 SetTrack(GetHeadingFromTo(_pos, _wp));
90 // TODO - set climbout if altitude is below normal cruising altitude?
92 // Assume it's OK to set the plane visible
93 _aip.setVisible(true);
94 //cout << "Setting visible true\n";
99 // Init at srcID to fly to destID
100 bool FGAIGAVFRTraffic::Init(const string& srcID, const string& destID, const string& callsign, OperatingState state) {
102 FGAILocalTraffic::Init(callsign, srcID, PARKED);
106 void FGAIGAVFRTraffic::Update(double dt) {
108 //cout << "_enroute\n";
109 //cout << "e" << flush;
111 //cout << "f" << flush;
113 //cout << "g" << flush;
114 FGAIPlane::Update(dt);
115 //cout << "h" << flush;
116 responseCounter += dt;
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);
125 //cout << "_local\n";
126 FGAILocalTraffic::Update(dt);
130 void FGAIGAVFRTraffic::FlyPlane(double dt) {
132 // Check whether to level off
133 if(_pos.elev() >= _cruise_alt) {
136 IAS = _cruise_ias; // FIXME - use smooth transistion to new speed and attitude.
141 IAS = _cruise_climb_ias;
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";
153 _towerContactPrinted = true;
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;
170 freq = (double)tower->get_freq() / 100.0;
171 tuned_station = tower;
173 freq = 122.8; // TODO - need to get the correct CTAF/Unicom frequency if no tower
174 tuned_station = NULL;
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);
184 pending_transmission = tower->get_name();
185 pending_transmission += " Tower ";
187 pending_transmission = "Traffic ";
188 // TODO - find some way of getting uncontrolled airport name
190 pending_transmission += plane.callsign;
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!
203 _towerContactedIncoming = true;
205 //cout << "?" << flush;
206 if(_clearedStraightIn && responseCounter > 5.5) {
207 //cout << "5 " << flush;
208 _clearedStraightIn = false;
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;
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);
245 if(_pos.elev() < (fgGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;
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).
257 //cout << "A " << flush;
258 if(fabs(orthopos.x()) < 10.0 && !_established) {
261 //cout << "Established at " << orthopos << '\n';
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';
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);
282 _aip.setVisible(true); // HACK
284 StraightInEntry(true);
286 } else if(_downwindEntry) {
287 //cout << "B" << flush;
289 //cout << "C" << flush;
291 if(fabs(_hdg - (rwy.hdg + 180)) < 2.0) { // TODO - use track instead of _hdg?
292 //cout << "Going Local...\n";
295 _aip.setVisible(true); // HACK
302 if(fabs(orthopos.x() - (patternDirection == 1 ? 1000 : -1000)) < (_e45 ? 175 : 550)) { // Caution - hardwired turn clearances.
303 //cout << "_turning...\n";
305 SetTrack(rwy.hdg + 180.0);
306 } // TODO - need to check for other traffic in the pattern and enter much more integilently than that!!!
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??
322 ConditionalTransmit(30);
324 SetTrack(patternDirection == 1 ? rwy.hdg - 135.0 : rwy.hdg + 135.0);
326 SetTrack(patternDirection == 1 ? rwy.hdg + 90.0 : rwy.hdg - 90.0);
328 //if(_hdg < 0.0) _hdg += 360.0;
331 SetTrack(GetHeadingFromTo(_pos, _wp));
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.
343 double vel = _cruise_ias;
344 double dist = vel * 0.514444 * dt;
345 _pos = dclUpdatePosition(_pos, track, slope, dist);
348 void FGAIGAVFRTraffic::RegisterTransmission(int code) {
350 case 1: // taxi request cleared
351 FGAILocalTraffic::RegisterTransmission(code);
353 case 2: // contact tower
354 FGAILocalTraffic::RegisterTransmission(code);
356 case 3: // Cleared to line up
357 FGAILocalTraffic::RegisterTransmission(code);
359 case 4: // cleared to take-off
360 FGAILocalTraffic::RegisterTransmission(code);
362 case 5: // contact ground
363 FGAILocalTraffic::RegisterTransmission(code);
365 case 6: // taxi to the GA parking
366 FGAILocalTraffic::RegisterTransmission(code);
368 case 7: // Cleared to land
369 FGAILocalTraffic::RegisterTransmission(code);
371 case 13: // Go around!
372 FGAILocalTraffic::RegisterTransmission(code);
374 case 14: // VFR approach for straight-in
376 _clearedStraightIn = true;
378 case 15: // VFR approach for downwind entry
380 _clearedDownwindEntry = true;
383 SG_LOG(SG_ATC, SG_WARN, "FGAIGAVFRTraffic::RegisterTransmission(...) called with unknown code " << code);
384 FGAILocalTraffic::RegisterTransmission(code);
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
398 // 14 - Contact Tower for VFR arrival
401 FGAILocalTraffic::ProcessCallback(code);
402 } else if(code == 14) {
404 tower->VFRArrivalContact(plane, this, FULL_STOP);
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);
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) {
417 // TODO - implement me!
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() {
427 //cout << "Calculating pattern approach pos for " << plane.callsign << '\n';
428 Point3D orthopos = ortho.ConvertToLocal(_pos);
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));
438 //cout << "45 deg entry... ";
440 tmp.setx(1000 * patternDirection * -1);
441 tmp.sety(rwy.end2ortho.y());
442 tmp.setelev(fgGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
444 //cout << "90 deg entry... ";
449 tmp.setelev((5400.0 / 6.0) + fgGetAirportElev(airportID) + 10.0);
450 //cout << "Straight in... ";
452 //cout << "Waypoint is " << tmp << '\n';
453 //cout << ortho.ConvertFromLocal(tmp) << '\n';
456 return ortho.ConvertFromLocal(tmp);