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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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(FGAISchedule *ref) :
66 FGAIBase(otAircraft) {
69 groundOffset = trafficRef->getGroundOffset();
78 // set heading and altitude locks
82 headingChangeRate = 0.0;
86 FGAIAircraft::~FGAIAircraft() {
90 void FGAIAircraft::readFromScenario(SGPropertyNode* scFileNode) {
94 FGAIBase::readFromScenario(scFileNode);
96 setPerformance(scFileNode->getStringValue("class", "jet_transport"));
97 setFlightPlan(scFileNode->getStringValue("flightplan"),
98 scFileNode->getBoolValue("repeat", false));
99 setCallSign(scFileNode->getStringValue("callsign"));
100 setTACANChannelID(scFileNode->getStringValue("TACAN-channel-ID"));
103 bool FGAIAircraft::init() {
104 refuel_node = fgGetNode("systems/refuel/contact", true);
105 return FGAIBase::init();
108 void FGAIAircraft::bind() {
111 props->tie("controls/gear/gear-down",
112 SGRawValueMethods<FGAIAircraft,bool>(*this,
113 &FGAIAircraft::_getGearDown));
114 props->tie("refuel/contact", SGRawValuePointer<bool>(&contact));
115 props->setStringValue("callsign", callsign.c_str());
116 props->setStringValue("navaids/tacan/channel-ID", TACAN_channel_id.c_str());
117 props->setBoolValue("tanker",isTanker);
120 void FGAIAircraft::unbind() {
123 props->untie("controls/gear/gear-down");
124 props->untie("refuel/contact");
128 void FGAIAircraft::update(double dt) {
130 FGAIBase::update(dt);
135 void FGAIAircraft::setPerformance(const std::string& acclass)
137 if (acclass == "light") {
138 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
139 } else if (acclass == "ww2_fighter") {
140 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
141 } else if (acclass == "jet_transport") {
142 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
143 } else if (acclass == "jet_fighter") {
144 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
145 } else if (acclass == "tanker") {
146 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
149 SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
153 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
159 void FGAIAircraft::Run(double dt) {
161 FGAIAircraft::dt = dt;
165 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
166 ProcessFlightPlan(dt, now);
167 if (now < fp->getStartTime())
169 // Do execute Ground elev for inactive aircraft, so they
170 // Are repositioned to the correct ground altitude when the user flies within visibility range.
173 Transform(); // make sure aip is initialized.
174 getGroundElev(dt); // make sure it's exectuted first time around, so force a large dt value
175 //getGroundElev(dt); // Need to do this twice.
176 //cerr << trafficRef->getRegistration() << " Setting altitude to " << tgt_altitude;
178 //cerr << " Actual altitude " << altitude << endl;
180 pos.setelev(altitude * SG_FEET_TO_METER);
186 double turn_radius_ft;
187 double turn_circum_ft;
188 double speed_north_deg_sec;
189 double speed_east_deg_sec;
190 double dist_covered_ft;
194 double speed_diff; //= tgt_speed - speed;
197 speed_diff = tgt_speed - speed;
201 speed_diff = groundTargetSpeed - speed;
203 if (fabs(speed_diff) > 0.2) {
204 if (speed_diff > 0.0) speed += performance->accel * dt;
205 if (speed_diff < 0.0) {
206 if (no_roll) { // was (!no_roll) but seems more logical this way (ground brakes).
207 speed -= performance->decel * dt * 3;
209 speed -= performance->decel * dt;
214 // convert speed to degrees per second
215 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
216 * speed * 1.686 / ft_per_deg_lat;
217 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
218 * speed * 1.686 / ft_per_deg_lon;
221 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
222 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
223 //if (!(finite(pos.lat()) && finite(pos.lon())))
225 // cerr << "Position is not finite" << endl;
226 // cerr << "speed = " << speed << endl;
227 // cerr << "dt" << dt << endl;
228 // cerr << "heading " << hdg << endl;
229 // cerr << "speed east " << speed_east_deg_sec << endl;
230 // cerr << "speed nrth " << speed_north_deg_sec << endl;
231 // cerr << "deg_lat " << ft_per_deg_lat << endl;
232 // cerr << "deg_lon " << ft_per_deg_lon << endl;
235 // adjust heading based on current bank angle
239 // double turnConstant;
241 // turnConstant = 0.0088362;
243 // turnConstant = 0.088362;
244 // If on ground, calculate heading change directly
246 double headingDiff = fabs(hdg-tgt_heading);
248 if (headingDiff > 180)
249 headingDiff = fabs(headingDiff - 360);
250 groundTargetSpeed = tgt_speed - (tgt_speed * (headingDiff/45));
251 if (sign(groundTargetSpeed) != sign(tgt_speed))
252 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
253 if (headingDiff > 30.0)
256 headingChangeRate += dt * sign(roll); // invert if pushed backward
257 // Print some debug statements to find out why aircraft may get stuck
259 //if (trafficRef->getDepartureAirport()->getId() == string("EHAM"))
261 //cerr << "Turning : " << trafficRef->getRegistration()
262 //cerr << " Speed = " << speed << " Heading " << hdg
263 //<< " Target Heading " << tgt_heading
264 // << " Lead Distance " << fp->getLeadDistance()
265 // << " Distance to go "
266 // << fp->getDistanceToGo(pos.lat(), pos.lon(), fp->getCurrentWaypoint())
267 // << "waypoint name " << fp->getCurrentWaypoint()->name
270 if (headingChangeRate > 30)
272 headingChangeRate = 30;
274 else if (headingChangeRate < -30)
276 headingChangeRate = -30;
281 if (fabs(headingChangeRate) > headingDiff)
282 headingChangeRate = headingDiff*sign(roll);
284 headingChangeRate += dt * sign(roll);
286 hdg += headingChangeRate * dt;
287 //cerr << "On ground. Heading: " << hdg << ". Target Heading: " << tgt_heading << ". Target speed: " << groundTargetSpeed << ". heading change rate" << headingChangeRate << endl;
290 if (fabs(speed) > 1.0) {
291 turn_radius_ft = 0.088362 * speed * speed
292 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
296 turn_radius_ft = 1.0; // Check if turn_radius_ft == 0; this might lead to a division by 0.
298 turn_circum_ft = SGD_2PI * turn_radius_ft;
299 dist_covered_ft = speed * 1.686 * dt;
300 alpha = dist_covered_ft / turn_circum_ft * 360.0;
301 hdg += alpha * sign(roll);
303 while ( hdg > 360.0 ) {
315 // adjust target bank angle if heading lock engaged
317 double bank_sense = 0.0;
318 double diff = fabs(hdg - tgt_heading);
319 if (diff > 180) diff = fabs(diff - 360);
320 double sum = hdg + diff;
321 if (sum > 360.0) sum -= 360.0;
322 if (fabs(sum - tgt_heading) < 1.0) {
323 bank_sense = 1.0; // right turn
325 bank_sense = -1.0; // left turn
328 tgt_roll = diff * bank_sense;
330 tgt_roll = 30.0 * bank_sense;
332 if ((fabs((double) spinCounter) > 1) && (diff > 30))
334 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
335 // The only way to resolve this is to make them slow down.
336 //if (tempReg.empty())
337 // tempReg = trafficRef->getRegistration();
338 //if (trafficRef->getRegistration() == tempReg)
340 // cerr << trafficRef->getRegistration()
341 // << " appears to be spinning: " << spinCounter << endl
342 // << " speed " << speed << endl
343 // << " heading " << hdg << endl
344 // << " lead distance " << fp->getLeadDistance() << endl
345 // << " waypoint " << fp->getCurrentWaypoint()->name
346 // << " target heading " << tgt_heading << endl
347 // << " lead in angle " << fp->getLeadInAngle()<< endl
348 // << " roll " << roll << endl
349 // << " target_roll " << tgt_roll << endl;
355 // adjust bank angle, use 9 degrees per second
356 double bank_diff = tgt_roll - roll;
357 if (fabs(bank_diff) > 0.2) {
358 if (bank_diff > 0.0) roll += 9.0 * dt;
359 if (bank_diff < 0.0) roll -= 9.0 * dt;
360 //while (roll > 180) roll -= 360;
361 //while (roll < 180) roll += 360;
364 // adjust altitude (meters) based on current vertical speed (fpm)
365 altitude += vs / 60.0 * dt;
366 pos.setelev(altitude * SG_FEET_TO_METER);
367 double altitude_ft = altitude;
369 // adjust target Altitude, based on ground elevation when on ground
377 // find target vertical speed if altitude lock engaged
378 if (alt_lock && use_perf_vs) {
379 if (altitude_ft < tgt_altitude) {
380 tgt_vs = tgt_altitude - altitude_ft;
381 if (tgt_vs > performance->climb_rate)
382 tgt_vs = performance->climb_rate;
384 tgt_vs = tgt_altitude - altitude_ft;
385 if (tgt_vs < (-performance->descent_rate))
386 tgt_vs = -performance->descent_rate;
390 if (alt_lock && !use_perf_vs) {
391 double max_vs = 4*(tgt_altitude - altitude);
393 if (tgt_altitude < altitude) min_vs = -100.0;
394 if ((fabs(tgt_altitude - altitude) < 1500.0) &&
395 (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
396 if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
399 // adjust vertical speed
400 double vs_diff = tgt_vs - vs;
401 if (fabs(vs_diff) > 10.0) {
404 if (vs > tgt_vs) vs = tgt_vs;
407 if (vs < tgt_vs) vs = tgt_vs;
411 // match pitch angle to vertical speed
418 //###########################//
419 // do calculations for radar //
420 //###########################//
421 double range_ft2 = UpdateRadar(manager);
423 //************************************//
425 //************************************//
428 if ( (range_ft2 < 250.0 * 250.0) &&
430 (elevation > 0.0) ) {
431 refuel_node->setBoolValue(true);
434 refuel_node->setBoolValue(false);
443 void FGAIAircraft::AccelTo(double speed) {
448 void FGAIAircraft::PitchTo(double angle) {
454 void FGAIAircraft::RollTo(double angle) {
460 void FGAIAircraft::YawTo(double angle) {
465 void FGAIAircraft::ClimbTo(double altitude) {
466 tgt_altitude = altitude;
471 void FGAIAircraft::TurnTo(double heading) {
472 tgt_heading = heading;
477 double FGAIAircraft::sign(double x) {
478 if ( x < 0.0 ) { return -1.0; }
482 void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat)
484 if (!flightplan.empty()){
485 FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
486 fp->setRepeat(repeat);
491 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
496 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now )
503 eraseWaypoints = true;
504 // cerr << trafficRef->getRegistration();
505 // cerr << "Departure airport " << endl;
506 // dep = trafficRef->getDepartureAirport();
508 // cerr << dep->getId() << endl;
509 // cerr << "Arrival airport " << endl;
510 // arr = trafficRef->getArrivalAirport();
512 // cerr << arr->getId() <<endl << endl;;
515 eraseWaypoints = false;
517 //cerr << "Processing Flightplan" << endl;
518 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
519 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
520 FGAIFlightPlan::waypoint* next = 0; // the next plus 1
521 prev = fp->getPreviousWaypoint();
522 curr = fp->getCurrentWaypoint();
523 next = fp->getNextWaypoint();
526 if (!prev) { //beginning of flightplan, do this initialization once
530 //prev_dist_to_go = HUGE;
531 //cerr << "Before increment " << curr-> name << endl;
532 fp->IncrementWaypoint(eraseWaypoints);
533 //prev = fp->getPreviousWaypoint(); //first waypoint
534 //curr = fp->getCurrentWaypoint(); //second waypoint
535 //next = fp->getNextWaypoint(); //third waypoint (might not exist!)
536 //cerr << "After increment " << prev-> name << endl;
537 if (!(fp->getNextWaypoint()) && trafficRef)
541 //cerr << "After load " << prev-> name << endl;
542 prev = fp->getPreviousWaypoint(); //first waypoint
543 curr = fp->getCurrentWaypoint(); //second waypoint
544 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
545 //cerr << "After load " << prev-> name << endl;
546 setLatitude(prev->latitude);
547 setLongitude(prev->longitude);
548 setSpeed(prev->speed);
549 setAltitude(prev->altitude);
550 if (prev->speed > 0.0)
551 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
554 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
556 // If next doesn't exist, as in incrementally created flightplans for
557 // AI/Trafficmanager created plans,
558 // Make sure lead distance is initialized otherwise
560 fp->setLeadDistance(speed, hdg, curr, next);
562 if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
564 tgt_vs = (curr->crossat - prev->altitude)/
565 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
566 6076.0/prev->speed*60.0);
567 tgt_altitude = curr->crossat;
570 tgt_altitude = prev->altitude;
572 alt_lock = hdg_lock = true;
573 no_roll = prev->on_ground;
576 Transform(); // make sure aip is initialized.
577 getGroundElev(60.1); // make sure it's exectuted first time around, so force a large dt value
578 //getGroundElev(60.1); // Need to do this twice.
579 //cerr << trafficRef->getRegistration() << " Setting altitude to " << tgt_altitude << endl;
580 doGroundAltitude(); //(tgt_altitude);
583 //cout << "First waypoint: " << prev->name << endl;
584 //cout << " Target speed: " << tgt_speed << endl;
585 //cout << " Target altitude: " << tgt_altitude << endl;
586 //cout << " Target heading: " << tgt_heading << endl << endl;
587 //cerr << "Done Flightplan init" << endl;
589 } // end of initialization
591 // let's only process the flight plan every 100 ms.
592 if ((dt_count < 0.1) || (now < fp->getStartTime()))
594 //cerr << "done fp dt" << endl;
599 // check to see if we've reached the lead point for our next turn
600 double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr);
602 //cerr << "2" << endl;
603 double lead_dist = fp->getLeadDistance();
604 //cerr << " Distance : " << dist_to_go << ": Lead distance " << lead_dist << endl;
605 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
606 if (lead_dist < fabs(2*speed))
608 lead_dist = fabs(2*speed); //don't skip over the waypoint
609 //cerr << "Extending lead distance to " << lead_dist << endl;
611 // FGAirport * apt = trafficRef->getDepartureAirport();
612 // if ((dist_to_go > prev_dist_to_go) && trafficRef && apt)
614 // if (apt->getId() == string("EHAM"))
615 // cerr << "Alert: " << trafficRef->getRegistration() << " is moving away from waypoint " << curr->name << endl
616 // << "Target heading : " << tgt_heading << "act heading " << hdg << " Tgt speed : " << tgt_speed << endl
617 // << "Lead distance : " << lead_dist << endl
618 // << "Distance to go: " << dist_to_go << endl;
621 prev_dist_to_go = dist_to_go;
622 //cerr << "2" << endl;
625 //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;
627 if ( dist_to_go < lead_dist ) {
628 //prev_dist_to_go = HUGE;
629 // For traffic manager generated aircraft:
630 // check if the aircraft flies of of user range. And adjust the
631 // Current waypoint's elevation according to Terrain Elevation
632 if (curr->finished) { //end of the flight plan
634 if (fp->getRepeat()) {
640 //cerr << "Done die end of fp" << endl;
645 // we've reached the lead-point for the waypoint ahead
646 //cerr << "4" << endl;
647 //cerr << "Situation after lead point" << endl;
648 //cerr << "Prviious: " << prev->name << endl;
649 //cerr << "Current : " << curr->name << endl;
650 //cerr << "Next : " << next->name << endl;
653 tgt_heading = fp->getBearing(curr, next);
656 fp->IncrementWaypoint(eraseWaypoints);
657 if (!(fp->getNextWaypoint()) && trafficRef)
661 prev = fp->getPreviousWaypoint();
662 curr = fp->getCurrentWaypoint();
663 next = fp->getNextWaypoint();
664 // Now that we have incremented the waypoints, excute some traffic manager specific code
665 // based on the name of the waypoint we just passed.
668 double userLatitude = fgGetDouble("/position/latitude-deg");
669 double userLongitude = fgGetDouble("/position/longitude-deg");
670 double course, distance;
671 SGWayPoint current (pos.lon(),
674 SGWayPoint user ( userLongitude,
677 user.CourseAndDistance(current, &course, &distance);
678 if ((distance * SG_METER_TO_NM) > TRAFFICTOAIDIST)
681 //cerr << "done fp die out of range" << endl;
685 FGAirport * dep = trafficRef->getDepartureAirport();
686 FGAirport * arr = trafficRef->getArrivalAirport();
687 // At parking the beginning of the airport
693 //if ((dep->getId() == string("EHAM") || (arr->getId() == string("EHAM"))))
695 // cerr << trafficRef->getRegistration()
696 // << " Enroute from " << dep->getId()
697 // << " to " << arr->getId()
698 // << " just crossed " << prev->name
699 // << " Assigned rwy " << fp->getRunwayId()
700 // << " " << fp->getRunway() << endl;
702 //if ((dep->getId() == string("EHAM")) && (prev->name == "park2"))
704 // cerr << "Schiphol ground "
705 // << trafficRef->getCallSign();
706 // if (trafficRef->getHeavy())
708 // cerr << ", is type "
709 // << trafficRef->getAircraft()
710 // << " ready to go. IFR to "
711 // << arr->getId() <<endl;
713 if (prev->name == "park2")
715 dep->getDynamics()->releaseParking(fp->getGate());
717 // Some debug messages, specific to testing the Logical networks.
718 // if ((arr->getId() == string("EHAM")) && (prev->name == "Center"))
721 // cerr << "Schiphol ground "
722 // << trafficRef->getRegistration() << " "
723 // << trafficRef->getCallSign();
724 // if (trafficRef->getHeavy())
726 // cerr << ", arriving from " << dep->getName() ;
727 // cerr << " landed runway "
728 // << fp->getRunway()
729 // << " request taxi to gate "
730 // << arr->getDynamics()->getParkingName(fp->getGate())
733 if (prev->name == "END")
734 fp->setTime(trafficRef->getDepartureTime());
735 //cerr << "5" << endl;
739 //cerr << "Current waypoint" << curr->name << endl;
740 //cerr << "Next waypoint" << next->name << endl;
741 fp->setLeadDistance(speed, tgt_heading, curr, next);
743 //cerr << "5.1" << endl;
744 if (!(prev->on_ground)) { // only update the tgt altitude from flightplan if not on the ground
745 tgt_altitude = prev->altitude;
746 if (curr->crossat > -1000.0) {
747 //cerr << "5.1a" << endl;
749 tgt_vs = (curr->crossat - altitude)/
750 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
751 //cerr << "5.1b" << endl;
752 tgt_altitude = curr->crossat;
754 //cerr << "5.1c" << endl;
756 //cerr << "5.1d" << endl;
758 //cerr << "Setting target altitude : " <<tgt_altitude << endl;
761 //cerr << "6" << endl;
762 tgt_speed = prev->speed;
763 hdg_lock = alt_lock = true;
764 no_roll = prev->on_ground;
765 //cout << "Crossing waypoint: " << prev->name << endl;
766 //cout << " Target speed: " << tgt_speed << endl;
767 //cout << " Target altitude: " << tgt_altitude << endl;
768 //cout << " Target heading: " << tgt_heading << endl << endl;
771 double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
772 //cerr << "Bearing = " << calc_bearing << endl;
776 if (calc_bearing > 360)
779 if (finite(calc_bearing))
781 double hdg_error = calc_bearing - tgt_heading;
782 if (fabs(hdg_error) > 1.0) {
783 TurnTo( calc_bearing );
788 cerr << "calc_bearing is not a finite number : "
790 << "pos : " << pos.lat() << ", " << pos.lon()
791 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
792 cerr << "waypoint name " << curr->name;
795 double speed_diff = speed - prevSpeed;
796 // Update the lead distance calculation if speed has changed sufficiently
797 // to prevent spinning (hopefully);
798 if (fabs(speed_diff) > 10)
801 fp->setLeadDistance(speed, tgt_heading, curr, next);
804 //cerr << "Done Processing FlightPlan"<< endl;
805 } // if (dt count) else
808 bool FGAIAircraft::_getGearDown() const {
809 return ((props->getFloatValue("position/altitude-agl-ft") < 900.0)
810 && (props->getFloatValue("velocities/airspeed-kt")
811 < performance->land_speed*1.25));
815 void FGAIAircraft::loadNextLeg()
818 //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
821 //FGAIModelEntity entity;
822 //entity.m_class = "jet_transport";
823 //entity.path = modelPath.c_str();
824 //entity.flightplan = "none";
825 //entity.latitude = _getLatitude();
826 //entity.longitude = _getLongitude();
827 //entity.altitude = trafficRef->getCruiseAlt() * 100; // convert from FL to feet
828 //entity.speed = 450; // HACK ALERT
829 //entity.fp = new FGAIFlightPlan(&entity, courseToDest, i->getDepartureTime(), dep, arr);
831 if ((leg = fp->getLeg()) == 10)
837 //cerr << "Resetting leg : " << leg << endl;
842 //cerr << "Creating leg number : " << leg << endl;
843 FGAirport *dep = trafficRef->getDepartureAirport();
844 FGAirport *arr = trafficRef->getArrivalAirport();
848 //cerr << "Failed to get airport in AIAircraft::ProcessFlightplan()" << endl;
850 // cerr << "Departure " << dep->getId() << endl;
852 // cerr << "Arrival " << arr->getId() << endl;
856 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
857 //cerr << "Creating new leg using " << cruiseAlt << " as cruise altitude."<< endl;
862 cruiseAlt, //(trafficRef->getCruiseAlt() * 100), // convert from FL to feet
863 trafficRef->getSpeed(),
867 trafficRef->getRadius(),
868 trafficRef->getFlightType(),
871 //prev = fp->getPreviousWaypoint();
872 //curr = fp->getCurrentWaypoint();
873 //next = fp->getNextWaypoint();
874 //cerr << "25" << endl;
877 // //cerr << "Next waypoint" << next->name << endl;
878 // fp->setLeadDistance(speed, tgt_heading, curr, next);
880 //cerr << "25.1" << endl;
881 //if (curr->crossat > -1000.0) {
882 // //cerr << "25.1a" << endl;
883 // use_perf_vs = false;
885 // tgt_vs = (curr->crossat - altitude)/
886 // (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
887 // //cerr << "25.1b" << endl;
888 // tgt_altitude = curr->crossat;
890 // //cerr << "25.1c" << endl;
891 // use_perf_vs = true;
892 // //cerr << "25.1d" << endl;
893 // tgt_altitude = prev->altitude;
894 // //cerr << "Setting target altitude : " <<tgt_altitude << endl;
896 //cerr << "26" << endl;
897 //tgt_speed = prev->speed;
898 //hdg_lock = alt_lock = true;
899 //no_roll = prev->on_ground;
906 //entity.fp = new FGAIFlightPlan(&entity,
908 // trafficRef->getDepartureTime(),
909 // trafficRef->getDepartureAirport(),
910 // trafficRef->getArrivalAirport(),
914 //SetFlightPlan(entity.fp);
919 // Note: This code is copied from David Luff's AILocalTraffic
920 // Warning - ground elev determination is CPU intensive
921 // Either this function or the logic of how often it is called
922 // will almost certainly change.
924 void FGAIAircraft::getGroundElev(double dt) {
927 if (dt_elev_count < (3.0) + (rand() % 10)) //Update minimally every three secs, but add some randomness to prevent all IA objects doing this in synchrony
935 // It would be nice if we could set the correct tile center here in order to get a correct
936 // answer with one call to the function, but what I tried in the two commented-out lines
937 // below only intermittently worked, and I haven't quite groked why yet.
938 //SGBucket buck(pos.lon(), pos.lat());
939 //aip.getSGLocation()->set_tile_center(Point3D(buck.get_center_lon(), buck.get_center_lat(), 0.0));
941 // Only do the proper hitlist stuff if we are within visible range of the viewer.
943 double visibility_meters = fgGetDouble("/environment/visibility-m");
946 FGViewer* vw = globals->get_current_view();
951 //Point3D currView(vw->getLongitude_deg(),
952 // vw->getLatitude_deg(), 0.0);
953 SGWayPoint current (pos.lon(),
956 SGWayPoint view ( vw->getLongitude_deg(),
957 vw->getLatitude_deg(),
959 view.CourseAndDistance(current, &course, &distance);
960 if(distance > visibility_meters) {
961 //aip.getSGLocation()->set_cur_elev_m(aptElev);
965 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
966 double range = 500.0;
967 if (!globals->get_tile_mgr()->scenery_available(pos.lat(), pos.lon(), range))
969 // Try to shedule tiles for that position.
970 globals->get_tile_mgr()->update( aip.getSGLocation(), range );
973 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
975 if (globals->get_scenery()->get_elevation_m(pos.lat(), pos.lon(),
977 tgt_altitude = alt * SG_METER_TO_FEET;
978 //cerr << "Target altitude : " << tgt_altitude << endl;
979 // if (globals->get_scenery()->get_elevation_m(pos.lat(), pos.lon(),
981 // tgt_altitude = alt * SG_METER_TO_FEET;
982 //cerr << "Target altitude : " << tgt_altitude << endl;
986 void FGAIAircraft::setCallSign(const string& s) {
990 void FGAIAircraft::setTACANChannelID(const string& id) {
991 TACAN_channel_id = id;
994 void FGAIAircraft::doGroundAltitude()
996 if (fabs(altitude - (tgt_altitude+groundOffset)) > 1000.0)
997 altitude = (tgt_altitude + groundOffset);
999 altitude += 0.1 * ((tgt_altitude+groundOffset) - altitude);