1 // FGAIAircraft - FGAIBase-derived class creates an AI airplane
3 // Written by David Culp, started October 2003.
5 // Copyright (C) 2003 David P. Culp - davidculp2@comcast.net
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/route/waypoint.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
29 #include <Main/viewer.hxx>
30 #include <Scenery/scenery.hxx>
31 #include <Scenery/tilemgr.hxx>
38 # define finite _finite
39 #elif defined(__sun) || defined(sgi)
45 #include "AIAircraft.hxx"
46 static string tempReg;
48 // accel, decel, climb_rate, descent_rate, takeoff_speed, climb_speed,
49 // cruise_speed, descent_speed, land_speed
51 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
53 {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0},
55 {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
57 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
59 {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0},
61 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0}
65 FGAIAircraft::FGAIAircraft(FGAIManager* mgr, FGAISchedule *ref) {
68 groundOffset = trafficRef->getGroundOffset();
72 _type_str = "aircraft";
80 // set heading and altitude locks
84 headingChangeRate = 0.0;
88 FGAIAircraft::~FGAIAircraft() {
92 bool FGAIAircraft::init() {
93 refuel_node = fgGetNode("systems/refuel/contact", true);
94 return FGAIBase::init();
97 void FGAIAircraft::bind() {
100 props->tie("controls/gear/gear-down",
101 SGRawValueMethods<FGAIAircraft,bool>(*this,
102 &FGAIAircraft::_getGearDown));
104 props->getNode("controls/lighting/landing-lights", true)
105 ->alias("controls/gear/gear-down");
109 void FGAIAircraft::unbind() {
112 props->untie("controls/gear/gear-down");
114 props->getNode("controls/lighting/landing-lights")->unalias();
119 void FGAIAircraft::update(double dt) {
121 FGAIBase::update(dt);
126 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
132 void FGAIAircraft::Run(double dt) {
134 FGAIAircraft::dt = dt;
138 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
139 ProcessFlightPlan(dt, now);
140 if (now < fp->getStartTime())
142 // Do execute Ground elev for inactive aircraft, so they
143 // Are repositioned to the correct ground altitude when the user flies within visibility range.
146 Transform(); // make sure aip is initialized.
147 getGroundElev(dt); // make sure it's exectuted first time around, so force a large dt value
148 //getGroundElev(dt); // Need to do this twice.
149 //cerr << trafficRef->getRegistration() << " Setting altitude to " << tgt_altitude << endl;
150 setAltitude(tgt_altitude);
156 double turn_radius_ft;
157 double turn_circum_ft;
158 double speed_north_deg_sec;
159 double speed_east_deg_sec;
160 double dist_covered_ft;
164 double speed_diff; //= tgt_speed - speed;
167 speed_diff = tgt_speed - speed;
171 speed_diff = groundTargetSpeed - speed;
173 if (fabs(speed_diff) > 0.2) {
174 if (speed_diff > 0.0) speed += performance->accel * dt;
175 if (speed_diff < 0.0) {
176 if (no_roll) { // was (!no_roll) but seems more logical this way (ground brakes).
177 speed -= performance->decel * dt * 3;
179 speed -= performance->decel * dt;
184 // convert speed to degrees per second
185 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
186 * speed * 1.686 / ft_per_deg_lat;
187 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
188 * speed * 1.686 / ft_per_deg_lon;
191 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
192 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
193 //if (!(finite(pos.lat()) && finite(pos.lon())))
195 // cerr << "Position is not finite" << endl;
196 // cerr << "speed = " << speed << endl;
197 // cerr << "dt" << dt << endl;
198 // cerr << "heading " << hdg << endl;
199 // cerr << "speed east " << speed_east_deg_sec << endl;
200 // cerr << "speed nrth " << speed_north_deg_sec << endl;
201 // cerr << "deg_lat " << ft_per_deg_lat << endl;
202 // cerr << "deg_lon " << ft_per_deg_lon << endl;
205 // adjust heading based on current bank angle
209 // double turnConstant;
211 // turnConstant = 0.0088362;
213 // turnConstant = 0.088362;
214 // If on ground, calculate heading change directly
216 double headingDiff = fabs(hdg-tgt_heading);
218 if (headingDiff > 180)
219 headingDiff = fabs(headingDiff - 360);
220 groundTargetSpeed = tgt_speed - (tgt_speed * (headingDiff/45));
221 if (sign(groundTargetSpeed) != sign(tgt_speed))
222 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
223 if (headingDiff > 30.0)
226 headingChangeRate += dt * sign(roll); // invert if pushed backward
227 // Print some debug statements to find out why aircraft may get stuck
229 //if (trafficRef->getDepartureAirport()->getId() == string("EHAM"))
231 //cerr << "Turning : " << trafficRef->getRegistration()
232 //cerr << " Speed = " << speed << " Heading " << hdg
233 //<< " Target Heading " << tgt_heading
234 // << " Lead Distance " << fp->getLeadDistance()
235 // << " Distance to go "
236 // << fp->getDistanceToGo(pos.lat(), pos.lon(), fp->getCurrentWaypoint())
237 // << "waypoint name " << fp->getCurrentWaypoint()->name
240 if (headingChangeRate > 30)
242 headingChangeRate = 30;
244 else if (headingChangeRate < -30)
246 headingChangeRate = -30;
251 if (fabs(headingChangeRate) > headingDiff)
252 headingChangeRate = headingDiff*sign(roll);
254 headingChangeRate += dt * sign(roll);
256 hdg += headingChangeRate * dt;
257 //cerr << "On ground. Heading: " << hdg << ". Target Heading: " << tgt_heading << ". Target speed: " << groundTargetSpeed << ". heading change rate" << headingChangeRate << endl;
260 if (fabs(speed) > 1.0) {
261 turn_radius_ft = 0.088362 * speed * speed
262 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
266 turn_radius_ft = 1.0; // Check if turn_radius_ft == 0; this might lead to a division by 0.
268 turn_circum_ft = SGD_2PI * turn_radius_ft;
269 dist_covered_ft = speed * 1.686 * dt;
270 alpha = dist_covered_ft / turn_circum_ft * 360.0;
271 hdg += alpha * sign(roll);
273 while ( hdg > 360.0 ) {
285 // adjust target bank angle if heading lock engaged
287 double bank_sense = 0.0;
288 double diff = fabs(hdg - tgt_heading);
289 if (diff > 180) diff = fabs(diff - 360);
290 double sum = hdg + diff;
291 if (sum > 360.0) sum -= 360.0;
292 if (fabs(sum - tgt_heading) < 1.0) {
293 bank_sense = 1.0; // right turn
295 bank_sense = -1.0; // left turn
298 tgt_roll = diff * bank_sense;
300 tgt_roll = 30.0 * bank_sense;
302 if ((fabs((double) spinCounter) > 1) && (diff > 30))
304 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
305 // The only way to resolve this is to make them slow down.
306 //if (tempReg.empty())
307 // tempReg = trafficRef->getRegistration();
308 //if (trafficRef->getRegistration() == tempReg)
310 // cerr << trafficRef->getRegistration()
311 // << " appears to be spinning: " << spinCounter << endl
312 // << " speed " << speed << endl
313 // << " heading " << hdg << endl
314 // << " lead distance " << fp->getLeadDistance() << endl
315 // << " waypoint " << fp->getCurrentWaypoint()->name
316 // << " target heading " << tgt_heading << endl
317 // << " lead in angle " << fp->getLeadInAngle()<< endl
318 // << " roll " << roll << endl
319 // << " target_roll " << tgt_roll << endl;
325 // adjust bank angle, use 9 degrees per second
326 double bank_diff = tgt_roll - roll;
327 if (fabs(bank_diff) > 0.2) {
328 if (bank_diff > 0.0) roll += 9.0 * dt;
329 if (bank_diff < 0.0) roll -= 9.0 * dt;
330 //while (roll > 180) roll -= 360;
331 //while (roll < 180) roll += 360;
334 // adjust altitude (meters) based on current vertical speed (fpm)
335 altitude += vs / 60.0 * dt;
336 pos.setelev(altitude * SG_FEET_TO_METER);
337 double altitude_ft = altitude;
339 // adjust target Altitude, based on ground elevation when on ground
344 // find target vertical speed if altitude lock engaged
345 if (alt_lock && use_perf_vs) {
346 if (altitude_ft < tgt_altitude) {
347 tgt_vs = tgt_altitude - altitude_ft;
348 if (tgt_vs > performance->climb_rate)
349 tgt_vs = performance->climb_rate;
351 tgt_vs = tgt_altitude - altitude_ft;
352 if (tgt_vs < (-performance->descent_rate))
353 tgt_vs = -performance->descent_rate;
357 if (alt_lock && !use_perf_vs) {
358 double max_vs = 4*(tgt_altitude - altitude);
360 if (tgt_altitude < altitude) min_vs = -100.0;
361 if ((fabs(tgt_altitude - altitude) < 1500.0) &&
362 (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
363 if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
366 // adjust vertical speed
367 double vs_diff = tgt_vs - vs;
368 if (fabs(vs_diff) > 10.0) {
371 if (vs > tgt_vs) vs = tgt_vs;
374 if (vs < tgt_vs) vs = tgt_vs;
378 // match pitch angle to vertical speed
385 //###########################//
386 // do calculations for radar //
387 //###########################//
388 double range_ft2 = UpdateRadar(manager);
390 //************************************//
392 //************************************//
395 if ( (range_ft2 < 250.0 * 250.0) &&
397 (elevation > 0.0) ) {
398 refuel_node->setBoolValue(true);
400 refuel_node->setBoolValue(false);
406 void FGAIAircraft::AccelTo(double speed) {
411 void FGAIAircraft::PitchTo(double angle) {
417 void FGAIAircraft::RollTo(double angle) {
423 void FGAIAircraft::YawTo(double angle) {
428 void FGAIAircraft::ClimbTo(double altitude) {
429 tgt_altitude = altitude;
434 void FGAIAircraft::TurnTo(double heading) {
435 tgt_heading = heading;
440 double FGAIAircraft::sign(double x) {
441 if ( x < 0.0 ) { return -1.0; }
445 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
449 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now )
456 eraseWaypoints = true;
457 // cerr << trafficRef->getRegistration();
458 // cerr << "Departure airport " << endl;
459 // dep = trafficRef->getDepartureAirport();
461 // cerr << dep->getId() << endl;
462 // cerr << "Arrival airport " << endl;
463 // arr = trafficRef->getArrivalAirport();
465 // cerr << arr->getId() <<endl << endl;;
468 eraseWaypoints = false;
470 //cerr << "Processing Flightplan" << endl;
471 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
472 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
473 FGAIFlightPlan::waypoint* next = 0; // the next plus 1
474 prev = fp->getPreviousWaypoint();
475 curr = fp->getCurrentWaypoint();
476 next = fp->getNextWaypoint();
479 if (!prev) { //beginning of flightplan, do this initialization once
483 //prev_dist_to_go = HUGE;
484 //cerr << "Before increment " << curr-> name << endl;
485 fp->IncrementWaypoint(eraseWaypoints);
486 //prev = fp->getPreviousWaypoint(); //first waypoint
487 //curr = fp->getCurrentWaypoint(); //second waypoint
488 //next = fp->getNextWaypoint(); //third waypoint (might not exist!)
489 //cerr << "After increment " << prev-> name << endl;
490 if (!(fp->getNextWaypoint()) && trafficRef)
494 //cerr << "After load " << prev-> name << endl;
495 prev = fp->getPreviousWaypoint(); //first waypoint
496 curr = fp->getCurrentWaypoint(); //second waypoint
497 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
498 //cerr << "After load " << prev-> name << endl;
499 setLatitude(prev->latitude);
500 setLongitude(prev->longitude);
501 setSpeed(prev->speed);
502 setAltitude(prev->altitude);
503 if (prev->speed > 0.0)
504 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
507 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
509 // If next doesn't exist, as in incrementally created flightplans for
510 // AI/Trafficmanager created plans,
511 // Make sure lead distance is initialized otherwise
513 fp->setLeadDistance(speed, hdg, curr, next);
515 if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
517 tgt_vs = (curr->crossat - prev->altitude)/
518 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
519 6076.0/prev->speed*60.0);
520 tgt_altitude = curr->crossat;
523 tgt_altitude = prev->altitude;
525 alt_lock = hdg_lock = true;
526 no_roll = prev->on_ground;
529 Transform(); // make sure aip is initialized.
530 getGroundElev(60.1); // make sure it's exectuted first time around, so force a large dt value
531 //getGroundElev(60.1); // Need to do this twice.
532 //cerr << trafficRef->getRegistration() << " Setting altitude to " << tgt_altitude << endl;
533 setAltitude(tgt_altitude);
536 //cout << "First waypoint: " << prev->name << endl;
537 //cout << " Target speed: " << tgt_speed << endl;
538 //cout << " Target altitude: " << tgt_altitude << endl;
539 //cout << " Target heading: " << tgt_heading << endl << endl;
540 //cerr << "Done Flightplan init" << endl;
542 } // end of initialization
544 // let's only process the flight plan every 100 ms.
545 if ((dt_count < 0.1) || (now < fp->getStartTime()))
547 //cerr << "done fp dt" << endl;
552 // check to see if we've reached the lead point for our next turn
553 double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr);
555 //cerr << "2" << endl;
556 double lead_dist = fp->getLeadDistance();
557 //cerr << " Distance : " << dist_to_go << ": Lead distance " << lead_dist << endl;
558 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
559 if (lead_dist < fabs(2*speed))
561 lead_dist = fabs(2*speed); //don't skip over the waypoint
562 //cerr << "Extending lead distance to " << lead_dist << endl;
564 // FGAirport * apt = trafficRef->getDepartureAirport();
565 // if ((dist_to_go > prev_dist_to_go) && trafficRef && apt)
567 // if (apt->getId() == string("EHAM"))
568 // cerr << "Alert: " << trafficRef->getRegistration() << " is moving away from waypoint " << curr->name << endl
569 // << "Target heading : " << tgt_heading << "act heading " << hdg << " Tgt speed : " << tgt_speed << endl
570 // << "Lead distance : " << lead_dist << endl
571 // << "Distance to go: " << dist_to_go << endl;
574 prev_dist_to_go = dist_to_go;
575 //cerr << "2" << endl;
578 //cout << "Leg : " << (fp->getLeg()-1) << ". dist_to_go: " << dist_to_go << ", lead_dist: " << lead_dist << ", tgt_speed " << tgt_speed << ", tgt_heading " << tgt_heading << " speed " << speed << " hdg " << hdg << ". Altitude " << altitude << " TAget alt :" << tgt_altitude << endl;
580 if ( dist_to_go < lead_dist ) {
581 //prev_dist_to_go = HUGE;
582 // For traffic manager generated aircraft:
583 // check if the aircraft flies of of user range. And adjust the
584 // Current waypoint's elevation according to Terrain Elevation
585 if (curr->finished) { //end of the flight plan
587 if (fp->getRepeat()) {
593 //cerr << "Done die end of fp" << endl;
598 // we've reached the lead-point for the waypoint ahead
599 //cerr << "4" << endl;
600 //cerr << "Situation after lead point" << endl;
601 //cerr << "Prviious: " << prev->name << endl;
602 //cerr << "Current : " << curr->name << endl;
603 //cerr << "Next : " << next->name << endl;
606 tgt_heading = fp->getBearing(curr, next);
609 fp->IncrementWaypoint(eraseWaypoints);
610 if (!(fp->getNextWaypoint()) && trafficRef)
614 prev = fp->getPreviousWaypoint();
615 curr = fp->getCurrentWaypoint();
616 next = fp->getNextWaypoint();
617 // Now that we have incremented the waypoints, excute some traffic manager specific code
618 // based on the name of the waypoint we just passed.
621 double userLatitude = fgGetDouble("/position/latitude-deg");
622 double userLongitude = fgGetDouble("/position/longitude-deg");
623 double course, distance;
624 SGWayPoint current (pos.lon(),
627 SGWayPoint user ( userLongitude,
630 user.CourseAndDistance(current, &course, &distance);
631 if ((distance * SG_METER_TO_NM) > TRAFFICTOAIDIST)
634 //cerr << "done fp die out of range" << endl;
638 FGAirport * dep = trafficRef->getDepartureAirport();
639 FGAirport * arr = trafficRef->getArrivalAirport();
640 // At parking the beginning of the airport
646 //if ((dep->getId() == string("EHAM") || (arr->getId() == string("EHAM"))))
648 // cerr << trafficRef->getRegistration()
649 // << " Enroute from " << dep->getId()
650 // << " to " << arr->getId()
651 // << " just crossed " << prev->name
652 // << " Assigned rwy " << fp->getRunwayId()
653 // << " " << fp->getRunway() << endl;
655 //if ((dep->getId() == string("EHAM")) && (prev->name == "park2"))
657 // cerr << "Schiphol ground "
658 // << trafficRef->getCallSign();
659 // if (trafficRef->getHeavy())
661 // cerr << ", is type "
662 // << trafficRef->getAircraft()
663 // << " ready to go. IFR to "
664 // << arr->getId() <<endl;
666 if (prev->name == "park2")
668 dep->getDynamics()->releaseParking(fp->getGate());
670 // Some debug messages, specific to TESTING THE Logical networks.
671 //if ((arr->getId() == string("EHAM")) && (prev->name == "Center"))
674 // cerr << "Schiphol ground "
675 // << trafficRef->getCallSign();
676 // if (trafficRef->getHeavy())
678 // cerr << " landed runway "
679 // << fp->getRunway()
680 // << " request taxi to gate "
681 // << arr->getParkingName(fp->getGate())
684 if (prev->name == "END")
685 fp->setTime(trafficRef->getDepartureTime());
686 //cerr << "5" << endl;
690 //cerr << "Current waypoint" << curr->name << endl;
691 //cerr << "Next waypoint" << next->name << endl;
692 fp->setLeadDistance(speed, tgt_heading, curr, next);
694 //cerr << "5.1" << endl;
695 if (curr->crossat > -1000.0) {
696 //cerr << "5.1a" << endl;
698 tgt_vs = (curr->crossat - altitude)/
699 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
700 //cerr << "5.1b" << endl;
701 tgt_altitude = curr->crossat;
703 //cerr << "5.1c" << endl;
705 //cerr << "5.1d" << endl;
706 tgt_altitude = prev->altitude;
707 //cerr << "Setting target altitude : " <<tgt_altitude << endl;
709 //cerr << "6" << endl;
710 tgt_speed = prev->speed;
711 hdg_lock = alt_lock = true;
712 no_roll = prev->on_ground;
713 //cout << "Crossing waypoint: " << prev->name << endl;
714 //cout << " Target speed: " << tgt_speed << endl;
715 //cout << " Target altitude: " << tgt_altitude << endl;
716 //cout << " Target heading: " << tgt_heading << endl << endl;
719 double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
720 //cerr << "Bearing = " << calc_bearing << endl;
724 if (calc_bearing > 360)
727 if (finite(calc_bearing))
729 double hdg_error = calc_bearing - tgt_heading;
730 if (fabs(hdg_error) > 1.0) {
731 TurnTo( calc_bearing );
736 cerr << "calc_bearing is not a finite number : "
738 << "pos : " << pos.lat() << ", " << pos.lon()
739 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
740 cerr << "waypoint name " << curr->name;
743 double speed_diff = speed - prevSpeed;
744 // Update the lead distance calculation if speed has changed sufficiently
745 // to prevent spinning (hopefully);
746 if (fabs(speed_diff) > 10)
749 fp->setLeadDistance(speed, tgt_heading, curr, next);
752 //cerr << "Done Processing FlightPlan"<< endl;
753 } // if (dt count) else
756 bool FGAIAircraft::_getGearDown() const {
757 return ((props->getFloatValue("position/altitude-agl-ft") < 900.0)
758 && (props->getFloatValue("velocities/airspeed-kt")
759 < performance->land_speed*1.25));
763 void FGAIAircraft::loadNextLeg()
766 //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
769 //FGAIModelEntity entity;
770 //entity.m_class = "jet_transport";
771 //entity.path = modelPath.c_str();
772 //entity.flightplan = "none";
773 //entity.latitude = _getLatitude();
774 //entity.longitude = _getLongitude();
775 //entity.altitude = trafficRef->getCruiseAlt() * 100; // convert from FL to feet
776 //entity.speed = 450; // HACK ALERT
777 //entity.fp = new FGAIFlightPlan(&entity, courseToDest, i->getDepartureTime(), dep, arr);
779 if ((leg = fp->getLeg()) == 10)
785 //cerr << "Resetting leg : " << leg << endl;
790 //cerr << "Creating leg number : " << leg << endl;
791 FGAirport *dep = trafficRef->getDepartureAirport();
792 FGAirport *arr = trafficRef->getArrivalAirport();
796 //cerr << "Failed to get airport in AIAircraft::ProcessFlightplan()" << endl;
798 // cerr << "Departure " << dep->getId() << endl;
800 // cerr << "Arrival " << arr->getId() << endl;
804 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
805 //cerr << "Creating new leg using " << cruiseAlt << " as cruise altitude."<< endl;
810 cruiseAlt, //(trafficRef->getCruiseAlt() * 100), // convert from FL to feet
811 trafficRef->getSpeed(),
815 trafficRef->getRadius(),
816 trafficRef->getFlightType(),
819 //prev = fp->getPreviousWaypoint();
820 //curr = fp->getCurrentWaypoint();
821 //next = fp->getNextWaypoint();
822 //cerr << "25" << endl;
825 // //cerr << "Next waypoint" << next->name << endl;
826 // fp->setLeadDistance(speed, tgt_heading, curr, next);
828 //cerr << "25.1" << endl;
829 //if (curr->crossat > -1000.0) {
830 // //cerr << "25.1a" << endl;
831 // use_perf_vs = false;
833 // tgt_vs = (curr->crossat - altitude)/
834 // (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
835 // //cerr << "25.1b" << endl;
836 // tgt_altitude = curr->crossat;
838 // //cerr << "25.1c" << endl;
839 // use_perf_vs = true;
840 // //cerr << "25.1d" << endl;
841 // tgt_altitude = prev->altitude;
842 // //cerr << "Setting target altitude : " <<tgt_altitude << endl;
844 //cerr << "26" << endl;
845 //tgt_speed = prev->speed;
846 //hdg_lock = alt_lock = true;
847 //no_roll = prev->on_ground;
854 //entity.fp = new FGAIFlightPlan(&entity,
856 // trafficRef->getDepartureTime(),
857 // trafficRef->getDepartureAirport(),
858 // trafficRef->getArrivalAirport(),
862 //SetFlightPlan(entity.fp);
867 // Note: This code is copied from David Luff's AILocalTraffic
868 // Warning - ground elev determination is CPU intensive
869 // Either this function or the logic of how often it is called
870 // will almost certainly change.
872 void FGAIAircraft::getGroundElev(double dt) {
875 if (dt_elev_count < (10.0 + (rand() % 100))) // Update minimally every 10 secs, but add some randomness to prevent them all IA objects doing this synced
883 // It would be nice if we could set the correct tile center here in order to get a correct
884 // answer with one call to the function, but what I tried in the two commented-out lines
885 // below only intermittently worked, and I haven't quite groked why yet.
886 //SGBucket buck(pos.lon(), pos.lat());
887 //aip.getSGLocation()->set_tile_center(Point3D(buck.get_center_lon(), buck.get_center_lat(), 0.0));
889 // Only do the proper hitlist stuff if we are within visible range of the viewer.
890 double visibility_meters = fgGetDouble("/environment/visibility-m");
893 FGViewer* vw = globals->get_current_view();
898 //Point3D currView(vw->getLongitude_deg(),
899 // vw->getLatitude_deg(), 0.0);
900 SGWayPoint current (pos.lon(),
903 SGWayPoint view ( vw->getLongitude_deg(),
904 vw->getLatitude_deg(),
906 view.CourseAndDistance(current, &course, &distance);
907 if(distance > visibility_meters) {
908 //aip.getSGLocation()->set_cur_elev_m(aptElev);
912 // FIXME: make shure the pos.lat/pos.lon values are in degrees ...
913 double range = 500.0;
914 if (!globals->get_tile_mgr()->scenery_available(pos.lat(), pos.lon(), range))
916 // Try to shedule tiles for that position.
917 globals->get_tile_mgr()->update( aip.getSGLocation(), range );
920 // FIXME: make shure the pos.lat/pos.lon values are in degrees ...
922 if (globals->get_scenery()->get_elevation_m(pos.lat(), pos.lon(),