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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 //#include <simgear/scene/model/location.hxx>
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>
41 #include "AILocalTraffic.hxx"
42 #include "AIGAVFRTraffic.hxx"
43 #include "ATCutils.hxx"
45 FGAIGAVFRTraffic::FGAIGAVFRTraffic() {
46 ATC = globals->get_ATC_mgr();
47 _towerContactedIncoming = false;
48 _clearedStraightIn = false;
49 _clearedDownwindEntry = false;
52 _downwindEntry = false;
59 _cruise_climb_ias = 90.0;
61 patternDirection = -1.0;
63 // TESTING - REMOVE OR COMMENT OUT BEFORE COMMIT!!!
64 //_towerContactPrinted = false;
67 FGAIGAVFRTraffic::~FGAIGAVFRTraffic() {
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.
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).
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
87 //_hdg = GetHeadingFromTo(_pos, _wp);
88 SetTrack(GetHeadingFromTo(_pos, _wp));
92 // TODO - set climbout if altitude is below normal cruising altitude?
94 // Assume it's OK to set the plane visible
95 _aip.setVisible(true);
96 //cout << "Setting visible true\n";
101 // Init at srcID to fly to destID
102 bool FGAIGAVFRTraffic::Init(string srcID, string destID, const string& callsign, OperatingState state) {
104 FGAILocalTraffic::Init(callsign, srcID, PARKED);
108 void FGAIGAVFRTraffic::Update(double dt) {
110 //cout << "_enroute\n";
111 //cout << "e" << flush;
113 //cout << "f" << flush;
115 //cout << "g" << flush;
116 FGAIPlane::Update(dt);
117 //cout << "h" << flush;
118 responseCounter += dt;
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);
127 //cout << "_local\n";
128 FGAILocalTraffic::Update(dt);
132 void FGAIGAVFRTraffic::FlyPlane(double dt) {
134 // Check whether to level off
135 if(_pos.elev() >= _cruise_alt) {
138 IAS = _cruise_ias; // FIXME - use smooth transistion to new speed and attitude.
143 IAS = _cruise_climb_ias;
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";
155 _towerContactPrinted = true;
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);
181 pending_transmission = tower->get_name();
182 pending_transmission += " Tower ";
183 pending_transmission += plane.callsign;
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!
196 _towerContactedIncoming = true;
198 //cout << "?" << flush;
199 if(_clearedStraightIn && responseCounter > 5.5) {
200 //cout << "5 " << flush;
201 _clearedStraightIn = false;
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;
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);
238 if(_pos.elev() < (dclGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;
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).
250 //cout << "A " << flush;
251 if(fabs(orthopos.x()) < 10.0 && !_established) {
254 //cout << "Established at " << orthopos << '\n';
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';
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);
275 _aip.setVisible(true); // HACK
277 StraightInEntry(true);
279 } else if(_downwindEntry) {
280 //cout << "B" << flush;
282 //cout << "C" << flush;
284 if(fabs(_hdg - (rwy.hdg + 180)) < 2.0) { // TODO - use track instead of _hdg?
285 //cout << "Going Local...\n";
288 _aip.setVisible(true); // HACK
295 if(fabs(orthopos.x() - (patternDirection == 1 ? 1000 : -1000)) < (_e45 ? 175 : 550)) { // Caution - hardwired turn clearances.
296 //cout << "_turning...\n";
298 SetTrack(rwy.hdg + 180.0);
299 } // TODO - need to check for other traffic in the pattern and enter much more integilently than that!!!
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??
315 ConditionalTransmit(30);
317 SetTrack(patternDirection == 1 ? rwy.hdg - 135.0 : rwy.hdg + 135.0);
319 SetTrack(patternDirection == 1 ? rwy.hdg + 90.0 : rwy.hdg - 90.0);
321 //if(_hdg < 0.0) _hdg += 360.0;
324 SetTrack(GetHeadingFromTo(_pos, _wp));
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.
336 double vel = _cruise_ias;
337 double dist = vel * 0.514444 * dt;
338 _pos = dclUpdatePosition(_pos, track, slope, dist);
341 void FGAIGAVFRTraffic::RegisterTransmission(int code) {
343 case 1: // taxi request cleared
344 FGAILocalTraffic::RegisterTransmission(code);
346 case 2: // contact tower
347 FGAILocalTraffic::RegisterTransmission(code);
349 case 3: // Cleared to line up
350 FGAILocalTraffic::RegisterTransmission(code);
352 case 4: // cleared to take-off
353 FGAILocalTraffic::RegisterTransmission(code);
355 case 5: // contact ground
356 FGAILocalTraffic::RegisterTransmission(code);
358 case 6: // taxi to the GA parking
359 FGAILocalTraffic::RegisterTransmission(code);
361 case 7: // Cleared to land
362 FGAILocalTraffic::RegisterTransmission(code);
364 case 13: // Go around!
365 FGAILocalTraffic::RegisterTransmission(code);
367 case 14: // VFR approach for straight-in
369 _clearedStraightIn = true;
371 case 15: // VFR approach for downwind entry
373 _clearedDownwindEntry = true;
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
389 // 14 - Contact Tower for VFR arrival
391 FGAILocalTraffic::ProcessCallback(code);
392 } else if(code == 14) {
393 tower->VFRArrivalContact(plane, this, FULL_STOP);
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) {
401 // TODO - implement me!
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() {
411 //cout << "Calculating pattern approach pos for " << plane.callsign << '\n';
412 Point3D orthopos = ortho.ConvertToLocal(_pos);
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));
422 //cout << "45 deg entry... ";
424 tmp.setx(1000 * patternDirection * -1);
425 tmp.sety(rwy.end2ortho.y());
426 tmp.setelev(dclGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
428 //cout << "90 deg entry... ";
433 tmp.setelev((5400.0 / 6.0) + dclGetAirportElev(airportID) + 10.0);
434 //cout << "Straight in... ";
436 //cout << "Waypoint is " << tmp << '\n';
437 //cout << ortho.ConvertFromLocal(tmp) << '\n';
440 return ortho.ConvertFromLocal(tmp);